    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.ConfigAttribute;
    21  import org.acegisecurity.ConfigAttributeDefinition;
    22  import org.acegisecurity.MockFilterChain;
    23  import org.acegisecurity.SecurityConfig;
    24  import org.acegisecurity.intercept.web.FilterInvocation;
    25
    26  import java.io.IOException;
    27
    28  import java.util.Iterator;
    29  import java.util.List;
    30  import java.util.Vector;
    31
    32  import javax.servlet.ServletException;
    33
    34  import org.springframework.mock.web.MockHttpServletRequest;
    35  import org.springframework.mock.web.MockHttpServletResponse;
    36
    37
    38  /***
    39   * Tests {@link ChannelDecisionManagerImpl}.
    40   *
    41   * @author Ben Alex
    42   * @version $Id: ChannelDecisionManagerImplTests.java,v 1.4 2005/11/17 00:55:48 benalex Exp $
    43   */
    44  public class ChannelDecisionManagerImplTests extends TestCase {
    45      //~ Methods ================================================================
    46
    47      public final void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      public static void main(String[] args) {
    52          junit.textui.TestRunner.run(ChannelDecisionManagerImplTests.class);
    53      }
    54
    55      public void testCannotSetEmptyChannelProcessorsList()
    56          throws Exception {
    57          ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    58
    59          try {
    60              cdm.setChannelProcessors(new Vector());
    61              fail("Should have thrown IllegalArgumentException");
    62          } catch (IllegalArgumentException expected) {
    63              assertEquals("A list of ChannelProcessors is required",
    64                  expected.getMessage());
    65          }
    66      }
    67
    68      public void testCannotSetIncorrectObjectTypesIntoChannelProcessorsList()
    69          throws Exception {
    70          ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    71          List list = new Vector();
    72          list.add("THIS IS NOT A CHANNELPROCESSOR");
    73
    74          try {
    75              cdm.setChannelProcessors(list);
    76              fail("Should have thrown IllegalArgumentException");
    77          } catch (IllegalArgumentException expected) {
    78              assertTrue(true);
    79          }
    80      }
    81
    82      public void testCannotSetNullChannelProcessorsList()
    83          throws Exception {
    84          ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    85
    86          try {
    87              cdm.setChannelProcessors(null);
    88              fail("Should have thrown IllegalArgumentException");
    89          } catch (IllegalArgumentException expected) {
    90              assertEquals("A list of ChannelProcessors is required",
    91                  expected.getMessage());
    92          }
    93      }
    94
    95      public void testDecideIsOperational() throws Exception {
    96          ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    97          MockChannelProcessor cpXyz = new MockChannelProcessor("xyz", false);
    98          MockChannelProcessor cpAbc = new MockChannelProcessor("abc", true);
    99          List list = new Vector();
    100         list.add(cpXyz);
    101         list.add(cpAbc);
    102         cdm.setChannelProcessors(list);
    103         cdm.afterPropertiesSet();
    104
    105         MockHttpServletRequest request = new MockHttpServletRequest();
    106         MockHttpServletResponse response = new MockHttpServletResponse();
    107         MockFilterChain chain = new MockFilterChain();
    108         FilterInvocation fi = new FilterInvocation(request, response, chain);
    109
    110         ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
    111         cad.addConfigAttribute(new SecurityConfig("xyz"));
    112
    113         cdm.decide(fi, cad);
    114         assertTrue(fi.getResponse().isCommitted());
    115     }
    116
    117     public void testDecideIteratesAllProcessorsIfNoneCommitAResponse()
    118         throws Exception {
    119         ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    120         MockChannelProcessor cpXyz = new MockChannelProcessor("xyz", false);
    121         MockChannelProcessor cpAbc = new MockChannelProcessor("abc", false);
    122         List list = new Vector();
    123         list.add(cpXyz);
    124         list.add(cpAbc);
    125         cdm.setChannelProcessors(list);
    126         cdm.afterPropertiesSet();
    127
    128         MockHttpServletRequest request = new MockHttpServletRequest();
    129         MockHttpServletResponse response = new MockHttpServletResponse();
    130         MockFilterChain chain = new MockFilterChain();
    131         FilterInvocation fi = new FilterInvocation(request, response, chain);
    132
    133         ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
    134         cad.addConfigAttribute(new SecurityConfig(
    135                 "SOME_ATTRIBUTE_NO_PROCESSORS_SUPPORT"));
    136
    137         cdm.decide(fi, cad);
    138         assertFalse(fi.getResponse().isCommitted());
    139     }
    140
    141     public void testDelegatesSupports() throws Exception {
    142         ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    143         MockChannelProcessor cpXyz = new MockChannelProcessor("xyz", false);
    144         MockChannelProcessor cpAbc = new MockChannelProcessor("abc", false);
    145         List list = new Vector();
    146         list.add(cpXyz);
    147         list.add(cpAbc);
    148         cdm.setChannelProcessors(list);
    149         cdm.afterPropertiesSet();
    150
    151         assertTrue(cdm.supports(new SecurityConfig("xyz")));
    152         assertTrue(cdm.supports(new SecurityConfig("abc")));
    153         assertFalse(cdm.supports(new SecurityConfig("UNSUPPORTED")));
    154     }
    155
    156     public void testGettersSetters() {
    157         ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    158         assertNull(cdm.getChannelProcessors());
    159
    160         MockChannelProcessor cpXyz = new MockChannelProcessor("xyz", false);
    161         MockChannelProcessor cpAbc = new MockChannelProcessor("abc", false);
    162         List list = new Vector();
    163         list.add(cpXyz);
    164         list.add(cpAbc);
    165         cdm.setChannelProcessors(list);
    166
    167         assertEquals(list, cdm.getChannelProcessors());
    168     }
    169
    170     public void testStartupFailsWithEmptyChannelProcessorsList()
    171         throws Exception {
    172         ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
    173
    174         try {
    175             cdm.afterPropertiesSet();
    176             fail("Should have thrown IllegalArgumentException");
    177         } catch (IllegalArgumentException expected) {
    178             assertEquals("A list of ChannelProcessors is required",
    179                 expected.getMessage());
    180         }
    181     }
    182
    183     //~ Inner Classes ==========================================================
    184
    185     private class MockChannelProcessor implements ChannelProcessor {
    186         private String configAttribute;
    187         private boolean failIfCalled;
    188
    189         public MockChannelProcessor(String configAttribute, boolean failIfCalled) {
    190             this.configAttribute = configAttribute;
    191             this.failIfCalled = failIfCalled;
    192         }
    193
    194         private MockChannelProcessor() {
    195             super();
    196         }
    197
    198         public void decide(FilterInvocation invocation,
    199             ConfigAttributeDefinition config)
    200             throws IOException, ServletException {
    201             Iterator iter = config.getConfigAttributes();
    202
    203             if (failIfCalled) {
    204                 fail("Should not have called this channel processor");
    205             }
    206
    207             while (iter.hasNext()) {
    208                 ConfigAttribute attr = (ConfigAttribute) iter.next();
    209
    210                 if (attr.equals(configAttribute)) {
    211                     invocation.getHttpResponse().sendRedirect("/redirected");
    212
    213                     return;
    214                 }
    215             }
    216         }
    217
    218         public boolean supports(ConfigAttribute attribute) {
    219             if (attribute.getAttribute().equals(configAttribute)) {
    220                 return true;
    221             } else {
    222                 return false;
    223             }
    224         }
    225     }
    226 }