Unit and integration tests
authorgilbert <gilbert@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 23 Aug 2005 14:28:20 +0000 (14:28 +0000)
committergilbert <gilbert@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 23 Aug 2005 14:28:20 +0000 (14:28 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@1821 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

tests/edu/internet2/middleware/shibboleth/serviceprovider/AuthenticationFilterTest.java [new file with mode: 0644]
tests/edu/internet2/middleware/shibboleth/serviceprovider/SPTestCase.java
tests/edu/internet2/middleware/shibboleth/serviceprovider/SSOTest.java [new file with mode: 0644]
tests/edu/internet2/middleware/shibboleth/serviceprovider/TestContextInitializer.java

diff --git a/tests/edu/internet2/middleware/shibboleth/serviceprovider/AuthenticationFilterTest.java b/tests/edu/internet2/middleware/shibboleth/serviceprovider/AuthenticationFilterTest.java
new file mode 100644 (file)
index 0000000..f673d45
--- /dev/null
@@ -0,0 +1,93 @@
+package edu.internet2.middleware.shibboleth.serviceprovider;
+import java.io.File;
+
+import com.mockrunner.mock.web.MockFilterConfig;
+import com.mockrunner.mock.web.MockHttpServletRequest;
+import com.mockrunner.mock.web.MockHttpServletResponse;
+import com.mockrunner.mock.web.MockServletContext;
+import com.mockrunner.mock.web.WebMockObjectFactory;
+import com.mockrunner.servlet.ServletTestModule;
+
+import edu.internet2.middleware.shibboleth.resource.AuthenticationFilter;
+import edu.internet2.middleware.shibboleth.resource.FilterSupport;
+import edu.internet2.middleware.shibboleth.resource.FilterSupport.RMAppInfo;
+
+/**
+ * Use Mockrunner to test the Shib Filter
+ */
+public class AuthenticationFilterTest extends SPTestCase {
+       
+       // The Factory creates the Request, Response, Session, etc.
+       WebMockObjectFactory factory = new WebMockObjectFactory();
+    
+    // The TestModule runs the Servlet and Filter methods in the simulated container
+       ServletTestModule testModule = new ServletTestModule(factory);
+    
+    // Now simulated Servlet API objects
+       MockServletContext servletContext= new MockServletContext();
+       MockFilterConfig filterConfig= factory.getMockFilterConfig();
+    MockHttpServletResponse response = factory.getMockResponse();
+    MockHttpServletRequest request = factory.getMockRequest();
+       
+       // Filter objects
+       private AuthenticationFilter filter;
+       
+       // SP configuration objects
+       private FilterSupport service;
+       private RMAppInfo rmAppInfo;
+
+       protected void setUp() throws Exception {
+               super.setUp();
+               
+        // ServletContext (argument to Filters and Servlets)
+               servletContext.setServletContextName("dummy Servlet Context");
+        servletContext.setInitParameter("requireId", ".+/test.+");
+               
+               // The FilterConfig (argument to Filter init)
+               filterConfig.setupServletContext(servletContext);
+               filterConfig.setFilterName("Test Filter under JUnit");
+               
+               // Create instance of Filter class, add to chain, call its init()
+               filter = (AuthenticationFilter) testModule.createFilter(AuthenticationFilter.class);
+        
+               // Initialize an SP Context and Confg
+               String configFileName = new File("data/spconfig.xml").toURI().toString();
+               initServiceProvider(configFileName); 
+               
+               // Plug an instance of FilterSupportImpl into the Filter
+               service = new FilterSupportImpl();
+               AuthenticationFilter.setFilterSupport(service);
+
+        // Get our own copy of SP Config info for Assert statements
+               rmAppInfo = service.getRMAppInfo("default");
+
+        request.setRemoteAddr("127.0.0.1");
+        request.setContextPath("/secure");
+        request.setProtocol("HTTP/1.1");
+        request.setScheme("https");
+        request.setServerName("sp.example.org");
+        request.setServerPort(9443);
+       }
+    
+    void setRequestUrls(String suffix) {
+        request.setMethod("GET");
+        request.setRequestURI("http://sp.example.org:9443/secure/"+suffix);
+        request.setRequestURL("http://sp.example.org:9443/secure/"+suffix);
+        request.setServletPath("/secure/"+suffix);
+        
+    }
+       
+       public void testInitialGET() {
+               
+               setRequestUrls("test.txt");
+        
+               // Run the Filter against the request
+               testModule.doFilter();
+               
+               // It should generate a Redirect to the WAYF, with added parameters
+               assertTrue(response.wasRedirectSent());
+               String wayfurl = response.getHeader("Location");
+               assertEquals(rmAppInfo.wayfUrl,wayfurl.substring(0,wayfurl.indexOf('?')));
+       }
+
+}
index d08f5cc..45ac68f 100644 (file)
@@ -6,6 +6,8 @@ import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PatternLayout;
 
+import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
+
 import junit.framework.TestCase;
 
 /**
@@ -20,5 +22,20 @@ public class SPTestCase extends TestCase {
         root.addAppender(consoleAppender);
         root.setLevel(Level.ERROR);
     }
+    
+    static ServiceProviderContext context   = ServiceProviderContext.getInstance();
+    
+    /**
+     * Load an SP configuration file.
+     * @param configFileName URL format string pointing to configuration file
+     * @throws ShibbolethConfigurationException
+     */
+    public void initServiceProvider(String configFileName) 
+        throws ShibbolethConfigurationException{
+            context.initialize();
+            ServiceProviderConfig config = new ServiceProviderConfig();
+            context.setServiceProviderConfig(config);
+            config.loadConfigObjects(configFileName);
+    }
 
 }
diff --git a/tests/edu/internet2/middleware/shibboleth/serviceprovider/SSOTest.java b/tests/edu/internet2/middleware/shibboleth/serviceprovider/SSOTest.java
new file mode 100644 (file)
index 0000000..f48a226
--- /dev/null
@@ -0,0 +1,107 @@
+package edu.internet2.middleware.shibboleth.serviceprovider;
+
+import java.io.File;
+
+import org.apache.commons.codec.binary.Base64;
+import org.opensaml.SAMLException;
+
+import com.mockrunner.mock.web.MockFilterConfig;
+import com.mockrunner.mock.web.MockHttpServletRequest;
+import com.mockrunner.mock.web.MockHttpServletResponse;
+import com.mockrunner.mock.web.MockServletContext;
+import com.mockrunner.mock.web.WebMockObjectFactory;
+import com.mockrunner.servlet.ServletTestModule;
+
+import edu.internet2.middleware.shibboleth.idp.IdPResponder;
+import edu.internet2.middleware.shibboleth.idp.provider.ShibbolethV1SSOHandler;
+import edu.internet2.middleware.shibboleth.resource.FilterSupport.NewSessionData;
+
+public class SSOTest extends SPTestCase {
+
+    // The Factory creates the Request, Response, Session, etc.
+    WebMockObjectFactory factory = new WebMockObjectFactory();
+    
+    // The TestModule runs the Servlet and Filter methods in the simulated container
+    ServletTestModule testModule = new ServletTestModule(factory);
+    
+    // Now simulated Servlet API objects
+    MockServletContext servletContext= factory.getMockServletContext();
+    MockFilterConfig filterConfig= factory.getMockFilterConfig();
+    MockHttpServletResponse response = factory.getMockResponse();
+    MockHttpServletRequest request = factory.getMockRequest();
+    
+    // Servlet objects
+    private IdPResponder sso;
+
+    // data returned from SSO
+    private String bin64assertion;
+    private String assertion;
+    private String handlerURL;
+    private String targetURL;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        // ServletContext (argument to Filters and Servlets)
+        servletContext.setServletContextName("dummy SSO Context");
+        servletContext.setInitParameter("IdPConfigFile", "file:/C:/usr/local/shibboleth-idp/etc/idp.xml");
+        
+        
+        // Create instance of Filter class, add to chain, call its init()
+        sso = (IdPResponder) testModule.createServlet(IdPResponder.class);
+        
+        // Initialize an SP Context and Confg
+        String configFileName = new File("data/spconfig.xml").toURI().toString();
+        initServiceProvider(configFileName); 
+        
+
+        request.setRemoteAddr("127.0.0.1");
+        request.setContextPath("/shibboleth-idp");
+        request.setProtocol("HTTP/1.1");
+        request.setScheme("https");
+        request.setServerName("idp.example.org");
+        request.setServerPort(443);
+    }
+    
+    void setRequestUrls(String suffix) {
+        request.setMethod("GET");
+        request.setRequestURI("https://idp.example.org/shibboleth-idp/"+suffix);
+        request.setRequestURL("https://idp.example.org/shibboleth-idp/"+suffix);
+        request.setServletPath("/shibboleth.idp/"+suffix);
+        
+    }
+    
+    public void testInitialGET() throws SAMLException {
+        
+        setRequestUrls("SSO");
+        testModule.addRequestParameter("target", "https://nonsense");
+        testModule.addRequestParameter("shire","https://sp.example.org/Shibboleth.sso/SAML/POST");
+        testModule.addRequestParameter("providerId", "https://sp.example.org/shibboleth");
+        request.setRemoteUser("BozoTClown");
+        
+        ShibbolethV1SSOHandler.pushAttributeDefault=true;
+        
+        testModule.doGet();
+        
+        bin64assertion = (String) request.getAttribute("assertion");
+        assertion = new String(Base64.decodeBase64(bin64assertion.getBytes()));
+        handlerURL = (String) request.getAttribute("shire");
+        targetURL = (String) request.getAttribute("target");
+        
+        // There is no need to use the Servlet interface to consume it
+        NewSessionData data = new NewSessionData();
+        data.applicationId="default";
+        data.handlerURL=handlerURL;
+        data.ipaddr=request.getRemoteAddr();
+        data.providerId="https://sp.example.org/shibboleth";
+        data.SAMLResponse = bin64assertion;
+        data.target=targetURL;
+        String sessionId = AssertionConsumerServlet.createSessionFromData(data);
+        
+        // Now get what was created in case you want to test it.
+        Session session = context.getSessionManager().findSession(sessionId, "default");
+        
+    }
+
+
+}
index 25a68c6..d0b7d35 100644 (file)
@@ -7,20 +7,6 @@ import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationExcepti
 import edu.internet2.middleware.shibboleth.serviceprovider.ServiceProviderConfig.ApplicationInfo;
 
 public class TestContextInitializer extends SPTestCase {
-       private static ServiceProviderContext context   = ServiceProviderContext.getInstance();
-    
-    /**
-     * Load an SP configuration file.
-     * @param configFileName URL format string pointing to configuration file
-     * @throws ShibbolethConfigurationException
-     */
-       public void initServiceProvider(String configFileName) 
-        throws ShibbolethConfigurationException{
-            context.initialize();
-                       ServiceProviderConfig config = new ServiceProviderConfig();
-                       context.setServiceProviderConfig(config);
-                       config.loadConfigObjects(configFileName);
-       }
        
     /**
      * Load the typical sample configuration file from the usual place.