Skip to content

Commit

Permalink
test: fix failing cases, due to representation of symbol / proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 21, 2024
1 parent a0b1bcd commit f889184
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
7 changes: 4 additions & 3 deletions test/hashes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
TYPE_TEST,
SPACE,
EMPTY,
repr,
} = require('./utils');

// NIST test vectors (https://www.di-mgt.com.au/sha_testvectors.html)
Expand Down Expand Up @@ -449,12 +450,12 @@ function init() {
should(`throw on wrong argument type`, () => {
// Allowed only: undefined (for compact form only), string, Uint8Array
for (const t of TYPE_TEST.bytes) {
throws(() => hash.fn(t), `compact(${t})`);
throws(() => hash.obj().update(t).digest(), `full(${t})`);
throws(() => hash.fn(t), `compact(${repr(t)})`);
throws(() => hash.obj().update(t).digest(), `full(${repr(t)})`);
}
throws(() => hash.fn(), `compact(undefined)`);
throws(() => hash.obj().update(undefined).digest(), `full(undefined)`);
for (const t of TYPE_TEST.opts) throws(() => hash.fn(undefined, t), `opt(${t})`);
for (const t of TYPE_TEST.opts) throws(() => hash.fn(undefined, t), `opt(${repr(t)})`);
});
should(`check types`, () => {
deepStrictEqual(hash.fn(SPACE.str), hash.fn(SPACE.bytes));
Expand Down
13 changes: 7 additions & 6 deletions test/hmac.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
bytesToHex,
truncate,
concatBytes,
repr,
TYPE_TEST,
SPACE,
EMPTY,
Expand Down Expand Up @@ -167,18 +168,18 @@ describe('hmac', () => {
hmac(sha256, 'key', 'msg');
hmac.create(sha256, 'key');
for (const t of TYPE_TEST.bytes) {
throws(() => hmac(sha256, t, 'msg'), `hmac(key=${t})`);
throws(() => hmac(sha256, 'key', t), `hmac(msg=${t})`);
throws(() => hmac.create(sha256, t), `hmac.create(key=${t})`);
throws(() => hmac(sha256, t, 'msg'), `hmac(key=${repr(t)})`);
throws(() => hmac(sha256, 'key', t), `hmac(msg=${repr(t)})`);
throws(() => hmac.create(sha256, t), `hmac.create(key=${repr(t)})`);
}
throws(() => hmac(sha256, undefined, 'msg'), `hmac(key=undefined)`);
throws(() => hmac(sha256, 'key'), `hmac(msg=undefined)`);
throws(() => hmac.create(sha256, undefined), `hmac.create(key=undefined)`);
// for (const t of TYPE_TEST.opts) {
// throws(() => hmac(sha256, 'key', 'salt', t), `hmac(opt=${t})`);
// throws(() => hmac.create(sha256, 'key', t), `hmac.create(opt=${t})`);
// throws(() => hmac(sha256, 'key', 'salt', t), `hmac(opt=${repr(t})`);
// throws(() => hmac.create(sha256, 'key', t), `hmac.create(opt=${repr(t)})`);
// }
for (const t of TYPE_TEST.hash) throws(() => hmac(t, 'key', 'salt'), `hmac(hash=${t})`);
for (const t of TYPE_TEST.hash) throws(() => hmac(t, 'key', 'salt'), `hmac(hash=${repr(t)})`);
deepStrictEqual(
hmac(sha512, SPACE.str, SPACE.str),
hmac(sha512, SPACE.bytes, SPACE.bytes),
Expand Down
67 changes: 38 additions & 29 deletions test/kdf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { sha512 } = require('../sha512');
const { extract: hkdf_extract, hkdf } = require('../hkdf');
const { pbkdf2, pbkdf2Async } = require('../pbkdf2');
const { scrypt, scryptAsync } = require('../scrypt');
const { utf8ToBytes, hexToBytes, TYPE_TEST, SPACE, EMPTY } = require('./utils');
const { repr, utf8ToBytes, hexToBytes, TYPE_TEST, SPACE, EMPTY } = require('./utils');
const { executeKDFTests } = require('./generator');
// HKDF test vectors from RFC 5869
const HKDF_VECTORS = [
Expand Down Expand Up @@ -187,17 +187,17 @@ describe('hkdf', () => {
hkdf(sha512, '', '', '', 16320);
throws(() => hkdf(sha512, '', '', '', 16320 + 1), `hkdf.dkLen(16320 + 1)`);
for (const t of TYPE_TEST.int) {
throws(() => hkdf(sha256, '', '', '', t), `hkdf.dkLen(${t})`);
throws(() => hkdf(sha256, '', '', '', t), `hkdf.dkLen(${repr(t)})`);
}
for (const t of TYPE_TEST.bytes) {
throws(() => hkdf(sha256, t, '', '', 32), `hkdf.ikm(${t})`);
throws(() => hkdf(sha256, '', t, '', 32), `hkdf.salt(${t})`);
throws(() => hkdf(sha256, '', '', t, 32), `hkdf.info(${t})`);
throws(() => hkdf(sha256, t, '', '', 32), `hkdf.ikm(${repr(t)})`);
throws(() => hkdf(sha256, '', t, '', 32), `hkdf.salt(${repr(t)})`);
throws(() => hkdf(sha256, '', '', t, 32), `hkdf.info(${repr(t)})`);
}
// for (const t of TYPE_TEST.opts)
// throws(() => hkdf(sha256, '', '', '', 32, t), `hkdf.opt(${t})`);
// throws(() => hkdf(sha256, '', '', '', 32, t), `hkdf.opt(${repr(t)})`);
throws(() => hkdf(sha256, undefined, '', '', 32), 'hkdf.ikm===undefined');
for (const t of TYPE_TEST.hash) throws(() => hkdf(t, '', '', '', 32), `hkdf(hash=${t})`);
for (const t of TYPE_TEST.hash) throws(() => hkdf(t, '', '', '', 32), `hkdf(hash=${repr(t)})`);

deepStrictEqual(
hkdf(sha256, SPACE.str, SPACE.str, SPACE.str),
Expand Down Expand Up @@ -240,22 +240,25 @@ describe('scrypt', () => {
await rejects(() => scryptAsync('pwd', 'salt', { ...opt, N: 1 }), `scrypt(N=1)`);
for (const t of TYPE_TEST.int) {
for (const k of ['N', 'r', 'p', 'dkLen']) {
throws(() => scrypt('pwd', 'salt', { ...opt, [k]: t }), `scrypt(${k}=${t})`);
await rejects(() => scryptAsync('pwd', 'salt', { ...opt, [k]: t }), `scrypt(${k}=${t})`);
throws(() => scrypt('pwd', 'salt', { ...opt, [k]: t }), `scrypt(${k}=${repr(t)})`);
await rejects(
() => scryptAsync('pwd', 'salt', { ...opt, [k]: t }),
`scrypt(${k}=${repr(t)})`
);
}
await rejects(
() => scryptAsync('pwd', 'salt', { ...opt, asyncTick: t }),
`scrypt(asyncTick=${t})`
`scrypt(asyncTick=${repr(t)})`
);
}
for (const t of TYPE_TEST.bytes.concat([undefined])) {
throws(() => scrypt('pwd', t, opt), `scrypt(salt=${t})`);
await rejects(() => scryptAsync('pwd', t, opt), `scrypt(salt=${t})`);
throws(() => scrypt(t, 'salt', opt), `scrypt(pwd=${t})`);
await rejects(() => scryptAsync(t, 'salt', opt), `scrypt(pwd=${t})`);
throws(() => scrypt('pwd', t, opt), `scrypt(salt=${repr(t)})`);
await rejects(() => scryptAsync('pwd', t, opt), `scrypt(salt=${repr(t)})`);
throws(() => scrypt(t, 'salt', opt), `scrypt(pwd=${repr(t)})`);
await rejects(() => scryptAsync(t, 'salt', opt), `scrypt(pwd=${repr(t)})`);
for (const t of TYPE_TEST.opts) {
throws(() => scrypt('pwd', 'salt', t), `scrypt(opt=${t})`);
await rejects(() => scryptAsync('pwd', 'salt', t), `scrypt(opt=${t})`);
throws(() => scrypt('pwd', 'salt', t), `scrypt(opt=${repr(t)})`);
await rejects(() => scryptAsync('pwd', 'salt', t), `scrypt(opt=${repr(t)})`);
}
}
deepStrictEqual(
Expand Down Expand Up @@ -298,31 +301,37 @@ describe('PBKDF2', () => {
throws(() => pbkdf2(sha256, 'pwd', 'salt', { c: 0, dkLen: 32 }), `pbkdf2(c=0)`);
await rejects(() => pbkdf2Async(sha256, 'pwd', 'salt', { c: 0, dkLen: 32 }), `pbkdf2(c=0)`);
for (const t of TYPE_TEST.int) {
throws(() => pbkdf2(sha256, 'pwd', 'salt', { ...opts, c: t }), `pbkdf2(c=${t})`);
await rejects(() => pbkdf2Async(sha256, 'pwd', 'salt', { ...opts, c: t }), `pbkdf2(c=${t})`);
throws(() => pbkdf2(sha256, 'pwd', 'salt', { ...opts, dkLen: t }), `pbkdf2(dkLen=${t})`);
throws(() => pbkdf2(sha256, 'pwd', 'salt', { ...opts, c: t }), `pbkdf2(c=${repr(t)})`);
await rejects(
() => pbkdf2Async(sha256, 'pwd', 'salt', { ...opts, c: t }),
`pbkdf2(c=${repr(t)})`
);
throws(
() => pbkdf2(sha256, 'pwd', 'salt', { ...opts, dkLen: t }),
`pbkdf2(dkLen=${repr(t)})`
);
await rejects(
() => pbkdf2Async(sha256, 'pwd', 'salt', { ...opts, dkLen: t }),
`pbkdf2(dkLen=${t})`
`pbkdf2(dkLen=${repr(t)})`
);
await rejects(
() => pbkdf2Async(sha256, 'pwd', 'salt', { ...opts, asyncTick: t }),
`pbkdf2(asyncTick=${t})`
`pbkdf2(asyncTick=${repr(t)})`
);
}
for (const t of TYPE_TEST.bytes.concat([undefined])) {
throws(() => pbkdf2(sha256, t, 'salt', opts), `pbkdf2(pwd=${t})`);
await rejects(() => pbkdf2Async(sha256, t, 'salt', opts), `pbkdf2(pwd=${t})`);
throws(() => pbkdf2(sha256, 'pwd', t, opts), `pbkdf2(salt=${t})`);
await rejects(() => pbkdf2Async(sha256, 'pwd', t, opts), `pbkdf2(salt=${t})`);
throws(() => pbkdf2(sha256, t, 'salt', opts), `pbkdf2(pwd=${repr(t)})`);
await rejects(() => pbkdf2Async(sha256, t, 'salt', opts), `pbkdf2(pwd=${repr(t)})`);
throws(() => pbkdf2(sha256, 'pwd', t, opts), `pbkdf2(salt=${repr(t)})`);
await rejects(() => pbkdf2Async(sha256, 'pwd', t, opts), `pbkdf2(salt=${repr(t)})`);
}
for (const t of TYPE_TEST.opts) {
throws(() => pbkdf2(sha256, 'pwd', 'salt', t), `pbkdf2(opt=${t})`);
await rejects(() => pbkdf2Async(sha256, 'pwd', 'salt', t), `pbkdf2(opt=${t})`);
throws(() => pbkdf2(sha256, 'pwd', 'salt', t), `pbkdf2(opt=${repr(t)})`);
await rejects(() => pbkdf2Async(sha256, 'pwd', 'salt', t), `pbkdf2(opt=${repr(t)})`);
}
for (const t of TYPE_TEST.hash) {
throws(() => pbkdf2(t, 'pwd', 'salt', t), `pbkdf2(hash=${t})`);
await rejects(() => pbkdf2Async(t, 'pwd', 'salt', t), `pbkdf2(hash=${t})`);
throws(() => pbkdf2(t, 'pwd', 'salt', t), `pbkdf2(hash=${repr(t)})`);
await rejects(() => pbkdf2Async(t, 'pwd', 'salt', t), `pbkdf2(hash=${repr(t)})`);
}
deepStrictEqual(
pbkdf2(sha256, SPACE.str, SPACE.str, opts),
Expand Down
14 changes: 13 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const TYPE_TEST_BASE = [
async () => {},
class Test {},
Symbol.for('a'),
new Proxy(new Uint8Array(), {}),
new Proxy(new Uint8Array(), {
get(t, p, r) {
if (p === 'isProxy') return true;
return Reflect.get(t, p, r);
},
}),
];

const TYPE_TEST_OPT = [
Expand Down Expand Up @@ -77,6 +82,12 @@ const TYPE_TEST = {
),
};

function repr(item) {
if (item && item.isProxy) return '[proxy]';
if (typeof item === 'symbol') return item.toString();
return `${item}`;
}

function median(list) {
const values = list.slice().sort((a, b) => a - b);
const half = (values.length / 2) | 0;
Expand Down Expand Up @@ -122,6 +133,7 @@ module.exports = {
truncate,
repeat,
TYPE_TEST,
repr,
SPACE: {
str: ' ',
bytes: new Uint8Array([0x20]),
Expand Down

0 comments on commit f889184

Please sign in to comment.