|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const dgram = require('dgram'); |
| 5 | + |
| 6 | +// bindSync() binds synchronously and returns the resolved address, including |
| 7 | +// the OS-assigned ephemeral port when port is 0. |
| 8 | +{ |
| 9 | + const sock = dgram.createSocket('udp4'); |
| 10 | + const addr = sock.bindSync({ address: '127.0.0.1', port: 0 }); |
| 11 | + |
| 12 | + assert.strictEqual(addr.address, '127.0.0.1'); |
| 13 | + assert.strictEqual(addr.family, 'IPv4'); |
| 14 | + assert.strictEqual(typeof addr.port, 'number'); |
| 15 | + assert.ok(addr.port > 0); |
| 16 | + |
| 17 | + // address() is valid synchronously and matches the returned address. |
| 18 | + assert.deepStrictEqual(sock.address(), addr); |
| 19 | + |
| 20 | + // The 'listening' event still fires on the next tick. |
| 21 | + sock.on('listening', common.mustCall(() => sock.close())); |
| 22 | +} |
| 23 | + |
| 24 | +// Closing synchronously after bindSync() suppresses the deferred 'listening'. |
| 25 | +{ |
| 26 | + const sock = dgram.createSocket('udp4'); |
| 27 | + sock.bindSync({ port: 0 }); |
| 28 | + sock.on('listening', common.mustNotCall()); |
| 29 | + sock.close(); |
| 30 | +} |
| 31 | + |
| 32 | +// Defaults the address to the udp4 wildcard when omitted. |
| 33 | +{ |
| 34 | + const sock = dgram.createSocket('udp4'); |
| 35 | + const addr = sock.bindSync(); |
| 36 | + assert.strictEqual(addr.address, '0.0.0.0'); |
| 37 | + assert.ok(addr.port > 0); |
| 38 | + sock.close(); |
| 39 | +} |
| 40 | + |
| 41 | +// 'message' events still flow asynchronously after a synchronous bind. |
| 42 | +{ |
| 43 | + const receiver = dgram.createSocket('udp4'); |
| 44 | + const addr = receiver.bindSync({ address: '127.0.0.1', port: 0 }); |
| 45 | + |
| 46 | + receiver.on('message', common.mustCall((msg) => { |
| 47 | + assert.strictEqual(msg.toString(), 'hello'); |
| 48 | + receiver.close(); |
| 49 | + })); |
| 50 | + |
| 51 | + const sender = dgram.createSocket('udp4'); |
| 52 | + sender.send('hello', addr.port, '127.0.0.1', common.mustCall(() => { |
| 53 | + sender.close(); |
| 54 | + })); |
| 55 | +} |
| 56 | + |
| 57 | +// Throws synchronously on EADDRINUSE. |
| 58 | +{ |
| 59 | + const first = dgram.createSocket('udp4'); |
| 60 | + const addr = first.bindSync({ address: '127.0.0.1', port: 0 }); |
| 61 | + |
| 62 | + const second = dgram.createSocket('udp4'); |
| 63 | + assert.throws(() => { |
| 64 | + second.bindSync({ address: '127.0.0.1', port: addr.port }); |
| 65 | + }, { |
| 66 | + code: 'EADDRINUSE', |
| 67 | + syscall: 'bind', |
| 68 | + }); |
| 69 | + |
| 70 | + first.close(); |
| 71 | + second.close(); |
| 72 | +} |
| 73 | + |
| 74 | +// Throws synchronously on a non-numeric address (no DNS resolution). |
| 75 | +{ |
| 76 | + const sock = dgram.createSocket('udp4'); |
| 77 | + assert.throws(() => { |
| 78 | + sock.bindSync({ address: 'localhost', port: 0 }); |
| 79 | + }, { |
| 80 | + code: 'ERR_INVALID_ARG_VALUE', |
| 81 | + name: 'TypeError', |
| 82 | + }); |
| 83 | + sock.close(); |
| 84 | +} |
| 85 | + |
| 86 | +// Rejects a non-string address. |
| 87 | +{ |
| 88 | + const sock = dgram.createSocket('udp4'); |
| 89 | + assert.throws(() => sock.bindSync({ address: 12345 }), { |
| 90 | + code: 'ERR_INVALID_ARG_TYPE', |
| 91 | + }); |
| 92 | + sock.close(); |
| 93 | +} |
| 94 | + |
| 95 | +// A rejected argument leaves the socket unbound and reusable. |
| 96 | +{ |
| 97 | + const sock = dgram.createSocket('udp4'); |
| 98 | + assert.throws(() => sock.bindSync({ port: -1 }), { |
| 99 | + code: 'ERR_SOCKET_BAD_PORT', |
| 100 | + }); |
| 101 | + const addr = sock.bindSync({ port: 0 }); |
| 102 | + assert.ok(addr.port > 0); |
| 103 | + sock.close(); |
| 104 | +} |
| 105 | + |
| 106 | +// Throws when already bound. |
| 107 | +{ |
| 108 | + const sock = dgram.createSocket('udp4'); |
| 109 | + sock.bindSync({ port: 0 }); |
| 110 | + assert.throws(() => sock.bindSync({ port: 0 }), { |
| 111 | + code: 'ERR_SOCKET_ALREADY_BOUND', |
| 112 | + }); |
| 113 | + sock.close(); |
| 114 | +} |
| 115 | + |
| 116 | +// Rejects a non-object options argument. |
| 117 | +{ |
| 118 | + const sock = dgram.createSocket('udp4'); |
| 119 | + assert.throws(() => sock.bindSync(0), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 120 | + sock.close(); |
| 121 | +} |
| 122 | + |
| 123 | +// udp6 wildcard default. |
| 124 | +if (common.hasIPv6) { |
| 125 | + const sock = dgram.createSocket('udp6'); |
| 126 | + const addr = sock.bindSync(); |
| 127 | + assert.strictEqual(addr.address, '::'); |
| 128 | + assert.strictEqual(addr.family, 'IPv6'); |
| 129 | + assert.ok(addr.port > 0); |
| 130 | + sock.close(); |
| 131 | +} |
| 132 | + |
| 133 | +// udp6 loopback with an OS-assigned ephemeral port, and async 'message' flow. |
| 134 | +if (common.hasIPv6) { |
| 135 | + const receiver = dgram.createSocket('udp6'); |
| 136 | + const addr = receiver.bindSync({ address: '::1', port: 0 }); |
| 137 | + |
| 138 | + assert.strictEqual(addr.address, '::1'); |
| 139 | + assert.strictEqual(addr.family, 'IPv6'); |
| 140 | + assert.ok(addr.port > 0); |
| 141 | + assert.deepStrictEqual(receiver.address(), addr); |
| 142 | + |
| 143 | + receiver.on('message', common.mustCall((msg) => { |
| 144 | + assert.strictEqual(msg.toString(), 'hello'); |
| 145 | + receiver.close(); |
| 146 | + })); |
| 147 | + |
| 148 | + const sender = dgram.createSocket('udp6'); |
| 149 | + sender.send('hello', addr.port, '::1', common.mustCall(() => { |
| 150 | + sender.close(); |
| 151 | + })); |
| 152 | +} |
| 153 | + |
| 154 | +// A zone-indexed (scoped) IPv6 literal is accepted as a numeric IP; no DNS |
| 155 | +// resolution occurs. Interface names are platform-specific, so this binds the |
| 156 | +// scoped loopback only where the interface name is known (Linux: 'lo'). |
| 157 | +if (common.hasIPv6 && process.platform === 'linux') { |
| 158 | + const sock = dgram.createSocket('udp6'); |
| 159 | + const addr = sock.bindSync({ address: '::1%lo', port: 0 }); |
| 160 | + assert.strictEqual(addr.address, '::1'); |
| 161 | + assert.strictEqual(addr.family, 'IPv6'); |
| 162 | + assert.ok(addr.port > 0); |
| 163 | + sock.close(); |
| 164 | +} |
| 165 | + |
| 166 | +// The ipv6Only flag is honored by the synchronous bind. |
| 167 | +if (common.hasIPv6) { |
| 168 | + const sock = dgram.createSocket({ type: 'udp6', ipv6Only: true }); |
| 169 | + const addr = sock.bindSync({ address: '::', port: 0 }); |
| 170 | + assert.strictEqual(addr.family, 'IPv6'); |
| 171 | + sock.close(); |
| 172 | +} |
0 commit comments