1   /*
2    * $Id: AuthenticationWithComponentSecuritySynch.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.extras.acegi;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.MuleProperties;
15  import org.mule.extras.client.MuleClient;
16  import org.mule.impl.security.MuleCredentials;
17  import org.mule.tck.FunctionalTestCase;
18  import org.mule.umo.UMOEncryptionStrategy;
19  import org.mule.umo.UMOMessage;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.acegisecurity.context.SecurityContextHolder;
25  import org.acegisecurity.context.SecurityContextImpl;
26  
27  public class AuthenticationWithComponentSecuritySynch extends FunctionalTestCase
28  {
29  
30      // Clear the security context after each test.
31      public void doFunctionalTearDown()
32      {
33          SecurityContextHolder.setContext(new SecurityContextImpl());
34      }
35  
36      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
37      {
38          MuleClient client = new MuleClient();
39          Map props = new HashMap();
40  
41          UMOEncryptionStrategy strategy = MuleManager.getInstance()
42              .getSecurityManager()
43              .getEncryptionStrategy("PBE");
44          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
45          props.put(MuleProperties.MULE_USER_PROPERTY, header);
46          UMOMessage m = client.send("vm://test", "Marie", props);
47          assertNotNull(m);
48          assertTrue(m.getPayload().equals("Marie"));
49      }
50  
51      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
52      {
53          MuleClient client = new MuleClient();
54          Map props = new HashMap();
55  
56          UMOEncryptionStrategy strategy = MuleManager.getInstance()
57              .getSecurityManager()
58              .getEncryptionStrategy("PBE");
59          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
60          props.put(MuleProperties.MULE_USER_PROPERTY, header);
61          UMOMessage m = client.send("vm://test", "Marie", props);
62          assertFalse(m.getPayload().equals("Marie"));
63      }
64  
65      public void testCaseBadAuthentication() throws Exception
66      {
67          MuleClient client = new MuleClient();
68          Map props = new HashMap();
69  
70          UMOEncryptionStrategy strategy = MuleManager.getInstance()
71              .getSecurityManager()
72              .getEncryptionStrategy("PBE");
73          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
74          props.put(MuleProperties.MULE_USER_PROPERTY, header);
75          UMOMessage m = client.send("vm://test", "Marie", props);
76          assertNotNull(m.getPayload());
77      }
78  
79      protected String getConfigResources()
80      {
81          return "test-mule-component-security-synchronous-config.xml";
82      }
83  }