Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/NoWaitForReplyTest.java
38855 views
/*1* Copyright (c) 2011, 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 674815626* @summary add an new JNDI property to control the boolean flag WaitForReply27*/2829import java.net.Socket;30import java.net.ServerSocket;31import java.io.*;32import javax.naming.*;33import javax.naming.directory.*;34import java.util.Hashtable;3536public class NoWaitForReplyTest {3738public static void main(String[] args) throws Exception {3940boolean passed = false;4142// start the LDAP server43DummyServer ldapServer = new DummyServer();44ldapServer.start();4546// Set up the environment for creating the initial context47Hashtable env = new Hashtable(11);48env.put(Context.PROVIDER_URL, "ldap://localhost:" +49ldapServer.getPortNumber());50env.put(Context.INITIAL_CONTEXT_FACTORY,51"com.sun.jndi.ldap.LdapCtxFactory");5253// Wait up to 10 seconds for a response from the LDAP server54env.put("com.sun.jndi.ldap.read.timeout", "10000");5556// Don't wait until the first search reply is received57env.put("com.sun.jndi.ldap.search.waitForReply", "false");5859// Send the LDAP search request without first authenticating (no bind)60env.put("java.naming.ldap.version", "3");616263try {6465// Create initial context66System.out.println("Client: connecting to the server");67DirContext ctx = new InitialDirContext(env);6869SearchControls scl = new SearchControls();70scl.setSearchScope(SearchControls.SUBTREE_SCOPE);71System.out.println("Client: performing search");72NamingEnumeration answer =73ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);7475// Server will never reply: either we waited in the call above until76// the timeout (fail) or we did not wait and reached here (pass).77passed = true;78System.out.println("Client: did not wait until first reply");7980// Close the context when we're done81ctx.close();8283} catch (NamingException e) {84// timeout (ignore)85}86ldapServer.interrupt();8788if (!passed) {89throw new Exception(90"Test FAILED: should not have waited until first search reply");91}92System.out.println("Test PASSED");93}9495static class DummyServer extends Thread {9697private final ServerSocket serverSocket;9899DummyServer() throws IOException {100this.serverSocket = new ServerSocket(0);101System.out.println("Server: listening on port " + serverSocket.getLocalPort());102}103104public int getPortNumber() {105return serverSocket.getLocalPort();106}107108public void run() {109try (Socket socket = serverSocket.accept()) {110System.out.println("Server: accepted a connection");111InputStream in = socket.getInputStream();112113while (!isInterrupted()) {114in.skip(in.available());115}116117} catch (Exception e) {118// ignore119120} finally {121System.out.println("Server: shutting down");122try {123serverSocket.close();124} catch (IOException e) {125// ignore126}127}128}129}130}131132133