FELIX-3935 - Testcases for JAAS integration

Adding testcases for following scenarios
-- passing of OSGi config to JAAS LoginModule options
-- Ordering of LoginModule based on config ranking

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1459177 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jaas/src/test/java/org/apache/felix/jaas/integration/ITJaasWithConfigBasedLoginModule.java b/jaas/src/test/java/org/apache/felix/jaas/integration/ITJaasWithConfigBasedLoginModule.java
index 7b76e05..b67fc67 100644
--- a/jaas/src/test/java/org/apache/felix/jaas/integration/ITJaasWithConfigBasedLoginModule.java
+++ b/jaas/src/test/java/org/apache/felix/jaas/integration/ITJaasWithConfigBasedLoginModule.java
@@ -19,11 +19,22 @@
 
 package org.apache.felix.jaas.integration;
 
-import java.io.IOException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.streamBundle;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
 
 import javax.inject.Inject;
 import javax.security.auth.Subject;
 import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.login.AppConfigurationEntry;
 import javax.security.auth.login.Configuration;
 import javax.security.auth.login.LoginContext;
 import javax.security.auth.login.LoginException;
@@ -39,11 +50,6 @@
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-import static org.ops4j.pax.exam.CoreOptions.composite;
-import static org.ops4j.pax.exam.CoreOptions.streamBundle;
-
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerClass.class)
 public class ITJaasWithConfigBasedLoginModule extends JaasTestBase
@@ -123,4 +129,92 @@
 
         }
     }
+
+    /**
+     * Validates that OSGi config do gets passed as part of options to the LoginModule
+     */
+    @Test
+    public void testJaasConfigPassing() throws Exception {
+        String realmName = name.getMethodName();
+        createConfigSpiConfig();
+
+        //1. Create sample config
+        org.osgi.service.cm.Configuration config =
+                ca.createFactoryConfiguration("org.apache.felix.jaas.Configuration.factory",null);
+        Dictionary<String,Object> p = new Hashtable<String, Object>();
+        p.put("jaas.classname","org.apache.felix.jaas.integration.sample1.ConfigLoginModule");
+        p.put("jaas.realmName", realmName);
+
+        //Following passed config gets validated in
+        //org.apache.felix.jaas.integration.sample1.ConfigLoginModule.validateConfig()
+        p.put("validateConfig", Boolean.TRUE);
+        p.put("key0", "val0");
+        p.put("key1", "val1");
+        p.put("key2", "val2");
+
+        //Override the value directly passed in config via options value explicitly
+        p.put("jaas.options", new String[]{"key3=val3", "key4=val4", "key0=valNew"});
+        config.update(p);
+
+        delay();
+
+        //2. Validate the login passes with this config. LoginModule would validate
+        //the config also
+        CallbackHandler handler = new SimpleCallbackHandler("foo","foo");
+        Configuration jaasConfig = Configuration.getInstance("JavaLoginConfig",null,"FelixJaasProvider");
+
+        Subject s = new Subject();
+        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        try
+        {
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            LoginContext lc = new LoginContext(realmName,s,handler,jaasConfig);
+            lc.login();
+        }
+        finally
+        {
+            Thread.currentThread().setContextClassLoader(cl);
+        }
+
+        assertFalse(s.getPrincipals().isEmpty());
+    }
+
+
+    @Test
+    public void testJaasConfigOrderedViaRanking() throws Exception {
+        String realmName = name.getMethodName();
+        createConfigSpiConfig();
+        List<Integer> ranks = Arrays.asList(1,2,3,4,5,6);
+        Collections.shuffle(ranks);
+
+        //1. Create LoginModule config with random rankings
+        for(Integer i : ranks)
+        {
+            org.osgi.service.cm.Configuration config =
+                    ca.createFactoryConfiguration("org.apache.felix.jaas.Configuration.factory",null);
+            Dictionary<String,Object> p = new Hashtable<String, Object>();
+            p.put("jaas.classname","org.apache.felix.jaas.integration.sample1.ConfigLoginModule");
+            p.put("jaas.realmName", realmName);
+            p.put("jaas.ranking", i);
+            p.put("order", i);
+
+            config.update(p);
+        }
+
+        delay();
+
+        Configuration jaasConfig = Configuration.getInstance("JavaLoginConfig",null,"FelixJaasProvider");
+        AppConfigurationEntry[] entries = jaasConfig.getAppConfigurationEntry(realmName);
+
+        assertEquals("No of entries does not match the no of created",ranks.size(),entries.length);
+
+        //Entries would be sorted via ranking. Higher ranking comes first
+        int ranking = 6;
+        for(AppConfigurationEntry e : entries){
+            Integer order = (Integer) e.getOptions().get("order");
+            assertEquals(ranking--,order.intValue());
+        }
+
+    }
+
 }
diff --git a/jaas/src/test/java/org/apache/felix/jaas/integration/sample1/ConfigLoginModule.java b/jaas/src/test/java/org/apache/felix/jaas/integration/sample1/ConfigLoginModule.java
index a2964a2..904ef6e 100644
--- a/jaas/src/test/java/org/apache/felix/jaas/integration/sample1/ConfigLoginModule.java
+++ b/jaas/src/test/java/org/apache/felix/jaas/integration/sample1/ConfigLoginModule.java
@@ -77,11 +77,30 @@
         char[] password = ((PasswordCallback) callbacks[1]).getPassword();
 
         boolean result = Arrays.equals(name.toCharArray(), password);
+        if(result){
+            result = validateConfig();
+        }
         succeeded = result;
         this.name = name;
         return result;
     }
 
+    /*
+    org.apache.felix.jaas.integration.ITJaasWithConfigBasedLoginModule.testJaasConfigPassing
+     */
+    private boolean validateConfig() {
+        if(!Boolean.TRUE.equals(options.get("validateConfig"))){
+            return true;
+        }
+        boolean result = true;
+        result &= "val1".equals(options.get("key1"));
+        result &= "val2".equals(options.get("key2"));
+        result &= "val3".equals(options.get("key3"));
+        result &= "val4".equals(options.get("key4"));
+        result &= "valNew".equals(options.get("key0"));
+        return result;
+    }
+
     @Override
     public boolean commit() throws LoginException
     {