1   /*
2    * $Id: PGPSecurityFilterTestCase.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
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              //check if file exists
60              FileReader outputFile = new FileReader(DIRECTORY+TARGET);
61              outputFile.close();
62              
63              //delete file not to be confused with tests to be performed later
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  }