Skip to content

Commit b2da039

Browse files
committed
Merge pull request #139 from crypto-browserify/browser-tests
Browser tests
2 parents 0d36370 + 3b645f7 commit b2da039

File tree

9 files changed

+197
-130
lines changed

9 files changed

+197
-130
lines changed

.travis.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
sudo: false
22
language: node_js
3-
node_js:
4-
- "0.10"
5-
- "0.11"
6-
- "0.12"
7-
- "iojs"
8-
env:
9-
- TEST_SUITE=standard
10-
- TEST_SUITE=unit
3+
matrix:
4+
include:
5+
- node_js: '0.10'
6+
env: TEST_SUITE=unit
7+
- node_js: '0.11'
8+
env: TEST_SUITE=unit
9+
- node_js: '0.12'
10+
env: TEST_SUITE=unit
11+
- node_js: '4'
12+
env: TEST_SUITE=unit
13+
- node_js: '4'
14+
env: TEST_SUITE=standard
15+
- node_js: '4'
16+
env: TEST_SUITE=browser BROWSER_NAME=ie BROWSER_VERSION="10..latest"
17+
- node_js: '4'
18+
env: TEST_SUITE=browser BROWSER_NAME=chrome BROWSER_VERSION="44..beta"
19+
- node_js: '4'
20+
env: TEST_SUITE=browser BROWSER_NAME=firefox BROWSER_VERSION="40..latest"
21+
- node_js: '4'
22+
env: TEST_SUITE=browser BROWSER_NAME=iphone BROWSER_VERSION="8.0..latest"
23+
- node_js: '4'
24+
env: TEST_SUITE=browser BROWSER_NAME=safari BROWSER_VERSION="5..latest"
25+
- node_js: '4'
26+
env: TEST_SUITE=browser BROWSER_NAME=android BROWSER_VERSION="4.0..latest"
1127
script: "npm run-script $TEST_SUITE"
28+
env:
29+
global:
30+
- secure: YHNUDQmx/WiW3gmDcRCfb6KLDeio7Mr5tqPY2kHPdZlBSytsQjNk75ytM4U6Cu8Uk8iEIoj/aFlxiVMpJNA8J4QSUyW/YkbVaIz0+1oywoV0Ht8aRBfZ1jvXfX6789+1Q9c4xaMkYYbJpXSh9JcirsiwmqWd4+IDd7hcESodsDQ=
31+
- secure: Nhj5yejKZxUbtHGZta+GjYWqXGaOZB7ainTkOuGcpXM+OwwjeDpYlTBrwS90Q7hqens7KXVzQM09aDbadpsDCsOo1nyaEigMtomAorZ1UC1CpEoVz1ZuikF9bEhb+/7M9pzuL1fX+Ke9Dx4mPPeb8sf/2SrAu1RqXkSwZV/duAc=

.zuul.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ui: tape

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
A port of node's `crypto` module to the browser.
44

5-
[![travis](https://img.shields.io/travis/crypto-browserify/crypto-browserify/master.svg?style=flat)](https://travis-ci.org/crypto-browserify/crypto-browserify)
5+
[![Build Status](https://travis-ci.org/crypto-browserify/crypto-browserify.svg?branch=master)](https://travis-ci.org/crypto-browserify/crypto-browserify)
66
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
7+
[![Sauce Test Status](https://saucelabs.com/browser-matrix/crypto-browserify.svg)](https://saucelabs.com/u/crypto-browserify)
78

89
The goal of this module is to reimplement node's crypto module,
910
in pure javascript so that it can run in the browser.
@@ -46,5 +47,3 @@ and post comments if you do.
4647
## License
4748

4849
MIT
49-
50-

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
"url": "git://github.com/crypto-browserify/crypto-browserify.git"
1010
},
1111
"scripts": {
12-
"prepublish": "npm ls && npm test",
1312
"standard": "standard",
1413
"test": "npm run standard && npm run unit",
15-
"unit": "set -e; for t in test/node/*.js test/*.js; do node $t; done"
14+
"unit": "node test/",
15+
"browser": "zuul --browser-version $BROWSER_VERSION --browser-name $BROWSER_NAME -- test/index.js"
1616
},
1717
"engines": {
1818
"node": "*"
1919
},
2020
"dependencies": {
2121
"browserify-cipher": "^1.0.0",
2222
"browserify-sign": "^3.0.1",
23-
"create-ecdh": "^2.0.0",
23+
"create-ecdh": "^2.0.2",
2424
"create-hash": "^1.1.0",
2525
"create-hmac": "^1.1.0",
2626
"diffie-hellman": "^3.0.1",
@@ -31,8 +31,10 @@
3131
},
3232
"devDependencies": {
3333
"hash-test-vectors": "~1.3.2",
34+
"pseudorandombytes": "^2.0.0",
3435
"standard": "^5.0.2",
35-
"tape": "~2.3.2"
36+
"tape": "~2.3.2",
37+
"zuul": "^3.6.0"
3638
},
3739
"optionalDependencies": {},
3840
"browser": {

test/aes.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
var test = require('tape')
22
var crypto = require('browserify-cipher/browser')
3-
var randomBytes = require('randombytes')
3+
var randomBytes = require('pseudorandombytes')
44

5-
test('ciphers', function (t) {
5+
function runIt (i) {
66
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'))
7+
test('run: ' + i, function (t) {
8+
t.test('ciphers: ' + cipher, function (t) {
9+
t.plan(1)
10+
var data = randomBytes(562)
11+
var password = randomBytes(20)
12+
var crypter = crypto.createCipher(cipher, password)
13+
var decrypter = crypto.createDecipher(cipher, password)
14+
var out = []
15+
out.push(decrypter.update(crypter.update(data)))
16+
out.push(decrypter.update(crypter.final()))
17+
if (cipher.indexOf('gcm') > -1) {
18+
decrypter.setAuthTag(crypter.getAuthTag())
19+
}
20+
out.push(decrypter.final())
21+
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
22+
})
2123
})
2224
})
23-
})
24-
25+
if (i < 4) {
26+
setTimeout(runIt, 0, i + 1)
27+
}
28+
}
29+
runIt(1)
2530
test('getCiphers', function (t) {
2631
t.plan(1)
2732
t.ok(crypto.getCiphers().length, 'get ciphers returns an array')

test/create-hash.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,43 @@ 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+
runTest(name, createHash, algorithm)
13+
})
14+
}
15+
function runTest (name, createHash, algorithm) {
16+
test(name + ' test ' + algorithm + ' against test vectors', function (t) {
17+
run(0)
18+
function run (i) {
19+
if (i >= vectors.length) {
20+
return t.end()
21+
}
22+
var obj = vectors[i]
23+
24+
var input = new Buffer(obj.input, 'base64')
25+
var node = obj[algorithm]
26+
var js = createHash(algorithm).update(input).digest('hex')
27+
if (js !== node) {
28+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
29+
}
2030

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-
})
29-
vectors.forEach(function (obj, i) {
30-
var input = new Buffer(obj.input, 'base64')
31-
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)
36-
})
37-
t.end()
31+
encodings.forEach(function (encoding) {
32+
var input = new Buffer(obj.input, 'base64').toString(encoding)
33+
var node = obj[algorithm]
34+
var js = createHash(algorithm).update(input, encoding).digest('hex')
35+
if (js !== node) {
36+
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
37+
}
3838
})
39-
})
39+
input = new Buffer(obj.input, 'base64')
40+
node = obj[algorithm]
41+
var hash = createHash(algorithm)
42+
hash.end(input)
43+
js = hash.read().toString('hex')
44+
if (js !== node) {
45+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
46+
}
47+
setTimeout(run, 0, i + 1)
48+
}
4049
})
4150
}

test/create-hmac.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@ 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+
run(0)
12+
function run (i) {
13+
if (i >= vectors.length) {
14+
return t.end()
15+
}
16+
var input = vectors[i]
17+
var output = createHmac(alg, new Buffer(input.key, 'hex'))
18+
.update(input.data, 'hex').digest()
1519

16-
output = input.truncate ? output.slice(0, input.truncate) : output
17-
t.equal(output.toString('hex'), input[alg])
18-
})
19-
t.end()
20-
})
20+
output = input.truncate ? output.slice(0, input.truncate) : output
21+
output = output.toString('hex')
22+
if (output !== input[alg]) {
23+
t.equal(output, input[alg])
24+
}
25+
setTimeout(run, 0, i + 1)
26+
}
27+
})
2128

22-
t.test('hmac(' + alg + ')', function (t) {
23-
vectors.forEach(function (input, i) {
24-
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
29+
test('hmac(' + alg + ')', function (t) {
30+
run(0)
31+
function run (i) {
32+
if (i >= vectors.length) {
33+
return t.end()
34+
}
35+
var input = vectors[i]
36+
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
2537

26-
hmac.end(input.data, 'hex')
27-
var output = hmac.read()
38+
hmac.end(input.data, 'hex')
39+
var output = hmac.read()
2840

29-
output = input.truncate ? output.slice(0, input.truncate) : output
30-
t.equal(output.toString('hex'), input[alg])
31-
})
32-
t.end()
33-
})
41+
output = input.truncate ? output.slice(0, input.truncate) : output
42+
output = output.toString('hex')
43+
if (output !== input[alg]) {
44+
t.equal(output, input[alg])
45+
}
46+
setTimeout(run, 0, i + 1)
47+
}
3448
})
3549
})
3650
}

test/ecdh.js

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,57 @@ var mods = [
55
'prime192v1'
66
]
77
var test = require('tape')
8-
var createECDH1 = require('../').createECDH
8+
var _crypto = require('../')
9+
var createECDH1 = _crypto.createECDH
910
var createECDH2 = require('create-ecdh/browser')
1011

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-
})
12+
mods.forEach(function (mod) {
13+
test('createECDH: ' + 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+
})
2626

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-
})
27+
test('createECDH: ' + 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+
})
4040

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-
})
41+
test('createECDH: ' + 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')
6160
})
6261
})

0 commit comments

Comments
 (0)