Path: blob/master/test/jdk/javax/naming/ldap/LdapName/LdapParserTests.java
41153 views
/*1* Copyright (c) 2003, 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 463561826* @summary Support for manipulating LDAP Names27*/2829import javax.naming.ldap.*;30import java.util.ArrayList;31import java.util.List;32import javax.naming.InvalidNameException;3334/**35* Tests for LDAP name parsing36*/3738public class LdapParserTests {39public static void main(String args[])40throws Exception {41Rdn rdn = null;4243/**44* Presence of any of these characters in an attribute value45* without a preceeding escape is considered Illegal by46* the LDAP parser.47*/48String[] mustEscSpecials = new String[]49{";", ",", "\\", "+"};5051/**52* The special characters that should be preceeded by an escape53*/54String[] specials = new String[]55{";", ",", "\\", "<", ">", "#", "\"", "+"};5657/**58* Test with unescaped speicial characters in the Rdn59*/60System.out.println();61System.out.print("*****Tests with unescaped special ");62System.out.println("characters in the Rdn*****");6364for (int i = 0; i < mustEscSpecials.length; i++) {65String rdnStr = "cn=Juicy" + mustEscSpecials[i] + "Fruit";66testInvalids(rdnStr);67}6869/*70* special characters with a preceeding backslash must be accepted.71*/72System.out.println();73System.out.println("******Special character escaping tests ******");74for (int i = 0; i < specials.length; i++) {75rdn = new Rdn("cn=Juicy\\" + specials[i] + "Fruit");76}77System.out.println("Escape leading space:" +78new Rdn("cn=\\ Juicy Fruit")); // escaped leading space79System.out.println("Escaped leading #:" +80new Rdn("cn=\\#Juicy Fruit")); // escaped leading # in string81System.out.println("Escaped trailing space:" +82new Rdn("cn=Juicy Fruit\\ ")); // escaped trailing space8384// Unescaped special characters at the beginning of a value85System.out.println();86System.out.println(87"******Other unescaped special character tests ******");88rdn = new Rdn("cn= Juicy Fruit");89System.out.println(90"Accepted Rdn with value containing leading spaces:" +91rdn.toString());92rdn = new Rdn("cn=Juicy Fruit ");93System.out.println(94"Accepted Rdn with value containing trailing spaces:" +95rdn.toString());9697String[] invalids = new String[]98{"cn=#xabc", "cn=#axcd", "cn=#abcx", "cn=#abcdex"};99100for (int i = 0; i < invalids.length; i++) {101testInvalids(invalids[i]);102}103104/**105* Other special cases106*/107System.out.println();108System.out.println(109"***************Other special cases****************");110111LdapName name = new LdapName("");112System.out.println("Empty LDAP name:" + name.toString());113114// Rdn with quoted value115rdn = new Rdn("cn=\"Juicy ,=+<>#; Fruit\"");116System.out.println("Quoted Rdn string:" + rdn.toString());117118// Rdn with unicode value119rdn = new Rdn("SN=Lu\\C4\\8Di\\C4\\87");120System.out.println("Unicode Rdn string:" + rdn.toString());121122/*123* oid type and binary value124*/125name = new LdapName(126"1.3.6.1.4.1.466.0=#04024869,O=Test,C=GB");127System.out.println("binary valued LDAP name:" + name.toString());128129// ';' seperated name- RFC 1779 style130name = new LdapName("CN=Steve Kille;O=Isode;C=GB");131System.out.println("';' seperated LDAP name:" + name.toString());132}133134static void testInvalids(String rdnStr) throws Exception {135boolean isExcepThrown = false;136Rdn rdn = null;137try {138rdn = new Rdn(rdnStr);139} catch (InvalidNameException e) {140System.out.println("Caught the right exception: " + e);141isExcepThrown = true;142} catch (IllegalArgumentException e) {143System.out.println("Caught the right exception: " + e);144isExcepThrown = true;145}146if (!isExcepThrown) {147throw new Exception(148"Accepted an invalid Rdn string:" +149rdnStr + " as Rdn: " + rdn);150}151}152}153154155