1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const fixtures = require('../common/fixtures'); 25const fs = require('fs'); 26const net = require('net'); 27const assert = require('assert'); 28 29// Test if ENOTSOCK is fired when trying to connect to a file which is not 30// a socket. 31 32let emptyTxt; 33 34if (common.isWindows) { 35 // On Win, common.PIPE will be a named pipe, so we use an existing empty 36 // file instead 37 emptyTxt = fixtures.path('empty.txt'); 38} else { 39 const tmpdir = require('../common/tmpdir'); 40 tmpdir.refresh(); 41 // Keep the file name very short so that we don't exceed the 108 char limit 42 // on CI for a POSIX socket. Even though this isn't actually a socket file, 43 // the error will be different from the one we are expecting if we exceed the 44 // limit. 45 emptyTxt = `${tmpdir.path}0.txt`; 46 47 function cleanup() { 48 try { 49 fs.unlinkSync(emptyTxt); 50 } catch (e) { 51 assert.strictEqual(e.code, 'ENOENT'); 52 } 53 } 54 process.on('exit', cleanup); 55 cleanup(); 56 fs.writeFileSync(emptyTxt, ''); 57} 58 59const notSocketClient = net.createConnection(emptyTxt, function() { 60 assert.fail('connection callback should not run'); 61}); 62 63notSocketClient.on('error', common.mustCall(function(err) { 64 assert(err.code === 'ENOTSOCK' || err.code === 'ECONNREFUSED', 65 `received ${err.code} instead of ENOTSOCK or ECONNREFUSED`); 66})); 67 68 69// Trying to connect to not-existing socket should result in ENOENT error 70const noEntSocketClient = net.createConnection('no-ent-file', function() { 71 assert.fail('connection to non-existent socket, callback should not run'); 72}); 73 74noEntSocketClient.on('error', common.mustCall(function(err) { 75 assert.strictEqual(err.code, 'ENOENT'); 76})); 77 78 79// On Windows or IBMi or when running as root, 80// a chmod has no effect on named pipes 81if (!common.isWindows && !common.isIBMi && process.getuid() !== 0) { 82 // Trying to connect to a socket one has no access to should result in EACCES 83 const accessServer = net.createServer( 84 common.mustNotCall('server callback should not run')); 85 accessServer.listen(common.PIPE, common.mustCall(function() { 86 fs.chmodSync(common.PIPE, 0); 87 88 const accessClient = net.createConnection(common.PIPE, function() { 89 assert.fail('connection should get EACCES, callback should not run'); 90 }); 91 92 accessClient.on('error', common.mustCall(function(err) { 93 assert.strictEqual(err.code, 'EACCES'); 94 accessServer.close(); 95 })); 96 })); 97} 98