1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/publicdomain/zero/1.0/
6 // Tests that the client authentication certificate chooser correctly displays
7 // provided information and correctly returns user input.
9 const TEST_HOSTNAME = "Test Hostname";
10 const TEST_ORG = "Test Org";
11 const TEST_ISSUER_ORG = "Test Issuer Org";
12 const TEST_PORT = 123;
14 var certDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
18 * Test certificate (i.e. build/pgo/certs/mochitest.client).
25 * Opens the client auth cert chooser dialog.
27 * @param {nsIX509Cert} cert The cert to pass to the dialog for display.
29 * A promise that resolves when the dialog has finished loading, with
30 * an array consisting of:
31 * 1. The window of the opened dialog.
32 * 2. The return value nsIWritablePropertyBag2 passed to the dialog.
34 function openClientAuthDialog(cert) {
35 let certList = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
36 certList.appendElement(cert);
38 let returnVals = Cc["@mozilla.org/hash-property-bag;1"].createInstance(
39 Ci.nsIWritablePropertyBag2
41 let win = window.openDialog(
42 "chrome://pippki/content/clientauthask.xhtml",
52 return TestUtils.topicObserved("cert-dialog-loaded").then(() => [
59 * Checks that the contents of the given cert chooser dialog match the details
60 * of build/pgo/certs/mochitest.client.
62 * @param {window} win The cert chooser window.
63 * @param {string} notBefore
64 * The formatted notBefore date of mochitest.client.
65 * @param {string} notAfter
66 * The formatted notAfter date of mochitest.client.
68 function checkDialogContents(win, notBefore, notAfter) {
70 win.document.getElementById("hostname").textContent,
71 `${TEST_HOSTNAME}:${TEST_PORT}`,
72 "Actual and expected hostname and port should be equal"
75 win.document.getElementById("organization").textContent,
76 `Organization: “${TEST_ORG}”`,
77 "Actual and expected organization should be equal"
80 win.document.getElementById("issuer").textContent,
81 `Issued Under: “${TEST_ISSUER_ORG}”`,
82 "Actual and expected issuer organization should be equal"
86 win.document.getElementById("nicknames").label,
87 "Mochitest client [03]",
88 "Actual and expected selected cert nickname and serial should be equal"
91 win.document.getElementById("nicknames").itemCount,
93 "correct number of items"
96 let [subject, serialNum, validity, issuer, tokenName] = win.document
97 .getElementById("details")
101 "Issued to: CN=Mochitest client",
102 "Actual and expected subject should be equal"
107 "Actual and expected serial number should be equal"
111 `Valid from ${notBefore} to ${notAfter}`,
112 "Actual and expected validity should be equal"
116 "Issued by: OU=Profile Guided Optimization,O=Mozilla Testing,CN=Temporary Certificate Authority",
117 "Actual and expected issuer should be equal"
121 "Stored on: Software Security Device",
122 "Actual and expected token name should be equal"
126 function findCertByCommonName(commonName) {
127 for (let cert of certDB.getCerts()) {
128 if (cert.commonName == commonName) {
135 add_setup(async function () {
136 cert = findCertByCommonName("Mochitest client");
137 isnot(cert, null, "Should be able to find the test client cert");
140 // Test that the contents of the dialog correspond to the details of the
142 add_task(async function testContents() {
143 const formatter = new Intl.DateTimeFormat(undefined, {
147 let [win] = await openClientAuthDialog(cert);
150 formatter.format(new Date(cert.validity.notBefore / 1000)),
151 formatter.format(new Date(cert.validity.notAfter / 1000))
153 await BrowserTestUtils.closeWindow(win);
156 // Test that the right values are returned when the dialog is accepted.
157 add_task(async function testAcceptDialogReturnValues() {
158 let [win, retVals] = await openClientAuthDialog(cert);
159 win.document.getElementById("rememberBox").checked = true;
160 info("Accepting dialog");
161 win.document.getElementById("certAuthAsk").acceptDialog();
162 await BrowserTestUtils.windowClosed(win);
165 retVals.get("certChosen"),
166 "Return value should signal user chose a certificate"
169 retVals.get("selectedIndex"),
171 "0 should be returned as the selected index"
174 retVals.get("rememberSelection"),
175 "Return value should signal 'Remember this decision' checkbox was checked"
179 // Test that the right values are returned when the dialog is canceled.
180 add_task(async function testCancelDialogReturnValues() {
181 let [win, retVals] = await openClientAuthDialog(cert);
182 win.document.getElementById("rememberBox").checked = false;
183 info("Canceling dialog");
184 win.document.getElementById("certAuthAsk").cancelDialog();
185 await BrowserTestUtils.windowClosed(win);
188 !retVals.get("certChosen"),
189 "Return value should signal user did not choose a certificate"
192 !retVals.get("rememberSelection"),
193 "Return value should signal 'Remember this decision' checkbox was unchecked"