1/*
2 * Copyright (c) 2005, 2007, 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 6181936
27 * @summary Test basic functionality of X500Principal(String, Map) constructor
28 * @author Sean Mullan
29 */
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.Map;
33import javax.security.auth.x500.X500Principal;
34
35public class KeywordMap {
36
37    public static void main(String[] args) throws Exception {
38
39        X500Principal p = null;
40        Map<String, String> m = null;
41
42        // test null keywordMap
43        try {
44            p = new X500Principal("CN=user", null);
45            throw new Exception
46                ("expected NullPointerException for null keywordMap");
47        } catch (NullPointerException npe) {}
48
49        // test improperly specified OID
50        m = Collections.singletonMap("FOO", "FOO");
51        try {
52            p = new X500Principal("FOO=user", m);
53            throw new Exception
54                ("expected IllegalArgumentException for bad OID");
55        } catch (IllegalArgumentException iae) {}
56
57        // ignore improperly specified keyword
58        m = Collections.singletonMap("?*&", "FOO");
59        p = new X500Principal("CN=user", m);
60
61        // throw exception if no mapping for keyword
62        m = Collections.singletonMap("BAR", "1.2.3");
63        try {
64            p = new X500Principal("FOO=user", m);
65            throw new Exception
66                ("expected IllegalArgumentExc for keyword with no mapping");
67        } catch (IllegalArgumentException iae) {}
68
69        // don't match keyword in lower-case
70        m = Collections.singletonMap("foo", "1.2.3");
71        try {
72            p = new X500Principal("FOO=user", m);
73            throw new Exception
74                ("expected IllegalArgumentExc for wrong-case keyword mapping");
75        } catch (IllegalArgumentException iae) {}
76
77        // allow duplicate OID mappings
78        m = new HashMap<String, String>();
79        m.put("FOO", "1.2.3");
80        m.put("BAR", "1.2.3");
81        p = new X500Principal("BAR=user", m);
82
83        // override builtin keywords
84        m = Collections.singletonMap("CN", "1.2.3");
85        p = new X500Principal("CN=user", m);
86        if (!p.getName().startsWith("1.2.3")) {
87            throw new Exception("mapping did not override builtin keyword");
88        }
89
90        // override builtin OIDs
91        m = Collections.singletonMap("FOO", "2.5.4.3");
92        p = new X500Principal("FOO=sean", m);
93        if (!p.getName().startsWith("CN")) {
94            throw new Exception("mapping did not override builtin OID");
95        }
96    }
97}
98