1   /* Copyright 2004, 2005, 2006 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  import org.acegisecurity.SecurityConfig;
23  
24  import org.acegisecurity.intercept.web.FilterInvocation;
25  
26  import org.springframework.mock.web.MockHttpServletRequest;
27  import org.springframework.mock.web.MockHttpServletResponse;
28  
29  
30  /**
31   * Tests {@link InsecureChannelProcessor}.
32   *
33   * @author Ben Alex
34   * @version $Id: InsecureChannelProcessorTests.java 1496 2006-05-23 13:38:33Z benalex $
35   */
36  public class InsecureChannelProcessorTests extends TestCase {
37      //~ Methods ========================================================================================================
38  
39      public static void main(String[] args) {
40          junit.textui.TestRunner.run(InsecureChannelProcessorTests.class);
41      }
42  
43      public final void setUp() throws Exception {
44          super.setUp();
45      }
46  
47      public void testDecideDetectsAcceptableChannel() throws Exception {
48          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
49          cad.addConfigAttribute(new SecurityConfig("SOME_IGNORED_ATTRIBUTE"));
50          cad.addConfigAttribute(new SecurityConfig("REQUIRES_INSECURE_CHANNEL"));
51  
52          MockHttpServletRequest request = new MockHttpServletRequest();
53          request.setQueryString("info=true");
54          request.setServerName("localhost");
55          request.setContextPath("/bigapp");
56          request.setServletPath("/servlet");
57          request.setScheme("http");
58          request.setServerPort(8080);
59  
60          MockHttpServletResponse response = new MockHttpServletResponse();
61          MockFilterChain chain = new MockFilterChain();
62          FilterInvocation fi = new FilterInvocation(request, response, chain);
63  
64          InsecureChannelProcessor processor = new InsecureChannelProcessor();
65          processor.decide(fi, cad);
66  
67          assertFalse(fi.getResponse().isCommitted());
68      }
69  
70      public void testDecideDetectsUnacceptableChannel()
71          throws Exception {
72          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
73          cad.addConfigAttribute(new SecurityConfig("SOME_IGNORED_ATTRIBUTE"));
74          cad.addConfigAttribute(new SecurityConfig("REQUIRES_INSECURE_CHANNEL"));
75  
76          MockHttpServletRequest request = new MockHttpServletRequest();
77          request.setQueryString("info=true");
78          request.setServerName("localhost");
79          request.setContextPath("/bigapp");
80          request.setServletPath("/servlet");
81          request.setScheme("https");
82          request.setSecure(true);
83          request.setServerPort(8443);
84  
85          MockHttpServletResponse response = new MockHttpServletResponse();
86          MockFilterChain chain = new MockFilterChain();
87          FilterInvocation fi = new FilterInvocation(request, response, chain);
88  
89          InsecureChannelProcessor processor = new InsecureChannelProcessor();
90          processor.decide(fi, cad);
91  
92          assertTrue(fi.getResponse().isCommitted());
93      }
94  
95      public void testDecideRejectsNulls() throws Exception {
96          InsecureChannelProcessor processor = new InsecureChannelProcessor();
97          processor.afterPropertiesSet();
98  
99          try {
100             processor.decide(null, null);
101             fail("Should have thrown IllegalArgumentException");
102         } catch (IllegalArgumentException expected) {
103             assertTrue(true);
104         }
105     }
106 
107     public void testGettersSetters() {
108         InsecureChannelProcessor processor = new InsecureChannelProcessor();
109         assertEquals("REQUIRES_INSECURE_CHANNEL", processor.getInsecureKeyword());
110         processor.setInsecureKeyword("X");
111         assertEquals("X", processor.getInsecureKeyword());
112 
113         assertTrue(processor.getEntryPoint() != null);
114         processor.setEntryPoint(null);
115         assertTrue(processor.getEntryPoint() == null);
116     }
117 
118     public void testMissingEntryPoint() throws Exception {
119         InsecureChannelProcessor processor = new InsecureChannelProcessor();
120         processor.setEntryPoint(null);
121 
122         try {
123             processor.afterPropertiesSet();
124             fail("Should have thrown IllegalArgumentException");
125         } catch (IllegalArgumentException expected) {
126             assertEquals("entryPoint required", expected.getMessage());
127         }
128     }
129 
130     public void testMissingSecureChannelKeyword() throws Exception {
131         InsecureChannelProcessor processor = new InsecureChannelProcessor();
132         processor.setInsecureKeyword(null);
133 
134         try {
135             processor.afterPropertiesSet();
136             fail("Should have thrown IllegalArgumentException");
137         } catch (IllegalArgumentException expected) {
138             assertEquals("insecureKeyword required", expected.getMessage());
139         }
140 
141         processor.setInsecureKeyword("");
142 
143         try {
144             processor.afterPropertiesSet();
145             fail("Should have thrown IllegalArgumentException");
146         } catch (IllegalArgumentException expected) {
147             assertEquals("insecureKeyword required", expected.getMessage());
148         }
149     }
150 
151     public void testSupports() {
152         InsecureChannelProcessor processor = new InsecureChannelProcessor();
153         assertTrue(processor.supports(new SecurityConfig("REQUIRES_INSECURE_CHANNEL")));
154         assertFalse(processor.supports(null));
155         assertFalse(processor.supports(new SecurityConfig("NOT_SUPPORTED")));
156     }
157 }