|
1 /* |
|
2 * Copyright (c) 2011, 2014, 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 * @run main/othervm DeadSSLLdapTimeoutTest |
|
27 * @bug 8141370 |
|
28 * @key intermittent |
|
29 */ |
|
30 |
|
31 import java.net.Socket; |
|
32 import java.net.ServerSocket; |
|
33 import java.net.SocketTimeoutException; |
|
34 import java.io.*; |
|
35 import javax.naming.*; |
|
36 import javax.naming.directory.*; |
|
37 import java.util.List; |
|
38 import java.util.Hashtable; |
|
39 import java.util.ArrayList; |
|
40 import java.util.concurrent.Callable; |
|
41 import java.util.concurrent.ExecutionException; |
|
42 import java.util.concurrent.Executors; |
|
43 import java.util.concurrent.ExecutorService; |
|
44 import java.util.concurrent.Future; |
|
45 import java.util.concurrent.ScheduledExecutorService; |
|
46 import java.util.concurrent.ScheduledFuture; |
|
47 import java.util.concurrent.TimeoutException; |
|
48 import java.util.concurrent.TimeUnit; |
|
49 import javax.net.ssl.SSLHandshakeException; |
|
50 |
|
51 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
52 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
|
53 |
|
54 |
|
55 class DeadServerTimeoutSSLTest implements Callable { |
|
56 |
|
57 Hashtable env; |
|
58 DeadSSLServer server; |
|
59 boolean passed = false; |
|
60 private int HANGING_TEST_TIMEOUT = 20_000; |
|
61 |
|
62 public DeadServerTimeoutSSLTest(Hashtable env) throws IOException { |
|
63 this.server = new DeadSSLServer(); |
|
64 this.env = env; |
|
65 } |
|
66 |
|
67 public void performOp(InitialContext ctx) throws NamingException {} |
|
68 |
|
69 public void handleNamingException(NamingException e, long start, long end) { |
|
70 if (e.getCause() instanceof SocketTimeoutException) { |
|
71 // SSL connect will timeout via readReply using |
|
72 // SocketTimeoutException |
|
73 e.printStackTrace(); |
|
74 pass(); |
|
75 } else if (e.getCause() instanceof SSLHandshakeException |
|
76 && e.getCause().getCause() instanceof EOFException) { |
|
77 // test seems to be failing intermittently on some |
|
78 // platforms. |
|
79 pass(); |
|
80 } else { |
|
81 fail(e); |
|
82 } |
|
83 } |
|
84 |
|
85 public void pass() { |
|
86 this.passed = true; |
|
87 } |
|
88 |
|
89 public void fail() { |
|
90 throw new RuntimeException("Test failed"); |
|
91 } |
|
92 |
|
93 public void fail(Exception e) { |
|
94 throw new RuntimeException("Test failed", e); |
|
95 } |
|
96 |
|
97 boolean shutItDown(InitialContext ctx) { |
|
98 try { |
|
99 if (ctx != null) ctx.close(); |
|
100 return true; |
|
101 } catch (NamingException ex) { |
|
102 return false; |
|
103 } |
|
104 } |
|
105 |
|
106 public Boolean call() { |
|
107 InitialContext ctx = null; |
|
108 ScheduledFuture killer = null; |
|
109 long start = System.nanoTime(); |
|
110 |
|
111 try { |
|
112 while(!server.accepting()) |
|
113 Thread.sleep(200); // allow the server to start up |
|
114 Thread.sleep(200); // to be sure |
|
115 |
|
116 env.put(Context.PROVIDER_URL, "ldap://localhost:" + |
|
117 server.getLocalPort()); |
|
118 |
|
119 try { |
|
120 ctx = new InitialDirContext(env); |
|
121 performOp(ctx); |
|
122 fail(); |
|
123 } catch (NamingException e) { |
|
124 long end = System.nanoTime(); |
|
125 System.out.println(this.getClass().toString() + " - elapsed: " |
|
126 + NANOSECONDS.toMillis(end - start)); |
|
127 handleNamingException(e, start, end); |
|
128 } finally { |
|
129 if (killer != null && !killer.isDone()) |
|
130 killer.cancel(true); |
|
131 shutItDown(ctx); |
|
132 server.close(); |
|
133 } |
|
134 return passed; |
|
135 } catch (IOException|InterruptedException e) { |
|
136 throw new RuntimeException(e); |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 class DeadSSLServer extends Thread { |
|
142 ServerSocket serverSock; |
|
143 boolean accepting = false; |
|
144 |
|
145 public DeadSSLServer() throws IOException { |
|
146 this.serverSock = new ServerSocket(0); |
|
147 start(); |
|
148 } |
|
149 |
|
150 public void run() { |
|
151 while(true) { |
|
152 try { |
|
153 accepting = true; |
|
154 Socket socket = serverSock.accept(); |
|
155 } catch (Exception e) { |
|
156 break; |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 public int getLocalPort() { |
|
162 return serverSock.getLocalPort(); |
|
163 } |
|
164 |
|
165 public boolean accepting() { |
|
166 return accepting; |
|
167 } |
|
168 |
|
169 public void close() throws IOException { |
|
170 serverSock.close(); |
|
171 } |
|
172 } |
|
173 |
|
174 public class DeadSSLLdapTimeoutTest { |
|
175 |
|
176 static Hashtable createEnv() { |
|
177 Hashtable env = new Hashtable(11); |
|
178 env.put(Context.INITIAL_CONTEXT_FACTORY, |
|
179 "com.sun.jndi.ldap.LdapCtxFactory"); |
|
180 return env; |
|
181 } |
|
182 |
|
183 public static void main(String[] args) throws Exception { |
|
184 |
|
185 InitialContext ctx = null; |
|
186 |
|
187 // |
|
188 // Running this test serially as it seems to tickle a problem |
|
189 // on older kernels |
|
190 // |
|
191 // run the DeadServerTest with connect / read timeouts set |
|
192 // and ssl enabled |
|
193 // this should exit with a SocketTimeoutException as the root cause |
|
194 // it should also use the connect timeout instead of the read timeout |
|
195 System.out.println("Running connect timeout test with 10ms connect timeout, 3000ms read timeout & SSL"); |
|
196 Hashtable sslenv = createEnv(); |
|
197 sslenv.put("com.sun.jndi.ldap.connect.timeout", "10"); |
|
198 sslenv.put("com.sun.jndi.ldap.read.timeout", "3000"); |
|
199 sslenv.put(Context.SECURITY_PROTOCOL, "ssl"); |
|
200 boolean testFailed = |
|
201 (new DeadServerTimeoutSSLTest(sslenv).call()) ? false : true; |
|
202 |
|
203 if (testFailed) { |
|
204 throw new AssertionError("some tests failed"); |
|
205 } |
|
206 |
|
207 } |
|
208 |
|
209 } |
|
210 |