Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_cert_verification_failure.js
blob11eb575dc1f2bc73565168f5b103fb336952147b
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_or_proxy, server_cert) {
22   let server = new server_or_proxy();
23   await server.start();
24   registerCleanupFunction(async () => {
25     await server.stop();
26   });
27   let chan = makeChan(`https://localhost:${server.port()}/test`);
28   let req = await new Promise(resolve => {
29     chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
30   });
31   equal(req.status, 0x805a1ff3); // SEC_ERROR_UNKNOWN_ISSUER
32   let secinfo = req.securityInfo;
33   secinfo.QueryInterface(Ci.nsITransportSecurityInfo);
34   if (server_cert) {
35     Assert.equal(secinfo.serverCert.commonName, " HTTP2 Test Cert");
36   } else {
37     Assert.equal(secinfo.serverCert.commonName, " Proxy Test Cert");
38   }
41 add_task(async function test_https() {
42   await test_cert_failure(NodeHTTPSServer, true);
43 });
45 add_task(async function test_http2() {
46   await test_cert_failure(NodeHTTP2Server, true);
47 });
49 add_task(async function test_https_proxy() {
50   let proxy = new NodeHTTPSProxyServer();
51   await proxy.start();
52   registerCleanupFunction(() => {
53     proxy.stop();
54   });
55   await test_cert_failure(NodeHTTPSServer, false);
56 });
58 add_task(async function test_http2_proxy() {
59   let proxy = new NodeHTTP2ProxyServer();
60   await proxy.start();
61   registerCleanupFunction(() => {
62     proxy.stop();
63   });
65   await test_cert_failure(NodeHTTPSServer, false);
66 });