Skip to content

Commit e351a29

Browse files
committed
Test development
1 parent 47ff6e7 commit e351a29

File tree

4 files changed

+236
-35
lines changed

4 files changed

+236
-35
lines changed

src/test/java/org/java_websocket/issues/Issue764Test.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.java_websocket.handshake.ServerHandshake;
3333
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
3434
import org.java_websocket.server.WebSocketServer;
35+
import org.java_websocket.util.SSLContextUtil;
3536
import org.java_websocket.util.SocketUtil;
3637
import org.junit.Test;
3738

@@ -76,24 +77,7 @@ public void onError(Exception ex) {
7677
};
7778
WebSocketServer server = new MyWebSocketServer(port, webSocket, countServerDownLatch);
7879

79-
// load up the key store
80-
String STORETYPE = "JKS";
81-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
82-
String STOREPASSWORD = "storepassword";
83-
String KEYPASSWORD = "keypassword";
84-
85-
KeyStore ks = KeyStore.getInstance(STORETYPE);
86-
File kf = new File(KEYSTORE);
87-
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
88-
89-
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90-
kmf.init(ks, KEYPASSWORD.toCharArray());
91-
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
92-
tmf.init(ks);
93-
94-
SSLContext sslContext = null;
95-
sslContext = SSLContext.getInstance("TLS");
96-
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
80+
SSLContext sslContext = SSLContextUtil.getContext();
9781

9882
server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
9983
webSocket.setSocketFactory(sslContext.getSocketFactory());

src/test/java/org/java_websocket/issues/Issue825Test.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.java_websocket.handshake.ServerHandshake;
3333
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
3434
import org.java_websocket.server.WebSocketServer;
35+
import org.java_websocket.util.SSLContextUtil;
3536
import org.java_websocket.util.SocketUtil;
3637
import org.junit.Test;
3738

@@ -75,23 +76,7 @@ public void onError(Exception ex) {
7576
WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch, countClientDownLatch);
7677

7778
// 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);
79+
SSLContext sslContext = SSLContextUtil.getContext();
9580

9681
server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
9782
webSocket.setSocketFactory(sslContext.getSocketFactory());
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.util;
28+
29+
import javax.net.ssl.KeyManagerFactory;
30+
import javax.net.ssl.SSLContext;
31+
import javax.net.ssl.TrustManagerFactory;
32+
import java.io.File;
33+
import java.io.FileInputStream;
34+
import java.io.FileNotFoundException;
35+
import java.io.IOException;
36+
import java.security.*;
37+
import java.security.cert.CertificateException;
38+
39+
public class SSLContextUtil {
40+
41+
public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
42+
// load up the key store
43+
String STORETYPE = "JKS";
44+
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
45+
String STOREPASSWORD = "storepassword";
46+
String KEYPASSWORD = "keypassword";
47+
48+
KeyStore ks = KeyStore.getInstance(STORETYPE);
49+
File kf = new File(KEYSTORE);
50+
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
51+
52+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
53+
kmf.init(ks, KEYPASSWORD.toCharArray());
54+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
55+
tmf.init(ks);
56+
57+
SSLContext sslContext = null;
58+
sslContext = SSLContext.getInstance("TLS");
59+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
60+
return sslContext;
61+
}
62+
}

0 commit comments

Comments
 (0)