Skip to content

Commit

Permalink
Hack nodejs module loader for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 13, 2025
1 parent f1f0653 commit 5354eec
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [14, 16, 18, 20, 22]
node: [18, 20, 22]
steps:
- uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 # v4
- name: Use Node.js ${{ matrix.node }}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"build:clean": "rm *.{js,d.ts,js.map,d.ts.map} esm/*.{js,d.ts,js.map,d.ts.map} 2> /dev/null",
"lint": "prettier --check 'src/**/*.{js,ts}' 'test/**/*.{js,ts,mjs}'",
"format": "prettier --write 'src/**/*.{js,ts}' 'test/**/*.{js,ts,mjs}'",
"test": "node test/index.js",
"test:coverage": "c8 node test/index.js"
"test": "node --import ./test/esm-register.js test/index.js",
"test:coverage": "c8 node --import ./test/esm-register.js test/index.js"
},
"author": "Paul Miller (https://paulmillr.com)",
"homepage": "https://paulmillr.com/noble/",
Expand Down
7 changes: 3 additions & 4 deletions test/aes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { should, describe } from 'micro-should';
import { bytesToHex, concatBytes, hexToBytes } from '../esm/utils.js';
import { ecb, cbc, ctr, siv, gcm, aeskw, aeskwp } from '../esm/aes.js';
import { json } from './utils.js';

// TODO: enable back name: 'GCM', groups: aes_gcm_test.testGroups
import * as web from '../esm/webcrypto.js';

// https://datatracker.ietf.org/doc/html/rfc8452#appendix-C
const NIST_VECTORS = json('./vectors/nist_800_38a.json');
Expand Down Expand Up @@ -104,8 +103,8 @@ describe('AES', () => {
describe('Wycheproof', () => {
const cases = [
{ name: 'GCM-SIV', groups: aes_gcm_siv_test.testGroups, cipher: 'siv' },
// { name: 'GCM', groups: aes_gcm_test.testGroups, cipher: 'gcm', webcipher: web.gcm },
// { name: 'CBC', groups: aes_cbc_test.testGroups, cipher: 'cbc', webcipher: web.cbc }, // PCKS5 is enabled by default
{ name: 'GCM', groups: aes_gcm_test.testGroups, cipher: 'gcm', webcipher: web.gcm },
{ name: 'CBC', groups: aes_cbc_test.testGroups, cipher: 'cbc', webcipher: web.cbc }, // PCKS5 is enabled by default
];
for (const c of cases) {
for (const g of c.groups) {
Expand Down
20 changes: 7 additions & 13 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { deepStrictEqual, throws } from 'node:assert';
import { should, describe } from 'micro-should';
// import { managedNonce } from '../esm/webcrypto.js';
import { managedNonce, randomBytes } from '../esm/webcrypto.js';
import { siv, gcm, ctr, ecb, cbc, cfb, aeskw, aeskwp } from '../esm/aes.js';
import { xsalsa20poly1305 } from '../esm/salsa.js';
import { chacha20poly1305, xchacha20poly1305 } from '../esm/chacha.js';
import { unalign, TYPE_TEST } from './utils.js';
import * as micro from '../esm/_micro.js';

// TODO: enable back managedNonce and randomBytes
function randomBytes(len) {
return new Uint8Array(len).fill(len % 251);
}

const CIPHERS = {
xsalsa20poly1305: { fn: xsalsa20poly1305, keyLen: 32, withNonce: true },
chacha20poly1305: { fn: chacha20poly1305, keyLen: 32, withNonce: true, withDST: true },
Expand Down Expand Up @@ -39,14 +34,13 @@ for (const keyLen of [16, 24, 32]) {
for (const k in CIPHERS) {
const opts = CIPHERS[k];
if (!opts.withNonce) continue;
// CIPHERS[`${k}_managedNonce`] = { ...opts, fn: managedNonce(opts.fn), withNonce: false };
CIPHERS[`${k}_managedNonce`] = { ...opts, fn: managedNonce(opts.fn), withNonce: false };
}
// TODO
// CIPHERS.managedCbcNoPadding = {
// fn: managedNonce(cbc),
// args: [{ disablePadding: true }],
// blockSize: 16,
// };
CIPHERS.managedCbcNoPadding = {
fn: managedNonce(cbc),
args: [{ disablePadding: true }],
blockSize: 16,
};

const checkBlockSize = (opts, len) => {
if (opts.minLength && len < opts.minLength) return false;
Expand Down
5 changes: 5 additions & 0 deletions test/esm-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { pathToFileURL } from 'node:url';
export function resolve(url, context, nextResolve) {
if (url === '@noble/ciphers/crypto') url = pathToFileURL('./esm/crypto.js').toString();
return nextResolve(url, context);
}
3 changes: 3 additions & 0 deletions test/esm-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { register } from 'node:module';
import { pathToFileURL } from 'node:url';
register('./test/esm-loader.js', pathToFileURL('./'));
5 changes: 5 additions & 0 deletions test/import_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"@noble/ciphers/crypto": "../src/crypto.ts"
}
}

0 comments on commit 5354eec

Please sign in to comment.