Group tests together by common prefix.
This commit is contained in:
parent
b8024d9537
commit
729066df32
@ -17,7 +17,7 @@ import {
|
|||||||
describe("computes checksum address", function() {
|
describe("computes checksum address", function() {
|
||||||
const tests = loadTests<TestCaseAccount>("accounts");
|
const tests = loadTests<TestCaseAccount>("accounts");
|
||||||
for (const test of tests) {
|
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.address), test.address);
|
||||||
assert.equal(getAddress(test.icap), test.address);
|
assert.equal(getAddress(test.icap), test.address);
|
||||||
assert.equal(getAddress(test.address.substring(2)), 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() {
|
describe("computes ICAP address", function() {
|
||||||
const tests = loadTests<TestCaseAccount>("accounts");
|
const tests = loadTests<TestCaseAccount>("accounts");
|
||||||
for (const test of tests) {
|
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(getIcapAddress(test.address), test.icap);
|
||||||
assert.equal(getAddress(test.address.toLowerCase()), test.address);
|
assert.equal(getAddress(test.address.toLowerCase()), test.address);
|
||||||
assert.equal(getAddress("0x" + test.address.substring(2).toUpperCase()), 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");
|
const tests = loadTests<TestCaseCreate>("create");
|
||||||
for (const { sender, creates } of tests) {
|
for (const { sender, creates } of tests) {
|
||||||
for (const { name, nonce, address } of creates) {
|
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);
|
assert.equal(getCreateAddress({ from: sender, nonce }), address);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ describe("computes create2 address", function() {
|
|||||||
const tests = loadTests<TestCaseCreate2>("create2");
|
const tests = loadTests<TestCaseCreate2>("create2");
|
||||||
for (const { sender, creates } of tests) {
|
for (const { sender, creates } of tests) {
|
||||||
for (const { name, salt, initCodeHash, address } of creates) {
|
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);
|
assert.equal(getCreate2Address(sender, salt, initCodeHash), address);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,13 @@ describe("Test RLP Coder", function() {
|
|||||||
const tests = loadTests<TestCaseRlp>("rlp");
|
const tests = loadTests<TestCaseRlp>("rlp");
|
||||||
|
|
||||||
tests.forEach(({ name, encoded, decoded }) => {
|
tests.forEach(({ name, encoded, decoded }) => {
|
||||||
it(`encodes ${ name }`, function() {
|
it(`encodes RLP: ${ name }`, function() {
|
||||||
assert.equal(encodeRlp(decoded), encoded);
|
assert.equal(encodeRlp(decoded), encoded);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tests.forEach(({ name, encoded, decoded }) => {
|
tests.forEach(({ name, encoded, decoded }) => {
|
||||||
it(`decodes ${ name }`, function() {
|
it(`decodes RLP: ${ name }`, function() {
|
||||||
assert.deepStrictEqual(decodeRlp(encoded), decoded);
|
assert.deepStrictEqual(decodeRlp(encoded), decoded);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -4,18 +4,29 @@ import { wordlists } from "@ethersproject/wordlists/lib/wordlists.js";
|
|||||||
|
|
||||||
import { loadTests } from "./utils.js";
|
import { loadTests } from "./utils.js";
|
||||||
|
|
||||||
import { HDNodeWallet, HDNodeVoidWallet } from "../hdwallet.js";
|
import { HDNodeWallet, HDNodeVoidWallet, Mnemonic } from "../index.js";
|
||||||
import { Mnemonic } from "../mnemonic.js";
|
|
||||||
|
import type { Wordlist } from "@ethersproject/wordlists/lib/index.js";
|
||||||
|
|
||||||
import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js";
|
import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
function fromHex(hex: string): string {
|
function fromHex(hex: string): string {
|
||||||
const data = Buffer.from(hex.substring(2), "hex");
|
const data = Buffer.from(hex.substring(2), "hex");
|
||||||
return decoder.decode(data);
|
return decoder.decode(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Test = {
|
||||||
|
phrase: string;
|
||||||
|
password: string;
|
||||||
|
wordlist: Wordlist;
|
||||||
|
mnemonic: Mnemonic;
|
||||||
|
checkMnemonic: (a: Mnemonic) => void;
|
||||||
|
test: TestCaseMnemonic;
|
||||||
|
};
|
||||||
|
|
||||||
describe("Test HDWallets", function() {
|
describe("Test HDWallets", function() {
|
||||||
function checkWallet(wallet: HDNodeWallet | HDNodeVoidWallet, test: TestCaseMnemonicNode): void {
|
function checkWallet(wallet: HDNodeWallet | HDNodeVoidWallet, test: TestCaseMnemonicNode): void {
|
||||||
assert.equal(wallet.chainCode, test.chainCode, "chainCode");
|
assert.equal(wallet.chainCode, test.chainCode, "chainCode");
|
||||||
@ -35,6 +46,8 @@ describe("Test HDWallets", function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tests = loadTests<TestCaseMnemonic>("mnemonics");
|
const tests = loadTests<TestCaseMnemonic>("mnemonics");
|
||||||
|
|
||||||
|
const checks: Array<Test> = [ ];
|
||||||
tests.forEach((test) => {
|
tests.forEach((test) => {
|
||||||
// The phrase and password are stored in the test as hex so they
|
// The phrase and password are stored in the test as hex so they
|
||||||
// are safe as ascii7 values for viewing, printing, etc.
|
// are safe as ascii7 values for viewing, printing, etc.
|
||||||
@ -58,6 +71,12 @@ describe("Test HDWallets", function() {
|
|||||||
assert.equal(actual.computeSeed(), mnemonic.computeSeed(), "seed");
|
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() {
|
it(`computes the HD keys by mnemonic: ${ test.name }`, function() {
|
||||||
for (const subtest of test.nodes) {
|
for (const subtest of test.nodes) {
|
||||||
const w = HDNodeWallet.fromPhrase(phrase, password, subtest.path, wordlist);
|
const w = HDNodeWallet.fromPhrase(phrase, password, subtest.path, wordlist);
|
||||||
@ -68,7 +87,9 @@ describe("Test HDWallets", function() {
|
|||||||
checkMnemonic(w.mnemonic as Mnemonic);
|
checkMnemonic(w.mnemonic as Mnemonic);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const { test } of checks) {
|
||||||
it(`computes the HD keys by entropy: ${ test.name }`, function() {
|
it(`computes the HD keys by entropy: ${ test.name }`, function() {
|
||||||
const seedRoot = HDNodeWallet.fromSeed(test.seed);
|
const seedRoot = HDNodeWallet.fromSeed(test.seed);
|
||||||
for (const subtest of test.nodes) {
|
for (const subtest of test.nodes) {
|
||||||
@ -79,7 +100,9 @@ describe("Test HDWallets", function() {
|
|||||||
assert.equal(w.mnemonic, null);
|
assert.equal(w.mnemonic, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const { test } of checks) {
|
||||||
it(`computes the HD keys by enxtended private key: ${ test.name }`, function() {
|
it(`computes the HD keys by enxtended private key: ${ test.name }`, function() {
|
||||||
for (const subtest of test.nodes) {
|
for (const subtest of test.nodes) {
|
||||||
const w = HDNodeWallet.fromExtendedKey(subtest.xpriv);
|
const w = HDNodeWallet.fromExtendedKey(subtest.xpriv);
|
||||||
@ -88,7 +111,9 @@ describe("Test HDWallets", function() {
|
|||||||
assert.equal(w.mnemonic, null);
|
assert.equal(w.mnemonic, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const { test, phrase, password, wordlist } of checks) {
|
||||||
it(`computes the neutered HD keys by paths: ${ test.name }`, function() {
|
it(`computes the neutered HD keys by paths: ${ test.name }`, function() {
|
||||||
const root = HDNodeWallet.fromPhrase(phrase, password, "m", wordlist).neuter();
|
const root = HDNodeWallet.fromPhrase(phrase, password, "m", wordlist).neuter();
|
||||||
for (const subtest of test.nodes) {
|
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() {
|
it(`computes the neutered HD keys by enxtended public key: ${ test.name }`, function() {
|
||||||
for (const subtest of test.nodes) {
|
for (const subtest of test.nodes) {
|
||||||
const w = HDNodeWallet.fromExtendedKey(subtest.xpub);
|
const w = HDNodeWallet.fromExtendedKey(subtest.xpub);
|
||||||
@ -117,5 +144,5 @@ describe("Test HDWallets", function() {
|
|||||||
checkWallet(w, subtest);
|
checkWallet(w, subtest);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
|
@ -13,31 +13,37 @@ import {
|
|||||||
describe("Tests JSON Wallet Formats", function() {
|
describe("Tests JSON Wallet Formats", function() {
|
||||||
const tests = loadTests<TestCaseWallet>("wallets");
|
const tests = loadTests<TestCaseWallet>("wallets");
|
||||||
tests.forEach((test) => {
|
tests.forEach((test) => {
|
||||||
if (test.type === "crowdsale") {
|
if (test.type !== "crowdsale") { return; }
|
||||||
it(`tests decrypting Crowdsale JSON: ${ test.name }`, async function() {
|
it(`tests decrypting Crowdsale JSON: ${ test.name }`, async function() {
|
||||||
const password = Buffer.from(test.password.substring(2), "hex");
|
const password = Buffer.from(test.password.substring(2), "hex");
|
||||||
const account = decryptCrowdsaleJson(test.content, password);
|
const account = decryptCrowdsaleJson(test.content, password);
|
||||||
assert.equal(account.address, test.address, "address");
|
assert.equal(account.address, test.address, "address");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
} else if (test.type === "keystore") {
|
tests.forEach((test) => {
|
||||||
it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() {
|
if (test.type !== "keystore") { return; }
|
||||||
this.timeout(20000);
|
it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() {
|
||||||
const password = Buffer.from(test.password.substring(2), "hex");
|
this.timeout(20000);
|
||||||
const account = decryptKeystoreJsonSync(test.content, password);
|
const password = Buffer.from(test.password.substring(2), "hex");
|
||||||
//console.log(account);
|
const account = decryptKeystoreJsonSync(test.content, password);
|
||||||
assert.equal(account.address, test.address, "address");
|
//console.log(account);
|
||||||
});
|
assert.equal(account.address, test.address, "address");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() {
|
tests.forEach((test) => {
|
||||||
this.timeout(20000);
|
if (test.type !== "keystore") { return; }
|
||||||
const password = Buffer.from(test.password.substring(2), "hex");
|
it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() {
|
||||||
const account = await decryptKeystoreJson(test.content, password);
|
this.timeout(20000);
|
||||||
//console.log(account);
|
const password = Buffer.from(test.password.substring(2), "hex");
|
||||||
assert.equal(account.address, test.address, "address");
|
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() {
|
it(`tests decrypting JSON (sync): ${ test.name }`, function() {
|
||||||
this.timeout(20000);
|
this.timeout(20000);
|
||||||
const password = Buffer.from(test.password.substring(2), "hex");
|
const password = Buffer.from(test.password.substring(2), "hex");
|
||||||
@ -45,7 +51,9 @@ describe("Tests JSON Wallet Formats", function() {
|
|||||||
//console.log(wallet);
|
//console.log(wallet);
|
||||||
assert.equal(wallet.address, test.address, "address");
|
assert.equal(wallet.address, test.address, "address");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tests.forEach((test) => {
|
||||||
it(`tests decrypting JSON (async): ${ test.name }`, async function() {
|
it(`tests decrypting JSON (async): ${ test.name }`, async function() {
|
||||||
this.timeout(20000);
|
this.timeout(20000);
|
||||||
const password = Buffer.from(test.password.substring(2), "hex");
|
const password = Buffer.from(test.password.substring(2), "hex");
|
||||||
@ -53,6 +61,5 @@ describe("Tests JSON Wallet Formats", function() {
|
|||||||
//console.log(wallet);
|
//console.log(wallet);
|
||||||
assert.equal(wallet.address, test.address, "address");
|
assert.equal(wallet.address, test.address, "address");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -6,68 +6,74 @@ import { loadTests } from "./utils.js";
|
|||||||
|
|
||||||
import type { TestCaseWordlist } from "./types.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() {
|
describe('Check Wordlists', function() {
|
||||||
const tests = loadTests<TestCaseWordlist>("wordlists");
|
const tests = loadTests<TestCaseWordlist>("wordlists");
|
||||||
|
|
||||||
tests.forEach((test) => {
|
tests.forEach((test) => {
|
||||||
let wordlist = wordlists[test.locale];
|
let wordlist = wordlists[test.locale];
|
||||||
if (wordlist == null) { return; }
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user