forked from OffchainLabs/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhasher_test.go
More file actions
executable file
·170 lines (156 loc) · 3.82 KB
/
Copy pathhasher_test.go
File metadata and controls
executable file
·170 lines (156 loc) · 3.82 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ssz
import (
"bytes"
"go/ast"
"go/parser"
"go/token"
"testing"
)
type validatorIndex uint64
func TestNextPowerOfTwo(t *testing.T) {
cases := []struct {
Num, Res uint64
}{
{0, 0},
{1, 1},
{2, 2},
{3, 4},
{4, 4},
{5, 8},
{6, 8},
{7, 8},
{8, 8},
{9, 16},
{10, 16},
{11, 16},
{13, 16},
}
for _, c := range cases {
if next := nextPowerOfTwo(c.Num); uint64(next) != c.Res {
t.Fatalf("num %d, expected %d but found %d", c.Num, c.Res, next)
}
}
}
func TestMerkleize8ByteVector(t *testing.T) {
result := merkleizeInput([]byte{'1', '2', '3', '4', '5', '6', '7', '8'}, 0)
if !bytes.Equal(result, []byte{49, 50, 51, 52, 53, 54, 55, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) {
t.Fatalf("Unexpected result: %v", result)
}
}
func TestAppendUintAcceptsNamedUint64(t *testing.T) {
var h Hasher
AppendUint(&h, validatorIndex(7))
want := MarshalUint64(nil, 7)
if !bytes.Equal(h.buf, want) {
t.Fatalf("unexpected result: %v", h.buf)
}
}
func TestPutUintAcceptsNamedUint64(t *testing.T) {
var h Hasher
PutUint(&h, validatorIndex(7))
want := make([]byte, 32)
copy(want, MarshalUint64(nil, 7))
if !bytes.Equal(h.buf, want) {
t.Fatalf("unexpected result: %v", h.buf)
}
}
func TestDeprecatedAppendWrappersCallAppendUint(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "hasher.go", nil, 0)
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"AppendUint8", "AppendUint64"} {
found := false
for _, decl := range file.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok || fn.Name.Name != name {
continue
}
found = true
if len(fn.Body.List) != 1 {
t.Fatalf("expected %s to have exactly one statement", name)
}
exprStmt, ok := fn.Body.List[0].(*ast.ExprStmt)
if !ok {
t.Fatalf("expected %s to delegate with a direct call", name)
}
call, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
t.Fatalf("expected %s body to be a call", name)
}
ident, ok := call.Fun.(*ast.Ident)
if !ok || ident.Name != "AppendUint" {
t.Fatalf("expected %s to call AppendUint", name)
}
}
if !found {
t.Fatalf("did not find %s", name)
}
}
}
func TestDeprecatedPutWrappersCallPutUint(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "hasher.go", nil, 0)
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"PutUint64", "PutUint32", "PutUint16", "PutUint8"} {
found := false
for _, decl := range file.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok || fn.Name.Name != name {
continue
}
found = true
if len(fn.Body.List) != 1 {
t.Fatalf("expected %s to have exactly one statement", name)
}
exprStmt, ok := fn.Body.List[0].(*ast.ExprStmt)
if !ok {
t.Fatalf("expected %s to delegate with a direct call", name)
}
call, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
t.Fatalf("expected %s body to be a call", name)
}
ident, ok := call.Fun.(*ast.Ident)
if !ok || ident.Name != "PutUint" {
t.Fatalf("expected %s to call PutUint", name)
}
}
if !found {
t.Fatalf("did not find %s", name)
}
}
}
func TestAppendUintUsesMarshalUintInternally(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "hasher.go", nil, 0)
if err != nil {
t.Fatal(err)
}
for _, decl := range file.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok || fn.Name.Name != "AppendUint" {
continue
}
callsMarshalUint := false
ast.Inspect(fn.Body, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
ident, ok := call.Fun.(*ast.Ident)
if ok && ident.Name == "MarshalUint" {
callsMarshalUint = true
}
return true
})
if !callsMarshalUint {
t.Fatal("expected AppendUint to use MarshalUint internally")
}
return
}
t.Fatal("did not find AppendUint")
}