changeset 58925:fa57d1ce1e1b

8242614: cleanup duplicated test ldap server in some com/sun/jndi/ldap/ tests Reviewed-by: aefimov, vtewari, dfuchs
author xyin
date Wed, 22 Apr 2020 09:44:10 +0800
parents c90ca650da3f
children 64ecf204bebf
files test/jdk/com/sun/jndi/ldap/DisconnectNPETest.java test/jdk/com/sun/jndi/ldap/LdapName/EmptyNameSearch.java test/jdk/com/sun/jndi/ldap/NoWaitForReplyTest.java
diffstat 3 files changed, 74 insertions(+), 207 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/com/sun/jndi/ldap/DisconnectNPETest.java	Tue Apr 21 21:25:26 2020 -0400
+++ b/test/jdk/com/sun/jndi/ldap/DisconnectNPETest.java	Wed Apr 22 09:44:10 2020 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,14 +25,13 @@
 import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.InitialDirContext;
-import java.io.Closeable;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.Hashtable;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * @test
@@ -41,6 +40,7 @@
  *          the LDAP directory server sends an (unsolicited)
  *          "Notice of Disconnection", make sure client handle it correctly,
  *          no NPE been thrown.
+ * @library lib/ /test/lib
  * @run main/othervm DisconnectNPETest
  */
 
@@ -49,27 +49,39 @@
     // case, we set repeat count to 1000 here.
     private static final int REPEAT_COUNT = 1000;
 
+    // "Notice of Disconnection" message
+    private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,
+            0x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,
+            0x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,
+            0x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,
+            0x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,
+            0x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,
+            (byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,
+            0x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,
+            0x32, 0x30, 0x30, 0x33, 0x36 };
+    private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,
+            0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
+
     public static void main(String[] args) throws IOException {
         new DisconnectNPETest().run();
     }
 
     private ServerSocket serverSocket;
     private Hashtable<Object, Object> env;
-    private TestLDAPServer server;
 
     private void initRes() throws IOException {
         serverSocket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
-        server = new TestLDAPServer();
-        server.start();
     }
 
     private void initTest() {
         env = new Hashtable<>();
         env.put(Context.INITIAL_CONTEXT_FACTORY,
                 "com.sun.jndi.ldap.LdapCtxFactory");
-        env.put(Context.PROVIDER_URL, String.format("ldap://%s:%d/",
-                InetAddress.getLoopbackAddress().getHostName(),
-                serverSocket.getLocalPort()));
+        env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()
+                        .scheme("ldap")
+                        .loopback()
+                        .port(serverSocket.getLocalPort())
+                        .buildUnchecked().toString());
         env.put(Context.SECURITY_AUTHENTICATION, "simple");
         env.put(Context.SECURITY_PRINCIPAL,
                 "cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US");
@@ -80,7 +92,17 @@
         initRes();
         initTest();
         int count = 0;
-        try {
+        try (var ignore = new BaseLdapServer(serverSocket) {
+            @Override
+            protected void handleRequest(Socket socket, LdapMessage request,
+                    OutputStream out) throws IOException {
+                if (request.getOperation()
+                        == LdapMessage.Operation.BIND_REQUEST) {
+                    out.write(BIND_RESPONSE);
+                    out.write(DISCONNECT_MSG);
+                }
+            }
+        }.start()) {
             while (count < REPEAT_COUNT) {
                 count++;
                 InitialDirContext context = null;
@@ -97,17 +119,9 @@
             }
         } finally {
             System.out.println("Test count: " + count + "/" + REPEAT_COUNT);
-            cleanupTest();
         }
     }
 
-    private void cleanupTest() {
-        if (server != null) {
-            server.stopServer();
-        }
-        cleanupClosableRes(serverSocket);
-    }
-
     private void cleanupContext(DirContext context) {
         if (context != null) {
             try {
@@ -117,77 +131,4 @@
             }
         }
     }
-
-    private static void cleanupClosableRes(Closeable res) {
-        if (res != null) {
-            try {
-                res.close();
-            } catch (Exception e) {
-                // ignore
-            }
-        }
-    }
-
-    class TestLDAPServer extends Thread {
-        private volatile boolean isRunning;
-
-        TestLDAPServer() {
-            isRunning = true;
-        }
-
-        private void stopServer() {
-            isRunning = false;
-        }
-
-        @Override
-        public void run() {
-            try {
-                while (isRunning) {
-                    Socket clientSocket = serverSocket.accept();
-                    Thread handler = new Thread(
-                            new LDAPServerHandler(clientSocket));
-                    handler.start();
-                }
-            } catch (IOException e) {
-                if (isRunning) {
-                    throw new RuntimeException(e);
-                }
-            }
-        }
-    }
-
-    static class LDAPServerHandler implements Runnable {
-        // "Notice of Disconnection" message
-        private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,
-                0x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,
-                0x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,
-                0x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,
-                0x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,
-                0x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,
-                (byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,
-                0x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,
-                0x32, 0x30, 0x30, 0x33, 0x36 };
-        private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,
-                0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
-        private final Socket clientSocket;
-
-        private LDAPServerHandler(final Socket clientSocket) {
-            this.clientSocket = clientSocket;
-        }
-
-        @Override
-        public void run() {
-            try (clientSocket;
-                    OutputStream out = clientSocket.getOutputStream();
-                    InputStream in = clientSocket.getInputStream()) {
-                if (in.read() > 0) {
-                    in.skip(in.available());
-                    out.write(BIND_RESPONSE);
-                    out.write(DISCONNECT_MSG);
-                }
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-    }
 }
--- a/test/jdk/com/sun/jndi/ldap/LdapName/EmptyNameSearch.java	Tue Apr 21 21:25:26 2020 -0400
+++ b/test/jdk/com/sun/jndi/ldap/LdapName/EmptyNameSearch.java	Wed Apr 22 09:44:10 2020 +0800
@@ -25,33 +25,61 @@
  * @test
  * @bug 6997561
  * @summary A request for better error handling in JNDI
+ * @library ../lib/ /test/lib
  */
 
 import java.net.Socket;
-import java.net.ServerSocket;
 import java.io.*;
 import javax.naming.*;
 import javax.naming.directory.*;
 import javax.naming.ldap.*;
 import java.util.Collections;
 import java.util.Hashtable;
+import jdk.test.lib.net.URIBuilder;
 
 public class EmptyNameSearch {
 
+    private static final byte[] bindResponse = {
+            0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
+            0x01, 0x00, 0x04, 0x00, 0x04, 0x00
+    };
+    private static final byte[] searchResponse = {
+            0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,
+            0x01, 0x02, 0x04, 0x00, 0x04, 0x00
+    };
+
     public static void main(String[] args) throws Exception {
 
         // Start the LDAP server
-        Server s = new Server();
-        s.start();
+        var ldapServer = new BaseLdapServer() {
+            @Override
+            protected void handleRequest(Socket socket, LdapMessage request,
+                    OutputStream out) throws IOException {
+                switch (request.getOperation()) {
+                    case BIND_REQUEST:
+                        out.write(bindResponse);
+                        break;
+                    case SEARCH_REQUEST:
+                        out.write(searchResponse);
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }.start();
         Thread.sleep(3000);
 
         // Setup JNDI parameters
         Hashtable<Object, Object> env = new Hashtable<>();
         env.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.sun.jndi.ldap.LdapCtxFactory");
-        env.put(Context.PROVIDER_URL, "ldap://localhost:" + s.getPortNumber());
+        env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()
+                .scheme("ldap")
+                .loopback()
+                .port(ldapServer.getPort())
+                .build().toString());
 
-        try {
+        try (ldapServer) {
 
             // Create initial context
             System.out.println("Client: connecting...");
@@ -72,70 +100,4 @@
             throw e;
         }
     }
-
-    static class Server extends Thread {
-
-        private int serverPort = 0;
-        private byte[] bindResponse = {
-            0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
-            0x01, 0x00, 0x04, 0x00, 0x04, 0x00
-        };
-        private byte[] searchResponse = {
-            0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A,
-            0x01, 0x02, 0x04, 0x00, 0x04, 0x00
-        };
-
-        Server() {
-        }
-
-        public int getPortNumber() {
-            return serverPort;
-        }
-
-        public void run() {
-            try {
-                ServerSocket serverSock = new ServerSocket(0);
-                serverPort = serverSock.getLocalPort();
-                System.out.println("Server: listening on port " + serverPort);
-
-                Socket socket = serverSock.accept();
-                System.out.println("Server: connection accepted");
-
-                InputStream in = socket.getInputStream();
-                OutputStream out = socket.getOutputStream();
-
-                // Read the LDAP BindRequest
-                System.out.println("Server: reading request...");
-                while (in.read() != -1) {
-                    in.skip(in.available());
-                    break;
-                }
-
-                // Write an LDAP BindResponse
-                System.out.println("Server: writing response...");
-                out.write(bindResponse);
-                out.flush();
-
-                // Read the LDAP SearchRequest
-                System.out.println("Server: reading request...");
-                while (in.read() != -1) {
-                    in.skip(in.available());
-                    break;
-                }
-
-                // Write an LDAP SearchResponse
-                System.out.println("Server: writing response...");
-                out.write(searchResponse);
-                out.flush();
-
-                in.close();
-                out.close();
-                socket.close();
-                serverSock.close();
-
-            } catch (IOException e) {
-                // ignore
-            }
-        }
-    }
 }
--- a/test/jdk/com/sun/jndi/ldap/NoWaitForReplyTest.java	Tue Apr 21 21:25:26 2020 -0400
+++ b/test/jdk/com/sun/jndi/ldap/NoWaitForReplyTest.java	Wed Apr 22 09:44:10 2020 +0800
@@ -25,14 +25,13 @@
  * @test
  * @bug 6748156
  * @summary add an new JNDI property to control the boolean flag WaitForReply
+ * @library lib/ /test/lib
  */
 
-import java.net.Socket;
-import java.net.ServerSocket;
-import java.io.*;
 import javax.naming.*;
 import javax.naming.directory.*;
 import java.util.Hashtable;
+import jdk.test.lib.net.URIBuilder;
 
 public class NoWaitForReplyTest {
 
@@ -41,13 +40,15 @@
         boolean passed = false;
 
         // start the LDAP server
-        DummyServer ldapServer = new DummyServer();
-        ldapServer.start();
+        var ldapServer = new BaseLdapServer().start();
 
         // Set up the environment for creating the initial context
         Hashtable<Object, Object> env = new Hashtable<>(11);
-        env.put(Context.PROVIDER_URL, "ldap://localhost:" +
-            ldapServer.getPortNumber());
+        env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()
+                .scheme("ldap")
+                .loopback()
+                .port(ldapServer.getPort())
+                .build().toString());
         env.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.sun.jndi.ldap.LdapCtxFactory");
 
@@ -61,7 +62,7 @@
         env.put("java.naming.ldap.version", "3");
 
 
-        try {
+        try (ldapServer) {
 
             // Create initial context
             System.out.println("Client: connecting to the server");
@@ -84,7 +85,6 @@
         } catch (NamingException e) {
             // timeout (ignore)
         }
-        ldapServer.interrupt();
 
         if (!passed) {
             throw new Exception(
@@ -92,40 +92,4 @@
         }
         System.out.println("Test PASSED");
     }
-
-    static class DummyServer extends Thread {
-
-        private final ServerSocket serverSocket;
-
-        DummyServer() throws IOException {
-            this.serverSocket = new ServerSocket(0);
-            System.out.println("Server: listening on port " + serverSocket.getLocalPort());
-        }
-
-        public int getPortNumber() {
-            return serverSocket.getLocalPort();
-        }
-
-        public void run() {
-            try (Socket socket = serverSocket.accept()) {
-                System.out.println("Server: accepted a connection");
-                InputStream in = socket.getInputStream();
-
-                while (!isInterrupted()) {
-                   in.skip(in.available());
-                }
-
-            } catch (Exception e) {
-                // ignore
-
-            } finally {
-                System.out.println("Server: shutting down");
-                try {
-                    serverSocket.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
-        }
-    }
 }