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