1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const fs = require('fs'); 26 27let caughtException = false; 28 29try { 30 // Should throw ENOENT, not EBADF 31 // see https://github.com/joyent/node/pull/1228 32 fs.openSync('/8hvftyuncxrt/path/to/file/that/does/not/exist', 'r'); 33} catch (e) { 34 assert.strictEqual(e.code, 'ENOENT'); 35 caughtException = true; 36} 37assert.strictEqual(caughtException, true); 38 39fs.openSync(__filename); 40 41fs.open(__filename, common.mustCall((err) => { 42 assert.ifError(err); 43})); 44 45fs.open(__filename, 'r', common.mustCall((err) => { 46 assert.ifError(err); 47})); 48 49fs.open(__filename, 'rs', common.mustCall((err) => { 50 assert.ifError(err); 51})); 52 53fs.open(__filename, 'r', 0, common.mustCall((err) => { 54 assert.ifError(err); 55})); 56 57fs.open(__filename, 'r', null, common.mustCall((err) => { 58 assert.ifError(err); 59})); 60 61async function promise() { 62 await fs.promises.open(__filename); 63 await fs.promises.open(__filename, 'r'); 64} 65 66promise().then(common.mustCall()).catch(common.mustNotCall()); 67 68assert.throws( 69 () => fs.open(__filename, 'r', 'boom', common.mustNotCall()), 70 { 71 code: 'ERR_INVALID_ARG_VALUE', 72 name: 'TypeError' 73 } 74); 75 76for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) { 77 assert.throws( 78 () => fs.open(__filename, ...extra), 79 { 80 code: 'ERR_INVALID_CALLBACK', 81 name: 'TypeError' 82 } 83 ); 84} 85 86[false, 1, [], {}, null, undefined].forEach((i) => { 87 assert.throws( 88 () => fs.open(i, 'r', common.mustNotCall()), 89 { 90 code: 'ERR_INVALID_ARG_TYPE', 91 name: 'TypeError' 92 } 93 ); 94 assert.throws( 95 () => fs.openSync(i, 'r', common.mustNotCall()), 96 { 97 code: 'ERR_INVALID_ARG_TYPE', 98 name: 'TypeError' 99 } 100 ); 101 assert.rejects( 102 fs.promises.open(i, 'r'), 103 { 104 code: 'ERR_INVALID_ARG_TYPE', 105 name: 'TypeError' 106 } 107 ); 108}); 109 110// Check invalid modes. 111[false, [], {}].forEach((mode) => { 112 assert.throws( 113 () => fs.open(__filename, 'r', mode, common.mustNotCall()), 114 { 115 message: /'mode' must be a 32-bit/, 116 code: 'ERR_INVALID_ARG_VALUE' 117 } 118 ); 119 assert.throws( 120 () => fs.openSync(__filename, 'r', mode, common.mustNotCall()), 121 { 122 message: /'mode' must be a 32-bit/, 123 code: 'ERR_INVALID_ARG_VALUE' 124 } 125 ); 126 assert.rejects( 127 fs.promises.open(__filename, 'r', mode), 128 { 129 message: /'mode' must be a 32-bit/, 130 code: 'ERR_INVALID_ARG_VALUE' 131 } 132 ); 133}); 134