forked from umbracle/fastrlp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzz_test.go
100 lines (85 loc) · 1.65 KB
/
fuzz_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
package fastrlp
import (
"fmt"
"testing"
)
func TestFuzzFramework(t *testing.T) {
obj := &Simple{}
if err := Fuzz(100, obj); err != nil {
t.Fatal(err)
}
}
type Simple struct {
Data1 []byte
Data2 [][]byte
Data3 uint64
}
func (s *Simple) MarshalRLPTo(dst []byte) ([]byte, error) {
return MarshalRLP(s)
}
func (s *Simple) MarshalRLPWith(ar *Arena) (*Value, error) {
vv := ar.NewArray()
// Data1
if len(s.Data1) == 0 {
vv.Set(ar.NewNull())
} else {
vv.Set(ar.NewBytes(s.Data1))
}
// Data2
if len(s.Data2) == 0 {
vv.Set(ar.NewNullArray())
} else {
committed := ar.NewArray()
for _, a := range s.Data2 {
if len(a) == 0 {
committed.Set(ar.NewNull())
} else {
committed.Set(ar.NewBytes(a))
}
}
vv.Set(committed)
}
// Data3
vv.Set(ar.NewUint(s.Data3))
return vv, nil
}
func (s *Simple) UnmarshalRLP(buf []byte) error {
return UnmarshalRLP(buf, s)
}
func (s *Simple) UnmarshalRLPWith(v *Value) error {
elems, err := v.GetElems()
if err != nil {
return err
}
if num := len(elems); num != 3 {
return fmt.Errorf("not enough elements to decode extra, expected 3 but found %d", num)
}
// Data1
{
if s.Data1, err = elems[0].GetBytes(s.Data1); err != nil {
return err
}
}
// Data2
{
vals, err := elems[1].GetElems()
if err != nil {
return fmt.Errorf("list expected for committed")
}
if len(vals) == 0 {
s.Data2 = nil
} else {
s.Data2 = make([][]byte, len(vals))
for indx, val := range vals {
if s.Data2[indx], err = val.GetBytes(s.Data2[indx]); err != nil {
return err
}
}
}
}
// Data3
if s.Data3, err = elems[2].GetUint64(); err != nil {
return err
}
return nil
}