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.Rdn;
31
32/**
33 * Tests for Rdn escapeValue and unescapeValue methods
34 */
35public class EscapeUnescapeTests {
36
37    public static void main(String args[]) throws Exception {
38
39        /**
40         * Unescape tests
41         */
42        String[] invalids = new String[] {"\\", "\\\\\\" };
43
44        String[] valids = new String[] {"\\\\", "\\\\\\\\"};
45
46        String val;
47        Object unescVal = null;
48        System.out.println("##### Unescape value tests #####");
49
50        for (int i = 0; i < valids.length; i++) {
51            unescVal = Rdn.unescapeValue(valids[i]);
52            System.out.println("Orig val: " + valids[i] +
53                                "       Unescaped val: " + unescVal);
54        }
55
56        boolean isExcepThrown = false;
57        for (int i = 0; i < invalids.length; i++) {
58            val = "Juicy" + invalids[i] + "Fruit";
59            try {
60                unescVal = Rdn.unescapeValue(val);
61            } catch (IllegalArgumentException e) {
62                System.out.println("Caught the right exception: " + e);
63                isExcepThrown = true;
64            }
65            if (!isExcepThrown) {
66                throw new Exception(
67                        "Unescaped successfully an invalid string "
68                        + val + " as Rdn: " + unescVal);
69            }
70            isExcepThrown = false;
71        }
72
73        /**
74         * Escape tests
75         */
76        String[] values = new String[] {";", "<<<", "###", "=="};
77        System.out.println("##### Escape value tests #####");
78        printEscapedVal(values);
79
80        // leading space, trailing space
81        values = new String[] {"  leading space", "trailing space  "};
82        printEscapedVal(values);
83
84        // binary values
85        byte[] bytes = new byte[] {1, 2, 3, 4};
86        String escVal = Rdn.escapeValue(bytes);
87        System.out.println("Orig val: " + bytes +
88                                "       Escaped val: " + escVal);
89    }
90
91    static void printEscapedVal(Object[] values) {
92        String escVal;
93        for (int i = 0; i < values.length; i++) {
94            escVal = Rdn.escapeValue(values[i]);
95            System.out.println("Orig val: " + values[i] +
96                                "       Escaped val: " + escVal);
97        }
98    }
99}
100