1   /*
2    * $Id: AuthenticationWithComponentSecurityAsynch.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 AuthenticationWithComponentSecurityAsynch 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          client.dispatch("vm://test", "Marie", props);
47          UMOMessage m = client.receive("vm://output", 3000);
48          assertNotNull(m);
49          assertEquals((String)m.getPayload(), "Marie");
50      }
51  
52      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
53      {
54          MuleClient client = new MuleClient();
55          Map props = new HashMap();
56  
57          UMOEncryptionStrategy strategy = MuleManager.getInstance()
58              .getSecurityManager()
59              .getEncryptionStrategy("PBE");
60          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
61          props.put(MuleProperties.MULE_USER_PROPERTY, header);
62          client.dispatch("vm://test", "Marie", props);
63          UMOMessage m = client.receive("vm://output", 3000);
64          assertNull(m);
65      }
66  
67      public void testCaseBadAuthentication() throws Exception
68      {
69          MuleClient client = new MuleClient();
70          Map props = new HashMap();
71  
72          UMOEncryptionStrategy strategy = MuleManager.getInstance()
73              .getSecurityManager()
74              .getEncryptionStrategy("PBE");
75          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
76          props.put(MuleProperties.MULE_USER_PROPERTY, header);
77          client.dispatch("vm://test", "USD,MTL", props);
78          UMOMessage m = client.receive("vm://output", 3000);
79          assertNull(m);
80      }
81  
82      protected String getConfigResources()
83      {
84          return "test-mule-component-security-asynchronous-config.xml";
85      }
86  }