    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.intercept.web;
    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 java.util.Iterator;
    27
    28  import org.springframework.mock.web.MockHttpServletRequest;
    29  import org.springframework.mock.web.MockHttpServletResponse;
    30
    31
    32  /***
    33   * Tests {@link FilterInvocationDefinitionSourceEditor} and its associated
    34   * default {@link RegExpBasedFilterInvocationDefinitionMap}.
    35   *
    36   * @author Ben Alex
    37   * @version $Id: FilterInvocationDefinitionSourceEditorTests.java,v 1.4 2005/11/17 00:55:50 benalex Exp $
    38   */
    39  public class FilterInvocationDefinitionSourceEditorTests extends TestCase {
    40      //~ Constructors ===========================================================
    41
    42      public FilterInvocationDefinitionSourceEditorTests() {
    43          super();
    44      }
    45
    46      public FilterInvocationDefinitionSourceEditorTests(String arg0) {
    47          super(arg0);
    48      }
    49
    50      //~ Methods ================================================================
    51
    52      public final void setUp() throws Exception {
    53          super.setUp();
    54      }
    55
    56      public static void main(String[] args) {
    57          junit.textui.TestRunner.run(FilterInvocationDefinitionSourceEditorTests.class);
    58      }
    59
    60      public void testConvertUrlToLowercaseDefaultSettingUnchangedByEditor() {
    61          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    62          editor.setAsText(
    63              "//A/secure/super.*//Z=ROLE_WE_DONT_HAVE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    64
    65          RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    66              .getValue();
    67          assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
    68      }
    69
    70      public void testConvertUrlToLowercaseSettingApplied() {
    71          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    72          editor.setAsText(
    73              "CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON\r\n//A/secure/super.*//Z=ROLE_WE_DONT_HAVE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    74
    75          RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    76              .getValue();
    77          assertTrue(map.isConvertUrlToLowercaseBeforeComparison());
    78      }
    79
    80      public void testDefaultIsRegularExpression() {
    81          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    82          editor.setAsText(
    83              "//A/secure/super.*//Z=ROLE_WE_DONT_HAVE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    84
    85          FilterInvocationDefinitionMap map = (FilterInvocationDefinitionMap) editor
    86              .getValue();
    87          assertTrue(map instanceof RegExpBasedFilterInvocationDefinitionMap);
    88      }
    89
    90      public void testEmptyStringReturnsEmptyMap() {
    91          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    92          editor.setAsText("");
    93
    94          RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    95              .getValue();
    96          assertEquals(0, map.getMapSize());
    97      }
    98
    99      public void testInvalidRegularExpressionsDetected()
    100         throws Exception {
    101         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    102
    103         try {
    104             editor.setAsText("*=SOME_ROLE");
    105         } catch (IllegalArgumentException expected) {
    106             assertEquals("Malformed regular expression: *",
    107                 expected.getMessage());
    108         }
    109     }
    110
    111     public void testIterator() {
    112         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    113         editor.setAsText(
    114             "//A/secure/super.*//Z=ROLE_WE_DONT_HAVE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    115
    116         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    117             .getValue();
    118         Iterator iter = map.getConfigAttributeDefinitions();
    119         int counter = 0;
    120
    121         while (iter.hasNext()) {
    122             iter.next();
    123             counter++;
    124         }
    125
    126         assertEquals(2, counter);
    127     }
    128
    129     public void testMapReturnsNullWhenNoMatchFound() throws Exception {
    130         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    131         editor.setAsText("//A/secure/super.*//Z=ROLE_WE_DONT_HAVE,ANOTHER_ROLE");
    132
    133         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    134             .getValue();
    135
    136         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    137                 null);
    138         httpRequest.setServletPath("/totally/different/path/index.html");
    139
    140         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    141                     httpRequest, new MockHttpServletResponse(),
    142                     new MockFilterChain()));
    143
    144         assertEquals(null, returned);
    145     }
    146
    147     public void testMultiUrlParsing() {
    148         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    149         editor.setAsText(
    150             "//A/secure/super.*//Z=ROLE_WE_DONT_HAVE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    151
    152         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    153             .getValue();
    154         assertEquals(2, map.getMapSize());
    155     }
    156
    157     public void testNoArgsConstructor() {
    158         try {
    159             new RegExpBasedFilterInvocationDefinitionMap().new EntryHolder();
    160             fail("Should have thrown IllegalArgumentException");
    161         } catch (IllegalArgumentException expected) {
    162             assertTrue(true);
    163         }
    164     }
    165
    166     public void testNullReturnsEmptyMap() {
    167         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    168         editor.setAsText(null);
    169
    170         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    171             .getValue();
    172         assertEquals(0, map.getMapSize());
    173     }
    174
    175     public void testOrderOfEntriesIsPreservedOrderA() {
    176         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    177         editor.setAsText(
    178             "//A/secure/super.*//Z=ROLE_WE_DONT_HAVE,ANOTHER_ROLE\r\n//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER");
    179
    180         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    181             .getValue();
    182
    183         // Test ensures we match the first entry, not the second
    184         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    185                 null);
    186         httpRequest.setServletPath("/secure/super/very_secret.html");
    187
    188         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    189                     httpRequest, new MockHttpServletResponse(),
    190                     new MockFilterChain()));
    191
    192         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    193         expected.addConfigAttribute(new SecurityConfig("ROLE_WE_DONT_HAVE"));
    194         expected.addConfigAttribute(new SecurityConfig("ANOTHER_ROLE"));
    195
    196         assertEquals(expected, returned);
    197     }
    198
    199     public void testOrderOfEntriesIsPreservedOrderB() {
    200         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    201         editor.setAsText(
    202             "//A/secure/.*//Z=ROLE_SUPERVISOR,ROLE_TELLER\r\n//A/secure/super.*//Z=ROLE_WE_DONT_HAVE,ANOTHER_ROLE");
    203
    204         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    205             .getValue();
    206
    207         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    208                 null);
    209         httpRequest.setServletPath("/secure/super/very_secret.html");
    210
    211         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    212                     httpRequest, new MockHttpServletResponse(),
    213                     new MockFilterChain()));
    214
    215         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    216         expected.addConfigAttribute(new SecurityConfig("ROLE_SUPERVISOR"));
    217         expected.addConfigAttribute(new SecurityConfig("ROLE_TELLER"));
    218
    219         assertEquals(expected, returned);
    220     }
    221
    222     public void testSingleUrlParsing() throws Exception {
    223         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    224         editor.setAsText("//A/secure/super.*//Z=ROLE_WE_DONT_HAVE,ANOTHER_ROLE");
    225
    226         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    227             .getValue();
    228
    229         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    230                 null);
    231         httpRequest.setServletPath("/secure/super/very_secret.html");
    232
    233         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    234                     httpRequest, new MockHttpServletResponse(),
    235                     new MockFilterChain()));
    236
    237         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    238         expected.addConfigAttribute(new SecurityConfig("ROLE_WE_DONT_HAVE"));
    239         expected.addConfigAttribute(new SecurityConfig("ANOTHER_ROLE"));
    240
    241         assertEquals(expected, returned);
    242     }
    243
    244     public void testWhitespaceAndCommentsAndLinesWithoutEqualsSignsAreIgnored() {
    245         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    246         editor.setAsText(
    247             "         //A/secure/super.*//Z=ROLE_WE_DONT_HAVE,ANOTHER_ROLE      \r\n   \r\n     \r\n   // comment line  \r\n   //A/testing.*//Z=ROLE_TEST   \r\n");
    248
    249         RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
    250             .getValue();
    251         assertEquals(2, map.getMapSize());
    252     }
    253 }