Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions test/parallel/test-cluster-bind-privileged-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ const net = require('net');
const { readFileSync } = require('fs');

if (common.isLinux) {
const unprivilegedPortStart = parseInt(readFileSync('/proc/sys/net/ipv4/ip_unprivileged_port_start'));
if (unprivilegedPortStart <= 42) {
common.skip('Port 42 is unprivileged');
try {
const unprivilegedPortStart = parseInt(readFileSync('/proc/sys/net/ipv4/ip_unprivileged_port_start'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit that you are free to ignore. I would put only readFileSync() in the try...catch.

let unprivilegedPortStart:

try {
  unprivilegedPortStart = readFileSync('/proc/sys/net/ipv4/ip_unprivileged_port_start');
} catch {
  // Do nothing.
}

if (parseInt(unprivilegedPortStart) <= 42) {
  common.skip('Port 42 is unprivileged');
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that (and even wrote it). The above I dislike a bit because parseInt returns NaN and Nan <=42 which means it'll work but it's very obscure. First thing you think is "isn't this broken"? So you probably want a "unprivilegedPortStart = 1024;" in the catch which is what the scenario REALLY is when the readFileSync fails.

BUT The entire test of minimum ports, etc is irrelevant when that read fails. That indicates the ability to change the minimum doesn't exist hence even considering it is irrelevant.

So I went with "if the read fails, this entire block of code is irrelevant".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could be explicit and do

if (unprivilegedPortStart !== undefined && parseInt(unprivilegedPortStart) <= 42) {
  common.skip('Port 42 is unprivileged');
}

My reasoning is that only things that are expected to throws errors should be in the try block to avoid hiding unexpected exception.

Anyway, it is only a nit. It is ok as is.

if (unprivilegedPortStart <= 42) {
common.skip('Port 42 is unprivileged');
}
} catch {
// Do nothing, feature doesn't exist, minimum is 1024 so 42 is usable.
// Continue...
}
}

Expand Down