Skip to content

Commit

Permalink
test: added input validation test for fchmod
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
lucamaraschi authored and BridgeAR committed Feb 6, 2018
1 parent a27f48d commit 075eef5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,8 @@ fs.fchmod = function(fd, mode, callback) {
mode = modeNum(mode);
validateUint32(fd, 'fd');
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
// values for mode < 0 are already checked via the validateUint32 function
if (mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');

const req = new FSReqWrap();
Expand Down
67 changes: 67 additions & 0 deletions test/parallel/test-fs-fchmod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';
const common = require('../common');
const fs = require('fs');

// This test ensures that input for fchmod is valid, testing for valid
// inputs for fd and mode

// Check input type
['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => {
common.expectsError(
() => fs.fchmod(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
common.expectsError(
() => fs.fchmodSync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);

common.expectsError(
() => fs.fchmod(1, i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "mode" argument must be of type integer'
}
);
common.expectsError(
() => fs.fchmodSync(1, i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "mode" argument must be of type integer'
}
);
});

// Check for mode values range
const modeUpperBoundaryValue = 0o777;
fs.fchmod(1, modeUpperBoundaryValue);
fs.fchmodSync(1, modeUpperBoundaryValue);

// umask of 0o777 is equal to 775
const modeOutsideUpperBoundValue = 776;
common.expectsError(
() => fs.fchmod(1, modeOutsideUpperBoundValue),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "mode" is out of range.'
}
);
common.expectsError(
() => fs.fchmodSync(1, modeOutsideUpperBoundValue),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "mode" is out of range.'
}
);
2 changes: 1 addition & 1 deletion test/parallel/test-fs-fchown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const common = require('../common');
const fs = require('fs');

['', false, null, undefined, {}, []].forEach((i) => {
['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => {
common.expectsError(
() => fs.fchown(i),
{
Expand Down

0 comments on commit 075eef5

Please sign in to comment.