forked from ethereum/go-verkle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding_test.go
60 lines (51 loc) · 1.67 KB
/
encoding_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package verkle
import (
"testing"
"github.com/crate-crypto/go-ipa/banderwagon"
)
func TestParseNodeEmptyPayload(t *testing.T) {
t.Parallel()
_, err := ParseNode([]byte{}, 0)
if err != errSerializedPayloadTooShort {
t.Fatalf("invalid error, got %v, expected %v", err, "unexpected EOF")
}
}
func TestLeafStemLength(t *testing.T) {
t.Parallel()
// Serialize a leaf with no values, but whose stem is 32 bytes. The
// serialization should trim the extra byte.
toolong := make([]byte, 32)
leaf, err := NewLeafNode(toolong, make([][]byte, NodeWidth))
if err != nil {
t.Fatal(err)
}
ser, err := leaf.Serialize()
if err != nil {
t.Fatal(err)
}
if len(ser) != nodeTypeSize+StemSize+bitlistSize+3*banderwagon.UncompressedSize {
t.Fatalf("invalid serialization when the stem is longer than 31 bytes: %x (%d bytes != %d)", ser, len(ser), nodeTypeSize+StemSize+bitlistSize+2*banderwagon.UncompressedSize)
}
}
func TestInvalidNodeEncoding(t *testing.T) {
t.Parallel()
// Test a short payload.
if _, err := ParseNode([]byte{leafRLPType}, 0); err != errSerializedPayloadTooShort {
t.Fatalf("invalid error, got %v, expected %v", err, errSerializedPayloadTooShort)
}
// Test an invalid node type.
values := make([][]byte, NodeWidth)
values[42] = testValue
ln, err := NewLeafNode(ffx32KeyTest, values)
if err != nil {
t.Fatal(err)
}
lnbytes, err := ln.Serialize()
if err != nil {
t.Fatalf("serializing leaf node: %v", err)
}
lnbytes[0] = leafRLPType + internalRLPType // Change the type of the node to something invalid.
if _, err := ParseNode(lnbytes, 0); err != ErrInvalidNodeEncoding {
t.Fatalf("invalid error, got %v, expected %v", err, ErrInvalidNodeEncoding)
}
}