--- /dev/null
+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('?')));
+ }
+
+}
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
+import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
+
import junit.framework.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);
+ }
}
--- /dev/null
+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");
+
+ }
+
+
+}
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.