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.util;
18  
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.extension.ExtendWith;
21  import org.mockito.junit.jupiter.MockitoExtension;
22  
23  import javax.net.ssl.SSLContext;
24  import javax.net.ssl.SSLParameters;
25  import javax.net.ssl.SSLServerSocketFactory;
26  import javax.net.ssl.SSLSocketFactory;
27  import java.security.NoSuchAlgorithmException;
28  
29  import static org.assertj.core.api.Assertions.assertThat;
30  import static org.mockito.Mockito.spy;
31  import static org.mockito.Mockito.times;
32  import static org.mockito.Mockito.verify;
33  
34  /**
35   * @author Hakan Altindag
36   */
37  @ExtendWith(MockitoExtension.class)
38  class SSLSocketUtilsShould {
39  
40      @Test
41      void createSslSocketFactory() throws NoSuchAlgorithmException {
42          SSLParameters sslParameters = spy(
43                  new SSLParameters(
44                          new String[] {"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
45                          new String[] {"TLSv1.2"}
46                  )
47          );
48  
49          SSLSocketFactory socketFactory = SSLContext.getDefault().getSocketFactory();
50  
51          SSLSocketFactory victim = SSLSocketUtils.createSslSocketFactory(socketFactory, sslParameters);
52          String[] defaultCipherSuites = victim.getDefaultCipherSuites();
53  
54          assertThat(defaultCipherSuites).containsExactly("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384");
55          verify(sslParameters, times(1)).getCipherSuites();
56      }
57  
58      @Test
59      void createSslServerSocketFactory() throws NoSuchAlgorithmException {
60          SSLParameters sslParameters = spy(
61                  new SSLParameters(
62                          new String[] {"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
63                          new String[] {"TLSv1.2"}
64                  )
65          );
66  
67          SSLServerSocketFactory socketFactory = SSLContext.getDefault().getServerSocketFactory();
68  
69          SSLServerSocketFactory victim = SSLSocketUtils.createSslServerSocketFactory(socketFactory, sslParameters);
70          String[] defaultCipherSuites = victim.getDefaultCipherSuites();
71  
72          assertThat(defaultCipherSuites).containsExactly("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384");
73          verify(sslParameters, times(1)).getCipherSuites();
74      }
75  
76  }