View Javadoc

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