View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.common.crypto;
21  
22  import java.io.InputStream;
23  import java.security.KeyStore;
24  
25  import org.apache.wss4j.common.util.Loader;
26  import org.junit.jupiter.api.AfterAll;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.RepeatedTest;
29  
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  
32  /**
33   * Some tests for the Merlin Crypto provider
34   */
35  public class MerlinTest {
36  
37      private static Merlin jksCrypto = new Merlin();
38      private static Merlin pkcs12Crypto = new Merlin();
39  
40      @BeforeAll
41      public static void setup() throws Exception {
42          WSProviderConfig.init();
43          KeyStore keyStore = loadKeyStore("keys/wss40.jks", "security");
44          jksCrypto.setKeyStore(keyStore);
45  
46          KeyStore pkcs12KeyStore = loadKeyStore("keys/wss40.p12", "security");
47          pkcs12Crypto.setKeyStore(pkcs12KeyStore);
48      }
49  
50      @AfterAll
51      public static void cleanup() {
52          jksCrypto.clearCache();
53          pkcs12Crypto.clearCache();
54      }
55  
56      @RepeatedTest(1000)
57      public void testGetPrivateKeyJKS() throws Exception {
58          assertNotNull(jksCrypto.getPrivateKey("wss40", "security"));
59      }
60  
61      @RepeatedTest(1000)
62      public void testGetPrivateKeyPKCS12() throws Exception {
63          assertNotNull(pkcs12Crypto.getPrivateKey("wss40", "security"));
64      }
65  
66      @RepeatedTest(1000)
67      public void testGetCertificateJKS() throws Exception {
68          CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
69          cryptoType.setAlias("wss40");
70          assertNotNull(jksCrypto.getX509Certificates(cryptoType));
71      }
72  
73      @RepeatedTest(1000)
74      public void testGetCertificatePKCS12() throws Exception {
75          CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
76          cryptoType.setAlias("wss40");
77          assertNotNull(pkcs12Crypto.getX509Certificates(cryptoType));
78      }
79  
80      private static KeyStore loadKeyStore(String path, String password) throws Exception {
81          KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
82          ClassLoader loader = Loader.getClassLoader(MerlinTest.class);
83          InputStream input = Merlin.loadInputStream(loader, path);
84          keyStore.load(input, password.toCharArray());
85          input.close();
86  
87          return keyStore;
88      }
89  }