1   /*
2    * $Id: MuleEndpointEncryptionFilterTestCase.java 7963 2007-08-21 08:53:15Z 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.ExceptionHelper;
15  import org.mule.config.MuleProperties;
16  import org.mule.extras.client.MuleClient;
17  import org.mule.impl.security.MuleCredentials;
18  import org.mule.providers.http.HttpConnector;
19  import org.mule.providers.http.HttpConstants;
20  import org.mule.tck.FunctionalTestCase;
21  import org.mule.umo.UMOEncryptionStrategy;
22  import org.mule.umo.UMOMessage;
23  import org.mule.umo.security.CredentialsNotSetException;
24  import org.mule.umo.security.UnauthorisedException;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  public class MuleEndpointEncryptionFilterTestCase extends FunctionalTestCase
30  {
31  
32      protected String getConfigResources()
33      {
34          return "test-acegi-encrypt-config.xml";
35      }
36  
37      public void testAuthenticationFailureNoContext() throws Exception
38      {
39          MuleClient client = new MuleClient();
40          UMOMessage m = client.send("vm://my.queue", "foo", null);
41          assertNotNull(m);
42          assertNotNull(m.getExceptionPayload());
43          assertEquals(ExceptionHelper.getErrorCode(CredentialsNotSetException.class), m.getExceptionPayload()
44              .getCode());
45      }
46  
47      public void testAuthenticationFailureBadCredentials() throws Exception
48      {
49          MuleClient client = new MuleClient();
50          Map props = new HashMap();
51          UMOEncryptionStrategy strategy = MuleManager.getInstance()
52              .getSecurityManager()
53              .getEncryptionStrategy("PBE");
54          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
55          props.put(MuleProperties.MULE_USER_PROPERTY, header);
56  
57          UMOMessage m = client.send("vm://my.queue", "foo", props);
58          assertNotNull(m);
59          assertNotNull(m.getExceptionPayload());
60          assertEquals(ExceptionHelper.getErrorCode(UnauthorisedException.class), m.getExceptionPayload()
61              .getCode());
62      }
63  
64      public void testAuthenticationAuthorised() throws Exception
65      {
66          MuleClient client = new MuleClient();
67  
68          Map props = new HashMap();
69          UMOEncryptionStrategy strategy = MuleManager.getInstance()
70              .getSecurityManager()
71              .getEncryptionStrategy("PBE");
72          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
73          props.put(MuleProperties.MULE_USER_PROPERTY, header);
74  
75          UMOMessage m = client.send("vm://my.queue", "foo", props);
76          assertNotNull(m);
77          assertNull(m.getExceptionPayload());
78      }
79  
80      public void testAuthenticationFailureBadCredentialsHttp() throws Exception
81      {
82          MuleClient client = new MuleClient();
83          Map props = new HashMap();
84          UMOEncryptionStrategy strategy = MuleManager.getInstance()
85              .getSecurityManager()
86              .getEncryptionStrategy("PBE");
87          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
88          props.put(MuleProperties.MULE_USER_PROPERTY, header);
89  
90          UMOMessage m = client.send("http://localhost:4567/index.html", "", props);
91          assertNotNull(m);
92  
93          int status = m.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
94          assertEquals(HttpConstants.SC_UNAUTHORIZED, status);
95      }
96  
97      public void testAuthenticationAuthorisedHttp() throws Exception
98      {
99          MuleClient client = new MuleClient();
100 
101         Map props = new HashMap();
102         UMOEncryptionStrategy strategy = MuleManager.getInstance()
103             .getSecurityManager()
104             .getEncryptionStrategy("PBE");
105         String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
106         props.put(MuleProperties.MULE_USER_PROPERTY, header);
107 
108         UMOMessage m = client.send("http://localhost:4567/index.html", "", props);
109         assertNotNull(m);
110         int status = m.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
111         assertEquals(HttpConstants.SC_OK, status);
112     }
113 
114 }