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 SecureChannelProcessor}.
031: *
032: * @author Ben Alex
033: * @version $Id: SecureChannelProcessorTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class SecureChannelProcessorTests extends TestCase {
036: //~ Methods ========================================================================================================
037:
038: public static void main(String[] args) {
039: junit.textui.TestRunner.run(SecureChannelProcessorTests.class);
040: }
041:
042: public final void setUp() throws Exception {
043: super .setUp();
044: }
045:
046: public void testDecideDetectsAcceptableChannel() throws Exception {
047: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
048: cad.addConfigAttribute(new SecurityConfig(
049: "SOME_IGNORED_ATTRIBUTE"));
050: cad.addConfigAttribute(new SecurityConfig(
051: "REQUIRES_SECURE_CHANNEL"));
052:
053: MockHttpServletRequest request = new MockHttpServletRequest();
054: request.setQueryString("info=true");
055: request.setServerName("localhost");
056: request.setContextPath("/bigapp");
057: request.setServletPath("/servlet");
058: request.setScheme("https");
059: request.setSecure(true);
060: request.setServerPort(8443);
061:
062: MockHttpServletResponse response = new MockHttpServletResponse();
063: MockFilterChain chain = new MockFilterChain();
064: FilterInvocation fi = new FilterInvocation(request, response,
065: chain);
066:
067: SecureChannelProcessor processor = new SecureChannelProcessor();
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_SECURE_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("http");
086: request.setServerPort(8080);
087:
088: MockHttpServletResponse response = new MockHttpServletResponse();
089: MockFilterChain chain = new MockFilterChain();
090: FilterInvocation fi = new FilterInvocation(request, response,
091: chain);
092:
093: SecureChannelProcessor processor = new SecureChannelProcessor();
094: processor.decide(fi, cad);
095:
096: assertTrue(fi.getResponse().isCommitted());
097: }
098:
099: public void testDecideRejectsNulls() throws Exception {
100: SecureChannelProcessor processor = new SecureChannelProcessor();
101: processor.afterPropertiesSet();
102:
103: try {
104: processor.decide(null, null);
105: fail("Should have thrown IllegalArgumentException");
106: } catch (IllegalArgumentException expected) {
107: assertTrue(true);
108: }
109: }
110:
111: public void testGettersSetters() {
112: SecureChannelProcessor processor = new SecureChannelProcessor();
113: assertEquals("REQUIRES_SECURE_CHANNEL", processor
114: .getSecureKeyword());
115: processor.setSecureKeyword("X");
116: assertEquals("X", processor.getSecureKeyword());
117:
118: assertTrue(processor.getEntryPoint() != null);
119: processor.setEntryPoint(null);
120: assertTrue(processor.getEntryPoint() == null);
121: }
122:
123: public void testMissingEntryPoint() throws Exception {
124: SecureChannelProcessor processor = new SecureChannelProcessor();
125: processor.setEntryPoint(null);
126:
127: try {
128: processor.afterPropertiesSet();
129: fail("Should have thrown IllegalArgumentException");
130: } catch (IllegalArgumentException expected) {
131: assertEquals("entryPoint required", expected.getMessage());
132: }
133: }
134:
135: public void testMissingSecureChannelKeyword() throws Exception {
136: SecureChannelProcessor processor = new SecureChannelProcessor();
137: processor.setSecureKeyword(null);
138:
139: try {
140: processor.afterPropertiesSet();
141: fail("Should have thrown IllegalArgumentException");
142: } catch (IllegalArgumentException expected) {
143: assertEquals("secureKeyword required", expected
144: .getMessage());
145: }
146:
147: processor.setSecureKeyword("");
148:
149: try {
150: processor.afterPropertiesSet();
151: fail("Should have thrown IllegalArgumentException");
152: } catch (IllegalArgumentException expected) {
153: assertEquals("secureKeyword required", expected
154: .getMessage());
155: }
156: }
157:
158: public void testSupports() {
159: SecureChannelProcessor processor = new SecureChannelProcessor();
160: assertTrue(processor.supports(new SecurityConfig(
161: "REQUIRES_SECURE_CHANNEL")));
162: assertFalse(processor.supports(null));
163: assertFalse(processor.supports(new SecurityConfig(
164: "NOT_SUPPORTED")));
165: }
166: }
|