1 /* |
|
2 * Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved. |
|
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 * |
|
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. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4393337 |
|
27 * @summary [TEST RUNS ON SOLARIS ONLY] Throw an InterruptedIOException |
|
28 * when read on SSLSocket is * interrupted. |
|
29 */ |
|
30 |
|
31 import java.io.InputStream; |
|
32 import java.io.IOException; |
|
33 import java.io.InterruptedIOException; |
|
34 import java.net.Socket; |
|
35 import javax.net.ssl.*; |
|
36 |
|
37 /** |
|
38 * Interrupts an SSL socket that is blocked on a read. An |
|
39 * InterruptedIOException will be thrown and handled within the test if the |
|
40 * test completes correctly. |
|
41 */ |
|
42 |
|
43 public class InterruptedIO { |
|
44 |
|
45 /** |
|
46 * Starts a client and a server thread. Gives the client enough time to |
|
47 * block in a read, then interrupts it. |
|
48 */ |
|
49 public static void main(String[] args) throws Exception { |
|
50 |
|
51 String reason = |
|
52 "Test valid only on SunOS.\n" + |
|
53 "=========================\n" + |
|
54 "It was determined that Thread.interrupt() could \n" + |
|
55 "not be reliably return InterruptedIOException \n" + |
|
56 "on non-Solaris implementations. Thread.interrupt() \n" + |
|
57 "API was updated in merlin (JDK 1.4) to reflect this.\n"; |
|
58 System.out.println(reason); |
|
59 |
|
60 String osName = System.getProperty("os.name", ""); |
|
61 if (!osName.equalsIgnoreCase("SunOS")) { |
|
62 System.out.println("Ignoring test on '" + osName + "'"); |
|
63 return; |
|
64 } |
|
65 |
|
66 String testRoot = System.getProperty("test.src", "."); |
|
67 System.setProperty("javax.net.ssl.keyStore", |
|
68 testRoot + |
|
69 "/../../../../../../../etc/keystore"); |
|
70 System.setProperty("javax.net.ssl.keyStorePassword", |
|
71 "passphrase"); |
|
72 System.setProperty("javax.net.ssl.trustStore", |
|
73 testRoot + |
|
74 "/../../../../../../../etc/truststore"); |
|
75 |
|
76 Server server = new Server(); |
|
77 server.start(); |
|
78 |
|
79 Client client = new Client(server.getPort()); // Will do handshake |
|
80 client.start(); // Will block in read |
|
81 |
|
82 // sleep for 5 seconds |
|
83 System.out.println("Main - Pausing for 5 seconds..."); |
|
84 Thread.sleep(5 * 1000); |
|
85 |
|
86 System.out.println("Main - Interrupting client reader thread"); |
|
87 client.interrupt(); |
|
88 client.join(); // Wait for client thread to complete |
|
89 |
|
90 if (client.failed()) |
|
91 throw new Exception("Main - Test InterruptedIO failed " |
|
92 + "on client side."); |
|
93 else |
|
94 System.out.println("Main - Test InterruptedIO successful!"); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Accepts an incoming SSL Connection. Then blocks in a read. |
|
99 */ |
|
100 static class Server extends Thread { |
|
101 |
|
102 private SSLServerSocket ss; |
|
103 |
|
104 public Server() throws Exception { |
|
105 ss = (SSLServerSocket) SSLServerSocketFactory.getDefault(). |
|
106 createServerSocket(0); |
|
107 } |
|
108 |
|
109 public int getPort() { |
|
110 return ss.getLocalPort(); |
|
111 } |
|
112 |
|
113 public void run() { |
|
114 try { |
|
115 System.out.println("Server - Will accept connections on port " |
|
116 + getPort()); |
|
117 Socket s = ss.accept(); |
|
118 InputStream is = s.getInputStream(); |
|
119 // We want the client to block so deadlock |
|
120 is.read(); |
|
121 } catch (IOException e) { |
|
122 // Happens when client closese connection. |
|
123 // If an error occurs, Client will detect problem |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 /** |
|
129 * Initiates an SSL connection to a server. Then blocks in a read. It |
|
130 * should be interrupted by another thread. An InterruptedIOException |
|
131 * is expected to be thrown. |
|
132 */ |
|
133 static class Client extends Thread { |
|
134 |
|
135 private SSLSocket socket; |
|
136 private InputStream inStream; |
|
137 private boolean failed = false; |
|
138 |
|
139 public Client(int port) throws Exception { |
|
140 socket = (SSLSocket) SSLSocketFactory.getDefault(). |
|
141 createSocket("localhost", port); |
|
142 inStream = socket.getInputStream(); |
|
143 System.out.println("Client - " |
|
144 + "Connected to: localhost" + ":" + port); |
|
145 System.out.println("Client - " |
|
146 + "Doing SSL Handshake..."); |
|
147 socket.startHandshake(); // Asynchronous call |
|
148 System.out.println("Client - Done with SSL Handshake"); |
|
149 } |
|
150 |
|
151 public void run() { |
|
152 |
|
153 try { |
|
154 System.out.println("Client - Reading from input stream ..."); |
|
155 if (inStream.read() == -1) { |
|
156 System.out.println("Client - End-of-stream detected"); |
|
157 failed = true; |
|
158 } |
|
159 } catch (InterruptedIOException e) { |
|
160 System.out.println("Client - " |
|
161 + "As expected, InterruptedIOException " |
|
162 + "was thrown. Message: " |
|
163 + e.getMessage()); |
|
164 } catch (Exception e) { |
|
165 System.out.println("Client - Unexpected exception:"); |
|
166 e.printStackTrace(); |
|
167 failed = true; |
|
168 } finally { |
|
169 try { |
|
170 socket.close(); |
|
171 } catch (IOException e) { |
|
172 // Squelch it |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 public boolean failed() { |
|
178 return failed; |
|
179 } |
|
180 |
|
181 } |
|
182 |
|
183 } |
|