|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.http.apache.internal.conn;
|
17 | 17 |
|
18 |
| -import static org.junit.Assert.assertTrue; |
19 |
| -import static org.junit.Assert.fail; |
20 |
| - |
21 |
| -import java.io.IOException; |
22 |
| -import java.security.NoSuchAlgorithmException; |
23 |
| -import java.util.ArrayList; |
24 |
| -import java.util.Arrays; |
25 |
| -import java.util.Collections; |
26 |
| -import java.util.List; |
| 18 | +import static org.mockito.Matchers.any; |
| 19 | +import static org.mockito.Mockito.never; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
27 | 23 | import javax.net.ssl.SSLContext;
|
28 | 24 | import javax.net.ssl.SSLSocket;
|
29 |
| -import org.junit.Test; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.mockito.Mockito; |
30 | 28 |
|
31 |
| -public class SdkTlsSocketFactoryTest { |
32 |
| - /** |
33 |
| - * Test when the edge case when the both supported and enabled protocols are null. |
34 |
| - */ |
35 |
| - @Test |
36 |
| - public void preparedSocket_NullProtocols() throws NoSuchAlgorithmException, IOException { |
37 |
| - SdkTlsSocketFactory f = new SdkTlsSocketFactory(SSLContext.getDefault(), null); |
38 |
| - try (SSLSocket socket = new TestSSLSocket() { |
39 |
| - @Override |
40 |
| - public String[] getSupportedProtocols() { |
41 |
| - return null; |
42 |
| - } |
43 |
| - |
44 |
| - @Override |
45 |
| - public String[] getEnabledProtocols() { |
46 |
| - return null; |
47 |
| - } |
48 |
| - |
49 |
| - @Override |
50 |
| - public void setEnabledProtocols(String[] protocols) { |
51 |
| - fail(); |
52 |
| - } |
53 |
| - }) { |
54 |
| - f.prepareSocket(socket); |
55 |
| - } |
| 29 | +class SdkTlsSocketFactoryTest { |
| 30 | + |
| 31 | + SdkTlsSocketFactory factory; |
| 32 | + SSLSocket socket; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void before() throws Exception { |
| 36 | + factory = new SdkTlsSocketFactory(SSLContext.getDefault(), null); |
| 37 | + socket = Mockito.mock(SSLSocket.class); |
56 | 38 | }
|
57 | 39 |
|
58 | 40 | @Test
|
59 |
| - public void typical() throws NoSuchAlgorithmException, IOException { |
60 |
| - SdkTlsSocketFactory f = new SdkTlsSocketFactory(SSLContext.getDefault(), null); |
61 |
| - try (SSLSocket socket = new TestSSLSocket() { |
62 |
| - @Override |
63 |
| - public String[] getSupportedProtocols() { |
64 |
| - return shuffle(new String[] {"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}); |
65 |
| - } |
66 |
| - |
67 |
| - @Override |
68 |
| - public String[] getEnabledProtocols() { |
69 |
| - return shuffle(new String[] {"SSLv3", "TLSv1"}); |
70 |
| - } |
71 |
| - |
72 |
| - @Override |
73 |
| - public void setEnabledProtocols(String[] protocols) { |
74 |
| - assertTrue(Arrays.equals(protocols, new String[] {"TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3"})); |
75 |
| - } |
76 |
| - }) { |
77 |
| - f.prepareSocket(socket); |
78 |
| - } |
| 41 | + void nullProtocols() { |
| 42 | + when(socket.getSupportedProtocols()).thenReturn(null); |
| 43 | + when(socket.getEnabledProtocols()).thenReturn(null); |
| 44 | + |
| 45 | + factory.prepareSocket(socket); |
| 46 | + |
| 47 | + verify(socket, never()).setEnabledProtocols(any()); |
79 | 48 | }
|
80 | 49 |
|
81 | 50 | @Test
|
82 |
| - public void noTLS() throws NoSuchAlgorithmException, IOException { |
83 |
| - SdkTlsSocketFactory f = new SdkTlsSocketFactory(SSLContext.getDefault(), null); |
84 |
| - try (SSLSocket socket = new TestSSLSocket() { |
85 |
| - @Override |
86 |
| - public String[] getSupportedProtocols() { |
87 |
| - return shuffle(new String[] {"SSLv2Hello", "SSLv3"}); |
88 |
| - } |
89 |
| - |
90 |
| - @Override |
91 |
| - public String[] getEnabledProtocols() { |
92 |
| - return new String[] {"SSLv3"}; |
93 |
| - } |
94 |
| - |
95 |
| - @Override |
96 |
| - public void setEnabledProtocols(String[] protocols) { |
97 |
| - // For backward compatibility |
98 |
| - assertTrue(Arrays.equals(protocols, new String[] {"SSLv3"})); |
99 |
| - } |
100 |
| - }) { |
101 |
| - f.prepareSocket(socket); |
102 |
| - } |
| 51 | + void amazonCorretto_8_0_292_defaultEnabledProtocols() { |
| 52 | + when(socket.getSupportedProtocols()).thenReturn(new String[] { |
| 53 | + "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3", "SSLv2Hello" |
| 54 | + }); |
| 55 | + when(socket.getEnabledProtocols()).thenReturn(new String[] { |
| 56 | + "TLSv1.2", "TLSv1.1", "TLSv1" |
| 57 | + }); |
| 58 | + |
| 59 | + factory.prepareSocket(socket); |
| 60 | + |
| 61 | + verify(socket, never()).setEnabledProtocols(any()); |
103 | 62 | }
|
104 | 63 |
|
105 | 64 | @Test
|
106 |
| - public void notIdeal() throws NoSuchAlgorithmException, IOException { |
107 |
| - SdkTlsSocketFactory f = new SdkTlsSocketFactory(SSLContext.getDefault(), null); |
108 |
| - try (SSLSocket socket = new TestSSLSocket() { |
109 |
| - @Override |
110 |
| - public String[] getSupportedProtocols() { |
111 |
| - return shuffle(new String[] {"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1"}); |
112 |
| - } |
113 |
| - |
114 |
| - @Override |
115 |
| - public String[] getEnabledProtocols() { |
116 |
| - return shuffle(new String[] {"SSLv3", "TLSv1"}); |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void setEnabledProtocols(String[] protocols) { |
121 |
| - assertTrue(Arrays.equals(protocols, new String[] {"TLSv1.1", "TLSv1", "SSLv3"})); |
122 |
| - } |
123 |
| - }) { |
124 |
| - f.prepareSocket(socket); |
125 |
| - } |
| 65 | + void amazonCorretto_11_0_08_defaultEnabledProtocols() { |
| 66 | + when(socket.getSupportedProtocols()).thenReturn(new String[] { |
| 67 | + "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3", "SSLv2Hello" |
| 68 | + }); |
| 69 | + when(socket.getEnabledProtocols()).thenReturn(new String[] { |
| 70 | + "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1" |
| 71 | + }); |
| 72 | + |
| 73 | + factory.prepareSocket(socket); |
| 74 | + |
| 75 | + verify(socket, never()).setEnabledProtocols(any()); |
126 | 76 | }
|
127 | 77 |
|
128 |
| - private String[] shuffle(String[] in) { |
129 |
| - List<String> list = new ArrayList<String>(Arrays.asList(in)); |
130 |
| - Collections.shuffle(list); |
131 |
| - return list.toArray(new String[0]); |
| 78 | + @Test |
| 79 | + void amazonCorretto_17_0_1_defaultEnabledProtocols() { |
| 80 | + when(socket.getSupportedProtocols()).thenReturn(new String[] { |
| 81 | + "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3", "SSLv2Hello" |
| 82 | + }); |
| 83 | + when(socket.getEnabledProtocols()).thenReturn(new String[] { |
| 84 | + "TLSv1.3", "TLSv1.2" |
| 85 | + }); |
| 86 | + |
| 87 | + factory.prepareSocket(socket); |
| 88 | + |
| 89 | + verify(socket, never()).setEnabledProtocols(any()); |
132 | 90 | }
|
133 | 91 | }
|
0 commit comments