1 // Any copyright is dedicated to the Public Domain.
2 // http://creativecommons.org/publicdomain/zero/1.0/
5 // Tests that the cert download/import UI correctly identifies the cert being
6 // downloaded, and allows the trust of the cert to be specified.
8 const { MockRegistrar } = ChromeUtils.importESModule(
9 "resource://testing-common/MockRegistrar.sys.mjs"
15 * @property {string} certFilename
16 * Filename of the cert for this test case.
17 * @property {string} expectedDisplayString
18 * The string we expect the UI to display to represent the given cert.
19 * @property {nsIX509Cert} cert
20 * Handle to the cert once read in setup().
24 * A list of test cases representing certs that get "downloaded".
29 { certFilename: "has-cn.pem", expectedDisplayString: "Foo", cert: null },
31 certFilename: "has-empty-subject.pem",
32 expectedDisplayString: "Certificate Authority (unnamed)",
38 * Opens the cert download dialog.
40 * @param {nsIX509Cert} cert
41 * The cert to pass to the dialog for display.
43 * A promise that resolves when the dialog has finished loading, with
44 * an array consisting of:
45 * 1. The window of the opened dialog.
46 * 2. The return value nsIWritablePropertyBag2 passed to the dialog.
48 function openCertDownloadDialog(cert) {
49 let returnVals = Cc["@mozilla.org/hash-property-bag;1"].createInstance(
50 Ci.nsIWritablePropertyBag2
52 let win = window.openDialog(
53 "chrome://pippki/content/downloadcert.xhtml",
59 return new Promise(resolve => {
63 executeSoon(() => resolve([win, returnVals]));
70 add_setup(async function () {
71 for (let testCase of TEST_CASES) {
72 testCase.cert = await readCertificate(testCase.certFilename, ",,");
76 `'${testCase.certFilename}' should have been read`
81 // Test that the trust header message corresponds to the provided cert, and that
82 // the View Cert button launches the cert viewer for the provided cert.
83 add_task(async function testTrustHeaderAndViewCertButton() {
84 for (let testCase of TEST_CASES) {
85 let [win] = await openCertDownloadDialog(testCase.cert);
86 let expectedTrustHeaderString =
87 `Do you want to trust \u201C${testCase.expectedDisplayString}\u201D ` +
88 "for the following purposes?";
90 win.document.getElementById("trustHeader").textContent,
91 expectedTrustHeaderString,
92 "Actual and expected trust header text should match for " +
93 `${testCase.certFilename}`
96 await BrowserTestUtils.closeWindow(win);
100 // Test that the right values are returned when the dialog is accepted.
101 add_task(async function testAcceptDialogReturnValues() {
102 let [win, retVals] = await openCertDownloadDialog(TEST_CASES[0].cert);
103 win.document.getElementById("trustSSL").checked = true;
104 win.document.getElementById("trustEmail").checked = false;
105 info("Accepting dialog");
106 win.document.getElementById("download_cert").acceptDialog();
107 await BrowserTestUtils.windowClosed(win);
110 retVals.get("importConfirmed"),
111 "Return value should signal user chose to import the cert"
114 retVals.get("trustForSSL"),
115 "Return value should signal SSL trust checkbox was checked"
118 !retVals.get("trustForEmail"),
119 "Return value should signal E-mail trust checkbox was unchecked"
123 // Test that the right values are returned when the dialog is canceled.
124 add_task(async function testCancelDialogReturnValues() {
125 let [win, retVals] = await openCertDownloadDialog(TEST_CASES[0].cert);
126 info("Canceling dialog");
127 win.document.getElementById("download_cert").cancelDialog();
128 await BrowserTestUtils.windowClosed(win);
131 !retVals.get("importConfirmed"),
132 "Return value should signal user chose not to import the cert"