2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2011-11-03 15:13:22 +01:00
|
|
|
var fs = require('fs');
|
|
|
|
var net = require('net');
|
|
|
|
var path = require('path');
|
|
|
|
var assert = require('assert');
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
var notSocketErrorFired = false;
|
|
|
|
var noEntErrorFired = false;
|
|
|
|
var accessErrorFired = false;
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// use common.PIPE to ensure we stay within POSIX socket path length
|
|
|
|
// restrictions, even on CI
|
2015-10-06 21:12:45 -07:00
|
|
|
common.refreshTmpDir();
|
2014-11-18 17:31:46 -08:00
|
|
|
emptyTxt = common.PIPE + '.txt';
|
|
|
|
|
2014-11-28 09:43:15 +11:00
|
|
|
function cleanup() {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(emptyTxt);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code != 'ENOENT')
|
|
|
|
throw e;
|
|
|
|
}
|
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() {
|
|
|
|
assert.ok(false);
|
|
|
|
});
|
2011-11-03 15:13:22 +01:00
|
|
|
|
2012-01-17 19:43:34 +01:00
|
|
|
notSocketClient.on('error', function(err) {
|
2011-11-04 05:28:30 +01:00
|
|
|
assert(err.code === 'ENOTSOCK' || err.code === 'ECONNREFUSED');
|
2011-11-03 15:13:22 +01:00
|
|
|
notSocketErrorFired = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 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() {
|
2011-11-03 15:13:22 +01:00
|
|
|
assert.ok(false);
|
|
|
|
});
|
|
|
|
|
2012-01-17 19:43:34 +01:00
|
|
|
noEntSocketClient.on('error', function(err) {
|
2011-11-03 15:13:22 +01:00
|
|
|
assert.equal(err.code, 'ENOENT');
|
|
|
|
noEntErrorFired = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
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
|
|
|
|
var accessServer = net.createServer(function() {
|
2011-11-03 15:13:22 +01:00
|
|
|
assert.ok(false);
|
|
|
|
});
|
2012-03-01 13:49:23 -08:00
|
|
|
accessServer.listen(common.PIPE, function() {
|
|
|
|
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() {
|
|
|
|
assert.ok(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
accessClient.on('error', function(err) {
|
|
|
|
assert.equal(err.code, 'EACCES');
|
|
|
|
accessErrorFired = true;
|
|
|
|
accessServer.close();
|
|
|
|
});
|
2011-11-03 15:13:22 +01:00
|
|
|
});
|
2012-03-01 13:49:23 -08:00
|
|
|
}
|
2011-11-03 15:13:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Assert that all error events were fired
|
2012-01-17 19:43:34 +01:00
|
|
|
process.on('exit', function() {
|
2011-11-03 15:13:22 +01:00
|
|
|
assert.ok(notSocketErrorFired);
|
|
|
|
assert.ok(noEntErrorFired);
|
2015-07-29 17:18:04 +05:30
|
|
|
if (!common.isWindows && process.getuid() !== 0) {
|
2012-03-01 13:49:23 -08:00
|
|
|
assert.ok(accessErrorFired);
|
|
|
|
}
|
2011-11-03 15:13:22 +01:00
|
|
|
});
|
|
|
|
|