Skip to content

Commit 8e27906

Browse files
committed
Update tests to run correctly on testling.
We can't run node's version of crypto on testling, so tests were actually comparing the test code of crypto-browserify with whatever version browserify happens to pull in (0.2.x iirc). Tests are now split into two similar files, one runs in node to keep the comparisons against node's crypto updated, and the other compares crypto-browserify's output to a cached copy.
1 parent 256178e commit 8e27906

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "git://github.com/dominictarr/crypto-browserify.git"
1010
},
1111
"scripts": {
12-
"test": "node test/simple.js"
12+
"test": "node test/node.js"
1313
},
1414
"engines": {
1515
"node": "*"
@@ -19,7 +19,7 @@
1919
"tape": "~1.0.4"
2020
},
2121
"testling": {
22-
"files": "test/*.js",
22+
"files": "test/browser.js",
2323
"browsers": [
2424
"ie/8..latest",
2525
"chrome/20..latest",

test/browser.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
console.log(process.ENV);
2+
3+
var test = require("tape")
4+
5+
var crypto = require('../')
6+
7+
var algorithms = ['sha1', 'sha256', 'md5'];
8+
var encodings = ['binary', 'hex', 'base64'];
9+
10+
11+
// We can't compare against node's crypto library directly because when
12+
// using testling we only have another version of crypto-browserify to
13+
// check against. So we'll use a cached version of the expected values
14+
// generated by node crypto.
15+
var EXPECTED = {};
16+
17+
EXPECTED['sha1-hash-binary'] = atob('qvTGHdzF6KLavt4PO0gs2a6pQ00=');
18+
EXPECTED['sha1-hash-hex'] = 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d';
19+
EXPECTED['sha1-hash-base64'] = 'qvTGHdzF6KLavt4PO0gs2a6pQ00=';
20+
21+
EXPECTED['sha256-hash-binary'] = atob('LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=');
22+
EXPECTED['sha256-hash-hex'] = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824';
23+
EXPECTED['sha256-hash-base64'] = 'LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=';
24+
25+
EXPECTED['md5-hash-binary'] = atob('XUFAKrxLKna5cZ2REBfFkg==');
26+
EXPECTED['md5-hash-hex'] = '5d41402abc4b2a76b9719d911017c592';
27+
EXPECTED['md5-hash-base64'] = 'XUFAKrxLKna5cZ2REBfFkg==';
28+
29+
EXPECTED['sha1-hmac-binary'] = atob('URIFXAX5RPhXVe/FzYlw4ZTp9Fs=');
30+
EXPECTED['sha1-hmac-hex'] = '5112055c05f944f85755efc5cd8970e194e9f45b';
31+
EXPECTED['sha1-hmac-base64'] = 'URIFXAX5RPhXVe/FzYlw4ZTp9Fs=';
32+
33+
EXPECTED['sha256-hmac-binary'] = atob('iKqz7ejTrflNJquQ07r9SiCDBww7zOnAFO4EpEOEfAs=');
34+
EXPECTED['sha256-hmac-hex'] = '88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b';
35+
EXPECTED['sha256-hmac-base64'] = 'iKqz7ejTrflNJquQ07r9SiCDBww7zOnAFO4EpEOEfAs=';
36+
37+
EXPECTED['md5-hmac-binary'] = atob('ut5jhjxh7QsxZYBuzWrO/A==');
38+
EXPECTED['md5-hmac-hex'] = 'bade63863c61ed0b3165806ecd6acefc';
39+
EXPECTED['md5-hmac-base64'] = 'ut5jhjxh7QsxZYBuzWrO/A==';
40+
41+
algorithms.forEach(function (algorithm) {
42+
encodings.forEach(function (encoding) {
43+
test(algorithm + ' hash using ' + encoding, function (t) {
44+
t.plan(1);
45+
var actual = crypto.createHash(algorithm).update('hello', 'utf-8').digest(encoding);
46+
var expected = EXPECTED[algorithm + '-hash-' + encoding];
47+
t.equal(actual, expected);
48+
t.end();
49+
});
50+
51+
test(algorithm + ' hmac using ' + encoding, function (t) {
52+
t.plan(1);
53+
var actual = crypto.createHmac(algorithm, 'secret').update('hello', 'utf-8').digest(encoding);
54+
var expected = EXPECTED[algorithm + '-hmac-' + encoding];
55+
t.equal(actual, expected);
56+
t.end();
57+
});
58+
});
59+
});
60+
61+
test('randomBytes', function (t) {
62+
t.plan(5)
63+
t.equal(crypto.randomBytes(10).length, 10)
64+
t.ok(crypto.randomBytes(10) instanceof Buffer)
65+
crypto.randomBytes(10, function(ex, bytes) {
66+
t.error(ex)
67+
t.equal(bytes.length, 10)
68+
t.ok(bytes instanceof Buffer)
69+
t.end()
70+
})
71+
})
File renamed without changes.

0 commit comments

Comments
 (0)