Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | |
michael@0 | 2 | const Cc = Components.classes; |
michael@0 | 3 | const Ci = Components.interfaces; |
michael@0 | 4 | const Cu = Components.utils; |
michael@0 | 5 | const Cr = Components.results; |
michael@0 | 6 | |
michael@0 | 7 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 8 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | |
michael@0 | 11 | do_get_profile(); |
michael@0 | 12 | |
michael@0 | 13 | // Dynamically generates a classID for our component, registers it to mask |
michael@0 | 14 | // the existing component, and stored the masked components classID to be |
michael@0 | 15 | // restored later, when we unregister. |
michael@0 | 16 | function registerTemporaryComponent(comp) |
michael@0 | 17 | { |
michael@0 | 18 | let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 19 | if (!comp.prototype.classID) { |
michael@0 | 20 | let uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); |
michael@0 | 21 | comp.prototype.classID = uuidgen.generateUUID(); |
michael@0 | 22 | } |
michael@0 | 23 | comp.prototype.maskedClassID = Components.ID(Cc[comp.prototype.contractID].number); |
michael@0 | 24 | if (!comp.prototype.factory) |
michael@0 | 25 | comp.prototype.factory = getFactory(comp); |
michael@0 | 26 | registrar.registerFactory(comp.prototype.classID, "", comp.prototype.contractID, comp.prototype.factory); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function unregisterTemporaryComponent(comp) |
michael@0 | 30 | { |
michael@0 | 31 | let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 32 | registrar.unregisterFactory(comp.prototype.classID, comp.prototype.factory); |
michael@0 | 33 | registrar.registerFactory(comp.prototype.maskedClassID, "", comp.prototype.contractID, null); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | let DownloadListener = { |
michael@0 | 37 | init: function () { |
michael@0 | 38 | let obs = Services.obs; |
michael@0 | 39 | obs.addObserver(this, "dl-done", true); |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | observe: function (subject, topic, data) { |
michael@0 | 43 | this.onFinished(subject, topic, data); |
michael@0 | 44 | }, |
michael@0 | 45 | |
michael@0 | 46 | QueryInterface: function (iid) { |
michael@0 | 47 | if (iid.equals(Ci.nsIObserver) || |
michael@0 | 48 | iid.equals(Ci.nsISupportsWeakReference) || |
michael@0 | 49 | iid.equals(Ci.nsISupports)) |
michael@0 | 50 | return this; |
michael@0 | 51 | |
michael@0 | 52 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | DownloadListener.init(); |
michael@0 | 56 | |
michael@0 | 57 | function HelperAppDlg() { } |
michael@0 | 58 | HelperAppDlg.prototype = { |
michael@0 | 59 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]), |
michael@0 | 60 | contractID: "@mozilla.org/helperapplauncherdialog;1", |
michael@0 | 61 | show: function (launcher, ctx, reason, usePrivateUI) { |
michael@0 | 62 | launcher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToFile; |
michael@0 | 63 | launcher.launchWithApplication(null, false); |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | promptForSaveToFile: function (launcher, ctx, defaultFile, suggestedExtension, forcePrompt) { } |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | // Stolen from XPCOMUtils, since this handy function is not public there |
michael@0 | 70 | function getFactory(comp) |
michael@0 | 71 | { |
michael@0 | 72 | return { |
michael@0 | 73 | createInstance: function (outer, iid) { |
michael@0 | 74 | if (outer) |
michael@0 | 75 | throw Cr.NS_ERROR_NO_AGGREGATION; |
michael@0 | 76 | return (new comp()).QueryInterface(iid); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // Override the download-manager-ui to prevent anyone from trying to open |
michael@0 | 82 | // a window. |
michael@0 | 83 | function DownloadMgrUI() { } |
michael@0 | 84 | DownloadMgrUI.prototype = { |
michael@0 | 85 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]), |
michael@0 | 86 | contractID: "@mozilla.org/download-manager-ui;1", |
michael@0 | 87 | show: function (ir, aID, reason) { }, |
michael@0 | 88 | |
michael@0 | 89 | visible: false, |
michael@0 | 90 | |
michael@0 | 91 | getAttention: function () { } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | function AlertsSVC() { } |
michael@0 | 95 | AlertsSVC.prototype = { |
michael@0 | 96 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService]), |
michael@0 | 97 | contractID: "@mozilla.org/alerts-service;1", |
michael@0 | 98 | showAlertNotification: function (url, title, text, clickable, cookie, listener, name) { }, |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | registerTemporaryComponent(HelperAppDlg); |
michael@0 | 102 | registerTemporaryComponent(DownloadMgrUI); |
michael@0 | 103 | registerTemporaryComponent(AlertsSVC); |
michael@0 | 104 | |
michael@0 | 105 | function initChildTestEnv() |
michael@0 | 106 | { |
michael@0 | 107 | sendCommand(' \ |
michael@0 | 108 | const Cc = Components.classes; \ |
michael@0 | 109 | const Ci = Components.interfaces; \ |
michael@0 | 110 | const Cr = Components.results; \ |
michael@0 | 111 | function WindowContext() { } \ |
michael@0 | 112 | \ |
michael@0 | 113 | WindowContext.prototype = { \ |
michael@0 | 114 | getInterface: function (iid) { \ |
michael@0 | 115 | if (iid.equals(Ci.nsIInterfaceRequestor) || \ |
michael@0 | 116 | iid.equals(Ci.nsIURIContentListener) || \ |
michael@0 | 117 | iid.equals(Ci.nsILoadGroup) || \ |
michael@0 | 118 | iid.equals(Ci.nsIDocumentLoader) || \ |
michael@0 | 119 | iid.equals(Ci.nsIDOMWindow)) \ |
michael@0 | 120 | return this; \ |
michael@0 | 121 | \ |
michael@0 | 122 | throw Cr.NS_ERROR_NO_INTERFACE; \ |
michael@0 | 123 | }, \ |
michael@0 | 124 | \ |
michael@0 | 125 | /* nsIURIContentListener */ \ |
michael@0 | 126 | onStartURIOpen: function (uri) { }, \ |
michael@0 | 127 | isPreferred: function (type, desiredtype) { return false; }, \ |
michael@0 | 128 | \ |
michael@0 | 129 | /* nsILoadGroup */ \ |
michael@0 | 130 | addRequest: function (request, context) { }, \ |
michael@0 | 131 | removeRequest: function (request, context, status) { } \ |
michael@0 | 132 | }; \ |
michael@0 | 133 | \ |
michael@0 | 134 | var ioservice = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);\ |
michael@0 | 135 | var uriloader = Cc["@mozilla.org/uriloader;1"].getService(Ci.nsIURILoader);\ |
michael@0 | 136 | '); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | function testFinisher(endFunc) { |
michael@0 | 140 | let ef = endFunc; |
michael@0 | 141 | return function (file) { |
michael@0 | 142 | ef(file); |
michael@0 | 143 | runNextTest(); |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | function runChildTestSet(set) |
michael@0 | 148 | { |
michael@0 | 149 | DownloadListener.onFinished = testFinisher(set[2]); |
michael@0 | 150 | sendCommand('\ |
michael@0 | 151 | let uri = ioservice.newURI("http://localhost:4444' + set[0] + '", null, null);\ |
michael@0 | 152 | let channel = ioservice.newChannelFromURI(uri); \ |
michael@0 | 153 | uriloader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED, new WindowContext()); \ |
michael@0 | 154 | '); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | var httpserver = null; |
michael@0 | 158 | let currentTest = 0; |
michael@0 | 159 | function runNextTest() |
michael@0 | 160 | { |
michael@0 | 161 | if (currentTest == tests.length) { |
michael@0 | 162 | httpserver.stop(do_test_finished); |
michael@0 | 163 | return; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | let set = tests[currentTest++]; |
michael@0 | 167 | runChildTestSet(set); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | const responseBody = [0x1f, 0x8b, 0x08, 0x00, 0x16, 0x5a, 0x8a, 0x48, 0x02, |
michael@0 | 171 | 0x03, 0x2b, 0x49, 0x2d, 0x2e, 0xe1, 0x02, 0x00, 0xc6, |
michael@0 | 172 | 0x35, 0xb9, 0x3b, 0x05, 0x00, 0x00, 0x00]; |
michael@0 | 173 | |
michael@0 | 174 | /* |
michael@0 | 175 | * First test: a file with Content-Type application/x-gzip and Content-Encoding gzip |
michael@0 | 176 | * should not be decoded in a round-trip |
michael@0 | 177 | */ |
michael@0 | 178 | function testResponse1(metadata, response) { |
michael@0 | 179 | response.setHeader("Content-Type", "application/x-gzip", false); |
michael@0 | 180 | response.setHeader("Content-Encoding", "gzip", false); |
michael@0 | 181 | response.setHeader("Content-Disposition", "attachment", false); |
michael@0 | 182 | |
michael@0 | 183 | var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream); |
michael@0 | 184 | bos.setOutputStream(response.bodyOutputStream); |
michael@0 | 185 | bos.writeByteArray(responseBody, responseBody.length); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | function finishTest1(subject, topic, data) { |
michael@0 | 189 | let file = subject.QueryInterface(Ci.nsIDownload).targetFile; |
michael@0 | 190 | do_check_true(file.path.search("test1.gz") != 0); |
michael@0 | 191 | let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); |
michael@0 | 192 | fis.init(file, -1, -1, 0); |
michael@0 | 193 | let bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 194 | bis.setInputStream(fis); |
michael@0 | 195 | let str = bis.readByteArray(bis.available()); |
michael@0 | 196 | do_check_true(str.length == responseBody.length); |
michael@0 | 197 | |
michael@0 | 198 | let cmp = 0; |
michael@0 | 199 | for (i = 0; i < str.length; i++) { |
michael@0 | 200 | cmp += str[i] - responseBody[i]; |
michael@0 | 201 | if (cmp != 0) break; |
michael@0 | 202 | } |
michael@0 | 203 | do_check_true(cmp == 0); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* |
michael@0 | 207 | * Second test: a file with Content-Type text/html and Content-Encoding gzip |
michael@0 | 208 | * should not be decoded in a round-trip, if its filename ends in ".gz". |
michael@0 | 209 | * We specify a Content-disposition header to force it to be saved as a file. |
michael@0 | 210 | */ |
michael@0 | 211 | function testResponse2(metadata, response) { |
michael@0 | 212 | response.setHeader("Content-Type", "text/html", false); |
michael@0 | 213 | response.setHeader("Content-Encoding", "gzip", false); |
michael@0 | 214 | response.setHeader("Content-Disposition", "attachment", false); |
michael@0 | 215 | |
michael@0 | 216 | var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream); |
michael@0 | 217 | bos.setOutputStream(response.bodyOutputStream); |
michael@0 | 218 | bos.writeByteArray(responseBody, responseBody.length); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | function finishTest2(subject, topic, data) { |
michael@0 | 222 | let file = subject.QueryInterface(Ci.nsIDownload).targetFile; |
michael@0 | 223 | do_check_true(file.path.search("test2.gz") != 0); |
michael@0 | 224 | let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); |
michael@0 | 225 | fis.init(file, -1, -1, 0); |
michael@0 | 226 | let bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 227 | bis.setInputStream(fis); |
michael@0 | 228 | let str = bis.readByteArray(bis.available()); |
michael@0 | 229 | do_check_true(str.length == responseBody.length); |
michael@0 | 230 | |
michael@0 | 231 | let cmp = 0; |
michael@0 | 232 | for (i = 0; i < str.length; i++) { |
michael@0 | 233 | cmp += str[i] - responseBody[i]; |
michael@0 | 234 | if (cmp != 0) break; |
michael@0 | 235 | } |
michael@0 | 236 | do_check_true(cmp == 0); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | function testResponse3(metadata, response) { |
michael@0 | 240 | response.setHeader("Content-Type", "text/html", false); |
michael@0 | 241 | response.setHeader("Content-Encoding", "gzip", false); |
michael@0 | 242 | response.setHeader("Content-Disposition", "attachment", false); |
michael@0 | 243 | |
michael@0 | 244 | var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream); |
michael@0 | 245 | bos.setOutputStream(response.bodyOutputStream); |
michael@0 | 246 | bos.writeByteArray(responseBody, responseBody.length); |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | function finishTest3(subject, topic, data) { |
michael@0 | 250 | let file = subject.QueryInterface(Ci.nsIDownload).targetFile; |
michael@0 | 251 | do_check_true(file.path.search("test3.txt") != 0); |
michael@0 | 252 | let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); |
michael@0 | 253 | fis.init(file, -1, -1, 0); |
michael@0 | 254 | let bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream); |
michael@0 | 255 | bis.setInputStream(fis); |
michael@0 | 256 | let str = bis.readByteArray(bis.available()); |
michael@0 | 257 | let decodedBody = [ 116, 101, 115, 116, 10 ]; // 't','e','s','t','\n' |
michael@0 | 258 | do_check_true(str.length == decodedBody.length); |
michael@0 | 259 | |
michael@0 | 260 | let cmp = 0; |
michael@0 | 261 | for (i = 0; i < str.length; i++) { |
michael@0 | 262 | cmp += str[i] - decodedBody[i]; |
michael@0 | 263 | if (cmp != 0) break; |
michael@0 | 264 | } |
michael@0 | 265 | do_check_true(cmp == 0); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | let tests = [ |
michael@0 | 269 | [ "/test1.gz", testResponse1, finishTest1 ], |
michael@0 | 270 | [ "/test2.gz", testResponse2, finishTest2 ], |
michael@0 | 271 | [ "/test3.txt", testResponse3, finishTest3 ], |
michael@0 | 272 | ]; |
michael@0 | 273 | |
michael@0 | 274 | function run_test() { |
michael@0 | 275 | // do_load_child_test_harness(); |
michael@0 | 276 | httpserver = new HttpServer(); |
michael@0 | 277 | httpserver.start(4444); |
michael@0 | 278 | do_test_pending(); |
michael@0 | 279 | |
michael@0 | 280 | initChildTestEnv(); |
michael@0 | 281 | |
michael@0 | 282 | for each (set in tests) |
michael@0 | 283 | httpserver.registerPathHandler(set[0], set[1]); |
michael@0 | 284 | |
michael@0 | 285 | runNextTest(); |
michael@0 | 286 | } |