test_pkcs11_safe_mode.js (1897B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 "use strict"; 6 7 // In safe mode, PKCS#11 modules should not be loaded. This test tests this by 8 // simulating starting in safe mode and then attempting to load a module. 9 10 function run_test() { 11 do_get_profile(); 12 13 // Simulate starting in safe mode. 14 let xulRuntime = { 15 inSafeMode: true, 16 logConsoleErrors: true, 17 OS: "XPCShell", 18 XPCOMABI: "noarch-spidermonkey", 19 invalidateCachesOnRestart: function invalidateCachesOnRestart() { 20 // Do nothing 21 }, 22 QueryInterface: ChromeUtils.generateQI(["nsIXULRuntime"]), 23 }; 24 25 let xulRuntimeFactory = { 26 createInstance(iid) { 27 return xulRuntime.QueryInterface(iid); 28 }, 29 }; 30 31 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 32 const XULRUNTIME_CONTRACTID = "@mozilla.org/xre/runtime;1"; 33 const XULRUNTIME_CID = Components.ID( 34 "{f0f0b230-5525-4127-98dc-7bca39059e70}" 35 ); 36 registrar.registerFactory( 37 XULRUNTIME_CID, 38 "XULRuntime", 39 XULRUNTIME_CONTRACTID, 40 xulRuntimeFactory 41 ); 42 43 // When starting in safe mode, the test module should fail to load. 44 let pkcs11ModuleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService( 45 Ci.nsIPKCS11ModuleDB 46 ); 47 let libraryName = ctypes.libraryName("pkcs11testmodule"); 48 let libraryFile = Services.dirsvc.get("CurWorkD", Ci.nsIFile); 49 libraryFile.append("pkcs11testmodule"); 50 libraryFile.append(libraryName); 51 ok(libraryFile.exists(), "The pkcs11testmodule file should exist"); 52 throws( 53 () => 54 pkcs11ModuleDB.addModule("PKCS11 Test Module", libraryFile.path, 0, 0), 55 /NS_ERROR_FAILURE/, 56 "addModule should throw when in safe mode" 57 ); 58 }