toolkit/mozapps/extensions/test/xpcshell/test_overrideblocklist.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_overrideblocklist.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,200 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +const KEY_PROFILEDIR                  = "ProfD";
     1.9 +const KEY_APPDIR                      = "XCurProcD";
    1.10 +const FILE_BLOCKLIST                  = "blocklist.xml";
    1.11 +
    1.12 +const PREF_BLOCKLIST_ENABLED          = "extensions.blocklist.enabled";
    1.13 +
    1.14 +const OLD = do_get_file("data/test_overrideblocklist/old.xml");
    1.15 +const NEW = do_get_file("data/test_overrideblocklist/new.xml");
    1.16 +const ANCIENT = do_get_file("data/test_overrideblocklist/ancient.xml");
    1.17 +const OLD_TSTAMP = 1296046918000;
    1.18 +const NEW_TSTAMP = 1396046918000;
    1.19 +
    1.20 +const gAppDir = FileUtils.getFile(KEY_APPDIR, []);
    1.21 +
    1.22 +let oldAddon = {
    1.23 +  id: "old@tests.mozilla.org",
    1.24 +  version: 1
    1.25 +}
    1.26 +let newAddon = {
    1.27 +  id: "new@tests.mozilla.org",
    1.28 +  version: 1
    1.29 +}
    1.30 +let ancientAddon = {
    1.31 +  id: "ancient@tests.mozilla.org",
    1.32 +  version: 1
    1.33 +}
    1.34 +let invalidAddon = {
    1.35 +  id: "invalid@tests.mozilla.org",
    1.36 +  version: 1
    1.37 +}
    1.38 +
    1.39 +function incrementAppVersion() {
    1.40 +  gAppInfo.version = "" + (parseInt(gAppInfo.version) + 1);
    1.41 +}
    1.42 +
    1.43 +function clearBlocklists() {
    1.44 +  let blocklist = FileUtils.getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
    1.45 +  if (blocklist.exists())
    1.46 +    blocklist.remove(true);
    1.47 +
    1.48 +  blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
    1.49 +  if (blocklist.exists())
    1.50 +    blocklist.remove(true);
    1.51 +}
    1.52 +
    1.53 +function reloadBlocklist() {
    1.54 +  Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false);
    1.55 +  Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true);
    1.56 +}
    1.57 +
    1.58 +function copyToApp(file) {
    1.59 +  file.clone().copyTo(gAppDir, FILE_BLOCKLIST);
    1.60 +}
    1.61 +
    1.62 +function copyToProfile(file, tstamp) {
    1.63 +  file = file.clone();
    1.64 +  file.copyTo(gProfD, FILE_BLOCKLIST);
    1.65 +  file = gProfD.clone();
    1.66 +  file.append(FILE_BLOCKLIST);
    1.67 +  file.lastModifiedTime = tstamp;
    1.68 +}
    1.69 +
    1.70 +function run_test() {
    1.71 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
    1.72 +
    1.73 +  let appBlocklist = FileUtils.getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
    1.74 +  if (appBlocklist.exists()) {
    1.75 +    try {
    1.76 +      appBlocklist.moveTo(gAppDir, "blocklist.old");
    1.77 +    }
    1.78 +    catch (e) {
    1.79 +      todo(false, "Aborting test due to unmovable blocklist file: " + e);
    1.80 +      return;
    1.81 +    }
    1.82 +    do_register_cleanup(function() {
    1.83 +      clearBlocklists();
    1.84 +      appBlocklist.moveTo(gAppDir, FILE_BLOCKLIST);
    1.85 +    });
    1.86 +  }
    1.87 +
    1.88 +  run_next_test();
    1.89 +}
    1.90 +
    1.91 +// On first run whataver is in the app dir should get copied to the profile
    1.92 +add_test(function test_copy() {
    1.93 +  clearBlocklists();
    1.94 +  copyToApp(OLD);
    1.95 +
    1.96 +  incrementAppVersion();
    1.97 +  startupManager();
    1.98 +
    1.99 +  reloadBlocklist();
   1.100 +  let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   1.101 +                  getService(AM_Ci.nsIBlocklistService);
   1.102 +  do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   1.103 +  do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   1.104 +  do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   1.105 +  do_check_false(blocklist.isAddonBlocklisted(newAddon));
   1.106 +
   1.107 +  shutdownManager();
   1.108 +
   1.109 +  run_next_test();
   1.110 +});
   1.111 +
   1.112 +// An ancient blocklist should be ignored
   1.113 +add_test(function test_ancient() {
   1.114 +  clearBlocklists();
   1.115 +  copyToApp(ANCIENT);
   1.116 +  copyToProfile(OLD, OLD_TSTAMP);
   1.117 +
   1.118 +  incrementAppVersion();
   1.119 +  startupManager();
   1.120 +
   1.121 +  reloadBlocklist();
   1.122 +  let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   1.123 +                  getService(AM_Ci.nsIBlocklistService);
   1.124 +  do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   1.125 +  do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   1.126 +  do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   1.127 +  do_check_false(blocklist.isAddonBlocklisted(newAddon));
   1.128 +
   1.129 +  shutdownManager();
   1.130 +
   1.131 +  run_next_test();
   1.132 +});
   1.133 +
   1.134 +// A new blocklist should override an old blocklist
   1.135 +add_test(function test_override() {
   1.136 +  clearBlocklists();
   1.137 +  copyToApp(NEW);
   1.138 +  copyToProfile(OLD, OLD_TSTAMP);
   1.139 +
   1.140 +  incrementAppVersion();
   1.141 +  startupManager();
   1.142 +
   1.143 +  reloadBlocklist();
   1.144 +  let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   1.145 +                  getService(AM_Ci.nsIBlocklistService);
   1.146 +  do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   1.147 +  do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   1.148 +  do_check_false(blocklist.isAddonBlocklisted(oldAddon));
   1.149 +  do_check_true(blocklist.isAddonBlocklisted(newAddon));
   1.150 +
   1.151 +  shutdownManager();
   1.152 +
   1.153 +  run_next_test();
   1.154 +});
   1.155 +
   1.156 +// An old blocklist shouldn't override a new blocklist
   1.157 +add_test(function test_retain() {
   1.158 +  clearBlocklists();
   1.159 +  copyToApp(OLD);
   1.160 +  copyToProfile(NEW, NEW_TSTAMP);
   1.161 +
   1.162 +  incrementAppVersion();
   1.163 +  startupManager();
   1.164 +
   1.165 +  reloadBlocklist();
   1.166 +  let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   1.167 +                  getService(AM_Ci.nsIBlocklistService);
   1.168 +  do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   1.169 +  do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   1.170 +  do_check_false(blocklist.isAddonBlocklisted(oldAddon));
   1.171 +  do_check_true(blocklist.isAddonBlocklisted(newAddon));
   1.172 +
   1.173 +  shutdownManager();
   1.174 +
   1.175 +  run_next_test();
   1.176 +});
   1.177 +
   1.178 +// A missing blocklist in the profile should still load an app-shipped blocklist
   1.179 +add_test(function test_missing() {
   1.180 +  clearBlocklists();
   1.181 +  copyToApp(OLD);
   1.182 +  copyToProfile(NEW, NEW_TSTAMP);
   1.183 +
   1.184 +  incrementAppVersion();
   1.185 +  startupManager();
   1.186 +  shutdownManager();
   1.187 +
   1.188 +  let blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
   1.189 +  blocklist.remove(true);
   1.190 +  startupManager(false);
   1.191 +
   1.192 +  reloadBlocklist();
   1.193 +  let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   1.194 +                  getService(AM_Ci.nsIBlocklistService);
   1.195 +  do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   1.196 +  do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   1.197 +  do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   1.198 +  do_check_false(blocklist.isAddonBlocklisted(newAddon));
   1.199 +
   1.200 +  shutdownManager();
   1.201 +
   1.202 +  run_next_test();
   1.203 +});

mercurial