1   /*
2    * $Id:EncryptionTransformerTestCase.java 5937 2007-04-09 22:35:04Z rossmason $
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.transformer.encryption;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.security.CryptoFailureException;
15  import org.mule.api.transformer.Transformer;
16  import org.mule.security.PasswordBasedEncryptionStrategy;
17  import org.mule.transformer.AbstractTransformerTestCase;
18  
19  import java.util.Arrays;
20  
21  public class EncryptionTransformerTestCase extends AbstractTransformerTestCase
22  {
23      private static final String TEST_DATA = "the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog";
24      
25      private PasswordBasedEncryptionStrategy strat;
26  
27      // @Override
28      protected void doSetUp() throws Exception
29      {
30          strat = new PasswordBasedEncryptionStrategy();
31          strat.setPassword("mule");
32          strat.initialise();
33      }
34  
35      public Object getResultData()
36      {
37          try
38          {
39              return strat.encrypt(TEST_DATA.getBytes(), null);
40          }
41          catch (CryptoFailureException e)
42          {
43              fail(e.getMessage());
44              return null;
45          }
46      }
47  
48      public Object getTestData()
49      {
50          return TEST_DATA;
51      }
52  
53      public Transformer getTransformer()
54      {
55          EncryptionTransformer transformer = new EncryptionTransformer();
56          transformer.setStrategy(strat);
57          try
58          {
59              transformer.initialise();
60          }
61          catch (InitialisationException e)
62          {
63              fail(e.getMessage());
64          }
65          return transformer;
66      }
67  
68      public Transformer getRoundTripTransformer()
69      {
70          DecryptionTransformer transformer = new DecryptionTransformer();
71          transformer.setStrategy(strat);
72          transformer.setReturnClass(String.class);
73          try
74          {
75              transformer.initialise();
76          }
77          catch (InitialisationException e)
78          {
79              fail(e.getMessage());
80          }
81          return transformer;
82      }
83  
84      // @Override
85      public boolean compareResults(Object src, Object result)
86      {
87          if (src == null && result == null)
88          {
89              return true;
90          }
91  
92          if (src == null || result == null)
93          {
94              return false;
95          }
96  
97          if (src instanceof byte[] && result instanceof byte[])
98          {
99              return Arrays.equals((byte[]) src, (byte[]) result);
100         }
101         else
102         {
103             return super.compareResults(src, result);
104         }
105     }
106 
107 }