summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-server-listen-options.js
blob: d2e70215dc1efbf54f09b8ffea3d4f4d23b753f3 (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');
const util = require('util');

function close() { this.close(); }

function listenError(literals, ...values) {
  let result = literals[0];
  for (const [i, value] of values.entries()) {
    const str = util.inspect(value);
    // Need to escape special characters.
    result += str.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&');
    result += literals[i + 1];
  }
  return new RegExp(`Error: Invalid listen argument: ${result}`);
}

{
  // Test listen()
  net.createServer().listen().on('listening', common.mustCall(close));
  // Test listen(cb)
  net.createServer().listen(common.mustCall(close));
  // Test listen(port)
  net.createServer().listen(0).on('listening', common.mustCall(close));
  // Test listen({port})
  net.createServer().listen({ port: 0 })
    .on('listening', common.mustCall(close));
}

// Test listen(port, cb) and listen({port: port}, cb) combinations
const listenOnPort = [
  (port, cb) => net.createServer().listen({ port }, cb),
  (port, cb) => net.createServer().listen(port, cb)
];

{
  const portError = /^RangeError: "port" argument must be >= 0 and < 65536$/;
  for (const listen of listenOnPort) {
    // Arbitrary unused ports
    listen('0', common.mustCall(close));
    listen(0, common.mustCall(close));
    listen(undefined, common.mustCall(close));
    // Test invalid ports
    assert.throws(() => listen(-1, common.mustNotCall()), portError);
    assert.throws(() => listen(NaN, common.mustNotCall()), portError);
    assert.throws(() => listen(123.456, common.mustNotCall()), portError);
    assert.throws(() => listen(65536, common.mustNotCall()), portError);
    assert.throws(() => listen(1 / 0, common.mustNotCall()), portError);
    assert.throws(() => listen(-1 / 0, common.mustNotCall()), portError);
  }
  // In listen(options, cb), port takes precedence over path
  assert.throws(() => {
    net.createServer().listen({ port: -1, path: common.PIPE },
                              common.mustNotCall());
  }, portError);
}

{
  function shouldFailToListen(options, optionsInMessage) {
    // Plain arguments get normalized into an object before
    // formatted in message, options objects don't.
    if (arguments.length === 1) {
      optionsInMessage = options;
    }
    const block = () => {
      net.createServer().listen(options, common.mustNotCall());
    };
    assert.throws(block, listenError`${optionsInMessage}`,
                  `expect listen(${util.inspect(options)}) to throw`);
  }

  shouldFailToListen(null, { port: null });
  shouldFailToListen({ port: null });
  shouldFailToListen(false, { port: false });
  shouldFailToListen({ port: false });
  shouldFailToListen(true, { port: true });
  shouldFailToListen({ port: true });
  // Invalid fd as listen(handle)
  shouldFailToListen({ fd: -1 });
  // Invalid path in listen(options)
  shouldFailToListen({ path: -1 });
  // Host without port
  shouldFailToListen({ host: 'localhost' });
}