harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r447420 - in /incubator/harmony/enhanced/classlib/trunk/modules/auth/src: main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java
Date Mon, 18 Sep 2006 14:47:50 GMT
Author: smishura
Date: Mon Sep 18 07:47:50 2006
New Revision: 447420

URL: http://svn.apache.org/viewvc?view=rev&rev=447420
Log:
Default values for kdc and realm are set by system properties:
- java.security.krb5.kdc
- java.security.krb5.realm

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java
    incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java?view=diff&rev=447420&r1=447419&r2=447420
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/Krb5LoginModule.java
Mon Sep 18 07:47:50 2006
@@ -30,11 +30,14 @@
 
 public class Krb5LoginModule implements LoginModule {
 
-    private static final String PRINCIPAL = "cname"; //$NON-NLS-1$
+    // default kdc server
+    private static final String DEFAULT_KDC = "java.security.krb5.kdc"; //$NON-NLS-1$
 
-    private static final String REALM = "realm"; //$NON-NLS-1$
+    // default realm
+    private static final String DEFAULT_REALM = "java.security.krb5.realm"; //$NON-NLS-1$
 
-    private static final String KDC = "kdc"; //$NON-NLS-1$
+    // client's principal identifier name
+    private static final String PRINCIPAL = "principal";//$NON-NLS-1$
 
     private Map<String, ?> options;
 
@@ -56,11 +59,21 @@
     }
 
     public boolean login() throws LoginException {
-        String kdc = (String) options.get(KDC);
+
+        //TODO put in doPrivileged
+        String kdc = System.getProperty(DEFAULT_KDC);
+        String realm = System.getProperty(DEFAULT_REALM);
+        if (kdc == null && realm != null || kdc != null && realm == null)
{
+            // both properties should be set or unset together
+            throw new LoginException();//FIXME message
+        } else if (kdc == null && realm == null) {
+            // reading config from configuration file 'krb5.conf'
+            throw new LoginException();//FIXME not yet implemented
+        }
+
         String name = (String) options.get(PRINCIPAL);
-        String realm = (String) options.get(REALM);
 
-        if (name == null || realm == null || kdc == null) {
+        if (name == null) {
             throw new LoginException();//FIXME check params
         }
 
@@ -80,7 +93,7 @@
         try {
             Ticket ticket = KrbClient.doAS(InetAddress.getByName(kdc), port,
                     cname, realm, krbtgt);
-            
+
             return true; //FIXME 
         } catch (Exception e) {
             LoginException ex = new LoginException();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java?view=diff&rev=447420&r1=447419&r2=447420
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/Krb5LoginModuleTest.java
Mon Sep 18 07:47:50 2006
@@ -21,41 +21,102 @@
 import java.net.DatagramSocket;
 import java.util.TreeMap;
 
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.login.LoginException;
 
 import junit.framework.TestCase;
 
 import org.apache.harmony.auth.module.Krb5LoginModule;
 import org.apache.harmony.auth.tests.internal.kerberos.v5.KerberosErrorMessageTest;
+import org.apache.harmony.auth.tests.support.TestUtils;
 
 public class Krb5LoginModuleTest extends TestCase {
 
+    // default kdc server
+    private static final String ENV_KDC = "java.security.krb5.kdc";
+
+    // default realm
+    private static final String ENV_REALM = "java.security.krb5.realm";
+
+    // old value of 'java.security.krb5.kdc' system property
+    private String kdc;
+
+    // old value of 'java.security.krb5.realm' system property
+    private String realm;
+
+    // embedded server
     private KrbServer server;
 
+    // module options
     private TreeMap<String, String> options = new TreeMap<String, String>();
 
+    /**
+     * Sets system env. properties and optionaly starts local mock server
+     */
     protected void setUp() throws Exception {
 
-        String kdc = System.getProperty("java.security.krb5.kdc");
+        // save old system properties
+        kdc = System.getProperty(ENV_KDC);
+        realm = System.getProperty(ENV_REALM);
+
         if (kdc == null) {
             // run test with embedded server
             server = new KrbServer();
 
             server.start();
             while (server.port == 0) {
+                // wait until server open datagram socket 
             }
 
-            options.put("kdc", "localhost:" + server.port);
-        } else {
-            // run test with external server
-            options.put("kdc", kdc);
-        }
+            System.setProperty(ENV_KDC, "localhost:" + server.port);
+        } // else: run test with external server
+
+        System.setProperty(ENV_REALM, "MY.REALM");
     }
 
+    /**
+     * Shuts down local server and restore system env. properties
+     */
     protected void tearDown() throws Exception {
         if (server != null) {
+            // shut down local server
             server.interrupt();
         }
+
+        // restore env. variables
+        TestUtils.setSystemProperty(ENV_KDC, kdc);
+        TestUtils.setSystemProperty(ENV_REALM, realm);
+    }
+
+    /**
+     * TODO
+     */
+    public void test_Config() throws Exception {
+
+        // create loging module for testing
+        Krb5LoginModule module = new Krb5LoginModule();
+        module.initialize(null, new MockCallbackHandler(), null, options);
+
+        // case 1: unset 'kdc' and set 'real' sys.props
+        TestUtils.setSystemProperty(ENV_KDC, null);
+        TestUtils.setSystemProperty(ENV_REALM, "some_value");
+        try {
+            module.login();
+            fail("No expected LoginException");
+        } catch (LoginException e) {
+        }
+
+        // case 2: set 'kdc' and unset 'real' sys.props
+        TestUtils.setSystemProperty(ENV_KDC, "some_value");
+        TestUtils.setSystemProperty(ENV_REALM, null);
+        try {
+            module.login();
+            fail("No expected LoginException");
+        } catch (LoginException e) {
+        }
+
+        //TODO: test reading config from configuration file 'krb5.conf'
     }
 
     /**
@@ -69,16 +130,26 @@
 
         Krb5LoginModule module = new Krb5LoginModule();
 
-        options.put("cname", "no_such_user");
-        options.put("realm", "MY.REALM");
+        options.put("principal", "no_such_user");
 
         module.initialize(null, null, null, options);
-
         try {
             module.login();
             fail("No expected LoginException");
         } catch (LoginException e) {
             System.out.println(e);
+        }
+    }
+
+    /**
+     * Mock callback handler
+     */
+    static class MockCallbackHandler implements CallbackHandler {
+
+        public MockCallbackHandler() {
+        }
+
+        public void handle(Callback[] callbacks) {
         }
     }
 



Mime
View raw message