|
| 1 | +// Flags: --experimental-permission --allow-fs-read=* --allow-child-process |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +common.skipIfWorker(); |
| 6 | +const fixtures = require('../common/fixtures'); |
| 7 | + |
| 8 | +const assert = require('node:assert'); |
| 9 | +const { spawnSync } = require('node:child_process'); |
| 10 | + |
| 11 | +{ |
| 12 | + const mainModule = fixtures.path('permission', 'main-module.js'); |
| 13 | + const requiredModule = fixtures.path('permission', 'required-module.js'); |
| 14 | + const { status, stdout, stderr } = spawnSync( |
| 15 | + process.execPath, |
| 16 | + [ |
| 17 | + '--experimental-permission', |
| 18 | + '--allow-fs-read', mainModule, |
| 19 | + '--allow-fs-read', requiredModule, |
| 20 | + mainModule, |
| 21 | + ] |
| 22 | + ); |
| 23 | + |
| 24 | + assert.strictEqual(status, 0, stderr.toString()); |
| 25 | + assert.strictEqual(stdout.toString(), 'ok\n'); |
| 26 | +} |
| 27 | + |
| 28 | +{ |
| 29 | + // When required module is not passed as allowed path |
| 30 | + const mainModule = fixtures.path('permission', 'main-module.js'); |
| 31 | + const { status, stderr } = spawnSync( |
| 32 | + process.execPath, |
| 33 | + [ |
| 34 | + '--experimental-permission', |
| 35 | + '--allow-fs-read', mainModule, |
| 36 | + mainModule, |
| 37 | + ] |
| 38 | + ); |
| 39 | + |
| 40 | + assert.strictEqual(status, 1, stderr.toString()); |
| 41 | + assert.match(stderr.toString(), /Error: Access to this API has been restricted/); |
| 42 | +} |
| 43 | + |
| 44 | +{ |
| 45 | + // ESM loader test |
| 46 | + const mainModule = fixtures.path('permission', 'main-module.mjs'); |
| 47 | + const requiredModule = fixtures.path('permission', 'required-module.mjs'); |
| 48 | + const { status, stdout, stderr } = spawnSync( |
| 49 | + process.execPath, |
| 50 | + [ |
| 51 | + '--experimental-permission', |
| 52 | + '--allow-fs-read', mainModule, |
| 53 | + '--allow-fs-read', requiredModule, |
| 54 | + mainModule, |
| 55 | + ] |
| 56 | + ); |
| 57 | + |
| 58 | + assert.strictEqual(status, 0, stderr.toString()); |
| 59 | + assert.strictEqual(stdout.toString(), 'ok\n'); |
| 60 | +} |
| 61 | + |
| 62 | +{ |
| 63 | + // When required module is not passed as allowed path (ESM) |
| 64 | + const mainModule = fixtures.path('permission', 'main-module.mjs'); |
| 65 | + const { status, stderr } = spawnSync( |
| 66 | + process.execPath, |
| 67 | + [ |
| 68 | + '--experimental-permission', |
| 69 | + '--allow-fs-read', mainModule, |
| 70 | + mainModule, |
| 71 | + ] |
| 72 | + ); |
| 73 | + |
| 74 | + assert.strictEqual(status, 1, stderr.toString()); |
| 75 | + assert.match(stderr.toString(), /Error: Access to this API has been restricted/); |
| 76 | +} |
0 commit comments