diff --git a/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java b/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java
index 008a8fc3c2d..cdb77128357 100644
--- a/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java
+++ b/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, 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
@@ -76,7 +76,7 @@ public class DefaultLdapDnsProvider {
}
LdapDnsProviderResult res = new LdapDnsProviderResult(domainName, endpoints);
- if (res.getEndpoints().size() == 0 && res.getDomainName().isEmpty()) {
+ if (res.getEndpoints().isEmpty() && res.getDomainName().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(res);
diff --git a/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java b/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
index d499b4c4831..1d6ac817f0b 100644
--- a/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
+++ b/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, 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
@@ -37,6 +37,8 @@ import sun.security.util.SecurityConstants;
* The {@code LdapDnsProviderService} is responsible for creating and providing
* access to the registered {@code LdapDnsProvider}s. The {@link ServiceLoader}
* is used to find and register any implementations of {@link LdapDnsProvider}.
+ *
+ *
Instances of this class are safe for use by multiple threads.
*/
final class LdapDnsProviderService {
@@ -68,11 +70,11 @@ final class LdapDnsProviderService {
}
/**
- * Retrieve the singleton static instance of LdapDnsProviderService.
+ * Retrieves the singleton instance of LdapDnsProviderService.
*/
static LdapDnsProviderService getInstance() {
if (service != null) return service;
- synchronized(LOCK) {
+ synchronized (LOCK) {
if (service != null) return service;
service = new LdapDnsProviderService();
}
@@ -80,7 +82,7 @@ final class LdapDnsProviderService {
}
/**
- * Retrieve result from the first provider that successfully resolves
+ * Retrieves result from the first provider that successfully resolves
* the endpoints. If no results are found when calling installed
* subclasses of {@code LdapDnsProvider} then this method will fall back
* to the {@code DefaultLdapDnsProvider}.
@@ -91,14 +93,15 @@ final class LdapDnsProviderService {
LdapDnsProviderResult lookupEndpoints(String url, Hashtable,?> env)
throws NamingException
{
- Iterator iterator = providers.iterator();
- Hashtable, ?> envCopy = new Hashtable<>(env);
LdapDnsProviderResult result = null;
-
- while (result == null && iterator.hasNext()) {
- result = iterator.next().lookupEndpoints(url, envCopy)
- .filter(r -> r.getEndpoints().size() > 0)
- .orElse(null);
+ Hashtable, ?> envCopy = new Hashtable<>(env);
+ synchronized (LOCK) {
+ Iterator iterator = providers.iterator();
+ while (result == null && iterator.hasNext()) {
+ result = iterator.next().lookupEndpoints(url, envCopy)
+ .filter(r -> !r.getEndpoints().isEmpty())
+ .orElse(null);
+ }
}
if (result == null) {
diff --git a/test/jdk/ProblemList.txt b/test/jdk/ProblemList.txt
index 055fb492909..6b399dab33a 100644
--- a/test/jdk/ProblemList.txt
+++ b/test/jdk/ProblemList.txt
@@ -872,8 +872,6 @@ sun/tools/jhsdb/HeapDumpTest.java 8193639 solaris-
com/sun/jndi/ldap/DeadSSLLdapTimeoutTest.java 8169942 linux-i586,macosx-all,windows-x64
-com/sun/jndi/ldap/LdapTimeoutTest.java 8151678 generic-all
-
com/sun/jndi/dns/ConfigTests/PortUnreachable.java 7164518 macosx-all
javax/rmi/ssl/SSLSocketParametersTest.sh 8162906 generic-all
diff --git a/test/jdk/com/sun/jndi/ldap/LdapTimeoutTest.java b/test/jdk/com/sun/jndi/ldap/LdapTimeoutTest.java
index 518dfc8f622..39bf24b9dbf 100644
--- a/test/jdk/com/sun/jndi/ldap/LdapTimeoutTest.java
+++ b/test/jdk/com/sun/jndi/ldap/LdapTimeoutTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, 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
@@ -21,424 +21,487 @@
* questions.
*/
-/**
+/*
* @test
- * @run main/othervm LdapTimeoutTest
- * @bug 7094377 8000487 6176036 7056489
+ * @library /test/lib
+ * lib/
+ * @run testng/othervm LdapTimeoutTest
+ * @bug 7094377 8000487 6176036 7056489 8151678
* @summary Timeout tests for ldap
*/
-import java.net.Socket;
-import java.net.ServerSocket;
-import java.net.SocketTimeoutException;
-import java.io.*;
-import javax.naming.*;
-import javax.naming.directory.*;
-import java.util.List;
-import java.util.Hashtable;
-import java.util.ArrayList;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeoutException;
-import java.util.concurrent.TimeUnit;
-import javax.net.ssl.SSLHandshakeException;
+import org.testng.Assert;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static java.lang.String.format;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
-
-
-abstract class LdapTest implements Callable {
-
- Hashtable env;
- TestServer server;
- ScheduledExecutorService killSwitchPool;
- boolean passed = false;
- private int HANGING_TEST_TIMEOUT = 20_000;
-
- public LdapTest (TestServer server, Hashtable env) {
- this.server = server;
- this.env = env;
- }
-
- public LdapTest(TestServer server, Hashtable env,
- ScheduledExecutorService killSwitchPool)
- {
- this(server, env);
- this.killSwitchPool = killSwitchPool;
- }
-
- public abstract void performOp(InitialContext ctx) throws NamingException;
- public abstract void handleNamingException(
- NamingException e, long start, long end);
-
- public void pass() {
- this.passed = true;
- }
-
- public void fail() {
- throw new RuntimeException("Test failed");
- }
-
- public void fail(Exception e) {
- throw new RuntimeException("Test failed", e);
- }
-
- boolean shutItDown(InitialContext ctx) {
- try {
- if (ctx != null) ctx.close();
- return true;
- } catch (NamingException ex) {
- return false;
- }
- }
-
- public Boolean call() {
- InitialContext ctx = null;
- ScheduledFuture killer = null;
- long start = System.nanoTime();
-
- try {
- while(!server.accepting())
- Thread.sleep(200); // allow the server to start up
- Thread.sleep(200); // to be sure
-
- // if this is a hanging test, scheduled a thread to
- // interrupt after a certain time
- if (killSwitchPool != null) {
- final Thread current = Thread.currentThread();
- killer = killSwitchPool.schedule(
- new Callable() {
- public Void call() throws Exception {
- current.interrupt();
- return null;
- }
- }, HANGING_TEST_TIMEOUT, MILLISECONDS);
- }
-
- env.put(Context.PROVIDER_URL, "ldap://localhost:" +
- server.getLocalPort());
-
- try {
- ctx = new InitialDirContext(env);
- performOp(ctx);
- fail();
- } catch (NamingException e) {
- long end = System.nanoTime();
- System.out.println(this.getClass().toString() + " - elapsed: "
- + NANOSECONDS.toMillis(end - start));
- handleNamingException(e, start, end);
- } finally {
- if (killer != null && !killer.isDone())
- killer.cancel(true);
- shutItDown(ctx);
- server.close();
- }
- return passed;
- } catch (IOException|InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-}
-
-abstract class ReadServerTest extends LdapTest {
-
- public ReadServerTest(Hashtable env) throws IOException {
- super(new BindableServer(), env);
- }
-
- public ReadServerTest(Hashtable env,
- ScheduledExecutorService killSwitchPool)
- throws IOException
- {
- super(new BindableServer(), env, killSwitchPool);
- }
-
- public void performOp(InitialContext ctx) throws NamingException {
- SearchControls scl = new SearchControls();
- scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
- NamingEnumeration answer = ((InitialDirContext)ctx)
- .search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
- }
-}
-
-abstract class DeadServerTest extends LdapTest {
-
- public DeadServerTest(Hashtable env) throws IOException {
- super(new DeadServer(), env);
- }
-
- public DeadServerTest(Hashtable env,
- ScheduledExecutorService killSwitchPool)
- throws IOException
- {
- super(new DeadServer(), env, killSwitchPool);
- }
-
- public void performOp(InitialContext ctx) throws NamingException {}
-}
-
-class DeadServerNoTimeoutTest extends DeadServerTest {
-
- public DeadServerNoTimeoutTest(Hashtable env,
- ScheduledExecutorService killSwitchPool)
- throws IOException
- {
- super(env, killSwitchPool);
- }
-
- public void handleNamingException(NamingException e, long start, long end) {
- if (e instanceof InterruptedNamingException) Thread.interrupted();
-
- if (NANOSECONDS.toMillis(end - start) < LdapTimeoutTest.MIN_TIMEOUT) {
- System.err.printf("DeadServerNoTimeoutTest fail: timeout should be " +
- "at least %s ms, actual time is %s ms%n",
- LdapTimeoutTest.MIN_TIMEOUT,
- NANOSECONDS.toMillis(end - start));
- fail();
- } else {
- pass();
- }
- }
-}
-
-class DeadServerTimeoutTest extends DeadServerTest {
-
- public DeadServerTimeoutTest(Hashtable env) throws IOException {
- super(env);
- }
-
- public void handleNamingException(NamingException e, long start, long end)
- {
- // non SSL connect will timeout via readReply using connectTimeout
- if (NANOSECONDS.toMillis(end - start) < 2_900) {
- pass();
- } else {
- System.err.println("Fail: Waited too long");
- fail();
- }
- }
-}
-
-
-class ReadServerNoTimeoutTest extends ReadServerTest {
-
- public ReadServerNoTimeoutTest(Hashtable env,
- ScheduledExecutorService killSwitchPool)
- throws IOException
- {
- super(env, killSwitchPool);
- }
-
- public void handleNamingException(NamingException e, long start, long end) {
- if (e instanceof InterruptedNamingException) Thread.interrupted();
-
- if (NANOSECONDS.toMillis(end - start) < LdapTimeoutTest.MIN_TIMEOUT) {
- System.err.printf("ReadServerNoTimeoutTest fail: timeout should be " +
- "at least %s ms, actual time is %s ms%n",
- LdapTimeoutTest.MIN_TIMEOUT,
- NANOSECONDS.toMillis(end - start));
- fail();
- } else {
- pass();
- }
- }
-}
-
-class ReadServerTimeoutTest extends ReadServerTest {
-
- public ReadServerTimeoutTest(Hashtable env) throws IOException {
- super(env);
- }
-
- public void handleNamingException(NamingException e, long start, long end) {
- System.out.println("ReadServerTimeoutTest: end-start=" + NANOSECONDS.toMillis(end - start));
- if (NANOSECONDS.toMillis(end - start) < 2_500) {
- fail();
- } else {
- pass();
- }
- }
-}
-
-class TestServer extends Thread {
- ServerSocket serverSock;
- boolean accepting = false;
-
- public TestServer() throws IOException {
- this.serverSock = new ServerSocket(0);
- start();
- }
-
- public int getLocalPort() {
- return serverSock.getLocalPort();
- }
-
- public boolean accepting() {
- return accepting;
- }
-
- public void close() throws IOException {
- serverSock.close();
- }
-}
-
-class BindableServer extends TestServer {
-
- public BindableServer() throws IOException {
- super();
- }
-
- private byte[] bindResponse = {
- 0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
- 0x01, 0x00, 0x04, 0x00, 0x04, 0x00
- };
-
- public void run() {
- try {
- accepting = true;
- Socket socket = serverSock.accept();
- InputStream in = socket.getInputStream();
- OutputStream out = socket.getOutputStream();
-
- // Read the LDAP BindRequest
- while (in.read() != -1) {
- in.skip(in.available());
- break;
- }
-
- // Write an LDAP BindResponse
- out.write(bindResponse);
- out.flush();
- } catch (IOException e) {
- // ignore
- }
- }
-}
-
-class DeadServer extends TestServer {
-
- public DeadServer() throws IOException {
- super();
- }
-
- public void run() {
- while(true) {
- try {
- accepting = true;
- Socket socket = serverSock.accept();
- } catch (Exception e) {
- break;
- }
- }
- }
-}
+import static jdk.test.lib.Utils.adjustTimeout;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
public class LdapTimeoutTest {
- private static final ExecutorService testPool =
- Executors.newFixedThreadPool(3);
- private static final ScheduledExecutorService killSwitchPool =
- Executors.newScheduledThreadPool(3);
- public static int MIN_TIMEOUT = 18_000;
+ // ------ configure test timeouts here ------
- static Hashtable createEnv() {
- Hashtable env = new Hashtable(11);
- env.put(Context.INITIAL_CONTEXT_FACTORY,
- "com.sun.jndi.ldap.LdapCtxFactory");
- return env;
+ /*
+ * Practical representation of an infinite timeout.
+ */
+ private static final long INFINITY_MILLIS = adjustTimeout(20_000);
+ /*
+ * The acceptable variation in timeout measurements.
+ */
+ private static final long TOLERANCE = adjustTimeout( 3_500);
+
+ private static final long CONNECT_MILLIS = adjustTimeout( 3_000);
+ private static final long READ_MILLIS = adjustTimeout(10_000);
+
+ static {
+ // a series of checks to make sure this timeouts configuration is
+ // consistent and the timeouts do not overlap
+
+ assert (TOLERANCE >= 0);
+ // context creation
+ assert (2 * CONNECT_MILLIS + TOLERANCE < READ_MILLIS);
+ // context creation immediately followed by search
+ assert (2 * CONNECT_MILLIS + READ_MILLIS + TOLERANCE < INFINITY_MILLIS);
}
- public static void main(String[] args) throws Exception {
-
- InitialContext ctx = null;
- List results = new ArrayList<>();
+ @BeforeTest
+ public void beforeTest() {
+ startAuxiliaryDiagnosticOutput();
+ }
+ /*
+ * These are timeout tests and they are run in parallel to reduce the total
+ * amount of run time.
+ *
+ * Currently it doesn't seem possible to instruct JTREG to run TestNG test
+ * methods in parallel. That said, this JTREG test is still
+ * a "TestNG-flavored" test for the sake of having org.testng.Assert
+ * capability.
+ */
+ @Test
+ public void test() throws Exception {
+ List> futures = new ArrayList<>();
+ ExecutorService executorService = Executors.newCachedThreadPool();
try {
- // run the DeadServerTest with no timeouts set
- // this should get stuck indefinitely, so we need to kill
- // it after a timeout
- System.out.println("Running connect timeout test with 20s kill switch");
- Hashtable env = createEnv();
- results.add(
- testPool.submit(new DeadServerNoTimeoutTest(env, killSwitchPool)));
-
- // run the ReadServerTest with connect timeout set
- // this should get stuck indefinitely so we need to kill
- // it after a timeout
- System.out.println("Running read timeout test with 10ms connect timeout & 20s kill switch");
- Hashtable env1 = createEnv();
- env1.put("com.sun.jndi.ldap.connect.timeout", "10");
- results.add(testPool.submit(
- new ReadServerNoTimeoutTest(env1, killSwitchPool)));
-
- // run the ReadServerTest with no timeouts set
- // this should get stuck indefinitely, so we need to kill
- // it after a timeout
- System.out.println("Running read timeout test with 20s kill switch");
- Hashtable env2 = createEnv();
- results.add(testPool.submit(
- new ReadServerNoTimeoutTest(env2, killSwitchPool)));
-
- // run the DeadServerTest with connect / read timeouts set
- // this should exit after the connect timeout expires
- System.out.println("Running connect timeout test with 10ms connect timeout, 3000ms read timeout");
- Hashtable env3 = createEnv();
- env3.put("com.sun.jndi.ldap.connect.timeout", "10");
- env3.put("com.sun.jndi.ldap.read.timeout", "3000");
- results.add(testPool.submit(new DeadServerTimeoutTest(env3)));
-
-
- // run the ReadServerTest with connect / read timeouts set
- // this should exit after the connect timeout expires
- //
- // NOTE: commenting this test out as it is failing intermittently.
- //
- // System.out.println("Running read timeout test with 10ms connect timeout, 3000ms read timeout");
- // Hashtable env4 = createEnv();
- // env4.put("com.sun.jndi.ldap.connect.timeout", "10");
- // env4.put("com.sun.jndi.ldap.read.timeout", "3000");
- // results.add(testPool.submit(new ReadServerTimeoutTest(env4)));
-
- // run the DeadServerTest with connect timeout set
- // this should exit after the connect timeout expires
- System.out.println("Running connect timeout test with 10ms connect timeout");
- Hashtable env5 = createEnv();
- env5.put("com.sun.jndi.ldap.connect.timeout", "10");
- results.add(testPool.submit(new DeadServerTimeoutTest(env5)));
-
- // 8000487: Java JNDI connection library on ldap conn is
- // not honoring configured timeout
- System.out.println("Running simple auth connection test");
- Hashtable env6 = createEnv();
- env6.put("com.sun.jndi.ldap.connect.timeout", "10");
- env6.put("com.sun.jndi.ldap.read.timeout", "3000");
- env6.put(Context.SECURITY_AUTHENTICATION, "simple");
- env6.put(Context.SECURITY_PRINCIPAL, "user");
- env6.put(Context.SECURITY_CREDENTIALS, "password");
- results.add(testPool.submit(new DeadServerTimeoutTest(env6)));
-
- boolean testFailed = false;
- for (Future test : results) {
- while (!test.isDone()) {
- if ((Boolean) test.get() == false)
- testFailed = true;
- }
- }
-
- if (testFailed) {
- throw new AssertionError("some tests failed");
- }
-
+ futures.add(executorService.submit(() -> { test1(); return null; }));
+ futures.add(executorService.submit(() -> { test2(); return null; }));
+ futures.add(executorService.submit(() -> { test3(); return null; }));
+ futures.add(executorService.submit(() -> { test4(); return null; }));
+ futures.add(executorService.submit(() -> { test5(); return null; }));
+ futures.add(executorService.submit(() -> { test6(); return null; }));
+ futures.add(executorService.submit(() -> { test7(); return null; }));
} finally {
- LdapTimeoutTest.killSwitchPool.shutdown();
- LdapTimeoutTest.testPool.shutdown();
+ executorService.shutdown();
+ }
+ int failedCount = 0;
+ for (var f : futures) {
+ try {
+ f.get();
+ } catch (ExecutionException e) {
+ failedCount++;
+ e.getCause().printStackTrace(System.out);
+ }
+ }
+ if (failedCount > 0)
+ throw new RuntimeException(failedCount + " (sub)tests failed");
+ }
+
+ static void test1() throws Exception {
+ Hashtable