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 2 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
38 changes: 23 additions & 15 deletions test/parallel/test-cluster-bind-privileged-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,41 @@

'use strict';
const common = require('../common');
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);

// Skip on OS X Mojave. https://github.com/nodejs/node/issues/21679
if (common.isOSX)
common.skip('macOS may allow ordinary processes to use any port');
common.skip('macOS may allow ordinary processes to use any port');
7suyash7 marked this conversation as resolved.
Show resolved Hide resolved

if (common.isIBMi)
common.skip('IBMi may allow ordinary processes to use any port');
common.skip('IBMi may allow ordinary processes to use any port');

if (common.isWindows)
common.skip('not reliable on Windows.');

common.skip('not reliable on Windows.');
if (process.getuid() === 0)
common.skip('Test is not supposed to be run as root.');

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);
}
});
}