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:
parent
a27f48d619
commit
075eef5956
@ -1340,7 +1340,8 @@ fs.fchmod = function(fd, mode, callback) {
|
|||||||
mode = modeNum(mode);
|
mode = modeNum(mode);
|
||||||
validateUint32(fd, 'fd');
|
validateUint32(fd, 'fd');
|
||||||
validateUint32(mode, 'mode');
|
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');
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
|
67
test/parallel/test-fs-fchmod.js
Normal file
67
test/parallel/test-fs-fchmod.js
Normal 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.'
|
||||||
|
}
|
||||||
|
);
|
@ -3,7 +3,7 @@
|
|||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
['', false, null, undefined, {}, []].forEach((i) => {
|
['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => {
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
() => fs.fchown(i),
|
() => fs.fchown(i),
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user