2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-02-29 12:45:47 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-05-10 19:49:57 -04:00
|
|
|
assertNotEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2021-01-14 23:32:27 -05:00
|
|
|
assertStrictEquals,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2020-02-29 12:45:47 -05:00
|
|
|
} from "./test_util.ts";
|
2024-07-25 01:30:28 -04:00
|
|
|
import { BufReader, BufWriter } from "@std/io";
|
|
|
|
import { readAll } from "@std/io/read-all";
|
|
|
|
import { writeAll } from "@std/io/write-all";
|
2022-10-17 18:57:31 -04:00
|
|
|
import { TextProtoReader } from "../testdata/run/textproto.ts";
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
2024-04-02 20:36:34 -04:00
|
|
|
const cert = Deno.readTextFileSync("tests/testdata/tls/localhost.crt");
|
|
|
|
const key = Deno.readTextFileSync("tests/testdata/tls/localhost.key");
|
|
|
|
const caCerts = [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")];
|
2019-09-23 14:40:38 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function sleep(msec: number) {
|
2021-05-10 19:49:57 -04:00
|
|
|
await new Promise((res, _rej) => setTimeout(res, msec));
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
function listenTls(
|
|
|
|
options?: { alpnProtocols?: string[]; reusePort?: boolean },
|
|
|
|
): { listener: Deno.TlsListener; port: number; hostname: string } {
|
|
|
|
const tlsOptions = { port: 0, hostname: "localhost", cert, key, ...options };
|
|
|
|
const listener = Deno.listenTls(tlsOptions);
|
|
|
|
return {
|
|
|
|
listener,
|
|
|
|
port: (<Deno.NetAddr> listener.addr).port,
|
|
|
|
hostname: "localhost",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function listenTcp(): {
|
|
|
|
listener: Deno.Listener;
|
|
|
|
port: number;
|
|
|
|
hostname: string;
|
|
|
|
} {
|
|
|
|
const listener = Deno.listen({ port: 0, hostname: "localhost" });
|
|
|
|
return {
|
|
|
|
listener,
|
|
|
|
port: (<Deno.NetAddr> listener.addr).port,
|
|
|
|
hostname: "localhost",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
function unreachable(): never {
|
|
|
|
throw new Error("Unreachable code reached");
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: false } }, async function connectTLSNoPerm() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-08-09 09:55:00 -04:00
|
|
|
await Deno.connectTls({ hostname: "deno.land", port: 443 });
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2019-09-23 14:40:38 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function connectTLSInvalidHost() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2022-05-18 07:32:12 -04:00
|
|
|
await Deno.connectTls({ hostname: "256.0.0.0", port: 3567 });
|
2021-04-26 15:39:55 -04:00
|
|
|
}, TypeError);
|
2021-02-11 06:45:10 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-10-17 22:28:27 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function startTlsWithoutExclusiveAccessToTcpConn() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, hostname, port } = listenTcp();
|
2022-10-17 22:28:27 -04:00
|
|
|
const [serverConn, clientConn] = await Promise.all([
|
2024-04-02 20:36:34 -04:00
|
|
|
listener.accept(),
|
2022-10-17 22:28:27 -04:00
|
|
|
Deno.connect({ hostname, port }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const buf = new Uint8Array(128);
|
|
|
|
const readPromise = clientConn.read(buf);
|
|
|
|
// `clientConn` is being used by a pending promise (`readPromise`) so
|
|
|
|
// `Deno.startTls` cannot consume the connection.
|
|
|
|
await assertRejects(
|
|
|
|
() => Deno.startTls(clientConn, { hostname }),
|
|
|
|
Deno.errors.BadResource,
|
|
|
|
);
|
|
|
|
|
|
|
|
serverConn.close();
|
2024-04-02 20:36:34 -04:00
|
|
|
listener.close();
|
2022-10-17 22:28:27 -04:00
|
|
|
await readPromise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function dialAndListenTLS() {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, port, hostname } = listenTls();
|
2020-03-04 11:31:14 -05:00
|
|
|
|
|
|
|
const response = encoder.encode(
|
2020-07-14 15:24:17 -04:00
|
|
|
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
listener.accept().then(
|
2021-08-05 07:08:58 -04:00
|
|
|
async (conn) => {
|
2020-03-04 11:31:14 -05:00
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr != null);
|
|
|
|
await conn.write(response);
|
|
|
|
// TODO(bartlomieju): this might be a bug
|
|
|
|
setTimeout(() => {
|
|
|
|
conn.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-03-04 11:31:14 -05:00
|
|
|
}, 0);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2022-02-23 23:16:56 -05:00
|
|
|
const conn = await Deno.connectTls({ hostname, port, caCerts });
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body = `GET / HTTP/1.1\r\nHost: ${hostname}:${port}\r\n\r\n`;
|
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null, `line must be read: ${String(statusLine)}`);
|
|
|
|
const m = statusLine.match(/^(.+?) (.+?) (.+?)$/);
|
|
|
|
assert(m !== null, "must be matched");
|
|
|
|
const [_, proto, status, ok] = m;
|
|
|
|
assertEquals(proto, "HTTP/1.1");
|
|
|
|
assertEquals(status, "200");
|
|
|
|
assertEquals(ok, "OK");
|
2022-10-17 18:57:31 -04:00
|
|
|
const headers = await tpr.readMimeHeader();
|
2022-02-23 23:16:56 -05:00
|
|
|
assert(headers !== null);
|
|
|
|
const contentLength = parseInt(headers.get("content-length")!);
|
|
|
|
const bodyBuf = new Uint8Array(contentLength);
|
|
|
|
await r.readFull(bodyBuf);
|
|
|
|
assertEquals(decoder.decode(bodyBuf), "Hello World\n");
|
|
|
|
conn.close();
|
|
|
|
listener.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2022-02-23 23:16:56 -05:00
|
|
|
},
|
|
|
|
);
|
2024-04-02 20:36:34 -04:00
|
|
|
|
2022-02-23 23:16:56 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: false, net: true } },
|
|
|
|
async function listenTlsWithCertAndKey() {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2022-02-23 23:16:56 -05:00
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, hostname, port } = listenTls();
|
2022-02-23 23:16:56 -05:00
|
|
|
|
|
|
|
const response = encoder.encode(
|
|
|
|
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
|
|
|
|
);
|
|
|
|
|
|
|
|
listener.accept().then(
|
|
|
|
async (conn) => {
|
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr != null);
|
|
|
|
await conn.write(response);
|
|
|
|
setTimeout(() => {
|
|
|
|
conn.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2022-02-23 23:16:56 -05:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn = await Deno.connectTls({ hostname, port, caCerts });
|
2020-03-04 11:31:14 -05:00
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body = `GET / HTTP/1.1\r\nHost: ${hostname}:${port}\r\n\r\n`;
|
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(statusLine !== null, `line must be read: ${String(statusLine)}`);
|
2020-03-04 11:31:14 -05:00
|
|
|
const m = statusLine.match(/^(.+?) (.+?) (.+?)$/);
|
|
|
|
assert(m !== null, "must be matched");
|
|
|
|
const [_, proto, status, ok] = m;
|
|
|
|
assertEquals(proto, "HTTP/1.1");
|
|
|
|
assertEquals(status, "200");
|
|
|
|
assertEquals(ok, "OK");
|
2022-10-17 18:57:31 -04:00
|
|
|
const headers = await tpr.readMimeHeader();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(headers !== null);
|
2020-03-04 11:31:14 -05:00
|
|
|
const contentLength = parseInt(headers.get("content-length")!);
|
|
|
|
const bodyBuf = new Uint8Array(contentLength);
|
|
|
|
await r.readFull(bodyBuf);
|
|
|
|
assertEquals(decoder.decode(bodyBuf), "Hello World\n");
|
|
|
|
conn.close();
|
|
|
|
listener.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-04-18 11:21:20 -04:00
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsPair(): Promise<[Deno.Conn, Deno.Conn]> {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, hostname, port } = listenTls();
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
const connectPromise = Deno.connectTls({
|
2024-04-02 20:36:34 -04:00
|
|
|
hostname,
|
2021-01-14 23:32:27 -05:00
|
|
|
port,
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
2021-01-14 23:32:27 -05:00
|
|
|
});
|
2021-05-10 19:49:57 -04:00
|
|
|
const endpoints = await Promise.all([acceptPromise, connectPromise]);
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
return endpoints;
|
2021-01-14 23:32:27 -05:00
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
async function tlsAlpn(
|
|
|
|
useStartTls: boolean,
|
|
|
|
): Promise<[Deno.TlsConn, Deno.TlsConn]> {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, port } = listenTls({
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols: ["deno", "rocks"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCerts = [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")];
|
2021-11-26 13:59:53 -05:00
|
|
|
const clientAlpnProtocols = ["rocks", "rises"];
|
|
|
|
let endpoints: [Deno.TlsConn, Deno.TlsConn];
|
|
|
|
|
|
|
|
if (!useStartTls) {
|
|
|
|
const connectPromise = Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
|
|
|
caCerts,
|
|
|
|
alpnProtocols: clientAlpnProtocols,
|
|
|
|
});
|
|
|
|
endpoints = await Promise.all([acceptPromise, connectPromise]);
|
|
|
|
} else {
|
|
|
|
const client = await Deno.connect({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
|
|
|
});
|
|
|
|
const connectPromise = Deno.startTls(client, {
|
|
|
|
hostname: "localhost",
|
|
|
|
caCerts,
|
|
|
|
alpnProtocols: clientAlpnProtocols,
|
|
|
|
});
|
|
|
|
endpoints = await Promise.all([acceptPromise, connectPromise]);
|
|
|
|
}
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
return endpoints;
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
async function sendThenCloseWriteThenReceive(
|
|
|
|
conn: Deno.Conn,
|
|
|
|
chunkCount: number,
|
|
|
|
chunkSize: number,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
const byteCount = chunkCount * chunkSize;
|
|
|
|
const buf = new Uint8Array(chunkSize); // Note: buf is size of _chunk_.
|
|
|
|
let n: number;
|
|
|
|
|
|
|
|
// Slowly send 42s.
|
|
|
|
buf.fill(42);
|
|
|
|
for (let remaining = byteCount; remaining > 0; remaining -= n) {
|
|
|
|
n = await conn.write(buf.subarray(0, remaining));
|
|
|
|
assert(n >= 1);
|
|
|
|
await sleep(10);
|
|
|
|
}
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
// Send EOF.
|
|
|
|
await conn.closeWrite();
|
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
// Receive 69s.
|
|
|
|
for (let remaining = byteCount; remaining > 0; remaining -= n) {
|
|
|
|
buf.fill(0);
|
|
|
|
n = await conn.read(buf) as number;
|
|
|
|
assert(n >= 1);
|
|
|
|
assertStrictEquals(buf[0], 69);
|
|
|
|
assertStrictEquals(buf[n - 1], 69);
|
|
|
|
}
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
async function receiveThenSend(
|
|
|
|
conn: Deno.Conn,
|
|
|
|
chunkCount: number,
|
|
|
|
chunkSize: number,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
const byteCount = chunkCount * chunkSize;
|
|
|
|
const buf = new Uint8Array(byteCount); // Note: buf size equals `byteCount`.
|
|
|
|
let n: number;
|
|
|
|
|
|
|
|
// Receive 42s.
|
|
|
|
for (let remaining = byteCount; remaining > 0; remaining -= n) {
|
|
|
|
buf.fill(0);
|
|
|
|
n = await conn.read(buf) as number;
|
|
|
|
assert(n >= 1);
|
|
|
|
assertStrictEquals(buf[0], 42);
|
|
|
|
assertStrictEquals(buf[n - 1], 42);
|
|
|
|
}
|
2021-01-14 23:32:27 -05:00
|
|
|
|
2021-05-10 19:49:57 -04:00
|
|
|
// Slowly send 69s.
|
|
|
|
buf.fill(69);
|
|
|
|
for (let remaining = byteCount; remaining > 0; remaining -= n) {
|
|
|
|
n = await conn.write(buf.subarray(0, remaining));
|
|
|
|
assert(n >= 1);
|
|
|
|
await sleep(10);
|
|
|
|
}
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function tlsServerAlpnListenConnect() {
|
|
|
|
const [serverConn, clientConn] = await tlsAlpn(false);
|
|
|
|
const [serverHS, clientHS] = await Promise.all([
|
|
|
|
serverConn.handshake(),
|
|
|
|
clientConn.handshake(),
|
|
|
|
]);
|
|
|
|
assertStrictEquals(serverHS.alpnProtocol, "rocks");
|
|
|
|
assertStrictEquals(clientHS.alpnProtocol, "rocks");
|
|
|
|
|
|
|
|
serverConn.close();
|
|
|
|
clientConn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function tlsServerAlpnListenStartTls() {
|
|
|
|
const [serverConn, clientConn] = await tlsAlpn(true);
|
|
|
|
const [serverHS, clientHS] = await Promise.all([
|
|
|
|
serverConn.handshake(),
|
|
|
|
clientConn.handshake(),
|
|
|
|
]);
|
|
|
|
assertStrictEquals(serverHS.alpnProtocol, "rocks");
|
|
|
|
assertStrictEquals(clientHS.alpnProtocol, "rocks");
|
|
|
|
|
|
|
|
serverConn.close();
|
|
|
|
clientConn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamHalfCloseSendOneByte() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(serverConn, 1, 1),
|
|
|
|
receiveThenSend(clientConn, 1, 1),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamHalfCloseSendOneByte() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(clientConn, 1, 1),
|
|
|
|
receiveThenSend(serverConn, 1, 1),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamHalfCloseSendOneChunk() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(serverConn, 1, 1 << 20 /* 1 MB */),
|
|
|
|
receiveThenSend(clientConn, 1, 1 << 20 /* 1 MB */),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamHalfCloseSendOneChunk() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(clientConn, 1, 1 << 20 /* 1 MB */),
|
|
|
|
receiveThenSend(serverConn, 1, 1 << 20 /* 1 MB */),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamHalfCloseSendManyBytes() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(serverConn, 100, 1),
|
|
|
|
receiveThenSend(clientConn, 100, 1),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamHalfCloseSendManyBytes() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(clientConn, 100, 1),
|
|
|
|
receiveThenSend(serverConn, 100, 1),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamHalfCloseSendManyChunks() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(serverConn, 100, 1 << 16 /* 64 kB */),
|
|
|
|
receiveThenSend(clientConn, 100, 1 << 16 /* 64 kB */),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamHalfCloseSendManyChunks() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
sendThenCloseWriteThenReceive(clientConn, 100, 1 << 16 /* 64 kB */),
|
|
|
|
receiveThenSend(serverConn, 100, 1 << 16 /* 64 kB */),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
const largeAmount = 1 << 20 /* 1 MB */;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function sendAlotReceiveNothing(conn: Deno.Conn) {
|
2021-01-14 23:32:27 -05:00
|
|
|
// Start receive op.
|
|
|
|
const readBuf = new Uint8Array(1024);
|
|
|
|
const readPromise = conn.read(readBuf);
|
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
throw new Error("Failed to send buffer in a reasonable amount of time");
|
|
|
|
}, 10_000);
|
|
|
|
|
2021-01-14 23:32:27 -05:00
|
|
|
// Send 1 MB of data.
|
2023-11-15 18:12:46 -05:00
|
|
|
const writeBuf = new Uint8Array(largeAmount);
|
2021-01-14 23:32:27 -05:00
|
|
|
writeBuf.fill(42);
|
2023-11-15 18:12:46 -05:00
|
|
|
await writeAll(conn, writeBuf);
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
// Send EOF.
|
|
|
|
await conn.closeWrite();
|
|
|
|
|
|
|
|
// Close the connection.
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
// Read op should be canceled.
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-01-14 23:32:27 -05:00
|
|
|
async () => await readPromise,
|
|
|
|
Deno.errors.Interrupted,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function receiveAlotSendNothing(conn: Deno.Conn) {
|
2021-01-14 23:32:27 -05:00
|
|
|
const readBuf = new Uint8Array(1024);
|
|
|
|
let n: number | null;
|
2023-11-15 18:12:46 -05:00
|
|
|
let nread = 0;
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to read buffer in a reasonable amount of time (got ${nread}/${largeAmount})`,
|
|
|
|
);
|
|
|
|
}, 10_000);
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
// Receive 1 MB of data.
|
2023-11-15 18:12:46 -05:00
|
|
|
try {
|
|
|
|
for (; nread < largeAmount; nread += n!) {
|
|
|
|
n = await conn.read(readBuf);
|
|
|
|
assertStrictEquals(typeof n, "number");
|
|
|
|
assert(n! > 0);
|
|
|
|
assertStrictEquals(readBuf[0], 42);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(
|
2024-09-05 09:22:31 -04:00
|
|
|
`Got an error (${
|
|
|
|
(e as Error).message
|
|
|
|
}) after reading ${nread}/${largeAmount} bytes`,
|
2023-11-15 18:12:46 -05:00
|
|
|
{ cause: e },
|
|
|
|
);
|
2021-01-14 23:32:27 -05:00
|
|
|
}
|
2023-11-15 18:12:46 -05:00
|
|
|
clearTimeout(timeout);
|
2021-01-14 23:32:27 -05:00
|
|
|
|
|
|
|
// Close the connection, without sending anything at all.
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamCancelRead() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
2021-01-14 23:32:27 -05:00
|
|
|
await Promise.all([
|
2021-05-10 19:49:57 -04:00
|
|
|
sendAlotReceiveNothing(serverConn),
|
|
|
|
receiveAlotSendNothing(clientConn),
|
2021-01-14 23:32:27 -05:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamCancelRead() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
2021-01-14 23:32:27 -05:00
|
|
|
await Promise.all([
|
2021-05-10 19:49:57 -04:00
|
|
|
sendAlotReceiveNothing(clientConn),
|
|
|
|
receiveAlotSendNothing(serverConn),
|
2021-01-14 23:32:27 -05:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function sendReceiveEmptyBuf(conn: Deno.Conn) {
|
2021-05-10 19:49:57 -04:00
|
|
|
const byteBuf = new Uint8Array([1]);
|
|
|
|
const emptyBuf = new Uint8Array(0);
|
|
|
|
let n: number | null;
|
|
|
|
|
|
|
|
n = await conn.write(emptyBuf);
|
|
|
|
assertStrictEquals(n, 0);
|
|
|
|
|
|
|
|
n = await conn.read(emptyBuf);
|
|
|
|
assertStrictEquals(n, 0);
|
|
|
|
|
|
|
|
n = await conn.write(byteBuf);
|
|
|
|
assertStrictEquals(n, 1);
|
|
|
|
|
|
|
|
n = await conn.read(byteBuf);
|
|
|
|
assertStrictEquals(n, 1);
|
|
|
|
|
|
|
|
await conn.closeWrite();
|
|
|
|
|
|
|
|
n = await conn.write(emptyBuf);
|
|
|
|
assertStrictEquals(n, 0);
|
|
|
|
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-05-10 19:49:57 -04:00
|
|
|
await conn.write(byteBuf);
|
2023-11-15 18:12:46 -05:00
|
|
|
}, Deno.errors.NotConnected);
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
n = await conn.write(emptyBuf);
|
|
|
|
assertStrictEquals(n, 0);
|
|
|
|
|
|
|
|
n = await conn.read(byteBuf);
|
|
|
|
assertStrictEquals(n, null);
|
|
|
|
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsStreamSendReceiveEmptyBuf() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
2021-01-14 23:32:27 -05:00
|
|
|
await Promise.all([
|
2021-05-10 19:49:57 -04:00
|
|
|
sendReceiveEmptyBuf(serverConn),
|
|
|
|
sendReceiveEmptyBuf(clientConn),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
function immediateClose(conn: Deno.Conn) {
|
2021-05-10 19:49:57 -04:00
|
|
|
conn.close();
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function closeWriteAndClose(conn: Deno.Conn) {
|
2021-05-10 19:49:57 -04:00
|
|
|
await conn.closeWrite();
|
|
|
|
|
|
|
|
if (await conn.read(new Uint8Array(1)) !== null) {
|
|
|
|
throw new Error("did not expect to receive data on TLS stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsServerStreamImmediateClose() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
immediateClose(serverConn),
|
|
|
|
closeWriteAndClose(clientConn),
|
2021-01-14 23:32:27 -05:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientStreamImmediateClose() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
2021-01-14 23:32:27 -05:00
|
|
|
await Promise.all([
|
2021-05-10 19:49:57 -04:00
|
|
|
closeWriteAndClose(serverConn),
|
|
|
|
immediateClose(clientConn),
|
2021-01-14 23:32:27 -05:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function tlsClientAndServerStreamImmediateClose() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const [serverConn, clientConn] = await tlsPair();
|
|
|
|
await Promise.all([
|
|
|
|
immediateClose(serverConn),
|
|
|
|
immediateClose(clientConn),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
async function tlsWithTcpFailureTestImpl(
|
|
|
|
phase: "handshake" | "traffic",
|
|
|
|
cipherByteCount: number,
|
|
|
|
failureMode: "corruption" | "shutdown",
|
|
|
|
reverse: boolean,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2024-04-02 20:36:34 -04:00
|
|
|
const tls = listenTls();
|
|
|
|
const tcp = listenTcp();
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
const [tlsServerConn, tcpServerConn] = await Promise.all([
|
2024-04-02 20:36:34 -04:00
|
|
|
tls.listener.accept(),
|
|
|
|
Deno.connect({ hostname: tls.hostname, port: tls.port }),
|
2021-05-10 19:49:57 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const [tcpClientConn, tlsClientConn] = await Promise.all([
|
2024-04-02 20:36:34 -04:00
|
|
|
tcp.listener.accept(),
|
2021-05-10 19:49:57 -04:00
|
|
|
Deno.connectTls({
|
2024-04-02 20:36:34 -04:00
|
|
|
hostname: tcp.hostname,
|
|
|
|
port: tcp.port,
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
2021-05-10 19:49:57 -04:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
tls.listener.close();
|
|
|
|
tcp.listener.close();
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
const {
|
|
|
|
tlsConn1,
|
|
|
|
tlsConn2,
|
|
|
|
tcpConn1,
|
|
|
|
tcpConn2,
|
|
|
|
} = reverse
|
|
|
|
? {
|
|
|
|
tlsConn1: tlsClientConn,
|
|
|
|
tlsConn2: tlsServerConn,
|
|
|
|
tcpConn1: tcpClientConn,
|
|
|
|
tcpConn2: tcpServerConn,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
tlsConn1: tlsServerConn,
|
|
|
|
tlsConn2: tlsClientConn,
|
|
|
|
tcpConn1: tcpServerConn,
|
|
|
|
tcpConn2: tcpClientConn,
|
|
|
|
};
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const tcpForwardingInterruptDeferred1 = Promise.withResolvers<void>();
|
2021-05-10 19:49:57 -04:00
|
|
|
const tcpForwardingPromise1 = forwardBytes(
|
|
|
|
tcpConn2,
|
|
|
|
tcpConn1,
|
|
|
|
cipherByteCount,
|
2023-11-22 06:11:20 -05:00
|
|
|
tcpForwardingInterruptDeferred1,
|
2021-05-10 19:49:57 -04:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const tcpForwardingInterruptDeferred2 = Promise.withResolvers<void>();
|
2021-05-10 19:49:57 -04:00
|
|
|
const tcpForwardingPromise2 = forwardBytes(
|
|
|
|
tcpConn1,
|
|
|
|
tcpConn2,
|
|
|
|
Infinity,
|
2023-11-22 06:11:20 -05:00
|
|
|
tcpForwardingInterruptDeferred2,
|
2021-05-10 19:49:57 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
switch (phase) {
|
|
|
|
case "handshake": {
|
|
|
|
let expectedError;
|
|
|
|
switch (failureMode) {
|
|
|
|
case "corruption":
|
|
|
|
expectedError = Deno.errors.InvalidData;
|
|
|
|
break;
|
|
|
|
case "shutdown":
|
|
|
|
expectedError = Deno.errors.UnexpectedEof;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
const tlsTrafficPromise1 = Promise.all([
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects(
|
2021-05-10 19:49:57 -04:00
|
|
|
() => sendBytes(tlsConn1, 0x01, 1),
|
|
|
|
expectedError,
|
|
|
|
),
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects(
|
2021-05-10 19:49:57 -04:00
|
|
|
() => receiveBytes(tlsConn1, 0x02, 1),
|
|
|
|
expectedError,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const tlsTrafficPromise2 = Promise.all([
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects(
|
2021-05-10 19:49:57 -04:00
|
|
|
() => sendBytes(tlsConn2, 0x02, 1),
|
|
|
|
Deno.errors.UnexpectedEof,
|
|
|
|
),
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects(
|
2021-05-10 19:49:57 -04:00
|
|
|
() => receiveBytes(tlsConn2, 0x01, 1),
|
|
|
|
Deno.errors.UnexpectedEof,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await tcpForwardingPromise1;
|
|
|
|
|
|
|
|
switch (failureMode) {
|
|
|
|
case "corruption":
|
|
|
|
await sendBytes(tcpConn1, 0xff, 1 << 14 /* 16 kB */);
|
|
|
|
break;
|
|
|
|
case "shutdown":
|
|
|
|
await tcpConn1.closeWrite();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
await tlsTrafficPromise1;
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
tcpForwardingInterruptDeferred2.resolve();
|
2021-05-10 19:49:57 -04:00
|
|
|
await tcpForwardingPromise2;
|
|
|
|
await tcpConn2.closeWrite();
|
|
|
|
await tlsTrafficPromise2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "traffic": {
|
|
|
|
await Promise.all([
|
|
|
|
sendBytes(tlsConn2, 0x88, 8888),
|
|
|
|
receiveBytes(tlsConn1, 0x88, 8888),
|
|
|
|
sendBytes(tlsConn1, 0x99, 99999),
|
|
|
|
receiveBytes(tlsConn2, 0x99, 99999),
|
|
|
|
]);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
tcpForwardingInterruptDeferred1.resolve();
|
|
|
|
await tcpForwardingInterruptDeferred1.promise;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
switch (failureMode) {
|
|
|
|
case "corruption":
|
|
|
|
await sendBytes(tcpConn1, 0xff, 1 << 14 /* 16 kB */);
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-05-10 19:49:57 -04:00
|
|
|
() => receiveEof(tlsConn1),
|
|
|
|
Deno.errors.InvalidData,
|
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
tcpForwardingInterruptDeferred2.resolve();
|
2021-05-10 19:49:57 -04:00
|
|
|
break;
|
|
|
|
case "shutdown":
|
|
|
|
await Promise.all([
|
|
|
|
tcpConn1.closeWrite(),
|
2023-11-15 18:12:46 -05:00
|
|
|
await assertRejects(
|
|
|
|
() => receiveEof(tlsConn1),
|
|
|
|
Deno.errors.UnexpectedEof,
|
|
|
|
),
|
2021-05-10 19:49:57 -04:00
|
|
|
await tlsConn1.closeWrite(),
|
|
|
|
await receiveEof(tlsConn2),
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
await tcpForwardingPromise2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsServerConn.close();
|
|
|
|
tlsClientConn.close();
|
|
|
|
tcpServerConn.close();
|
|
|
|
tcpClientConn.close();
|
|
|
|
|
|
|
|
async function sendBytes(
|
|
|
|
conn: Deno.Conn,
|
|
|
|
byte: number,
|
|
|
|
count: number,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
let buf = new Uint8Array(1 << 12 /* 4 kB */);
|
|
|
|
buf.fill(byte);
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
buf = buf.subarray(0, Math.min(buf.length, count));
|
|
|
|
const nwritten = await conn.write(buf);
|
|
|
|
assertStrictEquals(nwritten, buf.length);
|
|
|
|
count -= nwritten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function receiveBytes(
|
|
|
|
conn: Deno.Conn,
|
|
|
|
byte: number,
|
|
|
|
count: number,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
let buf = new Uint8Array(1 << 12 /* 4 kB */);
|
|
|
|
while (count > 0) {
|
|
|
|
buf = buf.subarray(0, Math.min(buf.length, count));
|
|
|
|
const r = await conn.read(buf);
|
|
|
|
assertNotEquals(r, null);
|
|
|
|
assert(buf.subarray(0, r!).every((b) => b === byte));
|
|
|
|
count -= r!;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function receiveEof(conn: Deno.Conn) {
|
|
|
|
const buf = new Uint8Array(1);
|
|
|
|
const r = await conn.read(buf);
|
|
|
|
assertStrictEquals(r, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function forwardBytes(
|
|
|
|
source: Deno.Conn,
|
|
|
|
sink: Deno.Conn,
|
|
|
|
count: number,
|
2023-11-22 06:11:20 -05:00
|
|
|
interruptPromise: ReturnType<typeof Promise.withResolvers<void>>,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
let buf = new Uint8Array(1 << 12 /* 4 kB */);
|
|
|
|
while (count > 0) {
|
|
|
|
buf = buf.subarray(0, Math.min(buf.length, count));
|
2023-11-22 06:11:20 -05:00
|
|
|
const nread = await Promise.race([
|
|
|
|
source.read(buf),
|
|
|
|
interruptPromise.promise,
|
|
|
|
]);
|
2021-05-10 19:49:57 -04:00
|
|
|
if (nread == null) break; // Either EOF or interrupted.
|
|
|
|
const nwritten = await sink.write(buf.subarray(0, nread));
|
|
|
|
assertStrictEquals(nread, nwritten);
|
|
|
|
count -= nwritten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpCorruptionImmediately() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 0, "corruption", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 0, "corruption", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpShutdownImmediately() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 0, "shutdown", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 0, "shutdown", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpCorruptionAfter70Bytes() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 76, "corruption", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 78, "corruption", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpShutdownAfter70bytes() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 77, "shutdown", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 79, "shutdown", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpCorruptionAfter200Bytes() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 200, "corruption", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 202, "corruption", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsHandshakeWithTcpShutdownAfter200bytes() {
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 201, "shutdown", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("handshake", 203, "shutdown", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsTrafficWithTcpCorruption() {
|
|
|
|
await tlsWithTcpFailureTestImpl("traffic", Infinity, "corruption", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("traffic", Infinity, "corruption", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-05-10 19:49:57 -04:00
|
|
|
async function tlsTrafficWithTcpShutdown() {
|
|
|
|
await tlsWithTcpFailureTestImpl("traffic", Infinity, "shutdown", false);
|
|
|
|
await tlsWithTcpFailureTestImpl("traffic", Infinity, "shutdown", true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
function createHttpsListener(): {
|
|
|
|
listener: Deno.TlsListener;
|
|
|
|
hostname: string;
|
|
|
|
port: number;
|
|
|
|
} {
|
2021-05-10 19:49:57 -04:00
|
|
|
// Query format: `curl --insecure https://localhost:8443/z/12345`
|
|
|
|
// The server returns a response consisting of 12345 times the letter 'z'.
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, hostname, port } = listenTls();
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
serve(listener);
|
2024-04-02 20:36:34 -04:00
|
|
|
return { listener, hostname, port };
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
async function serve(listener: Deno.Listener) {
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const EOL = "\r\n";
|
|
|
|
|
|
|
|
// Read GET request plus headers.
|
|
|
|
const buf = new Uint8Array(1 << 12 /* 4 kB */);
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
let req = "";
|
|
|
|
while (!req.endsWith(EOL + EOL)) {
|
|
|
|
const n = await conn.read(buf);
|
|
|
|
if (n === null) throw new Error("Unexpected EOF");
|
|
|
|
req += decoder.decode(buf.subarray(0, n));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse GET request.
|
|
|
|
const { filler, count, version } =
|
|
|
|
/^GET \/(?<filler>[^\/]+)\/(?<count>\d+) HTTP\/(?<version>1\.\d)\r\n/
|
|
|
|
.exec(req)!.groups as {
|
|
|
|
filler: string;
|
|
|
|
count: string;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate response.
|
|
|
|
const resBody = new TextEncoder().encode(filler.repeat(+count));
|
|
|
|
const resHead = new TextEncoder().encode(
|
|
|
|
[
|
|
|
|
`HTTP/${version} 200 OK`,
|
|
|
|
`Content-Length: ${resBody.length}`,
|
|
|
|
"Content-Type: text/plain",
|
|
|
|
].join(EOL) + EOL + EOL,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Send response.
|
2023-11-15 18:12:46 -05:00
|
|
|
await writeAll(conn, resHead);
|
|
|
|
await writeAll(conn, resBody);
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
// Close TCP connection.
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function curl(url: string): Promise<string> {
|
2023-11-15 18:12:46 -05:00
|
|
|
const { success, code, stdout, stderr } = await new Deno.Command("curl", {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: ["--insecure", url],
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2022-07-18 09:16:12 -04:00
|
|
|
if (!success) {
|
2023-11-15 18:12:46 -05:00
|
|
|
throw new Error(
|
|
|
|
`curl ${url} failed: ${code}:\n${new TextDecoder().decode(stderr)}`,
|
|
|
|
);
|
2021-05-10 19:49:57 -04:00
|
|
|
}
|
2022-05-18 16:00:11 -04:00
|
|
|
return new TextDecoder().decode(stdout);
|
2021-05-10 19:49:57 -04:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true, run: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function curlFakeHttpsServer() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, port } = createHttpsListener();
|
2021-05-10 19:49:57 -04:00
|
|
|
|
|
|
|
const res1 = await curl(`https://localhost:${port}/d/1`);
|
|
|
|
assertStrictEquals(res1, "d");
|
|
|
|
|
|
|
|
const res2 = await curl(`https://localhost:${port}/e/12345`);
|
|
|
|
assertStrictEquals(res2, "e".repeat(12345));
|
|
|
|
|
|
|
|
const count3 = 1 << 17; // 128 kB.
|
|
|
|
const res3 = await curl(`https://localhost:${port}/n/${count3}`);
|
|
|
|
assertStrictEquals(res3, "n".repeat(count3));
|
|
|
|
|
|
|
|
const count4 = 12345678;
|
|
|
|
const res4 = await curl(`https://localhost:${port}/o/${count4}`);
|
|
|
|
assertStrictEquals(res4, "o".repeat(count4));
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2023-06-14 21:33:24 -04:00
|
|
|
// Ignored because gmail appears to reject us on CI sometimes
|
|
|
|
{ ignore: true, permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function startTls() {
|
2020-04-18 11:21:20 -04:00
|
|
|
const hostname = "smtp.gmail.com";
|
|
|
|
const port = 587;
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
2022-02-27 09:18:30 -05:00
|
|
|
const conn = await Deno.connect({
|
2020-04-18 11:21:20 -04:00
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
});
|
|
|
|
|
|
|
|
let writer = new BufWriter(conn);
|
|
|
|
let reader = new TextProtoReader(new BufReader(conn));
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
let line: string | null = (await reader.readLine()) as string;
|
2020-04-18 11:21:20 -04:00
|
|
|
assert(line.startsWith("220"));
|
|
|
|
|
|
|
|
await writer.write(encoder.encode(`EHLO ${hostname}\r\n`));
|
|
|
|
await writer.flush();
|
|
|
|
|
|
|
|
while ((line = (await reader.readLine()) as string)) {
|
|
|
|
assert(line.startsWith("250"));
|
|
|
|
if (line.startsWith("250 ")) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await writer.write(encoder.encode("STARTTLS\r\n"));
|
|
|
|
await writer.flush();
|
|
|
|
|
|
|
|
line = await reader.readLine();
|
|
|
|
|
|
|
|
// Received the message that the server is ready to establish TLS
|
|
|
|
assertEquals(line, "220 2.0.0 Ready to start TLS");
|
|
|
|
|
2022-02-27 09:18:30 -05:00
|
|
|
const tlsConn = await Deno.startTls(conn, { hostname });
|
|
|
|
writer = new BufWriter(tlsConn);
|
|
|
|
reader = new TextProtoReader(new BufReader(tlsConn));
|
2020-04-18 11:21:20 -04:00
|
|
|
|
|
|
|
// After that use TLS communication again
|
|
|
|
await writer.write(encoder.encode(`EHLO ${hostname}\r\n`));
|
|
|
|
await writer.flush();
|
|
|
|
|
|
|
|
while ((line = (await reader.readLine()) as string)) {
|
|
|
|
assert(line.startsWith("250"));
|
|
|
|
if (line.startsWith("250 ")) break;
|
|
|
|
}
|
|
|
|
|
2022-02-27 09:18:30 -05:00
|
|
|
tlsConn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-18 11:21:20 -04:00
|
|
|
);
|
2021-08-09 09:55:00 -04:00
|
|
|
|
2024-02-18 09:30:58 -05:00
|
|
|
Deno.test(
|
2024-09-11 21:30:12 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2024-02-18 09:30:58 -05:00
|
|
|
async function connectTLSBadCertKey(): Promise<void> {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.connectTls({
|
|
|
|
hostname: "deno.land",
|
|
|
|
port: 443,
|
|
|
|
cert: "bad data",
|
2024-04-02 20:36:34 -04:00
|
|
|
key: Deno.readTextFileSync(
|
2024-02-18 09:30:58 -05:00
|
|
|
"tests/testdata/tls/localhost.key",
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function connectTLSBadKey(): Promise<void> {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.connectTls({
|
|
|
|
hostname: "deno.land",
|
|
|
|
port: 443,
|
2024-04-02 20:36:34 -04:00
|
|
|
cert: Deno.readTextFileSync(
|
2024-02-18 09:30:58 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
key: "bad data",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
2024-09-11 21:30:12 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2024-02-18 09:30:58 -05:00
|
|
|
async function connectTLSNotKey(): Promise<void> {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.connectTls({
|
|
|
|
hostname: "deno.land",
|
|
|
|
port: 443,
|
2024-04-02 20:36:34 -04:00
|
|
|
cert: Deno.readTextFileSync(
|
2024-02-18 09:30:58 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
key: "",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function connectWithCert() {
|
|
|
|
// The test_server running on port 4552 responds with 'PASS' if client
|
|
|
|
// authentication was successful. Try it by running test_server and
|
|
|
|
// curl --key cli/tests/testdata/tls/localhost.key \
|
|
|
|
// --cert cli/tests/testdata/tls/localhost.crt \
|
|
|
|
// --cacert cli/tests/testdata/tls/RootCA.crt https://localhost:4552/
|
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4552,
|
2024-04-02 20:36:34 -04:00
|
|
|
cert: Deno.readTextFileSync(
|
2024-02-18 09:30:58 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
2024-04-02 20:36:34 -04:00
|
|
|
key: Deno.readTextFileSync(
|
2024-02-18 09:30:58 -05:00
|
|
|
"tests/testdata/tls/localhost.key",
|
|
|
|
),
|
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
|
|
|
});
|
|
|
|
const result = decoder.decode(await readAll(conn));
|
|
|
|
assertEquals(result, "PASS");
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-30 03:26:15 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function connectTLSCaCerts() {
|
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4557,
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
2021-09-30 03:26:15 -04:00
|
|
|
});
|
|
|
|
const result = decoder.decode(await readAll(conn));
|
|
|
|
assertEquals(result, "PASS");
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-30 03:26:15 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function startTLSCaCerts() {
|
|
|
|
const plainConn = await Deno.connect({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4557,
|
|
|
|
});
|
|
|
|
const conn = await Deno.startTls(plainConn, {
|
|
|
|
hostname: "localhost",
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
2021-09-30 03:26:15 -04:00
|
|
|
});
|
|
|
|
const result = decoder.decode(await readAll(conn));
|
|
|
|
assertEquals(result, "PASS");
|
|
|
|
conn.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-19 19:30:04 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function tlsHandshakeSuccess() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener, hostname, port } = listenTls();
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
const connectPromise = Deno.connectTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
2024-09-10 17:55:42 -04:00
|
|
|
caCerts: [await Deno.readTextFile("tests/testdata/tls/RootCA.crt")],
|
2021-10-19 19:30:04 -04:00
|
|
|
});
|
|
|
|
const [conn1, conn2] = await Promise.all([acceptPromise, connectPromise]);
|
|
|
|
listener.close();
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
await Promise.all([conn1.handshake(), conn2.handshake()]);
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
// Begin sending a 10mb blob over the TLS connection.
|
|
|
|
const whole = new Uint8Array(10 << 20); // 10mb.
|
|
|
|
whole.fill(42);
|
2023-11-15 18:12:46 -05:00
|
|
|
const sendPromise = writeAll(conn1, whole);
|
2021-10-19 19:30:04 -04:00
|
|
|
// Set up the other end to receive half of the large blob.
|
|
|
|
const half = new Uint8Array(whole.byteLength / 2);
|
|
|
|
const receivePromise = readFull(conn2, half);
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
await conn1.handshake();
|
|
|
|
await conn2.handshake();
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
// Finish receiving the first 5mb.
|
|
|
|
assertEquals(await receivePromise, half.length);
|
|
|
|
|
|
|
|
// See that we can call `handshake()` in the middle of large reads and writes.
|
2021-10-26 16:27:47 -04:00
|
|
|
await conn1.handshake();
|
|
|
|
await conn2.handshake();
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
// Receive second half of large blob. Wait for the send promise and check it.
|
|
|
|
assertEquals(await readFull(conn2, half), half.length);
|
2023-11-15 18:12:46 -05:00
|
|
|
await sendPromise;
|
2021-10-19 19:30:04 -04:00
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
await conn1.handshake();
|
|
|
|
await conn2.handshake();
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
await conn1.closeWrite();
|
|
|
|
await conn2.closeWrite();
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
await conn1.handshake();
|
|
|
|
await conn2.handshake();
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
conn1.close();
|
|
|
|
conn2.close();
|
|
|
|
|
|
|
|
async function readFull(conn: Deno.Conn, buf: Uint8Array) {
|
|
|
|
let offset, n;
|
|
|
|
for (offset = 0; offset < buf.length; offset += n) {
|
|
|
|
n = await conn.read(buf.subarray(offset, buf.length));
|
|
|
|
assert(n != null && n > 0);
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-19 19:30:04 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function tlsHandshakeFailure() {
|
2024-04-02 20:36:34 -04:00
|
|
|
let tls: { listener: Deno.TlsListener; port: number; hostname: string };
|
2021-10-19 19:30:04 -04:00
|
|
|
|
|
|
|
async function server() {
|
2024-04-02 20:36:34 -04:00
|
|
|
for await (const conn of tls.listener) {
|
2021-10-19 19:30:04 -04:00
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
// Handshake fails because the client rejects the server certificate.
|
|
|
|
await assertRejects(
|
2021-10-26 16:27:47 -04:00
|
|
|
() => conn.handshake(),
|
2021-10-19 19:30:04 -04:00
|
|
|
Deno.errors.InvalidData,
|
2023-05-16 20:19:23 -04:00
|
|
|
"received fatal alert",
|
2021-10-19 19:30:04 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
conn.close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function connectTlsClient() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: tls.hostname,
|
|
|
|
port: tls.port,
|
|
|
|
});
|
2021-10-19 19:30:04 -04:00
|
|
|
// Handshake fails because the server presents a self-signed certificate.
|
|
|
|
await assertRejects(
|
2021-10-26 16:27:47 -04:00
|
|
|
() => conn.handshake(),
|
2021-10-19 19:30:04 -04:00
|
|
|
Deno.errors.InvalidData,
|
2023-11-15 18:12:46 -05:00
|
|
|
"invalid peer certificate: UnknownIssuer",
|
2021-10-19 19:30:04 -04:00
|
|
|
);
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
tls = listenTls();
|
2021-10-19 19:30:04 -04:00
|
|
|
await Promise.all([server(), connectTlsClient()]);
|
|
|
|
|
|
|
|
async function startTlsClient() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const tcpConn = await Deno.connect({
|
|
|
|
hostname: tls.hostname,
|
|
|
|
port: tls.port,
|
|
|
|
});
|
2021-10-19 19:30:04 -04:00
|
|
|
const tlsConn = await Deno.startTls(tcpConn, {
|
|
|
|
hostname: "foo.land",
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
caCerts: [Deno.readTextFileSync("tests/testdata/tls/RootCA.pem")],
|
2021-10-19 19:30:04 -04:00
|
|
|
});
|
|
|
|
// Handshake fails because hostname doesn't match the certificate.
|
|
|
|
await assertRejects(
|
2021-10-26 16:27:47 -04:00
|
|
|
() => tlsConn.handshake(),
|
2021-10-19 19:30:04 -04:00
|
|
|
Deno.errors.InvalidData,
|
2023-05-16 20:19:23 -04:00
|
|
|
"NotValidForName",
|
2021-10-19 19:30:04 -04:00
|
|
|
);
|
|
|
|
tlsConn.close();
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:36:34 -04:00
|
|
|
tls = listenTls();
|
2021-10-19 19:30:04 -04:00
|
|
|
await Promise.all([server(), startTlsClient()]);
|
|
|
|
},
|
|
|
|
);
|
2022-10-24 05:05:07 -04:00
|
|
|
|
|
|
|
Deno.test(
|
2022-10-25 14:15:20 -04:00
|
|
|
{ permissions: { net: true } },
|
2022-10-24 05:05:07 -04:00
|
|
|
async function listenTlsWithReuseAddr() {
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred1 = Promise.withResolvers<void>();
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener: listener1, port, hostname } = listenTls();
|
2022-10-24 05:05:07 -04:00
|
|
|
|
2022-10-25 14:15:20 -04:00
|
|
|
listener1.accept().then((conn) => {
|
|
|
|
conn.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred1.resolve();
|
2022-10-25 14:15:20 -04:00
|
|
|
});
|
2022-10-24 05:05:07 -04:00
|
|
|
|
|
|
|
const conn1 = await Deno.connectTls({ hostname, port, caCerts });
|
|
|
|
conn1.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred1.promise;
|
2022-10-25 14:15:20 -04:00
|
|
|
listener1.close();
|
2022-10-24 05:05:07 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred2 = Promise.withResolvers<void>();
|
2022-10-24 05:05:07 -04:00
|
|
|
const listener2 = Deno.listenTls({ hostname, port, cert, key });
|
|
|
|
|
2022-10-25 14:15:20 -04:00
|
|
|
listener2.accept().then((conn) => {
|
|
|
|
conn.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve();
|
2022-10-25 14:15:20 -04:00
|
|
|
});
|
2022-10-24 05:05:07 -04:00
|
|
|
|
|
|
|
const conn2 = await Deno.connectTls({ hostname, port, caCerts });
|
|
|
|
conn2.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred2.promise;
|
2022-10-25 14:15:20 -04:00
|
|
|
listener2.close();
|
2022-10-24 05:05:07 -04:00
|
|
|
},
|
|
|
|
);
|
2022-10-26 15:04:27 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os !== "linux",
|
|
|
|
permissions: { net: true },
|
|
|
|
}, async function listenTlsReusePort() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener: listener1, port, hostname } = listenTls({
|
2022-10-26 15:04:27 -04:00
|
|
|
reusePort: true,
|
|
|
|
});
|
|
|
|
const listener2 = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
key,
|
|
|
|
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;
|
2024-04-08 18:18:14 -04:00
|
|
|
listener1.close();
|
2022-10-26 15:04:27 -04:00
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
if (!p2) {
|
|
|
|
p2 = listener2.accept().then((conn) => {
|
|
|
|
conn.close();
|
|
|
|
listener2Recv = true;
|
|
|
|
p2 = undefined;
|
2024-04-08 18:18:14 -04:00
|
|
|
listener2.close();
|
2022-10-26 15:04:27 -04:00
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
const conn = await Deno.connectTls({ hostname, port, caCerts });
|
|
|
|
conn.close();
|
|
|
|
await Promise.race([p1, p2]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os === "linux",
|
|
|
|
permissions: { net: true },
|
|
|
|
}, function listenTlsReusePortDoesNothing() {
|
2024-04-02 20:36:34 -04:00
|
|
|
const { listener: listener1, hostname, port } = listenTls({
|
2022-10-26 15:04:27 -04:00
|
|
|
reusePort: true,
|
|
|
|
});
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenTls({ hostname, port, cert, key, reusePort: true });
|
|
|
|
}, Deno.errors.AddrInUse);
|
|
|
|
listener1.close();
|
|
|
|
});
|
2023-07-25 02:26:18 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
permissions: { net: true },
|
|
|
|
}, function listenTlsDoesNotThrowOnStringPort() {
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
// @ts-ignore String port is not allowed by typing, but it shouldn't throw
|
|
|
|
// for backwards compatibility.
|
|
|
|
port: "0",
|
|
|
|
cert,
|
|
|
|
key,
|
|
|
|
});
|
|
|
|
listener.close();
|
|
|
|
});
|
2023-08-14 20:11:12 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
function listenTLSInvalidCert() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
2024-04-02 20:36:34 -04:00
|
|
|
port: 0,
|
2024-09-10 17:55:42 -04:00
|
|
|
cert: Deno.readTextFileSync("tests/testdata/tls/invalid.crt"),
|
|
|
|
key: Deno.readTextFileSync("tests/testdata/tls/localhost.key"),
|
2023-08-14 20:11:12 -04:00
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
function listenTLSInvalidKey() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
2024-04-02 20:36:34 -04:00
|
|
|
port: 0,
|
2024-09-10 17:55:42 -04:00
|
|
|
cert: Deno.readTextFileSync("tests/testdata/tls/localhost.crt"),
|
|
|
|
key: Deno.readTextFileSync("tests/testdata/tls/invalid.key"),
|
2023-08-14 20:11:12 -04:00
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
2024-04-08 13:36:34 -04:00
|
|
|
|
|
|
|
Deno.test(
|
2024-09-10 17:55:42 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
2024-04-08 13:36:34 -04:00
|
|
|
function listenTLSEcKey() {
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 0,
|
2024-09-10 17:55:42 -04:00
|
|
|
cert: Deno.readTextFileSync("tests/testdata/tls/localhost_ecc.crt"),
|
|
|
|
key: Deno.readTextFileSync("tests/testdata/tls/localhost_ecc.key"),
|
2024-04-08 13:36:34 -04:00
|
|
|
});
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|