Skip to content

Commit 171b473

Browse files
authored
Port tests to Brittle (#179)
* Port tests to Brittle * Remove `sodium-test` and `sodium-vectors`
1 parent 2c30090 commit 171b473

34 files changed

+2518
-1443
lines changed

example.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var sodium = require('./')
1+
const sodium = require('.')
22

3-
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
4-
var key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES)
5-
var message = Buffer.from('Hello, World!')
6-
var cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
3+
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
4+
const key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES)
5+
const message = Buffer.from('Hello, World!')
6+
const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
77

88
sodium.randombytes_buf(nonce) // insert random data into nonce
99
sodium.randombytes_buf(key) // insert random data into key
@@ -13,7 +13,7 @@ sodium.crypto_secretbox_easy(cipher, message, nonce, key)
1313

1414
console.log('Encrypted message:', cipher)
1515

16-
var plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
16+
const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
1717

1818
if (!sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)) {
1919
console.log('Decryption failed!')

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121
"node-gyp-build": "^4.3.0"
2222
},
2323
"devDependencies": {
24+
"brittle": "^3.2.1",
2425
"prebuildify": "^4.2.1",
25-
"sodium-test": "^0.8.0",
26-
"sodium-vectors": "^1.0.0",
27-
"standard": "^14.3.1",
28-
"tape": "^5.3.1"
26+
"standard": "^17.0.0"
2927
},
3028
"scripts": {
3129
"dev": "node-gyp rebuild",
32-
"test": "standard && tape \"test/*.js\"",
30+
"test": "standard && brittle test/*.js",
3331
"install": "node-gyp-build",
3432
"prebuild": "prebuildify --napi --strip"
3533
},

test/core_ed25519.js

+68-73
Large diffs are not rendered by default.

test/crypto_aead_chacha20poly1305_ietf.js

+133-140
Large diffs are not rendered by default.

test/crypto_aead_xchacha20poly1305_ietf.js

+135-142
Large diffs are not rendered by default.

test/crypto_auth.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
var tape = require('tape')
2-
var sodium = require('../')
1+
const test = require('brittle')
2+
const sodium = require('..')
33

4-
tape('crypto_auth', function (t) {
5-
var key = Buffer.alloc(sodium.crypto_auth_KEYBYTES)
4+
test('crypto_auth', function (t) {
5+
const key = Buffer.alloc(sodium.crypto_auth_KEYBYTES)
66
sodium.randombytes_buf(key)
77

8-
var mac = Buffer.alloc(sodium.crypto_auth_BYTES)
9-
var value = Buffer.from('Hej, Verden')
8+
const mac = Buffer.alloc(sodium.crypto_auth_BYTES)
9+
const value = Buffer.from('Hej, Verden')
1010

1111
sodium.crypto_auth(mac, value, key)
1212

13-
t.notEqual(mac, Buffer.alloc(mac.length), 'mac not blank')
14-
t.notOk(sodium.crypto_auth_verify(Buffer.alloc(mac.length), value, key), 'does not verify')
13+
t.not(mac, Buffer.alloc(mac.length), 'mac not blank')
14+
t.absent(sodium.crypto_auth_verify(Buffer.alloc(mac.length), value, key), 'does not verify')
1515
t.ok(sodium.crypto_auth_verify(mac, value, key), 'verifies')
16-
17-
t.end()
1816
})

test/crypto_box.js

+52-61
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,108 @@
1-
var tape = require('tape')
2-
var sodium = require('../')
1+
const test = require('brittle')
2+
const sodium = require('..')
33

4-
tape('crypto_box_seed_keypair', function (t) {
5-
var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
6-
var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
7-
var seed = Buffer.alloc(sodium.crypto_box_SEEDBYTES, 'lo')
4+
test('crypto_box_seed_keypair', function (t) {
5+
const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
6+
const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
7+
const seed = Buffer.alloc(sodium.crypto_box_SEEDBYTES, 'lo')
88

9-
t.throws(function () {
9+
t.exception.all(function () {
1010
sodium.crypto_box_seed_keypair()
1111
}, 'should validate input')
1212

13-
t.throws(function () {
13+
t.exception.all(function () {
1414
sodium.crypto_box_seed_keypair(Buffer.alloc(0), Buffer.alloc(0), Buffer.alloc(0))
1515
}, 'should validate input length')
1616

1717
sodium.crypto_box_seed_keypair(pk, sk, seed)
1818

19-
var eSk = '8661a95d21b134adc02881022ad86d37f32a230d537b525b997bce27aa745afc'
20-
var ePk = '425c5ba523e70411c77300bb48dd846562e6c1fcf0142d81d2567d650ce76c3b'
19+
const eSk = '8661a95d21b134adc02881022ad86d37f32a230d537b525b997bce27aa745afc'
20+
const ePk = '425c5ba523e70411c77300bb48dd846562e6c1fcf0142d81d2567d650ce76c3b'
2121

22-
t.same(pk.toString('hex'), ePk, 'seeded public key')
23-
t.same(sk.toString('hex'), eSk, 'seeded secret key')
24-
t.end()
22+
t.alike(pk.toString('hex'), ePk, 'seeded public key')
23+
t.alike(sk.toString('hex'), eSk, 'seeded secret key')
2524
})
2625

27-
tape('crypto_box_keypair', function (t) {
28-
var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
29-
var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
26+
test('crypto_box_keypair', function (t) {
27+
const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
28+
const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
3029

3130
sodium.crypto_box_keypair(pk, sk)
3231

33-
t.notEqual(pk, Buffer.alloc(pk.length), 'made public key')
34-
t.notEqual(sk, Buffer.alloc(sk.length), 'made secret key')
32+
t.not(pk, Buffer.alloc(pk.length), 'made public key')
33+
t.not(sk, Buffer.alloc(sk.length), 'made secret key')
3534

36-
t.throws(function () {
35+
t.exception.all(function () {
3736
sodium.crypto_box_keypair()
3837
}, 'should validate input')
3938

40-
t.throws(function () {
39+
t.exception.all(function () {
4140
sodium.crypto_box_keypair(Buffer.alloc(0), Buffer.alloc(0))
4241
}, 'should validate input length')
43-
44-
t.end()
4542
})
4643

47-
tape('crypto_box_detached', function (t) {
48-
var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
49-
var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
50-
var nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES)
44+
test('crypto_box_detached', function (t) {
45+
const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
46+
const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
47+
const nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES)
5148

5249
sodium.crypto_box_keypair(pk, sk)
5350

54-
var message = Buffer.from('Hello, World!')
55-
var mac = Buffer.alloc(sodium.crypto_box_MACBYTES)
56-
var cipher = Buffer.alloc(message.length)
51+
const message = Buffer.from('Hello, World!')
52+
const mac = Buffer.alloc(sodium.crypto_box_MACBYTES)
53+
const cipher = Buffer.alloc(message.length)
5754

5855
sodium.crypto_box_detached(cipher, mac, message, nonce, pk, sk)
5956

60-
t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank')
57+
t.not(cipher, Buffer.alloc(cipher.length), 'not blank')
6158

62-
var plain = Buffer.alloc(cipher.length)
63-
t.notOk(sodium.crypto_box_open_detached(plain, cipher, Buffer.alloc(mac.length), nonce, pk, sk), 'does not decrypt')
59+
const plain = Buffer.alloc(cipher.length)
60+
t.absent(sodium.crypto_box_open_detached(plain, cipher, Buffer.alloc(mac.length), nonce, pk, sk), 'does not decrypt')
6461
t.ok(sodium.crypto_box_open_detached(plain, cipher, mac, nonce, pk, sk), 'decrypts')
65-
t.same(plain, message, 'same message')
66-
67-
t.end()
62+
t.alike(plain, message, 'same message')
6863
})
6964

70-
tape('crypto_box_easy', function (t) {
71-
var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
72-
var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
73-
var nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES)
65+
test('crypto_box_easy', function (t) {
66+
const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
67+
const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
68+
const nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES)
7469

7570
sodium.crypto_box_keypair(pk, sk)
7671

77-
var message = Buffer.from('Hello, World!')
78-
var cipher = Buffer.alloc(message.length + sodium.crypto_box_MACBYTES)
72+
const message = Buffer.from('Hello, World!')
73+
const cipher = Buffer.alloc(message.length + sodium.crypto_box_MACBYTES)
7974

8075
sodium.crypto_box_easy(cipher, message, nonce, pk, sk)
8176

82-
t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank')
77+
t.not(cipher, Buffer.alloc(cipher.length), 'not blank')
8378

84-
var plain = Buffer.alloc(cipher.length - sodium.crypto_box_MACBYTES)
85-
t.notOk(sodium.crypto_box_open_easy(plain, Buffer.alloc(cipher.length), nonce, pk, sk), 'does not decrypt')
79+
const plain = Buffer.alloc(cipher.length - sodium.crypto_box_MACBYTES)
80+
t.absent(sodium.crypto_box_open_easy(plain, Buffer.alloc(cipher.length), nonce, pk, sk), 'does not decrypt')
8681
t.ok(sodium.crypto_box_open_easy(plain, cipher, nonce, pk, sk), 'decrypts')
87-
t.same(plain, message, 'same message')
88-
89-
t.end()
82+
t.alike(plain, message, 'same message')
9083
})
9184

92-
tape('crypto_box_seal', function (t) {
93-
var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
94-
var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
85+
test('crypto_box_seal', function (t) {
86+
const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
87+
const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
9588

9689
sodium.crypto_box_keypair(pk, sk)
9790

98-
var pk2 = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
99-
var sk2 = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
91+
const pk2 = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
92+
const sk2 = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
10093

10194
sodium.crypto_box_keypair(pk2, sk2)
10295

103-
var message = Buffer.from('Hello, sealed World!')
104-
var cipher = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES)
96+
const message = Buffer.from('Hello, sealed World!')
97+
const cipher = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES)
10598

10699
sodium.crypto_box_seal(cipher, message, pk)
107-
t.notEqual(cipher, message, 'did not encrypt!')
100+
t.not(cipher, message, 'did not encrypt!')
108101

109-
t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank')
102+
t.not(cipher, Buffer.alloc(cipher.length), 'not blank')
110103

111-
var plain = Buffer.alloc(cipher.length - sodium.crypto_box_SEALBYTES)
112-
t.notOk(sodium.crypto_box_seal_open(plain, cipher, pk2, sk2), 'does not decrypt')
104+
const plain = Buffer.alloc(cipher.length - sodium.crypto_box_SEALBYTES)
105+
t.absent(sodium.crypto_box_seal_open(plain, cipher, pk2, sk2), 'does not decrypt')
113106
t.ok(sodium.crypto_box_seal_open(plain, cipher, pk, sk), 'decrypts')
114-
t.same(plain, message, 'same message')
115-
116-
t.end()
107+
t.alike(plain, message, 'same message')
117108
})

0 commit comments

Comments
 (0)