Skip to content

Commit

Permalink
[Tests] refactor so tests are not recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 25, 2023
1 parent e592c95 commit 5757219
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 51 deletions.
8 changes: 2 additions & 6 deletions test/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ var bcCrypto = require('browserify-cipher/browser');
var bcCyphers = bcCrypto.getCiphers();
var randomBytes = require('pseudorandombytes');

function runIt(i) {
for (var i = 0; i < 4; i += 1) {
bcCrypto.listCiphers().forEach(function (cipher) {
test('run: ' + i, function (t) {
/* eslint no-loop-func: 0 */
t.test('ciphers: ' + cipher, function (st) {
st.plan(1);
var data = randomBytes(562);
Expand All @@ -26,13 +27,8 @@ function runIt(i) {
});
});
});
if (i < 4) {
setTimeout(runIt, 0, i + 1);
}
}

runIt(1);

test('getCiphers', function (t) {
t.plan(1);
t.ok(bcCyphers.length, 'get ciphers returns an array');
Expand Down
25 changes: 6 additions & 19 deletions test/create-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,27 @@ var vectors = require('hash-test-vectors');

function runTest(name, createHash, algorithm) {
test(name + ' test ' + algorithm + ' against test vectors', function (t) {
function run(i) {
if (i >= vectors.length) {
t.end();
return;
}
var obj = vectors[i];

vectors.forEach(function (obj, i) {
var input = new Buffer(obj.input, 'base64');
var node = obj[algorithm];
var js = createHash(algorithm).update(input).digest('hex');
if (js !== node) {
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
}
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);

encodings.forEach(function (encoding) {
var eInput = new Buffer(obj.input, 'base64').toString(encoding);
var eNode = obj[algorithm];
var eJS = createHash(algorithm).update(eInput, encoding).digest('hex');
if (eJS !== eNode) {
t.equal(eJS, eNode, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + eNode);
}
t.equal(eJS, eNode, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + eNode);
});
input = new Buffer(obj.input, 'base64');
node = obj[algorithm];
var hash = createHash(algorithm);
hash.end(input);
js = hash.read().toString('hex');
if (js !== node) {
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
}
setTimeout(run, 0, i + 1);
}
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
});

run(0);
t.end();
});
}

Expand Down
32 changes: 8 additions & 24 deletions test/create-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,31 @@ var vectors = require('hash-test-vectors/hmac');
function testLib(name, createHmac) {
algorithms.forEach(function (alg) {
test(name + ' hmac(' + alg + ')', function (t) {
function run(i) {
if (i >= vectors.length) {
t.end();
return;
}
var input = vectors[i];
vectors.forEach(function (input) {
var output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
if (output !== input[alg]) {
t.equal(output, input[alg]);
}
setTimeout(run, 0, i + 1);
}
t.equal(output, input[alg]);
});

run(0);
t.end();
});

test('hmac(' + alg + ')', function (t) {
function run(i) {
if (i >= vectors.length) {
t.end();
return;
}
var input = vectors[i];
vectors.forEach(function (input) {
var hmac = createHmac(alg, new Buffer(input.key, 'hex'));

hmac.end(input.data, 'hex');
var output = hmac.read();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
if (output !== input[alg]) {
t.equal(output, input[alg]);
}
setTimeout(run, 0, i + 1);
}
t.equal(output, input[alg]);
});

run(0);
t.end();
});
});
}
Expand Down
7 changes: 5 additions & 2 deletions test/node/dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ test('diffie-hellman mod groups', function (t) {

test('diffie-hellman key lengths', function (t) {
[
64, 65, 192
64,
65,
192
].forEach(function (len) {
t.test(String(len), function (st) {
t.plan(3);
st.plan(3);

var dh2 = cryptoB.createDiffieHellman(len);
var prime2 = dh2.getPrime();
var p2 = prime2.toString('hex');
Expand Down

0 comments on commit 5757219

Please sign in to comment.