| 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 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 { | 84 { |
| 85 if (a.key < b.key) | 85 if (a.key < b.key) |
| 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 async 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 try |
| 99 { | 99 { |
| 100 let data = await testData; |
| 101 |
| 100 IO._setFileContents(filterStorage.sourceFile, data); | 102 IO._setFileContents(filterStorage.sourceFile, data); |
| 101 return filterStorage.loadFromDisk(); | 103 await filterStorage.loadFromDisk(); |
| 102 }).then(() => | 104 |
| 103 { | |
| 104 test.ok(filterStorage.initialized, "Initialize after the first load"); | 105 test.ok(filterStorage.initialized, "Initialize after the first load"); |
| 105 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); | 106 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); |
| 106 | 107 |
| 107 if (withExternal) | 108 if (withExternal) |
| 108 { | 109 { |
| 109 { | 110 { |
| 110 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); | 111 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); |
| 111 subscription.addFilter(Filter.fromText("foo")); | 112 subscription.addFilter(Filter.fromText("foo")); |
| 112 subscription.addFilter(Filter.fromText("bar")); | 113 subscription.addFilter(Filter.fromText("bar")); |
| 113 filterStorage.addSubscription(subscription); | 114 filterStorage.addSubscription(subscription); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 127 filterStorage.addSubscription(specialSubscription); | 128 filterStorage.addSubscription(specialSubscription); |
| 128 | 129 |
| 129 filterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription); | 130 filterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription); |
| 130 | 131 |
| 131 test.equal(specialSubscription.filterCount, 0, | 132 test.equal(specialSubscription.filterCount, 0, |
| 132 "No filters in special subscription"); | 133 "No filters in special subscription"); |
| 133 test.ok(new Set(filterStorage.subscriptions()).has(specialSubscription), | 134 test.ok(new Set(filterStorage.subscriptions()).has(specialSubscription), |
| 134 "Empty special subscription still in storage"); | 135 "Empty special subscription still in storage"); |
| 135 } | 136 } |
| 136 | 137 |
| 137 return filterStorage.saveToDisk(); | 138 await filterStorage.saveToDisk(); |
| 138 }).then(() => testData).then(expected => | 139 |
| 140 let expected = await testData; |
| 141 |
| 142 test.deepEqual(canonize(IO._getFileContents(filterStorage.sourceFile)), |
| 143 canonize(expected), "Read/write result"); |
| 144 } |
| 145 catch (error) |
| 139 { | 146 { |
| 140 test.deepEqual(canonize(IO._getFileContents(filterStorage.sourceFile)), | 147 unexpectedError.call(test, error); |
| 141 canonize(expected), "Read/write result"); | 148 } |
| 142 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 149 |
| 150 test.done(); |
| 143 } | 151 } |
| 144 | 152 |
| 145 exports.testReadAndSaveToFile = function(test) | 153 exports.testReadAndSaveToFile = function(test) |
| 146 { | 154 { |
| 147 testReadWrite(test, false); | 155 testReadWrite(test, false); |
| 148 }; | 156 }; |
| 149 | 157 |
| 150 exports.testReadAndSaveToFileWithExternalSubscription = function(test) | 158 exports.testReadAndSaveToFileWithExternalSubscription = function(test) |
| 151 { | 159 { |
| 152 testReadWrite(test, true); | 160 testReadWrite(test, true); |
| 153 }; | 161 }; |
| 154 | 162 |
| 155 exports.testReadAndSaveToFileWithEmptySpecial = function(test) | 163 exports.testReadAndSaveToFileWithEmptySpecial = function(test) |
| 156 { | 164 { |
| 157 testReadWrite(test, false, true); | 165 testReadWrite(test, false, true); |
| 158 }; | 166 }; |
| 159 | 167 |
| 160 exports.testImportExport = function(test) | 168 exports.testImportExport = async function(test) |
| 161 { | 169 { |
| 162 testData.then(lines => | 170 try |
| 163 { | 171 { |
| 172 let lines = await testData; |
| 173 |
| 164 if (lines.length && lines[lines.length - 1] == "") | 174 if (lines.length && lines[lines.length - 1] == "") |
| 165 lines.pop(); | 175 lines.pop(); |
| 166 | 176 |
| 167 let importer = filterStorage.importData(); | 177 let importer = filterStorage.importData(); |
| 168 for (let line of lines) | 178 for (let line of lines) |
| 169 importer(line); | 179 importer(line); |
| 170 importer(null); | 180 importer(null); |
| 171 | 181 |
| 172 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); | 182 test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion
, "File format version"); |
| 173 | 183 |
| 174 let exported = Array.from(filterStorage.exportData()); | 184 let exported = Array.from(filterStorage.exportData()); |
| 175 test.deepEqual(canonize(exported), canonize(lines), "Import/export result"); | 185 test.deepEqual(canonize(exported), canonize(lines), "Import/export result"); |
| 176 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 186 } |
| 187 catch (error) |
| 188 { |
| 189 unexpectedError.call(test, error); |
| 190 } |
| 191 |
| 192 test.done(); |
| 177 }; | 193 }; |
| 178 | 194 |
| 179 exports.testSavingWithoutBackups = function(test) | 195 exports.testSavingWithoutBackups = async function(test) |
| 180 { | 196 { |
| 181 Prefs.patternsbackups = 0; | 197 Prefs.patternsbackups = 0; |
| 182 Prefs.patternsbackupinterval = 24; | 198 Prefs.patternsbackupinterval = 24; |
| 183 | 199 |
| 184 filterStorage.saveToDisk().then(() => | 200 try |
| 185 { | 201 { |
| 186 return filterStorage.saveToDisk(); | 202 await filterStorage.saveToDisk(); |
| 187 }).then(() => | 203 await filterStorage.saveToDisk(); |
| 188 { | 204 |
| 189 test.ok(!IO._getFileContents(filterStorage.getBackupName(1)), | 205 test.ok(!IO._getFileContents(filterStorage.getBackupName(1)), |
| 190 "Backup shouldn't be created"); | 206 "Backup shouldn't be created"); |
| 191 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 207 } |
| 208 catch (error) |
| 209 { |
| 210 unexpectedError.call(test, error); |
| 211 } |
| 212 |
| 213 test.done(); |
| 192 }; | 214 }; |
| 193 | 215 |
| 194 exports.testSavingWithBackups = function(test) | 216 exports.testSavingWithBackups = async function(test) |
| 195 { | 217 { |
| 196 Prefs.patternsbackups = 2; | 218 Prefs.patternsbackups = 2; |
| 197 Prefs.patternsbackupinterval = 24; | 219 Prefs.patternsbackupinterval = 24; |
| 198 | 220 |
| 199 let backupFile = filterStorage.getBackupName(1); | 221 let backupFile = filterStorage.getBackupName(1); |
| 200 let backupFile2 = filterStorage.getBackupName(2); | 222 let backupFile2 = filterStorage.getBackupName(2); |
| 201 let backupFile3 = filterStorage.getBackupName(3); | 223 let backupFile3 = filterStorage.getBackupName(3); |
| 202 | 224 |
| 203 let oldModifiedTime; | 225 let oldModifiedTime; |
| 204 | 226 |
| 205 filterStorage.saveToDisk().then(() => | 227 try |
| 206 { | 228 { |
| 229 await filterStorage.saveToDisk(); |
| 230 |
| 207 // Save again immediately | 231 // Save again immediately |
| 208 return filterStorage.saveToDisk(); | 232 await filterStorage.saveToDisk(); |
| 209 }).then(() => | 233 |
| 210 { | |
| 211 test.ok(IO._getFileContents(backupFile), "First backup created"); | 234 test.ok(IO._getFileContents(backupFile), "First backup created"); |
| 212 | 235 |
| 213 oldModifiedTime = IO._getModifiedTime(backupFile) - 10000; | 236 oldModifiedTime = IO._getModifiedTime(backupFile) - 10000; |
| 214 IO._setModifiedTime(backupFile, oldModifiedTime); | 237 IO._setModifiedTime(backupFile, oldModifiedTime); |
| 215 return filterStorage.saveToDisk(); | 238 await filterStorage.saveToDisk(); |
| 216 }).then(() => | 239 |
| 217 { | |
| 218 test.equal(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup not ove
rwritten if it is only 10 seconds old"); | 240 test.equal(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup not ove
rwritten if it is only 10 seconds old"); |
| 219 | 241 |
| 220 oldModifiedTime -= 40 * 60 * 60 * 1000; | 242 oldModifiedTime -= 40 * 60 * 60 * 1000; |
| 221 IO._setModifiedTime(backupFile, oldModifiedTime); | 243 IO._setModifiedTime(backupFile, oldModifiedTime); |
| 222 return filterStorage.saveToDisk(); | 244 await filterStorage.saveToDisk(); |
| 223 }).then(() => | 245 |
| 224 { | |
| 225 test.notEqual(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup over
written if it is 40 hours old"); | 246 test.notEqual(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup over
written if it is 40 hours old"); |
| 226 | 247 |
| 227 test.ok(IO._getFileContents(backupFile2), "Second backup created when first
backup is overwritten"); | 248 test.ok(IO._getFileContents(backupFile2), "Second backup created when first
backup is overwritten"); |
| 228 | 249 |
| 229 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 20000); | 250 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 20000); |
| 230 oldModifiedTime = IO._getModifiedTime(backupFile2); | 251 oldModifiedTime = IO._getModifiedTime(backupFile2); |
| 231 return filterStorage.saveToDisk(); | 252 await filterStorage.saveToDisk(); |
| 232 }).then(() => | 253 |
| 233 { | |
| 234 test.equal(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup
not overwritten if first one is only 20 seconds old"); | 254 test.equal(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup
not overwritten if first one is only 20 seconds old"); |
| 235 | 255 |
| 236 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 25 * 60 *
60 * 1000); | 256 IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 25 * 60 *
60 * 1000); |
| 237 oldModifiedTime = IO._getModifiedTime(backupFile2); | 257 oldModifiedTime = IO._getModifiedTime(backupFile2); |
| 238 return filterStorage.saveToDisk(); | 258 await filterStorage.saveToDisk(); |
| 239 }).then(() => | 259 |
| 240 { | |
| 241 test.notEqual(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second bac
kup overwritten if first one is 25 hours old"); | 260 test.notEqual(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second bac
kup overwritten if first one is 25 hours old"); |
| 242 | 261 |
| 243 test.ok(!IO._getFileContents(backupFile3), "Third backup not created with pa
tternsbackups = 2"); | 262 test.ok(!IO._getFileContents(backupFile3), "Third backup not created with pa
tternsbackups = 2"); |
| 244 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 263 } |
| 264 catch (error) |
| 265 { |
| 266 unexpectedError.call(test, error); |
| 267 } |
| 268 |
| 269 test.done(); |
| 245 }; | 270 }; |
| 246 | 271 |
| 247 exports.testRestoringBackup = function(test) | 272 exports.testRestoringBackup = async function(test) |
| 248 { | 273 { |
| 249 Prefs.patternsbackups = 2; | 274 Prefs.patternsbackups = 2; |
| 250 Prefs.patternsbackupinterval = 24; | 275 Prefs.patternsbackupinterval = 24; |
| 251 | 276 |
| 252 filterStorage.saveToDisk().then(() => | 277 try |
| 253 { | 278 { |
| 279 await filterStorage.saveToDisk(); |
| 280 |
| 254 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Initial fi
lter count"); | 281 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Initial fi
lter count"); |
| 255 filterStorage.addFilter(Filter.fromText("barfoo")); | 282 filterStorage.addFilter(Filter.fromText("barfoo")); |
| 256 test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter cou
nt after adding a filter"); | 283 test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter cou
nt after adding a filter"); |
| 257 return filterStorage.saveToDisk(); | 284 await filterStorage.saveToDisk(); |
| 258 }).then(() => | 285 |
| 286 await filterStorage.loadFromDisk(); |
| 287 |
| 288 test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter cou
nt after adding filter and reloading"); |
| 289 await filterStorage.restoreBackup(1); |
| 290 |
| 291 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter cou
nt after restoring backup"); |
| 292 await filterStorage.loadFromDisk(); |
| 293 |
| 294 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter cou
nt after reloading"); |
| 295 } |
| 296 catch (error) |
| 259 { | 297 { |
| 260 return filterStorage.loadFromDisk(); | 298 unexpectedError.call(test, error); |
| 261 }).then(() => | 299 } |
| 262 { | 300 |
| 263 test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter cou
nt after adding filter and reloading"); | 301 test.done(); |
| 264 return filterStorage.restoreBackup(1); | |
| 265 }).then(() => | |
| 266 { | |
| 267 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter cou
nt after restoring backup"); | |
| 268 return filterStorage.loadFromDisk(); | |
| 269 }).then(() => | |
| 270 { | |
| 271 test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter cou
nt after reloading"); | |
| 272 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 273 }; | 302 }; |
| OLD | NEW |