github.com/ledgerwatch/erigon-lib@v1.0.0/rlp/parse_test.go (about) 1 package rlp 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/holiman/uint256" 8 "github.com/stretchr/testify/assert" 9 10 "github.com/ledgerwatch/erigon-lib/common/hexutility" 11 ) 12 13 var parseU64Tests = []struct { 14 expectErr error 15 payload []byte 16 expectPos int 17 expectRes uint64 18 }{ 19 {payload: hexutility.MustDecodeHex("820400"), expectPos: 3, expectRes: 1024}, 20 {payload: hexutility.MustDecodeHex("07"), expectPos: 1, expectRes: 7}, 21 {payload: hexutility.MustDecodeHex("8107"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 22 {payload: hexutility.MustDecodeHex("B8020004"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 23 {payload: hexutility.MustDecodeHex("C0"), expectErr: fmt.Errorf("%w: uint64 must be a string, not isList", ErrParse)}, 24 {payload: hexutility.MustDecodeHex("00"), expectErr: fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: 00", ErrParse)}, 25 {payload: hexutility.MustDecodeHex("8AFFFFFFFFFFFFFFFFFF7C"), expectErr: fmt.Errorf("%w: uint64 must not be more than 8 bytes long, got 10", ErrParse)}, 26 } 27 28 var parseU32Tests = []struct { 29 expectErr error 30 payload []byte 31 expectPos int 32 expectRes uint32 33 }{ 34 {payload: hexutility.MustDecodeHex("820400"), expectPos: 3, expectRes: 1024}, 35 {payload: hexutility.MustDecodeHex("07"), expectPos: 1, expectRes: 7}, 36 {payload: hexutility.MustDecodeHex("8107"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 37 {payload: hexutility.MustDecodeHex("B8020004"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 38 {payload: hexutility.MustDecodeHex("C0"), expectErr: fmt.Errorf("%w: uint32 must be a string, not isList", ErrParse)}, 39 {payload: hexutility.MustDecodeHex("00"), expectErr: fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: 00", ErrParse)}, 40 {payload: hexutility.MustDecodeHex("85FF6738FF7C"), expectErr: fmt.Errorf("%w: uint32 must not be more than 4 bytes long, got 5", ErrParse)}, 41 } 42 43 var parseU256Tests = []struct { 44 expectErr error 45 expectRes *uint256.Int 46 payload []byte 47 expectPos int 48 }{ 49 {payload: hexutility.MustDecodeHex("8BFFFFFFFFFFFFFFFFFF7C"), expectErr: fmt.Errorf("%w: unexpected end of payload", ErrParse)}, 50 {payload: hexutility.MustDecodeHex("8AFFFFFFFFFFFFFFFFFF7C"), expectPos: 11, expectRes: new(uint256.Int).SetBytes(hexutility.MustDecodeHex("FFFFFFFFFFFFFFFFFF7C"))}, 51 {payload: hexutility.MustDecodeHex("85CE05050505"), expectPos: 6, expectRes: new(uint256.Int).SetUint64(0xCE05050505)}, 52 {payload: hexutility.MustDecodeHex("820400"), expectPos: 3, expectRes: new(uint256.Int).SetUint64(1024)}, 53 {payload: hexutility.MustDecodeHex("07"), expectPos: 1, expectRes: new(uint256.Int).SetUint64(7)}, 54 {payload: hexutility.MustDecodeHex("8107"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 55 {payload: hexutility.MustDecodeHex("B8020004"), expectErr: fmt.Errorf("%w: non-canonical size information", ErrParse)}, 56 {payload: hexutility.MustDecodeHex("C0"), expectErr: fmt.Errorf("%w: must be a string, instead of a list", ErrParse)}, 57 {payload: hexutility.MustDecodeHex("00"), expectErr: fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: 00", ErrParse)}, 58 {payload: hexutility.MustDecodeHex("A101000000000000000000000000000000000000008B000000000000000000000000"), expectErr: fmt.Errorf("%w: uint256 must not be more than 32 bytes long, got 33", ErrParse)}, 59 } 60 61 func TestPrimitives(t *testing.T) { 62 for i, tt := range parseU64Tests { 63 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 64 assert := assert.New(t) 65 pos, res, err := U64(tt.payload, 0) 66 assert.Equal(tt.expectErr, err) 67 assert.Equal(tt.expectPos, pos) 68 assert.Equal(tt.expectRes, res) 69 }) 70 } 71 for i, tt := range parseU32Tests { 72 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 73 assert := assert.New(t) 74 pos, res, err := U32(tt.payload, 0) 75 assert.Equal(tt.expectErr, err) 76 assert.Equal(tt.expectPos, pos) 77 assert.Equal(tt.expectRes, res) 78 }) 79 } 80 for i, tt := range parseU256Tests { 81 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 82 assert := assert.New(t) 83 res := new(uint256.Int) 84 pos, err := U256(tt.payload, 0, res) 85 assert.Equal(tt.expectErr, err) 86 assert.Equal(tt.expectPos, pos) 87 if err == nil { 88 assert.Equal(tt.expectRes, res) 89 } 90 }) 91 } 92 }