github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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  
    31  func TestStreamKind(t *testing.T) {
    32  	tests := []struct {
    33  		input    string
    34  		wantKind Kind
    35  		wantLen  uint64
    36  	}{
    37  		{"00", Byte, 0},
    38  		{"01", Byte, 0},
    39  		{"7F", Byte, 0},
    40  		{"80", String, 0},
    41  		{"B7", String, 55},
    42  		{"B90400", String, 1024},
    43  		{"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)},
    44  		{"C0", List, 0},
    45  		{"C8", List, 8},
    46  		{"F7", List, 55},
    47  		{"F90400", List, 1024},
    48  		{"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)},
    49  	}
    50  
    51  	for i, test := range tests {
    52  		// using plainReader to inhibit input limit errors.
    53  		s := NewStream(newPlainReader(unhex(test.input)), 0)
    54  		kind, len, err := s.Kind()
    55  		if err != nil {
    56  			t.Errorf("test %d: Kind returned error: %v", i, err)
    57  			continue
    58  		}
    59  		if kind != test.wantKind {
    60  			t.Errorf("test %d: kind mismatch: got %d, want %d", i, kind, test.wantKind)
    61  		}
    62  		if len != test.wantLen {
    63  			t.Errorf("test %d: len mismatch: got %d, want %d", i, len, test.wantLen)
    64  		}
    65  	}
    66  }
    67  
    68  func TestStreamErrors(t *testing.T) {
    69  	withoutInputLimit := func(b []byte) *Stream {
    70  		return NewStream(newPlainReader(b), 0)
    71  	}
    72  	withCustomInputLimit := func(limit uint64) func([]byte) *Stream {
    73  		return func(b []byte) *Stream {
    74  			return NewStream(bytes.NewReader(b), limit)
    75  		}
    76  	}
    77  
    78  	type calls []string
    79  	tests := []struct {
    80  		string
    81  		calls
    82  		newStream func([]byte) *Stream // uses bytes.Reader if nil
    83  		error     error
    84  	}{
    85  		{"C0", calls{"Bytes"}, nil, ErrExpectedString},
    86  		{"C0", calls{"Uint"}, nil, ErrExpectedString},
    87  		{"89000000000000000001", calls{"Uint"}, nil, errUintOverflow},
    88  		{"00", calls{"List"}, nil, ErrExpectedList},
    89  		{"80", calls{"List"}, nil, ErrExpectedList},
    90  		{"C0", calls{"List", "Uint"}, nil, EOL},
    91  		{"C8C9010101010101010101", calls{"List", "Kind"}, nil, ErrElemTooLarge},
    92  		{"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, nil, EOL},
    93  		{"00", calls{"ListEnd"}, nil, errNotInList},
    94  		{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
    95  
    96  		// Non-canonical integers (e.g. leading zero bytes).
    97  		{"00", calls{"Uint"}, nil, ErrCanonInt},
    98  		{"820002", calls{"Uint"}, nil, ErrCanonInt},
    99  		{"8133", calls{"Uint"}, nil, ErrCanonSize},
   100  		{"817F", calls{"Uint"}, nil, ErrCanonSize},
   101  		{"8180", calls{"Uint"}, nil, nil},
   102  
   103  		// Non-valid boolean
   104  		{"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")},
   105  
   106  		// Size tags must use the smallest possible encoding.
   107  		// Leading zero bytes in the size tag are also rejected.
   108  		{"8100", calls{"Uint"}, nil, ErrCanonSize},
   109  		{"8100", calls{"Bytes"}, nil, ErrCanonSize},
   110  		{"8101", calls{"Bytes"}, nil, ErrCanonSize},
   111  		{"817F", calls{"Bytes"}, nil, ErrCanonSize},
   112  		{"8180", calls{"Bytes"}, nil, nil},
   113  		{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   114  		{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   115  		{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   116  		{"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize},
   117  		{"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   118  		{"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   119  		{"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   120  		{"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize},
   121  
   122  		// Expected EOF
   123  		{"", calls{"Kind"}, nil, io.EOF},
   124  		{"", calls{"Uint"}, nil, io.EOF},
   125  		{"", calls{"List"}, nil, io.EOF},
   126  		{"8180", calls{"Uint", "Uint"}, nil, io.EOF},
   127  		{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
   128  
   129  		{"", calls{"List"}, withoutInputLimit, io.EOF},
   130  		{"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF},
   131  		{"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF},
   132  
   133  		// Input limit errors.
   134  		{"81", calls{"Bytes"}, nil, ErrValueTooLarge},
   135  		{"81", calls{"Uint"}, nil, ErrValueTooLarge},
   136  		{"81", calls{"Raw"}, nil, ErrValueTooLarge},
   137  		{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
   138  		{"C801", calls{"List"}, nil, ErrValueTooLarge},
   139  
   140  		// Test for list element size check overflow.
   141  		{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},
   142  
   143  		// Test for input limit overflow. Since we are counting the limit
   144  		// down toward zero in Stream.remaining, reading too far can overflow
   145  		// remaining to a large value, effectively disabling the limit.
   146  		{"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF},
   147  		{"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
   148  
   149  		// Check that the same calls are fine without a limit.
   150  		{"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   151  		{"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   152  
   153  		// Unexpected EOF. This only happens when there is
   154  		// no input limit, so the reader needs to be 'dumbed down'.
   155  		{"81", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   156  		{"81", calls{"Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   157  		{"BFFFFFFFFFFFFFFF", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   158  		{"C801", calls{"List", "Uint", "Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   159  
   160  		// This test verifies that the input position is advanced
   161  		// correctly when calling Bytes for empty strings. Kind can be called
   162  		// any number of times in between and doesn't advance.
   163  		{"C3808080", calls{
   164  			"List",  // enter the list
   165  			"Bytes", // past first element
   166  
   167  			"Kind", "Kind", "Kind", // this shouldn't advance
   168  
   169  			"Bytes", // past second element
   170  
   171  			"Kind", "Kind", // can't hurt to try
   172  
   173  			"Bytes", // past final element
   174  			"Bytes", // this one should fail
   175  		}, nil, EOL},
   176  	}
   177  
   178  testfor:
   179  	for i, test := range tests {
   180  		if test.newStream == nil {
   181  			test.newStream = func(b []byte) *Stream { return NewStream(bytes.NewReader(b), 0) }
   182  		}
   183  		s := test.newStream(unhex(test.string))
   184  		rs := reflect.ValueOf(s)
   185  		for j, call := range test.calls {
   186  			fval := rs.MethodByName(call)
   187  			ret := fval.Call(nil)
   188  			err := "<nil>"
   189  			if lastret := ret[len(ret)-1].Interface(); lastret != nil {
   190  				err = lastret.(error).Error()
   191  			}
   192  			if j == len(test.calls)-1 {
   193  				want := "<nil>"
   194  				if test.error != nil {
   195  					want = test.error.Error()
   196  				}
   197  				if err != want {
   198  					t.Log(test)
   199  					t.Errorf("test %d: last call (%s) error mismatch\ngot:  %s\nwant: %s",
   200  						i, call, err, test.error)
   201  				}
   202  			} else if err != "<nil>" {
   203  				t.Log(test)
   204  				t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err)
   205  				continue testfor
   206  			}
   207  		}
   208  	}
   209  }
   210  
   211  func TestStreamList(t *testing.T) {
   212  	s := NewStream(bytes.NewReader(unhex("C80102030405060708")), 0)
   213  
   214  	len, err := s.List()
   215  	if err != nil {
   216  		t.Fatalf("List error: %v", err)
   217  	}
   218  	if len != 8 {
   219  		t.Fatalf("List returned invalid length, got %d, want 8", len)
   220  	}
   221  
   222  	for i := uint64(1); i <= 8; i++ {
   223  		v, err := s.Uint()
   224  		if err != nil {
   225  			t.Fatalf("Uint error: %v", err)
   226  		}
   227  		if i != v {
   228  			t.Errorf("Uint returned wrong value, got %d, want %d", v, i)
   229  		}
   230  	}
   231  
   232  	if _, err := s.Uint(); err != EOL {
   233  		t.Errorf("Uint error mismatch, got %v, want %v", err, EOL)
   234  	}
   235  	if err = s.ListEnd(); err != nil {
   236  		t.Fatalf("ListEnd error: %v", err)
   237  	}
   238  }
   239  
   240  func TestStreamRaw(t *testing.T) {
   241  	s := NewStream(bytes.NewReader(unhex("C58401010101")), 0)
   242  	s.List()
   243  
   244  	want := unhex("8401010101")
   245  	raw, err := s.Raw()
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  	if !bytes.Equal(want, raw) {
   250  		t.Errorf("raw mismatch: got %x, want %x", raw, want)
   251  	}
   252  }
   253  
   254  func TestDecodeErrors(t *testing.T) {
   255  	r := bytes.NewReader(nil)
   256  
   257  	if err := Decode(r, nil); err != errDecodeIntoNil {
   258  		t.Errorf("Decode(r, nil) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   259  	}
   260  
   261  	var nilptr *struct{}
   262  	if err := Decode(r, nilptr); err != errDecodeIntoNil {
   263  		t.Errorf("Decode(r, nilptr) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   264  	}
   265  
   266  	if err := Decode(r, struct{}{}); err != errNoPointer {
   267  		t.Errorf("Decode(r, struct{}{}) error mismatch, got %q, want %q", err, errNoPointer)
   268  	}
   269  
   270  	expectErr := "rlp: type chan bool is not RLP-serializable"
   271  	if err := Decode(r, new(chan bool)); err == nil || err.Error() != expectErr {
   272  		t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
   273  	}
   274  
   275  	if err := Decode(r, new(uint)); err != io.EOF {
   276  		t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
   277  	}
   278  }
   279  
   280  type decodeTest struct {
   281  	input string
   282  	ptr   interface{}
   283  	value interface{}
   284  	error string
   285  }
   286  
   287  type simplestruct struct {
   288  	A uint
   289  	B string
   290  }
   291  
   292  type recstruct struct {
   293  	I     uint
   294  	Child *recstruct `rlp:"nil"`
   295  }
   296  
   297  type invalidTail1 struct {
   298  	A uint `rlp:"tail"`
   299  	B string
   300  }
   301  
   302  type invalidTail2 struct {
   303  	A uint
   304  	B string `rlp:"tail"`
   305  }
   306  
   307  type tailRaw struct {
   308  	A    uint
   309  	Tail []RawValue `rlp:"tail"`
   310  }
   311  
   312  type tailUint struct {
   313  	A    uint
   314  	Tail []uint `rlp:"tail"`
   315  }
   316  
   317  var (
   318  	veryBigInt = big.NewInt(0).Add(
   319  		big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
   320  		big.NewInt(0xFFFF),
   321  	)
   322  )
   323  
   324  var decodeTests = []decodeTest{
   325  	// booleans
   326  	{input: "01", ptr: new(bool), value: true},
   327  	{input: "80", ptr: new(bool), value: false},
   328  	{input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"},
   329  
   330  	// integers
   331  	{input: "05", ptr: new(uint32), value: uint32(5)},
   332  	{input: "80", ptr: new(uint32), value: uint32(0)},
   333  	{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
   334  	{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
   335  	{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
   336  	{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
   337  	{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
   338  	{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   339  	{input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   340  	{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   341  	{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   342  
   343  	// slices
   344  	{input: "C0", ptr: new([]uint), value: []uint{}},
   345  	{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
   346  	{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
   347  
   348  	// arrays
   349  	{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
   350  	{input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   351  	{input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   352  	{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
   353  	{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
   354  
   355  	// zero sized arrays
   356  	{input: "C0", ptr: new([0]uint), value: [0]uint{}},
   357  	{input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"},
   358  
   359  	// byte slices
   360  	{input: "01", ptr: new([]byte), value: []byte{1}},
   361  	{input: "80", ptr: new([]byte), value: []byte{}},
   362  	{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
   363  	{input: "C0", ptr: new([]byte), error: "rlp: expected input string or byte for []uint8"},
   364  	{input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"},
   365  
   366  	// byte arrays
   367  	{input: "02", ptr: new([1]byte), value: [1]byte{2}},
   368  	{input: "8180", ptr: new([1]byte), value: [1]byte{128}},
   369  	{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
   370  
   371  	// byte array errors
   372  	{input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   373  	{input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   374  	{input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   375  	{input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   376  	{input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   377  	{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"},
   378  	{input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   379  	{input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   380  
   381  	// zero sized byte arrays
   382  	{input: "80", ptr: new([0]byte), value: [0]byte{}},
   383  	{input: "01", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   384  	{input: "8101", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   385  
   386  	// strings
   387  	{input: "00", ptr: new(string), value: "\000"},
   388  	{input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"},
   389  	{input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"},
   390  
   391  	// big ints
   392  	{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
   393  	{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
   394  	{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
   395  	{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
   396  	{input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   397  	{input: "8105", ptr: new(big.Int), error: "rlp: non-canonical size information for *big.Int"},
   398  
   399  	// structs
   400  	{
   401  		input: "C50583343434",
   402  		ptr:   new(simplestruct),
   403  		value: simplestruct{5, "444"},
   404  	},
   405  	{
   406  		input: "C601C402C203C0",
   407  		ptr:   new(recstruct),
   408  		value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
   409  	},
   410  
   411  	// struct errors
   412  	{
   413  		input: "C0",
   414  		ptr:   new(simplestruct),
   415  		error: "rlp: too few elements for rlp.simplestruct",
   416  	},
   417  	{
   418  		input: "C105",
   419  		ptr:   new(simplestruct),
   420  		error: "rlp: too few elements for rlp.simplestruct",
   421  	},
   422  	{
   423  		input: "C7C50583343434C0",
   424  		ptr:   new([]*simplestruct),
   425  		error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]",
   426  	},
   427  	{
   428  		input: "83222222",
   429  		ptr:   new(simplestruct),
   430  		error: "rlp: expected input list for rlp.simplestruct",
   431  	},
   432  	{
   433  		input: "C3010101",
   434  		ptr:   new(simplestruct),
   435  		error: "rlp: input list has too many elements for rlp.simplestruct",
   436  	},
   437  	{
   438  		input: "C501C3C00000",
   439  		ptr:   new(recstruct),
   440  		error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I",
   441  	},
   442  	{
   443  		input: "C0",
   444  		ptr:   new(invalidTail1),
   445  		error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail1.A (must be on last field)",
   446  	},
   447  	{
   448  		input: "C0",
   449  		ptr:   new(invalidTail2),
   450  		error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail2.B (field type is not slice)",
   451  	},
   452  	{
   453  		input: "C50102C20102",
   454  		ptr:   new(tailUint),
   455  		error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]",
   456  	},
   457  
   458  	// struct tag "tail"
   459  	{
   460  		input: "C3010203",
   461  		ptr:   new(tailRaw),
   462  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}},
   463  	},
   464  	{
   465  		input: "C20102",
   466  		ptr:   new(tailRaw),
   467  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}},
   468  	},
   469  	{
   470  		input: "C101",
   471  		ptr:   new(tailRaw),
   472  		value: tailRaw{A: 1, Tail: []RawValue{}},
   473  	},
   474  
   475  	// RawValue
   476  	{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
   477  	{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
   478  	{input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}},
   479  
   480  	// pointers
   481  	{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
   482  	{input: "80", ptr: new(*uint), value: uintp(0)},
   483  	{input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"},
   484  	{input: "07", ptr: new(*uint), value: uintp(7)},
   485  	{input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"},
   486  	{input: "8180", ptr: new(*uint), value: uintp(0x80)},
   487  	{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
   488  	{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
   489  
   490  	// check that input position is advanced also for empty values.
   491  	{input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}},
   492  
   493  	// interface{}
   494  	{input: "00", ptr: new(interface{}), value: []byte{0}},
   495  	{input: "01", ptr: new(interface{}), value: []byte{1}},
   496  	{input: "80", ptr: new(interface{}), value: []byte{}},
   497  	{input: "850505050505", ptr: new(interface{}), value: []byte{5, 5, 5, 5, 5}},
   498  	{input: "C0", ptr: new(interface{}), value: []interface{}{}},
   499  	{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
   500  	{
   501  		input: "C3010203",
   502  		ptr:   new([]io.Reader),
   503  		error: "rlp: type io.Reader is not RLP-serializable",
   504  	},
   505  
   506  	// fuzzer crashes
   507  	{
   508  		input: "c330f9c030f93030ce3030303030303030bd303030303030",
   509  		ptr:   new(interface{}),
   510  		error: "rlp: element is larger than containing list",
   511  	},
   512  }
   513  
   514  func uintp(i uint) *uint { return &i }
   515  
   516  func runTests(t *testing.T, decode func([]byte, interface{}) error) {
   517  	for i, test := range decodeTests {
   518  		input, err := hex.DecodeString(test.input)
   519  		if err != nil {
   520  			t.Errorf("test %d: invalid hex input %q", i, test.input)
   521  			continue
   522  		}
   523  		err = decode(input, test.ptr)
   524  		if err != nil && test.error == "" {
   525  			t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
   526  				i, err, test.ptr, test.input)
   527  			continue
   528  		}
   529  		if test.error != "" && fmt.Sprint(err) != test.error {
   530  			t.Errorf("test %d: Decode error mismatch\ngot  %v\nwant %v\ndecoding into %T\ninput %q",
   531  				i, err, test.error, test.ptr, test.input)
   532  			continue
   533  		}
   534  		deref := reflect.ValueOf(test.ptr).Elem().Interface()
   535  		if err == nil && !reflect.DeepEqual(deref, test.value) {
   536  			t.Errorf("test %d: value mismatch\ngot  %#v\nwant %#v\ndecoding into %T\ninput %q",
   537  				i, deref, test.value, test.ptr, test.input)
   538  		}
   539  	}
   540  }
   541  
   542  func TestDecodeWithByteReader(t *testing.T) {
   543  	runTests(t, func(input []byte, into interface{}) error {
   544  		return Decode(bytes.NewReader(input), into)
   545  	})
   546  }
   547  
   548  // plainReader reads from a byte slice but does not
   549  // implement ReadByte. It is also not recognized by the
   550  // size validation. This is useful to test how the decoder
   551  // behaves on a non-buffered input stream.
   552  type plainReader []byte
   553  
   554  func newPlainReader(b []byte) io.Reader {
   555  	return (*plainReader)(&b)
   556  }
   557  
   558  func (r *plainReader) Read(buf []byte) (n int, err error) {
   559  	if len(*r) == 0 {
   560  		return 0, io.EOF
   561  	}
   562  	n = copy(buf, *r)
   563  	*r = (*r)[n:]
   564  	return n, nil
   565  }
   566  
   567  func TestDecodeWithNonByteReader(t *testing.T) {
   568  	runTests(t, func(input []byte, into interface{}) error {
   569  		return Decode(newPlainReader(input), into)
   570  	})
   571  }
   572  
   573  func TestDecodeStreamReset(t *testing.T) {
   574  	s := NewStream(nil, 0)
   575  	runTests(t, func(input []byte, into interface{}) error {
   576  		s.Reset(bytes.NewReader(input), 0)
   577  		return s.Decode(into)
   578  	})
   579  }
   580  
   581  type testDecoder struct{ called bool }
   582  
   583  func (t *testDecoder) DecodeRLP(s *Stream) error {
   584  	if _, err := s.Uint(); err != nil {
   585  		return err
   586  	}
   587  	t.called = true
   588  	return nil
   589  }
   590  
   591  func TestDecodeDecoder(t *testing.T) {
   592  	var s struct {
   593  		T1 testDecoder
   594  		T2 *testDecoder
   595  		T3 **testDecoder
   596  	}
   597  	if err := Decode(bytes.NewReader(unhex("C3010203")), &s); err != nil {
   598  		t.Fatalf("Decode error: %v", err)
   599  	}
   600  
   601  	if !s.T1.called {
   602  		t.Errorf("DecodeRLP was not called for (non-pointer) testDecoder")
   603  	}
   604  
   605  	if s.T2 == nil {
   606  		t.Errorf("*testDecoder has not been allocated")
   607  	} else if !s.T2.called {
   608  		t.Errorf("DecodeRLP was not called for *testDecoder")
   609  	}
   610  
   611  	if s.T3 == nil || *s.T3 == nil {
   612  		t.Errorf("**testDecoder has not been allocated")
   613  	} else if !(*s.T3).called {
   614  		t.Errorf("DecodeRLP was not called for **testDecoder")
   615  	}
   616  }
   617  
   618  type byteDecoder byte
   619  
   620  func (bd *byteDecoder) DecodeRLP(s *Stream) error {
   621  	_, err := s.Uint()
   622  	*bd = 255
   623  	return err
   624  }
   625  
   626  func (bd byteDecoder) called() bool {
   627  	return bd == 255
   628  }
   629  
   630  // This test verifies that the byte slice/byte array logic
   631  // does not kick in for element types implementing Decoder.
   632  func TestDecoderInByteSlice(t *testing.T) {
   633  	var slice []byteDecoder
   634  	if err := Decode(bytes.NewReader(unhex("C101")), &slice); err != nil {
   635  		t.Errorf("unexpected Decode error %v", err)
   636  	} else if !slice[0].called() {
   637  		t.Errorf("DecodeRLP not called for slice element")
   638  	}
   639  
   640  	var array [1]byteDecoder
   641  	if err := Decode(bytes.NewReader(unhex("C101")), &array); err != nil {
   642  		t.Errorf("unexpected Decode error %v", err)
   643  	} else if !array[0].called() {
   644  		t.Errorf("DecodeRLP not called for array element")
   645  	}
   646  }
   647  
   648  func ExampleDecode() {
   649  	input, _ := hex.DecodeString("C90A1486666F6F626172")
   650  
   651  	type example struct {
   652  		A, B    uint
   653  		private uint // private fields are ignored
   654  		String  string
   655  	}
   656  
   657  	var s example
   658  	err := Decode(bytes.NewReader(input), &s)
   659  	if err != nil {
   660  		fmt.Printf("Error: %v\n", err)
   661  	} else {
   662  		fmt.Printf("Decoded value: %#v\n", s)
   663  	}
   664  	// Output:
   665  	// Decoded value: rlp.example{A:0xa, B:0x14, private:0x0, String:"foobar"}
   666  }
   667  
   668  func ExampleDecode_structTagNil() {
   669  	// In this example, we'll use the "nil" struct tag to change
   670  	// how a pointer-typed field is decoded. The input contains an RLP
   671  	// list of one element, an empty string.
   672  	input := []byte{0xC1, 0x80}
   673  
   674  	// This type uses the normal rules.
   675  	// The empty input string is decoded as a pointer to an empty Go string.
   676  	var normalRules struct {
   677  		String *string
   678  	}
   679  	Decode(bytes.NewReader(input), &normalRules)
   680  	fmt.Printf("normal: String = %q\n", *normalRules.String)
   681  
   682  	// This type uses the struct tag.
   683  	// The empty input string is decoded as a nil pointer.
   684  	var withEmptyOK struct {
   685  		String *string `rlp:"nil"`
   686  	}
   687  	Decode(bytes.NewReader(input), &withEmptyOK)
   688  	fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String)
   689  
   690  	// Output:
   691  	// normal: String = ""
   692  	// with nil tag: String = <nil>
   693  }
   694  
   695  func ExampleStream() {
   696  	input, _ := hex.DecodeString("C90A1486666F6F626172")
   697  	s := NewStream(bytes.NewReader(input), 0)
   698  
   699  	// Check what kind of value lies ahead
   700  	kind, size, _ := s.Kind()
   701  	fmt.Printf("Kind: %v size:%d\n", kind, size)
   702  
   703  	// Enter the list
   704  	if _, err := s.List(); err != nil {
   705  		fmt.Printf("List error: %v\n", err)
   706  		return
   707  	}
   708  
   709  	// Decode elements
   710  	fmt.Println(s.Uint())
   711  	fmt.Println(s.Uint())
   712  	fmt.Println(s.Bytes())
   713  
   714  	// Acknowledge end of list
   715  	if err := s.ListEnd(); err != nil {
   716  		fmt.Printf("ListEnd error: %v\n", err)
   717  	}
   718  	// Output:
   719  	// Kind: List size:9
   720  	// 10 <nil>
   721  	// 20 <nil>
   722  	// [102 111 111 98 97 114] <nil>
   723  }
   724  
   725  func BenchmarkDecode(b *testing.B) {
   726  	enc := encodeTestSlice(90000)
   727  	b.SetBytes(int64(len(enc)))
   728  	b.ReportAllocs()
   729  	b.ResetTimer()
   730  
   731  	for i := 0; i < b.N; i++ {
   732  		var s []uint
   733  		r := bytes.NewReader(enc)
   734  		if err := Decode(r, &s); err != nil {
   735  			b.Fatalf("Decode error: %v", err)
   736  		}
   737  	}
   738  }
   739  
   740  func BenchmarkDecodeIntSliceReuse(b *testing.B) {
   741  	enc := encodeTestSlice(100000)
   742  	b.SetBytes(int64(len(enc)))
   743  	b.ReportAllocs()
   744  	b.ResetTimer()
   745  
   746  	var s []uint
   747  	for i := 0; i < b.N; i++ {
   748  		r := bytes.NewReader(enc)
   749  		if err := Decode(r, &s); err != nil {
   750  			b.Fatalf("Decode error: %v", err)
   751  		}
   752  	}
   753  }
   754  
   755  func encodeTestSlice(n uint) []byte {
   756  	s := make([]uint, n)
   757  	for i := uint(0); i < n; i++ {
   758  		s[i] = i
   759  	}
   760  	b, err := EncodeToBytes(s)
   761  	if err != nil {
   762  		panic(fmt.Sprintf("encode error: %v", err))
   763  	}
   764  	return b
   765  }
   766  
   767  func unhex(str string) []byte {
   768  	b, err := hex.DecodeString(strings.Replace(str, " ", "", -1))
   769  	if err != nil {
   770  		panic(fmt.Sprintf("invalid hex string: %q", str))
   771  	}
   772  	return b
   773  }