From 63544b74278a97280da2fdf27038bc02ab658b97 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 8 Mar 2023 18:14:07 +0100 Subject: [PATCH] test: add coverage for custom loader hooks with permission model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/46977 Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: Jacob Smith Reviewed-By: Geoffrey Booth --- test/es-module/test-esm-loader-hooks.mjs | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index ce8085bbaf9d29..5c6bf16323f160 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -113,4 +113,56 @@ describe('Loader hooks', { concurrency: true }, () => { assert.strictEqual(signal, null); }); }); + + it('should work without worker permission', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-permission', + '--allow-fs-read', + '*', + '--experimental-loader', + fixtures.fileURL('empty.js'), + fixtures.path('es-modules/esm-top-level-await.mjs'), + ]); + + assert.strictEqual(stderr, ''); + assert.match(stdout, /^1\r?\n2\r?\n$/); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + }); + + it('should allow loader hooks to spawn workers when allowed by the CLI flags', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-permission', + '--allow-worker', + '--allow-fs-read', + '*', + '--experimental-loader', + `data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`, + fixtures.path('es-modules/esm-top-level-await.mjs'), + ]); + + assert.strictEqual(stderr, ''); + assert.match(stdout, /^1\r?\n2\r?\n$/); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + }); + + it('should not allow loader hooks to spawn workers if restricted by the CLI flags', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-permission', + '--allow-fs-read', + '*', + '--experimental-loader', + `data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`, + fixtures.path('es-modules/esm-top-level-await.mjs'), + ]); + + assert.match(stderr, /code: 'ERR_ACCESS_DENIED'/); + assert.strictEqual(stdout, ''); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); });