github.com/jimmyx0x/go-ethereum@v1.10.28/rlp/decode_test.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rlp
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"math/big"
    26  	"reflect"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/ethereum/go-ethereum/common/math"
    31  )
    32  
    33  func TestStreamKind(t *testing.T) {
    34  	tests := []struct {
    35  		input    string
    36  		wantKind Kind
    37  		wantLen  uint64
    38  	}{
    39  		{"00", Byte, 0},
    40  		{"01", Byte, 0},
    41  		{"7F", Byte, 0},
    42  		{"80", String, 0},
    43  		{"B7", String, 55},
    44  		{"B90400", String, 1024},
    45  		{"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)},
    46  		{"C0", List, 0},
    47  		{"C8", List, 8},
    48  		{"F7", List, 55},
    49  		{"F90400", List, 1024},
    50  		{"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)},
    51  	}
    52  
    53  	for i, test := range tests {
    54  		// using plainReader to inhibit input limit errors.
    55  		s := NewStream(newPlainReader(unhex(test.input)), 0)
    56  		kind, len, err := s.Kind()
    57  		if err != nil {
    58  			t.Errorf("test %d: Kind returned error: %v", i, err)
    59  			continue
    60  		}
    61  		if kind != test.wantKind {
    62  			t.Errorf("test %d: kind mismatch: got %d, want %d", i, kind, test.wantKind)
    63  		}
    64  		if len != test.wantLen {
    65  			t.Errorf("test %d: len mismatch: got %d, want %d", i, len, test.wantLen)
    66  		}
    67  	}
    68  }
    69  
    70  func TestNewListStream(t *testing.T) {
    71  	ls := NewListStream(bytes.NewReader(unhex("0101010101")), 3)
    72  	if k, size, err := ls.Kind(); k != List || size != 3 || err != nil {
    73  		t.Errorf("Kind() returned (%v, %d, %v), expected (List, 3, nil)", k, size, err)
    74  	}
    75  	if size, err := ls.List(); size != 3 || err != nil {
    76  		t.Errorf("List() returned (%d, %v), expected (3, nil)", size, err)
    77  	}
    78  	for i := 0; i < 3; i++ {
    79  		if val, err := ls.Uint(); val != 1 || err != nil {
    80  			t.Errorf("Uint() returned (%d, %v), expected (1, nil)", val, err)
    81  		}
    82  	}
    83  	if err := ls.ListEnd(); err != nil {
    84  		t.Errorf("ListEnd() returned %v, expected (3, nil)", err)
    85  	}
    86  }
    87  
    88  func TestStreamErrors(t *testing.T) {
    89  	withoutInputLimit := func(b []byte) *Stream {
    90  		return NewStream(newPlainReader(b), 0)
    91  	}
    92  	withCustomInputLimit := func(limit uint64) func([]byte) *Stream {
    93  		return func(b []byte) *Stream {
    94  			return NewStream(bytes.NewReader(b), limit)
    95  		}
    96  	}
    97  
    98  	type calls []string
    99  	tests := []struct {
   100  		string
   101  		calls
   102  		newStream func([]byte) *Stream // uses bytes.Reader if nil
   103  		error     error
   104  	}{
   105  		{"C0", calls{"Bytes"}, nil, ErrExpectedString},
   106  		{"C0", calls{"Uint"}, nil, ErrExpectedString},
   107  		{"89000000000000000001", calls{"Uint"}, nil, errUintOverflow},
   108  		{"00", calls{"List"}, nil, ErrExpectedList},
   109  		{"80", calls{"List"}, nil, ErrExpectedList},
   110  		{"C0", calls{"List", "Uint"}, nil, EOL},
   111  		{"C8C9010101010101010101", calls{"List", "Kind"}, nil, ErrElemTooLarge},
   112  		{"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, nil, EOL},
   113  		{"00", calls{"ListEnd"}, nil, errNotInList},
   114  		{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
   115  
   116  		// Non-canonical integers (e.g. leading zero bytes).
   117  		{"00", calls{"Uint"}, nil, ErrCanonInt},
   118  		{"820002", calls{"Uint"}, nil, ErrCanonInt},
   119  		{"8133", calls{"Uint"}, nil, ErrCanonSize},
   120  		{"817F", calls{"Uint"}, nil, ErrCanonSize},
   121  		{"8180", calls{"Uint"}, nil, nil},
   122  
   123  		// Non-valid boolean
   124  		{"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")},
   125  
   126  		// Size tags must use the smallest possible encoding.
   127  		// Leading zero bytes in the size tag are also rejected.
   128  		{"8100", calls{"Uint"}, nil, ErrCanonSize},
   129  		{"8100", calls{"Bytes"}, nil, ErrCanonSize},
   130  		{"8101", calls{"Bytes"}, nil, ErrCanonSize},
   131  		{"817F", calls{"Bytes"}, nil, ErrCanonSize},
   132  		{"8180", calls{"Bytes"}, nil, nil},
   133  		{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   134  		{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   135  		{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   136  		{"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize},
   137  		{"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   138  		{"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   139  		{"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   140  		{"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize},
   141  
   142  		// Expected EOF
   143  		{"", calls{"Kind"}, nil, io.EOF},
   144  		{"", calls{"Uint"}, nil, io.EOF},
   145  		{"", calls{"List"}, nil, io.EOF},
   146  		{"8180", calls{"Uint", "Uint"}, nil, io.EOF},
   147  		{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
   148  
   149  		{"", calls{"List"}, withoutInputLimit, io.EOF},
   150  		{"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF},
   151  		{"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF},
   152  
   153  		// Input limit errors.
   154  		{"81", calls{"Bytes"}, nil, ErrValueTooLarge},
   155  		{"81", calls{"Uint"}, nil, ErrValueTooLarge},
   156  		{"81", calls{"Raw"}, nil, ErrValueTooLarge},
   157  		{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
   158  		{"C801", calls{"List"}, nil, ErrValueTooLarge},
   159  
   160  		// Test for list element size check overflow.
   161  		{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},
   162  
   163  		// Test for input limit overflow. Since we are counting the limit
   164  		// down toward zero in Stream.remaining, reading too far can overflow
   165  		// remaining to a large value, effectively disabling the limit.
   166  		{"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF},
   167  		{"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
   168  
   169  		// Check that the same calls are fine without a limit.
   170  		{"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   171  		{"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   172  
   173  		// Unexpected EOF. This only happens when there is
   174  		// no input limit, so the reader needs to be 'dumbed down'.
   175  		{"81", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   176  		{"81", calls{"Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   177  		{"BFFFFFFFFFFFFFFF", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   178  		{"C801", calls{"List", "Uint", "Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   179  
   180  		// This test verifies that the input position is advanced
   181  		// correctly when calling Bytes for empty strings. Kind can be called
   182  		// any number of times in between and doesn't advance.
   183  		{"C3808080", calls{
   184  			"List",  // enter the list
   185  			"Bytes", // past first element
   186  
   187  			"Kind", "Kind", "Kind", // this shouldn't advance
   188  
   189  			"Bytes", // past second element
   190  
   191  			"Kind", "Kind", // can't hurt to try
   192  
   193  			"Bytes", // past final element
   194  			"Bytes", // this one should fail
   195  		}, nil, EOL},
   196  	}
   197  
   198  testfor:
   199  	for i, test := range tests {
   200  		if test.newStream == nil {
   201  			test.newStream = func(b []byte) *Stream { return NewStream(bytes.NewReader(b), 0) }
   202  		}
   203  		s := test.newStream(unhex(test.string))
   204  		rs := reflect.ValueOf(s)
   205  		for j, call := range test.calls {
   206  			fval := rs.MethodByName(call)
   207  			ret := fval.Call(nil)
   208  			err := "<nil>"
   209  			if lastret := ret[len(ret)-1].Interface(); lastret != nil {
   210  				err = lastret.(error).Error()
   211  			}
   212  			if j == len(test.calls)-1 {
   213  				want := "<nil>"
   214  				if test.error != nil {
   215  					want = test.error.Error()
   216  				}
   217  				if err != want {
   218  					t.Log(test)
   219  					t.Errorf("test %d: last call (%s) error mismatch\ngot:  %s\nwant: %s",
   220  						i, call, err, test.error)
   221  				}
   222  			} else if err != "<nil>" {
   223  				t.Log(test)
   224  				t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err)
   225  				continue testfor
   226  			}
   227  		}
   228  	}
   229  }
   230  
   231  func TestStreamList(t *testing.T) {
   232  	s := NewStream(bytes.NewReader(unhex("C80102030405060708")), 0)
   233  
   234  	len, err := s.List()
   235  	if err != nil {
   236  		t.Fatalf("List error: %v", err)
   237  	}
   238  	if len != 8 {
   239  		t.Fatalf("List returned invalid length, got %d, want 8", len)
   240  	}
   241  
   242  	for i := uint64(1); i <= 8; i++ {
   243  		v, err := s.Uint()
   244  		if err != nil {
   245  			t.Fatalf("Uint error: %v", err)
   246  		}
   247  		if i != v {
   248  			t.Errorf("Uint returned wrong value, got %d, want %d", v, i)
   249  		}
   250  	}
   251  
   252  	if _, err := s.Uint(); err != EOL {
   253  		t.Errorf("Uint error mismatch, got %v, want %v", err, EOL)
   254  	}
   255  	if err = s.ListEnd(); err != nil {
   256  		t.Fatalf("ListEnd error: %v", err)
   257  	}
   258  }
   259  
   260  func TestStreamRaw(t *testing.T) {
   261  	tests := []struct {
   262  		input  string
   263  		output string
   264  	}{
   265  		{
   266  			"C58401010101",
   267  			"8401010101",
   268  		},
   269  		{
   270  			"F842B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   271  			"B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   272  		},
   273  	}
   274  	for i, tt := range tests {
   275  		s := NewStream(bytes.NewReader(unhex(tt.input)), 0)
   276  		s.List()
   277  
   278  		want := unhex(tt.output)
   279  		raw, err := s.Raw()
   280  		if err != nil {
   281  			t.Fatal(err)
   282  		}
   283  		if !bytes.Equal(want, raw) {
   284  			t.Errorf("test %d: raw mismatch: got %x, want %x", i, raw, want)
   285  		}
   286  	}
   287  }
   288  
   289  func TestStreamReadBytes(t *testing.T) {
   290  	tests := []struct {
   291  		input string
   292  		size  int
   293  		err   string
   294  	}{
   295  		// kind List
   296  		{input: "C0", size: 1, err: "rlp: expected String or Byte"},
   297  		// kind Byte
   298  		{input: "04", size: 0, err: "input value has wrong size 1, want 0"},
   299  		{input: "04", size: 1},
   300  		{input: "04", size: 2, err: "input value has wrong size 1, want 2"},
   301  		// kind String
   302  		{input: "820102", size: 0, err: "input value has wrong size 2, want 0"},
   303  		{input: "820102", size: 1, err: "input value has wrong size 2, want 1"},
   304  		{input: "820102", size: 2},
   305  		{input: "820102", size: 3, err: "input value has wrong size 2, want 3"},
   306  	}
   307  
   308  	for _, test := range tests {
   309  		test := test
   310  		name := fmt.Sprintf("input_%s/size_%d", test.input, test.size)
   311  		t.Run(name, func(t *testing.T) {
   312  			s := NewStream(bytes.NewReader(unhex(test.input)), 0)
   313  			b := make([]byte, test.size)
   314  			err := s.ReadBytes(b)
   315  			if test.err == "" {
   316  				if err != nil {
   317  					t.Errorf("unexpected error %q", err)
   318  				}
   319  			} else {
   320  				if err == nil {
   321  					t.Errorf("expected error, got nil")
   322  				} else if err.Error() != test.err {
   323  					t.Errorf("wrong error %q", err)
   324  				}
   325  			}
   326  		})
   327  	}
   328  }
   329  
   330  func TestDecodeErrors(t *testing.T) {
   331  	r := bytes.NewReader(nil)
   332  
   333  	if err := Decode(r, nil); err != errDecodeIntoNil {
   334  		t.Errorf("Decode(r, nil) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   335  	}
   336  
   337  	var nilptr *struct{}
   338  	if err := Decode(r, nilptr); err != errDecodeIntoNil {
   339  		t.Errorf("Decode(r, nilptr) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   340  	}
   341  
   342  	if err := Decode(r, struct{}{}); err != errNoPointer {
   343  		t.Errorf("Decode(r, struct{}{}) error mismatch, got %q, want %q", err, errNoPointer)
   344  	}
   345  
   346  	expectErr := "rlp: type chan bool is not RLP-serializable"
   347  	if err := Decode(r, new(chan bool)); err == nil || err.Error() != expectErr {
   348  		t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
   349  	}
   350  
   351  	if err := Decode(r, new(uint)); err != io.EOF {
   352  		t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
   353  	}
   354  }
   355  
   356  type decodeTest struct {
   357  	input string
   358  	ptr   interface{}
   359  	value interface{}
   360  	error string
   361  }
   362  
   363  type simplestruct struct {
   364  	A uint
   365  	B string
   366  }
   367  
   368  type recstruct struct {
   369  	I     uint
   370  	Child *recstruct `rlp:"nil"`
   371  }
   372  
   373  type bigIntStruct struct {
   374  	I *big.Int
   375  	B string
   376  }
   377  
   378  type invalidNilTag struct {
   379  	X []byte `rlp:"nil"`
   380  }
   381  
   382  type invalidTail1 struct {
   383  	A uint `rlp:"tail"`
   384  	B string
   385  }
   386  
   387  type invalidTail2 struct {
   388  	A uint
   389  	B string `rlp:"tail"`
   390  }
   391  
   392  type tailRaw struct {
   393  	A    uint
   394  	Tail []RawValue `rlp:"tail"`
   395  }
   396  
   397  type tailUint struct {
   398  	A    uint
   399  	Tail []uint `rlp:"tail"`
   400  }
   401  
   402  type tailPrivateFields struct {
   403  	A    uint
   404  	Tail []uint `rlp:"tail"`
   405  	x, y bool   //lint:ignore U1000 unused fields required for testing purposes.
   406  }
   407  
   408  type nilListUint struct {
   409  	X *uint `rlp:"nilList"`
   410  }
   411  
   412  type nilStringSlice struct {
   413  	X *[]uint `rlp:"nilString"`
   414  }
   415  
   416  type intField struct {
   417  	X int
   418  }
   419  
   420  type optionalFields struct {
   421  	A uint
   422  	B uint `rlp:"optional"`
   423  	C uint `rlp:"optional"`
   424  }
   425  
   426  type optionalAndTailField struct {
   427  	A    uint
   428  	B    uint   `rlp:"optional"`
   429  	Tail []uint `rlp:"tail"`
   430  }
   431  
   432  type optionalBigIntField struct {
   433  	A uint
   434  	B *big.Int `rlp:"optional"`
   435  }
   436  
   437  type optionalPtrField struct {
   438  	A uint
   439  	B *[3]byte `rlp:"optional"`
   440  }
   441  
   442  type nonOptionalPtrField struct {
   443  	A uint
   444  	B *[3]byte
   445  }
   446  
   447  type multipleOptionalFields struct {
   448  	A *[3]byte `rlp:"optional"`
   449  	B *[3]byte `rlp:"optional"`
   450  }
   451  
   452  type optionalPtrFieldNil struct {
   453  	A uint
   454  	B *[3]byte `rlp:"optional,nil"`
   455  }
   456  
   457  type ignoredField struct {
   458  	A uint
   459  	B uint `rlp:"-"`
   460  	C uint
   461  }
   462  
   463  var (
   464  	veryBigInt = new(big.Int).Add(
   465  		new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
   466  		big.NewInt(0xFFFF),
   467  	)
   468  	veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)
   469  )
   470  
   471  var decodeTests = []decodeTest{
   472  	// booleans
   473  	{input: "01", ptr: new(bool), value: true},
   474  	{input: "80", ptr: new(bool), value: false},
   475  	{input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"},
   476  
   477  	// integers
   478  	{input: "05", ptr: new(uint32), value: uint32(5)},
   479  	{input: "80", ptr: new(uint32), value: uint32(0)},
   480  	{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
   481  	{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
   482  	{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
   483  	{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
   484  	{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
   485  	{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   486  	{input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   487  	{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   488  	{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   489  
   490  	// slices
   491  	{input: "C0", ptr: new([]uint), value: []uint{}},
   492  	{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
   493  	{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
   494  
   495  	// arrays
   496  	{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
   497  	{input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   498  	{input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   499  	{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
   500  	{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
   501  
   502  	// zero sized arrays
   503  	{input: "C0", ptr: new([0]uint), value: [0]uint{}},
   504  	{input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"},
   505  
   506  	// byte slices
   507  	{input: "01", ptr: new([]byte), value: []byte{1}},
   508  	{input: "80", ptr: new([]byte), value: []byte{}},
   509  	{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
   510  	{input: "C0", ptr: new([]byte), error: "rlp: expected input string or byte for []uint8"},
   511  	{input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"},
   512  
   513  	// byte arrays
   514  	{input: "02", ptr: new([1]byte), value: [1]byte{2}},
   515  	{input: "8180", ptr: new([1]byte), value: [1]byte{128}},
   516  	{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
   517  
   518  	// byte array errors
   519  	{input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   520  	{input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   521  	{input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   522  	{input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   523  	{input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   524  	{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"},
   525  	{input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   526  	{input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   527  
   528  	// zero sized byte arrays
   529  	{input: "80", ptr: new([0]byte), value: [0]byte{}},
   530  	{input: "01", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   531  	{input: "8101", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   532  
   533  	// strings
   534  	{input: "00", ptr: new(string), value: "\000"},
   535  	{input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"},
   536  	{input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"},
   537  
   538  	// big ints
   539  	{input: "80", ptr: new(*big.Int), value: big.NewInt(0)},
   540  	{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
   541  	{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
   542  	{input: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001", ptr: new(*big.Int), value: veryVeryBigInt},
   543  	{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
   544  	{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
   545  	{input: "00", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   546  	{input: "820001", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   547  	{input: "8105", ptr: new(*big.Int), error: "rlp: non-canonical size information for *big.Int"},
   548  
   549  	// structs
   550  	{
   551  		input: "C50583343434",
   552  		ptr:   new(simplestruct),
   553  		value: simplestruct{5, "444"},
   554  	},
   555  	{
   556  		input: "C601C402C203C0",
   557  		ptr:   new(recstruct),
   558  		value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
   559  	},
   560  	{
   561  		// This checks that empty big.Int works correctly in struct context. It's easy to
   562  		// miss the update of s.kind for this case, so it needs its own test.
   563  		input: "C58083343434",
   564  		ptr:   new(bigIntStruct),
   565  		value: bigIntStruct{new(big.Int), "444"},
   566  	},
   567  
   568  	// struct errors
   569  	{
   570  		input: "C0",
   571  		ptr:   new(simplestruct),
   572  		error: "rlp: too few elements for rlp.simplestruct",
   573  	},
   574  	{
   575  		input: "C105",
   576  		ptr:   new(simplestruct),
   577  		error: "rlp: too few elements for rlp.simplestruct",
   578  	},
   579  	{
   580  		input: "C7C50583343434C0",
   581  		ptr:   new([]*simplestruct),
   582  		error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]",
   583  	},
   584  	{
   585  		input: "83222222",
   586  		ptr:   new(simplestruct),
   587  		error: "rlp: expected input list for rlp.simplestruct",
   588  	},
   589  	{
   590  		input: "C3010101",
   591  		ptr:   new(simplestruct),
   592  		error: "rlp: input list has too many elements for rlp.simplestruct",
   593  	},
   594  	{
   595  		input: "C501C3C00000",
   596  		ptr:   new(recstruct),
   597  		error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I",
   598  	},
   599  	{
   600  		input: "C103",
   601  		ptr:   new(intField),
   602  		error: "rlp: type int is not RLP-serializable (struct field rlp.intField.X)",
   603  	},
   604  	{
   605  		input: "C50102C20102",
   606  		ptr:   new(tailUint),
   607  		error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]",
   608  	},
   609  	{
   610  		input: "C0",
   611  		ptr:   new(invalidNilTag),
   612  		error: `rlp: invalid struct tag "nil" for rlp.invalidNilTag.X (field is not a pointer)`,
   613  	},
   614  
   615  	// struct tag "tail"
   616  	{
   617  		input: "C3010203",
   618  		ptr:   new(tailRaw),
   619  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}},
   620  	},
   621  	{
   622  		input: "C20102",
   623  		ptr:   new(tailRaw),
   624  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}},
   625  	},
   626  	{
   627  		input: "C101",
   628  		ptr:   new(tailRaw),
   629  		value: tailRaw{A: 1, Tail: []RawValue{}},
   630  	},
   631  	{
   632  		input: "C3010203",
   633  		ptr:   new(tailPrivateFields),
   634  		value: tailPrivateFields{A: 1, Tail: []uint{2, 3}},
   635  	},
   636  	{
   637  		input: "C0",
   638  		ptr:   new(invalidTail1),
   639  		error: `rlp: invalid struct tag "tail" for rlp.invalidTail1.A (must be on last field)`,
   640  	},
   641  	{
   642  		input: "C0",
   643  		ptr:   new(invalidTail2),
   644  		error: `rlp: invalid struct tag "tail" for rlp.invalidTail2.B (field type is not slice)`,
   645  	},
   646  
   647  	// struct tag "-"
   648  	{
   649  		input: "C20102",
   650  		ptr:   new(ignoredField),
   651  		value: ignoredField{A: 1, C: 2},
   652  	},
   653  
   654  	// struct tag "nilList"
   655  	{
   656  		input: "C180",
   657  		ptr:   new(nilListUint),
   658  		error: "rlp: wrong kind of empty value (got String, want List) for *uint, decoding into (rlp.nilListUint).X",
   659  	},
   660  	{
   661  		input: "C1C0",
   662  		ptr:   new(nilListUint),
   663  		value: nilListUint{},
   664  	},
   665  	{
   666  		input: "C103",
   667  		ptr:   new(nilListUint),
   668  		value: func() interface{} {
   669  			v := uint(3)
   670  			return nilListUint{X: &v}
   671  		}(),
   672  	},
   673  
   674  	// struct tag "nilString"
   675  	{
   676  		input: "C1C0",
   677  		ptr:   new(nilStringSlice),
   678  		error: "rlp: wrong kind of empty value (got List, want String) for *[]uint, decoding into (rlp.nilStringSlice).X",
   679  	},
   680  	{
   681  		input: "C180",
   682  		ptr:   new(nilStringSlice),
   683  		value: nilStringSlice{},
   684  	},
   685  	{
   686  		input: "C2C103",
   687  		ptr:   new(nilStringSlice),
   688  		value: nilStringSlice{X: &[]uint{3}},
   689  	},
   690  
   691  	// struct tag "optional"
   692  	{
   693  		input: "C101",
   694  		ptr:   new(optionalFields),
   695  		value: optionalFields{1, 0, 0},
   696  	},
   697  	{
   698  		input: "C20102",
   699  		ptr:   new(optionalFields),
   700  		value: optionalFields{1, 2, 0},
   701  	},
   702  	{
   703  		input: "C3010203",
   704  		ptr:   new(optionalFields),
   705  		value: optionalFields{1, 2, 3},
   706  	},
   707  	{
   708  		input: "C401020304",
   709  		ptr:   new(optionalFields),
   710  		error: "rlp: input list has too many elements for rlp.optionalFields",
   711  	},
   712  	{
   713  		input: "C101",
   714  		ptr:   new(optionalAndTailField),
   715  		value: optionalAndTailField{A: 1},
   716  	},
   717  	{
   718  		input: "C20102",
   719  		ptr:   new(optionalAndTailField),
   720  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}},
   721  	},
   722  	{
   723  		input: "C401020304",
   724  		ptr:   new(optionalAndTailField),
   725  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{3, 4}},
   726  	},
   727  	{
   728  		input: "C101",
   729  		ptr:   new(optionalBigIntField),
   730  		value: optionalBigIntField{A: 1, B: nil},
   731  	},
   732  	{
   733  		input: "C20102",
   734  		ptr:   new(optionalBigIntField),
   735  		value: optionalBigIntField{A: 1, B: big.NewInt(2)},
   736  	},
   737  	{
   738  		input: "C101",
   739  		ptr:   new(optionalPtrField),
   740  		value: optionalPtrField{A: 1},
   741  	},
   742  	{
   743  		input: "C20180", // not accepted because "optional" doesn't enable "nil"
   744  		ptr:   new(optionalPtrField),
   745  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B",
   746  	},
   747  	{
   748  		input: "C20102",
   749  		ptr:   new(optionalPtrField),
   750  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B",
   751  	},
   752  	{
   753  		input: "C50183010203",
   754  		ptr:   new(optionalPtrField),
   755  		value: optionalPtrField{A: 1, B: &[3]byte{1, 2, 3}},
   756  	},
   757  	{
   758  		// all optional fields nil
   759  		input: "C0",
   760  		ptr:   new(multipleOptionalFields),
   761  		value: multipleOptionalFields{A: nil, B: nil},
   762  	},
   763  	{
   764  		// all optional fields set
   765  		input: "C88301020383010203",
   766  		ptr:   new(multipleOptionalFields),
   767  		value: multipleOptionalFields{A: &[3]byte{1, 2, 3}, B: &[3]byte{1, 2, 3}},
   768  	},
   769  	{
   770  		// nil optional field appears before a non-nil one
   771  		input: "C58083010203",
   772  		ptr:   new(multipleOptionalFields),
   773  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.multipleOptionalFields).A",
   774  	},
   775  	{
   776  		// decode a nil ptr into a ptr that is not nil or not optional
   777  		input: "C20180",
   778  		ptr:   new(nonOptionalPtrField),
   779  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.nonOptionalPtrField).B",
   780  	},
   781  	{
   782  		input: "C101",
   783  		ptr:   new(optionalPtrFieldNil),
   784  		value: optionalPtrFieldNil{A: 1},
   785  	},
   786  	{
   787  		input: "C20180", // accepted because "nil" tag allows empty input
   788  		ptr:   new(optionalPtrFieldNil),
   789  		value: optionalPtrFieldNil{A: 1},
   790  	},
   791  	{
   792  		input: "C20102",
   793  		ptr:   new(optionalPtrFieldNil),
   794  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrFieldNil).B",
   795  	},
   796  
   797  	// struct tag "optional" field clearing
   798  	{
   799  		input: "C101",
   800  		ptr:   &optionalFields{A: 9, B: 8, C: 7},
   801  		value: optionalFields{A: 1, B: 0, C: 0},
   802  	},
   803  	{
   804  		input: "C20102",
   805  		ptr:   &optionalFields{A: 9, B: 8, C: 7},
   806  		value: optionalFields{A: 1, B: 2, C: 0},
   807  	},
   808  	{
   809  		input: "C20102",
   810  		ptr:   &optionalAndTailField{A: 9, B: 8, Tail: []uint{7, 6, 5}},
   811  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}},
   812  	},
   813  	{
   814  		input: "C101",
   815  		ptr:   &optionalPtrField{A: 9, B: &[3]byte{8, 7, 6}},
   816  		value: optionalPtrField{A: 1},
   817  	},
   818  
   819  	// RawValue
   820  	{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
   821  	{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
   822  	{input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}},
   823  
   824  	// pointers
   825  	{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
   826  	{input: "80", ptr: new(*uint), value: uintp(0)},
   827  	{input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"},
   828  	{input: "07", ptr: new(*uint), value: uintp(7)},
   829  	{input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"},
   830  	{input: "8180", ptr: new(*uint), value: uintp(0x80)},
   831  	{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
   832  	{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
   833  
   834  	// check that input position is advanced also for empty values.
   835  	{input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}},
   836  
   837  	// interface{}
   838  	{input: "00", ptr: new(interface{}), value: []byte{0}},
   839  	{input: "01", ptr: new(interface{}), value: []byte{1}},
   840  	{input: "80", ptr: new(interface{}), value: []byte{}},
   841  	{input: "850505050505", ptr: new(interface{}), value: []byte{5, 5, 5, 5, 5}},
   842  	{input: "C0", ptr: new(interface{}), value: []interface{}{}},
   843  	{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
   844  	{
   845  		input: "C3010203",
   846  		ptr:   new([]io.Reader),
   847  		error: "rlp: type io.Reader is not RLP-serializable",
   848  	},
   849  
   850  	// fuzzer crashes
   851  	{
   852  		input: "c330f9c030f93030ce3030303030303030bd303030303030",
   853  		ptr:   new(interface{}),
   854  		error: "rlp: element is larger than containing list",
   855  	},
   856  }
   857  
   858  func uintp(i uint) *uint { return &i }
   859  
   860  func runTests(t *testing.T, decode func([]byte, interface{}) error) {
   861  	for i, test := range decodeTests {
   862  		input, err := hex.DecodeString(test.input)
   863  		if err != nil {
   864  			t.Errorf("test %d: invalid hex input %q", i, test.input)
   865  			continue
   866  		}
   867  		err = decode(input, test.ptr)
   868  		if err != nil && test.error == "" {
   869  			t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
   870  				i, err, test.ptr, test.input)
   871  			continue
   872  		}
   873  		if test.error != "" && fmt.Sprint(err) != test.error {
   874  			t.Errorf("test %d: Decode error mismatch\ngot  %v\nwant %v\ndecoding into %T\ninput %q",
   875  				i, err, test.error, test.ptr, test.input)
   876  			continue
   877  		}
   878  		deref := reflect.ValueOf(test.ptr).Elem().Interface()
   879  		if err == nil && !reflect.DeepEqual(deref, test.value) {
   880  			t.Errorf("test %d: value mismatch\ngot  %#v\nwant %#v\ndecoding into %T\ninput %q",
   881  				i, deref, test.value, test.ptr, test.input)
   882  		}
   883  	}
   884  }
   885  
   886  func TestDecodeWithByteReader(t *testing.T) {
   887  	runTests(t, func(input []byte, into interface{}) error {
   888  		return Decode(bytes.NewReader(input), into)
   889  	})
   890  }
   891  
   892  func testDecodeWithEncReader(t *testing.T, n int) {
   893  	s := strings.Repeat("0", n)
   894  	_, r, _ := EncodeToReader(s)
   895  	var decoded string
   896  	err := Decode(r, &decoded)
   897  	if err != nil {
   898  		t.Errorf("Unexpected decode error with n=%v: %v", n, err)
   899  	}
   900  	if decoded != s {
   901  		t.Errorf("Decode mismatch with n=%v", n)
   902  	}
   903  }
   904  
   905  // This is a regression test checking that decoding from encReader
   906  // works for RLP values of size 8192 bytes or more.
   907  func TestDecodeWithEncReader(t *testing.T) {
   908  	testDecodeWithEncReader(t, 8188) // length with header is 8191
   909  	testDecodeWithEncReader(t, 8189) // length with header is 8192
   910  }
   911  
   912  // plainReader reads from a byte slice but does not
   913  // implement ReadByte. It is also not recognized by the
   914  // size validation. This is useful to test how the decoder
   915  // behaves on a non-buffered input stream.
   916  type plainReader []byte
   917  
   918  func newPlainReader(b []byte) io.Reader {
   919  	return (*plainReader)(&b)
   920  }
   921  
   922  func (r *plainReader) Read(buf []byte) (n int, err error) {
   923  	if len(*r) == 0 {
   924  		return 0, io.EOF
   925  	}
   926  	n = copy(buf, *r)
   927  	*r = (*r)[n:]
   928  	return n, nil
   929  }
   930  
   931  func TestDecodeWithNonByteReader(t *testing.T) {
   932  	runTests(t, func(input []byte, into interface{}) error {
   933  		return Decode(newPlainReader(input), into)
   934  	})
   935  }
   936  
   937  func TestDecodeStreamReset(t *testing.T) {
   938  	s := NewStream(nil, 0)
   939  	runTests(t, func(input []byte, into interface{}) error {
   940  		s.Reset(bytes.NewReader(input), 0)
   941  		return s.Decode(into)
   942  	})
   943  }
   944  
   945  type testDecoder struct{ called bool }
   946  
   947  func (t *testDecoder) DecodeRLP(s *Stream) error {
   948  	if _, err := s.Uint(); err != nil {
   949  		return err
   950  	}
   951  	t.called = true
   952  	return nil
   953  }
   954  
   955  func TestDecodeDecoder(t *testing.T) {
   956  	var s struct {
   957  		T1 testDecoder
   958  		T2 *testDecoder
   959  		T3 **testDecoder
   960  	}
   961  	if err := Decode(bytes.NewReader(unhex("C3010203")), &s); err != nil {
   962  		t.Fatalf("Decode error: %v", err)
   963  	}
   964  
   965  	if !s.T1.called {
   966  		t.Errorf("DecodeRLP was not called for (non-pointer) testDecoder")
   967  	}
   968  
   969  	if s.T2 == nil {
   970  		t.Errorf("*testDecoder has not been allocated")
   971  	} else if !s.T2.called {
   972  		t.Errorf("DecodeRLP was not called for *testDecoder")
   973  	}
   974  
   975  	if s.T3 == nil || *s.T3 == nil {
   976  		t.Errorf("**testDecoder has not been allocated")
   977  	} else if !(*s.T3).called {
   978  		t.Errorf("DecodeRLP was not called for **testDecoder")
   979  	}
   980  }
   981  
   982  func TestDecodeDecoderNilPointer(t *testing.T) {
   983  	var s struct {
   984  		T1 *testDecoder `rlp:"nil"`
   985  		T2 *testDecoder
   986  	}
   987  	if err := Decode(bytes.NewReader(unhex("C2C002")), &s); err != nil {
   988  		t.Fatalf("Decode error: %v", err)
   989  	}
   990  	if s.T1 != nil {
   991  		t.Errorf("decoder T1 allocated for empty input (called: %v)", s.T1.called)
   992  	}
   993  	if s.T2 == nil || !s.T2.called {
   994  		t.Errorf("decoder T2 not allocated/called")
   995  	}
   996  }
   997  
   998  type byteDecoder byte
   999  
  1000  func (bd *byteDecoder) DecodeRLP(s *Stream) error {
  1001  	_, err := s.Uint()
  1002  	*bd = 255
  1003  	return err
  1004  }
  1005  
  1006  func (bd byteDecoder) called() bool {
  1007  	return bd == 255
  1008  }
  1009  
  1010  // This test verifies that the byte slice/byte array logic
  1011  // does not kick in for element types implementing Decoder.
  1012  func TestDecoderInByteSlice(t *testing.T) {
  1013  	var slice []byteDecoder
  1014  	if err := Decode(bytes.NewReader(unhex("C101")), &slice); err != nil {
  1015  		t.Errorf("unexpected Decode error %v", err)
  1016  	} else if !slice[0].called() {
  1017  		t.Errorf("DecodeRLP not called for slice element")
  1018  	}
  1019  
  1020  	var array [1]byteDecoder
  1021  	if err := Decode(bytes.NewReader(unhex("C101")), &array); err != nil {
  1022  		t.Errorf("unexpected Decode error %v", err)
  1023  	} else if !array[0].called() {
  1024  		t.Errorf("DecodeRLP not called for array element")
  1025  	}
  1026  }
  1027  
  1028  type unencodableDecoder func()
  1029  
  1030  func (f *unencodableDecoder) DecodeRLP(s *Stream) error {
  1031  	if _, err := s.List(); err != nil {
  1032  		return err
  1033  	}
  1034  	if err := s.ListEnd(); err != nil {
  1035  		return err
  1036  	}
  1037  	*f = func() {}
  1038  	return nil
  1039  }
  1040  
  1041  func TestDecoderFunc(t *testing.T) {
  1042  	var x func()
  1043  	if err := DecodeBytes([]byte{0xC0}, (*unencodableDecoder)(&x)); err != nil {
  1044  		t.Fatal(err)
  1045  	}
  1046  	x()
  1047  }
  1048  
  1049  // This tests the validity checks for fields with struct tag "optional".
  1050  func TestInvalidOptionalField(t *testing.T) {
  1051  	type (
  1052  		invalid1 struct {
  1053  			A uint `rlp:"optional"`
  1054  			B uint
  1055  		}
  1056  		invalid2 struct {
  1057  			T []uint `rlp:"tail,optional"`
  1058  		}
  1059  		invalid3 struct {
  1060  			T []uint `rlp:"optional,tail"`
  1061  		}
  1062  	)
  1063  
  1064  	tests := []struct {
  1065  		v   interface{}
  1066  		err string
  1067  	}{
  1068  		{v: new(invalid1), err: `rlp: invalid struct tag "" for rlp.invalid1.B (must be optional because preceding field "A" is optional)`},
  1069  		{v: new(invalid2), err: `rlp: invalid struct tag "optional" for rlp.invalid2.T (also has "tail" tag)`},
  1070  		{v: new(invalid3), err: `rlp: invalid struct tag "tail" for rlp.invalid3.T (also has "optional" tag)`},
  1071  	}
  1072  	for _, test := range tests {
  1073  		err := DecodeBytes(unhex("C20102"), test.v)
  1074  		if err == nil {
  1075  			t.Errorf("no error for %T", test.v)
  1076  		} else if err.Error() != test.err {
  1077  			t.Errorf("wrong error for %T: %v", test.v, err.Error())
  1078  		}
  1079  	}
  1080  }
  1081  
  1082  func ExampleDecode() {
  1083  	input, _ := hex.DecodeString("C90A1486666F6F626172")
  1084  
  1085  	type example struct {
  1086  		A, B   uint
  1087  		String string
  1088  	}
  1089  
  1090  	var s example
  1091  	err := Decode(bytes.NewReader(input), &s)
  1092  	if err != nil {
  1093  		fmt.Printf("Error: %v\n", err)
  1094  	} else {
  1095  		fmt.Printf("Decoded value: %#v\n", s)
  1096  	}
  1097  	// Output:
  1098  	// Decoded value: rlp.example{A:0xa, B:0x14, String:"foobar"}
  1099  }
  1100  
  1101  func ExampleDecode_structTagNil() {
  1102  	// In this example, we'll use the "nil" struct tag to change
  1103  	// how a pointer-typed field is decoded. The input contains an RLP
  1104  	// list of one element, an empty string.
  1105  	input := []byte{0xC1, 0x80}
  1106  
  1107  	// This type uses the normal rules.
  1108  	// The empty input string is decoded as a pointer to an empty Go string.
  1109  	var normalRules struct {
  1110  		String *string
  1111  	}
  1112  	Decode(bytes.NewReader(input), &normalRules)
  1113  	fmt.Printf("normal: String = %q\n", *normalRules.String)
  1114  
  1115  	// This type uses the struct tag.
  1116  	// The empty input string is decoded as a nil pointer.
  1117  	var withEmptyOK struct {
  1118  		String *string `rlp:"nil"`
  1119  	}
  1120  	Decode(bytes.NewReader(input), &withEmptyOK)
  1121  	fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String)
  1122  
  1123  	// Output:
  1124  	// normal: String = ""
  1125  	// with nil tag: String = <nil>
  1126  }
  1127  
  1128  func ExampleStream() {
  1129  	input, _ := hex.DecodeString("C90A1486666F6F626172")
  1130  	s := NewStream(bytes.NewReader(input), 0)
  1131  
  1132  	// Check what kind of value lies ahead
  1133  	kind, size, _ := s.Kind()
  1134  	fmt.Printf("Kind: %v size:%d\n", kind, size)
  1135  
  1136  	// Enter the list
  1137  	if _, err := s.List(); err != nil {
  1138  		fmt.Printf("List error: %v\n", err)
  1139  		return
  1140  	}
  1141  
  1142  	// Decode elements
  1143  	fmt.Println(s.Uint())
  1144  	fmt.Println(s.Uint())
  1145  	fmt.Println(s.Bytes())
  1146  
  1147  	// Acknowledge end of list
  1148  	if err := s.ListEnd(); err != nil {
  1149  		fmt.Printf("ListEnd error: %v\n", err)
  1150  	}
  1151  	// Output:
  1152  	// Kind: List size:9
  1153  	// 10 <nil>
  1154  	// 20 <nil>
  1155  	// [102 111 111 98 97 114] <nil>
  1156  }
  1157  
  1158  func BenchmarkDecodeUints(b *testing.B) {
  1159  	enc := encodeTestSlice(90000)
  1160  	b.SetBytes(int64(len(enc)))
  1161  	b.ReportAllocs()
  1162  	b.ResetTimer()
  1163  
  1164  	for i := 0; i < b.N; i++ {
  1165  		var s []uint
  1166  		r := bytes.NewReader(enc)
  1167  		if err := Decode(r, &s); err != nil {
  1168  			b.Fatalf("Decode error: %v", err)
  1169  		}
  1170  	}
  1171  }
  1172  
  1173  func BenchmarkDecodeUintsReused(b *testing.B) {
  1174  	enc := encodeTestSlice(100000)
  1175  	b.SetBytes(int64(len(enc)))
  1176  	b.ReportAllocs()
  1177  	b.ResetTimer()
  1178  
  1179  	var s []uint
  1180  	for i := 0; i < b.N; i++ {
  1181  		r := bytes.NewReader(enc)
  1182  		if err := Decode(r, &s); err != nil {
  1183  			b.Fatalf("Decode error: %v", err)
  1184  		}
  1185  	}
  1186  }
  1187  
  1188  func BenchmarkDecodeByteArrayStruct(b *testing.B) {
  1189  	enc, err := EncodeToBytes(&byteArrayStruct{})
  1190  	if err != nil {
  1191  		b.Fatal(err)
  1192  	}
  1193  	b.SetBytes(int64(len(enc)))
  1194  	b.ReportAllocs()
  1195  	b.ResetTimer()
  1196  
  1197  	var out byteArrayStruct
  1198  	for i := 0; i < b.N; i++ {
  1199  		if err := DecodeBytes(enc, &out); err != nil {
  1200  			b.Fatal(err)
  1201  		}
  1202  	}
  1203  }
  1204  
  1205  func BenchmarkDecodeBigInts(b *testing.B) {
  1206  	ints := make([]*big.Int, 200)
  1207  	for i := range ints {
  1208  		ints[i] = math.BigPow(2, int64(i))
  1209  	}
  1210  	enc, err := EncodeToBytes(ints)
  1211  	if err != nil {
  1212  		b.Fatal(err)
  1213  	}
  1214  	b.SetBytes(int64(len(enc)))
  1215  	b.ReportAllocs()
  1216  	b.ResetTimer()
  1217  
  1218  	var out []*big.Int
  1219  	for i := 0; i < b.N; i++ {
  1220  		if err := DecodeBytes(enc, &out); err != nil {
  1221  			b.Fatal(err)
  1222  		}
  1223  	}
  1224  }
  1225  
  1226  func encodeTestSlice(n uint) []byte {
  1227  	s := make([]uint, n)
  1228  	for i := uint(0); i < n; i++ {
  1229  		s[i] = i
  1230  	}
  1231  	b, err := EncodeToBytes(s)
  1232  	if err != nil {
  1233  		panic(fmt.Sprintf("encode error: %v", err))
  1234  	}
  1235  	return b
  1236  }
  1237  
  1238  func unhex(str string) []byte {
  1239  	b, err := hex.DecodeString(strings.ReplaceAll(str, " ", ""))
  1240  	if err != nil {
  1241  		panic(fmt.Sprintf("invalid hex string: %q", str))
  1242  	}
  1243  	return b
  1244  }