Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 3 | */ |
michael@0 | 4 | |
michael@0 | 5 | // Tests that various operations with file pointers work and do not affect the |
michael@0 | 6 | // source files |
michael@0 | 7 | |
michael@0 | 8 | var addon1 = { |
michael@0 | 9 | id: "addon1@tests.mozilla.org", |
michael@0 | 10 | version: "1.0", |
michael@0 | 11 | name: "Test 1", |
michael@0 | 12 | targetApplications: [{ |
michael@0 | 13 | id: "xpcshell@tests.mozilla.org", |
michael@0 | 14 | minVersion: "1", |
michael@0 | 15 | maxVersion: "1" |
michael@0 | 16 | }] |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | var addon1_2 = { |
michael@0 | 20 | id: "addon1@tests.mozilla.org", |
michael@0 | 21 | version: "2.0", |
michael@0 | 22 | name: "Test 1", |
michael@0 | 23 | targetApplications: [{ |
michael@0 | 24 | id: "xpcshell@tests.mozilla.org", |
michael@0 | 25 | minVersion: "1", |
michael@0 | 26 | maxVersion: "1" |
michael@0 | 27 | }] |
michael@0 | 28 | }; |
michael@0 | 29 | |
michael@0 | 30 | var addon2 = { |
michael@0 | 31 | id: "addon2@tests.mozilla.org", |
michael@0 | 32 | version: "1.0", |
michael@0 | 33 | name: "Test 2", |
michael@0 | 34 | targetApplications: [{ |
michael@0 | 35 | id: "xpcshell@tests.mozilla.org", |
michael@0 | 36 | minVersion: "1", |
michael@0 | 37 | maxVersion: "1" |
michael@0 | 38 | }] |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | const profileDir = gProfD.clone(); |
michael@0 | 42 | profileDir.append("extensions"); |
michael@0 | 43 | profileDir.create(AM_Ci.nsIFile.DIRECTORY_TYPE, 0755); |
michael@0 | 44 | |
michael@0 | 45 | const sourceDir = gProfD.clone(); |
michael@0 | 46 | sourceDir.append("source"); |
michael@0 | 47 | |
michael@0 | 48 | Components.utils.import("resource://testing-common/httpd.js"); |
michael@0 | 49 | var testserver; |
michael@0 | 50 | |
michael@0 | 51 | function writePointer(aId, aName) { |
michael@0 | 52 | let file = profileDir.clone(); |
michael@0 | 53 | file.append(aName ? aName : aId); |
michael@0 | 54 | |
michael@0 | 55 | let target = sourceDir.clone(); |
michael@0 | 56 | target.append(do_get_expected_addon_name(aId)); |
michael@0 | 57 | |
michael@0 | 58 | var fos = AM_Cc["@mozilla.org/network/file-output-stream;1"]. |
michael@0 | 59 | createInstance(AM_Ci.nsIFileOutputStream); |
michael@0 | 60 | fos.init(file, |
michael@0 | 61 | FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE, |
michael@0 | 62 | FileUtils.PERMS_FILE, 0); |
michael@0 | 63 | fos.write(target.path, target.path.length); |
michael@0 | 64 | fos.close(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function writeRelativePointer(aId, aName) { |
michael@0 | 68 | let file = profileDir.clone(); |
michael@0 | 69 | file.append(aName ? aName : aId); |
michael@0 | 70 | |
michael@0 | 71 | let absTarget = sourceDir.clone(); |
michael@0 | 72 | absTarget.append(do_get_expected_addon_name(aId)); |
michael@0 | 73 | |
michael@0 | 74 | var relTarget = absTarget.getRelativeDescriptor(profileDir); |
michael@0 | 75 | |
michael@0 | 76 | var fos = AM_Cc["@mozilla.org/network/file-output-stream;1"]. |
michael@0 | 77 | createInstance(AM_Ci.nsIFileOutputStream); |
michael@0 | 78 | fos.init(file, |
michael@0 | 79 | FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE, |
michael@0 | 80 | FileUtils.PERMS_FILE, 0); |
michael@0 | 81 | fos.write(relTarget, relTarget.length); |
michael@0 | 82 | fos.close(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function run_test() { |
michael@0 | 86 | // pointer files only work with unpacked directories |
michael@0 | 87 | if (Services.prefs.getBoolPref("extensions.alwaysUnpack") == false) |
michael@0 | 88 | return; |
michael@0 | 89 | |
michael@0 | 90 | do_test_pending(); |
michael@0 | 91 | createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1"); |
michael@0 | 92 | |
michael@0 | 93 | // Create and configure the HTTP server. |
michael@0 | 94 | testserver = new HttpServer(); |
michael@0 | 95 | testserver.registerDirectory("/data/", do_get_file("data")); |
michael@0 | 96 | testserver.registerDirectory("/addons/", do_get_file("addons")); |
michael@0 | 97 | testserver.start(-1); |
michael@0 | 98 | gPort = testserver.identity.primaryPort; |
michael@0 | 99 | |
michael@0 | 100 | run_test_1(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function end_test() { |
michael@0 | 104 | testserver.stop(do_test_finished); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // Tests that installing a new add-on by pointer works |
michael@0 | 108 | function run_test_1() { |
michael@0 | 109 | writeInstallRDFForExtension(addon1, sourceDir); |
michael@0 | 110 | writePointer(addon1.id); |
michael@0 | 111 | |
michael@0 | 112 | startupManager(); |
michael@0 | 113 | |
michael@0 | 114 | AddonManager.getAddonByID(addon1.id, function(a1) { |
michael@0 | 115 | do_check_neq(a1, null); |
michael@0 | 116 | do_check_eq(a1.version, "1.0"); |
michael@0 | 117 | |
michael@0 | 118 | let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file; |
michael@0 | 119 | do_check_eq(file.parent.path, sourceDir.path); |
michael@0 | 120 | |
michael@0 | 121 | let rootUri = do_get_addon_root_uri(sourceDir, addon1.id); |
michael@0 | 122 | let uri = a1.getResourceURI("/"); |
michael@0 | 123 | do_check_eq(uri.spec, rootUri); |
michael@0 | 124 | uri = a1.getResourceURI("install.rdf"); |
michael@0 | 125 | do_check_eq(uri.spec, rootUri + "install.rdf"); |
michael@0 | 126 | |
michael@0 | 127 | // Check that upgrade is disabled for addons installed by file-pointers. |
michael@0 | 128 | do_check_eq(a1.permissions & AddonManager.PERM_CAN_UPGRADE, 0); |
michael@0 | 129 | run_test_2(); |
michael@0 | 130 | }); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // Tests that installing the addon from some other source doesn't clobber |
michael@0 | 134 | // the original sources |
michael@0 | 135 | function run_test_2() { |
michael@0 | 136 | prepare_test({}, [ |
michael@0 | 137 | "onNewInstall", |
michael@0 | 138 | ]); |
michael@0 | 139 | |
michael@0 | 140 | let url = "http://localhost:" + gPort + "/addons/test_filepointer.xpi"; |
michael@0 | 141 | AddonManager.getInstallForURL(url, function(install) { |
michael@0 | 142 | ensure_test_completed(); |
michael@0 | 143 | |
michael@0 | 144 | prepare_test({ |
michael@0 | 145 | "addon1@tests.mozilla.org": [ |
michael@0 | 146 | "onInstalling" |
michael@0 | 147 | ] |
michael@0 | 148 | }, [ |
michael@0 | 149 | "onDownloadStarted", |
michael@0 | 150 | "onDownloadEnded", |
michael@0 | 151 | "onInstallStarted", |
michael@0 | 152 | "onInstallEnded" |
michael@0 | 153 | ], callback_soon(check_test_2)); |
michael@0 | 154 | |
michael@0 | 155 | install.install(); |
michael@0 | 156 | }, "application/x-xpinstall"); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | function check_test_2() { |
michael@0 | 160 | restartManager(); |
michael@0 | 161 | |
michael@0 | 162 | AddonManager.getAddonByID(addon1.id, function(a1) { |
michael@0 | 163 | do_check_neq(a1, null); |
michael@0 | 164 | do_check_eq(a1.version, "2.0"); |
michael@0 | 165 | |
michael@0 | 166 | let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file; |
michael@0 | 167 | do_check_eq(file.parent.path, profileDir.path); |
michael@0 | 168 | |
michael@0 | 169 | let rootUri = do_get_addon_root_uri(profileDir, addon1.id); |
michael@0 | 170 | let uri = a1.getResourceURI("/"); |
michael@0 | 171 | do_check_eq(uri.spec, rootUri); |
michael@0 | 172 | uri = a1.getResourceURI("install.rdf"); |
michael@0 | 173 | do_check_eq(uri.spec, rootUri + "install.rdf"); |
michael@0 | 174 | |
michael@0 | 175 | let source = sourceDir.clone(); |
michael@0 | 176 | source.append(addon1.id); |
michael@0 | 177 | do_check_true(source.exists()); |
michael@0 | 178 | |
michael@0 | 179 | a1.uninstall(); |
michael@0 | 180 | |
michael@0 | 181 | do_execute_soon(run_test_3); |
michael@0 | 182 | }); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | // Tests that uninstalling doesn't clobber the original sources |
michael@0 | 186 | function run_test_3() { |
michael@0 | 187 | restartManager(); |
michael@0 | 188 | |
michael@0 | 189 | writePointer(addon1.id); |
michael@0 | 190 | |
michael@0 | 191 | restartManager(); |
michael@0 | 192 | |
michael@0 | 193 | AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) { |
michael@0 | 194 | do_check_neq(a1, null); |
michael@0 | 195 | do_check_eq(a1.version, "1.0"); |
michael@0 | 196 | |
michael@0 | 197 | a1.uninstall(); |
michael@0 | 198 | |
michael@0 | 199 | restartManager(); |
michael@0 | 200 | |
michael@0 | 201 | let source = sourceDir.clone(); |
michael@0 | 202 | source.append(addon1.id); |
michael@0 | 203 | do_check_true(source.exists()); |
michael@0 | 204 | |
michael@0 | 205 | do_execute_soon(run_test_4); |
michael@0 | 206 | })); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | // Tests that misnaming a pointer doesn't clobber the sources |
michael@0 | 210 | function run_test_4() { |
michael@0 | 211 | writePointer("addon2@tests.mozilla.org", addon1.id); |
michael@0 | 212 | |
michael@0 | 213 | restartManager(); |
michael@0 | 214 | |
michael@0 | 215 | AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org", |
michael@0 | 216 | "addon2@tests.mozilla.org"], function([a1, a2]) { |
michael@0 | 217 | do_check_eq(a1, null); |
michael@0 | 218 | do_check_eq(a2, null); |
michael@0 | 219 | |
michael@0 | 220 | let source = sourceDir.clone(); |
michael@0 | 221 | source.append(addon1.id); |
michael@0 | 222 | do_check_true(source.exists()); |
michael@0 | 223 | |
michael@0 | 224 | let pointer = profileDir.clone(); |
michael@0 | 225 | pointer.append("addon2@tests.mozilla.org"); |
michael@0 | 226 | do_check_false(pointer.exists()); |
michael@0 | 227 | |
michael@0 | 228 | do_execute_soon(run_test_5); |
michael@0 | 229 | }); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | // Tests that changing the ID of an existing add-on doesn't clobber the sources |
michael@0 | 233 | function run_test_5() { |
michael@0 | 234 | var dest = writeInstallRDFForExtension(addon1, sourceDir); |
michael@0 | 235 | // Make sure the modification time changes enough to be detected. |
michael@0 | 236 | setExtensionModifiedTime(dest, dest.lastModifiedTime - 5000); |
michael@0 | 237 | writePointer(addon1.id); |
michael@0 | 238 | |
michael@0 | 239 | restartManager(); |
michael@0 | 240 | |
michael@0 | 241 | AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) { |
michael@0 | 242 | do_check_neq(a1, null); |
michael@0 | 243 | do_check_eq(a1.version, "1.0"); |
michael@0 | 244 | |
michael@0 | 245 | writeInstallRDFForExtension(addon2, sourceDir, addon1.id); |
michael@0 | 246 | |
michael@0 | 247 | restartManager(); |
michael@0 | 248 | |
michael@0 | 249 | AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org", |
michael@0 | 250 | "addon2@tests.mozilla.org"], function([a1, a2]) { |
michael@0 | 251 | do_check_eq(a1, null); |
michael@0 | 252 | do_check_eq(a2, null); |
michael@0 | 253 | |
michael@0 | 254 | let source = sourceDir.clone(); |
michael@0 | 255 | source.append(addon1.id); |
michael@0 | 256 | do_check_true(source.exists()); |
michael@0 | 257 | |
michael@0 | 258 | let pointer = profileDir.clone(); |
michael@0 | 259 | pointer.append(addon1.id); |
michael@0 | 260 | do_check_false(pointer.exists()); |
michael@0 | 261 | |
michael@0 | 262 | do_execute_soon(run_test_6); |
michael@0 | 263 | }); |
michael@0 | 264 | })); |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | // Removing the pointer file should uninstall the add-on |
michael@0 | 268 | function run_test_6() { |
michael@0 | 269 | var dest = writeInstallRDFForExtension(addon1, sourceDir); |
michael@0 | 270 | // Make sure the modification time changes enough to be detected in run_test_8. |
michael@0 | 271 | setExtensionModifiedTime(dest, dest.lastModifiedTime - 5000); |
michael@0 | 272 | writePointer(addon1.id); |
michael@0 | 273 | |
michael@0 | 274 | restartManager(); |
michael@0 | 275 | |
michael@0 | 276 | AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) { |
michael@0 | 277 | do_check_neq(a1, null); |
michael@0 | 278 | do_check_eq(a1.version, "1.0"); |
michael@0 | 279 | |
michael@0 | 280 | let pointer = profileDir.clone(); |
michael@0 | 281 | pointer.append(addon1.id); |
michael@0 | 282 | pointer.remove(false); |
michael@0 | 283 | |
michael@0 | 284 | restartManager(); |
michael@0 | 285 | |
michael@0 | 286 | AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) { |
michael@0 | 287 | do_check_eq(a1, null); |
michael@0 | 288 | |
michael@0 | 289 | do_execute_soon(run_test_7); |
michael@0 | 290 | }); |
michael@0 | 291 | })); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | // Removing the pointer file and replacing it with a directory should work |
michael@0 | 295 | function run_test_7() { |
michael@0 | 296 | writePointer(addon1.id); |
michael@0 | 297 | |
michael@0 | 298 | restartManager(); |
michael@0 | 299 | |
michael@0 | 300 | AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) { |
michael@0 | 301 | do_check_neq(a1, null); |
michael@0 | 302 | do_check_eq(a1.version, "1.0"); |
michael@0 | 303 | |
michael@0 | 304 | let pointer = profileDir.clone(); |
michael@0 | 305 | pointer.append(addon1.id); |
michael@0 | 306 | pointer.remove(false); |
michael@0 | 307 | |
michael@0 | 308 | writeInstallRDFForExtension(addon1_2, profileDir); |
michael@0 | 309 | |
michael@0 | 310 | restartManager(); |
michael@0 | 311 | |
michael@0 | 312 | AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) { |
michael@0 | 313 | do_check_neq(a1, null); |
michael@0 | 314 | do_check_eq(a1.version, "2.0"); |
michael@0 | 315 | |
michael@0 | 316 | a1.uninstall(); |
michael@0 | 317 | |
michael@0 | 318 | do_execute_soon(run_test_8); |
michael@0 | 319 | }); |
michael@0 | 320 | })); |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | // Changes to the source files should be detected |
michael@0 | 324 | function run_test_8() { |
michael@0 | 325 | restartManager(); |
michael@0 | 326 | |
michael@0 | 327 | writePointer(addon1.id); |
michael@0 | 328 | |
michael@0 | 329 | restartManager(); |
michael@0 | 330 | |
michael@0 | 331 | AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) { |
michael@0 | 332 | do_check_neq(a1, null); |
michael@0 | 333 | do_check_eq(a1.version, "1.0"); |
michael@0 | 334 | |
michael@0 | 335 | writeInstallRDFForExtension(addon1_2, sourceDir); |
michael@0 | 336 | |
michael@0 | 337 | restartManager(); |
michael@0 | 338 | |
michael@0 | 339 | AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) { |
michael@0 | 340 | do_check_neq(a1, null); |
michael@0 | 341 | do_check_eq(a1.version, "2.0"); |
michael@0 | 342 | |
michael@0 | 343 | a1.uninstall(); |
michael@0 | 344 | |
michael@0 | 345 | do_execute_soon(run_test_9); |
michael@0 | 346 | }); |
michael@0 | 347 | })); |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | // Removing the add-on the pointer file points at should uninstall the add-on |
michael@0 | 351 | function run_test_9() { |
michael@0 | 352 | restartManager(); |
michael@0 | 353 | |
michael@0 | 354 | var dest = writeInstallRDFForExtension(addon1, sourceDir); |
michael@0 | 355 | writePointer(addon1.id); |
michael@0 | 356 | |
michael@0 | 357 | restartManager(); |
michael@0 | 358 | |
michael@0 | 359 | AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) { |
michael@0 | 360 | do_check_neq(a1, null); |
michael@0 | 361 | do_check_eq(a1.version, "1.0"); |
michael@0 | 362 | |
michael@0 | 363 | dest.remove(true); |
michael@0 | 364 | |
michael@0 | 365 | restartManager(); |
michael@0 | 366 | |
michael@0 | 367 | AddonManager.getAddonByID(addon1.id, function(a1) { |
michael@0 | 368 | do_check_eq(a1, null); |
michael@0 | 369 | |
michael@0 | 370 | let pointer = profileDir.clone(); |
michael@0 | 371 | pointer.append(addon1.id); |
michael@0 | 372 | do_check_false(pointer.exists()); |
michael@0 | 373 | |
michael@0 | 374 | do_execute_soon(run_test_10); |
michael@0 | 375 | }); |
michael@0 | 376 | })); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | // Tests that installing a new add-on by pointer with a relative path works |
michael@0 | 380 | function run_test_10() { |
michael@0 | 381 | writeInstallRDFForExtension(addon1, sourceDir); |
michael@0 | 382 | writeRelativePointer(addon1.id); |
michael@0 | 383 | |
michael@0 | 384 | restartManager(); |
michael@0 | 385 | |
michael@0 | 386 | AddonManager.getAddonByID(addon1.id, function(a1) { |
michael@0 | 387 | do_check_neq(a1, null); |
michael@0 | 388 | do_check_eq(a1.version, "1.0"); |
michael@0 | 389 | |
michael@0 | 390 | let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file; |
michael@0 | 391 | do_check_eq(file.parent.path, sourceDir.path); |
michael@0 | 392 | |
michael@0 | 393 | let rootUri = do_get_addon_root_uri(sourceDir, addon1.id); |
michael@0 | 394 | let uri = a1.getResourceURI("/"); |
michael@0 | 395 | do_check_eq(uri.spec, rootUri); |
michael@0 | 396 | uri = a1.getResourceURI("install.rdf"); |
michael@0 | 397 | do_check_eq(uri.spec, rootUri + "install.rdf"); |
michael@0 | 398 | |
michael@0 | 399 | // Check that upgrade is disabled for addons installed by file-pointers. |
michael@0 | 400 | do_check_eq(a1.permissions & AddonManager.PERM_CAN_UPGRADE, 0); |
michael@0 | 401 | end_test(); |
michael@0 | 402 | }); |
michael@0 | 403 | } |