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 LdapTimeoutTest 27 * @bug 7094377 8000487 6176036 7056489 28 * @summary Timeout tests for ldap 29 */ 30 31 import com.sun.jndi.ldap.Connection; 32 33 import java.net.Socket; 34 import java.net.ServerSocket; 35 import java.net.SocketTimeoutException; 36 import java.io.*; 37 import javax.naming.*; 38 import javax.naming.directory.*; 39 import java.util.Hashtable; 40 import java.util.concurrent.Callable; 41 import java.util.concurrent.Executors; 42 import java.util.concurrent.ScheduledExecutorService; 43 import java.util.concurrent.ScheduledFuture; 44 45 import static java.util.concurrent.TimeUnit.MILLISECONDS; 46 import static java.util.concurrent.TimeUnit.NANOSECONDS; 47 48 public class LdapTimeoutTest { 49 50 static volatile int passed = 0, failed = 0; 51 static void pass() {passed++;} 52 static void fail() {failed++; Thread.dumpStack();} 53 54 public static void main(String[] args) throws Exception { 55 ServerSocket serverSock = new ServerSocket(0); 56 Server s = new Server(serverSock); 57 s.start(); 58 Thread.sleep(200); 59 60 Hashtable env = new Hashtable(11); 61 env.put(Context.INITIAL_CONTEXT_FACTORY, 62 "com.sun.jndi.ldap.LdapCtxFactory"); 63 env.put(Context.PROVIDER_URL, "ldap://localhost:" + 64 serverSock.getLocalPort()); 65 66 env.put(Context.SECURITY_AUTHENTICATION,"simple"); 67 68 env.put(Context.SECURITY_PRINCIPAL, "user"); 69 env.put(Context.SECURITY_CREDENTIALS, "password"); 70 71 InitialContext ctx = null; 72 try { 73 new LdapTimeoutTest().deadServerNoTimeout(env); 74 75 env.put("com.sun.jndi.ldap.connect.timeout", "10"); 76 env.put("com.sun.jndi.ldap.read.timeout", "3000"); 77 new LdapTimeoutTest().ldapReadTimeoutTest(env, false); 78 new LdapTimeoutTest().ldapReadTimeoutTest(env, true); 79 new LdapTimeoutTest().simpleAuthConnectTest(env); 80 } finally { 81 s.interrupt(); 82 } 83 84 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 85 if (failed > 0) throw new AssertionError("Some tests failed"); 86 } 87 88 void ldapReadTimeoutTest(Hashtable env, boolean ssl) { 89 InitialContext ctx = null; 90 if (ssl) env.put(Context.SECURITY_PROTOCOL, "ssl"); 91 long start = System.nanoTime(); 92 try { 93 ctx = new InitialDirContext(env); 94 SearchControls scl = new SearchControls(); 95 scl.setSearchScope(SearchControls.SUBTREE_SCOPE); 96 NamingEnumeration<SearchResult> answer = ((InitialDirContext)ctx) 97 .search("ou=People,o=JNDITutorial", "(objectClass=*)", scl); 98 // shouldn't reach here 99 fail(); 100 } catch (NamingException e) { 101 if (ssl) { 102 if (e.getCause() instanceof SocketTimeoutException) { 103 pass(); 104 } else if (e.getCause() instanceof InterruptedIOException) { 105 Thread.interrupted(); 106 fail(); 107 } 108 } else { 109 pass(); 110 } 111 } finally { 112 if (!shutItDown(ctx)) fail(); 113 } 114 } 115 116 void simpleAuthConnectTest(Hashtable env) { 117 InitialContext ctx = null; 118 long start = System.nanoTime(); 119 try { 120 ctx = new InitialDirContext(env); 121 // shouldn't reach here 122 System.err.println("Fail: InitialDirContext succeeded"); 123 fail(); 124 } catch (NamingException e) { 125 long end = System.nanoTime(); 126 if (e.getCause() instanceof SocketTimeoutException) { 127 if (NANOSECONDS.toMillis(end - start) < 2_900) { 128 pass(); 129 } else { 130 System.err.println("Fail: Waited too long"); 131 fail(); 132 } 133 } else if (e.getCause() instanceof InterruptedIOException) { 134 Thread.interrupted(); 135 fail(); 136 } else { 137 fail(); 138 } 139 } finally { 140 if (!shutItDown(ctx)) fail(); 141 } 142 } 143 144 void deadServerNoTimeout(Hashtable env) { 145 InitialContext ctx = null; 146 long start = System.currentTimeMillis(); 147 try { 148 ctx = new InitialDirContext(env); 149 SearchControls scl = new SearchControls(); 150 scl.setSearchScope(SearchControls.SUBTREE_SCOPE); 151 NamingEnumeration<SearchResult> answer = ((InitialDirContext)ctx) 152 .search("ou=People,o=JNDITutorial", "(objectClass=*)", scl); 153 // shouldn't reach here 154 fail(); 155 } catch (NamingException e) { 156 long elapsed = System.currentTimeMillis() - start; 157 if (elapsed < Connection.DEFAULT_READ_TIMEOUT_MILLIS) { 158 System.err.printf("fail: timeout should be at least %s ms, " + 159 "actual time is %s ms%n", 160 Connection.DEFAULT_READ_TIMEOUT_MILLIS, elapsed); 161 e.printStackTrace(); 162 fail(); 163 } else { 164 pass(); 165 } 166 } finally { 167 if (!shutItDown(ctx)) fail(); 168 } 169 } 170 171 boolean shutItDown(InitialContext ctx) { 172 try { 173 if (ctx != null) ctx.close(); 174 return true; 175 } catch (NamingException ex) { 176 return false; 177 } 178 } 179 180 static class Server extends Thread { 181 final ServerSocket serverSock; 182 183 Server(ServerSocket serverSock) { 184 this.serverSock = serverSock; 185 } 186 187 public void run() { 188 try { 189 Socket socket = serverSock.accept(); 190 } catch (IOException e) {} 191 } 192 } 193 } 194