OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 let {createSandbox} = require("./_common"); | 20 let { |
21 | 21 createSandbox, setupTimerAndXMLHttp, setupRandomResult, unexpectedError, Cr, |
22 const MILLIS_IN_SECOND = 1000; | 22 MILLIS_IN_SECOND, MILLIS_IN_HOUR |
23 const MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | 23 } = require("./_common"); |
24 const MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | |
25 const MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | |
26 | |
27 const Cr = { | |
28 NS_OK: 0, | |
29 NS_BINDING_ABORTED: 0x804B0002, | |
30 NS_ERROR_FAILURE: 0x80004005 | |
31 } | |
32 | 24 |
33 let Filter = null; | 25 let Filter = null; |
34 let FilterStorage = null; | 26 let FilterStorage = null; |
35 let Prefs = null; | 27 let Prefs = null; |
36 let Subscription = null; | 28 let Subscription = null; |
37 let Synchronizer = null; | 29 let Synchronizer = null; |
38 | 30 |
39 exports.setUp = function(callback) | 31 exports.setUp = function(callback) |
40 { | 32 { |
41 let currentTime = 100000 * MILLIS_IN_HOUR; | 33 let globals = Object.assign({}, setupTimerAndXMLHttp.call(this), |
42 let startTime = currentTime; | 34 setupRandomResult.call(this)); |
43 | 35 |
44 let fakeTimer = { | 36 let sandboxedRequire = createSandbox({globals}); |
45 callback: null, | |
46 delay: -1, | |
47 nextExecution: 0, | |
48 | |
49 initWithCallback: function(callback, delay, type) | |
50 { | |
51 if (this.callback) | |
52 throw new Error("Only one timer instance supported"); | |
53 if (type != 1) | |
54 throw new Error("Only TYPE_REPEATING_SLACK timers supported"); | |
55 | |
56 this.callback = callback; | |
57 this.delay = delay; | |
58 this.nextExecution = currentTime + delay; | |
59 }, | |
60 | |
61 trigger: function() | |
62 { | |
63 if (currentTime < this.nextExecution) | |
64 currentTime = this.nextExecution; | |
65 try | |
66 { | |
67 this.callback(); | |
68 } | |
69 finally | |
70 { | |
71 this.nextExecution = currentTime + this.delay; | |
72 } | |
73 }, | |
74 | |
75 cancel: function() | |
76 { | |
77 this.nextExecution = -1; | |
78 } | |
79 }; | |
80 | |
81 let requests = []; | |
82 function XMLHttpRequest() | |
83 { | |
84 this._host = "http://example.com"; | |
85 this._loadHandlers = []; | |
86 this._errorHandlers = []; | |
87 }; | |
88 XMLHttpRequest.prototype = | |
89 { | |
90 _path: null, | |
91 _data: null, | |
92 _queryString: null, | |
93 _loadHandlers: null, | |
94 _errorHandlers: null, | |
95 status: 0, | |
96 readyState: 0, | |
97 responseText: null, | |
98 | |
99 addEventListener: function(eventName, handler, capture) | |
100 { | |
101 let list; | |
102 if (eventName == "load") | |
103 list = this._loadHandlers; | |
104 else if (eventName == "error") | |
105 list = this._errorHandlers; | |
106 else | |
107 throw new Error("Event type " + eventName + " not supported"); | |
108 | |
109 if (list.indexOf(handler) < 0) | |
110 list.push(handler); | |
111 }, | |
112 | |
113 removeEventListener: function(eventName, handler, capture) | |
114 { | |
115 let list; | |
116 if (eventName == "load") | |
117 list = this._loadHandlers; | |
118 else if (eventName == "error") | |
119 list = this._errorHandlers; | |
120 else | |
121 throw new Error("Event type " + eventName + " not supported"); | |
122 | |
123 let index = list.indexOf(handler); | |
124 if (index >= 0) | |
125 list.splice(index, 1); | |
126 }, | |
127 | |
128 open: function(method, url, async, user, password) | |
129 { | |
130 if (method != "GET") | |
131 throw new Error("Only GET requests are supported"); | |
132 if (typeof async != "undefined" && !async) | |
133 throw new Error("Sync requests are not supported"); | |
134 if (typeof user != "undefined" || typeof password != "undefined") | |
135 throw new Error("User authentification is not supported"); | |
136 | |
137 let match = /^data:[^,]+,/.exec(url); | |
138 if (match) | |
139 { | |
140 this._data = decodeURIComponent(url.substr(match[0].length)); | |
141 return; | |
142 } | |
143 | |
144 if (url.substr(0, this._host.length + 1) != this._host + "/") | |
145 throw new Error("Unexpected URL: " + url + " (URL starting with " + this
._host + "expected)"); | |
146 | |
147 this._path = url.substr(this._host.length); | |
148 | |
149 let queryIndex = this._path.indexOf("?"); | |
150 this._queryString = ""; | |
151 if (queryIndex >= 0) | |
152 { | |
153 this._queryString = this._path.substr(queryIndex + 1); | |
154 this._path = this._path.substr(0, queryIndex); | |
155 } | |
156 }, | |
157 | |
158 send: function(data) | |
159 { | |
160 if (!this._data && !this._path) | |
161 throw new Error("No request path set"); | |
162 if (typeof data != "undefined" && data) | |
163 throw new Error("Sending data to server is not supported"); | |
164 | |
165 requests.push(Promise.resolve().then(() => | |
166 { | |
167 let result = [Cr.NS_OK, 404, ""]; | |
168 if (this._data) | |
169 result = [Cr.NS_OK, 0, this._data]; | |
170 else if (this._path in XMLHttpRequest.requestHandlers) | |
171 { | |
172 result = XMLHttpRequest.requestHandlers[this._path]({ | |
173 method: "GET", | |
174 path: this._path, | |
175 queryString: this._queryString | |
176 }); | |
177 } | |
178 | |
179 [this.channel.status, this.channel.responseStatus, this.responseText] =
result; | |
180 this.status = this.channel.responseStatus; | |
181 | |
182 let eventName = (this.channel.status == Cr.NS_OK ? "load" : "error"); | |
183 let event = {type: eventName}; | |
184 for (let handler of this["_" + eventName + "Handlers"]) | |
185 handler.call(this, event); | |
186 })); | |
187 }, | |
188 | |
189 overrideMimeType: function(mime) | |
190 { | |
191 }, | |
192 | |
193 channel: | |
194 { | |
195 status: -1, | |
196 responseStatus: 0, | |
197 loadFlags: 0, | |
198 INHIBIT_CACHING: 0, | |
199 VALIDATE_ALWAYS: 0, | |
200 QueryInterface: () => this | |
201 } | |
202 }; | |
203 | |
204 XMLHttpRequest.requestHandlers = {}; | |
205 this.registerHandler = (path, handler) => | |
206 { | |
207 XMLHttpRequest.requestHandlers[path] = handler; | |
208 }; | |
209 | |
210 function waitForRequests() | |
211 { | |
212 if (requests.length) | |
213 { | |
214 let result = Promise.all(requests); | |
215 requests = []; | |
216 return result.catch(e => | |
217 { | |
218 console.error(e); | |
219 }).then(() => waitForRequests()); | |
220 } | |
221 else | |
222 return Promise.resolve(); | |
223 } | |
224 | |
225 function runScheduledTasks(maxMillis) | |
226 { | |
227 let endTime = currentTime + maxMillis; | |
228 if (fakeTimer.nextExecution < 0 || fakeTimer.nextExecution > endTime) | |
229 { | |
230 currentTime = endTime; | |
231 return Promise.resolve(); | |
232 } | |
233 else | |
234 { | |
235 fakeTimer.trigger(); | |
236 return waitForRequests().then(() => runScheduledTasks(endTime - currentTim
e)); | |
237 } | |
238 | |
239 currentTime = endTime; | |
240 } | |
241 | |
242 this.runScheduledTasks = (maxHours, initial, skip) => | |
243 { | |
244 if (typeof maxHours != "number") | |
245 throw new Error("Numerical parameter expected"); | |
246 if (typeof initial != "number") | |
247 initial = 0; | |
248 if (typeof skip != "number") | |
249 skip = 0; | |
250 | |
251 startTime = currentTime; | |
252 return Promise.resolve().then(() => | |
253 { | |
254 if (initial >= 0) | |
255 { | |
256 maxHours -= initial; | |
257 return runScheduledTasks(initial * MILLIS_IN_HOUR); | |
258 } | |
259 }).then(() => | |
260 { | |
261 if (skip >= 0) | |
262 { | |
263 maxHours -= skip; | |
264 currentTime += skip * MILLIS_IN_HOUR; | |
265 } | |
266 return runScheduledTasks(maxHours * MILLIS_IN_HOUR); | |
267 }); | |
268 }; | |
269 | |
270 this.getTimeOffset = () => (currentTime - startTime) / MILLIS_IN_HOUR; | |
271 Object.defineProperty(this, "currentTime", { | |
272 get: () => currentTime | |
273 }); | |
274 | |
275 let randomResult = 0.5; | |
276 Object.defineProperty(this, "randomResult", { | |
277 get: () => randomResult, | |
278 set: value => randomResult = value | |
279 }); | |
280 | |
281 let sandboxedRequire = createSandbox({ | |
282 globals: { | |
283 Cc: { | |
284 "@mozilla.org/timer;1": { | |
285 createInstance: () => fakeTimer | |
286 } | |
287 }, | |
288 Ci: { | |
289 nsITimer: | |
290 { | |
291 TYPE_ONE_SHOT: 0, | |
292 TYPE_REPEATING_SLACK: 1, | |
293 TYPE_REPEATING_PRECISE: 2 | |
294 }, | |
295 nsIHttpChannel: () => null, | |
296 }, | |
297 Cr: Cr, | |
298 XMLHttpRequest: XMLHttpRequest, | |
299 Date: { | |
300 now: () => currentTime | |
301 }, | |
302 Math: { | |
303 random: () => randomResult, | |
304 min: Math.min, | |
305 max: Math.max, | |
306 round: Math.round | |
307 }, | |
308 URL: function(urlString) | |
309 { | |
310 return require("url").parse(urlString); | |
311 } | |
312 } | |
313 }); | |
314 | |
315 ( | 37 ( |
316 {Filter} = sandboxedRequire("../lib/filterClasses"), | 38 {Filter} = sandboxedRequire("../lib/filterClasses"), |
317 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | 39 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), |
318 {Prefs} = sandboxedRequire("./stub-modules/prefs"), | 40 {Prefs} = sandboxedRequire("./stub-modules/prefs"), |
319 {Subscription} = sandboxedRequire("../lib/subscriptionClasses"), | 41 {Subscription} = sandboxedRequire("../lib/subscriptionClasses"), |
320 {Synchronizer} = sandboxedRequire("../lib/synchronizer") | 42 {Synchronizer} = sandboxedRequire("../lib/synchronizer") |
321 ); | 43 ); |
322 | 44 |
323 callback(); | 45 callback(); |
324 }; | 46 }; |
325 | 47 |
326 function unexpectedError(error) | |
327 { | |
328 console.error(error); | |
329 this.ok(false, "Unexpected error: " + error); | |
330 } | |
331 | |
332 function resetSubscription(subscription) | 48 function resetSubscription(subscription) |
333 { | 49 { |
334 FilterStorage.updateSubscriptionFilters(subscription, []); | 50 FilterStorage.updateSubscriptionFilters(subscription, []); |
335 subscription.lastCheck = subscription.lastDownload = | 51 subscription.lastCheck = subscription.lastDownload = |
336 subscription.version = subscription.lastSuccess = | 52 subscription.version = subscription.lastSuccess = |
337 subscription.expires = subscription.softExpiration = 0; | 53 subscription.expires = subscription.softExpiration = 0; |
338 subscription.title = ""; | 54 subscription.title = ""; |
339 subscription.homepage = null; | 55 subscription.homepage = null; |
340 subscription.errors = 0; | 56 subscription.errors = 0; |
341 subscription.downloadStatus = null; | 57 subscription.downloadStatus = null; |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 return this.runScheduledTasks(24); | 555 return this.runScheduledTasks(24); |
840 }).then(() => | 556 }).then(() => |
841 { | 557 { |
842 test.equal(subscription.downloadStatus, "synchronize_connection_error", "dow
nloadStatus after download error"); | 558 test.equal(subscription.downloadStatus, "synchronize_connection_error", "dow
nloadStatus after download error"); |
843 test.equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + (26 + i
nitialDelay) * MILLIS_IN_HOUR, "lastDownload after download error"); | 559 test.equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + (26 + i
nitialDelay) * MILLIS_IN_HOUR, "lastDownload after download error"); |
844 test.equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + initialD
elay * MILLIS_IN_HOUR, "lastSuccess after download error"); | 560 test.equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + initialD
elay * MILLIS_IN_HOUR, "lastSuccess after download error"); |
845 test.equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + (27 + init
ialDelay) * MILLIS_IN_HOUR, "lastCheck after download error"); | 561 test.equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + (27 + init
ialDelay) * MILLIS_IN_HOUR, "lastCheck after download error"); |
846 test.equal(subscription.errors, 2, "errors after download error"); | 562 test.equal(subscription.errors, 2, "errors after download error"); |
847 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 563 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
848 }; | 564 }; |
OLD | NEW |