View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.encryption;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.api.security.CryptoFailureException;
11  import org.mule.api.transformer.Transformer;
12  import org.mule.security.PasswordBasedEncryptionStrategy;
13  import org.mule.transformer.AbstractTransformerTestCase;
14  
15  import java.io.ByteArrayInputStream;
16  import java.util.Arrays;
17  
18  import static org.junit.Assert.fail;
19  
20  public class EncryptionTransformerTestCase extends AbstractTransformerTestCase
21  {
22      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";
23      
24      private PasswordBasedEncryptionStrategy strat;
25  
26      @Override
27      protected void doSetUp() throws Exception
28      {
29          strat = new PasswordBasedEncryptionStrategy();
30          strat.setPassword("mule");
31          strat.initialise();
32      }
33  
34      @Override
35      public Object getResultData()
36      {
37          try
38          {
39              return new ByteArrayInputStream(strat.encrypt(TEST_DATA.getBytes(), null));
40          }
41          catch (CryptoFailureException e)
42          {
43              fail(e.getMessage());
44              return null;
45          }
46      }
47  
48      @Override
49      public Object getTestData()
50      {
51          return new ByteArrayInputStream(TEST_DATA.getBytes());
52      }
53  
54      @Override
55      public Transformer getTransformer()
56      {
57          EncryptionTransformer transformer = new EncryptionTransformer();
58          transformer.setStrategy(strat);
59          try
60          {
61              transformer.initialise();
62          }
63          catch (InitialisationException e)
64          {
65              fail(e.getMessage());
66          }
67          return transformer;
68      }
69  
70      @Override
71      public Transformer getRoundTripTransformer()
72      {
73          DecryptionTransformer transformer = new DecryptionTransformer();
74          transformer.setStrategy(strat);
75          try
76          {
77              transformer.initialise();
78          }
79          catch (InitialisationException e)
80          {
81              fail(e.getMessage());
82          }
83          return transformer;
84      }
85  
86      @Override
87      public boolean compareResults(Object src, Object result)
88      {
89          if (src == null && result == null)
90          {
91              return true;
92          }
93  
94          if (src == null || result == null)
95          {
96              return false;
97          }
98  
99          if (src instanceof byte[] && result instanceof byte[])
100         {
101             return Arrays.equals((byte[]) src, (byte[]) result);
102         }
103         else
104         {
105             return super.compareResults(src, result);
106         }
107     }
108 }