summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-socket-local-address.js
blob: c4f11db76bc4f2ccfdde19e08a2c496eea9cb4f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const common = require('../common');
// skip test in FreeBSD jails
if (common.inFreeBSDJail)
  common.skip('In a FreeBSD jail');

const assert = require('assert');
const net = require('net');

let conns = 0;
const clientLocalPorts = [];
const serverRemotePorts = [];
const client = new net.Socket();
const server = net.createServer((socket) => {
  serverRemotePorts.push(socket.remotePort);
  socket.end();
});

server.on('close', common.mustCall(() => {
  assert.deepStrictEqual(clientLocalPorts, serverRemotePorts,
                         'client and server should agree on the ports used');
  assert.strictEqual(2, conns);
}));

server.listen(0, common.localhostIPv4, connect);

function connect() {
  if (conns === 2) {
    server.close();
    return;
  }

  conns++;
  client.once('close', connect);
  assert.strictEqual(
    client,
    client.connect(server.address().port, common.localhostIPv4, () => {
      clientLocalPorts.push(client.localPort);
    })
  );
}