Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added input validation test for fchmod #18217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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