forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets_common_test.go
259 lines (184 loc) · 5.23 KB
/
secrets_common_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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"path/filepath"
"testing"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCommon_SaveReadData(t *testing.T) {
t.Parallel()
t.Run("invalid data save path", func(t *testing.T) {
t.Parallel()
assert.ErrorContains(
t,
saveSecretData(nil, ""),
"unable to save data to path",
)
})
t.Run("invalid data read path", func(t *testing.T) {
t.Parallel()
readData, err := readSecretData[p2p.NodeKey]("")
assert.Nil(t, readData)
assert.ErrorContains(
t,
err,
"unable to read data",
)
})
t.Run("invalid data read", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "key.json")
require.NoError(t, saveSecretData("totally valid key", path))
readData, err := readSecretData[p2p.NodeKey](path)
require.Nil(t, readData)
assert.ErrorContains(t, err, "unable to unmarshal data")
})
t.Run("valid data save and read", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "key.json")
key := generateNodeKey()
require.NoError(t, saveSecretData(key, path))
readKey, err := readSecretData[p2p.NodeKey](path)
require.NoError(t, err)
assert.Equal(t, key, readKey)
})
}
func TestCommon_ValidateValidatorKey(t *testing.T) {
t.Parallel()
t.Run("valid validator key", func(t *testing.T) {
t.Parallel()
key := generateValidatorPrivateKey()
assert.NoError(t, validateValidatorKey(key))
})
t.Run("invalid private key", func(t *testing.T) {
t.Parallel()
key := generateValidatorPrivateKey()
key.PrivKey = nil
assert.ErrorIs(t, validateValidatorKey(key), errInvalidPrivateKey)
})
t.Run("public key mismatch", func(t *testing.T) {
t.Parallel()
key := generateValidatorPrivateKey()
key.PubKey = nil
assert.ErrorIs(t, validateValidatorKey(key), errPublicKeyMismatch)
})
t.Run("address mismatch", func(t *testing.T) {
t.Parallel()
key := generateValidatorPrivateKey()
key.Address = crypto.Address{} // zero address
assert.ErrorIs(t, validateValidatorKey(key), errAddressMismatch)
})
}
func TestCommon_ValidateValidatorState(t *testing.T) {
t.Parallel()
t.Run("valid validator state", func(t *testing.T) {
t.Parallel()
state := generateLastSignValidatorState()
assert.NoError(t, validateValidatorState(state))
})
t.Run("invalid step", func(t *testing.T) {
t.Parallel()
state := generateLastSignValidatorState()
state.Step = -1
assert.ErrorIs(t, validateValidatorState(state), errInvalidSignStateStep)
})
t.Run("invalid height", func(t *testing.T) {
t.Parallel()
state := generateLastSignValidatorState()
state.Height = -1
assert.ErrorIs(t, validateValidatorState(state), errInvalidSignStateHeight)
})
t.Run("invalid round", func(t *testing.T) {
t.Parallel()
state := generateLastSignValidatorState()
state.Round = -1
assert.ErrorIs(t, validateValidatorState(state), errInvalidSignStateRound)
})
}
func TestCommon_ValidateStateSignature(t *testing.T) {
t.Parallel()
t.Run("valid state signature", func(t *testing.T) {
t.Parallel()
var (
key = generateValidatorPrivateKey()
state = generateLastSignValidatorState()
signData = []byte("random data")
)
// Prepare the signature
signature, err := key.PrivKey.Sign(signData)
require.NoError(t, err)
state.Signature = signature
state.SignBytes = signData
assert.NoError(t, validateValidatorStateSignature(state, key.PubKey))
})
t.Run("no state signature", func(t *testing.T) {
t.Parallel()
var (
key = generateValidatorPrivateKey()
state = generateLastSignValidatorState()
)
assert.NoError(t, validateValidatorStateSignature(state, key.PubKey))
})
t.Run("signature values missing, sign bytes", func(t *testing.T) {
t.Parallel()
var (
key = generateValidatorPrivateKey()
state = generateLastSignValidatorState()
)
state.Signature = []byte("signature")
assert.ErrorIs(
t,
validateValidatorStateSignature(state, key.PubKey),
errSignatureValuesMissing,
)
})
t.Run("signature values missing, signature", func(t *testing.T) {
t.Parallel()
var (
key = generateValidatorPrivateKey()
state = generateLastSignValidatorState()
)
state.SignBytes = []byte("signature")
assert.ErrorIs(
t,
validateValidatorStateSignature(state, key.PubKey),
errSignatureValuesMissing,
)
})
t.Run("signature mismatch", func(t *testing.T) {
t.Parallel()
var (
key = generateValidatorPrivateKey()
state = generateLastSignValidatorState()
signData = []byte("random data")
)
// Prepare the signature
signature, err := key.PrivKey.Sign(signData)
require.NoError(t, err)
state.Signature = signature
state.SignBytes = []byte("something different")
assert.ErrorIs(
t,
validateValidatorStateSignature(state, key.PubKey),
errSignatureMismatch,
)
})
}
func TestCommon_ValidateNodeKey(t *testing.T) {
t.Parallel()
t.Run("valid node key", func(t *testing.T) {
t.Parallel()
key := generateNodeKey()
assert.NoError(t, validateNodeKey(key))
})
t.Run("invalid node key", func(t *testing.T) {
t.Parallel()
key := generateNodeKey()
key.PrivKey = nil
assert.ErrorIs(t, validateNodeKey(key), errInvalidNodeKey)
})
}