    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 InsecureChannelProcessor}.
    33   *
    34   * @author Ben Alex
    35   * @version $Id: InsecureChannelProcessorTests.java,v 1.3 2005/11/17 00:55:48 benalex Exp $
    36   */
    37  public class InsecureChannelProcessorTests 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(InsecureChannelProcessorTests.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_INSECURE_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("http");
    59          request.setServerPort(8080);
    60
    61          MockHttpServletResponse response = new MockHttpServletResponse();
    62          MockFilterChain chain = new MockFilterChain();
    63          FilterInvocation fi = new FilterInvocation(request, response, chain);
    64
    65          InsecureChannelProcessor processor = new InsecureChannelProcessor();
    66          processor.decide(fi, cad);
    67
    68          assertFalse(fi.getResponse().isCommitted());
    69      }
    70
    71      public void testDecideDetectsUnacceptableChannel()
    72          throws Exception {
    73          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
    74          cad.addConfigAttribute(new SecurityConfig("SOME_IGNORED_ATTRIBUTE"));
    75          cad.addConfigAttribute(new SecurityConfig("REQUIRES_INSECURE_CHANNEL"));
    76
    77          MockHttpServletRequest request = new MockHttpServletRequest();
    78          request.setQueryString("info=true");
    79          request.setServerName("localhost");
    80          request.setContextPath("/bigapp");
    81          request.setServletPath("/servlet");
    82          request.setScheme("https");
    83          request.setSecure(true);
    84          request.setServerPort(8443);
    85
    86          MockHttpServletResponse response = new MockHttpServletResponse();
    87          MockFilterChain chain = new MockFilterChain();
    88          FilterInvocation fi = new FilterInvocation(request, response, chain);
    89
    90          InsecureChannelProcessor processor = new InsecureChannelProcessor();
    91          processor.decide(fi, cad);
    92
    93          assertTrue(fi.getResponse().isCommitted());
    94      }
    95
    96      public void testDecideRejectsNulls() throws Exception {
    97          InsecureChannelProcessor processor = new InsecureChannelProcessor();
    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         InsecureChannelProcessor processor = new InsecureChannelProcessor();
    110         assertEquals("REQUIRES_INSECURE_CHANNEL", processor.getInsecureKeyword());
    111         processor.setInsecureKeyword("X");
    112         assertEquals("X", processor.getInsecureKeyword());
    113
    114         assertTrue(processor.getEntryPoint() != null);
    115         processor.setEntryPoint(null);
    116         assertTrue(processor.getEntryPoint() == null);
    117     }
    118
    119     public void testMissingEntryPoint() throws Exception {
    120         InsecureChannelProcessor processor = new InsecureChannelProcessor();
    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         InsecureChannelProcessor processor = new InsecureChannelProcessor();
    133         processor.setInsecureKeyword(null);
    134
    135         try {
    136             processor.afterPropertiesSet();
    137             fail("Should have thrown IllegalArgumentException");
    138         } catch (IllegalArgumentException expected) {
    139             assertEquals("insecureKeyword required", expected.getMessage());
    140         }
    141
    142         processor.setInsecureKeyword("");
    143
    144         try {
    145             processor.afterPropertiesSet();
    146             fail("Should have thrown IllegalArgumentException");
    147         } catch (IllegalArgumentException expected) {
    148             assertEquals("insecureKeyword required", expected.getMessage());
    149         }
    150     }
    151
    152     public void testSupports() {
    153         InsecureChannelProcessor processor = new InsecureChannelProcessor();
    154         assertTrue(processor.supports(
    155                 new SecurityConfig("REQUIRES_INSECURE_CHANNEL")));
    156         assertFalse(processor.supports(null));
    157         assertFalse(processor.supports(new SecurityConfig("NOT_SUPPORTED")));
    158     }
    159 }