forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 34
/
verifier_test.go
105 lines (96 loc) · 2.46 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package preimage
import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestWithVerification(t *testing.T) {
validData := []byte{1, 2, 3, 4, 5, 6}
keccak256Key := Keccak256Key(Keccak256(validData))
sha256Key := Sha256Key(sha256.Sum256(validData))
anError := errors.New("boom")
validKeys := []Key{keccak256Key, sha256Key}
type testData struct {
name string
key Key
data []byte
err error
expectedErr error
expectedData []byte
}
tests := []testData{
{
name: "LocalKey NoVerification",
key: LocalIndexKey(1),
data: []byte{4, 3, 5, 7, 3},
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "BlobKey NoVerification",
key: BlobKey([32]byte{1, 2, 3, 4}),
data: []byte{4, 3, 5, 7, 3},
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "KZGPointEvaluationKey NoVerification",
key: PrecompileKey([32]byte{1, 2, 3, 4}),
data: []byte{4, 3, 5, 7, 3},
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "UnknownKey",
key: invalidKey([32]byte{0xaa}),
data: []byte{},
expectedErr: ErrUnsupportedKeyType,
},
}
for _, key := range validKeys {
name := reflect.TypeOf(key).Name()
tests = append(tests,
testData{
name: fmt.Sprintf("%v-Valid", name),
key: key,
data: validData,
expectedData: validData,
},
testData{
name: fmt.Sprintf("%v-Error", name),
key: key,
data: validData,
err: anError,
expectedErr: anError,
},
testData{
name: fmt.Sprintf("%v-InvalidData", name),
key: key,
data: []byte{6, 7, 8},
expectedErr: ErrIncorrectData,
},
testData{
name: fmt.Sprintf("%v-EmptyData", name),
key: key,
data: []byte{},
expectedErr: ErrIncorrectData,
})
}
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
}