...

Source file src/encoding/gob/encoder_test.go

Documentation: encoding/gob

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

View as plain text