Group tests together by common prefix.

This commit is contained in:
Richard Moore 2022-04-20 21:51:30 -04:00
parent b8024d9537
commit 729066df32
5 changed files with 129 additions and 89 deletions

View File

@ -17,7 +17,7 @@ import {
describe("computes checksum address", function() {
const tests = loadTests<TestCaseAccount>("accounts");
for (const test of tests) {
it(`computes the checksum address for ${ test.name }`, function() {
it(`computes the checksum address: ${ test.name }`, function() {
assert.equal(getAddress(test.address), test.address);
assert.equal(getAddress(test.icap), test.address);
assert.equal(getAddress(test.address.substring(2)), test.address);
@ -75,7 +75,7 @@ describe("computes checksum address", function() {
describe("computes ICAP address", function() {
const tests = loadTests<TestCaseAccount>("accounts");
for (const test of tests) {
it(`computes the ICAP address for ${ test.name }`, function() {
it(`computes the ICAP address: ${ test.name }`, function() {
assert.equal(getIcapAddress(test.address), test.icap);
assert.equal(getAddress(test.address.toLowerCase()), test.address);
assert.equal(getAddress("0x" + test.address.substring(2).toUpperCase()), test.address);
@ -87,7 +87,7 @@ describe("computes create address", function() {
const tests = loadTests<TestCaseCreate>("create");
for (const { sender, creates } of tests) {
for (const { name, nonce, address } of creates) {
it(`computes the create address for ${ name }`, function() {
it(`computes the create address: ${ name }`, function() {
assert.equal(getCreateAddress({ from: sender, nonce }), address);
});
}
@ -98,7 +98,7 @@ describe("computes create2 address", function() {
const tests = loadTests<TestCaseCreate2>("create2");
for (const { sender, creates } of tests) {
for (const { name, salt, initCodeHash, address } of creates) {
it(`computes the create2 address for ${ name }`, function() {
it(`computes the create2 address: ${ name }`, function() {
assert.equal(getCreate2Address(sender, salt, initCodeHash), address);
});
}

View File

@ -11,13 +11,13 @@ describe("Test RLP Coder", function() {
const tests = loadTests<TestCaseRlp>("rlp");
tests.forEach(({ name, encoded, decoded }) => {
it(`encodes ${ name }`, function() {
it(`encodes RLP: ${ name }`, function() {
assert.equal(encodeRlp(decoded), encoded);
});
});
tests.forEach(({ name, encoded, decoded }) => {
it(`decodes ${ name }`, function() {
it(`decodes RLP: ${ name }`, function() {
assert.deepStrictEqual(decodeRlp(encoded), decoded);
});
});

View File

@ -4,18 +4,29 @@ import { wordlists } from "@ethersproject/wordlists/lib/wordlists.js";
import { loadTests } from "./utils.js";
import { HDNodeWallet, HDNodeVoidWallet } from "../hdwallet.js";
import { Mnemonic } from "../mnemonic.js";
import { HDNodeWallet, HDNodeVoidWallet, Mnemonic } from "../index.js";
import type { Wordlist } from "@ethersproject/wordlists/lib/index.js";
import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js";
const decoder = new TextDecoder();
function fromHex(hex: string): string {
const data = Buffer.from(hex.substring(2), "hex");
return decoder.decode(data);
}
type Test = {
phrase: string;
password: string;
wordlist: Wordlist;
mnemonic: Mnemonic;
checkMnemonic: (a: Mnemonic) => void;
test: TestCaseMnemonic;
};
describe("Test HDWallets", function() {
function checkWallet(wallet: HDNodeWallet | HDNodeVoidWallet, test: TestCaseMnemonicNode): void {
assert.equal(wallet.chainCode, test.chainCode, "chainCode");
@ -35,6 +46,8 @@ describe("Test HDWallets", function() {
}
const tests = loadTests<TestCaseMnemonic>("mnemonics");
const checks: Array<Test> = [ ];
tests.forEach((test) => {
// The phrase and password are stored in the test as hex so they
// are safe as ascii7 values for viewing, printing, etc.
@ -58,6 +71,12 @@ describe("Test HDWallets", function() {
assert.equal(actual.computeSeed(), mnemonic.computeSeed(), "seed");
}
checks.push({
phrase, password, wordlist, mnemonic, checkMnemonic, test
});
});
for (const { test, checkMnemonic, phrase, password, wordlist } of checks) {
it(`computes the HD keys by mnemonic: ${ test.name }`, function() {
for (const subtest of test.nodes) {
const w = HDNodeWallet.fromPhrase(phrase, password, subtest.path, wordlist);
@ -68,7 +87,9 @@ describe("Test HDWallets", function() {
checkMnemonic(w.mnemonic as Mnemonic);
}
});
}
for (const { test } of checks) {
it(`computes the HD keys by entropy: ${ test.name }`, function() {
const seedRoot = HDNodeWallet.fromSeed(test.seed);
for (const subtest of test.nodes) {
@ -79,7 +100,9 @@ describe("Test HDWallets", function() {
assert.equal(w.mnemonic, null);
}
});
}
for (const { test } of checks) {
it(`computes the HD keys by enxtended private key: ${ test.name }`, function() {
for (const subtest of test.nodes) {
const w = HDNodeWallet.fromExtendedKey(subtest.xpriv);
@ -88,7 +111,9 @@ describe("Test HDWallets", function() {
assert.equal(w.mnemonic, null);
}
});
}
for (const { test, phrase, password, wordlist } of checks) {
it(`computes the neutered HD keys by paths: ${ test.name }`, function() {
const root = HDNodeWallet.fromPhrase(phrase, password, "m", wordlist).neuter();
for (const subtest of test.nodes) {
@ -109,7 +134,9 @@ describe("Test HDWallets", function() {
}
}
});
}
for (const { test } of checks) {
it(`computes the neutered HD keys by enxtended public key: ${ test.name }`, function() {
for (const subtest of test.nodes) {
const w = HDNodeWallet.fromExtendedKey(subtest.xpub);
@ -117,5 +144,5 @@ describe("Test HDWallets", function() {
checkWallet(w, subtest);
}
});
});
}
});

View File

@ -13,31 +13,37 @@ import {
describe("Tests JSON Wallet Formats", function() {
const tests = loadTests<TestCaseWallet>("wallets");
tests.forEach((test) => {
if (test.type === "crowdsale") {
it(`tests decrypting Crowdsale JSON: ${ test.name }`, async function() {
const password = Buffer.from(test.password.substring(2), "hex");
const account = decryptCrowdsaleJson(test.content, password);
assert.equal(account.address, test.address, "address");
});
if (test.type !== "crowdsale") { return; }
it(`tests decrypting Crowdsale JSON: ${ test.name }`, async function() {
const password = Buffer.from(test.password.substring(2), "hex");
const account = decryptCrowdsaleJson(test.content, password);
assert.equal(account.address, test.address, "address");
});
});
} else if (test.type === "keystore") {
it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
const account = decryptKeystoreJsonSync(test.content, password);
//console.log(account);
assert.equal(account.address, test.address, "address");
});
tests.forEach((test) => {
if (test.type !== "keystore") { return; }
it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
const account = decryptKeystoreJsonSync(test.content, password);
//console.log(account);
assert.equal(account.address, test.address, "address");
});
});
it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
const account = await decryptKeystoreJson(test.content, password);
//console.log(account);
assert.equal(account.address, test.address, "address");
});
}
tests.forEach((test) => {
if (test.type !== "keystore") { return; }
it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
const account = await decryptKeystoreJson(test.content, password);
//console.log(account);
assert.equal(account.address, test.address, "address");
});
});
tests.forEach((test) => {
it(`tests decrypting JSON (sync): ${ test.name }`, function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
@ -45,7 +51,9 @@ describe("Tests JSON Wallet Formats", function() {
//console.log(wallet);
assert.equal(wallet.address, test.address, "address");
});
});
tests.forEach((test) => {
it(`tests decrypting JSON (async): ${ test.name }`, async function() {
this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex");
@ -53,6 +61,5 @@ describe("Tests JSON Wallet Formats", function() {
//console.log(wallet);
assert.equal(wallet.address, test.address, "address");
});
});
});

View File

@ -6,68 +6,74 @@ import { loadTests } from "./utils.js";
import type { TestCaseWordlist } from "./types.js";
import type { Wordlist } from "../wordlist.js";
function checkWordlist(test: TestCaseWordlist, wordlist: Wordlist): void {
it(`matches wordlists: ${ test.locale }`, function() {
const words = test.content.split('\n');
let check = "";
for (let i = 0; i < 2048; i++) {
let word = wordlist.getWord(i);
check += (word + "\n");
assert.equal(word, words[i]);
assert.equal(wordlist.getWordIndex(word), i);
}
assert.equal(check, test.content);
});
it (`splitting and joining are equivalent: ${ test.locale }`, function() {
const words: Array<string> = [ ];
for (let i = 0; i < 12; i++) {
words.push(wordlist.getWord(i));
}
const phrase = wordlist.join(words);
const words2 = wordlist.split(phrase);
const phrase2 = wordlist.join(words2);
assert.deepStrictEqual(words2, words, "split words");
assert.deepStrictEqual(phrase2, phrase, "re-joined words");
});
it(`handles out-of-range values: ${ test.locale }`, function() {
assert.equal(wordlist.getWordIndex("foobar"), -1);
assert.throws(() => {
wordlist.getWord(-1);
}, (error) => {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid word index/) &&
error.argument === "index" &&
error.value === -1);
});
assert.throws(() => {
wordlist.getWord(2048);
}, (error) => {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid word index/) &&
error.argument === "index" &&
error.value === 2048);
});
});
}
describe('Check Wordlists', function() {
const tests = loadTests<TestCaseWordlist>("wordlists");
tests.forEach((test) => {
let wordlist = wordlists[test.locale];
if (wordlist == null) { return; }
checkWordlist(test, wordlist);
it(`matches wordlists: ${ test.locale }`, function() {
const words = test.content.split('\n');
let check = "";
for (let i = 0; i < 2048; i++) {
let word = wordlist.getWord(i);
check += (word + "\n");
assert.equal(word, words[i]);
assert.equal(wordlist.getWordIndex(word), i);
}
assert.equal(check, test.content);
});
});
tests.forEach((test) => {
let wordlist = wordlists[test.locale];
if (wordlist == null) { return; }
it (`splitting and joining are equivalent: ${ test.locale }`, function() {
const words: Array<string> = [ ];
for (let i = 0; i < 12; i++) {
words.push(wordlist.getWord(i));
}
const phrase = wordlist.join(words);
const words2 = wordlist.split(phrase);
const phrase2 = wordlist.join(words2);
assert.deepStrictEqual(words2, words, "split words");
assert.deepStrictEqual(phrase2, phrase, "re-joined words");
});
});
tests.forEach((test) => {
let wordlist = wordlists[test.locale];
if (wordlist == null) { return; }
it(`handles out-of-range values: ${ test.locale }`, function() {
assert.equal(wordlist.getWordIndex("foobar"), -1);
assert.throws(() => {
wordlist.getWord(-1);
}, (error) => {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid word index/) &&
error.argument === "index" &&
error.value === -1);
});
assert.throws(() => {
wordlist.getWord(2048);
}, (error) => {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid word index/) &&
error.argument === "index" &&
error.value === 2048);
});
});
});
});