1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const util = require('util'); 5const fs = require('fs'); 6 7// This test ensures that input for fchmod is valid, testing for valid 8// inputs for fd and mode 9 10// Check input type 11[false, null, undefined, {}, [], ''].forEach((input) => { 12 const errObj = { 13 code: 'ERR_INVALID_ARG_TYPE', 14 name: 'TypeError', 15 message: 'The "fd" argument must be of type number.' + 16 common.invalidArgTypeHelper(input) 17 }; 18 assert.throws(() => fs.fchmod(input), errObj); 19 assert.throws(() => fs.fchmodSync(input), errObj); 20}); 21 22 23[false, null, undefined, {}, [], '', '123x'].forEach((input) => { 24 const errObj = { 25 code: 'ERR_INVALID_ARG_VALUE', 26 name: 'TypeError', 27 message: 'The argument \'mode\' must be a 32-bit unsigned integer or an ' + 28 `octal string. Received ${util.inspect(input)}` 29 }; 30 assert.throws(() => fs.fchmod(1, input), errObj); 31 assert.throws(() => fs.fchmodSync(1, input), errObj); 32}); 33 34[-1, 2 ** 32].forEach((input) => { 35 const errObj = { 36 code: 'ERR_OUT_OF_RANGE', 37 name: 'RangeError', 38 message: 'The value of "fd" is out of range. It must be >= 0 && <= ' + 39 `2147483647. Received ${input}` 40 }; 41 assert.throws(() => fs.fchmod(input), errObj); 42 assert.throws(() => fs.fchmodSync(input), errObj); 43}); 44 45[-1, 2 ** 32].forEach((input) => { 46 const errObj = { 47 code: 'ERR_OUT_OF_RANGE', 48 name: 'RangeError', 49 message: 'The value of "mode" is out of range. It must be >= 0 && <= ' + 50 `4294967295. Received ${input}` 51 }; 52 53 assert.throws(() => fs.fchmod(1, input), errObj); 54 assert.throws(() => fs.fchmodSync(1, input), errObj); 55}); 56 57[NaN, Infinity].forEach((input) => { 58 const errObj = { 59 code: 'ERR_OUT_OF_RANGE', 60 name: 'RangeError', 61 message: 'The value of "fd" is out of range. It must be an integer. ' + 62 `Received ${input}` 63 }; 64 assert.throws(() => fs.fchmod(input), errObj); 65 assert.throws(() => fs.fchmodSync(input), errObj); 66 errObj.message = errObj.message.replace('fd', 'mode'); 67 assert.throws(() => fs.fchmod(1, input), errObj); 68 assert.throws(() => fs.fchmodSync(1, input), errObj); 69}); 70 71[1.5].forEach((input) => { 72 const errObj = { 73 code: 'ERR_OUT_OF_RANGE', 74 name: 'RangeError', 75 message: 'The value of "fd" is out of range. It must be an integer. ' + 76 `Received ${input}` 77 }; 78 assert.throws(() => fs.fchmod(input), errObj); 79 assert.throws(() => fs.fchmodSync(input), errObj); 80 errObj.message = errObj.message.replace('fd', 'mode'); 81 assert.throws(() => fs.fchmod(1, input), errObj); 82 assert.throws(() => fs.fchmodSync(1, input), errObj); 83}); 84