• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ];
31if (common.hasIPv6) remoteAddrCandidates.push('::ffff:127.0.0.1');
32
33const remoteFamilyCandidates = ['IPv4'];
34if (common.hasIPv6) remoteFamilyCandidates.push('IPv6');
35
36const server = net.createServer(common.mustCall(function(socket) {
37  assert.ok(remoteAddrCandidates.includes(socket.remoteAddress));
38  assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily));
39  assert.ok(socket.remotePort);
40  assert.notStrictEqual(socket.remotePort, this.address().port);
41  socket.on('end', function() {
42    if (++conns_closed === 2) server.close();
43  });
44  socket.on('close', function() {
45    assert.ok(remoteAddrCandidates.includes(socket.remoteAddress));
46    assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily));
47  });
48  socket.resume();
49}, 2));
50
51server.listen(0, 'localhost', function() {
52  const client = net.createConnection(this.address().port, 'localhost');
53  const client2 = net.createConnection(this.address().port);
54  client.on('connect', function() {
55    assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
56    assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
57    assert.strictEqual(client.remotePort, server.address().port);
58    client.end();
59  });
60  client.on('close', function() {
61    assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
62    assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
63  });
64  client2.on('connect', function() {
65    assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
66    assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
67    assert.strictEqual(client2.remotePort, server.address().port);
68    client2.end();
69  });
70  client2.on('close', function() {
71    assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
72    assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
73  });
74});
75