decode_test.go 34 KB

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