1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@meer.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // This testcase exercises the Protocol Proxy Service
41 var ios
= Components
.classes
["@mozilla.org/network/io-service;1"]
42 .getService(Components
.interfaces
.nsIIOService
);
43 var pps
= Components
.classes
["@mozilla.org/network/protocol-proxy-service;1"]
47 * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP
50 function TestProtocolHandler() {
52 TestProtocolHandler
.prototype = {
53 QueryInterface: function(iid
) {
54 if (iid
.equals(Components
.interfaces
.nsIProtocolHandler
) ||
55 iid
.equals(Components
.interfaces
.nsISupports
))
57 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
61 protocolFlags
: Components
.interfaces
.nsIProtocolHandler
.URI_NOAUTH
|
62 Components
.interfaces
.nsIProtocolHandler
.URI_NORELATIVE
|
63 Components
.interfaces
.nsIProtocolHandler
.ALLOWS_PROXY
|
64 Components
.interfaces
.nsIProtocolHandler
.URI_DANGEROUS_TO_LOAD
,
65 newURI: function(spec
, originCharset
, baseURI
) {
66 var uri
= Components
.classes
["@mozilla.org/network/simple-uri;1"]
67 .createInstance(Components
.interfaces
.nsIURI
);
71 newChannel: function(uri
) {
72 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
74 allowPort: function(port
, scheme
) {
79 function TestProtocolHandlerFactory() {
81 TestProtocolHandlerFactory
.prototype = {
82 createInstance: function(delegate
, iid
) {
83 return new TestProtocolHandler().QueryInterface(iid
);
85 lockFactory: function(lock
) {
89 function register_test_protocol_handler() {
90 var reg
= Components
.manager
.QueryInterface(
91 Components
.interfaces
.nsIComponentRegistrar
);
92 reg
.registerFactory(Components
.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"),
93 "TestProtocolHandler",
94 "@mozilla.org/network/protocol;1?name=moz-test",
95 new TestProtocolHandlerFactory());
98 function check_proxy(pi
, type
, host
, port
, flags
, timeout
, hasNext
) {
99 do_check_neq(pi
, null);
100 do_check_eq(pi
.type
, type
);
101 do_check_eq(pi
.host
, host
);
102 do_check_eq(pi
.port
, port
);
104 do_check_eq(pi
.flags
, flags
);
106 do_check_eq(pi
.failoverTimeout
, timeout
);
108 do_check_neq(pi
.failoverProxy
, null);
110 do_check_eq(pi
.failoverProxy
, null);
113 function TestFilter(type
, host
, port
, flags
, timeout
) {
118 this._timeout
= timeout
;
120 TestFilter
.prototype = {
126 QueryInterface: function(iid
) {
127 if (iid
.equals(Components
.interfaces
.nsIProtocolProxyFilter
) ||
128 iid
.equals(Components
.interfaces
.nsISupports
))
130 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
132 applyFilter: function(pps
, uri
, pi
) {
133 var pi_tail
= pps
.newProxyInfo(this._type
, this._host
, this._port
,
134 this._flags
, this._timeout
, null);
136 pi
.failoverProxy
= pi_tail
;
143 function BasicFilter() {}
144 BasicFilter
.prototype = {
145 QueryInterface: function(iid
) {
146 if (iid
.equals(Components
.interfaces
.nsIProtocolProxyFilter
) ||
147 iid
.equals(Components
.interfaces
.nsISupports
))
149 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
151 applyFilter: function(pps
, uri
, pi
) {
152 return pps
.newProxyInfo("http", "localhost", 8080, 0, 10,
153 pps
.newProxyInfo("direct", "", -1, 0, 0, null));
157 function run_filter_test() {
158 var uri
= ios
.newURI("http://www.mozilla.org/", null, null);
160 // Verify initial state
162 var pi
= pps
.resolve(uri
, 0);
163 do_check_eq(pi
, null);
165 // Push a filter and verify the results
167 var filter1
= new BasicFilter();
168 var filter2
= new BasicFilter();
169 pps
.registerFilter(filter1
, 10);
170 pps
.registerFilter(filter2
, 20);
172 pi
= pps
.resolve(uri
, 0);
173 check_proxy(pi
, "http", "localhost", 8080, 0, 10, true);
174 check_proxy(pi
.failoverProxy
, "direct", "", -1, 0, 0, false);
176 pps
.unregisterFilter(filter2
);
177 pi
= pps
.resolve(uri
, 0);
178 check_proxy(pi
, "http", "localhost", 8080, 0, 10, true);
179 check_proxy(pi
.failoverProxy
, "direct", "", -1, 0, 0, false);
181 // Remove filter and verify that we return to the initial state
183 pps
.unregisterFilter(filter1
);
184 pi
= pps
.resolve(uri
, 0);
185 do_check_eq(pi
, null);
188 function run_filter_test2() {
189 var uri
= ios
.newURI("http://www.mozilla.org/", null, null);
191 // Verify initial state
193 var pi
= pps
.resolve(uri
, 0);
194 do_check_eq(pi
, null);
196 // Push a filter and verify the results
198 var filter1
= new TestFilter("http", "foo", 8080, 0, 10);
199 var filter2
= new TestFilter("http", "bar", 8090, 0, 10);
200 pps
.registerFilter(filter1
, 20);
201 pps
.registerFilter(filter2
, 10);
203 pi
= pps
.resolve(uri
, 0);
204 check_proxy(pi
, "http", "bar", 8090, 0, 10, true);
205 check_proxy(pi
.failoverProxy
, "http", "foo", 8080, 0, 10, false);
207 pps
.unregisterFilter(filter2
);
208 pi
= pps
.resolve(uri
, 0);
209 check_proxy(pi
, "http", "foo", 8080, 0, 10, false);
211 // Remove filter and verify that we return to the initial state
213 pps
.unregisterFilter(filter1
);
214 pi
= pps
.resolve(uri
, 0);
215 do_check_eq(pi
, null);
218 function run_pref_test() {
219 var uri
= ios
.newURI("http://www.mozilla.org/", null, null);
221 var prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
222 .getService(Components
.interfaces
.nsIPrefBranch
);
224 // Verify 'direct' setting
226 prefs
.setIntPref("network.proxy.type", 0);
228 var pi
= pps
.resolve(uri
, 0);
229 do_check_eq(pi
, null);
231 // Verify 'manual' setting
233 prefs
.setIntPref("network.proxy.type", 1);
235 // nothing yet configured
236 pi
= pps
.resolve(uri
, 0);
237 do_check_eq(pi
, null);
239 // try HTTP configuration
240 prefs
.setCharPref("network.proxy.http", "foopy");
241 prefs
.setIntPref("network.proxy.http_port", 8080);
243 pi
= pps
.resolve(uri
, 0);
244 check_proxy(pi
, "http", "foopy", 8080, 0, -1, false);
246 prefs
.setCharPref("network.proxy.http", "");
247 prefs
.setIntPref("network.proxy.http_port", 0);
249 // try SOCKS configuration
250 prefs
.setCharPref("network.proxy.socks", "barbar");
251 prefs
.setIntPref("network.proxy.socks_port", 1203);
253 pi
= pps
.resolve(uri
, 0);
254 check_proxy(pi
, "socks", "barbar", 1203, 0, -1, false);
257 function run_protocol_handler_test() {
258 var uri
= ios
.newURI("moz-test:foopy", null, null);
260 var pi
= pps
.resolve(uri
, 0);
261 do_check_eq(pi
, null);
264 function TestResolveCallback() {
266 TestResolveCallback
.prototype = {
268 function TestResolveCallback_QueryInterface(iid
) {
269 if (iid
.equals(Components
.interfaces
.nsIProtocolProxyCallback
) ||
270 iid
.equals(Components
.interfaces
.nsISupports
))
272 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
276 function TestResolveCallback_onProxyAvailable(req
, uri
, pi
, status
) {
277 dump("*** uri=" + uri
.spec
+ ", status=" + status
+ "\n");
279 do_check_neq(req
, null);
280 do_check_neq(uri
, null);
281 do_check_eq(status
, 0);
282 do_check_neq(pi
, null);
284 check_proxy(pi
, "http", "foopy", 8080, 0, -1, true);
285 check_proxy(pi
.failoverProxy
, "direct", "", -1, -1, -1, false);
287 // verify direct query now that we know the PAC file is loaded
288 pi
= pps
.resolve(ios
.newURI("http://bazbat.com/", null, null), 0);
289 do_check_neq(pi
, null);
290 check_proxy(pi
, "http", "foopy", 8080, 0, -1, true);
291 check_proxy(pi
.failoverProxy
, "direct", "", -1, -1, -1, false);
293 run_protocol_handler_test();
295 var prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
296 .getService(Components
.interfaces
.nsIPrefBranch
);
297 prefs
.setCharPref("network.proxy.autoconfig_url", "");
298 prefs
.setIntPref("network.proxy.type", 0);
300 run_test_continued();
305 function run_pac_test() {
306 var pac
= 'data:text/plain,' +
307 'function FindProxyForURL(url, host) {' +
308 ' return "PROXY foopy:8080; DIRECT";' +
310 var uri
= ios
.newURI("http://www.mozilla.org/", null, null);
312 var prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
313 .getService(Components
.interfaces
.nsIPrefBranch
);
317 prefs
.setIntPref("network.proxy.type", 2);
318 prefs
.setCharPref("network.proxy.autoconfig_url", pac
);
320 // Test it out (we expect an "unknown" result since the PAC load is async)
321 var pi
= pps
.resolve(uri
, 0);
322 do_check_neq(pi
, null);
323 do_check_eq(pi
.type
, "unknown");
325 // We expect the NON_BLOCKING flag to trigger an exception here since
326 // we have configured the PPS to use PAC.
327 var hit_exception
= false;
329 pps
.resolve(uri
, pps
.RESOLVE_NON_BLOCKING
);
331 hit_exception
= true;
333 do_check_eq(hit_exception
, true);
335 var req
= pps
.asyncResolve(uri
, 0, new TestResolveCallback());
339 function TestResolveCancelationCallback() {
341 TestResolveCancelationCallback
.prototype = {
343 function TestResolveCallback_QueryInterface(iid
) {
344 if (iid
.equals(Components
.interfaces
.nsIProtocolProxyCallback
) ||
345 iid
.equals(Components
.interfaces
.nsISupports
))
347 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
351 function TestResolveCancelationCallback_onProxyAvailable(req
, uri
, pi
, status
) {
352 dump("*** uri=" + uri
.spec
+ ", status=" + status
+ "\n");
354 do_check_neq(req
, null);
355 do_check_neq(uri
, null);
356 do_check_eq(status
, Components
.results
.NS_ERROR_ABORT
);
357 do_check_eq(pi
, null);
359 var prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
360 .getService(Components
.interfaces
.nsIPrefBranch
);
361 prefs
.setCharPref("network.proxy.autoconfig_url", "");
362 prefs
.setIntPref("network.proxy.type", 0);
364 run_test_continued_2();
369 function run_pac_cancel_test() {
370 var uri
= ios
.newURI("http://www.mozilla.org/", null, null);
373 var pac
= 'data:text/plain,' +
374 'function FindProxyForURL(url, host) {' +
375 ' return "PROXY foopy:8080; DIRECT";' +
377 var prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
378 .getService(Components
.interfaces
.nsIPrefBranch
);
379 prefs
.setIntPref("network.proxy.type", 2);
380 prefs
.setCharPref("network.proxy.autoconfig_url", pac
);
382 var req
= pps
.asyncResolve(uri
, 0, new TestResolveCancelationCallback());
383 req
.cancel(Components
.results
.NS_ERROR_ABORT
);
387 function run_test() {
388 register_test_protocol_handler();
393 // additional tests may be added to run_test_continued
396 function run_test_continued() {
397 run_pac_cancel_test();
398 // additional tests may be added to run_test_continued_2
401 function run_test_continued_2() {