1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.luni.tests.java.net; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.net.Socket; 24 import java.net.InetSocketAddress; 25 import java.net.Proxy; 26 import java.net.SocketAddress; 27 import java.net.URL; 28 import java.net.URLConnection; 29 30 import tests.support.Support_Configuration; 31 import tests.support.resource.Support_Resources; 32 import junit.framework.TestCase; 33 34 /* 35 * This test is designed for collecting all the testcases which needs a proxy 36 * server. They will be moved to corresponding test class when ProxyHandler of 37 * Jetty is ready in the future. 38 * 39 */ 40 41 public class ExcludedProxyTest extends TestCase { 42 /** 43 * @tests java.net.HttpURLConnection#usingProxy() 44 */ 45 public void test_usingProxy() throws Exception { 46 try { 47 System.setProperty("http.proxyHost", 48 Support_Configuration.ProxyServerTestHost); 49 50 URL u1 = new URL("http://" + Support_Configuration.HomeAddress); 51 URLConnection conn1 = u1.openConnection(); 52 conn1.getInputStream(); 53 54 boolean exception = false; 55 try { 56 System.setProperty("http.proxyPort", "81"); 57 URL u3 = new URL("http://localhost"); 58 URLConnection conn3 = u3.openConnection(); 59 conn3.getInputStream(); 60 fail("Should throw IOException"); 61 } catch (IOException e) { 62 // expected 63 } 64 65 System.setProperty("http.proxyPort", "80"); 66 67 URL u2 = new URL("http://" 68 + Support_Configuration.ProxyServerTestHost 69 + "/cgi-bin/test.pl"); 70 java.net.HttpURLConnection conn2 = (java.net.HttpURLConnection) u2 71 .openConnection(); 72 conn2.setDoOutput(true); 73 conn2.setRequestMethod("POST"); 74 OutputStream out2 = conn2.getOutputStream(); 75 String posted2 = "this is a test"; 76 out2.write(posted2.getBytes()); 77 out2.close(); 78 conn2.getResponseCode(); 79 InputStream is2 = conn2.getInputStream(); 80 String response2 = ""; 81 byte[] b2 = new byte[1024]; 82 int count2 = 0; 83 while ((count2 = is2.read(b2)) > 0) 84 response2 += new String(b2, 0, count2); 85 assertTrue("Response to POST method invalid", response2 86 .equals(posted2)); 87 88 String posted4 = "just a test"; 89 URL u4 = new URL("http://" 90 + Support_Configuration.ProxyServerTestHost 91 + "/cgi-bin/test.pl"); 92 java.net.HttpURLConnection conn4 = (java.net.HttpURLConnection) u4 93 .openConnection(); 94 conn4.setDoOutput(true); 95 conn4.setRequestMethod("POST"); 96 conn4.setRequestProperty("Content-length", String.valueOf(posted4 97 .length())); 98 OutputStream out = conn4.getOutputStream(); 99 out.write(posted4.getBytes()); 100 out.close(); 101 conn4.getResponseCode(); 102 InputStream is = conn4.getInputStream(); 103 String response = ""; 104 byte[] b4 = new byte[1024]; 105 int count = 0; 106 while ((count = is.read(b4)) > 0) 107 response += new String(b4, 0, count); 108 assertTrue("Response to POST method invalid", response 109 .equals(posted4)); 110 } finally { 111 System.setProperties(null); 112 } 113 } 114 115 /** 116 * @tests java.net.SocketImpl#SocketImpl() 117 */ 118 public void test_Constructor() { 119 try { 120 try { 121 System.setProperty("socksProxyHost", 122 Support_Configuration.SocksServerTestHost); 123 System.setProperty("socksProxyPort", String 124 .valueOf(Support_Configuration.SocksServerTestPort)); 125 Socket s = new Socket(Support_Configuration.HomeAddress, 80); 126 OutputStream os = s.getOutputStream(); 127 os.write("GET / HTTP/1.0\r\n\r\n".getBytes()); 128 s.getInputStream(); 129 } catch (IOException e) { 130 fail("Could not open socket: " 131 + Support_Configuration.HomeAddress + " " + e); 132 } 133 boolean exception = false; 134 try { 135 System.setProperty("socksProxyHost", 136 Support_Configuration.SocksServerTestHost); 137 System 138 .setProperty( 139 "socksProxyPort", 140 String 141 .valueOf(Support_Configuration.SocksServerTestPort + 1)); 142 Socket s = new Socket(Support_Configuration.HomeAddress, 80); 143 OutputStream os = s.getOutputStream(); 144 os.write("GET / HTTP/1.0\r\n\r\n".getBytes()); 145 s.getInputStream(); 146 } catch (IOException e) { 147 exception = true; 148 } 149 assertTrue("Exception should have been thrown", exception); 150 } finally { 151 System.setProperties(null); 152 } 153 } 154 155 /** 156 * @tests java.net.URL#openConnection(Proxy) 157 */ 158 public void test_openConnectionLjava_net_Proxy() throws IOException { 159 SocketAddress addr1 = new InetSocketAddress( 160 Support_Configuration.ProxyServerTestHost, 808); 161 SocketAddress addr2 = new InetSocketAddress( 162 Support_Configuration.ProxyServerTestHost, 1080); 163 Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1); 164 Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr2); 165 Proxy proxyList[] = { proxy1, proxy2 }; 166 for (int i = 0; i < proxyList.length; ++i) { 167 String posted = "just a test"; 168 URL u = new URL("http://" 169 + Support_Configuration.ProxyServerTestHost 170 + "/cgi-bin/test.pl"); 171 java.net.HttpURLConnection conn = (java.net.HttpURLConnection) u 172 .openConnection(proxyList[i]); 173 conn.setDoOutput(true); 174 conn.setRequestMethod("POST"); 175 conn.setRequestProperty("Content-length", String.valueOf(posted 176 .length())); 177 OutputStream out = conn.getOutputStream(); 178 out.write(posted.getBytes()); 179 out.close(); 180 conn.getResponseCode(); 181 InputStream is = conn.getInputStream(); 182 String response = ""; 183 byte[] b = new byte[1024]; 184 int count = 0; 185 while ((count = is.read(b)) > 0) { 186 response += new String(b, 0, count); 187 } 188 assertTrue("Response to POST method invalid", response 189 .equals(posted)); 190 } 191 192 URL httpUrl = new URL("http://abc.com"); 193 URL jarUrl = new URL("jar:" 194 + Support_Resources.getResourceURL("/JUC/lf.jar!/plus.bmp")); 195 URL ftpUrl = new URL("ftp://" + Support_Configuration.FTPTestAddress 196 + "/nettest.txt"); 197 URL fileUrl = new URL("file://abc"); 198 URL[] urlList = { httpUrl, jarUrl, ftpUrl, fileUrl }; 199 for (int i = 0; i < urlList.length; ++i) { 200 try { 201 urlList[i].openConnection(null); 202 } catch (IllegalArgumentException iae) { 203 // expected 204 } 205 } 206 // should not throw exception 207 fileUrl.openConnection(Proxy.NO_PROXY); 208 } 209 } 210