Skip to content

Commit

Permalink
fix(net): set correct max size for Datagram (denoland#21611)
Browse files Browse the repository at this point in the history
  • Loading branch information
billywhizz authored Jul 10, 2024
1 parent ce7dc2b commit 26bf448
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ext/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
op_set_keepalive,
op_set_nodelay,
} from "ext:core/ops";
const UDP_DGRAM_MAXSIZE = 65507;

const {
Error,
Number,
Expand Down Expand Up @@ -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;
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 26bf448

Please sign in to comment.