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