From 93de82e9f70d5b2dff39baac29ce433c9272187a Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 20 Nov 2024 07:17:35 +0000 Subject: [PATCH] test: More type tests --- test/basic.test.js | 4 +++- test/utils.js | 29 ++++++++++++++++++++++++++--- test/utils.test.js | 21 ++++++++++++--------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/test/basic.test.js b/test/basic.test.js index 0eb01d5..53ca796 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -673,7 +673,9 @@ for (const name in CURVES) { throws(() => C.getPublicKey(undefined), 'undefined'); throws(() => C.getPublicKey(''), "''"); // NOTE: passes because of disabled hex padding checks for starknet, maybe enable? - // throws(() => C.getPublicKey('1'), "'1'"); + if (name !== 'starknet') { + // throws(() => C.getPublicKey('1'), "'1'"); + } throws(() => C.getPublicKey('key'), "'key'"); throws(() => C.getPublicKey({})); throws(() => C.getPublicKey(new Uint8Array([]))); diff --git a/test/utils.js b/test/utils.js index 33af72d..300e4e9 100644 --- a/test/utils.js +++ b/test/utils.js @@ -22,6 +22,7 @@ export function json(path) { } } +// Everything except undefined, string, Uint8Array const TYPE_TEST_BASE = [ null, [1, 2, 3], @@ -33,16 +34,37 @@ const TYPE_TEST_BASE = [ new Uint32Array([1, 2, 3]), 100n, new Set([1, 2, 3]), + new Map([['aa', 'bb']]), new Uint8ClampedArray([1, 2, 3]), new Int16Array([1, 2, 3]), + new Float32Array([1]), + new BigInt64Array([1n, 2n, 3n]), new ArrayBuffer(100), new DataView(new ArrayBuffer(100)), { constructor: { name: 'Uint8Array' }, length: '1e30' }, () => {}, async () => {}, class Test {}, + Symbol.for('a'), + new Proxy(new Uint8Array(), {}), ]; -const TYPE_TEST_NOT_STR = [ + +const TYPE_TEST_OPT = [ + '', + new Uint8Array(), + new (class Test {})(), + class Test {}, + () => {}, + 0, + 0.1234, + NaN, + null, +]; + +const TYPE_TEST_NOT_BOOL = [false, true]; +const TYPE_TEST_NOT_BYTES = ['', 'test', '1', new Uint8Array([]), new Uint8Array([1, 2, 3])]; +const TYPE_TEST_NOT_HEX = [ + '0xbe', ' 1 2 3 4 5', '010203040x', 'abcdefgh', @@ -50,7 +72,8 @@ const TYPE_TEST_NOT_STR = [ 'bee', new String('1234'), ]; +const TYPE_TEST_NOT_INT = [-0.0, 0, 1]; export const TYPE_TEST = { - hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR), - bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR), + bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL), + hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_HEX), }; diff --git a/test/utils.test.js b/test/utils.test.js index 659c7d9..f8290ca 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,22 +1,25 @@ -import { throws } from 'node:assert'; +import { deepStrictEqual, throws } from 'node:assert'; import { describe, should } from 'micro-should'; import { bytesToHex, hexToBytes } from '../esm/abstract/utils.js'; import { TYPE_TEST } from './utils.js'; -// Here goes test for tests... -describe('Tests', () => { +describe('utils', () => { + const staticHexVectors = [ + { bytes: Uint8Array.from([]), hex: '' }, + { bytes: Uint8Array.from([0xbe]), hex: 'be' }, + { bytes: Uint8Array.from([0xca, 0xfe]), hex: 'cafe' }, + { bytes: Uint8Array.from(new Array(1024).fill(0x69)), hex: '69'.repeat(1024) }, + ]; should('hexToBytes', () => { + for (let v of staticHexVectors) deepStrictEqual(hexToBytes(v.hex), v.bytes); for (let v of TYPE_TEST.hex) { - throws(() => { - hexToBytes(v); - }); + throws(() => hexToBytes(v)); } }); should('bytesToHex', () => { + for (let v of staticHexVectors) deepStrictEqual(bytesToHex(v.bytes), v.hex); for (let v of TYPE_TEST.bytes) { - throws(() => { - bytesToHex(v); - }); + throws(() => bytesToHex(v)); } }); });