2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-09-09 15:28:15 -07:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2011-04-13 12:19:15 -04:00
|
|
|
|
2016-09-09 15:28:15 -07:00
|
|
|
const net = require('net');
|
2011-04-13 12:19:15 -04:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let conns_closed = 0;
|
2011-04-13 12:19:15 -04:00
|
|
|
|
2020-01-29 13:12:15 +01:00
|
|
|
const remoteAddrCandidates = [ common.localhostIPv4,
|
|
|
|
'::1',
|
|
|
|
'::ffff:127.0.0.1' ];
|
2014-08-11 13:29:00 -07:00
|
|
|
|
2020-01-29 13:12:15 +01:00
|
|
|
const remoteFamilyCandidates = ['IPv4', 'IPv6'];
|
2014-08-11 13:29:00 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = net.createServer(common.mustCall(function(socket) {
|
2020-01-29 13:12:15 +01:00
|
|
|
assert.ok(remoteAddrCandidates.includes(socket.remoteAddress),
|
|
|
|
`Invalid remoteAddress: ${socket.remoteAddress}`);
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily),
|
|
|
|
`Invalid remoteFamily: ${socket.remoteFamily}`);
|
2011-09-05 02:09:24 +02:00
|
|
|
assert.ok(socket.remotePort);
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.notStrictEqual(socket.remotePort, this.address().port);
|
2011-04-13 12:19:15 -04:00
|
|
|
socket.on('end', function() {
|
2016-09-09 15:28:15 -07:00
|
|
|
if (++conns_closed === 2) server.close();
|
2011-04-13 12:19:15 -04:00
|
|
|
});
|
2015-03-10 16:48:19 -04:00
|
|
|
socket.on('close', function() {
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.ok(remoteAddrCandidates.includes(socket.remoteAddress));
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily));
|
2015-03-10 16:48:19 -04:00
|
|
|
});
|
2012-12-13 09:51:31 -08:00
|
|
|
socket.resume();
|
2016-07-15 15:43:24 -04:00
|
|
|
}, 2));
|
2011-04-13 12:19:15 -04:00
|
|
|
|
2020-01-29 13:12:15 +01:00
|
|
|
server.listen(0, function() {
|
|
|
|
const client = net.createConnection(this.address().port, '127.0.0.1');
|
2017-01-08 13:19:00 +00:00
|
|
|
const client2 = net.createConnection(this.address().port);
|
2022-05-08 23:54:08 +08:00
|
|
|
|
|
|
|
assert.strictEqual(client.remoteAddress, undefined);
|
|
|
|
assert.strictEqual(client.remoteFamily, undefined);
|
|
|
|
assert.strictEqual(client.remotePort, undefined);
|
|
|
|
assert.strictEqual(client2.remoteAddress, undefined);
|
|
|
|
assert.strictEqual(client2.remoteFamily, undefined);
|
|
|
|
assert.strictEqual(client2.remotePort, undefined);
|
|
|
|
|
2011-04-13 12:19:15 -04:00
|
|
|
client.on('connect', function() {
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
|
2016-09-09 15:28:15 -07:00
|
|
|
assert.strictEqual(client.remotePort, server.address().port);
|
2011-04-13 12:19:15 -04:00
|
|
|
client.end();
|
|
|
|
});
|
2015-03-10 16:48:19 -04:00
|
|
|
client.on('close', function() {
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
|
2015-03-10 16:48:19 -04:00
|
|
|
});
|
2011-04-13 12:19:15 -04:00
|
|
|
client2.on('connect', function() {
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
|
2016-09-09 15:28:15 -07:00
|
|
|
assert.strictEqual(client2.remotePort, server.address().port);
|
2011-04-13 12:19:15 -04:00
|
|
|
client2.end();
|
|
|
|
});
|
2015-03-10 16:48:19 -04:00
|
|
|
client2.on('close', function() {
|
2016-12-30 10:09:13 -05:00
|
|
|
assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
|
|
|
|
assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
|
2015-03-10 16:48:19 -04:00
|
|
|
});
|
2011-04-13 12:19:15 -04:00
|
|
|
});
|