diff --git a/lib/fs.js b/lib/fs.js index db262f5da8fc22..58a674a7724538 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1294,7 +1294,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(); diff --git a/test/parallel/test-fs-fchmod.js b/test/parallel/test-fs-fchmod.js new file mode 100644 index 00000000000000..42a11e8a96c02d --- /dev/null +++ b/test/parallel/test-fs-fchmod.js @@ -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.' + } +); diff --git a/test/parallel/test-fs-fchown.js b/test/parallel/test-fs-fchown.js index 8812fbbe3f4c37..a7e6bf6cbca571 100644 --- a/test/parallel/test-fs-fchown.js +++ b/test/parallel/test-fs-fchown.js @@ -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), {