@@ -5,17 +5,13 @@ package e2e
5
5
6
6
import (
7
7
"bytes"
8
- "crypto/ed25519"
9
- "crypto/rand"
10
- "crypto/x509"
11
8
_ "embed"
12
- "encoding/pem"
13
- "fmt"
14
9
"os"
15
10
"path/filepath"
16
11
"strings"
17
12
18
13
clicmd "github.com/agntcy/dir/cli/cmd"
14
+ initcmd "github.com/agntcy/dir/cli/cmd/network/init"
19
15
"github.com/onsi/ginkgo/v2"
20
16
"github.com/onsi/gomega"
21
17
"github.com/opencontainers/go-digest"
@@ -185,29 +181,29 @@ var _ = ginkgo.Describe("dirctl end-to-end tests", func() {
185
181
186
182
ginkgo .Context ("network commands" , func () {
187
183
var (
188
- tempKeyDir string
184
+ tempDir string
189
185
tempKeyPath string
190
186
)
191
187
192
188
ginkgo .BeforeEach (func () {
193
189
// Create temporary directory for test keys
194
190
var err error
195
- tempKeyDir , err = os .MkdirTemp ("" , "network-test" )
191
+ tempDir , err = os .MkdirTemp ("" , "network-test" )
196
192
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
197
193
198
194
// Generate OpenSSL-style ED25519 key
199
- privateKey , err := generateED25519OpenSSLKey ()
195
+ _ , privateKey , err := initcmd . GenerateED25519OpenSSLKey ()
200
196
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
201
197
202
198
// Write the private key to a temporary file
203
- tempKeyPath = filepath .Join (tempKeyDir , "test_key" )
199
+ tempKeyPath = filepath .Join (tempDir , "test_key" )
204
200
err = os .WriteFile (tempKeyPath , privateKey , 0o0600 )
205
201
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
206
202
})
207
203
208
204
ginkgo .AfterEach (func () {
209
205
// Cleanup temporary directory
210
- err := os .RemoveAll (tempKeyDir )
206
+ err := os .RemoveAll (tempDir )
211
207
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
212
208
})
213
209
@@ -263,82 +259,70 @@ var _ = ginkgo.Describe("dirctl end-to-end tests", func() {
263
259
})
264
260
265
261
ginkgo .Context ("init command" , func () {
266
- ginkgo .It ("should generate a new peer ID" , func () {
262
+ ginkgo .It ("should generate a new peer ID and save the key to specified output" , func () {
263
+ outputPath := filepath .Join (tempDir , "generated.key" )
267
264
var outputBuffer bytes.Buffer
268
265
269
266
initCmd := clicmd .RootCmd
270
267
initCmd .SetOut (& outputBuffer )
271
268
initCmd .SetArgs ([]string {
272
269
"network" ,
273
270
"init" ,
271
+ "--output" ,
272
+ outputPath ,
274
273
})
275
274
276
275
err := initCmd .Execute ()
277
276
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
278
277
279
- // Verify that the output is not empty
278
+ // Verify that the output file exists
279
+ _ , err = os .Stat (outputPath )
280
+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
281
+
282
+ // Verify file permissions
283
+ info , err := os .Stat (outputPath )
284
+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
285
+ gomega .Expect (info .Mode ().Perm ()).To (gomega .Equal (os .FileMode (0o0600 )))
286
+
287
+ // Verify that the output is not empty and is a valid peer ID
280
288
output := strings .TrimSpace (outputBuffer .String ())
281
289
gomega .Expect (output ).NotTo (gomega .BeEmpty ())
290
+ gomega .Expect (output ).To (gomega .HavePrefix ("12D3" ))
282
291
283
- // Store the first peer ID
284
- firstPeerID := output
285
-
286
- // Run the command again to verify we get a different peer ID
287
- var secondBuffer bytes.Buffer
288
- initCmd .SetOut (& secondBuffer )
292
+ // Verify that the generated key can be used with the info command
293
+ var infoBuffer bytes.Buffer
294
+ infoCmd := clicmd .RootCmd
295
+ infoCmd .SetOut (& infoBuffer )
296
+ infoCmd .SetArgs ([]string {
297
+ "network" ,
298
+ "info" ,
299
+ outputPath ,
300
+ })
289
301
290
- err = initCmd .Execute ()
302
+ err = infoCmd .Execute ()
291
303
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
292
304
293
- secondPeerID := strings .TrimSpace (secondBuffer .String ())
294
- gomega .Expect (secondPeerID ).NotTo (gomega .BeEmpty ())
295
-
296
- // Verify that we get different peer IDs each time
297
- gomega .Expect (secondPeerID ).NotTo (gomega .Equal (firstPeerID ))
305
+ // Verify that info command returns the same peer ID
306
+ infoOutput := strings .TrimSpace (infoBuffer .String ())
307
+ gomega .Expect (infoOutput ).To (gomega .Equal (output ))
298
308
})
299
309
300
- ginkgo .It ("should generate valid peer IDs" , func () {
310
+ ginkgo .It ("should fail when output directory doesn't exist and cannot be created" , func () {
311
+ // Try to write to a location that should fail
301
312
var outputBuffer bytes.Buffer
302
313
303
314
initCmd := clicmd .RootCmd
304
315
initCmd .SetOut (& outputBuffer )
305
316
initCmd .SetArgs ([]string {
306
317
"network" ,
307
318
"init" ,
319
+ "--output" ,
320
+ "/nonexistent/directory/key.pem" ,
308
321
})
309
322
310
323
err := initCmd .Execute ()
311
- gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
312
-
313
- output := strings .TrimSpace (outputBuffer .String ())
314
-
315
- // Verify the peer ID format
316
- // Peer IDs typically start with "12D3" in base58 encoding
317
- gomega .Expect (output ).To (gomega .HavePrefix ("12D3" ))
318
- gomega .Expect (len (output )).To (gomega .BeNumerically (">" , 40 )) // Peer IDs should be reasonably long
324
+ gomega .Expect (err ).To (gomega .HaveOccurred ())
319
325
})
320
326
})
321
327
})
322
328
})
323
-
324
- func generateED25519OpenSSLKey () ([]byte , error ) {
325
- // Generate ED25519 key pair
326
- _ , priv , err := ed25519 .GenerateKey (rand .Reader )
327
- if err != nil {
328
- return nil , fmt .Errorf ("failed to generate ED25519 key pair: %w" , err )
329
- }
330
-
331
- // Marshal the private key to PKCS#8 format
332
- privBytes , err := x509 .MarshalPKCS8PrivateKey (priv )
333
- if err != nil {
334
- return nil , fmt .Errorf ("failed to marshal private key: %w" , err )
335
- }
336
-
337
- // Create PEM block
338
- pemBlock := & pem.Block {
339
- Type : "PRIVATE KEY" ,
340
- Bytes : privBytes ,
341
- }
342
-
343
- return pem .EncodeToMemory (pemBlock ), nil
344
- }
0 commit comments