1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // This testcase exercises the Protocol Proxy Service's async filter functionality
8 // run_filter_*() are entry points for each individual test.
12 var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();
15 * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP
18 function TestProtocolHandler() {}
19 TestProtocolHandler.prototype = {
20 QueryInterface: ChromeUtils.generateQI(["nsIProtocolHandler"]),
24 Ci.nsIProtocolHandler.URI_NOAUTH |
25 Ci.nsIProtocolHandler.URI_NORELATIVE |
26 Ci.nsIProtocolHandler.ALLOWS_PROXY |
27 Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD,
28 newChannel(uri, aLoadInfo) {
29 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
31 allowPort(port, scheme) {
36 function TestProtocolHandlerFactory() {}
37 TestProtocolHandlerFactory.prototype = {
39 return new TestProtocolHandler().QueryInterface(iid);
43 function register_test_protocol_handler() {
44 var reg = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
46 Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"),
47 "TestProtocolHandler",
48 "@mozilla.org/network/protocol;1?name=moz-test",
49 new TestProtocolHandlerFactory()
53 function check_proxy(pi, type, host, port, flags, timeout, hasNext) {
54 Assert.notEqual(pi, null);
55 Assert.equal(pi.type, type);
56 Assert.equal(pi.host, host);
57 Assert.equal(pi.port, port);
59 Assert.equal(pi.flags, flags);
62 Assert.equal(pi.failoverTimeout, timeout);
65 Assert.notEqual(pi.failoverProxy, null);
67 Assert.equal(pi.failoverProxy, null);
75 function TestFilter(type, host, port, flags, timeout, result) {
80 this._timeout = timeout;
81 this._result = result;
83 TestFilter.prototype = {
92 QueryInterface: ChromeUtils.generateQI(["nsIProtocolProxyFilter"]),
94 applyFilter(uri, pi, cb) {
95 if (this._result == THROW) {
96 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
99 var pi_tail = pps.newProxyInfo(
110 pi.failoverProxy = pi_tail;
115 if (this._result == ASYNC) {
117 cb.onProxyFilterResult(pi);
120 cb.onProxyFilterResult(pi);
125 function resolveCallback() {}
126 resolveCallback.prototype = {
129 QueryInterface: ChromeUtils.generateQI(["nsIProtocolProxyCallback"]),
131 onProxyAvailable(req, channel, pi, status) {
132 this.nextFunction(pi);
136 // ==============================================================
142 function run_filter_test1() {
143 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC);
144 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC);
145 pps.registerFilter(filter1, 20);
146 pps.registerFilter(filter2, 10);
148 var cb = new resolveCallback();
149 cb.nextFunction = filter_test1_1;
150 var channel = NetUtil.newChannel({
151 uri: "http://www.mozilla.org/",
152 loadUsingSystemPrincipal: true,
154 pps.asyncResolve(channel, 0, cb);
157 function filter_test1_1(pi) {
158 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
159 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
161 pps.unregisterFilter(filter2);
163 var cb = new resolveCallback();
164 cb.nextFunction = filter_test1_2;
165 var channel = NetUtil.newChannel({
166 uri: "http://www.mozilla.org/",
167 loadUsingSystemPrincipal: true,
169 pps.asyncResolve(channel, 0, cb);
172 function filter_test1_2(pi) {
173 check_proxy(pi, "http", "foo", 8080, 0, 10, false);
175 pps.unregisterFilter(filter1);
177 var cb = new resolveCallback();
178 cb.nextFunction = filter_test1_3;
179 var channel = NetUtil.newChannel({
180 uri: "http://www.mozilla.org/",
181 loadUsingSystemPrincipal: true,
183 pps.asyncResolve(channel, 0, cb);
186 function filter_test1_3(pi) {
187 Assert.equal(pi, null);
188 run_filter2_sync_async();
191 function run_filter2_sync_async() {
192 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC);
193 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC);
194 pps.registerFilter(filter1, 20);
195 pps.registerFilter(filter2, 10);
197 var cb = new resolveCallback();
198 cb.nextFunction = filter_test2_1;
199 var channel = NetUtil.newChannel({
200 uri: "http://www.mozilla.org/",
201 loadUsingSystemPrincipal: true,
203 pps.asyncResolve(channel, 0, cb);
206 function filter_test2_1(pi) {
207 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
208 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
210 pps.unregisterFilter(filter1);
211 pps.unregisterFilter(filter2);
213 run_filter3_async_sync();
216 function run_filter3_async_sync() {
217 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC);
218 filter2 = new TestFilter("http", "bar", 8090, 0, 10, SYNC);
219 pps.registerFilter(filter1, 20);
220 pps.registerFilter(filter2, 10);
222 var cb = new resolveCallback();
223 cb.nextFunction = filter_test3_1;
224 var channel = NetUtil.newChannel({
225 uri: "http://www.mozilla.org/",
226 loadUsingSystemPrincipal: true,
228 pps.asyncResolve(channel, 0, cb);
231 function filter_test3_1(pi) {
232 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
233 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
235 pps.unregisterFilter(filter1);
236 pps.unregisterFilter(filter2);
238 run_filter4_throwing_sync_sync();
241 function run_filter4_throwing_sync_sync() {
242 filter1 = new TestFilter("", "", 0, 0, 0, THROW);
243 filter2 = new TestFilter("http", "foo", 8080, 0, 10, SYNC);
244 filter3 = new TestFilter("http", "bar", 8090, 0, 10, SYNC);
245 pps.registerFilter(filter1, 20);
246 pps.registerFilter(filter2, 10);
247 pps.registerFilter(filter3, 5);
249 var cb = new resolveCallback();
250 cb.nextFunction = filter_test4_1;
251 var channel = NetUtil.newChannel({
252 uri: "http://www.mozilla2.org/",
253 loadUsingSystemPrincipal: true,
255 pps.asyncResolve(channel, 0, cb);
258 function filter_test4_1(pi) {
259 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
260 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
262 pps.unregisterFilter(filter1);
263 pps.unregisterFilter(filter2);
264 pps.unregisterFilter(filter3);
266 run_filter5_sync_sync_throwing();
269 function run_filter5_sync_sync_throwing() {
270 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC);
271 filter2 = new TestFilter("http", "bar", 8090, 0, 10, SYNC);
272 filter3 = new TestFilter("", "", 0, 0, 0, THROW);
273 pps.registerFilter(filter1, 20);
274 pps.registerFilter(filter2, 10);
275 pps.registerFilter(filter3, 5);
277 var cb = new resolveCallback();
278 cb.nextFunction = filter_test5_1;
279 var channel = NetUtil.newChannel({
280 uri: "http://www.mozilla.org/",
281 loadUsingSystemPrincipal: true,
283 pps.asyncResolve(channel, 0, cb);
286 function filter_test5_1(pi) {
287 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
288 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
290 pps.unregisterFilter(filter1);
291 pps.unregisterFilter(filter2);
292 pps.unregisterFilter(filter3);
294 run_filter5_2_throwing_async_async();
297 function run_filter5_2_throwing_async_async() {
298 filter1 = new TestFilter("", "", 0, 0, 0, THROW);
299 filter2 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC);
300 filter3 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC);
301 pps.registerFilter(filter1, 20);
302 pps.registerFilter(filter2, 10);
303 pps.registerFilter(filter3, 5);
305 var cb = new resolveCallback();
306 cb.nextFunction = filter_test5_2;
307 var channel = NetUtil.newChannel({
308 uri: "http://www.mozilla.org/",
309 loadUsingSystemPrincipal: true,
311 pps.asyncResolve(channel, 0, cb);
314 function filter_test5_2(pi) {
315 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
316 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
318 pps.unregisterFilter(filter1);
319 pps.unregisterFilter(filter2);
320 pps.unregisterFilter(filter3);
322 run_filter6_async_async_throwing();
325 function run_filter6_async_async_throwing() {
326 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC);
327 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC);
328 filter3 = new TestFilter("", "", 0, 0, 0, THROW);
329 pps.registerFilter(filter1, 20);
330 pps.registerFilter(filter2, 10);
331 pps.registerFilter(filter3, 5);
333 var cb = new resolveCallback();
334 cb.nextFunction = filter_test6_1;
335 var channel = NetUtil.newChannel({
336 uri: "http://www.mozilla.org/",
337 loadUsingSystemPrincipal: true,
339 pps.asyncResolve(channel, 0, cb);
342 function filter_test6_1(pi) {
343 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
344 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
346 pps.unregisterFilter(filter1);
347 pps.unregisterFilter(filter2);
348 pps.unregisterFilter(filter3);
350 run_filter7_async_throwing_async();
353 function run_filter7_async_throwing_async() {
354 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC);
355 filter2 = new TestFilter("", "", 0, 0, 0, THROW);
356 filter3 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC);
357 pps.registerFilter(filter1, 20);
358 pps.registerFilter(filter2, 10);
359 pps.registerFilter(filter3, 5);
361 var cb = new resolveCallback();
362 cb.nextFunction = filter_test7_1;
363 var channel = NetUtil.newChannel({
364 uri: "http://www.mozilla.org/",
365 loadUsingSystemPrincipal: true,
367 pps.asyncResolve(channel, 0, cb);
370 function filter_test7_1(pi) {
371 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
372 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
374 pps.unregisterFilter(filter1);
375 pps.unregisterFilter(filter2);
376 pps.unregisterFilter(filter3);
378 run_filter8_sync_throwing_sync();
381 function run_filter8_sync_throwing_sync() {
382 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC);
383 filter2 = new TestFilter("", "", 0, 0, 0, THROW);
384 filter3 = new TestFilter("http", "bar", 8090, 0, 10, SYNC);
385 pps.registerFilter(filter1, 20);
386 pps.registerFilter(filter2, 10);
387 pps.registerFilter(filter3, 5);
389 var cb = new resolveCallback();
390 cb.nextFunction = filter_test8_1;
391 var channel = NetUtil.newChannel({
392 uri: "http://www.mozilla.org/",
393 loadUsingSystemPrincipal: true,
395 pps.asyncResolve(channel, 0, cb);
398 function filter_test8_1(pi) {
399 check_proxy(pi, "http", "bar", 8090, 0, 10, true);
400 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
402 pps.unregisterFilter(filter1);
403 pps.unregisterFilter(filter2);
404 pps.unregisterFilter(filter3);
406 run_filter9_throwing();
409 function run_filter9_throwing() {
410 filter1 = new TestFilter("", "", 0, 0, 0, THROW);
411 pps.registerFilter(filter1, 20);
413 var cb = new resolveCallback();
414 cb.nextFunction = filter_test9_1;
415 var channel = NetUtil.newChannel({
416 uri: "http://www.mozilla.org/",
417 loadUsingSystemPrincipal: true,
419 pps.asyncResolve(channel, 0, cb);
422 function filter_test9_1(pi) {
423 Assert.equal(pi, null);
427 // =========================================
429 function run_test() {
430 register_test_protocol_handler();
432 // start of asynchronous test chain