github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rlp/decode_test.go (about)

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