0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/parallel/test-compile-cache-permission-disallowed.js
Joyee Cheung b246f22554
src: update compile cache storage structure
This refactors the compile cache handler in preparation for the
JS API, and updates the compile cache storage structure into:

- $NODE_COMPILE_CACHE_DIR
  - $NODE_VERION-$ARCH-$CACHE_DATA_VERSION_TAG-$UID
    - $FILENAME_AND_MODULE_TYPE_HASH.cache

This also adds a magic number to the beginning of the cache
files for verification, and returns the status, compile
cache directory and/or error message of enabling the
compile cache in a structure, which can be converted as
JS counterparts by the upcoming JS API.

PR-URL: https://github.com/nodejs/node/pull/54291
Refs: https://github.com/nodejs/node/issues/53639
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-08-19 11:54:36 +00:00

101 lines
2.8 KiB
JavaScript

'use strict';
// This tests NODE_COMPILE_CACHE works in existing directory.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const fs = require('fs');
function testDisallowed(dummyDir, cacheDirInPermission, cacheDirInEnv) {
console.log(dummyDir, cacheDirInPermission, cacheDirInEnv); // Logging for debugging.
tmpdir.refresh();
const script = tmpdir.resolve(dummyDir, 'empty.js');
fs.mkdirSync(tmpdir.resolve(dummyDir));
fs.copyFileSync(fixtures.path('empty.js'), script);
// If the directory doesn't exist, permission will just be disallowed.
if (cacheDirInPermission !== '*') {
fs.mkdirSync(tmpdir.resolve(cacheDirInPermission));
}
spawnSyncAndAssert(
process.execPath,
[
'--experimental-permission',
`--allow-fs-read=${dummyDir}`, // No read or write permission for cache dir.
`--allow-fs-write=${dummyDir}`,
script,
],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: `${cacheDirInEnv}`
},
cwd: tmpdir.path
},
{
stderr(output) {
assert.match(output, /Skipping compile cache because write permission for .* is not granted/);
return true;
}
});
spawnSyncAndAssert(
process.execPath,
[
'--experimental-permission',
`--allow-fs-read=${dummyDir}`,
`--allow-fs-read=${cacheDirInPermission}`, // Read-only
`--allow-fs-write=${dummyDir}`,
script,
],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: `${cacheDirInEnv}`
},
cwd: tmpdir.path
},
{
stderr(output) {
assert.match(output, /Skipping compile cache because write permission for .* is not granted/);
return true;
}
});
spawnSyncAndAssert(
process.execPath,
[
'--experimental-permission',
`--allow-fs-read=${dummyDir}`,
`--allow-fs-write=${cacheDirInPermission}`, // Write-only
script,
],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: `${cacheDirInEnv}`
},
cwd: tmpdir.path
},
{
stderr(output) {
assert.match(output, /Skipping compile cache because read permission for .* is not granted/);
return true;
}
});
}
{
testDisallowed(tmpdir.resolve('dummy'), tmpdir.resolve('.compile_cache') + '/', '.compile_cache');
testDisallowed(tmpdir.resolve('dummy'), tmpdir.resolve('.compile_cache/') + '/', tmpdir.resolve('.compile_cache'));
testDisallowed(tmpdir.resolve('dummy'), '*', '.compile_cache');
testDisallowed(tmpdir.resolve('dummy'), '*', tmpdir.resolve('.compile_cache'));
}