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
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.net.URL;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 public class PGPSecurityFilterTestCase extends FunctionalTestCase
28 {
29
30 protected static final String TARGET = "/encrypted.txt";
31 protected static final String DIRECTORY = "output";
32 protected static final String MESSAGE_EXCEPTION = "No signed message found. Message payload is of type: String";
33
34 protected String getConfigResources()
35 {
36 return "test-pgp-encrypt-config.xml";
37 }
38
39 public void testAuthenticationAuthorised() throws Exception
40 {
41 URL url = Thread.currentThread().getContextClassLoader().getResource("./encrypted-signed.asc");
42
43
44 int length = (int) FileUtils.newFile(url.getFile()).length();
45 byte[] msg = new byte[length];
46
47 FileInputStream in = new FileInputStream(url.getFile());
48 in.read(msg);
49 in.close();
50
51 Map props = new HashMap();
52 props.put("TARGET_FILE", TARGET);
53 MuleClient client = new MuleClient();
54 MuleMessage reply = client.send("vm://echo", new String(msg), props);
55 assertNull(reply.getExceptionPayload());
56
57 try
58 {
59
60 FileReader outputFile = new FileReader(DIRECTORY+TARGET);
61 outputFile.close();
62
63
64 File f = FileUtils.newFile(DIRECTORY+TARGET);
65 f.delete();
66 }
67 catch (FileNotFoundException fileNotFound)
68 {
69 fail("File not successfully created");
70 }
71 }
72
73 public void testAuthenticationNotAuthorised() throws Exception
74 {
75 MuleClient client = new MuleClient();
76
77 MuleMessage reply = client.send("vm://echo", "An unsigned message", null);
78
79 assertNotNull(reply.getExceptionPayload());
80 ExceptionPayload excPayload = reply.getExceptionPayload();
81 assertEquals(MESSAGE_EXCEPTION, excPayload.getMessage());
82
83 }
84 }