diff --git a/test/common/README.md b/test/common/README.md index 01064a7a8b73f1..24468bdfd77b0e 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -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>] diff --git a/test/common/index.js b/test/common/index.js index 54b146814a453a..10fd637af997e1 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -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');