View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.acegi;
8   
9   import org.mule.api.EncryptionStrategy;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.security.CredentialsNotSetException;
13  import org.mule.api.security.UnauthorisedException;
14  import org.mule.config.ExceptionHelper;
15  import org.mule.module.client.MuleClient;
16  import org.mule.security.MuleCredentials;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  import org.mule.transport.http.HttpConnector;
19  import org.mule.transport.http.HttpConstants;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertNull;
29  
30  public class EncryptionFunctionalTestCase extends FunctionalTestCase
31  {
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "encryption-test.xml";
37      }
38  
39      @Test
40      public void testAuthenticationFailureNoContext() throws Exception
41      {
42          MuleClient client = new MuleClient(muleContext);
43          MuleMessage m = client.send("vm://my.queue", "foo", null);
44          assertNotNull(m);
45          assertNotNull(m.getExceptionPayload());
46          assertEquals(ExceptionHelper.getErrorCode(CredentialsNotSetException.class), m.getExceptionPayload()
47              .getCode());
48      }
49  
50      @Test
51      public void testAuthenticationFailureBadCredentials() throws Exception
52      {
53          MuleClient client = new MuleClient(muleContext);
54          Map props = new HashMap();
55          EncryptionStrategy strategy = muleContext
56              .getSecurityManager()
57              .getEncryptionStrategy("PBE");
58          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
59          props.put(MuleProperties.MULE_USER_PROPERTY, header);
60  
61          MuleMessage m = client.send("vm://my.queue", "foo", props);
62          assertNotNull(m);
63          assertNotNull(m.getExceptionPayload());
64          assertEquals(ExceptionHelper.getErrorCode(UnauthorisedException.class), m.getExceptionPayload()
65              .getCode());
66      }
67  
68      @Test
69      public void testAuthenticationAuthorised() throws Exception
70      {
71          MuleClient client = new MuleClient(muleContext);
72  
73          Map props = new HashMap();
74          EncryptionStrategy strategy = muleContext
75              .getSecurityManager()
76              .getEncryptionStrategy("PBE");
77          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
78          props.put(MuleProperties.MULE_USER_PROPERTY, header);
79  
80          MuleMessage m = client.send("vm://my.queue", "foo", props);
81          assertNotNull(m);
82          assertNull(m.getExceptionPayload());
83      }
84  
85      @Test
86      public void testAuthenticationFailureBadCredentialsHttp() throws Exception
87      {
88          MuleClient client = new MuleClient(muleContext);
89          Map props = new HashMap();
90          EncryptionStrategy strategy = muleContext
91              .getSecurityManager()
92              .getEncryptionStrategy("PBE");
93          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
94          props.put(MuleProperties.MULE_USER_PROPERTY, header);
95  
96          MuleMessage m = client.send("http://localhost:4567/index.html", "", props);
97          assertNotNull(m);
98  
99          int status = m.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
100         assertEquals(HttpConstants.SC_UNAUTHORIZED, status);
101     }
102 
103     @Test
104     public void testAuthenticationAuthorisedHttp() throws Exception
105     {
106         MuleClient client = new MuleClient(muleContext);
107 
108         Map props = new HashMap();
109         EncryptionStrategy strategy = muleContext
110             .getSecurityManager()
111             .getEncryptionStrategy("PBE");
112         String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
113         props.put(MuleProperties.MULE_USER_PROPERTY, header);
114 
115         MuleMessage m = client.send("http://localhost:4567/index.html", "", props);
116         assertNotNull(m);
117         int status = m.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
118         assertEquals(HttpConstants.SC_OK, status);
119     }
120 
121 }