toolkit/mozapps/extensions/test/xpcshell/test_overrideblocklist.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.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/
     3  */
     5 const KEY_PROFILEDIR                  = "ProfD";
     6 const KEY_APPDIR                      = "XCurProcD";
     7 const FILE_BLOCKLIST                  = "blocklist.xml";
     9 const PREF_BLOCKLIST_ENABLED          = "extensions.blocklist.enabled";
    11 const OLD = do_get_file("data/test_overrideblocklist/old.xml");
    12 const NEW = do_get_file("data/test_overrideblocklist/new.xml");
    13 const ANCIENT = do_get_file("data/test_overrideblocklist/ancient.xml");
    14 const OLD_TSTAMP = 1296046918000;
    15 const NEW_TSTAMP = 1396046918000;
    17 const gAppDir = FileUtils.getFile(KEY_APPDIR, []);
    19 let oldAddon = {
    20   id: "old@tests.mozilla.org",
    21   version: 1
    22 }
    23 let newAddon = {
    24   id: "new@tests.mozilla.org",
    25   version: 1
    26 }
    27 let ancientAddon = {
    28   id: "ancient@tests.mozilla.org",
    29   version: 1
    30 }
    31 let invalidAddon = {
    32   id: "invalid@tests.mozilla.org",
    33   version: 1
    34 }
    36 function incrementAppVersion() {
    37   gAppInfo.version = "" + (parseInt(gAppInfo.version) + 1);
    38 }
    40 function clearBlocklists() {
    41   let blocklist = FileUtils.getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
    42   if (blocklist.exists())
    43     blocklist.remove(true);
    45   blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
    46   if (blocklist.exists())
    47     blocklist.remove(true);
    48 }
    50 function reloadBlocklist() {
    51   Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false);
    52   Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true);
    53 }
    55 function copyToApp(file) {
    56   file.clone().copyTo(gAppDir, FILE_BLOCKLIST);
    57 }
    59 function copyToProfile(file, tstamp) {
    60   file = file.clone();
    61   file.copyTo(gProfD, FILE_BLOCKLIST);
    62   file = gProfD.clone();
    63   file.append(FILE_BLOCKLIST);
    64   file.lastModifiedTime = tstamp;
    65 }
    67 function run_test() {
    68   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
    70   let appBlocklist = FileUtils.getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
    71   if (appBlocklist.exists()) {
    72     try {
    73       appBlocklist.moveTo(gAppDir, "blocklist.old");
    74     }
    75     catch (e) {
    76       todo(false, "Aborting test due to unmovable blocklist file: " + e);
    77       return;
    78     }
    79     do_register_cleanup(function() {
    80       clearBlocklists();
    81       appBlocklist.moveTo(gAppDir, FILE_BLOCKLIST);
    82     });
    83   }
    85   run_next_test();
    86 }
    88 // On first run whataver is in the app dir should get copied to the profile
    89 add_test(function test_copy() {
    90   clearBlocklists();
    91   copyToApp(OLD);
    93   incrementAppVersion();
    94   startupManager();
    96   reloadBlocklist();
    97   let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
    98                   getService(AM_Ci.nsIBlocklistService);
    99   do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   100   do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   101   do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   102   do_check_false(blocklist.isAddonBlocklisted(newAddon));
   104   shutdownManager();
   106   run_next_test();
   107 });
   109 // An ancient blocklist should be ignored
   110 add_test(function test_ancient() {
   111   clearBlocklists();
   112   copyToApp(ANCIENT);
   113   copyToProfile(OLD, OLD_TSTAMP);
   115   incrementAppVersion();
   116   startupManager();
   118   reloadBlocklist();
   119   let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   120                   getService(AM_Ci.nsIBlocklistService);
   121   do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   122   do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   123   do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   124   do_check_false(blocklist.isAddonBlocklisted(newAddon));
   126   shutdownManager();
   128   run_next_test();
   129 });
   131 // A new blocklist should override an old blocklist
   132 add_test(function test_override() {
   133   clearBlocklists();
   134   copyToApp(NEW);
   135   copyToProfile(OLD, OLD_TSTAMP);
   137   incrementAppVersion();
   138   startupManager();
   140   reloadBlocklist();
   141   let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   142                   getService(AM_Ci.nsIBlocklistService);
   143   do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   144   do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   145   do_check_false(blocklist.isAddonBlocklisted(oldAddon));
   146   do_check_true(blocklist.isAddonBlocklisted(newAddon));
   148   shutdownManager();
   150   run_next_test();
   151 });
   153 // An old blocklist shouldn't override a new blocklist
   154 add_test(function test_retain() {
   155   clearBlocklists();
   156   copyToApp(OLD);
   157   copyToProfile(NEW, NEW_TSTAMP);
   159   incrementAppVersion();
   160   startupManager();
   162   reloadBlocklist();
   163   let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   164                   getService(AM_Ci.nsIBlocklistService);
   165   do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   166   do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   167   do_check_false(blocklist.isAddonBlocklisted(oldAddon));
   168   do_check_true(blocklist.isAddonBlocklisted(newAddon));
   170   shutdownManager();
   172   run_next_test();
   173 });
   175 // A missing blocklist in the profile should still load an app-shipped blocklist
   176 add_test(function test_missing() {
   177   clearBlocklists();
   178   copyToApp(OLD);
   179   copyToProfile(NEW, NEW_TSTAMP);
   181   incrementAppVersion();
   182   startupManager();
   183   shutdownManager();
   185   let blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
   186   blocklist.remove(true);
   187   startupManager(false);
   189   reloadBlocklist();
   190   let blocklist = AM_Cc["@mozilla.org/extensions/blocklist;1"].
   191                   getService(AM_Ci.nsIBlocklistService);
   192   do_check_false(blocklist.isAddonBlocklisted(invalidAddon));
   193   do_check_false(blocklist.isAddonBlocklisted(ancientAddon));
   194   do_check_true(blocklist.isAddonBlocklisted(oldAddon));
   195   do_check_false(blocklist.isAddonBlocklisted(newAddon));
   197   shutdownManager();
   199   run_next_test();
   200 });

mercurial