Bug 1260626 - Take advantage of the always present test token. r=keeler
authorCykesiopka <cykesiopka.bmo@gmail.com>
Wed, 02 Nov 2016 00:08:34 +0800
changeset 347162 e3ced3657d49b80da022d24cc7a605d2fdb57bf7
parent 347161 34bca938935a9d36820e6703bc5dbcac46483c71
child 347163 6f52a805df8339d0e068bf85667251a5c3e71132
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler
bugs1260626
milestone52.0a1
Bug 1260626 - Take advantage of the always present test token. r=keeler Now that we have an always present test token, we can add more tests, and make other tests not intermittently fail. MozReview-Commit-ID: LRLmOGGjshb
security/manager/ssl/tests/unit/test_pkcs11_insert_remove.js
security/manager/ssl/tests/unit/test_pkcs11_slot.js
security/manager/ssl/tests/unit/test_pkcs11_token.js
security/manager/ssl/tests/unit/test_pkcs11_tokenDB.js
security/manager/ssl/tests/unit/xpcshell-smartcards.ini
--- a/security/manager/ssl/tests/unit/test_pkcs11_insert_remove.js
+++ b/security/manager/ssl/tests/unit/test_pkcs11_insert_remove.js
@@ -11,61 +11,26 @@
 // has been succssfully observed, and then it unloads the test module.
 
 // Ensure that the appropriate initialization has happened.
 do_get_profile();
 Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
 
 const gExpectedTokenLabel = "Test PKCS11 Tokeñ Label";
 
-const gTokenDB = Cc["@mozilla.org/security/pk11tokendb;1"]
-                   .getService(Ci.nsIPK11TokenDB);
-
 function SmartcardObserver(type) {
   this.type = type;
   do_test_pending();
 }
 
 SmartcardObserver.prototype = {
   observe: function(subject, topic, data) {
     equal(topic, this.type, "Observed and expected types should match");
     equal(gExpectedTokenLabel, data,
           "Expected and observed token labels should match");
-
-    // Test that the token list contains the test token only when the test
-    // module is loaded.
-    // Note: This test is located here out of convenience. In particular,
-    //       observing the "smartcard-insert" event is the only time where it
-    //       is reasonably certain for the test token to be present (see the top
-    //       level comment for this file for why).
-    let tokenList = gTokenDB.listTokens();
-    let testTokenLabelFound = false;
-    while (tokenList.hasMoreElements()) {
-      let token = tokenList.getNext().QueryInterface(Ci.nsIPK11Token);
-      if (token.tokenLabel == gExpectedTokenLabel) {
-        testTokenLabelFound = true;
-        break;
-      }
-    }
-    let testTokenShouldBePresent = this.type == "smartcard-insert";
-    equal(testTokenLabelFound, testTokenShouldBePresent,
-          "Should find test token only when the test module is loaded");
-
-    // Test that the token is findable by name only when the test module is
-    // loaded.
-    // Note: Again, this test is located here out of convenience.
-    if (testTokenShouldBePresent) {
-      notEqual(gTokenDB.findTokenByName(gExpectedTokenLabel), null,
-               "Test token should be findable by name");
-    } else {
-      throws(() => gTokenDB.findTokenByName(gExpectedTokenLabel),
-             /NS_ERROR_FAILURE/,
-             "Non-present test token should not be findable by name");
-    }
-
     Services.obs.removeObserver(this, this.type);
     do_test_finished();
   }
 };
 
 function run_test() {
   Services.obs.addObserver(new SmartcardObserver("smartcard-insert"),
                            "smartcard-insert", false);
--- a/security/manager/ssl/tests/unit/test_pkcs11_slot.js
+++ b/security/manager/ssl/tests/unit/test_pkcs11_slot.js
@@ -9,35 +9,30 @@
 do_get_profile();
 
 function run_test() {
   loadPKCS11TestModule(false);
 
   let moduleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"]
                    .getService(Ci.nsIPKCS11ModuleDB);
   let testModule = moduleDB.findModuleByName("PKCS11 Test Module");
-  // TODO(Bug 1306632): Add a slot to the PSM PKCS 11 test module with a name
-  //                    with high codepoints, then add a test to ensure
-  //                    findSlotByName() is able to find the slot.
-  let testSlot = testModule.findSlotByName("Test PKCS11 Slot");
+  let testSlot = testModule.findSlotByName("Test PKCS11 Slot 二");
 
-  equal(testSlot.name, "Test PKCS11 Slot",
+  equal(testSlot.name, "Test PKCS11 Slot 二",
         "Actual and expected name should match");
-  equal(testSlot.desc, "Test PKCS11 Slot",
+  equal(testSlot.desc, "Test PKCS11 Slot 二",
         "Actual and expected description should match");
   equal(testSlot.manID, "Test PKCS11 Manufacturer ID",
         "Actual and expected manufacturer ID should match");
   equal(testSlot.HWVersion, "0.0",
         "Actual and expected hardware version should match");
   equal(testSlot.FWVersion, "0.0",
         "Actual and expected firmware version should match");
-  // Note: testSlot.status is not tested because the implementation calls
-  //       PK11_IsPresent(), which checks whether the test token is present.
-  //       The test module inserts and removes the test token in a tight loop,
-  //       so the result might not be deterministic.
+  equal(testSlot.status, Ci.nsIPKCS11Slot.SLOT_READY,
+        "Actual and expected status should match");
+  equal(testSlot.tokenName, "Test PKCS11 Tokeñ 2 Label",
+        "Actual and expected token name should match");
 
-  // Note: testSlot.tokenName isn't tested for the same reason testSlot.status
-  //       isn't.
   let testToken = testSlot.getToken();
   notEqual(testToken, null, "getToken() should succeed");
-  equal(testToken.tokenLabel, "Test PKCS11 Tokeñ Label",
+  equal(testToken.tokenLabel, "Test PKCS11 Tokeñ 2 Label",
         "Spot check: the actual and expected test token labels should be equal");
 }
--- a/security/manager/ssl/tests/unit/test_pkcs11_token.js
+++ b/security/manager/ssl/tests/unit/test_pkcs11_token.js
@@ -1,19 +1,23 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 // Any copyright is dedicated to the Public Domain.
 // http://creativecommons.org/publicdomain/zero/1.0/
 "use strict";
 
 // Tests the methods and attributes for interfacing with a PKCS #11 token, using
 // the internal key token.
-// Note: We don't use the test token in the test PKCS #11 module because it is
-//       inconvenient to test. See the top level comment in
-//       test_pkcs11_insert_remove.js for why. However, some token DB tests are
-//       located in that file out of convenience.
+// We don't use either of the test tokens in the test PKCS #11 module because:
+//   1. Test token 1 cyclically inserts and removes itself in a tight loop.
+//      Using token 1 would complicate the test and introduce intermittent
+//      failures.
+//   2. Neither test token implements login or password related functionality.
+//      We want to test such functionality.
+//   3. Using the internal token lets us actually test the internal token works
+//      as expected.
 
 // Ensure that the appropriate initialization has happened.
 do_get_profile();
 
 function checkBasicAttributes(token) {
   let strBundleSvc = Cc["@mozilla.org/intl/stringbundle;1"]
                        .getService(Ci.nsIStringBundleService);
   let bundle =
new file mode 100644
--- /dev/null
+++ b/security/manager/ssl/tests/unit/test_pkcs11_tokenDB.js
@@ -0,0 +1,45 @@
+// Any copyright is dedicated to the Public Domain.
+// http://creativecommons.org/publicdomain/zero/1.0/
+"use strict";
+
+// Tests the methods for interfacing with the PKCS #11 token database.
+
+// Ensure that the appropriate initialization has happened.
+do_get_profile();
+
+function run_test() {
+  let tokenDB = Cc["@mozilla.org/security/pk11tokendb;1"]
+                  .getService(Ci.nsIPK11TokenDB);
+
+  let tokenListPreLoad = tokenDB.listTokens();
+  while (tokenListPreLoad.hasMoreElements()) {
+    let token = tokenListPreLoad.getNext().QueryInterface(Ci.nsIPK11Token);
+    notEqual(token.tokenLabel, "Test PKCS11 Tokeñ Label",
+             "Test PKCS11 Token 1 should not be listed prior to module load");
+    notEqual(token.tokenLabel, "Test PKCS11 Tokeñ 2 Label",
+             "Test PKCS11 Token 2 should not be listed prior to module load");
+  }
+
+  throws(() => tokenDB.findTokenByName("Test PKCS11 Tokeñ Label"),
+         /NS_ERROR_FAILURE/,
+         "Non-present test token 1 should not be findable by name");
+  throws(() => tokenDB.findTokenByName("Test PKCS11 Tokeñ 2 Label"),
+         /NS_ERROR_FAILURE/,
+         "Non-present test token 2 should not be findable by name");
+
+  loadPKCS11TestModule(false);
+
+  // Test Token 1 is simulated to insert and remove itself in a tight loop, so
+  // we don't bother testing that it's present.
+  let tokenListPostLoad = tokenDB.listTokens();
+  let foundTokenNames = [];
+  while (tokenListPostLoad.hasMoreElements()) {
+    let token = tokenListPostLoad.getNext().QueryInterface(Ci.nsIPK11Token);
+    foundTokenNames.push(token.tokenName);
+  }
+  ok(foundTokenNames.includes("Test PKCS11 Tokeñ 2 Label"),
+     "Test PKCS11 Token 2 should be listed after module load");
+
+  notEqual(tokenDB.findTokenByName("Test PKCS11 Tokeñ 2 Label"), null,
+           "Test token 2 should be findable by name after loading test module");
+}
--- a/security/manager/ssl/tests/unit/xpcshell-smartcards.ini
+++ b/security/manager/ssl/tests/unit/xpcshell-smartcards.ini
@@ -6,8 +6,9 @@ skip-if = toolkit == 'android' || toolki
 support-files =
 
 [test_pkcs11_insert_remove.js]
 [test_pkcs11_module.js]
 [test_pkcs11_no_events_after_removal.js]
 [test_pkcs11_safe_mode.js]
 [test_pkcs11_slot.js]
 [test_pkcs11_token.js]
+[test_pkcs11_tokenDB.js]