Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / test / unit / test_cert_info.js
blobabf5a1d6344ad474eae07f4f1b5df2624c00f154
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 /* import-globals-from head_cache.js */
8 /* import-globals-from head_cookies.js */
9 /* import-globals-from head_channels.js */
10 /* import-globals-from head_servers.js */
12 function makeChan(uri) {
13   let chan = NetUtil.newChannel({
14     uri,
15     loadUsingSystemPrincipal: true,
16   }).QueryInterface(Ci.nsIHttpChannel);
17   chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
18   return chan;
21 async function test_cert_failure(server_creator, https_proxy) {
22   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
23     Ci.nsIX509CertDB
24   );
25   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
26   addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
28   let server = new server_creator();
29   await server.start();
30   registerCleanupFunction(async () => {
31     await server.stop();
32   });
34   await server.registerPathHandler("/test", (req, resp) => {
35     resp.writeHead(200);
36     resp.end("done");
37   });
38   let url;
39   if (server_creator == NodeHTTPServer) {
40     url = `http://localhost:${server.port()}/test`;
41   } else {
42     url = `https://localhost:${server.port()}/test`;
43   }
44   let chan = makeChan(url);
45   let req = await new Promise(resolve => {
46     chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
47   });
48   equal(req.status, Cr.NS_OK);
49   equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
50   let secinfo = req.securityInfo;
51   if (server_creator == NodeHTTPServer) {
52     if (!https_proxy) {
53       Assert.equal(secinfo, null);
54     } else {
55       // In the case we are connecting to an insecure HTTP server
56       // through a secure proxy, nsHttpChannel will have the security
57       // info from the proxy.
58       // We will discuss this behavir in bug 1785777.
59       secinfo.QueryInterface(Ci.nsITransportSecurityInfo);
60       Assert.equal(secinfo.serverCert.commonName, " Proxy Test Cert");
61     }
62   } else {
63     secinfo.QueryInterface(Ci.nsITransportSecurityInfo);
64     Assert.equal(secinfo.serverCert.commonName, " HTTP2 Test Cert");
65   }
68 add_task(async function test_http() {
69   await test_cert_failure(NodeHTTPServer, false);
70 });
72 add_task(async function test_https() {
73   await test_cert_failure(NodeHTTPSServer, false);
74 });
76 add_task(async function test_http2() {
77   await test_cert_failure(NodeHTTP2Server, false);
78 });
80 add_task(async function test_http_proxy_http_server() {
81   let proxy = new NodeHTTPProxyServer();
82   await proxy.start();
83   registerCleanupFunction(() => {
84     proxy.stop();
85   });
86   await test_cert_failure(NodeHTTPServer, false);
87 });
89 add_task(async function test_http_proxy_https_server() {
90   let proxy = new NodeHTTPProxyServer();
91   await proxy.start();
92   registerCleanupFunction(() => {
93     proxy.stop();
94   });
95   await test_cert_failure(NodeHTTPSServer, false);
96 });
98 add_task(async function test_http_proxy_http2_server() {
99   let proxy = new NodeHTTPProxyServer();
100   await proxy.start();
101   registerCleanupFunction(() => {
102     proxy.stop();
103   });
104   await test_cert_failure(NodeHTTP2Server, false);
107 add_task(async function test_https_proxy_http_server() {
108   let proxy = new NodeHTTPSProxyServer();
109   await proxy.start();
110   registerCleanupFunction(() => {
111     proxy.stop();
112   });
113   await test_cert_failure(NodeHTTPServer, true);
116 add_task(async function test_https_proxy_https_server() {
117   let proxy = new NodeHTTPSProxyServer();
118   await proxy.start();
119   registerCleanupFunction(() => {
120     proxy.stop();
121   });
122   await test_cert_failure(NodeHTTPSServer, true);
125 add_task(async function test_https_proxy_http2_server() {
126   let proxy = new NodeHTTPSProxyServer();
127   await proxy.start();
128   registerCleanupFunction(() => {
129     proxy.stop();
130   });
131   await test_cert_failure(NodeHTTP2Server, true);
134 add_task(async function test_http2_proxy_http_server() {
135   let proxy = new NodeHTTP2ProxyServer();
136   await proxy.start();
137   registerCleanupFunction(() => {
138     proxy.stop();
139   });
141   await test_cert_failure(NodeHTTPServer, true);
144 add_task(async function test_http2_proxy_https_server() {
145   let proxy = new NodeHTTP2ProxyServer();
146   await proxy.start();
147   registerCleanupFunction(() => {
148     proxy.stop();
149   });
151   await test_cert_failure(NodeHTTPSServer, true);
154 add_task(async function test_http2_proxy_http2_server() {
155   let proxy = new NodeHTTP2ProxyServer();
156   await proxy.start();
157   registerCleanupFunction(() => {
158     proxy.stop();
159   });
161   await test_cert_failure(NodeHTTP2Server, true);