1 /*
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 
18 package org.apache.harmony.tests.javax.security.auth.x500;
19 
20 import javax.security.auth.x500.X500Principal;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.InputStream;
24 import java.security.cert.CertificateFactory;
25 import java.security.cert.X509Certificate;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31 import junit.framework.TestCase;
32 import org.apache.harmony.testframework.serialization.SerializationTest;
33 import org.apache.harmony.security.tests.support.cert.TestUtils;
34 import tests.support.resource.Support_Resources;
35 
36 
37 /**
38  * Tests for <code>X500Principal</code> class constructors and methods.
39  *
40  */
41 public class X500PrincipalTest extends TestCase {
42 
43     /**
44      * javax.security.auth.x500.X500Principal#X500Principal(String name)
45      */
test_X500Principal_01()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      */
test_X500Principal_02()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      */
test_X500Principal_03()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      */
test_getName()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      */
test_getName_Format()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      */
test_hashCode()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      */
test_toString()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      */
test_getEncoded()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      */
test_equals()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 
getByteArray(byte[] array)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      */
test_ConstructorLjava_lang_String()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      */
test_ConstructorLjava_lang_String_java_util_Map()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      */
test_getNameLjava_lang_String()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      */
test_getNameLjava_lang_String_java_util_Map()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 
testStreamPosition()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 
testStreamPosition_0()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 
testStreamPosition_1()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 
testStreamPosition_2()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 
testEncodingFromFile()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 
testEncodingFromEncoding()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      */
testSafeEncoding()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      */
testToString()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      */
testHashCode()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      */
testEquals()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      */
test_equalsLjava_lang_Object()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      */
testKWAsOid_RFC1779()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      */
testKWAsOid_RFC2253()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      */
testKWAsOid_CANONICAL()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      */
testOid_RFC1779()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      */
testOid_RFC2253()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      */
testOid_CANONICAL()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      */
testNameGetEncoding()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      */
testNameGetEncoding_01()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      */
testGetName_RFC1779()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      */
testGetName_RFC2253()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      */
testGetName_CANONICAL()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      */
testStreamGetName_RFC1779()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      */
testStreamGetName_RFC2253()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      */
testStreamGetName_CANONICAL()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      */
testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC1779()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      */
testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_RFC2253()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      */
testGetName_EncodingWithWrongOidButGoodName_SeveralRDNs_CANONICAL()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      */
testGetName_wrongOidButGoodName_RFC1779()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      */
testGetName_wrongOidButGoodName_RFC2253()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      */
testGetName_CANONICAL_SortOrder()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      */
testGetName_CANONICAL_SortOrder_01()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      */
testGetName_CANONICAL_SortOrder_02()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      */
testGetName_CANONICAL_SortOrder_03()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      */
testGetName_CANONICAL_SortOrder_04()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      */
testGetName_CANONICAL_SortOrder_05()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      */
testGetName_CANONICAL_SortOrder_06()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      */
testGetName_CANONICAL_SortOrder_07()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      */
testGetNameUnicodeNormalized()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      */
testEmptyInputName()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      */
testNameSingleEscapedSpace()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      */
testNameOnlySpaces_RFC1779()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      */
testNameOnlySpaces_RFC2253()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      */
testNameOnlySpaces_CANONICAL()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      */
testIllegalInputName()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      */
testIllegalInputName_01()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      */
testIllegalInputName_02()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      */
testEmptyInputName_0()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 
testEmptyInputName_1()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 
testEmptyInputName_2()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 
testIllegalInputName_15()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 
testIllegalInputName_16()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      */
testIllegalInputName_04()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      */
testIllegalInputName_05()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      */
testIllegalInputName_06()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      */
testIllegalInputName_07()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      */
testIllegalInputName_08()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      */
testIllegalInputName_09()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      */
testIllegalInputName_10()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      */
testIllegalInputName_11()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      */
testIllegalInputName_12()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      */
testIllegalInputName_13()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      */
testSemiIllegalInputName_14()1320     public void testSemiIllegalInputName_14() {
1321         String dn = "CN=#7E0142";
1322         new X500Principal(dn);
1323     }
1324 
1325     /**
1326      * Change rev/d1c04dac850d upstream addresses the case of the string CN=prefix\<>suffix.
1327      *
1328      * Before said change, the string can be used to construct an X500Principal, although according
1329      * to RFC2253 is not possible. Also, characters after '<' are ignored. We have tests documenting
1330      * that we allow such strings, like testIllegalInputName_07, so we modified the change as to
1331      * allow the string. We check that the characters after '<' are not ignored.
1332      *
1333      * Note: the string CN=prefix\<>suffix in the test is escaped as CN=prefix\\<>suffix
1334      */
testSemiIllegalInputName_15()1335     public void testSemiIllegalInputName_15() {
1336         String dn = "CN=prefix\\<>suffix";
1337 
1338         X500Principal principal = new X500Principal(dn);
1339         assertEquals("CN=\"prefix<>suffix\"", principal.getName(X500Principal.RFC1779));
1340         assertEquals("CN=prefix\\<\\>suffix", principal.getName(X500Principal.RFC2253));
1341         assertEquals("cn=prefix\\<\\>suffix", principal.getName(X500Principal.CANONICAL));
1342     }
1343 
testInitClause()1344     public void testInitClause() {
1345         try {
1346             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1347                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1348                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1349             mess[3] = 0x12;//length field
1350             new X500Principal(mess);
1351 
1352             fail("No IllegalArgumentException on input array with improper length field");
1353         } catch (IllegalArgumentException e) {
1354         }
1355     }
1356 
1357     /**
1358      * Inits X500Principal with byte array = null
1359      * checks if proper exception is thrown
1360      */
testIllegalInputArray_0()1361     public void testIllegalInputArray_0() {
1362         try {
1363             byte[] mess = null;
1364             new X500Principal(mess);
1365             fail("No IllegalArgumentException on input array with improper length field");
1366         } catch (IllegalArgumentException e) {
1367         }
1368     }
1369 
1370     /**
1371      * Inits X500Principal with byte array with wrong length field
1372      * checks if proper exception is thrown
1373      */
testIllegalInputArray()1374     public void testIllegalInputArray() {
1375         try {
1376             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1377                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1378                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1379             mess[3] = 0x12;//length field
1380             new X500Principal(mess);
1381 
1382             fail("No IllegalArgumentException on input array with improper length field");
1383         } catch (IllegalArgumentException e) {
1384         }
1385     }
1386 
1387     /**
1388      * Inits X500Principal with input stream with wrong length field
1389      * checks if proper exception is thrown
1390      */
testIllegalInputArray_is()1391     public void testIllegalInputArray_is() {
1392         try {
1393             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1394                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1395                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1396             mess[3] = 0x12;//length field
1397             ByteArrayInputStream is = new ByteArrayInputStream(mess);
1398             new X500Principal(is);
1399 
1400             fail("No IllegalArgumentException on input array with improper length field");
1401         } catch (IllegalArgumentException e) {
1402         }
1403     }
1404 
1405     /**
1406      * Inits X500Principal with byte array with wrong inner Sequence tag field
1407      * checks if proper exception is thrown
1408      */
testIllegalInputArray_01()1409     public void testIllegalInputArray_01() {
1410         try {
1411             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1412                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1413                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1414             mess[4] = 0x12;//inner Sequence tag field
1415             new X500Principal(mess);
1416 
1417             fail("No IllegalArgumentException on input array with improper inner Sequence tag field");
1418         } catch (IllegalArgumentException e) {
1419         }
1420     }
1421 
1422     /**
1423      * Inits X500Principal with byte array with wrong last byte of OID
1424      * checks if proper exception is thrown
1425      */
testIllegalInputArray_02()1426     public void testIllegalInputArray_02() {
1427         try {
1428             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1429                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1430                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1431             mess[10] = (byte) 0xFE;//last byte of OID
1432             new X500Principal(mess);
1433 
1434             fail("No IllegalArgumentException on input array with improper last byte of OID");
1435         } catch (IllegalArgumentException e) {
1436         }
1437     }
1438 
1439     /**
1440      * Inits X500Principal with byte array with wrong length of OID
1441      * checks if proper exception is thrown
1442      */
testIllegalInputArray_03()1443     public void testIllegalInputArray_03() {
1444         try {
1445             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1446                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1447                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1448             mess[7] = 2;//length of OID
1449             new X500Principal(mess);
1450 
1451             fail("No IllegalArgumentException on input array with improper length of OID");
1452         } catch (IllegalArgumentException e) {
1453         }
1454     }
1455 
1456     /**
1457      * Inits X500Principal with byte array with wrong tag of value
1458      * checks if it is ignored
1459      */
testSemiIllegalInputArray_04()1460     public void testSemiIllegalInputArray_04() {
1461         byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55,
1462                 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08, 0x06,
1463                 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1464         mess[11] = (byte) 0x0F;//tag of value
1465         new X500Principal(mess);
1466     }
1467 
1468     /**
1469      * Inits X500Principal with byte array with wrong length of value
1470      * checks if proper exception is thrown
1471      */
testIllegalInputArray_05()1472     public void testIllegalInputArray_05() {
1473         try {
1474             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1475                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1476                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1477             mess[12] = 2;//length of value
1478             new X500Principal(mess);
1479 
1480             fail("No IllegalArgumentException on input array with improper length of value");
1481         } catch (IllegalArgumentException e) {
1482         }
1483     }
1484 
1485     /**
1486      * Inits X500Principal with input stream with wrong length of value
1487      * checks if proper exception is thrown
1488      */
testIllegalInputArray_05_is()1489     public void testIllegalInputArray_05_is() {
1490         try {
1491             byte[] mess = { 0x30, 0x18, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
1492                     0x55, 0x04, 0x03, 0x13, 0x01, 0x42, 0x31, 0x0A, 0x30, 0x08,
1493                     0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41 };
1494             mess[12] = 2;//length of value
1495             ByteArrayInputStream is = new ByteArrayInputStream(mess);
1496             new X500Principal(is);
1497 
1498             fail("No IllegalArgumentException on input array with improper length of value");
1499         } catch (IllegalArgumentException e) {
1500         }
1501     }
1502 
1503     /**
1504      * Inits X500Principal with string
1505      * Calls getName with improper parameter as format
1506      * checks if proper exception is thrown
1507      */
testIllegalFormat()1508     public void testIllegalFormat() {
1509         try {
1510             String dn = "CN=A";
1511             X500Principal principal = new X500Principal(dn);
1512             principal.getName("WRONG FORMAT");
1513             fail("No IllegalArgumentException on improper parameter as format");
1514         } catch (IllegalArgumentException e) {
1515         }
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 RFC1779 format
1523      * compares with expected value paying attention on sorting order of AVAs
1524      */
testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC1779()1525     public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC1779()
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.RFC1779);
1532         assertTrue("OID.2.16.4.3=B + CN=A".equals(s) ||
1533             "CN=A + OID.2.16.4.3=B".equals(s));
1534 
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 RFC2253 format
1542      * compares with expected value paying attention on sorting order of AVAs
1543      */
testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC2253()1544     public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_RFC2253()
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.RFC2253);
1551         assertTrue("2.16.4.3=#130142+CN=A".equals(s) ||
1552             "CN=A+2.16.4.3=#130142".equals(s));
1553 
1554     }
1555 
1556     /**
1557      * Inits X500Principal with a string, there are multiple AVAs and Oid which does not fall into any keyword
1558      * Gets encoding
1559      * Inits other X500Principal with the encoding
1560      * gets string in CANONICAL format
1561      * compares with expected value paying attention on sorting order of AVAs
1562      */
testGetName_EncodingWithWrongOidButGoodName_MultAVA_CANONICAL()1563     public void testGetName_EncodingWithWrongOidButGoodName_MultAVA_CANONICAL()
1564             throws Exception {
1565         String dn = "OID.2.16.4.3=B + CN=A";
1566         X500Principal principal = new X500Principal(dn);
1567         byte[] enc = principal.getEncoded();
1568         X500Principal principal2 = new X500Principal(enc);
1569         String s = principal2.getName(X500Principal.CANONICAL);
1570         assertEquals("cn=a+2.16.4.3=#130142", s);
1571 
1572     }
1573 
1574     /**
1575      * Inits X500Principal with byte array, where there are leading and tailing spaces
1576      * gets Name in RFC1779 format
1577      * compares with expected value of name
1578      */
testNameSpaceFromEncoding_RFC1779()1579     public void testNameSpaceFromEncoding_RFC1779() throws Exception {
1580         byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1581                 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1582         X500Principal principal = new X500Principal(mess);
1583         String s = principal.getName(X500Principal.RFC1779);
1584         assertEquals("CN=\" A \"", s);
1585 
1586     }
1587 
1588     /**
1589      * Inits X500Principal with byte array, where there are leading and tailing spaces
1590      * gets Name in RFC2253 format
1591      * compares with expected value of name
1592      */
testNameSpaceFromEncoding_RFC2253()1593     public void testNameSpaceFromEncoding_RFC2253() throws Exception {
1594         byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1595                 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1596         X500Principal principal = new X500Principal(mess);
1597         String s = principal.getName(X500Principal.RFC2253);
1598         assertEquals("CN=\\ A\\ ", s);
1599 
1600     }
1601 
1602     /**
1603      * Inits X500Principal with byte array, where there are leading and tailing spaces
1604      * gets Name in CANONICAL format
1605      * compares with expected value of name
1606      */
testNameSpaceFromEncoding_CANONICAL()1607     public void testNameSpaceFromEncoding_CANONICAL() throws Exception {
1608         byte[] mess = { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55,
1609                 0x04, 0x03, 0x13, 0x03, 0x20, 0x41, 0x20, };
1610         X500Principal principal = new X500Principal(mess);
1611         String s = principal.getName(X500Principal.CANONICAL);
1612         assertEquals("cn=a", s);
1613 
1614     }
1615 
1616     /**
1617      * Inits X500Principal with byte array, where there are special characters
1618      * gets Name in RFC1779 format
1619      * compares with expected value of name, checks if the string is in quotes
1620      */
testNameSpecialCharsFromEncoding_RFC1779()1621     public void testNameSpecialCharsFromEncoding_RFC1779() throws Exception {
1622         byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1623                 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1624         X500Principal principal = new X500Principal(mess);
1625         String s = principal.getName(X500Principal.RFC1779);
1626         assertEquals("CN=\";,\"", s);
1627 
1628     }
1629 
1630     /**
1631      * Inits X500Principal with byte array, where there are special characters
1632      * gets Name in RFC1779 format
1633      * compares with expected value of name, checks if the characters are escaped
1634      */
testNameSpecialCharsFromEncoding_RFC2253()1635     public void testNameSpecialCharsFromEncoding_RFC2253() throws Exception {
1636         byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1637                 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1638         X500Principal principal = new X500Principal(mess);
1639         String s = principal.getName(X500Principal.RFC2253);
1640         assertEquals("CN=\\;\\,", s);
1641 
1642     }
1643 
1644     /**
1645      * Inits X500Principal with byte array, where there are special characters
1646      * gets Name in CANONICAL format
1647      * compares with expected value of name, checks if the characters are escaped
1648      */
testNameSpecialCharsFromEncoding_CANONICAL()1649     public void testNameSpecialCharsFromEncoding_CANONICAL() throws Exception {
1650         byte[] mess = { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
1651                 0x04, 0x03, 0x0C, 0x02, 0x3B, 0x2C };
1652         X500Principal principal = new X500Principal(mess);
1653         String s = principal.getName(X500Principal.CANONICAL);
1654         assertEquals("cn=\\;\\,", s);
1655 
1656     }
1657 
1658     /**
1659      * Inits X500Principal with the string with special characters - \"B
1660      * gets Name in RFC1779 format
1661      * compares with expected value of name - "\B"
1662      */
testNameSpecialChars_RFC1779()1663     public void testNameSpecialChars_RFC1779() throws Exception {
1664         String dn = "CN=A,CN=\\\"B";
1665         X500Principal principal = new X500Principal(dn);
1666         String s = principal.getName(X500Principal.RFC1779);
1667         assertEquals("CN=A, CN=\"\\\"B\"", s);
1668 
1669     }
1670 
1671     /**
1672      * Inits X500Principal with the string with special characters - \"B
1673      * gets Name in RFC2253 format
1674      * compares with expected value of name - "\B"
1675      */
testNameSpecialChars_RFC2253()1676     public void testNameSpecialChars_RFC2253() throws Exception {
1677         String dn = "CN=A,CN=\\\"B";
1678         X500Principal principal = new X500Principal(dn);
1679         String s = principal.getName(X500Principal.RFC2253);
1680         assertEquals("CN=A,CN=\\\"B", s);
1681 
1682     }
1683 
1684     /**
1685      * Inits X500Principal with the string with special characters - \"B
1686      * gets Name in CANONICAL format
1687      * compares with expected value of name - "\b"
1688      */
testNameSpecialChars_CANONICAL()1689     public void testNameSpecialChars_CANONICAL() throws Exception {
1690         String dn = "CN=A,CN=\\\"B";
1691         X500Principal principal = new X500Principal(dn);
1692         String s = principal.getName(X500Principal.CANONICAL);
1693         assertEquals("cn=a,cn=\\\"b", s);
1694 
1695     }
1696 
1697     /**
1698      * Inits X500Principal with the string with special characters - \\nB
1699      * gets Name in RFC1779 format
1700      * compares with expected value of name - "\nB"
1701      */
testNameSpecialChars_RFC1779_01()1702     public void testNameSpecialChars_RFC1779_01() throws Exception {
1703         String dn = "CN=\\\nB";
1704         X500Principal principal = new X500Principal(dn);
1705         String s = principal.getName(X500Principal.RFC1779);
1706         assertEquals("CN=\"\nB\"", s);
1707     }
1708 
1709     /**
1710      * Inits X500Principal with the string with special characters - \\nB
1711      * gets Name in RFC2253 format
1712      * compares with expected value of name - \nB
1713      */
testNameSpecialChars_RFC2253_01()1714     public void testNameSpecialChars_RFC2253_01() throws Exception {
1715         X500Principal p = new X500Principal("CN=\\\nB");
1716         assertEquals("CN=\nB", p.getName(X500Principal.RFC2253));
1717     }
1718 
1719     /**
1720      * Inits X500Principal with the string with special characters - \\nB
1721      * gets Name in CANONICAL format
1722      * compares with expected value of name - \\nb
1723      */
testNameSpecialChars_CANONICAL_01()1724     public void testNameSpecialChars_CANONICAL_01() throws Exception {
1725         //FIXME testNameSpecialChars_RFC2253_01
1726         //        String dn = "CN=\\\nB";
1727         //        X500Principal principal = new X500Principal(dn);
1728         //        String s = principal.getName(X500Principal.CANONICAL);
1729         //        assertEquals("cn=b", s);
1730 
1731     }
1732 
1733     /**
1734      * Inits X500Principal with the string with special characters - \\B
1735      * gets Name in RFC1779 format
1736      * compares with expected value of name - "\B"
1737      */
testNameSpecialChars_RFC1779_02()1738     public void testNameSpecialChars_RFC1779_02() throws Exception {
1739         String dn = "CN=\\\\B";
1740         X500Principal principal = new X500Principal(dn);
1741         String s = principal.getName(X500Principal.RFC1779);
1742         assertEquals("CN=\"\\\\B\"", s);
1743 
1744     }
1745 
1746     /**
1747      * Inits X500Principal with the string with special characters - \\B
1748      * gets Name in RFC2253 format
1749      * compares with expected value of name - \\B
1750      */
testNameSpecialChars_RFC2253_02()1751     public void testNameSpecialChars_RFC2253_02() throws Exception {
1752         String dn = "CN=\\\\B";
1753         X500Principal principal = new X500Principal(dn);
1754         String s = principal.getName(X500Principal.RFC2253);
1755         assertEquals("CN=\\\\B", s);
1756 
1757     }
1758 
1759     /**
1760      * Inits X500Principal with the string with special characters - \\B
1761      * gets Name in CANONICAL format
1762      * compares with expected value of name - \\b
1763      */
testNameSpecialChars_CANONICAL_02()1764     public void testNameSpecialChars_CANONICAL_02() throws Exception {
1765         String dn = "CN=\\\\B";
1766         X500Principal principal = new X500Principal(dn);
1767         String s = principal.getName(X500Principal.CANONICAL);
1768         assertEquals("cn=\\\\b", s);
1769 
1770     }
1771 
1772     /**
1773      * Inits X500Principal with the string with special characters - ABC"DEF"
1774      * gets encoding
1775      * compares with expected encoding
1776      */
testNameWithQuotation()1777     public void testNameWithQuotation() throws Exception {
1778         String dn = "CN=\"ABCDEF\"";
1779 
1780         X500Principal principal = new X500Principal(dn);
1781         byte[] enc = principal.getEncoded();
1782         assertTrue(Arrays.equals(new byte[] { 0x30, 0x11, 0x31, 0x0F, 0x30,
1783                 0x0D, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x41, 0x42,
1784                 0x43, 0x44, 0x45, 0x46 }, enc));
1785 
1786     }
1787 
1788     /**
1789      * Inits X500Principal with the string with special characters - "ABCDEF
1790      * checks if the proper exception is thrown
1791      */
testNameWithQuotation_01()1792     public void testNameWithQuotation_01() throws Exception {
1793         String dn = "CN=\"ABCDEF";
1794         try {
1795             new X500Principal(dn);
1796             fail("No IllegalArgumentException on string with no closing quotations");
1797         } catch (IllegalArgumentException e) {
1798         }
1799     }
1800 
1801     /**
1802      * Inits X500Principal with the string with special characters - ABC"D#EF"
1803      * gets encoding
1804      * compares with expected encoding
1805      */
testNameWithQuotation_02()1806     public void testNameWithQuotation_02() throws Exception {
1807         String dn = "CN=\"ABCD#EF\"";
1808         X500Principal principal = new X500Principal(dn);
1809         byte[] enc = principal.getEncoded();
1810         assertTrue(Arrays.equals(new byte[] { 0x30, 0x12, 0x31, 0x10, 0x30,
1811                 0x0E, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x07, 0x41, 0x42,
1812                 0x43, 0x44, 0x23, 0x45, 0x46 }, enc));
1813     }
1814 
1815     /**
1816      * Inits X500Principal with the string with special characters - ABC"DEF"
1817      * Compatibility issue: according RFC 2253 such string is invalid
1818      * but we accept it, not string char is escaped
1819      */
testNameWithQuotation_03()1820     public void testNameWithQuotation_03() throws Exception {
1821         String dn = "CN=ABC\"DEF\"";
1822         X500Principal principal = new X500Principal(dn);
1823         assertEquals("CN=ABC\\\"DEF\\\"", principal
1824                 .getName(X500Principal.RFC2253));
1825     }
1826 
1827     /**
1828      * Inits X500Principal with the string with special characters - ABC"DEF"
1829      * gets Name in RFC1779 format
1830      * compares with expected value of name - "ABCDEF"
1831      */
testNameSpecialChars_RFC1779_03()1832     public void testNameSpecialChars_RFC1779_03() throws Exception {
1833         String dn = "CN=\"ABCDEF\"";
1834         X500Principal principal = new X500Principal(dn);
1835         String s = principal.getName(X500Principal.RFC1779);
1836         assertEquals("CN=ABCDEF", s);
1837 
1838     }
1839 
1840     /**
1841      * Inits X500Principal with the string with special characters - ABC"DEF"
1842      * gets Name in RFC2253 format
1843      * compares with expected value of name - ABC"DEF"
1844      */
testNameSpecialChars_RFC2253_03()1845     public void testNameSpecialChars_RFC2253_03() throws Exception {
1846         String dn = "CN=\"ABCDEF\"";
1847         X500Principal principal = new X500Principal(dn);
1848         String s = principal.getName(X500Principal.RFC2253);
1849         assertEquals("CN=ABCDEF", s);
1850 
1851     }
1852 
1853     /**
1854      * Inits X500Principal with the string with special characters - ABC"DEF"
1855      * gets Name in CANONICAL format
1856      * compares with expected value of name - abc"def"
1857      */
testNameSpecialChars_CANONICAL_03()1858     public void testNameSpecialChars_CANONICAL_03() throws Exception {
1859         String dn = "CN=\"ABCDEF\"";
1860         X500Principal principal = new X500Principal(dn);
1861         String s = principal.getName(X500Principal.CANONICAL);
1862         assertEquals("cn=abcdef", s);
1863 
1864     }
1865 
1866     /**
1867      * Inits X500Principal with the string with special characters - ABC"D#EF"
1868      * gets Name in RFC1779 format
1869      * compares with expected value of name - "ABCD#EF"
1870      */
testNameSpecialChars_RFC1779_04()1871     public void testNameSpecialChars_RFC1779_04() throws Exception {
1872         String dn = "CN=\"ABCD#EF\"";
1873         X500Principal principal = new X500Principal(dn);
1874         String s = principal.getName(X500Principal.RFC1779);
1875         assertEquals("CN=\"ABCD#EF\"", s);
1876 
1877     }
1878 
1879     /**
1880      * Inits X500Principal with the string with special characters - ABC"D#EF"
1881      * gets Name in RFC1779 format
1882      * compares with expected value of name - ABCD\#EF
1883      */
testNameSpecialChars_RFC2253_04()1884     public void testNameSpecialChars_RFC2253_04() throws Exception {
1885         String dn = "CN=\"ABCD#EF\"";
1886         X500Principal principal = new X500Principal(dn);
1887         String s = principal.getName(X500Principal.RFC2253);
1888         assertEquals("CN=ABCD\\#EF", s);
1889 
1890     }
1891 
1892     /**
1893      * Inits X500Principal with the string with special characters - ABC"D#EF"
1894      * gets Name in RFC1779 format
1895      * compares with expected value of name - abc"d#ef"
1896      */
testNameSpecialChars_CANONICAL_04()1897     public void testNameSpecialChars_CANONICAL_04() throws Exception {
1898         String dn = "CN=\"ABCD#EF\"";
1899         X500Principal principal = new X500Principal(dn);
1900         String s = principal.getName(X500Principal.CANONICAL);
1901         assertEquals("cn=abcd#ef", s);
1902 
1903     }
1904 
1905     /**
1906      * Inits X500Principal with the string with special characters - X#YZ
1907      * gets Name in RFC1779 format
1908      * compares with expected value of name - "X#YZ"
1909      */
testNameSpecialChars_RFC1779_05()1910     public void testNameSpecialChars_RFC1779_05() {
1911         String dn = "CN=X#YZ";
1912         X500Principal principal = new X500Principal(dn);
1913 
1914         String s = principal.getName(X500Principal.RFC1779);
1915         assertEquals("CN=\"X#YZ\"", s);
1916 
1917     }
1918 
1919     /**
1920      * Inits X500Principal with the string with special characters - X#YZ
1921      * gets Name in RFC2253 format
1922      * compares with expected value of name - X\#YZ
1923      */
testNameSpecialChars_RFC2253_05()1924     public void testNameSpecialChars_RFC2253_05() {
1925         String dn = "CN=X#YZ";
1926         X500Principal principal = new X500Principal(dn);
1927 
1928         String s = principal.getName(X500Principal.RFC2253);
1929 
1930         assertEquals("CN=X\\#YZ", s);
1931 
1932     }
1933 
1934     /**
1935      * Inits X500Principal with the string with special characters - X#YZ
1936      * gets Name in CANONICAL format
1937      * compares with expected value of name - x#yz
1938      */
testNameSpecialChars_CANONICAL_05()1939     public void testNameSpecialChars_CANONICAL_05() {
1940         String dn = "CN=X#YZ";
1941         X500Principal principal = new X500Principal(dn);
1942 
1943         String s = principal.getName(X500Principal.CANONICAL);
1944         assertEquals("cn=x#yz", s);
1945 
1946     }
1947 
1948     /**
1949      * Inits X500Principal with the string with special characters - CN=\#XYZ
1950      * gets Name in RFC1779 format
1951      * compares with expected value of name - CN="#XYZ"
1952      */
testNameSpecialChars_RFC1779_6()1953     public void testNameSpecialChars_RFC1779_6() throws Exception {
1954         String dn = "CN=\\#XYZ";
1955         X500Principal principal = new X500Principal(dn);
1956         String s = principal.getName(X500Principal.RFC1779);
1957         assertEquals("CN=\"#XYZ\"", s);
1958 
1959     }
1960 
1961     /**
1962      * Inits X500Principal with the string with special characters - CN=\#XYZ
1963      * gets Name in RFC2253 format
1964      * compares with expected value of name - CN=\#XYZ
1965      */
testNameSpecialChars_RFC2253_6()1966     public void testNameSpecialChars_RFC2253_6() throws Exception {
1967         String dn = "CN=\\#XYZ";
1968         X500Principal principal = new X500Principal(dn);
1969         String s = principal.getName(X500Principal.RFC2253);
1970         assertEquals("CN=\\#XYZ", s);
1971     }
1972 
1973     /**
1974      * Inits X500Principal with the string with special characters - CN=\#XYZ
1975      * gets Name in CANONICAL format
1976      * compares with expected value of name - cn=\#xyz
1977      */
testNameSpecialChars_CANONICAL_6()1978     public void testNameSpecialChars_CANONICAL_6() throws Exception {
1979         String dn = "CN=\\#XYZ";
1980         X500Principal principal = new X500Principal(dn);
1981         String s = principal.getName(X500Principal.CANONICAL);
1982         assertEquals("cn=\\#xyz", s);
1983     }
1984 
1985     /**
1986      * Inits X500Principal with the string with special characters - B\'space'
1987      * gets Name in RFC1779 format
1988      * compares with expected value of name - "B "
1989      */
testNameSpaces_RFC1779()1990     public void testNameSpaces_RFC1779() throws Exception {
1991         String dn = "CN=A,CN=B\\ ";
1992         X500Principal principal = new X500Principal(dn);
1993         String s = principal.getName(X500Principal.RFC1779);
1994         assertEquals("CN=A, CN=\"B \"", s);
1995 
1996     }
1997 
1998     /**
1999      * Inits X500Principal with the string with special characters - B\'space'
2000      * gets Name in RFC2253 format
2001      * compares with expected value of name - B\'space'
2002      */
testNameSpaces_RFC2253()2003     public void testNameSpaces_RFC2253() throws Exception {
2004         String dn = "CN=A,CN=B\\ ";
2005         X500Principal principal = new X500Principal(dn);
2006         String s = principal.getName(X500Principal.RFC2253);
2007         assertEquals("CN=A,CN=B\\ ", s);
2008 
2009     }
2010 
2011     /**
2012      * Inits X500Principal with the string with special characters - B\'space'
2013      * gets Name in CANONICAL format
2014      * compares with expected value of name - B\
2015      */
testNameSpaces_CANONICAL()2016     public void testNameSpaces_CANONICAL() throws Exception {
2017         String dn = "CN=A,CN=B\\ ";
2018         X500Principal principal = new X500Principal(dn);
2019         String s = principal.getName(X500Principal.CANONICAL);
2020         assertEquals("cn=a,cn=b", s);
2021 
2022     }
2023 
2024     /**
2025      * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2026      * gets Name in RFC1779 format
2027      * compares with expected value of name - "B   A"
2028      */
testNameSpaces_RFC1779_01()2029     public void testNameSpaces_RFC1779_01() throws Exception {
2030         String dn = "CN=\"B   A\"";
2031         X500Principal principal = new X500Principal(dn);
2032         String s = principal.getName(X500Principal.RFC1779);
2033         assertEquals("CN=\"B   A\"", s);
2034 
2035     }
2036 
2037     /**
2038      * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2039      * gets Name in 2253 format
2040      * compares with expected value of name - B'space''space''space'A
2041      */
testNameSpaces_RFC2253_01()2042     public void testNameSpaces_RFC2253_01() throws Exception {
2043         String dn = "CN=\"B   A\"";
2044         X500Principal principal = new X500Principal(dn);
2045         String s = principal.getName(X500Principal.RFC2253);
2046         assertEquals("CN=B   A", s);
2047 
2048     }
2049 
2050     /**
2051      * Inits X500Principal with the string with special characters - "B'space''space''space'A"
2052      * gets Name in CANONICAL format
2053      * compares with expected value of name - b'space'a
2054      */
testNameSpaces_CANONICAL_01()2055     public void testNameSpaces_CANONICAL_01() throws Exception {
2056         String dn = "CN=\"B   A\"";
2057         X500Principal principal = new X500Principal(dn);
2058         String s = principal.getName(X500Principal.CANONICAL);
2059         assertEquals("cn=b a", s);
2060 
2061     }
2062 
2063     /**
2064      * Inits X500Principal with the string with special characters - \\'space''space'B
2065      * gets Name in RFC1779 format
2066      * compares with expected value of name - "  B"
2067      */
testNameSpaces_RFC1779_02()2068     public void testNameSpaces_RFC1779_02() throws Exception {
2069         String dn = "CN=\\  B";
2070         X500Principal principal = new X500Principal(dn);
2071         String s = principal.getName(X500Principal.RFC1779);
2072         assertEquals("CN=\"  B\"", s);
2073 
2074     }
2075 
2076     /**
2077      * Inits X500Principal with the string with special characters - \\'space''space'B
2078      * gets Name in RFC1779 format
2079      * compares with expected value of name - \'space''space'B
2080      */
testNameSpaces_RFC2253_02()2081     public void testNameSpaces_RFC2253_02() throws Exception {
2082         String dn = "CN=\\  B";
2083         X500Principal principal = new X500Principal(dn);
2084         String s = principal.getName(X500Principal.RFC2253);
2085         assertEquals("CN=\\ \\ B", s);
2086 
2087     }
2088 
2089     /**
2090      * Inits X500Principal with the string with special characters - \\'space''space'B
2091      * gets Name in CANONICAL format
2092      * compares with expected value of name - \'space''space'b
2093      */
testNameSpaces_CANONICAL_02()2094     public void testNameSpaces_CANONICAL_02() throws Exception {
2095         String dn = "CN=\\  B";
2096         X500Principal principal = new X500Principal(dn);
2097         String s = principal.getName(X500Principal.CANONICAL);
2098         assertEquals("cn=b", s);
2099 
2100     }
2101 
2102     /**
2103      * Inits X500Principal with the string with special characters - ""B
2104      * checks if the proper exception is thrown
2105      */
testNameQu()2106     public void testNameQu() throws Exception {
2107         String dn = "CN=\"\"B";
2108         try {
2109             new X500Principal(dn);
2110             fail("No IllegalArgumentException on improper string");
2111         } catch (IllegalArgumentException e) {
2112         }
2113     }
2114 
2115     /**
2116      * Inits X500Principal with the string with special characters - "A\"B"
2117      * gets Name in RFC1779 format
2118      * compares with expected value of name - "A\"B"
2119      */
testNameQu_RFC1779_2()2120     public void testNameQu_RFC1779_2() throws Exception {
2121         String dn = "CN=\"A\\\"B\"";
2122         X500Principal principal = new X500Principal(dn);
2123         String s = principal.getName(X500Principal.RFC1779);
2124         assertEquals("CN=\"A\\\"B\"", s);
2125     }
2126 
2127     /**
2128      * Inits X500Principal with the string with special characters - "A\"B"
2129      * gets Name in RFC2253 format
2130      * compares with expected value of name - A\"B
2131      */
testNameQu_RFC2253_2()2132     public void testNameQu_RFC2253_2() throws Exception {
2133         String dn = "CN=\"A\\\"B\"";
2134         X500Principal principal = new X500Principal(dn);
2135         String s = principal.getName(X500Principal.RFC2253);
2136         assertEquals("CN=A\\\"B", s);
2137     }
2138 
2139     /**
2140      * Inits X500Principal with the string with special characters - "A\"B"
2141      * gets Name in CANONICAL format
2142      * compares with expected value of name - a\"b
2143      */
testNameQu_CANONICAL_2()2144     public void testNameQu_CANONICAL_2() throws Exception {
2145         String dn = "CN=\"A\\\"B\"";
2146         X500Principal principal = new X500Principal(dn);
2147         String s = principal.getName(X500Principal.CANONICAL);
2148         assertEquals("cn=a\\\"b", s);
2149 
2150     }
2151 
2152     /**
2153      * Inits X500Principal with the string with special characters - "A\""
2154      * gets Name in RFC1779 format
2155      * compares with expected value of name - "A\""
2156      */
testNameQu_RFC1779_3()2157     public void testNameQu_RFC1779_3() throws Exception {
2158         String dn = "CN=\"A\\\"\"";
2159         X500Principal principal = new X500Principal(dn);
2160         String s = principal.getName(X500Principal.RFC1779);
2161         assertEquals("CN=\"A\\\"\"", s);
2162     }
2163 
2164     /**
2165      * Inits X500Principal with the string with special characters - "A\""
2166      * gets Name in RFC2253 format
2167      * compares with expected value of name - A\"
2168      */
testNameQu_RFC2253_3()2169     public void testNameQu_RFC2253_3() throws Exception {
2170         String dn = "CN=\"A\\\"\"";
2171         X500Principal principal = new X500Principal(dn);
2172         String s = principal.getName(X500Principal.RFC2253);
2173         assertEquals("CN=A\\\"", s);
2174     }
2175 
2176     /**
2177      * Inits X500Principal with the string with special characters - "A\""
2178      * gets Name in CANONICAL format
2179      * compares with expected value of name - A\"
2180      */
testNameQu_CANONICAL_3()2181     public void testNameQu_CANONICAL_3() throws Exception {
2182         String dn = "CN=\"A\\\"\"";
2183         X500Principal principal = new X500Principal(dn);
2184         String s = principal.getName(X500Principal.CANONICAL);
2185         assertEquals("cn=a\\\"", s);
2186 
2187     }
2188 
2189     /**
2190      * Inits X500Principal with the string with special characters - "A\", C=B"
2191      * gets Name in RFC1779 format
2192      * compares with expected value of name - "A\", C=B"
2193      */
testNameQu_4()2194     public void testNameQu_4() throws Exception {
2195         String dn = "CN=\"A\\\", C=B\"";
2196         X500Principal principal = new X500Principal(dn);
2197         String s = principal.getName(X500Principal.RFC1779);
2198         assertEquals("CN=\"A\\\", C=B\"", s);
2199 
2200     }
2201 
2202     /**
2203      * Inits X500Principal with the string with special characters - CN="A\\", C=B
2204      * gets Name in RFC1779 format
2205      * compares with expected value of name - CN="A\\", C=B
2206      */
testNameQu_5()2207     public void testNameQu_5() throws Exception {
2208         String dn = "CN=\"A\\\\\", C=B";
2209         X500Principal principal = new X500Principal(dn);
2210         String s = principal.getName(X500Principal.RFC1779);
2211         assertEquals("CN=\"A\\\\\", C=B", s);
2212 
2213     }
2214 
2215     /**
2216      * Inits X500Principal with the string with special characters - CN=A\nB
2217      * gets Name in RFC1779 format
2218      * compares with expected value of name - CN="A\nB"
2219      */
testNameCR_RFC1779()2220     public void testNameCR_RFC1779() throws Exception {
2221         String dn = "CN=A\nB";
2222         X500Principal principal = new X500Principal(dn);
2223         String s = principal.getName(X500Principal.RFC1779);
2224         assertEquals("CN=\"A\nB\"", s);
2225     }
2226 
2227 
testNamePlus_RFC1779()2228     public void testNamePlus_RFC1779() throws Exception {
2229         String dn = "CN=A\\+B";
2230         X500Principal principal = new X500Principal(dn);
2231         String s = principal.getName(X500Principal.RFC1779);
2232         assertEquals("CN=\"A+B\"", s);
2233     }
2234 
2235     /**
2236      * Inits X500Principal with the string with special characters - CN=A\nB
2237      * gets Name in RFC2253 format
2238      * compares with expected value of name - CN=A\nB
2239      */
testNameCR_RFC2253()2240     public void testNameCR_RFC2253() throws Exception {
2241         String dn = "CN=A\nB";
2242         X500Principal principal = new X500Principal(dn);
2243         String s = principal.getName(X500Principal.RFC2253);
2244         assertEquals("CN=A\nB", s);
2245     }
2246 
2247     /**
2248      * Inits X500Principal with the string with special characters - CN=A\nB
2249      * gets Name in CANONICAL format
2250      * compares with expected value of name - cn=a\nb
2251      */
testNameCR_CANONICAL()2252     public void testNameCR_CANONICAL() throws Exception {
2253         String dn = "CN=A\nB";
2254         X500Principal principal = new X500Principal(dn);
2255         String s = principal.getName(X500Principal.CANONICAL);
2256         assertEquals("cn=a\nb", s);
2257     }
2258 
2259     public static final String[] RFC2253_SPECIAL = new String[] { ",", "=",
2260             "+", "<", ">", "#", ";" };
2261 
testValidDN()2262     public void testValidDN() throws Exception {
2263 
2264         TestList list = new TestList();
2265 
2266         list.add("", "", "", "", new byte[] { 0x30, 0x00 }); // empty RDN sequence
2267 
2268         // sequence of RDN: RDN *("," RDN)
2269         list.add("CN=A,C=B", "CN=A,C=B", "CN=A, C=B", "cn=a,c=b");
2270         list.add("C=B,CN=A", "C=B,CN=A", "C=B, CN=A", "c=b,cn=a");
2271         list.add("CN=A,CN=A", "CN=A,CN=A", "CN=A, CN=A", "cn=a,cn=a"); // duplicate RDNs
2272 
2273         // sequence of RDN: RFC 1779 compatibility
2274         list.add("CN=A , C=B", "CN=A,C=B", "CN=A, C=B");
2275         list.add("CN=A  ,  C=B", "CN=A,C=B", "CN=A, C=B");
2276         list.add("CN=A;C=B", "CN=A,C=B", "CN=A, C=B");
2277         list.add("CN=A ; C=B", "CN=A,C=B", "CN=A, C=B");
2278         //FIXME list.add("CN=A\r,\rC=B", "CN=A,C=B"); // <CR> & comma => comma
2279         list.add("  CN=A,C=B  ", "CN=A,C=B", "CN=A, C=B"); // spaces at beg&end
2280         list.add("  CN=A,C=\"B\"  ", "CN=A,C=B", "CN=A, C=B"); // spaces at beg&end
2281 
2282         // set of ATAV: ATAV *("+" ATAV)
2283         list.add("CN=A+ST=CA", "CN=A+ST=CA", "CN=A + ST=CA", "cn=a+st=ca");
2284         list.add("CN=A+CN=A", "CN=A+CN=A", "CN=A + CN=A", "cn=a+cn=a"); // duplicate AT
2285         list
2286                 .add("2.5.4.3=A+2.5.4.3=A", "CN=A+CN=A", "CN=A + CN=A",
2287                         "cn=a+cn=a"); // duplicate AT
2288 
2289         // set of ATAV: RFC 1779 compatibility
2290         list.add("CN=A + ST=CA", "CN=A+ST=CA", "CN=A + ST=CA");
2291         list.add("CN=A  +  ST=CA", "CN=A+ST=CA", "CN=A + ST=CA");
2292         //FIXME list.add("CN=A\r+\rST=CA", "CN=A+ST=CA"); // <CR> & '+' => '+'
2293 
2294         // ATAV = AttributeType "=" AttributeValue
2295         list.add("CN=A", "CN=A", "CN=A");
2296         list.add("cn=A", "CN=A", "CN=A"); // AT case insensitive
2297         list.add("cN=A", "CN=A", "CN=A"); // AT case insensitive
2298         list.add("cn=a", "CN=a", "CN=a"); // AT case insensitive
2299 
2300         // ATAV : RFC 1779 compatibility
2301         list.add("CN = A", "CN=A", "CN=A");
2302         list.add("CN  =  A", "CN=A", "CN=A");
2303         // FIXME list.add("CN\r=\rA", "CN=A"); // <CR> & '=' => '='
2304 
2305         // AttributeType = <name string> | <OID>
2306         // testing OID case :  OID => <name string>
2307         // tested all OIDs from RFC 2253 (2.3) and RFC 1779 (Table 1)
2308 
2309         // different variants of 2.5.4.3 (CN) OID
2310         list.add("OID.2.5.4.3=A", "CN=A", "CN=A");
2311         list.add("oid.2.5.4.3=A", "CN=A", "CN=A");
2312         list.add("2.5.4.3=A", "CN=A", "CN=A");
2313         list.add("02.5.4.3=A", "CN=A", "CN=A"); // first: 02 => 2
2314         list.add("2.5.4.0003=A", "CN=A", "CN=A"); // last: 0003 => 3
2315 
2316         // the rest of OIDs
2317         list.add("2.5.4.7=A", "L=A", "L=A", "l=a");
2318         list.add("2.5.4.8=A", "ST=A", "ST=A", "st=a");
2319         list.add("2.5.4.10=A", "O=A", "O=A", "o=a");
2320         list.add("2.5.4.11=A", "OU=A", "OU=A", "ou=a");
2321         list.add("2.5.4.6=A", "C=A", "C=A", "c=a");
2322         list.add("2.5.4.9=A", "STREET=A", "STREET=A", "street=a");
2323         list.add("0.9.2342.19200300.100.1.25=A", "DC=A",
2324                 "OID.0.9.2342.19200300.100.1.25=A", "dc=#160141");
2325         list.add("0.9.2342.19200300.100.1.1=A", "UID=A",
2326                 "OID.0.9.2342.19200300.100.1.1=A", "uid=a");
2327 
2328         // attribute types from RFC 2459 (see Appendix A)
2329         // keywords are from the API spec
2330         list.add("T=A", "2.5.4.12=#130141", "OID.2.5.4.12=A",
2331                 "2.5.4.12=#130141");
2332         list.add("DNQ=A", "2.5.4.46=#130141", "OID.2.5.4.46=A",
2333                 "2.5.4.46=#130141");
2334         list.add("DNQUALIFIER=A", "2.5.4.46=#130141", "OID.2.5.4.46=A",
2335                 "2.5.4.46=#130141");
2336         list.add("SURNAME=A", "2.5.4.4=#130141", "OID.2.5.4.4=A",
2337                 "2.5.4.4=#130141");
2338         list.add("GIVENNAME=A", "2.5.4.42=#130141", "OID.2.5.4.42=A",
2339                 "2.5.4.42=#130141");
2340         list.add("INITIALS=A", "2.5.4.43=#130141", "OID.2.5.4.43=A",
2341                 "2.5.4.43=#130141");
2342         list.add("GENERATION=A", "2.5.4.44=#130141", "OID.2.5.4.44=A",
2343                 "2.5.4.44=#130141");
2344         list.add("EMAILADDRESS=A", "1.2.840.113549.1.9.1=#160141",
2345                 "OID.1.2.840.113549.1.9.1=A", "1.2.840.113549.1.9.1=#160141",
2346                 null, (byte) 0x05); //FIXME bug???
2347         list.add("SERIALNUMBER=A", "2.5.4.5=#130141", "OID.2.5.4.5=A",
2348                 "2.5.4.5=#130141");
2349 
2350         // AttributeValue => BER encoding (if OID in dotted-decimal form)
2351         // see RFC 2253 (2.4)
2352         list.add("OID.2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2353         list.add("oid.2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2354         list.add("2.5.4.12=A", "2.5.4.12=#130141", "OID.2.5.4.12=A");
2355         list.add("1.1=A", "1.1=#130141", "OID.1.1=A");
2356 
2357         //
2358         // AttributeValue first alternative : *( stringchar / pair )
2359         // testing pair characters.
2360         //
2361         // Note: for RFC1779 quoted string is returned (unspecified)
2362         //
2363         list.add("CN=", "CN=", "CN="); // zero string chars
2364         list.add("CN= ", "CN=", "CN="); // zero string chars
2365         list.add("CN=A+ST=", "CN=A+ST=", "CN=A + ST="); // zero string chars
2366         list.add("CN=+ST=A", "CN=+ST=A", "CN= + ST=A"); // empty value for 1 RDN
2367         list.add("CN=A+ST= ", "CN=A+ST=", "CN=A + ST="); // empty value for 1 RDN
2368         list.add("CN=+ST=", "CN=+ST=", "CN= + ST="); // empty value for both RDNs
2369         list.add("CN=,ST=B", "CN=,ST=B", "CN=, ST=B"); // empty value for 1 RDN
2370         list.add("CN=,ST=", "CN=,ST=", "CN=, ST="); // empty value for both RDNs
2371         list.add("CN=;ST=B", "CN=,ST=B", "CN=, ST=B"); // empty value for 1 RDN
2372         list.add("CN=;ST=", "CN=,ST=", "CN=, ST="); // empty value for both RDNs
2373         for (String element : RFC2253_SPECIAL) {
2374             // \special
2375             list.add("CN=\\" + element,
2376                     "CN=\\" + element, "CN=\"" + element
2377                     + "\"");
2378 
2379             // A + \special + B
2380             list.add("CN=A\\" + element + "B", "CN=A\\"
2381                     + element + "B", "CN=\"A" + element
2382                     + "B\"");
2383         }
2384 
2385         // pair = \"
2386         list.add("CN=\\\"", "CN=\\\"", "CN=\"\\\"\"", null, (byte) 0x02);
2387         list.add("CN=\\\"A", "CN=\\\"A", "CN=\"\\\"A\"", null, (byte) 0x02);
2388         list.add("CN=\\\",C=\\\"", "CN=\\\",C=\\\"", "CN=\"\\\"\", C=\"\\\"\"",
2389                 null, (byte) 0x02); // 2 RDN
2390         list.add("CN=A\\\"B", "CN=A\\\"B", "CN=\"A\\\"B\"", null, (byte) 0x02); // A\"B
2391         list.add("CN=A ST=B", "CN=A ST\\=B", "CN=\"A ST=B\""); // no RDN separator
2392 
2393         // pair = \space
2394         list.add("CN=\\ ", "CN=\\ ", "CN=\" \"", "cn=");
2395 
2396         // pair = \hexpair
2397         list.add("CN=\\41", "CN=A", "CN=A"); // 0x41=='A'
2398         list.add("CN=\\41\\2C", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2C=','
2399         list.add("CN=\\41\\2c", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2c=','
2400         list.add("CN=\\D0\\AF", "CN=" + ((char) 1071), "CN=" + ((char) 1071),
2401                 new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2402                         0x55, 0x04, 0x03,
2403                         // UTF8 String
2404                         0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // 0xD0AF == the last letter(capital) of Russian alphabet
2405         list.add("CN=\\D0\\AFA\\41", "CN=" + ((char) 1071) + "AA", "CN="
2406                 + ((char) 1071) + "AA", new byte[] { 0x30, 0x0F, 0x31, 0x0D,
2407                 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
2408                 // UTF8 String
2409                 0x0C, 0x04, (byte) 0xD0, (byte) 0xAF, 0x41, 0x41 }); // 0xD0AF == the last letter(capital) of Russian alphabet
2410         // UTF-8(0xE090AF) is non-shortest form of UTF-8(0xD0AF)
2411         //FIXME list.add("CN=\\E0\\90\\AF", "CN=" + ((char) 1071), "CN="
2412         //        + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2413         //        0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2414         //        // UTF8 String
2415         //        0x0C, 0x02, (byte) 0xD0, (byte) 0xAF });
2416         // UTF-8(0xF08090AF) is non-shortest form of UTF-8(0xD0AF)
2417         //FIXME list.add("CN=\\F0\\80\\90\\AF", "CN=" + ((char) 1071), "CN="
2418         //        + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2419         //        0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2420         //        // UTF8 String
2421         //        0x0C, 0x02, (byte) 0xD0, (byte) 0xAF });
2422         //FIXME        list.add("CN=\\D0", "CN=" + ((char) 65533), "CN=" + ((char) 65533),
2423         //                new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2424         //                        0x55, 0x04, 0x03,
2425         //                        // UTF8 String
2426         //                        0x0C, 0x01, 0x3F }); // 0xD0 is not correct UTF8 char => '?'
2427         list.add("CN=\\41+ST=A", "CN=A+ST=A", "CN=A + ST=A"); // 0x41=='A'
2428         list.add("CN=\\41\\2C+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2C=','
2429         list.add("CN=\\41\\2c+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2c=','
2430 
2431         // stringchar '=' or not leading '#'
2432         //FIXME RFC 2253 grammar violation: '=' and '#' is a special char
2433         list.add("CN==", "CN=\\=", "CN=\"=\"");
2434         list.add("CN=A=", "CN=A\\=", "CN=\"A=\"");
2435         list.add("CN=A#", "CN=A\\#", "CN=\"A#\"");
2436 
2437         // not leading or trailing spaces
2438         list.add("CN=A B", "CN=A B", "CN=A B", "cn=a b");
2439         list.add("CN=A\\ B", "CN=A B", "CN=A B", "cn=a b");
2440         list.add("CN=A \\,B", "CN=A \\,B", "CN=\"A ,B\"", "cn=a \\,b");
2441 
2442         //not alphabet chars
2443         list.add("CN=$", "CN=$", "CN=$", new byte[] { 0x30, 0x0C, 0x31, 0x0A,
2444                 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2445                 //UTF-8 String: "$"
2446                 0x0C, 0x01, 0x24 });
2447         list.add("CN=(", "CN=(", "CN=(", new byte[] { 0x30, 0x0C, 0x31, 0x0A,
2448                 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2449                 //PrintableString: "("
2450                 0x13, 0x01, 0x28 });
2451 
2452         //
2453         //
2454         // AttributeValue second alternative : "#" hexstring
2455         //
2456         //
2457         list.add("CN=#130141", "CN=A", "CN=A", "cn=a"); // ASN1 Printable hex string = 'A'
2458         list.add("CN=#140141", "CN=A", "CN=A", "cn=a", new byte[] { 0x30,
2459                 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2460                 0x14, 0x01, 0x41 }); // ASN1 Teletex hex string = 'A'
2461 
2462         list.add("CN=#010100", "CN=#010100", "CN=#010100", "cn=#010100"); // ASN1 Boolean = FALSE
2463         list.add("CN=#0101fF", "CN=#0101ff", "CN=#0101FF", "cn=#0101ff"); // ASN1 Boolean = TRUE
2464         //FIXME list.add("CN=#3000", "CN=#3000", "CN=#3000"); // ASN1 Sequence
2465         //FIXME list.add("CN=#0500", "CN=A", "CN=A"); // ASN1 Null
2466         list.add("CN= #0101fF", "CN=#0101ff", "CN=#0101FF", // space at beginning
2467                 new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2468                         0x55, 0x04, 0x03, 0x01, 0x01, (byte) 0xFF } // ASN.1 Boolean = TRUE
2469         );
2470         list.add("CN= #0101fF+ST=A", "CN=#0101ff+ST=A", "CN=#0101FF + ST=A",
2471                 "cn=#0101ff+st=a"); //space
2472         list.add("CN=  \n  #0101fF+ST=A", "CN=#0101ff+ST=A", "CN=#0101FF + ST=A",
2473                 "cn=#0101ff+st=a"); // multiple spaces
2474         list.add("CN= #0101fF ", "CN=#0101ff", "CN=#0101FF", // space at the end
2475                 new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2476                         0x55, 0x04, 0x03, 0x01, 0x01, (byte) 0xFF } // ASN.1 Boolean = TRUE
2477                 , (byte) 0x00);
2478         list.add("CN= #0101fF  \n  ", "CN=#0101ff", "CN=#0101FF", // multiple spaces at the end
2479                 new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2480                         0x55, 0x04, 0x03, 0x01, 0x01, (byte) 0xFF } // ASN.1 Boolean = TRUE
2481                 , (byte) 0x00);
2482 
2483         //FIXME unspecified output for RFC1779
2484         //FIXME list.add("CN=#1C0141", "CN=A", "CN=A"); // ASN1 Universal hex string = 'A'
2485         //FIXME list.add("CN=#1E0141", "CN=A", "CN=A"); // ASN1 Bmp hex string = 'A'
2486 
2487         //
2488         // AttributeValue third alternative : " *( quotechar / pair ) "
2489         // quotechar = <any character except '\' or '"' >
2490         //
2491         // Note:
2492         // RFC2253: passed quoted AV string is unquoted, special chars are escaped
2493         // RFC1779: escaped quoted chars are unescaped
2494         //
2495         list.add("CN=\"\"", "CN=", "CN="); // empty quoted string
2496         list.add("CN=\"A\"", "CN=A", "CN=A"); // "A"
2497         for (String element : RFC2253_SPECIAL) {
2498             // "special" => \special
2499             list.add("CN=\"" + element + "\"", "CN=\\"
2500                     + element, "CN=\"" + element + "\"");
2501 
2502             // "A + special + B" => A + \special + B
2503             list.add("CN=\"A" + element + "B\"", "CN=A\\"
2504                     + element + "B", "CN=\"A" + element
2505                     + "B\"");
2506         }
2507         for (String element : RFC2253_SPECIAL) {
2508             // "\special" => \special
2509             list.add("CN=\"\\" + element + "\"", "CN=\\"
2510                     + element, "CN=\"" + element + "\"");
2511 
2512             // "A + \special + B" => A + \special + B
2513             list.add("CN=\"A\\" + element + "B\"", "CN=A\\"
2514                     + element + "B", "CN=\"A" + element
2515                     + "B\"");
2516         }
2517         list.add("CN=\"\\\"\"", "CN=\\\"", "CN=\"\\\"\"", null, (byte) 0x02); // "\""
2518         list.add("CN=\"A\\\"B\"", "CN=A\\\"B", "CN=\"A\\\"B\"", null,
2519                 (byte) 0x02); // "A\"B"
2520 
2521         // pair = \hexpair (test cases are the same as for the first alternative)
2522         list.add("CN=\"\\41\"", "CN=A", "CN=A"); // 0x41=='A'
2523         list.add("CN=\"\\41\\2C\"", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2C=','
2524         list.add("CN=\"\\41\\2c\"", "CN=A\\,", "CN=\"A,\""); // 0x41=='A', 0x2c=','
2525         list.add("CN=\"\\D0\\AF\"", "CN=" + ((char) 1071), "CN="
2526                 + ((char) 1071), new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30,
2527                 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
2528                 // UTF8 String
2529                 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }); // 0xD0AF == the last letter(capital) of Russian alphabet
2530         list.add("CN=\"\\D0\\AFA\\41\"", "CN=" + ((char) 1071) + "AA", "CN="
2531                 + ((char) 1071) + "AA", new byte[] { 0x30, 0x0F, 0x31, 0x0D,
2532                 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
2533                 // UTF8 String
2534                 0x0C, 0x04, (byte) 0xD0, (byte) 0xAF, 0x41, 0x41 }); // 0xD0AF == the last letter(capital) of Russian alphabet
2535         list.add("CN=\"\\E0\\90\\AF\"", "CN=\ufffd\ufffd\ufffd", "CN=\ufffd\ufffd\ufffd",
2536                 new byte[] { 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03,
2537                 // UTF8 String
2538                 0x0C, 0x09, (byte) 0xEF, (byte) 0xBF, (byte) 0xBD, (byte) 0xEF, (byte) 0xBF,
2539                 (byte) 0xBD, (byte) 0xEF, (byte) 0xBF, (byte) 0xBD });
2540         // UTF8(0xE090AF) is not correct because it's a overlong form of UTF8(0xD0AF).
2541         list.add("CN=\"\\F0\\80\\90\\AF\"", "CN=\ufffd\ufffd\ufffd\ufffd",
2542                 "CN=\ufffd\ufffd\ufffd\ufffd",
2543                 new byte[] { 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03,
2544                 // UTF8 String
2545                 0x0C, 0x0C, (byte) 0xEF, (byte) 0xBF, (byte) 0xBD, (byte) 0xEF, (byte) 0xBF,
2546                 (byte) 0xBD, (byte) 0xEF, (byte) 0xBF, (byte) 0xBD, (byte) 0xEF, (byte) 0xBF,
2547                 (byte) 0xBD });
2548         // UTF8(0xF08090AF) is not correct because it's a overlong form of UTF8(0xD0AF).
2549 
2550         list.add("CN=\"\\41\"+ST=A", "CN=A+ST=A", "CN=A + ST=A"); // 0x41=='A'
2551         list.add("CN=\"\\41\\2C\"+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2C=','
2552         list.add("CN=\"\\41\\2c\"+ST=A", "CN=A\\,+ST=A", "CN=\"A,\" + ST=A"); // 0x41=='A', 0x2c=','
2553 
2554         // AttributeValue third alternative : RFC 1779 compatibility
2555         //FIXME list.add("CN=\"\r\"", "CN=\"\r\""); // "<CR>"
2556         //FIXME list.add("CN=\"\\\r\"", "CN=\"\\\r\""); // "\<CR>"
2557 
2558         // AttributeValue : RFC 1779 compatibility
2559         list.add("CN=  A  ", "CN=A", "CN=A", "cn=a"); // leading & trailing spaces
2560         list.add("CN=\\  A  ", "CN=\\ \\ A", "CN=\"  A\"", "cn=a", null,
2561                 (byte) 0x01); // escaped leading space
2562         list.add("CN=  A \\ ", "CN=A\\ \\ ", "CN=\"A  \"", "cn=a", null,
2563                 (byte) 0x01); // escaped trailing space
2564 
2565         list.add("CN=  \"A\"  ", "CN=A", "CN=A", "cn=a"); // leading & trailing spaces
2566 
2567         StringBuffer errorMsg = new StringBuffer();
2568         for (int i = 0; i < list.size(); i++) {
2569 
2570             Object[] obj = list.get(i);
2571 
2572             String dn = (String) obj[0];
2573             String rfc2253 = (String) obj[1];
2574             String rfc1779 = (String) obj[2];
2575             String canonical = (String) obj[3];
2576             byte[] encoded = (byte[]) obj[4];
2577             byte mask = ((byte[]) obj[5])[0];
2578 
2579             try {
2580                 X500Principal p = new X500Principal(dn);
2581                 if (!rfc2253.equals(p.getName(X500Principal.RFC2253))) {
2582                     if (!testing || ((mask & 0x01) == 0)) {
2583 
2584                         errorMsg.append("\nRFC2253: " + i);
2585                         errorMsg.append(" \tparm: '" + dn + "'");
2586                         errorMsg.append("\t\texpected: '" + rfc2253 + "'");
2587                         errorMsg.append("\treturned: '"
2588                                 + p.getName(X500Principal.RFC2253) + "'");
2589                     }
2590                 }
2591 
2592                 if (!rfc1779.equals(p.getName(X500Principal.RFC1779))) {
2593                     if (!testing || ((mask & 0x02) == 0)) {
2594 
2595                         errorMsg.append("\nRFC1779: " + i);
2596                         errorMsg.append(" \tparm: '" + dn + "'");
2597                         errorMsg.append("\t\texpected: '" + rfc1779 + "'");
2598                         errorMsg.append("\treturned: '"
2599                                 + p.getName(X500Principal.RFC1779) + "'");
2600                     }
2601                 }
2602 
2603                 if (canonical != null) {
2604                     if (!canonical.equals(p.getName(X500Principal.CANONICAL))) {
2605                         if (!testing || ((mask & 0x04) == 0)) {
2606 
2607                             errorMsg.append("\nCANONICAL: " + i);
2608                             errorMsg.append("\tparm: '" + dn + "'");
2609                             errorMsg.append("\t\texpected: '" + canonical + "'");
2610                             errorMsg.append("\treturned: '"
2611                                     + p.getName(X500Principal.CANONICAL) + "'");
2612                         }
2613                     }
2614                 }
2615 
2616                 if (encoded != null) {
2617                     if (!Arrays.equals(encoded, p.getEncoded())) {
2618                         if (!testing || ((mask & 0x08) == 0)) {
2619 
2620                             errorMsg.append("\nUnexpected encoding for: " + i
2621                                     + ", dn= '" + dn + "'");
2622 
2623                             System.out.println("\nI " + i);
2624                             byte[] enc = p.getEncoded();
2625                             for (byte element : enc) {
2626                                 System.out.print(", 0x"
2627                                         + Integer.toHexString(element));
2628                             }
2629                         }
2630                     }
2631                 }
2632             } catch (IllegalArgumentException e) {
2633                 errorMsg.append("\nIllegalArgumentException: " + i);
2634                 errorMsg.append("\tparm: '" + dn + "'");
2635             } catch (Exception e) {
2636                 errorMsg.append("\nException: " + i);
2637                 errorMsg.append("\tparm: '" + dn + "'");
2638                 errorMsg.append("\texcep: " + e.getClass().getName());
2639             }
2640         }
2641 
2642         if (errorMsg.length() != 0) {
2643             fail(errorMsg.toString());
2644         }
2645 
2646     }
2647 
testInvalidDN()2648     public void testInvalidDN() {
2649         String[] illegalDN = new String[] {
2650                 // RDN
2651                 //FIXME " ", // space only
2652                 "CN", // attribute type only
2653                 "CN=A;", // RFC 1779: BNF allows this, but ...
2654                 "CN=A,", // RFC 1779: BNF allows this, but ...
2655                 ",CN=A", // no AttributeType for first RDN
2656                 "CN=,A", // no AttributeType for second RDN
2657                 "CN=A+", // no AttributeTypeAndValue for second RDN
2658                 "CN=#130141 ST=B", // no RDN separator
2659 
2660                 // AttributeType = <name string> | <OID>
2661                 "AAA=A", // no such <name string>
2662                 "1..1=A", // wrong OID
2663                 ".1.1=A", // wrong OID
2664                 "11=A", // wrong OID
2665                 "1=A", // wrong OID
2666                 "AID.1.1=A", // wrong OID
2667                 "1.50=A", // wrong OID
2668                 "5.1.0=A", // wrong OID
2669                 "2.-5.4.3=A", // wrong OID
2670                 "2.5.-4.3=A", // wrong OID
2671                 "2.5.4-.3=A", // wrong OID
2672                 //FIXME "2.5.4.-3=A", // wrong OID
2673 
2674                 // AttributeValue first alternative : *( stringchar / pair )
2675                 "CN=,", // stringchar = ','
2676                 //FIXME "CN==",
2677                 "CN=+", // stringchar = '+'
2678                 //FIXME "CN=<", // stringchar = '<'
2679                 //FIXME "CN=>", // stringchar = '>'
2680                 "CN=#", // stringchar = '#'
2681                 //FIXME "CN=Z#", // stringchar = '#'
2682                 "CN=;", // stringchar = ';'
2683                 "CN=\"", // stringchar = "
2684                 //FIXME "CN=A\"B", // stringchar = "
2685                 "CN=\\", // stringchar = \
2686                 "CN=A\\", // stringchar = \
2687                 "CN=A\\B", // stringchar = \
2688                 "CN=\\z", // invalid pair = \z
2689                 "CN=\\4", // invalid pair = \4
2690                 "CN=\\4Z", // invalid pair = \4Z
2691                 "CN=\\4\\2c", // invalid pair = \4\2c
2692 
2693                 // AttributeValue second alternative : "#" hexstring
2694                 "CN=#", // no hex string
2695                 "CN=#2", // no hex pair
2696                 "CN=#22", // hexpair is not BER encoding
2697                 "CN=#0001", // invalid BER encoding (missed content)
2698                 "CN=#000201", // invalid BER encoding (wrong length)
2699                 "CN=#0002010101", // invalid BER encoding (wrong length)
2700                 "CN=#00FF", // invalid BER encoding (wrong length)
2701                 "CN=#ZZ", // not hex pair
2702 
2703                 // FIXME boolean with indefinite length
2704                 //"CN=#0100010000", // invalid BER encoding (wrong length)
2705 
2706                 // AttributeValue third alternative : " *( quotechar / pair ) "
2707                 "CN=\"A\" B", // TODO comment me
2708                 "CN=\"A\\", // TODO comment me
2709                 "CN=\"\\4\"", // invalid pair = \4
2710                 "CN=\"\\4Z\"", // invalid pair = \4Z
2711                 "CN=\"\\4\\2c\"", // invalid pair = \4\2c
2712         };
2713 
2714         StringBuffer errorMsg = new StringBuffer();
2715         for (String element : illegalDN) {
2716 
2717             try {
2718                 new X500Principal(element);
2719                 errorMsg.append("No IllegalArgumentException: '" + element
2720                         + "'\n");
2721             } catch (IllegalArgumentException e) {
2722             }
2723         }
2724 
2725         if (errorMsg.length() != 0) {
2726             fail(errorMsg.toString());
2727         }
2728     }
2729 
testValidEncoding()2730     public void testValidEncoding() {
2731         TestList list = new TestList();
2732 
2733         //
2734         // Empty
2735         //
2736         list.add(new byte[] { 0x30, 0x00 }, "", "", "");
2737         list.add(new byte[] { 0x30, 0x02, 0x31, 0x00 }, "", "", ""); //??? invalid size constraints
2738 
2739         //
2740         // Known OID + string with different tags(all string)
2741         //
2742         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2743                 0x55, 0x04, 0x03,
2744                 // PrintableString
2745                 0x13, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2746         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2747                 0x55, 0x04, 0x03,
2748                 // TeletexString
2749                 0x14, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2750         //FIXME:compatibility        list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2751         //                0x55, 0x04, 0x03,
2752         //                // UniversalString
2753         //                0x1C, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2754         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2755                 0x55, 0x04, 0x03,
2756                 // UTF8String
2757                 0x0C, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2758         //FIXME:compatibility        list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2759         //                0x55, 0x04, 0x03,
2760         //                // BMPString
2761         //                0x1E, 0x01, 0x5A }, "CN=Z", "CN=Z", "cn=z");
2762 
2763         //
2764         // Unknown OID + string with different tags(all string)
2765         //
2766         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2767                 0x00,
2768                 // PrintableString
2769                 0x13, 0x01, 0x5A }, "0.0=#13015a", "OID.0.0=Z", "0.0=#13015a");
2770         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2771                 0x00,
2772                 // TeletexString
2773                 0x14, 0x01, 0x5A }, "0.0=#14015a", "OID.0.0=Z", "0.0=#14015a");
2774         //FIXME:compatibility        list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2775         //                0x00,
2776         //                // UniversalString
2777         //                0x1C, 0x01, 0x5A }, "0.0=#1c015a", "OID.0.0=Z", "cn=z");
2778         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2779                 0x00,
2780                 // UTF8String
2781                 0x0C, 0x01, 0x5A }, "0.0=#0c015a", "OID.0.0=Z", "0.0=#0c015a");
2782         //FIXME:compatibility        list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2783         //                0x00,
2784         //                // BMPString
2785         //                0x1E, 0x01, 0x5A }, "0.0=#1e015a", "OID.0.0=Z", "cn=z");
2786 
2787         //
2788         // Known OID + not a string value
2789         //
2790         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2791                 0x55, 0x04, 0x03,
2792                 // Boolean
2793                 0x01, 0x01, (byte) 0xFF }, "CN=#0101ff", "CN=#0101FF",
2794                 "cn=#0101ff");
2795         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2796                 0x55, 0x04, 0x03,
2797                 // Integer
2798                 0x02, 0x01, 0x0F }, "CN=#02010f", "CN=#02010F", "cn=#02010f");
2799         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2800                 0x55, 0x04, 0x03,
2801                 // BitString
2802                 0x03, 0x01, 0x00 }, "CN=#030100", "CN=#030100", "cn=#030100");
2803         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2804                 0x55, 0x04, 0x03,
2805                 // SEQUENCE
2806                 0x30, 0x01, 0x0A }, "CN=#30010a", "CN=#30010A", "cn=#30010a");
2807 
2808         //
2809         // unknown OID + not a string value
2810         //
2811         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2812                 0x00,
2813                 // Boolean
2814                 0x01, 0x01, (byte) 0xFF }, "0.0=#0101ff", "OID.0.0=#0101FF",
2815                 "0.0=#0101ff");
2816         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2817                 0x00,
2818                 // Integer
2819                 0x02, 0x01, 0x0F }, "0.0=#02010f", "OID.0.0=#02010F",
2820                 "0.0=#02010f");
2821         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2822                 0x00,
2823                 // BitString
2824                 0x03, 0x01, 0x00 }, "0.0=#030100", "OID.0.0=#030100",
2825                 "0.0=#030100");
2826         list.add(new byte[] { 0x30, 0x0A, 0x31, 0x08, 0x30, 0x06, 0x06, 0x01,
2827                 0x00,
2828                 // SEQUENCE
2829                 0x30, 0x01, 0x0A }, "0.0=#30010a", "OID.0.0=#30010A",
2830                 "0.0=#30010a");
2831 
2832         //
2833         // Known OID + UTF-8 string with chars to be escaped
2834         //
2835 
2836         // spaces
2837         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2838                 0x55, 0x04, 0x03,
2839                 // UTF8String: a single space char
2840                 0x0C, 0x01, 0x20 }, "CN=\\ ", "CN=\" \"", "cn=");
2841         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2842                 0x55, 0x04, 0x03,
2843                 // UTF8String: a space char at the beginning
2844                 0x0C, 0x02, 0x20, 0x5A }, "CN=\\ Z", "CN=\" Z\"", "cn=z");
2845         list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2846                 0x55, 0x04, 0x03,
2847                 // UTF8String: two space chars at the beginning
2848                 0x0C, 0x03, 0x20, 0x20, 0x5A }, "CN=\\ \\ Z", "CN=\"  Z\"",
2849                 "cn=z", (byte) 0x01);
2850         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2851                 0x55, 0x04, 0x03,
2852                 // UTF8String: a space char at the end
2853                 0x0C, 0x02, 0x5A, 0x20 }, "CN=Z\\ ", "CN=\"Z \"", "cn=z");
2854         list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2855                 0x55, 0x04, 0x03,
2856                 // UTF8String: two space chars at the end
2857                 0x0C, 0x03, 0x5A, 0x20, 0x20 }, "CN=Z\\ \\ ", "CN=\"Z  \"",
2858                 "cn=z", (byte) 0x01);
2859 
2860         // special chars
2861         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2862                 0x55, 0x04, 0x03,
2863                 // UTF8String: a '#' char at the beginning
2864                 0x0C, 0x02, 0x23, 0x5A }, "CN=\\#Z", "CN=\"#Z\"", "cn=\\#z");
2865         list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2866                 0x55, 0x04, 0x03,
2867                 // UTF8String: two '#' chars
2868                 0x0C, 0x03, 0x23, 0x5A, 0x23 }, "CN=\\#Z\\#", "CN=\"#Z#\"",
2869                 "cn=\\#z#");
2870         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2871                 0x55, 0x04, 0x03,
2872                 // UTF8String: ','
2873                 0x0C, 0x02, 0x5A, 0x2C }, "CN=Z\\,", "CN=\"Z,\"", "cn=z\\,");
2874         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2875                 0x55, 0x04, 0x03,
2876                 // UTF8String: '+'
2877                 0x0C, 0x02, 0x5A, 0x2B }, "CN=Z\\+", "CN=\"Z+\"", "cn=z\\+");
2878         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2879                 0x55, 0x04, 0x03,
2880                 // UTF8String: '"'
2881                 0x0C, 0x02, 0x5A, 0x22 }, "CN=Z\\\"", "CN=\"Z\\\"\"",
2882                 "cn=z\\\"", (byte) 0x02);
2883         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2884                 0x55, 0x04, 0x03,
2885                 // UTF8String: '\'
2886                 0x0C, 0x02, 0x5A, 0x5C }, "CN=Z\\\\", "CN=\"Z\\\\\"",
2887                 "cn=z\\\\", (byte) 0x02);
2888         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2889                 0x55, 0x04, 0x03,
2890                 // UTF8String: '<'
2891                 0x0C, 0x02, 0x5A, 0x3C }, "CN=Z\\<", "CN=\"Z<\"", "cn=z\\<");
2892         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2893                 0x55, 0x04, 0x03,
2894                 // UTF8String: '>'
2895                 0x0C, 0x02, 0x5A, 0x3E }, "CN=Z\\>", "CN=\"Z>\"", "cn=z\\>");
2896         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2897                 0x55, 0x04, 0x03,
2898                 // UTF8String: ';'
2899                 0x0C, 0x02, 0x5A, 0x3B }, "CN=Z\\;", "CN=\"Z;\"", "cn=z\\;");
2900         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2901                 0x55, 0x04, 0x03,
2902                 // UTF8String: '='
2903                 0x0C, 0x02, 0x5A, 0x3D }, "CN=Z\\=", "CN=\"Z=\"", "cn=z=");
2904         //FIXME        list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2905         //                0x55, 0x04, 0x03,
2906         //                // UTF8String: ';'
2907         //                0x0C, 0x02, 0x5A, 0x0D }, "CN=Z\\\r", "CN=\"Z\r\"", "cn=z");
2908 
2909         // combinations
2910         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2911                 0x55, 0x04, 0x03,
2912                 // UTF8String: '\ '
2913                 0x0C, 0x02, 0x5C, 0x20 }, "CN=\\\\\\ ", "CN=\"\\\\ \"",
2914                 "cn=\\\\", (byte) 0x02);
2915         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2916                 0x55, 0x04, 0x03,
2917                 // UTF8String: ' \'
2918                 0x0C, 0x02, 0x20, 0x5C }, "CN=\\ \\\\", "CN=\" \\\\\"",
2919                 "cn=\\\\", (byte) 0x02);
2920         list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2921                 0x55, 0x04, 0x03,
2922                 // UTF8String: ' \ '
2923                 0x0C, 0x03, 0x20, 0x5C, 0x20 }, "CN=\\ \\\\\\ ",
2924                 "CN=\" \\\\ \"", "cn=\\\\", (byte) 0x02);
2925         list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2926                 0x55, 0x04, 0x03,
2927                 // UTF8String: 'Z Z' no escaping
2928                 0x0C, 0x03, 0x5A, 0x20, 0x5A }, "CN=Z Z", "CN=Z Z", "cn=z z");
2929         list.add(new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2930                 0x55, 0x04, 0x03,
2931                 // UTF8String: 'Z  Z' no escaping
2932                 0x0C, 0x04, 0x5A, 0x20, 0x20, 0x5A }, "CN=Z  Z", "CN=\"Z  Z\"",
2933                 "cn=z z", (byte) 0x02);
2934         list.add(new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2935                 0x55, 0x04, 0x03,
2936                 // UTF8String: ' #Z ' no escaping
2937                 0x0C, 0x04, 0x20, 0x23, 0x5A, 0x20 }, "CN=\\ \\#Z\\ ",
2938                 "CN=\" #Z \"", "cn=#z");
2939 
2940         //
2941         // Special cases
2942         //
2943         //        list.add(new byte[] {
2944         //        // Name
2945         //                0x30, 0x13, 0x31, 0x11, 0x30, 0x0F,
2946         //                // OID
2947         //                0x06, 0x0A, 0x09, (byte) 0x92, 0x26, (byte) 0x89, (byte) 0x93,
2948         //                (byte) 0xF2, 0x2C, 0x64, 0x01, 0x01,
2949         //                // ANY
2950         //                0x13, 0x01, 0x41 }, "UID=A", "OID.0.9.2342.19200300.100.1.1=A",
2951         //                "uid=a");
2952         //
2953         //        list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2954         //                0x55, 0x04, 0x03, 0x1E, 0x01, 0x5A }, "CN=Z", "CN=Z",
2955         //                "cn=#1e015a");
2956 
2957         //
2958         // Multi-valued DN
2959         //
2960         list.add(new byte[] { 0x30, 0x14, 0x31, 0x12,
2961                 // 1
2962                 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03,
2963                 // UTF8String: 'Z'
2964                 0x0C, 0x01, 0x5A,
2965                 //2
2966                 0x30, 0x06, 0x06, 0x01, 0x01,
2967                 // UTF8String: 'A'
2968                 0x0C, 0x01, 0x41 }, "CN=Z+0.1=#0c0141", "CN=Z + OID.0.1=A",
2969                 "cn=z+0.1=#0c0141");
2970 
2971         //
2972         //
2973         //
2974         list.add(new byte[] { 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
2975                 0x55, 0x04, 0x03,
2976                 // UTF8String: the last letter(capital) of Russian alphabet
2977                 0x0C, 0x02, (byte) 0xD0, (byte) 0xAF }, "CN=" + ((char) 1071),
2978                 "CN=" + ((char) 1071), "cn=" + ((char) 1103));
2979         // FIXME list.add(new byte[] { 0x30, 0x0E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
2980         //        0x55, 0x04, 0x03,
2981         //        // UTF8String: the last letter(capital) of Russian alphabet
2982         //        0x0C, 0x03, (byte) 0xE0, (byte) 0x90, (byte) 0xAF }, "CN="
2983         //        + ((char) 1071), "CN=" + ((char) 1071), "cn=" + ((char) 1103));
2984         // FIXME list.add(
2985         //        new byte[] { 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03,
2986         //                0x55, 0x04, 0x03,
2987         //                // UTF8String: the last letter(capital) of Russian alphabet
2988         //                0x0C, 0x04, (byte) 0xF0, (byte) 0x80, (byte) 0x90,
2989         //                (byte) 0xAF }, "CN=" + ((char) 1071), "CN="
2990         //                + ((char) 1071), "cn=" + ((char) 1103));
2991         list.add(new byte[] { 0x30, 0x0C, 0x31, 0x0A, 0x30, 0x08, 0x06, 0x03,
2992                 0x55, 0x04, 0x03,
2993                 // PrintableString: char '$' is not in table 8 (X.680)
2994                 0x13, 0x01, 0x24 }, "CN=$", "CN=$", "cn=$");
2995 
2996         StringBuffer errorMsg = new StringBuffer();
2997         for (int i = 0; i < list.size(); i++) {
2998 
2999             Object[] values = list.get(i);
3000             byte[] encoded = (byte[]) values[0];
3001             String rfc2253 = (String) values[1];
3002             String rfc1179 = (String) values[2];
3003             String canonical = (String) values[3];
3004             byte mask = ((byte[]) values[4])[0];
3005 
3006             X500Principal p;
3007             try {
3008                 p = new X500Principal(encoded);
3009 
3010                 if (!rfc2253.equals(p.getName(X500Principal.RFC2253))) {
3011                     if (!testing || ((mask & 0x01) == 0)) {
3012                         errorMsg.append("RFC2253: " + i);
3013                         errorMsg.append("\t\texpected: '" + rfc2253 + "'");
3014                         errorMsg.append("\treturned: '"
3015                                 + p.getName(X500Principal.RFC2253) + "'\n");
3016                     }
3017                 }
3018 
3019                 if (!rfc1179.equals(p.getName(X500Principal.RFC1779))) {
3020                     if (!testing || ((mask & 0x02) == 0)) {
3021                         errorMsg.append("RFC1779: " + i);
3022                         errorMsg.append("\t\texpected: '" + rfc1179 + "'");
3023                         errorMsg.append("\treturned: '"
3024                                 + p.getName(X500Principal.RFC1779) + "'\n");
3025                     }
3026                 }
3027 
3028                 if (!canonical.equals(p.getName(X500Principal.CANONICAL))) {
3029                     if (!testing || ((mask & 0x04) == 0)) {
3030                         errorMsg.append("CANONICAL: " + i);
3031                         errorMsg.append("\t\texpected: " + canonical + "'");
3032                         errorMsg.append("\treturned: '"
3033                                 + p.getName(X500Principal.CANONICAL) + "'\n");
3034                     }
3035                 }
3036 
3037             } catch (IllegalArgumentException e) {
3038                 errorMsg.append("\nIllegalArgumentException: " + i + ", for "
3039                         + rfc2253);
3040                 continue;
3041             } catch (Exception e) {
3042                 errorMsg.append("Exception: " + i + ", for " + rfc2253);
3043                 errorMsg.append("\texcep: " + e.getClass().getName() + "\n");
3044                 continue;
3045             }
3046 
3047         }
3048 
3049         if (errorMsg.length() != 0) {
3050             fail(errorMsg.toString());
3051         }
3052     }
3053 
3054     @SuppressWarnings("serial")
3055     public static class TestList extends ArrayList<Object[]> {
3056         //
3057         // TODO comment me
3058         //
add(String param, String rfc2253, String rfc1779)3059         public void add(String param, String rfc2253, String rfc1779) {
3060             add(param, rfc2253, rfc1779, (byte[]) null);
3061         }
3062 
add(String param, String rfc2253, String rfc1779, String canonical)3063         public void add(String param, String rfc2253, String rfc1779,
3064                 String canonical) {
3065             add(param, rfc2253, rfc1779, canonical, null);
3066         }
3067 
add(String param, String rfc2253, String rfc1779, byte[] encoded)3068         public void add(String param, String rfc2253, String rfc1779,
3069                 byte[] encoded) {
3070             add(new Object[] { param, rfc2253, rfc1779, null, encoded,
3071                     emptyMask });
3072         }
3073 
add(String param, String rfc2253, String rfc1779, byte[] encoded, byte mask)3074         public void add(String param, String rfc2253, String rfc1779,
3075                 byte[] encoded, byte mask) {
3076             add(new Object[] { param, rfc2253, rfc1779, null, encoded,
3077                     new byte[] { mask } });
3078         }
3079 
add(String param, String rfc2253, String rfc1779, String canonical, byte[] encoded)3080         public void add(String param, String rfc2253, String rfc1779,
3081                 String canonical, byte[] encoded) {
3082             add(new Object[] { param, rfc2253, rfc1779, canonical, encoded,
3083                     emptyMask });
3084         }
3085 
add(String param, String rfc2253, String rfc1779, String canonical, byte[] encoded, byte mask)3086         public void add(String param, String rfc2253, String rfc1779,
3087                 String canonical, byte[] encoded, byte mask) {
3088             add(new Object[] { param, rfc2253, rfc1779, canonical, encoded,
3089                     new byte[] { mask } });
3090         }
3091 
3092         //
3093         // TODO comment me
3094         //
3095 
3096         private static final byte[] emptyMask = new byte[] { 0x00 };
3097 
add(byte[] encoding, String rfc2253, String rfc1779, String canonical)3098         public void add(byte[] encoding, String rfc2253, String rfc1779,
3099                 String canonical) {
3100             add(new Object[] { encoding, rfc2253, rfc1779, canonical, emptyMask });
3101         }
3102 
add(byte[] encoding, String rfc2253, String rfc1779, String canonical, byte mask)3103         public void add(byte[] encoding, String rfc2253, String rfc1779,
3104                 String canonical, byte mask) {
3105             add(new Object[] { encoding, rfc2253, rfc1779, canonical,
3106                     new byte[] { mask } });
3107         }
3108     }
3109 
3110 
testSerializationSelf()3111     public void testSerializationSelf() throws Exception {
3112         SerializationTest.verifySelf(getSerializationData());
3113     }
3114 
testSerializationGolden()3115     public void testSerializationGolden() throws Exception {
3116         SerializationTest.verifyGolden(this, getSerializationData());
3117     }
3118 
getSerializationData()3119     private Object[] getSerializationData() {
3120         return new Object[] { new X500Principal("CN=A"),
3121                 new X500Principal("CN=A, C=B"),
3122                 new X500Principal("CN=A, CN=B + C=C") };
3123     }
3124 }
3125