001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.securechannel;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.ConfigAttributeDefinition;
021: import org.acegisecurity.MockFilterChain;
022: import org.acegisecurity.SecurityConfig;
023:
024: import org.acegisecurity.intercept.web.FilterInvocation;
025:
026: import org.springframework.mock.web.MockHttpServletRequest;
027: import org.springframework.mock.web.MockHttpServletResponse;
028:
029: /**
030: * Tests {@link InsecureChannelProcessor}.
031: *
032: * @author Ben Alex
033: * @version $Id: InsecureChannelProcessorTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class InsecureChannelProcessorTests extends TestCase {
036: //~ Methods ========================================================================================================
037:
038: public static void main(String[] args) {
039: junit.textui.TestRunner
040: .run(InsecureChannelProcessorTests.class);
041: }
042:
043: public final void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: public void testDecideDetectsAcceptableChannel() throws Exception {
048: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
049: cad.addConfigAttribute(new SecurityConfig(
050: "SOME_IGNORED_ATTRIBUTE"));
051: cad.addConfigAttribute(new SecurityConfig(
052: "REQUIRES_INSECURE_CHANNEL"));
053:
054: MockHttpServletRequest request = new MockHttpServletRequest();
055: request.setQueryString("info=true");
056: request.setServerName("localhost");
057: request.setContextPath("/bigapp");
058: request.setServletPath("/servlet");
059: request.setScheme("http");
060: request.setServerPort(8080);
061:
062: MockHttpServletResponse response = new MockHttpServletResponse();
063: MockFilterChain chain = new MockFilterChain();
064: FilterInvocation fi = new FilterInvocation(request, response,
065: chain);
066:
067: InsecureChannelProcessor processor = new InsecureChannelProcessor();
068: processor.decide(fi, cad);
069:
070: assertFalse(fi.getResponse().isCommitted());
071: }
072:
073: public void testDecideDetectsUnacceptableChannel() throws Exception {
074: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
075: cad.addConfigAttribute(new SecurityConfig(
076: "SOME_IGNORED_ATTRIBUTE"));
077: cad.addConfigAttribute(new SecurityConfig(
078: "REQUIRES_INSECURE_CHANNEL"));
079:
080: MockHttpServletRequest request = new MockHttpServletRequest();
081: request.setQueryString("info=true");
082: request.setServerName("localhost");
083: request.setContextPath("/bigapp");
084: request.setServletPath("/servlet");
085: request.setScheme("https");
086: request.setSecure(true);
087: request.setServerPort(8443);
088:
089: MockHttpServletResponse response = new MockHttpServletResponse();
090: MockFilterChain chain = new MockFilterChain();
091: FilterInvocation fi = new FilterInvocation(request, response,
092: chain);
093:
094: InsecureChannelProcessor processor = new InsecureChannelProcessor();
095: processor.decide(fi, cad);
096:
097: assertTrue(fi.getResponse().isCommitted());
098: }
099:
100: public void testDecideRejectsNulls() throws Exception {
101: InsecureChannelProcessor processor = new InsecureChannelProcessor();
102: processor.afterPropertiesSet();
103:
104: try {
105: processor.decide(null, null);
106: fail("Should have thrown IllegalArgumentException");
107: } catch (IllegalArgumentException expected) {
108: assertTrue(true);
109: }
110: }
111:
112: public void testGettersSetters() {
113: InsecureChannelProcessor processor = new InsecureChannelProcessor();
114: assertEquals("REQUIRES_INSECURE_CHANNEL", processor
115: .getInsecureKeyword());
116: processor.setInsecureKeyword("X");
117: assertEquals("X", processor.getInsecureKeyword());
118:
119: assertTrue(processor.getEntryPoint() != null);
120: processor.setEntryPoint(null);
121: assertTrue(processor.getEntryPoint() == null);
122: }
123:
124: public void testMissingEntryPoint() throws Exception {
125: InsecureChannelProcessor processor = new InsecureChannelProcessor();
126: processor.setEntryPoint(null);
127:
128: try {
129: processor.afterPropertiesSet();
130: fail("Should have thrown IllegalArgumentException");
131: } catch (IllegalArgumentException expected) {
132: assertEquals("entryPoint required", expected.getMessage());
133: }
134: }
135:
136: public void testMissingSecureChannelKeyword() throws Exception {
137: InsecureChannelProcessor processor = new InsecureChannelProcessor();
138: processor.setInsecureKeyword(null);
139:
140: try {
141: processor.afterPropertiesSet();
142: fail("Should have thrown IllegalArgumentException");
143: } catch (IllegalArgumentException expected) {
144: assertEquals("insecureKeyword required", expected
145: .getMessage());
146: }
147:
148: processor.setInsecureKeyword("");
149:
150: try {
151: processor.afterPropertiesSet();
152: fail("Should have thrown IllegalArgumentException");
153: } catch (IllegalArgumentException expected) {
154: assertEquals("insecureKeyword required", expected
155: .getMessage());
156: }
157: }
158:
159: public void testSupports() {
160: InsecureChannelProcessor processor = new InsecureChannelProcessor();
161: assertTrue(processor.supports(new SecurityConfig(
162: "REQUIRES_INSECURE_CHANNEL")));
163: assertFalse(processor.supports(null));
164: assertFalse(processor.supports(new SecurityConfig(
165: "NOT_SUPPORTED")));
166: }
167: }
|