Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/DeadSSLLdapTimeoutTest.java
38855 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 SocketTimeoutException70|| e.getCause().getCause() instanceof SocketTimeoutException) {71// SSL connect will timeout via readReply using72// SocketTimeoutException73e.printStackTrace();74pass();75} else if (e.getCause() instanceof SSLHandshakeException76&& e.getCause().getCause() instanceof EOFException) {77// test seems to be failing intermittently on some78// platforms.79pass();80} else {81fail(e);82}83}8485public void pass() {86this.passed = true;87}8889public void fail() {90throw new RuntimeException("Test failed");91}9293public void fail(Exception e) {94throw new RuntimeException("Test failed", e);95}9697boolean shutItDown(InitialContext ctx) {98try {99if (ctx != null) ctx.close();100return true;101} catch (NamingException ex) {102return false;103}104}105106public Boolean call() {107InitialContext ctx = null;108ScheduledFuture killer = null;109long start = System.nanoTime();110111try {112while(!server.accepting())113Thread.sleep(200); // allow the server to start up114Thread.sleep(200); // to be sure115116env.put(Context.PROVIDER_URL, "ldap://localhost:" +117server.getLocalPort());118119try {120ctx = new InitialDirContext(env);121performOp(ctx);122fail();123} catch (NamingException e) {124long end = System.nanoTime();125System.out.println(this.getClass().toString() + " - elapsed: "126+ NANOSECONDS.toMillis(end - start));127handleNamingException(e, start, end);128} finally {129if (killer != null && !killer.isDone())130killer.cancel(true);131shutItDown(ctx);132server.close();133}134return passed;135} catch (IOException|InterruptedException e) {136throw new RuntimeException(e);137}138}139}140141class DeadSSLServer extends Thread {142ServerSocket serverSock;143boolean accepting = false;144145public DeadSSLServer() throws IOException {146this.serverSock = new ServerSocket(0);147start();148}149150public void run() {151while(true) {152try {153accepting = true;154Socket socket = serverSock.accept();155} catch (Exception e) {156break;157}158}159}160161public int getLocalPort() {162return serverSock.getLocalPort();163}164165public boolean accepting() {166return accepting;167}168169public void close() throws IOException {170serverSock.close();171}172}173174public class DeadSSLLdapTimeoutTest {175176static Hashtable createEnv() {177Hashtable env = new Hashtable(11);178env.put(Context.INITIAL_CONTEXT_FACTORY,179"com.sun.jndi.ldap.LdapCtxFactory");180return env;181}182183public static void main(String[] args) throws Exception {184185InitialContext ctx = null;186187//188// Running this test serially as it seems to tickle a problem189// on older kernels190//191// run the DeadServerTest with connect / read timeouts set192// and ssl enabled193// this should exit with a SocketTimeoutException as the root cause194// it should also use the connect timeout instead of the read timeout195System.out.println("Running connect timeout test with 10ms connect timeout, 3000ms read timeout & SSL");196Hashtable sslenv = createEnv();197sslenv.put("com.sun.jndi.ldap.connect.timeout", "10");198sslenv.put("com.sun.jndi.ldap.read.timeout", "3000");199sslenv.put(Context.SECURITY_PROTOCOL, "ssl");200boolean testFailed =201(new DeadServerTimeoutSSLTest(sslenv).call()) ? false : true;202203if (testFailed) {204throw new AssertionError("some tests failed");205}206207}208209}210211212213