blob: d730650570359132b6ef1d9795010c275903a896 [file] [log] [blame]
Narayan Kamath62542d82013-11-25 15:06:09 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.tests.javax.security.auth.x500;
19
20import javax.security.auth.x500.X500Principal;
21import java.io.ByteArrayInputStream;
Kenny Root155ee0c2014-01-13 14:51:25 -080022import java.io.ByteArrayOutputStream;
Narayan Kamath62542d82013-11-25 15:06:09 +000023import java.io.InputStream;
24import java.security.cert.CertificateFactory;
25import java.security.cert.X509Certificate;
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.HashMap;
29import java.util.Locale;
30import java.util.Map;
31import junit.framework.TestCase;
32import org.apache.harmony.testframework.serialization.SerializationTest;
33import org.apache.harmony.security.tests.support.cert.TestUtils;
34import tests.support.resource.Support_Resources;
35
36
37/**
38 * Tests for <code>X500Principal</code> class constructors and methods.
39 *
40 */
41public class X500PrincipalTest extends TestCase {
42
43 /**
44 * javax.security.auth.x500.X500Principal#X500Principal(String name)
45 */
46 public void test_X500Principal_01() {
47 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
48
49 try {
50 X500Principal xpr = new X500Principal(name);
51 assertNotNull("Null object returned", xpr);
52 String resName = xpr.getName();
53 assertEquals(name, resName);
54 } catch (Exception e) {
55 fail("Unexpected exception: " + e);
56 }
57
58 try {
59 X500Principal xpr = new X500Principal((String)null);
60 fail("NullPointerException wasn't thrown");
61 } catch (NullPointerException npe) {
62 } catch (Exception e) {
63 fail(e + " was thrown instead of NullPointerException");
64 }
65
66 try {
67 X500Principal xpr = new X500Principal("X500PrincipalName");
68 fail("IllegalArgumentException wasn't thrown");
69 } catch (IllegalArgumentException npe) {
70 } catch (Exception e) {
71 fail(e + " was thrown instead of IllegalArgumentException");
72 }
73 }
74
75 /**
76 * javax.security.auth.x500.X500Principal#X500Principal(InputStream is)
77 */
78 public void test_X500Principal_02() {
79 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
80 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
81 ByteArrayInputStream is = new ByteArrayInputStream(ba);
82 InputStream isNull = null;
83
84 try {
85 X500Principal xpr = new X500Principal(is);
86 assertNotNull("Null object returned", xpr);
87 byte[] resArray = xpr.getEncoded();
88 assertEquals(ba.length, resArray.length);
89 } catch (Exception e) {
90 fail("Unexpected exception: " + e);
91 }
92
93 try {
94 X500Principal xpr = new X500Principal(isNull);
95 fail("NullPointerException wasn't thrown");
96 } catch (NullPointerException npe) {
97 } catch (Exception e) {
98 fail(e + " was thrown instead of NullPointerException");
99 }
100
101 is = new ByteArrayInputStream(name.getBytes());
102 try {
103 X500Principal xpr = new X500Principal(is);
104 fail("IllegalArgumentException wasn't thrown");
105 } catch (IllegalArgumentException npe) {
106 } catch (Exception e) {
107 fail(e + " was thrown instead of IllegalArgumentException");
108 }
109 }
110
111 /**
112 * javax.security.auth.x500.X500Principal#X500Principal(byte[] name)
113 */
114 public void test_X500Principal_03() {
115 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
116 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
117 byte[] baNull = null;
118
119 try {
120 X500Principal xpr = new X500Principal(ba);
121 assertNotNull("Null object returned", xpr);
122 byte[] resArray = xpr.getEncoded();
123 assertEquals(ba.length, resArray.length);
124 } catch (Exception e) {
125 fail("Unexpected exception: " + e);
126 }
127
128 try {
129 X500Principal xpr = new X500Principal(baNull);
130 fail("IllegalArgumentException wasn't thrown");
131 } catch (IllegalArgumentException npe) {
132 } catch (Exception e) {
133 fail(e + " was thrown instead of IllegalArgumentException");
134 }
135
136 ba = name.getBytes();
137 try {
138 X500Principal xpr = new X500Principal(ba);
139 fail("IllegalArgumentException wasn't thrown");
140 } catch (IllegalArgumentException npe) {
141 } catch (Exception e) {
142 fail(e + " was thrown instead of IllegalArgumentException");
143 }
144 }
145
146 /**
147 * javax.security.auth.x500.X500Principal#getName()
148 */
149 public void test_getName() {
150 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
151 X500Principal xpr = new X500Principal(name);
152 try {
153 String resName = xpr.getName();
154 assertEquals(name, resName);
155 } catch (Exception e) {
156 fail("Unexpected exception: " + e);
157 }
158 }
159
160 /**
161 * javax.security.auth.x500.X500Principal#getName(String format)
162 */
163 public void test_getName_Format() {
164 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
165 String expectedName = "cn=duke,ou=javasoft,o=sun microsystems,c=us";
166 X500Principal xpr = new X500Principal(name);
167 try {
168 String resName = xpr.getName(X500Principal.CANONICAL);
169 assertEquals(expectedName, resName);
170 } catch (Exception e) {
171 fail("Unexpected exception: " + e);
172 }
173
174 expectedName = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
175 try {
176 String resName = xpr.getName(X500Principal.RFC1779);
177 assertEquals(expectedName, resName);
178 } catch (Exception e) {
179 fail("Unexpected exception: " + e);
180 }
181
182 try {
183 String resName = xpr.getName(X500Principal.RFC2253);
184 assertEquals(name, resName);
185 } catch (Exception e) {
186 fail("Unexpected exception: " + e);
187 }
188
189 try {
190 String resName = xpr.getName(null);
191 fail("IllegalArgumentException wasn't thrown");
192 } catch (IllegalArgumentException iae) {
193 }
194 try {
195 String resName = xpr.getName("RFC2254");
196 fail("IllegalArgumentException wasn't thrown");
197 } catch (IllegalArgumentException iae) {
198 }
199 }
200
201 /**
202 * javax.security.auth.x500.X500Principal#hashCode()
203 */
204 public void test_hashCode() {
205 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
206 X500Principal xpr = new X500Principal(name);
207 try {
208 int res = xpr.hashCode();
209 assertNotNull(res);
210 } catch (Exception e) {
211 fail("Unexpected exception: " + e);
212 }
213 }
214
215 /**
216 * javax.security.auth.x500.X500Principal#toString()
217 */
218 public void test_toString() {
219 String name = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
220 X500Principal xpr = new X500Principal(name);
221 try {
222 String res = xpr.toString();
223 assertNotNull(res);
224 assertEquals(name, res);
225 } catch (Exception e) {
226 fail("Unexpected exception: " + e);
227 }
228 }
229
230 /**
231 * javax.security.auth.x500.X500Principal#getEncoded()
232 */
233 public void test_getEncoded() {
234 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
235 X500Principal xpr = new X500Principal(ba);
236 try {
237 byte[] res = xpr.getEncoded();
238 assertNotNull(res);
239 assertEquals(ba.length, res.length);
240 } catch (Exception e) {
241 fail("Unexpected exception: " + e);
242 }
243 }
244
245 /**
246 * javax.security.auth.x500.X500Principal#equals(Object o)
247 */
248 public void test_equals() {
249 String name1 = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
250 String name2 = "cn=duke,ou=javasoft,o=sun microsystems,c=us";
251 String name3 = "CN=Alex Astapchuk, OU=SSG, O=Intel ZAO, C=RU";
252 X500Principal xpr1 = new X500Principal(name1);
253 X500Principal xpr2 = new X500Principal(name2);
254 X500Principal xpr3 = new X500Principal(name3);
255 try {
256 assertTrue("False returned", xpr1.equals(xpr2));
257 assertFalse("True returned", xpr1.equals(xpr3));
258 } catch (Exception e) {
259 fail("Unexpected exception: " + e);
260 }
261 }
262
263 private byte[] getByteArray(byte[] array) {
264 byte[] x = null;
265 try {
266 ByteArrayInputStream is = new ByteArrayInputStream(array);
267 CertificateFactory cf = CertificateFactory.getInstance("X.509");
268 X509Certificate cert = (X509Certificate)cf.generateCertificate(is);
269 X500Principal xx = cert.getIssuerX500Principal();
270 x = xx.getEncoded();
271 } catch (Exception e) {
272 return null;
273 }
274 return x;
275 }
276
277 /**
278 * @tests javax.security.auth.x500.X500Principal#X500Principal(java.lang.String)
279 */
280 public void test_ConstructorLjava_lang_String() {
281 X500Principal principal = new X500Principal(
282 "CN=Hermione Granger, O=Apache Software Foundation, OU=Harmony, L=Hogwarts, ST=Hants, C=GB");
283 String name = principal.getName();
284 String expectedOuput = "CN=Hermione Granger,O=Apache Software Foundation,OU=Harmony,L=Hogwarts,ST=Hants,C=GB";
285 assertEquals("Output order precedence problem", expectedOuput, name);
286 }
287
288 /**
289 * @tests javax.security.auth.x500.X500Principal#X500Principal(java.lang.String, java.util.Map)
290 */
291 public void test_ConstructorLjava_lang_String_java_util_Map() {
292 Map<String, String> keyword = new HashMap<String, String>();
293 keyword.put("CN", "2.19");
294 keyword.put("OU", "1.2.5.19");
295 keyword.put("O", "1.2.5");
296 X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US ,CN=DD", keyword);
297 String name = X500p.getName();
298 String expectedOut = "2.19=#130444756b65,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,2.19=#13024444";
299 assertEquals("Output order precedence problem", expectedOut, name);
300 }
301
302 /**
303 * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String)
304 */
305 public void test_getNameLjava_lang_String() {
306 X500Principal principal = new X500Principal(
307 "CN=Dumbledore, OU=Administration, O=Hogwarts School, C=GB");
308 String canonical = principal.getName(X500Principal.CANONICAL);
309 String expected = "cn=dumbledore,ou=administration,o=hogwarts school,c=gb";
310 assertEquals("CANONICAL output differs from expected result", expected,
311 canonical);
312 }
313
314 /**
315 * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String, java.util.Map)
316 */
317 public void test_getNameLjava_lang_String_java_util_Map() {
318 Map<String, String> keyword = new HashMap<String, String>();
319 keyword.put("CN", "2.19");
320 keyword.put("OU", "1.2.5.19");
321 keyword.put("O", "1.2.5");
322 X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US ,CN=DD", keyword);
323 keyword = new HashMap<String, String>();
324 keyword.put("2.19", "mystring");
325 String rfc1779Name = X500p.getName("RFC1779", keyword);
326 String rfc2253Name = X500p.getName("RFC2253", keyword);
327 String expected1779Out = "mystring=Duke, OID.1.2.5.19=JavaSoft, OID.1.2.5=Sun Microsystems, C=US, mystring=DD";
328 String expected2253Out = "mystring=Duke,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,mystring=DD";
329 assertEquals("Output order precedence problem", expected1779Out, rfc1779Name);
330 assertEquals("Output order precedence problem", expected2253Out, rfc2253Name);
331 try {
332 X500p.getName("CANONICAL", keyword);
333 fail("Should throw IllegalArgumentException exception here");
334 } catch (IllegalArgumentException e) {
335 //expected IllegalArgumentException here
336 }
337 }
338
339 private boolean testing = false;
340
341 public void testStreamPosition() throws Exception {
342 //this encoding is read from the file
343 /*byte [] mess = {0x30, 0x30,
344 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41,
345 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41,
346 1, 2, 3//extra bytes
347 };
348 */
349
350 InputStream is = Support_Resources
351 .getResourceStream("X500PrincipalTest.0.dat");
352 X500Principal principal = new X500Principal(is);
353 String s = principal.toString();
354 assertEquals("CN=A, CN=B, CN=A, CN=B", s);
355 byte[] restBytes = new byte[] { 0, 0, 0 };
356 is.read(restBytes);
357 assertEquals(restBytes[0], 1);
358 assertEquals(restBytes[1], 2);
359 assertEquals(restBytes[2], 3);
360 is.close();
361 }
362
363 public void testStreamPosition_0() throws Exception {
364 //this encoding is read from the file
365 /*byte [] mess = {0x30, 0x30,
366 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41,
367 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41,
368 };
369 */
370
371 InputStream is = Support_Resources
372 .getResourceStream("X500PrincipalTest.1.dat");
373 X500Principal principal = new X500Principal(is);
374 String s = principal.toString();
375 assertEquals("CN=A, CN=B, CN=A, CN=B", s);
376 assertEquals(0, is.available());
377 is.close();
378 }
379
380 public void testStreamPosition_1() throws Exception {
381 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
382 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
383 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
384 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
385 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
386 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
387 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
388 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
389 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
390 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
391 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
392 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
393 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
394 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
395 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
396 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 2,
397 3, 4 };
398
399 ByteArrayInputStream is = new ByteArrayInputStream(mess);
400 X500Principal principal = new X500Principal(is);
401
402 String s = principal.getName(X500Principal.RFC1779);
403 assertEquals(
404 "CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=Z",
405 s);
406 assertEquals(3, is.available());
407 assertEquals(2, is.read());
408 assertEquals(3, is.read());
409 assertEquals(4, is.read());
410 }
411
412 public void testStreamPosition_2() throws Exception {
413 byte[] mess = { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
414 0x04, 0x03, 0x13, 0x01, 0x41, 2 };
415 ByteArrayInputStream is = new ByteArrayInputStream(mess);
416 X500Principal principal = new X500Principal(is);
417 String s = principal.getName(X500Principal.RFC1779);
418 assertEquals("CN=A", s);
419 assertEquals(1, is.available());
420 assertEquals(2, is.read());
421 }
422
423 public void testEncodingFromFile() throws Exception {
424 //this encoding is read from the file
425 /*byte [] mess = {0x30, 0x30,
426 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41,
427 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41
428 };
429 */
430 InputStream is = Support_Resources
431 .getResourceStream("X500PrincipalTest.1.dat");
432 X500Principal principal = new X500Principal(is);
433 String s = principal.toString();
434 assertEquals("CN=A, CN=B, CN=A, CN=B", s);
435 is.close();
436 }
437
438 public void testEncodingFromEncoding() {
439 byte[] arr1 = new X500Principal("O=Org.").getEncoded();
440 byte[] arr2 = new X500Principal(new X500Principal("O=Org.")
441 .getEncoded()).getEncoded();
442 assertTrue(Arrays.equals(arr1, arr2));
443 }
444
445 /**
446 * tests if the encoding is backed
447 */
448 public void testSafeEncoding() {
449 byte[] mess = { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
450 0x04, 0x03, 0x13, 0x01, 0x41 };
451 X500Principal principal = new X500Principal(mess);
452 mess[mess.length - 1] = (byte) 0xFF;
453 byte[] enc = principal.getEncoded();
454 assertEquals(enc[mess.length - 1], 0x41);
455 }
456
457 /**
458 * Inits X500Principal with byte array
459 * gets toString
460 * checks the result
461 */
462 public void testToString() throws Exception {
463 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
464 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
465 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
466 X500Principal principal = new X500Principal(mess);
467 String s = principal.toString();
468 assertNotNull(s);
469 }
470
471 /**
472 * Inits X500Principal with byte array
473 * gets hashCode
474 * compares with expected value
475 */
476 public void testHashCode() throws Exception {
477 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
478 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
479 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
480 X500Principal principal = new X500Principal(mess);
481 int hash = principal.hashCode();
482 assertEquals(principal.getName(X500Principal.CANONICAL).hashCode(),
483 hash);
484 }
485
486 /**
487 * Inits X500Principal with byte array
488 * Inits other X500Principal with equivalent string
489 * checks if <code>equals</code> returns true for first against second one
490 */
491 public void testEquals() throws Exception {
492 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
493 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
494 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
495 X500Principal principal = new X500Principal(mess);
496 X500Principal principal2 = new X500Principal("CN=A, CN=B");
497 assertTrue(principal.equals(principal2));
498 }
499
500 /**
501 * @tests javax.security.auth.x500.X500Principal#equals(Object)
502 */
503 public void test_equalsLjava_lang_Object() {
504 X500Principal xp1 = new X500Principal(
505 "C=US, ST=California, L=San Diego, O=Apache, OU=Project Harmony, CN=Test cert");
506 assertEquals(
507 "C=US,ST=California,L=San Diego,O=Apache,OU=Project Harmony,CN=Test cert",
508 xp1.getName());
509 }
510
511 /**
512 * Inits X500Principal with byte array, where Oid does fall into any keyword, but not given as a keyword
513 * Value is given as hex value
514 * (extra spaces are given)
515 * gets Name in RFC1779 format
516 * compares with expected value of name
517 */
518 public void testKWAsOid_RFC1779() throws Exception {
519 String dn = "CN=A, OID.2.5.4.3 = #130142";
520 X500Principal principal = new X500Principal(dn);
521
522 String s = principal.getName(X500Principal.RFC1779);
523 assertEquals("CN=A, CN=B", s);
524 }
525
526 /**
527 * Inits X500Principal with byte array, where Oid does fall into any keyword, but not given as a keyword
528 * Value is given as hex value
529 * (extra spaces are given)
530 * gets Name in RFC2253 format
531 * compares with expected value of name
532 */
533 public void testKWAsOid_RFC2253() throws Exception {
534 String dn = "CN=A, OID.2.5.4.3 = #130142";
535 X500Principal principal = new X500Principal(dn);
536
537 String s = principal.getName(X500Principal.RFC2253);
538 assertEquals("CN=A,CN=B", s);
539 }
540
541 /**
542 * Inits X500Principal with byte array, where Oid does fall into any keyword, but not given as a keyword
543 * Value is given as hex value
544 * (extra spaces are given)
545 * gets Name in CANONICAL format
546 * compares with expected value of name
547 */
548 public void testKWAsOid_CANONICAL() throws Exception {
549 String dn = "CN=A, OID.2.5.4.3 = #130142";
550 X500Principal principal = new X500Principal(dn);
551
552 String s = principal.getName(X500Principal.CANONICAL);
553 assertEquals("cn=a,cn=b", s);
554 }
555
556 /**
557 * Inits X500Principal with byte array, where Oid does not fall into any keyword
558 * gets Name in RFC1779 format
559 * compares with expected value of name
560 */
561 public void testOid_RFC1779() throws Exception {
562 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
563 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
564 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
565 mess[8] = 0x60;
566 X500Principal principal = new X500Principal(mess);
567
568 String s = principal.getName(X500Principal.RFC1779);
569 assertEquals("CN=A, OID.2.16.4.3=B", s);
570 }
571
572 /**
573 * Inits X500Principal with byte array, where Oid does not fall into any keyword
574 * gets Name in RFC2253 format
575 * compares with expected value of name
576 */
577 public void testOid_RFC2253() throws Exception {
578 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
579 0x04, 0x03, 0x13, 0x01, 0x4F, 0x31, 0x0A, 0x30, 0x08, 0x06,
580 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
581 mess[8] = 0x60;
582 X500Principal principal = new X500Principal(mess);
583
584 String s = principal.getName(X500Principal.RFC2253);
585 assertEquals("CN=A,2.16.4.3=#13014f", s);
586 }
587
588 /**
589 * Inits X500Principal with byte array, where Oid does not fall into any keyword
590 * gets Name in CANONICAL format
591 * compares with expected value of name
592 */
593 public void testOid_CANONICAL() throws Exception {
594 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
595 0x04, 0x03, 0x13, 0x01, 0x4F, 0x31, 0x0A, 0x30, 0x08, 0x06,
596 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
597 mess[8] = 0x60;
598 X500Principal principal = new X500Principal(mess);
599
600 String s = principal.getName(X500Principal.CANONICAL);
601 assertEquals("cn=a,2.16.4.3=#13014f", s);
602 }
603
604 /**
605 * Inits X500Principal with a string
606 * gets encoded form
607 * compares with expected byte array
608 */
609 public void testNameGetEncoding() throws Exception {
610 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
611 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
612 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
613 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
614 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
615 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
616 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
617 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
618 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
619 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
620 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
621 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
622 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
623 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
624 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
625 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
626 String dn = "CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=Z";
627 X500Principal principal = new X500Principal(dn);
628 byte[] s = principal.getEncoded();
629
630 assertTrue(Arrays.equals(mess, s));
631 }
632
633 /**
634 * Inits X500Principal with a string
635 * gets encoded form
636 * compares with expected byte array
637 */
638 public void testNameGetEncoding_01() throws Exception {
639 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
640 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
641 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
642 String dn = "CN=A,CN=B";
643 X500Principal principal = new X500Principal(dn);
644 byte[] s = principal.getEncoded();
645
646 assertTrue(Arrays.equals(mess, s));
647 }
648
649 /**
650 * Inits X500Principal with byte array
651 * gets Name in RFC1779 format
652 * compares with expected value of name
653 */
654 public void testGetName_RFC1779() throws Exception {
655 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
656 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
657 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
658 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
659 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
660 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
661 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
662 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
663 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
664 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
665 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
666 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
667 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
668 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
669 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
670 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
671 X500Principal principal = new X500Principal(mess);
672
673 String s = principal.getName(X500Principal.RFC1779);
674 assertEquals(
675 "CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=Z",
676 s);
677
678 }
679
680 /**
681 * Inits X500Principal with byte array
682 * gets Name in RFC2253 format
683 * compares with expected value of name
684 */
685 public void testGetName_RFC2253() throws Exception {
686 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
687 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
688 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
689 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
690 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
691 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
692 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
693 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
694 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
695 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
696 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
697 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
698 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
699 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
700 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
701 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
702 X500Principal principal = new X500Principal(mess);
703
704 String s = principal.getName(X500Principal.RFC2253);
705 assertEquals(
706 "CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=Z",
707 s);
708 }
709
710 /**
711 * Inits X500Principal with byte array
712 * gets Name in CANONICAL format
713 * compares with expected value of name
714 */
715 public void testGetName_CANONICAL() throws Exception {
716 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
717 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
718 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
719 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
720 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
721 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
722 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
723 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
724 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
725 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
726 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
727 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
728 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
729 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
730 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
731 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
732 X500Principal principal = new X500Principal(mess);
733
734 String s = principal.getName(X500Principal.CANONICAL);
735 assertEquals(
736 "CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=Z"
737 .toLowerCase(Locale.US), s);
738 }
739
740 /**
741 * Inits X500Principal with byte array
742 * gets Name in RFC1779 format
743 * compares with expected value of name
744 */
745 public void testStreamGetName_RFC1779() throws Exception {
746 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
747 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
748 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
749 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
750 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
751 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
752 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
753 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
754 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
755 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
756 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
757 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
758 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
759 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
760 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
761 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
762 ByteArrayInputStream is = new ByteArrayInputStream(mess);
763 X500Principal principal = new X500Principal(is);
764
765 String s = principal.getName(X500Principal.RFC1779);
766 assertEquals(
767 "CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=A + ST=CA, O=B, L=C, C=D, OU=E, CN=Z",
768 s);
769 }
770
771 /**
772 * Inits X500Principal with byte array
773 * gets Name in RFC2253 format
774 * compares with expected value of name
775 */
776 public void testStreamGetName_RFC2253() throws Exception {
777 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
778 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
779 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
780 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
781 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
782 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
783 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
784 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
785 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
786 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
787 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
788 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
789 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
790 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
791 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
792 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
793 ByteArrayInputStream is = new ByteArrayInputStream(mess);
794 X500Principal principal = new X500Principal(is);
795
796 String s = principal.getName(X500Principal.RFC2253);
797 assertEquals(
798 "CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=Z",
799 s);
800 }
801
802 /**
803 * Inits X500Principal with byte array
804 * gets Name in CANONICAL format
805 * compares with expected value of name
806 */
807 public void testStreamGetName_CANONICAL() throws Exception {
808 byte[] mess = { 0x30, (byte) 0x81, (byte) 0x9A, 0x31, 0x0A, 0x30, 0x08,
809 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x5A, 0x31, 0x0A,
810 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01, 0x45,
811 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
812 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
813 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
814 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30, 0x08,
815 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30, 0x09,
816 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41, 0x31,
817 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x01,
818 0x45, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x06,
819 0x13, 0x01, 0x44, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
820 0x04, 0x07, 0x13, 0x01, 0x43, 0x31, 0x0A, 0x30, 0x08, 0x06,
821 0x03, 0x55, 0x04, 0x0A, 0x13, 0x01, 0x42, 0x31, 0x15, 0x30,
822 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x30,
823 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x43, 0x41 };
824 ByteArrayInputStream is = new ByteArrayInputStream(mess);
825 X500Principal principal = new X500Principal(is);
826
827 String s = principal.getName(X500Principal.CANONICAL);
828 assertEquals(
829 "CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=A+ST=CA,O=B,L=C,C=D,OU=E,CN=Z"
830 .toLowerCase(Locale.US), s);
831 }
832
833 /**
834 * Inits X500Principal with a string, where OID does not fall into any keyword
835 * gets encoded form
836 * inits new X500Principal with the encoding
837 * gets string in RFC1779 format
838 * compares with expected value
839 */
840 public void testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC1779()
841 throws Exception {
842 String dn = "OID.2.16.4.3=B; CN=A";
843 X500Principal principal = new X500Principal(dn);
844 byte[] enc = principal.getEncoded();
845 X500Principal principal2 = new X500Principal(enc);
846 String s = principal2.getName(X500Principal.RFC1779);
847 assertEquals("OID.2.16.4.3=B, CN=A", s);
848
849 }
850
851 /**
852 * Inits X500Principal with a string, where OID does not fall into any keyword
853 * gets encoded form
854 * inits new X500Principal with the encoding
855 * gets string in RFC2253 format
856 * compares with expected value
857 */
858 public void testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC2253()
859 throws Exception {
860 String dn = "OID.2.16.4.3=B; CN=A";
861 X500Principal principal = new X500Principal(dn);
862 byte[] enc = principal.getEncoded();
863 X500Principal principal2 = new X500Principal(enc);
864 String s = principal2.getName(X500Principal.RFC2253);
865 assertEquals("2.16.4.3=#130142,CN=A", s);
866
867 }
868
869 /**
870 * Inits X500Principal with a string, where OID does not fall into any keyword
871 * gets encoded form
872 * inits new X500Principal with the encoding
873 * gets string in CANONICAL format
874 * compares with expected value
875 */
876 public void testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_CANONICAL()
877 throws Exception {
878 String dn = "OID.2.16.4.3=B; CN=A";
879 X500Principal principal = new X500Principal(dn);
880 byte[] enc = principal.getEncoded();
881 X500Principal principal2 = new X500Principal(enc);
882 String s = principal2.getName(X500Principal.CANONICAL);
883 assertEquals("2.16.4.3=#130142,cn=a", s);
884
885 }
886
887 /**
888 * Inits X500Principal with a string, where OID does not fall into any keyword
889 * gets string in RFC1779 format
890 * compares with expected value
891 */
892 public void testGetName_wrongOidButGoodName_RFC1779() throws Exception {
893 String dn = "OID.2.16.4.3=B + CN=A";
894 X500Principal principal = new X500Principal(dn);
895
896 String s = principal.getName(X500Principal.RFC1779);
897 assertEquals("OID.2.16.4.3=B + CN=A", s);
898 }
899
900 /**
901 * Inits X500Principal with a string, where OID does not fall into any keyword
902 * gets string in RFC2253 format
903 * compares with expected value
904 */
905 public void testGetName_wrongOidButGoodName_RFC2253() throws Exception {
906 String dn = "OID.2.16.4.3=B + CN=A";
907 X500Principal principal = new X500Principal(dn);
908
909 String s = principal.getName(X500Principal.RFC2253);
910 assertEquals("2.16.4.3=#130142+CN=A", s);
911 }
912
913 /**
914 * Inits X500Principal with a string, there are multiple AVAs
915 * gets string in CANONICAL format
916 * compares with expected value paying attention on sorting order of AVAs
917 */
918 public void testGetName_CANONICAL_SortOrder() throws Exception {
919 String dn = "ST=C + CN=A; OU=B + CN=D";
920 X500Principal principal = new X500Principal(dn);
921
922 String s = principal.getName(X500Principal.CANONICAL);
923 assertEquals("cn=a+st=c,cn=d+ou=b", s);
924
925 }
926
927 /**
928 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
929 * gets string in CANONICAL format
930 * compares with expected value paying attention on sorting order of AVAs
931 */
932 public void testGetName_CANONICAL_SortOrder_01() throws Exception {
933 String dn = "OID.2.16.4.3=B + CN=A";
934 X500Principal principal = new X500Principal(dn);
935
936 String s = principal.getName(X500Principal.CANONICAL);
937 assertEquals("cn=a+2.16.4.3=#130142", s);
938
939 }
940
941 /**
942 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword, and value given in hex format
943 * gets string in CANONICAL format
944 * compares with expected value paying attention on sorting order of AVAs
945 */
946 public void testGetName_CANONICAL_SortOrder_02() throws Exception {
947 String dn = "OID.2.16.4.3=#13024220+ CN=A";
948 X500Principal principal = new X500Principal(dn);
949
950 String s = principal.getName(X500Principal.CANONICAL);
951 assertEquals("cn=a+2.16.4.3=#13024220", s);
952
953 }
954
955 /**
956 * Inits X500Principal with a string, there are multiple AVAs and 2 Oids which do not fall into any keyword
957 * gets string in CANONICAL format
958 * compares with expected value paying attention on sorting order of AVAs
959 */
960 public void testGetName_CANONICAL_SortOrder_03() throws Exception {
961 String dn = "OID.2.16.4.9=A + OID.2.16.4.3=B";
962 X500Principal principal = new X500Principal(dn);
963
964 String s = principal.getName(X500Principal.CANONICAL);
965 assertEquals("2.16.4.3=#130142+2.16.4.9=#130141", s);
966
967 }
968
969 /**
970 * Inits X500Principal with a string, there are multiple AVAs and 2 Oids which do not fall into any keyword
971 * gets string in CANONICAL format
972 * compares with expected value paying attention on sorting order of AVAs
973 */
974 public void testGetName_CANONICAL_SortOrder_04() throws Exception {
975 String dn = "OID.2.2.2.2=A + OID.1.1.1.1=B";
976 X500Principal principal = new X500Principal(dn);
977
978 String s = principal.getName(X500Principal.CANONICAL);
979 assertEquals("1.1.1.1=#130142+2.2.2.2=#130141", s);
980
981 }
982
983 /**
984 * Inits X500Principal with a string, there are multiple AVAs and 2 Oids which do not fall into any keyword
985 * gets string in CANONICAL format
986 * compares with expected value paying attention on sorting order of AVAs
987 */
988 public void testGetName_CANONICAL_SortOrder_05() throws Exception {
989 String dn = "OID.2.16.4.9=A + OID.2.16.4=B";
990 X500Principal principal = new X500Principal(dn);
991
992 String s = principal.getName(X500Principal.CANONICAL);
993 assertEquals("2.16.4=#130142+2.16.4.9=#130141", s);
994
995 }
996
997 /**
998 * Inits X500Principal with a string, there are multiple AVAs and 2 Oids which do not fall into any keyword
999 * gets string in CANONICAL format
1000 * compares with expected value paying attention on sorting order of AVAs
1001 */
1002 public void testGetName_CANONICAL_SortOrder_06() throws Exception {
1003 String dn = "OID.1.1.2=A + OID.1.2=B";
1004 X500Principal principal = new X500Principal(dn);
1005
1006 String s = principal.getName(X500Principal.CANONICAL);
1007 assertEquals("1.1.2=#130141+1.2=#130142", s);
1008
1009 }
1010
1011 /**
1012 * Inits X500Principal with a string, there are multiple AVAs and 2 Oids which do not fall into any keyword
1013 * gets string in CANONICAL format
1014 * compares with expected value paying attention on sorting order of AVAs
1015 */
1016 public void testGetName_CANONICAL_SortOrder_07() throws Exception {
1017 String dn = "OID.1.1.1=A + OID.1.1=B";
1018 X500Principal principal = new X500Principal(dn);
1019
1020 String s = principal.getName(X500Principal.CANONICAL);
1021 assertEquals("1.1=#130142+1.1.1=#130141", s);
1022
1023 }
1024
1025 /**
1026 * FIXME test is failed - implement unicode normalization
1027 *
1028 * @throws Exception
1029 */
1030 public void testGetNameUnicodeNormalized() throws Exception {
1031 String unicodeStr = "CN= \u0401\u0410";
1032 X500Principal principal = new X500Principal(unicodeStr);
1033 principal.getName(X500Principal.CANONICAL);
1034 }
1035
1036 /**
1037 * Inits X500Principal with empty string
1038 * gets encoding
1039 * compares with expected encoding
1040 */
1041 public void testEmptyInputName() {
1042 String dn = "CN=\"\"";
1043 byte[] mess = { 0x30, 0x0B, 0x31, 0x09, 0x30, 0x07, 0x06, 0x03, 0x55,
1044 0x04, 0x03, 0x13, 0x00 };
1045 X500Principal principal = new X500Principal(dn);
1046 assertTrue(Arrays.equals(mess, principal.getEncoded()));
1047 }
1048
1049 /**
1050 * Inits X500Principal with string as single escaped space
1051 * gets encoding
1052 * compares with expected encoding
1053 */
1054 public void testNameSingleEscapedSpace() {
1055 String dn = "CN=\\ ";
1056 byte[] mess = { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
1057 0x04, 0x03, 0x13, 0x01, 0x20 };
1058 X500Principal principal = new X500Principal(dn);
1059 assertTrue(Arrays.equals(mess, principal.getEncoded()));
1060 }
1061
1062 /**
1063 * Inits X500Principal with string with spaces
1064 * gets Name in RFC2253 format
1065 * compares with expected value of name
1066 */
1067 public void testNameOnlySpaces_RFC1779() {
1068 String dn = "CN=\" \"";
1069 X500Principal principal = new X500Principal(dn);
1070 assertEquals("CN=\" \"", principal.getName(X500Principal.RFC1779));
1071 }
1072
1073 /**
1074 * Inits X500Principal with string with spaces
1075 * gets Name in RFC2253 format
1076 * compares with expected value of name
1077 */
1078 public void testNameOnlySpaces_RFC2253() {
1079 String dn = "CN=\" \"";
1080 X500Principal principal = new X500Principal(dn);
1081 assertEquals("CN=\\ \\ ", principal.getName(X500Principal.RFC2253));
1082 }
1083
1084 /**
1085 * Inits X500Principal with string with only spaces,
1086 * gets Name in CANONICAL format:leading and trailing white space
1087 * chars are removed even string doesn't have other chars (bug???)
1088 */
1089 public void testNameOnlySpaces_CANONICAL() {
1090 String dn = "CN=\" \"";
1091 X500Principal principal = new X500Principal(dn);
1092 assertEquals("cn=", principal.getName(X500Principal.CANONICAL));
1093 }
1094
1095 ///*** Negative Tests ***///
1096
1097 /**
1098 * Inits X500Principal with string, where DN name is improper "CNN"
1099 * checks if proper exception is thrown
1100 */
1101 public void testIllegalInputName() {
1102 try {
1103 String dn = "CNN=A";
1104 new X500Principal(dn);
1105 fail("No IllegalArgumentException on improper input name \"CNN\"");
1106 } catch (IllegalArgumentException e) {
1107 }
1108 }
1109
1110 /**
1111 * Inits X500Principal with string, where there is leading ';'
1112 * checks if proper exception is thrown
1113 */
1114 public void testIllegalInputName_01() {
1115 try {
1116 String dn = ";CN=A";
1117 new X500Principal(dn);
1118 fail("No IllegalArgumentException on leading ';' in input name");
1119 } catch (IllegalArgumentException e) {
1120 }
1121 }
1122
1123 /**
1124 * Inits X500Principal with string, where there is leading '='
1125 * checks if proper exception is thrown
1126 */
1127 public void testIllegalInputName_02() {
1128 try {
1129 String dn = "=CN=A";
1130 new X500Principal(dn);
1131 fail("No IllegalArgumentException on leading '=' in input name");
1132 } catch (IllegalArgumentException e) {
1133 }
1134 }
1135
1136 /**
1137 * Inits X500Principal with string, where there is no value
1138 * checks if proper exception is thrown
1139 */
1140 public void testEmptyInputName_0() {
1141 String dn = "CN=";
1142 byte[] mess = { 0x30, 0x0B, 0x31, 0x09, 0x30, 0x07, 0x06, 0x03, 0x55,
1143 0x04, 0x03, 0x13, 0x00 };
1144 X500Principal principal = new X500Principal(dn);
1145 assertTrue(Arrays.equals(mess, principal.getEncoded()));
1146 }
1147
1148 public void testEmptyInputName_1() {
1149 String dn = "CN=\"\", C=\"\"";
1150 X500Principal principal = new X500Principal(dn);
1151 dn = "CN=, C=";
1152 X500Principal principal2 = new X500Principal(dn);
1153 assertTrue(Arrays.equals(principal.getEncoded(), principal2
1154 .getEncoded()));
1155
1156 }
1157
1158 public void testEmptyInputName_2() {
1159 String dn = "CN=\"\" + OU=A, C=\"\"";
1160 X500Principal principal = new X500Principal(dn);
1161 dn = "CN=+OU=A, C=";
1162 X500Principal principal2 = new X500Principal(dn);
1163 assertTrue(Arrays.equals(principal.getEncoded(), principal2
1164 .getEncoded()));
1165
1166 }
1167
1168 public void testIllegalInputName_15() {
1169 try {
1170 String dn = "CN=,C";
1171 new X500Principal(dn);
1172 fail("No IllegalArgumentException on improper attribute value");
1173 } catch (IllegalArgumentException e) {
1174 }
1175 }
1176
1177 public void testIllegalInputName_16() {
1178 try {
1179 String dn = "CN=,C=+";
1180 new X500Principal(dn);
1181 fail("No IllegalArgumentException on improper attribute value");
1182 } catch (IllegalArgumentException e) {
1183 }
1184 }
1185
1186 /**
1187 * Inits X500Principal with string, where value is given in wrong hex format
1188 * checks if proper exception is thrown
1189 */
1190 public void testIllegalInputName_04() {
1191 try {
1192 String dn = "CN=#XYZ";
1193 new X500Principal(dn);
1194 fail("No IllegalArgumentException on improper hex value");
1195 } catch (IllegalArgumentException e) {
1196 }
1197 }
1198
1199 /**
1200 * Inits X500Principal with string, where value is given with special chars
1201 * checks if proper exception is thrown
1202 */
1203 public void testIllegalInputName_05() {
1204 try {
1205 String dn = "CN=X+YZ";
1206 new X500Principal(dn);
1207 fail("No IllegalArgumentException on improper attribute value");
1208 } catch (IllegalArgumentException e) {
1209 }
1210 }
1211
1212 /**
1213 * Inits X500Principal with string, where value is given with special chars
1214 * Compatibility issue: according RFC 2253 such string is invalid
1215 * but we accept it, not string char is escaped
1216 */
1217 public void testIllegalInputName_06() {
1218 String dn = "CN=X=YZ";
1219 X500Principal p = new X500Principal(dn);
1220 assertEquals("CN=X\\=YZ", p.getName(X500Principal.RFC2253));
1221 }
1222
1223 /**
1224 * Inits X500Principal with string, where value is given with not string chars
1225 * Compatibility issue: according RFC 2253 such string is invalid
1226 * but we accept it, not string char is escaped
1227 */
1228 public void testIllegalInputName_07() {
1229 String dn = "CN=X\"YZ";
1230 X500Principal p = new X500Principal(dn);
1231 assertEquals("CN=X\\\"YZ", p.getName(X500Principal.RFC2253));
1232 }
1233
1234 /**
1235 * Inits X500Principal with string, where value is given with special chars
1236 * Compatibility issue: according RFC 2253 such string is invalid
1237 * but we accept it, special char is escaped
1238 */
1239 public void testIllegalInputName_08() {
1240 String dn = "CN=X<YZ";
1241 X500Principal p = new X500Principal(dn);
1242 assertEquals("CN=X\\<YZ", p.getName(X500Principal.RFC2253));
1243 }
1244
1245 /**
1246 * Inits X500Principal with string, where value is given with special chars
1247 * checks if proper exception is thrown
1248 */
1249 public void testIllegalInputName_09() {
1250 try {
1251 String dn = "CN=#";
1252 new X500Principal(dn);
1253 fail("No IllegalArgumentException on improper attribute hex value");
1254 } catch (IllegalArgumentException e) {
1255 //ignore
1256 }
1257
1258 }
1259
1260 /**
1261 * Inits X500Principal with string, where value is given with special chars
1262 * checks if proper exception is thrown
1263 */
1264 public void testIllegalInputName_10() {
1265 try {
1266 String dn = "CN=#13";
1267 new X500Principal(dn);
1268 fail("No IllegalArgumentException on improper attribute hex value");
1269 } catch (IllegalArgumentException e) {
1270 //ignore
1271 }
1272
1273 }
1274
1275 /**
1276 * Inits X500Principal with string, where value is given with special chars
1277 * checks if proper exception is thrown
1278 */
1279 public void testIllegalInputName_11() {
1280 try {
1281 String dn = "CN=#1301";
1282 new X500Principal(dn);
1283 fail("No IllegalArgumentException on improper attribute hex value");
1284 } catch (IllegalArgumentException e) {
1285 //ignore
1286 }
1287
1288 }
1289
1290 /**
1291 * Inits X500Principal with string, where value is given with special chars
1292 * checks if proper exception is thrown
1293 */
1294 public void testIllegalInputName_12() {
1295 try {
1296 String dn = "CN=#13010101";
1297 new X500Principal(dn);
1298 fail("No IllegalArgumentException on improper attribute hex value");
1299 } catch (IllegalArgumentException e) {
1300 }
1301 }
1302
1303 /**
1304 * Inits X500Principal with string, where value is given with special chars
1305 * checks if proper exception is thrown
1306 */
1307 public void testIllegalInputName_13() {
1308 try {
1309 String dn = "CN=# 0";
1310 new X500Principal(dn);
1311 fail("No IllegalArgumentException on improper attribute hex value");
1312 } catch (IllegalArgumentException e) {
1313 }
1314 }
1315
1316 /**
1317 * Inits X500Principal with string, where value is given in hex format, but improper tag
1318 * checks if it is ignored
1319 */
1320 public void testSemiIllegalInputName_14() {
1321 String dn = "CN=#7E0142";
1322 new X500Principal(dn);
1323 }
1324
1325 public void testInitClause() {
1326 try {
1327 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1328 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1329 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1330 mess[3] = 0x12;//length field
1331 new X500Principal(mess);
1332
1333 fail("No IllegalArgumentException on input array with improper length field");
1334 } catch (IllegalArgumentException e) {
1335 }
1336 }
1337
1338 /**
1339 * Inits X500Principal with byte array = null
1340 * checks if proper exception is thrown
1341 */
1342 public void testIllegalInputArray_0() {
1343 try {
1344 byte[] mess = null;
1345 new X500Principal(mess);
1346 fail("No IllegalArgumentException on input array with improper length field");
1347 } catch (IllegalArgumentException e) {
1348 }
1349 }
1350
1351 /**
1352 * Inits X500Principal with byte array with wrong length field
1353 * checks if proper exception is thrown
1354 */
1355 public void testIllegalInputArray() {
1356 try {
1357 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1358 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1359 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1360 mess[3] = 0x12;//length field
1361 new X500Principal(mess);
1362
1363 fail("No IllegalArgumentException on input array with improper length field");
1364 } catch (IllegalArgumentException e) {
1365 }
1366 }
1367
1368 /**
1369 * Inits X500Principal with input stream with wrong length field
1370 * checks if proper exception is thrown
1371 */
1372 public void testIllegalInputArray_is() {
1373 try {
1374 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1375 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1376 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1377 mess[3] = 0x12;//length field
1378 ByteArrayInputStream is = new ByteArrayInputStream(mess);
1379 new X500Principal(is);
1380
1381 fail("No IllegalArgumentException on input array with improper length field");
1382 } catch (IllegalArgumentException e) {
1383 }
1384 }
1385
1386 /**
1387 * Inits X500Principal with byte array with wrong inner Sequence tag field
1388 * checks if proper exception is thrown
1389 */
1390 public void testIllegalInputArray_01() {
1391 try {
1392 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1393 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1394 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1395 mess[4] = 0x12;//inner Sequence tag field
1396 new X500Principal(mess);
1397
1398 fail("No IllegalArgumentException on input array with improper inner Sequence tag field");
1399 } catch (IllegalArgumentException e) {
1400 }
1401 }
1402
1403 /**
1404 * Inits X500Principal with byte array with wrong last byte of OID
1405 * checks if proper exception is thrown
1406 */
1407 public void testIllegalInputArray_02() {
1408 try {
1409 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1410 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1411 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1412 mess[10] = (byte) 0xFE;//last byte of OID
1413 new X500Principal(mess);
1414
1415 fail("No IllegalArgumentException on input array with improper last byte of OID");
1416 } catch (IllegalArgumentException e) {
1417 }
1418 }
1419
1420 /**
1421 * Inits X500Principal with byte array with wrong length of OID
1422 * checks if proper exception is thrown
1423 */
1424 public void testIllegalInputArray_03() {
1425 try {
1426 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1427 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1428 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1429 mess[7] = 2;//length of OID
1430 new X500Principal(mess);
1431
1432 fail("No IllegalArgumentException on input array with improper length of OID");
1433 } catch (IllegalArgumentException e) {
1434 }
1435 }
1436
1437 /**
1438 * Inits X500Principal with byte array with wrong tag of value
1439 * checks if it is ignored
1440 */
1441 public void testSemiIllegalInputArray_04() {
1442 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
1443 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
1444 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1445 mess[11] = (byte) 0x0F;//tag of value
1446 new X500Principal(mess);
1447 }
1448
1449 /**
1450 * Inits X500Principal with byte array with wrong length of value
1451 * checks if proper exception is thrown
1452 */
1453 public void testIllegalInputArray_05() {
1454 try {
1455 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1456 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1457 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1458 mess[12] = 2;//length of value
1459 new X500Principal(mess);
1460
1461 fail("No IllegalArgumentException on input array with improper length of value");
1462 } catch (IllegalArgumentException e) {
1463 }
1464 }
1465
1466 /**
1467 * Inits X500Principal with input stream with wrong length of value
1468 * checks if proper exception is thrown
1469 */
1470 public void testIllegalInputArray_05_is() {
1471 try {
1472 byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1473 0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1474 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1475 mess[12] = 2;//length of value
1476 ByteArrayInputStream is = new ByteArrayInputStream(mess);
1477 new X500Principal(is);
1478
1479 fail("No IllegalArgumentException on input array with improper length of value");
1480 } catch (IllegalArgumentException e) {
1481 }
1482 }
1483
1484 /**
1485 * Inits X500Principal with string
1486 * Calls getName with improper parameter as format
1487 * checks if proper exception is thrown
1488 */
1489 public void testIllegalFormat() {
1490 try {
1491 String dn = "CN=A";
1492 X500Principal principal = new X500Principal(dn);
1493 principal.getName("WRONG FORMAT");
1494 fail("No IllegalArgumentException on improper parameter as format");
1495 } catch (IllegalArgumentException e) {
1496 }
1497 }
1498
1499 /**
1500 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
1501 * Gets encoding
1502 * Inits other X500Principal with the encoding
1503 * gets string in RFC1779 format
1504 * compares with expected value paying attention on sorting order of AVAs
1505 */
1506 public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC1779()
1507 throws Exception {
1508 String dn = "OID.2.16.4.3=B + CN=A";
1509 X500Principal principal = new X500Principal(dn);
1510 byte[] enc = principal.getEncoded();
1511 X500Principal principal2 = new X500Principal(enc);
1512 String s = principal2.getName(X500Principal.RFC1779);
Piotr Jastrzebskic0c7fab2015-04-01 14:34:53 +01001513 assertTrue("OID.2.16.4.3=B + CN=A".equals(s) ||
1514 "CN=A + OID.2.16.4.3=B".equals(s));
Narayan Kamath62542d82013-11-25 15:06:09 +00001515
1516 }
1517
1518 /**
1519 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
1520 * Gets encoding
1521 * Inits other X500Principal with the encoding
1522 * gets string in RFC2253 format
1523 * compares with expected value paying attention on sorting order of AVAs
1524 */
1525 public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC2253()
1526 throws Exception {
1527 String dn = "OID.2.16.4.3=B + CN=A";
1528 X500Principal principal = new X500Principal(dn);
1529 byte[] enc = principal.getEncoded();
1530 X500Principal principal2 = new X500Principal(enc);
1531 String s = principal2.getName(X500Principal.RFC2253);
Piotr Jastrzebskic0c7fab2015-04-01 14:34:53 +01001532 assertTrue("2.16.4.3=#130142+CN=A".equals(s) ||
1533 "CN=A+2.16.4.3=#130142".equals(s));
Narayan Kamath62542d82013-11-25 15:06:09 +00001534
1535 }
1536
1537 /**
1538 * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
1539 * Gets encoding
1540 * Inits other X500Principal with the encoding
1541 * gets string in CANONICAL format
1542 * compares with expected value paying attention on sorting order of AVAs
1543 */
1544 public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_CANONICAL()
1545 throws Exception {
1546 String dn = "OID.2.16.4.3=B + CN=A";
1547 X500Principal principal = new X500Principal(dn);
1548 byte[] enc = principal.getEncoded();
1549 X500Principal principal2 = new X500Principal(enc);
1550 String s = principal2.getName(X500Principal.CANONICAL);
1551 assertEquals("cn=a+2.16.4.3=#130142", s);
1552
1553 }
1554
1555 /**
1556 * Inits X500Principal with byte array, where there are leading and tailing spaces
1557 * gets Name in RFC1779 format
1558 * compares with expected value of name
1559 */
1560 public void testNameSpaceFromEncoding_RFC1779() throws Exception {
1561 byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1562 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1563 X500Principal principal = new X500Principal(mess);
1564 String s = principal.getName(X500Principal.RFC1779);
1565 assertEquals("CN=\" A \"", s);
1566
1567 }
1568
1569 /**
1570 * Inits X500Principal with byte array, where there are leading and tailing spaces
1571 * gets Name in RFC2253 format
1572 * compares with expected value of name
1573 */
1574 public void testNameSpaceFromEncoding_RFC2253() throws Exception {
1575 byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1576 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1577 X500Principal principal = new X500Principal(mess);
1578 String s = principal.getName(X500Principal.RFC2253);
1579 assertEquals("CN=\\ A\\ ", s);
1580
1581 }
1582
1583 /**
1584 * Inits X500Principal with byte array, where there are leading and tailing spaces
1585 * gets Name in CANONICAL format
1586 * compares with expected value of name
1587 */
1588 public void testNameSpaceFromEncoding_CANONICAL() throws Exception {
1589 byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1590 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1591 X500Principal principal = new X500Principal(mess);
1592 String s = principal.getName(X500Principal.CANONICAL);
1593 assertEquals("cn=a", s);
1594
1595 }
1596
1597 /**
1598 * Inits X500Principal with byte array, where there are special characters
1599 * gets Name in RFC1779 format
1600 * compares with expected value of name, checks if the string is in quotes
1601 */
1602 public void testNameSpecialCharsFromEncoding_RFC1779() throws Exception {
1603 byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1604 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1605 X500Principal principal = new X500Principal(mess);
1606 String s = principal.getName(X500Principal.RFC1779);
1607 assertEquals("CN=\";,\"", s);
1608
1609 }
1610
1611 /**
1612 * Inits X500Principal with byte array, where there are special characters
1613 * gets Name in RFC1779 format
1614 * compares with expected value of name, checks if the characters are escaped
1615 */
1616 public void testNameSpecialCharsFromEncoding_RFC2253() throws Exception {
1617 byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1618 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1619 X500Principal principal = new X500Principal(mess);
1620 String s = principal.getName(X500Principal.RFC2253);
1621 assertEquals("CN=\\;\\,", s);
1622
1623 }
1624
1625 /**
1626 * Inits X500Principal with byte array, where there are special characters
1627 * gets Name in CANONICAL format
1628 * compares with expected value of name, checks if the characters are escaped
1629 */
1630 public void testNameSpecialCharsFromEncoding_CANONICAL() throws Exception {
1631 byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1632 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1633 X500Principal principal = new X500Principal(mess);
1634 String s = principal.getName(X500Principal.CANONICAL);
1635 assertEquals("cn=\\;\\,", s);
1636
1637 }
1638
1639 /**
1640 * Inits X500Principal with the string with special characters - \"B
1641 * gets Name in RFC1779 format
1642 * compares with expected value of name - "\B"
1643 */
1644 public void testNameSpecialChars_RFC1779() throws Exception {
1645 String dn = "CN=A,CN=\\\"B";
1646 X500Principal principal = new X500Principal(dn);
1647 String s = principal.getName(X500Principal.RFC1779);
1648 assertEquals("CN=A, CN=\"\\\"B\"", s);
1649
1650 }
1651
1652 /**
1653 * Inits X500Principal with the string with special characters - \"B
1654 * gets Name in RFC2253 format
1655 * compares with expected value of name - "\B"
1656 */
1657 public void testNameSpecialChars_RFC2253() throws Exception {
1658 String dn = "CN=A,CN=\\\"B";
1659 X500Principal principal = new X500Principal(dn);
1660 String s = principal.getName(X500Principal.RFC2253);
1661 assertEquals("CN=A,CN=\\\"B", s);
1662
1663 }
1664
1665 /**
1666 * Inits X500Principal with the string with special characters - \"B
1667 * gets Name in CANONICAL format
1668 * compares with expected value of name - "\b"
1669 */
1670 public void testNameSpecialChars_CANONICAL() throws Exception {
1671 String dn = "CN=A,CN=\\\"B";
1672 X500Principal principal = new X500Principal(dn);
1673 String s = principal.getName(X500Principal.CANONICAL);
1674 assertEquals("cn=a,cn=\\\"b", s);
1675
1676 }
1677
1678 /**
1679 * Inits X500Principal with the string with special characters - \\nB
1680 * gets Name in RFC1779 format
1681 * compares with expected value of name - "\nB"
1682 */
1683 public void testNameSpecialChars_RFC1779_01() throws Exception {
1684 //FIXME see testNameSpecialChars_RFC2253_01
1685 // String dn = "CN=\\\nB";
1686 // X500Principal principal = new X500Principal(dn);
1687 // String s = principal.getName(X500Principal.RFC1779);
1688 // assertEquals("CN=\"\nB\"", s);
1689
1690 }
1691
1692 /**
1693 * Inits X500Principal with the string with special characters - \\nB
1694 * gets Name in RFC2253 format
Piotr Jastrzebskie6ace472015-04-02 14:52:56 +01001695 * compares with expected value of name - \nB
Narayan Kamath62542d82013-11-25 15:06:09 +00001696 */
1697 public void testNameSpecialChars_RFC2253_01() throws Exception {
Piotr Jastrzebskie6ace472015-04-02 14:52:56 +01001698 X500Principal p = new X500Principal("CN=\\\nB");
1699 assertEquals("CN=\nB", p.getName(X500Principal.RFC2253));
Narayan Kamath62542d82013-11-25 15:06:09 +00001700 }
1701
1702 /**
1703 * Inits X500Principal with the string with special characters - \\nB
1704 * gets Name in CANONICAL format
1705 * compares with expected value of name - \\nb
1706 */
1707 public void testNameSpecialChars_CANONICAL_01() throws Exception {
1708 //FIXME testNameSpecialChars_RFC2253_01
1709 // String dn = "CN=\\\nB";
1710 // X500Principal principal = new X500Principal(dn);
1711 // String s = principal.getName(X500Principal.CANONICAL);
1712 // assertEquals("cn=b", s);
1713
1714 }
1715
1716 /**
1717 * Inits X500Principal with the string with special characters - \\B
1718 * gets Name in RFC1779 format
1719 * compares with expected value of name - "\B"
1720 */
1721 public void testNameSpecialChars_RFC1779_02() throws Exception {
1722 String dn = "CN=\\\\B";
1723 X500Principal principal = new X500Principal(dn);
1724 String s = principal.getName(X500Principal.RFC1779);
1725 assertEquals("CN=\"\\\\B\"", s);
1726
1727 }
1728
1729 /**
1730 * Inits X500Principal with the string with special characters - \\B
1731 * gets Name in RFC2253 format
1732 * compares with expected value of name - \\B
1733 */
1734 public void testNameSpecialChars_RFC2253_02() throws Exception {
1735 String dn = "CN=\\\\B";
1736 X500Principal principal = new X500Principal(dn);
1737 String s = principal.getName(X500Principal.RFC2253);
1738 assertEquals("CN=\\\\B", s);
1739
1740 }
1741
1742 /**
1743 * Inits X500Principal with the string with special characters - \\B
1744 * gets Name in CANONICAL format
1745 * compares with expected value of name - \\b
1746 */
1747 public void testNameSpecialChars_CANONICAL_02() throws Exception {
1748 String dn = "CN=\\\\B";
1749 X500Principal principal = new X500Principal(dn);
1750 String s = principal.getName(X500Principal.CANONICAL);
1751 assertEquals("cn=\\\\b", s);
1752
1753 }
1754
1755 /**
1756 * Inits X500Principal with the string with special characters - ABC"DEF"
1757 * gets encoding
1758 * compares with expected encoding
1759 */
1760 public void testNameWithQuotation() throws Exception {
1761 String dn = "CN=\"ABCDEF\"";
1762
1763 X500Principal principal = new X500Principal(dn);
1764 byte[] enc = principal.getEncoded();
1765 assertTrue(Arrays.equals(new byte[] { 0x30, 0x11, 0x31, 0x0F, 0x30,
1766 0x0D, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x41, 0x42,
1767 0x43, 0x44, 0x45, 0x46 }, enc));
1768
1769 }
1770
1771 /**
1772 * Inits X500Principal with the string with special characters - "ABCDEF
1773 * checks if the proper exception is thrown
1774 */
1775 public void testNameWithQuotation_01() throws Exception {
1776 String dn = "CN=\"ABCDEF";
1777 try {
1778 new X500Principal(dn);
1779 fail("No IllegalArgumentException on string with no closing quotations");
1780 } catch (IllegalArgumentException e) {
1781 }
1782 }
1783
1784 /**
1785 * Inits X500Principal with the string with special characters - ABC"D#EF"
1786 * gets encoding
1787 * compares with expected encoding
1788 */
1789 public void testNameWithQuotation_02() throws Exception {
1790 String dn = "CN=\"ABCD#EF\"";
1791 X500Principal principal = new X500Principal(dn);
1792 byte[] enc = principal.getEncoded();
1793 assertTrue(Arrays.equals(new byte[] { 0x30, 0x12, 0x31, 0x10, 0x30,
1794 0x0E, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x07, 0x41, 0x42,
1795 0x43, 0x44, 0x23, 0x45, 0x46 }, enc));
1796 }
1797
1798 /**
1799 * Inits X500Principal with the string with special characters - ABC"DEF"
1800 * Compatibility issue: according RFC 2253 such string is invalid
1801 * but we accept it, not string char is escaped
1802 */
1803 public void testNameWithQuotation_03() throws Exception {
1804 String dn = "CN=ABC\"DEF\"";
1805 X500Principal principal = new X500Principal(dn);
1806 assertEquals("CN=ABC\\\"DEF\\\"", principal
1807 .getName(X500Principal.RFC2253));
1808 }
1809
1810 /**
1811 * Inits X500Principal with the string with special characters - ABC"DEF"
1812 * gets Name in RFC1779 format
1813 * compares with expected value of name - "ABCDEF"
1814 */
1815 public void testNameSpecialChars_RFC1779_03() throws Exception {
1816 String dn = "CN=\"ABCDEF\"";
1817 X500Principal principal = new X500Principal(dn);
1818 String s = principal.getName(X500Principal.RFC1779);
1819 assertEquals("CN=ABCDEF", s);
1820
1821 }
1822
1823 /**
1824 * Inits X500Principal with the string with special characters - ABC"DEF"
1825 * gets Name in RFC2253 format
1826 * compares with expected value of name - ABC"DEF"
1827 */
1828 public void testNameSpecialChars_RFC2253_03() throws Exception {
1829 String dn = "CN=\"ABCDEF\"";
1830 X500Principal principal = new X500Principal(dn);
1831 String s = principal.getName(X500Principal.RFC2253);
1832 assertEquals("CN=ABCDEF", s);
1833
1834 }
1835
1836 /**
1837 * Inits X500Principal with the string with special characters - ABC"DEF"
1838 * gets Name in CANONICAL format
1839 * compares with expected value of name - abc"def"
1840 */
1841 public void testNameSpecialChars_CANONICAL_03() throws Exception {
1842 String dn = "CN=\"ABCDEF\"";
1843 X500Principal principal = new X500Principal(dn);
1844 String s = principal.getName(X500Principal.CANONICAL);
1845 assertEquals("cn=abcdef", s);
1846
1847 }
1848
1849 /**
1850 * Inits X500Principal with the string with special characters - ABC"D#EF"
1851 * gets Name in RFC1779 format
1852 * compares with expected value of name - "ABCD#EF"
1853 */
1854 public void testNameSpecialChars_RFC1779_04() throws Exception {
1855 String dn = "CN=\"ABCD#EF\"";
1856 X500Principal principal = new X500Principal(dn);
1857 String s = principal.getName(X500Principal.RFC1779);
1858 assertEquals("CN=\"ABCD#EF\"", s);
1859
1860 }
1861
1862 /**
1863 * Inits X500Principal with the string with special characters - ABC"D#EF"
1864 * gets Name in RFC1779 format
1865 * compares with expected value of name - ABCD\#EF
1866 */
1867 public void testNameSpecialChars_RFC2253_04() throws Exception {
1868 String dn = "CN=\"ABCD#EF\"";
1869 X500Principal principal = new X500Principal(dn);
1870 String s = principal.getName(X500Principal.RFC2253);
1871 assertEquals("CN=ABCD\\#EF", s);
1872
1873 }
1874
1875 /**
1876 * Inits X500Principal with the string with special characters - ABC"D#EF"
1877 * gets Name in RFC1779 format
1878 * compares with expected value of name - abc"d#ef"
1879 */
1880 public void testNameSpecialChars_CANONICAL_04() throws Exception {
1881 String dn = "CN=\"ABCD#EF\"";
1882 X500Principal principal = new X500Principal(dn);
1883 String s = principal.getName(X500Principal.CANONICAL);
1884 assertEquals("cn=abcd#ef", s);
1885
1886 }
1887
1888 /**
1889 * Inits X500Principal with the string with special characters - X#YZ
1890 * gets Name in RFC1779 format
1891 * compares with expected value of name - "X#YZ"
1892 */
1893 public void testNameSpecialChars_RFC1779_05() {
1894 String dn = "CN=X#YZ";
1895 X500Principal principal = new X500Principal(dn);
1896
1897 String s = principal.getName(X500Principal.RFC1779);
1898 assertEquals("CN=\"X#YZ\"", s);
1899
1900 }
1901
1902 /**
1903 * Inits X500Principal with the string with special characters - X#YZ
1904 * gets Name in RFC2253 format
1905 * compares with expected value of name - X\#YZ
1906 */
1907 public void testNameSpecialChars_RFC2253_05() {
1908 String dn = "CN=X#YZ";
1909 X500Principal principal = new X500Principal(dn);
1910
1911 String s = principal.getName(X500Principal.RFC2253);
1912
1913 assertEquals("CN=X\\#YZ", s);
1914
1915 }
1916
1917 /**
1918 * Inits X500Principal with the string with special characters - X#YZ
1919 * gets Name in CANONICAL format
1920 * compares with expected value of name - x#yz
1921 */
1922 public void testNameSpecialChars_CANONICAL_05() {
1923 String dn = "CN=X#YZ";
1924 X500Principal principal = new X500Principal(dn);
1925
1926 String s = principal.getName(X500Principal.CANONICAL);
1927 assertEquals("cn=x#yz", s);
1928
1929 }
1930
1931 /**
1932 * Inits X500Principal with the string with special characters - CN=\#XYZ
1933 * gets Name in RFC1779 format
1934 * compares with expected value of name - CN="#XYZ"
1935 */
1936 public void testNameSpecialChars_RFC1779_6() throws Exception {
1937 String dn = "CN=\\#XYZ";
1938 X500Principal principal = new X500Principal(dn);
1939 String s = principal.getName(X500Principal.RFC1779);
1940 assertEquals("CN=\"#XYZ\"", s);
1941
1942 }
1943
1944 /**
1945 * Inits X500Principal with the string with special characters - CN=\#XYZ
1946 * gets Name in RFC2253 format
1947 * compares with expected value of name - CN=\#XYZ
1948 */
1949 public void testNameSpecialChars_RFC2253_6() throws Exception {
1950 String dn = "CN=\\#XYZ";
1951 X500Principal principal = new X500Principal(dn);
1952 String s = principal.getName(X500Principal.RFC2253);
1953 assertEquals("CN=\\#XYZ", s);
1954 }
1955
1956 /**
1957 * Inits X500Principal with the string with special characters - CN=\#XYZ
1958 * gets Name in CANONICAL format
1959 * compares with expected value of name - cn=\#xyz
1960 */
1961 public void testNameSpecialChars_CANONICAL_6() throws Exception {
1962 String dn = "CN=\\#XYZ";
1963 X500Principal principal = new X500Principal(dn);
1964 String s = principal.getName(X500Principal.CANONICAL);
1965 assertEquals("cn=\\#xyz", s);
1966 }
1967
1968 /**
1969 * Inits X500Principal with the string with special characters - B\'space'
1970 * gets Name in RFC1779 format
1971 * compares with expected value of name - "B "
1972 */
1973 public void testNameSpaces_RFC1779() throws Exception {
1974 String dn = "CN=A,CN=B\\ ";
1975 X500Principal principal = new X500Principal(dn);
1976 String s = principal.getName(X500Principal.RFC1779);
1977 assertEquals("CN=A, CN=\"B \"", s);
1978
1979 }
1980
1981 /**
1982 * Inits X500Principal with the string with special characters - B\'space'
1983 * gets Name in RFC2253 format
1984 * compares with expected value of name - B\'space'
1985 */
1986 public void testNameSpaces_RFC2253() throws Exception {
1987 String dn = "CN=A,CN=B\\ ";
1988 X500Principal principal = new X500Principal(dn);
1989 String s = principal.getName(X500Principal.RFC2253);
1990 assertEquals("CN=A,CN=B\\ ", s);
1991
1992 }
1993
1994 /**
1995 * Inits X500Principal with the string with special characters - B\'space'
1996 * gets Name in CANONICAL format
1997 * compares with expected value of name - B\
1998 */
1999 public void testNameSpaces_CANONICAL() throws Exception {
2000 String dn = "CN=A,CN=B\\ ";
2001 X500Principal principal = new X500Principal(dn);
2002 String s = principal.getName(X500Principal.CANONICAL);
2003 assertEquals("cn=a,cn=b", s);
2004
2005 }
2006
2007 /**
2008 * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2009 * gets Name in RFC1779 format
2010 * compares with expected value of name - "B A"
2011 */
2012 public void testNameSpaces_RFC1779_01() throws Exception {
2013 String dn = "CN=\"B A\"";
2014 X500Principal principal = new X500Principal(dn);
2015 String s = principal.getName(X500Principal.RFC1779);
Kenny Root155ee0c2014-01-13 14:51:25 -08002016 assertEquals("CN=\"B A\"", s);
Narayan Kamath62542d82013-11-25 15:06:09 +00002017
2018 }
2019
2020 /**
2021 * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2022 * gets Name in 2253 format
2023 * compares with expected value of name - B'space''space''space'A
2024 */
2025 public void testNameSpaces_RFC2253_01() throws Exception {
2026 String dn = "CN=\"B A\"";
2027 X500Principal principal = new X500Principal(dn);
2028 String s = principal.getName(X500Principal.RFC2253);
2029 assertEquals("CN=B A", s);
2030
2031 }
2032
2033 /**
2034 * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2035 * gets Name in CANONICAL format
2036 * compares with expected value of name - b'space'a
2037 */
2038 public void testNameSpaces_CANONICAL_01() throws Exception {
2039 String dn = "CN=\"B A\"";
2040 X500Principal principal = new X500Principal(dn);
2041 String s = principal.getName(X500Principal.CANONICAL);
2042 assertEquals("cn=b a", s);
2043
2044 }
2045
2046 /**
2047 * Inits X500Principal with the string with special characters - \\'space''space'B
2048 * gets Name in RFC1779 format
2049 * compares with expected value of name - " B"
2050 */
2051 public void testNameSpaces_RFC1779_02() throws Exception {
2052 String dn = "CN=\\ B";
2053 X500Principal principal = new X500Principal(dn);
2054 String s = principal.getName(X500Principal.RFC1779);
2055 assertEquals("CN=\" B\"", s);
2056
2057 }
2058
2059 /**
2060 * Inits X500Principal with the string with special characters - \\'space''space'B
2061 * gets Name in RFC1779 format
2062 * compares with expected value of name - \'space''space'B
2063 */
2064 public void testNameSpaces_RFC2253_02() throws Exception {
2065 String dn = "CN=\\ B";
2066 X500Principal principal = new X500Principal(dn);
2067 String s = principal.getName(X500Principal.RFC2253);
Kenny Root155ee0c2014-01-13 14:51:25 -08002068 assertEquals("CN=\\ \\ B", s);
Narayan Kamath62542d82013-11-25 15:06:09 +00002069
2070 }
2071
2072 /**
2073 * Inits X500Principal with the string with special characters - \\'space''space'B
2074 * gets Name in CANONICAL format
2075 * compares with expected value of name - \'space''space'b
2076 */
2077 public void testNameSpaces_CANONICAL_02() throws Exception {
2078 String dn = "CN=\\ B";
2079 X500Principal principal = new X500Principal(dn);
2080 String s = principal.getName(X500Principal.CANONICAL);
2081 assertEquals("cn=b", s);
2082
2083 }
2084
2085 /**
2086 * Inits X500Principal with the string with special characters - ""B
2087 * checks if the proper exception is thrown
2088 */
2089 public void testNameQu() throws Exception {
2090 String dn = "CN=\"\"B";
2091 try {
2092 new X500Principal(dn);
2093 fail("No IllegalArgumentException on improper string");
2094 } catch (IllegalArgumentException e) {
2095 }
2096 }
2097
2098 /**
2099 * Inits X500Principal with the string with special characters - "A\"B"
2100 * gets Name in RFC1779 format
2101 * compares with expected value of name - "A\"B"
2102 */
2103 public void testNameQu_RFC1779_2() throws Exception {
2104 String dn = "CN=\"A\\\"B\"";
2105 X500Principal principal = new X500Principal(dn);
2106 String s = principal.getName(X500Principal.RFC1779);
2107 assertEquals("CN=\"A\\\"B\"", s);
2108 }
2109
2110 /**
2111 * Inits X500Principal with the string with special characters - "A\"B"
2112 * gets Name in RFC2253 format
2113 * compares with expected value of name - A\"B
2114 */
2115 public void testNameQu_RFC2253_2() throws Exception {
2116 String dn = "CN=\"A\\\"B\"";
2117 X500Principal principal = new X500Principal(dn);
2118 String s = principal.getName(X500Principal.RFC2253);
2119 assertEquals("CN=A\\\"B", s);
2120 }
2121
2122 /**
2123 * Inits X500Principal with the string with special characters - "A\"B"
2124 * gets Name in CANONICAL format
2125 * compares with expected value of name - a\"b
2126 */
2127 public void testNameQu_CANONICAL_2() throws Exception {
2128 String dn = "CN=\"A\\\"B\"";
2129 X500Principal principal = new X500Principal(dn);
2130 String s = principal.getName(X500Principal.CANONICAL);
2131 assertEquals("cn=a\\\"b", s);
2132
2133 }
2134
2135 /**
2136 * Inits X500Principal with the string with special characters - "A\""
2137 * gets Name in RFC1779 format
2138 * compares with expected value of name - "A\""
2139 */
2140 public void testNameQu_RFC1779_3() throws Exception {
2141 String dn = "CN=\"A\\\"\"";
2142 X500Principal principal = new X500Principal(dn);
2143 String s = principal.getName(X500Principal.RFC1779);
2144 assertEquals("CN=\"A\\\"\"", s);
2145 }
2146
2147 /**
2148 * Inits X500Principal with the string with special characters - "A\""
2149 * gets Name in RFC2253 format
2150 * compares with expected value of name - A\"
2151 */
2152 public void testNameQu_RFC2253_3() throws Exception {
2153 String dn = "CN=\"A\\\"\"";
2154 X500Principal principal = new X500Principal(dn);
2155 String s = principal.getName(X500Principal.RFC2253);
2156 assertEquals("CN=A\\\"", s);
2157 }
2158
2159 /**
2160 * Inits X500Principal with the string with special characters - "A\""
2161 * gets Name in CANONICAL format
2162 * compares with expected value of name - A\"
2163 */
2164 public void testNameQu_CANONICAL_3() throws Exception {
2165 String dn = "CN=\"A\\\"\"";
2166 X500Principal principal = new X500Principal(dn);
2167 String s = principal.getName(X500Principal.CANONICAL);
2168 assertEquals("cn=a\\\"", s);
2169
2170 }
2171
2172 /**
2173 * Inits X500Principal with the string with special characters - "A\", C=B"
2174 * gets Name in RFC1779 format
2175 * compares with expected value of name - "A\", C=B"
2176 */
2177 public void testNameQu_4() throws Exception {
2178 String dn = "CN=\"A\\\", C=B\"";
2179 X500Principal principal = new X500Principal(dn);
2180 String s = principal.getName(X500Principal.RFC1779);
2181 assertEquals("CN=\"A\\\", C=B\"", s);
2182
2183 }
2184
2185 /**
2186 * Inits X500Principal with the string with special characters - CN="A\\", C=B
2187 * gets Name in RFC1779 format
2188 * compares with expected value of name - CN="A\\", C=B
2189 */
2190 public void testNameQu_5() throws Exception {
2191 String dn = "CN=\"A\\\\\", C=B";
2192 X500Principal principal = new X500Principal(dn);
2193 String s = principal.getName(X500Principal.RFC1779);
2194 assertEquals("CN=\"A\\\\\", C=B", s);
2195
2196 }
2197
2198 /**
2199 * Inits X500Principal with the string with special characters - CN=A\nB
2200 * gets Name in RFC1779 format
2201 * compares with expected value of name - CN="A\nB"
2202 */
2203 public void testNameCR_RFC1779() throws Exception {
2204 String dn = "CN=A\nB";
2205 X500Principal principal = new X500Principal(dn);
2206 String s = principal.getName(X500Principal.RFC1779);
Piotr Jastrzebskif96e7e72015-04-02 08:35:06 +01002207 assertEquals("CN=\"A\nB\"", s);
Narayan Kamath62542d82013-11-25 15:06:09 +00002208 }
2209
2210 /**
2211 * Inits X500Principal with the string with special characters - CN=A\nB
2212 * gets Name in RFC2253 format
2213 * compares with expected value of name - CN=A\nB
2214 */
2215 public void testNameCR_RFC2253() throws Exception {
2216 String dn = "CN=A\nB";
2217 X500Principal principal = new X500Principal(dn);
2218 String s = principal.getName(X500Principal.RFC2253);
2219 assertEquals("CN=A\nB", s);
2220 }
2221
2222 /**
2223 * Inits X500Principal with the string with special characters - CN=A\nB
2224 * gets Name in CANONICAL format
2225 * compares with expected value of name - cn=a\nb
2226 */
2227 public void testNameCR_CANONICAL() throws Exception {
2228 String dn = "CN=A\nB";
2229 X500Principal principal = new X500Principal(dn);
2230 String s = principal.getName(X500Principal.CANONICAL);
2231 assertEquals("cn=a\nb", s);
2232 }
2233
2234 public static final String[] RFC2253_SPECIAL = new String[] { ",", "=",
2235 "+", "<", ">", "#", ";" };
2236
2237 public void testValidDN() throws Exception {
2238
2239 TestList list = new TestList();
2240
2241 list.add("", "", "", "", new byte[] { 0x30, 0x00 }); // empty RDN sequence
2242
2243 // sequence of RDN: RDN *("," RDN)
2244 list.add("CN=A,C=B", "CN=A,C=B", "CN=A, C=B", "cn=a,c=b");
2245 list.add("C=B,CN=A", "C=B,CN=A", "C=B, CN=A", "c=b,cn=a");
2246 list.add("CN=A,CN=A", "CN=A,CN=A", "CN=A, CN=A", "cn=a,cn=a"); // duplicate RDNs
2247
2248 // sequence of RDN: RFC 1779 compatibility
2249 list.add("CN=A , C=B", "CN=A,C=B", "CN=A, C=B");
2250 list.add("CN=A , C=B", "CN=A,C=B", "CN=A, C=B");
2251 list.add("CN=A;C=B", "CN=A,C=B", "CN=A, C=B");
2252 list.add("CN=A ; C=B", "CN=A,C=B", "CN=A, C=B");
2253 //FIXME list.add("CN=A\r,\rC=B", "CN=A,C=B"); // <CR> & comma => comma
2254 list.add(" CN=A,C=B ", "CN=A,C=B", "CN=A, C=B"); // spaces at beg&end
2255 list.add(" CN=A,C=\"B\" ", "CN=A,C=B", "CN=A, C=B"); // spaces at beg&end
2256
2257 // set of ATAV: ATAV *("+" ATAV)
2258 list.add("CN=A+ST=CA", "CN=A+ST=CA", "CN=A + ST=CA", "cn=a+st=ca");
2259 list.add("CN=A+CN=A", "CN=A+CN=A", "CN=A + CN=A", "cn=a+cn=a"); // duplicate AT
2260 list
2261 .add("2.5.4.3=A+2.5.4.3=A", "CN=A+CN=A", "CN=A + CN=A",
2262 "cn=a+cn=a"); // duplicate AT
2263
2264 // set of ATAV: RFC 1779 compatibility
2265 list.add("CN=A + ST=CA", "CN=A+ST=CA", "CN=A + ST=CA");
2266 list.add("CN=A + ST=CA", "CN=A+ST=CA", "CN=A + ST=CA");
2267 //FIXME list.add("CN=A\r+\rST=CA", "CN=A+ST=CA"); // <CR> & '+' => '+'
2268
2269 // ATAV = AttributeType "=" AttributeValue
2270 list.add("CN=A", "CN=A", "CN=A");
2271 list.add("cn=A", "CN=A", "CN=A"); // AT case insensitive
2272 list.add("cN=A", "CN=A", "CN=A"); // AT case insensitive
2273 list.add("cn=a", "CN=a", "CN=a"); // AT case insensitive
2274
2275 // ATAV : RFC 1779 compatibility
2276 list.add("CN = A", "CN=A", "CN=A");
2277 list.add("CN = A", "CN=A", "CN=A");
2278 // FIXME list.add("CN\r=\rA", "CN=A"); // <CR> & '=' => '='
2279
2280 // AttributeType = <name string> | <OID>
2281 // testing OID case : OID => <name string>
2282 // tested all OIDs from RFC 2253 (2.3) and RFC 1779 (Table 1)
2283
2284 // different variants of 2.5.4.3 (CN) OID
2285 list.add("OID.2.5.4.3=A", "CN=A", "CN=A");
2286 list.add("oid.2.5.4.3=A", "CN=A", "CN=A");
2287 list.add("2.5.4.3=A", "CN=A", "CN=A");
2288 list.add("02.5.4.3=A", "CN=A", "CN=A"); // first: 02 => 2
2289 list.add("2.5.4.0003=A", "CN=A", "CN=A"); // last: 0003 => 3
2290
2291 // the rest of OIDs
2292 list.add("2.5.4.7=A", "L=A", "L=A", "l=a");
2293 list.add("2.5.4.8=A", "ST=A", "ST=A", "st=a");
2294 list.add("2.5.4.10=A", "O=A", "O=A", "o=a");
2295 list.add("2.5.4.11=A", "OU=A", "OU=A", "ou=a");
2296 list.add("2.5.4.6=A", "C=A", "C=A", "c=a");
2297 list.add("2.5.4.9=A", "STREET=A", "STREET=A", "street=a");
2298 list.add("0.9.2342.19200300.100.1.25=A", "DC=A",
Kenny Root155ee0c2014-01-13 14:51:25 -08002299 "OID.0.9.2342.19200300.100.1.25=A", "dc=#160141");
Narayan Kamath62542d82013-11-25 15:06:09 +00002300 list.add("0.9.2342.19200300.100.1.1=A", "UID=A",
2301 "OID.0.9.2342.19200300.100.1.1=A", "uid=a");
2302
2303 // attribute types from RFC 2459 (see Appendix A)
2304 // keywords are from the API spec
2305 list.add("T=A", "2.5.4.12=#130141", "OID.2.5.4.12=A",
2306 "2.5.4.12=#130141");
2307 list.add("DNQ=A", "2.5.4.46=#130141", "OID.2.5.4.46=A",
2308 "2.5.4.46=#130141");
2309 list.add("DNQUALIFIER=A", "2.5.4.46=#130141", "OID.2.5.4.46=A",
2310 "2.5.4.46=#130141");
2311 list.add("SURNAME=A", "2.5.4.4=#130141", "OID.2.5.4.4=A",
2312 "2.5.4.4=#130141");
2313 list.add("GIVENNAME=A", "2.5.4.42=#130141", "OID.2.5.4.42=A",
2314 "2.5.4.42=#130141");
2315 list.add("INITIALS=A", "2.5.4.43=#130141", "OID.2.5.4.43=A",
2316 "2.5.4.43=#130141");
2317 list.add("GENERATION=A", "2.5.4.44=#130141", "OID.2.5.4.44=A",
2318 "2.5.4.44=#130141");
Kenny Root155ee0c2014-01-13 14:51:25 -08002319 list.add("EMAILADDRESS=A", "1.2.840.113549.1.9.1=#160141",
2320 "OID.1.2.840.113549.1.9.1=A", "1.2.840.113549.1.9.1=#160141",
Narayan Kamath62542d82013-11-25 15:06:09 +00002321 null, (byte) 0x05); //FIXME bug???
2322 list.add("SERIALNUMBER=A", "2.5.4.5=#130141", "OID.2.5.4.5=A",
2323 "2.5.4.5=#130141");
2324
2325 // AttributeValue => BER encoding (if OID in dotted-decimal form)
2326 // see RFC 2253 (2.4)
2327 list.add("OID.2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2328 list.add("oid.2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2329 list.add("2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2330 list.add("1.1=A", "1.1=#130141", "OID.1.1=A");
2331
2332 //
2333 // AttributeValue first alternative : *( stringchar / pair )
2334 // testing pair characters.
2335 //
2336 // Note: for RFC1779 quoted string is returned (unspecified)
2337 //
2338 list.add("CN=", "CN=", "CN="); // zero string chars
2339 list.add("CN= ", "CN=", "CN="); // zero string chars
2340 list.add("CN=A+ST=", "CN=A+ST=", "CN=A + ST="); // zero string chars
2341 list.add("CN=+ST=A", "CN=+ST=A", "CN= + ST=A"); // empty value for 1 RDN
2342 list.add("CN=A+ST= ", "CN=A+ST=", "CN=A + ST="); // empty value for 1 RDN
2343 list.add("CN=+ST=", "CN=+ST=", "CN= + ST="); // empty value for both RDNs
2344 list.add("CN=,ST=B", "CN=,ST=B", "CN=, ST=B"); // empty value for 1 RDN
2345 list.add("CN=,ST=", "CN=,ST=", "CN=, ST="); // empty value for both RDNs
2346 list.add("CN=;ST=B", "CN=,ST=B", "CN=, ST=B"); // empty value for 1 RDN
2347 list.add("CN=;ST=", "CN=,ST=", "CN=, ST="); // empty value for both RDNs
2348 for (String element : RFC2253_SPECIAL) {
2349 // \special
2350 list.add("CN=\\" + element,
2351 "CN=\\" + element, "CN=\"" + element
2352 + "\"");
2353
2354 // A + \special + B
2355 list.add("CN=A\\" + element + "B", "CN=A\\"
2356 + element + "B", "CN=\"A" + element
2357 + "B\"");
2358 }
2359
2360 // pair = \"
2361 list.add("CN=\\\"", "CN=\\\"", "CN=\"\\\"\"", null, (byte) 0x02);
2362 list.add("CN=\\\"A", "CN=\\\"A", "CN=\"\\\"A\"", null, (byte) 0x02);
2363 list.add("CN=\\\",C=\\\"", "CN=\\\",C=\\\"", "CN=\"\\\"\", C=\"\\\"\"",
2364 null, (byte) 0x02); // 2 RDN
2365 list.add("CN=A\\\"B", "CN=A\\\"B", "CN=\"A\\\"B\"", null, (byte) 0x02); // A\"B
2366 list.add("CN=A ST=B", "CN=A ST\\=B", "CN=\"A ST=B\""); // no RDN separator
2367
2368 // pair = \space
2369 list.add("CN=\\ ", "CN=\\ ", "CN=\" \"", "cn=");
2370
2371 // pair = \hexpair
2372 list.add("CN=\\41", "CN=A", "CN=A"); // 0x41=='A'
2373 list.add("CN=\\41\\2C", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2C=','
2374 list.add("CN=\\41\\2c", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2c=','
2375 list.add("CN=\\D0\\AF", "CN=" + ((char) 1071), "CN=" + ((char) 1071),
2376 new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2377 0x55, 0x04, 0x03,
2378 // UTF8 String
2379 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // 0xD0AF == the last letter(capital) of Russian alphabet
2380 list.add("CN=\\D0\\AFA\\41", "CN=" + ((char) 1071) + "AA", "CN="
2381 + ((char) 1071) + "AA", new byte[] { 0x30, 0x0F, 0x31, 0x0D,
2382 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
2383 // UTF8 String
2384 0x0C, 0x04, (byte) 0xD0, (byte) 0xAF, 0x41, 0x41 }); // 0xD0AF == the last letter(capital) of Russian alphabet
2385 // UTF-8(0xE090AF) is non-shortest form of UTF-8(0xD0AF)
2386 //FIXME list.add("CN=\\E0\\90\\AF", "CN=" + ((char) 1071), "CN="
2387 // + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2388 // 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2389 // // UTF8 String
2390 // 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF });
2391 // UTF-8(0xF08090AF) is non-shortest form of UTF-8(0xD0AF)
2392 //FIXME list.add("CN=\\F0\\80\\90\\AF", "CN=" + ((char) 1071), "CN="
2393 // + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2394 // 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2395 // // UTF8 String
2396 // 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF });
2397 //FIXME list.add("CN=\\D0", "CN=" + ((char) 65533), "CN=" + ((char) 65533),
2398 // new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2399 // 0x55, 0x04, 0x03,
2400 // // UTF8 String
2401 // 0x0C, 0x01, 0x3F }); // 0xD0 is not correct UTF8 char => '?'
2402 list.add("CN=\\41+ST=A", "CN=A+ST=A", "CN=A + ST=A"); // 0x41=='A'
2403 list.add("CN=\\41\\2C+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2C=','
2404 list.add("CN=\\41\\2c+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2c=','
2405
2406 // stringchar '=' or not leading '#'
2407 //FIXME RFC 2253 grammar violation: '=' and '#' is a special char
2408 list.add("CN==", "CN=\\=", "CN=\"=\"");
2409 list.add("CN=A=", "CN=A\\=", "CN=\"A=\"");
2410 list.add("CN=A#", "CN=A\\#", "CN=\"A#\"");
2411
2412 // not leading or trailing spaces
2413 list.add("CN=A B", "CN=A B", "CN=A B", "cn=a b");
2414 list.add("CN=A\\ B", "CN=A B", "CN=A B", "cn=a b");
2415 list.add("CN=A \\,B", "CN=A \\,B", "CN=\"A ,B\"", "cn=a \\,b");
2416
2417 //not alphabet chars
2418 list.add("CN=$", "CN=$", "CN=$", new byte[] { 0x30, 0x0C, 0x31, 0x0A,
2419 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2420 //UTF-8 String: "$"
2421 0x0C, 0x01, 0x24 });
2422 list.add("CN=(", "CN=(", "CN=(", new byte[] { 0x30, 0x0C, 0x31, 0x0A,
2423 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2424 //PrintableString: "("
2425 0x13, 0x01, 0x28 });
2426
2427 //
2428 //
2429 // AttributeValue second alternative : "#" hexstring
2430 //
2431 //
2432 list.add("CN=#130141", "CN=A", "CN=A", "cn=a"); // ASN1 Printable hex string = 'A'
Kenny Root155ee0c2014-01-13 14:51:25 -08002433 list.add("CN=#140141", "CN=A", "CN=A", "cn=a", new byte[] { 0x30,
Narayan Kamath62542d82013-11-25 15:06:09 +00002434 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2435 0x14, 0x01, 0x41 }); // ASN1 Teletex hex string = 'A'
2436
2437 list.add("CN=#010100", "CN=#010100", "CN=#010100", "cn=#010100"); // ASN1 Boolean = FALSE
2438 list.add("CN=#0101fF", "CN=#0101ff", "CN=#0101FF", "cn=#0101ff"); // ASN1 Boolean = TRUE
2439 //FIXME list.add("CN=#3000", "CN=#3000", "CN=#3000"); // ASN1 Sequence
2440 //FIXME list.add("CN=#0500", "CN=A", "CN=A"); // ASN1 Null
2441 list.add("CN= #0101fF", "CN=#0101ff", "CN=#0101FF", // space at beginning
2442 new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2443 0x55, 0x04, 0x03, 0x01, 0x01, (byte) 0xFF } // ASN.1 Boolean = TRUE
2444 );
2445 list.add("CN= #0101fF+ST=A", "CN=#0101ff+ST=A", "CN=#0101FF + ST=A",
2446 "cn=#0101ff+st=a"); //space
2447 list.add("CN= #0101fF ", "CN=#0101ff", "CN=#0101FF", // space at the end
2448 new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2449 0x55, 0x04, 0x03, 0x01, 0x01, (byte) 0xFF } // ASN.1 Boolean = TRUE
2450 , (byte) 0x00);
2451
2452 //FIXME unspecified output for RFC1779
2453 //FIXME list.add("CN=#1C0141", "CN=A", "CN=A"); // ASN1 Universal hex string = 'A'
2454 //FIXME list.add("CN=#1E0141", "CN=A", "CN=A"); // ASN1 Bmp hex string = 'A'
2455
2456 //
2457 // AttributeValue third alternative : " *( quotechar / pair ) "
2458 // quotechar = <any character except '\' or '"' >
2459 //
2460 // Note:
2461 // RFC2253: passed quoted AV string is unquoted, special chars are escaped
2462 // RFC1779: escaped quoted chars are unescaped
2463 //
2464 list.add("CN=\"\"", "CN=", "CN="); // empty quoted string
2465 list.add("CN=\"A\"", "CN=A", "CN=A"); // "A"
2466 for (String element : RFC2253_SPECIAL) {
2467 // "special" => \special
2468 list.add("CN=\"" + element + "\"", "CN=\\"
2469 + element, "CN=\"" + element + "\"");
2470
2471 // "A + special + B" => A + \special + B
2472 list.add("CN=\"A" + element + "B\"", "CN=A\\"
2473 + element + "B", "CN=\"A" + element
2474 + "B\"");
2475 }
2476 for (String element : RFC2253_SPECIAL) {
2477 // "\special" => \special
2478 list.add("CN=\"\\" + element + "\"", "CN=\\"
2479 + element, "CN=\"" + element + "\"");
2480
2481 // "A + \special + B" => A + \special + B
2482 list.add("CN=\"A\\" + element + "B\"", "CN=A\\"
2483 + element + "B", "CN=\"A" + element
2484 + "B\"");
2485 }
2486 list.add("CN=\"\\\"\"", "CN=\\\"", "CN=\"\\\"\"", null, (byte) 0x02); // "\""
2487 list.add("CN=\"A\\\"B\"", "CN=A\\\"B", "CN=\"A\\\"B\"", null,
2488 (byte) 0x02); // "A\"B"
2489
2490 // pair = \hexpair (test cases are the same as for the first alternative)
2491 list.add("CN=\"\\41\"", "CN=A", "CN=A"); // 0x41=='A'
2492 list.add("CN=\"\\41\\2C\"", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2C=','
2493 list.add("CN=\"\\41\\2c\"", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2c=','
2494 list.add("CN=\"\\D0\\AF\"", "CN=" + ((char) 1071), "CN="
2495 + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2496 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2497 // UTF8 String
2498 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // 0xD0AF == the last letter(capital) of Russian alphabet
2499 list.add("CN=\"\\D0\\AFA\\41\"", "CN=" + ((char) 1071) + "AA", "CN="
2500 + ((char) 1071) + "AA", new byte[] { 0x30, 0x0F, 0x31, 0x0D,
2501 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
2502 // UTF8 String
2503 0x0C, 0x04, (byte) 0xD0, (byte) 0xAF, 0x41, 0x41 }); // 0xD0AF == the last letter(capital) of Russian alphabet
2504 list.add("CN=\"\\E0\\90\\AF\"", "CN=" + ((char) 1071), "CN="
2505 + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2506 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2507 // UTF8 String
2508 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // UTF8(0xE090AF that is not quite correct)== UTF8(0xD0AF) == the last letter(capital) of Russian alphabet
2509 list.add("CN=\"\\F0\\80\\90\\AF\"", "CN=" + ((char) 1071), "CN="
2510 + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2511 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2512 // UTF8 String
2513 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // UTF8(0xF08090AF that is not quite correct)== UTF8(0xD0AF) == the last letter(capital) of Russian alphabet
2514
2515 list.add("CN=\"\\41\"+ST=A", "CN=A+ST=A", "CN=A + ST=A"); // 0x41=='A'
2516 list.add("CN=\"\\41\\2C\"+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2C=','
2517 list.add("CN=\"\\41\\2c\"+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2c=','
2518
2519 // AttributeValue third alternative : RFC 1779 compatibility
2520 //FIXME list.add("CN=\"\r\"", "CN=\"\r\""); // "<CR>"
2521 //FIXME list.add("CN=\"\\\r\"", "CN=\"\\\r\""); // "\<CR>"
2522
2523 // AttributeValue : RFC 1779 compatibility
2524 list.add("CN= A ", "CN=A", "CN=A", "cn=a"); // leading & trailing spaces
Kenny Root155ee0c2014-01-13 14:51:25 -08002525 list.add("CN=\\ A ", "CN=\\ \\ A", "CN=\" A\"", "cn=a", null,
Narayan Kamath62542d82013-11-25 15:06:09 +00002526 (byte) 0x01); // escaped leading space
Kenny Root155ee0c2014-01-13 14:51:25 -08002527 list.add("CN= A \\ ", "CN=A\\ \\ ", "CN=\"A \"", "cn=a", null,
Narayan Kamath62542d82013-11-25 15:06:09 +00002528 (byte) 0x01); // escaped trailing space
2529
2530 list.add("CN= \"A\" ", "CN=A", "CN=A", "cn=a"); // leading & trailing spaces
2531
2532 StringBuffer errorMsg = new StringBuffer();
2533 for (int i = 0; i < list.size(); i++) {
2534
2535 Object[] obj = list.get(i);
2536
2537 String dn = (String) obj[0];
2538 String rfc2253 = (String) obj[1];
2539 String rfc1779 = (String) obj[2];
2540 String canonical = (String) obj[3];
2541 byte[] encoded = (byte[]) obj[4];
2542 byte mask = ((byte[]) obj[5])[0];
2543
2544 try {
2545 X500Principal p = new X500Principal(dn);
2546 if (!rfc2253.equals(p.getName(X500Principal.RFC2253))) {
2547 if (!testing || ((mask & 0x01) == 0)) {
2548
Kenny Root155ee0c2014-01-13 14:51:25 -08002549 errorMsg.append("\nRFC2253: " + i);
2550 errorMsg.append(" \tparm: '" + dn + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002551 errorMsg.append("\t\texpected: '" + rfc2253 + "'");
2552 errorMsg.append("\treturned: '"
Kenny Root155ee0c2014-01-13 14:51:25 -08002553 + p.getName(X500Principal.RFC2253) + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002554 }
2555 }
2556
2557 if (!rfc1779.equals(p.getName(X500Principal.RFC1779))) {
2558 if (!testing || ((mask & 0x02) == 0)) {
2559
Kenny Root155ee0c2014-01-13 14:51:25 -08002560 errorMsg.append("\nRFC1779: " + i);
2561 errorMsg.append(" \tparm: '" + dn + "'");
2562 errorMsg.append("\t\texpected: '" + rfc1779 + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002563 errorMsg.append("\treturned: '"
Kenny Root155ee0c2014-01-13 14:51:25 -08002564 + p.getName(X500Principal.RFC1779) + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002565 }
2566 }
2567
2568 if (canonical != null) {
2569 if (!canonical.equals(p.getName(X500Principal.CANONICAL))) {
2570 if (!testing || ((mask & 0x04) == 0)) {
2571
Kenny Root155ee0c2014-01-13 14:51:25 -08002572 errorMsg.append("\nCANONICAL: " + i);
Narayan Kamath62542d82013-11-25 15:06:09 +00002573 errorMsg.append("\tparm: '" + dn + "'");
Kenny Root155ee0c2014-01-13 14:51:25 -08002574 errorMsg.append("\t\texpected: '" + canonical + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002575 errorMsg.append("\treturned: '"
Kenny Root155ee0c2014-01-13 14:51:25 -08002576 + p.getName(X500Principal.CANONICAL) + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002577 }
2578 }
2579 }
2580
2581 if (encoded != null) {
2582 if (!Arrays.equals(encoded, p.getEncoded())) {
2583 if (!testing || ((mask & 0x08) == 0)) {
2584
Kenny Root155ee0c2014-01-13 14:51:25 -08002585 errorMsg.append("\nUnexpected encoding for: " + i
2586 + ", dn= '" + dn + "'");
Narayan Kamath62542d82013-11-25 15:06:09 +00002587
2588 System.out.println("\nI " + i);
2589 byte[] enc = p.getEncoded();
2590 for (byte element : enc) {
2591 System.out.print(", 0x"
2592 + Integer.toHexString(element));
2593 }
2594 }
2595 }
2596 }
2597 } catch (IllegalArgumentException e) {
Kenny Root155ee0c2014-01-13 14:51:25 -08002598 errorMsg.append("\nIllegalArgumentException: " + i);
Narayan Kamath62542d82013-11-25 15:06:09 +00002599 errorMsg.append("\tparm: '" + dn + "'");
Kenny Root155ee0c2014-01-13 14:51:25 -08002600 } catch (Exception e) {
2601 errorMsg.append("\nException: " + i);
2602 errorMsg.append("\tparm: '" + dn + "'");
2603 errorMsg.append("\texcep: " + e.getClass().getName());
Narayan Kamath62542d82013-11-25 15:06:09 +00002604 }
2605 }
2606
2607 if (errorMsg.length() != 0) {
2608 fail(errorMsg.toString());
2609 }
2610
2611 }
2612
2613 public void testInvalidDN() {
2614 String[] illegalDN = new String[] {
2615 // RDN
2616 //FIXME " ", // space only
2617 "CN", // attribute type only
2618 "CN=A;", // RFC 1779: BNF allows this, but ...
2619 "CN=A,", // RFC 1779: BNF allows this, but ...
2620 ",CN=A", // no AttributeType for first RDN
2621 "CN=,A", // no AttributeType for second RDN
2622 "CN=A+", // no AttributeTypeAndValue for second RDN
2623 "CN=#130141 ST=B", // no RDN separator
2624
2625 // AttributeType = <name string> | <OID>
2626 "AAA=A", // no such <name string>
2627 "1..1=A", // wrong OID
2628 ".1.1=A", // wrong OID
2629 "11=A", // wrong OID
2630 "1=A", // wrong OID
2631 "AID.1.1=A", // wrong OID
2632 "1.50=A", // wrong OID
2633 "5.1.0=A", // wrong OID
2634 "2.-5.4.3=A", // wrong OID
2635 "2.5.-4.3=A", // wrong OID
2636 "2.5.4-.3=A", // wrong OID
2637 //FIXME "2.5.4.-3=A", // wrong OID
2638
2639 // AttributeValue first alternative : *( stringchar / pair )
2640 "CN=,", // stringchar = ','
2641 //FIXME "CN==",
2642 "CN=+", // stringchar = '+'
2643 //FIXME "CN=<", // stringchar = '<'
2644 //FIXME "CN=>", // stringchar = '>'
2645 "CN=#", // stringchar = '#'
2646 //FIXME "CN=Z#", // stringchar = '#'
2647 "CN=;", // stringchar = ';'
2648 "CN=\"", // stringchar = "
2649 //FIXME "CN=A\"B", // stringchar = "
2650 "CN=\\", // stringchar = \
2651 "CN=A\\", // stringchar = \
2652 "CN=A\\B", // stringchar = \
2653 "CN=\\z", // invalid pair = \z
2654 "CN=\\4", // invalid pair = \4
2655 "CN=\\4Z", // invalid pair = \4Z
2656 "CN=\\4\\2c", // invalid pair = \4\2c
2657
2658 // AttributeValue second alternative : "#" hexstring
2659 "CN=#", // no hex string
2660 "CN=#2", // no hex pair
2661 "CN=#22", // hexpair is not BER encoding
2662 "CN=#0001", // invalid BER encoding (missed content)
2663 "CN=#000201", // invalid BER encoding (wrong length)
2664 "CN=#0002010101", // invalid BER encoding (wrong length)
2665 "CN=#00FF", // invalid BER encoding (wrong length)
2666 "CN=#ZZ", // not hex pair
2667
2668 // FIXME boolean with indefinite length
2669 //"CN=#0100010000", // invalid BER encoding (wrong length)
2670
2671 // AttributeValue third alternative : " *( quotechar / pair ) "
2672 "CN=\"A\" B", // TODO comment me
2673 "CN=\"A\\", // TODO comment me
2674 "CN=\"\\4\"", // invalid pair = \4
2675 "CN=\"\\4Z\"", // invalid pair = \4Z
2676 "CN=\"\\4\\2c\"", // invalid pair = \4\2c
2677 };
2678
2679 StringBuffer errorMsg = new StringBuffer();
2680 for (String element : illegalDN) {
2681
2682 try {
2683 new X500Principal(element);
2684 errorMsg.append("No IllegalArgumentException: '" + element
2685 + "'\n");
2686 } catch (IllegalArgumentException e) {
2687 }
2688 }
2689
2690 if (errorMsg.length() != 0) {
2691 fail(errorMsg.toString());
2692 }
2693 }
2694
2695 public void testValidEncoding() {
2696 TestList list = new TestList();
2697
2698 //
2699 // Empty
2700 //
2701 list.add(new byte[] { 0x30, 0x00 }, "", "", "");
2702 list.add(new byte[] { 0x30, 0x02, 0x31, 0x00 }, "", "", ""); //??? invalid size constraints
2703
2704 //
2705 // Known OID + string with different tags(all string)
2706 //
2707 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2708 0x55, 0x04, 0x03,
2709 // PrintableString
2710 0x13, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2711 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2712 0x55, 0x04, 0x03,
2713 // TeletexString
Kenny Root155ee0c2014-01-13 14:51:25 -08002714 0x14, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
Narayan Kamath62542d82013-11-25 15:06:09 +00002715 //FIXME:compatibility list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2716 // 0x55, 0x04, 0x03,
2717 // // UniversalString
2718 // 0x1C, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2719 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2720 0x55, 0x04, 0x03,
2721 // UTF8String
2722 0x0C, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2723 //FIXME:compatibility list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2724 // 0x55, 0x04, 0x03,
2725 // // BMPString
2726 // 0x1E, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2727
2728 //
2729 // Unknown OID + string with different tags(all string)
2730 //
2731 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2732 0x00,
2733 // PrintableString
2734 0x13, 0x01, 0x5A }, "0.0=#13015a", "OID.0.0=Z", "0.0=#13015a");
2735 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2736 0x00,
2737 // TeletexString
2738 0x14, 0x01, 0x5A }, "0.0=#14015a", "OID.0.0=Z", "0.0=#14015a");
2739 //FIXME:compatibility list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2740 // 0x00,
2741 // // UniversalString
2742 // 0x1C, 0x01, 0x5A }, "0.0=#1c015a", "OID.0.0=Z", "cn=z");
2743 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2744 0x00,
2745 // UTF8String
2746 0x0C, 0x01, 0x5A }, "0.0=#0c015a", "OID.0.0=Z", "0.0=#0c015a");
2747 //FIXME:compatibility list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2748 // 0x00,
2749 // // BMPString
2750 // 0x1E, 0x01, 0x5A }, "0.0=#1e015a", "OID.0.0=Z", "cn=z");
2751
2752 //
2753 // Known OID + not a string value
2754 //
2755 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2756 0x55, 0x04, 0x03,
2757 // Boolean
2758 0x01, 0x01, (byte) 0xFF }, "CN=#0101ff", "CN=#0101FF",
2759 "cn=#0101ff");
2760 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2761 0x55, 0x04, 0x03,
2762 // Integer
2763 0x02, 0x01, 0x0F }, "CN=#02010f", "CN=#02010F", "cn=#02010f");
2764 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2765 0x55, 0x04, 0x03,
2766 // BitString
2767 0x03, 0x01, 0x00 }, "CN=#030100", "CN=#030100", "cn=#030100");
2768 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2769 0x55, 0x04, 0x03,
2770 // SEQUENCE
2771 0x30, 0x01, 0x0A }, "CN=#30010a", "CN=#30010A", "cn=#30010a");
2772
2773 //
2774 // unknown OID + not a string value
2775 //
2776 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2777 0x00,
2778 // Boolean
2779 0x01, 0x01, (byte) 0xFF }, "0.0=#0101ff", "OID.0.0=#0101FF",
2780 "0.0=#0101ff");
2781 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2782 0x00,
2783 // Integer
2784 0x02, 0x01, 0x0F }, "0.0=#02010f", "OID.0.0=#02010F",
2785 "0.0=#02010f");
2786 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2787 0x00,
2788 // BitString
2789 0x03, 0x01, 0x00 }, "0.0=#030100", "OID.0.0=#030100",
2790 "0.0=#030100");
2791 list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2792 0x00,
2793 // SEQUENCE
2794 0x30, 0x01, 0x0A }, "0.0=#30010a", "OID.0.0=#30010A",
2795 "0.0=#30010a");
2796
2797 //
2798 // Known OID + UTF-8 string with chars to be escaped
2799 //
2800
2801 // spaces
2802 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2803 0x55, 0x04, 0x03,
2804 // UTF8String: a single space char
2805 0x0C, 0x01, 0x20 }, "CN=\\ ", "CN=\" \"", "cn=");
2806 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2807 0x55, 0x04, 0x03,
2808 // UTF8String: a space char at the beginning
2809 0x0C, 0x02, 0x20, 0x5A }, "CN=\\ Z", "CN=\" Z\"", "cn=z");
2810 list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2811 0x55, 0x04, 0x03,
2812 // UTF8String: two space chars at the beginning
Kenny Root155ee0c2014-01-13 14:51:25 -08002813 0x0C, 0x03, 0x20, 0x20, 0x5A }, "CN=\\ \\ Z", "CN=\" Z\"",
Narayan Kamath62542d82013-11-25 15:06:09 +00002814 "cn=z", (byte) 0x01);
2815 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2816 0x55, 0x04, 0x03,
2817 // UTF8String: a space char at the end
2818 0x0C, 0x02, 0x5A, 0x20 }, "CN=Z\\ ", "CN=\"Z \"", "cn=z");
2819 list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2820 0x55, 0x04, 0x03,
2821 // UTF8String: two space chars at the end
Kenny Root155ee0c2014-01-13 14:51:25 -08002822 0x0C, 0x03, 0x5A, 0x20, 0x20 }, "CN=Z\\ \\ ", "CN=\"Z \"",
Narayan Kamath62542d82013-11-25 15:06:09 +00002823 "cn=z", (byte) 0x01);
2824
2825 // special chars
2826 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2827 0x55, 0x04, 0x03,
2828 // UTF8String: a '#' char at the beginning
2829 0x0C, 0x02, 0x23, 0x5A }, "CN=\\#Z", "CN=\"#Z\"", "cn=\\#z");
2830 list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2831 0x55, 0x04, 0x03,
2832 // UTF8String: two '#' chars
2833 0x0C, 0x03, 0x23, 0x5A, 0x23 }, "CN=\\#Z\\#", "CN=\"#Z#\"",
2834 "cn=\\#z#");
2835 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2836 0x55, 0x04, 0x03,
2837 // UTF8String: ','
2838 0x0C, 0x02, 0x5A, 0x2C }, "CN=Z\\,", "CN=\"Z,\"", "cn=z\\,");
2839 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2840 0x55, 0x04, 0x03,
2841 // UTF8String: '+'
2842 0x0C, 0x02, 0x5A, 0x2B }, "CN=Z\\+", "CN=\"Z+\"", "cn=z\\+");
2843 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2844 0x55, 0x04, 0x03,
2845 // UTF8String: '"'
2846 0x0C, 0x02, 0x5A, 0x22 }, "CN=Z\\\"", "CN=\"Z\\\"\"",
2847 "cn=z\\\"", (byte) 0x02);
2848 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2849 0x55, 0x04, 0x03,
2850 // UTF8String: '\'
2851 0x0C, 0x02, 0x5A, 0x5C }, "CN=Z\\\\", "CN=\"Z\\\\\"",
2852 "cn=z\\\\", (byte) 0x02);
2853 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2854 0x55, 0x04, 0x03,
2855 // UTF8String: '<'
2856 0x0C, 0x02, 0x5A, 0x3C }, "CN=Z\\<", "CN=\"Z<\"", "cn=z\\<");
2857 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2858 0x55, 0x04, 0x03,
2859 // UTF8String: '>'
2860 0x0C, 0x02, 0x5A, 0x3E }, "CN=Z\\>", "CN=\"Z>\"", "cn=z\\>");
2861 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2862 0x55, 0x04, 0x03,
2863 // UTF8String: ';'
2864 0x0C, 0x02, 0x5A, 0x3B }, "CN=Z\\;", "CN=\"Z;\"", "cn=z\\;");
2865 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2866 0x55, 0x04, 0x03,
2867 // UTF8String: '='
2868 0x0C, 0x02, 0x5A, 0x3D }, "CN=Z\\=", "CN=\"Z=\"", "cn=z=");
2869 //FIXME list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2870 // 0x55, 0x04, 0x03,
2871 // // UTF8String: ';'
2872 // 0x0C, 0x02, 0x5A, 0x0D }, "CN=Z\\\r", "CN=\"Z\r\"", "cn=z");
2873
2874 // combinations
2875 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2876 0x55, 0x04, 0x03,
2877 // UTF8String: '\ '
2878 0x0C, 0x02, 0x5C, 0x20 }, "CN=\\\\\\ ", "CN=\"\\\\ \"",
2879 "cn=\\\\", (byte) 0x02);
2880 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2881 0x55, 0x04, 0x03,
2882 // UTF8String: ' \'
2883 0x0C, 0x02, 0x20, 0x5C }, "CN=\\ \\\\", "CN=\" \\\\\"",
2884 "cn=\\\\", (byte) 0x02);
2885 list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2886 0x55, 0x04, 0x03,
2887 // UTF8String: ' \ '
2888 0x0C, 0x03, 0x20, 0x5C, 0x20 }, "CN=\\ \\\\\\ ",
2889 "CN=\" \\\\ \"", "cn=\\\\", (byte) 0x02);
2890 list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2891 0x55, 0x04, 0x03,
2892 // UTF8String: 'Z Z' no escaping
2893 0x0C, 0x03, 0x5A, 0x20, 0x5A }, "CN=Z Z", "CN=Z Z", "cn=z z");
2894 list.add(new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2895 0x55, 0x04, 0x03,
2896 // UTF8String: 'Z Z' no escaping
Kenny Root155ee0c2014-01-13 14:51:25 -08002897 0x0C, 0x04, 0x5A, 0x20, 0x20, 0x5A }, "CN=Z Z", "CN=\"Z Z\"",
Narayan Kamath62542d82013-11-25 15:06:09 +00002898 "cn=z z", (byte) 0x02);
2899 list.add(new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2900 0x55, 0x04, 0x03,
2901 // UTF8String: ' #Z ' no escaping
2902 0x0C, 0x04, 0x20, 0x23, 0x5A, 0x20 }, "CN=\\ \\#Z\\ ",
2903 "CN=\" #Z \"", "cn=#z");
2904
2905 //
2906 // Special cases
2907 //
2908 // list.add(new byte[] {
2909 // // Name
2910 // 0x30, 0x13, 0x31, 0x11, 0x30, 0x0F,
2911 // // OID
2912 // 0x06, 0x0A, 0x09, (byte) 0x92, 0x26, (byte) 0x89, (byte) 0x93,
2913 // (byte) 0xF2, 0x2C, 0x64, 0x01, 0x01,
2914 // // ANY
2915 // 0x13, 0x01, 0x41 }, "UID=A", "OID.0.9.2342.19200300.100.1.1=A",
2916 // "uid=a");
2917 //
2918 // list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2919 // 0x55, 0x04, 0x03, 0x1E, 0x01, 0x5A }, "CN=Z", "CN=Z",
2920 // "cn=#1e015a");
2921
2922 //
2923 // Multi-valued DN
2924 //
2925 list.add(new byte[] { 0x30, 0x14, 0x31, 0x12,
2926 // 1
2927 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2928 // UTF8String: 'Z'
2929 0x0C, 0x01, 0x5A,
2930 //2
2931 0x30, 0x06, 0x06, 0x01, 0x01,
2932 // UTF8String: 'A'
2933 0x0C, 0x01, 0x41 }, "CN=Z+0.1=#0c0141", "CN=Z + OID.0.1=A",
2934 "cn=z+0.1=#0c0141");
2935
2936 //
2937 //
2938 //
2939 list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2940 0x55, 0x04, 0x03,
2941 // UTF8String: the last letter(capital) of Russian alphabet
2942 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }, "CN=" + ((char) 1071),
2943 "CN=" + ((char) 1071), "cn=" + ((char) 1103));
2944 // FIXME list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2945 // 0x55, 0x04, 0x03,
2946 // // UTF8String: the last letter(capital) of Russian alphabet
2947 // 0x0C, 0x03, (byte) 0xE0, (byte) 0x90, (byte) 0xAF }, "CN="
2948 // + ((char) 1071), "CN=" + ((char) 1071), "cn=" + ((char) 1103));
2949 // FIXME list.add(
2950 // new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2951 // 0x55, 0x04, 0x03,
2952 // // UTF8String: the last letter(capital) of Russian alphabet
2953 // 0x0C, 0x04, (byte) 0xF0, (byte) 0x80, (byte) 0x90,
2954 // (byte) 0xAF }, "CN=" + ((char) 1071), "CN="
2955 // + ((char) 1071), "cn=" + ((char) 1103));
2956 list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2957 0x55, 0x04, 0x03,
2958 // PrintableString: char '$' is not in table 8 (X.680)
2959 0x13, 0x01, 0x24 }, "CN=$", "CN=$", "cn=$");
2960
2961 StringBuffer errorMsg = new StringBuffer();
2962 for (int i = 0; i < list.size(); i++) {
2963
2964 Object[] values = list.get(i);
2965 byte[] encoded = (byte[]) values[0];
2966 String rfc2253 = (String) values[1];
2967 String rfc1179 = (String) values[2];
2968 String canonical = (String) values[3];
2969 byte mask = ((byte[]) values[4])[0];
2970
2971 X500Principal p;
2972 try {
2973 p = new X500Principal(encoded);
2974
2975 if (!rfc2253.equals(p.getName(X500Principal.RFC2253))) {
2976 if (!testing || ((mask & 0x01) == 0)) {
2977 errorMsg.append("RFC2253: " + i);
2978 errorMsg.append("\t\texpected: '" + rfc2253 + "'");
2979 errorMsg.append("\treturned: '"
2980 + p.getName(X500Principal.RFC2253) + "'\n");
2981 }
2982 }
2983
2984 if (!rfc1179.equals(p.getName(X500Principal.RFC1779))) {
2985 if (!testing || ((mask & 0x02) == 0)) {
2986 errorMsg.append("RFC1779: " + i);
2987 errorMsg.append("\t\texpected: '" + rfc1179 + "'");
2988 errorMsg.append("\treturned: '"
2989 + p.getName(X500Principal.RFC1779) + "'\n");
2990 }
2991 }
2992
2993 if (!canonical.equals(p.getName(X500Principal.CANONICAL))) {
2994 if (!testing || ((mask & 0x04) == 0)) {
2995 errorMsg.append("CANONICAL: " + i);
2996 errorMsg.append("\t\texpected: " + canonical + "'");
2997 errorMsg.append("\treturned: '"
2998 + p.getName(X500Principal.CANONICAL) + "'\n");
2999 }
3000 }
3001
3002 } catch (IllegalArgumentException e) {
3003 errorMsg.append("\nIllegalArgumentException: " + i + ", for "
3004 + rfc2253);
3005 continue;
3006 } catch (Exception e) {
3007 errorMsg.append("Exception: " + i + ", for " + rfc2253);
3008 errorMsg.append("\texcep: " + e.getClass().getName() + "\n");
3009 continue;
3010 }
3011
3012 }
3013
3014 if (errorMsg.length() != 0) {
3015 fail(errorMsg.toString());
3016 }
3017 }
3018
3019 @SuppressWarnings("serial")
3020 public static class TestList extends ArrayList<Object[]> {
3021 //
3022 // TODO comment me
3023 //
3024 public void add(String param, String rfc2253, String rfc1779) {
3025 add(param, rfc2253, rfc1779, (byte[]) null);
3026 }
3027
3028 public void add(String param, String rfc2253, String rfc1779,
3029 String canonical) {
3030 add(param, rfc2253, rfc1779, canonical, null);
3031 }
3032
3033 public void add(String param, String rfc2253, String rfc1779,
3034 byte[] encoded) {
3035 add(new Object[] { param, rfc2253, rfc1779, null, encoded,
3036 emptyMask });
3037 }
3038
3039 public void add(String param, String rfc2253, String rfc1779,
3040 byte[] encoded, byte mask) {
3041 add(new Object[] { param, rfc2253, rfc1779, null, encoded,
3042 new byte[] { mask } });
3043 }
3044
3045 public void add(String param, String rfc2253, String rfc1779,
3046 String canonical, byte[] encoded) {
3047 add(new Object[] { param, rfc2253, rfc1779, canonical, encoded,
3048 emptyMask });
3049 }
3050
3051 public void add(String param, String rfc2253, String rfc1779,
3052 String canonical, byte[] encoded, byte mask) {
3053 add(new Object[] { param, rfc2253, rfc1779, canonical, encoded,
3054 new byte[] { mask } });
3055 }
3056
3057 //
3058 // TODO comment me
3059 //
3060
3061 private static final byte[] emptyMask = new byte[] { 0x00 };
3062
3063 public void add(byte[] encoding, String rfc2253, String rfc1779,
3064 String canonical) {
3065 add(new Object[] { encoding, rfc2253, rfc1779, canonical, emptyMask });
3066 }
3067
3068 public void add(byte[] encoding, String rfc2253, String rfc1779,
3069 String canonical, byte mask) {
3070 add(new Object[] { encoding, rfc2253, rfc1779, canonical,
3071 new byte[] { mask } });
3072 }
3073 }
3074
3075
3076 public void testSerializationSelf() throws Exception {
3077 SerializationTest.verifySelf(getSerializationData());
3078 }
3079
3080 public void testSerializationGolden() throws Exception {
3081 SerializationTest.verifyGolden(this, getSerializationData());
3082 }
3083
3084 private Object[] getSerializationData() {
3085 return new Object[] { new X500Principal("CN=A"),
3086 new X500Principal("CN=A, C=B"),
3087 new X500Principal("CN=A, CN=B + C=C") };
3088 }
3089}
3090