Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 9x 9x 9x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 9x 8x 8x 9x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 1x 1x 1x 1x | import assert from "assert"; import { lock, computeHmac, keccak256, ripemd160, sha256, sha512, pbkdf2, scrypt, scryptSync, randomBytes } from "../index.js"; interface Algorithm { (...args: Array<any>): string | Uint8Array | Promise<string>; register: (func: any) => void; lock: () => void; _: (...args: Array<any>) => any; } interface TestCase { name: string; params: Array<any>; algorithm: Algorithm; hijackTag: string; postCheck?: (value: any) => boolean; } describe("test registration", function() { let hijack = ""; function getHijack(algo: string) { return function(...args: Array<any>) { hijack = `hijacked ${ algo }: ${ JSON.stringify(args) }`; return "0x42"; } } const tests: Array<TestCase> = [ { name: "keccak256", params: [ "0x" ], hijackTag: 'hijacked keccak256: [{}]', algorithm: keccak256 }, { name: "sha256", params: [ "0x" ], hijackTag: 'hijacked sha256: [{}]', algorithm: sha256 }, { name: "sha512", params: [ "0x" ], hijackTag: 'hijacked sha512: [{}]', algorithm: sha512 }, { name: "ripemd160", params: [ "0x" ], hijackTag: 'hijacked ripemd160: [{}]', algorithm: ripemd160 }, { name: "pbkdf2", params: [ "0x", "0x", 1024, 32, "sha256" ], hijackTag: 'hijacked pbkdf2: [{},{},1024,32,"sha256"]', algorithm: pbkdf2 }, { name: "scryptSync", params: [ "0x", "0x", 1024, 8, 1, 32 ], hijackTag: 'hijacked scryptSync: [{},{},1024,8,1,32]', algorithm: scryptSync }, { name: "scrypt", params: [ "0x", "0x", 1024, 8, 1, 32 ], hijackTag: 'hijacked scrypt: [{},{},1024,8,1,32,null]', algorithm: scrypt }, { name: "computeHmac", params: [ "sha256", "0x", "0x" ], hijackTag: 'hijacked computeHmac: ["sha256",{},{}]', 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, postCheck }) => { it(`swaps in hijacked callback: ${ name }`, async function() { const initial = await algorithm(...params); algorithm.register(getHijack(name)); assert.equal(await algorithm(...params), "0x42"); assert.equal(hijack, hijackTag); algorithm.register(algorithm._); if (postCheck) { assert.ok(postCheck(await algorithm(...params))); } else { assert.equal(await algorithm(...params), initial); } }); }); it("prevents swapping after locked", function() { lock(); tests.forEach(({ name, params, hijackTag, algorithm }) => { assert.throws(function() { algorithm.register(getHijack("test")); }, function(error: any) { return (error.message === `${ name } is locked`); }); }); }); }); |