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 assert = require('assert'); 25 26const net = require('net'); 27 28let conns_closed = 0; 29 30const remoteAddrCandidates = [ common.localhostIPv4, 31 '::1', 32 '::ffff:127.0.0.1' ]; 33 34const remoteFamilyCandidates = ['IPv4', 'IPv6']; 35 36const server = net.createServer(common.mustCall(function(socket) { 37 assert.ok(remoteAddrCandidates.includes(socket.remoteAddress), 38 `Invalid remoteAddress: ${socket.remoteAddress}`); 39 assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily), 40 `Invalid remoteFamily: ${socket.remoteFamily}`); 41 assert.ok(socket.remotePort); 42 assert.notStrictEqual(socket.remotePort, this.address().port); 43 socket.on('end', function() { 44 if (++conns_closed === 2) server.close(); 45 }); 46 socket.on('close', function() { 47 assert.ok(remoteAddrCandidates.includes(socket.remoteAddress)); 48 assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily)); 49 }); 50 socket.resume(); 51}, 2)); 52 53server.listen(0, function() { 54 const client = net.createConnection(this.address().port, '127.0.0.1'); 55 const client2 = net.createConnection(this.address().port); 56 57 assert.strictEqual(client.remoteAddress, undefined); 58 assert.strictEqual(client.remoteFamily, undefined); 59 assert.strictEqual(client.remotePort, undefined); 60 assert.strictEqual(client2.remoteAddress, undefined); 61 assert.strictEqual(client2.remoteFamily, undefined); 62 assert.strictEqual(client2.remotePort, undefined); 63 64 client.on('connect', function() { 65 assert.ok(remoteAddrCandidates.includes(client.remoteAddress)); 66 assert.ok(remoteFamilyCandidates.includes(client.remoteFamily)); 67 assert.strictEqual(client.remotePort, server.address().port); 68 client.end(); 69 }); 70 client.on('close', function() { 71 assert.ok(remoteAddrCandidates.includes(client.remoteAddress)); 72 assert.ok(remoteFamilyCandidates.includes(client.remoteFamily)); 73 }); 74 client2.on('connect', function() { 75 assert.ok(remoteAddrCandidates.includes(client2.remoteAddress)); 76 assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily)); 77 assert.strictEqual(client2.remotePort, server.address().port); 78 client2.end(); 79 }); 80 client2.on('close', function() { 81 assert.ok(remoteAddrCandidates.includes(client2.remoteAddress)); 82 assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily)); 83 }); 84}); 85