summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-server-bind.js
blob: 3617ff3e951c6084faba860d656483d692763300 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');


// With only a callback, server should get a port assigned by the OS

let address0;
const server0 = net.createServer(common.noop);

server0.listen(function() {
  address0 = server0.address();
  console.log('address0 %j', address0);
  server0.close();
});


// No callback to listen(), assume we can bind in 100 ms

let address1;
let connectionKey1;
const server1 = net.createServer(common.noop);

server1.listen(common.PORT);

setTimeout(function() {
  address1 = server1.address();
  connectionKey1 = server1._connectionKey;
  console.log('address1 %j', address1);
  server1.close();
}, 100);


// Callback to listen()

let address2;
const server2 = net.createServer(common.noop);

server2.listen(common.PORT + 1, function() {
  address2 = server2.address();
  console.log('address2 %j', address2);
  server2.close();
});


// Backlog argument

let address3;
const server3 = net.createServer(common.noop);

server3.listen(common.PORT + 2, '0.0.0.0', 127, function() {
  address3 = server3.address();
  console.log('address3 %j', address3);
  server3.close();
});


// Backlog argument without host argument

let address4;
const server4 = net.createServer(common.noop);

server4.listen(common.PORT + 3, 127, function() {
  address4 = server4.address();
  console.log('address4 %j', address4);
  server4.close();
});


process.on('exit', function() {
  assert.ok(address0.port > 100);
  assert.strictEqual(common.PORT, address1.port);

  let expectedConnectionKey1;

  if (address1.family === 'IPv6')
    expectedConnectionKey1 = '6::::' + address1.port;
  else
    expectedConnectionKey1 = '4:0.0.0.0:' + address1.port;

  assert.strictEqual(connectionKey1, expectedConnectionKey1);
  assert.strictEqual(common.PORT + 1, address2.port);
  assert.strictEqual(common.PORT + 2, address3.port);
  assert.strictEqual(common.PORT + 3, address4.port);
});