From acf2fd39f7461b36d895582e7b7d7a8b3be03eff Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 19 Feb 2018 17:36:59 +0800 Subject: [PATCH] test: introduce common.runWithInvalidFD() This provides a more reliable way to get a fd that can be used to tirgger EBADF. PR-URL: https://github.com/nodejs/node/pull/18864 Fixes: https://github.com/nodejs/node/issues/18820 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/common/README.md | 8 ++++++++ test/common/index.js | 13 +++++++++++++ 2 files changed, 21 insertions(+) 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');