tests: added more tests

This commit is contained in:
Richard Moore 2022-09-30 20:00:11 -04:00
parent 58ae6b7fc5
commit f4d2721baf
5 changed files with 93 additions and 140 deletions

@ -98,141 +98,3 @@ describe("test hmac", function() {
});
});
});
/*
describe("test registration", function() {
let hijack = "";
function getHijack(algo: string) {
return function(...args: Array<any>) {
hijack = `hijacked ${ algo }: ${ JSON.stringify(args) }`;
return "0x42";
}
}
it("hijacks keccak256", function() {
const initial = keccak256("0x");
keccak256.register(getHijack("kecak256"));
assert.equal(keccak256("0x"), "0x42");
assert.equal(hijack, 'hijacked kecak256: [{}]');
keccak256.register(keccak256._);
assert.equal(keccak256("0x"), initial);
keccak256.lock();
assert.throws(function() {
keccak256.register(getHijack("test"));
}, function(error) {
return (error.message === "keccak256 is locked");
});
});
it("hijacks sha256", function() {
const initial = sha256("0x");
sha256.register(getHijack("sha256"));
assert.equal(sha256("0x"), "0x42");
assert.equal(hijack, 'hijacked sha256: [{}]');
sha256.register(sha256._);
assert.equal(sha256("0x"), initial);
sha256.lock();
assert.throws(function() {
sha256.register(getHijack("test"));
}, function(error) {
return (error.message === "sha256 is locked");
});
});
it("hijacks sha512", function() {
const initial = sha512("0x");
sha512.register(getHijack("sha512"));
assert.equal(sha512("0x"), "0x42");
assert.equal(hijack, 'hijacked sha512: [{}]');
sha512.register(sha512._);
assert.equal(sha512("0x"), initial);
sha512.lock();
assert.throws(function() {
sha512.register(getHijack("test"));
}, function(error) {
return (error.message === "sha512 is locked");
});
});
it("hijacks pbkdf2", function() {
const initial = pbkdf2("0x", "0x", 1024, 32, "sha256");
pbkdf2.register(getHijack("pbkdf2"));
assert.equal(pbkdf2("0x", "0x", 1024, 32, "sha256"), "0x42");
assert.equal(hijack, 'hijacked pbkdf2: [{},{},1024,32,"sha256"]');
pbkdf2.register(pbkdf2._);
assert.equal(pbkdf2("0x", "0x", 1024, 32, "sha256"), initial);
pbkdf2.lock();
assert.throws(function() {
pbkdf2.register(getHijack("test"));
}, function(error) {
return (error.message === "pbkdf2 is locked");
});
});
it("hijacks scryptSync", function() {
function getHijack(...args: Array<any>) {
hijack = `hijacked scryptSync: ${ JSON.stringify(args) }`;
return new Uint8Array([ 0x42 ]);
}
const initial = scryptSync("0x", "0x", 1024, 8, 1, 32);
scryptSync.register(getHijack);
assert.equal(scryptSync("0x", "0x", 1024, 8, 1, 32), "0x42");
assert.equal(hijack, 'hijacked scryptSync: [{},{},1024,8,1,32]');
scryptSync.register(scryptSync._);
assert.equal(scryptSync("0x", "0x", 1024, 8, 1, 32), initial);
scryptSync.lock();
assert.throws(function() {
scryptSync.register(getHijack);
}, function(error) {
return (error.message === "scryptSync is locked");
});
});
it("hijacks scrypt", async function() {
function getHijack(...args: Array<any>) {
hijack = `hijacked scrypt: ${ JSON.stringify(args) }`;
return Promise.resolve(new Uint8Array([ 0x42 ]));
}
const initial = await scrypt("0x", "0x", 1024, 8, 1, 32);
scrypt.register(getHijack);
assert.equal(await scrypt("0x", "0x", 1024, 8, 1, 32), "0x42");
assert.equal(hijack, 'hijacked scrypt: [{},{},1024,8,1,32,null]');
scrypt.register(scrypt._);
assert.equal(await scrypt("0x", "0x", 1024, 8, 1, 32), initial);
scrypt.lock();
assert.throws(function() {
scrypt.register(getHijack);
}, function(error) {
return (error.message === "scrypt is locked");
});
});
});
*/

@ -1,7 +1,15 @@
/*
import assert from "assert";
import {
hashMessage,
solidityPackedKeccak256, solidityPackedSha256
} from "../index.js";
import { loadTests } from "./utils.js"
import type { TestCaseNamehash } from "./types.js";
import type { TestCaseSolidityHash } from "./types.js";
/*
import { dnsEncode, isValidName, namehash } from "../index.js";
@ -108,3 +116,45 @@ describe("Tests DNS Names", function() {
});
*/
describe("Test EIP-191 Personal Message Hash", function() {
const tests = [
{
test: "hello-world",
message: "Hello World",
hash: "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2"
},
{
test: "binary-message",
message: new Uint8Array([ 0x42, 0x43 ]),
hash: "0x0d3abc18ec299cf9b42ba439ac6f7e3e6ec9f5c048943704e30fc2d9c7981438"
},
{
test: "hex-looking-string",
message: "0x4243",
hash: "0x6d91b221f765224b256762dcba32d62209cf78e9bebb0a1b758ca26c76db3af4"
}
];
for (const test of tests) {
it(`tests hashMessage: ${ test.test }`, function() {
assert.equal(hashMessage(test.message), test.hash);
});
}
})
describe("Test Solidity Hash functions", function() {
const tests = loadTests<TestCaseSolidityHash>("solidity-hashes");
for (const test of tests) {
it(`computes the solidity keccak256: ${ test.name }`, function() {
assert.equal(solidityPackedKeccak256(test.types, test.values), test.keccak256);
});
}
for (const test of tests) {
it(`computes the solidity sha256: ${ test.name }`, function() {
assert.equal(solidityPackedSha256(test.types, test.values), test.sha256);
});
}
})

@ -0,0 +1,33 @@
import assert from "assert";
import {
decodeBase64, encodeBase64,
toUtf8Bytes
} from "../index.js";
describe("Base64 Coding", function() {
const tests = [
{
name: "wikipedia",
plaintext: toUtf8Bytes("Many hands make light work."),
encoded: "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"
}
];
for (const test of tests) {
it(`encodes base64: ${ test.name }`, function() {
assert.equal(encodeBase64(test.plaintext), test.encoded);
});
}
for (const test of tests) {
it(`decodes base64: ${ test.name }`, function() {
const decoded = decodeBase64(test.encoded);
assert.equal(decoded.length, test.plaintext.length, "data.length");
for (let i = 0; i < decoded.length; i++) {
assert.equal(decoded[i], test.plaintext[i]);
}
});
}
})

@ -138,6 +138,14 @@ export interface TestCaseTypedData {
signature?: string;
}
export interface TestCaseSolidityHash {
name: string;
types: Array<string>;
keccak256: string;
ripemd160: string;
sha256: string;
values: Array<any>
}
/////////////////////////////
// rlp

Binary file not shown.