|
| 1 | +package org.java_websocket.issues; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.URISyntaxException; |
| 11 | +import java.security.KeyManagementException; |
| 12 | +import java.security.KeyStoreException; |
| 13 | +import java.security.NoSuchAlgorithmException; |
| 14 | +import java.security.UnrecoverableKeyException; |
| 15 | +import java.security.cert.CertificateException; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import javax.net.ssl.SSLContext; |
| 18 | +import org.java_websocket.WebSocket; |
| 19 | +import org.java_websocket.client.WebSocketClient; |
| 20 | +import org.java_websocket.handshake.ClientHandshake; |
| 21 | +import org.java_websocket.handshake.ServerHandshake; |
| 22 | +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; |
| 23 | +import org.java_websocket.server.WebSocketServer; |
| 24 | +import org.java_websocket.util.SSLContextUtil; |
| 25 | +import org.java_websocket.util.SocketUtil; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class Issue1142Test { |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + @Test(timeout = 4000) |
| 33 | + public void testWithoutSSLSession() |
| 34 | + throws IOException, URISyntaxException, InterruptedException { |
| 35 | + int port = SocketUtil.getAvailablePort(); |
| 36 | + final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 37 | + final WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) { |
| 38 | + @Override |
| 39 | + public void onOpen(ServerHandshake handshakedata) { |
| 40 | + countServerDownLatch.countDown(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onMessage(String message) { |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onClose(int code, String reason, boolean remote) { |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onError(Exception ex) { |
| 53 | + } |
| 54 | + }; |
| 55 | + WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch); |
| 56 | + server.start(); |
| 57 | + countServerDownLatch.await(); |
| 58 | + webSocket.connectBlocking(); |
| 59 | + assertFalse(webSocket.hasSSLSupport()); |
| 60 | + try { |
| 61 | + webSocket.getSSLSession(); |
| 62 | + assertFalse(false); |
| 63 | + } catch (IllegalArgumentException e) { |
| 64 | + // Fine |
| 65 | + } |
| 66 | + server.stop(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test(timeout = 4000) |
| 70 | + public void testWithSSLSession() |
| 71 | + throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException { |
| 72 | + int port = SocketUtil.getAvailablePort(); |
| 73 | + final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 74 | + final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) { |
| 75 | + @Override |
| 76 | + public void onOpen(ServerHandshake handshakedata) { |
| 77 | + countServerDownLatch.countDown(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onMessage(String message) { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onClose(int code, String reason, boolean remote) { |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onError(Exception ex) { |
| 90 | + } |
| 91 | + }; |
| 92 | + WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch); |
| 93 | + SSLContext sslContext = SSLContextUtil.getContext(); |
| 94 | + |
| 95 | + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); |
| 96 | + webSocket.setSocketFactory(sslContext.getSocketFactory()); |
| 97 | + server.start(); |
| 98 | + countServerDownLatch.await(); |
| 99 | + webSocket.connectBlocking(); |
| 100 | + assertTrue(webSocket.hasSSLSupport()); |
| 101 | + assertNotNull(webSocket.getSSLSession()); |
| 102 | + server.stop(); |
| 103 | + } |
| 104 | + |
| 105 | + private static class MyWebSocketServer extends WebSocketServer { |
| 106 | + |
| 107 | + private final CountDownLatch countServerLatch; |
| 108 | + |
| 109 | + |
| 110 | + public MyWebSocketServer(int port, CountDownLatch serverDownLatch) { |
| 111 | + super(new InetSocketAddress(port)); |
| 112 | + this.countServerLatch = serverDownLatch; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onMessage(WebSocket conn, String message) { |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onError(WebSocket conn, Exception ex) { |
| 130 | + ex.printStackTrace(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onStart() { |
| 135 | + countServerLatch.countDown(); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments