blob: c245ddc2ae78600f0d46960380dd349a6a3f9662 [file] [log] [blame]
Roman Reissf29762f2015-05-19 11:00:061'use strict';
koichik6c121ed2011-06-21 14:53:022var common = require('../common');
3var assert = require('assert');
4var net = require('net');
5
6var tcpPort = common.PORT;
cjihrig9d2b89d2015-02-23 16:23:537var expectedConnections = 7;
koichikc60cdcd2011-07-04 15:34:588var clientConnected = 0;
9var serverConnected = 0;
koichik6c121ed2011-06-21 14:53:0210
11var server = net.createServer(function(socket) {
koichik6c121ed2011-06-21 14:53:0212 socket.end();
cjihrig9d2b89d2015-02-23 16:23:5313 if (++serverConnected === expectedConnections) {
koichikc60cdcd2011-07-04 15:34:5814 server.close();
15 }
koichik6c121ed2011-06-21 14:53:0216});
cjihrig9d2b89d2015-02-23 16:23:5317
koichik6c121ed2011-06-21 14:53:0218server.listen(tcpPort, 'localhost', function() {
koichikc60cdcd2011-07-04 15:34:5819 function cb() {
20 ++clientConnected;
21 }
22
cjihrig9d2b89d2015-02-23 16:23:5323 function fail(opts, errtype, msg) {
24 assert.throws(function() {
25 var client = net.createConnection(opts, cb);
Roman Reissf29762f2015-05-19 11:00:0626 }, function(err) {
cjihrig9d2b89d2015-02-23 16:23:5327 return err instanceof errtype && msg === err.message;
28 });
29 }
30
koichikc60cdcd2011-07-04 15:34:5831 net.createConnection(tcpPort).on('connect', cb);
32 net.createConnection(tcpPort, 'localhost').on('connect', cb);
33 net.createConnection(tcpPort, cb);
34 net.createConnection(tcpPort, 'localhost', cb);
cjihrig9d2b89d2015-02-23 16:23:5335 net.createConnection(tcpPort + '', 'localhost', cb);
36 net.createConnection({port: tcpPort + ''}).on('connect', cb);
37 net.createConnection({port: '0x' + tcpPort.toString(16)}, cb);
Maciej MaƂeckid80d1312013-09-04 22:57:4338
cjihrig9d2b89d2015-02-23 16:23:5339 fail({
40 port: true
41 }, TypeError, 'port should be a number or string: true');
42
43 fail({
44 port: false
45 }, TypeError, 'port should be a number or string: false');
46
47 fail({
48 port: []
49 }, TypeError, 'port should be a number or string: ');
50
51 fail({
52 port: {}
53 }, TypeError, 'port should be a number or string: [object Object]');
54
55 fail({
56 port: null
57 }, TypeError, 'port should be a number or string: null');
58
59 fail({
60 port: ''
61 }, RangeError, 'port should be >= 0 and < 65536: ');
62
63 fail({
64 port: ' '
65 }, RangeError, 'port should be >= 0 and < 65536: ');
66
67 fail({
68 port: '0x'
69 }, RangeError, 'port should be >= 0 and < 65536: 0x');
70
71 fail({
72 port: '-0x1'
73 }, RangeError, 'port should be >= 0 and < 65536: -0x1');
74
75 fail({
76 port: NaN
77 }, RangeError, 'port should be >= 0 and < 65536: NaN');
78
79 fail({
80 port: Infinity
81 }, RangeError, 'port should be >= 0 and < 65536: Infinity');
82
83 fail({
84 port: -1
85 }, RangeError, 'port should be >= 0 and < 65536: -1');
86
87 fail({
88 port: 65536
89 }, RangeError, 'port should be >= 0 and < 65536: 65536');
90});
91
92// Try connecting to random ports, but do so once the server is closed
93server.on('close', function() {
94 function nop() {}
95
96 net.createConnection({port: 0}).on('error', nop);
97 net.createConnection({port: undefined}).on('error', nop);
koichik6c121ed2011-06-21 14:53:0298});
99
Colton Baker87286cc2011-10-04 22:08:18100process.on('exit', function() {
cjihrig9d2b89d2015-02-23 16:23:53101 assert.equal(clientConnected, expectedConnections);
koichik6c121ed2011-06-21 14:53:02102});