1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.securechannel;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.ConfigAttributeDefinition;
21  import org.acegisecurity.MockFilterChain;
22  
23  
24  import org.acegisecurity.SecurityConfig;
25  
26  import org.acegisecurity.intercept.web.FilterInvocation;
27  import org.springframework.mock.web.MockHttpServletRequest;
28  import org.springframework.mock.web.MockHttpServletResponse;
29  
30  
31  /***
32   * Tests {@link SecureChannelProcessor}.
33   *
34   * @author Ben Alex
35   * @version $Id: SecureChannelProcessorTests.java,v 1.3 2005/11/17 00:55:48 benalex Exp $
36   */
37  public class SecureChannelProcessorTests extends TestCase {
38      //~ Methods ================================================================
39  
40      public final void setUp() throws Exception {
41          super.setUp();
42      }
43  
44      public static void main(String[] args) {
45          junit.textui.TestRunner.run(SecureChannelProcessorTests.class);
46      }
47  
48      public void testDecideDetectsAcceptableChannel() throws Exception {
49          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
50          cad.addConfigAttribute(new SecurityConfig("SOME_IGNORED_ATTRIBUTE"));
51          cad.addConfigAttribute(new SecurityConfig("REQUIRES_SECURE_CHANNEL"));
52  
53          MockHttpServletRequest request = new MockHttpServletRequest();
54          request.setQueryString("info=true");
55          request.setServerName("localhost");
56          request.setContextPath("/bigapp");
57          request.setServletPath("/servlet");
58          request.setScheme("https");
59          request.setSecure(true);
60          request.setServerPort(8443);
61  
62          MockHttpServletResponse response = new MockHttpServletResponse();
63          MockFilterChain chain = new MockFilterChain();
64          FilterInvocation fi = new FilterInvocation(request, response, chain);
65  
66          SecureChannelProcessor processor = new SecureChannelProcessor();
67          processor.decide(fi, cad);
68  
69          assertFalse(fi.getResponse().isCommitted());
70      }
71  
72      public void testDecideDetectsUnacceptableChannel()
73          throws Exception {
74          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
75          cad.addConfigAttribute(new SecurityConfig("SOME_IGNORED_ATTRIBUTE"));
76          cad.addConfigAttribute(new SecurityConfig("REQUIRES_SECURE_CHANNEL"));
77  
78          MockHttpServletRequest request = new MockHttpServletRequest();
79          request.setQueryString("info=true");
80          request.setServerName("localhost");
81          request.setContextPath("/bigapp");
82          request.setServletPath("/servlet");
83          request.setScheme("http");
84          request.setServerPort(8080);
85  
86          MockHttpServletResponse response = new MockHttpServletResponse();
87          MockFilterChain chain = new MockFilterChain();
88          FilterInvocation fi = new FilterInvocation(request, response, chain);
89  
90          SecureChannelProcessor processor = new SecureChannelProcessor();
91          processor.decide(fi, cad);
92  
93          assertTrue(fi.getResponse().isCommitted());
94      }
95  
96      public void testDecideRejectsNulls() throws Exception {
97          SecureChannelProcessor processor = new SecureChannelProcessor();
98          processor.afterPropertiesSet();
99  
100         try {
101             processor.decide(null, null);
102             fail("Should have thrown IllegalArgumentException");
103         } catch (IllegalArgumentException expected) {
104             assertTrue(true);
105         }
106     }
107 
108     public void testGettersSetters() {
109         SecureChannelProcessor processor = new SecureChannelProcessor();
110         assertEquals("REQUIRES_SECURE_CHANNEL", processor.getSecureKeyword());
111         processor.setSecureKeyword("X");
112         assertEquals("X", processor.getSecureKeyword());
113 
114         assertTrue(processor.getEntryPoint() != null);
115         processor.setEntryPoint(null);
116         assertTrue(processor.getEntryPoint() == null);
117     }
118 
119     public void testMissingEntryPoint() throws Exception {
120         SecureChannelProcessor processor = new SecureChannelProcessor();
121         processor.setEntryPoint(null);
122 
123         try {
124             processor.afterPropertiesSet();
125             fail("Should have thrown IllegalArgumentException");
126         } catch (IllegalArgumentException expected) {
127             assertEquals("entryPoint required", expected.getMessage());
128         }
129     }
130 
131     public void testMissingSecureChannelKeyword() throws Exception {
132         SecureChannelProcessor processor = new SecureChannelProcessor();
133         processor.setSecureKeyword(null);
134 
135         try {
136             processor.afterPropertiesSet();
137             fail("Should have thrown IllegalArgumentException");
138         } catch (IllegalArgumentException expected) {
139             assertEquals("secureKeyword required", expected.getMessage());
140         }
141 
142         processor.setSecureKeyword("");
143 
144         try {
145             processor.afterPropertiesSet();
146             fail("Should have thrown IllegalArgumentException");
147         } catch (IllegalArgumentException expected) {
148             assertEquals("secureKeyword required", expected.getMessage());
149         }
150     }
151 
152     public void testSupports() {
153         SecureChannelProcessor processor = new SecureChannelProcessor();
154         assertTrue(processor.supports(
155                 new SecurityConfig("REQUIRES_SECURE_CHANNEL")));
156         assertFalse(processor.supports(null));
157         assertFalse(processor.supports(new SecurityConfig("NOT_SUPPORTED")));
158     }
159 }