Skip to content

Commit 1fb0e70

Browse files
authored
Fix portNumbers argument validation and number generation (#64)
1 parent 6afe540 commit 1fb0e70

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ export function portNumbers(from, to) {
160160
throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
161161
}
162162

163-
if (to < minPort || to > maxPort + 1) {
164-
throw new RangeError(`'to' must be between ${minPort} and ${maxPort + 1}`);
163+
if (to < minPort || to > maxPort) {
164+
throw new RangeError(`'to' must be between ${minPort} and ${maxPort}`);
165165
}
166166

167-
if (to < from) {
167+
if (from > to) {
168168
throw new RangeError('`to` must be greater than or equal to `from`');
169169
}
170170

test.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,20 @@ test('non-array iterables work', async t => {
116116
t.is(port, 9920);
117117
});
118118

119-
test('makeRange throws on invalid ranges', t => {
120-
t.throws(() => {
121-
portNumbers(1025, 1024);
122-
});
119+
test('portNumbers throws on invalid ranges', t => {
120+
t.throws(() => portNumbers('abc', 3000), {instanceOf: TypeError}, '`from` is not an integer number');
121+
t.throws(() => portNumbers(3000, 'abc'), {instanceOf: TypeError}, '`to` is not an integer number');
123122

124-
// Invalid port values
125-
t.throws(() => {
126-
portNumbers(0, 0);
127-
});
128-
t.throws(() => {
129-
portNumbers(1023, 1023);
130-
});
131-
t.throws(() => {
132-
portNumbers(65_536, 65_536);
133-
});
123+
t.throws(() => portNumbers(1023, 1024), {instanceOf: RangeError}, '`from` is less than the minimum port');
124+
t.throws(() => portNumbers(65_536, 65_536), {instanceOf: RangeError}, '`from` is greater than the maximum port');
125+
126+
t.throws(() => portNumbers(1024, 1023), {instanceOf: RangeError}, '`to` is less than the minimum port');
127+
t.throws(() => portNumbers(65_535, 65_537), {instanceOf: RangeError}, '`to` is greater than the maximum port');
128+
129+
t.throws(() => portNumbers(1025, 1024), {instanceOf: RangeError}, '`from` is less than `to`');
134130
});
135131

136-
test('makeRange produces valid ranges', t => {
132+
test('portNumbers produces valid ranges', t => {
137133
t.deepEqual([...portNumbers(1024, 1024)], [1024]);
138134
t.deepEqual([...portNumbers(1024, 1025)], [1024, 1025]);
139135
t.deepEqual([...portNumbers(1024, 1027)], [1024, 1025, 1026, 1027]);

0 commit comments

Comments
 (0)