From bef9df692e5de5275279a5db27b2c391d889da7d Mon Sep 17 00:00:00 2001 From: rclarey Date: Wed, 23 Mar 2022 16:05:49 +0900 Subject: [PATCH] fix: unskip tests now that issue is fixed in Deno --- client.ts | 6 +- client_test.ts | 152 +++++++++++++++++++++++++------------------------ 2 files changed, 84 insertions(+), 74 deletions(-) diff --git a/client.ts b/client.ts index 50c4fe8..91e7ea8 100644 --- a/client.ts +++ b/client.ts @@ -237,7 +237,7 @@ export class Client { }; }; - async connect(opts: Deno.ConnectOptions): Promise { + async connect(opts: Deno.ConnectOptions): Promise { const remoteAddr = { hostname: opts.hostname ?? "127.0.0.1", port: opts.port, @@ -362,6 +362,10 @@ export class Client { return udpConn.send(msg, proxyInfo!.addr); }, async receive(p?: Uint8Array): Promise<[Uint8Array, Deno.Addr]> { + if (!proxyInfo) { + await isReady; + } + const buff = new Uint8Array(p ? p.length + 1024 : 2048); const [res] = await udpConn.receive(buff); // if first two reserved bytes are not zero, or the fragment value is diff --git a/client_test.ts b/client_test.ts index 35d5c6f..6a004ed 100644 --- a/client_test.ts +++ b/client_test.ts @@ -548,79 +548,85 @@ Deno.test("listenDatagram() - send() encodes header correctly", async () => { close(); }); -// FIXME: unskip these when bug in Deno is fixed https://github.com/denoland/deno/issues/13729 -// Deno.test("listenDatagram() - receive() ignores datagrams with non-zero reserved bytes", async () => { -// const server = mockServer(); -// let udpServer: Deno.DatagramConn; -// let buff: Uint8Array; -// (async () => { -// const addr = { -// ...listenOptions, -// hostname: "127.0.0.1", -// }; -// udpServer = Deno.listenDatagram({ port: 2080, transport: "udp" }); -// // reserve bytes are 01 -// const nonZeroReserve = new Uint8Array(12); -// nonZeroReserve[1] = 1; -// await udpServer.send(nonZeroReserve, addr); -// buff = crypto.getRandomValues(new Uint8Array(8)); -// // reserve bytes are 00 -// udpServer.send( -// Uint8Array.from([0, 0, 0, ...ip4Address.serialized, ...buff]), -// addr, -// ); -// })(); - -// const client = new Client(config); -// const conn = client.listenDatagram(listenOptions); -// const recv = await conn.receive(); -// assertEquals(recv[0], buff!); -// assertEquals(recv[1], { -// hostname: ip4Address.hostname, -// port: ip4Address.port, -// transport: "udp", -// }); - -// udpServer!.close(); -// server.close(); -// }); - -// Deno.test("listenDatagram() - receive() ignores datagrams with non-zero fragment", async () => { -// const server = mockServer(); -// let udpServer: Deno.DatagramConn; -// let buff: Uint8Array; -// (async () => { -// const addr = { -// ...listenOptions, -// hostname: "127.0.0.1", -// }; -// udpServer = Deno.listenDatagram({ port: 2080, transport: "udp" }); -// // fragment byte is 1 -// const nonZeroReserve = new Uint8Array(12); -// nonZeroReserve[2] = 1; -// await udpServer.send(nonZeroReserve, addr); -// buff = crypto.getRandomValues(new Uint8Array(8)); -// // fragment byte is 0 -// udpServer.send( -// Uint8Array.from([0, 0, 0, ...ip4Address.serialized, ...buff]), -// addr, -// ); -// })(); - -// const client = new Client(config); -// const conn = client.listenDatagram(listenOptions); -// const recv = await conn.receive(); -// assertEquals(recv[0], buff!); -// assertEquals(recv[1], { -// hostname: ip4Address.hostname, -// port: ip4Address.port, -// transport: "udp", -// }); - -// conn.close(); -// udpServer!.close(); -// server.close(); -// }); +Deno.test( + "listenDatagram() - receive() ignores datagrams with non-zero reserved bytes", + async () => { + const server = mockServer(); + let udpServer: Deno.DatagramConn; + let buff: Uint8Array; + (async () => { + const addr = { + ...listenOptions, + hostname: "127.0.0.1", + }; + udpServer = Deno.listenDatagram({ port: 2080, transport: "udp" }); + // reserve bytes are 01 + const nonZeroReserve = new Uint8Array(12); + nonZeroReserve[1] = 1; + await udpServer.send(nonZeroReserve, addr); + buff = crypto.getRandomValues(new Uint8Array(8)); + // reserve bytes are 00 + udpServer.send( + Uint8Array.from([0, 0, 0, ...ip4Address.serialized, ...buff]), + addr, + ); + })(); + + const client = new Client(config); + const conn = client.listenDatagram(listenOptions); + const recv = await conn.receive(); + assertEquals(recv[0], buff!); + assertEquals(recv[1], { + hostname: ip4Address.hostname, + port: ip4Address.port, + transport: "udp", + }); + + conn.close(); + udpServer!.close(); + server.close(); + }, +); + +Deno.test( + "listenDatagram() - receive() ignores datagrams with non-zero fragment", + async () => { + const server = mockServer(); + let udpServer: Deno.DatagramConn; + let buff: Uint8Array; + (async () => { + const addr = { + ...listenOptions, + hostname: "127.0.0.1", + }; + udpServer = Deno.listenDatagram({ port: 2080, transport: "udp" }); + // fragment byte is 1 + const nonZeroReserve = new Uint8Array(12); + nonZeroReserve[2] = 1; + await udpServer.send(nonZeroReserve, addr); + buff = crypto.getRandomValues(new Uint8Array(8)); + // fragment byte is 0 + udpServer.send( + Uint8Array.from([0, 0, 0, ...ip4Address.serialized, ...buff]), + addr, + ); + })(); + + const client = new Client(config); + const conn = client.listenDatagram(listenOptions); + const recv = await conn.receive(); + assertEquals(recv[0], buff!); + assertEquals(recv[1], { + hostname: ip4Address.hostname, + port: ip4Address.port, + transport: "udp", + }); + + conn.close(); + udpServer!.close(); + server.close(); + }, +); Deno.test("listenDatagram() - receive() decodes header correctly", async () => { const server = mockServer({