Source file src/encoding/gob/encoder_test.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gob
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"io"
    12  	"math"
    13  	"reflect"
    14  	"sort"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  // Test basic operations in a safe manner.
    20  func TestBasicEncoderDecoder(t *testing.T) {
    21  	var values = []any{
    22  		true,
    23  		int(123),
    24  		int8(123),
    25  		int16(-12345),
    26  		int32(123456),
    27  		int64(-1234567),
    28  		uint(123),
    29  		uint8(123),
    30  		uint16(12345),
    31  		uint32(123456),
    32  		uint64(1234567),
    33  		uintptr(12345678),
    34  		float32(1.2345),
    35  		float64(1.2345678),
    36  		complex64(1.2345 + 2.3456i),
    37  		complex128(1.2345678 + 2.3456789i),
    38  		[]byte("hello"),
    39  		string("hello"),
    40  	}
    41  	for _, value := range values {
    42  		b := new(bytes.Buffer)
    43  		enc := NewEncoder(b)
    44  		err := enc.Encode(value)
    45  		if err != nil {
    46  			t.Error("encoder fail:", err)
    47  		}
    48  		dec := NewDecoder(b)
    49  		result := reflect.New(reflect.TypeOf(value))
    50  		err = dec.Decode(result.Interface())
    51  		if err != nil {
    52  			t.Fatalf("error decoding %T: %v:", reflect.TypeOf(value), err)
    53  		}
    54  		if !reflect.DeepEqual(value, result.Elem().Interface()) {
    55  			t.Fatalf("%T: expected %v got %v", value, value, result.Elem().Interface())
    56  		}
    57  	}
    58  }
    59  
    60  func TestEncodeIntSlice(t *testing.T) {
    61  
    62  	s8 := []int8{1, 5, 12, 22, 35, 51, 70, 92, 117}
    63  	s16 := []int16{145, 176, 210, 247, 287, 330, 376, 425, 477}
    64  	s32 := []int32{532, 590, 651, 715, 782, 852, 925, 1001, 1080}
    65  	s64 := []int64{1162, 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926}
    66  
    67  	t.Run("int8", func(t *testing.T) {
    68  		var sink bytes.Buffer
    69  		enc := NewEncoder(&sink)
    70  		enc.Encode(s8)
    71  
    72  		dec := NewDecoder(&sink)
    73  		res := make([]int8, 9)
    74  		dec.Decode(&res)
    75  
    76  		if !reflect.DeepEqual(s8, res) {
    77  			t.Fatalf("EncodeIntSlice: expected %v, got %v", s8, res)
    78  		}
    79  	})
    80  
    81  	t.Run("int16", func(t *testing.T) {
    82  		var sink bytes.Buffer
    83  		enc := NewEncoder(&sink)
    84  		enc.Encode(s16)
    85  
    86  		dec := NewDecoder(&sink)
    87  		res := make([]int16, 9)
    88  		dec.Decode(&res)
    89  
    90  		if !reflect.DeepEqual(s16, res) {
    91  			t.Fatalf("EncodeIntSlice: expected %v, got %v", s16, res)
    92  		}
    93  	})
    94  
    95  	t.Run("int32", func(t *testing.T) {
    96  		var sink bytes.Buffer
    97  		enc := NewEncoder(&sink)
    98  		enc.Encode(s32)
    99  
   100  		dec := NewDecoder(&sink)
   101  		res := make([]int32, 9)
   102  		dec.Decode(&res)
   103  
   104  		if !reflect.DeepEqual(s32, res) {
   105  			t.Fatalf("EncodeIntSlice: expected %v, got %v", s32, res)
   106  		}
   107  	})
   108  
   109  	t.Run("int64", func(t *testing.T) {
   110  		var sink bytes.Buffer
   111  		enc := NewEncoder(&sink)
   112  		enc.Encode(s64)
   113  
   114  		dec := NewDecoder(&sink)
   115  		res := make([]int64, 9)
   116  		dec.Decode(&res)
   117  
   118  		if !reflect.DeepEqual(s64, res) {
   119  			t.Fatalf("EncodeIntSlice: expected %v, got %v", s64, res)
   120  		}
   121  	})
   122  
   123  }
   124  
   125  type ET0 struct {
   126  	A int
   127  	B string
   128  }
   129  
   130  type ET2 struct {
   131  	X string
   132  }
   133  
   134  type ET1 struct {
   135  	A    int
   136  	Et2  *ET2
   137  	Next *ET1
   138  }
   139  
   140  // Like ET1 but with a different name for a field
   141  type ET3 struct {
   142  	A             int
   143  	Et2           *ET2
   144  	DifferentNext *ET1
   145  }
   146  
   147  // Like ET1 but with a different type for a field
   148  type ET4 struct {
   149  	A    int
   150  	Et2  float64
   151  	Next int
   152  }
   153  
   154  func TestEncoderDecoder(t *testing.T) {
   155  	b := new(bytes.Buffer)
   156  	enc := NewEncoder(b)
   157  	et0 := new(ET0)
   158  	et0.A = 7
   159  	et0.B = "gobs of fun"
   160  	err := enc.Encode(et0)
   161  	if err != nil {
   162  		t.Error("encoder fail:", err)
   163  	}
   164  	//fmt.Printf("% x %q\n", b, b)
   165  	//Debug(b)
   166  	dec := NewDecoder(b)
   167  	newEt0 := new(ET0)
   168  	err = dec.Decode(newEt0)
   169  	if err != nil {
   170  		t.Fatal("error decoding ET0:", err)
   171  	}
   172  
   173  	if !reflect.DeepEqual(et0, newEt0) {
   174  		t.Fatalf("invalid data for et0: expected %+v; got %+v", *et0, *newEt0)
   175  	}
   176  	if b.Len() != 0 {
   177  		t.Error("not at eof;", b.Len(), "bytes left")
   178  	}
   179  	//	t.FailNow()
   180  
   181  	b = new(bytes.Buffer)
   182  	enc = NewEncoder(b)
   183  	et1 := new(ET1)
   184  	et1.A = 7
   185  	et1.Et2 = new(ET2)
   186  	err = enc.Encode(et1)
   187  	if err != nil {
   188  		t.Error("encoder fail:", err)
   189  	}
   190  	dec = NewDecoder(b)
   191  	newEt1 := new(ET1)
   192  	err = dec.Decode(newEt1)
   193  	if err != nil {
   194  		t.Fatal("error decoding ET1:", err)
   195  	}
   196  
   197  	if !reflect.DeepEqual(et1, newEt1) {
   198  		t.Fatalf("invalid data for et1: expected %+v; got %+v", *et1, *newEt1)
   199  	}
   200  	if b.Len() != 0 {
   201  		t.Error("not at eof;", b.Len(), "bytes left")
   202  	}
   203  
   204  	enc.Encode(et1)
   205  	newEt1 = new(ET1)
   206  	err = dec.Decode(newEt1)
   207  	if err != nil {
   208  		t.Fatal("round 2: error decoding ET1:", err)
   209  	}
   210  	if !reflect.DeepEqual(et1, newEt1) {
   211  		t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v", *et1, *newEt1)
   212  	}
   213  	if b.Len() != 0 {
   214  		t.Error("round 2: not at eof;", b.Len(), "bytes left")
   215  	}
   216  
   217  	// Now test with a running encoder/decoder pair that we recognize a type mismatch.
   218  	err = enc.Encode(et1)
   219  	if err != nil {
   220  		t.Error("round 3: encoder fail:", err)
   221  	}
   222  	newEt2 := new(ET2)
   223  	err = dec.Decode(newEt2)
   224  	if err == nil {
   225  		t.Fatal("round 3: expected `bad type' error decoding ET2")
   226  	}
   227  }
   228  
   229  // Run one value through the encoder/decoder, but use the wrong type.
   230  // Input is always an ET1; we compare it to whatever is under 'e'.
   231  func badTypeCheck(e any, shouldFail bool, msg string, t *testing.T) {
   232  	b := new(bytes.Buffer)
   233  	enc := NewEncoder(b)
   234  	et1 := new(ET1)
   235  	et1.A = 7
   236  	et1.Et2 = new(ET2)
   237  	err := enc.Encode(et1)
   238  	if err != nil {
   239  		t.Error("encoder fail:", err)
   240  	}
   241  	dec := NewDecoder(b)
   242  	err = dec.Decode(e)
   243  	if shouldFail && err == nil {
   244  		t.Error("expected error for", msg)
   245  	}
   246  	if !shouldFail && err != nil {
   247  		t.Error("unexpected error for", msg, err)
   248  	}
   249  }
   250  
   251  // Test that we recognize a bad type the first time.
   252  func TestWrongTypeDecoder(t *testing.T) {
   253  	badTypeCheck(new(ET2), true, "no fields in common", t)
   254  	badTypeCheck(new(ET3), false, "different name of field", t)
   255  	badTypeCheck(new(ET4), true, "different type of field", t)
   256  }
   257  
   258  // Types not supported at top level by the Encoder.
   259  var unsupportedValues = []any{
   260  	make(chan int),
   261  	func(a int) bool { return true },
   262  }
   263  
   264  func TestUnsupported(t *testing.T) {
   265  	var b bytes.Buffer
   266  	enc := NewEncoder(&b)
   267  	for _, v := range unsupportedValues {
   268  		err := enc.Encode(v)
   269  		if err == nil {
   270  			t.Errorf("expected error for %T; got none", v)
   271  		}
   272  	}
   273  }
   274  
   275  func encAndDec(in, out any) error {
   276  	b := new(bytes.Buffer)
   277  	enc := NewEncoder(b)
   278  	err := enc.Encode(in)
   279  	if err != nil {
   280  		return err
   281  	}
   282  	dec := NewDecoder(b)
   283  	err = dec.Decode(out)
   284  	if err != nil {
   285  		return err
   286  	}
   287  	return nil
   288  }
   289  
   290  func TestTypeToPtrType(t *testing.T) {
   291  	// Encode a T, decode a *T
   292  	type Type0 struct {
   293  		A int
   294  	}
   295  	t0 := Type0{7}
   296  	t0p := new(Type0)
   297  	if err := encAndDec(t0, t0p); err != nil {
   298  		t.Error(err)
   299  	}
   300  }
   301  
   302  func TestPtrTypeToType(t *testing.T) {
   303  	// Encode a *T, decode a T
   304  	type Type1 struct {
   305  		A uint
   306  	}
   307  	t1p := &Type1{17}
   308  	var t1 Type1
   309  	if err := encAndDec(t1, t1p); err != nil {
   310  		t.Error(err)
   311  	}
   312  }
   313  
   314  func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
   315  	type Type2 struct {
   316  		A ****float64
   317  	}
   318  	t2 := Type2{}
   319  	t2.A = new(***float64)
   320  	*t2.A = new(**float64)
   321  	**t2.A = new(*float64)
   322  	***t2.A = new(float64)
   323  	****t2.A = 27.4
   324  	t2pppp := new(***Type2)
   325  	if err := encAndDec(t2, t2pppp); err != nil {
   326  		t.Fatal(err)
   327  	}
   328  	if ****(****t2pppp).A != ****t2.A {
   329  		t.Errorf("wrong value after decode: %g not %g", ****(****t2pppp).A, ****t2.A)
   330  	}
   331  }
   332  
   333  func TestSlice(t *testing.T) {
   334  	type Type3 struct {
   335  		A []string
   336  	}
   337  	t3p := &Type3{[]string{"hello", "world"}}
   338  	var t3 Type3
   339  	if err := encAndDec(t3, t3p); err != nil {
   340  		t.Error(err)
   341  	}
   342  }
   343  
   344  func TestValueError(t *testing.T) {
   345  	// Encode a *T, decode a T
   346  	type Type4 struct {
   347  		A int
   348  	}
   349  	t4p := &Type4{3}
   350  	var t4 Type4 // note: not a pointer.
   351  	if err := encAndDec(t4p, t4); err == nil || !strings.Contains(err.Error(), "pointer") {
   352  		t.Error("expected error about pointer; got", err)
   353  	}
   354  }
   355  
   356  func TestArray(t *testing.T) {
   357  	type Type5 struct {
   358  		A [3]string
   359  		B [3]byte
   360  	}
   361  	type Type6 struct {
   362  		A [2]string // can't hold t5.a
   363  	}
   364  	t5 := Type5{[3]string{"hello", ",", "world"}, [3]byte{1, 2, 3}}
   365  	var t5p Type5
   366  	if err := encAndDec(t5, &t5p); err != nil {
   367  		t.Error(err)
   368  	}
   369  	var t6 Type6
   370  	if err := encAndDec(t5, &t6); err == nil {
   371  		t.Error("should fail with mismatched array sizes")
   372  	}
   373  }
   374  
   375  func TestRecursiveMapType(t *testing.T) {
   376  	type recursiveMap map[string]recursiveMap
   377  	r1 := recursiveMap{"A": recursiveMap{"B": nil, "C": nil}, "D": nil}
   378  	r2 := make(recursiveMap)
   379  	if err := encAndDec(r1, &r2); err != nil {
   380  		t.Error(err)
   381  	}
   382  }
   383  
   384  func TestRecursiveSliceType(t *testing.T) {
   385  	type recursiveSlice []recursiveSlice
   386  	r1 := recursiveSlice{0: recursiveSlice{0: nil}, 1: nil}
   387  	r2 := make(recursiveSlice, 0)
   388  	if err := encAndDec(r1, &r2); err != nil {
   389  		t.Error(err)
   390  	}
   391  }
   392  
   393  // Regression test for bug: must send zero values inside arrays
   394  func TestDefaultsInArray(t *testing.T) {
   395  	type Type7 struct {
   396  		B []bool
   397  		I []int
   398  		S []string
   399  		F []float64
   400  	}
   401  	t7 := Type7{
   402  		[]bool{false, false, true},
   403  		[]int{0, 0, 1},
   404  		[]string{"hi", "", "there"},
   405  		[]float64{0, 0, 1},
   406  	}
   407  	var t7p Type7
   408  	if err := encAndDec(t7, &t7p); err != nil {
   409  		t.Error(err)
   410  	}
   411  }
   412  
   413  var testInt int
   414  var testFloat32 float32
   415  var testString string
   416  var testSlice []string
   417  var testMap map[string]int
   418  var testArray [7]int
   419  
   420  type SingleTest struct {
   421  	in  any
   422  	out any
   423  	err string
   424  }
   425  
   426  var singleTests = []SingleTest{
   427  	{17, &testInt, ""},
   428  	{float32(17.5), &testFloat32, ""},
   429  	{"bike shed", &testString, ""},
   430  	{[]string{"bike", "shed", "paint", "color"}, &testSlice, ""},
   431  	{map[string]int{"seven": 7, "twelve": 12}, &testMap, ""},
   432  	{[7]int{4, 55, 0, 0, 0, 0, 0}, &testArray, ""}, // case that once triggered a bug
   433  	{[7]int{4, 55, 1, 44, 22, 66, 1234}, &testArray, ""},
   434  
   435  	// Decode errors
   436  	{172, &testFloat32, "type"},
   437  }
   438  
   439  func TestSingletons(t *testing.T) {
   440  	b := new(bytes.Buffer)
   441  	enc := NewEncoder(b)
   442  	dec := NewDecoder(b)
   443  	for _, test := range singleTests {
   444  		b.Reset()
   445  		err := enc.Encode(test.in)
   446  		if err != nil {
   447  			t.Errorf("error encoding %v: %s", test.in, err)
   448  			continue
   449  		}
   450  		err = dec.Decode(test.out)
   451  		switch {
   452  		case err != nil && test.err == "":
   453  			t.Errorf("error decoding %v: %s", test.in, err)
   454  			continue
   455  		case err == nil && test.err != "":
   456  			t.Errorf("expected error decoding %v: %s", test.in, test.err)
   457  			continue
   458  		case err != nil && test.err != "":
   459  			if !strings.Contains(err.Error(), test.err) {
   460  				t.Errorf("wrong error decoding %v: wanted %s, got %v", test.in, test.err, err)
   461  			}
   462  			continue
   463  		}
   464  		// Get rid of the pointer in the rhs
   465  		val := reflect.ValueOf(test.out).Elem().Interface()
   466  		if !reflect.DeepEqual(test.in, val) {
   467  			t.Errorf("decoding singleton: expected %v got %v", test.in, val)
   468  		}
   469  	}
   470  }
   471  
   472  func TestStructNonStruct(t *testing.T) {
   473  	type Struct struct {
   474  		A string
   475  	}
   476  	type NonStruct string
   477  	s := Struct{"hello"}
   478  	var sp Struct
   479  	if err := encAndDec(s, &sp); err != nil {
   480  		t.Error(err)
   481  	}
   482  	var ns NonStruct
   483  	if err := encAndDec(s, &ns); err == nil {
   484  		t.Error("should get error for struct/non-struct")
   485  	} else if !strings.Contains(err.Error(), "type") {
   486  		t.Error("for struct/non-struct expected type error; got", err)
   487  	}
   488  	// Now try the other way
   489  	var nsp NonStruct
   490  	if err := encAndDec(ns, &nsp); err != nil {
   491  		t.Error(err)
   492  	}
   493  	if err := encAndDec(ns, &s); err == nil {
   494  		t.Error("should get error for non-struct/struct")
   495  	} else if !strings.Contains(err.Error(), "type") {
   496  		t.Error("for non-struct/struct expected type error; got", err)
   497  	}
   498  }
   499  
   500  type interfaceIndirectTestI interface {
   501  	F() bool
   502  }
   503  
   504  type interfaceIndirectTestT struct{}
   505  
   506  func (this *interfaceIndirectTestT) F() bool {
   507  	return true
   508  }
   509  
   510  // A version of a bug reported on golang-nuts. Also tests top-level
   511  // slice of interfaces. The issue was registering *T caused T to be
   512  // stored as the concrete type.
   513  func TestInterfaceIndirect(t *testing.T) {
   514  	Register(&interfaceIndirectTestT{})
   515  	b := new(bytes.Buffer)
   516  	w := []interfaceIndirectTestI{&interfaceIndirectTestT{}}
   517  	err := NewEncoder(b).Encode(w)
   518  	if err != nil {
   519  		t.Fatal("encode error:", err)
   520  	}
   521  
   522  	var r []interfaceIndirectTestI
   523  	err = NewDecoder(b).Decode(&r)
   524  	if err != nil {
   525  		t.Fatal("decode error:", err)
   526  	}
   527  }
   528  
   529  // Now follow various tests that decode into things that can't represent the
   530  // encoded value, all of which should be legal.
   531  
   532  // Also, when the ignored object contains an interface value, it may define
   533  // types. Make sure that skipping the value still defines the types by using
   534  // the encoder/decoder pair to send a value afterwards. If an interface
   535  // is sent, its type in the test is always NewType0, so this checks that the
   536  // encoder and decoder don't skew with respect to type definitions.
   537  
   538  type Struct0 struct {
   539  	I any
   540  }
   541  
   542  type NewType0 struct {
   543  	S string
   544  }
   545  
   546  type ignoreTest struct {
   547  	in, out any
   548  }
   549  
   550  var ignoreTests = []ignoreTest{
   551  	// Decode normal struct into an empty struct
   552  	{&struct{ A int }{23}, &struct{}{}},
   553  	// Decode normal struct into a nil.
   554  	{&struct{ A int }{23}, nil},
   555  	// Decode singleton string into a nil.
   556  	{"hello, world", nil},
   557  	// Decode singleton slice into a nil.
   558  	{[]int{1, 2, 3, 4}, nil},
   559  	// Decode struct containing an interface into a nil.
   560  	{&Struct0{&NewType0{"value0"}}, nil},
   561  	// Decode singleton slice of interfaces into a nil.
   562  	{[]any{"hi", &NewType0{"value1"}, 23}, nil},
   563  }
   564  
   565  func TestDecodeIntoNothing(t *testing.T) {
   566  	Register(new(NewType0))
   567  	for i, test := range ignoreTests {
   568  		b := new(bytes.Buffer)
   569  		enc := NewEncoder(b)
   570  		err := enc.Encode(test.in)
   571  		if err != nil {
   572  			t.Errorf("%d: encode error %s:", i, err)
   573  			continue
   574  		}
   575  		dec := NewDecoder(b)
   576  		err = dec.Decode(test.out)
   577  		if err != nil {
   578  			t.Errorf("%d: decode error: %s", i, err)
   579  			continue
   580  		}
   581  		// Now see if the encoder and decoder are in a consistent state.
   582  		str := fmt.Sprintf("Value %d", i)
   583  		err = enc.Encode(&NewType0{str})
   584  		if err != nil {
   585  			t.Fatalf("%d: NewType0 encode error: %s", i, err)
   586  		}
   587  		ns := new(NewType0)
   588  		err = dec.Decode(ns)
   589  		if err != nil {
   590  			t.Fatalf("%d: NewType0 decode error: %s", i, err)
   591  		}
   592  		if ns.S != str {
   593  			t.Fatalf("%d: expected %q got %q", i, str, ns.S)
   594  		}
   595  	}
   596  }
   597  
   598  func TestIgnoreRecursiveType(t *testing.T) {
   599  	// It's hard to build a self-contained test for this because
   600  	// we can't build compatible types in one package with
   601  	// different items so something is ignored. Here is
   602  	// some data that represents, according to debug.go:
   603  	// type definition {
   604  	//	slice "recursiveSlice" id=106
   605  	//		elem id=106
   606  	// }
   607  	data := []byte{
   608  		0x1d, 0xff, 0xd3, 0x02, 0x01, 0x01, 0x0e, 0x72,
   609  		0x65, 0x63, 0x75, 0x72, 0x73, 0x69, 0x76, 0x65,
   610  		0x53, 0x6c, 0x69, 0x63, 0x65, 0x01, 0xff, 0xd4,
   611  		0x00, 0x01, 0xff, 0xd4, 0x00, 0x00, 0x07, 0xff,
   612  		0xd4, 0x00, 0x02, 0x01, 0x00, 0x00,
   613  	}
   614  	dec := NewDecoder(bytes.NewReader(data))
   615  	// Issue 10415: This caused infinite recursion.
   616  	err := dec.Decode(nil)
   617  	if err != nil {
   618  		t.Fatal(err)
   619  	}
   620  }
   621  
   622  // Another bug from golang-nuts, involving nested interfaces.
   623  type Bug0Outer struct {
   624  	Bug0Field any
   625  }
   626  
   627  type Bug0Inner struct {
   628  	A int
   629  }
   630  
   631  func TestNestedInterfaces(t *testing.T) {
   632  	var buf bytes.Buffer
   633  	e := NewEncoder(&buf)
   634  	d := NewDecoder(&buf)
   635  	Register(new(Bug0Outer))
   636  	Register(new(Bug0Inner))
   637  	f := &Bug0Outer{&Bug0Outer{&Bug0Inner{7}}}
   638  	var v any = f
   639  	err := e.Encode(&v)
   640  	if err != nil {
   641  		t.Fatal("Encode:", err)
   642  	}
   643  	err = d.Decode(&v)
   644  	if err != nil {
   645  		t.Fatal("Decode:", err)
   646  	}
   647  	// Make sure it decoded correctly.
   648  	outer1, ok := v.(*Bug0Outer)
   649  	if !ok {
   650  		t.Fatalf("v not Bug0Outer: %T", v)
   651  	}
   652  	outer2, ok := outer1.Bug0Field.(*Bug0Outer)
   653  	if !ok {
   654  		t.Fatalf("v.Bug0Field not Bug0Outer: %T", outer1.Bug0Field)
   655  	}
   656  	inner, ok := outer2.Bug0Field.(*Bug0Inner)
   657  	if !ok {
   658  		t.Fatalf("v.Bug0Field.Bug0Field not Bug0Inner: %T", outer2.Bug0Field)
   659  	}
   660  	if inner.A != 7 {
   661  		t.Fatalf("final value %d; expected %d", inner.A, 7)
   662  	}
   663  }
   664  
   665  // The bugs keep coming. We forgot to send map subtypes before the map.
   666  
   667  type Bug1Elem struct {
   668  	Name string
   669  	Id   int
   670  }
   671  
   672  type Bug1StructMap map[string]Bug1Elem
   673  
   674  func TestMapBug1(t *testing.T) {
   675  	in := make(Bug1StructMap)
   676  	in["val1"] = Bug1Elem{"elem1", 1}
   677  	in["val2"] = Bug1Elem{"elem2", 2}
   678  
   679  	b := new(bytes.Buffer)
   680  	enc := NewEncoder(b)
   681  	err := enc.Encode(in)
   682  	if err != nil {
   683  		t.Fatal("encode:", err)
   684  	}
   685  	dec := NewDecoder(b)
   686  	out := make(Bug1StructMap)
   687  	err = dec.Decode(&out)
   688  	if err != nil {
   689  		t.Fatal("decode:", err)
   690  	}
   691  	if !reflect.DeepEqual(in, out) {
   692  		t.Errorf("mismatch: %v %v", in, out)
   693  	}
   694  }
   695  
   696  func TestGobMapInterfaceEncode(t *testing.T) {
   697  	m := map[string]any{
   698  		"up": uintptr(0),
   699  		"i0": []int{-1},
   700  		"i1": []int8{-1},
   701  		"i2": []int16{-1},
   702  		"i3": []int32{-1},
   703  		"i4": []int64{-1},
   704  		"u0": []uint{1},
   705  		"u1": []uint8{1},
   706  		"u2": []uint16{1},
   707  		"u3": []uint32{1},
   708  		"u4": []uint64{1},
   709  		"f0": []float32{1},
   710  		"f1": []float64{1},
   711  		"c0": []complex64{complex(2, -2)},
   712  		"c1": []complex128{complex(2, float64(-2))},
   713  		"us": []uintptr{0},
   714  		"bo": []bool{false},
   715  		"st": []string{"s"},
   716  	}
   717  	enc := NewEncoder(new(bytes.Buffer))
   718  	err := enc.Encode(m)
   719  	if err != nil {
   720  		t.Errorf("encode map: %s", err)
   721  	}
   722  }
   723  
   724  func TestSliceReusesMemory(t *testing.T) {
   725  	buf := new(bytes.Buffer)
   726  	// Bytes
   727  	{
   728  		x := []byte("abcd")
   729  		enc := NewEncoder(buf)
   730  		err := enc.Encode(x)
   731  		if err != nil {
   732  			t.Errorf("bytes: encode: %s", err)
   733  		}
   734  		// Decode into y, which is big enough.
   735  		y := []byte("ABCDE")
   736  		addr := &y[0]
   737  		dec := NewDecoder(buf)
   738  		err = dec.Decode(&y)
   739  		if err != nil {
   740  			t.Fatal("bytes: decode:", err)
   741  		}
   742  		if !bytes.Equal(x, y) {
   743  			t.Errorf("bytes: expected %q got %q\n", x, y)
   744  		}
   745  		if addr != &y[0] {
   746  			t.Errorf("bytes: unnecessary reallocation")
   747  		}
   748  	}
   749  	// general slice
   750  	{
   751  		x := []rune("abcd")
   752  		enc := NewEncoder(buf)
   753  		err := enc.Encode(x)
   754  		if err != nil {
   755  			t.Errorf("ints: encode: %s", err)
   756  		}
   757  		// Decode into y, which is big enough.
   758  		y := []rune("ABCDE")
   759  		addr := &y[0]
   760  		dec := NewDecoder(buf)
   761  		err = dec.Decode(&y)
   762  		if err != nil {
   763  			t.Fatal("ints: decode:", err)
   764  		}
   765  		if !reflect.DeepEqual(x, y) {
   766  			t.Errorf("ints: expected %q got %q\n", x, y)
   767  		}
   768  		if addr != &y[0] {
   769  			t.Errorf("ints: unnecessary reallocation")
   770  		}
   771  	}
   772  }
   773  
   774  // Used to crash: negative count in recvMessage.
   775  func TestBadCount(t *testing.T) {
   776  	b := []byte{0xfb, 0xa5, 0x82, 0x2f, 0xca, 0x1}
   777  	if err := NewDecoder(bytes.NewReader(b)).Decode(nil); err == nil {
   778  		t.Error("expected error from bad count")
   779  	} else if err.Error() != errBadCount.Error() {
   780  		t.Error("expected bad count error; got", err)
   781  	}
   782  }
   783  
   784  // Verify that sequential Decoders built on a single input will
   785  // succeed if the input implements ReadByte and there is no
   786  // type information in the stream.
   787  func TestSequentialDecoder(t *testing.T) {
   788  	b := new(bytes.Buffer)
   789  	enc := NewEncoder(b)
   790  	const count = 10
   791  	for i := 0; i < count; i++ {
   792  		s := fmt.Sprintf("%d", i)
   793  		if err := enc.Encode(s); err != nil {
   794  			t.Error("encoder fail:", err)
   795  		}
   796  	}
   797  	for i := 0; i < count; i++ {
   798  		dec := NewDecoder(b)
   799  		var s string
   800  		if err := dec.Decode(&s); err != nil {
   801  			t.Fatal("decoder fail:", err)
   802  		}
   803  		if s != fmt.Sprintf("%d", i) {
   804  			t.Fatalf("decode expected %d got %s", i, s)
   805  		}
   806  	}
   807  }
   808  
   809  // Should be able to have unrepresentable fields (chan, func, *chan etc.); we just ignore them.
   810  type Bug2 struct {
   811  	A   int
   812  	C   chan int
   813  	CP  *chan int
   814  	F   func()
   815  	FPP **func()
   816  }
   817  
   818  func TestChanFuncIgnored(t *testing.T) {
   819  	c := make(chan int)
   820  	f := func() {}
   821  	fp := &f
   822  	b0 := Bug2{23, c, &c, f, &fp}
   823  	var buf bytes.Buffer
   824  	enc := NewEncoder(&buf)
   825  	if err := enc.Encode(b0); err != nil {
   826  		t.Fatal("error encoding:", err)
   827  	}
   828  	var b1 Bug2
   829  	err := NewDecoder(&buf).Decode(&b1)
   830  	if err != nil {
   831  		t.Fatal("decode:", err)
   832  	}
   833  	if b1.A != b0.A {
   834  		t.Fatalf("got %d want %d", b1.A, b0.A)
   835  	}
   836  	if b1.C != nil || b1.CP != nil || b1.F != nil || b1.FPP != nil {
   837  		t.Fatal("unexpected value for chan or func")
   838  	}
   839  }
   840  
   841  func TestSliceIncompatibility(t *testing.T) {
   842  	var in = []byte{1, 2, 3}
   843  	var out []int
   844  	if err := encAndDec(in, &out); err == nil {
   845  		t.Error("expected compatibility error")
   846  	}
   847  }
   848  
   849  // Mutually recursive slices of structs caused problems.
   850  type Bug3 struct {
   851  	Num      int
   852  	Children []*Bug3
   853  }
   854  
   855  func TestGobPtrSlices(t *testing.T) {
   856  	in := []*Bug3{
   857  		{1, nil},
   858  		{2, nil},
   859  	}
   860  	b := new(bytes.Buffer)
   861  	err := NewEncoder(b).Encode(&in)
   862  	if err != nil {
   863  		t.Fatal("encode:", err)
   864  	}
   865  
   866  	var out []*Bug3
   867  	err = NewDecoder(b).Decode(&out)
   868  	if err != nil {
   869  		t.Fatal("decode:", err)
   870  	}
   871  	if !reflect.DeepEqual(in, out) {
   872  		t.Fatalf("got %v; wanted %v", out, in)
   873  	}
   874  }
   875  
   876  // getDecEnginePtr cached engine for ut.base instead of ut.user so we passed
   877  // a *map and then tried to reuse its engine to decode the inner map.
   878  func TestPtrToMapOfMap(t *testing.T) {
   879  	Register(make(map[string]any))
   880  	subdata := make(map[string]any)
   881  	subdata["bar"] = "baz"
   882  	data := make(map[string]any)
   883  	data["foo"] = subdata
   884  
   885  	b := new(bytes.Buffer)
   886  	err := NewEncoder(b).Encode(data)
   887  	if err != nil {
   888  		t.Fatal("encode:", err)
   889  	}
   890  	var newData map[string]any
   891  	err = NewDecoder(b).Decode(&newData)
   892  	if err != nil {
   893  		t.Fatal("decode:", err)
   894  	}
   895  	if !reflect.DeepEqual(data, newData) {
   896  		t.Fatalf("expected %v got %v", data, newData)
   897  	}
   898  }
   899  
   900  // Test that untyped nils generate an error, not a panic.
   901  // See Issue 16204.
   902  func TestCatchInvalidNilValue(t *testing.T) {
   903  	encodeErr, panicErr := encodeAndRecover(nil)
   904  	if panicErr != nil {
   905  		t.Fatalf("panicErr=%v, should not panic encoding untyped nil", panicErr)
   906  	}
   907  	if encodeErr == nil {
   908  		t.Errorf("got err=nil, want non-nil error when encoding untyped nil value")
   909  	} else if !strings.Contains(encodeErr.Error(), "nil value") {
   910  		t.Errorf("expected 'nil value' error; got err=%v", encodeErr)
   911  	}
   912  }
   913  
   914  // A top-level nil pointer generates a panic with a helpful string-valued message.
   915  func TestTopLevelNilPointer(t *testing.T) {
   916  	var ip *int
   917  	encodeErr, panicErr := encodeAndRecover(ip)
   918  	if encodeErr != nil {
   919  		t.Fatal("error in encode:", encodeErr)
   920  	}
   921  	if panicErr == nil {
   922  		t.Fatal("top-level nil pointer did not panic")
   923  	}
   924  	errMsg := panicErr.Error()
   925  	if !strings.Contains(errMsg, "nil pointer") {
   926  		t.Fatal("expected nil pointer error, got:", errMsg)
   927  	}
   928  }
   929  
   930  func encodeAndRecover(value any) (encodeErr, panicErr error) {
   931  	defer func() {
   932  		e := recover()
   933  		if e != nil {
   934  			switch err := e.(type) {
   935  			case error:
   936  				panicErr = err
   937  			default:
   938  				panicErr = fmt.Errorf("%v", err)
   939  			}
   940  		}
   941  	}()
   942  
   943  	encodeErr = NewEncoder(io.Discard).Encode(value)
   944  	return
   945  }
   946  
   947  func TestNilPointerPanics(t *testing.T) {
   948  	var (
   949  		nilStringPtr      *string
   950  		intMap            = make(map[int]int)
   951  		intMapPtr         = &intMap
   952  		nilIntMapPtr      *map[int]int
   953  		zero              int
   954  		nilBoolChannel    chan bool
   955  		nilBoolChannelPtr *chan bool
   956  		nilStringSlice    []string
   957  		stringSlice       = make([]string, 1)
   958  		nilStringSlicePtr *[]string
   959  	)
   960  
   961  	testCases := []struct {
   962  		value     any
   963  		mustPanic bool
   964  	}{
   965  		{nilStringPtr, true},
   966  		{intMap, false},
   967  		{intMapPtr, false},
   968  		{nilIntMapPtr, true},
   969  		{zero, false},
   970  		{nilStringSlice, false},
   971  		{stringSlice, false},
   972  		{nilStringSlicePtr, true},
   973  		{nilBoolChannel, false},
   974  		{nilBoolChannelPtr, true},
   975  	}
   976  
   977  	for _, tt := range testCases {
   978  		_, panicErr := encodeAndRecover(tt.value)
   979  		if tt.mustPanic {
   980  			if panicErr == nil {
   981  				t.Errorf("expected panic with input %#v, did not panic", tt.value)
   982  			}
   983  			continue
   984  		}
   985  		if panicErr != nil {
   986  			t.Fatalf("expected no panic with input %#v, got panic=%v", tt.value, panicErr)
   987  		}
   988  	}
   989  }
   990  
   991  func TestNilPointerInsideInterface(t *testing.T) {
   992  	var ip *int
   993  	si := struct {
   994  		I any
   995  	}{
   996  		I: ip,
   997  	}
   998  	buf := new(bytes.Buffer)
   999  	err := NewEncoder(buf).Encode(si)
  1000  	if err == nil {
  1001  		t.Fatal("expected error, got none")
  1002  	}
  1003  	errMsg := err.Error()
  1004  	if !strings.Contains(errMsg, "nil pointer") || !strings.Contains(errMsg, "interface") {
  1005  		t.Fatal("expected error about nil pointer and interface, got:", errMsg)
  1006  	}
  1007  }
  1008  
  1009  type Bug4Public struct {
  1010  	Name   string
  1011  	Secret Bug4Secret
  1012  }
  1013  
  1014  type Bug4Secret struct {
  1015  	a int // error: no exported fields.
  1016  }
  1017  
  1018  // Test that a failed compilation doesn't leave around an executable encoder.
  1019  // Issue 3723.
  1020  func TestMutipleEncodingsOfBadType(t *testing.T) {
  1021  	x := Bug4Public{
  1022  		Name:   "name",
  1023  		Secret: Bug4Secret{1},
  1024  	}
  1025  	buf := new(bytes.Buffer)
  1026  	enc := NewEncoder(buf)
  1027  	err := enc.Encode(x)
  1028  	if err == nil {
  1029  		t.Fatal("first encoding: expected error")
  1030  	}
  1031  	buf.Reset()
  1032  	enc = NewEncoder(buf)
  1033  	err = enc.Encode(x)
  1034  	if err == nil {
  1035  		t.Fatal("second encoding: expected error")
  1036  	}
  1037  	if !strings.Contains(err.Error(), "no exported fields") {
  1038  		t.Errorf("expected error about no exported fields; got %v", err)
  1039  	}
  1040  }
  1041  
  1042  // There was an error check comparing the length of the input with the
  1043  // length of the slice being decoded. It was wrong because the next
  1044  // thing in the input might be a type definition, which would lead to
  1045  // an incorrect length check. This test reproduces the corner case.
  1046  
  1047  type Z struct {
  1048  }
  1049  
  1050  func Test29ElementSlice(t *testing.T) {
  1051  	Register(Z{})
  1052  	src := make([]any, 100) // Size needs to be bigger than size of type definition.
  1053  	for i := range src {
  1054  		src[i] = Z{}
  1055  	}
  1056  	buf := new(bytes.Buffer)
  1057  	err := NewEncoder(buf).Encode(src)
  1058  	if err != nil {
  1059  		t.Fatalf("encode: %v", err)
  1060  		return
  1061  	}
  1062  
  1063  	var dst []any
  1064  	err = NewDecoder(buf).Decode(&dst)
  1065  	if err != nil {
  1066  		t.Errorf("decode: %v", err)
  1067  		return
  1068  	}
  1069  }
  1070  
  1071  // Don't crash, just give error when allocating a huge slice.
  1072  // Issue 8084.
  1073  func TestErrorForHugeSlice(t *testing.T) {
  1074  	// Encode an int slice.
  1075  	buf := new(bytes.Buffer)
  1076  	slice := []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  1077  	err := NewEncoder(buf).Encode(slice)
  1078  	if err != nil {
  1079  		t.Fatal("encode:", err)
  1080  	}
  1081  	// Reach into the buffer and smash the count to make the encoded slice very long.
  1082  	buf.Bytes()[buf.Len()-len(slice)-1] = 0xfa
  1083  	// Decode and see error.
  1084  	err = NewDecoder(buf).Decode(&slice)
  1085  	if err == nil {
  1086  		t.Fatal("decode: no error")
  1087  	}
  1088  	if !strings.Contains(err.Error(), "slice too big") {
  1089  		t.Fatalf("decode: expected slice too big error, got %s", err.Error())
  1090  	}
  1091  }
  1092  
  1093  type badDataTest struct {
  1094  	input string // The input encoded as a hex string.
  1095  	error string // A substring of the error that should result.
  1096  	data  any    // What to decode into.
  1097  }
  1098  
  1099  var badDataTests = []badDataTest{
  1100  	{"", "EOF", nil},
  1101  	{"7F6869", "unexpected EOF", nil},
  1102  	{"036e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e", "unknown type id", new(ET2)},
  1103  	{"0424666f6f", "field numbers out of bounds", new(ET2)}, // Issue 6323.
  1104  	{"05100028557b02027f8302", "interface encoding", nil},   // Issue 10270.
  1105  	// Issue 10273.
  1106  	{"130a00fb5dad0bf8ff020263e70002fa28020202a89859", "slice length too large", nil},
  1107  	{"0f1000fb285d003316020735ff023a65c5", "interface encoding", nil},
  1108  	{"03fffb0616fffc00f902ff02ff03bf005d02885802a311a8120228022c028ee7", "GobDecoder", nil},
  1109  	// Issue 10491.
  1110  	{"10fe010f020102fe01100001fe010e000016fe010d030102fe010e00010101015801fe01100000000bfe011000f85555555555555555", "exceeds input size", nil},
  1111  }
  1112  
  1113  // TestBadData tests that various problems caused by malformed input
  1114  // are caught as errors and do not cause panics.
  1115  func TestBadData(t *testing.T) {
  1116  	for i, test := range badDataTests {
  1117  		data, err := hex.DecodeString(test.input)
  1118  		if err != nil {
  1119  			t.Fatalf("#%d: hex error: %s", i, err)
  1120  		}
  1121  		d := NewDecoder(bytes.NewReader(data))
  1122  		err = d.Decode(test.data)
  1123  		if err == nil {
  1124  			t.Errorf("decode: no error")
  1125  			continue
  1126  		}
  1127  		if !strings.Contains(err.Error(), test.error) {
  1128  			t.Errorf("#%d: decode: expected %q error, got %s", i, test.error, err.Error())
  1129  		}
  1130  	}
  1131  }
  1132  
  1133  func TestDecodeErrorMultipleTypes(t *testing.T) {
  1134  	type Test struct {
  1135  		A string
  1136  		B int
  1137  	}
  1138  	var b bytes.Buffer
  1139  	NewEncoder(&b).Encode(Test{"one", 1})
  1140  
  1141  	var result, result2 Test
  1142  	dec := NewDecoder(&b)
  1143  	err := dec.Decode(&result)
  1144  	if err != nil {
  1145  		t.Errorf("decode: unexpected error %v", err)
  1146  	}
  1147  
  1148  	b.Reset()
  1149  	NewEncoder(&b).Encode(Test{"two", 2})
  1150  	err = dec.Decode(&result2)
  1151  	if err == nil {
  1152  		t.Errorf("decode: expected duplicate type error, got nil")
  1153  	} else if !strings.Contains(err.Error(), "duplicate type") {
  1154  		t.Errorf("decode: expected duplicate type error, got %s", err.Error())
  1155  	}
  1156  }
  1157  
  1158  // Issue 24075
  1159  func TestMarshalFloatMap(t *testing.T) {
  1160  	nan1 := math.NaN()
  1161  	nan2 := math.Float64frombits(math.Float64bits(nan1) ^ 1) // A different NaN in the same class.
  1162  
  1163  	in := map[float64]string{
  1164  		nan1: "a",
  1165  		nan1: "b",
  1166  		nan2: "c",
  1167  	}
  1168  
  1169  	var b bytes.Buffer
  1170  	enc := NewEncoder(&b)
  1171  	if err := enc.Encode(in); err != nil {
  1172  		t.Errorf("Encode : %v", err)
  1173  	}
  1174  
  1175  	out := map[float64]string{}
  1176  	dec := NewDecoder(&b)
  1177  	if err := dec.Decode(&out); err != nil {
  1178  		t.Fatalf("Decode : %v", err)
  1179  	}
  1180  
  1181  	type mapEntry struct {
  1182  		keyBits uint64
  1183  		value   string
  1184  	}
  1185  	readMap := func(m map[float64]string) (entries []mapEntry) {
  1186  		for k, v := range m {
  1187  			entries = append(entries, mapEntry{math.Float64bits(k), v})
  1188  		}
  1189  		sort.Slice(entries, func(i, j int) bool {
  1190  			ei, ej := entries[i], entries[j]
  1191  			if ei.keyBits != ej.keyBits {
  1192  				return ei.keyBits < ej.keyBits
  1193  			}
  1194  			return ei.value < ej.value
  1195  		})
  1196  		return entries
  1197  	}
  1198  
  1199  	got := readMap(out)
  1200  	want := readMap(in)
  1201  	if !reflect.DeepEqual(got, want) {
  1202  		t.Fatalf("\nEncode: %v\nDecode: %v", want, got)
  1203  	}
  1204  }
  1205  
  1206  func TestDecodePartial(t *testing.T) {
  1207  	type T struct {
  1208  		X []int
  1209  		Y string
  1210  	}
  1211  
  1212  	var buf bytes.Buffer
  1213  	t1 := T{X: []int{1, 2, 3}, Y: "foo"}
  1214  	t2 := T{X: []int{4, 5, 6}, Y: "bar"}
  1215  	enc := NewEncoder(&buf)
  1216  
  1217  	t1start := 0
  1218  	if err := enc.Encode(&t1); err != nil {
  1219  		t.Fatal(err)
  1220  	}
  1221  
  1222  	t2start := buf.Len()
  1223  	if err := enc.Encode(&t2); err != nil {
  1224  		t.Fatal(err)
  1225  	}
  1226  
  1227  	data := buf.Bytes()
  1228  	for i := 0; i <= len(data); i++ {
  1229  		bufr := bytes.NewReader(data[:i])
  1230  
  1231  		// Decode both values, stopping at the first error.
  1232  		var t1b, t2b T
  1233  		dec := NewDecoder(bufr)
  1234  		var err error
  1235  		err = dec.Decode(&t1b)
  1236  		if err == nil {
  1237  			err = dec.Decode(&t2b)
  1238  		}
  1239  
  1240  		switch i {
  1241  		case t1start, t2start:
  1242  			// Either the first or the second Decode calls had zero input.
  1243  			if err != io.EOF {
  1244  				t.Errorf("%d/%d: expected io.EOF: %v", i, len(data), err)
  1245  			}
  1246  		case len(data):
  1247  			// We reached the end of the entire input.
  1248  			if err != nil {
  1249  				t.Errorf("%d/%d: unexpected error: %v", i, len(data), err)
  1250  			}
  1251  			if !reflect.DeepEqual(t1b, t1) {
  1252  				t.Fatalf("t1 value mismatch: got %v, want %v", t1b, t1)
  1253  			}
  1254  			if !reflect.DeepEqual(t2b, t2) {
  1255  				t.Fatalf("t2 value mismatch: got %v, want %v", t2b, t2)
  1256  			}
  1257  		default:
  1258  			// In between, we must see io.ErrUnexpectedEOF.
  1259  			// The decoder used to erroneously return io.EOF in some cases here,
  1260  			// such as if the input was cut off right after some type specs,
  1261  			// but before any value was actually transmitted.
  1262  			if err != io.ErrUnexpectedEOF {
  1263  				t.Errorf("%d/%d: expected io.ErrUnexpectedEOF: %v", i, len(data), err)
  1264  			}
  1265  		}
  1266  	}
  1267  }
  1268  

View as plain text