2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-03-10 11:38:02 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-09-27 06:22:32 -04:00
|
|
|
assertNotEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2020-11-26 11:22:36 -05:00
|
|
|
deferred,
|
2021-06-24 21:44:14 -04:00
|
|
|
delay,
|
2022-03-22 23:04:20 -04:00
|
|
|
execCode,
|
2022-12-02 12:41:52 -05:00
|
|
|
execCode2,
|
2020-03-10 11:38:02 -04:00
|
|
|
} from "./test_util.ts";
|
2022-10-23 18:45:45 -04:00
|
|
|
import { join } from "../../../test_util/std/path/mod.ts";
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2021-08-16 10:15:00 -04:00
|
|
|
let isCI: boolean;
|
|
|
|
try {
|
|
|
|
isCI = Deno.env.get("CI") !== undefined;
|
|
|
|
} catch {
|
|
|
|
isCI = true;
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, function netTcpListenClose() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: 3500 });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(listener.addr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(listener.addr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(listener.addr.port, 3500);
|
2020-06-08 12:24:51 -04:00
|
|
|
assertNotEquals(listener.rid, 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function netUdpListenClose() {
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
2020-03-04 11:31:14 -05:00
|
|
|
hostname: "127.0.0.1",
|
2020-05-11 14:49:19 -04:00
|
|
|
port: 3500,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "udp",
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(socket.addr.transport === "udp");
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(socket.addr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(socket.addr.port, 3500);
|
2020-03-04 11:31:14 -05:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-15 20:36:51 -05:00
|
|
|
|
2022-10-23 18:45:45 -04:00
|
|
|
function tmpUnixSocketPath(): string {
|
|
|
|
const folder = Deno.makeTempDirSync();
|
|
|
|
return join(folder, "socket");
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function netUnixListenClose() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-03-23 18:02:51 -04:00
|
|
|
const socket = Deno.listen({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unix",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(socket.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function netUnixPacketListenClose() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unixpacket",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unixpacket");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(socket.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: false },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function netUnixListenWritePermission() {
|
2020-11-27 15:02:25 -05:00
|
|
|
assertThrows(() => {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-11-27 13:43:39 -05:00
|
|
|
const socket = Deno.listen({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unix",
|
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unix");
|
|
|
|
assertEquals(socket.addr.path, filePath);
|
|
|
|
socket.close();
|
2020-11-27 15:02:25 -05:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-11-27 13:43:39 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: false },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function netUnixPacketListenWritePermission() {
|
2020-11-27 15:02:25 -05:00
|
|
|
assertThrows(() => {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-11-27 13:43:39 -05:00
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unixpacket");
|
|
|
|
assertEquals(socket.addr.path, filePath);
|
|
|
|
socket.close();
|
2020-11-27 15:02:25 -05:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-11-27 13:43:39 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netTcpCloseWhileAccept() {
|
2020-03-24 12:24:58 -04:00
|
|
|
const listener = Deno.listen({ port: 4501 });
|
2020-03-04 11:31:14 -05:00
|
|
|
const p = listener.accept();
|
|
|
|
listener.close();
|
2021-10-09 16:37:19 -04:00
|
|
|
// TODO(piscisaureus): the error type should be `Interrupted` here, which
|
|
|
|
// gets thrown, but then ext/net catches it and rethrows `BadResource`.
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-10-09 16:37:19 -04:00
|
|
|
() => p,
|
2020-06-24 18:57:08 -04:00
|
|
|
Deno.errors.BadResource,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Listener has been closed",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixCloseWhileAccept() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-03-23 18:02:51 -04:00
|
|
|
const listener = Deno.listen({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unix",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
const p = listener.accept();
|
|
|
|
listener.close();
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-10-09 16:37:19 -04:00
|
|
|
() => p,
|
2021-12-14 17:27:04 -05:00
|
|
|
Deno.errors.BadResource,
|
|
|
|
"Listener has been closed",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netTcpConcurrentAccept() {
|
2022-02-16 10:51:32 -05:00
|
|
|
const listener = Deno.listen({ port: 4510 });
|
2020-03-04 11:31:14 -05:00
|
|
|
let acceptErrCount = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const checkErr = (e: Error) => {
|
2020-03-04 11:31:14 -05:00
|
|
|
if (e.message === "Listener has been closed") {
|
|
|
|
assertEquals(acceptErrCount, 1);
|
|
|
|
} else if (e.message === "Another accept task is ongoing") {
|
|
|
|
acceptErrCount++;
|
|
|
|
} else {
|
|
|
|
throw new Error("Unexpected error message");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = listener.accept().catch(checkErr);
|
|
|
|
const p1 = listener.accept().catch(checkErr);
|
|
|
|
await Promise.race([p, p1]);
|
|
|
|
listener.close();
|
|
|
|
await Promise.all([p, p1]);
|
|
|
|
assertEquals(acceptErrCount, 1);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-15 20:36:51 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-09 16:37:19 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixConcurrentAccept() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-04-28 12:37:59 -04:00
|
|
|
const listener = Deno.listen({ transport: "unix", path: filePath });
|
2020-03-23 18:02:51 -04:00
|
|
|
let acceptErrCount = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const checkErr = (e: Error) => {
|
2021-12-14 17:27:04 -05:00
|
|
|
if (e.message === "Listener has been closed") {
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(acceptErrCount, 1);
|
2021-10-09 16:37:19 -04:00
|
|
|
} else if (e instanceof Deno.errors.Busy) { // "Listener already in use"
|
2020-03-23 18:02:51 -04:00
|
|
|
acceptErrCount++;
|
|
|
|
} else {
|
2021-10-09 16:37:19 -04:00
|
|
|
throw e;
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = listener.accept().catch(checkErr);
|
|
|
|
const p1 = listener.accept().catch(checkErr);
|
|
|
|
await Promise.race([p, p1]);
|
|
|
|
listener.close();
|
2021-10-09 16:37:19 -04:00
|
|
|
await Promise.all([p, p1]);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(acceptErrCount, 1);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function netTcpDialListen() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const listener = Deno.listen({ port: 3500 });
|
2019-04-21 16:40:10 -04:00
|
|
|
listener.accept().then(
|
2021-08-05 07:08:58 -04:00
|
|
|
async (conn) => {
|
2019-08-27 11:35:32 -04:00
|
|
|
assert(conn.remoteAddr != null);
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.localAddr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.localAddr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(conn.localAddr.port, 3500);
|
2019-04-21 16:40:10 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2020-03-24 12:24:58 -04:00
|
|
|
|
2020-05-11 14:49:19 -04:00
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(conn.remoteAddr.port, 3500);
|
2019-08-27 11:35:32 -04:00
|
|
|
assert(conn.localAddr != null);
|
2018-10-03 23:58:29 -04:00
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(3, readResult);
|
2019-05-01 16:58:09 -04:00
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
|
2022-01-31 10:36:54 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function netTcpSetNoDelay() {
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
listener.accept().then(
|
|
|
|
async (conn) => {
|
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr.transport === "tcp");
|
|
|
|
assertEquals(conn.localAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.localAddr.port, 3500);
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
conn.setNoDelay(true);
|
|
|
|
assert(conn.remoteAddr.transport === "tcp");
|
|
|
|
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.remoteAddr.port, 3500);
|
|
|
|
assert(conn.localAddr != null);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
|
|
|
assert(readResult !== null);
|
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
|
|
|
assertEquals(readResult2, null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({ permissions: { net: true } }, async function netTcpSetKeepAlive() {
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
listener.accept().then(
|
|
|
|
async (conn) => {
|
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr.transport === "tcp");
|
|
|
|
assertEquals(conn.localAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.localAddr.port, 3500);
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
conn.setKeepAlive(true);
|
|
|
|
assert(conn.remoteAddr.transport === "tcp");
|
|
|
|
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.remoteAddr.port, 3500);
|
|
|
|
assert(conn.localAddr != null);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
|
|
|
assert(readResult !== null);
|
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
|
|
|
assertEquals(readResult2, null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixDialListen() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-04-28 12:37:59 -04:00
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
listener.accept().then(
|
2021-08-05 07:08:58 -04:00
|
|
|
async (conn) => {
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(conn.localAddr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
2020-04-28 12:37:59 -04:00
|
|
|
const conn = await Deno.connect({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(conn.remoteAddr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUdpSendReceive() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const alice = Deno.listenDatagram({ port: 3500, transport: "udp" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(alice.addr.transport === "udp");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(alice.addr.port, 3500);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(alice.addr.hostname, "127.0.0.1");
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-04-28 15:46:39 -04:00
|
|
|
const bob = Deno.listenDatagram({ port: 4501, transport: "udp" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(bob.addr.transport === "udp");
|
2020-03-24 12:24:58 -04:00
|
|
|
assertEquals(bob.addr.port, 4501);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(bob.addr.hostname, "127.0.0.1");
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
2020-06-13 10:14:31 -04:00
|
|
|
const byteLength = await alice.send(sent, bob.addr);
|
|
|
|
|
|
|
|
assertEquals(byteLength, 3);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const [recvd, remote] = await bob.receive();
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(remote.transport === "udp");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(remote.port, 3500);
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
alice.close();
|
|
|
|
bob.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2021-11-29 05:14:46 -05:00
|
|
|
Deno.test(
|
2022-03-20 17:46:39 -04:00
|
|
|
{ permissions: { net: true }, ignore: true },
|
2021-11-29 05:14:46 -05:00
|
|
|
async function netUdpSendReceiveBroadcast() {
|
|
|
|
// Must bind sender to an address that can send to the broadcast address on MacOS.
|
|
|
|
// Macos will give us error 49 when sending the broadcast packet if we omit hostname here.
|
|
|
|
const alice = Deno.listenDatagram({
|
|
|
|
port: 3500,
|
|
|
|
transport: "udp",
|
|
|
|
hostname: "0.0.0.0",
|
|
|
|
});
|
|
|
|
|
|
|
|
const bob = Deno.listenDatagram({
|
|
|
|
port: 4501,
|
|
|
|
transport: "udp",
|
|
|
|
hostname: "0.0.0.0",
|
|
|
|
});
|
|
|
|
assert(bob.addr.transport === "udp");
|
|
|
|
assertEquals(bob.addr.port, 4501);
|
|
|
|
assertEquals(bob.addr.hostname, "0.0.0.0");
|
|
|
|
|
|
|
|
const broadcastAddr = { ...bob.addr, hostname: "255.255.255.255" };
|
|
|
|
|
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
|
|
|
const byteLength = await alice.send(sent, broadcastAddr);
|
|
|
|
|
|
|
|
assertEquals(byteLength, 3);
|
|
|
|
const [recvd, remote] = await bob.receive();
|
|
|
|
assert(remote.transport === "udp");
|
|
|
|
assertEquals(remote.port, 3500);
|
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
alice.close();
|
|
|
|
bob.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUdpConcurrentSendReceive() {
|
2020-12-16 11:14:12 -05:00
|
|
|
const socket = Deno.listenDatagram({ port: 3500, transport: "udp" });
|
|
|
|
assert(socket.addr.transport === "udp");
|
|
|
|
assertEquals(socket.addr.port, 3500);
|
|
|
|
assertEquals(socket.addr.hostname, "127.0.0.1");
|
|
|
|
|
|
|
|
const recvPromise = socket.receive();
|
|
|
|
|
|
|
|
const sendBuf = new Uint8Array([1, 2, 3]);
|
|
|
|
const sendLen = await socket.send(sendBuf, socket.addr);
|
|
|
|
assertEquals(sendLen, 3);
|
|
|
|
|
2021-04-09 17:35:29 -04:00
|
|
|
const [recvBuf, _recvAddr] = await recvPromise;
|
2020-12-16 11:14:12 -05:00
|
|
|
assertEquals(recvBuf.length, 3);
|
|
|
|
assertEquals(1, recvBuf[0]);
|
|
|
|
assertEquals(2, recvBuf[1]);
|
|
|
|
assertEquals(3, recvBuf[2]);
|
|
|
|
|
|
|
|
socket.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUdpBorrowMutError() {
|
2020-06-10 11:26:28 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
port: 4501,
|
|
|
|
transport: "udp",
|
|
|
|
});
|
|
|
|
// Panic happened on second send: BorrowMutError
|
|
|
|
const a = socket.send(new Uint8Array(), socket.addr);
|
|
|
|
const b = socket.send(new Uint8Array(), socket.addr);
|
|
|
|
await Promise.all([a, b]);
|
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-10 11:26:28 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixPacketSendReceive() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const aliceFilePath = tmpUnixSocketPath();
|
2020-04-28 15:46:39 -04:00
|
|
|
const alice = Deno.listenDatagram({
|
2022-10-23 18:45:45 -04:00
|
|
|
path: aliceFilePath,
|
2020-04-28 15:46:39 -04:00
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(alice.addr.transport === "unixpacket");
|
2022-10-23 18:45:45 -04:00
|
|
|
assertEquals(alice.addr.path, aliceFilePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
2022-10-23 18:45:45 -04:00
|
|
|
const bobFilePath = tmpUnixSocketPath();
|
2020-04-28 15:46:39 -04:00
|
|
|
const bob = Deno.listenDatagram({
|
2022-10-23 18:45:45 -04:00
|
|
|
path: bobFilePath,
|
2020-04-28 15:46:39 -04:00
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(bob.addr.transport === "unixpacket");
|
2022-10-23 18:45:45 -04:00
|
|
|
assertEquals(bob.addr.path, bobFilePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
2020-06-15 12:20:45 -04:00
|
|
|
const byteLength = await alice.send(sent, bob.addr);
|
|
|
|
assertEquals(byteLength, 3);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const [recvd, remote] = await bob.receive();
|
|
|
|
assert(remote.transport === "unixpacket");
|
2022-10-23 18:45:45 -04:00
|
|
|
assertEquals(remote.path, aliceFilePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
alice.close();
|
|
|
|
bob.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2022-10-23 18:45:45 -04:00
|
|
|
// TODO(lucacasonato): support concurrent reads and writes on unixpacket sockets
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ ignore: true, permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixPacketConcurrentSendReceive() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-12-16 11:14:12 -05:00
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unixpacket");
|
|
|
|
assertEquals(socket.addr.path, filePath);
|
|
|
|
|
|
|
|
const recvPromise = socket.receive();
|
|
|
|
|
|
|
|
const sendBuf = new Uint8Array([1, 2, 3]);
|
|
|
|
const sendLen = await socket.send(sendBuf, socket.addr);
|
|
|
|
assertEquals(sendLen, 3);
|
|
|
|
|
2021-04-09 17:35:29 -04:00
|
|
|
const [recvBuf, _recvAddr] = await recvPromise;
|
2020-12-16 11:14:12 -05:00
|
|
|
assertEquals(recvBuf.length, 3);
|
|
|
|
assertEquals(1, recvBuf[0]);
|
|
|
|
assertEquals(2, recvBuf[1]);
|
|
|
|
assertEquals(3, recvBuf[2]);
|
|
|
|
|
|
|
|
socket.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netTcpListenIteratorBreakClosesResource() {
|
|
|
|
async function iterate(listener: Deno.Listener) {
|
2020-04-28 19:08:02 -04:00
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
for await (const conn of listener) {
|
|
|
|
conn.close();
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i > 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = { hostname: "127.0.0.1", port: 8888 };
|
|
|
|
const listener = Deno.listen(addr);
|
2021-08-25 16:04:14 -04:00
|
|
|
const iteratePromise = iterate(listener);
|
2020-04-28 19:08:02 -04:00
|
|
|
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(100);
|
2020-04-28 19:08:02 -04:00
|
|
|
const conn1 = await Deno.connect(addr);
|
|
|
|
conn1.close();
|
|
|
|
const conn2 = await Deno.connect(addr);
|
|
|
|
conn2.close();
|
|
|
|
|
2021-08-25 16:04:14 -04:00
|
|
|
await iteratePromise;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 19:08:02 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netTcpListenCloseWhileIterating() {
|
2021-07-31 21:10:19 -04:00
|
|
|
const listener = Deno.listen({ port: 8001 });
|
2020-02-21 11:26:54 -05:00
|
|
|
const nextWhileClosing = listener[Symbol.asyncIterator]().next();
|
|
|
|
listener.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = listener[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-02-21 11:26:54 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUdpListenCloseWhileIterating() {
|
2021-01-25 10:02:03 -05:00
|
|
|
const socket = Deno.listenDatagram({ port: 8000, transport: "udp" });
|
2020-02-21 11:26:54 -05:00
|
|
|
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
socket.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-02-21 11:26:54 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixListenCloseWhileIterating() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-04-28 12:37:59 -04:00
|
|
|
const socket = Deno.listen({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
socket.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netUnixPacketListenCloseWhileIterating() {
|
2022-10-23 18:45:45 -04:00
|
|
|
const filePath = tmpUnixSocketPath();
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
socket.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-09 16:37:19 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function netListenAsyncIterator() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-10 11:38:02 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2021-08-05 07:08:58 -04:00
|
|
|
const runAsyncIterator = async () => {
|
2020-03-10 11:38:02 -04:00
|
|
|
for await (const conn of listener) {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
runAsyncIterator();
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
listener.close();
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-10 11:38:02 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function netCloseWriteSuccess() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-10 11:38:02 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2020-11-26 11:22:36 -05:00
|
|
|
const closeDeferred = deferred();
|
2020-03-28 13:03:49 -04:00
|
|
|
listener.accept().then(async (conn) => {
|
2020-03-10 11:38:02 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred;
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
conn.closeWrite(); // closing write
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
// Check read not impacted
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
2021-01-19 23:11:08 -05:00
|
|
|
// Verify that the write end of the socket is closed.
|
|
|
|
// TODO(piscisaureus): assert that thrown error is of a specific type.
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-03-10 11:38:02 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
});
|
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
2019-04-28 17:15:15 -04:00
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2020-03-11 18:19:24 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-11 18:19:24 -04:00
|
|
|
{
|
2021-08-16 10:15:00 -04:00
|
|
|
// https://github.com/denoland/deno/issues/11580
|
|
|
|
ignore: Deno.build.os === "darwin" && isCI,
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-11 18:19:24 -04:00
|
|
|
},
|
|
|
|
async function netHangsOnClose() {
|
|
|
|
let acceptedConn: Deno.Conn;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function iteratorReq(listener: Deno.Listener) {
|
2020-03-11 18:19:24 -04:00
|
|
|
const p = new Uint8Array(10);
|
|
|
|
const conn = await listener.accept();
|
|
|
|
acceptedConn = conn;
|
|
|
|
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const nread = await conn.read(p);
|
2020-04-28 12:40:43 -04:00
|
|
|
if (nread === null) {
|
2020-03-11 18:19:24 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-10-09 16:37:19 -04:00
|
|
|
assert(err);
|
|
|
|
assert(err instanceof Deno.errors.Interrupted);
|
2020-03-11 18:19:24 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 12:24:58 -04:00
|
|
|
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-11 18:19:24 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2021-08-04 19:45:10 -04:00
|
|
|
const listenerPromise = iteratorReq(listener);
|
|
|
|
const connectionPromise = (async () => {
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3, 4]));
|
|
|
|
const buf = new Uint8Array(10);
|
|
|
|
await conn.read(buf);
|
|
|
|
conn!.close();
|
|
|
|
acceptedConn!.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
listenerPromise,
|
|
|
|
connectionPromise,
|
|
|
|
]);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-11 18:19:24 -04:00
|
|
|
);
|
2020-09-27 10:44:53 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-09-27 10:44:53 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-09-27 10:44:53 -04:00
|
|
|
},
|
|
|
|
function netExplicitUndefinedHostname() {
|
|
|
|
const listener = Deno.listen({ hostname: undefined, port: 8080 });
|
|
|
|
assertEquals((listener.addr as Deno.NetAddr).hostname, "0.0.0.0");
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2021-10-02 13:09:59 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-02 13:09:59 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os !== "linux",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
},
|
|
|
|
function netUnixAbstractPathShouldNotPanic() {
|
|
|
|
const listener = Deno.listen({
|
|
|
|
path: "\0aaa",
|
|
|
|
transport: "unix",
|
|
|
|
});
|
|
|
|
assert("not panic");
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
Deno.test({ permissions: { net: true } }, async function whatwgStreams() {
|
|
|
|
(async () => {
|
|
|
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
await conn.readable.pipeTo(conn.writable);
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
const reader = conn.readable.getReader();
|
|
|
|
const writer = conn.writable.getWriter();
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const data = encoder.encode("Hello World");
|
|
|
|
|
|
|
|
await writer.write(data);
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
assert(!done);
|
|
|
|
assertEquals(decoder.decode(value), "Hello World");
|
|
|
|
await reader.cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function readableStreamTextEncoderPipe() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2022-02-15 07:35:22 -05:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
const readable = file.readable.pipeThrough(new TextDecoderStream());
|
|
|
|
const chunks = [];
|
|
|
|
for await (const chunk of readable) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks.length, 1);
|
|
|
|
assertEquals(chunks[0].length, 12);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writableStream() {
|
|
|
|
const path = await Deno.makeTempFile();
|
|
|
|
const file = await Deno.open(path, { write: true });
|
|
|
|
assert(file.writable instanceof WritableStream);
|
|
|
|
const readable = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new TextEncoder().encode("hello "));
|
|
|
|
controller.enqueue(new TextEncoder().encode("world!"));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await readable.pipeTo(file.writable);
|
|
|
|
const res = await Deno.readTextFile(path);
|
|
|
|
assertEquals(res, "hello world!");
|
|
|
|
},
|
|
|
|
);
|
2022-03-22 23:04:20 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, run: true } },
|
|
|
|
async function netListenUnref() {
|
|
|
|
const [statusCode, _output] = await execCode(`
|
|
|
|
async function main() {
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
listener.unref();
|
|
|
|
await listener.accept(); // This doesn't block the program from exiting
|
|
|
|
}
|
|
|
|
main();
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, run: true } },
|
|
|
|
async function netListenUnref() {
|
|
|
|
const [statusCode, _output] = await execCode(`
|
|
|
|
async function main() {
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
await listener.accept();
|
|
|
|
listener.unref();
|
|
|
|
await listener.accept(); // The program exits here
|
|
|
|
throw new Error(); // The program doesn't reach here
|
|
|
|
}
|
|
|
|
main();
|
|
|
|
const conn = await Deno.connect({ port: 3500 });
|
|
|
|
conn.close();
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, run: true, net: true } },
|
|
|
|
async function netListenUnrefAndRef() {
|
2022-12-02 12:41:52 -05:00
|
|
|
const p = execCode2(`
|
2022-03-22 23:04:20 -04:00
|
|
|
async function main() {
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
listener.unref();
|
|
|
|
listener.ref(); // This restores 'ref' state of listener
|
2022-12-02 12:41:52 -05:00
|
|
|
console.log("started");
|
2022-03-22 23:04:20 -04:00
|
|
|
await listener.accept();
|
|
|
|
console.log("accepted")
|
|
|
|
}
|
|
|
|
main();
|
|
|
|
`);
|
2022-12-02 12:41:52 -05:00
|
|
|
await p.waitStdoutText("started");
|
|
|
|
const conn = await Deno.connect({ port: 3500 });
|
|
|
|
conn.close();
|
|
|
|
const [statusCode, output] = await p.finished();
|
2022-03-22 23:04:20 -04:00
|
|
|
assertEquals(statusCode, 0);
|
2022-12-02 12:41:52 -05:00
|
|
|
assertEquals(output.trim(), "started\naccepted");
|
2022-03-22 23:04:20 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function netListenUnrefConcurrentAccept() {
|
|
|
|
const timer = setTimeout(() => {}, 1000);
|
|
|
|
const listener = Deno.listen({ port: 3500 });
|
|
|
|
listener.accept().catch(() => {});
|
|
|
|
listener.unref();
|
|
|
|
// Unref'd listener still causes Busy error
|
|
|
|
// on concurrent accept calls.
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await listener.accept(); // The program exits here
|
|
|
|
}, Deno.errors.Busy);
|
|
|
|
listener.close();
|
|
|
|
clearTimeout(timer);
|
|
|
|
},
|
|
|
|
);
|
2022-10-23 18:45:45 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { read: true, write: true },
|
|
|
|
}, function netUnixListenAddrAlreadyInUse() {
|
|
|
|
const filePath = tmpUnixSocketPath();
|
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.listen({ path: filePath, transport: "unix" });
|
|
|
|
},
|
|
|
|
Deno.errors.AddrInUse,
|
|
|
|
);
|
|
|
|
listener.close();
|
|
|
|
});
|
2022-10-24 05:05:07 -04:00
|
|
|
|
|
|
|
Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
|
|
|
|
const listener1 = Deno.listen({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 3500,
|
|
|
|
});
|
|
|
|
listener1.accept().then(
|
|
|
|
(conn) => {
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn1 = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
const buf1 = new Uint8Array(1024);
|
|
|
|
await conn1.read(buf1);
|
|
|
|
listener1.close();
|
|
|
|
conn1.close();
|
|
|
|
|
|
|
|
const listener2 = Deno.listen({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 3500,
|
|
|
|
});
|
|
|
|
|
|
|
|
listener2.accept().then(
|
|
|
|
(conn) => {
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn2 = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
|
|
|
const buf2 = new Uint8Array(1024);
|
|
|
|
await conn2.read(buf2);
|
|
|
|
|
|
|
|
listener2.close();
|
|
|
|
conn2.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function netUdpReuseAddr() {
|
|
|
|
const sender = Deno.listenDatagram({
|
|
|
|
port: 4002,
|
|
|
|
transport: "udp",
|
|
|
|
});
|
|
|
|
const listener1 = Deno.listenDatagram({
|
|
|
|
port: 4000,
|
|
|
|
transport: "udp",
|
|
|
|
reuseAddress: true,
|
|
|
|
});
|
|
|
|
const listener2 = Deno.listenDatagram({
|
|
|
|
port: 4000,
|
|
|
|
transport: "udp",
|
|
|
|
reuseAddress: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
|
|
|
await sender.send(sent, listener1.addr);
|
|
|
|
await Promise.any([listener1.receive(), listener2.receive()]).then(
|
|
|
|
([recvd, remote]) => {
|
|
|
|
assert(remote.transport === "udp");
|
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
sender.close();
|
|
|
|
listener1.close();
|
|
|
|
listener2.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
function netUdpNoReuseAddr() {
|
|
|
|
let listener1;
|
|
|
|
try {
|
|
|
|
listener1 = Deno.listenDatagram({
|
|
|
|
port: 4001,
|
|
|
|
transport: "udp",
|
|
|
|
reuseAddress: false,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
assert(err);
|
|
|
|
assert(err instanceof Deno.errors.AddrInUse); // AddrInUse from previous test
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenDatagram({
|
|
|
|
port: 4001,
|
|
|
|
transport: "udp",
|
|
|
|
reuseAddress: false,
|
|
|
|
});
|
|
|
|
}, Deno.errors.AddrInUse);
|
|
|
|
if (typeof listener1 !== "undefined") {
|
|
|
|
listener1.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-10-26 15:04:27 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os !== "linux",
|
|
|
|
permissions: { net: true },
|
|
|
|
}, async function netTcpListenReusePort() {
|
|
|
|
const port = 4003;
|
|
|
|
const listener1 = Deno.listen({ port, reusePort: true });
|
|
|
|
const listener2 = Deno.listen({ port, reusePort: true });
|
|
|
|
let p1;
|
|
|
|
let p2;
|
|
|
|
let listener1Recv = false;
|
|
|
|
let listener2Recv = false;
|
|
|
|
while (!listener1Recv || !listener2Recv) {
|
|
|
|
if (!p1) {
|
|
|
|
p1 = listener1.accept().then((conn) => {
|
|
|
|
conn.close();
|
|
|
|
listener1Recv = true;
|
|
|
|
p1 = undefined;
|
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
if (!p2) {
|
|
|
|
p2 = listener2.accept().then((conn) => {
|
|
|
|
conn.close();
|
|
|
|
listener2Recv = true;
|
|
|
|
p2 = undefined;
|
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
const conn = await Deno.connect({ port });
|
|
|
|
conn.close();
|
|
|
|
await Promise.race([p1, p2]);
|
|
|
|
}
|
|
|
|
listener1.close();
|
|
|
|
listener2.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os === "linux",
|
|
|
|
permissions: { net: true },
|
|
|
|
}, function netTcpListenReusePortDoesNothing() {
|
|
|
|
const listener1 = Deno.listen({ port: 4003, reusePort: true });
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listen({ port: 4003, reusePort: true });
|
|
|
|
}, Deno.errors.AddrInUse);
|
|
|
|
listener1.close();
|
|
|
|
});
|