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.config.MuleProperties;
16 import org.mule.module.client.MuleClient;
17 import org.mule.tck.FunctionalTestCase;
18 import org.mule.util.FileUtils;
19 import org.mule.util.IOUtils;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.net.URL;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 public class PGPSecurityFilterTestCase extends FunctionalTestCase
31 {
32 protected static final String TARGET = "/encrypted.txt";
33 protected static final String DIRECTORY = "output";
34 protected static final String MESSAGE_EXCEPTION = "The required object/property \"UserId\" is null. Message payload is of type: String";
35
36 @Override
37 protected boolean isDisabledInThisEnvironment()
38 {
39 return (AbstractEncryptionStrategyTestCase.isCryptographyExtensionInstalled() == false);
40 }
41
42 protected String getConfigResources()
43 {
44 return "test-pgp-encrypt-config.xml";
45 }
46
47 public void testAuthenticationAuthorised() throws Exception
48 {
49 byte[] msg = loadEncryptedMessage();
50
51 Map<String, String> props = new HashMap<String, String>();
52 props.put("TARGET_FILE", TARGET);
53 props.put(MuleProperties.MULE_USER_PROPERTY, "Mule server <mule_server@mule.com>");
54
55 MuleClient client = new MuleClient(muleContext);
56 MuleMessage reply = client.send("vm://echo", new String(msg), props);
57 assertNull(reply.getExceptionPayload());
58
59
60 File pollingFile = null;
61 for(int i = 0; i < 5; i++)
62 {
63 pollingFile = new File(DIRECTORY + TARGET);
64 if(!pollingFile.exists())
65 {
66 Thread.sleep(1000);
67 }
68 }
69 pollingFile = null;
70
71 try
72 {
73
74 FileReader outputFile = new FileReader(DIRECTORY + TARGET);
75 String fileContents = IOUtils.toString(outputFile);
76 outputFile.close();
77
78
79 assertTrue(fileContents.contains("This is a test message"));
80
81
82 File f = FileUtils.newFile(DIRECTORY + TARGET);
83 assertTrue("Deleting the output file failed", f.delete());
84 }
85 catch (FileNotFoundException fileNotFound)
86 {
87 fail("File not successfully created");
88 }
89 }
90
91 private byte[] loadEncryptedMessage() throws IOException
92 {
93 URL url = Thread.currentThread().getContextClassLoader().getResource("./encrypted-signed.asc");
94
95 FileInputStream in = new FileInputStream(url.getFile());
96 byte[] msg = IOUtils.toByteArray(in);
97 in.close();
98
99 return msg;
100 }
101
102
103 public void testAuthenticationNotAuthorised() throws Exception
104 {
105 MuleClient client = new MuleClient(muleContext);
106
107 MuleMessage reply = client.send("vm://echo", "An unsigned message", null);
108
109 assertNotNull(reply.getExceptionPayload());
110 ExceptionPayload excPayload = reply.getExceptionPayload();
111 assertEquals(MESSAGE_EXCEPTION, excPayload.getMessage());
112 }
113 }