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.trustmanager;
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.ManagerFactoryParameters;
25  import javax.net.ssl.TrustManagerFactory;
26  import javax.net.ssl.X509ExtendedTrustManager;
27  import java.security.InvalidAlgorithmParameterException;
28  import java.security.KeyStore;
29  import java.security.KeyStoreException;
30  import java.security.Provider;
31  
32  import static org.assertj.core.api.Assertions.assertThat;
33  import static org.mockito.Mockito.mock;
34  
35  /**
36   * @author Hakan Altindag
37   */
38  @ExtendWith(MockitoExtension.class)
39  class TrustManagerFactoryWrapperShould {
40  
41      private final LogCaptor logCaptor = LogCaptor.forName("nl.altindag.ssl.trustmanager");
42  
43      @Test
44      void createInstanceFromTrustManager() {
45          X509ExtendedTrustManager trustManager = mock(X509ExtendedTrustManager.class);
46          TrustManagerFactoryWrapper trustManagerFactory = new TrustManagerFactoryWrapper(trustManager);
47  
48          assertThat(trustManagerFactory)
49                  .isNotNull()
50                  .isInstanceOf(TrustManagerFactory.class);
51  
52          assertThat(trustManagerFactory.getAlgorithm()).isEqualTo("no-algorithm");
53          assertThat(trustManagerFactory.getTrustManagers()).containsExactly(trustManager);
54  
55          Provider provider = trustManagerFactory.getProvider();
56          assertThat(provider.getName()).isEmpty();
57          assertThat(provider.getInfo()).isEmpty();
58          assertThat(provider.getVersion()).isEqualTo(1.0);
59      }
60  
61      @Test
62      void ignoreProvidedKeyStore() throws KeyStoreException {
63          X509ExtendedTrustManager trustManager = mock(X509ExtendedTrustManager.class);
64          TrustManagerFactoryWrapper trustManagerFactory = new TrustManagerFactoryWrapper(trustManager);
65  
66          trustManagerFactory.init((KeyStore) null);
67          assertThat(logCaptor.getInfoLogs()).contains("Ignoring provided KeyStore");
68      }
69      @Test
70      void ignoreProvidedManagerFactoryParameters() throws InvalidAlgorithmParameterException {
71          X509ExtendedTrustManager trustManager = mock(X509ExtendedTrustManager.class);
72          TrustManagerFactoryWrapper trustManagerFactory = new TrustManagerFactoryWrapper(trustManager);
73  
74          trustManagerFactory.init((ManagerFactoryParameters) null);
75          assertThat(logCaptor.getInfoLogs()).contains("Ignoring provided ManagerFactoryParameters");
76      }
77  
78  }