1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.pgp;
12
13 import org.mule.api.ExceptionPayload;
14 import org.mule.api.MuleMessage;
15 import org.mule.api.client.MuleClient;
16 import org.mule.api.config.MuleProperties;
17 import org.mule.tck.AbstractServiceAndFlowTestCase;
18 import org.mule.util.IOUtils;
19
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.net.URL;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.Test;
29 import org.junit.runners.Parameterized.Parameters;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33
34 public class PGPSecurityFilterTestCase extends AbstractServiceAndFlowTestCase
35 {
36 protected static final String TARGET = "/encrypted.txt";
37 protected static final String DIRECTORY = "output";
38 protected static final String MESSAGE_EXCEPTION = "Crypto Failure";
39
40 @Parameters
41 public static Collection<Object[]> parameters()
42 {
43 return Arrays.asList(new Object[][]{
44 {ConfigVariant.SERVICE, "test-pgp-encrypt-config-service.xml"},
45 {ConfigVariant.FLOW, "test-pgp-encrypt-config-flow.xml"}});
46 }
47
48 public PGPSecurityFilterTestCase(ConfigVariant variant, String configResources)
49 {
50 super(variant, configResources);
51 }
52
53 @Override
54 protected boolean isDisabledInThisEnvironment()
55 {
56 return (AbstractEncryptionStrategyTestCase.isCryptographyExtensionInstalled() == false);
57 }
58
59 @Test
60 public void testAuthenticationAuthorised() throws Exception
61 {
62 MuleClient client = muleContext.getClient();
63
64 byte[] msg = loadEncryptedMessage();
65 Map<String, Object> props = createMessageProperties();
66
67 client.dispatch("vm://echo", new String(msg), props);
68
69 MuleMessage message = client.request("vm://output", RECEIVE_TIMEOUT);
70 assertEquals("This is a test message.\r\nThis is another line.\r\n", message.getPayloadAsString());
71 }
72
73 @Test
74 public void testAuthenticationNotAuthorised() throws Exception
75 {
76 Map<String, Object> props = createMessageProperties();
77 MuleMessage reply = muleContext.getClient().send("vm://echo", "An unsigned message", props);
78 assertNotNull(reply.getExceptionPayload());
79 ExceptionPayload excPayload = reply.getExceptionPayload();
80 assertEquals(MESSAGE_EXCEPTION, excPayload.getMessage());
81 }
82
83 private byte[] loadEncryptedMessage() throws IOException
84 {
85 URL url = Thread.currentThread().getContextClassLoader().getResource("./encrypted-signed.asc");
86
87 FileInputStream in = new FileInputStream(url.getFile());
88 byte[] msg = IOUtils.toByteArray(in);
89 in.close();
90
91 return msg;
92 }
93
94 private Map<String, Object> createMessageProperties()
95 {
96 Map<String, Object> props = new HashMap<String, Object>();
97 props.put("TARGET_FILE", TARGET);
98 props.put(MuleProperties.MULE_USER_PROPERTY, "Mule server <mule_server@mule.com>");
99 return props;
100 }
101 }