Skip to content

Commit

Permalink
test(test/): Modify tests for portable invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Aug 14, 2018
1 parent d1a95b6 commit 4a33bea
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 118 deletions.
23 changes: 15 additions & 8 deletions test/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { auth, verify } = require('../auth')
const { randomBytes } = require('../random-bytes')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

/* eslint-disable camelcase */
const {
Expand All @@ -11,20 +11,23 @@ const {
crypto_auth,
} = require('../sodium')

test('auth(message, key) is a function', (t) => {
test.cb('auth(message, key) is a function', (t) => {
t.true('function' === typeof auth)
t.end()
})

test('verify(mac, message, key) is a function', (t) => {
test.cb('verify(mac, message, key) is a function', (t) => {
t.true('function' === typeof verify)
t.end()
})

test('auth.verify(mac, message, key) is a function', (t) => {
test.cb('auth.verify(mac, message, key) is a function', (t) => {
t.true('function' === typeof auth.verify)
t.true(auth.verify === verify)
t.end()
})

test('auth(message, key) throws on bad input', (t) => {
test.cb('auth(message, key) throws on bad input', (t) => {
t.throws(() => auth(), TypeError)
t.throws(() => auth(null, null), TypeError)
t.throws(() => auth(1, {}), TypeError)
Expand All @@ -33,9 +36,10 @@ test('auth(message, key) throws on bad input', (t) => {
t.throws(() => auth(Buffer.alloc(0), Buffer.alloc(0)), TypeError)
t.throws(() => auth(Buffer.from('hello'), Buffer.alloc(0)), TypeError)
t.throws(() => auth(Buffer.from('hello'), Buffer.from('key')), TypeError)
t.end()
})

test('verify(mac, message, key) throws on bad input', (t) => {
test.cb('verify(mac, message, key) throws on bad input', (t) => {
const hello = Buffer.from('hello')
const key = Buffer.from('key')
t.throws(() => verify(), TypeError)
Expand All @@ -51,18 +55,20 @@ test('verify(mac, message, key) throws on bad input', (t) => {
t.throws(() => verify(null, hello, key), TypeError)
t.throws(() => verify('', hello, key), TypeError)
t.throws(() => verify(hello, hello, key), TypeError)
t.end()
})

test('auth(message, key) creates a MAC', (t) => {
test.cb('auth(message, key) creates a MAC', (t) => {
const message = Buffer.from('hello')
const key = randomBytes(crypto_auth_KEYBYTES)
const mac = auth(message, key)
t.true(isBuffer(mac))
t.true(crypto_auth_BYTES === mac.length)
t.true(Boolean(crypto_auth_verify(mac, message, key)))
t.end()
})

test('verify(mac, message, key) verifies a MAC', (t) => {
test.cb('verify(mac, message, key) verifies a MAC', (t) => {
const message = Buffer.from('hello')
const key = randomBytes(crypto_auth_KEYBYTES)
const mac = auth(message, key)
Expand All @@ -75,4 +81,5 @@ test('verify(mac, message, key) verifies a MAC', (t) => {
const out = Buffer.allocUnsafe(crypto_auth_BYTES)
crypto_auth(out, message, key)
t.true(verify(out, message, key))
t.end()
})
2 changes: 1 addition & 1 deletion test/blake2b.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { blake2b } = require('../blake2b')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

test('blake2b(buffer, size)', async (t) => {
t.throws(() => blake2b(0, 0), TypeError)
Expand Down
25 changes: 17 additions & 8 deletions test/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ const { randomBytes } = require('../random-bytes')
const increment = require('increment-buffer')
const { unbox } = require('../unbox')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

/* eslint-disable camelcase */
const {
crypto_secretbox_MACBYTES,
crypto_secretbox_NONCEBYTES,
} = require('sodium-universal')

test('box(buffer, opts) is a function', (t) => {
test.cb('box(buffer, opts) is a function', (t) => {
t.true('function' === typeof box)
t.end()
})

test('box(buffer, opts) throws on bad input', (t) => {
test.cb('box(buffer, opts) throws on bad input', (t) => {
t.throws(() => box(), TypeError)
t.throws(() => box(null), TypeError)
t.throws(() => box(true), TypeError)
Expand All @@ -24,9 +25,10 @@ test('box(buffer, opts) throws on bad input', (t) => {
t.throws(() => box(Buffer.from('hello')), TypeError)
t.throws(() => box(Buffer.from('hello'), { secret: null }), TypeError)
t.throws(() => box(Buffer.from('hello'), { key: null }), TypeError)
t.end()
})

test('box(buffer, opts) basic with key and nonce (detached)', (t) => {
test.cb('box(buffer, opts) basic with key and nonce (detached)', (t) => {
const buffer = Buffer.from('hello')
const nonce = randomBytes(crypto_secretbox_NONCEBYTES)
const key = Buffer.alloc(32)
Expand All @@ -40,9 +42,11 @@ test('box(buffer, opts) basic with key and nonce (detached)', (t) => {
+ crypto_secretbox_MACBYTES
+ buffer.length
))

t.end()
})

test('box(buffer, opts) basic secret', (t) => {
test.cb('box(buffer, opts) basic secret', (t) => {
const buffer = Buffer.from('hello')
const nonce = randomBytes(crypto_secretbox_NONCEBYTES)
const key = Buffer.alloc(32)
Expand All @@ -57,9 +61,11 @@ test('box(buffer, opts) basic secret', (t) => {
+ crypto_secretbox_MACBYTES
+ buffer.length
))

t.end()
})

test('box(buffer, opts) exposes nonce', (t) => {
test.cb('box(buffer, opts) exposes nonce', (t) => {
const buffer = Buffer.from('hello')
const nonce = randomBytes(crypto_secretbox_NONCEBYTES)
const key = Buffer.alloc(32)
Expand All @@ -68,17 +74,20 @@ test('box(buffer, opts) exposes nonce', (t) => {
const boxed = box(buffer, { secret })

t.true(isBuffer(boxed.nonce))
t.end()
})

test('createBoxStream(opts) is a function', (t) => {
test.cb('createBoxStream(opts) is a function', (t) => {
t.true('function' === typeof createBoxStream)
t.end()
})

test('createBoxStream(opts) throws on bad input', (t) => {
test.cb('createBoxStream(opts) throws on bad input', (t) => {
t.throws(() => createBoxStream(null), TypeError)
t.throws(() => createBoxStream(true), TypeError)
t.throws(() => createBoxStream(123), TypeError)
t.throws(() => createBoxStream(), TypeError)
t.end()
})

test.cb('createBoxStream(opts) returns stream that boxes input', (t) => {
Expand Down
10 changes: 7 additions & 3 deletions test/curve25519.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { keyPair, shared } = require('../curve25519')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

test('curve25519.keyPair(seed)', (t) => {
test.cb('curve25519.keyPair(seed)', (t) => {
t.throws(() => keyPair(null), TypeError)
t.throws(() => keyPair(0), TypeError)
t.throws(() => keyPair(''), TypeError)
Expand All @@ -18,9 +18,11 @@ test('curve25519.keyPair(seed)', (t) => {
t.true(isBuffer(keyPair().secretKey))
t.true(32 === keyPair().publicKey.length)
t.true(32 === keyPair().secretKey.length)

t.end()
})

test('curve25519.shared(secretKey, publicKey)', (t) => {
test.cb('curve25519.shared(secretKey, publicKey)', (t) => {
t.throws(() => shared(), TypeError)
t.throws(() => shared(0, 0), TypeError)
t.throws(() => shared('', ''), TypeError)
Expand All @@ -37,4 +39,6 @@ test('curve25519.shared(secretKey, publicKey)', (t) => {

t.true(isBuffer(key))
t.true(32 === key.length)

t.end()
})
7 changes: 5 additions & 2 deletions test/discovery-key.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const { discoveryKey } = require('../discovery-key')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

test('discoveryKey(buffer, size, key)', async (t) => {
test.cb('discoveryKey(buffer, size, key)', (t) => {
t.throws(() => discoveryKey(0, 0), TypeError)
t.throws(() => discoveryKey(Buffer.alloc(0), 0), TypeError)
t.throws(() => discoveryKey(Buffer.from('message'), 0), TypeError)
t.throws(() => discoveryKey(Buffer.from('message'), -1), TypeError)

t.true(isBuffer(discoveryKey(Buffer.from('message'))))
t.true(isBuffer(discoveryKey(Buffer.from('message'), 16)))
t.true(32 === discoveryKey(Buffer.from('message')).length)
t.true(64 === discoveryKey(Buffer.from('message'), 64).length)

t.end()
})
15 changes: 11 additions & 4 deletions test/ed25519.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { keyPair, verify, sign } = require('../ed25519')
const isBuffer = require('is-buffer')
const test = require('ava')
const test = require('./helpers/runner')

/* eslint-disable camelcase */
const {
Expand All @@ -9,7 +9,7 @@ const {
crypto_sign_BYTES,
} = require('sodium-universal')

test('ed25519.keyPair(seed)', (t) => {
test.cb('ed25519.keyPair(seed)', (t) => {
t.throws(() => keyPair(null), TypeError)
t.throws(() => keyPair(0), TypeError)
t.throws(() => keyPair(''), TypeError)
Expand All @@ -18,15 +18,18 @@ test('ed25519.keyPair(seed)', (t) => {
t.throws(() => keyPair(Buffer.alloc(0)), TypeError)
t.throws(() => keyPair(Buffer.alloc(crypto_sign_SEEDBYTES + 1)), TypeError)
t.throws(() => keyPair(Buffer.alloc(crypto_sign_SEEDBYTES - 1)), TypeError)

t.true('object' === typeof keyPair())
t.true('object' === typeof keyPair(Buffer.alloc(32).fill('hello')))
t.true(isBuffer(keyPair().publicKey))
t.true(isBuffer(keyPair().secretKey))
t.true(32 === keyPair().publicKey.length)
t.true(64 === keyPair().secretKey.length)

t.end()
})

test('ed25519.verify(signature, message, publicKey)', (t) => {
test.cb('ed25519.verify(signature, message, publicKey)', (t) => {
const { publicKey, secretKey } = keyPair()

t.throws(() => verify(0, 0, 0), TypeError)
Expand Down Expand Up @@ -102,9 +105,11 @@ test('ed25519.verify(signature, message, publicKey)', (t) => {
t.throws(() => verify(signature, message, ''), TypeError)

t.true(verify(signature, message, publicKey))

t.end()
})

test('ed25519.sign(message, secretKey)', (t) => {
test.cb('ed25519.sign(message, secretKey)', (t) => {
t.throws(() => sign(0, 0), TypeError)
t.throws(() => sign(null, 0), TypeError)
t.throws(() => sign(true, 0), TypeError)
Expand All @@ -127,4 +132,6 @@ test('ed25519.sign(message, secretKey)', (t) => {
const { secretKey } = keyPair()

t.true(isBuffer(sign(Buffer.from('message'), secretKey)))

t.end()
})
Loading

0 comments on commit 4a33bea

Please sign in to comment.