Home | History | Annotate | Download | only in kerberos
      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.auth.tests.javax.security.auth.kerberos;
     19 
     20 import java.io.File;
     21 import java.io.FileOutputStream;
     22 
     23 import javax.security.auth.kerberos.KerberosPrincipal;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.apache.harmony.auth.tests.support.TestUtils;
     28 
     29 import tests.support.Support_Exec;
     30 
     31 /**
     32  * Tests KerberosPrincipal class implementation.
     33  */
     34 public class KerberosPrincipalTest extends TestCase {
     35 
     36     // system property for specifying the default realm
     37     private static final String KRB5_REALM_SYS_PROP = "java.security.krb5.realm";
     38 
     39     // system property for specifying the default kdc
     40     private static final String KRB5_KDC_SYS_PROP = "java.security.krb5.kdc";
     41 
     42     // system property for specifying the default config file
     43     private static final String KRB5_CONF_SYS_PROP = "java.security.krb5.conf";
     44 
     45     /**
     46      * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
     47      *        java.lang.String)
     48      */
     49     public void test_Ctor1() {
     50 
     51         // null value is invalid
     52         try {
     53             new KerberosPrincipal(null);
     54             fail("No expected IllegalArgumentException for null");
     55         } catch (IllegalArgumentException e) {
     56         }
     57 
     58         // testing illegal kerberos principal names
     59         String[] illegalNames = new String[] { "bbb@a:a.com", // ':' char
     60                 "bbb@a/a.com", // '/' char
     61                 "bbb@a\0a.com",// null char
     62                 "@/" // Regression for HARMONY-770
     63         };
     64         for (String illegalName : illegalNames) {
     65             try {
     66                 new KerberosPrincipal(illegalName);
     67 
     68                 fail("No expected IllegalArgumentException for: " + illegalName);
     69             } catch (IllegalArgumentException e) {
     70             }
     71         }
     72 
     73         // valid values
     74         KerberosPrincipal principal = new KerberosPrincipal("name (at) apache.org");
     75 
     76         assertEquals("name (at) apache.org", principal.getName());
     77         assertEquals("apache.org", principal.getRealm());
     78         assertEquals(KerberosPrincipal.KRB_NT_PRINCIPAL, principal
     79                 .getNameType());
     80 
     81         // Regression for HARMONY-1182
     82         principal = new KerberosPrincipal("file:C://@apache.org");
     83         assertEquals("file:C:@apache.org", principal.getName());
     84     }
     85 
     86     /**
     87      * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
     88      *        java.lang.String, int)
     89      */
     90     public void test_Ctor2() {
     91 
     92         // null value is invalid
     93         try {
     94             new KerberosPrincipal(null, KerberosPrincipal.KRB_NT_UNKNOWN);
     95             fail("No expected IllegalArgumentException for null");
     96         } catch (IllegalArgumentException e) {
     97         }
     98 
     99         // '-1' nameType value is invalid
    100         try {
    101             new KerberosPrincipal("name (at) apache.org", -1);
    102             fail("No expected IllegalArgumentException for -1 nameType value");
    103         } catch (IllegalArgumentException e) {
    104         }
    105 
    106         // '6' nameType value is invalid
    107         try {
    108             new KerberosPrincipal("name (at) apache.org", 6);
    109             fail("No expected IllegalArgumentException 6 nameType value");
    110         } catch (IllegalArgumentException e) {
    111         }
    112 
    113         // valid values
    114         KerberosPrincipal principal = new KerberosPrincipal("name (at) apache.org",
    115                 KerberosPrincipal.KRB_NT_UNKNOWN);
    116 
    117         assertEquals("name (at) apache.org", principal.getName());
    118         assertEquals("apache.org", principal.getRealm());
    119         assertEquals(KerberosPrincipal.KRB_NT_UNKNOWN, principal.getNameType());
    120     }
    121 
    122     /**
    123      * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
    124      *        java.lang.String)
    125      */
    126     public void test_Ctor_defaultRealm() throws Exception {
    127 
    128         // test: if the input name has no realm part then default realm is used.
    129         //
    130         // the test forks a separate VM for each case because
    131         // default realm value is cashed
    132 
    133         // test: default realm is unset (has null value)
    134         Support_Exec.execJava(new String[] { DefaultRealm_NullValue.class
    135                 .getName() }, null, true);
    136 
    137         // test: default realm name is specified via system property
    138         Support_Exec.execJava(new String[] { DefaultRealm_SystemProperty.class
    139                 .getName() }, null, true);
    140 
    141         // test: default realm name is specified in config file
    142         Support_Exec.execJava(new String[] { DefaultRealm_ConfigFile.class
    143                 .getName() }, null, true);
    144     }
    145 
    146     /**
    147      * Test: default realm is unset
    148      */
    149     public static class DefaultRealm_NullValue {
    150 
    151         // Regression for HARMONY-1090
    152         public static void main(String[] av) throws Exception {
    153 
    154             // clear system properties
    155             TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
    156             TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
    157 
    158             // point to empty config file
    159             File f = File.createTempFile("krb5", "conf");
    160             f.deleteOnExit();
    161             TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
    162                     .getCanonicalPath());
    163 
    164             // test: default realm value is 'null'
    165             KerberosPrincipal principal = new KerberosPrincipal("name");
    166             assertEquals("name", principal.getName());
    167             assertNull(principal.getRealm());
    168         }
    169     }
    170 
    171     /**
    172      * Tests: default realm name is specified via system property
    173      */
    174     public static class DefaultRealm_SystemProperty {
    175         public static void main(String[] av) {
    176 
    177             // case 1: unset 'kdc' and set 'realm'
    178             TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, "some_value");
    179             TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
    180             try {
    181                 new KerberosPrincipal("name");
    182                 fail("No expected IllegalArgumentException");
    183             } catch (IllegalArgumentException e) {
    184             } finally {
    185                 TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
    186             }
    187 
    188             // case 2: set 'kdc' and unset 'realm' sys.props
    189             TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, "some_value");
    190             try {
    191                 new KerberosPrincipal("name");
    192                 fail("No expected IllegalArgumentException");
    193             } catch (IllegalArgumentException e) {
    194             } finally {
    195                 TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
    196             }
    197 
    198             String testRealm = "This_is_test_realm";
    199 
    200             System.setProperty(KRB5_REALM_SYS_PROP, testRealm);
    201             System.setProperty(KRB5_KDC_SYS_PROP, "some_value");
    202 
    203             // test
    204             KerberosPrincipal principal = new KerberosPrincipal("name");
    205             assertEquals("name@" + testRealm, principal.getName());
    206             assertEquals(testRealm, principal.getRealm());
    207 
    208             // test: default realm value is cashed
    209             // change system property value
    210             System.setProperty(KRB5_REALM_SYS_PROP,
    211                     "Another_test_realm");
    212             principal = new KerberosPrincipal("name");
    213             assertEquals("name@" + testRealm, principal.getName());
    214             assertEquals(testRealm, principal.getRealm());
    215         }
    216     }
    217 
    218     /**
    219      * Tests: default realm name is specified in config file
    220      */
    221     public static class DefaultRealm_ConfigFile {
    222         public static void main(String[] av) throws Exception {
    223 
    224             // clear system properties
    225             TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
    226             TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
    227 
    228             // point to config file
    229             File f = File.createTempFile("krb5", "conf");
    230             f.deleteOnExit();
    231             FileOutputStream out = new FileOutputStream(f);
    232             out.write("[libdefaults]\n".getBytes());
    233             out.write("         default_realm = MY.TEST_REALM".getBytes());
    234             out.close();
    235 
    236             TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
    237                     .getCanonicalPath());
    238 
    239             // test
    240             KerberosPrincipal principal = new KerberosPrincipal("name");
    241             assertEquals("name (at) MY.TEST_REALM", principal.getName());
    242             assertEquals("MY.TEST_REALM", principal.getRealm());
    243         }
    244     }
    245 
    246     /**
    247      * @tests javax.security.auth.kerberos.KerberosPrincipal#hashCode()
    248      */
    249     public void test_hashCode() {
    250         KerberosPrincipal principal = new KerberosPrincipal("name (at) apache.org");
    251 
    252         assertEquals(principal.getName().hashCode(), principal.hashCode());
    253     }
    254 
    255     /**
    256      * @tests javax.security.auth.kerberos.KerberosPrincipal#toString()
    257      */
    258     public void test_toString() {
    259 
    260         String name = "name (at) apache.org";
    261 
    262         KerberosPrincipal principal = new KerberosPrincipal(name);
    263 
    264         assertEquals(name, principal.toString());
    265     }
    266 
    267     /**
    268      * @tests javax.security.auth.kerberos.KerberosPrincipal#equals(Object)
    269      */
    270     public void test_equals() {
    271 
    272         KerberosPrincipal p = new KerberosPrincipal("A@B");
    273 
    274         assertTrue(p.equals(new KerberosPrincipal("A@B")));
    275         assertFalse(p.equals(new KerberosPrincipal("A (at) B.org")));
    276         assertFalse(p.equals(new KerberosPrincipal("aaa@B")));
    277         assertFalse(p.equals(new KerberosPrincipal("A@B",
    278                 KerberosPrincipal.KRB_NT_UID)));
    279 
    280         // Regression for HARMONY-744
    281         assertFalse(p.equals(null));
    282         assertFalse(p.equals(new Object()));
    283     }
    284 }
    285