View Javadoc

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