2
2
/* eslint-env mocha */
3
3
'use strict'
4
4
5
- const chai = require ( 'chai' )
6
- const { expect } = chai
5
+ const { chai, expect } = require ( 'aegir/utils/chai' )
7
6
const fail = expect . fail
8
- chai . use ( require ( 'dirty-chai' ) )
9
7
chai . use ( require ( 'chai-string' ) )
10
8
11
9
const peerUtils = require ( '../utils/creators/peer' )
@@ -40,8 +38,8 @@ describe('keychain', () => {
40
38
emptyKeystore = new Keychain ( datastore1 , { passPhrase : passPhrase } )
41
39
} )
42
40
43
- it ( 'needs a pass phrase to encrypt a key ' , ( ) => {
44
- expect ( ( ) => new Keychain ( datastore2 ) ) . to . throw ( )
41
+ it ( 'can start without a password ' , ( ) => {
42
+ expect ( ( ) => new Keychain ( datastore2 ) ) . to . not . throw ( )
45
43
} )
46
44
47
45
it ( 'needs a NIST SP 800-132 non-weak pass phrase' , ( ) => {
@@ -56,12 +54,48 @@ describe('keychain', () => {
56
54
expect ( Keychain . options ) . to . exist ( )
57
55
} )
58
56
59
- it ( 'needs a supported hashing alorithm ' , ( ) => {
57
+ it ( 'supports supported hashing alorithms ' , ( ) => {
60
58
const ok = new Keychain ( datastore2 , { passPhrase : passPhrase , dek : { hash : 'sha2-256' } } )
61
59
expect ( ok ) . to . exist ( )
60
+ } )
61
+
62
+ it ( 'does not support unsupported hashing alorithms' , ( ) => {
62
63
expect ( ( ) => new Keychain ( datastore2 , { passPhrase : passPhrase , dek : { hash : 'my-hash' } } ) ) . to . throw ( )
63
64
} )
64
65
66
+ it ( 'can list keys without a password' , async ( ) => {
67
+ const keychain = new Keychain ( datastore2 )
68
+
69
+ expect ( await keychain . listKeys ( ) ) . to . have . lengthOf ( 0 )
70
+ } )
71
+
72
+ it ( 'can find a key without a password' , async ( ) => {
73
+ const keychain = new Keychain ( datastore2 )
74
+ const keychainWithPassword = new Keychain ( datastore2 , { passPhrase : `hello-${ Date . now ( ) } -${ Date . now ( ) } ` } )
75
+ const id = `key-${ Math . random ( ) } `
76
+
77
+ await keychainWithPassword . createKey ( id , 'rsa' , 2048 )
78
+
79
+ await expect ( keychain . findKeyById ( id ) ) . to . eventually . be . ok ( )
80
+ } )
81
+
82
+ it ( 'can remove a key without a password' , async ( ) => {
83
+ const keychainWithoutPassword = new Keychain ( datastore2 )
84
+ const keychainWithPassword = new Keychain ( datastore2 , { passPhrase : `hello-${ Date . now ( ) } -${ Date . now ( ) } ` } )
85
+ const name = `key-${ Math . random ( ) } `
86
+
87
+ expect ( await keychainWithPassword . createKey ( name , 'rsa' , 2048 ) ) . to . have . property ( 'name' , name )
88
+ expect ( await keychainWithoutPassword . findKeyByName ( name ) ) . to . have . property ( 'name' , name )
89
+ await keychainWithoutPassword . removeKey ( name )
90
+ await expect ( keychainWithoutPassword . findKeyByName ( name ) ) . to . be . rejectedWith ( / d o e s n o t e x i s t / )
91
+ } )
92
+
93
+ it ( 'requires a key to create a password' , async ( ) => {
94
+ const keychain = new Keychain ( datastore2 )
95
+
96
+ await expect ( keychain . createKey ( 'derp' ) ) . to . be . rejected ( )
97
+ } )
98
+
65
99
it ( 'can generate options' , ( ) => {
66
100
const options = Keychain . generateOptions ( )
67
101
options . passPhrase = passPhrase
@@ -475,7 +509,7 @@ describe('libp2p.keychain', () => {
475
509
throw new Error ( 'should throw an error using the keychain if no passphrase provided' )
476
510
} )
477
511
478
- it ( 'can be used if a passphrase is provided' , async ( ) => {
512
+ it ( 'can be used when a passphrase is provided' , async ( ) => {
479
513
const [ libp2p ] = await peerUtils . createPeer ( {
480
514
started : false ,
481
515
config : {
@@ -492,6 +526,22 @@ describe('libp2p.keychain', () => {
492
526
expect ( kInfo ) . to . exist ( )
493
527
} )
494
528
529
+ it ( 'does not require a keychain passphrase' , async ( ) => {
530
+ const [ libp2p ] = await peerUtils . createPeer ( {
531
+ started : false ,
532
+ config : {
533
+ keychain : {
534
+ datastore : new MemoryDatastore ( )
535
+ }
536
+ }
537
+ } )
538
+
539
+ await libp2p . loadKeychain ( )
540
+
541
+ const kInfo = await libp2p . keychain . createKey ( 'keyName' , 'ed25519' )
542
+ expect ( kInfo ) . to . exist ( )
543
+ } )
544
+
495
545
it ( 'can reload keys' , async ( ) => {
496
546
const datastore = new MemoryDatastore ( )
497
547
const [ libp2p ] = await peerUtils . createPeer ( {
0 commit comments