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