|
1 /* |
|
2 * Copyright (c) 2011, 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 7094377 |
|
27 * @summary Com.sun.jndi.ldap.read.timeout doesn't work with ldaps. |
|
28 */ |
|
29 |
|
30 import java.net.Socket; |
|
31 import java.net.ServerSocket; |
|
32 import java.io.*; |
|
33 import javax.naming.*; |
|
34 import javax.naming.directory.*; |
|
35 import java.util.Hashtable; |
|
36 |
|
37 public class LdapsReadTimeoutTest { |
|
38 |
|
39 public static void main(String[] args) throws Exception { |
|
40 boolean passed = false; |
|
41 |
|
42 // create the server |
|
43 try (Server server = Server.create()) { |
|
44 // Set up the environment for creating the initial context |
|
45 Hashtable<String,Object> env = new Hashtable<>(11); |
|
46 env.put(Context.INITIAL_CONTEXT_FACTORY, |
|
47 "com.sun.jndi.ldap.LdapCtxFactory"); |
|
48 env.put("com.sun.jndi.ldap.connect.timeout", "1000"); |
|
49 env.put("com.sun.jndi.ldap.read.timeout", "1000"); |
|
50 env.put(Context.PROVIDER_URL, "ldaps://localhost:" + server.port()); |
|
51 |
|
52 |
|
53 // Create initial context |
|
54 DirContext ctx = new InitialDirContext(env); |
|
55 try { |
|
56 System.out.println("LDAP Client: Connected to the Server"); |
|
57 |
|
58 SearchControls scl = new SearchControls(); |
|
59 scl.setSearchScope(SearchControls.SUBTREE_SCOPE); |
|
60 System.out.println("Performing Search"); |
|
61 NamingEnumeration<SearchResult> answer = |
|
62 ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl); |
|
63 } finally { |
|
64 // Close the context when we're done |
|
65 ctx.close(); |
|
66 } |
|
67 } catch (NamingException e) { |
|
68 passed = true; |
|
69 e.printStackTrace(); |
|
70 } |
|
71 |
|
72 if (!passed) { |
|
73 throw new Exception("Read timeout test failed," + |
|
74 " read timeout exception not thrown"); |
|
75 } |
|
76 System.out.println("The test PASSED"); |
|
77 } |
|
78 |
|
79 static class Server implements Runnable, Closeable { |
|
80 private final ServerSocket ss; |
|
81 private Socket sref; |
|
82 |
|
83 private Server(ServerSocket ss) { |
|
84 this.ss = ss; |
|
85 } |
|
86 |
|
87 static Server create() throws IOException { |
|
88 Server server = new Server(new ServerSocket(0)); |
|
89 new Thread(server).start(); |
|
90 return server; |
|
91 } |
|
92 |
|
93 int port() { |
|
94 return ss.getLocalPort(); |
|
95 } |
|
96 |
|
97 public void run() { |
|
98 try (Socket s = ss.accept()) { |
|
99 sref = s; |
|
100 System.out.println("Server: Connection accepted"); |
|
101 BufferedInputStream bis = |
|
102 new BufferedInputStream(s.getInputStream()); |
|
103 byte[] buf = new byte[100]; |
|
104 int n; |
|
105 do { |
|
106 n = bis.read(buf); |
|
107 } while (n > 0); |
|
108 } catch (IOException e) { |
|
109 // ignore |
|
110 } |
|
111 } |
|
112 |
|
113 public void close() throws IOException { |
|
114 ss.close(); |
|
115 sref.close(); |
|
116 } |
|
117 } |
|
118 } |