toolkit/mozapps/update/tests/unit_aus_update/canCheckForAndCanApplyUpdates.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4 */
michael@0 5
michael@0 6 function run_test() {
michael@0 7 setupTestCommon();
michael@0 8
michael@0 9 // Verify write access to the custom app dir
michael@0 10 logTestInfo("testing write access to the application directory");
michael@0 11 let testFile = getCurrentProcessDir();
michael@0 12 testFile.append("update_write_access_test");
michael@0 13 testFile.create(AUS_Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
michael@0 14 do_check_true(testFile.exists());
michael@0 15 testFile.remove(false);
michael@0 16 do_check_false(testFile.exists());
michael@0 17
michael@0 18 standardInit();
michael@0 19
michael@0 20 if (IS_WIN) {
michael@0 21 // Create a mutex to prevent being able to check for or apply updates.
michael@0 22 logTestInfo("attempting to create mutex");
michael@0 23 let handle = createMutex(getPerInstallationMutexName());
michael@0 24
michael@0 25 logTestInfo("testing that the mutex was successfully created");
michael@0 26 do_check_neq(handle, null);
michael@0 27
michael@0 28 // Check if available updates cannot be checked for when there is a mutex
michael@0 29 // for this installation.
michael@0 30 logTestInfo("testing nsIApplicationUpdateService:canCheckForUpdates is " +
michael@0 31 "false when there is a mutex");
michael@0 32 do_check_false(gAUS.canCheckForUpdates);
michael@0 33
michael@0 34 // Check if updates cannot be applied when there is a mutex for this
michael@0 35 // installation.
michael@0 36 logTestInfo("testing nsIApplicationUpdateService:canApplyUpdates is " +
michael@0 37 "false when there is a mutex");
michael@0 38 do_check_false(gAUS.canApplyUpdates);
michael@0 39
michael@0 40 logTestInfo("destroying mutex");
michael@0 41 closeHandle(handle)
michael@0 42 }
michael@0 43
michael@0 44 // Check if available updates can be checked for
michael@0 45 logTestInfo("testing nsIApplicationUpdateService:canCheckForUpdates is true");
michael@0 46 do_check_true(gAUS.canCheckForUpdates);
michael@0 47 // Check if updates can be applied
michael@0 48 logTestInfo("testing nsIApplicationUpdateService:canApplyUpdates is true");
michael@0 49 do_check_true(gAUS.canApplyUpdates);
michael@0 50
michael@0 51 if (IS_WIN) {
michael@0 52 // Attempt to create a mutex when application update has already created one
michael@0 53 // with the same name.
michael@0 54 logTestInfo("attempting to create mutex");
michael@0 55 let handle = createMutex(getPerInstallationMutexName());
michael@0 56
michael@0 57 logTestInfo("testing that the mutex was not successfully created");
michael@0 58 do_check_eq(handle, null);
michael@0 59 }
michael@0 60
michael@0 61 doTestFinish();
michael@0 62 }
michael@0 63
michael@0 64 if (IS_WIN) {
michael@0 65 /**
michael@0 66 * Determines a unique mutex name for the installation.
michael@0 67 *
michael@0 68 * @return Global mutex path.
michael@0 69 */
michael@0 70 function getPerInstallationMutexName() {
michael@0 71 let hasher = AUS_Cc["@mozilla.org/security/hash;1"].
michael@0 72 createInstance(AUS_Ci.nsICryptoHash);
michael@0 73 hasher.init(hasher.SHA1);
michael@0 74
michael@0 75 let exeFile = Services.dirsvc.get(XRE_EXECUTABLE_FILE, AUS_Ci.nsILocalFile);
michael@0 76
michael@0 77 let converter = AUS_Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 78 createInstance(AUS_Ci.nsIScriptableUnicodeConverter);
michael@0 79 converter.charset = "UTF-8";
michael@0 80 let data = converter.convertToByteArray(exeFile.path.toLowerCase());
michael@0 81
michael@0 82 hasher.update(data, data.length);
michael@0 83 return "Global\\MozillaUpdateMutex-" + hasher.finish(true);
michael@0 84 }
michael@0 85
michael@0 86 /**
michael@0 87 * Closes a Win32 handle.
michael@0 88 *
michael@0 89 * @param aHandle
michael@0 90 * The handle to close.
michael@0 91 */
michael@0 92 function closeHandle(aHandle) {
michael@0 93 let lib = ctypes.open("kernel32.dll");
michael@0 94 let CloseHandle = lib.declare("CloseHandle",
michael@0 95 ctypes.winapi_abi,
michael@0 96 ctypes.int32_t, /* success */
michael@0 97 ctypes.void_t.ptr); /* handle */
michael@0 98 CloseHandle(aHandle);
michael@0 99 lib.close();
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Creates a mutex.
michael@0 104 *
michael@0 105 * @param aName
michael@0 106 * The name for the mutex.
michael@0 107 * @return The Win32 handle to the mutex.
michael@0 108 */
michael@0 109 function createMutex(aName) {
michael@0 110 const INITIAL_OWN = 1;
michael@0 111 const ERROR_ALREADY_EXISTS = 0xB7;
michael@0 112 let lib = ctypes.open("kernel32.dll");
michael@0 113 let CreateMutexW = lib.declare("CreateMutexW",
michael@0 114 ctypes.winapi_abi,
michael@0 115 ctypes.void_t.ptr, /* return handle */
michael@0 116 ctypes.void_t.ptr, /* security attributes */
michael@0 117 ctypes.int32_t, /* initial owner */
michael@0 118 ctypes.jschar.ptr); /* name */
michael@0 119
michael@0 120 let handle = CreateMutexW(null, INITIAL_OWN, aName);
michael@0 121 lib.close();
michael@0 122 let alreadyExists = ctypes.winLastError == ERROR_ALREADY_EXISTS;
michael@0 123 if (handle && !handle.isNull() && alreadyExists) {
michael@0 124 closeHandle(handle);
michael@0 125 handle = null;
michael@0 126 }
michael@0 127
michael@0 128 if (handle && handle.isNull()) {
michael@0 129 handle = null;
michael@0 130 }
michael@0 131
michael@0 132 return handle;
michael@0 133 }
michael@0 134 }

mercurial