mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
f7321dc7f9
Currently, if this test is run as the root user the following failure will occur: === release test-fs-access === Path: parallel/test-fs-access (node:46733) internal/test/binding: These APIs are for internal testing only. Do not use them. Can't clean tmpdir: /root/node/test/.tmp.522 Files blocking: [ 'read_only_file', 'read_write_file' ] /root/node/test/common/tmpdir.js:136 throw e; ^ Error: EACCES: permission denied, rmdir '/root/node/test/.tmp.522' at Object.rmdirSync (fs.js:693:3) at rmdirSync (/root/node/test/common/tmpdir.js:72:8) at rimrafSync (/root/node/test/common/tmpdir.js:41:7) at process.onexit (/root/node/test/common/tmpdir.js:121:5) at process.emit (events.js:214:15) { errno: -13, syscall: 'rmdir', code: 'EACCES', path: '/root/node/test/.tmp.522' } Command: ./node --expose-internals test/parallel/test-fs-access.js This commit adds a root user check and skips this test if running as the user root. PR-URL: https://github.com/nodejs/node/pull/29092 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
189 lines
5.6 KiB
JavaScript
189 lines
5.6 KiB
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
|
|
// This tests that fs.access and fs.accessSync works as expected
|
|
// and the errors thrown from these APIs include the desired properties
|
|
|
|
const common = require('../common');
|
|
if (!common.isWindows && process.getuid() === 0)
|
|
common.skip('as this test should not be run as `root`');
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const { internalBinding } = require('internal/test/binding');
|
|
const { UV_ENOENT } = internalBinding('uv');
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
const doesNotExist = path.join(tmpdir.path, '__this_should_not_exist');
|
|
const readOnlyFile = path.join(tmpdir.path, 'read_only_file');
|
|
const readWriteFile = path.join(tmpdir.path, 'read_write_file');
|
|
|
|
function createFileWithPerms(file, mode) {
|
|
fs.writeFileSync(file, '');
|
|
fs.chmodSync(file, mode);
|
|
}
|
|
|
|
tmpdir.refresh();
|
|
createFileWithPerms(readOnlyFile, 0o444);
|
|
createFileWithPerms(readWriteFile, 0o666);
|
|
|
|
/*
|
|
* On non-Windows supported platforms, fs.access(readOnlyFile, W_OK, ...)
|
|
* always succeeds if node runs as the super user, which is sometimes the
|
|
* case for tests running on our continuous testing platform agents.
|
|
*
|
|
* In this case, this test tries to change its process user id to a
|
|
* non-superuser user so that the test that checks for write access to a
|
|
* read-only file can be more meaningful.
|
|
*
|
|
* The change of user id is done after creating the fixtures files for the same
|
|
* reason: the test may be run as the superuser within a directory in which
|
|
* only the superuser can create files, and thus it may need superuser
|
|
* privileges to create them.
|
|
*
|
|
* There's not really any point in resetting the process' user id to 0 after
|
|
* changing it to 'nobody', since in the case that the test runs without
|
|
* superuser privilege, it is not possible to change its process user id to
|
|
* superuser.
|
|
*
|
|
* It can prevent the test from removing files created before the change of user
|
|
* id, but that's fine. In this case, it is the responsibility of the
|
|
* continuous integration platform to take care of that.
|
|
*/
|
|
let hasWriteAccessForReadonlyFile = false;
|
|
if (!common.isWindows && process.getuid() === 0) {
|
|
hasWriteAccessForReadonlyFile = true;
|
|
try {
|
|
process.setuid('nobody');
|
|
hasWriteAccessForReadonlyFile = false;
|
|
} catch {
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(typeof fs.F_OK, 'number');
|
|
assert.strictEqual(typeof fs.R_OK, 'number');
|
|
assert.strictEqual(typeof fs.W_OK, 'number');
|
|
assert.strictEqual(typeof fs.X_OK, 'number');
|
|
|
|
const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
|
|
|
|
fs.access(__filename, common.mustCall(function(...args) {
|
|
assert.deepStrictEqual(args, [null]);
|
|
}));
|
|
fs.promises.access(__filename)
|
|
.then(common.mustCall())
|
|
.catch(throwNextTick);
|
|
fs.access(__filename, fs.R_OK, common.mustCall(function(...args) {
|
|
assert.deepStrictEqual(args, [null]);
|
|
}));
|
|
fs.promises.access(__filename, fs.R_OK)
|
|
.then(common.mustCall())
|
|
.catch(throwNextTick);
|
|
fs.access(readOnlyFile, fs.F_OK | fs.R_OK, common.mustCall(function(...args) {
|
|
assert.deepStrictEqual(args, [null]);
|
|
}));
|
|
fs.promises.access(readOnlyFile, fs.F_OK | fs.R_OK)
|
|
.then(common.mustCall())
|
|
.catch(throwNextTick);
|
|
|
|
{
|
|
const expectedError = (err) => {
|
|
assert.notStrictEqual(err, null);
|
|
assert.strictEqual(err.code, 'ENOENT');
|
|
assert.strictEqual(err.path, doesNotExist);
|
|
};
|
|
fs.access(doesNotExist, common.mustCall(expectedError));
|
|
fs.promises.access(doesNotExist)
|
|
.then(common.mustNotCall(), common.mustCall(expectedError))
|
|
.catch(throwNextTick);
|
|
}
|
|
|
|
{
|
|
function expectedError(err) {
|
|
assert.strictEqual(this, undefined);
|
|
if (hasWriteAccessForReadonlyFile) {
|
|
assert.ifError(err);
|
|
} else {
|
|
assert.notStrictEqual(err, null);
|
|
assert.strictEqual(err.path, readOnlyFile);
|
|
}
|
|
}
|
|
fs.access(readOnlyFile, fs.W_OK, common.mustCall(expectedError));
|
|
fs.promises.access(readOnlyFile, fs.W_OK)
|
|
.then(common.mustNotCall(), common.mustCall(expectedError))
|
|
.catch(throwNextTick);
|
|
}
|
|
|
|
{
|
|
const expectedError = (err) => {
|
|
assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
|
|
assert.ok(err instanceof TypeError);
|
|
return true;
|
|
};
|
|
assert.throws(
|
|
() => { fs.access(100, fs.F_OK, common.mustNotCall()); },
|
|
expectedError
|
|
);
|
|
|
|
fs.promises.access(100, fs.F_OK)
|
|
.then(common.mustNotCall(), common.mustCall(expectedError))
|
|
.catch(throwNextTick);
|
|
}
|
|
|
|
common.expectsError(
|
|
() => {
|
|
fs.access(__filename, fs.F_OK);
|
|
},
|
|
{
|
|
code: 'ERR_INVALID_CALLBACK',
|
|
type: TypeError
|
|
});
|
|
|
|
common.expectsError(
|
|
() => {
|
|
fs.access(__filename, fs.F_OK, {});
|
|
},
|
|
{
|
|
code: 'ERR_INVALID_CALLBACK',
|
|
type: TypeError
|
|
});
|
|
|
|
// Regular access should not throw.
|
|
fs.accessSync(__filename);
|
|
const mode = fs.F_OK | fs.R_OK | fs.W_OK;
|
|
fs.accessSync(readWriteFile, mode);
|
|
|
|
assert.throws(
|
|
() => { fs.accessSync(doesNotExist); },
|
|
(err) => {
|
|
assert.strictEqual(err.code, 'ENOENT');
|
|
assert.strictEqual(err.path, doesNotExist);
|
|
assert.strictEqual(
|
|
err.message,
|
|
`ENOENT: no such file or directory, access '${doesNotExist}'`
|
|
);
|
|
assert.strictEqual(err.constructor, Error);
|
|
assert.strictEqual(err.syscall, 'access');
|
|
assert.strictEqual(err.errno, UV_ENOENT);
|
|
return true;
|
|
}
|
|
);
|
|
|
|
assert.throws(
|
|
() => { fs.accessSync(Buffer.from(doesNotExist)); },
|
|
(err) => {
|
|
assert.strictEqual(err.code, 'ENOENT');
|
|
assert.strictEqual(err.path, doesNotExist);
|
|
assert.strictEqual(
|
|
err.message,
|
|
`ENOENT: no such file or directory, access '${doesNotExist}'`
|
|
);
|
|
assert.strictEqual(err.constructor, Error);
|
|
assert.strictEqual(err.syscall, 'access');
|
|
assert.strictEqual(err.errno, UV_ENOENT);
|
|
return true;
|
|
}
|
|
);
|