test/jdk/com/sun/jndi/ldap/LdapCtx/Reconnect.java
author prappo
Wed, 14 Aug 2019 11:14:54 +0100
changeset 57739 6717d7e59db4
permissions -rw-r--r--
8217606: LdapContext#reconnect always opens a new connection Reviewed-by: lancea, vtewari, rriggs Contributed-by: Chris Yin <xu.y.yin@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     1
/*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     4
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     8
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    13
 * accompanied this code).
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    14
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    18
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    21
 * questions.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    22
 */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    23
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    24
import javax.naming.Context;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    25
import javax.naming.ldap.InitialLdapContext;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    26
import java.io.IOException;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    27
import java.io.OutputStream;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    28
import java.net.InetAddress;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    29
import java.net.Socket;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    30
import java.util.Hashtable;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    31
import java.util.concurrent.Semaphore;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    32
import java.util.concurrent.TimeUnit;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    33
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    34
/*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    35
 * @test
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    36
 * @bug 8217606
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    37
 * @summary The LdapContext.reconnect method allows LDAP clients to initiate an
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    38
 *          LDAP bind operation on the existing connection. Invoking this method
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    39
 *          should not open a new connection under those circumstances.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    40
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    41
 * @library ../lib/
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    42
 * @run main Reconnect
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    43
 */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    44
public class Reconnect {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    45
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    46
    private static final byte[] BIND_RESPONSE = {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    47
            0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    48
            0x01, 0x00, 0x04, 0x00, 0x04, 0x00
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    49
    };
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    50
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    51
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    52
     * This test checks that there's only one connection from the client to
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    53
     * the server.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    54
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    55
     * The mechanics is as follows. The first connection is awaited for some
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    56
     * generous timeout to factor in a possibility of running on a slow system.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    57
     * Once the connection has been made, the second timeout begins. This
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    58
     * second timeout is smaller. The test then verifies that no further
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    59
     * connections have been made for that amount of time.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    60
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    61
    public static void main(String[] args) throws Exception {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    62
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    63
        final Semaphore s = new Semaphore(0);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    64
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    65
        BaseLdapServer server = new BaseLdapServer() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    66
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    67
            @Override
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    68
            protected void beforeConnectionHandled(Socket socket) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    69
                // Increment the number of connections from LDAP client
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    70
                s.release(1);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    71
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    72
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    73
            @Override
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    74
            protected void handleRequest(Socket socket,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    75
                                         LdapMessage msg,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    76
                                         OutputStream out)
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    77
                    throws IOException
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    78
            {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    79
                switch (msg.getOperation()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    80
                    case BIND_REQUEST:
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    81
                        out.write(BIND_RESPONSE);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    82
                    default:
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    83
                        break;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    84
                }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    85
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    86
        };
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    87
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    88
        try (var s1 = server.start()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    89
            Hashtable<String, Object> env = new Hashtable<>();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    90
            env.put(Context.INITIAL_CONTEXT_FACTORY,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    91
                    "com.sun.jndi.ldap.LdapCtxFactory");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    92
            env.put(Context.PROVIDER_URL,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    93
                    "ldap://" + InetAddress.getLoopbackAddress().getHostName()
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    94
                            + ":" + server.getPort());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    95
            env.put("java.naming.ldap.version", "3");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    96
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    97
            // open connection
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    98
            InitialLdapContext context = new InitialLdapContext(env, null);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    99
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   100
            // send bind request
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   101
            context.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   102
            context.addToEnvironment(Context.SECURITY_PRINCIPAL, "test");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   103
            context.addToEnvironment(Context.SECURITY_CREDENTIALS, "secret");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   104
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   105
            context.reconnect(null);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   106
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   107
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   108
        if (!s.tryAcquire(60L, TimeUnit.SECONDS)) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   109
            throw new RuntimeException("No connection has been made");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   110
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   111
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   112
        if (s.tryAcquire(5L, TimeUnit.SECONDS)) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   113
            throw new RuntimeException("Expected 1 connection, but found: "
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   114
                                               + (s.availablePermits() + 2));
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   115
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   116
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   117
}