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

View as plain text