Path: blob/master/test/jdk/com/sun/jndi/ldap/InvalidLdapFilters.java
43836 views
/*1* Copyright (c) 2010, 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 6916202 704112526* @library /test/lib27* @summary More cases of invalid ldap filters accepted and processed28* LDAP API does not catch malformed filters that contain two operands29* for the ! operator30* @run main/othervm InvalidLdapFilters valid (cn=Babs)31* @run main/othervm InvalidLdapFilters valid (&(cn=Bob))32* @run main/othervm InvalidLdapFilters valid (&(objectClass=*)(uid=*))33* @run main/othervm InvalidLdapFilters valid (|(cn=Bob))34* @run main/othervm InvalidLdapFilters valid (|(objectClass=*)(uid=*))35* @run main/othervm InvalidLdapFilters valid (!(cn=Tim))36* @run main/othervm InvalidLdapFilters valid (!(!(cn=Tim)))37* @run main/othervm InvalidLdapFilters valid (!(&(objectClass=*)(uid=*)))38* @run main/othervm InvalidLdapFilters valid (!(|(objectClass=*)(uid=*)))39* @run main/othervm InvalidLdapFilters valid (&(objectClass=*)(!(uid=*)))40* @run main/othervm InvalidLdapFilters valid (o=univ*of*mich*)41* @run main/othervm InvalidLdapFilters valid (seeAlso=)42* @run main/othervm InvalidLdapFilters valid (cn:caseExactMatch:=Flintstone)43* @run main/othervm InvalidLdapFilters valid (cn:=Betty)44* @run main/othervm InvalidLdapFilters valid (sn:dn:2.4.6.8.10:=Barney)45* @run main/othervm InvalidLdapFilters valid (o:dn:=Ace)46* @run main/othervm InvalidLdapFilters valid (:1.2.3:=Wilma)47* @run main/othervm InvalidLdapFilters valid (:DN:2.4.6.8.10:=Dino)48* @run main/othervm InvalidLdapFilters valid (1.2.3=abc)49* @run main/othervm InvalidLdapFilters valid (cn;lang-de;lang-en=abc)50* @run main/othervm InvalidLdapFilters valid (owner=abc)51* @run main/othervm InvalidLdapFilters valid (sn;lang-en:dn:2.4.6.8.10:=Barney)52* @run main/othervm InvalidLdapFilters valid53(&(objectClass=Person)(|(sn=Jensen)(cn=Bab*)))54* @run main/othervm InvalidLdapFilters valid55(orcluserapplnprovstatus;EMAIL_email=PROVISIONING_FAILURE)56* @run main/othervm InvalidLdapFilters invalid "(&(cn=Robert Dean)))"57* @run main/othervm InvalidLdapFilters invalid (&|(cn=Bob))58* @run main/othervm InvalidLdapFilters invalid (&&(cn=Bob))59* @run main/othervm InvalidLdapFilters invalid (|&(cn=Bob))60* @run main/othervm InvalidLdapFilters invalid (||(cn=Bob))61* @run main/othervm InvalidLdapFilters invalid (:1.2.:=Wilma)62* @run main/othervm InvalidLdapFilters invalid (::DN:2.4.6.8.10:=Dino)63* @run main/othervm InvalidLdapFilters invalid (:DN::2.4.6.8.10:=Dino)64* @run main/othervm InvalidLdapFilters invalid (:DN:2.4.6.8.10::=Dino)65* @run main/othervm InvalidLdapFilters invalid (:DN:2.4.6..8.10:=Dino)66* @run main/othervm InvalidLdapFilters invalid (:DN:2.4.6.8.:=Dino)67* @run main/othervm InvalidLdapFilters invalid (1.2.;::=abc)68* @run main/othervm InvalidLdapFilters invalid (1.2.3;::=abc)69* @run main/othervm InvalidLdapFilters invalid (1.2.3;x;=abc)70* @run main/othervm InvalidLdapFilters invalid (1.2.3:x::=abc)71* @run main/othervm InvalidLdapFilters invalid (1.2.3:x=abc)72* @run main/othervm InvalidLdapFilters invalid (sn;:dn:2.4.6.8.10:=Barney)73* @run main/othervm InvalidLdapFilters invalid "\"((objectClass=*)&(uid=*))\""74* @run main/othervm InvalidLdapFilters invalid "&(objectClass=*)(uid=*)"75* @run main/othervm InvalidLdapFilters invalid "(:DN:2.4.6.8.10:cn:=Dino)"76* @run main/othervm InvalidLdapFilters invalid "(:DN:2.4.6.8.10:cn=Dino)"77* @run main/othervm InvalidLdapFilters invalid78"((objectCategory=person)(cn=u)(!(cn=u2*)))"79* @run main/othervm InvalidLdapFilters invalid80"((&(objectClass=user)(cn=andy*)(cn=steve*)(cn=bob*)))"81* @run main/othervm InvalidLdapFilters invalid82(&(objectClass=Person)(!(sn=Jensen)(cn=Bab)))83*84* @author Xuelei Fan85*/8687import java.io.*;88import javax.naming.*;89import javax.naming.directory.*;90import java.net.InetAddress;91import java.net.InetSocketAddress;92import java.net.SocketAddress;93import java.util.Hashtable;9495import java.net.Socket;96import java.net.ServerSocket;9798import jdk.test.lib.net.URIBuilder;99100public class InvalidLdapFilters {101// Should we run the client or server in a separate thread?102//103// Both sides can throw exceptions, but do you have a preference104// as to which side should be the main thread.105static boolean separateServerThread = true;106107// use any free port by default108volatile int serverPort = 0;109110// Is the server ready to serve?111volatile static boolean serverReady = false;112113// Define the server side of the test.114//115// If the server prematurely exits, serverReady will be set to true116// to avoid infinite hangs.117void doServerSide() throws Exception {118ServerSocket serverSock = new ServerSocket();119SocketAddress sockAddr = new InetSocketAddress(120InetAddress.getLoopbackAddress(), serverPort);121// Bind server socket122serverSock.bind(sockAddr);123124// signal client, it's ready to accept connection125serverPort = serverSock.getLocalPort();126serverReady = true;127128// accept a connection129Socket socket = serverSock.accept();130System.out.println("Server: Connection accepted");131132InputStream is = socket.getInputStream();133OutputStream os = socket.getOutputStream();134135// read the bindRequest136while (is.read() != -1) {137// ignore138is.skip(is.available());139break;140}141142byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,1430x01, 0x00, 0x04, 0x00, 0x04, 0x00};144// write bindResponse145os.write(bindResponse);146os.flush();147148// ignore any more request.149while (is.read() != -1) {150// ignore151is.skip(is.available());152}153154is.close();155os.close();156socket.close();157serverSock.close();158}159160// Define the client side of the test.161//162// If the server prematurely exits, serverReady will be set to true163// to avoid infinite hangs.164void doClientSide() throws Exception {165// Wait for server to get started.166while (!serverReady) {167Thread.sleep(50);168}169170// set up the environment for creating the initial context171Hashtable<Object, Object> env = new Hashtable<>();172env.put(Context.INITIAL_CONTEXT_FACTORY,173"com.sun.jndi.ldap.LdapCtxFactory");174String providerUrl = URIBuilder.newBuilder()175.scheme("ldap")176.loopback()177.port(serverPort)178.build()179.toString();180env.put(Context.PROVIDER_URL, providerUrl);181env.put("com.sun.jndi.ldap.read.timeout", "1000");182183// env.put(Context.SECURITY_AUTHENTICATION, "simple");184// env.put(Context.SECURITY_PRINCIPAL,"cn=root");185// env.put(Context.SECURITY_CREDENTIALS,"root");186187// create initial context188DirContext context = null;189int i = 0;190while (true) {191try {192context = new InitialDirContext(env);193break;194} catch (NamingException ne) {195// may be a connection or read timeout, try again196// no more than 5 times197if (i++ > 5) {198throw new Exception(199"Maybe timeout during context initialization", ne);200}201}202}203204// searching205SearchControls scs = new SearchControls();206scs.setSearchScope(SearchControls.SUBTREE_SCOPE);207208try {209NamingEnumeration<SearchResult> answer =210context.search("o=sun,c=us", searchFilter, scs);211} catch (InvalidSearchFilterException isfe) {212if (filterIsValid) {213// unexpected filter exception.214throw new Exception("Unexpected ISFE", isfe);215} else {216// ignore, it is the expected filter exception.217System.out.println("Expected exception: " + isfe.getMessage());218}219} catch (NamingException ne) {220// maybe a read timeout exception, as the server does not response.221if (filterIsValid) {222System.out.println("Expected exception: " + ne.getMessage());223} else {224throw new Exception("Not an InvalidSearchFilterException", ne);225}226}227228context.close();229}230231private static boolean filterIsValid;232private static String searchFilter;233234private static void parseArguments(String[] args) {235System.out.println("arguments length: " + args.length);236if (args[0].equals("valid")) {237filterIsValid = true;238}239240searchFilter = args[1];241}242243/*244* ============================================================245* The remainder is just support stuff246*/247248// client and server thread249Thread clientThread = null;250Thread serverThread = null;251252// client and server exceptions253volatile Exception serverException = null;254volatile Exception clientException = null;255256void startServer(boolean newThread) throws Exception {257if (newThread) {258serverThread = new Thread() {259public void run() {260try {261doServerSide();262} catch (Exception e) {263/*264* Our server thread just died.265*266* Release the client, if not active already...267*/268System.err.println("Server died...");269System.err.println(e);270serverReady = true;271serverException = e;272}273}274};275serverThread.start();276} else {277doServerSide();278}279}280281void startClient(boolean newThread) throws Exception {282if (newThread) {283clientThread = new Thread() {284public void run() {285try {286doClientSide();287} catch (Exception e) {288/*289* Our client thread just died.290*/291System.err.println("Client died...");292clientException = e;293}294}295};296clientThread.start();297} else {298doClientSide();299}300}301302// Primary constructor, used to drive remainder of the test.303InvalidLdapFilters() throws Exception {304if (separateServerThread) {305startServer(true);306startClient(false);307} else {308startClient(true);309startServer(false);310}311312/*313* Wait for other side to close down.314*/315if (separateServerThread) {316serverThread.join();317} else {318clientThread.join();319}320321/*322* When we get here, the test is pretty much over.323*324* If the main thread excepted, that propagates back325* immediately. If the other thread threw an exception, we326* should report back.327*/328if (serverException != null) {329System.out.print("Server Exception:");330throw serverException;331}332if (clientException != null) {333System.out.print("Client Exception:");334throw clientException;335}336}337338public static void main(String[] args) throws Exception {339// parse the customized arguments340parseArguments(args);341342// start the test343new InvalidLdapFilters();344}345346}347348349