Skip to content
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
8 changes: 8 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ consisting of all `ArrayBufferView` and an `ArrayBuffer`.

Returns the file name and line number for the provided Function.

### runWithInvalidFD(func)
* `func` [<Function>]

Runs `func` with an invalid file descriptor that is an unsigned integer and
can be used to trigger `EBADF` as the first argument. If no such file
descriptor could be generated, a skip message will be printed and the `func`
will not be run.

### globalCheck
* [<Boolean>]

Expand Down
13 changes: 13 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,19 @@ function restoreWritable(name) {
delete process[name].writeTimes;
}

exports.runWithInvalidFD = function(func) {
let fd = 1 << 30;
// Get first known bad file descriptor. 1 << 30 is usually unlikely to
// be an valid one.
try {
while (fs.fstatSync(fd--) && fd > 0);
} catch (e) {
return func(fd);
}

exports.printSkipMessage('Could not generate an invalid fd');
};

exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
exports.restoreStdout = restoreWritable.bind(null, 'stdout');
Expand Down
40 changes: 0 additions & 40 deletions test/parallel/test-fs-close-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// include the desired properties

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const uv = process.binding('uv');

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
Expand All @@ -26,41 +24,3 @@ const uv = process.binding('uv');
}
);
});

{
assert.throws(
() => {
const fd = fs.openSync(__filename, 'r');
fs.closeSync(fd);
fs.closeSync(fd);
},
(err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
return true;
}
);
}

{
const fd = fs.openSync(__filename, 'r');
fs.close(fd, common.mustCall((err) => {
assert.ifError(err);
fs.close(fd, common.mustCall((err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
}));
}));
}
83 changes: 50 additions & 33 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fstat(fd, common.mustCall(validateError));

assert.throws(
() => fs.fstatSync(fd),
validateError
);
common.runWithInvalidFD((fd) => {
fs.fstat(fd, common.mustCall(validateError));

assert.throws(
() => fs.fstatSync(fd),
validateError
);
});
}

// realpath
Expand Down Expand Up @@ -414,6 +413,27 @@ function re(literals, ...values) {
);
}


// close
{
const validateError = (err) => {
assert.strictEqual(err.message, 'EBADF: bad file descriptor, close');
assert.strictEqual(err.errno, uv.UV_EBADF);
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(err.syscall, 'close');
return true;
};

common.runWithInvalidFD((fd) => {
fs.close(fd, common.mustCall(validateError));

assert.throws(
() => fs.closeSync(fd),
validateError
);
});
}

// readFile
{
const validateError = (err) => {
Expand Down Expand Up @@ -472,15 +492,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);
common.runWithInvalidFD((fd) => {
fs.ftruncate(fd, 4, common.mustCall(validateError));

fs.ftruncate(fd, 4, common.mustCall(validateError));

assert.throws(
() => fs.ftruncateSync(fd, 4),
validateError
);
assert.throws(
() => fs.ftruncateSync(fd, 4),
validateError
);
});
}

// fdatasync
Expand All @@ -493,15 +512,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fdatasync(fd, common.mustCall(validateError));
common.runWithInvalidFD((fd) => {
fs.fdatasync(fd, common.mustCall(validateError));

assert.throws(
() => fs.fdatasyncSync(fd),
validateError
);
assert.throws(
() => fs.fdatasyncSync(fd),
validateError
);
});
}

// fsync
Expand All @@ -514,13 +532,12 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fsync(fd, common.mustCall(validateError));
common.runWithInvalidFD((fd) => {
fs.fsync(fd, common.mustCall(validateError));

assert.throws(
() => fs.fsyncSync(fd),
validateError
);
assert.throws(
() => fs.fsyncSync(fd),
validateError
);
});
}
19 changes: 6 additions & 13 deletions test/parallel/test-ttywrap-invalid-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Flags: --expose-internals

const common = require('../common');
const fs = require('fs');
const tty = require('tty');
const { SystemError } = require('internal/errors');

Expand All @@ -22,12 +21,9 @@ common.expectsError(

common.expectsError(
() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.WriteStream(fd);
common.runWithInvalidFD((fd) => {
new tty.WriteStream(fd);
});
}, {
code: 'ERR_SYSTEM_ERROR',
type: SystemError,
Expand All @@ -37,12 +33,9 @@ common.expectsError(

common.expectsError(
() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.ReadStream(fd);
common.runWithInvalidFD((fd) => {
new tty.ReadStream(fd);
});
}, {
code: 'ERR_SYSTEM_ERROR',
type: SystemError,
Expand Down