tests: simplified imports for browser testing

This commit is contained in:
Richard Moore 2023-05-06 15:03:58 +09:00
parent c7b9be977c
commit 25ab579634
9 changed files with 68 additions and 41 deletions

31
src.ts/_tests/index.ts Normal file
View File

@ -0,0 +1,31 @@
import "./test-abi.js";
import "./test-address.js"
import "./test-contract.js";
import "./test-crypto.js";
import "./test-hash.js";
import "./test-hash-typeddata.js";
import "./test-providers-avatar.js";
import "./test-providers-ccip.js";
import "./test-providers-wildcard.js";
import "./test-rlp.js"
import "./test-transaction.js";
import "./test-utils-maths.js";
import "./test-utils-misc.js";
import "./test-utils-units.js";
import "./test-utils-utf8.js";
import "./test-wallet.js";
import "./test-wallet-hd.js";
import "./test-wallet-json.js";
import "./test-wallet-mnemonic.js";
import "./test-wordlists.js";
////import "./test-contract-integ.js";
////import "./test-providers-data.js";
//import "./test-providers-errors.js";
////import "./test-providers-extra.js";
////import "./test-providers-send.js";
/*
test-crypto-algoswap.ts
*/

View File

@ -1,7 +1,7 @@
import assert from "assert"; import assert from "assert";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import { TestCaseAbi, TestCaseAbiVerbose } from "./types.js"; import type { TestCaseAbi, TestCaseAbiVerbose } from "./types.js";
import { import {
AbiCoder, Interface, AbiCoder, Interface,

View File

@ -2,6 +2,8 @@ import assert from "assert";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import { getBytes } from "../index.js";
import type { TestCaseHash, TestCaseHmac, TestCasePbkdf } from "./types.js"; import type { TestCaseHash, TestCaseHmac, TestCasePbkdf } from "./types.js";
import { import {
@ -45,8 +47,8 @@ describe("test password-based key derivation", function() {
tests.forEach((test) => { tests.forEach((test) => {
it(`computes pbkdf2: ${ test.name}`, function() { it(`computes pbkdf2: ${ test.name}`, function() {
const password = Buffer.from(test.password.substring(2), "hex"); const password = getBytes(test.password);
const salt = Buffer.from(test.salt.substring(2), "hex"); const salt = getBytes(test.salt);
const { iterations, algorithm, key } = test.pbkdf2; const { iterations, algorithm, key } = test.pbkdf2;
const result = pbkdf2(password, salt, iterations, test.dkLen, algorithm); const result = pbkdf2(password, salt, iterations, test.dkLen, algorithm);
assert.equal(result, key); assert.equal(result, key);
@ -57,8 +59,8 @@ describe("test password-based key derivation", function() {
it(`computes scrypt (sync): ${ test.name}`, function() { it(`computes scrypt (sync): ${ test.name}`, function() {
this.timeout(1000); this.timeout(1000);
const password = Buffer.from(test.password.substring(2), "hex"); const password = getBytes(test.password);
const salt = Buffer.from(test.salt.substring(2), "hex"); const salt = getBytes(test.salt);
const { N, r, p, key } = test.scrypt; const { N, r, p, key } = test.scrypt;
const result = scryptSync(password, salt, N, r, p, test.dkLen); const result = scryptSync(password, salt, N, r, p, test.dkLen);
assert.equal(result, key); assert.equal(result, key);
@ -69,8 +71,8 @@ describe("test password-based key derivation", function() {
it(`computes scrypt (async): ${ test.name}`, async function() { it(`computes scrypt (async): ${ test.name}`, async function() {
this.timeout(1000); this.timeout(1000);
const password = Buffer.from(test.password.substring(2), "hex"); const password = getBytes(test.password);
const salt = Buffer.from(test.salt.substring(2), "hex"); const salt = getBytes(test.salt);
const { N, r, p, key } = test.scrypt; const { N, r, p, key } = test.scrypt;
let progressCount = 0, progressOk = true, lastProgress = -1; let progressCount = 0, progressOk = true, lastProgress = -1;

View File

@ -2,7 +2,7 @@ import assert from "assert";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import { decodeRlp, encodeRlp } from "../index.js"; import { decodeRlp, encodeRlp, hexlify } from "../index.js";
import type { TestCaseRlp } from "./types.js"; import type { TestCaseRlp } from "./types.js";
@ -18,7 +18,7 @@ describe("Test RLP Coder", function() {
tests.forEach(({ name, encoded, decoded }) => { tests.forEach(({ name, encoded, decoded }) => {
it(`decodes RLP: ${ name }`, function() { it(`decodes RLP: ${ name }`, function() {
assert.deepStrictEqual(decodeRlp(encoded), decoded); assert.deepEqual(decodeRlp(encoded), decoded);
}); });
}); });
}); });
@ -52,7 +52,7 @@ describe("Test bad RLP Data", function() {
}, (error: any) => { }, (error: any) => {
return (error.code === "BUFFER_OVERRUN" && return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^data too short/) && error.message.match(/^data too short/) &&
Buffer.from(error.buffer).toString("hex") === "" && hexlify(error.buffer) === "0x" &&
error.offset === 1 && error.offset === 1 &&
error.length === 0) error.length === 0)
}); });
@ -64,7 +64,7 @@ describe("Test bad RLP Data", function() {
}, (error: any) => { }, (error: any) => {
return (error.code === "BUFFER_OVERRUN" && return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^child data too short/) && error.message.match(/^child data too short/) &&
Buffer.from(error.buffer).toString("hex") === "c8880102030405060708" && hexlify(error.buffer) === "0xc8880102030405060708" &&
error.offset === 0 && error.offset === 0 &&
error.length === 8) error.length === 8)
}); });
@ -78,7 +78,7 @@ describe("Test bad RLP Data", function() {
}, (error: any) => { }, (error: any) => {
return (error.code === "BUFFER_OVERRUN" && return (error.code === "BUFFER_OVERRUN" &&
error.message.match(/^data short segment too short/) && error.message.match(/^data short segment too short/) &&
Buffer.from(error.buffer).toString("hex") === "c8c382c3823145" && hexlify(error.buffer) === "0xc8c382c3823145" &&
error.offset === 9 && error.offset === 9 &&
error.length === 7) error.length === 7)
}); });

View File

@ -1,27 +1,21 @@
import assert from "assert"; import assert from "assert";
import { wordlists } from "../wordlists/wordlists.js";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import { HDNodeWallet, HDNodeVoidWallet, Mnemonic } from "../index.js"; import {
getBytes, wordlists,
HDNodeWallet, HDNodeVoidWallet, Mnemonic
} from "../index.js";
import type { Wordlist } from "../wordlists/index.js"; import type { Wordlist } from "../wordlists/index.js";
import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js"; import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js";
/*
declare global {
class TextDecoder {
decode(data: Uint8Array): string;
}
}
*/
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 = getBytes(hex);
return decoder.decode(data); return decoder.decode(data);
} }

View File

@ -6,6 +6,7 @@ import type { TestCaseWallet } from "./types.js";
import { import {
isError, isError,
getBytes,
decryptCrowdsaleJson, decryptKeystoreJson, decryptKeystoreJsonSync, decryptCrowdsaleJson, decryptKeystoreJson, decryptKeystoreJsonSync,
encryptKeystoreJson, encryptKeystoreJsonSync, encryptKeystoreJson, encryptKeystoreJsonSync,
@ -19,7 +20,7 @@ describe("Tests JSON Wallet Formats", function() {
tests.forEach((test) => { tests.forEach((test) => {
if (test.type !== "crowdsale") { return; } 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 = getBytes(test.password);
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");
}); });
@ -29,7 +30,7 @@ describe("Tests JSON Wallet Formats", function() {
if (test.type !== "keystore") { return; } if (test.type !== "keystore") { return; }
it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() { it(`tests decrypting Keystore JSON (sync): ${ test.name }`, function() {
this.timeout(20000); this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex"); const password = getBytes(test.password);
const account = decryptKeystoreJsonSync(test.content, password); const account = decryptKeystoreJsonSync(test.content, password);
//console.log(account); //console.log(account);
assert.equal(account.address, test.address, "address"); assert.equal(account.address, test.address, "address");
@ -40,7 +41,7 @@ describe("Tests JSON Wallet Formats", function() {
if (test.type !== "keystore") { return; } if (test.type !== "keystore") { return; }
it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() { it(`tests decrypting Keystore JSON (async): ${ test.name }`, async function() {
this.timeout(20000); this.timeout(20000);
const password = Buffer.from(test.password.substring(2), "hex"); const password = getBytes(test.password);
const account = await decryptKeystoreJson(test.content, password); const account = await decryptKeystoreJson(test.content, password);
//console.log(account); //console.log(account);
assert.equal(account.address, test.address, "address"); assert.equal(account.address, test.address, "address");
@ -50,7 +51,7 @@ describe("Tests JSON Wallet Formats", function() {
tests.forEach((test) => { 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 = getBytes(test.password);
const wallet = Wallet.fromEncryptedJsonSync(test.content, password); const wallet = Wallet.fromEncryptedJsonSync(test.content, password);
//console.log(wallet); //console.log(wallet);
assert.equal(wallet.address, test.address, "address"); assert.equal(wallet.address, test.address, "address");
@ -60,7 +61,7 @@ describe("Tests JSON Wallet Formats", function() {
tests.forEach((test) => { 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 = getBytes(test.password);
const wallet = await Wallet.fromEncryptedJson(test.content, password); const wallet = await Wallet.fromEncryptedJson(test.content, password);
//console.log(wallet); //console.log(wallet);
assert.equal(wallet.address, test.address, "address"); assert.equal(wallet.address, test.address, "address");

View File

@ -1,19 +1,19 @@
import assert from "assert"; import assert from "assert";
import { sha256 } from "../crypto/index.js"; import {
import { toUtf8Bytes } from "../utils/utf8.js"; getBytes, sha256, toUtf8Bytes,
Mnemonic, wordlists,
import { wordlists } from "../wordlists/wordlists.js"; } from "../index.js";
import { Mnemonic } from "../index.js";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import type { TestCaseMnemonic } from "./types.js"; import type { TestCaseMnemonic } 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 = getBytes(hex);
return decoder.decode(data); return decoder.decode(data);
} }

View File

@ -1,13 +1,12 @@
import assert from 'assert'; import assert from "assert";
import { wordlists } from "../wordlists/wordlists.js"; import { wordlists } from "../index.js";
import { loadTests } from "./utils.js"; import { loadTests } from "./utils.js";
import type { TestCaseWordlist } from "./types.js"; import type { TestCaseWordlist } from "./types.js";
describe('Check Wordlists', function() { describe('Check Wordlists', function() {
const tests = loadTests<TestCaseWordlist>("wordlists"); const tests = loadTests<TestCaseWordlist>("wordlists");
@ -45,8 +44,8 @@ describe('Check Wordlists', function() {
const words2 = wordlist.split(phrase); const words2 = wordlist.split(phrase);
const phrase2 = wordlist.join(words2); const phrase2 = wordlist.join(words2);
assert.deepStrictEqual(words2, words, "split words"); assert.deepEqual(words2, words, "split words");
assert.deepStrictEqual(phrase2, phrase, "re-joined words"); assert.deepEqual(phrase2, phrase, "re-joined words");
}); });
}); });

View File

@ -1,7 +1,7 @@
import fs from "fs" import fs from "fs"
import path from "path"; import path from "path";
import zlib from 'zlib'; import zlib from "zlib";
// Find the package root (based on the nyc output/ folder) // Find the package root (based on the nyc output/ folder)
const root = (function() { const root = (function() {