1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const fs = require('fs'); 5 6// This test ensures that input for fchmod is valid, testing for valid 7// inputs for fd and mode 8 9// Check input type 10[false, null, undefined, {}, [], ''].forEach((input) => { 11 const errObj = { 12 code: 'ERR_INVALID_ARG_TYPE', 13 name: 'TypeError', 14 message: 'The "fd" argument must be of type number.' + 15 common.invalidArgTypeHelper(input) 16 }; 17 assert.throws(() => fs.fchmod(input), errObj); 18 assert.throws(() => fs.fchmodSync(input), errObj); 19}); 20 21 22[false, null, {}, []].forEach((input) => { 23 const errObj = { 24 code: 'ERR_INVALID_ARG_TYPE', 25 }; 26 assert.throws(() => fs.fchmod(1, input), errObj); 27 assert.throws(() => fs.fchmodSync(1, input), errObj); 28}); 29 30assert.throws(() => fs.fchmod(1, '123x'), { 31 code: 'ERR_INVALID_ARG_VALUE' 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