1'use strict';
2const common = require('../common');
3
4common.skipIfInspectorDisabled();
5common.skipIfWorker();
6
7const assert = require('assert');
8const kMinPort = 1024;
9const kMaxPort = 65535;
10
11function check(value, expected) {
12  process.debugPort = value;
13  assert.strictEqual(process.debugPort, expected);
14}
15
16// Expected usage with numbers.
17check(0, 0);
18check(kMinPort, kMinPort);
19check(kMinPort + 1, kMinPort + 1);
20check(kMaxPort - 1, kMaxPort - 1);
21check(kMaxPort, kMaxPort);
22
23// Numeric strings coerce.
24check('0', 0);
25check(`${kMinPort}`, kMinPort);
26check(`${kMinPort + 1}`, kMinPort + 1);
27check(`${kMaxPort - 1}`, kMaxPort - 1);
28check(`${kMaxPort}`, kMaxPort);
29
30// Most other values are coerced to 0.
31check('', 0);
32check(false, 0);
33check(NaN, 0);
34check(Infinity, 0);
35check(-Infinity, 0);
36check(function() {}, 0);
37check({}, 0);
38check([], 0);
39
40// Symbols do not coerce.
41assert.throws(() => {
42  process.debugPort = Symbol();
43}, /^TypeError: Cannot convert a Symbol value to a number$/);
44
45// Verify port bounds checking.
46[
47  true,
48  -1,
49  1,
50  kMinPort - 1,
51  kMaxPort + 1,
52  '-1',
53  '1',
54  `${kMinPort - 1}`,
55  `${kMaxPort + 1}`,
56].forEach((value) => {
57  assert.throws(() => {
58    process.debugPort = value;
59  }, /^RangeError: process\.debugPort must be 0 or in range 1024 to 65535$/);
60});
61