fix(net): set correct max size for Datagram (#21611)

This commit is contained in:
Andrew Johnston 2024-07-09 20:30:22 -07:00 committed by GitHub
parent ce7dc2be92
commit 26bf4480ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 1 deletions

View file

@ -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;

View file

@ -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() {