Path: blob/master/test/jdk/com/sun/jndi/ldap/BalancedParentheses.java
41153 views
/*1* Copyright (c) 2009, 2020, 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 644957426* @library /test/lib27* @summary Invalid ldap filter is accepted and processed28*/2930import java.io.*;31import javax.naming.*;32import javax.naming.directory.*;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.SocketAddress;36import java.util.Hashtable;3738import java.net.Socket;39import java.net.ServerSocket;4041import jdk.test.lib.net.URIBuilder;4243public class BalancedParentheses {44// Should we run the client or server in a separate thread?45//46// Both sides can throw exceptions, but do you have a preference47// as to which side should be the main thread.48static boolean separateServerThread = true;4950// use any free port by default51volatile int serverPort = 0;5253// Is the server ready to serve?54volatile static boolean serverReady = false;5556// Define the server side of the test.57//58// If the server prematurely exits, serverReady will be set to true59// to avoid infinite hangs.60void doServerSide() throws Exception {61// Create unbound server socket62ServerSocket serverSock = new ServerSocket();6364// And bind it to the loopback address65SocketAddress sockAddr = new InetSocketAddress(66InetAddress.getLoopbackAddress(), 0);67serverSock.bind(sockAddr);6869// signal client, it's ready to accecpt connection70serverPort = serverSock.getLocalPort();71serverReady = true;7273// accept a connection74Socket socket = serverSock.accept();75System.out.println("Server: Connection accepted");7677InputStream is = socket.getInputStream();78OutputStream os = socket.getOutputStream();7980// read the bindRequest81while (is.read() != -1) {82// ignore83is.skip(is.available());84break;85}8687byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,880x01, 0x00, 0x04, 0x00, 0x04, 0x00};89// write bindResponse90os.write(bindResponse);91os.flush();9293// ignore any more request.94while (is.read() != -1) {95// ignore96is.skip(is.available());97}9899is.close();100os.close();101socket.close();102serverSock.close();103}104105// Define the client side of the test.106//107// If the server prematurely exits, serverReady will be set to true108// to avoid infinite hangs.109void doClientSide() throws Exception {110// Wait for server to get started.111while (!serverReady) {112Thread.sleep(50);113}114115// set up the environment for creating the initial context116Hashtable<Object, Object> env = new Hashtable<>();117env.put(Context.INITIAL_CONTEXT_FACTORY,118"com.sun.jndi.ldap.LdapCtxFactory");119// Construct the provider URL120String providerURL = URIBuilder.newBuilder()121.scheme("ldap")122.loopback()123.port(serverPort)124.build().toString();125env.put(Context.PROVIDER_URL, providerURL);126env.put("com.sun.jndi.ldap.read.timeout", "1000");127128// env.put(Context.SECURITY_AUTHENTICATION, "simple");129// env.put(Context.SECURITY_PRINCIPAL,"cn=root");130// env.put(Context.SECURITY_CREDENTIALS,"root");131132// create initial context133DirContext context = new InitialDirContext(env);134135// searching136SearchControls scs = new SearchControls();137scs.setSearchScope(SearchControls.SUBTREE_SCOPE);138139try {140NamingEnumeration<SearchResult> answer = context.search(141"o=sun,c=us", "(&(cn=Bob)))", scs);142} catch (InvalidSearchFilterException isfe) {143// ignore, it is the expected filter exception.144System.out.println("Expected exception: " + isfe.getMessage());145} catch (NamingException ne) {146// maybe a read timeout exception, as the server does not response.147throw new Exception("Expect a InvalidSearchFilterException", ne);148}149150try {151NamingEnumeration<SearchResult> answer = context.search(152"o=sun,c=us", ")(&(cn=Bob)", scs);153} catch (InvalidSearchFilterException isfe) {154// ignore, it is the expected filter exception.155System.out.println("Expected exception: " + isfe.getMessage());156} catch (NamingException ne) {157// maybe a read timeout exception, as the server does not response.158throw new Exception("Expect a InvalidSearchFilterException", ne);159}160161try {162NamingEnumeration<SearchResult> answer = context.search(163"o=sun,c=us", "(&(cn=Bob))", scs);164} catch (InvalidSearchFilterException isfe) {165// ignore, it is the expected filter exception.166throw new Exception("Unexpected ISFE", isfe);167} catch (NamingException ne) {168// maybe a read timeout exception, as the server does not response.169System.out.println("Expected exception: " + ne.getMessage());170}171172context.close();173}174175/*176* ============================================================177* The remainder is just support stuff178*/179180// client and server thread181Thread clientThread = null;182Thread serverThread = null;183184// client and server exceptions185volatile Exception serverException = null;186volatile Exception clientException = null;187188void startServer(boolean newThread) throws Exception {189if (newThread) {190serverThread = new Thread() {191public void run() {192try {193doServerSide();194} catch (Exception e) {195/*196* Our server thread just died.197*198* Release the client, if not active already...199*/200System.err.println("Server died...");201System.err.println(e);202serverReady = true;203serverException = e;204}205}206};207serverThread.start();208} else {209doServerSide();210}211}212213void startClient(boolean newThread) throws Exception {214if (newThread) {215clientThread = new Thread() {216public void run() {217try {218doClientSide();219} catch (Exception e) {220/*221* Our client thread just died.222*/223System.err.println("Client died...");224clientException = e;225}226}227};228clientThread.start();229} else {230doClientSide();231}232}233234// Primary constructor, used to drive remainder of the test.235BalancedParentheses() throws Exception {236if (separateServerThread) {237startServer(true);238startClient(false);239} else {240startClient(true);241startServer(false);242}243244/*245* Wait for other side to close down.246*/247if (separateServerThread) {248serverThread.join();249} else {250clientThread.join();251}252253/*254* When we get here, the test is pretty much over.255*256* If the main thread excepted, that propagates back257* immediately. If the other thread threw an exception, we258* should report back.259*/260if (serverException != null) {261System.out.print("Server Exception:");262throw serverException;263}264if (clientException != null) {265System.out.print("Client Exception:");266throw clientException;267}268}269270public static void main(String[] args) throws Exception {271// start the test272new BalancedParentheses();273}274275}276277278