8177
|
1 |
/*
|
57345
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
8177
|
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 |
|
8172
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6997561
|
57345
|
27 |
* @library ../lib/
|
8172
|
28 |
* @summary A request for better error handling in JNDI
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
import javax.naming.*;
|
|
33 |
import javax.naming.directory.*;
|
|
34 |
import javax.naming.ldap.*;
|
|
35 |
import java.util.Collections;
|
|
36 |
import java.util.Hashtable;
|
|
37 |
|
|
38 |
public class EmptyNameSearch {
|
|
39 |
|
|
40 |
public static void main(String[] args) throws Exception {
|
|
41 |
|
|
42 |
// Start the LDAP server
|
|
43 |
Server s = new Server();
|
|
44 |
s.start();
|
|
45 |
Thread.sleep(3000);
|
|
46 |
|
|
47 |
// Setup JNDI parameters
|
57345
|
48 |
Hashtable<String, Object> env = new Hashtable<>();
|
8172
|
49 |
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
|
50 |
"com.sun.jndi.ldap.LdapCtxFactory");
|
57345
|
51 |
env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPort());
|
8172
|
52 |
|
57345
|
53 |
try (s) {
|
8172
|
54 |
|
|
55 |
// Create initial context
|
|
56 |
System.out.println("Client: connecting...");
|
|
57 |
DirContext ctx = new InitialDirContext(env);
|
|
58 |
|
|
59 |
System.out.println("Client: performing search...");
|
|
60 |
ctx.search(new LdapName(Collections.EMPTY_LIST), "cn=*", null);
|
|
61 |
ctx.close();
|
|
62 |
|
|
63 |
// Exit
|
|
64 |
throw new RuntimeException();
|
|
65 |
|
|
66 |
} catch (NamingException e) {
|
|
67 |
System.err.println("Passed: caught the expected Exception - " + e);
|
|
68 |
|
|
69 |
} catch (Exception e) {
|
|
70 |
System.err.println("Failed: caught an unexpected Exception - " + e);
|
|
71 |
throw e;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
57345
|
75 |
static class Server extends BaseLdapServer {
|
8172
|
76 |
|
|
77 |
private byte[] bindResponse = {
|
|
78 |
0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
|
|
79 |
0x01, 0x00, 0x04, 0x00, 0x04, 0x00
|
|
80 |
};
|
|
81 |
private byte[] searchResponse = {
|
|
82 |
0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,
|
|
83 |
0x01, 0x02, 0x04, 0x00, 0x04, 0x00
|
|
84 |
};
|
|
85 |
|
57345
|
86 |
Server() throws IOException {
|
|
87 |
setDebugLevel(DebugLevel.FULL);
|
|
88 |
setCommonRequestHandler((msg, out) -> {
|
|
89 |
switch (msg.getOperation()) {
|
|
90 |
case BIND_REQUEST:
|
|
91 |
// Write an LDAP BindResponse
|
|
92 |
debug("writing response...");
|
|
93 |
out.write(bindResponse);
|
|
94 |
break;
|
|
95 |
case SEARCH_REQUEST:
|
|
96 |
// Write an LDAP SearchResponse
|
|
97 |
debug("writing response...");
|
|
98 |
out.write(searchResponse);
|
|
99 |
break;
|
|
100 |
default:
|
|
101 |
break;
|
8172
|
102 |
}
|
57345
|
103 |
});
|
8172
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|