Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Test download resume to do real-resume instead of stalling-the-channel |
michael@0 | 7 | * resume. Also test bug 395537 for resuming files that are deleted before |
michael@0 | 8 | * they're resumed. Bug 398216 is checked by making sure the reported progress |
michael@0 | 9 | * and file size are updated for finished downloads. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | const nsIF = Ci.nsIFile; |
michael@0 | 13 | const nsIDM = Ci.nsIDownloadManager; |
michael@0 | 14 | const nsIWBP = Ci.nsIWebBrowserPersist; |
michael@0 | 15 | const nsIWPL = Ci.nsIWebProgressListener; |
michael@0 | 16 | const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDM); |
michael@0 | 17 | |
michael@0 | 18 | function run_test() |
michael@0 | 19 | { |
michael@0 | 20 | if (oldDownloadManagerDisabled()) { |
michael@0 | 21 | return; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * 1. Create data for http server to send |
michael@0 | 26 | */ |
michael@0 | 27 | // data starts at 10 bytes |
michael@0 | 28 | var data = "1234567890"; |
michael@0 | 29 | // data * 10^4 = 100,000 bytes (actually 101,111 bytes with newline) |
michael@0 | 30 | for (var i = 0; i < 4; i++) { |
michael@0 | 31 | data = [data,data,data,data,data,data,data,data,data,data,"\n"].join(""); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * 2. Start the http server that can handle resume |
michael@0 | 36 | */ |
michael@0 | 37 | var httpserv = new HttpServer(); |
michael@0 | 38 | var didResumeServer = false; |
michael@0 | 39 | httpserv.registerPathHandler("/resume", function(meta, resp) { |
michael@0 | 40 | var body = data; |
michael@0 | 41 | resp.setHeader("Content-Type", "text/html", false); |
michael@0 | 42 | if (meta.hasHeader("Range")) { |
michael@0 | 43 | // track that we resumed for testing |
michael@0 | 44 | didResumeServer = true; |
michael@0 | 45 | // Syntax: bytes=[from]-[to] (we don't support multiple ranges) |
michael@0 | 46 | var matches = meta.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); |
michael@0 | 47 | var from = (matches[1] === undefined) ? 0 : matches[1]; |
michael@0 | 48 | var to = (matches[2] === undefined) ? data.length - 1 : matches[2]; |
michael@0 | 49 | if (from >= data.length) { |
michael@0 | 50 | resp.setStatusLine(meta.httpVersion, 416, "Start pos too high"); |
michael@0 | 51 | resp.setHeader("Content-Range", "*/" + data.length, false); |
michael@0 | 52 | return; |
michael@0 | 53 | } |
michael@0 | 54 | body = body.substring(from, to + 1); |
michael@0 | 55 | // always respond to successful range requests with 206 |
michael@0 | 56 | resp.setStatusLine(meta.httpVersion, 206, "Partial Content"); |
michael@0 | 57 | resp.setHeader("Content-Range", from + "-" + to + "/" + data.length, false); |
michael@0 | 58 | } |
michael@0 | 59 | resp.bodyOutputStream.write(body, body.length); |
michael@0 | 60 | }); |
michael@0 | 61 | httpserv.start(-1); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * 3. Perform various actions for certain download states |
michael@0 | 65 | */ |
michael@0 | 66 | var didPause = false; |
michael@0 | 67 | var didResumeDownload = false; |
michael@0 | 68 | dm.addListener({ |
michael@0 | 69 | onDownloadStateChange: function(a, aDl) { |
michael@0 | 70 | if (aDl.state == nsIDM.DOWNLOAD_DOWNLOADING && !didPause) { |
michael@0 | 71 | /** |
michael@0 | 72 | * (1) queued -> downloading = pause the download |
michael@0 | 73 | */ |
michael@0 | 74 | dm.pauseDownload(aDl.id); |
michael@0 | 75 | } else if (aDl.state == nsIDM.DOWNLOAD_PAUSED) { |
michael@0 | 76 | /** |
michael@0 | 77 | * (2) downloading -> paused = remove the file |
michael@0 | 78 | */ |
michael@0 | 79 | didPause = true; |
michael@0 | 80 | // Test bug 395537 by removing the file before we actually resume |
michael@0 | 81 | aDl.targetFile.remove(false); |
michael@0 | 82 | } else if (aDl.state == nsIDM.DOWNLOAD_FINISHED) { |
michael@0 | 83 | /** |
michael@0 | 84 | * (4) downloading (resumed) -> finished = check tests |
michael@0 | 85 | */ |
michael@0 | 86 | // did we pause at all? |
michael@0 | 87 | do_check_true(didPause); |
michael@0 | 88 | // did we real-resume and not fake-resume? |
michael@0 | 89 | do_check_true(didResumeDownload); |
michael@0 | 90 | // extra real-resume check for the server |
michael@0 | 91 | do_check_true(didResumeServer); |
michael@0 | 92 | // did we download the whole file? |
michael@0 | 93 | do_check_eq(data.length, aDl.targetFile.fileSize); |
michael@0 | 94 | // extra sanity checks on size (test bug 398216) |
michael@0 | 95 | do_check_eq(data.length, aDl.amountTransferred); |
michael@0 | 96 | do_check_eq(data.length, aDl.size); |
michael@0 | 97 | |
michael@0 | 98 | httpserv.stop(do_test_finished); |
michael@0 | 99 | } |
michael@0 | 100 | }, |
michael@0 | 101 | onStateChange: function(a, b, aState, d, aDl) { |
michael@0 | 102 | if ((aState & nsIWPL.STATE_STOP) && didPause && !didResumeServer && |
michael@0 | 103 | !didResumeDownload) { |
michael@0 | 104 | /** |
michael@0 | 105 | * (3) paused -> stopped = resume the download |
michael@0 | 106 | */ |
michael@0 | 107 | dm.resumeDownload(aDl.id); |
michael@0 | 108 | didResumeDownload = true; |
michael@0 | 109 | } |
michael@0 | 110 | }, |
michael@0 | 111 | onProgressChange: function(a, b, c, d, e, f, g) { }, |
michael@0 | 112 | onSecurityChange: function(a, b, c, d) { } |
michael@0 | 113 | }); |
michael@0 | 114 | dm.addListener(getDownloadListener()); |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * 4. Start the download |
michael@0 | 118 | */ |
michael@0 | 119 | var destFile = dirSvc.get("ProfD", nsIF); |
michael@0 | 120 | destFile.append("resumed"); |
michael@0 | 121 | if (destFile.exists()) |
michael@0 | 122 | destFile.remove(false); |
michael@0 | 123 | var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]. |
michael@0 | 124 | createInstance(nsIWBP); |
michael@0 | 125 | persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES | |
michael@0 | 126 | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE | |
michael@0 | 127 | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; |
michael@0 | 128 | var dl = dm.addDownload(nsIDM.DOWNLOAD_TYPE_DOWNLOAD, |
michael@0 | 129 | createURI("http://localhost:" + |
michael@0 | 130 | httpserv.identity.primaryPort + "/resume"), |
michael@0 | 131 | createURI(destFile), null, null, |
michael@0 | 132 | Math.round(Date.now() * 1000), null, persist, false); |
michael@0 | 133 | persist.progressListener = dl.QueryInterface(nsIWPL); |
michael@0 | 134 | persist.saveURI(dl.source, null, null, null, null, dl.targetFile, null); |
michael@0 | 135 | |
michael@0 | 136 | // Mark as pending, so clear this when we actually finish the download |
michael@0 | 137 | do_test_pending(); |
michael@0 | 138 | } |