Bug 1186286 - Move some code around to facilitate code reuse for the subsequent patch. r=mgoodwin draft
authorCykesiopka <cykesiopka.bmo@gmail.com>
Fri, 07 Oct 2016 20:11:35 +0800
changeset 422090 ac1f8bddeb3f2f6bf3457909b637d07c7e93fa00
parent 422089 e907031a16123846a0817935a3f64b67286a5d73
child 422091 50ce122972802693fcfa3a16f3166a377c056eb5
push id31687
push usercykesiopka.bmo@gmail.com
push dateFri, 07 Oct 2016 12:33:49 +0000
reviewersmgoodwin
bugs1186286
milestone52.0a1
Bug 1186286 - Move some code around to facilitate code reuse for the subsequent patch. r=mgoodwin The existing code in head.js is moved out because it doesn't need to be there now, and probably not in the near future. MozReview-Commit-ID: GWUW9wT3fzF
security/manager/ssl/tests/mochitest/browser/browser.ini
security/manager/ssl/tests/mochitest/browser/browser_bug627234_perwindowpb.js
security/manager/ssl/tests/mochitest/browser/browser_certViewer.js
security/manager/ssl/tests/mochitest/browser/head.js
--- a/security/manager/ssl/tests/mochitest/browser/browser.ini
+++ b/security/manager/ssl/tests/mochitest/browser/browser.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 tags = psm
-support-files = head.js
+support-files =
+  head.js
+  *.pem
 
 [browser_bug627234_perwindowpb.js]
 [browser_certificateManagerLeak.js]
 [browser_certViewer.js]
-support-files = *.pem
 [browser_clientAuth_connection.js]
 [browser_clientAuth_ui.js]
--- a/security/manager/ssl/tests/mochitest/browser/browser_bug627234_perwindowpb.js
+++ b/security/manager/ssl/tests/mochitest/browser/browser_bug627234_perwindowpb.js
@@ -21,16 +21,24 @@ FakeSSLStatus.prototype = {
     if (aIID.equals(Ci.nsISSLStatus) ||
         aIID.equals(Ci.nsISupports)) {
       return this;
     }
     throw new Error(Cr.NS_ERROR_NO_INTERFACE);
   },
 };
 
+function whenNewWindowLoaded(aOptions, aCallback) {
+  let win = OpenBrowserWindow(aOptions);
+  win.addEventListener("load", function onLoad() {
+    win.removeEventListener("load", onLoad, false);
+    aCallback(win);
+  }, false);
+}
+
 // This is a template to help porting global private browsing tests
 // to per-window private browsing tests
 function test() {
   // initialization
   waitForExplicitFinish();
   let windowsToClose = [];
   let testURI = "about:blank";
   let uri;
--- a/security/manager/ssl/tests/mochitest/browser/browser_certViewer.js
+++ b/security/manager/ssl/tests/mochitest/browser/browser_certViewer.js
@@ -16,159 +16,124 @@ registerCleanupFunction(function() {
   let certdb = Cc["@mozilla.org/security/x509certdb;1"]
                  .getService(Ci.nsIX509CertDB);
   certificates.forEach(cert => {
     certdb.deleteCertificate(cert);
   });
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("ca.pem", "CTu,CTu,CTu");
+  let cert = yield readCertificate("ca.pem", "CTu,CTu,CTu", certificates);
   let win = yield displayCertificate(cert);
   checkUsages(win, ["SSL Certificate Authority"]);
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("ssl-ee.pem", ",,");
+  let cert = yield readCertificate("ssl-ee.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkUsages(win, ["SSL Server Certificate", "SSL Client Certificate"]);
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("email-ee.pem", ",,");
+  let cert = yield readCertificate("email-ee.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkUsages(win, ["Email Recipient Certificate", "Email Signer Certificate"]);
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("code-ee.pem", ",,");
+  let cert = yield readCertificate("code-ee.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkUsages(win, ["Object Signer"]);
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("expired-ca.pem", ",,");
+  let cert = yield readCertificate("expired-ca.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win, "Could not verify this certificate because it has expired.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("ee-from-expired-ca.pem", ",,");
+  let cert = yield readCertificate("ee-from-expired-ca.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because the CA certificate " +
              "is invalid.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("unknown-issuer.pem", ",,");
+  let cert = yield readCertificate("unknown-issuer.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because the issuer is " +
              "unknown.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("md5-ee.pem", ",,");
+  let cert = yield readCertificate("md5-ee.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because it was signed using " +
              "a signature algorithm that was disabled because that algorithm " +
              "is not secure.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("untrusted-ca.pem", "p,p,p");
+  let cert = yield readCertificate("untrusted-ca.pem", "p,p,p", certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because it is not trusted.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
-  let cert = yield readCertificate("ee-from-untrusted-ca.pem", ",,");
+  let cert = yield readCertificate("ee-from-untrusted-ca.pem", ",,",
+                                   certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because the issuer is not " +
              "trusted.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
   // Note that there's currently no way to un-do this. This should only be a
   // problem if another test re-uses a certificate with this same key (perhaps
   // likely) and subject (less likely).
   let certBlocklist = Cc["@mozilla.org/security/certblocklist;1"]
                         .getService(Ci.nsICertBlocklist);
   certBlocklist.revokeCertBySubjectAndPubKey(
     "MBIxEDAOBgNVBAMMB3Jldm9rZWQ=", // CN=revoked
     "VCIlmPM9NkgFQtrs4Oa5TeFcDu6MWRTKSNdePEhOgD8="); // hash of the shared key
-  let cert = yield readCertificate("revoked.pem", ",,");
+  let cert = yield readCertificate("revoked.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win,
              "Could not verify this certificate because it has been revoked.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 add_task(function* () {
   // This certificate has a keyUsage extension asserting cRLSign and
   // keyCertSign, but it doesn't have a basicConstraints extension. This
   // shouldn't be valid for any usage. Sadly, we give a pretty lame error
   // message in this case.
-  let cert = yield readCertificate("invalid.pem", ",,");
+  let cert = yield readCertificate("invalid.pem", ",,", certificates);
   let win = yield displayCertificate(cert);
   checkError(win, "Could not verify this certificate for unknown reasons.");
   yield BrowserTestUtils.closeWindow(win);
 });
 
 /**
- * Helper for readCertificate.
- */
-function pemToBase64(pem) {
-  return pem.replace(/-----BEGIN CERTIFICATE-----/, "")
-            .replace(/-----END CERTIFICATE-----/, "")
-            .replace(/[\r\n]/g, "");
-}
-
-/**
- * Given the filename of a certificate, returns a promise that will resolve with
- * a handle to the certificate when that certificate has been read and imported
- * with the given trust settings.
- *
- * @param {String} filename
- *        The filename of the certificate (assumed to be in the same directory).
- * @param {String} trustString
- *        A string describing how the certificate should be trusted (see
- *        `certutil -A --help`).
- * @return {Promise}
- *         A promise that will resolve with a handle to the certificate.
- */
-function readCertificate(filename, trustString) {
-  return OS.File.read(getTestFilePath(filename)).then(data => {
-    let decoder = new TextDecoder();
-    let pem = decoder.decode(data);
-    let certdb = Cc["@mozilla.org/security/x509certdb;1"]
-                   .getService(Ci.nsIX509CertDB);
-    let base64 = pemToBase64(pem);
-    certdb.addCertFromBase64(base64, trustString, "unused");
-    let cert = certdb.constructX509FromBase64(base64);
-    certificates.push(cert); // so we remember to delete this at the end
-    return cert;
-  }, error => { throw error; });
-}
-
-/**
  * Given a certificate, returns a promise that will resolve when the certificate
  * viewer has opened is displaying that certificate, and has finished
  * determining its valid usages.
  *
  * @param {nsIX509Cert} certificate
  *        The certificate to view and determine usages for.
  * @return {Promise}
  *         A promise that will resolve with a handle on the opened certificate
--- a/security/manager/ssl/tests/mochitest/browser/head.js
+++ b/security/manager/ssl/tests/mochitest/browser/head.js
@@ -1,11 +1,42 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-function whenNewWindowLoaded(aOptions, aCallback) {
-  let win = OpenBrowserWindow(aOptions);
-  win.addEventListener("load", function onLoad() {
-    win.removeEventListener("load", onLoad, false);
-    aCallback(win);
-  }, false);
+/**
+ * This function serves the same purpose as the one defined in head_psm.js.
+ */
+function pemToBase64(pem) {
+  return pem.replace(/-----BEGIN CERTIFICATE-----/, "")
+            .replace(/-----END CERTIFICATE-----/, "")
+            .replace(/[\r\n]/g, "");
 }
+
+/**
+ * Given the filename of a certificate, returns a promise that will resolve with
+ * a handle to the certificate when that certificate has been read and imported
+ * with the given trust settings.
+ *
+ * @param {String} filename
+ *        The filename of the certificate (assumed to be in the same directory).
+ * @param {String} trustString
+ *        A string describing how the certificate should be trusted (see
+ *        `certutil -A --help`).
+ * @param {nsIX509Cert[]} certificates
+ *        An array to append the imported cert to. Useful for making sure
+ *        imported certs are cleaned up.
+ * @return {Promise}
+ *         A promise that will resolve with a handle to the certificate.
+ */
+function readCertificate(filename, trustString, certificates) {
+  return OS.File.read(getTestFilePath(filename)).then(data => {
+    let decoder = new TextDecoder();
+    let pem = decoder.decode(data);
+    let certdb = Cc["@mozilla.org/security/x509certdb;1"]
+                   .getService(Ci.nsIX509CertDB);
+    let base64 = pemToBase64(pem);
+    certdb.addCertFromBase64(base64, trustString, "unused");
+    let cert = certdb.constructX509FromBase64(base64);
+    certificates.push(cert);
+    return cert;
+  }, error => { throw error; });
+}