Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/security/auth/x500/X500Principal/NameFormat.java
41155 views
1
/*
2
* Copyright (c) 2012, 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 4505980 5109882 7049963 7090565
27
* @summary X500Principal input name parsing issues and wrong exception thrown
28
* @modules java.base/sun.security.x509
29
* @run main/othervm -Djava.security.debug=x509,ava NameFormat
30
*
31
* The debug=ava above must be set in order to check for escaped hex chars.
32
*/
33
import javax.security.auth.x500.X500Principal;
34
35
public class NameFormat {
36
37
public static void main(String[] args) throws Exception {
38
39
// tests for leading/trailing escaped/non-escaped spaces
40
41
testName("cn=\\ duke ", "RFC1779", "CN=\" duke\"", 1);
42
testName("cn=\\ duke ", "RFC2253", "CN=\\ duke", 2);
43
testName("cn=\\ duke ", "CANONICAL", "cn=duke", 3);
44
testName("cn=\\ duke ", "toString", "CN=\" duke\"", 4);
45
46
testName("cn= duke", "RFC1779", "CN=duke", 5);
47
testName("cn= duke", "RFC2253", "CN=duke", 6);
48
testName("cn= duke", "CANONICAL", "cn=duke", 7);
49
testName("cn= duke", "toString", "CN=duke", 8);
50
51
testName("cn=duke\\ ", "RFC1779", "CN=\"duke \"", 9);
52
testName("cn=duke\\ ", "RFC2253", "CN=duke\\ ", 10);
53
testName("cn=duke\\ ", "CANONICAL", "cn=duke", 11);
54
testName("cn=duke\\ ", "toString", "CN=\"duke \"", 12);
55
56
testName("cn=duke\\ , ou= sun\\ ", "RFC1779",
57
"CN=\"duke \", OU=\"sun \"", 13);
58
testName("cn=duke\\ , ou= sun\\ ", "RFC2253",
59
"CN=duke\\ ,OU=sun\\ ", 14);
60
testName("cn=duke\\ , ou= sun\\ ", "CANONICAL",
61
"cn=duke,ou=sun", 15);
62
testName("cn=duke\\ , ou= sun\\ ", "toString",
63
"CN=\"duke \", OU=\"sun \"", 16);
64
65
// tests for trailing escaped backslash
66
67
testName("cn=duke \\\\\\,test,O=java", "CANONICAL",
68
"cn=duke \\\\\\,test,o=java", 17);
69
70
testName("cn=duke\\\\, o=java", "CANONICAL",
71
"cn=duke\\\\,o=java", 18);
72
73
X500Principal p = new X500Principal("cn=duke \\\\\\,test,o=java");
74
X500Principal p2 = new X500Principal(p.getName("CANONICAL"));
75
if (p.getName("CANONICAL").equals(p2.getName("CANONICAL"))) {
76
System.out.println("test 19 succeeded");
77
} else {
78
throw new SecurityException("test 19 failed\n" +
79
p.getName("CANONICAL") + " not equal to " +
80
p2.getName("CANONICAL"));
81
}
82
83
try {
84
p = new X500Principal("cn=duke \\\\,test,o=java");
85
throw new SecurityException("test 19.5 failed:\n" +
86
p.getName("CANONICAL"));
87
} catch (IllegalArgumentException iae) {
88
System.out.println("test 19.5 succeeded");
89
iae.printStackTrace();
90
}
91
92
// tests for wrong exception thrown
93
try {
94
byte[] encoding = {
95
(byte)0x17, (byte)0x80, (byte)0x70, (byte)0x41,
96
(byte)0x6b, (byte)0x15, (byte)0xdc, (byte)0x84,
97
(byte)0xef, (byte)0x58, (byte)0xac, (byte)0x88,
98
(byte)0xae, (byte)0xb0, (byte)0x19, (byte)0x7c,
99
(byte)0x6f, (byte)0xea, (byte)0xf5, (byte)0x56,
100
};
101
p = new X500Principal(new java.io.DataInputStream
102
(new java.io.ByteArrayInputStream(encoding)));
103
} catch (IllegalArgumentException iae) {
104
System.out.println("test 20 succeeded");
105
iae.printStackTrace();
106
} catch (Exception e) {
107
System.out.println("test 20 failed");
108
throw e;
109
}
110
111
// tests for escaping '+' in canonical form
112
113
testName("cn=se\\+an, ou= sun\\ ", "CANONICAL",
114
"cn=se\\+an,ou=sun", 21);
115
116
// tests for embedded hex pairs
117
118
testName("CN=Before\\0dAfter,DC=example,DC=net", "toString",
119
"CN=Before\\0DAfter, DC=example, DC=net", 22);
120
testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC1779",
121
"CN=Before\\0DAfter, " +
122
"OID.0.9.2342.19200300.100.1.25=example, " +
123
"OID.0.9.2342.19200300.100.1.25=net", 23);
124
testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC2253",
125
"CN=Before\\0DAfter,DC=example,DC=net", 24);
126
testName("CN=Before\\0dAfter,DC=example,DC=net", "CANONICAL",
127
"cn=before\\0dafter,dc=#16076578616d706c65,dc=#16036e6574", 25);
128
129
testName("CN=Lu\\C4\\8Di\\C4\\87", "toString",
130
"CN=Lu\\C4\\8Di\\C4\\87", 26);
131
testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC1779",
132
"CN=Lu\\C4\\8Di\\C4\\87", 27);
133
testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC2253",
134
"CN=Lu\\C4\\8Di\\C4\\87", 28);
135
testName("CN=Lu\\C4\\8Di\\C4\\87", "CANONICAL",
136
"cn=lu\\c4\\8di\\c4\\87", 29);
137
138
try {
139
p = new X500Principal("cn=\\gg");
140
throw new SecurityException("test 30 failed");
141
} catch (IllegalArgumentException iae) {
142
System.out.println("test 30 succeeded");
143
}
144
145
// tests for invalid escaped chars
146
147
try {
148
p = new X500Principal("cn=duke \\test");
149
throw new SecurityException("test 31 failed");
150
} catch (IllegalArgumentException iae) {
151
System.out.println("test 31 succeeded");
152
}
153
154
try {
155
p = new X500Principal("cn=duke \\?test");
156
throw new SecurityException("test 32 failed");
157
} catch (IllegalArgumentException iae) {
158
System.out.println("test 32 succeeded");
159
}
160
161
// tests for X500Name using RFC2253 as format
162
163
try {
164
// invalid non-escaped leading space
165
sun.security.x509.X500Name name =
166
new sun.security.x509.X500Name("cn= duke test", "RFC2253");
167
throw new SecurityException("test 33 failed");
168
} catch (java.io.IOException ioe) {
169
ioe.printStackTrace();
170
System.out.println("test 33 succeeded");
171
}
172
173
try {
174
// invalid non-escaped trailing space
175
sun.security.x509.X500Name name =
176
new sun.security.x509.X500Name("cn=duke test ", "RFC2253");
177
throw new SecurityException("test 34 failed");
178
} catch (java.io.IOException ioe) {
179
System.out.println("test 34 succeeded");
180
}
181
182
testName("CN=SPECIAL CHARS,OU=\\#\\\"\\,\\<\\>\\+\\;,O=foo, " +
183
"L=bar, ST=baz, C=JP", "RFC1779",
184
"CN=SPECIAL CHARS, OU=\"#\\\",<>+;\", O=foo, L=bar, " +
185
"ST=baz, C=JP", 35);
186
187
// test that double-quoted string is not escaped in RFC 1779 format
188
testName("CN=\"\\\"Duke\\\"\"", "RFC1779", "CN=\"Duke\"", 36);
189
}
190
191
public static void testName(String in, String outFormat,
192
String expect, int n)
193
throws Exception {
194
195
X500Principal p = new X500Principal(in);
196
if (outFormat.equalsIgnoreCase("toString")) {
197
if (p.toString().equals(expect)) {
198
System.out.println("test " + n + " succeeded");
199
} else {
200
throw new SecurityException("test " + n + " failed:\n" +
201
"expected '" + expect + "'\n" +
202
"got '" + p.toString() + "'");
203
}
204
} else {
205
if (p.getName(outFormat).equals(expect)) {
206
System.out.println("test " + n + " succeeded");
207
} else {
208
throw new SecurityException("test " + n + " failed:\n" +
209
"expected '" + expect + "'\n" +
210
"got '" + p.getName(outFormat) + "'");
211
}
212
}
213
}
214
}
215
216