Skip to content

Commit 0670985

Browse files
committed
Rework after review
1 parent 2dbe2d3 commit 0670985

File tree

8 files changed

+32
-13
lines changed

8 files changed

+32
-13
lines changed

src/main/example/SSLClientExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.FileInputStream;
2929
import java.io.InputStreamReader;
3030
import java.net.URI;
31+
import java.nio.file.Paths;
3132
import java.security.KeyStore;
3233

3334
import javax.net.ssl.KeyManagerFactory;
@@ -83,7 +84,7 @@ public static void main( String[] args ) throws Exception {
8384

8485
// load up the key store
8586
String STORETYPE = "JKS";
86-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
87+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
8788
String STOREPASSWORD = "storepassword";
8889
String KEYPASSWORD = "keypassword";
8990

src/main/example/SSLServerCustomWebsocketFactoryExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.net.ssl.TrustManagerFactory;
3333
import java.io.File;
3434
import java.io.FileInputStream;
35+
import java.nio.file.Paths;
3536
import java.security.KeyStore;
3637
import java.util.ArrayList;
3738
import java.util.Arrays;
@@ -52,7 +53,7 @@ public static void main(String[] args) throws Exception {
5253

5354
// load up the key store
5455
String STORETYPE = "JKS";
55-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
56+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5657
String STOREPASSWORD = "storepassword";
5758
String KEYPASSWORD = "keypassword";
5859

src/main/example/SSLServerExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.File;
2929
import java.io.FileInputStream;
30+
import java.nio.file.Paths;
3031
import java.security.KeyStore;
3132

3233
import javax.net.ssl.KeyManagerFactory;
@@ -48,7 +49,7 @@ public static void main( String[] args ) throws Exception {
4849

4950
// load up the key store
5051
String STORETYPE = "JKS";
51-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
52+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5253
String STOREPASSWORD = "storepassword";
5354
String KEYPASSWORD = "keypassword";
5455

src/main/example/TwoWaySSLServerExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.net.ssl.TrustManagerFactory;
3434
import java.io.File;
3535
import java.io.FileInputStream;
36+
import java.nio.file.Paths;
3637
import java.security.KeyStore;
3738

3839
/**
@@ -51,7 +52,7 @@ public static void main( String[] args ) throws Exception {
5152

5253
// load up the key store
5354
String STORETYPE = "JKS";
54-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
55+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5556
String STOREPASSWORD = "storepassword";
5657
String KEYPASSWORD = "keypassword";
5758

src/main/java/org/java_websocket/client/WebSocketClient.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ public void run() {
472472
if (socket instanceof SSLSocket) {
473473
SSLSocket sslSocket = (SSLSocket)socket;
474474
SSLParameters sslParameters = sslSocket.getSSLParameters();
475+
// Make sure we perform hostname validation
476+
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
475477
onSetSSLParameters(sslParameters);
476478
sslSocket.setSSLParameters(sslParameters);
477479
}
@@ -517,12 +519,11 @@ public void run() {
517519
}
518520

519521
/**
520-
* Apply specific
522+
* Apply specific SSLParameters
523+
*
521524
* @param sslParameters the SSLParameters which will be used for the SSLSocket
522525
*/
523526
protected void onSetSSLParameters(SSLParameters sslParameters) {
524-
// Make sure we perform hostname validation
525-
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
526527
}
527528

528529
/**

src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.net.InetSocketAddress;
4444
import java.net.UnknownHostException;
4545
import java.nio.ByteBuffer;
46+
import java.nio.file.Paths;
4647
import java.security.KeyStore;
4748
import java.security.spec.ECField;
4849
import java.util.Collections;
@@ -102,7 +103,7 @@ public static void main( String[] args ) throws UnknownHostException {
102103
try {
103104
// load up the key store
104105
String STORETYPE = "JKS";
105-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
106+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
106107
String STOREPASSWORD = "storepassword";
107108
String KEYPASSWORD = "keypassword";
108109

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public void test_localServer_ServerLocalhost_Client127_CheckInactive() throws Ce
6767
assertFalse(client.onSSLError);
6868
}
6969

70+
@Test(timeout=2000)
71+
public void test_localServer_ServerLocalhost_Client127_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
72+
SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
73+
assertFalse(client.onOpen);
74+
assertTrue(client.onSSLError);
75+
}
76+
7077
@Test(timeout=2000)
7178
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckActive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
7279
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "HTTPS");
@@ -80,6 +87,13 @@ public void test_localServer_ServerLocalhost_ClientLocalhost_CheckInactive() thr
8087
assertFalse(client.onSSLError);
8188
}
8289

90+
@Test(timeout=2000)
91+
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
92+
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
93+
assertTrue(client.onOpen);
94+
assertFalse(client.onSSLError);
95+
}
96+
8397

8498
public SSLWebSocketClient testIssueWithLocalServer(String address, int port, SSLContext serverContext, SSLContext clientContext, String endpointIdentificationAlgorithm) throws IOException, URISyntaxException, InterruptedException {
8599
CountDownLatch countServerDownLatch = new CountDownLatch(1);
@@ -129,9 +143,7 @@ public void onError(Exception ex) {
129143

130144
@Override
131145
protected void onSetSSLParameters(SSLParameters sslParameters) {
132-
if (endpointIdentificationAlgorithm == null) {
133-
super.onSetSSLParameters(sslParameters);
134-
} else {
146+
if (endpointIdentificationAlgorithm != null) {
135147
sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
136148
}
137149
}

src/test/java/org/java_websocket/util/SSLContextUtil.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.File;
3333
import java.io.FileInputStream;
3434
import java.io.IOException;
35+
import java.nio.file.Paths;
3536
import java.security.*;
3637
import java.security.cert.CertificateException;
3738

@@ -40,7 +41,7 @@ public class SSLContextUtil {
4041
public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
4142
// load up the key store
4243
String STORETYPE = "JKS";
43-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
44+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
4445
String STOREPASSWORD = "storepassword";
4546
String KEYPASSWORD = "keypassword";
4647

@@ -62,7 +63,7 @@ public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManage
6263
public static SSLContext getLocalhostOnlyContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
6364
// load up the key store
6465
String STORETYPE = "JKS";
65-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore_localhost_only.jks", File.separator);
66+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore_localhost_only.jks").toString();
6667
String STOREPASSWORD = "storepassword";
6768
String KEYPASSWORD = "keypassword";
6869

0 commit comments

Comments
 (0)