author | mchung |
Thu, 28 May 2015 10:54:48 -0700 | |
changeset 30820 | 0d4717a011d3 |
parent 23052 | 241885315119 |
child 34522 | fa15f1bdaec2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6216082 |
|
10328 | 27 |
* @summary Redirect problem with HttpsURLConnection using a proxy |
28 |
* SunJSSE does not support dynamic system properties, no way to re-use |
|
29 |
* system properties in samevm/agentvm mode. |
|
30820 | 30 |
* @modules java.base/sun.net.www |
31 |
* @library .. |
|
32 |
* @build HttpCallback TestHttpsServer ClosedChannelList HttpTransaction TunnelProxy |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
33 |
* @run main/othervm B6216082 |
10328 | 34 |
*/ |
2 | 35 |
|
36 |
import java.io.*; |
|
37 |
import java.net.*; |
|
38 |
import javax.net.ssl.*; |
|
39 |
import java.util.*; |
|
40 |
||
41 |
public class B6216082 { |
|
42 |
static SimpleHttpTransaction httpTrans; |
|
13788 | 43 |
static TestHttpsServer server; |
2 | 44 |
static TunnelProxy proxy; |
45 |
||
46 |
// it seems there's no proxy ever if a url points to 'localhost', |
|
47 |
// even if proxy related properties are set. so we need to bind |
|
48 |
// our simple http proxy and http server to a non-loopback address |
|
49 |
static InetAddress firstNonLoAddress = null; |
|
50 |
||
51 |
public static void main(String[] args) throws Exception { |
|
10328 | 52 |
HostnameVerifier reservedHV = |
53 |
HttpsURLConnection.getDefaultHostnameVerifier(); |
|
54 |
try { |
|
55 |
// XXX workaround for CNFE |
|
56 |
Class.forName("java.nio.channels.ClosedByInterruptException"); |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
57 |
if (!setupEnv()) { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
58 |
return; |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
59 |
} |
2 | 60 |
|
10328 | 61 |
startHttpServer(); |
2 | 62 |
|
10328 | 63 |
// https.proxyPort can only be set after the TunnelProxy has been |
64 |
// created as it will use an ephemeral port. |
|
65 |
System.setProperty("https.proxyPort", |
|
66 |
(new Integer(proxy.getLocalPort())).toString() ); |
|
2 | 67 |
|
10328 | 68 |
makeHttpCall(); |
2 | 69 |
|
10328 | 70 |
if (httpTrans.hasBadRequest) { |
71 |
throw new RuntimeException("Test failed : bad http request"); |
|
72 |
} |
|
73 |
} finally { |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
74 |
if (proxy != null) { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
75 |
proxy.terminate(); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
76 |
} |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
77 |
if (server != null) { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
78 |
server.terminate(); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
79 |
} |
10328 | 80 |
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV); |
2 | 81 |
} |
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* Where do we find the keystores for ssl? |
|
86 |
*/ |
|
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
13788
diff
changeset
|
87 |
static String pathToStores = "../../../../../../javax/net/ssl/etc"; |
2 | 88 |
static String keyStoreFile = "keystore"; |
89 |
static String trustStoreFile = "truststore"; |
|
90 |
static String passwd = "passphrase"; |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
91 |
public static boolean setupEnv() throws Exception { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
92 |
firstNonLoAddress = getNonLoAddress(); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
93 |
if (firstNonLoAddress == null) { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
94 |
System.err.println("The test needs at least one non-loopback address to run. Quit now."); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
95 |
return false; |
2 | 96 |
} |
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
97 |
System.out.println(firstNonLoAddress.getHostAddress()); |
2 | 98 |
// will use proxy |
99 |
System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress()); |
|
100 |
||
101 |
// setup properties to do ssl |
|
102 |
String keyFilename = System.getProperty("test.src", "./") + "/" + |
|
103 |
pathToStores + "/" + keyStoreFile; |
|
104 |
String trustFilename = System.getProperty("test.src", "./") + "/" + |
|
105 |
pathToStores + "/" + trustStoreFile; |
|
106 |
||
107 |
System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
108 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
109 |
System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
110 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
111 |
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
112 |
return true; |
2 | 113 |
} |
114 |
||
115 |
public static InetAddress getNonLoAddress() throws Exception { |
|
116 |
NetworkInterface loNIC = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost")); |
|
117 |
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); |
|
118 |
while (nics.hasMoreElements()) { |
|
119 |
NetworkInterface nic = nics.nextElement(); |
|
120 |
if (!nic.getName().equalsIgnoreCase(loNIC.getName())) { |
|
121 |
Enumeration<InetAddress> addrs = nic.getInetAddresses(); |
|
122 |
while (addrs.hasMoreElements()) { |
|
123 |
InetAddress addr = addrs.nextElement(); |
|
124 |
if (!addr.isLoopbackAddress()) |
|
125 |
return addr; |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
return null; |
|
131 |
} |
|
132 |
||
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
133 |
public static void startHttpServer() throws IOException { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
134 |
// Both the https server and the proxy let the |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
135 |
// system pick up an ephemeral port. |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
136 |
httpTrans = new SimpleHttpTransaction(); |
13788 | 137 |
server = new TestHttpsServer(httpTrans, 1, 10, 0); |
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
138 |
proxy = new TunnelProxy(1, 10, 0); |
2 | 139 |
} |
140 |
||
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
141 |
public static void makeHttpCall() throws Exception { |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
142 |
System.out.println("https server listen on: " + server.getLocalPort()); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
143 |
System.out.println("https proxy listen on: " + proxy.getLocalPort()); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
144 |
URL url = new URL("https" , firstNonLoAddress.getHostAddress(), |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
145 |
server.getLocalPort(), "/"); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
146 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
147 |
System.out.println(uc.getResponseCode()); |
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
148 |
uc.disconnect(); |
2 | 149 |
} |
150 |
||
151 |
static class NameVerifier implements HostnameVerifier { |
|
152 |
public boolean verify(String hostname, SSLSession session) { |
|
153 |
return true; |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
class SimpleHttpTransaction implements HttpCallback { |
|
159 |
public boolean hasBadRequest = false; |
|
160 |
||
161 |
/* |
|
162 |
* Our http server which simply redirect first call |
|
163 |
*/ |
|
164 |
public void request(HttpTransaction trans) { |
|
165 |
try { |
|
166 |
String path = trans.getRequestURI().getPath(); |
|
167 |
if (path.equals("/")) { |
|
168 |
// the first call, redirect it |
|
169 |
String location = "/redirect"; |
|
170 |
trans.addResponseHeader("Location", location); |
|
171 |
trans.sendResponse(302, "Moved Temporarily"); |
|
172 |
} else { |
|
173 |
// if the bug exsits, it'll send 2 GET commands |
|
174 |
// check 2nd GET here |
|
175 |
String duplicatedGet = trans.getRequestHeader(null); |
|
176 |
if (duplicatedGet != null && |
|
177 |
duplicatedGet.toUpperCase().indexOf("GET") >= 0) { |
|
178 |
trans.sendResponse(400, "Bad Request"); |
|
179 |
hasBadRequest = true; |
|
180 |
} else { |
|
181 |
trans.sendResponse(200, "OK"); |
|
182 |
} |
|
183 |
} |
|
184 |
} catch (Exception e) { |
|
13669
0f1340f8a157
7195733: TEST_BUG: sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java failing
xuelei
parents:
10328
diff
changeset
|
185 |
throw new RuntimeException(e); |
2 | 186 |
} |
187 |
} |
|
188 |
} |