blob: 304e3a2dd005d863dfcb7d3e1962901f05f6dbd0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2007 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 * @summary Make sure names that are equal are treated as such.
27 * @bug 4273559
28 * @author Yassir Elley
29 */
30
31import sun.security.x509.*;
32import sun.security.util.ObjectIdentifier;
33
34public class AltNamesEqualsTest{
35
36 private final static String baseDNSName = "bob.example.com";
37 private final static String baseDNSNameSame = "BOB.EXAMPLE.COM";
38 private final static String baseDNSNameDiff = "fred.example.com";
39
40 private final static String baseRFCName = "bob@example.com";
41 private final static String baseRFCNameSame = "BOB@EXAMPLE.COM";
42 private final static String baseRFCNameDiff = "fred@example.com";
43
44 private final static String baseURIName = "http://bob.example.com/bob.html";
45 private final static String baseURINameSame ="HTTP://BOB.EXAMPLE.COM/bob.html";
46 private final static String baseURINameDiff ="http://bob.example.com/BOB.html";
47
48 private final static String baseOIDName = "1.2.3.4";
49 private final static String baseOIDNameDiff = "1.2.3.5";
50
51 private final static String baseIPName = "1.2.3.4";
52 private final static String baseIPNameDiff = "1.2.3.5";
53
54 private DNSName dnsName, dnsNameSame, dnsNameDiff;
55 private RFC822Name rfcName, rfcNameSame, rfcNameDiff;
56 private URIName uriName, uriNameSame, uriNameDiff;
57 private OIDName oidName, oidNameSame, oidNameDiff;
58 private IPAddressName ipName, ipNameSame, ipNameDiff;
59
60 public static void main(String [] args) throws Exception {
61 AltNamesEqualsTest test = new AltNamesEqualsTest();
62 test.run();
63 }
64
65 private void run() throws Exception {
66 initializeNames();
67 testNames("DNSNames", dnsName, dnsNameSame, dnsNameDiff);
68 testNames("RFC822Names", rfcName, rfcNameSame, rfcNameDiff);
69 testNames("URINames", uriName, uriNameSame, uriNameDiff);
70 testNames("OIDNames", oidName, oidNameSame, oidNameDiff);
71 testNames("IPAddressNames", ipName, ipNameSame, ipNameDiff);
72 }
73
74 private void initializeNames() throws Exception {
75 dnsName = new DNSName(baseDNSName);
76 dnsNameSame = new DNSName(baseDNSNameSame);
77 dnsNameDiff = new DNSName(baseDNSNameDiff);
78
79 rfcName = new RFC822Name(baseRFCName);
80 rfcNameSame = new RFC822Name(baseRFCNameSame);
81 rfcNameDiff = new RFC822Name(baseRFCNameDiff);
82
83 uriName = new URIName(baseURIName);
84 uriNameSame = new URIName(baseURINameSame);
85 uriNameDiff = new URIName(baseURINameDiff);
86
87 oidName = stringToOIDName(baseOIDName);
88 oidNameSame = stringToOIDName(baseOIDName);
89 oidNameDiff = stringToOIDName(baseOIDNameDiff);
90
91 ipName = stringToIPName(baseIPName);
92 ipNameSame = stringToIPName(baseIPName);
93 ipNameDiff = stringToIPName(baseIPNameDiff);
94 }
95
96 private void testNames(String nameType, GeneralNameInterface name,
97 GeneralNameInterface sameName,
98 GeneralNameInterface diffName)
99 throws Exception
100 {
101 if (!name.equals(sameName)){
102 throw new Exception("Equal " + nameType + " are being " +
103 "considered unequal");
104 }
105 if (name.equals(diffName)){
106 throw new Exception("Unequal " + nameType + " are being " +
107 "considered equal");
108 }
109 }
110
111 private OIDName stringToOIDName(String name)
112 throws Exception
113 {
114 OIDName oidName = null;
115 ObjectIdentifier oid = new ObjectIdentifier(name);
116 oidName = new OIDName(oid);
117 return oidName;
118 }
119
120 private IPAddressName stringToIPName(String name)
121 throws Exception
122 {
123 IPAddressName ipAddr = null;
124
125 //Convert to byte array
126 int ch = '.';
127 int start = 0;
128 int end = 0;
129 byte components [];
130 int componentLen;
131
132 // Calculate length of IP address
133 componentLen = 0;
134 while ((end = name.indexOf(ch,start)) != -1) {
135 start = end + 1;
136 componentLen += 1;
137 }
138 componentLen += 1;
139 if (componentLen != 4)
140 throw new Exception("Invalid IP address: need four components");
141 components = new byte[componentLen];
142
143 start = 0;
144 int i = 0;
145 String comp = null;
146 Integer compVal = new Integer(0);
147 while ((end = name.indexOf(ch,start)) != -1) {
148 comp = name.substring(start,end);
149 compVal = Integer.valueOf(comp);
150 if (compVal.intValue() < 0 || compVal.intValue() > 255)
151 throw new Exception("Invalid IP address: component value " +
152 "not between 0-255");
153 components[i++] = compVal.byteValue();
154 start = end + 1;
155 }
156 comp = name.substring(start);
157 components[i] = Integer.valueOf(comp).byteValue();
158 ipAddr = new IPAddressName(components);
159 return ipAddr;
160 }
161}