Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapCtx/Reconnect.java
41159 views
/*1* Copyright (c) 2019, 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*/2223import javax.naming.Context;24import javax.naming.ldap.InitialLdapContext;25import java.io.IOException;26import java.io.OutputStream;27import java.net.InetAddress;28import java.net.Socket;29import java.util.Hashtable;30import java.util.concurrent.Semaphore;31import java.util.concurrent.TimeUnit;3233/*34* @test35* @bug 821760636* @summary The LdapContext.reconnect method allows LDAP clients to initiate an37* LDAP bind operation on the existing connection. Invoking this method38* should not open a new connection under those circumstances.39*40* @library ../lib/41* @run main Reconnect42*/43public class Reconnect {4445private static final byte[] BIND_RESPONSE = {460x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,470x01, 0x00, 0x04, 0x00, 0x04, 0x0048};4950/*51* This test checks that there's only one connection from the client to52* the server.53*54* The mechanics is as follows. The first connection is awaited for some55* generous timeout to factor in a possibility of running on a slow system.56* Once the connection has been made, the second timeout begins. This57* second timeout is smaller. The test then verifies that no further58* connections have been made for that amount of time.59*/60public static void main(String[] args) throws Exception {6162final Semaphore s = new Semaphore(0);6364BaseLdapServer server = new BaseLdapServer() {6566@Override67protected void beforeConnectionHandled(Socket socket) {68// Increment the number of connections from LDAP client69s.release(1);70}7172@Override73protected void handleRequest(Socket socket,74LdapMessage msg,75OutputStream out)76throws IOException77{78switch (msg.getOperation()) {79case BIND_REQUEST:80out.write(BIND_RESPONSE);81default:82break;83}84}85};8687try (var s1 = server.start()) {88Hashtable<String, Object> env = new Hashtable<>();89env.put(Context.INITIAL_CONTEXT_FACTORY,90"com.sun.jndi.ldap.LdapCtxFactory");91env.put(Context.PROVIDER_URL,92"ldap://" + InetAddress.getLoopbackAddress().getHostName()93+ ":" + server.getPort());94env.put("java.naming.ldap.version", "3");9596// open connection97InitialLdapContext context = new InitialLdapContext(env, null);9899// send bind request100context.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");101context.addToEnvironment(Context.SECURITY_PRINCIPAL, "test");102context.addToEnvironment(Context.SECURITY_CREDENTIALS, "secret");103104context.reconnect(null);105}106107if (!s.tryAcquire(60L, TimeUnit.SECONDS)) {108throw new RuntimeException("No connection has been made");109}110111if (s.tryAcquire(5L, TimeUnit.SECONDS)) {112throw new RuntimeException("Expected 1 connection, but found: "113+ (s.availablePermits() + 2));114}115}116}117118119