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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9f9175739caf
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4
5 const KEY_PROFILEDIR = "ProfD";
6 const KEY_APPDIR = "XCurProcD";
7 const FILE_BLOCKLIST = "blocklist.xml";
8
9 const PREF_BLOCKLIST_ENABLED = "extensions.blocklist.enabled";
10
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;
16
17 const gAppDir = FileUtils.getFile(KEY_APPDIR, []);
18
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 }
35
36 function incrementAppVersion() {
37 gAppInfo.version = "" + (parseInt(gAppInfo.version) + 1);
38 }
39
40 function clearBlocklists() {
41 let blocklist = FileUtils.getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
42 if (blocklist.exists())
43 blocklist.remove(true);
44
45 blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
46 if (blocklist.exists())
47 blocklist.remove(true);
48 }
49
50 function reloadBlocklist() {
51 Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false);
52 Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true);
53 }
54
55 function copyToApp(file) {
56 file.clone().copyTo(gAppDir, FILE_BLOCKLIST);
57 }
58
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 }
66
67 function run_test() {
68 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
69
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 }
84
85 run_next_test();
86 }
87
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);
92
93 incrementAppVersion();
94 startupManager();
95
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));
103
104 shutdownManager();
105
106 run_next_test();
107 });
108
109 // An ancient blocklist should be ignored
110 add_test(function test_ancient() {
111 clearBlocklists();
112 copyToApp(ANCIENT);
113 copyToProfile(OLD, OLD_TSTAMP);
114
115 incrementAppVersion();
116 startupManager();
117
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));
125
126 shutdownManager();
127
128 run_next_test();
129 });
130
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);
136
137 incrementAppVersion();
138 startupManager();
139
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));
147
148 shutdownManager();
149
150 run_next_test();
151 });
152
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);
158
159 incrementAppVersion();
160 startupManager();
161
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));
169
170 shutdownManager();
171
172 run_next_test();
173 });
174
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);
180
181 incrementAppVersion();
182 startupManager();
183 shutdownManager();
184
185 let blocklist = FileUtils.getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
186 blocklist.remove(true);
187 startupManager(false);
188
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));
196
197 shutdownManager();
198
199 run_next_test();
200 });

mercurial