Skip to content

Commit f2ce238

Browse files
committed
asyncize the hash and hmac tests so they report back more frequently
1 parent 94c6a8b commit f2ce238

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

test/create-hash.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,36 @@ testLib('create-hash/browser', require('create-hash/browser'))
99

1010
function testLib (name, createHash) {
1111
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-
})
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+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
1928

2029
encodings.forEach(function (encoding) {
21-
vectors.forEach(function (obj, i) {
22-
var input = new Buffer(obj.input, 'base64').toString(encoding)
23-
var node = obj[algorithm]
24-
var js = createHash(algorithm).update(input, encoding).digest('hex')
25-
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
26-
})
27-
})
28-
vectors.forEach(function (obj, i) {
29-
var input = new Buffer(obj.input, 'base64')
30+
var input = new Buffer(obj.input, 'base64').toString(encoding)
3031
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)
32+
var js = createHash(algorithm).update(input, encoding).digest('hex')
33+
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
3534
})
36-
t.end()
37-
})
35+
input = new Buffer(obj.input, 'base64')
36+
node = obj[algorithm]
37+
var hash = createHash(algorithm)
38+
hash.end(input)
39+
js = hash.read().toString('hex')
40+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
41+
setTimeout(run, 0, i + 1)
42+
}
3843
})
3944
}

test/create-hmac.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,37 @@ testLib('create-hmac/browser', require('create-hmac/browser'))
88
function testLib (name, createHmac) {
99
algorithms.forEach(function (alg) {
1010
test(name + ' hmac(' + alg + ')', function (t) {
11-
vectors.forEach(function (input, i) {
11+
run(0)
12+
function run (i) {
13+
if (i >= vectors.length) {
14+
return t.end()
15+
}
16+
var input = vectors[i]
1217
var output = createHmac(alg, new Buffer(input.key, 'hex'))
1318
.update(input.data, 'hex').digest()
1419

1520
output = input.truncate ? output.slice(0, input.truncate) : output
1621
t.equal(output.toString('hex'), input[alg])
17-
})
18-
t.end()
22+
setTimeout(run, 0, i + 1)
23+
}
1924
})
2025

2126
test('hmac(' + alg + ')', function (t) {
22-
vectors.forEach(function (input, i) {
27+
run(0)
28+
function run (i) {
29+
if (i >= vectors.length) {
30+
return t.end()
31+
}
32+
var input = vectors[i]
2333
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
2434

2535
hmac.end(input.data, 'hex')
2636
var output = hmac.read()
2737

2838
output = input.truncate ? output.slice(0, input.truncate) : output
2939
t.equal(output.toString('hex'), input[alg])
30-
})
31-
t.end()
40+
setTimeout(run, 0, i + 1)
41+
}
3242
})
3343
})
3444
}

0 commit comments

Comments
 (0)