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 /* globals __dirname */ |
| 19 |
18 "use strict"; | 20 "use strict"; |
19 | 21 |
20 let {createSandbox, unexpectedError} = require("./_common"); | 22 const {createSandbox, unexpectedError} = require("./_common"); |
21 | 23 |
22 let Filter = null; | 24 let Filter = null; |
23 let FilterNotifier = null; | 25 let FilterNotifier = null; |
24 let FilterStorage = null; | 26 let FilterStorage = null; |
25 let IO = null; | 27 let IO = null; |
26 let Prefs = null; | 28 let Prefs = null; |
27 let Subscription = null; | 29 let Subscription = null; |
28 let ExternalSubscription = null; | 30 let ExternalSubscription = null; |
29 | 31 |
30 exports.setUp = function(callback) | 32 exports.setUp = function(callback) |
31 { | 33 { |
32 let sandboxedRequire = createSandbox(); | 34 let sandboxedRequire = createSandbox(); |
33 ( | 35 ( |
34 {Filter} = sandboxedRequire("../lib/filterClasses"), | 36 {Filter} = sandboxedRequire("../lib/filterClasses"), |
35 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"), | 37 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"), |
36 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | 38 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), |
37 {IO} = sandboxedRequire("./stub-modules/io"), | 39 {IO} = sandboxedRequire("./stub-modules/io"), |
38 {Prefs} = sandboxedRequire("./stub-modules/prefs"), | 40 {Prefs} = sandboxedRequire("./stub-modules/prefs"), |
39 {Subscription, ExternalSubscription} = sandboxedRequire("../lib/subscription
Classes") | 41 {Subscription, ExternalSubscription} = sandboxedRequire("../lib/subscription
Classes") |
40 ); | 42 ); |
41 | 43 |
42 FilterStorage.addSubscription(Subscription.fromURL("~fl~")); | 44 FilterStorage.addSubscription(Subscription.fromURL("~fl~")); |
43 callback(); | 45 callback(); |
44 } | 46 }; |
45 | 47 |
46 let testData = new Promise((resolve, reject) => | 48 let testData = new Promise((resolve, reject) => |
47 { | 49 { |
48 let fs = require("fs"); | 50 const fs = require("fs"); |
49 let path = require("path"); | 51 const path = require("path"); |
50 let datapath = path.resolve(__dirname, "data", "patterns.ini"); | 52 let datapath = path.resolve(__dirname, "data", "patterns.ini"); |
51 | 53 |
52 fs.readFile(datapath, "utf-8", (error, data) => | 54 fs.readFile(datapath, "utf-8", (error, data) => |
53 { | 55 { |
54 if (error) | 56 if (error) |
55 reject(error); | 57 reject(error); |
56 else | 58 else |
57 resolve(data); | 59 resolve(data); |
58 }); | 60 }); |
59 }); | 61 }); |
(...skipping 29 matching lines...) Expand all Loading... |
89 curSection = {header: line, data: []}; | 91 curSection = {header: line, data: []}; |
90 } | 92 } |
91 else if (curSection && /\S/.test(line)) | 93 else if (curSection && /\S/.test(line)) |
92 curSection.data.push(line); | 94 curSection.data.push(line); |
93 } | 95 } |
94 for (let section of sections) | 96 for (let section of sections) |
95 { | 97 { |
96 section.key = section.header + " " + section.data[0]; | 98 section.key = section.header + " " + section.data[0]; |
97 section.data.sort(); | 99 section.data.sort(); |
98 } | 100 } |
99 sections.sort(function(a, b) | 101 sections.sort((a, b) => |
100 { | 102 { |
101 if (a.key < b.key) | 103 if (a.key < b.key) |
102 return -1; | 104 return -1; |
103 else if (a.key > b.key) | 105 else if (a.key > b.key) |
104 return 1; | 106 return 1; |
105 else | 107 return 0; |
106 return 0; | |
107 }); | 108 }); |
108 return sections.map(function(section) { | 109 return sections.map( |
109 return [section.header].concat(section.data).join("\n"); | 110 section => [section.header].concat(section.data).join("\n") |
110 }).join("\n"); | 111 ).join("\n"); |
111 } | 112 } |
112 | 113 |
113 return testData.then(data => | 114 return testData.then(data => |
114 { | 115 { |
115 tempFile.contents = data; | 116 tempFile.contents = data; |
116 return loadFilters(tempFile); | 117 return loadFilters(tempFile); |
117 }).then(() => | 118 }).then(() => |
118 { | 119 { |
119 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); | 120 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); |
120 | 121 |
121 if (withExternal) | 122 if (withExternal) |
122 { | 123 { |
123 let subscription = new ExternalSubscription("~external~external subscripti
on ID", "External subscription"); | 124 { |
124 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; | 125 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); |
125 FilterStorage.addSubscription(subscription); | 126 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; |
| 127 FilterStorage.addSubscription(subscription); |
| 128 } |
126 | 129 |
127 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio
n => subscription instanceof ExternalSubscription); | 130 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio
n => subscription instanceof ExternalSubscription); |
128 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); | 131 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); |
129 | 132 |
130 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); | 133 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); |
131 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); | 134 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); |
132 } | 135 } |
133 | 136 |
134 return saveFilters(tempFile2); | 137 return saveFilters(tempFile2); |
135 }).then(() => testData).then(expected => | 138 }).then(() => testData).then(expected => |
(...skipping 15 matching lines...) Expand all Loading... |
151 exports.testLegacyGroups = {}; | 154 exports.testLegacyGroups = {}; |
152 | 155 |
153 for (let url of ["~wl~", "~fl~", "~eh~"]) | 156 for (let url of ["~wl~", "~fl~", "~eh~"]) |
154 { | 157 { |
155 exports.testLegacyGroups["read empty " + url] = function(test) | 158 exports.testLegacyGroups["read empty " + url] = function(test) |
156 { | 159 { |
157 let data = "[Subscription]\nurl=" + url; | 160 let data = "[Subscription]\nurl=" + url; |
158 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); | 161 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); |
159 tempFile.contents = data; | 162 tempFile.contents = data; |
160 | 163 |
161 loadFilters(tempFile, function() | 164 loadFilters(tempFile, () => |
162 { | 165 { |
163 test.equal(FilterStorage.subscriptions.length, 0, "Number of filter subscr
iptions"); | 166 test.equal(FilterStorage.subscriptions.length, 0, "Number of filter subscr
iptions"); |
164 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 167 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
165 }; | 168 }; |
166 | 169 |
167 exports.testLegacyGroups["read non-empty " + url] = function(test) | 170 exports.testLegacyGroups["read non-empty " + url] = function(test) |
168 { | 171 { |
169 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo"; | 172 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo"; |
170 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); | 173 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); |
171 tempFile.contents = data; | 174 tempFile.contents = data; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 { | 259 { |
257 test.ok(backupFile.exists(), "First backup created"); | 260 test.ok(backupFile.exists(), "First backup created"); |
258 | 261 |
259 backupFile.lastModifiedTime -= 10000; | 262 backupFile.lastModifiedTime -= 10000; |
260 oldModifiedTime = backupFile.lastModifiedTime; | 263 oldModifiedTime = backupFile.lastModifiedTime; |
261 return saveFilters(null); | 264 return saveFilters(null); |
262 }).then(() => | 265 }).then(() => |
263 { | 266 { |
264 test.equal(backupFile.lastModifiedTime, oldModifiedTime, "Backup not overwri
tten if it is only 10 seconds old"); | 267 test.equal(backupFile.lastModifiedTime, oldModifiedTime, "Backup not overwri
tten if it is only 10 seconds old"); |
265 | 268 |
266 backupFile.lastModifiedTime -= 40*60*60*1000; | 269 backupFile.lastModifiedTime -= 40 * 60 * 60 * 1000; |
267 oldModifiedTime = backupFile.lastModifiedTime; | 270 oldModifiedTime = backupFile.lastModifiedTime; |
268 return saveFilters(null); | 271 return saveFilters(null); |
269 }).then(() => | 272 }).then(() => |
270 { | 273 { |
271 test.notEqual(backupFile.lastModifiedTime, oldModifiedTime, "Backup overwrit
ten if it is 40 hours old"); | 274 test.notEqual(backupFile.lastModifiedTime, oldModifiedTime, "Backup overwrit
ten if it is 40 hours old"); |
272 | 275 |
273 test.ok(backupFile2.exists(), "Second backup created when first backup is ov
erwritten"); | 276 test.ok(backupFile2.exists(), "Second backup created when first backup is ov
erwritten"); |
274 | 277 |
275 backupFile.lastModifiedTime -= 20000; | 278 backupFile.lastModifiedTime -= 20000; |
276 oldModifiedTime = backupFile2.lastModifiedTime; | 279 oldModifiedTime = backupFile2.lastModifiedTime; |
277 return saveFilters(null); | 280 return saveFilters(null); |
278 }).then(() => | 281 }).then(() => |
279 { | 282 { |
280 test.equal(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup not
overwritten if first one is only 20 seconds old"); | 283 test.equal(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup not
overwritten if first one is only 20 seconds old"); |
281 | 284 |
282 backupFile.lastModifiedTime -= 25*60*60*1000; | 285 backupFile.lastModifiedTime -= 25 * 60 * 60 * 1000; |
283 oldModifiedTime = backupFile2.lastModifiedTime; | 286 oldModifiedTime = backupFile2.lastModifiedTime; |
284 return saveFilters(null); | 287 return saveFilters(null); |
285 }).then(() => | 288 }).then(() => |
286 { | 289 { |
287 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup
overwritten if first one is 25 hours old"); | 290 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup
overwritten if first one is 25 hours old"); |
288 | 291 |
289 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup
s = 2"); | 292 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup
s = 2"); |
290 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 293 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
291 }; | 294 }; |
OLD | NEW |