Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix portNumbers argument validation and number generation #64

Merged
merged 1 commit into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -160,11 +160,11 @@ export function portNumbers(from, to) {
throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
}

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

if (to < from) {
if (from > to) {
throw new RangeError('`to` must be greater than or equal to `from`');
}

Expand Down
26 changes: 11 additions & 15 deletions test.js
Expand Up @@ -116,24 +116,20 @@ test('non-array iterables work', async t => {
t.is(port, 9920);
});

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

// Invalid port values
t.throws(() => {
portNumbers(0, 0);
});
t.throws(() => {
portNumbers(1023, 1023);
});
t.throws(() => {
portNumbers(65_536, 65_536);
});
t.throws(() => portNumbers(1023, 1024), {instanceOf: RangeError}, '`from` is less than the minimum port');
t.throws(() => portNumbers(65_536, 65_536), {instanceOf: RangeError}, '`from` is greater than the maximum port');

t.throws(() => portNumbers(1024, 1023), {instanceOf: RangeError}, '`to` is less than the minimum port');
t.throws(() => portNumbers(65_535, 65_537), {instanceOf: RangeError}, '`to` is greater than the maximum port');

t.throws(() => portNumbers(1025, 1024), {instanceOf: RangeError}, '`from` is less than `to`');
});

test('makeRange produces valid ranges', t => {
test('portNumbers produces valid ranges', t => {
t.deepEqual([...portNumbers(1024, 1024)], [1024]);
t.deepEqual([...portNumbers(1024, 1025)], [1024, 1025]);
t.deepEqual([...portNumbers(1024, 1027)], [1024, 1025, 1026, 1027]);
Expand Down