2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-29 12:45:47 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-03-04 11:31:14 -05:00
|
|
|
createResolvable,
|
|
|
|
unitTest
|
2020-02-29 12:45:47 -05:00
|
|
|
} from "./test_util.ts";
|
2020-03-09 20:06:47 -04:00
|
|
|
import { BufWriter, BufReader } from "../../../std/io/bufio.ts";
|
|
|
|
import { TextProtoReader } from "../../../std/textproto/mod.ts";
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
2019-09-23 14:40:38 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function connectTLSNoPerm(): Promise<void> {
|
2019-09-23 14:40:38 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2020-01-18 12:35:12 -05:00
|
|
|
await Deno.connectTLS({ hostname: "github.com", port: 443 });
|
2019-09-23 14:40:38 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-09-23 14:40:38 -04:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function connectTLSCertFileNoReadPerm(): Promise<void> {
|
2019-10-21 14:38:28 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2020-01-18 12:35:12 -05:00
|
|
|
await Deno.connectTLS({
|
2019-10-21 14:38:28 -04:00
|
|
|
hostname: "github.com",
|
|
|
|
port: 443,
|
|
|
|
certFile: "cli/tests/tls/RootCA.crt"
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-10-21 14:38:28 -04:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, net: true } },
|
2019-10-21 14:38:28 -04:00
|
|
|
async function listenTLSNonExistentCertKeyFiles(): Promise<void> {
|
|
|
|
let err;
|
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4500,
|
|
|
|
certFile: "cli/tests/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/tls/localhost.key"
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
Deno.listenTLS({
|
|
|
|
...options,
|
|
|
|
certFile: "./non/existent/file"
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
Deno.listenTLS({
|
|
|
|
...options,
|
|
|
|
keyFile: "./non/existent/file"
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function listenTLSNoReadPerm(): Promise<void> {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.listenTLS({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4500,
|
|
|
|
certFile: "cli/tests/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/tls/localhost.key"
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-10-21 14:38:28 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { read: true, write: true, net: true }
|
|
|
|
},
|
2019-10-21 14:38:28 -04:00
|
|
|
async function listenTLSEmptyKeyFile(): Promise<void> {
|
|
|
|
let err;
|
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4500,
|
|
|
|
certFile: "cli/tests/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/tls/localhost.key"
|
|
|
|
};
|
|
|
|
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const keyFilename = testDir + "/key.pem";
|
|
|
|
Deno.writeFileSync(keyFilename, new Uint8Array([]), {
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: 0o666
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Deno.listenTLS({
|
|
|
|
...options,
|
|
|
|
keyFile: keyFilename
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true, net: true } },
|
2019-10-21 14:38:28 -04:00
|
|
|
async function listenTLSEmptyCertFile(): Promise<void> {
|
|
|
|
let err;
|
|
|
|
const options = {
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4500,
|
|
|
|
certFile: "cli/tests/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/tls/localhost.key"
|
|
|
|
};
|
|
|
|
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const certFilename = testDir + "/cert.crt";
|
|
|
|
Deno.writeFileSync(certFilename, new Uint8Array([]), {
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: 0o666
|
2019-10-21 14:38:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Deno.listenTLS({
|
|
|
|
...options,
|
|
|
|
certFile: certFilename
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, net: true } },
|
|
|
|
async function dialAndListenTLS(): Promise<void> {
|
|
|
|
const resolvable = createResolvable();
|
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 4500;
|
|
|
|
|
|
|
|
const listener = Deno.listenTLS({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
certFile: "cli/tests/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/tls/localhost.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): Promise<void> => {
|
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr != null);
|
|
|
|
await conn.write(response);
|
|
|
|
// TODO(bartlomieju): this might be a bug
|
|
|
|
setTimeout(() => {
|
|
|
|
conn.close();
|
|
|
|
resolvable.resolve();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const conn = await Deno.connectTLS({
|
|
|
|
hostname,
|
|
|
|
port,
|
|
|
|
certFile: "cli/tests/tls/RootCA.pem"
|
|
|
|
});
|
|
|
|
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 !== Deno.EOF, `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");
|
|
|
|
const headers = await tpr.readMIMEHeader();
|
|
|
|
assert(headers !== Deno.EOF);
|
|
|
|
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();
|
|
|
|
await resolvable;
|
|
|
|
}
|
|
|
|
);
|