1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }