Bug 1174555 - Clean up some SiteSecurityService state file related tests. r=keeler draft
authorCykesiopka <cykesiopka.bmo@gmail.com>
Sun, 21 May 2017 10:43:32 +0800
changeset 582089 ba47e0cfe9317703895df02277568e59cc56591c
parent 582088 e77e8b1ba70bef6f0ff794b7d066bbbdebe8f58e
child 582090 f02a317fd958909d42bad9cd206f5a74f36d8689
push id59968
push usercykesiopka.bmo@gmail.com
push dateSun, 21 May 2017 05:35:57 +0000
reviewerskeeler
bugs1174555
milestone55.0a1
Bug 1174555 - Clean up some SiteSecurityService state file related tests. r=keeler MozReview-Commit-ID: 6qXV04CUElu
security/manager/ssl/tests/unit/head_psm.js
security/manager/ssl/tests/unit/test_pinning_dynamic.js
security/manager/ssl/tests/unit/test_sss_readstate.js
security/manager/ssl/tests/unit/test_sss_readstate_child.js
security/manager/ssl/tests/unit/test_sss_readstate_garbage.js
security/manager/ssl/tests/unit/test_sss_readstate_huge.js
--- a/security/manager/ssl/tests/unit/head_psm.js
+++ b/security/manager/ssl/tests/unit/head_psm.js
@@ -862,8 +862,22 @@ function loadPKCS11TestModule(expectModu
  */
 function hexify(data) {
   // |slice(-2)| chomps off the last two characters of a string.
   // Therefore, if the Unicode value is < 0x10, we have a single-character hex
   // string when we want one that's two characters, and unconditionally
   // prepending a "0" solves the problem.
   return Array.from(data, (c, i) => ("0" + data.charCodeAt(i).toString(16)).slice(-2)).join("");
 }
+
+/**
+ * @param {String[]} lines
+ *        Lines to write. Each line automatically has "\n" appended to it when
+ *        being written.
+ * @param {nsIFileOutputStream} outputStream
+ */
+function writeLinesAndClose(lines, outputStream) {
+  for (let line of lines) {
+    line += "\n";
+    outputStream.write(line, line.length);
+  }
+  outputStream.close();
+}
--- a/security/manager/ssl/tests/unit/test_pinning_dynamic.js
+++ b/security/manager/ssl/tests/unit/test_pinning_dynamic.js
@@ -1,20 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // The purpose of this test is to create a site security service state file
 // and see that the site security service reads it properly.
 
-function writeLine(aLine, aOutputStream) {
-  aOutputStream.write(aLine, aLine.length);
-}
-
 var gSSService = null;
 var gSSSStateSeen = false;
 var gPreloadStateSeen = false;
 
 var profileDir = do_get_profile();
 var certdb;
 
 function certFromFile(cert_name) {
@@ -45,30 +41,33 @@ function run_test() {
 
   let stateFile = profileDir.clone();
   stateFile.append(SSS_STATE_FILE_NAME);
   // Assuming we're working with a clean slate, the SSS_STATE file shouldn't
   // exist until we create it.
   ok(!stateFile.exists(),
      "State file should not exist when working with a clean slate");
   let outputStream = FileUtils.openFileOutputStream(stateFile);
-  let now = (new Date()).getTime();
-  writeLine(`a.pinning2.example.com:HPKP\t0\t0\t${now + 100000},1,0,${PINNING_ROOT_KEY_HASH}\n`, outputStream);
-  writeLine(`b.pinning2.example.com:HPKP\t0\t0\t${now + 100000},1,1,${PINNING_ROOT_KEY_HASH}\n`, outputStream);
-
-  outputStream.close();
+  let expiryTime = Date.now() + 100000;
+  let lines = [
+    `a.pinning2.example.com:HPKP\t0\t0\t${expiryTime},1,0,${PINNING_ROOT_KEY_HASH}`,
+    `b.pinning2.example.com:HPKP\t0\t0\t${expiryTime},1,1,${PINNING_ROOT_KEY_HASH}`,
+  ];
+  writeLinesAndClose(lines, outputStream);
 
   let preloadFile = profileDir.clone();
   preloadFile.append(PRELOAD_STATE_FILE_NAME);
   ok(!preloadFile.exists(),
      "Preload file should not exist when working with a clean slate");
 
   outputStream = FileUtils.openFileOutputStream(preloadFile);
-  writeLine(`a.preload.example.com:HPKP\t0\t0\t${now + 100000},1,1,${PINNING_ROOT_KEY_HASH}\n`, outputStream);
-  outputStream.close();
+  lines = [
+    `a.preload.example.com:HPKP\t0\t0\t${expiryTime},1,1,${PINNING_ROOT_KEY_HASH}`,
+  ];
+  writeLinesAndClose(lines, outputStream);
 
   Services.obs.addObserver(checkStateRead, "data-storage-ready");
   do_test_pending();
   gSSService = Cc["@mozilla.org/ssservice;1"]
                  .getService(Ci.nsISiteSecurityService);
   notEqual(gSSService, null,
            "SiteSecurityService should have initialized successfully using" +
            " the generated state file");
--- a/security/manager/ssl/tests/unit/test_sss_readstate.js
+++ b/security/manager/ssl/tests/unit/test_sss_readstate.js
@@ -1,20 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // The purpose of this test is to create a site security service state file
 // and see that the site security service reads it properly.
 
-function writeLine(aLine, aOutputStream) {
-  aOutputStream.write(aLine, aLine.length);
-}
-
 var gSSService = null;
 
 function checkStateRead(aSubject, aTopic, aData) {
   if (aData == PRELOAD_STATE_FILE_NAME) {
     return;
   }
 
   equal(aData, SSS_STATE_FILE_NAME);
@@ -74,23 +70,25 @@ function checkStateRead(aSubject, aTopic
 function run_test() {
   let profileDir = do_get_profile();
   let stateFile = profileDir.clone();
   stateFile.append(SSS_STATE_FILE_NAME);
   // Assuming we're working with a clean slate, the file shouldn't exist
   // until we create it.
   ok(!stateFile.exists());
   let outputStream = FileUtils.openFileOutputStream(stateFile);
-  let now = (new Date()).getTime();
-  writeLine("expired.example.com:HSTS\t0\t0\t" + (now - 100000) + ",1,0\n", outputStream);
-  writeLine("notexpired.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  // This overrides an entry on the preload list.
-  writeLine("includesubdomains.preloaded.test:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  writeLine("incsubdomain.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,1\n", outputStream);
-  // This overrides an entry on the preload list.
-  writeLine("includesubdomains2.preloaded.test:HSTS\t0\t0\t0,2,0\n", outputStream);
-  outputStream.close();
+  let now = Date.now();
+  let lines = [
+    `expired.example.com:HSTS\t0\t0\t${now - 100000},1,0`,
+    `notexpired.example.com:HSTS\t0\t0\t${now + 100000},1,0`,
+    // This overrides an entry on the preload list.
+    `includesubdomains.preloaded.test:HSTS\t0\t0\t${now + 100000},1,0`,
+    `incsubdomain.example.com:HSTS\t0\t0\t${now + 100000},1,1`,
+    // This overrides an entry on the preload list.
+    "includesubdomains2.preloaded.test:HSTS\t0\t0\t0,2,0",
+  ];
+  writeLinesAndClose(lines, outputStream);
   Services.obs.addObserver(checkStateRead, "data-storage-ready");
   do_test_pending();
   gSSService = Cc["@mozilla.org/ssservice;1"]
                  .getService(Ci.nsISiteSecurityService);
   notEqual(gSSService, null);
 }
--- a/security/manager/ssl/tests/unit/test_sss_readstate_child.js
+++ b/security/manager/ssl/tests/unit/test_sss_readstate_child.js
@@ -2,40 +2,38 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // The purpose of this test is to create a site security service state file
 // and see that the site security service reads it properly. We also verify
 // that state changes are reflected in the child process.
 
-function writeLine(aLine, aOutputStream) {
-  aOutputStream.write(aLine, aLine.length);
-}
-
 function start_test_in_child() {
   run_test_in_child("sss_readstate_child_worker.js");
   do_test_finished();
 }
 
 function run_test() {
   let profileDir = do_get_profile();
   let stateFile = profileDir.clone();
   stateFile.append(SSS_STATE_FILE_NAME);
   // Assuming we're working with a clean slate, the file shouldn't exist
   // until we create it.
   ok(!stateFile.exists());
   let outputStream = FileUtils.openFileOutputStream(stateFile);
-  let now = (new Date()).getTime();
-  writeLine("expired.example.com:HSTS\t0\t0\t" + (now - 100000) + ",1,0\n", outputStream);
-  writeLine("notexpired.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  // This overrides an entry on the preload list.
-  writeLine("includesubdomains.preloaded.test:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  writeLine("incsubdomain.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,1\n", outputStream);
-  // This overrides an entry on the preload list.
-  writeLine("includesubdomains2.preloaded.test:HSTS\t0\t0\t0,2,0\n", outputStream);
-  outputStream.close();
+  let now = Date.now();
+  let lines = [
+    `expired.example.com:HSTS\t0\t0\t${now - 100000},1,0`,
+    `notexpired.example.com:HSTS\t0\t0\t${now + 100000},1,0`,
+    // This overrides an entry on the preload list.
+    `includesubdomains.preloaded.test:HSTS\t0\t0\t${now + 100000},1,0`,
+    `incsubdomain.example.com:HSTS\t0\t0\t${now + 100000},1,1`,
+    // This overrides an entry on the preload list.
+    "includesubdomains2.preloaded.test:HSTS\t0\t0\t0,2,0",
+  ];
+  writeLinesAndClose(lines, outputStream);
   Services.obs.addObserver(start_test_in_child, "data-storage-ready");
   do_test_pending();
   let SSService = Cc["@mozilla.org/ssservice;1"]
                     .getService(Ci.nsISiteSecurityService);
   notEqual(SSService, null);
 }
--- a/security/manager/ssl/tests/unit/test_sss_readstate_garbage.js
+++ b/security/manager/ssl/tests/unit/test_sss_readstate_garbage.js
@@ -1,20 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // The purpose of this test is to create a mostly bogus site security service
 // state file and see that the site security service handles it properly.
 
-function writeLine(aLine, aOutputStream) {
-  aOutputStream.write(aLine, aLine.length);
-}
-
 var gSSService = null;
 
 function checkStateRead(aSubject, aTopic, aData) {
   if (aData == PRELOAD_STATE_FILE_NAME) {
     return;
   }
 
   equal(aData, SSS_STATE_FILE_NAME);
@@ -36,24 +32,26 @@ function checkStateRead(aSubject, aTopic
 function run_test() {
   let profileDir = do_get_profile();
   let stateFile = profileDir.clone();
   stateFile.append(SSS_STATE_FILE_NAME);
   // Assuming we're working with a clean slate, the file shouldn't exist
   // until we create it.
   ok(!stateFile.exists());
   let outputStream = FileUtils.openFileOutputStream(stateFile);
-  let now = (new Date()).getTime();
-  writeLine("example1.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  writeLine("I'm a lumberjack and I'm okay; I work all night and I sleep all day!\n", outputStream);
-  writeLine("This is a totally bogus entry\t\n", outputStream);
-  writeLine("0\t0\t0\t0\t\n", outputStream);
-  writeLine("\t\t\t\t\t\t\t\n", outputStream);
-  writeLine("example.com:HSTS\t\t\t\t\t\t\t\n", outputStream);
-  writeLine("example3.example.com:HSTS\t0\t\t\t\t\t\t\n", outputStream);
-  writeLine("example2.example.com:HSTS\t0\t0\t" + (now + 100000) + ",1,0\n", outputStream);
-  outputStream.close();
+  let expiryTime = Date.now() + 100000;
+  let lines = [
+    `example1.example.com:HSTS\t0\t0\t${expiryTime},1,0`,
+    "I'm a lumberjack and I'm okay; I work all night and I sleep all day!",
+    "This is a totally bogus entry\t",
+    "0\t0\t0\t0\t",
+    "\t\t\t\t\t\t\t",
+    "example.com:HSTS\t\t\t\t\t\t\t",
+    "example3.example.com:HSTS\t0\t\t\t\t\t\t",
+    `example2.example.com:HSTS\t0\t0\t${expiryTime},1,0`,
+  ];
+  writeLinesAndClose(lines, outputStream);
   Services.obs.addObserver(checkStateRead, "data-storage-ready");
   do_test_pending();
   gSSService = Cc["@mozilla.org/ssservice;1"]
                  .getService(Ci.nsISiteSecurityService);
   notEqual(gSSService, null);
 }
--- a/security/manager/ssl/tests/unit/test_sss_readstate_huge.js
+++ b/security/manager/ssl/tests/unit/test_sss_readstate_huge.js
@@ -2,20 +2,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // The purpose of this test is to create a site security service state file
 // that is too large and see that the site security service reads it properly
 // (this means discarding all entries after the 1024th).
 
-function writeLine(aLine, aOutputStream) {
-  aOutputStream.write(aLine, aLine.length);
-}
-
 var gSSService = null;
 
 function checkStateRead(aSubject, aTopic, aData) {
   if (aData == PRELOAD_STATE_FILE_NAME) {
     return;
   }
 
   equal(aData, SSS_STATE_FILE_NAME);
@@ -47,21 +43,25 @@ function checkStateRead(aSubject, aTopic
 function run_test() {
   let profileDir = do_get_profile();
   let stateFile = profileDir.clone();
   stateFile.append(SSS_STATE_FILE_NAME);
   // Assuming we're working with a clean slate, the file shouldn't exist
   // until we create it.
   ok(!stateFile.exists());
   let outputStream = FileUtils.openFileOutputStream(stateFile);
-  let now = (new Date()).getTime();
+  let expiryTime = Date.now() + 100000;
+  let lines = [];
   for (let i = 0; i < 10000; i++) {
     // The 0s will all get squashed down into one 0 when they are read.
     // This is just to make the file size large (>2MB).
-    writeLine("example" + i + ".example.com:HSTS\t0000000000000000000000000000000000000000000000000\t00000000000000000000000000000000000000\t" + (now + 100000) + ",1,0000000000000000000000000000000000000000000000000000000000000000000000000\n", outputStream);
+    lines.push(`example${i}.example.com:HSTS\t` +
+               "0000000000000000000000000000000000000000000000000\t" +
+               "00000000000000000000000000000000000000\t" +
+               `${expiryTime},1,0000000000000000000000000000000000000000000000000000000000000000000000000`);
   }
-  outputStream.close();
+  writeLinesAndClose(lines, outputStream);
   Services.obs.addObserver(checkStateRead, "data-storage-ready");
   do_test_pending();
   gSSService = Cc["@mozilla.org/ssservice;1"]
                  .getService(Ci.nsISiteSecurityService);
   notEqual(gSSService, null);
 }