|
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 /* 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/. */ |
|
6 |
|
7 // This testcase exercises the Protocol Proxy Service |
|
8 |
|
9 // These are the major sub tests: |
|
10 // run_filter_test(); |
|
11 // run_filter_test2() |
|
12 // run_filter_test3() |
|
13 // run_pref_test(); |
|
14 // run_pac_test(); |
|
15 // run_pac_cancel_test(); |
|
16 // run_proxy_host_filters_test(); |
|
17 // run_myipaddress_test(); |
|
18 // run_failed_script_test(); |
|
19 // run_isresolvable_test(); |
|
20 |
|
21 var ios = Components.classes["@mozilla.org/network/io-service;1"] |
|
22 .getService(Components.interfaces.nsIIOService); |
|
23 var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"] |
|
24 .getService(); |
|
25 var prefs = Components.classes["@mozilla.org/preferences-service;1"] |
|
26 .getService(Components.interfaces.nsIPrefBranch); |
|
27 |
|
28 /** |
|
29 * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP |
|
30 * proxying. |
|
31 */ |
|
32 function TestProtocolHandler() { |
|
33 } |
|
34 TestProtocolHandler.prototype = { |
|
35 QueryInterface: function(iid) { |
|
36 if (iid.equals(Components.interfaces.nsIProtocolHandler) || |
|
37 iid.equals(Components.interfaces.nsISupports)) |
|
38 return this; |
|
39 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
40 }, |
|
41 scheme: "moz-test", |
|
42 defaultPort: -1, |
|
43 protocolFlags: Components.interfaces.nsIProtocolHandler.URI_NOAUTH | |
|
44 Components.interfaces.nsIProtocolHandler.URI_NORELATIVE | |
|
45 Components.interfaces.nsIProtocolHandler.ALLOWS_PROXY | |
|
46 Components.interfaces.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD, |
|
47 newURI: function(spec, originCharset, baseURI) { |
|
48 var uri = Components.classes["@mozilla.org/network/simple-uri;1"] |
|
49 .createInstance(Components.interfaces.nsIURI); |
|
50 uri.spec = spec; |
|
51 return uri; |
|
52 }, |
|
53 newChannel: function(uri) { |
|
54 throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
|
55 }, |
|
56 allowPort: function(port, scheme) { |
|
57 return true; |
|
58 } |
|
59 }; |
|
60 |
|
61 function TestProtocolHandlerFactory() { |
|
62 } |
|
63 TestProtocolHandlerFactory.prototype = { |
|
64 createInstance: function(delegate, iid) { |
|
65 return new TestProtocolHandler().QueryInterface(iid); |
|
66 }, |
|
67 lockFactory: function(lock) { |
|
68 } |
|
69 }; |
|
70 |
|
71 function register_test_protocol_handler() { |
|
72 var reg = Components.manager.QueryInterface( |
|
73 Components.interfaces.nsIComponentRegistrar); |
|
74 reg.registerFactory(Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"), |
|
75 "TestProtocolHandler", |
|
76 "@mozilla.org/network/protocol;1?name=moz-test", |
|
77 new TestProtocolHandlerFactory()); |
|
78 } |
|
79 |
|
80 function check_proxy(pi, type, host, port, flags, timeout, hasNext) { |
|
81 do_check_neq(pi, null); |
|
82 do_check_eq(pi.type, type); |
|
83 do_check_eq(pi.host, host); |
|
84 do_check_eq(pi.port, port); |
|
85 if (flags != -1) |
|
86 do_check_eq(pi.flags, flags); |
|
87 if (timeout != -1) |
|
88 do_check_eq(pi.failoverTimeout, timeout); |
|
89 if (hasNext) |
|
90 do_check_neq(pi.failoverProxy, null); |
|
91 else |
|
92 do_check_eq(pi.failoverProxy, null); |
|
93 } |
|
94 |
|
95 function TestFilter(type, host, port, flags, timeout) { |
|
96 this._type = type; |
|
97 this._host = host; |
|
98 this._port = port; |
|
99 this._flags = flags; |
|
100 this._timeout = timeout; |
|
101 } |
|
102 TestFilter.prototype = { |
|
103 _type: "", |
|
104 _host: "", |
|
105 _port: -1, |
|
106 _flags: 0, |
|
107 _timeout: 0, |
|
108 QueryInterface: function(iid) { |
|
109 if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || |
|
110 iid.equals(Components.interfaces.nsISupports)) |
|
111 return this; |
|
112 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
113 }, |
|
114 applyFilter: function(pps, uri, pi) { |
|
115 var pi_tail = pps.newProxyInfo(this._type, this._host, this._port, |
|
116 this._flags, this._timeout, null); |
|
117 if (pi) |
|
118 pi.failoverProxy = pi_tail; |
|
119 else |
|
120 pi = pi_tail; |
|
121 return pi; |
|
122 } |
|
123 }; |
|
124 |
|
125 function BasicFilter() {} |
|
126 BasicFilter.prototype = { |
|
127 QueryInterface: function(iid) { |
|
128 if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || |
|
129 iid.equals(Components.interfaces.nsISupports)) |
|
130 return this; |
|
131 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
132 }, |
|
133 applyFilter: function(pps, uri, pi) { |
|
134 return pps.newProxyInfo("http", "localhost", 8080, 0, 10, |
|
135 pps.newProxyInfo("direct", "", -1, 0, 0, null)); |
|
136 } |
|
137 }; |
|
138 |
|
139 function resolveCallback() { } |
|
140 resolveCallback.prototype = { |
|
141 nextFunction: null, |
|
142 |
|
143 QueryInterface : function (iid) { |
|
144 const interfaces = [Components.interfaces.nsIProtocolProxyCallback, |
|
145 Components.interfaces.nsISupports]; |
|
146 if (!interfaces.some( function(v) { return iid.equals(v) } )) |
|
147 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
148 return this; |
|
149 }, |
|
150 |
|
151 onProxyAvailable : function (req, uri, pi, status) { |
|
152 this.nextFunction(pi); |
|
153 } |
|
154 }; |
|
155 |
|
156 function run_filter_test() { |
|
157 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
158 |
|
159 // Verify initial state |
|
160 var cb = new resolveCallback(); |
|
161 cb.nextFunction = filter_test0_1; |
|
162 var req = pps.asyncResolve(uri, 0, cb); |
|
163 } |
|
164 |
|
165 var filter01; |
|
166 var filter02; |
|
167 |
|
168 function filter_test0_1(pi) { |
|
169 do_check_eq(pi, null); |
|
170 |
|
171 // Push a filter and verify the results |
|
172 |
|
173 filter01 = new BasicFilter(); |
|
174 filter02 = new BasicFilter(); |
|
175 pps.registerFilter(filter01, 10); |
|
176 pps.registerFilter(filter02, 20); |
|
177 |
|
178 var cb = new resolveCallback(); |
|
179 cb.nextFunction = filter_test0_2; |
|
180 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
181 var req = pps.asyncResolve(uri, 0, cb); |
|
182 } |
|
183 |
|
184 function filter_test0_2(pi) |
|
185 { |
|
186 check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
|
187 check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
|
188 |
|
189 pps.unregisterFilter(filter02); |
|
190 |
|
191 var cb = new resolveCallback(); |
|
192 cb.nextFunction = filter_test0_3; |
|
193 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
194 var req = pps.asyncResolve(uri, 0, cb); |
|
195 } |
|
196 |
|
197 function filter_test0_3(pi) |
|
198 { |
|
199 check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
|
200 check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
|
201 |
|
202 // Remove filter and verify that we return to the initial state |
|
203 |
|
204 pps.unregisterFilter(filter01); |
|
205 |
|
206 var cb = new resolveCallback(); |
|
207 cb.nextFunction = filter_test0_4; |
|
208 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
209 var req = pps.asyncResolve(uri, 0, cb); |
|
210 } |
|
211 |
|
212 function filter_test0_4(pi) |
|
213 { |
|
214 do_check_eq(pi, null); |
|
215 run_filter_test2(); |
|
216 } |
|
217 |
|
218 var filter11; |
|
219 var filter12; |
|
220 |
|
221 function run_filter_test2() { |
|
222 // Push a filter and verify the results |
|
223 |
|
224 filter11 = new TestFilter("http", "foo", 8080, 0, 10); |
|
225 filter12 = new TestFilter("http", "bar", 8090, 0, 10); |
|
226 pps.registerFilter(filter11, 20); |
|
227 pps.registerFilter(filter12, 10); |
|
228 |
|
229 var cb = new resolveCallback(); |
|
230 cb.nextFunction = filter_test1_1; |
|
231 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
232 var req = pps.asyncResolve(uri, 0, cb); |
|
233 } |
|
234 |
|
235 function filter_test1_1(pi) { |
|
236 check_proxy(pi, "http", "bar", 8090, 0, 10, true); |
|
237 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); |
|
238 |
|
239 pps.unregisterFilter(filter12); |
|
240 |
|
241 var cb = new resolveCallback(); |
|
242 cb.nextFunction = filter_test1_2; |
|
243 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
244 var req = pps.asyncResolve(uri, 0, cb); |
|
245 } |
|
246 |
|
247 function filter_test1_2(pi) { |
|
248 check_proxy(pi, "http", "foo", 8080, 0, 10, false); |
|
249 |
|
250 // Remove filter and verify that we return to the initial state |
|
251 |
|
252 pps.unregisterFilter(filter11); |
|
253 |
|
254 var cb = new resolveCallback(); |
|
255 cb.nextFunction = filter_test1_3; |
|
256 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
257 var req = pps.asyncResolve(uri, 0, cb); |
|
258 } |
|
259 |
|
260 function filter_test1_3(pi) { |
|
261 do_check_eq(pi, null); |
|
262 run_filter_test3(); |
|
263 } |
|
264 |
|
265 var filter_3_1; |
|
266 |
|
267 function run_filter_test3() { |
|
268 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
269 |
|
270 // Push a filter and verify the results asynchronously |
|
271 |
|
272 filter_3_1 = new TestFilter("http", "foo", 8080, 0, 10); |
|
273 pps.registerFilter(filter_3_1, 20); |
|
274 |
|
275 var cb = new resolveCallback(); |
|
276 cb.nextFunction = filter_test3_1; |
|
277 var req = pps.asyncResolve(uri, 0, cb); |
|
278 } |
|
279 |
|
280 function filter_test3_1(pi) { |
|
281 check_proxy(pi, "http", "foo", 8080, 0, 10, false); |
|
282 pps.unregisterFilter(filter_3_1); |
|
283 run_pref_test(); |
|
284 } |
|
285 |
|
286 function run_pref_test() { |
|
287 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
288 |
|
289 // Verify 'direct' setting |
|
290 |
|
291 prefs.setIntPref("network.proxy.type", 0); |
|
292 |
|
293 var cb = new resolveCallback(); |
|
294 cb.nextFunction = pref_test1_1; |
|
295 var req = pps.asyncResolve(uri, 0, cb); |
|
296 } |
|
297 |
|
298 function pref_test1_1(pi) |
|
299 { |
|
300 do_check_eq(pi, null); |
|
301 |
|
302 // Verify 'manual' setting |
|
303 prefs.setIntPref("network.proxy.type", 1); |
|
304 |
|
305 var cb = new resolveCallback(); |
|
306 cb.nextFunction = pref_test1_2; |
|
307 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
308 var req = pps.asyncResolve(uri, 0, cb); |
|
309 } |
|
310 |
|
311 function pref_test1_2(pi) |
|
312 { |
|
313 // nothing yet configured |
|
314 do_check_eq(pi, null); |
|
315 |
|
316 // try HTTP configuration |
|
317 prefs.setCharPref("network.proxy.http", "foopy"); |
|
318 prefs.setIntPref("network.proxy.http_port", 8080); |
|
319 |
|
320 var cb = new resolveCallback(); |
|
321 cb.nextFunction = pref_test1_3; |
|
322 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
323 var req = pps.asyncResolve(uri, 0, cb); |
|
324 } |
|
325 |
|
326 function pref_test1_3(pi) |
|
327 { |
|
328 check_proxy(pi, "http", "foopy", 8080, 0, -1, false); |
|
329 |
|
330 prefs.setCharPref("network.proxy.http", ""); |
|
331 prefs.setIntPref("network.proxy.http_port", 0); |
|
332 |
|
333 // try SOCKS configuration |
|
334 prefs.setCharPref("network.proxy.socks", "barbar"); |
|
335 prefs.setIntPref("network.proxy.socks_port", 1203); |
|
336 |
|
337 var cb = new resolveCallback(); |
|
338 cb.nextFunction = pref_test1_4; |
|
339 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
340 var req = pps.asyncResolve(uri, 0, cb); |
|
341 } |
|
342 |
|
343 function pref_test1_4(pi) |
|
344 { |
|
345 check_proxy(pi, "socks", "barbar", 1203, 0, -1, false); |
|
346 run_pac_test(); |
|
347 } |
|
348 |
|
349 function run_protocol_handler_test() { |
|
350 var uri = ios.newURI("moz-test:foopy", null, null); |
|
351 |
|
352 var cb = new resolveCallback(); |
|
353 cb.nextFunction = protocol_handler_test_1; |
|
354 var req = pps.asyncResolve(uri, 0, cb); |
|
355 } |
|
356 |
|
357 function protocol_handler_test_1(pi) |
|
358 { |
|
359 do_check_eq(pi, null); |
|
360 prefs.setCharPref("network.proxy.autoconfig_url", ""); |
|
361 prefs.setIntPref("network.proxy.type", 0); |
|
362 |
|
363 run_pac_cancel_test(); |
|
364 } |
|
365 |
|
366 function TestResolveCallback() { |
|
367 } |
|
368 TestResolveCallback.prototype = { |
|
369 QueryInterface: |
|
370 function TestResolveCallback_QueryInterface(iid) { |
|
371 if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || |
|
372 iid.equals(Components.interfaces.nsISupports)) |
|
373 return this; |
|
374 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
375 }, |
|
376 |
|
377 onProxyAvailable: |
|
378 function TestResolveCallback_onProxyAvailable(req, uri, pi, status) { |
|
379 dump("*** uri=" + uri.spec + ", status=" + status + "\n"); |
|
380 |
|
381 do_check_neq(req, null); |
|
382 do_check_neq(uri, null); |
|
383 do_check_eq(status, 0); |
|
384 do_check_neq(pi, null); |
|
385 |
|
386 check_proxy(pi, "http", "foopy", 8080, 0, -1, true); |
|
387 check_proxy(pi.failoverProxy, "direct", "", -1, -1, -1, false); |
|
388 |
|
389 run_protocol_handler_test(); |
|
390 } |
|
391 }; |
|
392 |
|
393 function run_pac_test() { |
|
394 var pac = 'data:text/plain,' + |
|
395 'function FindProxyForURL(url, host) {' + |
|
396 ' return "PROXY foopy:8080; DIRECT";' + |
|
397 '}'; |
|
398 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
399 |
|
400 // Configure PAC |
|
401 |
|
402 prefs.setIntPref("network.proxy.type", 2); |
|
403 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
404 |
|
405 var req = pps.asyncResolve(uri, 0, new TestResolveCallback()); |
|
406 } |
|
407 |
|
408 function TestResolveCancelationCallback() { |
|
409 } |
|
410 TestResolveCancelationCallback.prototype = { |
|
411 QueryInterface: |
|
412 function TestResolveCallback_QueryInterface(iid) { |
|
413 if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || |
|
414 iid.equals(Components.interfaces.nsISupports)) |
|
415 return this; |
|
416 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
417 }, |
|
418 |
|
419 onProxyAvailable: |
|
420 function TestResolveCancelationCallback_onProxyAvailable(req, uri, pi, status) { |
|
421 dump("*** uri=" + uri.spec + ", status=" + status + "\n"); |
|
422 |
|
423 do_check_neq(req, null); |
|
424 do_check_neq(uri, null); |
|
425 do_check_eq(status, Components.results.NS_ERROR_ABORT); |
|
426 do_check_eq(pi, null); |
|
427 |
|
428 prefs.setCharPref("network.proxy.autoconfig_url", ""); |
|
429 prefs.setIntPref("network.proxy.type", 0); |
|
430 |
|
431 run_proxy_host_filters_test(); |
|
432 } |
|
433 }; |
|
434 |
|
435 function run_pac_cancel_test() { |
|
436 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
437 |
|
438 // Configure PAC |
|
439 var pac = 'data:text/plain,' + |
|
440 'function FindProxyForURL(url, host) {' + |
|
441 ' return "PROXY foopy:8080; DIRECT";' + |
|
442 '}'; |
|
443 prefs.setIntPref("network.proxy.type", 2); |
|
444 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
445 |
|
446 var req = pps.asyncResolve(uri, 0, new TestResolveCancelationCallback()); |
|
447 req.cancel(Components.results.NS_ERROR_ABORT); |
|
448 } |
|
449 |
|
450 var hostList; |
|
451 var hostIDX; |
|
452 var bShouldBeFiltered; |
|
453 var hostNextFX; |
|
454 |
|
455 function check_host_filters(hl, shouldBe, nextFX) { |
|
456 hostList = hl; |
|
457 hostIDX = 0; |
|
458 bShouldBeFiltered = shouldBe; |
|
459 hostNextFX = nextFX; |
|
460 |
|
461 if (hostList.length > hostIDX) |
|
462 check_host_filter(hostIDX); |
|
463 } |
|
464 |
|
465 function check_host_filters_cb() |
|
466 { |
|
467 hostIDX++; |
|
468 if (hostList.length > hostIDX) |
|
469 check_host_filter(hostIDX); |
|
470 else |
|
471 hostNextFX(); |
|
472 } |
|
473 |
|
474 function check_host_filter(i) { |
|
475 var uri; |
|
476 dump("*** uri=" + hostList[i] + " bShouldBeFiltered=" + bShouldBeFiltered + "\n"); |
|
477 uri = ios.newURI(hostList[i], null, null); |
|
478 |
|
479 var cb = new resolveCallback(); |
|
480 cb.nextFunction = host_filter_cb; |
|
481 var req = pps.asyncResolve(uri, 0, cb); |
|
482 } |
|
483 |
|
484 function host_filter_cb(proxy) |
|
485 { |
|
486 if (bShouldBeFiltered) { |
|
487 do_check_eq(proxy, null); |
|
488 } else { |
|
489 do_check_neq(proxy, null); |
|
490 // Just to be sure, let's check that the proxy is correct |
|
491 // - this should match the proxy setup in the calling function |
|
492 check_proxy(proxy, "http", "foopy", 8080, 0, -1, false); |
|
493 } |
|
494 check_host_filters_cb(); |
|
495 } |
|
496 |
|
497 |
|
498 // Verify that hists in the host filter list are not proxied |
|
499 // refers to "network.proxy.no_proxies_on" |
|
500 |
|
501 var uriStrUseProxyList; |
|
502 var uriStrUseProxyList; |
|
503 var hostFilterList; |
|
504 |
|
505 function run_proxy_host_filters_test() { |
|
506 // Get prefs object from DOM |
|
507 // Setup a basic HTTP proxy configuration |
|
508 // - pps.resolve() needs this to return proxy info for non-filtered hosts |
|
509 prefs.setIntPref("network.proxy.type", 1); |
|
510 prefs.setCharPref("network.proxy.http", "foopy"); |
|
511 prefs.setIntPref("network.proxy.http_port", 8080); |
|
512 |
|
513 // Setup host filter list string for "no_proxies_on" |
|
514 hostFilterList = "www.mozilla.org, www.google.com, www.apple.com, " |
|
515 + ".domain, .domain2.org" |
|
516 prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList); |
|
517 do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), hostFilterList); |
|
518 |
|
519 var rv; |
|
520 // Check the hosts that should be filtered out |
|
521 uriStrFilterList = [ "http://www.mozilla.org/", |
|
522 "http://www.google.com/", |
|
523 "http://www.apple.com/", |
|
524 "http://somehost.domain/", |
|
525 "http://someotherhost.domain/", |
|
526 "http://somehost.domain2.org/", |
|
527 "http://somehost.subdomain.domain2.org/" ]; |
|
528 check_host_filters(uriStrFilterList, true, host_filters_1); |
|
529 } |
|
530 |
|
531 function host_filters_1() |
|
532 { |
|
533 // Check the hosts that should be proxied |
|
534 uriStrUseProxyList = [ "http://www.mozilla.com/", |
|
535 "http://mail.google.com/", |
|
536 "http://somehost.domain.co.uk/", |
|
537 "http://somelocalhost/" ]; |
|
538 check_host_filters(uriStrUseProxyList, false, host_filters_2); |
|
539 } |
|
540 |
|
541 function host_filters_2() |
|
542 { |
|
543 // Set no_proxies_on to include local hosts |
|
544 prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList + ", <local>"); |
|
545 do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), |
|
546 hostFilterList + ", <local>"); |
|
547 // Amend lists - move local domain to filtered list |
|
548 uriStrFilterList.push(uriStrUseProxyList.pop()); |
|
549 check_host_filters(uriStrFilterList, true, host_filters_3); |
|
550 } |
|
551 |
|
552 function host_filters_3() |
|
553 { |
|
554 check_host_filters(uriStrUseProxyList, false, host_filters_4); |
|
555 } |
|
556 |
|
557 function host_filters_4() |
|
558 { |
|
559 // Cleanup |
|
560 prefs.setCharPref("network.proxy.no_proxies_on", ""); |
|
561 do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), ""); |
|
562 |
|
563 run_myipaddress_test(); |
|
564 } |
|
565 |
|
566 function run_myipaddress_test() |
|
567 { |
|
568 // This test makes sure myIpAddress() comes up with some valid |
|
569 // IP address other than localhost. The DUT must be configured with |
|
570 // an Internet route for this to work - though no Internet traffic |
|
571 // should be created. |
|
572 |
|
573 var pac = 'data:text/plain,' + |
|
574 'function FindProxyForURL(url, host) {' + |
|
575 ' return "PROXY " + myIpAddress() + ":1234";' + |
|
576 '}'; |
|
577 |
|
578 // no traffic to this IP is ever sent, it is just a public IP that |
|
579 // does not require DNS to determine a route. |
|
580 var uri = ios.newURI("http://192.0.43.10/", null, null); |
|
581 |
|
582 prefs.setIntPref("network.proxy.type", 2); |
|
583 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
584 |
|
585 var cb = new resolveCallback(); |
|
586 cb.nextFunction = myipaddress_callback; |
|
587 var req = pps.asyncResolve(uri, 0, cb); |
|
588 } |
|
589 |
|
590 function myipaddress_callback(pi) |
|
591 { |
|
592 do_check_neq(pi, null); |
|
593 do_check_eq(pi.type, "http"); |
|
594 do_check_eq(pi.port, 1234); |
|
595 |
|
596 // make sure we didn't return localhost |
|
597 do_check_neq(pi.host, null); |
|
598 do_check_neq(pi.host, "127.0.0.1"); |
|
599 do_check_neq(pi.host, "::1"); |
|
600 |
|
601 run_myipaddress_test_2(); |
|
602 } |
|
603 |
|
604 function run_myipaddress_test_2() |
|
605 { |
|
606 // test that myIPAddress() can be used outside of the scope of |
|
607 // FindProxyForURL(). bug 829646. |
|
608 |
|
609 var pac = 'data:text/plain,' + |
|
610 'var myaddr = myIpAddress(); ' + |
|
611 'function FindProxyForURL(url, host) {' + |
|
612 ' return "PROXY " + myaddr + ":5678";' + |
|
613 '}'; |
|
614 |
|
615 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
616 prefs.setIntPref("network.proxy.type", 2); |
|
617 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
618 |
|
619 var cb = new resolveCallback(); |
|
620 cb.nextFunction = myipaddress2_callback; |
|
621 var req = pps.asyncResolve(uri, 0, cb); |
|
622 } |
|
623 |
|
624 function myipaddress2_callback(pi) |
|
625 { |
|
626 do_check_neq(pi, null); |
|
627 do_check_eq(pi.type, "http"); |
|
628 do_check_eq(pi.port, 5678); |
|
629 |
|
630 // make sure we didn't return localhost |
|
631 do_check_neq(pi.host, null); |
|
632 do_check_neq(pi.host, "127.0.0.1"); |
|
633 do_check_neq(pi.host, "::1"); |
|
634 |
|
635 run_failed_script_test(); |
|
636 } |
|
637 |
|
638 function run_failed_script_test() |
|
639 { |
|
640 // test to make sure we go direct with invalid PAC |
|
641 var pac = 'data:text/plain,' + |
|
642 '\nfor(;\n'; |
|
643 |
|
644 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
645 |
|
646 prefs.setIntPref("network.proxy.type", 2); |
|
647 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
648 |
|
649 var cb = new resolveCallback(); |
|
650 cb.nextFunction = failed_script_callback; |
|
651 var req = pps.asyncResolve(uri, 0, cb); |
|
652 } |
|
653 |
|
654 var directFilter; |
|
655 |
|
656 function failed_script_callback(pi) |
|
657 { |
|
658 // we should go direct |
|
659 do_check_eq(pi, null); |
|
660 |
|
661 // test that we honor filters when configured to go direct |
|
662 prefs.setIntPref("network.proxy.type", 0); |
|
663 directFilter = new TestFilter("http", "127.0.0.1", 7246, 0, 0); |
|
664 pps.registerFilter(directFilter, 10); |
|
665 |
|
666 // test that on-modify-request contains the proxy info too |
|
667 var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); |
|
668 obs = obs.QueryInterface(Components.interfaces.nsIObserverService); |
|
669 obs.addObserver(directFilterListener, "http-on-modify-request", false); |
|
670 |
|
671 var chan = ios.newChannel("http://127.0.0.1:7247", "", null); |
|
672 chan.asyncOpen(directFilterListener, chan); |
|
673 } |
|
674 |
|
675 var directFilterListener = { |
|
676 onModifyRequestCalled : false, |
|
677 |
|
678 onStartRequest: function test_onStart(request, ctx) { }, |
|
679 onDataAvailable: function test_OnData() { }, |
|
680 |
|
681 onStopRequest: function test_onStop(request, ctx, status) { |
|
682 // check on the PI from the channel itself |
|
683 ctx.QueryInterface(Components.interfaces.nsIProxiedChannel); |
|
684 check_proxy(ctx.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); |
|
685 pps.unregisterFilter(directFilter); |
|
686 |
|
687 // check on the PI from on-modify-request |
|
688 do_check_true(this.onModifyRequestCalled); |
|
689 var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); |
|
690 obs = obs.QueryInterface(Components.interfaces.nsIObserverService); |
|
691 obs.removeObserver(this, "http-on-modify-request"); |
|
692 |
|
693 run_isresolvable_test(); |
|
694 }, |
|
695 |
|
696 observe: function(subject, topic, data) { |
|
697 if (topic === "http-on-modify-request" && |
|
698 subject instanceof Components.interfaces.nsIHttpChannel && |
|
699 subject instanceof Components.interfaces.nsIProxiedChannel) { |
|
700 check_proxy(subject.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); |
|
701 this.onModifyRequestCalled = true; |
|
702 } |
|
703 } |
|
704 }; |
|
705 |
|
706 function run_isresolvable_test() |
|
707 { |
|
708 // test a non resolvable host in the pac file |
|
709 |
|
710 var pac = 'data:text/plain,' + |
|
711 'function FindProxyForURL(url, host) {' + |
|
712 ' if (isResolvable("nonexistant.lan"))' + |
|
713 ' return "DIRECT";' + |
|
714 ' return "PROXY 127.0.0.1:1234";' + |
|
715 '}'; |
|
716 |
|
717 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
718 |
|
719 prefs.setIntPref("network.proxy.type", 2); |
|
720 prefs.setCharPref("network.proxy.autoconfig_url", pac); |
|
721 |
|
722 var cb = new resolveCallback(); |
|
723 cb.nextFunction = isresolvable_callback; |
|
724 var req = pps.asyncResolve(uri, 0, cb); |
|
725 } |
|
726 |
|
727 function isresolvable_callback(pi) |
|
728 { |
|
729 do_check_neq(pi, null); |
|
730 do_check_eq(pi.type, "http"); |
|
731 do_check_eq(pi.port, 1234); |
|
732 do_check_eq(pi.host, "127.0.0.1"); |
|
733 |
|
734 prefs.setIntPref("network.proxy.type", 0); |
|
735 do_test_finished(); |
|
736 } |
|
737 |
|
738 function run_deprecated_sync_test() |
|
739 { |
|
740 var uri = ios.newURI("http://www.mozilla.org/", null, null); |
|
741 |
|
742 pps.QueryInterface(Components.interfaces.nsIProtocolProxyService2); |
|
743 |
|
744 // Verify initial state |
|
745 var pi = pps.deprecatedBlockingResolve(uri, 0); |
|
746 do_check_eq(pi, null); |
|
747 |
|
748 // Push a filter and verify the results |
|
749 var filter1 = new BasicFilter(); |
|
750 var filter2 = new BasicFilter(); |
|
751 pps.registerFilter(filter1, 10); |
|
752 pps.registerFilter(filter2, 20); |
|
753 |
|
754 pi = pps.deprecatedBlockingResolve(uri, 0); |
|
755 check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
|
756 check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
|
757 |
|
758 pps.unregisterFilter(filter2); |
|
759 pi = pps.deprecatedBlockingResolve(uri, 0); |
|
760 check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
|
761 check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
|
762 |
|
763 // Remove filter and verify that we return to the initial state |
|
764 pps.unregisterFilter(filter1); |
|
765 pi = pps.deprecatedBlockingResolve(uri, 0); |
|
766 do_check_eq(pi, null); |
|
767 } |
|
768 |
|
769 function run_test() { |
|
770 register_test_protocol_handler(); |
|
771 |
|
772 // any synchronous tests |
|
773 run_deprecated_sync_test(); |
|
774 |
|
775 // start of asynchronous test chain |
|
776 run_filter_test(); |
|
777 do_test_pending(); |
|
778 } |