Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /** |
michael@0 | 5 | * Tests the DownloadImport object. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | "use strict"; |
michael@0 | 9 | |
michael@0 | 10 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 11 | //// Globals |
michael@0 | 12 | |
michael@0 | 13 | XPCOMUtils.defineLazyModuleGetter(this, "Sqlite", |
michael@0 | 14 | "resource://gre/modules/Sqlite.jsm"); |
michael@0 | 15 | XPCOMUtils.defineLazyModuleGetter(this, "DownloadImport", |
michael@0 | 16 | "resource://gre/modules/DownloadImport.jsm"); |
michael@0 | 17 | |
michael@0 | 18 | // Importable states |
michael@0 | 19 | const DOWNLOAD_NOTSTARTED = -1; |
michael@0 | 20 | const DOWNLOAD_DOWNLOADING = 0; |
michael@0 | 21 | const DOWNLOAD_PAUSED = 4; |
michael@0 | 22 | const DOWNLOAD_QUEUED = 5; |
michael@0 | 23 | |
michael@0 | 24 | // Non importable states |
michael@0 | 25 | const DOWNLOAD_FAILED = 2; |
michael@0 | 26 | const DOWNLOAD_CANCELED = 3; |
michael@0 | 27 | const DOWNLOAD_BLOCKED_PARENTAL = 6; |
michael@0 | 28 | const DOWNLOAD_SCANNING = 7; |
michael@0 | 29 | const DOWNLOAD_DIRTY = 8; |
michael@0 | 30 | const DOWNLOAD_BLOCKED_POLICY = 9; |
michael@0 | 31 | |
michael@0 | 32 | // The TEST_DATA_TAINTED const is a version of TEST_DATA_SHORT in which the |
michael@0 | 33 | // beginning of the data was changed (with the TEST_DATA_REPLACEMENT value). |
michael@0 | 34 | // We use this to test that the entityID is properly imported and the download |
michael@0 | 35 | // can be resumed from where it was paused. |
michael@0 | 36 | // For simplification purposes, the test requires that TEST_DATA_SHORT and |
michael@0 | 37 | // TEST_DATA_TAINTED have the same length. |
michael@0 | 38 | const TEST_DATA_REPLACEMENT = "-changed- "; |
michael@0 | 39 | const TEST_DATA_TAINTED = TEST_DATA_REPLACEMENT + |
michael@0 | 40 | TEST_DATA_SHORT.substr(TEST_DATA_REPLACEMENT.length); |
michael@0 | 41 | const TEST_DATA_LENGTH = TEST_DATA_SHORT.length; |
michael@0 | 42 | |
michael@0 | 43 | // The length of the partial file that we'll write to disk as an existing |
michael@0 | 44 | // ongoing download. |
michael@0 | 45 | const TEST_DATA_PARTIAL_LENGTH = TEST_DATA_REPLACEMENT.length; |
michael@0 | 46 | |
michael@0 | 47 | // The value of the "maxBytes" column stored in the DB about the downloads. |
michael@0 | 48 | // It's intentionally different than TEST_DATA_LENGTH to test that each value |
michael@0 | 49 | // is seen when expected. |
michael@0 | 50 | const MAXBYTES_IN_DB = TEST_DATA_LENGTH - 10; |
michael@0 | 51 | |
michael@0 | 52 | let gDownloadsRowToImport; |
michael@0 | 53 | let gDownloadsRowNonImportable; |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * Creates a database with an empty moz_downloads table and leaves an |
michael@0 | 57 | * open connection to it. |
michael@0 | 58 | * |
michael@0 | 59 | * @param aPath |
michael@0 | 60 | * String containing the path of the database file to be created. |
michael@0 | 61 | * @param aSchemaVersion |
michael@0 | 62 | * Number with the version of the database schema to set. |
michael@0 | 63 | * |
michael@0 | 64 | * @return {Promise} |
michael@0 | 65 | * @resolves The open connection to the database. |
michael@0 | 66 | * @rejects If an error occurred during the database creation. |
michael@0 | 67 | */ |
michael@0 | 68 | function promiseEmptyDatabaseConnection({aPath, aSchemaVersion}) { |
michael@0 | 69 | return Task.spawn(function () { |
michael@0 | 70 | let connection = yield Sqlite.openConnection({ path: aPath }); |
michael@0 | 71 | |
michael@0 | 72 | yield connection.execute("CREATE TABLE moz_downloads (" |
michael@0 | 73 | + "id INTEGER PRIMARY KEY," |
michael@0 | 74 | + "name TEXT," |
michael@0 | 75 | + "source TEXT," |
michael@0 | 76 | + "target TEXT," |
michael@0 | 77 | + "tempPath TEXT," |
michael@0 | 78 | + "startTime INTEGER," |
michael@0 | 79 | + "endTime INTEGER," |
michael@0 | 80 | + "state INTEGER," |
michael@0 | 81 | + "referrer TEXT," |
michael@0 | 82 | + "entityID TEXT," |
michael@0 | 83 | + "currBytes INTEGER NOT NULL DEFAULT 0," |
michael@0 | 84 | + "maxBytes INTEGER NOT NULL DEFAULT -1," |
michael@0 | 85 | + "mimeType TEXT," |
michael@0 | 86 | + "preferredApplication TEXT," |
michael@0 | 87 | + "preferredAction INTEGER NOT NULL DEFAULT 0," |
michael@0 | 88 | + "autoResume INTEGER NOT NULL DEFAULT 0," |
michael@0 | 89 | + "guid TEXT)"); |
michael@0 | 90 | |
michael@0 | 91 | yield connection.setSchemaVersion(aSchemaVersion); |
michael@0 | 92 | |
michael@0 | 93 | throw new Task.Result(connection); |
michael@0 | 94 | }); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Inserts a new entry in the database with the given columns' values. |
michael@0 | 99 | * |
michael@0 | 100 | * @param aConnection |
michael@0 | 101 | * The database connection. |
michael@0 | 102 | * @param aDownloadRow |
michael@0 | 103 | * An object representing the values for each column of the row |
michael@0 | 104 | * being inserted. |
michael@0 | 105 | * |
michael@0 | 106 | * @return {Promise} |
michael@0 | 107 | * @resolves When the operation completes. |
michael@0 | 108 | * @rejects If there's an error inserting the row. |
michael@0 | 109 | */ |
michael@0 | 110 | function promiseInsertRow(aConnection, aDownloadRow) { |
michael@0 | 111 | // We can't use the aDownloadRow obj directly in the execute statement |
michael@0 | 112 | // because the obj bind code in Sqlite.jsm doesn't allow objects |
michael@0 | 113 | // with extra properties beyond those being binded. So we might as well |
michael@0 | 114 | // use an array as it is simpler. |
michael@0 | 115 | let values = [ |
michael@0 | 116 | aDownloadRow.source, aDownloadRow.target, aDownloadRow.tempPath, |
michael@0 | 117 | aDownloadRow.startTime.getTime() * 1000, aDownloadRow.state, |
michael@0 | 118 | aDownloadRow.referrer, aDownloadRow.entityID, aDownloadRow.maxBytes, |
michael@0 | 119 | aDownloadRow.mimeType, aDownloadRow.preferredApplication, |
michael@0 | 120 | aDownloadRow.preferredAction, aDownloadRow.autoResume |
michael@0 | 121 | ]; |
michael@0 | 122 | |
michael@0 | 123 | return aConnection.execute("INSERT INTO moz_downloads (" |
michael@0 | 124 | + "name, source, target, tempPath, startTime," |
michael@0 | 125 | + "endTime, state, referrer, entityID, currBytes," |
michael@0 | 126 | + "maxBytes, mimeType, preferredApplication," |
michael@0 | 127 | + "preferredAction, autoResume, guid)" |
michael@0 | 128 | + "VALUES (" |
michael@0 | 129 | + "'', ?, ?, ?, ?, " //name, |
michael@0 | 130 | + "0, ?, ?, ?, 0, " //endTime, currBytes |
michael@0 | 131 | + " ?, ?, ?, " // |
michael@0 | 132 | + " ?, ?, '')", //and guid are not imported |
michael@0 | 133 | values); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Retrieves the number of rows in the moz_downloads table of the |
michael@0 | 138 | * database. |
michael@0 | 139 | * |
michael@0 | 140 | * @param aConnection |
michael@0 | 141 | * The database connection. |
michael@0 | 142 | * |
michael@0 | 143 | * @return {Promise} |
michael@0 | 144 | * @resolves With the number of rows. |
michael@0 | 145 | * @rejects Never. |
michael@0 | 146 | */ |
michael@0 | 147 | function promiseTableCount(aConnection) { |
michael@0 | 148 | return aConnection.execute("SELECT COUNT(*) FROM moz_downloads") |
michael@0 | 149 | .then(res => res[0].getResultByName("COUNT(*)")) |
michael@0 | 150 | .then(null, Cu.reportError); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * Briefly opens a network channel to a given URL to retrieve |
michael@0 | 155 | * the entityID of this url, as generated by the network code. |
michael@0 | 156 | * |
michael@0 | 157 | * @param aUrl |
michael@0 | 158 | * The URL to retrieve the entityID. |
michael@0 | 159 | * |
michael@0 | 160 | * @return {Promise} |
michael@0 | 161 | * @resolves The EntityID of the given URL. |
michael@0 | 162 | * @rejects When there's a problem accessing the URL. |
michael@0 | 163 | */ |
michael@0 | 164 | function promiseEntityID(aUrl) { |
michael@0 | 165 | let deferred = Promise.defer(); |
michael@0 | 166 | let entityID = ""; |
michael@0 | 167 | let channel = NetUtil.newChannel(NetUtil.newURI(aUrl)); |
michael@0 | 168 | |
michael@0 | 169 | channel.asyncOpen({ |
michael@0 | 170 | onStartRequest: function (aRequest) { |
michael@0 | 171 | if (aRequest instanceof Ci.nsIResumableChannel) { |
michael@0 | 172 | entityID = aRequest.entityID; |
michael@0 | 173 | } |
michael@0 | 174 | aRequest.cancel(Cr.NS_BINDING_ABORTED); |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | onStopRequest: function (aRequest, aContext, aStatusCode) { |
michael@0 | 178 | if (aStatusCode == Cr.NS_BINDING_ABORTED) { |
michael@0 | 179 | deferred.resolve(entityID); |
michael@0 | 180 | } else { |
michael@0 | 181 | deferred.reject("Unexpected status code received"); |
michael@0 | 182 | } |
michael@0 | 183 | }, |
michael@0 | 184 | |
michael@0 | 185 | onDataAvailable: function () {} |
michael@0 | 186 | }, null); |
michael@0 | 187 | |
michael@0 | 188 | return deferred.promise; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | /** |
michael@0 | 192 | * Gets a file path to a temporary writeable download target, in the |
michael@0 | 193 | * correct format as expected to be stored in the downloads database, |
michael@0 | 194 | * which is file:///absolute/path/to/file |
michael@0 | 195 | * |
michael@0 | 196 | * @param aLeafName |
michael@0 | 197 | * A hint leaf name for the file. |
michael@0 | 198 | * |
michael@0 | 199 | * @return String The path to the download target. |
michael@0 | 200 | */ |
michael@0 | 201 | function getDownloadTarget(aLeafName) { |
michael@0 | 202 | return NetUtil.newURI(getTempFile(aLeafName)).spec; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | /** |
michael@0 | 206 | * Generates a temporary partial file to use as an in-progress |
michael@0 | 207 | * download. The file is written to disk with a part of the total expected |
michael@0 | 208 | * download content pre-written. |
michael@0 | 209 | * |
michael@0 | 210 | * @param aLeafName |
michael@0 | 211 | * A hint leaf name for the file. |
michael@0 | 212 | * @param aTainted |
michael@0 | 213 | * A boolean value. When true, the partial content of the file |
michael@0 | 214 | * will be different from the expected content of the original source |
michael@0 | 215 | * file. See the declaration of TEST_DATA_TAINTED for more information. |
michael@0 | 216 | * |
michael@0 | 217 | * @return {Promise} |
michael@0 | 218 | * @resolves When the operation completes, and returns a string with the path |
michael@0 | 219 | * to the generated file. |
michael@0 | 220 | * @rejects If there's an error writing the file. |
michael@0 | 221 | */ |
michael@0 | 222 | function getPartialFile(aLeafName, aTainted = false) { |
michael@0 | 223 | let tempDownload = getTempFile(aLeafName); |
michael@0 | 224 | let partialContent = aTainted |
michael@0 | 225 | ? TEST_DATA_TAINTED.substr(0, TEST_DATA_PARTIAL_LENGTH) |
michael@0 | 226 | : TEST_DATA_SHORT.substr(0, TEST_DATA_PARTIAL_LENGTH); |
michael@0 | 227 | |
michael@0 | 228 | return OS.File.writeAtomic(tempDownload.path, partialContent, |
michael@0 | 229 | { tmpPath: tempDownload.path + ".tmp", |
michael@0 | 230 | flush: true }) |
michael@0 | 231 | .then(() => tempDownload.path); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | /** |
michael@0 | 235 | * Generates a Date object to be used as the startTime for the download rows |
michael@0 | 236 | * in the DB. A date that is obviously different from the current time is |
michael@0 | 237 | * generated to make sure this stored data and a `new Date()` can't collide. |
michael@0 | 238 | * |
michael@0 | 239 | * @param aOffset |
michael@0 | 240 | * A offset from the base generated date is used to differentiate each |
michael@0 | 241 | * row in the database. |
michael@0 | 242 | * |
michael@0 | 243 | * @return A Date object. |
michael@0 | 244 | */ |
michael@0 | 245 | function getStartTime(aOffset) { |
michael@0 | 246 | return new Date(1000000 + (aOffset * 10000)); |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | /** |
michael@0 | 250 | * Performs various checks on an imported Download object to make sure |
michael@0 | 251 | * all properties are properly set as expected from the import procedure. |
michael@0 | 252 | * |
michael@0 | 253 | * @param aDownload |
michael@0 | 254 | * The Download object to be checked. |
michael@0 | 255 | * @param aDownloadRow |
michael@0 | 256 | * An object that represents a row from the original database table, |
michael@0 | 257 | * with extra properties describing expected values that are not |
michael@0 | 258 | * explictly part of the database. |
michael@0 | 259 | * |
michael@0 | 260 | * @return {Promise} |
michael@0 | 261 | * @resolves When the operation completes |
michael@0 | 262 | * @rejects Never |
michael@0 | 263 | */ |
michael@0 | 264 | function checkDownload(aDownload, aDownloadRow) { |
michael@0 | 265 | return Task.spawn(function() { |
michael@0 | 266 | do_check_eq(aDownload.source.url, aDownloadRow.source); |
michael@0 | 267 | do_check_eq(aDownload.source.referrer, aDownloadRow.referrer); |
michael@0 | 268 | |
michael@0 | 269 | do_check_eq(aDownload.target.path, |
michael@0 | 270 | NetUtil.newURI(aDownloadRow.target) |
michael@0 | 271 | .QueryInterface(Ci.nsIFileURL).file.path); |
michael@0 | 272 | |
michael@0 | 273 | do_check_eq(aDownload.target.partFilePath, aDownloadRow.tempPath); |
michael@0 | 274 | |
michael@0 | 275 | if (aDownloadRow.expectedResume) { |
michael@0 | 276 | do_check_true(!aDownload.stopped || aDownload.succeeded); |
michael@0 | 277 | yield promiseDownloadStopped(aDownload); |
michael@0 | 278 | |
michael@0 | 279 | do_check_true(aDownload.succeeded); |
michael@0 | 280 | do_check_eq(aDownload.progress, 100); |
michael@0 | 281 | // If the download has resumed, a new startTime will be set. |
michael@0 | 282 | // By calling toJSON we're also testing that startTime is a Date object. |
michael@0 | 283 | do_check_neq(aDownload.startTime.toJSON(), |
michael@0 | 284 | aDownloadRow.startTime.toJSON()); |
michael@0 | 285 | } else { |
michael@0 | 286 | do_check_false(aDownload.succeeded); |
michael@0 | 287 | do_check_eq(aDownload.startTime.toJSON(), |
michael@0 | 288 | aDownloadRow.startTime.toJSON()); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | do_check_eq(aDownload.stopped, true); |
michael@0 | 292 | |
michael@0 | 293 | let serializedSaver = aDownload.saver.toSerializable(); |
michael@0 | 294 | if (typeof(serializedSaver) == "object") { |
michael@0 | 295 | do_check_eq(serializedSaver.type, "copy"); |
michael@0 | 296 | } else { |
michael@0 | 297 | do_check_eq(serializedSaver, "copy"); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | if (aDownloadRow.entityID) { |
michael@0 | 301 | do_check_eq(aDownload.saver.entityID, aDownloadRow.entityID); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | do_check_eq(aDownload.currentBytes, aDownloadRow.expectedCurrentBytes); |
michael@0 | 305 | do_check_eq(aDownload.totalBytes, aDownloadRow.expectedTotalBytes); |
michael@0 | 306 | |
michael@0 | 307 | if (aDownloadRow.expectedContent) { |
michael@0 | 308 | let fileToCheck = aDownloadRow.expectedResume |
michael@0 | 309 | ? aDownload.target.path |
michael@0 | 310 | : aDownload.target.partFilePath; |
michael@0 | 311 | yield promiseVerifyContents(fileToCheck, aDownloadRow.expectedContent); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | do_check_eq(aDownload.contentType, aDownloadRow.expectedContentType); |
michael@0 | 315 | do_check_eq(aDownload.launcherPath, aDownloadRow.preferredApplication); |
michael@0 | 316 | |
michael@0 | 317 | do_check_eq(aDownload.launchWhenSucceeded, |
michael@0 | 318 | aDownloadRow.preferredAction != Ci.nsIMIMEInfo.saveToDisk); |
michael@0 | 319 | }); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 323 | //// Preparation tasks |
michael@0 | 324 | |
michael@0 | 325 | /** |
michael@0 | 326 | * Prepares the list of downloads to be added to the database that should |
michael@0 | 327 | * be imported by the import procedure. |
michael@0 | 328 | */ |
michael@0 | 329 | add_task(function prepareDownloadsToImport() { |
michael@0 | 330 | |
michael@0 | 331 | let sourceUrl = httpUrl("source.txt"); |
michael@0 | 332 | let sourceEntityId = yield promiseEntityID(sourceUrl); |
michael@0 | 333 | |
michael@0 | 334 | gDownloadsRowToImport = [ |
michael@0 | 335 | // Paused download with autoResume and a partial file. By |
michael@0 | 336 | // setting the correct entityID the download can resume from |
michael@0 | 337 | // where it stopped, and to test that this works properly we |
michael@0 | 338 | // intentionally set different data in the beginning of the |
michael@0 | 339 | // partial file to make sure it was not replaced. |
michael@0 | 340 | { |
michael@0 | 341 | source: sourceUrl, |
michael@0 | 342 | target: getDownloadTarget("inprogress1.txt"), |
michael@0 | 343 | tempPath: yield getPartialFile("inprogress1.txt.part", true), |
michael@0 | 344 | startTime: getStartTime(1), |
michael@0 | 345 | state: DOWNLOAD_PAUSED, |
michael@0 | 346 | referrer: httpUrl("referrer1"), |
michael@0 | 347 | entityID: sourceEntityId, |
michael@0 | 348 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 349 | mimeType: "mimeType1", |
michael@0 | 350 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 351 | preferredApplication: "prerredApplication1", |
michael@0 | 352 | autoResume: 1, |
michael@0 | 353 | |
michael@0 | 354 | // Even though the information stored in the DB said |
michael@0 | 355 | // maxBytes was MAXBYTES_IN_DB, the download turned out to be |
michael@0 | 356 | // a different length. Here we make sure the totalBytes property |
michael@0 | 357 | // was correctly set with the actual value. The same consideration |
michael@0 | 358 | // applies to the contentType. |
michael@0 | 359 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 360 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 361 | expectedResume: true, |
michael@0 | 362 | expectedContentType: "text/plain", |
michael@0 | 363 | expectedContent: TEST_DATA_TAINTED, |
michael@0 | 364 | }, |
michael@0 | 365 | |
michael@0 | 366 | // Paused download with autoResume and a partial file, |
michael@0 | 367 | // but missing entityID. This means that the download will |
michael@0 | 368 | // start from beginning, and the entire original content of the |
michael@0 | 369 | // source file should replace the different data that was stored |
michael@0 | 370 | // in the partial file. |
michael@0 | 371 | { |
michael@0 | 372 | source: sourceUrl, |
michael@0 | 373 | target: getDownloadTarget("inprogress2.txt"), |
michael@0 | 374 | tempPath: yield getPartialFile("inprogress2.txt.part", true), |
michael@0 | 375 | startTime: getStartTime(2), |
michael@0 | 376 | state: DOWNLOAD_PAUSED, |
michael@0 | 377 | referrer: httpUrl("referrer2"), |
michael@0 | 378 | entityID: "", |
michael@0 | 379 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 380 | mimeType: "mimeType2", |
michael@0 | 381 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 382 | preferredApplication: "prerredApplication2", |
michael@0 | 383 | autoResume: 1, |
michael@0 | 384 | |
michael@0 | 385 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 386 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 387 | expectedResume: true, |
michael@0 | 388 | expectedContentType: "text/plain", |
michael@0 | 389 | expectedContent: TEST_DATA_SHORT |
michael@0 | 390 | }, |
michael@0 | 391 | |
michael@0 | 392 | // Paused download with no autoResume and a partial file. |
michael@0 | 393 | { |
michael@0 | 394 | source: sourceUrl, |
michael@0 | 395 | target: getDownloadTarget("inprogress3.txt"), |
michael@0 | 396 | tempPath: yield getPartialFile("inprogress3.txt.part"), |
michael@0 | 397 | startTime: getStartTime(3), |
michael@0 | 398 | state: DOWNLOAD_PAUSED, |
michael@0 | 399 | referrer: httpUrl("referrer3"), |
michael@0 | 400 | entityID: "", |
michael@0 | 401 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 402 | mimeType: "mimeType3", |
michael@0 | 403 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 404 | preferredApplication: "prerredApplication3", |
michael@0 | 405 | autoResume: 0, |
michael@0 | 406 | |
michael@0 | 407 | // Since this download has not been resumed, the actual data |
michael@0 | 408 | // about its total size and content type is not known. |
michael@0 | 409 | // Therefore, we're going by the information imported from the DB. |
michael@0 | 410 | expectedCurrentBytes: TEST_DATA_PARTIAL_LENGTH, |
michael@0 | 411 | expectedTotalBytes: MAXBYTES_IN_DB, |
michael@0 | 412 | expectedResume: false, |
michael@0 | 413 | expectedContentType: "mimeType3", |
michael@0 | 414 | expectedContent: TEST_DATA_SHORT.substr(0, TEST_DATA_PARTIAL_LENGTH), |
michael@0 | 415 | }, |
michael@0 | 416 | |
michael@0 | 417 | // Paused download with autoResume and no partial file. |
michael@0 | 418 | { |
michael@0 | 419 | source: sourceUrl, |
michael@0 | 420 | target: getDownloadTarget("inprogress4.txt"), |
michael@0 | 421 | tempPath: "", |
michael@0 | 422 | startTime: getStartTime(4), |
michael@0 | 423 | state: DOWNLOAD_PAUSED, |
michael@0 | 424 | referrer: httpUrl("referrer4"), |
michael@0 | 425 | entityID: "", |
michael@0 | 426 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 427 | mimeType: "text/plain", |
michael@0 | 428 | preferredAction: Ci.nsIMIMEInfo.useHelperApp, |
michael@0 | 429 | preferredApplication: "prerredApplication4", |
michael@0 | 430 | autoResume: 1, |
michael@0 | 431 | |
michael@0 | 432 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 433 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 434 | expectedResume: true, |
michael@0 | 435 | expectedContentType: "text/plain", |
michael@0 | 436 | expectedContent: TEST_DATA_SHORT |
michael@0 | 437 | }, |
michael@0 | 438 | |
michael@0 | 439 | // Paused download with no autoResume and no partial file. |
michael@0 | 440 | { |
michael@0 | 441 | source: sourceUrl, |
michael@0 | 442 | target: getDownloadTarget("inprogress5.txt"), |
michael@0 | 443 | tempPath: "", |
michael@0 | 444 | startTime: getStartTime(5), |
michael@0 | 445 | state: DOWNLOAD_PAUSED, |
michael@0 | 446 | referrer: httpUrl("referrer4"), |
michael@0 | 447 | entityID: "", |
michael@0 | 448 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 449 | mimeType: "text/plain", |
michael@0 | 450 | preferredAction: Ci.nsIMIMEInfo.useSystemDefault, |
michael@0 | 451 | preferredApplication: "prerredApplication5", |
michael@0 | 452 | autoResume: 0, |
michael@0 | 453 | |
michael@0 | 454 | expectedCurrentBytes: 0, |
michael@0 | 455 | expectedTotalBytes: MAXBYTES_IN_DB, |
michael@0 | 456 | expectedResume: false, |
michael@0 | 457 | expectedContentType: "text/plain", |
michael@0 | 458 | }, |
michael@0 | 459 | |
michael@0 | 460 | // Queued download with no autoResume and no partial file. |
michael@0 | 461 | // Even though autoResume=0, queued downloads always autoResume. |
michael@0 | 462 | { |
michael@0 | 463 | source: sourceUrl, |
michael@0 | 464 | target: getDownloadTarget("inprogress6.txt"), |
michael@0 | 465 | tempPath: "", |
michael@0 | 466 | startTime: getStartTime(6), |
michael@0 | 467 | state: DOWNLOAD_QUEUED, |
michael@0 | 468 | referrer: httpUrl("referrer6"), |
michael@0 | 469 | entityID: "", |
michael@0 | 470 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 471 | mimeType: "text/plain", |
michael@0 | 472 | preferredAction: Ci.nsIMIMEInfo.useHelperApp, |
michael@0 | 473 | preferredApplication: "prerredApplication6", |
michael@0 | 474 | autoResume: 0, |
michael@0 | 475 | |
michael@0 | 476 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 477 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 478 | expectedResume: true, |
michael@0 | 479 | expectedContentType: "text/plain", |
michael@0 | 480 | expectedContent: TEST_DATA_SHORT |
michael@0 | 481 | }, |
michael@0 | 482 | |
michael@0 | 483 | // Notstarted download with no autoResume and no partial file. |
michael@0 | 484 | // Even though autoResume=0, notstarted downloads always autoResume. |
michael@0 | 485 | { |
michael@0 | 486 | source: sourceUrl, |
michael@0 | 487 | target: getDownloadTarget("inprogress7.txt"), |
michael@0 | 488 | tempPath: "", |
michael@0 | 489 | startTime: getStartTime(7), |
michael@0 | 490 | state: DOWNLOAD_NOTSTARTED, |
michael@0 | 491 | referrer: httpUrl("referrer7"), |
michael@0 | 492 | entityID: "", |
michael@0 | 493 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 494 | mimeType: "text/plain", |
michael@0 | 495 | preferredAction: Ci.nsIMIMEInfo.useHelperApp, |
michael@0 | 496 | preferredApplication: "prerredApplication7", |
michael@0 | 497 | autoResume: 0, |
michael@0 | 498 | |
michael@0 | 499 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 500 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 501 | expectedResume: true, |
michael@0 | 502 | expectedContentType: "text/plain", |
michael@0 | 503 | expectedContent: TEST_DATA_SHORT |
michael@0 | 504 | }, |
michael@0 | 505 | |
michael@0 | 506 | // Downloading download with no autoResume and a partial file. |
michael@0 | 507 | // Even though autoResume=0, downloading downloads always autoResume. |
michael@0 | 508 | { |
michael@0 | 509 | source: sourceUrl, |
michael@0 | 510 | target: getDownloadTarget("inprogress8.txt"), |
michael@0 | 511 | tempPath: yield getPartialFile("inprogress8.txt.part", true), |
michael@0 | 512 | startTime: getStartTime(8), |
michael@0 | 513 | state: DOWNLOAD_DOWNLOADING, |
michael@0 | 514 | referrer: httpUrl("referrer8"), |
michael@0 | 515 | entityID: sourceEntityId, |
michael@0 | 516 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 517 | mimeType: "text/plain", |
michael@0 | 518 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 519 | preferredApplication: "prerredApplication8", |
michael@0 | 520 | autoResume: 0, |
michael@0 | 521 | |
michael@0 | 522 | expectedCurrentBytes: TEST_DATA_LENGTH, |
michael@0 | 523 | expectedTotalBytes: TEST_DATA_LENGTH, |
michael@0 | 524 | expectedResume: true, |
michael@0 | 525 | expectedContentType: "text/plain", |
michael@0 | 526 | expectedContent: TEST_DATA_TAINTED |
michael@0 | 527 | }, |
michael@0 | 528 | ]; |
michael@0 | 529 | }); |
michael@0 | 530 | |
michael@0 | 531 | /** |
michael@0 | 532 | * Prepares the list of downloads to be added to the database that should |
michael@0 | 533 | * *not* be imported by the import procedure. |
michael@0 | 534 | */ |
michael@0 | 535 | add_task(function prepareNonImportableDownloads() |
michael@0 | 536 | { |
michael@0 | 537 | gDownloadsRowNonImportable = [ |
michael@0 | 538 | // Download with no source (should never happen in normal circumstances). |
michael@0 | 539 | { |
michael@0 | 540 | source: "", |
michael@0 | 541 | target: "nonimportable1.txt", |
michael@0 | 542 | tempPath: "", |
michael@0 | 543 | startTime: getStartTime(1), |
michael@0 | 544 | state: DOWNLOAD_PAUSED, |
michael@0 | 545 | referrer: "", |
michael@0 | 546 | entityID: "", |
michael@0 | 547 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 548 | mimeType: "mimeType1", |
michael@0 | 549 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 550 | preferredApplication: "prerredApplication1", |
michael@0 | 551 | autoResume: 1 |
michael@0 | 552 | }, |
michael@0 | 553 | |
michael@0 | 554 | // state = DOWNLOAD_FAILED |
michael@0 | 555 | { |
michael@0 | 556 | source: httpUrl("source.txt"), |
michael@0 | 557 | target: "nonimportable2.txt", |
michael@0 | 558 | tempPath: "", |
michael@0 | 559 | startTime: getStartTime(2), |
michael@0 | 560 | state: DOWNLOAD_FAILED, |
michael@0 | 561 | referrer: "", |
michael@0 | 562 | entityID: "", |
michael@0 | 563 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 564 | mimeType: "mimeType2", |
michael@0 | 565 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 566 | preferredApplication: "prerredApplication2", |
michael@0 | 567 | autoResume: 1 |
michael@0 | 568 | }, |
michael@0 | 569 | |
michael@0 | 570 | // state = DOWNLOAD_CANCELED |
michael@0 | 571 | { |
michael@0 | 572 | source: httpUrl("source.txt"), |
michael@0 | 573 | target: "nonimportable3.txt", |
michael@0 | 574 | tempPath: "", |
michael@0 | 575 | startTime: getStartTime(3), |
michael@0 | 576 | state: DOWNLOAD_CANCELED, |
michael@0 | 577 | referrer: "", |
michael@0 | 578 | entityID: "", |
michael@0 | 579 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 580 | mimeType: "mimeType3", |
michael@0 | 581 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 582 | preferredApplication: "prerredApplication3", |
michael@0 | 583 | autoResume: 1 |
michael@0 | 584 | }, |
michael@0 | 585 | |
michael@0 | 586 | // state = DOWNLOAD_BLOCKED_PARENTAL |
michael@0 | 587 | { |
michael@0 | 588 | source: httpUrl("source.txt"), |
michael@0 | 589 | target: "nonimportable4.txt", |
michael@0 | 590 | tempPath: "", |
michael@0 | 591 | startTime: getStartTime(4), |
michael@0 | 592 | state: DOWNLOAD_BLOCKED_PARENTAL, |
michael@0 | 593 | referrer: "", |
michael@0 | 594 | entityID: "", |
michael@0 | 595 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 596 | mimeType: "mimeType4", |
michael@0 | 597 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 598 | preferredApplication: "prerredApplication4", |
michael@0 | 599 | autoResume: 1 |
michael@0 | 600 | }, |
michael@0 | 601 | |
michael@0 | 602 | // state = DOWNLOAD_SCANNING |
michael@0 | 603 | { |
michael@0 | 604 | source: httpUrl("source.txt"), |
michael@0 | 605 | target: "nonimportable5.txt", |
michael@0 | 606 | tempPath: "", |
michael@0 | 607 | startTime: getStartTime(5), |
michael@0 | 608 | state: DOWNLOAD_SCANNING, |
michael@0 | 609 | referrer: "", |
michael@0 | 610 | entityID: "", |
michael@0 | 611 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 612 | mimeType: "mimeType5", |
michael@0 | 613 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 614 | preferredApplication: "prerredApplication5", |
michael@0 | 615 | autoResume: 1 |
michael@0 | 616 | }, |
michael@0 | 617 | |
michael@0 | 618 | // state = DOWNLOAD_DIRTY |
michael@0 | 619 | { |
michael@0 | 620 | source: httpUrl("source.txt"), |
michael@0 | 621 | target: "nonimportable6.txt", |
michael@0 | 622 | tempPath: "", |
michael@0 | 623 | startTime: getStartTime(6), |
michael@0 | 624 | state: DOWNLOAD_DIRTY, |
michael@0 | 625 | referrer: "", |
michael@0 | 626 | entityID: "", |
michael@0 | 627 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 628 | mimeType: "mimeType6", |
michael@0 | 629 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 630 | preferredApplication: "prerredApplication6", |
michael@0 | 631 | autoResume: 1 |
michael@0 | 632 | }, |
michael@0 | 633 | |
michael@0 | 634 | // state = DOWNLOAD_BLOCKED_POLICY |
michael@0 | 635 | { |
michael@0 | 636 | source: httpUrl("source.txt"), |
michael@0 | 637 | target: "nonimportable7.txt", |
michael@0 | 638 | tempPath: "", |
michael@0 | 639 | startTime: getStartTime(7), |
michael@0 | 640 | state: DOWNLOAD_BLOCKED_POLICY, |
michael@0 | 641 | referrer: "", |
michael@0 | 642 | entityID: "", |
michael@0 | 643 | maxBytes: MAXBYTES_IN_DB, |
michael@0 | 644 | mimeType: "mimeType7", |
michael@0 | 645 | preferredAction: Ci.nsIMIMEInfo.saveToDisk, |
michael@0 | 646 | preferredApplication: "prerredApplication7", |
michael@0 | 647 | autoResume: 1 |
michael@0 | 648 | }, |
michael@0 | 649 | ]; |
michael@0 | 650 | }); |
michael@0 | 651 | |
michael@0 | 652 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 653 | //// Test |
michael@0 | 654 | |
michael@0 | 655 | /** |
michael@0 | 656 | * Creates a temporary Sqlite database with download data and perform an |
michael@0 | 657 | * import of that data to the new Downloads API to verify that the import |
michael@0 | 658 | * worked correctly. |
michael@0 | 659 | */ |
michael@0 | 660 | add_task(function test_downloadImport() |
michael@0 | 661 | { |
michael@0 | 662 | let connection = null; |
michael@0 | 663 | let downloadsSqlite = getTempFile("downloads.sqlite").path; |
michael@0 | 664 | |
michael@0 | 665 | try { |
michael@0 | 666 | // Set up the database. |
michael@0 | 667 | connection = yield promiseEmptyDatabaseConnection({ |
michael@0 | 668 | aPath: downloadsSqlite, |
michael@0 | 669 | aSchemaVersion: 9 |
michael@0 | 670 | }); |
michael@0 | 671 | |
michael@0 | 672 | // Insert both the importable and non-importable |
michael@0 | 673 | // downloads together. |
michael@0 | 674 | for (let downloadRow of gDownloadsRowToImport) { |
michael@0 | 675 | yield promiseInsertRow(connection, downloadRow); |
michael@0 | 676 | } |
michael@0 | 677 | |
michael@0 | 678 | for (let downloadRow of gDownloadsRowNonImportable) { |
michael@0 | 679 | yield promiseInsertRow(connection, downloadRow); |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | // Check that every item was inserted. |
michael@0 | 683 | do_check_eq((yield promiseTableCount(connection)), |
michael@0 | 684 | gDownloadsRowToImport.length + |
michael@0 | 685 | gDownloadsRowNonImportable.length); |
michael@0 | 686 | } finally { |
michael@0 | 687 | // Close the connection so that DownloadImport can open it. |
michael@0 | 688 | yield connection.close(); |
michael@0 | 689 | } |
michael@0 | 690 | |
michael@0 | 691 | // Import items. |
michael@0 | 692 | let list = yield promiseNewList(false); |
michael@0 | 693 | yield new DownloadImport(list, downloadsSqlite).import(); |
michael@0 | 694 | let items = yield list.getAll(); |
michael@0 | 695 | |
michael@0 | 696 | do_check_eq(items.length, gDownloadsRowToImport.length); |
michael@0 | 697 | |
michael@0 | 698 | for (let i = 0; i < gDownloadsRowToImport.length; i++) { |
michael@0 | 699 | yield checkDownload(items[i], gDownloadsRowToImport[i]); |
michael@0 | 700 | } |
michael@0 | 701 | }) |