-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal_test.go
109 lines (101 loc) · 2.71 KB
/
marshal_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// dynssz: Dynamic SSZ encoding/decoding for Ethereum with fastssz efficiency.
// This file is part of the dynssz package.
// Copyright (c) 2024 by pk910. Refer to LICENSE for more information.
package dynssz_test
import (
"bytes"
"testing"
. "github.com/pk910/dynamic-ssz"
)
var marshalTestMatrix = []struct {
payload any
expected []byte
}{
// primitive types
{bool(false), fromHex("0x00")},
{bool(true), fromHex("0x01")},
{uint8(0), fromHex("0x00")},
{uint8(255), fromHex("0xff")},
{uint8(42), fromHex("0x2a")},
{uint16(0), fromHex("0x0000")},
{uint16(65535), fromHex("0xffff")},
{uint16(1337), fromHex("0x3905")},
{uint32(0), fromHex("0x00000000")},
{uint32(4294967295), fromHex("0xffffffff")},
{uint32(817482215), fromHex("0xe7c9b930")},
{uint64(0), fromHex("0x0000000000000000")},
{uint64(18446744073709551615), fromHex("0xffffffffffffffff")},
{uint64(848028848028), fromHex("0x9c4f7572c5000000")},
// arrays & slices
{[]uint8{}, fromHex("0x")},
{[]uint8{1, 2, 3, 4, 5}, fromHex("0x0102030405")},
{[5]uint8{1, 2, 3, 4, 5}, fromHex("0x0102030405")},
{[10]uint8{1, 2, 3, 4, 5}, fromHex("0x01020304050000000000")},
// complex types
{
struct {
F1 bool
F2 uint8
F3 uint16
F4 uint32
F5 uint64
}{true, 1, 2, 3, 4},
fromHex("0x01010200030000000400000000000000"),
},
{
struct {
F1 bool
F2 []uint8 // dynamic field
F3 []uint16 `ssz-size:"5"` // static field due to tag
F4 uint32
}{true, []uint8{1, 1, 1, 1}, []uint16{2, 2, 2, 2}, 3},
fromHex("0x0113000000020002000200020000000300000001010101"),
},
{
struct {
F1 uint8
F2 [][]uint8 `ssz-size:"?,2"`
F3 uint8
}{42, [][]uint8{{2, 2}, {3}}, 43},
fromHex("0x2a060000002b02020300"),
},
{
struct {
F1 uint8
F2 []slug_DynStruct1 `ssz-size:"3"`
F3 uint8
}{42, []slug_DynStruct1{{true, []uint8{4}}, {true, []uint8{4, 8, 4}}}, 43},
fromHex("0x2a060000002b0c000000120000001a00000001050000000401050000000408040005000000"),
},
{
struct {
F1 uint8
F2 []*slug_StaticStruct1 `ssz-size:"3"`
F3 uint8
}{42, []*slug_StaticStruct1{nil, {true, []uint8{4, 8, 4}}}, 43},
fromHex("0x2a0000000001040804000000002b"),
},
{
struct {
F1 uint8
F2 []*slug_StaticStruct1 `ssz-size:"3"`
F3 uint8
}{42, []*slug_StaticStruct1{nil, nil, nil, nil}, 43},
nil, // size too long error
},
}
func TestMarshal(t *testing.T) {
dynssz := NewDynSsz(nil)
dynssz.NoFastSsz = true
for idx, test := range marshalTestMatrix {
buf, err := dynssz.MarshalSSZ(test.payload)
switch {
case test.expected == nil && err != nil:
// expected error
case err != nil:
t.Errorf("test %v error: %v", idx, err)
case !bytes.Equal(buf, test.expected):
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", idx, buf, test.expected)
}
}
}