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