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: refactor test-net-pipe-connect-errors #8473

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 17 additions & 29 deletions test/parallel/test-net-pipe-connect-errors.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
var common = require('../common');
var fs = require('fs');
var net = require('net');
var path = require('path');
var assert = require('assert');

var accessErrorFired = false;
const common = require('../common');
const fs = require('fs');
const net = require('net');
const path = require('path');
const assert = require('assert');

// Test if ENOTSOCK is fired when trying to connect to a file which is not
// a socket.
Expand All @@ -28,8 +26,7 @@ if (common.isWindows) {
try {
fs.unlinkSync(emptyTxt);
} catch (e) {
if (e.code != 'ENOENT')
throw e;
assert.strictEqual(e.code, 'ENOENT');
}
}
process.on('exit', cleanup);
Expand All @@ -38,7 +35,7 @@ if (common.isWindows) {
}

var notSocketClient = net.createConnection(emptyTxt, function() {
assert.ok(false);
common.fail('connection callback should not run');
});

notSocketClient.on('error', common.mustCall(function(err) {
Expand All @@ -49,39 +46,30 @@ notSocketClient.on('error', common.mustCall(function(err) {

// Trying to connect to not-existing socket should result in ENOENT error
var noEntSocketClient = net.createConnection('no-ent-file', function() {
assert.ok(false);
common.fail('connection to non-existent socket, callback should not run');
});

noEntSocketClient.on('error', common.mustCall(function(err) {
assert.equal(err.code, 'ENOENT');
assert.strictEqual(err.code, 'ENOENT');
}));


// On Windows or when running as root, a chmod has no effect on named pipes
if (!common.isWindows && process.getuid() !== 0) {
// Trying to connect to a socket one has no access to should result in EACCES
var accessServer = net.createServer(function() {
assert.ok(false);
const accessServer = net.createServer(function() {
common.fail('server callback should not run');
});
accessServer.listen(common.PIPE, function() {
accessServer.listen(common.PIPE, common.mustCall(function() {
fs.chmodSync(common.PIPE, 0);

var accessClient = net.createConnection(common.PIPE, function() {
assert.ok(false);
common.fail('connection should get EACCES, callback should not run');
});

accessClient.on('error', function(err) {
assert.equal(err.code, 'EACCES');
accessErrorFired = true;
accessClient.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'EACCES');
accessServer.close();
});
});
}));
}));
}


// Assert that all error events were fired
process.on('exit', function() {
if (!common.isWindows && process.getuid() !== 0) {
assert.ok(accessErrorFired);
}
});