2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-09-10 00:57:51 +02:00
|
|
|
const common = require('../common');
|
|
|
|
const fs = require('fs');
|
|
|
|
const net = require('net');
|
|
|
|
const path = require('path');
|
|
|
|
const assert = require('assert');
|
2011-11-03 15:13:22 +01:00
|
|
|
|
|
|
|
// Test if ENOTSOCK is fired when trying to connect to a file which is not
|
|
|
|
// a socket.
|
2014-11-19 02:31:46 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let emptyTxt;
|
2014-11-19 02:31:46 +01:00
|
|
|
|
2015-07-29 13:48:04 +02:00
|
|
|
if (common.isWindows) {
|
2014-11-19 02:31:46 +01:00
|
|
|
// 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 {
|
2015-10-07 06:12:45 +02:00
|
|
|
common.refreshTmpDir();
|
2015-12-30 08:09:25 +01:00
|
|
|
// 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';
|
2014-11-19 02:31:46 +01:00
|
|
|
|
2014-11-27 23:43:15 +01:00
|
|
|
function cleanup() {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(emptyTxt);
|
|
|
|
} catch (e) {
|
2016-09-10 00:57:51 +02:00
|
|
|
assert.strictEqual(e.code, 'ENOENT');
|
2014-11-27 23:43:15 +01:00
|
|
|
}
|
2014-11-06 05:30:10 +01:00
|
|
|
}
|
2014-11-27 23:43:15 +01:00
|
|
|
process.on('exit', cleanup);
|
|
|
|
cleanup();
|
|
|
|
fs.writeFileSync(emptyTxt, '');
|
2014-11-06 05:30:10 +01:00
|
|
|
}
|
2014-11-27 23:43:15 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const notSocketClient = net.createConnection(emptyTxt, function() {
|
2016-09-10 00:57:51 +02:00
|
|
|
common.fail('connection callback should not run');
|
2012-01-17 19:43:34 +01:00
|
|
|
});
|
2011-11-03 15:13:22 +01:00
|
|
|
|
2016-07-15 21:43:24 +02:00
|
|
|
notSocketClient.on('error', common.mustCall(function(err) {
|
2015-12-29 06:28:36 +01:00
|
|
|
assert(err.code === 'ENOTSOCK' || err.code === 'ECONNREFUSED',
|
2016-05-12 23:33:46 +02:00
|
|
|
`received ${err.code} instead of ENOTSOCK or ECONNREFUSED`);
|
2016-07-15 21:43:24 +02:00
|
|
|
}));
|
2011-11-03 15:13:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Trying to connect to not-existing socket should result in ENOENT error
|
2017-01-08 14:19:00 +01:00
|
|
|
const noEntSocketClient = net.createConnection('no-ent-file', function() {
|
2016-09-10 00:57:51 +02:00
|
|
|
common.fail('connection to non-existent socket, callback should not run');
|
2011-11-03 15:13:22 +01:00
|
|
|
});
|
|
|
|
|
2016-07-15 21:43:24 +02:00
|
|
|
noEntSocketClient.on('error', common.mustCall(function(err) {
|
2016-09-10 00:57:51 +02:00
|
|
|
assert.strictEqual(err.code, 'ENOENT');
|
2016-07-15 21:43:24 +02:00
|
|
|
}));
|
2011-11-03 15:13:22 +01:00
|
|
|
|
|
|
|
|
2012-05-20 05:42:07 +02:00
|
|
|
// On Windows or when running as root, a chmod has no effect on named pipes
|
2015-07-29 13:48:04 +02:00
|
|
|
if (!common.isWindows && process.getuid() !== 0) {
|
2012-03-01 22:49:23 +01:00
|
|
|
// Trying to connect to a socket one has no access to should result in EACCES
|
2017-02-03 20:54:19 +01:00
|
|
|
const accessServer = net.createServer(
|
|
|
|
common.mustNotCall('server callback should not run'));
|
2016-09-10 00:57:51 +02:00
|
|
|
accessServer.listen(common.PIPE, common.mustCall(function() {
|
2012-03-01 22:49:23 +01:00
|
|
|
fs.chmodSync(common.PIPE, 0);
|
2011-11-03 15:13:22 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const accessClient = net.createConnection(common.PIPE, function() {
|
2016-09-10 00:57:51 +02:00
|
|
|
common.fail('connection should get EACCES, callback should not run');
|
2012-03-01 22:49:23 +01:00
|
|
|
});
|
|
|
|
|
2016-09-10 00:57:51 +02:00
|
|
|
accessClient.on('error', common.mustCall(function(err) {
|
|
|
|
assert.strictEqual(err.code, 'EACCES');
|
2012-03-01 22:49:23 +01:00
|
|
|
accessServer.close();
|
2016-09-10 00:57:51 +02:00
|
|
|
}));
|
|
|
|
}));
|
2012-03-01 22:49:23 +01:00
|
|
|
}
|