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 | /* 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 | /** |
michael@0 | 6 | * Tests some basic jar channel functionality |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | const {classes: Cc, |
michael@0 | 11 | interfaces: Ci, |
michael@0 | 12 | results: Cr, |
michael@0 | 13 | Constructor: ctor |
michael@0 | 14 | } = Components; |
michael@0 | 15 | |
michael@0 | 16 | const ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 17 | getService(Ci.nsIIOService); |
michael@0 | 18 | const dirSvc = Cc["@mozilla.org/file/directory_service;1"]. |
michael@0 | 19 | getService(Ci.nsIProperties); |
michael@0 | 20 | const obs = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 21 | getService(Ci.nsIObserverService); |
michael@0 | 22 | |
michael@0 | 23 | const nsIBinaryInputStream = ctor("@mozilla.org/binaryinputstream;1", |
michael@0 | 24 | "nsIBinaryInputStream", |
michael@0 | 25 | "setInputStream" |
michael@0 | 26 | ); |
michael@0 | 27 | |
michael@0 | 28 | const fileBase = "test_bug637286.zip"; |
michael@0 | 29 | const file = do_get_file("data/" + fileBase); |
michael@0 | 30 | // on child we'll test with jar:remoteopenfile:// instead of jar:file:// |
michael@0 | 31 | const jarBase = "jar:" + filePrefix + ios.newFileURI(file).spec + "!"; |
michael@0 | 32 | const tmpDir = dirSvc.get("TmpD", Ci.nsIFile); |
michael@0 | 33 | |
michael@0 | 34 | function Listener(callback) { |
michael@0 | 35 | this._callback = callback; |
michael@0 | 36 | } |
michael@0 | 37 | Listener.prototype = { |
michael@0 | 38 | gotStartRequest: false, |
michael@0 | 39 | available: -1, |
michael@0 | 40 | gotStopRequest: false, |
michael@0 | 41 | QueryInterface: function(iid) { |
michael@0 | 42 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 43 | iid.equals(Ci.nsIRequestObserver)) |
michael@0 | 44 | return this; |
michael@0 | 45 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 46 | }, |
michael@0 | 47 | onDataAvailable: function(request, ctx, stream, offset, count) { |
michael@0 | 48 | try { |
michael@0 | 49 | this.available = stream.available(); |
michael@0 | 50 | do_check_eq(this.available, count); |
michael@0 | 51 | // Need to consume stream to avoid assertion |
michael@0 | 52 | new nsIBinaryInputStream(stream).readBytes(count); |
michael@0 | 53 | } |
michael@0 | 54 | catch (ex) { |
michael@0 | 55 | do_throw(ex); |
michael@0 | 56 | } |
michael@0 | 57 | }, |
michael@0 | 58 | onStartRequest: function(request, ctx) { |
michael@0 | 59 | this.gotStartRequest = true; |
michael@0 | 60 | }, |
michael@0 | 61 | onStopRequest: function(request, ctx) { |
michael@0 | 62 | this.gotStopRequest = true; |
michael@0 | 63 | if (this._callback) { |
michael@0 | 64 | this._callback.call(null, this); |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * Basic reading test for asynchronously opened jar channel |
michael@0 | 71 | */ |
michael@0 | 72 | function testAsync() { |
michael@0 | 73 | var uri = jarBase + "/inner40.zip"; |
michael@0 | 74 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 75 | do_check_true(chan.contentLength < 0); |
michael@0 | 76 | chan.asyncOpen(new Listener(function(l) { |
michael@0 | 77 | do_check_true(chan.contentLength > 0); |
michael@0 | 78 | do_check_true(l.gotStartRequest); |
michael@0 | 79 | do_check_true(l.gotStopRequest); |
michael@0 | 80 | do_check_eq(l.available, chan.contentLength); |
michael@0 | 81 | |
michael@0 | 82 | run_next_test(); |
michael@0 | 83 | }), null); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | add_test(testAsync); |
michael@0 | 87 | // Run same test again so we test the codepath for a zipcache hit |
michael@0 | 88 | add_test(testAsync); |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | // In e10s child processes we don't currently support |
michael@0 | 92 | // 1) synchronously opening jar files on parent |
michael@0 | 93 | // 2) nested jar channels in e10s: (app:// doesn't use them). |
michael@0 | 94 | // 3) we can't do file lock checks on android, so skip those tests too. |
michael@0 | 95 | if (!inChild) { |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Basic reading test for synchronously opened jar channels |
michael@0 | 99 | */ |
michael@0 | 100 | add_test(function testSync() { |
michael@0 | 101 | var uri = jarBase + "/inner40.zip"; |
michael@0 | 102 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 103 | var stream = chan.open(); |
michael@0 | 104 | do_check_true(chan.contentLength > 0); |
michael@0 | 105 | do_check_eq(stream.available(), chan.contentLength); |
michael@0 | 106 | stream.close(); |
michael@0 | 107 | stream.close(); // should still not throw |
michael@0 | 108 | |
michael@0 | 109 | run_next_test(); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Basic reading test for synchronously opened, nested jar channels |
michael@0 | 115 | */ |
michael@0 | 116 | add_test(function testSyncNested() { |
michael@0 | 117 | var uri = "jar:" + jarBase + "/inner40.zip!/foo"; |
michael@0 | 118 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 119 | var stream = chan.open(); |
michael@0 | 120 | do_check_true(chan.contentLength > 0); |
michael@0 | 121 | do_check_eq(stream.available(), chan.contentLength); |
michael@0 | 122 | stream.close(); |
michael@0 | 123 | stream.close(); // should still not throw |
michael@0 | 124 | |
michael@0 | 125 | run_next_test(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * Basic reading test for asynchronously opened, nested jar channels |
michael@0 | 130 | */ |
michael@0 | 131 | add_test(function testAsyncNested(next) { |
michael@0 | 132 | var uri = "jar:" + jarBase + "/inner40.zip!/foo"; |
michael@0 | 133 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 134 | chan.asyncOpen(new Listener(function(l) { |
michael@0 | 135 | do_check_true(chan.contentLength > 0); |
michael@0 | 136 | do_check_true(l.gotStartRequest); |
michael@0 | 137 | do_check_true(l.gotStopRequest); |
michael@0 | 138 | do_check_eq(l.available, chan.contentLength); |
michael@0 | 139 | |
michael@0 | 140 | run_next_test(); |
michael@0 | 141 | }), null); |
michael@0 | 142 | }); |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Verify that file locks are released when closing a synchronously |
michael@0 | 146 | * opened jar channel stream |
michael@0 | 147 | */ |
michael@0 | 148 | add_test(function testSyncCloseUnlocks() { |
michael@0 | 149 | var copy = tmpDir.clone(); |
michael@0 | 150 | copy.append(fileBase); |
michael@0 | 151 | file.copyTo(copy.parent, copy.leafName); |
michael@0 | 152 | |
michael@0 | 153 | var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip"; |
michael@0 | 154 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 155 | var stream = chan.open(); |
michael@0 | 156 | do_check_true(chan.contentLength > 0); |
michael@0 | 157 | stream.close(); |
michael@0 | 158 | |
michael@0 | 159 | // Drop any jar caches |
michael@0 | 160 | obs.notifyObservers(null, "chrome-flush-caches", null); |
michael@0 | 161 | |
michael@0 | 162 | try { |
michael@0 | 163 | copy.remove(false); |
michael@0 | 164 | } |
michael@0 | 165 | catch (ex) { |
michael@0 | 166 | do_throw(ex); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | run_next_test(); |
michael@0 | 170 | }); |
michael@0 | 171 | |
michael@0 | 172 | /** |
michael@0 | 173 | * Verify that file locks are released when closing an asynchronously |
michael@0 | 174 | * opened jar channel stream |
michael@0 | 175 | */ |
michael@0 | 176 | add_test(function testAsyncCloseUnlocks() { |
michael@0 | 177 | var copy = tmpDir.clone(); |
michael@0 | 178 | copy.append(fileBase); |
michael@0 | 179 | file.copyTo(copy.parent, copy.leafName); |
michael@0 | 180 | |
michael@0 | 181 | var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip"; |
michael@0 | 182 | var chan = ios.newChannel(uri, null, null); |
michael@0 | 183 | chan.asyncOpen(new Listener(function (l) { |
michael@0 | 184 | do_check_true(chan.contentLength > 0); |
michael@0 | 185 | |
michael@0 | 186 | // Drop any jar caches |
michael@0 | 187 | obs.notifyObservers(null, "chrome-flush-caches", null); |
michael@0 | 188 | |
michael@0 | 189 | try { |
michael@0 | 190 | copy.remove(false); |
michael@0 | 191 | } |
michael@0 | 192 | catch (ex) { |
michael@0 | 193 | do_throw(ex); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | run_next_test(); |
michael@0 | 197 | }), null); |
michael@0 | 198 | }); |
michael@0 | 199 | |
michael@0 | 200 | } // if !inChild |
michael@0 | 201 | |
michael@0 | 202 | function run_test() run_next_test(); |