Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/NamingExceptionMessageTest.java
38855 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 lib/28* @library /lib/testlibrary29* @run testng NamingExceptionMessageTest30*/3132import javax.naming.Context;33import javax.naming.NamingException;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.testlibrary.net.URIBuilder;4849public class NamingExceptionMessageTest {5051@Test52public void timeoutMessageTest() throws Exception {53try (TestLdapServer ldapServer = TestLdapServer.newInstance(false)) {54ldapServer.start();55ldapServer.awaitStartup();56Hashtable<Object, Object> env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE);57Exception 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 (TestLdapServer ldapServer = TestLdapServer.newInstance(true)) {66ldapServer.start();67ldapServer.awaitStartup();68Hashtable<Object, Object> env = ldapServer.getInitialLdapCtxEnvironment(0);69Exception namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));70System.out.println("Got naming exception:" + namingException);71Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE);72}73}7475// Test LDAP server76private static class TestLdapServer extends BaseLdapServer {7778private final boolean closeConnections;79private final CountDownLatch startupLatch = new CountDownLatch(1);8081public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) {82// Create environment for initial LDAP context83Hashtable<Object, Object> env = new Hashtable<>();8485// Activate LDAPv386env.put("java.naming.ldap.version", "3");8788// De-activate the ManageDsaIT control89env.put(Context.REFERRAL, "follow");90env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");91env.put(Context.PROVIDER_URL, getUrlString());92env.put(Context.SECURITY_AUTHENTICATION, "simple");93env.put(Context.SECURITY_PRINCIPAL, "name");94env.put(Context.SECURITY_CREDENTIALS, "pwd");9596if (readTimeoutValue > 0) {97env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue));98env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue));99}100101return env;102}103104private String getUrlString() {105String url = URIBuilder.newBuilder()106.scheme("ldap")107.loopback()108.port(getPort())109.buildUnchecked()110.toString();111return url;112}113114public static TestLdapServer newInstance(boolean closeConnections) throws IOException {115ServerSocket srvSock = new ServerSocket();116srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));117return new TestLdapServer(srvSock, closeConnections);118}119120void awaitStartup() throws InterruptedException {121startupLatch.await();122}123124private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) {125super(serverSocket);126this.closeConnections = closeConnections;127128}129130@Override131protected void beforeAcceptingConnections() {132startupLatch.countDown();133}134135@Override136protected void handleRequest(Socket socket,137LdapMessage msg,138OutputStream out)139throws IOException {140switch (msg.getOperation()) {141case BIND_REQUEST:142if (closeConnections) {143closeSilently(socket);144} else {145try {146TimeUnit.DAYS.sleep(Integer.MAX_VALUE);147} catch (InterruptedException e) {148Thread.currentThread().interrupt();149}150}151default:152break;153}154}155}156157// Expected message for case when connection is closed on server side158private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed";159// read and connect timeouts value160private static final int TIMEOUT_VALUE = 129;161// Expected message text when connection is timed-out162private static final String EXPECTED_TIMEOUT_MESSAGE = String.format(163"LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE);164}165166167