jdk/test/com/sun/jndi/ldap/NoWaitForReplyTest.java
changeset 8564 d99f879a35ab
child 8574 8b25e0b1d00e
equal deleted inserted replaced
8561:ca8d6ccdd9dc 8564:d99f879a35ab
       
     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 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     // Set up the environment for creating the initial context
       
    44     Hashtable env = new Hashtable(11);
       
    45         env.put(Context.PROVIDER_URL, "ldap://localhost:22001");
       
    46     env.put(Context.INITIAL_CONTEXT_FACTORY,
       
    47         "com.sun.jndi.ldap.LdapCtxFactory");
       
    48 
       
    49     // Wait up to 10 seconds for a response from the LDAP server
       
    50     env.put("com.sun.jndi.ldap.read.timeout", "10000");
       
    51 
       
    52     // Don't wait until the first search reply is received
       
    53     env.put("com.sun.jndi.ldap.search.waitForReply", "false");
       
    54 
       
    55     // Send the LDAP search request without first authenticating (no bind)
       
    56     env.put("java.naming.ldap.version", "3");
       
    57 
       
    58     DummyServer ldapServer = new DummyServer();
       
    59 
       
    60     try {
       
    61 
       
    62         // start the LDAP server
       
    63         ldapServer.start();
       
    64 
       
    65         // Create initial context
       
    66         System.out.println("Client: connecting to the server");
       
    67         DirContext ctx = new InitialDirContext(env);
       
    68 
       
    69         SearchControls scl = new SearchControls();
       
    70         scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
       
    71         System.out.println("Client: performing search");
       
    72         NamingEnumeration answer =
       
    73         ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
       
    74 
       
    75         // Server will never reply: either we waited in the call above until
       
    76         // the timeout (fail) or we did not wait and reached here (pass).
       
    77         passed = true;
       
    78         System.out.println("Client: did not wait until first reply");
       
    79 
       
    80         // Close the context when we're done
       
    81         ctx.close();
       
    82 
       
    83     } catch (NamingException e) {
       
    84         // timeout (ignore)
       
    85     }
       
    86     ldapServer.interrupt();
       
    87 
       
    88     if (!passed) {
       
    89         throw new Exception(
       
    90         "Test FAILED: should not have waited until first search reply");
       
    91     }
       
    92     System.out.println("Test PASSED");
       
    93     }
       
    94 
       
    95     static class DummyServer extends Thread {
       
    96 
       
    97         static int serverPort = 22001;
       
    98 
       
    99     DummyServer() {
       
   100     }
       
   101 
       
   102     public void run() {
       
   103         try {
       
   104         ServerSocket serverSock = new ServerSocket(serverPort);
       
   105                 Socket socket = serverSock.accept();
       
   106                 System.out.println("Server: accepted a connection");
       
   107                 BufferedInputStream bin =
       
   108             new BufferedInputStream(socket.getInputStream());
       
   109 
       
   110                 while (true) {
       
   111                     bin.read();
       
   112                 }
       
   113         } catch (IOException e) {
       
   114         // ignore
       
   115         }
       
   116     }
       
   117 }
       
   118 }