tests: fixed locking randomBytes test
This commit is contained in:
parent
d3b6cfce84
commit
58ae6b7fc5
@ -2,16 +2,14 @@ import assert from "assert";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
lock,
|
lock,
|
||||||
|
|
||||||
computeHmac,
|
computeHmac,
|
||||||
|
|
||||||
keccak256, ripemd160, sha256, sha512,
|
keccak256, ripemd160, sha256, sha512,
|
||||||
|
pbkdf2, scrypt, scryptSync,
|
||||||
pbkdf2, scrypt, scryptSync
|
randomBytes
|
||||||
} from "../index.js";
|
} from "../index.js";
|
||||||
|
|
||||||
interface Algorithm {
|
interface Algorithm {
|
||||||
(...args: Array<any>): string | Promise<string>;
|
(...args: Array<any>): string | Uint8Array | Promise<string>;
|
||||||
|
|
||||||
register: (func: any) => void;
|
register: (func: any) => void;
|
||||||
lock: () => void;
|
lock: () => void;
|
||||||
@ -23,6 +21,7 @@ interface TestCase {
|
|||||||
params: Array<any>;
|
params: Array<any>;
|
||||||
algorithm: Algorithm;
|
algorithm: Algorithm;
|
||||||
hijackTag: string;
|
hijackTag: string;
|
||||||
|
postCheck?: (value: any) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -85,9 +84,18 @@ describe("test registration", function() {
|
|||||||
hijackTag: 'hijacked computeHmac: ["sha256",{},{}]',
|
hijackTag: 'hijacked computeHmac: ["sha256",{},{}]',
|
||||||
algorithm: computeHmac
|
algorithm: computeHmac
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "randomBytes",
|
||||||
|
params: [ 32 ],
|
||||||
|
hijackTag: "hijacked randomBytes: [32]",
|
||||||
|
algorithm: randomBytes,
|
||||||
|
postCheck: (value: any) => {
|
||||||
|
return (value instanceof Uint8Array && value.length === 32);
|
||||||
|
}
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
tests.forEach(({ name, params, hijackTag, algorithm }) => {
|
tests.forEach(({ name, params, hijackTag, algorithm, postCheck }) => {
|
||||||
it(`swaps in hijacked callback: ${ name }`, async function() {
|
it(`swaps in hijacked callback: ${ name }`, async function() {
|
||||||
const initial = await algorithm(...params);
|
const initial = await algorithm(...params);
|
||||||
|
|
||||||
@ -97,7 +105,11 @@ describe("test registration", function() {
|
|||||||
assert.equal(hijack, hijackTag);
|
assert.equal(hijack, hijackTag);
|
||||||
|
|
||||||
algorithm.register(algorithm._);
|
algorithm.register(algorithm._);
|
||||||
assert.equal(await algorithm(...params), initial);
|
if (postCheck) {
|
||||||
|
assert.ok(postCheck(await algorithm(...params)));
|
||||||
|
} else {
|
||||||
|
assert.equal(await algorithm(...params), initial);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ function lock(): void {
|
|||||||
scryptSync.lock();
|
scryptSync.lock();
|
||||||
sha256.lock();
|
sha256.lock();
|
||||||
sha512.lock();
|
sha512.lock();
|
||||||
|
randomBytes.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
export { lock };
|
export { lock };
|
||||||
|
@ -15,7 +15,7 @@ export function randomBytes(length: number): Uint8Array {
|
|||||||
randomBytes._ = _randomBytes;
|
randomBytes._ = _randomBytes;
|
||||||
randomBytes.lock = function(): void { locked = true; }
|
randomBytes.lock = function(): void { locked = true; }
|
||||||
randomBytes.register = function(func: (length: number) => Uint8Array) {
|
randomBytes.register = function(func: (length: number) => Uint8Array) {
|
||||||
if (locked) { throw new Error("random is locked"); }
|
if (locked) { throw new Error("randomBytes is locked"); }
|
||||||
__randomBytes = func;
|
__randomBytes = func;
|
||||||
}
|
}
|
||||||
Object.freeze(randomBytes);
|
Object.freeze(randomBytes);
|
||||||
|
Loading…
Reference in New Issue
Block a user