Path: blob/master/test/jdk/com/sun/jndi/ldap/NamingExceptionMessageTest.java
41153 views
/*1* Copyright (c) 2020, 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* @bug 806294726* @summary Test that NamingException message text matches the failure reason27* @library /test/lib lib28* @run testng NamingExceptionMessageTest29*/3031import javax.naming.Context;32import javax.naming.NamingException;33import javax.naming.ServiceUnavailableException;34import javax.naming.directory.InitialDirContext;35import java.io.IOException;36import java.io.OutputStream;37import java.net.InetAddress;38import java.net.InetSocketAddress;39import java.net.ServerSocket;40import java.net.Socket;41import java.util.Hashtable;42import java.util.concurrent.CountDownLatch;43import java.util.concurrent.TimeUnit;4445import org.testng.annotations.Test;46import org.testng.Assert;47import jdk.test.lib.net.URIBuilder;4849public class NamingExceptionMessageTest {5051@Test52public void timeoutMessageTest() throws Exception {53try (var ldapServer = TestLdapServer.newInstance(false)) {54ldapServer.start();55ldapServer.awaitStartup();56var env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE);57var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));58System.out.println("Got naming exception:" + namingException);59Assert.assertEquals(namingException.getMessage(), EXPECTED_TIMEOUT_MESSAGE);60}61}6263@Test64public void connectionClosureMessageTest() throws Exception {65try (var ldapServer = TestLdapServer.newInstance(true)) {66ldapServer.start();67ldapServer.awaitStartup();68var env = ldapServer.getInitialLdapCtxEnvironment(0);69var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));70if (namingException instanceof ServiceUnavailableException) {71// If naming exception is ServiceUnavailableException it could mean72// that the connection was closed on test server-side before LDAP client starts73// read-out of the reply message. For such cases test run is considered as successful.74System.out.println("Got ServiceUnavailableException: Test PASSED");75} else {76// If exception is not ServiceUnavailableException - check the exception message77System.out.println("Got NamingException:" + namingException);78Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE);79}80}81}8283// Test LDAP server84private static class TestLdapServer extends BaseLdapServer {8586private final boolean closeConnections;87private final CountDownLatch startupLatch = new CountDownLatch(1);8889public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) {90// Create environment for initial LDAP context91Hashtable<Object, Object> env = new Hashtable<>();9293// Activate LDAPv394env.put("java.naming.ldap.version", "3");9596// De-activate the ManageDsaIT control97env.put(Context.REFERRAL, "follow");98env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");99env.put(Context.PROVIDER_URL, getUrlString());100env.put(Context.SECURITY_AUTHENTICATION, "simple");101env.put(Context.SECURITY_PRINCIPAL, "name");102env.put(Context.SECURITY_CREDENTIALS, "pwd");103104if (readTimeoutValue > 0) {105env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue));106env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue));107}108109return env;110}111112private String getUrlString() {113String url = URIBuilder.newBuilder()114.scheme("ldap")115.loopback()116.port(getPort())117.buildUnchecked()118.toString();119return url;120}121122public static TestLdapServer newInstance(boolean closeConnections) throws IOException {123ServerSocket srvSock = new ServerSocket();124srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));125return new TestLdapServer(srvSock, closeConnections);126}127128void awaitStartup() throws InterruptedException {129startupLatch.await();130}131132private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) {133super(serverSocket);134this.closeConnections = closeConnections;135136}137138@Override139protected void beforeAcceptingConnections() {140startupLatch.countDown();141}142143@Override144protected void handleRequest(Socket socket,145LdapMessage msg,146OutputStream out)147throws IOException {148switch (msg.getOperation()) {149case BIND_REQUEST:150if (closeConnections) {151// Give some time for LDAP client to start-up152try {153TimeUnit.MILLISECONDS.sleep(100);154} catch (InterruptedException e) {155}156// Close the socket157closeSilently(socket);158} else {159try {160TimeUnit.DAYS.sleep(Integer.MAX_VALUE);161} catch (InterruptedException e) {162Thread.currentThread().interrupt();163}164}165default:166break;167}168}169}170171// Expected message for case when connection is closed on server side172private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed";173// read and connect timeouts value174private static final int TIMEOUT_VALUE = 129;175// Expected message text when connection is timed-out176private static final String EXPECTED_TIMEOUT_MESSAGE = String.format(177"LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE);178}179180181