Permalink
Browse files

test: refactor test-net-pipe-connect-errors

* var -> const
* try/catch -> assert.throws()
* assert.ok(fail) -> common.fail()
* assert.equal() -> assert.strictEqual()
* replace `exit` handler with `common.mustCall()`

PR-URL: #8473
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
Trott authored and Fishrock123 committed Sep 9, 2016
1 parent 92acff8 commit d2b6b66037f1656c32aad09b60568adc4db645c7
Showing with 17 additions and 29 deletions.
  1. +17 −29 test/parallel/test-net-pipe-connect-errors.js
@@ -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.
@@ -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);
@@ -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) {
@@ -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);
}
});

0 comments on commit d2b6b66

Please sign in to comment.