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

test: add fix so that test exits if port 42 is unprivileged #45904

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Changes from 4 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
35 changes: 23 additions & 12 deletions test/parallel/test-cluster-bind-privileged-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@

'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');

const { execSync } = require('child_process');
const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString();
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved
const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10);
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved

if (common.isLinux) {
if (unprivilegedPortStart === 42) {
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved
common.skip('42 is unprivileged');
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Skip on OS X Mojave. https://github.com/nodejs/node/issues/21679
if (common.isOSX)
Expand All @@ -35,19 +48,17 @@ if (common.isWindows)
if (process.getuid() === 0)
common.skip('Test is not supposed to be run as root.');

const assert = require('assert');
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved
const cluster = require('cluster');
const net = require('net');

if (cluster.isPrimary) {
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved
cluster.fork().on('exit', common.mustCall((exitCode) => {
assert.strictEqual(exitCode, 0);
}));
cluster.fork().on('exit', (exitCode) => {
assert.strictEqual(exitCode, 1);
});
} else {
const s = net.createServer(common.mustNotCall());
s.listen(42, common.mustNotCall('listen should have failed'));
s.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'EACCES');
process.disconnect();
}));
s.listen(unprivilegedPortStart, (err) => {
if (err && err.code === 'EACCES') {
process.disconnect();
} else {
process.exit(0);
}
});
}