View Javadoc
1   /*
2    * Copyright 2019-2021 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Hakan Altindag
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  }