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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Luca Maraschi 2018-01-17 15:30:03 -08:00 committed by Ruben Bridgewater
parent a27f48d619
commit 075eef5956
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 70 additions and 2 deletions

View File

@ -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();

View File

@ -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.'
}
);

View File

@ -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),
{