View Javadoc

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