|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8062947 |
| 27 | + * @summary Test that NamingException message text matches the failure reason |
| 28 | + * @library /test/lib lib |
| 29 | + * @run testng NamingExceptionMessageTest |
| 30 | + */ |
| 31 | + |
| 32 | +import javax.naming.Context; |
| 33 | +import javax.naming.NamingException; |
| 34 | +import javax.naming.directory.InitialDirContext; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.net.InetAddress; |
| 38 | +import java.net.InetSocketAddress; |
| 39 | +import java.net.ServerSocket; |
| 40 | +import java.net.Socket; |
| 41 | +import java.util.Hashtable; |
| 42 | +import java.util.concurrent.CountDownLatch; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +import org.testng.annotations.Test; |
| 46 | +import org.testng.Assert; |
| 47 | +import jdk.test.lib.net.URIBuilder; |
| 48 | + |
| 49 | +public class NamingExceptionMessageTest { |
| 50 | + |
| 51 | + @Test |
| 52 | + public void timeoutMessageTest() throws Exception { |
| 53 | + try (var ldapServer = TestLdapServer.newInstance(false)) { |
| 54 | + ldapServer.start(); |
| 55 | + ldapServer.awaitStartup(); |
| 56 | + var env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE); |
| 57 | + var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env)); |
| 58 | + System.out.println("Got naming exception:" + namingException); |
| 59 | + Assert.assertEquals(namingException.getMessage(), EXPECTED_TIMEOUT_MESSAGE); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void connectionClosureMessageTest() throws Exception { |
| 65 | + try (var ldapServer = TestLdapServer.newInstance(true)) { |
| 66 | + ldapServer.start(); |
| 67 | + ldapServer.awaitStartup(); |
| 68 | + var env = ldapServer.getInitialLdapCtxEnvironment(0); |
| 69 | + var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env)); |
| 70 | + System.out.println("Got naming exception:" + namingException); |
| 71 | + Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Test LDAP server |
| 76 | + private static class TestLdapServer extends BaseLdapServer { |
| 77 | + |
| 78 | + private final boolean closeConnections; |
| 79 | + private final CountDownLatch startupLatch = new CountDownLatch(1); |
| 80 | + |
| 81 | + public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) { |
| 82 | + // Create environment for initial LDAP context |
| 83 | + Hashtable<Object, Object> env = new Hashtable<>(); |
| 84 | + |
| 85 | + // Activate LDAPv3 |
| 86 | + env.put("java.naming.ldap.version", "3"); |
| 87 | + |
| 88 | + // De-activate the ManageDsaIT control |
| 89 | + env.put(Context.REFERRAL, "follow"); |
| 90 | + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); |
| 91 | + env.put(Context.PROVIDER_URL, getUrlString()); |
| 92 | + env.put(Context.SECURITY_AUTHENTICATION, "simple"); |
| 93 | + env.put(Context.SECURITY_PRINCIPAL, "name"); |
| 94 | + env.put(Context.SECURITY_CREDENTIALS, "pwd"); |
| 95 | + |
| 96 | + if (readTimeoutValue > 0) { |
| 97 | + env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue)); |
| 98 | + env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue)); |
| 99 | + } |
| 100 | + |
| 101 | + return env; |
| 102 | + } |
| 103 | + |
| 104 | + private String getUrlString() { |
| 105 | + String url = URIBuilder.newBuilder() |
| 106 | + .scheme("ldap") |
| 107 | + .loopback() |
| 108 | + .port(getPort()) |
| 109 | + .buildUnchecked() |
| 110 | + .toString(); |
| 111 | + return url; |
| 112 | + } |
| 113 | + |
| 114 | + public static TestLdapServer newInstance(boolean closeConnections) throws IOException { |
| 115 | + ServerSocket srvSock = new ServerSocket(); |
| 116 | + srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
| 117 | + return new TestLdapServer(srvSock, closeConnections); |
| 118 | + } |
| 119 | + |
| 120 | + void awaitStartup() throws InterruptedException { |
| 121 | + startupLatch.await(); |
| 122 | + } |
| 123 | + |
| 124 | + private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) { |
| 125 | + super(serverSocket); |
| 126 | + this.closeConnections = closeConnections; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void beforeAcceptingConnections() { |
| 132 | + startupLatch.countDown(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void handleRequest(Socket socket, |
| 137 | + LdapMessage msg, |
| 138 | + OutputStream out) |
| 139 | + throws IOException { |
| 140 | + switch (msg.getOperation()) { |
| 141 | + case BIND_REQUEST: |
| 142 | + if (closeConnections) { |
| 143 | + closeSilently(socket); |
| 144 | + } else { |
| 145 | + try { |
| 146 | + TimeUnit.DAYS.sleep(Integer.MAX_VALUE); |
| 147 | + } catch (InterruptedException e) { |
| 148 | + Thread.currentThread().interrupt(); |
| 149 | + } |
| 150 | + } |
| 151 | + default: |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Expected message for case when connection is closed on server side |
| 158 | + private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed"; |
| 159 | + // read and connect timeouts value |
| 160 | + private static final int TIMEOUT_VALUE = 129; |
| 161 | + // Expected message text when connection is timed-out |
| 162 | + private static final String EXPECTED_TIMEOUT_MESSAGE = String.format( |
| 163 | + "LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE); |
| 164 | +} |
0 commit comments