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 6748156
  27  * @summary add an new JNDI property to control the boolean flag WaitForReply
  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 NoWaitForReplyTest {
  38 
  39     public static void main(String[] args) throws Exception {
  40 
  41         boolean passed = false;
  42 
  43         // start the LDAP server
  44         DummyServer ldapServer = new DummyServer();
  45         ldapServer.start();
  46 
  47         // Set up the environment for creating the initial context
  48         Hashtable<Object, Object> env = new Hashtable<>(11);
  49         env.put(Context.PROVIDER_URL, "ldap://localhost:" +
  50             ldapServer.getPortNumber());
  51         env.put(Context.INITIAL_CONTEXT_FACTORY,
  52             "com.sun.jndi.ldap.LdapCtxFactory");
  53 
  54         // Wait up to 10 seconds for a response from the LDAP server
  55         env.put("com.sun.jndi.ldap.read.timeout", "10000");
  56 
  57         // Don't wait until the first search reply is received
  58         env.put("com.sun.jndi.ldap.search.waitForReply", "false");
  59 
  60         // Send the LDAP search request without first authenticating (no bind)
  61         env.put("java.naming.ldap.version", "3");
  62 
  63 
  64         try {
  65 
  66             // Create initial context
  67             System.out.println("Client: connecting to the server");
  68             DirContext ctx = new InitialDirContext(env);
  69 
  70             SearchControls scl = new SearchControls();
  71             scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
  72             System.out.println("Client: performing search");
  73             NamingEnumeration<?> answer =
  74             ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
  75 
  76             // Server will never reply: either we waited in the call above until
  77             // the timeout (fail) or we did not wait and reached here (pass).
  78             passed = true;
  79             System.out.println("Client: did not wait until first reply");
  80 
  81             // Close the context when we're done
  82             ctx.close();
  83 
  84         } catch (NamingException e) {
  85             // timeout (ignore)
  86         }
  87         ldapServer.interrupt();
  88 
  89         if (!passed) {
  90             throw new Exception(
  91                 "Test FAILED: should not have waited until first search reply");
  92         }
  93         System.out.println("Test PASSED");
  94     }
  95 
  96     static class DummyServer extends Thread {
  97 
  98         private final ServerSocket serverSocket;
  99 
 100         DummyServer() throws IOException {
 101             this.serverSocket = new ServerSocket(0);
 102             System.out.println("Server: listening on port " + serverSocket.getLocalPort());
 103         }
 104 
 105         public int getPortNumber() {
 106             return serverSocket.getLocalPort();
 107         }
 108 
 109         public void run() {
 110             try (Socket socket = serverSocket.accept()) {
 111                 System.out.println("Server: accepted a connection");
 112                 InputStream in = socket.getInputStream();
 113 
 114                 while (!isInterrupted()) {
 115                    in.skip(in.available());
 116                 }
 117 
 118             } catch (Exception e) {
 119                 // ignore
 120 
 121             } finally {
 122                 System.out.println("Server: shutting down");
 123                 try {
 124                     serverSocket.close();
 125                 } catch (IOException e) {
 126                     // ignore
 127                 }
 128             }
 129         }
 130     }
 131 }