1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.apache.harmony.luni.tests.java.net;
17
18import java.io.IOException;
19import java.net.InetSocketAddress;
20import java.net.NetPermission;
21import java.net.Proxy;
22import java.net.ProxySelector;
23import java.net.SocketAddress;
24import java.net.SocketException;
25import java.net.URI;
26import java.net.URISyntaxException;
27import java.security.Permission;
28import java.util.List;
29import java.util.Properties;
30
31import junit.framework.TestCase;
32
33public class ProxySelectorTest extends TestCase {
34
35	private static final String HTTP_PROXY_HOST = "127.0.0.1";
36
37	private static final int HTTP_PROXY_PORT = 80;
38
39	private static final String HTTPS_PROXY_HOST = "127.0.0.2";
40
41	private static final int HTTPS_PROXY_PORT = 443;
42
43	private static final String FTP_PROXY_HOST = "127.0.0.3";
44
45	private static final int FTP_PROXY_PORT = 80;
46
47	private static final String SOCKS_PROXY_HOST = "127.0.0.4";
48
49	private static final int SOCKS_PROXY_PORT = 1080;
50
51	private static URI httpUri;
52
53	private static URI ftpUri;
54
55	private static URI httpsUri;
56
57	private static URI tcpUri;
58
59	private List proxyList;
60
61	private ProxySelector selector = ProxySelector.getDefault();
62
63	static {
64		try {
65			httpUri = new URI("http://test.com");
66			ftpUri = new URI("ftp://test.com");
67			httpsUri = new URI("https://test.com");
68			tcpUri = new URI("socket://host.com");
69		} catch (URISyntaxException e) {
70
71		}
72	}
73
74	/*
75	 * Original system properties must be restored after running each test case.
76	 */
77	private Properties originalSystemProperties;
78
79	/**
80	 * @tests java.net.ProxySelector#getDefault()
81	 */
82	public void test_getDefault() {
83		ProxySelector selector1 = ProxySelector.getDefault();
84		assertNotNull(selector1);
85
86		ProxySelector selector2 = ProxySelector.getDefault();
87		assertSame(selector1, selector2);
88	}
89
90	/**
91	 * @tests java.net.ProxySelector#setDefault(ProxySelector)}
92	 */
93	public void test_setDefaultLjava_net_ProxySelector() {
94		ProxySelector originalSelector = ProxySelector.getDefault();
95		try {
96			ProxySelector newSelector = new MockProxySelector();
97			ProxySelector.setDefault(newSelector);
98			assertSame(newSelector, ProxySelector.getDefault());
99			// use null to unset
100			ProxySelector.setDefault(null);
101			assertSame(null, ProxySelector.getDefault());
102		} finally {
103			ProxySelector.setDefault(originalSelector);
104		}
105	}
106
107	/**
108	 * @tests java.net.ProxySelector#select(URI)
109	 */
110	public void test_selectLjava_net_URI_SelectExact()
111			throws URISyntaxException {
112		// no proxy, return a proxyList only contains NO_PROXY
113		proxyList = selector.select(httpUri);
114		assertProxyEquals(proxyList,Proxy.NO_PROXY);
115
116		// set http proxy
117		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
118		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
119		// set https proxy
120		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
121		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
122		// set ftp proxy
123		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
124		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
125		// set socks proxy
126		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
127		System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
128
129		proxyList = selector.select(httpUri);
130		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
131
132		proxyList = selector.select(httpsUri);
133		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
134
135		proxyList = selector.select(ftpUri);
136		assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
137
138		proxyList = selector.select(tcpUri);
139		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
140
141	}
142
143    /**
144     * @tests java.net.ProxySelector#select(URI)
145     */
146    public void test_selectLjava_net_URI_SelectExact_NullHost()
147            throws URISyntaxException {
148        // regression test for Harmony-1063
149        httpUri = new URI("http://a@");
150        ftpUri = new URI("ftp://a@");
151        httpsUri = new URI("https://a@");
152        tcpUri = new URI("socket://a@");
153        // no proxy, return a proxyList only contains NO_PROXY
154        proxyList = selector.select(httpUri);
155        assertProxyEquals(proxyList, Proxy.NO_PROXY);
156
157        // set http proxy
158        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
159        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
160        // set https proxy
161        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
162        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
163        // set ftp proxy
164        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
165        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
166        // set socks proxy
167        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
168        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
169
170        proxyList = selector.select(httpUri);
171        assertProxyEquals(proxyList, Proxy.Type.HTTP, HTTP_PROXY_HOST,
172                HTTP_PROXY_PORT);
173
174        proxyList = selector.select(httpsUri);
175        assertProxyEquals(proxyList, Proxy.Type.HTTP, HTTPS_PROXY_HOST,
176                HTTPS_PROXY_PORT);
177
178        proxyList = selector.select(ftpUri);
179        assertProxyEquals(proxyList, Proxy.Type.HTTP, FTP_PROXY_HOST,
180                FTP_PROXY_PORT);
181
182        proxyList = selector.select(tcpUri);
183        assertProxyEquals(proxyList, Proxy.Type.SOCKS, SOCKS_PROXY_HOST,
184                SOCKS_PROXY_PORT);
185
186    }
187
188    //Regression for HARMONY-4281
189    public void test_selectLjava_net_URI_SelectExact_NullHost_withNoProxyHostsProperty(){
190        System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
191        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
192        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
193        // set https proxy
194        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
195        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
196        // set ftp proxy
197        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
198        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
199        // set socks proxy
200        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
201        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
202
203        proxyList = selector.select(httpUri);
204        assertProxyEquals(proxyList, Proxy.Type.HTTP, HTTP_PROXY_HOST,
205                HTTP_PROXY_PORT);
206    }
207
208	/**
209	 * @tests java.net.ProxySelector#select(URI)
210	 */
211	public void test_selectLjava_net_URI_SelectExact_DefaultPort()
212			throws URISyntaxException {
213		// set http proxy
214		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
215
216		// set https proxy
217		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
218		// set ftp proxy
219		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
220		// set socks proxy
221		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
222
223		proxyList = selector.select(httpUri);
224		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
225
226		proxyList = selector.select(httpsUri);
227		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
228
229		proxyList = selector.select(ftpUri);
230		assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
231
232		proxyList = selector.select(tcpUri);
233		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
234
235	}
236
237	/**
238	 * @tests java.net.ProxySelector#select(URI)
239	 */
240	public void test_selectLjava_net_URI_SelectExact_InvalidPort()
241			throws URISyntaxException {
242		final String INVALID_PORT = "abc";
243
244		// set http proxy
245		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
246		System.setProperty("http.proxyPort", INVALID_PORT);
247		// set https proxy
248		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
249		System.setProperty("https.proxyPort", INVALID_PORT);
250		// set ftp proxy
251		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
252		System.setProperty("ftp.proxyPort", INVALID_PORT);
253		// set socks proxy
254		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
255		System.setProperty("socksproxyPort", INVALID_PORT);
256
257		proxyList = selector.select(httpUri);
258		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
259
260		proxyList = selector.select(httpsUri);
261		assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
262
263		proxyList = selector.select(ftpUri);
264		assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
265
266		proxyList = selector.select(tcpUri);
267		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
268	}
269
270	/**
271	 * @tests java.net.ProxySelector#select(URI)
272	 */
273	// RI may fail this test case.
274	// Uncomment this test case when regex.jar is ready.
275	/*
276	public void test_selectLjava_net_URI_Select_NonProxyHosts()
277			throws URISyntaxException {
278		// RI's bug. Some RIs may fail this test case.
279		URI[] httpUris = { new URI("http://test.com"),
280				new URI("http://10.10.1.2"), new URI("http://a"),
281				new URI("http://def.abc.com") };
282		URI[] ftpUris = { new URI("ftp://test.com"),
283				new URI("ftp://10.10.1.2"), new URI("ftp://a"),
284				new URI("ftp://def.abc.com") };
285
286		// set http proxy
287		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
288		System.setProperty("http.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
289		// set ftp proxy
290		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
291		System.setProperty("ftp.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
292
293		for (int i = 0; i < httpUris.length; i++) {
294			proxyList = selector.select(httpUris[i]);
295			assertProxyEquals(proxyList,Proxy.NO_PROXY);
296		}
297
298		for (int i = 0; i < ftpUris.length; i++) {
299			proxyList = selector.select(ftpUris[i]);
300			assertProxyEquals(proxyList,Proxy.NO_PROXY);
301		}
302	}*/
303
304	/**
305	 * @tests java.net.ProxySelector#select(URI)
306	 */
307	public void test_selectLjava_net_URI_SelectLikeHTTP()
308			throws URISyntaxException {
309		System.setProperty("http.proxyHost", "");
310		// set https proxy
311		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
312		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
313		// set ftp proxy
314		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
315		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
316		// set socks proxy
317		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
318		System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
319
320		proxyList = selector.select(httpUri);
321		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
322	}
323
324	/**
325	 * @tests java.net.ProxySelector#select(URI)
326	 */
327	public void test_selectLjava_net_URI_SelectNoHTTP()
328			throws URISyntaxException {
329		// set https proxy
330		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
331		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
332		// set ftp proxy
333		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
334		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
335
336		proxyList = selector.select(httpUri);
337		assertProxyEquals(proxyList,Proxy.NO_PROXY);
338	}
339
340	/**
341	 * @tests java.net.ProxySelector#select(URI)
342	 */
343	public void test_selectLjava_net_URI_SelectLikeHTTPS()
344			throws URISyntaxException {
345		// set http proxy
346		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
347		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
348		// set https proxy host empty
349		System.setProperty("http.proxyHost", "");
350		// set ftp proxy
351		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
352		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
353		// set socks proxy
354		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
355		System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
356
357		proxyList = selector.select(httpsUri);
358		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
359	}
360
361	/**
362	 * @tests java.net.ProxySelector#select(URI)
363	 */
364	public void test_selectLjava_net_URI_SelectNoHTTPS()
365			throws URISyntaxException {
366		// set https proxy
367		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
368		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
369		// set ftp proxy
370		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
371		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
372
373		proxyList = selector.select(httpsUri);
374		assertProxyEquals(proxyList,Proxy.NO_PROXY);
375	}
376
377	/**
378	 * @tests java.net.ProxySelector#select(URI)
379	 */
380	public void test_selectLjava_net_URI_SelectLikeFTP()
381			throws URISyntaxException {
382		// set http proxy
383		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
384		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
385		// set ftp host empty
386		System.setProperty("ftp.proxyHost", "");
387		// set https proxy
388		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
389		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
390		// set socks proxy
391		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
392		System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
393
394		proxyList = selector.select(ftpUri);
395		assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
396	}
397
398	/**
399	 * @tests java.net.ProxySelector#select(URI)
400	 */
401	public void test_selectLjava_net_URI_SelectNoFTP()
402			throws URISyntaxException {
403		// set http proxy
404		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
405		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
406		// set https proxy
407		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
408		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
409
410		proxyList = selector.select(ftpUri);
411		assertProxyEquals(proxyList,Proxy.NO_PROXY);
412	}
413
414	/**
415	 * @tests java.net.ProxySelector#select(URI)
416	 */
417	public void test_selectLjava_net_URI_SelectNoSOCKS()
418			throws URISyntaxException {
419		// set http proxy
420		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
421		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
422		// set https proxy
423		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
424		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
425		// set socks proxy
426		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
427		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
428
429		proxyList = selector.select(tcpUri);
430		assertProxyEquals(proxyList,Proxy.NO_PROXY);
431	}
432
433	/**
434	 * @tests java.net.ProxySelector#select(URI)
435	 */
436	public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException()
437			throws URISyntaxException {
438		// set http proxy
439		System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
440		System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
441		// set https proxy
442		System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
443		System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
444		// set ftp proxy
445		System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
446		System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
447		// set socks proxy
448		System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
449		System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
450
451		List proxyList1 = selector.select(httpUri);
452		assertNotNull(proxyList1);
453		assertEquals(1, proxyList1.size());
454		Proxy proxy1 = (Proxy) proxyList1.get(0);
455		selector
456				.connectFailed(httpUri, proxy1.address(), new SocketException());
457
458		List proxyList2 = selector.select(httpUri);
459		assertNotNull(proxyList2);
460		assertEquals(1, proxyList2.size());
461		Proxy proxy2 = (Proxy) proxyList2.get(0);
462		// Default implementation doesn't change the proxy list
463		assertEquals(proxy1, proxy2);
464	}
465
466	/**
467	 * @tests java.net.ProxySelector#select(URI)
468	 */
469	public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException_IllegalArguement()
470			throws URISyntaxException {
471		SocketAddress sa = InetSocketAddress.createUnresolved("127.0.0.1", 0);
472		try {
473			selector.connectFailed(null, sa, new SocketException());
474			fail("should throw IllegalArgumentException if any argument is null.");
475		} catch (IllegalArgumentException e) {
476			// expected
477		}
478		try {
479			selector.connectFailed(httpUri, null, new SocketException());
480			fail("should throw IllegalArgumentException if any argument is null.");
481		} catch (IllegalArgumentException e) {
482			// expected
483		}
484		try {
485			selector.connectFailed(httpUri, sa, null);
486			fail("should throw IllegalArgumentException if any argument is null.");
487		} catch (IllegalArgumentException e) {
488			// expected
489		}
490
491	}
492
493	/**
494	 * @tests java.net.ProxySelector#select(URI)
495	 */
496	public void test_selectLjava_net_URI_IllegalArgument()
497			throws URISyntaxException {
498		URI[] illegalUris = { new URI("abc"), new URI("http"), null };
499		for (int i = 0; i < illegalUris.length; i++) {
500			try {
501				selector.select(illegalUris[i]);
502				fail("should throw IllegalArgumentException");
503			} catch (IllegalArgumentException e) {
504				// expected
505			}
506		}
507	}
508
509	/*
510	 * asserts whether selectedProxyList contains one and only one element,
511	 * and the element equals proxy.
512	 */
513	private void assertProxyEquals(List selectedProxyList, Proxy proxy) {
514		assertNotNull(selectedProxyList);
515		assertEquals(1, selectedProxyList.size());
516		assertEquals((Proxy) selectedProxyList.get(0), proxy);
517	}
518
519	/*
520	 * asserts whether selectedProxyList contains one and only one element,
521	 * and the element equals proxy which is represented by arguments "type",
522	 * "host","port".
523	 */
524	private void assertProxyEquals(List selectedProxyList, Proxy.Type type,
525			String host, int port) {
526		SocketAddress sa = InetSocketAddress.createUnresolved(host, port);
527		Proxy proxy = new Proxy(type, sa);
528		assertProxyEquals(selectedProxyList, proxy);
529	}
530
531	/*
532	 * Mock selector for setDefault test
533	 */
534	static class MockProxySelector extends ProxySelector {
535
536		public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
537
538		}
539
540		public List <Proxy> select(URI uri) {
541			return null;
542		}
543	}
544
545	/*
546	 * @see junit.framework.TestCase#setUp()
547	 */
548	protected void setUp() throws Exception {
549		super.setUp();
550		// save original system properties
551		originalSystemProperties = (Properties) System.getProperties().clone();
552	}
553
554	/*
555	 * @see junit.framework.TestCase#tearDown()
556	 */
557	protected void tearDown() throws Exception {
558		// restore original system properties
559		System.setProperties(originalSystemProperties);
560		super.tearDown();
561	}
562}
563