diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-10-21 20:38:28 +0200 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-10-21 14:38:28 -0400 |
commit | 6c5a981fd2afad21af73a1345c4e30fb6b30b09a (patch) | |
tree | c6065fe502cc99f29d7f5554257729552920f7f4 /cli/js/tls_test.ts | |
parent | 1f52c66ced9bed0cae6bff065dfa7563cbfaee29 (diff) |
feat: Deno.listenTLS (#3152)
Diffstat (limited to 'cli/js/tls_test.ts')
-rw-r--r-- | cli/js/tls_test.ts | 171 |
1 files changed, 163 insertions, 8 deletions
diff --git a/cli/js/tls_test.ts b/cli/js/tls_test.ts index 79e6bcad8..58cafd23e 100644 --- a/cli/js/tls_test.ts +++ b/cli/js/tls_test.ts @@ -3,8 +3,9 @@ import { test, testPerm, assert, assertEquals } from "./test_util.ts"; import { BufWriter, BufReader } from "../../std/io/bufio.ts"; import { TextProtoReader } from "../../std/textproto/mod.ts"; import { runIfMain } from "../../std/testing/mod.ts"; -// TODO(ry) The tests in this file use github.com:443, but it would be better to -// not rely on an internet connection and rather use a localhost TLS server. + +const encoder = new TextEncoder(); +const decoder = new TextDecoder(); test(async function dialTLSNoPerm(): Promise<void> { let err; @@ -17,15 +18,168 @@ test(async function dialTLSNoPerm(): Promise<void> { assertEquals(err.name, "PermissionDenied"); }); -testPerm({ net: true }, async function dialTLSBasic(): Promise<void> { - const conn = await Deno.dialTLS({ hostname: "github.com", port: 443 }); +test(async function dialTLSCertFileNoReadPerm(): Promise<void> { + let err; + try { + await Deno.dialTLS({ + hostname: "github.com", + port: 443, + certFile: "cli/tests/tls/RootCA.crt" + }); + } catch (e) { + err = e; + } + assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assertEquals(err.name, "PermissionDenied"); +}); + +testPerm( + { read: true, net: true }, + 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; + } + assertEquals(err.kind, Deno.ErrorKind.NotFound); + assertEquals(err.name, "NotFound"); + + try { + Deno.listenTLS({ + ...options, + keyFile: "./non/existent/file" + }); + } catch (e) { + err = e; + } + assertEquals(err.kind, Deno.ErrorKind.NotFound); + assertEquals(err.name, "NotFound"); + } +); + +testPerm({ 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; + } + assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assertEquals(err.name, "PermissionDenied"); +}); + +testPerm( + { read: true, write: true, net: true }, + 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([]), { + perm: 0o666 + }); + + try { + Deno.listenTLS({ + ...options, + keyFile: keyFilename + }); + } catch (e) { + err = e; + } + assertEquals(err.kind, Deno.ErrorKind.Other); + assertEquals(err.name, "Other"); + } +); + +testPerm( + { read: true, write: true, net: true }, + 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([]), { + perm: 0o666 + }); + + try { + Deno.listenTLS({ + ...options, + certFile: certFilename + }); + } catch (e) { + err = e; + } + assertEquals(err.kind, Deno.ErrorKind.Other); + assertEquals(err.name, "Other"); + } +); + +testPerm({ read: true, net: true }, async function dialAndListenTLS(): Promise< + void +> { + 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); + conn.close(); + } + ); + + const conn = await Deno.dialTLS({ + hostname, + port, + certFile: "cli/tests/tls/RootCA.pem" + }); assert(conn.rid > 0); const w = new BufWriter(conn); const r = new BufReader(conn); - let body = "GET / HTTP/1.1\r\n"; - body += "Host: github.com\r\n"; - body += "\r\n"; - const writeResult = await w.write(new TextEncoder().encode(body)); + 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); @@ -41,6 +195,7 @@ testPerm({ net: true }, async function dialTLSBasic(): Promise<void> { 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(); }); |