-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathkeypairgen_test.go
More file actions
273 lines (218 loc) · 8.1 KB
/
Copy pathkeypairgen_test.go
File metadata and controls
273 lines (218 loc) · 8.1 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package certfier
import (
"os"
"path/filepath"
"testing"
driver2 "github.com/hyperledger-labs/fabric-token-sdk/token/core"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockPPMFactory struct {
pp driver.PublicParameters
ppErr error
publicParamsManager driver.PublicParamsManager
publicParamsManagerErr error
}
func (m *mockPPMFactory) PublicParametersFromBytes(params []byte) (driver.PublicParameters, error) {
return m.pp, m.ppErr
}
func (m *mockPPMFactory) NewPublicParametersManager(pp driver.PublicParameters) (driver.PublicParamsManager, error) {
return m.publicParamsManager, m.publicParamsManagerErr
}
func (m *mockPPMFactory) NewValidator(pp driver.PublicParameters) (driver.Validator, error) {
return nil, nil
}
// TestKeyPairGenCmd tests the KeyPairGenCmd function.
func TestKeyPairGenCmd(t *testing.T) {
cmd := KeyPairGenCmd()
assert.NotNil(t, cmd)
assert.Equal(t, "certifier-keygen", cmd.Use)
}
// TestKeyPairGen tests the keyPairGen function.
func TestKeyPairGen(t *testing.T) {
wd, _ := os.Getwd()
testdataPath := filepath.Join(wd, "..", "..", "testdata", "zkatdlognoghv1_pp.json")
tempDir := t.TempDir()
t.Run("success_mock", func(t *testing.T) {
defer func() { ppmFactoryService = nil }()
mockPPM := &mock.PublicParamsManager{}
mockPPM.NewCertifierKeyPairReturns([]byte("sk"), []byte("pk"), nil)
mockPP := &mock.PublicParameters{}
mockPP.TokenDriverNameReturns("mock")
mockPP.TokenDriverVersionReturns(1)
mockFactory := &mockPPMFactory{
pp: mockPP,
publicParamsManager: mockPPM,
}
ppmFactoryService = driver2.NewPPManagerFactoryService(driver2.NamedFactory[driver.PPMFactory]{
Name: "mock.v1",
Driver: mockFactory,
})
ppFile := filepath.Join(tempDir, "mock_pp.json")
err := os.WriteFile(ppFile, []byte(`{"identifier":"mock.v1","raw":"YmFzZTY0"}`), 0644)
require.NoError(t, err)
ppPath = ppFile
output = filepath.Join(tempDir, "out")
err = keyPairGen()
require.NoError(t, err)
assert.FileExists(t, filepath.Join(output, "certifier.sk"))
assert.FileExists(t, filepath.Join(output, "certifier.pk"))
})
t.Run("success_real", func(t *testing.T) {
ppPath = testdataPath
output = tempDir
_ = keyPairGen()
// NewCertifierKeyPair is currently hardcoded to return "not supported" in core/common/ppm.go
})
t.Run("read_pp_fail", func(t *testing.T) {
ppPath = "nonexistent.json"
output = tempDir
err := keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed reading public parameters")
})
t.Run("unmarshal_pp_fail", func(t *testing.T) {
invalidPP := filepath.Join(tempDir, "invalid_pp.json")
err := os.WriteFile(invalidPP, []byte("invalid content"), 0644)
require.NoError(t, err)
ppPath = invalidPP
output = tempDir
err = keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed unmarshalling public parameters")
})
t.Run("new_ppm_fail", func(t *testing.T) {
// Provide bytes that pass deserialization but fail ppm instantiation (e.g. invalid internal content)
// We'll use the same trick as before but for zkatdlognogh directly if possible,
// or just use a mock that returns error on NewPublicParametersManager.
defer func() { ppmFactoryService = nil }()
mockPP := &mock.PublicParameters{}
mockPP.TokenDriverNameReturns("mock")
mockPP.TokenDriverVersionReturns(1)
mockFactory := &mockPPMFactory{
pp: mockPP,
publicParamsManagerErr: assert.AnError,
}
ppmFactoryService = driver2.NewPPManagerFactoryService(driver2.NamedFactory[driver.PPMFactory]{
Name: "mock.v1",
Driver: mockFactory,
})
ppFile := filepath.Join(tempDir, "mock_pp_fail.json")
err := os.WriteFile(ppFile, []byte(`{"identifier":"mock.v1","raw":""}`), 0644)
require.NoError(t, err)
ppPath = ppFile
err = keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed instantiating public parameters manager")
})
t.Run("mkdir_fail", func(t *testing.T) {
defer func() { ppmFactoryService = nil }()
mockPPM := &mock.PublicParamsManager{}
mockPPM.NewCertifierKeyPairReturns([]byte("sk"), []byte("pk"), nil)
mockPP := &mock.PublicParameters{}
mockPP.TokenDriverNameReturns("mock")
mockPP.TokenDriverVersionReturns(1)
mockFactory := &mockPPMFactory{
pp: mockPP,
publicParamsManager: mockPPM,
}
ppmFactoryService = driver2.NewPPManagerFactoryService(driver2.NamedFactory[driver.PPMFactory]{
Name: "mock.v1",
Driver: mockFactory,
})
ppFile := filepath.Join(tempDir, "mock_pp_mkdir.json")
err := os.WriteFile(ppFile, []byte(`{"identifier":"mock.v1","raw":""}`), 0644)
require.NoError(t, err)
// Create a file where a directory should be
blockedPath := filepath.Join(tempDir, "blocked")
err = os.WriteFile(blockedPath, []byte("file"), 0644)
require.NoError(t, err)
ppPath = ppFile
output = filepath.Join(blockedPath, "subdir")
err = keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed making output dir")
})
t.Run("write_sk_fail", func(t *testing.T) {
defer func() { ppmFactoryService = nil }()
mockPPM := &mock.PublicParamsManager{}
mockPPM.NewCertifierKeyPairReturns([]byte("sk"), []byte("pk"), nil)
mockPP := &mock.PublicParameters{}
mockPP.TokenDriverNameReturns("mock")
mockPP.TokenDriverVersionReturns(1)
mockFactory := &mockPPMFactory{
pp: mockPP,
publicParamsManager: mockPPM,
}
ppmFactoryService = driver2.NewPPManagerFactoryService(driver2.NamedFactory[driver.PPMFactory]{
Name: "mock.v1",
Driver: mockFactory,
})
ppFile := filepath.Join(tempDir, "mock_pp_sk.json")
err := os.WriteFile(ppFile, []byte(`{"identifier":"mock.v1","raw":""}`), 0644)
require.NoError(t, err)
ppPath = ppFile
// Use a directory that exists but we'll try to write to a path that's a directory
skDir := filepath.Join(tempDir, "certifier.sk")
err = os.MkdirAll(skDir, 0750)
require.NoError(t, err)
output = tempDir
err = keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed writing certifier secret key to file")
})
t.Run("write_pk_fail", func(t *testing.T) {
defer func() { ppmFactoryService = nil }()
mockPPM := &mock.PublicParamsManager{}
mockPPM.NewCertifierKeyPairReturns([]byte("sk"), []byte("pk"), nil)
mockPP := &mock.PublicParameters{}
mockPP.TokenDriverNameReturns("mock")
mockPP.TokenDriverVersionReturns(1)
mockFactory := &mockPPMFactory{
pp: mockPP,
publicParamsManager: mockPPM,
}
ppmFactoryService = driver2.NewPPManagerFactoryService(driver2.NamedFactory[driver.PPMFactory]{
Name: "mock.v1",
Driver: mockFactory,
})
ppFile := filepath.Join(tempDir, "mock_pp_pk.json")
err := os.WriteFile(ppFile, []byte(`{"identifier":"mock.v1","raw":""}`), 0644)
require.NoError(t, err)
ppPath = ppFile
// Use a directory that exists but we'll try to write to a path that's a directory
pkDir := filepath.Join(tempDir, "certifier.pk")
err = os.MkdirAll(pkDir, 0750)
require.NoError(t, err)
// ensure sk is NOT a directory
_ = os.RemoveAll(filepath.Join(tempDir, "certifier.sk"))
output = tempDir
err = keyPairGen()
require.Error(t, err)
assert.Contains(t, err.Error(), "failed writing certifier public key to file")
})
}
// TestCobraCommand tests the Cobra command for generating certifier key pairs.
func TestCobraCommand(t *testing.T) {
wd, _ := os.Getwd()
testdataPath := filepath.Join(wd, "..", "..", "testdata", "zkatdlognoghv1_pp.json")
tempDir := t.TempDir()
cmd := KeyPairGenCmd()
cmd.SetArgs([]string{"--pppath", testdataPath, "--output", tempDir})
err := cmd.Execute()
// It will return "not supported" currently
if err != nil {
assert.Contains(t, err.Error(), "not supported")
}
// Test trailing args
cmd.SetArgs([]string{"--pppath", testdataPath, "extra"})
err = cmd.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "trailing args detected")
}