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