|
41 | 41 | import javax.net.ssl.SSLContext; |
42 | 42 | import javax.net.ssl.SSLHandshakeException; |
43 | 43 | import javax.net.ssl.SSLParameters; |
| 44 | + |
44 | 45 | import org.java_websocket.WebSocket; |
45 | 46 | import org.java_websocket.client.WebSocketClient; |
46 | 47 | import org.java_websocket.handshake.ClientHandshake; |
|
57 | 58 |
|
58 | 59 | public class Issue997Test { |
59 | 60 |
|
60 | | - @Test() |
61 | | - @Timeout(2000) |
62 | | - public void test_localServer_ServerLocalhost_Client127_CheckActive() |
63 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
64 | | - SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
65 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), |
66 | | - "HTTPS"); |
67 | | - assertFalse(client.onOpen); |
68 | | - assertTrue(client.onSSLError); |
69 | | - } |
70 | | - |
71 | | - @Test() |
72 | | - @Timeout(2000) |
73 | | - public void test_localServer_ServerLocalhost_Client127_CheckInactive() |
74 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
75 | | - SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
76 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), ""); |
77 | | - assertTrue(client.onOpen); |
78 | | - assertFalse(client.onSSLError); |
79 | | - } |
80 | | - |
81 | | - @Test() |
82 | | - @Timeout(2000) |
83 | | - public void test_localServer_ServerLocalhost_Client127_CheckDefault() |
84 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
85 | | - SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
86 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null); |
87 | | - assertFalse(client.onOpen); |
88 | | - assertTrue(client.onSSLError); |
89 | | - } |
90 | | - |
91 | | - @Test() |
92 | | - @Timeout(2000) |
93 | | - public void test_localServer_ServerLocalhost_ClientLocalhost_CheckActive() |
94 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
95 | | - SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
96 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), |
97 | | - "HTTPS"); |
98 | | - assertTrue(client.onOpen); |
99 | | - assertFalse(client.onSSLError); |
100 | | - } |
101 | | - |
102 | | - @Test() |
103 | | - @Timeout(2000) |
104 | | - public void test_localServer_ServerLocalhost_ClientLocalhost_CheckInactive() |
105 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
106 | | - SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
107 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), ""); |
108 | | - assertTrue(client.onOpen); |
109 | | - assertFalse(client.onSSLError); |
110 | | - } |
111 | | - |
112 | | - @Test() |
113 | | - @Timeout(2000) |
114 | | - public void test_localServer_ServerLocalhost_ClientLocalhost_CheckDefault() |
115 | | - throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
116 | | - SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
117 | | - SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null); |
118 | | - assertTrue(client.onOpen); |
119 | | - assertFalse(client.onSSLError); |
120 | | - } |
121 | | - |
122 | | - |
123 | | - public SSLWebSocketClient testIssueWithLocalServer(String address, int port, |
124 | | - SSLContext serverContext, SSLContext clientContext, String endpointIdentificationAlgorithm) |
125 | | - throws IOException, URISyntaxException, InterruptedException { |
126 | | - CountDownLatch countServerDownLatch = new CountDownLatch(1); |
127 | | - SSLWebSocketClient client = new SSLWebSocketClient(address, port, |
128 | | - endpointIdentificationAlgorithm); |
129 | | - WebSocketServer server = new SSLWebSocketServer(port, countServerDownLatch); |
130 | | - |
131 | | - server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(serverContext)); |
132 | | - if (clientContext != null) { |
133 | | - client.setSocketFactory(clientContext.getSocketFactory()); |
| 61 | + @Test() |
| 62 | + @Timeout(2000) |
| 63 | + public void test_localServer_ServerLocalhost_Client127_CheckActive() |
| 64 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 65 | + SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
| 66 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), |
| 67 | + "HTTPS"); |
| 68 | + assertFalse(client.onOpen, "client is not open"); |
| 69 | + assertTrue(client.onSSLError, "client has caught a SSLHandshakeException"); |
134 | 70 | } |
135 | | - server.start(); |
136 | | - countServerDownLatch.await(); |
137 | | - client.connectBlocking(1, TimeUnit.SECONDS); |
138 | | - return client; |
139 | | - } |
140 | | - |
141 | | - |
142 | | - private static class SSLWebSocketClient extends WebSocketClient { |
143 | 71 |
|
144 | | - private final String endpointIdentificationAlgorithm; |
145 | | - public boolean onSSLError = false; |
146 | | - public boolean onOpen = false; |
147 | | - |
148 | | - public SSLWebSocketClient(String address, int port, String endpointIdentificationAlgorithm) |
149 | | - throws URISyntaxException { |
150 | | - super(new URI("wss://" + address + ':' + port)); |
151 | | - this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm; |
| 72 | + @Test() |
| 73 | + @Timeout(2000) |
| 74 | + public void test_localServer_ServerLocalhost_Client127_CheckInactive() |
| 75 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 76 | + SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
| 77 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), ""); |
| 78 | + assertTrue(client.onOpen, "client is open"); |
| 79 | + assertFalse(client.onSSLError, "client has not caught a SSLHandshakeException"); |
152 | 80 | } |
153 | 81 |
|
154 | | - @Override |
155 | | - public void onOpen(ServerHandshake handshakedata) { |
156 | | - this.onOpen = true; |
| 82 | + @Test() |
| 83 | + @Timeout(2000) |
| 84 | + public void test_localServer_ServerLocalhost_Client127_CheckDefault() |
| 85 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 86 | + SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), |
| 87 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null); |
| 88 | + assertFalse(client.onOpen, "client is not open"); |
| 89 | + assertTrue(client.onSSLError, "client has caught a SSLHandshakeException"); |
157 | 90 | } |
158 | 91 |
|
159 | | - @Override |
160 | | - public void onMessage(String message) { |
| 92 | + @Test() |
| 93 | + @Timeout(2000) |
| 94 | + public void test_localServer_ServerLocalhost_ClientLocalhost_CheckActive() |
| 95 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 96 | + SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
| 97 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), |
| 98 | + "HTTPS"); |
| 99 | + assertTrue(client.onOpen, "client is open"); |
| 100 | + assertFalse(client.onSSLError, "client has not caught a SSLHandshakeException"); |
161 | 101 | } |
162 | 102 |
|
163 | | - @Override |
164 | | - public void onClose(int code, String reason, boolean remote) { |
| 103 | + @Test() |
| 104 | + @Timeout(2000) |
| 105 | + public void test_localServer_ServerLocalhost_ClientLocalhost_CheckInactive() |
| 106 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 107 | + SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
| 108 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), ""); |
| 109 | + assertTrue(client.onOpen, "client is open"); |
| 110 | + assertFalse(client.onSSLError, "client has not caught a SSLHandshakeException"); |
165 | 111 | } |
166 | 112 |
|
167 | | - @Override |
168 | | - public void onError(Exception ex) { |
169 | | - if (ex instanceof SSLHandshakeException) { |
170 | | - this.onSSLError = true; |
171 | | - } |
| 113 | + @Test() |
| 114 | + @Timeout(2000) |
| 115 | + public void test_localServer_ServerLocalhost_ClientLocalhost_CheckDefault() |
| 116 | + throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException { |
| 117 | + SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), |
| 118 | + SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null); |
| 119 | + assertTrue(client.onOpen, "client is open"); |
| 120 | + assertFalse(client.onSSLError, "client has not caught a SSLHandshakeException"); |
172 | 121 | } |
173 | 122 |
|
174 | | - @Override |
175 | | - protected void onSetSSLParameters(SSLParameters sslParameters) { |
176 | | - // Always call super to ensure hostname validation is active by default |
177 | | - super.onSetSSLParameters(sslParameters); |
178 | | - if (endpointIdentificationAlgorithm != null) { |
179 | | - sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm); |
180 | | - } |
| 123 | + |
| 124 | + public SSLWebSocketClient testIssueWithLocalServer(String address, int port, |
| 125 | + SSLContext serverContext, SSLContext clientContext, String endpointIdentificationAlgorithm) |
| 126 | + throws IOException, URISyntaxException, InterruptedException { |
| 127 | + CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 128 | + SSLWebSocketClient client = new SSLWebSocketClient(address, port, |
| 129 | + endpointIdentificationAlgorithm); |
| 130 | + WebSocketServer server = new SSLWebSocketServer(port, countServerDownLatch); |
| 131 | + |
| 132 | + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(serverContext)); |
| 133 | + if (clientContext != null) { |
| 134 | + client.setSocketFactory(clientContext.getSocketFactory()); |
| 135 | + } |
| 136 | + server.start(); |
| 137 | + countServerDownLatch.await(); |
| 138 | + client.connectBlocking(1, TimeUnit.SECONDS); |
| 139 | + return client; |
181 | 140 | } |
182 | 141 |
|
183 | | - } |
184 | 142 |
|
| 143 | + private static class SSLWebSocketClient extends WebSocketClient { |
185 | 144 |
|
186 | | - private static class SSLWebSocketServer extends WebSocketServer { |
| 145 | + private final String endpointIdentificationAlgorithm; |
| 146 | + public boolean onSSLError = false; |
| 147 | + public boolean onOpen = false; |
187 | 148 |
|
188 | | - private final CountDownLatch countServerDownLatch; |
| 149 | + public SSLWebSocketClient(String address, int port, String endpointIdentificationAlgorithm) |
| 150 | + throws URISyntaxException { |
| 151 | + super(new URI("wss://" + address + ':' + port)); |
| 152 | + this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm; |
| 153 | + } |
189 | 154 |
|
| 155 | + @Override |
| 156 | + public void onOpen(ServerHandshake handshakedata) { |
| 157 | + this.onOpen = true; |
| 158 | + } |
190 | 159 |
|
191 | | - public SSLWebSocketServer(int port, CountDownLatch countServerDownLatch) { |
192 | | - super(new InetSocketAddress(port)); |
193 | | - this.countServerDownLatch = countServerDownLatch; |
194 | | - } |
| 160 | + @Override |
| 161 | + public void onMessage(String message) { |
| 162 | + } |
195 | 163 |
|
196 | | - @Override |
197 | | - public void onOpen(WebSocket conn, ClientHandshake handshake) { |
198 | | - } |
| 164 | + @Override |
| 165 | + public void onClose(int code, String reason, boolean remote) { |
| 166 | + } |
199 | 167 |
|
200 | | - @Override |
201 | | - public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
202 | | - } |
| 168 | + @Override |
| 169 | + public void onError(Exception ex) { |
| 170 | + if (ex instanceof SSLHandshakeException) { |
| 171 | + this.onSSLError = true; |
| 172 | + } |
| 173 | + } |
203 | 174 |
|
204 | | - @Override |
205 | | - public void onMessage(WebSocket conn, String message) { |
| 175 | + @Override |
| 176 | + protected void onSetSSLParameters(SSLParameters sslParameters) { |
| 177 | + // Always call super to ensure hostname validation is active by default |
| 178 | + super.onSetSSLParameters(sslParameters); |
| 179 | + if (endpointIdentificationAlgorithm != null) { |
| 180 | + sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm); |
| 181 | + } |
| 182 | + } |
206 | 183 |
|
207 | 184 | } |
208 | 185 |
|
209 | | - @Override |
210 | | - public void onError(WebSocket conn, Exception ex) { |
211 | | - ex.printStackTrace(); |
212 | | - } |
213 | 186 |
|
214 | | - @Override |
215 | | - public void onStart() { |
216 | | - countServerDownLatch.countDown(); |
| 187 | + private static class SSLWebSocketServer extends WebSocketServer { |
| 188 | + |
| 189 | + private final CountDownLatch countServerDownLatch; |
| 190 | + |
| 191 | + |
| 192 | + public SSLWebSocketServer(int port, CountDownLatch countServerDownLatch) { |
| 193 | + super(new InetSocketAddress(port)); |
| 194 | + this.countServerDownLatch = countServerDownLatch; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public void onMessage(WebSocket conn, String message) { |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public void onError(WebSocket conn, Exception ex) { |
| 212 | + ex.printStackTrace(); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void onStart() { |
| 217 | + countServerDownLatch.countDown(); |
| 218 | + } |
217 | 219 | } |
218 | | - } |
219 | 220 | } |
0 commit comments