View Javadoc

1   /*
2    * $Id: AbstractEncryptionStrategyTestCase.java 20310 2010-11-24 10:40:35Z esteban.robles $
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.module.pgp;
12  
13  import org.mule.security.MuleHeaderCredentialsAccessor;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.net.URL;
17  import java.security.NoSuchAlgorithmException;
18  
19  import javax.crypto.Cipher;
20  
21  public abstract class AbstractEncryptionStrategyTestCase extends AbstractMuleTestCase
22  {
23      protected KeyBasedEncryptionStrategy kbStrategy;
24      protected PGPKeyRing keyManager;
25  
26      protected static boolean isCryptographyExtensionInstalled()
27      {
28          // see MULE-3671
29          try
30          {
31              int maxKeyLength = Cipher.getMaxAllowedKeyLength("DES/CBC/PKCS5Padding");
32              // if JCE is not installed, maxKeyLength will be 64
33              return maxKeyLength == Integer.MAX_VALUE;
34          }
35          catch (NoSuchAlgorithmException e)
36          {
37              throw new AssertionError(e);
38          }
39      }
40      
41      @Override
42      protected boolean isDisabledInThisEnvironment()
43      {
44          return (isCryptographyExtensionInstalled() == false);
45      }
46  
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          PGPKeyRingImpl keyM = new PGPKeyRingImpl();
51          ClassLoader loader = Thread.currentThread().getContextClassLoader();
52          
53          URL url = loader.getResource("./serverPublic.gpg");
54          keyM.setPublicKeyRingFileName(url.getFile());
55      
56          url = loader.getResource("./serverPrivate.gpg");
57          keyM.setSecretKeyRingFileName(url.getFile());
58      
59          keyM.setSecretAliasId("6247672658342245276");
60          keyM.setSecretPassphrase("TestingPassphrase");
61          keyM.initialise();
62      
63          kbStrategy = new KeyBasedEncryptionStrategy();
64          kbStrategy.setKeyManager(keyM);
65          kbStrategy.setCredentialsAccessor(new FakeCredentialAccessor("Mule server <mule_server@mule.com>"));
66          kbStrategy.initialise();
67          
68          keyManager = keyM;
69      }
70  
71      @Override
72      protected void doTearDown() throws Exception
73      {
74          kbStrategy = null;
75          keyManager = null;
76      }
77  }
78  
79