2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2011-06-21 23:53:02 +09:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var net = require('net');
|
|
|
|
|
|
|
|
var tcpPort = common.PORT;
|
2015-02-23 11:23:53 -05:00
|
|
|
var expectedConnections = 7;
|
2011-07-05 00:34:58 +09:00
|
|
|
var clientConnected = 0;
|
|
|
|
var serverConnected = 0;
|
2011-06-21 23:53:02 +09:00
|
|
|
|
|
|
|
var server = net.createServer(function(socket) {
|
|
|
|
socket.end();
|
2015-02-23 11:23:53 -05:00
|
|
|
if (++serverConnected === expectedConnections) {
|
2011-07-05 00:34:58 +09:00
|
|
|
server.close();
|
|
|
|
}
|
2011-06-21 23:53:02 +09:00
|
|
|
});
|
2015-02-23 11:23:53 -05:00
|
|
|
|
2011-06-21 23:53:02 +09:00
|
|
|
server.listen(tcpPort, 'localhost', function() {
|
2011-07-05 00:34:58 +09:00
|
|
|
function cb() {
|
|
|
|
++clientConnected;
|
|
|
|
}
|
|
|
|
|
2015-02-23 11:23:53 -05:00
|
|
|
function fail(opts, errtype, msg) {
|
|
|
|
assert.throws(function() {
|
2015-12-26 13:48:54 -08:00
|
|
|
net.createConnection(opts, cb);
|
2015-05-19 13:00:06 +02:00
|
|
|
}, function(err) {
|
2015-02-23 11:23:53 -05:00
|
|
|
return err instanceof errtype && msg === err.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-07-05 00:34:58 +09:00
|
|
|
net.createConnection(tcpPort).on('connect', cb);
|
|
|
|
net.createConnection(tcpPort, 'localhost').on('connect', cb);
|
|
|
|
net.createConnection(tcpPort, cb);
|
|
|
|
net.createConnection(tcpPort, 'localhost', cb);
|
2015-02-23 11:23:53 -05:00
|
|
|
net.createConnection(tcpPort + '', 'localhost', cb);
|
|
|
|
net.createConnection({port: tcpPort + ''}).on('connect', cb);
|
|
|
|
net.createConnection({port: '0x' + tcpPort.toString(16)}, cb);
|
|
|
|
|
|
|
|
fail({
|
|
|
|
port: true
|
2015-11-09 18:10:49 -05:00
|
|
|
}, TypeError, '"port" option should be a number or string: true');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: false
|
2015-11-09 18:10:49 -05:00
|
|
|
}, TypeError, '"port" option should be a number or string: false');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: []
|
2015-11-09 18:10:49 -05:00
|
|
|
}, TypeError, '"port" option should be a number or string: ');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: {}
|
2015-11-09 18:10:49 -05:00
|
|
|
}, TypeError, '"port" option should be a number or string: [object Object]');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: null
|
2015-11-09 18:10:49 -05:00
|
|
|
}, TypeError, '"port" option should be a number or string: null');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: ''
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: ');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: ' '
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: ');
|
2013-09-04 18:57:43 -04:00
|
|
|
|
2015-02-23 11:23:53 -05:00
|
|
|
fail({
|
|
|
|
port: '0x'
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: 0x');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: '-0x1'
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: -0x1');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: NaN
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: NaN');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: Infinity
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: Infinity');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: -1
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: -1');
|
2015-02-23 11:23:53 -05:00
|
|
|
|
|
|
|
fail({
|
|
|
|
port: 65536
|
2015-11-09 18:10:49 -05:00
|
|
|
}, RangeError, '"port" option should be >= 0 and < 65536: 65536');
|
2011-06-21 23:53:02 +09:00
|
|
|
});
|
|
|
|
|
2015-02-23 11:23:53 -05:00
|
|
|
// Try connecting to random ports, but do so once the server is closed
|
|
|
|
server.on('close', function() {
|
|
|
|
function nop() {}
|
|
|
|
|
|
|
|
net.createConnection({port: 0}).on('error', nop);
|
|
|
|
net.createConnection({port: undefined}).on('error', nop);
|
2011-06-21 23:53:02 +09:00
|
|
|
});
|
|
|
|
|
2015-02-23 11:23:53 -05:00
|
|
|
process.on('exit', function() {
|
|
|
|
assert.equal(clientConnected, expectedConnections);
|
|
|
|
});
|