|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const net = require('net'); |
| 5 | +const util = require('util'); |
| 6 | + |
| 7 | +function close() { this.close(); } |
| 8 | + |
| 9 | +function listenError(literals, ...values) { |
| 10 | + let result = literals[0]; |
| 11 | + for (const [i, value] of values.entries()) { |
| 12 | + const str = util.inspect(value); |
| 13 | + // Need to escape special characters. |
| 14 | + result += str.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&'); |
| 15 | + result += literals[i + 1]; |
| 16 | + } |
| 17 | + return new RegExp(`Error: Invalid listen argument: ${result}`); |
| 18 | +} |
| 19 | + |
| 20 | +{ |
| 21 | + // Test listen() |
| 22 | + net.createServer().listen().on('listening', common.mustCall(close)); |
| 23 | + // Test listen(cb) |
| 24 | + net.createServer().listen(common.mustCall(close)); |
| 25 | +} |
| 26 | + |
| 27 | +// Test listen(port, cb) and listen({port: port}, cb) combinations |
| 28 | +const listenOnPort = [ |
| 29 | + (port, cb) => net.createServer().listen({port}, cb), |
| 30 | + (port, cb) => net.createServer().listen(port, cb) |
| 31 | +]; |
| 32 | + |
| 33 | +{ |
| 34 | + const portError = /^RangeError: "port" argument must be >= 0 and < 65536$/; |
| 35 | + for (const listen of listenOnPort) { |
| 36 | + // Arbitrary unused ports |
| 37 | + listen('0', common.mustCall(close)); |
| 38 | + listen(0, common.mustCall(close)); |
| 39 | + listen(undefined, common.mustCall(close)); |
| 40 | + // Test invalid ports |
| 41 | + assert.throws(() => listen(-1, common.mustNotCall()), portError); |
| 42 | + assert.throws(() => listen(NaN, common.mustNotCall()), portError); |
| 43 | + assert.throws(() => listen(123.456, common.mustNotCall()), portError); |
| 44 | + assert.throws(() => listen(65536, common.mustNotCall()), portError); |
| 45 | + assert.throws(() => listen(1 / 0, common.mustNotCall()), portError); |
| 46 | + assert.throws(() => listen(-1 / 0, common.mustNotCall()), portError); |
| 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 | + }, portError); |
| 53 | +} |
| 54 | + |
| 55 | +{ |
| 56 | + function shouldFailToListen(options, optionsInMessage) { |
| 57 | + // Plain arguments get normalized into an object before |
| 58 | + // formatted in message, options objects don't. |
| 59 | + if (arguments.length === 1) { |
| 60 | + optionsInMessage = options; |
| 61 | + } |
| 62 | + const block = () => { |
| 63 | + net.createServer().listen(options, common.mustNotCall()); |
| 64 | + }; |
| 65 | + assert.throws(block, listenError`${optionsInMessage}`, |
| 66 | + `expect listen(${util.inspect(options)}) to throw`); |
| 67 | + } |
| 68 | + |
| 69 | + shouldFailToListen(null, { port: null }); |
| 70 | + shouldFailToListen({ port: null }); |
| 71 | + shouldFailToListen(false, { port: false }); |
| 72 | + shouldFailToListen({ port: false }); |
| 73 | + shouldFailToListen(true, { port: true }); |
| 74 | + shouldFailToListen({ port: true }); |
| 75 | + // Invalid fd as listen(handle) |
| 76 | + shouldFailToListen({ fd: -1 }); |
| 77 | + // Invalid path in listen(options) |
| 78 | + shouldFailToListen({ path: -1 }); |
| 79 | + // Host without port |
| 80 | + shouldFailToListen({ host: 'localhost' }); |
| 81 | +} |
0 commit comments