Home | History | Annotate | Download | only in auth
      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 tests.api.javax.security.auth;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import javax.security.auth.x500.X500Principal;
     23 
     24 import java.io.ByteArrayInputStream;
     25 import java.io.InputStream;
     26 import java.security.cert.CertificateFactory;
     27 import java.security.cert.X509Certificate;
     28 import org.apache.harmony.security.tests.support.cert.TestUtils;
     29 
     30 /**
     31  * Tests for <code>X500Principal</code> class constructors and methods.
     32  *
     33  */
     34 public class X500PrincipalTest extends TestCase {
     35 
     36     /**
     37      * javax.security.auth.x500.X500Principal#X500Principal(String name)
     38      */
     39     public void test_X500Principal_01() {
     40         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
     41 
     42         try {
     43             X500Principal xpr = new X500Principal(name);
     44             assertNotNull("Null object returned", xpr);
     45             String resName = xpr.getName();
     46             assertEquals(name, resName);
     47         } catch (Exception e) {
     48             fail("Unexpected exception: " + e);
     49         }
     50 
     51         try {
     52             X500Principal xpr = new X500Principal((String)null);
     53             fail("NullPointerException wasn't thrown");
     54         } catch (NullPointerException npe) {
     55         } catch (Exception e) {
     56             fail(e + " was thrown instead of NullPointerException");
     57         }
     58 
     59         try {
     60             X500Principal xpr = new X500Principal("X500PrincipalName");
     61             fail("IllegalArgumentException wasn't thrown");
     62         } catch (IllegalArgumentException npe) {
     63         } catch (Exception e) {
     64             fail(e + " was thrown instead of IllegalArgumentException");
     65         }
     66     }
     67 
     68     /**
     69      * javax.security.auth.x500.X500Principal#X500Principal(InputStream is)
     70      */
     71     public void test_X500Principal_02() {
     72         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
     73         byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
     74         ByteArrayInputStream is = new ByteArrayInputStream(ba);
     75         InputStream isNull = null;
     76 
     77         try {
     78             X500Principal xpr = new X500Principal(is);
     79             assertNotNull("Null object returned", xpr);
     80             byte[] resArray = xpr.getEncoded();
     81             assertEquals(ba.length, resArray.length);
     82         } catch (Exception e) {
     83             fail("Unexpected exception: " + e);
     84         }
     85 
     86         try {
     87             X500Principal xpr = new X500Principal(isNull);
     88             fail("NullPointerException wasn't thrown");
     89         } catch (NullPointerException npe) {
     90         } catch (Exception e) {
     91             fail(e + " was thrown instead of NullPointerException");
     92         }
     93 
     94         is = new ByteArrayInputStream(name.getBytes());
     95         try {
     96             X500Principal xpr = new X500Principal(is);
     97             fail("IllegalArgumentException wasn't thrown");
     98         } catch (IllegalArgumentException npe) {
     99         } catch (Exception e) {
    100             fail(e + " was thrown instead of IllegalArgumentException");
    101         }
    102     }
    103 
    104     /**
    105      * javax.security.auth.x500.X500Principal#X500Principal(byte[] name)
    106      */
    107     public void test_X500Principal_03() {
    108         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    109         byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
    110         byte[] baNull = null;
    111 
    112         try {
    113             X500Principal xpr = new X500Principal(ba);
    114             assertNotNull("Null object returned", xpr);
    115             byte[] resArray = xpr.getEncoded();
    116             assertEquals(ba.length, resArray.length);
    117         } catch (Exception e) {
    118             fail("Unexpected exception: " + e);
    119         }
    120 
    121         try {
    122             X500Principal xpr = new X500Principal(baNull);
    123             fail("IllegalArgumentException wasn't thrown");
    124         } catch (IllegalArgumentException npe) {
    125         } catch (Exception e) {
    126             fail(e + " was thrown instead of IllegalArgumentException");
    127         }
    128 
    129         ba = name.getBytes();
    130         try {
    131             X500Principal xpr = new X500Principal(ba);
    132             fail("IllegalArgumentException wasn't thrown");
    133         } catch (IllegalArgumentException npe) {
    134         } catch (Exception e) {
    135             fail(e + " was thrown instead of IllegalArgumentException");
    136         }
    137     }
    138 
    139     /**
    140      * javax.security.auth.x500.X500Principal#getName()
    141      */
    142     public void test_getName() {
    143         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    144         X500Principal xpr = new X500Principal(name);
    145         try {
    146             String resName = xpr.getName();
    147             assertEquals(name, resName);
    148         } catch (Exception e) {
    149             fail("Unexpected exception: " + e);
    150         }
    151     }
    152 
    153     /**
    154      * javax.security.auth.x500.X500Principal#getName(String format)
    155      */
    156     public void test_getName_Format() {
    157         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    158         String expectedName = "cn=duke,ou=javasoft,o=sun microsystems,c=us";
    159         X500Principal xpr = new X500Principal(name);
    160         try {
    161             String resName = xpr.getName(X500Principal.CANONICAL);
    162             assertEquals(expectedName, resName);
    163         } catch (Exception e) {
    164             fail("Unexpected exception: " + e);
    165         }
    166 
    167         expectedName = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
    168         try {
    169             String resName = xpr.getName(X500Principal.RFC1779);
    170             assertEquals(expectedName, resName);
    171         } catch (Exception e) {
    172             fail("Unexpected exception: " + e);
    173         }
    174 
    175         try {
    176             String resName = xpr.getName(X500Principal.RFC2253);
    177             assertEquals(name, resName);
    178         } catch (Exception e) {
    179             fail("Unexpected exception: " + e);
    180         }
    181 
    182         try {
    183             String resName = xpr.getName(null);
    184             fail("IllegalArgumentException  wasn't thrown");
    185         } catch (IllegalArgumentException  iae) {
    186         }
    187         try {
    188             String resName = xpr.getName("RFC2254");
    189             fail("IllegalArgumentException  wasn't thrown");
    190         } catch (IllegalArgumentException  iae) {
    191         }
    192     }
    193 
    194     /**
    195      * javax.security.auth.x500.X500Principal#hashCode()
    196      */
    197     public void test_hashCode() {
    198         String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    199         X500Principal xpr = new X500Principal(name);
    200         try {
    201             int res = xpr.hashCode();
    202             assertNotNull(res);
    203         } catch (Exception e) {
    204             fail("Unexpected exception: " + e);
    205         }
    206     }
    207 
    208     /**
    209      * javax.security.auth.x500.X500Principal#toString()
    210      */
    211     public void test_toString() {
    212         String name = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
    213         X500Principal xpr = new X500Principal(name);
    214         try {
    215             String res = xpr.toString();
    216             assertNotNull(res);
    217             assertEquals(name, res);
    218         } catch (Exception e) {
    219             fail("Unexpected exception: " + e);
    220         }
    221     }
    222 
    223     /**
    224      * javax.security.auth.x500.X500Principal#getEncoded()
    225      */
    226     public void test_getEncoded() {
    227         byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
    228         X500Principal xpr = new X500Principal(ba);
    229         try {
    230             byte[] res = xpr.getEncoded();
    231             assertNotNull(res);
    232             assertEquals(ba.length, res.length);
    233         } catch (Exception e) {
    234             fail("Unexpected exception: " + e);
    235         }
    236     }
    237 
    238     /**
    239      * javax.security.auth.x500.X500Principal#equals(Object o)
    240      */
    241     public void test_equals() {
    242         String name1 = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US";
    243         String name2 = "cn=duke,ou=javasoft,o=sun microsystems,c=us";
    244         String name3 = "CN=Alex Astapchuk, OU=SSG, O=Intel ZAO, C=RU";
    245         X500Principal xpr1 = new X500Principal(name1);
    246         X500Principal xpr2 = new X500Principal(name2);
    247         X500Principal xpr3 = new X500Principal(name3);
    248         try {
    249             assertTrue("False returned", xpr1.equals(xpr2));
    250             assertFalse("True returned", xpr1.equals(xpr3));
    251         } catch (Exception e) {
    252             fail("Unexpected exception: " + e);
    253         }
    254     }
    255 
    256     private byte[] getByteArray(byte[] array) {
    257         byte[] x = null;
    258         try {
    259             ByteArrayInputStream is = new ByteArrayInputStream(array);
    260             CertificateFactory cf = CertificateFactory.getInstance("X.509");
    261             X509Certificate cert = (X509Certificate)cf.generateCertificate(is);
    262             X500Principal xx = cert.getIssuerX500Principal();
    263             x = xx.getEncoded();
    264         } catch (Exception e) {
    265             return null;
    266         }
    267         return x;
    268     }
    269 }
    270 
    271