@@ -18,17 +18,19 @@ package rlp
1818
1919import (
2020 "bytes"
21+ "encoding/binary"
2122 "encoding/hex"
2223 "math/rand"
2324 "runtime"
2425 "testing"
2526
2627 "github.com/google/go-cmp/cmp"
2728 "github.com/google/go-cmp/cmp/cmpopts"
29+ "github.com/stretchr/testify/assert"
2830 "github.com/stretchr/testify/require"
2931)
3032
31- func TestParseTreeNoErrorOnUpstreamTests (t * testing.T ) {
33+ func TestTreeRoundTripOnUpstream (t * testing.T ) {
3234 for _ , tt := range encTests {
3335 t .Run ("" , func (t * testing.T ) {
3436 if tt .error != "" {
@@ -47,24 +49,28 @@ func TestParseTreeNoErrorOnUpstreamTests(t *testing.T) {
4749 buf , err := hex .DecodeString (tt .output )
4850 require .NoErrorf (t , err , "hex.DecodeString(%T.output)" , tt )
4951
50- _ , err = ParseTree (buf )
52+ node , err : = ParseTree (buf )
5153 require .NoErrorf (t , err , "ParseTree(%T => %#x)" , tt .val , buf )
54+
55+ var got bytes.Buffer
56+ require .NoError (t , node .EncodeRLP (& got ))
57+ assert .Equal (t , buf , got .Bytes ())
5258 })
5359 }
5460}
5561
56- func TestParseTree (t * testing.T ) {
62+ func TestTreeRoundTripFuzzed (t * testing.T ) {
5763 t .Parallel ()
5864
5965 type test struct {
6066 fuzzed bool
6167 fuzzSeed int64
6268 value any
63- want ItemNode
69+ node ItemNode
6470 }
6571 tests := []test {{
6672 value : []byte (nil ),
67- want : StringNode {},
73+ node : StringNode {},
6874 }}
6975
7076 for i := 0 ; i < 3e3 ; i ++ {
@@ -75,7 +81,7 @@ func TestParseTree(t *testing.T) {
7581 fuzzed : true ,
7682 fuzzSeed : seed ,
7783 value : val ,
78- want : node ,
84+ node : node ,
7985 })
8086 }
8187
@@ -91,16 +97,25 @@ func TestParseTree(t *testing.T) {
9197 t .Logf ("Fuzzing seed: %d" , tt .fuzzSeed )
9298 }
9399
94- buf := bytes .NewBuffer (nil )
95- require .NoError (t , Encode (buf , tt .value ))
96- t .Logf ("RLP encoding of %T: %#x\n Value: %x" , tt .value , buf .Bytes (), tt .value )
97-
98- got , err := ParseTree (buf .Bytes ())
99- require .NoError (t , err )
100-
101- if diff := cmp .Diff (tt .want , got , cmpopts .EquateEmpty ()); diff != "" {
102- t .Errorf ("%s" , diff )
103- }
100+ rlp , err := EncodeToBytes (tt .value )
101+ require .NoError (t , err , "EncodeToBytes([concrete value])" )
102+ t .Logf ("RLP encoding of %T: %#x\n Value: %x" , tt .value , rlp , tt .value )
103+
104+ t .Run ("ParseTree" , func (t * testing.T ) {
105+ got , err := ParseTree (rlp )
106+ require .NoError (t , err , "ParseTree()" )
107+ if diff := cmp .Diff (tt .node , got , cmpopts .EquateEmpty ()); diff != "" {
108+ t .Errorf ("ParseTree() diff (-want +got): \n %s" , diff )
109+ }
110+ })
111+
112+ t .Run ("ItemNode.EncodeRLP()" , func (t * testing.T ) {
113+ got , err := EncodeToBytes (tt .node )
114+ require .NoErrorf (t , err , "EncodeToBytes(..., %T)" , tt .node )
115+ if diff := cmp .Diff (rlp , got ); diff != "" {
116+ t .Errorf ("diff -Encode([concrete values]) +Encode(%T):\n %s" , tt .node , diff )
117+ }
118+ })
104119 })
105120 }
106121}
@@ -142,3 +157,77 @@ func randomItemNode(rng *rand.Rand) (any, ItemNode) {
142157 return vals , list
143158 }
144159}
160+
161+ func TestEncodeItemNote (t * testing.T ) {
162+ encodeToHex := func (v any ) string {
163+ b , err := EncodeToBytes (v )
164+ require .NoError (t , err )
165+ return hex .EncodeToString (b )
166+ }
167+ makeBytes := func (n int ) []byte {
168+ return append ([]byte {1 }, make ([]byte , n - 1 )... )
169+ }
170+
171+ tests := []struct {
172+ node ItemNode
173+ wantHex string
174+ }{
175+ {ByteNode (0 ), "00" },
176+ {ByteNode (1 ), "01" },
177+ {ByteNode (127 ), "7f" },
178+ {ByteNode (128 ), "8180" },
179+ {ByteNode (255 ), "81ff" },
180+ {StringNode {}, "80" },
181+ {StringNode {0 }, "8100" },
182+ {StringNode {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }, "89010203040506070809" },
183+ {
184+ StringNode (makeBytes (55 )),
185+ encodeToHex (makeBytes (55 )),
186+ },
187+ {
188+ StringNode (makeBytes (56 )),
189+ encodeToHex (makeBytes (56 )),
190+ },
191+ }
192+
193+ for _ , tt := range tests {
194+ var got bytes.Buffer
195+ require .NoError (t , tt .node .EncodeRLP (& got ))
196+ assert .Equal (t , unhex (tt .wantHex ), got .Bytes ())
197+ }
198+ }
199+
200+ func TestByteLength (t * testing.T ) {
201+ tests := []struct { n , want uint64 }{
202+ {0 , 0 },
203+ {1 << 8 - 1 , 1 },
204+ {1 << 8 , 2 },
205+ {1 << 16 - 1 , 2 },
206+ {1 << 16 , 3 },
207+ {1 << 24 - 1 , 3 },
208+ {1 << 24 , 4 },
209+ }
210+ for _ , tt := range tests {
211+ assert .Equalf (t , tt .want , byteLength (tt .n ), "byteLength(%d)" , tt .n )
212+ }
213+ }
214+
215+ func TestBigEndian (t * testing.T ) {
216+ assert .Empty (t , bigEndian (0 ), "bigEndian(0)" )
217+
218+ rng := rand .New (rand .NewSource (42 )) //nolint:gosec // Reproducible fuzzing required
219+
220+ for i := 1 ; i <= 8 ; i ++ {
221+ prefix := make ([]byte , 8 - i )
222+ for j := 0 ; j < 100 ; j ++ {
223+ suffix := make ([]byte , i )
224+ for suffix [0 ] == 0 {
225+ rng .Read (suffix ) //nolint:gosec // Documented as always returning nil error
226+ }
227+
228+ n := binary .BigEndian .Uint64 (append (prefix , suffix ... ))
229+ assert .Equalf (t , suffix , bigEndian (n ), "bigEndian(%d)" , n )
230+ }
231+ }
232+
233+ }
0 commit comments