Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/BalancedParentheses.java
38855 views
/*1* Copyright (c) 2009, 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* @summary Invalid ldap filter is accepted and processed27*/2829import java.io.*;30import javax.naming.*;31import javax.naming.directory.*;32import java.util.Properties;33import java.util.Hashtable;3435import java.net.Socket;36import java.net.ServerSocket;3738public class BalancedParentheses {39// Should we run the client or server in a separate thread?40//41// Both sides can throw exceptions, but do you have a preference42// as to which side should be the main thread.43static boolean separateServerThread = true;4445// use any free port by default46volatile int serverPort = 0;4748// Is the server ready to serve?49volatile static boolean serverReady = false;5051// Define the server side of the test.52//53// If the server prematurely exits, serverReady will be set to true54// to avoid infinite hangs.55void doServerSide() throws Exception {56ServerSocket serverSock = new ServerSocket(serverPort);5758// signal client, it's ready to accecpt connection59serverPort = serverSock.getLocalPort();60serverReady = true;6162// accept a connection63Socket socket = serverSock.accept();64System.out.println("Server: Connection accepted");6566InputStream is = socket.getInputStream();67OutputStream os = socket.getOutputStream();6869// read the bindRequest70while (is.read() != -1) {71// ignore72is.skip(is.available());73break;74}7576byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,770x01, 0x00, 0x04, 0x00, 0x04, 0x00};78// write bindResponse79os.write(bindResponse);80os.flush();8182// ignore any more request.83while (is.read() != -1) {84// ignore85is.skip(is.available());86}8788is.close();89os.close();90socket.close();91serverSock.close();92}9394// Define the client side of the test.95//96// If the server prematurely exits, serverReady will be set to true97// to avoid infinite hangs.98void doClientSide() throws Exception {99// Wait for server to get started.100while (!serverReady) {101Thread.sleep(50);102}103104// set up the environment for creating the initial context105Hashtable<Object, Object> env = new Hashtable<Object, Object>();106env.put(Context.INITIAL_CONTEXT_FACTORY,107"com.sun.jndi.ldap.LdapCtxFactory");108env.put(Context.PROVIDER_URL, "ldap://localhost:" + serverPort);109env.put("com.sun.jndi.ldap.read.timeout", "1000");110111// env.put(Context.SECURITY_AUTHENTICATION, "simple");112// env.put(Context.SECURITY_PRINCIPAL,"cn=root");113// env.put(Context.SECURITY_CREDENTIALS,"root");114115// create initial context116DirContext context = new InitialDirContext(env);117118// searching119SearchControls scs = new SearchControls();120scs.setSearchScope(SearchControls.SUBTREE_SCOPE);121122try {123NamingEnumeration answer = context.search(124"o=sun,c=us", "(&(cn=Bob)))", scs);125} catch (InvalidSearchFilterException isfe) {126// ignore, it is the expected filter exception.127System.out.println("Expected exception: " + isfe.getMessage());128} catch (NamingException ne) {129// maybe a read timeout exception, as the server does not response.130throw new Exception("Expect a InvalidSearchFilterException", ne);131}132133try {134NamingEnumeration answer = context.search(135"o=sun,c=us", ")(&(cn=Bob)", scs);136} catch (InvalidSearchFilterException isfe) {137// ignore, it is the expected filter exception.138System.out.println("Expected exception: " + isfe.getMessage());139} catch (NamingException ne) {140// maybe a read timeout exception, as the server does not response.141throw new Exception("Expect a InvalidSearchFilterException", ne);142}143144try {145NamingEnumeration answer = context.search(146"o=sun,c=us", "(&(cn=Bob))", scs);147} catch (InvalidSearchFilterException isfe) {148// ignore, it is the expected filter exception.149throw new Exception("Unexpected ISFE", isfe);150} catch (NamingException ne) {151// maybe a read timeout exception, as the server does not response.152System.out.println("Expected exception: " + ne.getMessage());153}154155context.close();156}157158/*159* ============================================================160* The remainder is just support stuff161*/162163// client and server thread164Thread clientThread = null;165Thread serverThread = null;166167// client and server exceptions168volatile Exception serverException = null;169volatile Exception clientException = null;170171void startServer(boolean newThread) throws Exception {172if (newThread) {173serverThread = new Thread() {174public void run() {175try {176doServerSide();177} catch (Exception e) {178/*179* Our server thread just died.180*181* Release the client, if not active already...182*/183System.err.println("Server died...");184System.err.println(e);185serverReady = true;186serverException = e;187}188}189};190serverThread.start();191} else {192doServerSide();193}194}195196void startClient(boolean newThread) throws Exception {197if (newThread) {198clientThread = new Thread() {199public void run() {200try {201doClientSide();202} catch (Exception e) {203/*204* Our client thread just died.205*/206System.err.println("Client died...");207clientException = e;208}209}210};211clientThread.start();212} else {213doClientSide();214}215}216217// Primary constructor, used to drive remainder of the test.218BalancedParentheses() throws Exception {219if (separateServerThread) {220startServer(true);221startClient(false);222} else {223startClient(true);224startServer(false);225}226227/*228* Wait for other side to close down.229*/230if (separateServerThread) {231serverThread.join();232} else {233clientThread.join();234}235236/*237* When we get here, the test is pretty much over.238*239* If the main thread excepted, that propagates back240* immediately. If the other thread threw an exception, we241* should report back.242*/243if (serverException != null) {244System.out.print("Server Exception:");245throw serverException;246}247if (clientException != null) {248System.out.print("Client Exception:");249throw clientException;250}251}252253public static void main(String[] args) throws Exception {254// start the test255new BalancedParentheses();256}257258}259260261