• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3// Skip test in FreeBSD jails
4if (common.inFreeBSDJail)
5  common.skip('In a FreeBSD jail');
6
7const assert = require('assert');
8const net = require('net');
9
10let conns = 0;
11const clientLocalPorts = [];
12const serverRemotePorts = [];
13const client = new net.Socket();
14const server = net.createServer((socket) => {
15  serverRemotePorts.push(socket.remotePort);
16  socket.end();
17});
18
19server.on('close', common.mustCall(() => {
20  // Client and server should agree on the ports used
21  assert.deepStrictEqual(serverRemotePorts, clientLocalPorts);
22  assert.strictEqual(conns, 2);
23}));
24
25server.listen(0, common.localhostIPv4, connect);
26
27function connect() {
28  if (conns === 2) {
29    server.close();
30    return;
31  }
32
33  conns++;
34  client.once('close', connect);
35  assert.strictEqual(
36    client,
37    client.connect(server.address().port, common.localhostIPv4, () => {
38      clientLocalPorts.push(client.localPort);
39    })
40  );
41}
42