From 26bf4480ff5b6f72b40997618d4020127eb5b0ba Mon Sep 17 00:00:00 2001 From: Andrew Johnston Date: Tue, 9 Jul 2024 20:30:22 -0700 Subject: fix(net): set correct max size for Datagram (#21611) --- tests/unit/net_test.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests') 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 @@ -385,6 +385,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() { -- cgit v1.2.3