|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2018 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.SocketUtil; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import javax.net.ssl.KeyManagerFactory; |
| 39 | +import javax.net.ssl.SSLContext; |
| 40 | +import javax.net.ssl.TrustManagerFactory; |
| 41 | +import java.io.File; |
| 42 | +import java.io.FileInputStream; |
| 43 | +import java.io.IOException; |
| 44 | +import java.net.InetSocketAddress; |
| 45 | +import java.net.URI; |
| 46 | +import java.net.URISyntaxException; |
| 47 | +import java.security.*; |
| 48 | +import java.security.cert.CertificateException; |
| 49 | +import java.util.concurrent.CountDownLatch; |
| 50 | + |
| 51 | +public class Issue825Test { |
| 52 | + private CountDownLatch countClientDownLatch = new CountDownLatch(3); |
| 53 | + private CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 54 | + |
| 55 | + @Test(timeout = 15000) |
| 56 | + public void testIssue() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException { |
| 57 | + int port = SocketUtil.getAvailablePort(); |
| 58 | + final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) { |
| 59 | + @Override |
| 60 | + public void onOpen(ServerHandshake handshakedata) { |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onMessage(String message) { |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onClose(int code, String reason, boolean remote) { |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onError(Exception ex) { |
| 73 | + } |
| 74 | + }; |
| 75 | + WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch, countClientDownLatch); |
| 76 | + |
| 77 | + // load up the key store |
| 78 | + String STORETYPE = "JKS"; |
| 79 | + String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator); |
| 80 | + String STOREPASSWORD = "storepassword"; |
| 81 | + String KEYPASSWORD = "keypassword"; |
| 82 | + |
| 83 | + KeyStore ks = KeyStore.getInstance(STORETYPE); |
| 84 | + File kf = new File(KEYSTORE); |
| 85 | + ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); |
| 86 | + |
| 87 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 88 | + kmf.init(ks, KEYPASSWORD.toCharArray()); |
| 89 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 90 | + tmf.init(ks); |
| 91 | + |
| 92 | + SSLContext sslContext = null; |
| 93 | + sslContext = SSLContext.getInstance("TLS"); |
| 94 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 95 | + |
| 96 | + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); |
| 97 | + webSocket.setSocketFactory(sslContext.getSocketFactory()); |
| 98 | + server.start(); |
| 99 | + countServerDownLatch.await(); |
| 100 | + webSocket.connectBlocking(); |
| 101 | + webSocket.send( "hi" ); |
| 102 | + Thread.sleep( 10 ); |
| 103 | + webSocket.closeBlocking(); |
| 104 | + |
| 105 | + //Disconnect manually and reconnect blocking |
| 106 | + webSocket.reconnectBlocking(); |
| 107 | + webSocket.send( "it's" ); |
| 108 | + Thread.sleep( 10000 ); |
| 109 | + webSocket.closeBlocking(); |
| 110 | + //Disconnect manually and reconnect |
| 111 | + webSocket.reconnect(); |
| 112 | + Thread.sleep( 100 ); |
| 113 | + webSocket.send( "me" ); |
| 114 | + Thread.sleep( 100 ); |
| 115 | + webSocket.closeBlocking(); |
| 116 | + countClientDownLatch.await(); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private static class MyWebSocketServer extends WebSocketServer { |
| 121 | + private final CountDownLatch countServerDownLatch; |
| 122 | + private final CountDownLatch countClientDownLatch; |
| 123 | + |
| 124 | + |
| 125 | + public MyWebSocketServer(int port, CountDownLatch serverDownLatch, CountDownLatch countClientDownLatch) { |
| 126 | + super(new InetSocketAddress(port)); |
| 127 | + this.countServerDownLatch = serverDownLatch; |
| 128 | + this.countClientDownLatch = countClientDownLatch; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onMessage(WebSocket conn, String message) { |
| 141 | + countClientDownLatch.countDown(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void onError(WebSocket conn, Exception ex) { |
| 146 | + ex.printStackTrace(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onStart() { |
| 151 | + countServerDownLatch.countDown(); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments