github.com/chain5j/chain5j-pkg@v1.0.7/codec/rlp/decode_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rlp 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "errors" 23 "fmt" 24 "io" 25 "math/big" 26 "reflect" 27 "strings" 28 "testing" 29 30 "github.com/chain5j/chain5j-pkg/math" 31 ) 32 33 func TestStreamKind(t *testing.T) { 34 tests := []struct { 35 input string 36 wantKind Kind 37 wantLen uint64 38 }{ 39 {"00", Byte, 0}, 40 {"01", Byte, 0}, 41 {"7F", Byte, 0}, 42 {"80", String, 0}, 43 {"B7", String, 55}, 44 {"B90400", String, 1024}, 45 {"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)}, 46 {"C0", List, 0}, 47 {"C8", List, 8}, 48 {"F7", List, 55}, 49 {"F90400", List, 1024}, 50 {"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)}, 51 } 52 53 for i, test := range tests { 54 // using plainReader to inhibit input limit errors. 55 s := NewStream(newPlainReader(unhex(test.input)), 0) 56 kind, len, err := s.Kind() 57 if err != nil { 58 t.Errorf("test %d: Kind returned error: %v", i, err) 59 continue 60 } 61 if kind != test.wantKind { 62 t.Errorf("test %d: kind mismatch: got %d, want %d", i, kind, test.wantKind) 63 } 64 if len != test.wantLen { 65 t.Errorf("test %d: len mismatch: got %d, want %d", i, len, test.wantLen) 66 } 67 } 68 } 69 70 func TestNewListStream(t *testing.T) { 71 ls := NewListStream(bytes.NewReader(unhex("0101010101")), 3) 72 if k, size, err := ls.Kind(); k != List || size != 3 || err != nil { 73 t.Errorf("Kind() returned (%v, %d, %v), expected (List, 3, nil)", k, size, err) 74 } 75 if size, err := ls.List(); size != 3 || err != nil { 76 t.Errorf("List() returned (%d, %v), expected (3, nil)", size, err) 77 } 78 for i := 0; i < 3; i++ { 79 if val, err := ls.Uint(); val != 1 || err != nil { 80 t.Errorf("Uint() returned (%d, %v), expected (1, nil)", val, err) 81 } 82 } 83 if err := ls.ListEnd(); err != nil { 84 t.Errorf("ListEnd() returned %v, expected (3, nil)", err) 85 } 86 } 87 88 func TestStreamErrors(t *testing.T) { 89 withoutInputLimit := func(b []byte) *Stream { 90 return NewStream(newPlainReader(b), 0) 91 } 92 withCustomInputLimit := func(limit uint64) func([]byte) *Stream { 93 return func(b []byte) *Stream { 94 return NewStream(bytes.NewReader(b), limit) 95 } 96 } 97 98 type calls []string 99 tests := []struct { 100 string 101 calls 102 newStream func([]byte) *Stream // uses bytes.Reader if nil 103 error error 104 }{ 105 {"C0", calls{"Bytes"}, nil, ErrExpectedString}, 106 {"C0", calls{"Uint"}, nil, ErrExpectedString}, 107 {"89000000000000000001", calls{"Uint"}, nil, errUintOverflow}, 108 {"00", calls{"List"}, nil, ErrExpectedList}, 109 {"80", calls{"List"}, nil, ErrExpectedList}, 110 {"C0", calls{"List", "Uint"}, nil, EOL}, 111 {"C8C9010101010101010101", calls{"List", "Kind"}, nil, ErrElemTooLarge}, 112 {"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, nil, EOL}, 113 {"00", calls{"ListEnd"}, nil, errNotInList}, 114 {"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL}, 115 116 // Non-canonical integers (e.g. leading zero bytes). 117 {"00", calls{"Uint"}, nil, ErrCanonInt}, 118 {"820002", calls{"Uint"}, nil, ErrCanonInt}, 119 {"8133", calls{"Uint"}, nil, ErrCanonSize}, 120 {"817F", calls{"Uint"}, nil, ErrCanonSize}, 121 {"8180", calls{"Uint"}, nil, nil}, 122 123 // Non-valid boolean 124 {"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")}, 125 126 // Size tags must use the smallest possible encoding. 127 // Leading zero bytes in the size tag are also rejected. 128 {"8100", calls{"Uint"}, nil, ErrCanonSize}, 129 {"8100", calls{"Bytes"}, nil, ErrCanonSize}, 130 {"8101", calls{"Bytes"}, nil, ErrCanonSize}, 131 {"817F", calls{"Bytes"}, nil, ErrCanonSize}, 132 {"8180", calls{"Bytes"}, nil, nil}, 133 {"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 134 {"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 135 {"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 136 {"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize}, 137 {"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 138 {"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 139 {"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize}, 140 {"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize}, 141 142 // Expected EOF 143 {"", calls{"Kind"}, nil, io.EOF}, 144 {"", calls{"Uint"}, nil, io.EOF}, 145 {"", calls{"List"}, nil, io.EOF}, 146 {"8180", calls{"Uint", "Uint"}, nil, io.EOF}, 147 {"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF}, 148 149 {"", calls{"List"}, withoutInputLimit, io.EOF}, 150 {"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF}, 151 {"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF}, 152 153 // Input limit errors. 154 {"81", calls{"Bytes"}, nil, ErrValueTooLarge}, 155 {"81", calls{"Uint"}, nil, ErrValueTooLarge}, 156 {"81", calls{"Raw"}, nil, ErrValueTooLarge}, 157 {"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge}, 158 {"C801", calls{"List"}, nil, ErrValueTooLarge}, 159 160 // Test for list element size check overflow. 161 {"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge}, 162 163 // Test for input limit overflow. Since we are counting the limit 164 // down toward zero in Stream.remaining, reading too far can overflow 165 // remaining to a large value, effectively disabling the limit. 166 {"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF}, 167 {"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge}, 168 169 // Check that the same calls are fine without a limit. 170 {"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil}, 171 {"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil}, 172 173 // Unexpected EOF. This only happens when there is 174 // no input limit, so the reader needs to be 'dumbed down'. 175 {"81", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF}, 176 {"81", calls{"Uint"}, withoutInputLimit, io.ErrUnexpectedEOF}, 177 {"BFFFFFFFFFFFFFFF", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF}, 178 {"C801", calls{"List", "Uint", "Uint"}, withoutInputLimit, io.ErrUnexpectedEOF}, 179 180 // This test verifies that the input position is advanced 181 // correctly when calling Bytes for empty strings. Kind can be called 182 // any number of times in between and doesn't advance. 183 {"C3808080", calls{ 184 "List", // enter the list 185 "Bytes", // past first element 186 187 "Kind", "Kind", "Kind", // this shouldn't advance 188 189 "Bytes", // past second element 190 191 "Kind", "Kind", // can't hurt to try 192 193 "Bytes", // past final element 194 "Bytes", // this one should fail 195 }, nil, EOL}, 196 } 197 198 testfor: 199 for i, test := range tests { 200 if test.newStream == nil { 201 test.newStream = func(b []byte) *Stream { return NewStream(bytes.NewReader(b), 0) } 202 } 203 s := test.newStream(unhex(test.string)) 204 rs := reflect.ValueOf(s) 205 for j, call := range test.calls { 206 fval := rs.MethodByName(call) 207 ret := fval.Call(nil) 208 err := "<nil>" 209 if lastret := ret[len(ret)-1].Interface(); lastret != nil { 210 err = lastret.(error).Error() 211 } 212 if j == len(test.calls)-1 { 213 want := "<nil>" 214 if test.error != nil { 215 want = test.error.Error() 216 } 217 if err != want { 218 t.Log(test) 219 t.Errorf("test %d: last call (%s) error mismatch\ngot: %s\nwant: %s", 220 i, call, err, test.error) 221 } 222 } else if err != "<nil>" { 223 t.Log(test) 224 t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err) 225 continue testfor 226 } 227 } 228 } 229 } 230 231 func TestStreamList(t *testing.T) { 232 s := NewStream(bytes.NewReader(unhex("C80102030405060708")), 0) 233 234 len, err := s.List() 235 if err != nil { 236 t.Fatalf("List error: %v", err) 237 } 238 if len != 8 { 239 t.Fatalf("List returned invalid length, got %d, want 8", len) 240 } 241 242 for i := uint64(1); i <= 8; i++ { 243 v, err := s.Uint() 244 if err != nil { 245 t.Fatalf("Uint error: %v", err) 246 } 247 if i != v { 248 t.Errorf("Uint returned wrong value, got %d, want %d", v, i) 249 } 250 } 251 252 if _, err := s.Uint(); err != EOL { 253 t.Errorf("Uint error mismatch, got %v, want %v", err, EOL) 254 } 255 if err = s.ListEnd(); err != nil { 256 t.Fatalf("ListEnd error: %v", err) 257 } 258 } 259 260 func TestStreamRaw(t *testing.T) { 261 tests := []struct { 262 input string 263 output string 264 }{ 265 { 266 "C58401010101", 267 "8401010101", 268 }, 269 { 270 "F842B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101", 271 "B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101", 272 }, 273 } 274 for i, tt := range tests { 275 s := NewStream(bytes.NewReader(unhex(tt.input)), 0) 276 s.List() 277 278 want := unhex(tt.output) 279 raw, err := s.Raw() 280 if err != nil { 281 t.Fatal(err) 282 } 283 if !bytes.Equal(want, raw) { 284 t.Errorf("test %d: raw mismatch: got %x, want %x", i, raw, want) 285 } 286 } 287 } 288 289 func TestStreamReadBytes(t *testing.T) { 290 tests := []struct { 291 input string 292 size int 293 err string 294 }{ 295 // kind List 296 {input: "C0", size: 1, err: "rlp: expected String or Byte"}, 297 // kind Byte 298 {input: "04", size: 0, err: "input value has wrong size 1, want 0"}, 299 {input: "04", size: 1}, 300 {input: "04", size: 2, err: "input value has wrong size 1, want 2"}, 301 // kind String 302 {input: "820102", size: 0, err: "input value has wrong size 2, want 0"}, 303 {input: "820102", size: 1, err: "input value has wrong size 2, want 1"}, 304 {input: "820102", size: 2}, 305 {input: "820102", size: 3, err: "input value has wrong size 2, want 3"}, 306 } 307 308 for _, test := range tests { 309 test := test 310 name := fmt.Sprintf("input_%s/size_%d", test.input, test.size) 311 t.Run(name, func(t *testing.T) { 312 s := NewStream(bytes.NewReader(unhex(test.input)), 0) 313 b := make([]byte, test.size) 314 err := s.ReadBytes(b) 315 if test.err == "" { 316 if err != nil { 317 t.Errorf("unexpected error %q", err) 318 } 319 } else { 320 if err == nil { 321 t.Errorf("expected error, got nil") 322 } else if err.Error() != test.err { 323 t.Errorf("wrong error %q", err) 324 } 325 } 326 }) 327 } 328 } 329 330 func TestDecodeErrors(t *testing.T) { 331 r := bytes.NewReader(nil) 332 333 if err := Decode(r, nil); err != errDecodeIntoNil { 334 t.Errorf("Decode(r, nil) error mismatch, got %q, want %q", err, errDecodeIntoNil) 335 } 336 337 var nilptr *struct{} 338 if err := Decode(r, nilptr); err != errDecodeIntoNil { 339 t.Errorf("Decode(r, nilptr) error mismatch, got %q, want %q", err, errDecodeIntoNil) 340 } 341 342 if err := Decode(r, struct{}{}); err != errNoPointer { 343 t.Errorf("Decode(r, struct{}{}) error mismatch, got %q, want %q", err, errNoPointer) 344 } 345 346 expectErr := "rlp: type chan bool is not RLP-serializable" 347 if err := Decode(r, new(chan bool)); err == nil || err.Error() != expectErr { 348 t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr) 349 } 350 351 if err := Decode(r, new(uint)); err != io.EOF { 352 t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF) 353 } 354 } 355 356 type decodeTest struct { 357 input string 358 ptr interface{} 359 value interface{} 360 error string 361 } 362 363 type simplestruct struct { 364 A uint 365 B string 366 } 367 368 type recstruct struct { 369 I uint 370 Child *recstruct `rlp:"nil"` 371 } 372 373 type bigIntStruct struct { 374 I *big.Int 375 B string 376 } 377 378 type invalidNilTag struct { 379 X []byte `rlp:"nil"` 380 } 381 382 type invalidTail1 struct { 383 A uint `rlp:"tail"` 384 B string 385 } 386 387 type invalidTail2 struct { 388 A uint 389 B string `rlp:"tail"` 390 } 391 392 type tailRaw struct { 393 A uint 394 Tail []RawValue `rlp:"tail"` 395 } 396 397 type tailUint struct { 398 A uint 399 Tail []uint `rlp:"tail"` 400 } 401 402 type tailPrivateFields struct { 403 A uint 404 Tail []uint `rlp:"tail"` 405 x, y bool //lint:ignore U1000 unused fields required for testing purposes. 406 } 407 408 type nilListUint struct { 409 X *uint `rlp:"nilList"` 410 } 411 412 type nilStringSlice struct { 413 X *[]uint `rlp:"nilString"` 414 } 415 416 type intField struct { 417 X int 418 } 419 420 type optionalFields struct { 421 A uint 422 B uint `rlp:"optional"` 423 C uint `rlp:"optional"` 424 } 425 426 type optionalAndTailField struct { 427 A uint 428 B uint `rlp:"optional"` 429 Tail []uint `rlp:"tail"` 430 } 431 432 type optionalBigIntField struct { 433 A uint 434 B *big.Int `rlp:"optional"` 435 } 436 437 type optionalPtrField struct { 438 A uint 439 B *[3]byte `rlp:"optional"` 440 } 441 442 type optionalPtrFieldNil struct { 443 A uint 444 B *[3]byte `rlp:"optional,nil"` 445 } 446 447 type ignoredField struct { 448 A uint 449 B uint `rlp:"-"` 450 C uint 451 } 452 453 var ( 454 veryBigInt = new(big.Int).Add( 455 new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16), 456 big.NewInt(0xFFFF), 457 ) 458 veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil) 459 ) 460 461 var decodeTests = []decodeTest{ 462 // booleans 463 {input: "01", ptr: new(bool), value: true}, 464 {input: "80", ptr: new(bool), value: false}, 465 {input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"}, 466 467 // integers 468 {input: "05", ptr: new(uint32), value: uint32(5)}, 469 {input: "80", ptr: new(uint32), value: uint32(0)}, 470 {input: "820505", ptr: new(uint32), value: uint32(0x0505)}, 471 {input: "83050505", ptr: new(uint32), value: uint32(0x050505)}, 472 {input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)}, 473 {input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"}, 474 {input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"}, 475 {input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"}, 476 {input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"}, 477 {input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"}, 478 {input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"}, 479 480 // slices 481 {input: "C0", ptr: new([]uint), value: []uint{}}, 482 {input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}}, 483 {input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"}, 484 485 // arrays 486 {input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}}, 487 {input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"}, 488 {input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"}, 489 {input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"}, 490 {input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"}, 491 492 // zero sized arrays 493 {input: "C0", ptr: new([0]uint), value: [0]uint{}}, 494 {input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"}, 495 496 // byte slices 497 {input: "01", ptr: new([]byte), value: []byte{1}}, 498 {input: "80", ptr: new([]byte), value: []byte{}}, 499 {input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")}, 500 {input: "C0", ptr: new([]byte), error: "rlp: expected input string or byte for []uint8"}, 501 {input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"}, 502 503 // byte arrays 504 {input: "02", ptr: new([1]byte), value: [1]byte{2}}, 505 {input: "8180", ptr: new([1]byte), value: [1]byte{128}}, 506 {input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}}, 507 508 // byte array errors 509 {input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"}, 510 {input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"}, 511 {input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"}, 512 {input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"}, 513 {input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"}, 514 {input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"}, 515 {input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"}, 516 {input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"}, 517 518 // zero sized byte arrays 519 {input: "80", ptr: new([0]byte), value: [0]byte{}}, 520 {input: "01", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"}, 521 {input: "8101", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"}, 522 523 // strings 524 {input: "00", ptr: new(string), value: "\000"}, 525 {input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"}, 526 {input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"}, 527 528 // big ints 529 {input: "80", ptr: new(*big.Int), value: big.NewInt(0)}, 530 {input: "01", ptr: new(*big.Int), value: big.NewInt(1)}, 531 {input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt}, 532 {input: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001", ptr: new(*big.Int), value: veryVeryBigInt}, 533 {input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works 534 {input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"}, 535 {input: "00", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"}, 536 {input: "820001", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"}, 537 {input: "8105", ptr: new(*big.Int), error: "rlp: non-canonical size information for *big.Int"}, 538 539 // structs 540 { 541 input: "C50583343434", 542 ptr: new(simplestruct), 543 value: simplestruct{5, "444"}, 544 }, 545 { 546 input: "C601C402C203C0", 547 ptr: new(recstruct), 548 value: recstruct{1, &recstruct{2, &recstruct{3, nil}}}, 549 }, 550 { 551 // This checks that empty big.Int works correctly in struct context. It's easy to 552 // miss the update of s.kind for this case, so it needs its own test. 553 input: "C58083343434", 554 ptr: new(bigIntStruct), 555 value: bigIntStruct{new(big.Int), "444"}, 556 }, 557 558 // struct errors 559 { 560 input: "C0", 561 ptr: new(simplestruct), 562 error: "rlp: too few elements for rlp.simplestruct", 563 }, 564 { 565 input: "C105", 566 ptr: new(simplestruct), 567 error: "rlp: too few elements for rlp.simplestruct", 568 }, 569 { 570 input: "C7C50583343434C0", 571 ptr: new([]*simplestruct), 572 error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]", 573 }, 574 { 575 input: "83222222", 576 ptr: new(simplestruct), 577 error: "rlp: expected input list for rlp.simplestruct", 578 }, 579 { 580 input: "C3010101", 581 ptr: new(simplestruct), 582 error: "rlp: input list has too many elements for rlp.simplestruct", 583 }, 584 { 585 input: "C501C3C00000", 586 ptr: new(recstruct), 587 error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I", 588 }, 589 { 590 input: "C103", 591 ptr: new(intField), 592 error: "rlp: type int is not RLP-serializable (struct field rlp.intField.X)", 593 }, 594 { 595 input: "C50102C20102", 596 ptr: new(tailUint), 597 error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]", 598 }, 599 { 600 input: "C0", 601 ptr: new(invalidNilTag), 602 error: `rlp: invalid struct tag "nil" for rlp.invalidNilTag.X (field is not a pointer)`, 603 }, 604 605 // struct tag "tail" 606 { 607 input: "C3010203", 608 ptr: new(tailRaw), 609 value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}}, 610 }, 611 { 612 input: "C20102", 613 ptr: new(tailRaw), 614 value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}}, 615 }, 616 { 617 input: "C101", 618 ptr: new(tailRaw), 619 value: tailRaw{A: 1, Tail: []RawValue{}}, 620 }, 621 { 622 input: "C3010203", 623 ptr: new(tailPrivateFields), 624 value: tailPrivateFields{A: 1, Tail: []uint{2, 3}}, 625 }, 626 { 627 input: "C0", 628 ptr: new(invalidTail1), 629 error: `rlp: invalid struct tag "tail" for rlp.invalidTail1.A (must be on last field)`, 630 }, 631 { 632 input: "C0", 633 ptr: new(invalidTail2), 634 error: `rlp: invalid struct tag "tail" for rlp.invalidTail2.B (field type is not slice)`, 635 }, 636 637 // struct tag "-" 638 { 639 input: "C20102", 640 ptr: new(ignoredField), 641 value: ignoredField{A: 1, C: 2}, 642 }, 643 644 // struct tag "nilList" 645 { 646 input: "C180", 647 ptr: new(nilListUint), 648 error: "rlp: wrong kind of empty value (got String, want List) for *uint, decoding into (rlp.nilListUint).X", 649 }, 650 { 651 input: "C1C0", 652 ptr: new(nilListUint), 653 value: nilListUint{}, 654 }, 655 { 656 input: "C103", 657 ptr: new(nilListUint), 658 value: func() interface{} { 659 v := uint(3) 660 return nilListUint{X: &v} 661 }(), 662 }, 663 664 // struct tag "nilString" 665 { 666 input: "C1C0", 667 ptr: new(nilStringSlice), 668 error: "rlp: wrong kind of empty value (got List, want String) for *[]uint, decoding into (rlp.nilStringSlice).X", 669 }, 670 { 671 input: "C180", 672 ptr: new(nilStringSlice), 673 value: nilStringSlice{}, 674 }, 675 { 676 input: "C2C103", 677 ptr: new(nilStringSlice), 678 value: nilStringSlice{X: &[]uint{3}}, 679 }, 680 681 // struct tag "optional" 682 { 683 input: "C101", 684 ptr: new(optionalFields), 685 value: optionalFields{1, 0, 0}, 686 }, 687 { 688 input: "C20102", 689 ptr: new(optionalFields), 690 value: optionalFields{1, 2, 0}, 691 }, 692 { 693 input: "C3010203", 694 ptr: new(optionalFields), 695 value: optionalFields{1, 2, 3}, 696 }, 697 { 698 input: "C401020304", 699 ptr: new(optionalFields), 700 error: "rlp: input list has too many elements for rlp.optionalFields", 701 }, 702 { 703 input: "C101", 704 ptr: new(optionalAndTailField), 705 value: optionalAndTailField{A: 1}, 706 }, 707 { 708 input: "C20102", 709 ptr: new(optionalAndTailField), 710 value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}}, 711 }, 712 { 713 input: "C401020304", 714 ptr: new(optionalAndTailField), 715 value: optionalAndTailField{A: 1, B: 2, Tail: []uint{3, 4}}, 716 }, 717 { 718 input: "C101", 719 ptr: new(optionalBigIntField), 720 value: optionalBigIntField{A: 1, B: nil}, 721 }, 722 { 723 input: "C20102", 724 ptr: new(optionalBigIntField), 725 value: optionalBigIntField{A: 1, B: big.NewInt(2)}, 726 }, 727 { 728 input: "C101", 729 ptr: new(optionalPtrField), 730 value: optionalPtrField{A: 1}, 731 }, 732 { 733 input: "C20180", // not accepted because "optional" doesn't enable "nil" 734 ptr: new(optionalPtrField), 735 error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B", 736 }, 737 { 738 input: "C20102", 739 ptr: new(optionalPtrField), 740 error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B", 741 }, 742 { 743 input: "C50183010203", 744 ptr: new(optionalPtrField), 745 value: optionalPtrField{A: 1, B: &[3]byte{1, 2, 3}}, 746 }, 747 { 748 input: "C101", 749 ptr: new(optionalPtrFieldNil), 750 value: optionalPtrFieldNil{A: 1}, 751 }, 752 { 753 input: "C20180", // accepted because "nil" tag allows empty input 754 ptr: new(optionalPtrFieldNil), 755 value: optionalPtrFieldNil{A: 1}, 756 }, 757 { 758 input: "C20102", 759 ptr: new(optionalPtrFieldNil), 760 error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrFieldNil).B", 761 }, 762 763 // struct tag "optional" field clearing 764 { 765 input: "C101", 766 ptr: &optionalFields{A: 9, B: 8, C: 7}, 767 value: optionalFields{A: 1, B: 0, C: 0}, 768 }, 769 { 770 input: "C20102", 771 ptr: &optionalFields{A: 9, B: 8, C: 7}, 772 value: optionalFields{A: 1, B: 2, C: 0}, 773 }, 774 { 775 input: "C20102", 776 ptr: &optionalAndTailField{A: 9, B: 8, Tail: []uint{7, 6, 5}}, 777 value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}}, 778 }, 779 { 780 input: "C101", 781 ptr: &optionalPtrField{A: 9, B: &[3]byte{8, 7, 6}}, 782 value: optionalPtrField{A: 1}, 783 }, 784 785 // RawValue 786 {input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))}, 787 {input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))}, 788 {input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}}, 789 790 // pointers 791 {input: "00", ptr: new(*[]byte), value: &[]byte{0}}, 792 {input: "80", ptr: new(*uint), value: uintp(0)}, 793 {input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"}, 794 {input: "07", ptr: new(*uint), value: uintp(7)}, 795 {input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"}, 796 {input: "8180", ptr: new(*uint), value: uintp(0x80)}, 797 {input: "C109", ptr: new(*[]uint), value: &[]uint{9}}, 798 {input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}}, 799 800 // check that input position is advanced also for empty values. 801 {input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}}, 802 803 // interface{} 804 {input: "00", ptr: new(interface{}), value: []byte{0}}, 805 {input: "01", ptr: new(interface{}), value: []byte{1}}, 806 {input: "80", ptr: new(interface{}), value: []byte{}}, 807 {input: "850505050505", ptr: new(interface{}), value: []byte{5, 5, 5, 5, 5}}, 808 {input: "C0", ptr: new(interface{}), value: []interface{}{}}, 809 {input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}}, 810 { 811 input: "C3010203", 812 ptr: new([]io.Reader), 813 error: "rlp: type io.Reader is not RLP-serializable", 814 }, 815 816 // fuzzer crashes 817 { 818 input: "c330f9c030f93030ce3030303030303030bd303030303030", 819 ptr: new(interface{}), 820 error: "rlp: element is larger than containing list", 821 }, 822 } 823 824 func uintp(i uint) *uint { return &i } 825 826 func runTests(t *testing.T, decode func([]byte, interface{}) error) { 827 for i, test := range decodeTests { 828 input, err := hex.DecodeString(test.input) 829 if err != nil { 830 t.Errorf("test %d: invalid hex input %q", i, test.input) 831 continue 832 } 833 err = decode(input, test.ptr) 834 if err != nil && test.error == "" { 835 t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q", 836 i, err, test.ptr, test.input) 837 continue 838 } 839 if test.error != "" && fmt.Sprint(err) != test.error { 840 t.Errorf("test %d: Decode error mismatch\ngot %v\nwant %v\ndecoding into %T\ninput %q", 841 i, err, test.error, test.ptr, test.input) 842 continue 843 } 844 deref := reflect.ValueOf(test.ptr).Elem().Interface() 845 if err == nil && !reflect.DeepEqual(deref, test.value) { 846 t.Errorf("test %d: value mismatch\ngot %#v\nwant %#v\ndecoding into %T\ninput %q", 847 i, deref, test.value, test.ptr, test.input) 848 } 849 } 850 } 851 852 func TestDecodeWithByteReader(t *testing.T) { 853 runTests(t, func(input []byte, into interface{}) error { 854 return Decode(bytes.NewReader(input), into) 855 }) 856 } 857 858 func testDecodeWithEncReader(t *testing.T, n int) { 859 s := strings.Repeat("0", n) 860 _, r, _ := EncodeToReader(s) 861 var decoded string 862 err := Decode(r, &decoded) 863 if err != nil { 864 t.Errorf("Unexpected decode error with n=%v: %v", n, err) 865 } 866 if decoded != s { 867 t.Errorf("Decode mismatch with n=%v", n) 868 } 869 } 870 871 // This is a regression test checking that decoding from encReader 872 // works for RLP values of size 8192 bytes or more. 873 func TestDecodeWithEncReader(t *testing.T) { 874 testDecodeWithEncReader(t, 8188) // length with header is 8191 875 testDecodeWithEncReader(t, 8189) // length with header is 8192 876 } 877 878 // plainReader reads from a byte slice but does not 879 // implement ReadByte. It is also not recognized by the 880 // size validation. This is useful to test how the decoder 881 // behaves on a non-buffered input stream. 882 type plainReader []byte 883 884 func newPlainReader(b []byte) io.Reader { 885 return (*plainReader)(&b) 886 } 887 888 func (r *plainReader) Read(buf []byte) (n int, err error) { 889 if len(*r) == 0 { 890 return 0, io.EOF 891 } 892 n = copy(buf, *r) 893 *r = (*r)[n:] 894 return n, nil 895 } 896 897 func TestDecodeWithNonByteReader(t *testing.T) { 898 runTests(t, func(input []byte, into interface{}) error { 899 return Decode(newPlainReader(input), into) 900 }) 901 } 902 903 func TestDecodeStreamReset(t *testing.T) { 904 s := NewStream(nil, 0) 905 runTests(t, func(input []byte, into interface{}) error { 906 s.Reset(bytes.NewReader(input), 0) 907 return s.Decode(into) 908 }) 909 } 910 911 type testDecoder struct{ called bool } 912 913 func (t *testDecoder) DecodeRLP(s *Stream) error { 914 if _, err := s.Uint(); err != nil { 915 return err 916 } 917 t.called = true 918 return nil 919 } 920 921 func TestDecodeDecoder(t *testing.T) { 922 var s struct { 923 T1 testDecoder 924 T2 *testDecoder 925 T3 **testDecoder 926 } 927 if err := Decode(bytes.NewReader(unhex("C3010203")), &s); err != nil { 928 t.Fatalf("Decode error: %v", err) 929 } 930 931 if !s.T1.called { 932 t.Errorf("DecodeRLP was not called for (non-pointer) testDecoder") 933 } 934 935 if s.T2 == nil { 936 t.Errorf("*testDecoder has not been allocated") 937 } else if !s.T2.called { 938 t.Errorf("DecodeRLP was not called for *testDecoder") 939 } 940 941 if s.T3 == nil || *s.T3 == nil { 942 t.Errorf("**testDecoder has not been allocated") 943 } else if !(*s.T3).called { 944 t.Errorf("DecodeRLP was not called for **testDecoder") 945 } 946 } 947 948 func TestDecodeDecoderNilPointer(t *testing.T) { 949 var s struct { 950 T1 *testDecoder `rlp:"nil"` 951 T2 *testDecoder 952 } 953 if err := Decode(bytes.NewReader(unhex("C2C002")), &s); err != nil { 954 t.Fatalf("Decode error: %v", err) 955 } 956 if s.T1 != nil { 957 t.Errorf("decoder T1 allocated for empty input (called: %v)", s.T1.called) 958 } 959 if s.T2 == nil || !s.T2.called { 960 t.Errorf("decoder T2 not allocated/called") 961 } 962 } 963 964 type byteDecoder byte 965 966 func (bd *byteDecoder) DecodeRLP(s *Stream) error { 967 _, err := s.Uint() 968 *bd = 255 969 return err 970 } 971 972 func (bd byteDecoder) called() bool { 973 return bd == 255 974 } 975 976 // This test verifies that the byte slice/byte array logic 977 // does not kick in for element types implementing Decoder. 978 func TestDecoderInByteSlice(t *testing.T) { 979 var slice []byteDecoder 980 if err := Decode(bytes.NewReader(unhex("C101")), &slice); err != nil { 981 t.Errorf("unexpected Decode error %v", err) 982 } else if !slice[0].called() { 983 t.Errorf("DecodeRLP not called for slice element") 984 } 985 986 var array [1]byteDecoder 987 if err := Decode(bytes.NewReader(unhex("C101")), &array); err != nil { 988 t.Errorf("unexpected Decode error %v", err) 989 } else if !array[0].called() { 990 t.Errorf("DecodeRLP not called for array element") 991 } 992 } 993 994 type unencodableDecoder func() 995 996 func (f *unencodableDecoder) DecodeRLP(s *Stream) error { 997 if _, err := s.List(); err != nil { 998 return err 999 } 1000 if err := s.ListEnd(); err != nil { 1001 return err 1002 } 1003 *f = func() {} 1004 return nil 1005 } 1006 1007 func TestDecoderFunc(t *testing.T) { 1008 var x func() 1009 if err := DecodeBytes([]byte{0xC0}, (*unencodableDecoder)(&x)); err != nil { 1010 t.Fatal(err) 1011 } 1012 x() 1013 } 1014 1015 // This tests the validity checks for fields with struct tag "optional". 1016 func TestInvalidOptionalField(t *testing.T) { 1017 type ( 1018 invalid1 struct { 1019 A uint `rlp:"optional"` 1020 B uint 1021 } 1022 invalid2 struct { 1023 T []uint `rlp:"tail,optional"` 1024 } 1025 invalid3 struct { 1026 T []uint `rlp:"optional,tail"` 1027 } 1028 ) 1029 1030 tests := []struct { 1031 v interface{} 1032 err string 1033 }{ 1034 {v: new(invalid1), err: `rlp: invalid struct tag "" for rlp.invalid1.B (must be optional because preceding field "A" is optional)`}, 1035 {v: new(invalid2), err: `rlp: invalid struct tag "optional" for rlp.invalid2.T (also has "tail" tag)`}, 1036 {v: new(invalid3), err: `rlp: invalid struct tag "tail" for rlp.invalid3.T (also has "optional" tag)`}, 1037 } 1038 for _, test := range tests { 1039 err := DecodeBytes(unhex("C20102"), test.v) 1040 if err == nil { 1041 t.Errorf("no error for %T", test.v) 1042 } else if err.Error() != test.err { 1043 t.Errorf("wrong error for %T: %v", test.v, err.Error()) 1044 } 1045 } 1046 1047 } 1048 1049 func ExampleDecode() { 1050 input, _ := hex.DecodeString("C90A1486666F6F626172") 1051 1052 type example struct { 1053 A, B uint 1054 String string 1055 } 1056 1057 var s example 1058 err := Decode(bytes.NewReader(input), &s) 1059 if err != nil { 1060 fmt.Printf("Error: %v\n", err) 1061 } else { 1062 fmt.Printf("Decoded value: %#v\n", s) 1063 } 1064 // Output: 1065 // Decoded value: rlp.example{A:0xa, B:0x14, String:"foobar"} 1066 } 1067 1068 func ExampleDecode_structTagNil() { 1069 // In this example, we'll use the "nil" struct tag to change 1070 // how a pointer-typed field is decoded. The input contains an RLP 1071 // list of one element, an empty string. 1072 input := []byte{0xC1, 0x80} 1073 1074 // This type uses the normal rules. 1075 // The empty input string is decoded as a pointer to an empty Go string. 1076 var normalRules struct { 1077 String *string 1078 } 1079 Decode(bytes.NewReader(input), &normalRules) 1080 fmt.Printf("normal: String = %q\n", *normalRules.String) 1081 1082 // This type uses the struct tag. 1083 // The empty input string is decoded as a nil pointer. 1084 var withEmptyOK struct { 1085 String *string `rlp:"nil"` 1086 } 1087 Decode(bytes.NewReader(input), &withEmptyOK) 1088 fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String) 1089 1090 // Output: 1091 // normal: String = "" 1092 // with nil tag: String = <nil> 1093 } 1094 1095 func ExampleStream() { 1096 input, _ := hex.DecodeString("C90A1486666F6F626172") 1097 s := NewStream(bytes.NewReader(input), 0) 1098 1099 // Check what kind of value lies ahead 1100 kind, size, _ := s.Kind() 1101 fmt.Printf("Kind: %v size:%d\n", kind, size) 1102 1103 // Enter the list 1104 if _, err := s.List(); err != nil { 1105 fmt.Printf("List error: %v\n", err) 1106 return 1107 } 1108 1109 // Decode elements 1110 fmt.Println(s.Uint()) 1111 fmt.Println(s.Uint()) 1112 fmt.Println(s.Bytes()) 1113 1114 // Acknowledge end of list 1115 if err := s.ListEnd(); err != nil { 1116 fmt.Printf("ListEnd error: %v\n", err) 1117 } 1118 // Output: 1119 // Kind: List size:9 1120 // 10 <nil> 1121 // 20 <nil> 1122 // [102 111 111 98 97 114] <nil> 1123 } 1124 1125 func BenchmarkDecodeUints(b *testing.B) { 1126 enc := encodeTestSlice(90000) 1127 b.SetBytes(int64(len(enc))) 1128 b.ReportAllocs() 1129 b.ResetTimer() 1130 1131 for i := 0; i < b.N; i++ { 1132 var s []uint 1133 r := bytes.NewReader(enc) 1134 if err := Decode(r, &s); err != nil { 1135 b.Fatalf("Decode error: %v", err) 1136 } 1137 } 1138 } 1139 1140 func BenchmarkDecodeUintsReused(b *testing.B) { 1141 enc := encodeTestSlice(100000) 1142 b.SetBytes(int64(len(enc))) 1143 b.ReportAllocs() 1144 b.ResetTimer() 1145 1146 var s []uint 1147 for i := 0; i < b.N; i++ { 1148 r := bytes.NewReader(enc) 1149 if err := Decode(r, &s); err != nil { 1150 b.Fatalf("Decode error: %v", err) 1151 } 1152 } 1153 } 1154 1155 func BenchmarkDecodeByteArrayStruct(b *testing.B) { 1156 enc, err := EncodeToBytes(&byteArrayStruct{}) 1157 if err != nil { 1158 b.Fatal(err) 1159 } 1160 b.SetBytes(int64(len(enc))) 1161 b.ReportAllocs() 1162 b.ResetTimer() 1163 1164 var out byteArrayStruct 1165 for i := 0; i < b.N; i++ { 1166 if err := DecodeBytes(enc, &out); err != nil { 1167 b.Fatal(err) 1168 } 1169 } 1170 } 1171 1172 func BenchmarkDecodeBigInts(b *testing.B) { 1173 ints := make([]*big.Int, 200) 1174 for i := range ints { 1175 ints[i] = math.BigPow(2, int64(i)) 1176 } 1177 enc, err := EncodeToBytes(ints) 1178 if err != nil { 1179 b.Fatal(err) 1180 } 1181 b.SetBytes(int64(len(enc))) 1182 b.ReportAllocs() 1183 b.ResetTimer() 1184 1185 var out []*big.Int 1186 for i := 0; i < b.N; i++ { 1187 if err := DecodeBytes(enc, &out); err != nil { 1188 b.Fatal(err) 1189 } 1190 } 1191 } 1192 1193 func encodeTestSlice(n uint) []byte { 1194 s := make([]uint, n) 1195 for i := uint(0); i < n; i++ { 1196 s[i] = i 1197 } 1198 b, err := EncodeToBytes(s) 1199 if err != nil { 1200 panic(fmt.Sprintf("encode error: %v", err)) 1201 } 1202 return b 1203 } 1204 1205 func unhex(str string) []byte { 1206 b, err := hex.DecodeString(strings.ReplaceAll(str, " ", "")) 1207 if err != nil { 1208 panic(fmt.Sprintf("invalid hex string: %q", str)) 1209 } 1210 return b 1211 }