diff options
author | Andrew Johnston <apjohnsto@gmail.com> | 2024-07-09 20:30:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-10 03:30:22 +0000 |
commit | 26bf4480ff5b6f72b40997618d4020127eb5b0ba (patch) | |
tree | 1f18679954c7d7f9389b0760f73ada7c913fd871 | |
parent | ce7dc2be92499f15b4b0315bfca3ee9d61fc3c5e (diff) |
fix(net): set correct max size for Datagram (#21611)
-rw-r--r-- | ext/net/01_net.js | 4 | ||||
-rw-r--r-- | tests/unit/net_test.ts | 64 |
2 files changed, 67 insertions, 1 deletions
diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 0b0dec4d1..517ab127e 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -28,6 +28,8 @@ import { op_set_keepalive, op_set_nodelay, } from "ext:core/ops"; +const UDP_DGRAM_MAXSIZE = 65507; + const { Error, Number, @@ -378,7 +380,7 @@ class DatagramConn { #unref = false; #promise = null; - constructor(rid, addr, bufSize = 1024) { + constructor(rid, addr, bufSize = UDP_DGRAM_MAXSIZE) { this.#rid = rid; this.#addr = addr; this.bufSize = bufSize; diff --git a/tests/unit/net_test.ts b/tests/unit/net_test.ts index dff3cc31f..ddc55b8c4 100644 --- a/tests/unit/net_test.ts +++ b/tests/unit/net_test.ts @@ -386,6 +386,70 @@ Deno.test( ); Deno.test( + { permissions: { net: true } }, + async function netUdpSendReceiveTestSizeLimits() { + // Ensure payload being sent is within UDP limit, which seems to be 65507 + // bytes + const alice = Deno.listenDatagram({ + port: listenPort, + transport: "udp", + hostname: "127.0.0.1", + }); + // wrap this in a try/catch so other tests can continue if we fail + // if we don't close here then listening on future tests fails + try { + assert(alice.addr.transport === "udp"); + assertEquals(alice.addr.port, listenPort); + assertEquals(alice.addr.hostname, "127.0.0.1"); + } catch (err) { + alice.close(); + throw err; + } + + const bob = Deno.listenDatagram({ + port: listenPort2, + transport: "udp", + hostname: "127.0.0.1", + }); + try { + assert(bob.addr.transport === "udp"); + assertEquals(bob.addr.port, listenPort2); + assertEquals(bob.addr.hostname, "127.0.0.1"); + } catch (err) { + bob.close(); + throw err; + } + + const sizes = [0, 1, 2, 256, 1024, 4096, 16384, 65506, 65507, 65508, 65536]; + const rx = /.+ \(os error \d+\)/; + + for (const size of sizes) { + const tosend = new Uint8Array(size); + let byteLength = 0; + try { + byteLength = await alice.send(tosend, bob.addr); + } catch (err) { + // Note: we have to do the test this way as different OS's have + // different UDP size limits enabled, so we will just ensure if + // an error is thrown it is the one we are expecting. + assert(err.message.match(rx)); + alice.close(); + bob.close(); + return; + } + assertEquals(byteLength, size); + const [recvd, remote] = await bob.receive(); + assert(remote.transport === "udp"); + assertEquals(remote.port, listenPort); + assertEquals(recvd.length, size); + } + + alice.close(); + bob.close(); + }, +); + +Deno.test( { permissions: { net: true }, ignore: true }, async function netUdpSendReceiveBroadcast() { // Must bind sender to an address that can send to the broadcast address on MacOS. |