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 */
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 javax.naming.ldap.*;
36 import java.util.Collections;
37 import java.util.Hashtable;
38
39 public class EmptyNameSearch {
40
41 public static void main(String[] args) throws Exception {
42
43 // Start the LDAP server
44 Server s = new Server();
45 s.start();
46 Thread.sleep(3000);
47
48 // Setup JNDI parameters
49 Hashtable env = new Hashtable();
50 env.put(Context.INITIAL_CONTEXT_FACTORY,
51 "com.sun.jndi.ldap.LdapCtxFactory");
52 env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber());
53
54 try {
55
56 // Create initial context
57 System.out.println("Client: connecting...");
58 DirContext ctx = new InitialDirContext(env);
59
60 System.out.println("Client: performing search...");
61 ctx.search(new LdapName(Collections.EMPTY_LIST), "cn=*", null);
62 ctx.close();
63
64 // Exit
65 throw new RuntimeException();
66
67 } catch (NamingException e) {
68 System.err.println("Passed: caught the expected Exception - " + e);
69
70 } catch (Exception e) {
71 System.err.println("Failed: caught an unexpected Exception - " + e);
72 throw e;
73 }
74 }
75
76 static class Server extends Thread {
77
78 private int serverPort = 0;
79 private byte[] bindResponse = {
80 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
81 0x01, 0x00, 0x04, 0x00, 0x04, 0x00
| 1 /*
2 * Copyright (c) 2011, 2020, 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 */
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 javax.naming.ldap.*;
36 import java.util.Collections;
37 import java.util.Hashtable;
38
39 public class EmptyNameSearch {
40
41 public static void main(String[] args) throws Exception {
42
43 // Start the LDAP server
44 Server s = new Server();
45 s.start();
46 Thread.sleep(3000);
47
48 // Setup JNDI parameters
49 Hashtable<Object, Object> env = new Hashtable<>();
50 env.put(Context.INITIAL_CONTEXT_FACTORY,
51 "com.sun.jndi.ldap.LdapCtxFactory");
52 env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber());
53
54 try {
55
56 // Create initial context
57 System.out.println("Client: connecting...");
58 DirContext ctx = new InitialDirContext(env);
59
60 System.out.println("Client: performing search...");
61 ctx.search(new LdapName(Collections.emptyList()), "cn=*", null);
62 ctx.close();
63
64 // Exit
65 throw new RuntimeException();
66
67 } catch (NamingException e) {
68 System.err.println("Passed: caught the expected Exception - " + e);
69
70 } catch (Exception e) {
71 System.err.println("Failed: caught an unexpected Exception - " + e);
72 throw e;
73 }
74 }
75
76 static class Server extends Thread {
77
78 private int serverPort = 0;
79 private byte[] bindResponse = {
80 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
81 0x01, 0x00, 0x04, 0x00, 0x04, 0x00
|