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  */
  23 
  24 /**
  25  * @test
  26  * @bug 6997561
  27  * @summary A request for better error handling in JNDI
  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 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
  82         };
  83         private byte[] searchResponse = {
  84             0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,
  85             0x01, 0x02, 0x04, 0x00, 0x04, 0x00
  86         };
  87 
  88         Server() {
  89         }
  90 
  91         public int getPortNumber() {
  92             return serverPort;
  93         }
  94 
  95         public void run() {
  96             try {
  97                 ServerSocket serverSock = new ServerSocket(0);
  98                 serverPort = serverSock.getLocalPort();
  99                 System.out.println("Server: listening on port " + serverPort);
 100 
 101                 Socket socket = serverSock.accept();
 102                 System.out.println("Server: connection accepted");
 103 
 104                 InputStream in = socket.getInputStream();
 105                 OutputStream out = socket.getOutputStream();
 106 
 107                 // Read the LDAP BindRequest
 108                 System.out.println("Server: reading request...");
 109                 while (in.read() != -1) {
 110                     in.skip(in.available());
 111                     break;
 112                 }
 113 
 114                 // Write an LDAP BindResponse
 115                 System.out.println("Server: writing response...");
 116                 out.write(bindResponse);
 117                 out.flush();
 118 
 119                 // Read the LDAP SearchRequest
 120                 System.out.println("Server: reading request...");
 121                 while (in.read() != -1) {
 122                     in.skip(in.available());
 123                     break;
 124                 }
 125 
 126                 // Write an LDAP SearchResponse
 127                 System.out.println("Server: writing response...");
 128                 out.write(searchResponse);
 129                 out.flush();
 130 
 131                 in.close();
 132                 out.close();
 133                 socket.close();
 134                 serverSock.close();
 135 
 136             } catch (IOException e) {
 137                 // ignore
 138             }
 139         }
 140     }
 141 }