|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const net = require('net'); |
| 6 | +const fs = require('fs'); |
| 7 | +const uv = process.binding('uv'); |
| 8 | +const TCP = process.binding('tcp_wrap').TCP; |
| 9 | +const Pipe = process.binding('pipe_wrap').Pipe; |
| 10 | + |
| 11 | +common.refreshTmpDir(); |
| 12 | + |
| 13 | +function closeServer() { |
| 14 | + return common.mustCall(function() { |
| 15 | + this.close(); |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +// server.listen(pipe) creates a new pipe wrap, |
| 20 | +// so server.close() doesn't actually unlink this existing pipe. |
| 21 | +// It needs to be unlinked separately via handle.close() |
| 22 | +function closePipeServer(handle) { |
| 23 | + return common.mustCall(function() { |
| 24 | + this.close(); |
| 25 | + handle.close(); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +let counter = 0; |
| 30 | + |
| 31 | +// Avoid conflict with listen-path |
| 32 | +function randomPipePath() { |
| 33 | + return common.PIPE + '-listen-handle-' + (counter++); |
| 34 | +} |
| 35 | + |
| 36 | +function randomHandle(type) { |
| 37 | + let handle, errno, handleName; |
| 38 | + if (type === 'tcp') { |
| 39 | + handle = new TCP(); |
| 40 | + errno = handle.bind('0.0.0.0', 0); |
| 41 | + handleName = 'arbitrary tcp port'; |
| 42 | + } else { |
| 43 | + const path = randomPipePath(); |
| 44 | + handle = new Pipe(); |
| 45 | + errno = handle.bind(path); |
| 46 | + handleName = `pipe ${path}`; |
| 47 | + } |
| 48 | + |
| 49 | + if (errno < 0) { // uv.errname requires err < 0 |
| 50 | + assert(errno >= 0, `unable to bind ${handleName}: ${uv.errname(errno)}`); |
| 51 | + } |
| 52 | + if (!common.isWindows) { // fd doesn't work on windows |
| 53 | + // err >= 0 but fd = -1, should not happen |
| 54 | + assert.notStrictEqual(handle.fd, -1, |
| 55 | + `Bound ${handleName} has fd -1 and errno ${errno}`); |
| 56 | + } |
| 57 | + return handle; |
| 58 | +} |
| 59 | + |
| 60 | +// Not a public API, used by child_process |
| 61 | +{ |
| 62 | + // Test listen(tcp) |
| 63 | + net.createServer() |
| 64 | + .listen(randomHandle('tcp')) |
| 65 | + .on('listening', closeServer()); |
| 66 | + // Test listen(tcp, cb) |
| 67 | + net.createServer() |
| 68 | + .listen(randomHandle('tcp'), closeServer()); |
| 69 | +} |
| 70 | + |
| 71 | +function randomPipes(number) { |
| 72 | + const arr = []; |
| 73 | + for (let i = 0; i < number; ++i) { |
| 74 | + arr.push(randomHandle('pipe')); |
| 75 | + } |
| 76 | + return arr; |
| 77 | +} |
| 78 | + |
| 79 | +// Not a public API, used by child_process |
| 80 | +if (!common.isWindows) { // Windows doesn't support {fd: <n>} |
| 81 | + const handles = randomPipes(2); // generate pipes in advance |
| 82 | + // Test listen(pipe) |
| 83 | + net.createServer() |
| 84 | + .listen(handles[0]) |
| 85 | + .on('listening', closePipeServer(handles[0])); |
| 86 | + // Test listen(pipe, cb) |
| 87 | + net.createServer() |
| 88 | + .listen(handles[1], closePipeServer(handles[1])); |
| 89 | +} |
| 90 | + |
| 91 | +{ |
| 92 | + // Test listen({handle: tcp}, cb) |
| 93 | + net.createServer() |
| 94 | + .listen({handle: randomHandle('tcp')}, closeServer()); |
| 95 | + // Test listen({handle: tcp}) |
| 96 | + net.createServer() |
| 97 | + .listen({handle: randomHandle('tcp')}) |
| 98 | + .on('listening', closeServer()); |
| 99 | + // Test listen({_handle: tcp}, cb) |
| 100 | + net.createServer() |
| 101 | + .listen({_handle: randomHandle('tcp')}, closeServer()); |
| 102 | + // Test listen({_handle: tcp}) |
| 103 | + net.createServer() |
| 104 | + .listen({_handle: randomHandle('tcp')}) |
| 105 | + .on('listening', closeServer()); |
| 106 | +} |
| 107 | + |
| 108 | +if (!common.isWindows) { // Windows doesn't support {fd: <n>} |
| 109 | + // Test listen({fd: tcp.fd}, cb) |
| 110 | + net.createServer() |
| 111 | + .listen({fd: randomHandle('tcp').fd}, closeServer()); |
| 112 | + // Test listen({fd: tcp.fd}) |
| 113 | + net.createServer() |
| 114 | + .listen({fd: randomHandle('tcp').fd}) |
| 115 | + .on('listening', closeServer()); |
| 116 | +} |
| 117 | + |
| 118 | +if (!common.isWindows) { // Windows doesn't support {fd: <n>} |
| 119 | + const handles = randomPipes(6); // generate pipes in advance |
| 120 | + // Test listen({handle: pipe}, cb) |
| 121 | + net.createServer() |
| 122 | + .listen({handle: handles[0]}, closePipeServer(handles[0])); |
| 123 | + // Test listen({handle: pipe}) |
| 124 | + net.createServer() |
| 125 | + .listen({handle: handles[1]}) |
| 126 | + .on('listening', closePipeServer(handles[1])); |
| 127 | + // Test listen({_handle: pipe}, cb) |
| 128 | + net.createServer() |
| 129 | + .listen({_handle: handles[2]}, closePipeServer(handles[2])); |
| 130 | + // Test listen({_handle: pipe}) |
| 131 | + net.createServer() |
| 132 | + .listen({_handle: handles[3]}) |
| 133 | + .on('listening', closePipeServer(handles[3])); |
| 134 | + // Test listen({fd: pipe.fd}, cb) |
| 135 | + net.createServer() |
| 136 | + .listen({fd: handles[4].fd}, closePipeServer(handles[4])); |
| 137 | + // Test listen({fd: pipe.fd}) |
| 138 | + net.createServer() |
| 139 | + .listen({fd: handles[5].fd}) |
| 140 | + .on('listening', closePipeServer(handles[5])); |
| 141 | +} |
| 142 | + |
| 143 | +if (!common.isWindows) { // Windows doesn't support {fd: <n>} |
| 144 | + // Test invalid fd |
| 145 | + const fd = fs.openSync(__filename, 'r'); |
| 146 | + net.createServer() |
| 147 | + .listen({fd: fd}, common.mustNotCall()) |
| 148 | + .on('error', common.mustCall(function(err) { |
| 149 | + assert.strictEqual(err + '', 'Error: listen EINVAL'); |
| 150 | + this.close(); |
| 151 | + })); |
| 152 | +} |
0 commit comments