View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.pgp;
8   
9   import org.mule.api.ExceptionPayload;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.MuleClient;
12  import org.mule.api.config.MuleProperties;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.util.IOUtils;
15  
16  import java.io.FileInputStream;
17  import java.io.IOException;
18  import java.net.URL;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertNull;
27  
28  public class PGPSecurityFilterTestCase extends FunctionalTestCase
29  {
30      protected static final String TARGET = "/encrypted.txt";
31      protected static final String DIRECTORY = "output";
32      protected static final String MESSAGE_EXCEPTION = "Crypto Failure";
33  
34      @Override
35      protected boolean isDisabledInThisEnvironment()
36      {
37          return (AbstractEncryptionStrategyTestCase.isCryptographyExtensionInstalled() == false);
38      }
39  
40      @Override
41      protected String getConfigResources()
42      {
43          return "test-pgp-encrypt-config.xml";
44      }
45  
46      @Test
47      public void testAuthenticationAuthorised() throws Exception
48      {
49          MuleClient client = muleContext.getClient();
50  
51          byte[] msg = loadEncryptedMessage();
52          Map<String, Object> props = createMessageProperties();
53  
54          MuleMessage reply = client.send("vm://echo", new String(msg), props);
55          assertNull(reply.getExceptionPayload());
56  
57          MuleMessage message = client.request("vm://output", RECEIVE_TIMEOUT);
58          assertEquals("This is a test message.\r\nThis is another line.\r\n", message.getPayloadAsString());
59      }
60  
61      @Test
62      public void testAuthenticationNotAuthorised() throws Exception
63      {
64          Map<String, Object> props = createMessageProperties();
65          MuleMessage reply = muleContext.getClient().send("vm://echo", "An unsigned message", props);
66          assertNotNull(reply.getExceptionPayload());
67          ExceptionPayload excPayload = reply.getExceptionPayload();
68          assertEquals(MESSAGE_EXCEPTION, excPayload.getMessage());
69      }
70  
71      private byte[] loadEncryptedMessage() throws IOException
72      {
73          URL url = Thread.currentThread().getContextClassLoader().getResource("./encrypted-signed.asc");
74  
75          FileInputStream in = new FileInputStream(url.getFile());
76          byte[] msg = IOUtils.toByteArray(in);
77          in.close();
78  
79          return msg;
80      }
81  
82      private Map<String, Object> createMessageProperties()
83      {
84          Map<String, Object> props = new HashMap<String, Object>();
85          props.put("TARGET_FILE", TARGET);
86          props.put(MuleProperties.MULE_USER_PROPERTY, "Mule server <mule_server@mule.com>");
87          return props;
88      }
89  
90  }