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