Skip to content

Commit

Permalink
fix: unskip tests now that issue is fixed in Deno
Browse files Browse the repository at this point in the history
  • Loading branch information
rclarey committed Mar 23, 2022
1 parent 4978ae3 commit bef9df6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
6 changes: 5 additions & 1 deletion client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class Client {
};
};

async connect(opts: Deno.ConnectOptions): Promise<Deno.Conn> {
async connect(opts: Deno.ConnectOptions): Promise<Deno.TcpConn> {
const remoteAddr = {
hostname: opts.hostname ?? "127.0.0.1",
port: opts.port,
Expand Down Expand Up @@ -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
Expand Down
152 changes: 79 additions & 73 deletions client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit bef9df6

Please sign in to comment.