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";
|
2023-01-15 15:09:26 -05:00
|
|
|
import { BufReader, BufWriter } from "../../../test_util/std/io/mod.ts";
|
|
|
|
import { readAll } from "../../../test_util/std/streams/read_all.ts";
|
2023-11-15 18:12:46 -05:00
|
|
|
import { writeAll } from "../../../test_util/std/streams/write_all.ts";
|
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();
|
2022-02-23 23:16:56 -05:00
|
|
|
const cert = await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt");
|
|
|
|
const key = await Deno.readTextFile("cli/tests/testdata/tls/localhost.key");
|
|
|
|
const caCerts = [await Deno.readTextFile("cli/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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
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
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: false } },
|
|
|
|
async function connectTLSCertFileNoReadPerm() {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.connectTls({
|
|
|
|
hostname: "deno.land",
|
|
|
|
port: 443,
|
|
|
|
certFile: "cli/tests/testdata/tls/RootCA.crt",
|
|
|
|
});
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|
2019-10-21 14:38:28 -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
|
|
|
function listenTLSNonExistentCertKeyFiles() {
|
2019-10-21 14:38:28 -04:00
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
2020-05-11 14:49:19 -04:00
|
|
|
port: 3500,
|
2021-08-11 10:20:47 -04:00
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
2019-10-21 14:38:28 -04:00
|
|
|
};
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-24 17:29:14 -04:00
|
|
|
Deno.listenTls({
|
2019-10-21 14:38:28 -04:00
|
|
|
...options,
|
2020-03-28 13:03:49 -04:00
|
|
|
certFile: "./non/existent/file",
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-10-21 14:38:28 -04:00
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-24 17:29:14 -04:00
|
|
|
Deno.listenTls({
|
2019-10-21 14:38:28 -04:00
|
|
|
...options,
|
2020-03-28 13:03:49 -04:00
|
|
|
keyFile: "./non/existent/file",
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-10-21 14:38:28 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: false } },
|
|
|
|
function listenTLSNoReadPerm() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 3500,
|
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
|
|
|
});
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|
2019-10-21 14:38:28 -04: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: { read: true, write: true, net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function listenTLSEmptyKeyFile() {
|
2019-10-21 14:38:28 -04:00
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
2020-05-11 14:49:19 -04:00
|
|
|
port: 3500,
|
2021-08-11 10:20:47 -04:00
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
2019-10-21 14:38:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const keyFilename = testDir + "/key.pem";
|
|
|
|
Deno.writeFileSync(keyFilename, new Uint8Array([]), {
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o666,
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-24 17:29:14 -04:00
|
|
|
Deno.listenTls({
|
2019-10-21 14:38:28 -04:00
|
|
|
...options,
|
2020-03-28 13:03:49 -04:00
|
|
|
keyFile: keyFilename,
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Error);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-10-21 14:38:28 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function listenTLSEmptyCertFile() {
|
2019-10-21 14:38:28 -04:00
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
2020-05-11 14:49:19 -04:00
|
|
|
port: 3500,
|
2021-08-11 10:20:47 -04:00
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
2019-10-21 14:38:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const certFilename = testDir + "/cert.crt";
|
|
|
|
Deno.writeFileSync(certFilename, new Uint8Array([]), {
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o666,
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-24 17:29:14 -04:00
|
|
|
Deno.listenTls({
|
2019-10-21 14:38:28 -04:00
|
|
|
...options,
|
2020-03-28 13:03:49 -04:00
|
|
|
certFile: certFilename,
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Error);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-10-21 14:38:28 -04:00
|
|
|
);
|
|
|
|
|
2022-10-17 22:28:27 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function startTlsWithoutExclusiveAccessToTcpConn() {
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = getPort();
|
|
|
|
|
|
|
|
const tcpListener = Deno.listen({ hostname, port });
|
|
|
|
const [serverConn, clientConn] = await Promise.all([
|
|
|
|
tcpListener.accept(),
|
|
|
|
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();
|
|
|
|
tcpListener.close();
|
|
|
|
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>();
|
2020-03-04 11:31:14 -05:00
|
|
|
const hostname = "localhost";
|
2020-05-11 14:49:19 -04:00
|
|
|
const port = 3500;
|
2020-03-04 11:31:14 -05:00
|
|
|
|
2020-04-24 17:29:14 -04:00
|
|
|
const listener = Deno.listenTls({
|
2020-03-04 11:31:14 -05:00
|
|
|
hostname,
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"),
|
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 });
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
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
|
|
|
},
|
|
|
|
);
|
|
|
|
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
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 3500;
|
|
|
|
|
|
|
|
const listener = Deno.listenTls({ hostname, port, cert, key });
|
|
|
|
|
|
|
|
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
|
|
|
assert(conn.rid > 0);
|
|
|
|
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
|
|
|
let nextPort = 3501;
|
|
|
|
function getPort() {
|
|
|
|
return nextPort++;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function tlsPair(): Promise<[Deno.Conn, Deno.Conn]> {
|
|
|
|
const port = getPort();
|
2021-01-14 23:32:27 -05:00
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"),
|
2021-01-14 23:32:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
const connectPromise = Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts: [Deno.readTextFileSync("cli/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]> {
|
|
|
|
const port = getPort();
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"),
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols: ["deno", "rocks"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
|
|
|
|
const caCerts = [Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem")];
|
|
|
|
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(
|
|
|
|
`Got an error (${e.message}) after reading ${nread}/${largeAmount} bytes`,
|
|
|
|
{ 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
|
|
|
) {
|
2021-05-10 19:49:57 -04:00
|
|
|
const tlsPort = getPort();
|
|
|
|
const tlsListener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: tlsPort,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"),
|
2021-05-10 19:49:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const tcpPort = getPort();
|
|
|
|
const tcpListener = Deno.listen({ hostname: "localhost", port: tcpPort });
|
|
|
|
|
|
|
|
const [tlsServerConn, tcpServerConn] = await Promise.all([
|
|
|
|
tlsListener.accept(),
|
|
|
|
Deno.connect({ hostname: "localhost", port: tlsPort }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const [tcpClientConn, tlsClientConn] = await Promise.all([
|
|
|
|
tcpListener.accept(),
|
|
|
|
Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: tcpPort,
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts: [Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem")],
|
2021-05-10 19:49:57 -04:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
tlsListener.close();
|
|
|
|
tcpListener.close();
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
function createHttpsListener(port: number): Deno.Listener {
|
|
|
|
// Query format: `curl --insecure https://localhost:8443/z/12345`
|
|
|
|
// The server returns a response consisting of 12345 times the letter 'z'.
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: Deno.readTextFileSync("./cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: Deno.readTextFileSync("./cli/tests/testdata/tls/localhost.key"),
|
2021-05-10 19:49:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
serve(listener);
|
|
|
|
return listener;
|
|
|
|
|
|
|
|
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() {
|
2021-05-10 19:49:57 -04:00
|
|
|
const port = getPort();
|
|
|
|
const listener = createHttpsListener(port);
|
|
|
|
|
|
|
|
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
|
|
|
|
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-09 09:55:00 -04:00
|
|
|
async function connectTLSBadClientCertPrivateKey(): Promise<void> {
|
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,
|
|
|
|
certChain: "bad data",
|
2021-08-11 10:20:47 -04:00
|
|
|
privateKey: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.key",
|
|
|
|
),
|
2021-08-09 09:55:00 -04:00
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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-09 09:55:00 -04:00
|
|
|
async function connectTLSBadPrivateKey(): Promise<void> {
|
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,
|
2021-08-11 10:20:47 -04:00
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
2021-08-09 09:55:00 -04:00
|
|
|
privateKey: "bad data",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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-09 09:55:00 -04:00
|
|
|
async function connectTLSNotPrivateKey(): Promise<void> {
|
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,
|
2021-08-11 10:20:47 -04:00
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
2021-08-09 09:55:00 -04:00
|
|
|
privateKey: "",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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-09 09:55:00 -04:00
|
|
|
async function connectWithClientCert() {
|
|
|
|
// The test_server running on port 4552 responds with 'PASS' if client
|
|
|
|
// authentication was successful. Try it by running test_server and
|
2021-08-11 10:20:47 -04:00
|
|
|
// 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/
|
2021-08-09 09:55:00 -04:00
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4552,
|
2021-08-11 10:20:47 -04:00
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
privateKey: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.key",
|
|
|
|
),
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts: [Deno.readTextFileSync("cli/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,
|
|
|
|
caCerts: [Deno.readTextFileSync("cli/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 connectTLSCertFile() {
|
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4557,
|
|
|
|
certFile: "cli/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 startTLSCaCerts() {
|
|
|
|
const plainConn = await Deno.connect({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4557,
|
|
|
|
});
|
|
|
|
const conn = await Deno.startTls(plainConn, {
|
|
|
|
hostname: "localhost",
|
|
|
|
caCerts: [Deno.readTextFileSync("cli/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-10-19 19:30:04 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
|
|
|
async function tlsHandshakeSuccess() {
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = getPort();
|
|
|
|
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"),
|
2021-10-19 19:30:04 -04:00
|
|
|
});
|
|
|
|
const acceptPromise = listener.accept();
|
|
|
|
const connectPromise = Deno.connectTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
certFile: "cli/tests/testdata/tls/RootCA.crt",
|
|
|
|
});
|
|
|
|
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() {
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = getPort();
|
|
|
|
|
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
2024-01-23 11:35:23 -05:00
|
|
|
cert: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.crt"),
|
|
|
|
key: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.key"),
|
2021-10-19 19:30:04 -04:00
|
|
|
});
|
|
|
|
for await (const conn of listener) {
|
|
|
|
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() {
|
|
|
|
const conn = await Deno.connectTls({ hostname, port });
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([server(), connectTlsClient()]);
|
|
|
|
|
|
|
|
async function startTlsClient() {
|
|
|
|
const tcpConn = await Deno.connect({ hostname, port });
|
|
|
|
const tlsConn = await Deno.startTls(tcpConn, {
|
|
|
|
hostname: "foo.land",
|
2021-10-29 11:13:31 -04:00
|
|
|
caCerts: [Deno.readTextFileSync("cli/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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
2022-10-24 05:05:07 -04:00
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 3500;
|
|
|
|
|
|
|
|
const listener1 = Deno.listenTls({ hostname, port, cert, key });
|
|
|
|
|
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() {
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 4003;
|
|
|
|
const listener1 = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
key,
|
|
|
|
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;
|
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
if (!p2) {
|
|
|
|
p2 = listener2.accept().then((conn) => {
|
|
|
|
conn.close();
|
|
|
|
listener2Recv = true;
|
|
|
|
p2 = undefined;
|
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
const conn = await Deno.connectTls({ hostname, port, caCerts });
|
|
|
|
conn.close();
|
|
|
|
await Promise.race([p1, p2]);
|
|
|
|
}
|
|
|
|
listener1.close();
|
|
|
|
listener2.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
ignore: Deno.build.os === "linux",
|
|
|
|
permissions: { net: true },
|
|
|
|
}, function listenTlsReusePortDoesNothing() {
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 4003;
|
|
|
|
const listener1 = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
key,
|
|
|
|
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",
|
|
|
|
port: 3500,
|
|
|
|
certFile: "cli/tests/testdata/tls/invalid.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
function listenTLSInvalidKey() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.listenTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 3500,
|
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/invalid.key",
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|