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