Browse Source

add a test

Kieran Gibb 3 years ago
parent
commit
07a2f87bb4
No account linked to committer's email address
2 changed files with 11 additions and 1 deletions
  1. 1
    1
      index.js
  2. 10
    0
      test/index.test.js

+ 1
- 1
index.js View File

@@ -39,7 +39,7 @@ class Crypto {
39 39
   }
40 40
 
41 41
   isKey (key) {
42
-    if (!(key instanceof Buffer) || typeof key !== 'string') return
42
+    if (!(key instanceof Buffer || typeof key === 'string')) return false
43 43
     const length = Buffer.from(key, 'hex').length
44 44
     return length === sodium.crypto_sign_PUBLICKEYBYTES ||
45 45
       length === sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_secretbox_KEYBYTES

+ 10
- 0
test/index.test.js View File

@@ -57,6 +57,16 @@ describe('key generation', (context) => {
57 57
     next()
58 58
   })
59 59
 
60
+  context('check a key is a valid key', (assert, next) => {
61
+    const accessKey = crypto.accessKey()
62
+    assert.ok(crypto.isKey(accessKey), '64 byte buffer is a valid read/write key')
63
+    assert.ok(crypto.isKey(accessKey.toString('hex')), '64 byte string is a valid read/write key')
64
+    const blindKey = crypto.keyPair().publicKey
65
+    assert.ok(crypto.isKey(blindKey), '32 byte public key is a valid blind replication key')
66
+    assert.ok(crypto.isKey(blindKey.toString('hex')), '32 byte string is a valid blind replication key')
67
+    next()
68
+  })
69
+
60 70
   context('check a key is a valid blind replication / public key', (assert, next) => {
61 71
     const accessKey = crypto.accessKey()
62 72
     assert.same(false, crypto.isBlindKey(accessKey), 'access key is not a valid blind replication key')