From cb00945e8f3b20a15cb0f36264fe3f1e9ef5c6e2 Mon Sep 17 00:00:00 2001 From: Suyash Date: Sun, 18 Dec 2022 16:26:34 +0530 Subject: [PATCH 1/6] test: add fix so that test exits if port 42 is unprivileged --- .../test-cluster-bind-privileged-port.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index b952ac1c6ce5d3..fd66e9472c0b65 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -40,14 +40,17 @@ const cluster = require('cluster'); const net = require('net'); if (cluster.isPrimary) { - 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(42, (err) => { + if (err && err.code === 'EACCES') { + process.disconnect(); + } else { + process.exit(0); + } + }); } + From df939819d7fb3ca3122d28577e931243679384bb Mon Sep 17 00:00:00 2001 From: Suyash Date: Mon, 19 Dec 2022 10:41:17 +0530 Subject: [PATCH 2/6] test: added execSync() call --- .../test-cluster-bind-privileged-port.js | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index fd66e9472c0b65..fba3528e2263f3 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -21,36 +21,41 @@ 'use strict'; const common = require('../common'); +const { execSync } = require('child_process'); + +const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); + +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'); 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'); const cluster = require('cluster'); const net = require('net'); - + if (cluster.isPrimary) { cluster.fork().on('exit', (exitCode) => { assert.strictEqual(exitCode, 1); - }); +}); } else { const s = net.createServer(common.mustNotCall()); - s.listen(42, (err) => { + s.listen(unprivilegedPortStart, (err) => { if (err && err.code === 'EACCES') { process.disconnect(); } else { process.exit(0); - } + } }); } From 2ba859a6d12dcf04e421b57591ae0e603922321c Mon Sep 17 00:00:00 2001 From: Suyash Date: Mon, 19 Dec 2022 23:08:33 +0530 Subject: [PATCH 3/6] test: added suggested changes --- .../test-cluster-bind-privileged-port.js | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index fba3528e2263f3..30dc3944331029 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -21,32 +21,36 @@ 'use strict'; const common = require('../common'); -const { execSync } = require('child_process'); - -const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); +const assert = require('assert'); +const cluster = require('cluster'); +const net = require('net'); +const { copyFile } = require('fs'); -const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); +if(common.isLinux) { + const { execSync } = require('child_process'); + const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); + const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); + if(unprivilegedPortStart === 42) { + common.skip('42 is unprivileged'); + } +} // 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'); 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.'); - -const assert = require('assert'); -const cluster = require('cluster'); -const net = require('net'); + common.skip('Test is not supposed to be run as root.'); if (cluster.isPrimary) { cluster.fork().on('exit', (exitCode) => { - assert.strictEqual(exitCode, 1); + assert.strictEqual(exitCode, 1); }); } else { const s = net.createServer(common.mustNotCall()); From e9d8d6b9ed8dfc9632f85752e573d418f0d54f41 Mon Sep 17 00:00:00 2001 From: Suyash Date: Wed, 21 Dec 2022 09:24:48 +0530 Subject: [PATCH 4/6] test: fix js-lint errors --- .../test-cluster-bind-privileged-port.js | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index 30dc3944331029..156707e8ab97e1 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -24,13 +24,13 @@ const common = require('../common'); const assert = require('assert'); const cluster = require('cluster'); const net = require('net'); -const { copyFile } = require('fs'); -if(common.isLinux) { - const { execSync } = require('child_process'); - const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); - const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); - if(unprivilegedPortStart === 42) { +const { execSync } = require('child_process'); +const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); +const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); + +if (common.isLinux) { + if (unprivilegedPortStart === 42) { common.skip('42 is unprivileged'); } } @@ -44,14 +44,14 @@ if (common.isIBMi) if (common.isWindows) common.skip('not reliable on Windows.'); - + if (process.getuid() === 0) common.skip('Test is not supposed to be run as root.'); - + if (cluster.isPrimary) { cluster.fork().on('exit', (exitCode) => { - assert.strictEqual(exitCode, 1); -}); + assert.strictEqual(exitCode, 1); + }); } else { const s = net.createServer(common.mustNotCall()); s.listen(unprivilegedPortStart, (err) => { @@ -59,7 +59,6 @@ if (cluster.isPrimary) { process.disconnect(); } else { process.exit(0); - } + } }); } - From 2b31776638fabb5bee83f66014849a7c37d6b2f2 Mon Sep 17 00:00:00 2001 From: Suyash Date: Wed, 21 Dec 2022 18:05:13 +0530 Subject: [PATCH 5/6] test: add suggested changes --- .../test-cluster-bind-privileged-port.js | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index 156707e8ab97e1..e392c9d64a2183 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -24,14 +24,13 @@ 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(); -const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); if (common.isLinux) { - if (unprivilegedPortStart === 42) { - common.skip('42 is unprivileged'); + const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); + const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); + if (unprivilegedPortStart <= 42) { + common.skip('Port 42 is unprivileged'); } } @@ -49,16 +48,14 @@ if (process.getuid() === 0) common.skip('Test is not supposed to be run as root.'); if (cluster.isPrimary) { - cluster.fork().on('exit', (exitCode) => { - assert.strictEqual(exitCode, 1); - }); + cluster.fork().on('exit', common.mustCall((exitCode) => { + assert.strictEqual(exitCode, 0); + })); } else { const s = net.createServer(common.mustNotCall()); - s.listen(unprivilegedPortStart, (err) => { - if (err && err.code === 'EACCES') { - process.disconnect(); - } else { - process.exit(0); - } - }); + s.listen(42, common.mustNotCall('listen should have failed')); + s.on('error', common.mustCall((err) => { + assert.strictEqual(err.code, 'EACCES'); + process.disconnect(); + })); } From c8d3d7905bd9fef615608b8c13f38f2f494395c3 Mon Sep 17 00:00:00 2001 From: Suyash Date: Thu, 22 Dec 2022 10:08:38 +0530 Subject: [PATCH 6/6] test: fix index for unprivilegedPort --- test/parallel/test-cluster-bind-privileged-port.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-cluster-bind-privileged-port.js b/test/parallel/test-cluster-bind-privileged-port.js index e392c9d64a2183..1249230177d2ec 100644 --- a/test/parallel/test-cluster-bind-privileged-port.js +++ b/test/parallel/test-cluster-bind-privileged-port.js @@ -28,7 +28,7 @@ const { execSync } = require('child_process'); if (common.isLinux) { const sysctlOutput = execSync('sysctl net.ipv4.ip_unprivileged_port_start').toString(); - const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[1], 10); + const unprivilegedPortStart = parseInt(sysctlOutput.split(' ')[2], 10); if (unprivilegedPortStart <= 42) { common.skip('Port 42 is unprivileged'); }