Skip to content

Commit

Permalink
test: introduce common.runWithInvalidFD()
Browse files Browse the repository at this point in the history
This provides a more reliable way to get a fd that can be used
to tirgger EBADF.

PR-URL: #18864
Fixes: #18820
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
joyeecheung committed Feb 22, 2018
1 parent 13637d2 commit acf2fd3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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` [&lt;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
* [&lt;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 @@ -816,6 +816,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

0 comments on commit acf2fd3

Please sign in to comment.