Create parser pool interface and move current pool to an implementation of this inter...
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / idp / SSOTest.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.idp;
18
19 import java.io.File;
20
21 import org.apache.commons.codec.binary.Base64;
22
23 import edu.internet2.middleware.shibboleth.utils.FileUtils;
24
25 /**
26  * MockObject unit tests for Shibboleth IdP Single Sign On component
27  * 
28  * @author Will Norris (wnorris@memphis.edu)
29  */
30 public class SSOTest extends IdpTestCase {
31
32         /**
33          * Initialize SSO request object
34          */
35         private void initRequest() {
36
37                 request.setRemoteAddr("127.0.0.1");
38                 request.setContextPath("/shibboleth-idp");
39                 request.setProtocol("HTTP/1.1");
40                 request.setScheme("https");
41                 request.setServerName("idp.example.org");
42                 request.setServerPort(443);
43
44                 request.setMethod("GET");
45                 request.setRequestURL("https://idp.example.org/shibboleth-idp/SSO");
46                 request.setRequestURI("https://idp.example.org/shibboleth-idp/SSO");
47         }
48
49         /**
50          * Basic working SSO flow using Artifact
51          * 
52          * @throws Exception
53          */
54         public void testBasicSsoArtifactFlow() throws Exception {
55
56                 resetServlet("data/idp/blackbox/conf/standard");
57
58                 initRequest();
59                 request.setupAddParameter("target", "https://sp.example.org/cgi-bin/login.cgi");
60                 request.setupAddParameter("shire", "https://sp.example.org/Shibboleth.sso/SAML/Artifact");
61                 request.setupAddParameter("providerId", "urn:x-shibtest:SP");
62                 request.setRemoteUser("gpburdell");
63
64                 testModule.doGet();
65
66                 assertTrue(response.getHeader("Location").matches(
67                                 "https://sp.example.org/Shibboleth.sso/SAML/Artifact?.*"
68                                                 + "TARGET=https%3A%2F%2Fsp.example.org%2Fcgi-bin%2Flogin.cgi" + "&SAMLart=[^&]+"
69                                                 + "&SAMLart=[^&]+"));
70         }
71
72         /**
73          * Basic working SSO flow using POST
74          * 
75          * @throws Exception
76          */
77         public void testBasicSsoPostFlow() throws Exception {
78
79                 resetServlet("data/idp/blackbox/conf/ssoPost");
80
81                 initRequest();
82                 request.setupAddParameter("target", "https://sp.example.org/cgi-bin/login.cgi");
83                 request.setupAddParameter("shire", "https://sp.example.org/Shibboleth.sso/SAML/POST");
84                 request.setupAddParameter("providerId", "urn:x-shibtest:SP");
85                 request.setRemoteUser("gpburdell");
86
87                 testModule.doGet();
88
89                 String bin64assertion = (String) request.getAttribute("assertion");
90                 String assertion = new String(Base64.decodeBase64(bin64assertion.getBytes()));
91
92                 assertTrue(responsesAreEqual(FileUtils.readFileToString(new File("data/idp/blackbox/sso/response01.txt"),
93                                 "utf-8"), assertion));
94         }
95
96         /**
97          * SSO flow with invalid SP Acceptance URL
98          * 
99          * @throws Exception
100          */
101         public void testSsoFlowWithInvalidSpAcceptanceUrl() throws Exception {
102
103                 resetServlet("data/idp/blackbox/conf/standard");
104
105                 initRequest();
106                 request.setupAddParameter("target", "https://sp.example.org/cgi-bin/login.cgi");
107                 request.setupAddParameter("shire", "https://invalid.edu/Shibboleth.sso/SAML/Artifact");
108                 request.setupAddParameter("providerId", "urn:x-shibtest:SP");
109                 request.setRemoteUser("gpburdell");
110
111                 testModule.doGet();
112
113                 assertEquals("edu.internet2.middleware.shibboleth.idp.RequestHandlingException: Unable to handle request.  "
114                                 + "Client data is invalid: edu.internet2.middleware.shibboleth.idp.InvalidClientDataException: "
115                                 + "Invalid assertion consumer service URL.", request.getAttribute("errorText"));
116         }
117
118         /**
119          * SSO flow with signed assertions
120          * 
121          * @throws Exception
122          */
123         public void testSsoFlowWithSignedAssertions() throws Exception {
124
125                 resetServlet("data/idp/blackbox/conf/signAssertions");
126
127                 initRequest();
128                 request.setupAddParameter("target", "https://sp.example.org/cgi-bin/login.cgi");
129                 request.setupAddParameter("shire", "https://sp.example.org/Shibboleth.sso/SAML/POST");
130                 request.setupAddParameter("providerId", "urn:x-shibtest:SP");
131                 request.setRemoteUser("gpburdell");
132
133                 testModule.doGet();
134
135                 String bin64assertion = (String) request.getAttribute("assertion");
136                 String assertion = new String(Base64.decodeBase64(bin64assertion.getBytes()));
137
138                 assertTrue(responsesAreEqual(FileUtils.readFileToString(new File("data/idp/blackbox/sso/response03.txt"),
139                                 "utf-8"), assertion));
140         }
141
142 }