Use describe.each for onlyJs flag (#1029)

* Use describe.each for onlyJs flag

* base validate checks
This commit is contained in:
Jaco
2021-06-23 12:55:29 +02:00
committed by GitHub
parent 470b43578e
commit ab3268407c
14 changed files with 113 additions and 139 deletions
+2 -2
View File
@@ -13,8 +13,8 @@ interface CheckConfig {
const BASE_CONFIG = { alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' };
export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: string | null, ipfsCompat?: boolean): true {
assert(value, () => `Expected non-null, non-empty ${type} input`);
export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: unknown, ipfsCompat?: boolean): true {
assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`);
assert(!ipfsCompat || value[0] === ipfsChar, () => `Expected ${type} to start with '${ipfsChar}'`);
for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) {
+1 -1
View File
@@ -10,7 +10,7 @@ import { assert } from '@polkadot/util';
* Validates that the supplied value is valid base64
*/
export function base64Validate (value?: string | null): value is string {
assert(value, 'Expected non-null, non-empty base64 input');
assert(value && typeof value === 'string', 'Expected non-null, non-empty base64 string input');
assert(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value), 'Invalid base64 encoding');
return true;
@@ -8,9 +8,7 @@ import { isEthereumChecksum } from './isChecksum';
export function isEthereumAddress (address?: string): boolean {
if (!address || address.length !== 42 || !isHex(address)) {
return false;
}
if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
return true;
}
@@ -6,7 +6,11 @@ import { u8aToHex } from '@polkadot/util';
import { keccakAsU8a } from '../keccak';
function isInvalidChar (char: string, byte: number): boolean {
return (byte > 7 && char !== char.toUpperCase()) || (byte <= 7 && char !== char.toLowerCase());
return char !== (
byte > 7
? char.toUpperCase()
: char.toLowerCase()
);
}
export function isEthereumChecksum (_address: string): boolean {
+5 -15
View File
@@ -6,8 +6,6 @@ import { waitReady } from '@polkadot/wasm-crypto';
import { keccakAsU8a } from '.';
const BITS: (256 | 512)[] = [256, 512];
describe('keccakAsU8a', (): void => {
beforeEach(async (): Promise<void> => {
await waitReady();
@@ -23,29 +21,21 @@ describe('keccakAsU8a', (): void => {
)
};
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (string, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
it('returns an hex representation (string)', (): void => {
expect(
keccakAsU8a(input, bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Buffer, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
it('returns an hex representation (Buffer)', (): void => {
expect(
keccakAsU8a(Buffer.from(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Uint8Array, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
it('returns an hex representation (Uint8Array)', (): void => {
expect(
keccakAsU8a(stringToU8a(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
@@ -16,20 +16,16 @@ describe('mnemonicGenerate', (): void => {
).toEqual(true);
});
[false, true].forEach((onlyJs): void => {
([12, 15, 18, 21, 24] as WordCount[]).forEach((num: WordCount): void => {
it(`generates a valid mnemonic (${num} words, onlyJs=${onlyJs.toString()})`, (): void => {
const mnemonic = mnemonicGenerate(num, onlyJs);
const isValid = mnemonicValidate(mnemonic);
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it.each([12, 15, 18, 21, 24] as WordCount[])('generates a valid mnemonic (%p words)', (num): void => {
const mnemonic = mnemonicGenerate(num, onlyJs);
const isValid = mnemonicValidate(mnemonic);
expect(mnemonic.split(' ')).toHaveLength(num);
expect(isValid).toEqual(true);
});
expect(mnemonic.split(' ')).toHaveLength(num);
expect(isValid).toEqual(true);
});
});
[true, false].forEach((onlyJs): void => {
it(`generates not deterministic (onlyJs=${onlyJs.toString()})`, (): void => {
it('generates not deterministic', (): void => {
const m1 = mnemonicGenerate(24, onlyJs);
const m2 = mnemonicGenerate(24, onlyJs);
@@ -12,9 +12,9 @@ describe('mnemonicToEntropy', (): void => {
await cryptoWaitReady();
});
tests.forEach(([mnemonic, entropy], index): void => {
[false, true].forEach((onlyJs): void => {
it(`Created correct entropy for ${index} (onlyJs = ${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
tests.forEach(([mnemonic, entropy], index): void => {
it(`Created correct entropy for ${index}`, (): void => {
expect(u8aToHex(mnemonicToEntropy(mnemonic, onlyJs))).toEqual(entropy);
});
});
@@ -15,35 +15,29 @@ describe('mnemonicToLegacySeed', (): void => {
await waitReady();
});
[undefined, 'foo', 'bar'].forEach((password): void => {
it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => {
expect(
u8aEq(
mnemonicToLegacySeed(MNEMONIC, password, true),
mnemonicToLegacySeed(MNEMONIC, password, false)
)
).toEqual(true);
});
it.each([undefined, 'foo', 'bar'])('generates Wasm & Js equivalents (password = %p', (password): void => {
expect(
u8aEq(
mnemonicToLegacySeed(MNEMONIC, password, true),
mnemonicToLegacySeed(MNEMONIC, password, false)
)
).toEqual(true);
});
[false, true].forEach((onlyJs): void => {
it(`generates a valid 64bytes seed (onlyJs = ${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('generates a valid 64bytes seed', (): void => {
expect(
u8aToHex(mnemonicToLegacySeed(MNEMONIC, undefined, onlyJs, 64))
).toEqual(SEED_64);
});
});
[false, true].forEach((onlyJs): void => {
it(`generates a valid 32bytes seed (onlyJs = ${onlyJs.toString()})`, (): void => {
it('generates a valid 32bytes seed', (): void => {
expect(
u8aToHex(mnemonicToLegacySeed(MNEMONIC, undefined, onlyJs))
).toEqual(SEED_32);
});
});
[false, true].forEach((onlyJs): void => {
it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => {
it('fails with non-mnemonics', (): void => {
expect(
() => mnemonicToLegacySeed('foo bar baz', undefined, onlyJs)
).toThrow(/mnemonic specified/);
@@ -15,28 +15,30 @@ describe('mnemonicToMiniSecret', (): void => {
await cryptoWaitReady();
});
[undefined, 'foo', 'bar'].forEach((password): void => {
it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => {
expect(
u8aEq(
mnemonicToMiniSecret(MNEMONIC, password, true),
mnemonicToMiniSecret(MNEMONIC, password, false)
)
).toEqual(true);
});
it.each([undefined, 'foo', 'bar'])('generates Wasm & Js equivalents for password = %p', (password): void => {
expect(
u8aEq(
mnemonicToMiniSecret(MNEMONIC, password, true),
mnemonicToMiniSecret(MNEMONIC, password, false)
)
).toEqual(true);
});
[false, true].forEach((onlyJs): void => {
it(`generates a valid seed (onlyJs = ${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('generates a valid seed', (): void => {
expect(
u8aToHex(mnemonicToMiniSecret(MNEMONIC, undefined, onlyJs))
).toEqual(SEED);
});
});
tests.forEach(([mnemonic, , seed], index): void => {
[false, true].forEach((onlyJs): void => {
it(`Created correct seed for ${index} (onlyJs = ${onlyJs.toString()})`, (): void => {
it('fails with non-mnemonics', (): void => {
expect(
() => mnemonicToMiniSecret('foo bar baz', undefined, onlyJs)
).toThrow(/mnemonic specified/);
});
tests.forEach(([mnemonic, , seed], index): void => {
it(`Created correct seed for ${index}`, (): void => {
expect(
u8aToHex(mnemonicToMiniSecret(mnemonic, 'Substrate', onlyJs))
).toEqual(
@@ -46,12 +48,4 @@ describe('mnemonicToMiniSecret', (): void => {
});
});
});
[false, true].forEach((onlyJs): void => {
it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => {
expect(
() => mnemonicToMiniSecret('foo bar baz', undefined, onlyJs)
).toThrow(/mnemonic specified/);
});
});
});
+2 -2
View File
@@ -11,8 +11,8 @@ describe('naclSign', (): void => {
await waitReady();
});
[false, true].forEach((onlyJs): void => {
it(`returns a valid signature for the message (onlyJs=${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('returns a valid signature for the message', (): void => {
expect(
naclSign(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
+43 -39
View File
@@ -19,8 +19,8 @@ describe('naclVerify', (): void => {
signature = new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7]);
});
[false, true].forEach((onlyJs): void => {
it(`validates a correctly signed message (onlyJs=${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('validates a correctly signed message', (): void => {
expect(
naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
@@ -30,47 +30,51 @@ describe('naclVerify', (): void => {
)
).toEqual(true);
});
});
it('fails a correctly signed message (message changed)', (): void => {
expect(
naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]),
signature,
publicKey
)
).toEqual(false);
});
it('fails a correctly signed message (message changed)', (): void => {
expect(
naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]),
signature,
publicKey,
onlyJs
)
).toEqual(false);
});
it('fails a correctly signed message (signature changed)', (): void => {
signature[0] = 0xff;
it('fails a correctly signed message (signature changed)', (): void => {
signature[0] = 0xff;
expect(
naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
signature,
publicKey
)
).toEqual(false);
});
expect(
naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
signature,
publicKey,
onlyJs
)
).toEqual(false);
});
it('throws error when publicKey lengths do not match', (): void => {
expect(
() => naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
signature,
new Uint8Array([1, 2])
)
).toThrow(/Invalid publicKey/);
});
it('throws error when publicKey lengths do not match', (): void => {
expect(
() => naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
signature,
new Uint8Array([1, 2]),
onlyJs
)
).toThrow(/Invalid publicKey/);
});
it('throws error when signature lengths do not match', (): void => {
expect(
() => naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
new Uint8Array([1, 2]),
publicKey
)
).toThrow(/Invalid signature/);
it('throws error when signature lengths do not match', (): void => {
expect(
() => naclVerify(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
new Uint8Array([1, 2]),
publicKey,
onlyJs
)
).toThrow(/Invalid signature/);
});
});
});
+10 -12
View File
@@ -15,21 +15,19 @@ describe('pbkdf2Encode', (): void => {
await waitReady();
});
[256, 1024, 2048].forEach((rounds): void => {
it(`has equivalent Wasm & JS results (${rounds} rounds)`, (): void => {
const salt = randomAsU8a();
it.each([256, 1024, 2048])('has equivalent Wasm & JS results (%p rounds)', (rounds): void => {
const salt = randomAsU8a();
expect(
u8aEq(
pbkdf2Encode(TEST_PASSWORD, salt, rounds, false).password,
pbkdf2Encode(TEST_PASSWORD, salt, rounds, true).password
)
).toBe(true);
});
expect(
u8aEq(
pbkdf2Encode(TEST_PASSWORD, salt, rounds, false).password,
pbkdf2Encode(TEST_PASSWORD, salt, rounds, true).password
)
).toBe(true);
});
[false, true].forEach((onlyJs): void => {
it(`creates known iterations (onlyJs = ${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('creates known iterations', (): void => {
expect(
u8aToHex(pbkdf2Encode(TEST_PASSWORD, KNOWN_SALT, 2048, onlyJs).password)
).toEqual(
@@ -10,8 +10,8 @@ describe('sha512AsU8a', (): void => {
await waitReady();
});
[false, true].forEach((onlyJs): void => {
it(`creates a sha-512 hash (onlyJs=${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('creates a sha-512 hash', (): void => {
expect(
sha512AsU8a(Uint8Array.from([0x61, 0x62, 0x63, 0x64]), onlyJs)
).toEqual(
@@ -11,28 +11,24 @@ describe('xxhashAsU8a', (): void => {
await waitReady();
});
[false, true].forEach((onlyJs): void => {
it(`returns a 64-bit value by default (onlyJs=${onlyJs.toString()})`, (): void => {
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it('returns a 64-bit value by default', (): void => {
expect(
xxhashAsU8a('abc', undefined, onlyJs)
).toEqual(
hexToU8a('0x990977adf52cbc44')
);
});
});
[false, true].forEach((onlyJs): void => {
it(`returns a 128-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
it('returns a 128-bit value (as specified', (): void => {
expect(
xxhashAsU8a('abc', 128, onlyJs)
).toEqual(
hexToU8a('0x990977adf52cbc440889329981caa9be')
);
});
});
[false, true].forEach((onlyJs): void => {
it(`returns a 256-bit value (as specified, onlyJs=${onlyJs.toString()})`, (): void => {
it('returns a 256-bit value (as specified)', (): void => {
expect(
xxhashAsU8a('abc', 256, onlyJs)
).toEqual(