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