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