11/*
2- * Copyright (c) 2001, 2011 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2001, 2019 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- /*
24+ /**
2525 * @test
26- * @bug 4482187
27- * @summary HttpsClient tests are failing for build 71
26+ * @bug 4191815
2827 * @library /test/lib
29- * @run main/othervm GetResponseCode
30- *
31- * SunJSSE does not support dynamic system properties, no way to re-use
32- * system properties in samevm/agentvm mode.
33- *
34- * @author Yingxian Wang
28+ * @summary Check that getResponseCode doesn't throw exception if http
29+ * respone code is >= 400.
3530 */
36- import java .io .*;
3731import java .net .*;
38- import javax .net .ssl .*;
39- import java .security .cert .Certificate ;
40-
41- import jdk .test .lib .Utils ;
42- import jdk .test .lib .security .SecurityUtils ;
43-
44- public class GetResponseCode implements HostnameVerifier {
45- /*
46- * =============================================================
47- * Set the various variables needed for the tests, then
48- * specify what tests to run on each side.
49- */
50-
51- /*
52- * Should we run the client or server in a separate thread?
53- * Both sides can throw exceptions, but do you have a preference
54- * as to which side should be the main thread.
55- */
56- static boolean separateServerThread = true ;
57-
58- /*
59- * Where do we find the keystores?
60- */
61- static String pathToStores = "../etc" ;
62- static String keyStoreFile = "keystore" ;
63- static String trustStoreFile = "truststore" ;
64- static String passwd = "passphrase" ;
65-
66- /*
67- * Is the server ready to serve?
68- */
69- volatile static boolean serverReady = false ;
70-
71- /*
72- * Turn on SSL debugging?
73- */
74- static boolean debug = false ;
75-
76- /*
77- * If the client or server is doing some kind of object creation
78- * that the other side depends on, and that thread prematurely
79- * exits, you may experience a hang. The test harness will
80- * terminate all hung threads after its timeout has expired,
81- * currently 3 minutes by default, but you might try to be
82- * smart about it....
83- */
32+ import java .io .*;
8433
85- /*
86- * Define the server side of the test.
87- *
88- * If the server prematurely exits, serverReady will be set to true
89- * to avoid infinite hangs.
90- */
91- void doServerSide () throws Exception {
92- SSLServerSocketFactory sslssf =
93- (SSLServerSocketFactory ) SSLServerSocketFactory .getDefault ();
94- SSLServerSocket sslServerSocket =
95- (SSLServerSocket ) sslssf .createServerSocket (serverPort );
96- serverPort = sslServerSocket .getLocalPort ();
34+ import jdk .test .lib .net .URIBuilder ;
9735
98- /*
99- * Signal Client, we're ready for his connect.
100- */
101- serverReady = true ;
36+ public class GetResponseCode implements Runnable {
10237
103- SSLSocket sslSocket = (SSLSocket ) sslServerSocket .accept ();
104- OutputStream sslOS = sslSocket .getOutputStream ();
105- BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (sslOS ));
106- bw .write ("HTTP/1.1 200 OK\r \n \r \n \r \n " );
107- bw .flush ();
108- Thread .sleep (5000 );
109- sslSocket .close ();
110- }
38+ ServerSocket ss ;
11139
11240 /*
113- * Define the client side of the test.
114- *
115- * If the server prematurely exits, serverReady will be set to true
116- * to avoid infinite hangs.
41+ * Our "http" server to return a 404
11742 */
118- void doClientSide () throws Exception {
119-
120- /*
121- * Wait for server to get started.
122- */
123- while (!serverReady ) {
124- Thread .sleep (50 );
125- }
126-
127- URL url = new URL ("https://localhost:" +serverPort +"/index.html" );
128- HttpsURLConnection urlc = (HttpsURLConnection )url .openConnection ();
129- urlc .setHostnameVerifier (this );
130- urlc .getInputStream ();
131-
132- if (urlc .getResponseCode () == -1 ) {
133- throw new RuntimeException ("getResponseCode() returns -1" );
43+ public void run () {
44+ try {
45+ Socket s = ss .accept ();
46+
47+ PrintStream out = new PrintStream (
48+ new BufferedOutputStream (
49+ s .getOutputStream () ));
50+
51+ /* send the header */
52+ out .print ("HTTP/1.1 404 Not Found\r \n " );
53+ out .print ("Content-Type: text/html; charset=iso-8859-1\r \n " );
54+ out .print ("Connection: close\r \n " );
55+ out .print ("\r \n " );
56+ out .print ("<HTML>" );
57+ out .print ("<HEAD><TITLE>404 Not Found</TITLE></HEAD>" );
58+ out .print ("<BODY>The requested URL was not found.</BODY>" );
59+ out .print ("</HTML>" );
60+ out .flush ();
61+
62+ /*
63+ * Sleep added to avoid connection reset
64+ * on the client side
65+ */
66+ Thread .sleep (1000 );
67+ s .close ();
68+ ss .close ();
69+ } catch (Exception e ) {
70+ e .printStackTrace ();
13471 }
13572 }
13673
137- /*
138- * =============================================================
139- * The remainder is just support stuff
140- */
141-
142- // use any free port by default
143- volatile int serverPort = 0 ;
144-
145- volatile Exception serverException = null ;
146- volatile Exception clientException = null ;
147-
148- public static void main (String [] args ) throws Exception {
149- String keyFilename =
150- System .getProperty ("test.src" , "./" ) + "/" + pathToStores +
151- "/" + keyStoreFile ;
152- String trustFilename =
153- System .getProperty ("test.src" , "./" ) + "/" + pathToStores +
154- "/" + trustStoreFile ;
155-
156- if (SecurityUtils .isFIPS ()) {
157- keyFilename = SecurityUtils .revertJKSToPKCS12 (keyFilename , passwd );
158- trustFilename = SecurityUtils .revertJKSToPKCS12 (trustFilename , passwd );
159- }
160-
161- System .setProperty ("javax.net.ssl.keyStore" , keyFilename );
162- System .setProperty ("javax.net.ssl.keyStorePassword" , passwd );
163- System .setProperty ("javax.net.ssl.trustStore" , trustFilename );
164- System .setProperty ("javax.net.ssl.trustStorePassword" , passwd );
165-
166- if (debug )
167- System .setProperty ("javax.net.debug" , "all" );
168-
169- /*
170- * Start the tests.
171- */
172- new GetResponseCode ();
173- }
74+ GetResponseCode () throws Exception {
17475
175- Thread clientThread = null ;
176- Thread serverThread = null ;
76+ /* start the server */
77+ ss = new ServerSocket ();
78+ ss .bind (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ));
79+ (new Thread (this )).start ();
17780
178- /*
179- * Primary constructor, used to drive remainder of the test.
180- *
181- * Fork off the other side, then do your work.
182- */
183- GetResponseCode () throws Exception {
184- if (separateServerThread ) {
185- startServer (true );
186- startClient (false );
187- } else {
188- startClient (true );
189- startServer (false );
190- }
81+ /* establish http connection to server */
82+ URL url = URIBuilder .newBuilder ()
83+ .scheme ("http" )
84+ .loopback ()
85+ .port (ss .getLocalPort ())
86+ .path ("/missing.nohtml" )
87+ .toURL ();
19188
192- /*
193- * Wait for other side to close down.
194- */
195- if (separateServerThread ) {
196- serverThread .join ();
197- } else {
198- clientThread .join ();
199- }
89+ HttpURLConnection http = (HttpURLConnection ) url .openConnection (Proxy .NO_PROXY );
20090
201- /*
202- * When we get here, the test is pretty much over.
203- *
204- * If the main thread excepted, that propagates back
205- * immediately. If the other thread threw an exception, we
206- * should report back.
207- */
208- if (serverException != null )
209- throw serverException ;
210- if (clientException != null )
211- throw clientException ;
212- }
91+ int respCode = http .getResponseCode ();
21392
214- void startServer (boolean newThread ) throws Exception {
215- if (newThread ) {
216- serverThread = new Thread () {
217- public void run () {
218- try {
219- doServerSide ();
220- } catch (Exception e ) {
221- /*
222- * Our server thread just died.
223- *
224- * Release the client, if not active already...
225- */
226- System .err .println ("Server died..." );
227- serverReady = true ;
228- serverException = e ;
229- }
230- }
231- };
232- serverThread .start ();
233- } else {
234- doServerSide ();
235- }
236- }
93+ http .disconnect ();
23794
238- void startClient (boolean newThread ) throws Exception {
239- if (newThread ) {
240- clientThread = new Thread () {
241- public void run () {
242- try {
243- doClientSide ();
244- } catch (Exception e ) {
245- /*
246- * Our client thread just died.
247- */
248- System .err .println ("Client died..." );
249- clientException = e ;
250- }
251- }
252- };
253- clientThread .start ();
254- } else {
255- doClientSide ();
256- }
25795 }
25896
259- // Simple test method to blindly agree that hostname and certname match
260- public boolean verify (String hostname , SSLSession session ) {
261- return true ;
97+ public static void main (String args []) throws Exception {
98+ new GetResponseCode ();
26299 }
263100
264- }
101+ }
0 commit comments