blob: 34243f7f02ebc1d18735dfc4f9a4b613c33a8b4c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4385031
27 * @bug 4459925
28 * @summary X500Principal.equals can be optimized,
29 * equals and hashcode are underspecified
30 */
31
32import javax.security.auth.x500.X500Principal;
33
34public class Equals {
35
36 private static final String p1String =
37 "O=sun, Ou=eng,cn=Test 1, EMAILADDRESS=duke@sun.com, UID=1 ";
38
39 private static final String p2String =
40 " o=SUN,OU=eng, cn=test 1,emailaddress = duke@sun.com,UID=1";
41
42 private static final String p3String =
43 " o = SUN, cn=test 1+ emailaddress = duke@sun.com + uId =5";
44
45 private static final String p4String =
46 "o=SUN,uid=5 + emailaddress = duke@sun.com +cn=test 1";
47
48 private static final String p5String =
49 "emailaddress = duke@sun.com + SURNAME=blah";
50
51 private static final String p6String =
52 "surname=blah+ emailAddress = duke@sun.com ";
53
54 private static final String p7String =
55 "o=sun, ou=esc\\\"quote, cn=duke";
56
57 private static final String p8String =
58 "o=sun, ou= esc\\\"quote,cn=duke";
59
60 public static void main(String[] args) {
61
62 // test regular equals
63 X500Principal p1 = new X500Principal(p1String);
64 X500Principal p2 = new X500Principal(p2String);
65
66 printName("Principal 1:", p1String, p1);
67 printName("Principal 2:", p2String, p2);
68
69 if (!p1.equals(p2))
70 throw new SecurityException("Equals test failed: #1");
71
72 X500Principal notEqual = new X500Principal("cn=test2");
73 if (p1.equals(notEqual))
74 throw new SecurityException("Equals test failed: #2");
75
76 if (p1.equals(null))
77 throw new SecurityException("Equals test failed: #3");
78
79 if (p1.hashCode() != p2.hashCode())
80 throw new SecurityException("Equals test failed: #4");
81
82 // test multiple AVA's in an RDN
83 X500Principal p3 = new X500Principal(p3String);
84 X500Principal p4 = new X500Principal(p4String);
85
86 printName("Principal 3:", p3String, p3);
87 printName("Principal 4:", p4String, p4);
88
89 if (!p3.equals(p4))
90 throw new SecurityException("Equals test failed: #5");
91
92 if (p1.equals(p3) || p2.equals(p3))
93 throw new SecurityException("Equals test failed: #6");
94
95 if (p3.hashCode() != p4.hashCode())
96 throw new SecurityException("Equals test failed: #7");
97
98 X500Principal p5 = new X500Principal(p5String);
99 X500Principal p6 = new X500Principal(p6String);
100
101 printName("Principal 5:", p5String, p5);
102 printName("Principal 6:", p6String, p6);
103
104 if (!p5.equals(p6))
105 throw new SecurityException("Equals test failed: #8");
106
107 if (p5.hashCode() != p6.hashCode())
108 throw new SecurityException("Equals test failed: #9");
109
110 X500Principal p7 = new X500Principal(p7String);
111 X500Principal p8 = new X500Principal(p8String);
112
113 printName("Principal 7:", p7String, p7);
114 printName("Principal 8:", p8String, p8);
115
116 if (!p7.equals(p8))
117 throw new SecurityException("Equals test failed: #10");
118
119 if (p7.hashCode() != p8.hashCode())
120 throw new SecurityException("Equals test failed: #11");
121
122 System.out.println("Equals test passed");
123 }
124
125 static void printName(String heading, String input, X500Principal p) {
126 System.out.println(heading);
127 System.out.println(" input:\t\t" + input);
128 System.out.println();
129 System.out.println(" toString:\t" + p.toString());
130 System.out.println();
131 System.out.println(" getName:\t" + p.getName());
132 System.out.println();
133 System.out.println(" getName 1779:\t" + p.getName("rfc1779"));
134 System.out.println();
135 System.out.println(" getName 2253:\t" + p.getName("rfc2253"));
136 System.out.println();
137 System.out.println(" getName canon:\t" + p.getName("canonical"));
138 System.out.println();
139 }
140}