• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const net = require('net');
5
6function close() { this.close(); }
7
8{
9  // Test listen()
10  net.createServer().listen().on('listening', common.mustCall(close));
11  // Test listen(cb)
12  net.createServer().listen(common.mustCall(close));
13  // Test listen(port)
14  net.createServer().listen(0).on('listening', common.mustCall(close));
15  // Test listen({port})
16  net.createServer().listen({ port: 0 })
17    .on('listening', common.mustCall(close));
18}
19
20// Test listen(port, cb) and listen({ port }, cb) combinations
21const listenOnPort = [
22  (port, cb) => net.createServer().listen({ port }, cb),
23  (port, cb) => net.createServer().listen(port, cb),
24];
25
26{
27  const assertPort = () => {
28    return common.expectsError({
29      code: 'ERR_SOCKET_BAD_PORT',
30      name: 'RangeError'
31    });
32  };
33
34  for (const listen of listenOnPort) {
35    // Arbitrary unused ports
36    listen('0', common.mustCall(close));
37    listen(0, common.mustCall(close));
38    listen(undefined, common.mustCall(close));
39    listen(null, common.mustCall(close));
40    // Test invalid ports
41    assert.throws(() => listen(-1, common.mustNotCall()), assertPort());
42    assert.throws(() => listen(NaN, common.mustNotCall()), assertPort());
43    assert.throws(() => listen(123.456, common.mustNotCall()), assertPort());
44    assert.throws(() => listen(65536, common.mustNotCall()), assertPort());
45    assert.throws(() => listen(1 / 0, common.mustNotCall()), assertPort());
46    assert.throws(() => listen(-1 / 0, common.mustNotCall()), assertPort());
47  }
48  // In listen(options, cb), port takes precedence over path
49  assert.throws(() => {
50    net.createServer().listen({ port: -1, path: common.PIPE },
51                              common.mustNotCall());
52  }, assertPort());
53}
54
55{
56  function shouldFailToListen(options) {
57    const fn = () => {
58      net.createServer().listen(options, common.mustNotCall());
59    };
60
61    if (typeof options === 'object' &&
62      !(('port' in options) || ('path' in options))) {
63      assert.throws(fn,
64                    {
65                      code: 'ERR_INVALID_ARG_VALUE',
66                      name: 'TypeError',
67                      message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
68                    });
69    } else {
70      assert.throws(fn,
71                    {
72                      code: 'ERR_INVALID_OPT_VALUE',
73                      name: 'TypeError',
74                      message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/,
75                    });
76    }
77  }
78
79  shouldFailToListen(false, { port: false });
80  shouldFailToListen({ port: false });
81  shouldFailToListen(true);
82  shouldFailToListen({ port: true });
83  // Invalid fd as listen(handle)
84  shouldFailToListen({ fd: -1 });
85  // Invalid path in listen(options)
86  shouldFailToListen({ path: -1 });
87
88  // Neither port or path are specified in options
89  shouldFailToListen({});
90  shouldFailToListen({ host: 'localhost' });
91  shouldFailToListen({ host: 'localhost:3000' });
92  shouldFailToListen({ host: { port: 3000 } });
93  shouldFailToListen({ exclusive: true });
94}
95