|
| 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 | +var fs = require('fs'); |
| 23 | +var net = require('net'); |
| 24 | +var path = require('path'); |
| 25 | +var assert = require('assert'); |
| 26 | +var common = require('../common'); |
| 27 | + |
| 28 | +var notSocketErrorFired = false; |
| 29 | +var noEntErrorFired = false; |
| 30 | +var accessErrorFired = false; |
| 31 | + |
| 32 | +// Test if ENOTSOCK is fired when trying to connect to a file which is not |
| 33 | +// a socket. |
| 34 | +var notSocketClient = net.createConnection( |
| 35 | + path.join(common.fixturesDir, 'empty.txt'), |
| 36 | + function () { |
| 37 | + assert.ok(false); |
| 38 | + } |
| 39 | +); |
| 40 | + |
| 41 | +notSocketClient.on('error', function (err) { |
| 42 | + assert.equal(err.code, 'ENOTSOCK'); |
| 43 | + notSocketErrorFired = true; |
| 44 | +}); |
| 45 | + |
| 46 | + |
| 47 | +// Trying to connect to not-existing socket should result in ENOENT error |
| 48 | +var noEntSocketClient = net.createConnection('no-ent-file', function () { |
| 49 | + assert.ok(false); |
| 50 | +}); |
| 51 | + |
| 52 | +noEntSocketClient.on('error', function (err) { |
| 53 | + assert.equal(err.code, 'ENOENT'); |
| 54 | + noEntErrorFired = true; |
| 55 | +}); |
| 56 | + |
| 57 | + |
| 58 | +// Trying to connect to a socket one has no access to should result in EACCES |
| 59 | +var accessServer = net.createServer(function () { |
| 60 | + assert.ok(false); |
| 61 | +}); |
| 62 | +accessServer.listen(common.PIPE, function () { |
| 63 | + fs.chmodSync(common.PIPE, 0); |
| 64 | + |
| 65 | + var accessClient = net.createConnection(common.PIPE, function () { |
| 66 | + assert.ok(false); |
| 67 | + }); |
| 68 | + |
| 69 | + accessClient.on('error', function (err) { |
| 70 | + assert.equal(err.code, 'EACCES'); |
| 71 | + accessErrorFired = true; |
| 72 | + accessServer.close(); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +// Assert that all error events were fired |
| 78 | +process.on('exit', function () { |
| 79 | + assert.ok(notSocketErrorFired); |
| 80 | + assert.ok(noEntErrorFired); |
| 81 | + assert.ok(accessErrorFired); |
| 82 | +}); |
| 83 | + |
0 commit comments