1/*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4635618
27 * @summary Support for manipulating LDAP Names
28 */
29
30import javax.naming.ldap.*;
31import java.util.ArrayList;
32import java.util.List;
33import javax.naming.InvalidNameException;
34
35/**
36 * Tests for LDAP name parsing
37 */
38
39public class LdapParserTests {
40    public static void main(String args[])
41                throws Exception {
42        Rdn rdn = null;
43
44        /**
45         * Presence of any of these characters in an attribute value
46         * without a preceeding escape is considered Illegal by
47         * the LDAP parser.
48         */
49        String[] mustEscSpecials = new String[]
50                                {";", ",", "\\", "+"};
51
52        /**
53         * The special characters that should be preceeded by an escape
54         */
55        String[] specials = new String[]
56                        {";", ",", "\\", "<", ">", "#", "\"", "+"};
57
58        /**
59         * Test with unescaped speicial characters in the Rdn
60         */
61        System.out.println();
62        System.out.print("*****Tests with unescaped special ");
63        System.out.println("characters in the Rdn*****");
64
65        for (int i = 0; i < mustEscSpecials.length; i++) {
66            String rdnStr = "cn=Juicy" + mustEscSpecials[i] + "Fruit";
67            testInvalids(rdnStr);
68        }
69
70        /*
71         * special characters with a preceeding backslash must be accepted.
72         */
73        System.out.println();
74        System.out.println("******Special character escaping tests ******");
75        for (int i = 0; i < specials.length; i++) {
76            rdn = new Rdn("cn=Juicy\\" + specials[i] + "Fruit");
77        }
78        System.out.println("Escape leading space:" +
79                new Rdn("cn=\\  Juicy Fruit")); // escaped leading space
80        System.out.println("Escaped leading #:" +
81                new Rdn("cn=\\#Juicy Fruit"));  // escaped leading # in string
82        System.out.println("Escaped trailing space:" +
83                new Rdn("cn=Juicy Fruit\\  ")); // escaped trailing space
84
85        // Unescaped special characters at the beginning of a value
86        System.out.println();
87        System.out.println(
88                "******Other unescaped special character tests ******");
89        rdn = new Rdn("cn=  Juicy Fruit");
90        System.out.println(
91            "Accepted Rdn with value containing leading spaces:" +
92            rdn.toString());
93        rdn = new Rdn("cn=Juicy Fruit  ");
94        System.out.println(
95            "Accepted Rdn with value containing trailing spaces:" +
96            rdn.toString());
97
98        String[] invalids =  new String[]
99                {"cn=#xabc", "cn=#axcd", "cn=#abcx", "cn=#abcdex"};
100
101        for (int i = 0; i < invalids.length; i++) {
102            testInvalids(invalids[i]);
103        }
104
105        /**
106         * Other special cases
107         */
108        System.out.println();
109        System.out.println(
110                "***************Other special cases****************");
111
112        LdapName name = new LdapName("");
113        System.out.println("Empty LDAP name:" + name.toString());
114
115        // Rdn with quoted value
116        rdn = new Rdn("cn=\"Juicy ,=+<>#; Fruit\"");
117        System.out.println("Quoted Rdn string:" + rdn.toString());
118
119        // Rdn with unicode value
120        rdn = new Rdn("SN=Lu\\C4\\8Di\\C4\\87");
121        System.out.println("Unicode Rdn string:" + rdn.toString());
122
123        /*
124         * oid type and binary value
125         */
126        name = new LdapName(
127                "1.3.6.1.4.1.466.0=#04024869,O=Test,C=GB");
128        System.out.println("binary valued LDAP name:" + name.toString());
129
130        // ';' seperated name- RFC 1779 style
131        name = new LdapName("CN=Steve Kille;O=Isode;C=GB");
132        System.out.println("';' seperated LDAP name:" + name.toString());
133    }
134
135    static void testInvalids(String rdnStr) throws Exception {
136        boolean isExcepThrown = false;
137        Rdn rdn = null;
138        try {
139            rdn = new Rdn(rdnStr);
140        } catch (InvalidNameException e) {
141            System.out.println("Caught the right exception: " + e);
142            isExcepThrown = true;
143        } catch (IllegalArgumentException e) {
144            System.out.println("Caught the right exception: " + e);
145            isExcepThrown = true;
146        }
147        if (!isExcepThrown) {
148            throw new Exception(
149                    "Accepted an invalid Rdn string:" +
150                    rdnStr + " as Rdn: " + rdn);
151        }
152    }
153}
154