browser_downloadCert_ui.js (4204B)
1 // Any copyright is dedicated to the Public Domain. 2 // http://creativecommons.org/publicdomain/zero/1.0/ 3 "use strict"; 4 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. 7 8 const { MockRegistrar } = ChromeUtils.importESModule( 9 "resource://testing-common/MockRegistrar.sys.mjs" 10 ); 11 12 /** 13 * @typedef TestCase 14 * @type {object} 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(). 21 */ 22 23 /** 24 * A list of test cases representing certs that get "downloaded". 25 * 26 * @type {TestCase[]} 27 */ 28 const TEST_CASES = [ 29 { certFilename: "has-cn.pem", expectedDisplayString: "Foo", cert: null }, 30 { 31 certFilename: "has-empty-subject.pem", 32 expectedDisplayString: "Certificate Authority (unnamed)", 33 cert: null, 34 }, 35 ]; 36 37 /** 38 * Opens the cert download dialog. 39 * 40 * @param {nsIX509Cert} cert 41 * The cert to pass to the dialog for display. 42 * @returns {Promise} 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. 47 */ 48 function openCertDownloadDialog(cert) { 49 let returnVals = Cc["@mozilla.org/hash-property-bag;1"].createInstance( 50 Ci.nsIWritablePropertyBag2 51 ); 52 let win = window.openDialog( 53 "chrome://pippki/content/downloadcert.xhtml", 54 "", 55 "", 56 cert, 57 returnVals 58 ); 59 return new Promise(resolve => { 60 win.addEventListener( 61 "load", 62 function () { 63 executeSoon(() => resolve([win, returnVals])); 64 }, 65 { once: true } 66 ); 67 }); 68 } 69 70 add_setup(async function () { 71 for (let testCase of TEST_CASES) { 72 testCase.cert = await readCertificate(testCase.certFilename, ",,"); 73 Assert.notEqual( 74 testCase.cert, 75 null, 76 `'${testCase.certFilename}' should have been read` 77 ); 78 } 79 }); 80 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?"; 89 Assert.equal( 90 win.document.getElementById("trustHeader").textContent, 91 expectedTrustHeaderString, 92 "Actual and expected trust header text should match for " + 93 `${testCase.certFilename}` 94 ); 95 96 await BrowserTestUtils.closeWindow(win); 97 } 98 }); 99 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); 108 109 Assert.ok( 110 retVals.get("importConfirmed"), 111 "Return value should signal user chose to import the cert" 112 ); 113 Assert.ok( 114 retVals.get("trustForSSL"), 115 "Return value should signal SSL trust checkbox was checked" 116 ); 117 Assert.ok( 118 !retVals.get("trustForEmail"), 119 "Return value should signal E-mail trust checkbox was unchecked" 120 ); 121 }); 122 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); 129 130 Assert.ok( 131 !retVals.get("importConfirmed"), 132 "Return value should signal user chose not to import the cert" 133 ); 134 });