1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.spring.security;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16
17 import org.mule.api.EncryptionStrategy;
18 import org.mule.api.MuleMessage;
19 import org.mule.api.config.MuleProperties;
20 import org.mule.api.security.CredentialsNotSetException;
21 import org.mule.api.security.CryptoFailureException;
22 import org.mule.security.MuleCredentials;
23 import org.mule.tck.AbstractServiceAndFlowTestCase;
24 import org.mule.transport.http.HttpConnector;
25 import org.mule.transport.http.HttpConstants;
26
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.junit.Test;
33 import org.junit.runners.Parameterized.Parameters;
34
35 public class EncryptionFunctionalTestCase extends AbstractServiceAndFlowTestCase
36 {
37 public EncryptionFunctionalTestCase(ConfigVariant variant, String configResources)
38 {
39 super(variant, configResources);
40 }
41
42 @Parameters
43 public static Collection<Object[]> parameters()
44 {
45 return Arrays.asList(new Object[][]{
46 {ConfigVariant.SERVICE, "encryption-test-service.xml"},
47 {ConfigVariant.FLOW, "encryption-test-flow.xml"}
48 });
49 }
50
51 @Test
52 public void testAuthenticationFailureNoContext() throws Exception
53 {
54 MuleMessage result = muleContext.getClient().send("vm://my.queue", "foo", null);
55 assertNotNull(result);
56 assertNotNull(result.getExceptionPayload());
57 assertEquals(CredentialsNotSetException.class, result.getExceptionPayload().getException().getClass());
58 }
59
60 @Test
61 public void testAuthenticationFailureBadCredentials() throws Exception
62 {
63 Map<String, Object> props = createMessagePropertiesWithCredentials("anonX", "anonX");
64
65 MuleMessage result = muleContext.getClient().send("vm://my.queue", "foo", null);
66 assertNotNull(result);
67 assertNotNull(result.getExceptionPayload());
68 assertEquals(CredentialsNotSetException.class, result.getExceptionPayload().getException().getClass());
69 }
70
71 @Test
72 public void testAuthenticationAuthorised() throws Exception
73 {
74 Map<String, Object> props = createMessagePropertiesWithCredentials("anon", "anon");
75 MuleMessage m = muleContext.getClient().send("vm://my.queue", "foo", props);
76 assertNotNull(m);
77 assertNull(m.getExceptionPayload());
78 }
79
80 @Test
81 public void testAuthenticationFailureBadCredentialsHttp() throws Exception
82 {
83 Map<String, Object> props = createMessagePropertiesWithCredentials("anonX", "anonX");
84 MuleMessage m = muleContext.getClient().send("http://localhost:4567/index.html", "", props);
85 assertNotNull(m);
86
87 int status = m.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
88 assertEquals(HttpConstants.SC_UNAUTHORIZED, status);
89 }
90
91 @Test
92 public void testAuthenticationAuthorisedHttp() throws Exception
93 {
94 Map<String, Object> props = createMessagePropertiesWithCredentials("anon", "anon");
95 MuleMessage m = muleContext.getClient().send("http://localhost:4567/index.html", "", props);
96 assertNotNull(m);
97 int status = m.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, -1);
98 assertEquals(HttpConstants.SC_OK, status);
99 }
100
101 private Map<String, Object> createMessagePropertiesWithCredentials(String username, String password) throws CryptoFailureException
102 {
103 Map<String, Object> props = new HashMap<String, Object>();
104 EncryptionStrategy strategy = muleContext.getSecurityManager().getEncryptionStrategy("PBE");
105 String header = MuleCredentials.createHeader(username, password, "PBE", strategy);
106 props.put(MuleProperties.MULE_USER_PROPERTY, header);
107 return props;
108 }
109 }