[Tests] add coverage · browserify/pbkdf2@d0d534b (original) (raw)
`@@ -4,12 +4,13 @@
`
4
4
`// SHA-256/SHA-512 test vectors from:
`
5
5
`// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
`
6
6
`// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors
`
7
``
`-
var fixtures = require('./fixtures');
`
8
7
`var tape = require('tape');
`
9
8
`var satisfies = require('semver').satisfies;
`
10
9
`var Buffer = require('safe-buffer').Buffer;
`
11
10
``
12
11
`var node = require('crypto');
`
``
12
+
``
13
`+
var fixtures = require('./fixtures');
`
13
14
`var js = require('../browser');
`
14
15
`var browserImpl = require('../lib/sync-browser');
`
15
16
``
`@@ -173,19 +174,26 @@ function runTests(name, compat) {
`
173
174
`var description = algorithm + ' encodes "' + key + '" (' + keyType + ') with salt "' + salt + '" (' + saltType + ') with ' + algorithm + ' to ' + expected;
`
174
175
``
175
176
`t.test(name + ' async w/ ' + description, function (st) {
`
176
``
`-
st.plan(2);
`
``
177
`+
st.plan(3);
`
177
178
``
178
179
`compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) {
`
179
180
`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);
`
181
186
`});
`
182
187
`});
`
183
188
``
184
189
`t.test(name + 'sync w/ ' + description, function (st) {
`
185
``
`-
st.plan(1);
`
``
190
`+
st.plan(2);
`
186
191
``
187
192
`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);
`
189
197
`});
`
190
198
`});
`
191
199
``
`@@ -238,6 +246,7 @@ tape('does not return all zeroes for any algorithm', function (t) {
`
238
246
`var throwCount = 0;
`
239
247
`var impls = { proto: null, node: node.pbkdf2Sync, lib: js.pbkdf2Sync, browser: browserImpl };
`
240
248
`var results = { proto: null };
`
``
249
`+
var throws = { proto: null };
`
241
250
`for (var implName in impls) { // eslint-disable-line no-restricted-syntax
`
242
251
`var pbkdf2Sync = impls[implName];
`
243
252
`try {
`
`@@ -246,13 +255,15 @@ tape('does not return all zeroes for any algorithm', function (t) {
`
246
255
`t.doesNotMatch(key, /^0+$/, implName + ' does not return all zeros for ' + algo);
`
247
256
`} catch (e) {
`
248
257
`throwCount += 1;
`
``
258
`+
throws[implName] = true;
`
249
259
`t.ok(e, implName + ' throws for ' + algo);
`
250
260
`t.comment(e);
`
251
261
`}
`
252
262
`}
`
253
263
``
254
264
`if (throwCount === 0) {
`
255
265
`t.equal(throwCount, 0, 'all implementations return a value for ' + algo);
`
``
266
`+
t.deepEqual(throws, { proto: null }, 'no implementations throw for ' + algo);
`
256
267
`t.equal(
`
257
268
`results.node,
`
258
269
`results.lib,
`
`@@ -265,9 +276,47 @@ tape('does not return all zeroes for any algorithm', function (t) {
`
265
276
`'node and browser pbkdf2Sync should return the same value for ' + algo
`
266
277
`);
`
267
278
`} 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,
`
271
320
`'all implementations throw for ' + algo,
`
272
321
`{ todo: throwCount === 1 && algo === 'sha512-256' && 'sha.js does not yet support sha512-256' }
`
273
322
`);
`