|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2019 Nathan Rajlich |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +package org.java_websocket.issues; |
| 28 | + |
| 29 | +import org.java_websocket.WebSocket; |
| 30 | +import org.java_websocket.client.WebSocketClient; |
| 31 | +import org.java_websocket.handshake.ClientHandshake; |
| 32 | +import org.java_websocket.handshake.ServerHandshake; |
| 33 | +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; |
| 34 | +import org.java_websocket.server.WebSocketServer; |
| 35 | +import org.java_websocket.util.SSLContextUtil; |
| 36 | +import org.java_websocket.util.SocketUtil; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +import javax.net.ssl.KeyManagerFactory; |
| 40 | +import javax.net.ssl.SSLContext; |
| 41 | +import javax.net.ssl.SSLSession; |
| 42 | +import javax.net.ssl.TrustManagerFactory; |
| 43 | +import java.io.File; |
| 44 | +import java.io.FileInputStream; |
| 45 | +import java.io.IOException; |
| 46 | +import java.net.InetSocketAddress; |
| 47 | +import java.net.URI; |
| 48 | +import java.net.URISyntaxException; |
| 49 | +import java.security.*; |
| 50 | +import java.security.cert.CertificateException; |
| 51 | +import java.util.concurrent.CountDownLatch; |
| 52 | + |
| 53 | +import static org.junit.Assert.*; |
| 54 | + |
| 55 | +public class Issue890Test { |
| 56 | + |
| 57 | + |
| 58 | + @Test(timeout = 4000) |
| 59 | + public void testWithSSLSession() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException { |
| 60 | + int port = SocketUtil.getAvailablePort(); |
| 61 | + final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 62 | + final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) { |
| 63 | + @Override |
| 64 | + public void onOpen(ServerHandshake handshakedata) { |
| 65 | + countServerDownLatch.countDown(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onMessage(String message) { |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onClose(int code, String reason, boolean remote) { |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onError(Exception ex) { |
| 78 | + } |
| 79 | + }; |
| 80 | + TestResult testResult = new TestResult(); |
| 81 | + WebSocketServer server = new MyWebSocketServer(port, testResult, countServerDownLatch); |
| 82 | + SSLContext sslContext = SSLContextUtil.getContext(); |
| 83 | + |
| 84 | + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); |
| 85 | + webSocket.setSocketFactory(sslContext.getSocketFactory()); |
| 86 | + server.start(); |
| 87 | + countServerDownLatch.await(); |
| 88 | + webSocket.connectBlocking(); |
| 89 | + assertTrue(testResult.hasSSLSupport); |
| 90 | + assertNotNull(testResult.sslSession); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(timeout = 4000) |
| 94 | + public void testWithOutSSLSession() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException { |
| 95 | + int port = SocketUtil.getAvailablePort(); |
| 96 | + final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 97 | + final WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) { |
| 98 | + @Override |
| 99 | + public void onOpen(ServerHandshake handshakedata) { |
| 100 | + countServerDownLatch.countDown(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onMessage(String message) { |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onClose(int code, String reason, boolean remote) { |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onError(Exception ex) { |
| 113 | + } |
| 114 | + }; |
| 115 | + TestResult testResult = new TestResult(); |
| 116 | + WebSocketServer server = new MyWebSocketServer(port, testResult, countServerDownLatch); |
| 117 | + server.start(); |
| 118 | + countServerDownLatch.await(); |
| 119 | + webSocket.connectBlocking(); |
| 120 | + assertFalse(testResult.hasSSLSupport); |
| 121 | + assertNull(testResult.sslSession); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + private static class MyWebSocketServer extends WebSocketServer { |
| 126 | + |
| 127 | + private final TestResult testResult; |
| 128 | + private final CountDownLatch countServerDownLatch; |
| 129 | + |
| 130 | + public MyWebSocketServer(int port, TestResult testResult, CountDownLatch countServerDownLatch) { |
| 131 | + super(new InetSocketAddress(port)); |
| 132 | + this.testResult = testResult; |
| 133 | + this.countServerDownLatch = countServerDownLatch; |
| 134 | + } |
| 135 | + @Override |
| 136 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 137 | + testResult.hasSSLSupport = conn.hasSSLSupport(); |
| 138 | + try { |
| 139 | + testResult.sslSession = conn.getSSLSession(); |
| 140 | + } catch (IllegalArgumentException e){ |
| 141 | + // Ignore |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onMessage(WebSocket conn, String message) { |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onError(WebSocket conn, Exception ex) { |
| 156 | + ex.printStackTrace(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onStart() { |
| 161 | + countServerDownLatch.countDown(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private class TestResult { |
| 166 | + public SSLSession sslSession = null; |
| 167 | + |
| 168 | + public boolean hasSSLSupport = false; |
| 169 | + } |
| 170 | +} |
0 commit comments