test_pkcs11_slot.js (4752B)
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/ 4 "use strict"; 5 6 // Tests the methods and attributes for interfacing with a PKCS #11 slot. 7 8 // Ensure that the appropriate initialization has happened. 9 do_get_profile(); 10 11 function find_slot_by_name(module, name) { 12 for (let slot of module.listSlots()) { 13 if (slot.name == name) { 14 return slot; 15 } 16 } 17 return null; 18 } 19 20 function find_module_by_name(moduleDB, name) { 21 for (let slot of moduleDB.listModules()) { 22 if (slot.name == name) { 23 return slot; 24 } 25 } 26 return null; 27 } 28 29 var gPrompt = { 30 QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]), 31 32 // This intentionally does not use arrow function syntax to avoid an issue 33 // where in the context of the arrow function, |this != gPrompt| due to 34 // how objects get wrapped when going across xpcom boundaries. 35 alert(title, text) { 36 equal( 37 text, 38 "Please authenticate to the token “Test PKCS11 Tokeñ 2 Label”. " + 39 "How to do so depends on the token (for example, using a fingerprint " + 40 "reader or entering a code with a keypad)." 41 ); 42 }, 43 }; 44 45 const gPromptFactory = { 46 QueryInterface: ChromeUtils.generateQI(["nsIPromptFactory"]), 47 getPrompt: () => gPrompt, 48 }; 49 50 function run_test() { 51 MockRegistrar.register("@mozilla.org/prompter;1", gPromptFactory); 52 53 let libraryFile = Services.dirsvc.get("CurWorkD", Ci.nsIFile); 54 libraryFile.append("pkcs11testmodule"); 55 libraryFile.append(ctypes.libraryName("pkcs11testmodule")); 56 loadPKCS11Module(libraryFile, "PKCS11 Test Module", false); 57 58 let moduleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService( 59 Ci.nsIPKCS11ModuleDB 60 ); 61 let testModule = find_module_by_name(moduleDB, "PKCS11 Test Module"); 62 notEqual(testModule, null, "should be able to find test module"); 63 let testSlot = find_slot_by_name(testModule, "Test PKCS11 Slot 二"); 64 notEqual(testSlot, null, "should be able to find 'Test PKCS11 Slot 二'"); 65 66 equal( 67 testSlot.name, 68 "Test PKCS11 Slot 二", 69 "Actual and expected name should match" 70 ); 71 equal( 72 testSlot.desc, 73 "Test PKCS11 Slot 二", 74 "Actual and expected description should match" 75 ); 76 equal( 77 testSlot.manID, 78 "Test PKCS11 Manufacturer ID", 79 "Actual and expected manufacturer ID should match" 80 ); 81 equal( 82 testSlot.HWVersion, 83 "0.0", 84 "Actual and expected hardware version should match" 85 ); 86 equal( 87 testSlot.FWVersion, 88 "0.0", 89 "Actual and expected firmware version should match" 90 ); 91 equal( 92 testSlot.status, 93 Ci.nsIPKCS11Slot.SLOT_NOT_LOGGED_IN, 94 "Actual and expected status should match" 95 ); 96 equal( 97 testSlot.tokenName, 98 "Test PKCS11 Tokeñ 2 Label", 99 "Actual and expected token name should match" 100 ); 101 102 let testToken = testSlot.getToken(); 103 notEqual(testToken, null, "getToken() should succeed"); 104 equal( 105 testToken.tokenName, 106 "Test PKCS11 Tokeñ 2 Label", 107 "Spot check: the actual and expected test token names should be equal" 108 ); 109 ok(!testToken.isInternalKeyToken, "This token is not the internal key token"); 110 111 testToken.login(true); 112 ok(testToken.isLoggedIn(), "Should have 'logged in' successfully"); 113 114 testSlot = find_slot_by_name(testModule, "Empty PKCS11 Slot"); 115 notEqual(testSlot, null, "should be able to find 'Empty PKCS11 Slot'"); 116 equal(testSlot.tokenName, null, "Empty slot is empty"); 117 equal( 118 testSlot.status, 119 Ci.nsIPKCS11Slot.SLOT_NOT_PRESENT, 120 "Actual and expected status should match" 121 ); 122 123 let bundle = Services.strings.createBundle( 124 "chrome://pipnss/locale/pipnss.properties" 125 ); 126 let internalModule = find_module_by_name( 127 moduleDB, 128 "NSS Internal PKCS #11 Module" 129 ); 130 notEqual(internalModule, null, "should be able to find internal module"); 131 let cryptoSlot = find_slot_by_name( 132 internalModule, 133 bundle.GetStringFromName("TokenDescription") 134 ); 135 notEqual(cryptoSlot, "should be able to find internal crypto slot"); 136 equal( 137 cryptoSlot.desc, 138 bundle.GetStringFromName("SlotDescription"), 139 "crypto slot should have expected 'desc'" 140 ); 141 equal( 142 cryptoSlot.manID, 143 bundle.GetStringFromName("ManufacturerID"), 144 "crypto slot should have expected 'manID'" 145 ); 146 let keySlot = find_slot_by_name( 147 internalModule, 148 bundle.GetStringFromName("PrivateTokenDescription") 149 ); 150 notEqual(keySlot, "should be able to find internal key slot"); 151 equal( 152 keySlot.desc, 153 bundle.GetStringFromName("PrivateSlotDescription"), 154 "key slot should have expected 'desc'" 155 ); 156 equal( 157 keySlot.manID, 158 bundle.GetStringFromName("ManufacturerID"), 159 "key slot should have expected 'manID'" 160 ); 161 }