Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f980201

Browse filesBrowse files
aduh95danielleadams
authored andcommitted
test_runner: validate timeout option
PR-URL: #43843 Reviewed-By: Feng Yu <F3n67u@outlook.com>
1 parent 2266a4b commit f980201
Copy full SHA for f980201

File tree

Expand file treeCollapse file tree

3 files changed

+33
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-3
lines changed
Open diff view settings
Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ const {
3131
kEmptyObject,
3232
} = require('internal/util');
3333
const { isPromise } = require('internal/util/types');
34-
const { isUint32, validateAbortSignal } = require('internal/validators');
34+
const {
35+
isUint32,
36+
validateAbortSignal,
37+
validateNumber,
38+
} = require('internal/validators');
3539
const { setTimeout } = require('timers/promises');
40+
const { TIMEOUT_MAX } = require('internal/timers');
3641
const { cpus } = require('os');
3742
const { bigint: hrtime } = process.hrtime;
3843
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
@@ -148,7 +153,8 @@ class Test extends AsyncResource {
148153
this.concurrency = concurrency;
149154
}
150155

151-
if (isUint32(timeout)) {
156+
if (timeout != null && timeout !== Infinity) {
157+
validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX);
152158
this.timeout = timeout;
153159
}
154160

Collapse file

‎lib/internal/validators.js‎

Copy file name to clipboardExpand all lines: lib/internal/validators.js
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeJoin,
77
ArrayPrototypeMap,
88
NumberIsInteger,
9+
NumberIsNaN,
910
NumberMAX_SAFE_INTEGER,
1011
NumberMIN_SAFE_INTEGER,
1112
NumberParseInt,
@@ -115,9 +116,17 @@ function validateString(value, name) {
115116
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
116117
}
117118

118-
function validateNumber(value, name) {
119+
function validateNumber(value, name, min = undefined, max) {
119120
if (typeof value !== 'number')
120121
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
122+
123+
if ((min != null && value < min) || (max != null && value > max) ||
124+
((min != null || max != null) && NumberIsNaN(value))) {
125+
throw new ERR_OUT_OF_RANGE(
126+
name,
127+
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`,
128+
value);
129+
}
121130
}
122131

123132
const validateOneOf = hideStackFrames((value, name, oneOf) => {
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const test = require('node:test');
5+
6+
[Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => {
7+
assert.throws(() => test({ timeout }), { code: 'ERR_INVALID_ARG_TYPE' });
8+
});
9+
[-1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((timeout) => {
10+
assert.throws(() => test({ timeout }), { code: 'ERR_OUT_OF_RANGE' });
11+
});
12+
[null, undefined, Infinity, 0, 1, 1.1].forEach((timeout) => {
13+
// Valid values should not throw.
14+
test({ timeout });
15+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.