From 729066df329052ef273b7dfcfbb3b457d25108b9 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Wed, 20 Apr 2022 21:51:30 -0400 Subject: [PATCH] Group tests together by common prefix. --- packages/address/src.ts/tests/test-address.ts | 8 +- packages/rlp/src.ts/tests/test-rlp.ts | 4 +- packages/wallet/src.ts/tests/test-hdwallet.ts | 33 ++++- packages/wallet/src.ts/tests/test-json.ts | 53 ++++---- .../wordlists/src.ts/tests/test-wordlists.ts | 120 +++++++++--------- 5 files changed, 129 insertions(+), 89 deletions(-) diff --git a/packages/address/src.ts/tests/test-address.ts b/packages/address/src.ts/tests/test-address.ts index 633f9aa8d..3e5663750 100644 --- a/packages/address/src.ts/tests/test-address.ts +++ b/packages/address/src.ts/tests/test-address.ts @@ -17,7 +17,7 @@ import { describe("computes checksum address", function() { const tests = loadTests("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("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("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("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); }); } diff --git a/packages/rlp/src.ts/tests/test-rlp.ts b/packages/rlp/src.ts/tests/test-rlp.ts index 132da22c5..5e4114500 100644 --- a/packages/rlp/src.ts/tests/test-rlp.ts +++ b/packages/rlp/src.ts/tests/test-rlp.ts @@ -11,13 +11,13 @@ describe("Test RLP Coder", function() { const tests = loadTests("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); }); }); diff --git a/packages/wallet/src.ts/tests/test-hdwallet.ts b/packages/wallet/src.ts/tests/test-hdwallet.ts index 81de0fc4a..ffc8ab8aa 100644 --- a/packages/wallet/src.ts/tests/test-hdwallet.ts +++ b/packages/wallet/src.ts/tests/test-hdwallet.ts @@ -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("mnemonics"); + + const checks: Array = [ ]; 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); } }); - }); + } }); diff --git a/packages/wallet/src.ts/tests/test-json.ts b/packages/wallet/src.ts/tests/test-json.ts index 7cbabbe29..de4a0b3da 100644 --- a/packages/wallet/src.ts/tests/test-json.ts +++ b/packages/wallet/src.ts/tests/test-json.ts @@ -13,31 +13,37 @@ import { describe("Tests JSON Wallet Formats", function() { const tests = loadTests("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"); }); - }); }); diff --git a/packages/wordlists/src.ts/tests/test-wordlists.ts b/packages/wordlists/src.ts/tests/test-wordlists.ts index 8dfa732e5..55b8374ff 100644 --- a/packages/wordlists/src.ts/tests/test-wordlists.ts +++ b/packages/wordlists/src.ts/tests/test-wordlists.ts @@ -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 = [ ]; - 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("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 = [ ]; + 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); + }); + }); + }); });