1492 lines · 34.1 KB · Go
RawDownload
// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file. package gob import (	"bytes"	"errors"	"flag"	"math"	"math/rand"	"reflect"	"strings"	"testing"	"time") var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow") // Guarantee encoding format by comparing some encodings to hand-written valuestype EncodeT struct {	x uint64	b []byte} var encodeT = []EncodeT{	{0x00, []byte{0x00}},	{0x0F, []byte{0x0F}},	{0xFF, []byte{0xFF, 0xFF}},	{0xFFFF, []byte{0xFE, 0xFF, 0xFF}},	{0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}},	{0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}},	{0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},	{0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},	{0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},	{0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},	{0x1111, []byte{0xFE, 0x11, 0x11}},	{0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},	{0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}},	{1 << 63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},} // testError is meant to be used as a deferred function to turn a panic(gobError) into a// plain test.Error call.func testError(t *testing.T) {	if e := recover(); e != nil {		t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error.	}	return} func newDecBuffer(data []byte) *decBuffer {	return &decBuffer{		data: data,	}} // Test basic encode/decode routines for unsigned integersfunc TestUintCodec(t *testing.T) {	defer testError(t)	b := new(encBuffer)	encState := newEncoderState(b)	for _, tt := range encodeT {		b.Reset()		encState.encodeUint(tt.x)		if !bytes.Equal(tt.b, b.Bytes()) {			t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())		}	}	for u := uint64(0); ; u = (u + 1) * 7 {		b.Reset()		encState.encodeUint(u)		decState := newDecodeState(newDecBuffer(b.Bytes()))		v := decState.decodeUint()		if u != v {			t.Errorf("Encode/Decode: sent %#x received %#x", u, v)		}		if u&(1<<63) != 0 {			break		}	}} func verifyInt(i int64, t *testing.T) {	defer testError(t)	var b = new(encBuffer)	encState := newEncoderState(b)	encState.encodeInt(i)	decState := newDecodeState(newDecBuffer(b.Bytes()))	j := decState.decodeInt()	if i != j {		t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j))	}} // Test basic encode/decode routines for signed integersfunc TestIntCodec(t *testing.T) {	for u := uint64(0); ; u = (u + 1) * 7 {		// Do positive and negative values		i := int64(u)		verifyInt(i, t)		verifyInt(-i, t)		verifyInt(^i, t)		if u&(1<<63) != 0 {			break		}	}	verifyInt(-1<<63, t) // a tricky case} // The result of encoding a true boolean with field number 7var boolResult = []byte{0x07, 0x01} // The result of encoding a number 17 with field number 7var signedResult = []byte{0x07, 2 * 17}var unsignedResult = []byte{0x07, 17}var floatResult = []byte{0x07, 0xFE, 0x31, 0x40} // The result of encoding a number 17+19i with field number 7var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40} // The result of encoding "hello" with field number 7var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'} func newDecodeState(buf *decBuffer) *decoderState {	d := new(decoderState)	d.b = buf	return d} func newEncoderState(b *encBuffer) *encoderState {	b.Reset()	state := &encoderState{enc: nil, b: b}	state.fieldnum = -1	return state} // Test instruction execution for encoding.// Do not run the machine yet; instead do individual instructions crafted by hand.func TestScalarEncInstructions(t *testing.T) {	var b = new(encBuffer) 	// bool	{		var data bool = true		instr := &encInstr{encBool, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(boolResult, b.Bytes()) {			t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes())		}	} 	// int	{		b.Reset()		var data int = 17		instr := &encInstr{encInt, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(signedResult, b.Bytes()) {			t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes())		}	} 	// uint	{		b.Reset()		var data uint = 17		instr := &encInstr{encUint, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(unsignedResult, b.Bytes()) {			t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes())		}	} 	// int8	{		b.Reset()		var data int8 = 17		instr := &encInstr{encInt, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(signedResult, b.Bytes()) {			t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes())		}	} 	// uint8	{		b.Reset()		var data uint8 = 17		instr := &encInstr{encUint, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(unsignedResult, b.Bytes()) {			t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes())		}	} 	// int16	{		b.Reset()		var data int16 = 17		instr := &encInstr{encInt, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(signedResult, b.Bytes()) {			t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes())		}	} 	// uint16	{		b.Reset()		var data uint16 = 17		instr := &encInstr{encUint, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(unsignedResult, b.Bytes()) {			t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes())		}	} 	// int32	{		b.Reset()		var data int32 = 17		instr := &encInstr{encInt, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(signedResult, b.Bytes()) {			t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes())		}	} 	// uint32	{		b.Reset()		var data uint32 = 17		instr := &encInstr{encUint, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(unsignedResult, b.Bytes()) {			t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes())		}	} 	// int64	{		b.Reset()		var data int64 = 17		instr := &encInstr{encInt, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(signedResult, b.Bytes()) {			t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes())		}	} 	// uint64	{		b.Reset()		var data uint64 = 17		instr := &encInstr{encUint, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(unsignedResult, b.Bytes()) {			t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes())		}	} 	// float32	{		b.Reset()		var data float32 = 17		instr := &encInstr{encFloat, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(floatResult, b.Bytes()) {			t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes())		}	} 	// float64	{		b.Reset()		var data float64 = 17		instr := &encInstr{encFloat, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(floatResult, b.Bytes()) {			t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes())		}	} 	// bytes == []uint8	{		b.Reset()		data := []byte("hello")		instr := &encInstr{encUint8Array, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(bytesResult, b.Bytes()) {			t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes())		}	} 	// string	{		b.Reset()		var data string = "hello"		instr := &encInstr{encString, 6, nil, 0}		state := newEncoderState(b)		instr.op(instr, state, reflect.ValueOf(data))		if !bytes.Equal(bytesResult, b.Bytes()) {			t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes())		}	}} func execDec(typ string, instr *decInstr, state *decoderState, t *testing.T, value reflect.Value) {	defer testError(t)	v := int(state.decodeUint())	if v+state.fieldnum != 6 {		t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)	}	instr.op(instr, state, value.Elem())	state.fieldnum = 6} func newDecodeStateFromData(data []byte) *decoderState {	b := newDecBuffer(data)	state := newDecodeState(b)	state.fieldnum = -1	return state} // Test instruction execution for decoding.// Do not run the machine yet; instead do individual instructions crafted by hand.func TestScalarDecInstructions(t *testing.T) {	ovfl := errors.New("overflow") 	// bool	{		var data bool		instr := &decInstr{decBool, 6, nil, ovfl}		state := newDecodeStateFromData(boolResult)		execDec("bool", instr, state, t, reflect.ValueOf(&data))		if data != true {			t.Errorf("bool a = %v not true", data)		}	}	// int	{		var data int		instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl}		state := newDecodeStateFromData(signedResult)		execDec("int", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("int a = %v not 17", data)		}	} 	// uint	{		var data uint		instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uint", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uint a = %v not 17", data)		}	} 	// int8	{		var data int8		instr := &decInstr{decInt8, 6, nil, ovfl}		state := newDecodeStateFromData(signedResult)		execDec("int8", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("int8 a = %v not 17", data)		}	} 	// uint8	{		var data uint8		instr := &decInstr{decUint8, 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uint8", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uint8 a = %v not 17", data)		}	} 	// int16	{		var data int16		instr := &decInstr{decInt16, 6, nil, ovfl}		state := newDecodeStateFromData(signedResult)		execDec("int16", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("int16 a = %v not 17", data)		}	} 	// uint16	{		var data uint16		instr := &decInstr{decUint16, 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uint16", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uint16 a = %v not 17", data)		}	} 	// int32	{		var data int32		instr := &decInstr{decInt32, 6, nil, ovfl}		state := newDecodeStateFromData(signedResult)		execDec("int32", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("int32 a = %v not 17", data)		}	} 	// uint32	{		var data uint32		instr := &decInstr{decUint32, 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uint32", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uint32 a = %v not 17", data)		}	} 	// uintptr	{		var data uintptr		instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uintptr", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uintptr a = %v not 17", data)		}	} 	// int64	{		var data int64		instr := &decInstr{decInt64, 6, nil, ovfl}		state := newDecodeStateFromData(signedResult)		execDec("int64", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("int64 a = %v not 17", data)		}	} 	// uint64	{		var data uint64		instr := &decInstr{decUint64, 6, nil, ovfl}		state := newDecodeStateFromData(unsignedResult)		execDec("uint64", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("uint64 a = %v not 17", data)		}	} 	// float32	{		var data float32		instr := &decInstr{decFloat32, 6, nil, ovfl}		state := newDecodeStateFromData(floatResult)		execDec("float32", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("float32 a = %v not 17", data)		}	} 	// float64	{		var data float64		instr := &decInstr{decFloat64, 6, nil, ovfl}		state := newDecodeStateFromData(floatResult)		execDec("float64", instr, state, t, reflect.ValueOf(&data))		if data != 17 {			t.Errorf("float64 a = %v not 17", data)		}	} 	// complex64	{

Lines 1–500 of 1492.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t