Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 28d0cbb

Browse files
author
Julien Gilli
committed
test: fix test-fs-access.js
On non-windows supported platforms, fs.access(readOnlyFile, W_OK, ...) is expected to fail, but always succeeds if node runs as the super user, which is often the case for tests running on our continuous integration platform. This change makes the test try to change its process user id to nobody on non-windows platforms so that the above mentioned test can pass and still perform the actual desired test. If changing the process user id to a nobody is not possible, then the test checks that fs.access(readOnlyFile, W_OK, ...) actually succeeds. Fixes #9033. Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy J Fontaine <[email protected]>
1 parent 70d04e7 commit 28d0cbb

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

test/simple/test-fs-access.js

+47-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,57 @@ var fs = require('fs');
2525
var path = require('path');
2626
var doesNotExist = __filename + '__this_should_not_exist';
2727
var readOnlyFile = path.join(common.tmpDir, 'read_only_file');
28+
var readWriteFile = path.join(common.tmpDir, 'read_write_file');
2829

29-
var removeFile = function(file) {
30+
function removeFile(file) {
3031
try {
3132
fs.unlinkSync(file);
3233
} catch (err) {
3334
// Ignore error
3435
}
3536
};
3637

37-
var createReadOnlyFile = function(file) {
38+
function createFileWithPerms(file, mode) {
3839
removeFile(file);
3940
fs.writeFileSync(file, '');
40-
fs.chmodSync(file, 0444);
41+
fs.chmodSync(file, mode);
4142
};
4243

43-
createReadOnlyFile(readOnlyFile);
44+
createFileWithPerms(readOnlyFile, 0444);
45+
createFileWithPerms(readWriteFile, 0666);
46+
47+
/*
48+
* On non-Windows supported platforms, fs.access(readOnlyFile, W_OK, ...)
49+
* always succeeds if node runs as the super user, which is sometimes the
50+
* case for tests running on our continuous testing platform agents.
51+
*
52+
* In this case, this test tries to change its process user id to a
53+
* non-superuser user so that the test that checks for write access to a
54+
* read-only file can be more meaningful.
55+
*
56+
* The change of user id is done after creating the fixtures files for the same
57+
* reason: the test may be run as the superuser within a directory in which
58+
* only the superuser can create files, and thus it may need superuser
59+
* priviledges to create them.
60+
*
61+
* There's not really any point in resetting the process' user id to 0 after
62+
* changing it to 'nobody', since in the case that the test runs without
63+
* superuser priviledge, it is not possible to change its process user id to
64+
* superuser.
65+
*
66+
* It can prevent the test from removing files created before the change of user
67+
* id, but that's fine. In this case, it is the responsability of the
68+
* continuous integration platform to take care of that.
69+
*/
70+
var hasWiteAccessForReadonlyFile = false;
71+
if (process.platform !== 'win32' && process.getuid() === 0) {
72+
hasWiteAccessForReadonlyFile = true;
73+
try {
74+
process.setuid('nobody');
75+
hasWiteAccessForReadonlyFile = false;
76+
} catch (err) {
77+
}
78+
}
4479

4580
assert(typeof fs.F_OK === 'number');
4681
assert(typeof fs.R_OK === 'number');
@@ -66,8 +101,12 @@ fs.access(readOnlyFile, fs.F_OK | fs.R_OK, function(err) {
66101
});
67102

68103
fs.access(readOnlyFile, fs.W_OK, function(err) {
69-
assert.notEqual(err, null, 'error should exist');
70-
assert.strictEqual(err.path, readOnlyFile);
104+
if (hasWiteAccessForReadonlyFile) {
105+
assert.equal(err, null, 'error should not exist');
106+
} else {
107+
assert.notEqual(err, null, 'error should exist');
108+
assert.strictEqual(err.path, readOnlyFile);
109+
}
71110
});
72111

73112
assert.throws(function() {
@@ -85,7 +124,7 @@ assert.doesNotThrow(function() {
85124
assert.doesNotThrow(function() {
86125
var mode = fs.F_OK | fs.R_OK | fs.W_OK;
87126

88-
fs.accessSync(__filename, mode);
127+
fs.accessSync(readWriteFile, mode);
89128
});
90129

91130
assert.throws(function() {
@@ -96,4 +135,5 @@ assert.throws(function() {
96135

97136
process.on('exit', function() {
98137
removeFile(readOnlyFile);
138+
removeFile(readWriteFile);
99139
});

0 commit comments

Comments
 (0)