From 6c09e023049631ea8b168aec53db4f652a1ec0aa Mon Sep 17 00:00:00 2001 From: Michael Busby Date: Mon, 29 Nov 2021 04:14:46 -0600 Subject: feat(ext/net): enable sending to broadcast address (#12860) You can now send UDP datagrams to the broadcast address. --- cli/tests/unit/net_test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'cli/tests/unit') diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts index 7ea1b8bcf..7efc7e689 100644 --- a/cli/tests/unit/net_test.ts +++ b/cli/tests/unit/net_test.ts @@ -306,6 +306,44 @@ Deno.test( }, ); +Deno.test( + { permissions: { net: true } }, + async function netUdpSendReceiveBroadcast() { + // Must bind sender to an address that can send to the broadcast address on MacOS. + // Macos will give us error 49 when sending the broadcast packet if we omit hostname here. + const alice = Deno.listenDatagram({ + port: 3500, + transport: "udp", + hostname: "0.0.0.0", + }); + + const bob = Deno.listenDatagram({ + port: 4501, + transport: "udp", + hostname: "0.0.0.0", + }); + assert(bob.addr.transport === "udp"); + assertEquals(bob.addr.port, 4501); + assertEquals(bob.addr.hostname, "0.0.0.0"); + + const broadcastAddr = { ...bob.addr, hostname: "255.255.255.255" }; + + const sent = new Uint8Array([1, 2, 3]); + const byteLength = await alice.send(sent, broadcastAddr); + + assertEquals(byteLength, 3); + const [recvd, remote] = await bob.receive(); + assert(remote.transport === "udp"); + assertEquals(remote.port, 3500); + assertEquals(recvd.length, 3); + assertEquals(1, recvd[0]); + assertEquals(2, recvd[1]); + assertEquals(3, recvd[2]); + alice.close(); + bob.close(); + }, +); + Deno.test( { permissions: { net: true } }, async function netUdpConcurrentSendReceive() { -- cgit v1.2.3