0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-net-pipe-connect-errors.js
cjihrig 7dd82dd1c3 test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: https://github.com/nodejs/node/pull/11152
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
2017-02-06 14:07:55 -05:00

75 lines
2.3 KiB
JavaScript

'use strict';
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.
let emptyTxt;
if (common.isWindows) {
// on Win, common.PIPE will be a named pipe, so we use an existing empty
// file instead
emptyTxt = path.join(common.fixturesDir, 'empty.txt');
} else {
common.refreshTmpDir();
// Keep the file name very short so tht we don't exceed the 108 char limit
// on CI for a POSIX socket. Even though this isn't actually a socket file,
// the error will be different from the one we are expecting if we exceed the
// limit.
emptyTxt = common.tmpDir + '0.txt';
function cleanup() {
try {
fs.unlinkSync(emptyTxt);
} catch (e) {
assert.strictEqual(e.code, 'ENOENT');
}
}
process.on('exit', cleanup);
cleanup();
fs.writeFileSync(emptyTxt, '');
}
const notSocketClient = net.createConnection(emptyTxt, function() {
common.fail('connection callback should not run');
});
notSocketClient.on('error', common.mustCall(function(err) {
assert(err.code === 'ENOTSOCK' || err.code === 'ECONNREFUSED',
`received ${err.code} instead of ENOTSOCK or ECONNREFUSED`);
}));
// Trying to connect to not-existing socket should result in ENOENT error
const noEntSocketClient = net.createConnection('no-ent-file', function() {
common.fail('connection to non-existent socket, callback should not run');
});
noEntSocketClient.on('error', common.mustCall(function(err) {
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
const accessServer = net.createServer(
common.mustNotCall('server callback should not run'));
accessServer.listen(common.PIPE, common.mustCall(function() {
fs.chmodSync(common.PIPE, 0);
const accessClient = net.createConnection(common.PIPE, function() {
common.fail('connection should get EACCES, callback should not run');
});
accessClient.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'EACCES');
accessServer.close();
}));
}));
}