• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const net = require('net');
26
27// Test on IPv4 Server
28{
29  const family = 'IPv4';
30  const server = net.createServer();
31
32  server.on('error', common.mustNotCall());
33
34  server
35    .listen(common.PORT + 1, common.localhostIPv4, common.mustCall(() => {
36      const address4 = server.address();
37      assert.strictEqual(address4.address, common.localhostIPv4);
38      assert.strictEqual(address4.port, common.PORT + 1);
39      assert.strictEqual(address4.family, family);
40      server.close();
41    }));
42}
43
44if (!common.hasIPv6) {
45  common.printSkipMessage('ipv6 part of test, no IPv6 support');
46  return;
47}
48
49const family6 = 'IPv6';
50const anycast6 = '::';
51
52// Test on IPv6 Server
53{
54  const localhost = '::1';
55
56  const server = net.createServer();
57
58  server.on('error', common.mustNotCall());
59
60  server.listen(common.PORT + 2, localhost, common.mustCall(() => {
61    const address = server.address();
62    assert.strictEqual(address.address, localhost);
63    assert.strictEqual(address.port, common.PORT + 2);
64    assert.strictEqual(address.family, family6);
65    server.close();
66  }));
67}
68
69// Test without hostname or ip
70{
71  const server = net.createServer();
72
73  server.on('error', common.mustNotCall());
74
75  // Specify the port number
76  server.listen(common.PORT + 3, common.mustCall(() => {
77    const address = server.address();
78    assert.strictEqual(address.address, anycast6);
79    assert.strictEqual(address.port, common.PORT + 3);
80    assert.strictEqual(address.family, family6);
81    server.close();
82  }));
83}
84
85// Test without hostname or port
86{
87  const server = net.createServer();
88
89  server.on('error', common.mustNotCall());
90
91  // Don't specify the port number
92  server.listen(common.mustCall(() => {
93    const address = server.address();
94    assert.strictEqual(address.address, anycast6);
95    assert.strictEqual(address.family, family6);
96    server.close();
97  }));
98}
99
100// Test without hostname, but with a false-y port
101{
102  const server = net.createServer();
103
104  server.on('error', common.mustNotCall());
105
106  // Specify a false-y port number
107  server.listen(0, common.mustCall(() => {
108    const address = server.address();
109    assert.strictEqual(address.address, anycast6);
110    assert.strictEqual(address.family, family6);
111    server.close();
112  }));
113}
114