1   /*
2    * $Id: EncryptionTransformerTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.transformers.encryption;
12  
13  import org.mule.impl.security.PasswordBasedEncryptionStrategy;
14  import org.mule.tck.AbstractTransformerTestCase;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.security.CryptoFailureException;
17  import org.mule.umo.transformer.UMOTransformer;
18  
19  import java.util.Arrays;
20  
21  public class EncryptionTransformerTestCase extends AbstractTransformerTestCase
22  {
23      private PasswordBasedEncryptionStrategy strat;
24  
25      // @Override
26      protected void doSetUp() throws Exception
27      {
28          strat = new PasswordBasedEncryptionStrategy();
29          strat.setPassword("mule");
30          strat.initialise();
31      }
32  
33      public Object getResultData()
34      {
35          try
36          {
37              return strat.encrypt(getTestData().toString().getBytes(), null);
38          }
39          catch (CryptoFailureException e)
40          {
41              fail(e.getMessage());
42              return null;
43          }
44      }
45  
46      public Object getTestData()
47      {
48          return "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";
49      }
50  
51      public UMOTransformer getTransformer()
52      {
53          EncryptionTransformer transformer = new EncryptionTransformer();
54          transformer.setStrategy(strat);
55          try
56          {
57              transformer.initialise();
58          }
59          catch (InitialisationException e)
60          {
61              fail(e.getMessage());
62          }
63          return transformer;
64      }
65  
66      public UMOTransformer getRoundTripTransformer()
67      {
68          DecryptionTransformer transformer = new DecryptionTransformer();
69          transformer.setStrategy(strat);
70          transformer.setReturnClass(String.class);
71          try
72          {
73              transformer.initialise();
74          }
75          catch (InitialisationException e)
76          {
77              fail(e.getMessage());
78          }
79          return transformer;
80      }
81  
82      // @Override
83      public boolean compareResults(Object src, Object result)
84      {
85          if (src == null && result == null)
86          {
87              return true;
88          }
89  
90          if (src == null || result == null)
91          {
92              return false;
93          }
94  
95          if (src instanceof byte[] && result instanceof byte[])
96          {
97              return Arrays.equals((byte[]) src, (byte[]) result);
98          }
99          else
100         {
101             return super.compareResults(src, result);
102         }
103     }
104 
105     // @Override
106     protected void doTestClone(UMOTransformer original, UMOTransformer clone) throws Exception
107     {
108         super.doTestClone(original, clone);
109 
110         EncryptionTransformer t1 = (EncryptionTransformer) original;
111         EncryptionTransformer t2 = (EncryptionTransformer) clone;
112 
113         // strategyName must be equal
114         assertEquals("strategyName", t1.getStrategyName(), t2.getStrategyName());
115         // strategy instance must be the same (shared);
116         // see AbstractEncryptionTransformer.clone()
117         assertSame("strategy", t1.getStrategy(), t2.getStrategy());
118     }
119 
120 }