From 075eef59561c259b7d9df9f72672419feaf4a40e Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Wed, 17 Jan 2018 15:30:03 -0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/18217 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Anna Henningsen --- lib/fs.js | 3 +- test/parallel/test-fs-fchmod.js | 67 +++++++++++++++++++++++++++++++++ test/parallel/test-fs-fchown.js | 2 +- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-fs-fchmod.js diff --git a/lib/fs.js b/lib/fs.js index 2bac855d0f3b48..7087a5f03c5f9e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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(); 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), {