1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package nl.altindag.ssl.keymanager;
18
19 import nl.altindag.log.LogCaptor;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.junit.jupiter.MockitoExtension;
23
24 import javax.net.ssl.KeyManagerFactory;
25 import javax.net.ssl.X509ExtendedKeyManager;
26
27 import java.security.InvalidAlgorithmParameterException;
28 import java.security.KeyStoreException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.Provider;
31 import java.security.UnrecoverableKeyException;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35
36
37
38
39 @ExtendWith(MockitoExtension.class)
40 class KeyManagerFactoryWrapperShould {
41
42 private final LogCaptor logCaptor = LogCaptor.forName("nl.altindag.ssl.keymanager");
43
44 @Test
45 void createInstanceFromKeyManager() {
46 X509ExtendedKeyManager keyManager = mock(X509ExtendedKeyManager.class);
47 KeyManagerFactoryWrapper keyManagerFactory = new KeyManagerFactoryWrapper(keyManager);
48
49 assertThat(keyManagerFactory)
50 .isNotNull()
51 .isInstanceOf(KeyManagerFactory.class);
52
53 assertThat(keyManagerFactory.getAlgorithm()).isEqualTo("no-algorithm");
54 assertThat(keyManagerFactory.getKeyManagers()).containsExactly(keyManager);
55
56 Provider provider = keyManagerFactory.getProvider();
57 assertThat(provider.getName()).isEmpty();
58 assertThat(provider.getInfo()).isEmpty();
59 assertThat(provider.getVersion()).isEqualTo(1.0);
60 }
61
62 @Test
63 void ignoreProvidedKeyStore() throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
64 X509ExtendedKeyManager keyManager = mock(X509ExtendedKeyManager.class);
65 KeyManagerFactoryWrapper keyManagerFactory = new KeyManagerFactoryWrapper(keyManager);
66
67 keyManagerFactory.init(null, null);
68 assertThat(logCaptor.getInfoLogs()).contains("Ignoring provided KeyStore");
69 }
70 @Test
71 void ignoreProvidedManagerFactoryParameters() throws InvalidAlgorithmParameterException {
72 X509ExtendedKeyManager keyManager = mock(X509ExtendedKeyManager.class);
73 KeyManagerFactoryWrapper keyManagerFactory = new KeyManagerFactoryWrapper(keyManager);
74
75 keyManagerFactory.init(null);
76 assertThat(logCaptor.getInfoLogs()).contains("Ignoring provided ManagerFactoryParameters");
77 }
78
79 }