test/jdk/com/sun/jndi/ldap/RemoveNamingListenerTest.java
branchJDK-8210696-branch
changeset 57345 ff884a2f247b
parent 47216 71c04702a3d5
child 57346 3efc6cb7ffdb
equal deleted inserted replaced
57343:9a11a7e1c035 57345:ff884a2f247b
     1 /*
     1 /*
     2  * Copyright (c) 2011, 2017, 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.
    18  *
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 import java.io.BufferedInputStream;
    23 
    24 import java.io.IOException;
    24 import java.io.IOException;
    25 import java.io.OutputStreamWriter;
    25 import java.net.InetAddress;
    26 import java.io.PrintWriter;
       
    27 import java.net.ServerSocket;
    26 import java.net.ServerSocket;
    28 import java.net.Socket;
       
    29 import java.nio.charset.StandardCharsets;
       
    30 import java.util.ConcurrentModificationException;
    27 import java.util.ConcurrentModificationException;
    31 import java.util.Hashtable;
    28 import java.util.Hashtable;
    32 import javax.naming.Context;
    29 import javax.naming.Context;
    33 import javax.naming.InitialContext;
    30 import javax.naming.InitialContext;
    34 import javax.naming.NamingException;
    31 import javax.naming.NamingException;
    42  * @test
    39  * @test
    43  * @bug 8176192
    40  * @bug 8176192
    44  * @summary Incorrect usage of Iterator in Java 8 In com.sun.jndi.ldap.
    41  * @summary Incorrect usage of Iterator in Java 8 In com.sun.jndi.ldap.
    45  * EventSupport.removeNamingListener
    42  * EventSupport.removeNamingListener
    46  * @modules java.naming
    43  * @modules java.naming
       
    44  * @library lib/
    47  * @run main RemoveNamingListenerTest
    45  * @run main RemoveNamingListenerTest
    48  */
    46  */
    49 public class RemoveNamingListenerTest {
    47 public class RemoveNamingListenerTest {
    50 
    48 
    51     private static volatile Exception exception;
    49     private static volatile Exception exception;
   128             //do nothing
   126             //do nothing
   129         }
   127         }
   130     }
   128     }
   131 }
   129 }
   132 
   130 
   133 class TestLDAPServer extends Thread {
   131 class TestLDAPServer extends BaseLdapServer {
   134 
   132 
   135     private final int LDAP_PORT;
   133     private byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
   136     private final ServerSocket serverSocket;
   134     private byte[] searchResponse = {0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
   137     private volatile boolean isRunning;
       
   138 
   135 
   139     TestLDAPServer() throws IOException {
   136     public TestLDAPServer() throws IOException {
   140         serverSocket = new ServerSocket(0);
   137         super(new ServerSocket(0, 0, InetAddress.getLoopbackAddress()), true);
   141         isRunning = true;
   138         setCommonRequestHandler((msg, out) -> {
   142         LDAP_PORT = serverSocket.getLocalPort();
   139             switch (msg.getOperation()) {
   143         setDaemon(true);
   140                 case BIND_REQUEST:
   144     }
   141                     // Write an LDAP BindResponse
   145 
   142                     out.write(bindResponse);
   146     public int getPort() {
   143                     break;
   147         return LDAP_PORT;
   144                 case SEARCH_REQUEST:
   148     }
   145                     // Write an LDAP SearchResponse
   149 
   146                     out.write(searchResponse);
   150     public void stopServer() {
   147                     break;
   151         isRunning = false;
   148                 default:
   152         if (serverSocket != null && !serverSocket.isClosed()) {
   149                     break;
   153             try {
       
   154                 // this will cause ServerSocket.accept() to throw SocketException.
       
   155                 serverSocket.close();
       
   156             } catch (IOException ignored) {
       
   157             }
   150             }
   158         }
   151         });
   159     }
       
   160 
       
   161     @Override
       
   162     public void run() {
       
   163         try {
       
   164             while (isRunning) {
       
   165                 Socket clientSocket = serverSocket.accept();
       
   166                 Thread handler = new Thread(new LDAPServerHandler(clientSocket));
       
   167                 handler.setDaemon(true);
       
   168                 handler.start();
       
   169             }
       
   170         } catch (IOException iOException) {
       
   171             //do not throw exception if server is not running.
       
   172             if (isRunning) {
       
   173                 throw new RuntimeException(iOException);
       
   174             }
       
   175         } finally {
       
   176             stopServer();
       
   177         }
       
   178     }
   152     }
   179 }
   153 }
   180 
       
   181 class LDAPServerHandler implements Runnable {
       
   182 
       
   183     private final Socket clientSocket;
       
   184 
       
   185     public LDAPServerHandler(final Socket clientSocket) {
       
   186         this.clientSocket = clientSocket;
       
   187     }
       
   188 
       
   189     @Override
       
   190     public void run() {
       
   191         BufferedInputStream in = null;
       
   192         PrintWriter out = null;
       
   193         byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
       
   194         byte[] searchResponse = {0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
       
   195         try {
       
   196             in = new BufferedInputStream(clientSocket.getInputStream());
       
   197             out = new PrintWriter(new OutputStreamWriter(
       
   198                     clientSocket.getOutputStream(), StandardCharsets.UTF_8), true);
       
   199             while (true) {
       
   200 
       
   201                 // Read the LDAP BindRequest
       
   202                 while (in.read() != -1) {
       
   203                     in.skip(in.available());
       
   204                     break;
       
   205                 }
       
   206 
       
   207                 // Write an LDAP BindResponse
       
   208                 out.write(new String(bindResponse));
       
   209                 out.flush();
       
   210 
       
   211                 // Read the LDAP SearchRequest
       
   212                 while (in.read() != -1) {
       
   213                     in.skip(in.available());
       
   214                     break;
       
   215                 }
       
   216 
       
   217                 // Write an LDAP SearchResponse
       
   218                 out.write(new String(searchResponse));
       
   219                 out.flush();
       
   220             }
       
   221         } catch (IOException iOException) {
       
   222             throw new RuntimeException(iOException);
       
   223         } finally {
       
   224             if (in != null) {
       
   225                 try {
       
   226                     in.close();
       
   227                 } catch (IOException ignored) {
       
   228                 }
       
   229             }
       
   230             if (out != null) {
       
   231                 out.close();
       
   232             }
       
   233             if (clientSocket != null) {
       
   234                 try {
       
   235                     clientSocket.close();
       
   236                 } catch (IOException ignored) {
       
   237                 }
       
   238             }
       
   239         }
       
   240     }
       
   241 }