summaryrefslogtreecommitdiffstats
path: root/toolkit/components/certviewer/tests/browser/browser_openTabAndSendCertInfo.js
blob: 24e9ed97670059289e62cbd1f3d810aea8a8d4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_CERT_BASE64 =
  "MIIGRjCCBS6gAwIBAgIQDJduPkI49CDWPd+G7+u6kDANBgkqhkiG9w0BAQsFADBNMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5EaWdpQ2VydCBTSEEyIFNlY3VyZSBTZXJ2ZXIgQ0EwHhcNMTgxMTA1MDAwMDAwWhcNMTkxMTEzMTIwMDAwWjCBgzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxHDAaBgNVBAoTE01vemlsbGEgQ29ycG9y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TZXJ2ZXJDQS5jcnQwDAYDVR0TAQH/BAIwADCCAQIGCisGAQQB1nkCBAIEgfMEgfAA7gB1AKS5CZC0GFgUh7sTosxncAo8NZgE+RvfuON3zQ7IDdwQAAABZuYWiHwAAAQDAEYwRAIgZnMSH1JdG6NASHWTwD0mlP/zbr0hzP263c02Ym0DU64CIEe4QHJDP47j0b6oTFu6RrZz1NQ9cq8Az1KnMKRuaFAlAHUAh3W/51l8+IxDmV+9827/Vo1HVjb/SrVgwbTq/16ggw8AAAFm5haJAgAABAMARjBEAiAxGLXkUaOAkZhXNeNR3pWyahZeKmSaMXadgu18SfK1ZAIgKtwu5eGxK76rgaszLCZ9edBIjuU0DKorzPUuxUXFY0QwDQYJKoZIhvcNAQELBQADggEBAKLJAFO3wuaP5MM/ed1lhk5Uc2aDokhcM7XyvdhEKSHbgPhcgMoT9YIVoPa70gNC6KHcwoXu0g8wt7X6Vm1ql/68G5q844kFuC6JPl4LVT9mciD+VW6bHUSXD9xifL9DqdJ0Ic0SllTlM+oq5aAeOxUQGXhXIqj6fSQv9fQN6mXxQIoc/gjxteskq/Vl8YmY1FIZP9Bh7g27kxZ9GAAGQtjTL03RzKAuSg6yeImYVdQWasc7UPnBXlRAzZ8+OJThUbzK16a2CI3Rg4agKSJk+uA47h1/ImmngpFLRb/MvRX6H1oWcUuyH6O7PZdl0YpwTpw1THIuqCGl/wpPgyQgcTM=";

function checkSpec(spec) {
  Assert.ok(
    spec.startsWith("about:certificate"),
    "about:certificate was opened"
  );

  let newUrl = new URL(spec);
  let certEncoded = newUrl.searchParams.get("cert");
  let certDecoded = decodeURIComponent(certEncoded);
  Assert.equal(
    btoa(atob(certDecoded)),
    certDecoded,
    "We received an base64 string"
  );
}

function checksCertTab(tabsCount) {
  Assert.equal(gBrowser.tabs.length, tabsCount + 1, "New tab was opened");
  let spec = gBrowser.tabs[tabsCount].linkedBrowser.documentURI.spec;
  checkSpec(spec);
}

async function checkCertChain(browser) {
  await SpecialPowers.spawn(browser, [], async function () {
    let certificateTabs;
    await ContentTaskUtils.waitForCondition(() => {
      let certificateSection = content.document.querySelector(
        "certificate-section"
      );
      if (!certificateSection) {
        return false;
      }
      certificateTabs =
        certificateSection.shadowRoot.querySelectorAll(".certificate-tab");
      return certificateTabs.length;
    }, "Found certificate tabs.");

    Assert.greaterOrEqual(
      certificateTabs.length,
      1,
      "Certificate chain tabs shown"
    );
  });
}

// taken from https://searchfox.org/mozilla-central/rev/7ed8e2d3d1d7a1464ba42763a33fd2e60efcaedc/security/manager/ssl/tests/mochitest/browser/browser_downloadCert_ui.js#47
function openCertDownloadDialog(cert) {
  let returnVals = Cc["@mozilla.org/hash-property-bag;1"].createInstance(
    Ci.nsIWritablePropertyBag2
  );
  let win = window.openDialog(
    "chrome://pippki/content/downloadcert.xhtml",
    "",
    "",
    cert,
    returnVals
  );
  return new Promise(resolve => {
    win.addEventListener(
      "load",
      function () {
        executeSoon(() => resolve([win]));
      },
      { once: true }
    );
  });
}

add_task(async function openFromPopUp() {
  info("Testing openFromPopUp");

  const certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  let cert = certdb.constructX509FromBase64(TEST_CERT_BASE64);

  let [win] = await openCertDownloadDialog(cert);
  let viewCertButton = win.document.getElementById("viewC-button");
  let newWinOpened = BrowserTestUtils.waitForNewWindow();
  viewCertButton.click();

  let topWin = await newWinOpened;
  let browser = topWin.gBrowser.selectedBrowser;

  // We're racing against messages sent up from the content process that
  // loads about:certificate. It may or may not have had the opportunity
  // to tell the parent that it had loaded the page yet. If not, we wait
  // for the page to load.
  //
  // Note that we can't use the URL parameter for
  // BrowserTestUtils.waitForNewWindow, since we need to use a function
  // to choose the right URL.
  if (!browser.currentURI.spec.startsWith("about:certificate")) {
    await BrowserTestUtils.browserLoaded(browser, false, spec =>
      spec.startsWith("about:certificate")
    );
  }

  let spec = browser.currentURI.spec;
  checkSpec(spec);

  await checkCertChain(browser);

  await BrowserTestUtils.closeWindow(topWin); // closes about:certificate
  win.document.getElementById("download_cert").cancelDialog();
  await BrowserTestUtils.windowClosed(win);
});

add_task(async function testBadCert() {
  info("Testing bad cert");

  let tab = await openErrorPage();

  let tabsCount = gBrowser.tabs.length;
  let loaded = BrowserTestUtils.waitForNewTab(gBrowser, null, true);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    let advancedButton = content.document.getElementById("advancedButton");
    Assert.ok(advancedButton, "advancedButton found");
    Assert.equal(
      advancedButton.hasAttribute("disabled"),
      false,
      "advancedButton should be clickable"
    );
    advancedButton.click();
    let viewCertificate = content.document.getElementById("viewCertificate");
    Assert.ok(viewCertificate, "viewCertificate found");
    Assert.equal(
      viewCertificate.hasAttribute("disabled"),
      false,
      "viewCertificate should be clickable"
    );

    viewCertificate.click();
  });
  await loaded;
  checksCertTab(tabsCount);
  await checkCertChain(gBrowser.selectedBrowser);

  gBrowser.removeCurrentTab(); // closes about:certificate
  gBrowser.removeCurrentTab(); // closes https://expired.example.com/
});

add_task(async function testBadCertIframe() {
  info("Testing bad cert in an iframe");

  let tab = await openErrorPage(true);

  let tabsCount = gBrowser.tabs.length;
  let loaded = BrowserTestUtils.waitForNewTab(gBrowser, null, true);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    let doc = content.document.querySelector("iframe").contentDocument;
    let advancedButton = doc.getElementById("advancedButton");
    Assert.ok(advancedButton, "advancedButton found");
    Assert.equal(
      advancedButton.hasAttribute("disabled"),
      false,
      "advancedButton should be clickable"
    );
    advancedButton.click();
    let viewCertificate = doc.getElementById("viewCertificate");
    Assert.ok(viewCertificate, "viewCertificate found");
    Assert.equal(
      viewCertificate.hasAttribute("disabled"),
      false,
      "viewCertificate should be clickable"
    );

    viewCertificate.click();
  });
  await loaded;
  checksCertTab(tabsCount);
  await checkCertChain(gBrowser.selectedBrowser);

  gBrowser.removeCurrentTab(); // closes about:certificate
  gBrowser.removeCurrentTab(); // closes https://expired.example.com/
});

add_task(async function testGoodCert() {
  info("Testing page info");
  let url = "https://example.com/";

  let tabsCount = gBrowser.tabs.length;

  info(`Loading ${url}`);
  await BrowserTestUtils.withNewTab({ gBrowser, url }, async function () {
    info("Opening pageinfo");
    let pageInfo = BrowserCommands.pageInfo(url, "securityTab", {});
    await BrowserTestUtils.waitForEvent(pageInfo, "load");

    let securityTab = pageInfo.document.getElementById("securityTab");
    await TestUtils.waitForCondition(
      () => BrowserTestUtils.isVisible(securityTab),
      "Security tab should be visible."
    );
    Assert.ok(securityTab, "Security tab is available");
    let viewCertButton = pageInfo.document.getElementById("security-view-cert");
    await TestUtils.waitForCondition(
      () => BrowserTestUtils.isVisible(viewCertButton),
      "view cert button should be visible."
    );

    let loaded = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
    checkAndClickButton(pageInfo.document, "security-view-cert");
    await loaded;

    await checkCertChain(gBrowser.selectedBrowser);
    pageInfo.close();
  });
  checksCertTab(tabsCount);

  gBrowser.removeCurrentTab(); // closes about:certificate
});

add_task(async function testPreferencesCert() {
  info("Testing preferences cert");
  let url = "about:preferences#privacy";

  let tabsCount;

  info(`Loading ${url}`);
  await BrowserTestUtils.withNewTab(
    { gBrowser, url },
    async function (browser) {
      tabsCount = gBrowser.tabs.length;
      checkAndClickButton(browser.contentDocument, "viewCertificatesButton");

      let certDialogLoaded = promiseLoadSubDialog(
        "chrome://pippki/content/certManager.xhtml"
      );
      let dialogWin = await certDialogLoaded;
      let doc = dialogWin.document;
      Assert.ok(doc, "doc loaded");

      doc.getElementById("certmanagertabs").selectedTab =
        doc.getElementById("mine_tab");
      let treeView = doc.getElementById("user-tree").view;
      let selectedCert;
      // See https://searchfox.org/mozilla-central/rev/40ef22080910c2e2c27d9e2120642376b1d8b8b2/browser/components/preferences/in-content/tests/browser_cert_export.js#41
      for (let i = 0; i < treeView.rowCount; i++) {
        treeView.selection.select(i);
        dialogWin.getSelectedCerts();
        let certs = dialogWin.selected_certs;
        if (certs && certs[0]) {
          selectedCert = certs[0];
          break;
        }
      }
      Assert.ok(selectedCert, "A cert should be selected");
      let viewButton = doc.getElementById("mine_viewButton");
      Assert.equal(viewButton.disabled, false, "Should enable view button");

      let loaded = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
      viewButton.click();
      await loaded;

      checksCertTab(tabsCount);
      await checkCertChain(gBrowser.selectedBrowser);
    }
  );
  gBrowser.removeCurrentTab(); // closes about:certificate
});