test/jdk/com/sun/jndi/ldap/NoWaitForReplyTest.java
branchJDK-8210696-branch
changeset 57345 ff884a2f247b
parent 47216 71c04702a3d5
equal deleted inserted replaced
57343:9a11a7e1c035 57345:ff884a2f247b
     1 /*
     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 6748156
    26  * @bug 6748156
       
    27  * @library lib/
    27  * @summary add an new JNDI property to control the boolean flag WaitForReply
    28  * @summary add an new JNDI property to control the boolean flag WaitForReply
    28  */
    29  */
    29 
    30 
    30 import java.net.Socket;
       
    31 import java.net.ServerSocket;
       
    32 import java.io.*;
       
    33 import javax.naming.*;
    31 import javax.naming.*;
    34 import javax.naming.directory.*;
    32 import javax.naming.directory.*;
    35 import java.util.Hashtable;
    33 import java.util.Hashtable;
    36 
    34 
    37 public class NoWaitForReplyTest {
    35 public class NoWaitForReplyTest {
    39     public static void main(String[] args) throws Exception {
    37     public static void main(String[] args) throws Exception {
    40 
    38 
    41         boolean passed = false;
    39         boolean passed = false;
    42 
    40 
    43         // start the LDAP server
    41         // start the LDAP server
    44         DummyServer ldapServer = new DummyServer();
    42         BaseLdapServer ldapServer = new BaseLdapServer()
    45         ldapServer.start();
    43                 .setDebugLevel(BaseLdapServer.DebugLevel.FULL).startServer();
    46 
    44 
    47         // Set up the environment for creating the initial context
    45         // Set up the environment for creating the initial context
    48         Hashtable env = new Hashtable(11);
    46         Hashtable<String, Object> env = new Hashtable<>(11);
    49         env.put(Context.PROVIDER_URL, "ldap://localhost:" +
    47         env.put(Context.PROVIDER_URL, "ldap://localhost:" +
    50             ldapServer.getPortNumber());
    48                 ldapServer.getPort());
    51         env.put(Context.INITIAL_CONTEXT_FACTORY,
    49         env.put(Context.INITIAL_CONTEXT_FACTORY,
    52             "com.sun.jndi.ldap.LdapCtxFactory");
    50             "com.sun.jndi.ldap.LdapCtxFactory");
    53 
    51 
    54         // Wait up to 10 seconds for a response from the LDAP server
    52         // Wait up to 10 seconds for a response from the LDAP server
    55         env.put("com.sun.jndi.ldap.read.timeout", "10000");
    53         env.put("com.sun.jndi.ldap.read.timeout", "10000");
    59 
    57 
    60         // Send the LDAP search request without first authenticating (no bind)
    58         // Send the LDAP search request without first authenticating (no bind)
    61         env.put("java.naming.ldap.version", "3");
    59         env.put("java.naming.ldap.version", "3");
    62 
    60 
    63 
    61 
    64         try {
    62         try (ldapServer) {
    65 
    63 
    66             // Create initial context
    64             // Create initial context
    67             System.out.println("Client: connecting to the server");
    65             System.out.println("Client: connecting to the server");
    68             DirContext ctx = new InitialDirContext(env);
    66             DirContext ctx = new InitialDirContext(env);
    69 
    67 
    70             SearchControls scl = new SearchControls();
    68             SearchControls scl = new SearchControls();
    71             scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    69             scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    72             System.out.println("Client: performing search");
    70             System.out.println("Client: performing search");
    73             NamingEnumeration answer =
    71             NamingEnumeration<?> answer =
    74             ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
    72             ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
    75 
    73 
    76             // Server will never reply: either we waited in the call above until
    74             // 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).
    75             // the timeout (fail) or we did not wait and reached here (pass).
    78             passed = true;
    76             passed = true;
    82             ctx.close();
    80             ctx.close();
    83 
    81 
    84         } catch (NamingException e) {
    82         } catch (NamingException e) {
    85             // timeout (ignore)
    83             // timeout (ignore)
    86         }
    84         }
    87         ldapServer.interrupt();
       
    88 
    85 
    89         if (!passed) {
    86         if (!passed) {
    90             throw new Exception(
    87             throw new Exception(
    91                 "Test FAILED: should not have waited until first search reply");
    88                 "Test FAILED: should not have waited until first search reply");
    92         }
    89         }
    93         System.out.println("Test PASSED");
    90         System.out.println("Test PASSED");
    94     }
    91     }
    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 }
    92 }