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 6176036 7056489 |
|
27 * @summary Read-timeout specification for LDAP operations |
|
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 ReadTimeoutTest { |
|
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.read.timeout", "1000"); |
|
49 env.put(Context.PROVIDER_URL, "ldap://localhost:" + server.port()); |
|
50 |
|
51 |
|
52 // Create initial context |
|
53 DirContext ctx = new InitialDirContext(env); |
|
54 try { |
|
55 System.out.println("LDAP Client: Connected to the Server"); |
|
56 |
|
57 SearchControls scl = new SearchControls(); |
|
58 scl.setSearchScope(SearchControls.SUBTREE_SCOPE); |
|
59 System.out.println("Performing Search"); |
|
60 NamingEnumeration<SearchResult> answer = |
|
61 ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl); |
|
62 } finally { |
|
63 // Close the context when we're done |
|
64 ctx.close(); |
|
65 } |
|
66 } catch (NamingException e) { |
|
67 passed = true; |
|
68 e.printStackTrace(); |
|
69 } |
|
70 |
|
71 if (!passed) { |
|
72 throw new Exception("Read timeout test failed," + |
|
73 " read timeout exception not thrown"); |
|
74 } |
|
75 System.out.println("The test PASSED"); |
|
76 } |
|
77 |
|
78 static class Server implements Runnable, Closeable { |
|
79 private final ServerSocket ss; |
|
80 |
|
81 private Server(ServerSocket ss) { |
|
82 this.ss = ss; |
|
83 } |
|
84 |
|
85 static Server create() throws IOException { |
|
86 Server server = new Server(new ServerSocket(0)); |
|
87 new Thread(server).start(); |
|
88 return server; |
|
89 } |
|
90 |
|
91 int port() { |
|
92 return ss.getLocalPort(); |
|
93 } |
|
94 |
|
95 public void run() { |
|
96 try (Socket s = ss.accept()) { |
|
97 System.out.println("Server: Connection accepted"); |
|
98 BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); |
|
99 byte[] buf = new byte[100]; |
|
100 int n; |
|
101 do { |
|
102 n = bis.read(buf); |
|
103 } while (n > 0); |
|
104 } catch (IOException e) { |
|
105 // ignore |
|
106 } |
|
107 } |
|
108 |
|
109 public void close() throws IOException { |
|
110 ss.close(); |
|
111 } |
|
112 } |
|
113 } |
|