1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug430120.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + */ 1.8 + 1.9 +const BLOCKLIST_TIMER = "blocklist-background-update-timer"; 1.10 +const PREF_BLOCKLIST_URL = "extensions.blocklist.url"; 1.11 +const PREF_BLOCKLIST_ENABLED = "extensions.blocklist.enabled"; 1.12 +const PREF_APP_DISTRIBUTION = "distribution.id"; 1.13 +const PREF_APP_DISTRIBUTION_VERSION = "distribution.version"; 1.14 +const PREF_APP_UPDATE_CHANNEL = "app.update.channel"; 1.15 +const PREF_GENERAL_USERAGENT_LOCALE = "general.useragent.locale"; 1.16 +const CATEGORY_UPDATE_TIMER = "update-timer"; 1.17 + 1.18 +// Get the HTTP server. 1.19 +Components.utils.import("resource://testing-common/httpd.js"); 1.20 +var testserver; 1.21 +var gOSVersion; 1.22 +var gBlocklist; 1.23 + 1.24 +// This is a replacement for the timer service so we can trigger timers 1.25 +var timerService = { 1.26 + 1.27 + hasTimer: function(id) { 1.28 + var catMan = Components.classes["@mozilla.org/categorymanager;1"] 1.29 + .getService(Components.interfaces.nsICategoryManager); 1.30 + var entries = catMan.enumerateCategory(CATEGORY_UPDATE_TIMER); 1.31 + while (entries.hasMoreElements()) { 1.32 + var entry = entries.getNext().QueryInterface(Components.interfaces.nsISupportsCString).data; 1.33 + var value = catMan.getCategoryEntry(CATEGORY_UPDATE_TIMER, entry); 1.34 + var timerID = value.split(",")[2]; 1.35 + if (id == timerID) { 1.36 + return true; 1.37 + } 1.38 + } 1.39 + return false; 1.40 + }, 1.41 + 1.42 + fireTimer: function(id) { 1.43 + gBlocklist.QueryInterface(Components.interfaces.nsITimerCallback).notify(null); 1.44 + }, 1.45 + 1.46 + QueryInterface: function(iid) { 1.47 + if (iid.equals(Components.interfaces.nsIUpdateTimerManager) 1.48 + || iid.equals(Components.interfaces.nsISupports)) 1.49 + return this; 1.50 + 1.51 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.52 + } 1.53 +}; 1.54 + 1.55 +var TimerServiceFactory = { 1.56 + createInstance: function (outer, iid) { 1.57 + if (outer != null) 1.58 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.59 + return timerService.QueryInterface(iid); 1.60 + } 1.61 +}; 1.62 +var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); 1.63 +registrar.registerFactory(Components.ID("{61189e7a-6b1b-44b8-ac81-f180a6105085}"), "TimerService", 1.64 + "@mozilla.org/updates/timer-manager;1", TimerServiceFactory); 1.65 + 1.66 +function failHandler(metadata, response) { 1.67 + do_throw("Should not have attempted to retrieve the blocklist when it is disabled"); 1.68 +} 1.69 + 1.70 +function pathHandler(metadata, response) { 1.71 + var ABI = "noarch-spidermonkey"; 1.72 + // the blacklist service special-cases ABI for Universal binaries, 1.73 + // so do the same here. 1.74 + if ("@mozilla.org/xpcom/mac-utils;1" in Components.classes) { 1.75 + var macutils = Components.classes["@mozilla.org/xpcom/mac-utils;1"] 1.76 + .getService(Components.interfaces.nsIMacUtils); 1.77 + if (macutils.isUniversalBinary) 1.78 + ABI += "-u-" + macutils.architecturesInBinary; 1.79 + } 1.80 + do_check_eq(metadata.queryString, 1.81 + "xpcshell@tests.mozilla.org&1&XPCShell&1&2007010101&" + 1.82 + "XPCShell_" + ABI + "&locale&updatechannel&" + 1.83 + gOSVersion + "&1.9&distribution&distribution-version"); 1.84 + gBlocklist.observe(null, "quit-application", ""); 1.85 + gBlocklist.observe(null, "xpcom-shutdown", ""); 1.86 + testserver.stop(do_test_finished); 1.87 +} 1.88 + 1.89 +function run_test() { 1.90 + var osVersion; 1.91 + var sysInfo = Components.classes["@mozilla.org/system-info;1"] 1.92 + .getService(Components.interfaces.nsIPropertyBag2); 1.93 + try { 1.94 + osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version"); 1.95 + if (osVersion) { 1.96 + try { 1.97 + osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")"; 1.98 + } 1.99 + catch (e) { 1.100 + } 1.101 + gOSVersion = encodeURIComponent(osVersion); 1.102 + } 1.103 + } 1.104 + catch (e) { 1.105 + } 1.106 + 1.107 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); 1.108 + 1.109 + testserver = new HttpServer(); 1.110 + testserver.registerPathHandler("/1", failHandler); 1.111 + testserver.registerPathHandler("/2", pathHandler); 1.112 + testserver.start(-1); 1.113 + gPort = testserver.identity.primaryPort; 1.114 + 1.115 + // Initialise the blocklist service 1.116 + gBlocklist = Components.classes["@mozilla.org/extensions/blocklist;1"] 1.117 + .getService(Components.interfaces.nsIBlocklistService) 1.118 + .QueryInterface(Components.interfaces.nsIObserver); 1.119 + gBlocklist.observe(null, "profile-after-change", ""); 1.120 + 1.121 + do_check_true(timerService.hasTimer(BLOCKLIST_TIMER)); 1.122 + 1.123 + do_test_pending(); 1.124 + 1.125 + // This should have no effect as the blocklist is disabled 1.126 + Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://localhost:" + gPort + "/1"); 1.127 + Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false); 1.128 + timerService.fireTimer(BLOCKLIST_TIMER); 1.129 + 1.130 + // Some values have to be on the default branch to work 1.131 + var defaults = Services.prefs.QueryInterface(Components.interfaces.nsIPrefService) 1.132 + .getDefaultBranch(null); 1.133 + defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "updatechannel"); 1.134 + defaults.setCharPref(PREF_APP_DISTRIBUTION, "distribution"); 1.135 + defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "distribution-version"); 1.136 + defaults.setCharPref(PREF_GENERAL_USERAGENT_LOCALE, "locale"); 1.137 + 1.138 + // This should correctly escape everything 1.139 + Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://localhost:" + gPort + "/2?" + 1.140 + "%APP_ID%&%APP_VERSION%&%PRODUCT%&%VERSION%&%BUILD_ID%&" + 1.141 + "%BUILD_TARGET%&%LOCALE%&%CHANNEL%&" + 1.142 + "%OS_VERSION%&%PLATFORM_VERSION%&%DISTRIBUTION%&%DISTRIBUTION_VERSION%"); 1.143 + Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true); 1.144 + timerService.fireTimer(BLOCKLIST_TIMER); 1.145 +}