Skip to content

Commit 075eef5

Browse files
lucamaraschiBridgeAR
authored andcommitted
test: added input validation test for fchmod
Added a test to ensure input validation for FD and mode for fs.fchmod. Removed check for values lower than 0 for `mode` as it's already checked by `validateUint32`. PR-URL: #18217 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent a27f48d commit 075eef5

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/fs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,8 @@ fs.fchmod = function(fd, mode, callback) {
13401340
mode = modeNum(mode);
13411341
validateUint32(fd, 'fd');
13421342
validateUint32(mode, 'mode');
1343-
if (mode < 0 || mode > 0o777)
1343+
// values for mode < 0 are already checked via the validateUint32 function
1344+
if (mode > 0o777)
13441345
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
13451346

13461347
const req = new FSReqWrap();

test/parallel/test-fs-fchmod.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fs = require('fs');
4+
5+
// This test ensures that input for fchmod is valid, testing for valid
6+
// inputs for fd and mode
7+
8+
// Check input type
9+
['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => {
10+
common.expectsError(
11+
() => fs.fchmod(i),
12+
{
13+
code: 'ERR_INVALID_ARG_TYPE',
14+
type: TypeError,
15+
message: 'The "fd" argument must be of type integer'
16+
}
17+
);
18+
common.expectsError(
19+
() => fs.fchmodSync(i),
20+
{
21+
code: 'ERR_INVALID_ARG_TYPE',
22+
type: TypeError,
23+
message: 'The "fd" argument must be of type integer'
24+
}
25+
);
26+
27+
common.expectsError(
28+
() => fs.fchmod(1, i),
29+
{
30+
code: 'ERR_INVALID_ARG_TYPE',
31+
type: TypeError,
32+
message: 'The "mode" argument must be of type integer'
33+
}
34+
);
35+
common.expectsError(
36+
() => fs.fchmodSync(1, i),
37+
{
38+
code: 'ERR_INVALID_ARG_TYPE',
39+
type: TypeError,
40+
message: 'The "mode" argument must be of type integer'
41+
}
42+
);
43+
});
44+
45+
// Check for mode values range
46+
const modeUpperBoundaryValue = 0o777;
47+
fs.fchmod(1, modeUpperBoundaryValue);
48+
fs.fchmodSync(1, modeUpperBoundaryValue);
49+
50+
// umask of 0o777 is equal to 775
51+
const modeOutsideUpperBoundValue = 776;
52+
common.expectsError(
53+
() => fs.fchmod(1, modeOutsideUpperBoundValue),
54+
{
55+
code: 'ERR_OUT_OF_RANGE',
56+
type: RangeError,
57+
message: 'The value of "mode" is out of range.'
58+
}
59+
);
60+
common.expectsError(
61+
() => fs.fchmodSync(1, modeOutsideUpperBoundValue),
62+
{
63+
code: 'ERR_OUT_OF_RANGE',
64+
type: RangeError,
65+
message: 'The value of "mode" is out of range.'
66+
}
67+
);

test/parallel/test-fs-fchown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const fs = require('fs');
55

6-
['', false, null, undefined, {}, []].forEach((i) => {
6+
['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => {
77
common.expectsError(
88
() => fs.fchown(i),
99
{

0 commit comments

Comments
 (0)