forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier_test.go
81 lines (74 loc) · 1.76 KB
/
verifier_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
package preimage
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestWithVerification(t *testing.T) {
validData := []byte{1, 2, 3, 4, 5, 6}
keccak256Key := Keccak256Key(Keccak256(validData))
anError := errors.New("boom")
tests := []struct {
name string
key Key
data []byte
err error
expectedErr error
expectedData []byte
}{
{
name: "LocalKey NoVerification",
key: LocalIndexKey(1),
data: []byte{4, 3, 5, 7, 3},
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "Keccak256 Valid",
key: keccak256Key,
data: validData,
expectedData: validData,
},
{
name: "Keccak256 Error",
key: keccak256Key,
data: validData,
err: anError,
expectedErr: anError,
},
{
name: "Keccak256 InvalidData",
key: keccak256Key,
data: []byte{6, 7, 8},
expectedErr: ErrIncorrectData,
},
{
name: "EmptyData",
key: keccak256Key,
data: []byte{},
expectedErr: ErrIncorrectData,
},
{
name: "UnknownKey",
key: invalidKey([32]byte{0xaa}),
data: []byte{},
expectedErr: ErrUnsupportedKeyType,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
source := WithVerification(func(key [32]byte) ([]byte, error) {
return test.data, test.err
})
actual, err := source(test.key.PreimageKey())
require.ErrorIs(t, err, test.expectedErr)
require.Equal(t, test.expectedData, actual)
})
}
}
type invalidKey [32]byte
func (k invalidKey) PreimageKey() (out [32]byte) {
out = k // copy the source hash
out[0] = byte(254) // apply invalid prefix
return
}