Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 1 | 'use strict'; |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 2 | var common = require('../common'); |
| 3 | var assert = require('assert'); |
| 4 | var net = require('net'); |
| 5 | |
| 6 | var tcpPort = common.PORT; |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 7 | var expectedConnections = 7; |
koichik | c60cdcd | 2011-07-04 15:34:58 | [diff] [blame] | 8 | var clientConnected = 0; |
| 9 | var serverConnected = 0; |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 10 | |
| 11 | var server = net.createServer(function(socket) { |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 12 | socket.end(); |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 13 | if (++serverConnected === expectedConnections) { |
koichik | c60cdcd | 2011-07-04 15:34:58 | [diff] [blame] | 14 | server.close(); |
| 15 | } |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 16 | }); |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 17 | |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 18 | server.listen(tcpPort, 'localhost', function() { |
koichik | c60cdcd | 2011-07-04 15:34:58 | [diff] [blame] | 19 | function cb() { |
| 20 | ++clientConnected; |
| 21 | } |
| 22 | |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 23 | function fail(opts, errtype, msg) { |
| 24 | assert.throws(function() { |
| 25 | var client = net.createConnection(opts, cb); |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 26 | }, function(err) { |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 27 | return err instanceof errtype && msg === err.message; |
| 28 | }); |
| 29 | } |
| 30 | |
koichik | c60cdcd | 2011-07-04 15:34:58 | [diff] [blame] | 31 | 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); |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 35 | net.createConnection(tcpPort + '', 'localhost', cb); |
| 36 | net.createConnection({port: tcpPort + ''}).on('connect', cb); |
| 37 | net.createConnection({port: '0x' + tcpPort.toString(16)}, cb); |
Maciej MaĆecki | d80d131 | 2013-09-04 22:57:43 | [diff] [blame] | 38 | |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 39 | 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 |
| 93 | server.on('close', function() { |
| 94 | function nop() {} |
| 95 | |
| 96 | net.createConnection({port: 0}).on('error', nop); |
| 97 | net.createConnection({port: undefined}).on('error', nop); |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 98 | }); |
| 99 | |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 100 | process.on('exit', function() { |
cjihrig | 9d2b89d | 2015-02-23 16:23:53 | [diff] [blame] | 101 | assert.equal(clientConnected, expectedConnections); |
koichik | 6c121ed | 2011-06-21 14:53:02 | [diff] [blame] | 102 | }); |