github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/rlp/decode_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:45</date>
    10  //</624342662916345856>
    11  
    12  
    13  package rlp
    14  
    15  import (
    16  	"bytes"
    17  	"encoding/hex"
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  	"math/big"
    22  	"reflect"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  func TestStreamKind(t *testing.T) {
    28  	tests := []struct {
    29  		input    string
    30  		wantKind Kind
    31  		wantLen  uint64
    32  	}{
    33  		{"00", Byte, 0},
    34  		{"01", Byte, 0},
    35  		{"7F", Byte, 0},
    36  		{"80", String, 0},
    37  		{"B7", String, 55},
    38  		{"B90400", String, 1024},
    39  		{"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)},
    40  		{"C0", List, 0},
    41  		{"C8", List, 8},
    42  		{"F7", List, 55},
    43  		{"F90400", List, 1024},
    44  		{"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)},
    45  	}
    46  
    47  	for i, test := range tests {
    48  //使用普通读卡器抑制输入极限误差。
    49  		s := NewStream(newPlainReader(unhex(test.input)), 0)
    50  		kind, len, err := s.Kind()
    51  		if err != nil {
    52  			t.Errorf("test %d: Kind returned error: %v", i, err)
    53  			continue
    54  		}
    55  		if kind != test.wantKind {
    56  			t.Errorf("test %d: kind mismatch: got %d, want %d", i, kind, test.wantKind)
    57  		}
    58  		if len != test.wantLen {
    59  			t.Errorf("test %d: len mismatch: got %d, want %d", i, len, test.wantLen)
    60  		}
    61  	}
    62  }
    63  
    64  func TestNewListStream(t *testing.T) {
    65  	ls := NewListStream(bytes.NewReader(unhex("0101010101")), 3)
    66  	if k, size, err := ls.Kind(); k != List || size != 3 || err != nil {
    67  		t.Errorf("Kind() returned (%v, %d, %v), expected (List, 3, nil)", k, size, err)
    68  	}
    69  	if size, err := ls.List(); size != 3 || err != nil {
    70  		t.Errorf("List() returned (%d, %v), expected (3, nil)", size, err)
    71  	}
    72  	for i := 0; i < 3; i++ {
    73  		if val, err := ls.Uint(); val != 1 || err != nil {
    74  			t.Errorf("Uint() returned (%d, %v), expected (1, nil)", val, err)
    75  		}
    76  	}
    77  	if err := ls.ListEnd(); err != nil {
    78  		t.Errorf("ListEnd() returned %v, expected (3, nil)", err)
    79  	}
    80  }
    81  
    82  func TestStreamErrors(t *testing.T) {
    83  	withoutInputLimit := func(b []byte) *Stream {
    84  		return NewStream(newPlainReader(b), 0)
    85  	}
    86  	withCustomInputLimit := func(limit uint64) func([]byte) *Stream {
    87  		return func(b []byte) *Stream {
    88  			return NewStream(bytes.NewReader(b), limit)
    89  		}
    90  	}
    91  
    92  	type calls []string
    93  	tests := []struct {
    94  		string
    95  		calls
    96  newStream func([]byte) *Stream //使用bytes.reader(如果为nil)
    97  		error     error
    98  	}{
    99  		{"C0", calls{"Bytes"}, nil, ErrExpectedString},
   100  		{"C0", calls{"Uint"}, nil, ErrExpectedString},
   101  		{"89000000000000000001", calls{"Uint"}, nil, errUintOverflow},
   102  		{"00", calls{"List"}, nil, ErrExpectedList},
   103  		{"80", calls{"List"}, nil, ErrExpectedList},
   104  		{"C0", calls{"List", "Uint"}, nil, EOL},
   105  		{"C8C9010101010101010101", calls{"List", "Kind"}, nil, ErrElemTooLarge},
   106  		{"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, nil, EOL},
   107  		{"00", calls{"ListEnd"}, nil, errNotInList},
   108  		{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
   109  
   110  //非规范整数(例如前导零字节)。
   111  		{"00", calls{"Uint"}, nil, ErrCanonInt},
   112  		{"820002", calls{"Uint"}, nil, ErrCanonInt},
   113  		{"8133", calls{"Uint"}, nil, ErrCanonSize},
   114  		{"817F", calls{"Uint"}, nil, ErrCanonSize},
   115  		{"8180", calls{"Uint"}, nil, nil},
   116  
   117  //无效布尔值
   118  		{"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")},
   119  
   120  //大小标记必须使用尽可能小的编码。
   121  //大小标记中的前导零字节也被拒绝。
   122  		{"8100", calls{"Uint"}, nil, ErrCanonSize},
   123  		{"8100", calls{"Bytes"}, nil, ErrCanonSize},
   124  		{"8101", calls{"Bytes"}, nil, ErrCanonSize},
   125  		{"817F", calls{"Bytes"}, nil, ErrCanonSize},
   126  		{"8180", calls{"Bytes"}, nil, nil},
   127  		{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   128  		{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   129  		{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   130  		{"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize},
   131  		{"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   132  		{"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   133  		{"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   134  		{"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize},
   135  
   136  //预期EOF
   137  		{"", calls{"Kind"}, nil, io.EOF},
   138  		{"", calls{"Uint"}, nil, io.EOF},
   139  		{"", calls{"List"}, nil, io.EOF},
   140  		{"8180", calls{"Uint", "Uint"}, nil, io.EOF},
   141  		{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
   142  
   143  		{"", calls{"List"}, withoutInputLimit, io.EOF},
   144  		{"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF},
   145  		{"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF},
   146  
   147  //输入限制错误。
   148  		{"81", calls{"Bytes"}, nil, ErrValueTooLarge},
   149  		{"81", calls{"Uint"}, nil, ErrValueTooLarge},
   150  		{"81", calls{"Raw"}, nil, ErrValueTooLarge},
   151  		{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
   152  		{"C801", calls{"List"}, nil, ErrValueTooLarge},
   153  
   154  //测试列表元素大小检查溢出。
   155  		{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},
   156  
   157  //输入极限溢出测试。因为我们在计算限额
   158  //在stream.remaining中向下接近零,如果读数太远,则可能溢出
   159  //保留为大值,有效禁用限制。
   160  		{"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF},
   161  		{"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
   162  
   163  //检查相同的呼叫是否可以无限制地进行。
   164  		{"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   165  		{"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   166  
   167  //意外的EOF。只有当
   168  //没有输入限制,所以读卡器需要“减速”。
   169  		{"81", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   170  		{"81", calls{"Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   171  		{"BFFFFFFFFFFFFFFF", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   172  		{"C801", calls{"List", "Uint", "Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   173  
   174  //此测试验证输入位置是否提前。
   175  //为空字符串调用字节时正确。可以称之为
   176  //和之间的任何次数都不会提前。
   177  		{"C3808080", calls{
   178  "List",  //进入列表
   179  "Bytes", //超过第一个元素
   180  
   181  "Kind", "Kind", "Kind", //这不应该提前
   182  
   183  "Bytes", //过去的第二个元素
   184  
   185  "Kind", "Kind", //尝试是不会受伤的
   186  
   187  "Bytes", //过去的最后一个元素
   188  "Bytes", //这个应该失败
   189  		}, nil, EOL},
   190  	}
   191  
   192  testfor:
   193  	for i, test := range tests {
   194  		if test.newStream == nil {
   195  			test.newStream = func(b []byte) *Stream { return NewStream(bytes.NewReader(b), 0) }
   196  		}
   197  		s := test.newStream(unhex(test.string))
   198  		rs := reflect.ValueOf(s)
   199  		for j, call := range test.calls {
   200  			fval := rs.MethodByName(call)
   201  			ret := fval.Call(nil)
   202  			err := "<nil>"
   203  			if lastret := ret[len(ret)-1].Interface(); lastret != nil {
   204  				err = lastret.(error).Error()
   205  			}
   206  			if j == len(test.calls)-1 {
   207  				want := "<nil>"
   208  				if test.error != nil {
   209  					want = test.error.Error()
   210  				}
   211  				if err != want {
   212  					t.Log(test)
   213  					t.Errorf("test %d: last call (%s) error mismatch\ngot:  %s\nwant: %s",
   214  						i, call, err, test.error)
   215  				}
   216  			} else if err != "<nil>" {
   217  				t.Log(test)
   218  				t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err)
   219  				continue testfor
   220  			}
   221  		}
   222  	}
   223  }
   224  
   225  func TestStreamList(t *testing.T) {
   226  	s := NewStream(bytes.NewReader(unhex("C80102030405060708")), 0)
   227  
   228  	len, err := s.List()
   229  	if err != nil {
   230  		t.Fatalf("List error: %v", err)
   231  	}
   232  	if len != 8 {
   233  		t.Fatalf("List returned invalid length, got %d, want 8", len)
   234  	}
   235  
   236  	for i := uint64(1); i <= 8; i++ {
   237  		v, err := s.Uint()
   238  		if err != nil {
   239  			t.Fatalf("Uint error: %v", err)
   240  		}
   241  		if i != v {
   242  			t.Errorf("Uint returned wrong value, got %d, want %d", v, i)
   243  		}
   244  	}
   245  
   246  	if _, err := s.Uint(); err != EOL {
   247  		t.Errorf("Uint error mismatch, got %v, want %v", err, EOL)
   248  	}
   249  	if err = s.ListEnd(); err != nil {
   250  		t.Fatalf("ListEnd error: %v", err)
   251  	}
   252  }
   253  
   254  func TestStreamRaw(t *testing.T) {
   255  	tests := []struct {
   256  		input  string
   257  		output string
   258  	}{
   259  		{
   260  			"C58401010101",
   261  			"8401010101",
   262  		},
   263  		{
   264  			"F842B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   265  			"B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   266  		},
   267  	}
   268  	for i, tt := range tests {
   269  		s := NewStream(bytes.NewReader(unhex(tt.input)), 0)
   270  		s.List()
   271  
   272  		want := unhex(tt.output)
   273  		raw, err := s.Raw()
   274  		if err != nil {
   275  			t.Fatal(err)
   276  		}
   277  		if !bytes.Equal(want, raw) {
   278  			t.Errorf("test %d: raw mismatch: got %x, want %x", i, raw, want)
   279  		}
   280  	}
   281  }
   282  
   283  func TestDecodeErrors(t *testing.T) {
   284  	r := bytes.NewReader(nil)
   285  
   286  	if err := Decode(r, nil); err != errDecodeIntoNil {
   287  		t.Errorf("Decode(r, nil) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   288  	}
   289  
   290  	var nilptr *struct{}
   291  	if err := Decode(r, nilptr); err != errDecodeIntoNil {
   292  		t.Errorf("Decode(r, nilptr) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   293  	}
   294  
   295  	if err := Decode(r, struct{}{}); err != errNoPointer {
   296  		t.Errorf("Decode(r, struct{}{}) error mismatch, got %q, want %q", err, errNoPointer)
   297  	}
   298  
   299  	expectErr := "rlp: type chan bool is not RLP-serializable"
   300  	if err := Decode(r, new(chan bool)); err == nil || err.Error() != expectErr {
   301  		t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
   302  	}
   303  
   304  	if err := Decode(r, new(uint)); err != io.EOF {
   305  		t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
   306  	}
   307  }
   308  
   309  type decodeTest struct {
   310  	input string
   311  	ptr   interface{}
   312  	value interface{}
   313  	error string
   314  }
   315  
   316  type simplestruct struct {
   317  	A uint
   318  	B string
   319  }
   320  
   321  type recstruct struct {
   322  	I     uint
   323  	Child *recstruct `rlp:"nil"`
   324  }
   325  
   326  type invalidTail1 struct {
   327  	A uint `rlp:"tail"`
   328  	B string
   329  }
   330  
   331  type invalidTail2 struct {
   332  	A uint
   333  	B string `rlp:"tail"`
   334  }
   335  
   336  type tailRaw struct {
   337  	A    uint
   338  	Tail []RawValue `rlp:"tail"`
   339  }
   340  
   341  type tailUint struct {
   342  	A    uint
   343  	Tail []uint `rlp:"tail"`
   344  }
   345  
   346  var (
   347  	veryBigInt = big.NewInt(0).Add(
   348  		big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
   349  		big.NewInt(0xFFFF),
   350  	)
   351  )
   352  
   353  type hasIgnoredField struct {
   354  	A uint
   355  	B uint `rlp:"-"`
   356  	C uint
   357  }
   358  
   359  var decodeTests = []decodeTest{
   360  //布尔运算
   361  	{input: "01", ptr: new(bool), value: true},
   362  	{input: "80", ptr: new(bool), value: false},
   363  	{input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"},
   364  
   365  //整数
   366  	{input: "05", ptr: new(uint32), value: uint32(5)},
   367  	{input: "80", ptr: new(uint32), value: uint32(0)},
   368  	{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
   369  	{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
   370  	{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
   371  	{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
   372  	{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
   373  	{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   374  	{input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   375  	{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   376  	{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   377  
   378  //片
   379  	{input: "C0", ptr: new([]uint), value: []uint{}},
   380  	{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
   381  	{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
   382  
   383  //数组
   384  	{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
   385  	{input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   386  	{input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   387  	{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
   388  	{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
   389  
   390  //零大小数组
   391  	{input: "C0", ptr: new([0]uint), value: [0]uint{}},
   392  	{input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"},
   393  
   394  //字节切片
   395  	{input: "01", ptr: new([]byte), value: []byte{1}},
   396  	{input: "80", ptr: new([]byte), value: []byte{}},
   397  	{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
   398  	{input: "C0", ptr: new([]byte), error: "rlp: expected input string or byte for []uint8"},
   399  	{input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"},
   400  
   401  //字节数组
   402  	{input: "02", ptr: new([1]byte), value: [1]byte{2}},
   403  	{input: "8180", ptr: new([1]byte), value: [1]byte{128}},
   404  	{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
   405  
   406  //字节数组错误
   407  	{input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   408  	{input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   409  	{input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   410  	{input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   411  	{input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   412  	{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"},
   413  	{input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   414  	{input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   415  
   416  //零大小字节数组
   417  	{input: "80", ptr: new([0]byte), value: [0]byte{}},
   418  	{input: "01", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   419  	{input: "8101", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   420  
   421  //串
   422  	{input: "00", ptr: new(string), value: "\000"},
   423  	{input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"},
   424  	{input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"},
   425  
   426  //大企业
   427  	{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
   428  	{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
   429  {input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, //非指针也有效
   430  	{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
   431  	{input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   432  	{input: "8105", ptr: new(big.Int), error: "rlp: non-canonical size information for *big.Int"},
   433  
   434  //结构体
   435  	{
   436  		input: "C50583343434",
   437  		ptr:   new(simplestruct),
   438  		value: simplestruct{5, "444"},
   439  	},
   440  	{
   441  		input: "C601C402C203C0",
   442  		ptr:   new(recstruct),
   443  		value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
   444  	},
   445  
   446  //结构错误
   447  	{
   448  		input: "C0",
   449  		ptr:   new(simplestruct),
   450  		error: "rlp: too few elements for rlp.simplestruct",
   451  	},
   452  	{
   453  		input: "C105",
   454  		ptr:   new(simplestruct),
   455  		error: "rlp: too few elements for rlp.simplestruct",
   456  	},
   457  	{
   458  		input: "C7C50583343434C0",
   459  		ptr:   new([]*simplestruct),
   460  		error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]",
   461  	},
   462  	{
   463  		input: "83222222",
   464  		ptr:   new(simplestruct),
   465  		error: "rlp: expected input list for rlp.simplestruct",
   466  	},
   467  	{
   468  		input: "C3010101",
   469  		ptr:   new(simplestruct),
   470  		error: "rlp: input list has too many elements for rlp.simplestruct",
   471  	},
   472  	{
   473  		input: "C501C3C00000",
   474  		ptr:   new(recstruct),
   475  		error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I",
   476  	},
   477  	{
   478  		input: "C0",
   479  		ptr:   new(invalidTail1),
   480  		error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail1.A (must be on last field)",
   481  	},
   482  	{
   483  		input: "C0",
   484  		ptr:   new(invalidTail2),
   485  		error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail2.B (field type is not slice)",
   486  	},
   487  	{
   488  		input: "C50102C20102",
   489  		ptr:   new(tailUint),
   490  		error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]",
   491  	},
   492  
   493  //结构标记“tail”
   494  	{
   495  		input: "C3010203",
   496  		ptr:   new(tailRaw),
   497  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}},
   498  	},
   499  	{
   500  		input: "C20102",
   501  		ptr:   new(tailRaw),
   502  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}},
   503  	},
   504  	{
   505  		input: "C101",
   506  		ptr:   new(tailRaw),
   507  		value: tailRaw{A: 1, Tail: []RawValue{}},
   508  	},
   509  
   510  //“标签”结构
   511  	{
   512  		input: "C20102",
   513  		ptr:   new(hasIgnoredField),
   514  		value: hasIgnoredField{A: 1, C: 2},
   515  	},
   516  
   517  //RAW值
   518  	{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
   519  	{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
   520  	{input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}},
   521  
   522  //指针
   523  	{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
   524  	{input: "80", ptr: new(*uint), value: uintp(0)},
   525  	{input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"},
   526  	{input: "07", ptr: new(*uint), value: uintp(7)},
   527  	{input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"},
   528  	{input: "8180", ptr: new(*uint), value: uintp(0x80)},
   529  	{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
   530  	{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
   531  
   532  //检查输入位置是否也为空值。
   533  	{input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}},
   534  
   535  //接口{}
   536  	{input: "00", ptr: new(interface{}), value: []byte{0}},
   537  	{input: "01", ptr: new(interface{}), value: []byte{1}},
   538  	{input: "80", ptr: new(interface{}), value: []byte{}},
   539  	{input: "850505050505", ptr: new(interface{}), value: []byte{5, 5, 5, 5, 5}},
   540  	{input: "C0", ptr: new(interface{}), value: []interface{}{}},
   541  	{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
   542  	{
   543  		input: "C3010203",
   544  		ptr:   new([]io.Reader),
   545  		error: "rlp: type io.Reader is not RLP-serializable",
   546  	},
   547  
   548  //模糊崩溃
   549  	{
   550  		input: "c330f9c030f93030ce3030303030303030bd303030303030",
   551  		ptr:   new(interface{}),
   552  		error: "rlp: element is larger than containing list",
   553  	},
   554  }
   555  
   556  func uintp(i uint) *uint { return &i }
   557  
   558  func runTests(t *testing.T, decode func([]byte, interface{}) error) {
   559  	for i, test := range decodeTests {
   560  		input, err := hex.DecodeString(test.input)
   561  		if err != nil {
   562  			t.Errorf("test %d: invalid hex input %q", i, test.input)
   563  			continue
   564  		}
   565  		err = decode(input, test.ptr)
   566  		if err != nil && test.error == "" {
   567  			t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
   568  				i, err, test.ptr, test.input)
   569  			continue
   570  		}
   571  		if test.error != "" && fmt.Sprint(err) != test.error {
   572  			t.Errorf("test %d: Decode error mismatch\ngot  %v\nwant %v\ndecoding into %T\ninput %q",
   573  				i, err, test.error, test.ptr, test.input)
   574  			continue
   575  		}
   576  		deref := reflect.ValueOf(test.ptr).Elem().Interface()
   577  		if err == nil && !reflect.DeepEqual(deref, test.value) {
   578  			t.Errorf("test %d: value mismatch\ngot  %#v\nwant %#v\ndecoding into %T\ninput %q",
   579  				i, deref, test.value, test.ptr, test.input)
   580  		}
   581  	}
   582  }
   583  
   584  func TestDecodeWithByteReader(t *testing.T) {
   585  	runTests(t, func(input []byte, into interface{}) error {
   586  		return Decode(bytes.NewReader(input), into)
   587  	})
   588  }
   589  
   590  //PlainReader从字节片读取,但不读取
   591  //实现readbyte。它也不被
   592  //尺寸验证。这对于测试解码器
   593  //在非缓冲输入流上运行。
   594  type plainReader []byte
   595  
   596  func newPlainReader(b []byte) io.Reader {
   597  	return (*plainReader)(&b)
   598  }
   599  
   600  func (r *plainReader) Read(buf []byte) (n int, err error) {
   601  	if len(*r) == 0 {
   602  		return 0, io.EOF
   603  	}
   604  	n = copy(buf, *r)
   605  	*r = (*r)[n:]
   606  	return n, nil
   607  }
   608  
   609  func TestDecodeWithNonByteReader(t *testing.T) {
   610  	runTests(t, func(input []byte, into interface{}) error {
   611  		return Decode(newPlainReader(input), into)
   612  	})
   613  }
   614  
   615  func TestDecodeStreamReset(t *testing.T) {
   616  	s := NewStream(nil, 0)
   617  	runTests(t, func(input []byte, into interface{}) error {
   618  		s.Reset(bytes.NewReader(input), 0)
   619  		return s.Decode(into)
   620  	})
   621  }
   622  
   623  type testDecoder struct{ called bool }
   624  
   625  func (t *testDecoder) DecodeRLP(s *Stream) error {
   626  	if _, err := s.Uint(); err != nil {
   627  		return err
   628  	}
   629  	t.called = true
   630  	return nil
   631  }
   632  
   633  func TestDecodeDecoder(t *testing.T) {
   634  	var s struct {
   635  		T1 testDecoder
   636  		T2 *testDecoder
   637  		T3 **testDecoder
   638  	}
   639  	if err := Decode(bytes.NewReader(unhex("C3010203")), &s); err != nil {
   640  		t.Fatalf("Decode error: %v", err)
   641  	}
   642  
   643  	if !s.T1.called {
   644  		t.Errorf("DecodeRLP was not called for (non-pointer) testDecoder")
   645  	}
   646  
   647  	if s.T2 == nil {
   648  		t.Errorf("*testDecoder has not been allocated")
   649  	} else if !s.T2.called {
   650  		t.Errorf("DecodeRLP was not called for *testDecoder")
   651  	}
   652  
   653  	if s.T3 == nil || *s.T3 == nil {
   654  		t.Errorf("**testDecoder has not been allocated")
   655  	} else if !(*s.T3).called {
   656  		t.Errorf("DecodeRLP was not called for **testDecoder")
   657  	}
   658  }
   659  
   660  type byteDecoder byte
   661  
   662  func (bd *byteDecoder) DecodeRLP(s *Stream) error {
   663  	_, err := s.Uint()
   664  	*bd = 255
   665  	return err
   666  }
   667  
   668  func (bd byteDecoder) called() bool {
   669  	return bd == 255
   670  }
   671  
   672  //此测试验证字节片/字节数组逻辑
   673  //不适用于实现解码器的元素类型。
   674  func TestDecoderInByteSlice(t *testing.T) {
   675  	var slice []byteDecoder
   676  	if err := Decode(bytes.NewReader(unhex("C101")), &slice); err != nil {
   677  		t.Errorf("unexpected Decode error %v", err)
   678  	} else if !slice[0].called() {
   679  		t.Errorf("DecodeRLP not called for slice element")
   680  	}
   681  
   682  	var array [1]byteDecoder
   683  	if err := Decode(bytes.NewReader(unhex("C101")), &array); err != nil {
   684  		t.Errorf("unexpected Decode error %v", err)
   685  	} else if !array[0].called() {
   686  		t.Errorf("DecodeRLP not called for array element")
   687  	}
   688  }
   689  
   690  func ExampleDecode() {
   691  	input, _ := hex.DecodeString("C90A1486666F6F626172")
   692  
   693  	type example struct {
   694  		A, B    uint
   695  private uint //忽略私有字段
   696  		String  string
   697  	}
   698  
   699  	var s example
   700  	err := Decode(bytes.NewReader(input), &s)
   701  	if err != nil {
   702  		fmt.Printf("Error: %v\n", err)
   703  	} else {
   704  		fmt.Printf("Decoded value: %#v\n", s)
   705  	}
   706  //输出:
   707  //解码值:rlp。示例a:0xa,b:0x14,private:0x0,string:“foobar”
   708  }
   709  
   710  func ExampleDecode_structTagNil() {
   711  //在本例中,我们将使用“nil”结构标记来更改
   712  //如何解码指针类型字段。输入包含一个RLP
   713  //一个元素的列表,空字符串。
   714  	input := []byte{0xC1, 0x80}
   715  
   716  //此类型使用常规规则。
   717  //空输入字符串被解码为指向空go字符串的指针。
   718  	var normalRules struct {
   719  		String *string
   720  	}
   721  	Decode(bytes.NewReader(input), &normalRules)
   722  	fmt.Printf("normal: String = %q\n", *normalRules.String)
   723  
   724  //此类型使用struct标记。
   725  //空输入字符串被解码为零指针。
   726  	var withEmptyOK struct {
   727  		String *string `rlp:"nil"`
   728  	}
   729  	Decode(bytes.NewReader(input), &withEmptyOK)
   730  	fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String)
   731  
   732  //输出:
   733  //正常:字符串=“”
   734  //带nil标记:string=<nil>
   735  }
   736  
   737  func ExampleStream() {
   738  	input, _ := hex.DecodeString("C90A1486666F6F626172")
   739  	s := NewStream(bytes.NewReader(input), 0)
   740  
   741  //检查前面有什么价值
   742  	kind, size, _ := s.Kind()
   743  	fmt.Printf("Kind: %v size:%d\n", kind, size)
   744  
   745  //进入列表
   746  	if _, err := s.List(); err != nil {
   747  		fmt.Printf("List error: %v\n", err)
   748  		return
   749  	}
   750  
   751  //解码元件
   752  	fmt.Println(s.Uint())
   753  	fmt.Println(s.Uint())
   754  	fmt.Println(s.Bytes())
   755  
   756  //确认列表结尾
   757  	if err := s.ListEnd(); err != nil {
   758  		fmt.Printf("ListEnd error: %v\n", err)
   759  	}
   760  //输出:
   761  //种类:列表大小:9
   762  //10 <NIL >
   763  //20 <NIL >
   764  //[102 111 111 98 97 114]<nil>
   765  }
   766  
   767  func BenchmarkDecode(b *testing.B) {
   768  	enc := encodeTestSlice(90000)
   769  	b.SetBytes(int64(len(enc)))
   770  	b.ReportAllocs()
   771  	b.ResetTimer()
   772  
   773  	for i := 0; i < b.N; i++ {
   774  		var s []uint
   775  		r := bytes.NewReader(enc)
   776  		if err := Decode(r, &s); err != nil {
   777  			b.Fatalf("Decode error: %v", err)
   778  		}
   779  	}
   780  }
   781  
   782  func BenchmarkDecodeIntSliceReuse(b *testing.B) {
   783  	enc := encodeTestSlice(100000)
   784  	b.SetBytes(int64(len(enc)))
   785  	b.ReportAllocs()
   786  	b.ResetTimer()
   787  
   788  	var s []uint
   789  	for i := 0; i < b.N; i++ {
   790  		r := bytes.NewReader(enc)
   791  		if err := Decode(r, &s); err != nil {
   792  			b.Fatalf("Decode error: %v", err)
   793  		}
   794  	}
   795  }
   796  
   797  func encodeTestSlice(n uint) []byte {
   798  	s := make([]uint, n)
   799  	for i := uint(0); i < n; i++ {
   800  		s[i] = i
   801  	}
   802  	b, err := EncodeToBytes(s)
   803  	if err != nil {
   804  		panic(fmt.Sprintf("encode error: %v", err))
   805  	}
   806  	return b
   807  }
   808  
   809  func unhex(str string) []byte {
   810  	b, err := hex.DecodeString(strings.Replace(str, " ", "", -1))
   811  	if err != nil {
   812  		panic(fmt.Sprintf("invalid hex string: %q", str))
   813  	}
   814  	return b
   815  }
   816