View Javadoc

1   /*
2    * $Id: PGPSecurityFilterTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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          //poll for the output file; wait for a max of 5 seconds
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              // check if file exists
72              FileReader outputFile = new FileReader(DIRECTORY + TARGET);
73              String fileContents = IOUtils.toString(outputFile);
74              outputFile.close();
75              
76              // see the GenerateTestMessage class for the content of the message
77              assertTrue(fileContents.contains("This is a test message")); 
78              
79              // delete file not to be confused with tests to be performed later
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     // see MULE-3672
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 }