Skip to content

Commit 934a256

Browse files
committed
break up tests a little more
1 parent 87de1de commit 934a256

File tree

4 files changed

+99
-107
lines changed

4 files changed

+99
-107
lines changed

test/aes.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@ var test = require('tape')
22
var crypto = require('browserify-cipher/browser')
33
var randomBytes = require('randombytes')
44

5-
test('ciphers', function (t) {
6-
crypto.listCiphers().forEach(function (cipher) {
7-
t.test(cipher, function (t) {
8-
t.plan(1)
9-
var data = randomBytes(562)
10-
var password = randomBytes(20)
11-
var crypter = crypto.createCipher(cipher, password)
12-
var decrypter = crypto.createDecipher(cipher, password)
13-
var out = []
14-
out.push(decrypter.update(crypter.update(data)))
15-
out.push(decrypter.update(crypter.final()))
16-
if (cipher.indexOf('gcm') > -1) {
17-
decrypter.setAuthTag(crypter.getAuthTag())
18-
}
19-
out.push(decrypter.final())
20-
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
21-
})
5+
crypto.listCiphers().forEach(function (cipher) {
6+
test('ciphers: ' + cipher, function (t) {
7+
t.plan(1)
8+
var data = randomBytes(562)
9+
var password = randomBytes(20)
10+
var crypter = crypto.createCipher(cipher, password)
11+
var decrypter = crypto.createDecipher(cipher, password)
12+
var out = []
13+
out.push(decrypter.update(crypter.update(data)))
14+
out.push(decrypter.update(crypter.final()))
15+
if (cipher.indexOf('gcm') > -1) {
16+
decrypter.setAuthTag(crypter.getAuthTag())
17+
}
18+
out.push(decrypter.final())
19+
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
2220
})
2321
})
2422

test/create-hash.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,32 @@ testLib('createHash in crypto-browserify', require('../').createHash)
88
testLib('create-hash/browser', require('create-hash/browser'))
99

1010
function testLib (name, createHash) {
11-
test(name, function (t) {
12-
algorithms.forEach(function (algorithm) {
13-
t.test('test ' + algorithm + ' against test vectors', function (t) {
14-
vectors.forEach(function (obj, i) {
15-
var input = new Buffer(obj.input, 'base64')
16-
var node = obj[algorithm]
17-
var js = createHash(algorithm).update(input).digest('hex')
18-
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
19-
})
11+
algorithms.forEach(function (algorithm) {
12+
test(name + ' test ' + algorithm + ' against test vectors', function (t) {
13+
vectors.forEach(function (obj, i) {
14+
var input = new Buffer(obj.input, 'base64')
15+
var node = obj[algorithm]
16+
var js = createHash(algorithm).update(input).digest('hex')
17+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
18+
})
2019

21-
encodings.forEach(function (encoding) {
22-
vectors.forEach(function (obj, i) {
23-
var input = new Buffer(obj.input, 'base64').toString(encoding)
24-
var node = obj[algorithm]
25-
var js = createHash(algorithm).update(input, encoding).digest('hex')
26-
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
27-
})
28-
})
20+
encodings.forEach(function (encoding) {
2921
vectors.forEach(function (obj, i) {
30-
var input = new Buffer(obj.input, 'base64')
22+
var input = new Buffer(obj.input, 'base64').toString(encoding)
3123
var node = obj[algorithm]
32-
var hash = createHash(algorithm)
33-
hash.end(input)
34-
var js = hash.read().toString('hex')
35-
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
24+
var js = createHash(algorithm).update(input, encoding).digest('hex')
25+
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
3626
})
37-
t.end()
3827
})
28+
vectors.forEach(function (obj, i) {
29+
var input = new Buffer(obj.input, 'base64')
30+
var node = obj[algorithm]
31+
var hash = createHash(algorithm)
32+
hash.end(input)
33+
var js = hash.read().toString('hex')
34+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
35+
})
36+
t.end()
3937
})
4038
})
4139
}

test/create-hmac.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@ testLib('createHmac in crypto-browserify', require('../').createHmac)
66
testLib('create-hmac/browser', require('create-hmac/browser'))
77

88
function testLib (name, createHmac) {
9-
test(name, function (t) {
10-
algorithms.forEach(function (alg) {
11-
t.test('hmac(' + alg + ')', function (t) {
12-
vectors.forEach(function (input, i) {
13-
var output = createHmac(alg, new Buffer(input.key, 'hex'))
14-
.update(input.data, 'hex').digest()
9+
algorithms.forEach(function (alg) {
10+
test(name + ' hmac(' + alg + ')', function (t) {
11+
vectors.forEach(function (input, i) {
12+
var output = createHmac(alg, new Buffer(input.key, 'hex'))
13+
.update(input.data, 'hex').digest()
1514

16-
output = input.truncate ? output.slice(0, input.truncate) : output
17-
t.equal(output.toString('hex'), input[alg])
18-
})
19-
t.end()
15+
output = input.truncate ? output.slice(0, input.truncate) : output
16+
t.equal(output.toString('hex'), input[alg])
2017
})
18+
t.end()
19+
})
2120

22-
t.test('hmac(' + alg + ')', function (t) {
23-
vectors.forEach(function (input, i) {
24-
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
21+
test('hmac(' + alg + ')', function (t) {
22+
vectors.forEach(function (input, i) {
23+
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
2524

26-
hmac.end(input.data, 'hex')
27-
var output = hmac.read()
25+
hmac.end(input.data, 'hex')
26+
var output = hmac.read()
2827

29-
output = input.truncate ? output.slice(0, input.truncate) : output
30-
t.equal(output.toString('hex'), input[alg])
31-
})
32-
t.end()
28+
output = input.truncate ? output.slice(0, input.truncate) : output
29+
t.equal(output.toString('hex'), input[alg])
3330
})
31+
t.end()
3432
})
3533
})
3634
}

test/ecdh.js

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,53 @@ var test = require('tape')
88
var createECDH1 = require('../').createECDH
99
var createECDH2 = require('create-ecdh/browser')
1010

11-
test('createECDH', function (t) {
12-
mods.forEach(function (mod) {
13-
t.test(mod + ' uncompressed', function (t) {
14-
t.plan(2)
15-
var dh1 = createECDH1(mod)
16-
dh1.generateKeys()
17-
var dh2 = createECDH2(mod)
18-
dh2.generateKeys()
19-
var pubk1 = dh1.getPublicKey()
20-
var pubk2 = dh2.getPublicKey()
21-
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
22-
var pub1 = dh1.computeSecret(pubk2).toString('hex')
23-
var pub2 = dh2.computeSecret(pubk1).toString('hex')
24-
t.equals(pub1, pub2, 'equal secrets')
25-
})
11+
mods.forEach(function (mod) {
12+
test('createECDH: ' + mod + ' uncompressed', function (t) {
13+
t.plan(2)
14+
var dh1 = createECDH1(mod)
15+
dh1.generateKeys()
16+
var dh2 = createECDH2(mod)
17+
dh2.generateKeys()
18+
var pubk1 = dh1.getPublicKey()
19+
var pubk2 = dh2.getPublicKey()
20+
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
21+
var pub1 = dh1.computeSecret(pubk2).toString('hex')
22+
var pub2 = dh2.computeSecret(pubk1).toString('hex')
23+
t.equals(pub1, pub2, 'equal secrets')
24+
})
2625

27-
t.test(mod + ' compressed', function (t) {
28-
t.plan(2)
29-
var dh1 = createECDH1(mod)
30-
dh1.generateKeys()
31-
var dh2 = createECDH2(mod)
32-
dh2.generateKeys()
33-
var pubk1 = dh1.getPublicKey(null, 'compressed')
34-
var pubk2 = dh2.getPublicKey(null, 'compressed')
35-
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
36-
var pub1 = dh1.computeSecret(pubk2).toString('hex')
37-
var pub2 = dh2.computeSecret(pubk1).toString('hex')
38-
t.equals(pub1, pub2, 'equal secrets')
39-
})
26+
test('createECDH: ' + mod + ' compressed', function (t) {
27+
t.plan(2)
28+
var dh1 = createECDH1(mod)
29+
dh1.generateKeys()
30+
var dh2 = createECDH2(mod)
31+
dh2.generateKeys()
32+
var pubk1 = dh1.getPublicKey(null, 'compressed')
33+
var pubk2 = dh2.getPublicKey(null, 'compressed')
34+
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
35+
var pub1 = dh1.computeSecret(pubk2).toString('hex')
36+
var pub2 = dh2.computeSecret(pubk1).toString('hex')
37+
t.equals(pub1, pub2, 'equal secrets')
38+
})
4039

41-
t.test(mod + ' set stuff', function (t) {
42-
t.plan(5)
43-
var dh1 = createECDH1(mod)
44-
var dh2 = createECDH2(mod)
45-
dh1.generateKeys()
46-
dh2.generateKeys()
47-
dh1.setPrivateKey(dh2.getPrivateKey())
48-
dh1.setPublicKey(dh2.getPublicKey())
49-
var priv1 = dh1.getPrivateKey('hex')
50-
var priv2 = dh2.getPrivateKey('hex')
51-
t.equals(priv1, priv2, 'same private key')
52-
var pubk1 = dh1.getPublicKey()
53-
var pubk2 = dh2.getPublicKey()
54-
t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed')
55-
t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed')
56-
t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid')
57-
var pub1 = dh1.computeSecret(pubk2).toString('hex')
58-
var pub2 = dh2.computeSecret(pubk1).toString('hex')
59-
t.equals(pub1, pub2, 'equal secrets')
60-
})
40+
test('createECDH: ' + mod + ' set stuff', function (t) {
41+
t.plan(5)
42+
var dh1 = createECDH1(mod)
43+
var dh2 = createECDH2(mod)
44+
dh1.generateKeys()
45+
dh2.generateKeys()
46+
dh1.setPrivateKey(dh2.getPrivateKey())
47+
dh1.setPublicKey(dh2.getPublicKey())
48+
var priv1 = dh1.getPrivateKey('hex')
49+
var priv2 = dh2.getPrivateKey('hex')
50+
t.equals(priv1, priv2, 'same private key')
51+
var pubk1 = dh1.getPublicKey()
52+
var pubk2 = dh2.getPublicKey()
53+
t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed')
54+
t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed')
55+
t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid')
56+
var pub1 = dh1.computeSecret(pubk2).toString('hex')
57+
var pub2 = dh2.computeSecret(pubk1).toString('hex')
58+
t.equals(pub1, pub2, 'equal secrets')
6159
})
6260
})

0 commit comments

Comments
 (0)