-
Notifications
You must be signed in to change notification settings - Fork 374
/
secrets_init_test.go
242 lines (195 loc) · 5.19 KB
/
secrets_init_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
package main
import (
"context"
"path/filepath"
"testing"
"github.com/gnolang/gno/tm2/pkg/bft/privval"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func verifyValidatorKey(t *testing.T, path string) {
t.Helper()
validatorKey, err := readSecretData[privval.FilePVKey](path)
require.NoError(t, err)
assert.NoError(t, validateValidatorKey(validatorKey))
}
func verifyValidatorState(t *testing.T, path string) {
t.Helper()
validatorState, err := readSecretData[privval.FilePVLastSignState](path)
require.NoError(t, err)
assert.Zero(t, validatorState.Height)
assert.Zero(t, validatorState.Round)
assert.Zero(t, validatorState.Step)
assert.Nil(t, validatorState.Signature)
assert.Nil(t, validatorState.SignBytes)
}
func verifyNodeKey(t *testing.T, path string) {
t.Helper()
nodeKey, err := readSecretData[p2p.NodeKey](path)
require.NoError(t, err)
assert.NoError(t, validateNodeKey(nodeKey))
}
func TestSecrets_Init_All(t *testing.T) {
t.Parallel()
t.Run("invalid data directory", func(t *testing.T) {
t.Parallel()
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"secrets",
"init",
"--data-dir",
"",
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
assert.ErrorContains(t, cmdErr, errInvalidDataDir.Error())
})
t.Run("all secrets initialized", func(t *testing.T) {
t.Parallel()
// Create a temporary directory
tempDir := t.TempDir()
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"secrets",
"init",
"--data-dir",
tempDir,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the validator key is saved
verifyValidatorKey(t, filepath.Join(tempDir, defaultValidatorKeyName))
// Verify the last sign validator state is saved
verifyValidatorState(t, filepath.Join(tempDir, defaultValidatorStateName))
// Verify the node p2p key is saved
verifyNodeKey(t, filepath.Join(tempDir, defaultNodeKeyName))
})
t.Run("no secrets overwritten", func(t *testing.T) {
t.Parallel()
// Create a temporary directory
tempDir := t.TempDir()
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"secrets",
"init",
"--data-dir",
tempDir,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the validator key is saved
verifyValidatorKey(t, filepath.Join(tempDir, defaultValidatorKeyName))
// Verify the last sign validator state is saved
verifyValidatorState(t, filepath.Join(tempDir, defaultValidatorStateName))
// Verify the node p2p key is saved
verifyNodeKey(t, filepath.Join(tempDir, defaultNodeKeyName))
// Attempt to reinitialize the secrets, without the overwrite permission
cmdErr = cmd.ParseAndRun(context.Background(), args)
require.ErrorIs(t, cmdErr, errOverwriteNotEnabled)
})
}
func TestSecrets_Init_Single(t *testing.T) {
t.Parallel()
testTable := []struct {
name string
keyValue string
expectedFile string
verifyFn func(*testing.T, string)
}{
{
"validator key initialized",
validatorPrivateKeyKey,
defaultValidatorKeyName,
verifyValidatorKey,
},
{
"validator state initialized",
validatorStateKey,
defaultValidatorStateName,
verifyValidatorState,
},
{
"node p2p key initialized",
nodeIDKey,
defaultNodeKeyName,
verifyNodeKey,
},
}
for _, testCase := range testTable {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
var (
tempDir = t.TempDir()
expectedPath = filepath.Join(tempDir, testCase.expectedFile)
)
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"secrets",
"init",
"--data-dir",
tempDir,
testCase.keyValue,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the validator key is saved
testCase.verifyFn(t, expectedPath)
})
}
}
func TestSecrets_Init_Single_Overwrite(t *testing.T) {
t.Parallel()
testTable := []struct {
name string
keyValue string
expectedFile string
}{
{
"validator key not overwritten",
validatorPrivateKeyKey,
defaultValidatorKeyName,
},
{
"validator state not overwritten",
validatorStateKey,
defaultValidatorStateName,
},
{
"node p2p key not overwritten",
nodeIDKey,
defaultNodeKeyName,
},
}
for _, testCase := range testTable {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"secrets",
"init",
"--data-dir",
tempDir,
testCase.keyValue,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Attempt to reinitialize the secret, without the overwrite permission
cmdErr = cmd.ParseAndRun(context.Background(), args)
require.ErrorIs(t, cmdErr, errOverwriteNotEnabled)
})
}
}