decode_test.go 25 KB

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