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