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