Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/com/sun/jndi/ldap/DeadSSLLdapTimeoutTest.java
48797 views
/*1* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @run main/othervm DeadSSLLdapTimeoutTest26* @bug 814137027* @key intermittent28*/2930import java.net.Socket;31import java.net.ServerSocket;32import java.net.SocketTimeoutException;33import java.io.*;34import javax.naming.*;35import javax.naming.directory.*;36import java.util.List;37import java.util.Hashtable;38import java.util.ArrayList;39import java.util.concurrent.Callable;40import java.util.concurrent.ExecutionException;41import java.util.concurrent.Executors;42import java.util.concurrent.ExecutorService;43import java.util.concurrent.Future;44import java.util.concurrent.ScheduledExecutorService;45import java.util.concurrent.ScheduledFuture;46import java.util.concurrent.TimeoutException;47import java.util.concurrent.TimeUnit;48import javax.net.ssl.SSLHandshakeException;4950import static java.util.concurrent.TimeUnit.MILLISECONDS;51import static java.util.concurrent.TimeUnit.NANOSECONDS;525354class DeadServerTimeoutSSLTest implements Callable {5556Hashtable env;57DeadSSLServer server;58boolean passed = false;59private int HANGING_TEST_TIMEOUT = 20_000;6061public DeadServerTimeoutSSLTest(Hashtable env) throws IOException {62this.server = new DeadSSLServer();63this.env = env;64}6566public void performOp(InitialContext ctx) throws NamingException {}6768public void handleNamingException(NamingException e, long start, long end) {69if (e.getCause() instanceof SocketTimeoutException) {70// SSL connect will timeout via readReply using71// SocketTimeoutException72e.printStackTrace();73pass();74} else if (e.getCause() instanceof SSLHandshakeException75&& e.getCause().getCause() instanceof EOFException) {76// test seems to be failing intermittently on some77// platforms.78pass();79} else {80fail(e);81}82}8384public void pass() {85this.passed = true;86}8788public void fail() {89throw new RuntimeException("Test failed");90}9192public void fail(Exception e) {93throw new RuntimeException("Test failed", e);94}9596boolean shutItDown(InitialContext ctx) {97try {98if (ctx != null) ctx.close();99return true;100} catch (NamingException ex) {101return false;102}103}104105public Boolean call() {106InitialContext ctx = null;107ScheduledFuture killer = null;108long start = System.nanoTime();109110try {111while(!server.accepting())112Thread.sleep(200); // allow the server to start up113Thread.sleep(200); // to be sure114115env.put(Context.PROVIDER_URL, "ldap://localhost:" +116server.getLocalPort());117118try {119ctx = new InitialDirContext(env);120performOp(ctx);121fail();122} catch (NamingException e) {123long end = System.nanoTime();124System.out.println(this.getClass().toString() + " - elapsed: "125+ NANOSECONDS.toMillis(end - start));126handleNamingException(e, start, end);127} finally {128if (killer != null && !killer.isDone())129killer.cancel(true);130shutItDown(ctx);131server.close();132}133return passed;134} catch (IOException|InterruptedException e) {135throw new RuntimeException(e);136}137}138}139140class DeadSSLServer extends Thread {141ServerSocket serverSock;142boolean accepting = false;143144public DeadSSLServer() throws IOException {145this.serverSock = new ServerSocket(0);146start();147}148149public void run() {150while(true) {151try {152accepting = true;153Socket socket = serverSock.accept();154} catch (Exception e) {155break;156}157}158}159160public int getLocalPort() {161return serverSock.getLocalPort();162}163164public boolean accepting() {165return accepting;166}167168public void close() throws IOException {169serverSock.close();170}171}172173public class DeadSSLLdapTimeoutTest {174175static Hashtable createEnv() {176Hashtable env = new Hashtable(11);177env.put(Context.INITIAL_CONTEXT_FACTORY,178"com.sun.jndi.ldap.LdapCtxFactory");179return env;180}181182public static void main(String[] args) throws Exception {183184InitialContext ctx = null;185186//187// Running this test serially as it seems to tickle a problem188// on older kernels189//190// run the DeadServerTest with connect / read timeouts set191// and ssl enabled192// this should exit with a SocketTimeoutException as the root cause193// it should also use the connect timeout instead of the read timeout194System.out.println("Running connect timeout test with 10ms connect timeout, 3000ms read timeout & SSL");195Hashtable sslenv = createEnv();196sslenv.put("com.sun.jndi.ldap.connect.timeout", "10");197sslenv.put("com.sun.jndi.ldap.read.timeout", "3000");198sslenv.put(Context.SECURITY_PROTOCOL, "ssl");199boolean testFailed =200(new DeadServerTimeoutSSLTest(sslenv).call()) ? false : true;201202if (testFailed) {203throw new AssertionError("some tests failed");204}205206}207208}209210211212