• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2// Copyright Joyent, Inc. and other Node contributors.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to permit
9// persons to whom the Software is furnished to do so, subject to the
10// following conditions:
11//
12// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21// USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23'use strict';
24require('../common');
25const assert = require('assert');
26const fs = require('fs');
27const { internalBinding } = require('internal/test/binding');
28const { UV_EBADF } = internalBinding('uv');
29
30// Ensure that (read|write|append)FileSync() closes the file descriptor
31fs.openSync = function() {
32  return 42;
33};
34fs.closeSync = function(fd) {
35  assert.strictEqual(fd, 42);
36  close_called++;
37};
38fs.readSync = function() {
39  throw new Error('BAM');
40};
41fs.writeSync = function() {
42  throw new Error('BAM');
43};
44
45internalBinding('fs').fstat = function(fd, bigint, _, ctx) {
46  ctx.errno = UV_EBADF;
47  ctx.syscall = 'fstat';
48};
49
50let close_called = 0;
51ensureThrows(function() {
52  fs.readFileSync('dummy');
53}, 'EBADF: bad file descriptor, fstat');
54ensureThrows(function() {
55  fs.writeFileSync('dummy', 'xxx');
56}, 'BAM');
57ensureThrows(function() {
58  fs.appendFileSync('dummy', 'xxx');
59}, 'BAM');
60
61function ensureThrows(cb, message) {
62  let got_exception = false;
63
64  close_called = 0;
65  try {
66    cb();
67  } catch (e) {
68    assert.strictEqual(e.message, message);
69    got_exception = true;
70  }
71
72  assert.strictEqual(close_called, 1);
73  assert.strictEqual(got_exception, true);
74}
75