11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 251cb0ef41Sopenharmony_ciconst fs = require('fs'); 261cb0ef41Sopenharmony_ciconst net = require('net'); 271cb0ef41Sopenharmony_ciconst assert = require('assert'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci// Test if ENOTSOCK is fired when trying to connect to a file which is not 301cb0ef41Sopenharmony_ci// a socket. 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cilet emptyTxt; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciif (common.isWindows) { 351cb0ef41Sopenharmony_ci // On Win, common.PIPE will be a named pipe, so we use an existing empty 361cb0ef41Sopenharmony_ci // file instead 371cb0ef41Sopenharmony_ci emptyTxt = fixtures.path('empty.txt'); 381cb0ef41Sopenharmony_ci} else { 391cb0ef41Sopenharmony_ci const tmpdir = require('../common/tmpdir'); 401cb0ef41Sopenharmony_ci tmpdir.refresh(); 411cb0ef41Sopenharmony_ci // Keep the file name very short so that we don't exceed the 108 char limit 421cb0ef41Sopenharmony_ci // on CI for a POSIX socket. Even though this isn't actually a socket file, 431cb0ef41Sopenharmony_ci // the error will be different from the one we are expecting if we exceed the 441cb0ef41Sopenharmony_ci // limit. 451cb0ef41Sopenharmony_ci emptyTxt = `${tmpdir.path}0.txt`; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci function cleanup() { 481cb0ef41Sopenharmony_ci try { 491cb0ef41Sopenharmony_ci fs.unlinkSync(emptyTxt); 501cb0ef41Sopenharmony_ci } catch (e) { 511cb0ef41Sopenharmony_ci assert.strictEqual(e.code, 'ENOENT'); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci process.on('exit', cleanup); 551cb0ef41Sopenharmony_ci cleanup(); 561cb0ef41Sopenharmony_ci fs.writeFileSync(emptyTxt, ''); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciconst notSocketClient = net.createConnection(emptyTxt, function() { 601cb0ef41Sopenharmony_ci assert.fail('connection callback should not run'); 611cb0ef41Sopenharmony_ci}); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_cinotSocketClient.on('error', common.mustCall(function(err) { 641cb0ef41Sopenharmony_ci assert(err.code === 'ENOTSOCK' || err.code === 'ECONNREFUSED', 651cb0ef41Sopenharmony_ci `received ${err.code} instead of ENOTSOCK or ECONNREFUSED`); 661cb0ef41Sopenharmony_ci})); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci// Trying to connect to not-existing socket should result in ENOENT error 701cb0ef41Sopenharmony_ciconst noEntSocketClient = net.createConnection('no-ent-file', function() { 711cb0ef41Sopenharmony_ci assert.fail('connection to non-existent socket, callback should not run'); 721cb0ef41Sopenharmony_ci}); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cinoEntSocketClient.on('error', common.mustCall(function(err) { 751cb0ef41Sopenharmony_ci assert.strictEqual(err.code, 'ENOENT'); 761cb0ef41Sopenharmony_ci})); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci// On Windows or IBMi or when running as root, 801cb0ef41Sopenharmony_ci// a chmod has no effect on named pipes 811cb0ef41Sopenharmony_ciif (!common.isWindows && !common.isIBMi && process.getuid() !== 0) { 821cb0ef41Sopenharmony_ci // Trying to connect to a socket one has no access to should result in EACCES 831cb0ef41Sopenharmony_ci const accessServer = net.createServer( 841cb0ef41Sopenharmony_ci common.mustNotCall('server callback should not run')); 851cb0ef41Sopenharmony_ci accessServer.listen(common.PIPE, common.mustCall(function() { 861cb0ef41Sopenharmony_ci fs.chmodSync(common.PIPE, 0); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci const accessClient = net.createConnection(common.PIPE, function() { 891cb0ef41Sopenharmony_ci assert.fail('connection should get EACCES, callback should not run'); 901cb0ef41Sopenharmony_ci }); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci accessClient.on('error', common.mustCall(function(err) { 931cb0ef41Sopenharmony_ci assert.strictEqual(err.code, 'EACCES'); 941cb0ef41Sopenharmony_ci accessServer.close(); 951cb0ef41Sopenharmony_ci })); 961cb0ef41Sopenharmony_ci })); 971cb0ef41Sopenharmony_ci} 98