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