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