jdk/test/com/sun/jndi/ldap/BalancedParentheses.java
changeset 3325 0e7d9c6c9994
child 3330 04c1ec47b42e
equal deleted inserted replaced
3324:02cc89024ea2 3325:0e7d9c6c9994
       
     1 /**
       
     2  * @test
       
     3  * @bug 6449574
       
     4  * @summary Invalid ldap filter is accepted and processed
       
     5  */
       
     6 
       
     7 import java.io.*;
       
     8 import javax.naming.*;
       
     9 import javax.naming.directory.*;
       
    10 import java.util.Properties;
       
    11 import java.util.Hashtable;
       
    12 
       
    13 import java.net.Socket;
       
    14 import java.net.ServerSocket;
       
    15 
       
    16 public class BalancedParentheses {
       
    17     // Should we run the client or server in a separate thread?
       
    18     //
       
    19     // Both sides can throw exceptions, but do you have a preference
       
    20     // as to which side should be the main thread.
       
    21     static boolean separateServerThread = true;
       
    22 
       
    23     // use any free port by default
       
    24     volatile int serverPort = 0;
       
    25 
       
    26     // Is the server ready to serve?
       
    27     volatile static boolean serverReady = false;
       
    28 
       
    29     // Define the server side of the test.
       
    30     //
       
    31     // If the server prematurely exits, serverReady will be set to true
       
    32     // to avoid infinite hangs.
       
    33     void doServerSide() throws Exception {
       
    34         ServerSocket serverSock = new ServerSocket(serverPort);
       
    35 
       
    36         // signal client, it's ready to accecpt connection
       
    37         serverPort = serverSock.getLocalPort();
       
    38         serverReady = true;
       
    39 
       
    40         // accept a connection
       
    41         Socket socket = serverSock.accept();
       
    42         System.out.println("Server: Connection accepted");
       
    43 
       
    44         InputStream is = socket.getInputStream();
       
    45         OutputStream os = socket.getOutputStream();
       
    46 
       
    47         // read the bindRequest
       
    48         while (is.read() != -1) {
       
    49             // ignore
       
    50             is.skip(is.available());
       
    51             break;
       
    52         }
       
    53 
       
    54         byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
       
    55                                0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
       
    56         // write bindResponse
       
    57         os.write(bindResponse);
       
    58         os.flush();
       
    59 
       
    60         // ignore any more request.
       
    61         while (is.read() != -1) {
       
    62             // ignore
       
    63             is.skip(is.available());
       
    64         }
       
    65 
       
    66         is.close();
       
    67         os.close();
       
    68         socket.close();
       
    69         serverSock.close();
       
    70     }
       
    71 
       
    72     //  Define the client side of the test.
       
    73     //
       
    74     // If the server prematurely exits, serverReady will be set to true
       
    75     // to avoid infinite hangs.
       
    76     void doClientSide() throws Exception {
       
    77         // Wait for server to get started.
       
    78         while (!serverReady) {
       
    79             Thread.sleep(50);
       
    80         }
       
    81 
       
    82         // set up the environment for creating the initial context
       
    83         Hashtable<Object, Object> env = new Hashtable<Object, Object>();
       
    84         env.put(Context.INITIAL_CONTEXT_FACTORY,
       
    85                                 "com.sun.jndi.ldap.LdapCtxFactory");
       
    86         env.put(Context.PROVIDER_URL, "ldap://localhost:" + serverPort);
       
    87         env.put("com.sun.jndi.ldap.read.timeout", "1000");
       
    88 
       
    89         // env.put(Context.SECURITY_AUTHENTICATION, "simple");
       
    90         // env.put(Context.SECURITY_PRINCIPAL,"cn=root");
       
    91         // env.put(Context.SECURITY_CREDENTIALS,"root");
       
    92 
       
    93         // create initial context
       
    94         DirContext context = new InitialDirContext(env);
       
    95 
       
    96         // searching
       
    97         SearchControls scs = new SearchControls();
       
    98         scs.setSearchScope(SearchControls.SUBTREE_SCOPE);
       
    99 
       
   100         try {
       
   101             NamingEnumeration answer = context.search(
       
   102                                         "o=sun,c=us", "(&(cn=Bob)))", scs);
       
   103         } catch (InvalidSearchFilterException isfe) {
       
   104             // ignore, it is the expected filter exception.
       
   105             System.out.println("Expected exception: " + isfe.getMessage());
       
   106         } catch (NamingException ne) {
       
   107             // maybe a read timeout exception, as the server does not response.
       
   108             throw new Exception("Expect a InvalidSearchFilterException", ne);
       
   109         }
       
   110 
       
   111         try {
       
   112             NamingEnumeration answer = context.search(
       
   113                                         "o=sun,c=us", ")(&(cn=Bob)", scs);
       
   114         } catch (InvalidSearchFilterException isfe) {
       
   115             // ignore, it is the expected filter exception.
       
   116             System.out.println("Expected exception: " + isfe.getMessage());
       
   117         } catch (NamingException ne) {
       
   118             // maybe a read timeout exception, as the server does not response.
       
   119             throw new Exception("Expect a InvalidSearchFilterException", ne);
       
   120         }
       
   121 
       
   122         try {
       
   123             NamingEnumeration answer = context.search(
       
   124                                         "o=sun,c=us", "(&(cn=Bob))", scs);
       
   125         } catch (InvalidSearchFilterException isfe) {
       
   126             // ignore, it is the expected filter exception.
       
   127             throw new Exception("Unexpected ISFE", isfe);
       
   128         } catch (NamingException ne) {
       
   129             // maybe a read timeout exception, as the server does not response.
       
   130             System.out.println("Expected exception: " + ne.getMessage());
       
   131         }
       
   132 
       
   133         context.close();
       
   134     }
       
   135 
       
   136     /*
       
   137      * ============================================================
       
   138      * The remainder is just support stuff
       
   139      */
       
   140 
       
   141     // client and server thread
       
   142     Thread clientThread = null;
       
   143     Thread serverThread = null;
       
   144 
       
   145     // client and server exceptions
       
   146     volatile Exception serverException = null;
       
   147     volatile Exception clientException = null;
       
   148 
       
   149     void startServer(boolean newThread) throws Exception {
       
   150         if (newThread) {
       
   151             serverThread = new Thread() {
       
   152                 public void run() {
       
   153                     try {
       
   154                         doServerSide();
       
   155                     } catch (Exception e) {
       
   156                         /*
       
   157                          * Our server thread just died.
       
   158                          *
       
   159                          * Release the client, if not active already...
       
   160                          */
       
   161                         System.err.println("Server died...");
       
   162                         System.err.println(e);
       
   163                         serverReady = true;
       
   164                         serverException = e;
       
   165                     }
       
   166                 }
       
   167             };
       
   168             serverThread.start();
       
   169         } else {
       
   170             doServerSide();
       
   171         }
       
   172     }
       
   173 
       
   174     void startClient(boolean newThread) throws Exception {
       
   175         if (newThread) {
       
   176             clientThread = new Thread() {
       
   177                 public void run() {
       
   178                     try {
       
   179                         doClientSide();
       
   180                     } catch (Exception e) {
       
   181                         /*
       
   182                          * Our client thread just died.
       
   183                          */
       
   184                         System.err.println("Client died...");
       
   185                         clientException = e;
       
   186                     }
       
   187                 }
       
   188             };
       
   189             clientThread.start();
       
   190         } else {
       
   191             doClientSide();
       
   192         }
       
   193     }
       
   194 
       
   195     // Primary constructor, used to drive remainder of the test.
       
   196     BalancedParentheses() throws Exception {
       
   197         if (separateServerThread) {
       
   198             startServer(true);
       
   199             startClient(false);
       
   200         } else {
       
   201             startClient(true);
       
   202             startServer(false);
       
   203         }
       
   204 
       
   205         /*
       
   206          * Wait for other side to close down.
       
   207          */
       
   208         if (separateServerThread) {
       
   209             serverThread.join();
       
   210         } else {
       
   211             clientThread.join();
       
   212         }
       
   213 
       
   214         /*
       
   215          * When we get here, the test is pretty much over.
       
   216          *
       
   217          * If the main thread excepted, that propagates back
       
   218          * immediately.  If the other thread threw an exception, we
       
   219          * should report back.
       
   220          */
       
   221         if (serverException != null) {
       
   222             System.out.print("Server Exception:");
       
   223             throw serverException;
       
   224         }
       
   225         if (clientException != null) {
       
   226             System.out.print("Client Exception:");
       
   227             throw clientException;
       
   228         }
       
   229     }
       
   230 
       
   231     public static void main(String[] args) throws Exception {
       
   232         // start the test
       
   233         new BalancedParentheses();
       
   234     }
       
   235 
       
   236 }