[Tests] add coverage · browserify/pbkdf2@d0d534b

@@ -4,12 +4,13 @@

44

// SHA-256/SHA-512 test vectors from:

55

// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors

66

// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors

7-

var fixtures = require('./fixtures');

87

var tape = require('tape');

98

var satisfies = require('semver').satisfies;

109

var Buffer = require('safe-buffer').Buffer;

11101211

var node = require('crypto');

12+13+

var fixtures = require('./fixtures');

1314

var js = require('../browser');

1415

var browserImpl = require('../lib/sync-browser');

1516

@@ -173,19 +174,26 @@ function runTests(name, compat) {

173174

var description = algorithm + ' encodes "' + key + '" (' + keyType + ') with salt "' + salt + '" (' + saltType + ') with ' + algorithm + ' to ' + expected;

174175175176

t.test(name + ' async w/ ' + description, function (st) {

176-

st.plan(2);

177+

st.plan(3);

177178178179

compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) {

179180

st.error(err);

180-

st.equal(result.toString('hex'), expected);

181+182+

var hash = result.toString('hex');

183+184+

st.doesNotMatch(hash, /^0+$/, 'is not the all-zeroes result');

185+

st.equal(hash, expected);

181186

});

182187

});

183188184189

t.test(name + 'sync w/ ' + description, function (st) {

185-

st.plan(1);

190+

st.plan(2);

186191187192

var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm);

188-

st.equal(result.toString('hex'), expected);

193+

var hash = result.toString('hex');

194+195+

st.doesNotMatch(hash, /^0+$/, 'is not the all-zeroes result');

196+

st.equal(hash, expected);

189197

});

190198

});

191199

@@ -238,6 +246,7 @@ tape('does not return all zeroes for any algorithm', function (t) {

238246

var throwCount = 0;

239247

var impls = { __proto__: null, node: node.pbkdf2Sync, lib: js.pbkdf2Sync, browser: browserImpl };

240248

var results = { __proto__: null };

249+

var throws = { __proto__: null };

241250

for (var implName in impls) { // eslint-disable-line no-restricted-syntax

242251

var pbkdf2Sync = impls[implName];

243252

try {

@@ -246,13 +255,15 @@ tape('does not return all zeroes for any algorithm', function (t) {

246255

t.doesNotMatch(key, /^0+$/, implName + ' does not return all zeros for ' + algo);

247256

} catch (e) {

248257

throwCount += 1;

258+

throws[implName] = true;

249259

t.ok(e, implName + ' throws for ' + algo);

250260

t.comment(e);

251261

}

252262

}

253263254264

if (throwCount === 0) {

255265

t.equal(throwCount, 0, 'all implementations return a value for ' + algo);

266+

t.deepEqual(throws, { __proto__: null }, 'no implementations throw for ' + algo);

256267

t.equal(

257268

results.node,

258269

results.lib,

@@ -265,9 +276,47 @@ tape('does not return all zeroes for any algorithm', function (t) {

265276

'node and browser pbkdf2Sync should return the same value for ' + algo

266277

);

267278

} else {

268-

t.equal(

269-

throwCount,

270-

3,

279+

var expected = {

280+

__proto__: null,

281+

node: true,

282+

lib: true,

283+

browser: true

284+

};

285+

if (

286+

(algo.toLowerCase() === 'ripemd-160' || algo.toLowerCase() === 'rmd160')

287+

&& satisfies(process.version, '< 18') // node < 18 doesn't support ripemd160

288+

) {

289+

if (!throws.browser) {

290+

delete expected.browser;

291+

}

292+

if (!throws.lib) {

293+

delete expected.lib;

294+

}

295+

}

296+297+

if (

298+

algo.toLowerCase() === 'sha-1'

299+

&& satisfies(process.version, '< 17') // node < 17 doesn't support "sha-1"

300+

&& !throws.lib

301+

&& !throws.browser

302+

) {

303+

delete expected.lib;

304+

delete expected.browser;

305+

}

306+307+

if (

308+

(algo.toLowerCase() === 'sha256' || algo.toLowerCase() === 'sha512')

309+

&& satisfies(process.version, '< 10') // node < 10 doesn't support "sha256" or "sha512"

310+

&& !throws.lib

311+

&& !throws.browser

312+

) {

313+

delete expected.lib;

314+

delete expected.browser;

315+

}

316+317+

t.deepEqual(

318+

throws,

319+

expected,

271320

'all implementations throw for ' + algo,

272321

{ todo: throwCount === 1 && algo === 'sha512-256' && 'sha.js does not yet support sha512-256' }

273322

);