You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/10/20 13:02:44 UTC

svn commit: r466061 - in /incubator/harmony/enhanced/classlib/trunk/modules/auth/src: main/java/common/javax/security/auth/kerberos/ test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/

Author: smishura
Date: Fri Oct 20 04:02:42 2006
New Revision: 466061

URL: http://svn.apache.org/viewvc?view=rev&rev=466061
Log:
Fix and regression test for HARMONY-1090 ([classlib][auth] unexpected UnsupportedOperationException for javax.security.auth.kerberos.KerberosPrincipal.KerberosPrincipal(DD))

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/javax/security/auth/kerberos/KerberosPrincipal.java
    incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/KerberosPrincipalTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/javax/security/auth/kerberos/KerberosPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/javax/security/auth/kerberos/KerberosPrincipal.java?view=diff&rev=466061&r1=466060&r2=466061
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/javax/security/auth/kerberos/KerberosPrincipal.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/javax/security/auth/kerberos/KerberosPrincipal.java Fri Oct 20 04:02:42 2006
@@ -21,9 +21,12 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.security.AccessController;
 import java.security.Principal;
+import java.security.PrivilegedAction;
 import java.util.StringTokenizer;
 
+import org.apache.harmony.auth.internal.kerberos.v5.KrbConfig;
 import org.apache.harmony.auth.internal.kerberos.v5.PrincipalName;
 import org.apache.harmony.auth.internal.nls.Messages;
 import org.apache.harmony.security.asn1.ASN1StringType;
@@ -44,6 +47,9 @@
 
     public static final int KRB_NT_UID = 5;
 
+    // default realm 
+    private static String defaultRealm;
+    
     // the full name of principal
     private transient String name;
 
@@ -63,19 +69,55 @@
         if (name == null || name.trim().length() == 0) {
             throw new IllegalArgumentException(Messages.getString("auth.23")); //$NON-NLS-1$
         }
+
         int pos = name.indexOf('@');
         if (pos != -1) {
             realm = name.substring(pos + 1, name.length());
-        } else {
-            // look for local realm name
-            throw new UnsupportedOperationException();
-        }
-        this.name = name;
 
-        // verify realm name according to RFC 1964(2.1.1 (2))
-        // check invalid chars '/', ':' and null
-        if (realm.indexOf('/') != -1 || realm.indexOf(':') != -1 || realm.indexOf(0) != -1) {
-            throw new IllegalArgumentException(Messages.getString("auth.24")); //$NON-NLS-1$
+            // verify realm name according to RFC 1964(2.1.1 (2))
+            // check invalid chars '/', ':' and null
+            if (realm.indexOf('/') != -1 || realm.indexOf(':') != -1
+                    || realm.indexOf(0) != -1) {
+                throw new IllegalArgumentException(Messages
+                        .getString("auth.24")); //$NON-NLS-1$
+            }
+
+            this.name = name;
+        } else {
+            // look for default realm name
+            // TODO sync defaultRealm update
+            if (defaultRealm == null) {
+                defaultRealm = AccessController
+                        .doPrivileged(new PrivilegedAction<String>() {
+                            public String run() {
+
+                                String v = System
+                                        .getProperty("java.security.krb5.realm"); //$NON-NLS-1$
+                                if (v == null) {
+                                    // look in config files 
+                                    KrbConfig config = null;
+                                    try {
+                                        config = KrbConfig.getSystemConfig();
+                                    } catch (IOException e) {
+                                        // ignore
+                                    }
+                                    if (config != null) {
+                                        v = config.getValue("libdefaults", //$NON-NLS-1$
+                                                "default_realm"); //$NON-NLS-1$
+                                    }
+                                }
+                                return v;
+                            }
+                        });
+
+            }
+
+            realm = defaultRealm;
+            if (realm != null) {
+                this.name = name + '@' + defaultRealm;
+            } else {
+                this.name = name;
+            }
         }
     }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/KerberosPrincipalTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/KerberosPrincipalTest.java?view=diff&rev=466061&r1=466060&r2=466061
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/KerberosPrincipalTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/kerberos/KerberosPrincipalTest.java Fri Oct 20 04:02:42 2006
@@ -17,15 +17,31 @@
 
 package org.apache.harmony.auth.tests.javax.security.auth.kerberos;
 
+import java.io.File;
+import java.io.FileOutputStream;
+
 import javax.security.auth.kerberos.KerberosPrincipal;
 
 import junit.framework.TestCase;
 
+import org.apache.harmony.auth.tests.support.TestUtils;
+
+import tests.support.Support_Exec;
+
 /**
  * Tests KerberosPrincipal class implementation.
  */
 public class KerberosPrincipalTest extends TestCase {
 
+    // system property for specifying the default realm
+    private static final String KRB5_REALM_SYS_PROP = "java.security.krb5.realm";
+
+    // system property for specifying the default kdc
+    private static final String KRB5_KDC_SYS_PROP = "java.security.krb5.kdc";
+
+    // system property for specifying the default config file
+    private static final String KRB5_CONF_SYS_PROP = "java.security.krb5.conf";
+
     /**
      * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
      *        java.lang.String)
@@ -61,7 +77,6 @@
         assertEquals("apache.org", principal.getRealm());
         assertEquals(KerberosPrincipal.KRB_NT_PRINCIPAL, principal
                 .getNameType());
-
     }
 
     /**
@@ -98,6 +113,109 @@
         assertEquals("name@apache.org", principal.getName());
         assertEquals("apache.org", principal.getRealm());
         assertEquals(KerberosPrincipal.KRB_NT_UNKNOWN, principal.getNameType());
+    }
+
+    /**
+     * @tests javax.security.auth.kerberos.KerberosPrincipal#KerberosPrincipal(
+     *        java.lang.String)
+     */
+    public void test_Ctor_defaultRealm() throws Exception {
+
+        // test: if the input name has no realm part then default realm is used.
+        //
+        // the test forks a separate VM for each case because   
+        // default realm value is cashed
+
+        // test: default realm is unset (has null value)
+        Support_Exec.execJava(new String[] { DefaultRealm_NullValue.class
+                .getName() }, null, true);
+
+        // test: default realm name is specified via system property
+        Support_Exec.execJava(new String[] { DefaultRealm_SystemProperty.class
+                .getName() }, null, true);
+
+        // test: default realm name is specified in config file
+        Support_Exec.execJava(new String[] { DefaultRealm_ConfigFile.class
+                .getName() }, null, true);
+    }
+
+    /**
+     * Test: default realm is unset
+     */
+    public static class DefaultRealm_NullValue {
+
+        // Regression for HARMONY-1090
+        public static void main(String[] av) throws Exception {
+
+            // clear system properties
+            TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
+            TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
+
+            // point to empty config file
+            File f = File.createTempFile("krb5", "conf");
+            f.deleteOnExit();
+            TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
+                    .getCanonicalPath());
+
+            // test: default realm value is 'null'
+            KerberosPrincipal principal = new KerberosPrincipal("name");
+            assertEquals("name", principal.getName());
+            assertNull(principal.getRealm());
+        }
+    }
+
+    /**
+     * Tests: default realm name is specified via system property
+     */
+    public static class DefaultRealm_SystemProperty {
+        public static void main(String[] av) {
+
+            String testRealm = "This_is_test_realm";
+
+            System.setProperty(KRB5_REALM_SYS_PROP, testRealm);
+            System.setProperty(KRB5_KDC_SYS_PROP, "some_value");
+
+            // test
+            KerberosPrincipal principal = new KerberosPrincipal("name");
+            assertEquals("name@" + testRealm, principal.getName());
+            assertEquals(testRealm, principal.getRealm());
+
+            // test: default realm value is cashed 
+            // change system property value 
+            System.setProperty(KRB5_REALM_SYS_PROP,
+                    "Another_test_realm");
+            principal = new KerberosPrincipal("name");
+            assertEquals("name@" + testRealm, principal.getName());
+            assertEquals(testRealm, principal.getRealm());
+        }
+    }
+
+    /**
+     * Tests: default realm name is specified in config file
+     */
+    public static class DefaultRealm_ConfigFile {
+        public static void main(String[] av) throws Exception {
+
+            // clear system properties
+            TestUtils.setSystemProperty(KRB5_REALM_SYS_PROP, null);
+            TestUtils.setSystemProperty(KRB5_KDC_SYS_PROP, null);
+
+            // point to config file
+            File f = File.createTempFile("krb5", "conf");
+            f.deleteOnExit();
+            FileOutputStream out = new FileOutputStream(f);
+            out.write("[libdefaults]\n".getBytes());
+            out.write("         default_realm = MY.TEST_REALM".getBytes());
+            out.close();
+            
+            TestUtils.setSystemProperty(KRB5_CONF_SYS_PROP, f
+                    .getCanonicalPath());
+
+            // test
+            KerberosPrincipal principal = new KerberosPrincipal("name");
+            assertEquals("name@MY.TEST_REALM", principal.getName());
+            assertEquals("MY.TEST_REALM", principal.getRealm());
+        }
     }
 
     /**