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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 const {createSandbox, unexpectedError} = require("./_common"); | 20 const {createSandbox, unexpectedError} = require("./_common"); |
21 | 21 |
22 let Filter = null; | 22 let Filter = null; |
23 let FilterStorage = null; | 23 let filterStorage = null; |
24 let IO = null; | 24 let IO = null; |
25 let Prefs = null; | 25 let Prefs = null; |
26 let ExternalSubscription = null; | 26 let ExternalSubscription = null; |
27 let SpecialSubscription = null; | 27 let SpecialSubscription = null; |
28 | 28 |
29 exports.setUp = function(callback) | 29 exports.setUp = function(callback) |
30 { | 30 { |
31 let sandboxedRequire = createSandbox(); | 31 let sandboxedRequire = createSandbox(); |
32 ( | 32 ( |
33 {Filter} = sandboxedRequire("../lib/filterClasses"), | 33 {Filter} = sandboxedRequire("../lib/filterClasses"), |
34 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | 34 {filterStorage} = sandboxedRequire("../lib/filterStorage"), |
35 {IO} = sandboxedRequire("./stub-modules/io"), | 35 {IO} = sandboxedRequire("./stub-modules/io"), |
36 {Prefs} = sandboxedRequire("./stub-modules/prefs"), | 36 {Prefs} = sandboxedRequire("./stub-modules/prefs"), |
37 {ExternalSubscription, SpecialSubscription} = sandboxedRequire("../lib/subsc
riptionClasses") | 37 {ExternalSubscription, SpecialSubscription} = sandboxedRequire("../lib/subsc
riptionClasses") |
38 ); | 38 ); |
39 | 39 |
40 FilterStorage.addFilter(Filter.fromText("foobar")); | 40 filterStorage.addFilter(Filter.fromText("foobar")); |
41 callback(); | 41 callback(); |
42 }; | 42 }; |
43 | 43 |
44 let testData = new Promise((resolve, reject) => | 44 let testData = new Promise((resolve, reject) => |
45 { | 45 { |
46 const fs = require("fs"); | 46 const fs = require("fs"); |
47 const path = require("path"); | 47 const path = require("path"); |
48 let datapath = path.resolve(__dirname, "data", "patterns.ini"); | 48 let datapath = path.resolve(__dirname, "data", "patterns.ini"); |
49 | 49 |
50 fs.readFile(datapath, "utf-8", (error, data) => | 50 fs.readFile(datapath, "utf-8", (error, data) => |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 return -1; | 86 return -1; |
87 else if (a.key > b.key) | 87 else if (a.key > b.key) |
88 return 1; | 88 return 1; |
89 return 0; | 89 return 0; |
90 }); | 90 }); |
91 return sections; | 91 return sections; |
92 } | 92 } |
93 | 93 |
94 function testReadWrite(test, withExternal, withEmptySpecial) | 94 function testReadWrite(test, withExternal, withEmptySpecial) |
95 { | 95 { |
96 test.ok(!FilterStorage.initialized, "Uninitialized before the first load"); | 96 test.ok(!filterStorage.initialized, "Uninitialized before the first load"); |
97 | 97 |
98 return testData.then(data => | 98 return testData.then(data => |
99 { | 99 { |
100 IO._setFileContents(FilterStorage.sourceFile, data); | 100 IO._setFileContents(filterStorage.sourceFile, data); |
101 return FilterStorage.loadFromDisk(); | 101 return filterStorage.loadFromDisk(); |
102 }).then(() => | 102 }).then(() => |
103 { | 103 { |
104 test.ok(FilterStorage.initialized, "Initialize after the first load"); | 104 test.ok(filterStorage.initialized, "Initialize after the first load"); |
105 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); | 105 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); |
106 | 106 |
107 if (withExternal) | 107 if (withExternal) |
108 { | 108 { |
109 { | 109 { |
110 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); | 110 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); |
111 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; | 111 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; |
112 FilterStorage.addSubscription(subscription); | 112 filterStorage.addSubscription(subscription); |
113 } | 113 } |
114 | 114 |
115 let externalSubscriptions = [...FilterStorage.subscriptions()].filter(subs
cription => subscription instanceof ExternalSubscription); | 115 let externalSubscriptions = [...filterStorage.subscriptions()].filter(subs
cription => subscription instanceof ExternalSubscription); |
116 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); | 116 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); |
117 | 117 |
118 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); | 118 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); |
119 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); | 119 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); |
120 } | 120 } |
121 | 121 |
122 if (withEmptySpecial) | 122 if (withEmptySpecial) |
123 { | 123 { |
124 let specialSubscription = | 124 let specialSubscription = |
125 SpecialSubscription.createForFilter(Filter.fromText("!foo")); | 125 SpecialSubscription.createForFilter(Filter.fromText("!foo")); |
126 FilterStorage.addSubscription(specialSubscription); | 126 filterStorage.addSubscription(specialSubscription); |
127 | 127 |
128 FilterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription); | 128 filterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription); |
129 | 129 |
130 test.equal(specialSubscription.filters.length, 0, | 130 test.equal(specialSubscription.filters.length, 0, |
131 "No filters in special subscription"); | 131 "No filters in special subscription"); |
132 test.ok(new Set(FilterStorage.subscriptions()).has(specialSubscription), | 132 test.ok(new Set(filterStorage.subscriptions()).has(specialSubscription), |
133 "Empty special subscription still in storage"); | 133 "Empty special subscription still in storage"); |
134 } | 134 } |
135 | 135 |
136 return FilterStorage.saveToDisk(); | 136 return filterStorage.saveToDisk(); |
137 }).then(() => testData).then(expected => | 137 }).then(() => testData).then(expected => |
138 { | 138 { |
139 test.deepEqual(canonize(IO._getFileContents(FilterStorage.sourceFile)), | 139 test.deepEqual(canonize(IO._getFileContents(filterStorage.sourceFile)), |
140 canonize(expected), "Read/write result"); | 140 canonize(expected), "Read/write result"); |
141 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 141 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
142 } | 142 } |
143 | 143 |
144 exports.testReadAndSaveToFile = function(test) | 144 exports.testReadAndSaveToFile = function(test) |
145 { | 145 { |
146 testReadWrite(test, false); | 146 testReadWrite(test, false); |
147 }; | 147 }; |
148 | 148 |
149 exports.testReadAndSaveToFileWithExternalSubscription = function(test) | 149 exports.testReadAndSaveToFileWithExternalSubscription = function(test) |
150 { | 150 { |
151 testReadWrite(test, true); | 151 testReadWrite(test, true); |
152 }; | 152 }; |
153 | 153 |
154 exports.testReadAndSaveToFileWithEmptySpecial = function(test) | 154 exports.testReadAndSaveToFileWithEmptySpecial = function(test) |
155 { | 155 { |
156 testReadWrite(test, false, true); | 156 testReadWrite(test, false, true); |
157 }; | 157 }; |
158 | 158 |
159 exports.testImportExport = function(test) | 159 exports.testImportExport = function(test) |
160 { | 160 { |
161 testData.then(lines => | 161 testData.then(lines => |
162 { | 162 { |
163 if (lines.length && lines[lines.length - 1] == "") | 163 if (lines.length && lines[lines.length - 1] == "") |
164 lines.pop(); | 164 lines.pop(); |
165 | 165 |
166 let importer = FilterStorage.importData(); | 166 let importer = filterStorage.importData(); |
167 for (let line of lines) | 167 for (let line of lines) |
168 importer(line); | 168 importer(line); |
169 importer(null); | 169 importer(null); |
170 | 170 |
171 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); | 171 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); |
172 | 172 |
173 let exported = Array.from(FilterStorage.exportData()); | 173 let exported = Array.from(filterStorage.exportData()); |
174 test.deepEqual(canonize(exported), canonize(lines), "Import/export result"); | 174 test.deepEqual(canonize(exported), canonize(lines), "Import/export result"); |
175 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 175 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
176 }; | 176 }; |
177 | 177 |
178 exports.testSavingWithoutBackups = function(test) | 178 exports.testSavingWithoutBackups = function(test) |
179 { | 179 { |
180 Prefs.patternsbackups = 0; | 180 Prefs.patternsbackups = 0; |
181 Prefs.patternsbackupinterval = 24; | 181 Prefs.patternsbackupinterval = 24; |
182 | 182 |
183 FilterStorage.saveToDisk().then(() => | 183 filterStorage.saveToDisk().then(() => |
184 { | 184 { |
185 return FilterStorage.saveToDisk(); | 185 return filterStorage.saveToDisk(); |
186 }).then(() => | 186 }).then(() => |
187 { | 187 { |
188 test.ok(!IO._getFileContents(FilterStorage.getBackupName(1)), | 188 test.ok(!IO._getFileContents(filterStorage.getBackupName(1)), |
189 "Backup shouldn't be created"); | 189 "Backup shouldn't be created"); |
190 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 190 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
191 }; | 191 }; |
192 | 192 |
193 exports.testSavingWithBackups = function(test) | 193 exports.testSavingWithBackups = function(test) |
194 { | 194 { |
195 Prefs.patternsbackups = 2; | 195 Prefs.patternsbackups = 2; |
196 Prefs.patternsbackupinterval = 24; | 196 Prefs.patternsbackupinterval = 24; |
197 | 197 |
198 let backupFile = FilterStorage.getBackupName(1); | 198 let backupFile = filterStorage.getBackupName(1); |
199 let backupFile2 = FilterStorage.getBackupName(2); | 199 let backupFile2 = filterStorage.getBackupName(2); |
200 let backupFile3 = FilterStorage.getBackupName(3); | 200 let backupFile3 = filterStorage.getBackupName(3); |
201 | 201 |
202 let oldModifiedTime; | 202 let oldModifiedTime; |
203 | 203 |
204 FilterStorage.saveToDisk().then(() => | 204 filterStorage.saveToDisk().then(() => |
205 { | 205 { |
206 // Save again immediately | 206 // Save again immediately |
207 return FilterStorage.saveToDisk(); | 207 return filterStorage.saveToDisk(); |
208 }).then(() => | 208 }).then(() => |
209 { | 209 { |
210 test.ok(IO._getFileContents(backupFile), "First backup created"); | 210 test.ok(IO._getFileContents(backupFile), "First backup created"); |
211 | 211 |
212 oldModifiedTime = IO._getModifiedTime(backupFile) - 10000; | 212 oldModifiedTime = IO._getModifiedTime(backupFile) - 10000; |
213 IO._setModifiedTime(backupFile, oldModifiedTime); | 213 IO._setModifiedTime(backupFile, oldModifiedTime); |
214 return FilterStorage.saveToDisk(); | 214 return filterStorage.saveToDisk(); |
215 }).then(() => | 215 }).then(() => |
216 { | 216 { |
217 test.equal(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup not ove
rwritten if it is only 10 seconds old"); | 217 test.equal(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup not ove
rwritten if it is only 10 seconds old"); |
218 | 218 |
219 oldModifiedTime -= 40 * 60 * 60 * 1000; | 219 oldModifiedTime -= 40 * 60 * 60 * 1000; |
220 IO._setModifiedTime(backupFile, oldModifiedTime); | 220 IO._setModifiedTime(backupFile, oldModifiedTime); |
221 return FilterStorage.saveToDisk(); | 221 return filterStorage.saveToDisk(); |
222 }).then(() => | 222 }).then(() => |
223 { | 223 { |
224 test.notEqual(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup over
written if it is 40 hours old"); | 224 test.notEqual(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup over
written if it is 40 hours old"); |
225 | 225 |
226 test.ok(IO._getFileContents(backupFile2), "Second backup created when first
backup is overwritten"); | 226 test.ok(IO._getFileContents(backupFile2), "Second backup created when first
backup is overwritten"); |
227 | 227 |
228 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 20000); | 228 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 20000); |
229 oldModifiedTime = IO._getModifiedTime(backupFile2); | 229 oldModifiedTime = IO._getModifiedTime(backupFile2); |
230 return FilterStorage.saveToDisk(); | 230 return filterStorage.saveToDisk(); |
231 }).then(() => | 231 }).then(() => |
232 { | 232 { |
233 test.equal(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup
not overwritten if first one is only 20 seconds old"); | 233 test.equal(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup
not overwritten if first one is only 20 seconds old"); |
234 | 234 |
235 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 25 * 60 *
60 * 1000); | 235 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 25 * 60 *
60 * 1000); |
236 oldModifiedTime = IO._getModifiedTime(backupFile2); | 236 oldModifiedTime = IO._getModifiedTime(backupFile2); |
237 return FilterStorage.saveToDisk(); | 237 return filterStorage.saveToDisk(); |
238 }).then(() => | 238 }).then(() => |
239 { | 239 { |
240 test.notEqual(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second bac
kup overwritten if first one is 25 hours old"); | 240 test.notEqual(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second bac
kup overwritten if first one is 25 hours old"); |
241 | 241 |
242 test.ok(!IO._getFileContents(backupFile3), "Third backup not created with pa
tternsbackups = 2"); | 242 test.ok(!IO._getFileContents(backupFile3), "Third backup not created with pa
tternsbackups = 2"); |
243 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 243 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
244 }; | 244 }; |
245 | 245 |
246 exports.testRestoringBackup = function(test) | 246 exports.testRestoringBackup = function(test) |
247 { | 247 { |
248 Prefs.patternsbackups = 2; | 248 Prefs.patternsbackups = 2; |
249 Prefs.patternsbackupinterval = 24; | 249 Prefs.patternsbackupinterval = 24; |
250 | 250 |
251 FilterStorage.saveToDisk().then(() => | 251 filterStorage.saveToDisk().then(() => |
252 { | 252 { |
253 test.equal([...FilterStorage.subscriptions()][0].filters.length, 1, "Initial
filter count"); | 253 test.equal([...filterStorage.subscriptions()][0].filters.length, 1, "Initial
filter count"); |
254 FilterStorage.addFilter(Filter.fromText("barfoo")); | 254 filterStorage.addFilter(Filter.fromText("barfoo")); |
255 test.equal([...FilterStorage.subscriptions()][0].filters.length, 2, "Filter
count after adding a filter"); | 255 test.equal([...filterStorage.subscriptions()][0].filters.length, 2, "Filter
count after adding a filter"); |
256 return FilterStorage.saveToDisk(); | 256 return filterStorage.saveToDisk(); |
257 }).then(() => | 257 }).then(() => |
258 { | 258 { |
259 return FilterStorage.loadFromDisk(); | 259 return filterStorage.loadFromDisk(); |
260 }).then(() => | 260 }).then(() => |
261 { | 261 { |
262 test.equal([...FilterStorage.subscriptions()][0].filters.length, 2, "Filter
count after adding filter and reloading"); | 262 test.equal([...filterStorage.subscriptions()][0].filters.length, 2, "Filter
count after adding filter and reloading"); |
263 return FilterStorage.restoreBackup(1); | 263 return filterStorage.restoreBackup(1); |
264 }).then(() => | 264 }).then(() => |
265 { | 265 { |
266 test.equal([...FilterStorage.subscriptions()][0].filters.length, 1, "Filter
count after restoring backup"); | 266 test.equal([...filterStorage.subscriptions()][0].filters.length, 1, "Filter
count after restoring backup"); |
267 return FilterStorage.loadFromDisk(); | 267 return filterStorage.loadFromDisk(); |
268 }).then(() => | 268 }).then(() => |
269 { | 269 { |
270 test.equal([...FilterStorage.subscriptions()][0].filters.length, 1, "Filter
count after reloading"); | 270 test.equal([...filterStorage.subscriptions()][0].filters.length, 1, "Filter
count after reloading"); |
271 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 271 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
272 }; | 272 }; |
OLD | NEW |