test/com/sun/jndi/ldap/NamingExceptionMessageTest.java

changeset 14206
ab2e99db6702
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/com/sun/jndi/ldap/NamingExceptionMessageTest.java	Wed Sep 09 14:19:14 2020 -0400
     1.3 @@ -0,0 +1,165 @@
     1.4 +/*
     1.5 + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8062947
    1.30 + * @summary Test that NamingException message text matches the failure reason
    1.31 + * @library lib/
    1.32 + * @library /lib/testlibrary
    1.33 + * @run testng NamingExceptionMessageTest
    1.34 + */
    1.35 +
    1.36 +import javax.naming.Context;
    1.37 +import javax.naming.NamingException;
    1.38 +import javax.naming.directory.InitialDirContext;
    1.39 +import java.io.IOException;
    1.40 +import java.io.OutputStream;
    1.41 +import java.net.InetAddress;
    1.42 +import java.net.InetSocketAddress;
    1.43 +import java.net.ServerSocket;
    1.44 +import java.net.Socket;
    1.45 +import java.util.Hashtable;
    1.46 +import java.util.concurrent.CountDownLatch;
    1.47 +import java.util.concurrent.TimeUnit;
    1.48 +
    1.49 +import org.testng.annotations.Test;
    1.50 +import org.testng.Assert;
    1.51 +import jdk.testlibrary.net.URIBuilder;
    1.52 +
    1.53 +public class NamingExceptionMessageTest {
    1.54 +
    1.55 +    @Test
    1.56 +    public void timeoutMessageTest() throws Exception {
    1.57 +        try (TestLdapServer ldapServer = TestLdapServer.newInstance(false)) {
    1.58 +            ldapServer.start();
    1.59 +            ldapServer.awaitStartup();
    1.60 +            Hashtable<Object, Object> env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE);
    1.61 +            Exception namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
    1.62 +            System.out.println("Got naming exception:" + namingException);
    1.63 +            Assert.assertEquals(namingException.getMessage(), EXPECTED_TIMEOUT_MESSAGE);
    1.64 +        }
    1.65 +    }
    1.66 +
    1.67 +    @Test
    1.68 +    public void connectionClosureMessageTest() throws Exception {
    1.69 +        try (TestLdapServer ldapServer = TestLdapServer.newInstance(true)) {
    1.70 +            ldapServer.start();
    1.71 +            ldapServer.awaitStartup();
    1.72 +            Hashtable<Object, Object> env = ldapServer.getInitialLdapCtxEnvironment(0);
    1.73 +            Exception namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
    1.74 +            System.out.println("Got naming exception:" + namingException);
    1.75 +            Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE);
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    // Test LDAP server
    1.80 +    private static class TestLdapServer extends BaseLdapServer {
    1.81 +
    1.82 +        private final boolean closeConnections;
    1.83 +        private final CountDownLatch startupLatch = new CountDownLatch(1);
    1.84 +
    1.85 +        public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) {
    1.86 +            // Create environment for initial LDAP context
    1.87 +            Hashtable<Object, Object> env = new Hashtable<>();
    1.88 +
    1.89 +            // Activate LDAPv3
    1.90 +            env.put("java.naming.ldap.version", "3");
    1.91 +
    1.92 +            // De-activate the ManageDsaIT control
    1.93 +            env.put(Context.REFERRAL, "follow");
    1.94 +            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    1.95 +            env.put(Context.PROVIDER_URL, getUrlString());
    1.96 +            env.put(Context.SECURITY_AUTHENTICATION, "simple");
    1.97 +            env.put(Context.SECURITY_PRINCIPAL, "name");
    1.98 +            env.put(Context.SECURITY_CREDENTIALS, "pwd");
    1.99 +
   1.100 +            if (readTimeoutValue > 0) {
   1.101 +                env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue));
   1.102 +                env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue));
   1.103 +            }
   1.104 +
   1.105 +            return env;
   1.106 +        }
   1.107 +
   1.108 +        private String getUrlString() {
   1.109 +            String url = URIBuilder.newBuilder()
   1.110 +                    .scheme("ldap")
   1.111 +                    .loopback()
   1.112 +                    .port(getPort())
   1.113 +                    .buildUnchecked()
   1.114 +                    .toString();
   1.115 +            return url;
   1.116 +        }
   1.117 +
   1.118 +        public static TestLdapServer newInstance(boolean closeConnections) throws IOException {
   1.119 +            ServerSocket srvSock = new ServerSocket();
   1.120 +            srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
   1.121 +            return new TestLdapServer(srvSock, closeConnections);
   1.122 +        }
   1.123 +
   1.124 +        void awaitStartup() throws InterruptedException {
   1.125 +            startupLatch.await();
   1.126 +        }
   1.127 +
   1.128 +        private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) {
   1.129 +            super(serverSocket);
   1.130 +            this.closeConnections = closeConnections;
   1.131 +
   1.132 +        }
   1.133 +
   1.134 +        @Override
   1.135 +        protected void beforeAcceptingConnections() {
   1.136 +            startupLatch.countDown();
   1.137 +        }
   1.138 +
   1.139 +        @Override
   1.140 +        protected void handleRequest(Socket socket,
   1.141 +                                     LdapMessage msg,
   1.142 +                                     OutputStream out)
   1.143 +                throws IOException {
   1.144 +            switch (msg.getOperation()) {
   1.145 +                case BIND_REQUEST:
   1.146 +                    if (closeConnections) {
   1.147 +                        closeSilently(socket);
   1.148 +                    } else {
   1.149 +                        try {
   1.150 +                            TimeUnit.DAYS.sleep(Integer.MAX_VALUE);
   1.151 +                        } catch (InterruptedException e) {
   1.152 +                            Thread.currentThread().interrupt();
   1.153 +                        }
   1.154 +                    }
   1.155 +                default:
   1.156 +                    break;
   1.157 +            }
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    // Expected message for case when connection is closed on server side
   1.162 +    private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed";
   1.163 +    // read and connect timeouts value
   1.164 +    private static final int TIMEOUT_VALUE = 129;
   1.165 +    // Expected message text when connection is timed-out
   1.166 +    private static final String EXPECTED_TIMEOUT_MESSAGE = String.format(
   1.167 +            "LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE);
   1.168 +}

mercurial