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