browser_certificateManager.js (3206B)
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/. */ 4 "use strict"; 5 6 async function checkServerCertificates(win, expectedValues = []) { 7 await TestUtils.waitForCondition( 8 () => { 9 return ( 10 win.document.getElementById("serverList").itemChildren.length == 11 expectedValues.length 12 ); 13 }, 14 `Expected to have ${expectedValues.length} but got ${win.document.getElementById("serverList").itemChildren.length}` 15 ); 16 await new Promise(win.requestAnimationFrame); 17 18 let labels = win.document 19 .getElementById("serverList") 20 .querySelectorAll("label"); 21 22 // The strings we will get from the DOM are localized with Fluent. 23 // This will wait until the translation is applied. 24 if (expectedValues.length) { 25 await BrowserTestUtils.waitForCondition( 26 () => labels[1].value || !!labels[1].textContent.length, 27 "At least one label is populated" 28 ); 29 } 30 31 expectedValues.forEach((item, i) => { 32 let hostPort = labels[i * 3].value; 33 let fingerprint = labels[i * 3 + 1].value || labels[i * 3 + 1].textContent; 34 35 Assert.equal( 36 hostPort, 37 item.hostPort, 38 `Expected override to be ${item.hostPort} but got ${hostPort}` 39 ); 40 41 Assert.equal( 42 fingerprint, 43 item.fingerprint, 44 `Expected override to have field ${item.fingerprint}` 45 ); 46 }); 47 } 48 49 async function deleteOverride(win, expectedLength) { 50 win.document.getElementById("serverList").selectedIndex = 0; 51 await TestUtils.waitForCondition(() => { 52 return ( 53 win.document.getElementById("serverList").itemChildren.length == 54 expectedLength 55 ); 56 }); 57 let newWinPromise = BrowserTestUtils.domWindowOpenedAndLoaded(); 58 // Since the .click() blocks we need to dispatch it to the main thread avoid that. 59 Services.tm.dispatchToMainThread(() => 60 win.document.getElementById("websites_deleteButton").click() 61 ); 62 let newWin = await newWinPromise; 63 newWin.document.getElementById("deleteCertificate").acceptDialog(); 64 Assert.equal( 65 win.document.getElementById("serverList").selectedIndex, 66 0, 67 "After deletion we expect the selectedItem to be reset." 68 ); 69 } 70 71 add_task(async function test_cert_manager_server_tab() { 72 let win = await openCertManager(); 73 74 await checkServerCertificates(win); 75 76 win.document.getElementById("certmanager").acceptDialog(); 77 await BrowserTestUtils.windowClosed(win); 78 79 let cert = await readCertificate("md5-ee.pem", ",,"); 80 let certOverrideService = Cc[ 81 "@mozilla.org/security/certoverride;1" 82 ].getService(Ci.nsICertOverrideService); 83 certOverrideService.rememberValidityOverride( 84 "example.com", 85 443, 86 {}, 87 cert, 88 false 89 ); 90 91 win = await openCertManager(); 92 93 await checkServerCertificates(win, [ 94 { 95 hostPort: "example.com:443", 96 fingerprint: cert.sha256Fingerprint, 97 }, 98 ]); 99 100 await deleteOverride(win, 1); 101 102 await checkServerCertificates(win, []); 103 104 win.document.getElementById("certmanager").acceptDialog(); 105 await BrowserTestUtils.windowClosed(win); 106 107 certOverrideService.clearAllOverrides(); 108 });