forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSLConnectionTest.java
More file actions
118 lines (103 loc) · 3.68 KB
/
Copy pathSSLConnectionTest.java
File metadata and controls
118 lines (103 loc) · 3.68 KB
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
package io.socket.client;
import io.socket.emitter.Emitter;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@RunWith(JUnit4.class)
public class SSLConnectionTest extends Connection {
// for test on localhost
static HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return hostname.equals("localhost");
}
};
private Socket socket;
@Override
String uri() {
return "https://localhost:" + PORT;
}
@Override
IO.Options createOptions() {
IO.Options opts = super.createOptions();
opts.secure = true;
return opts;
}
@Override
String[] createEnv() {
return new String[] {"DEBUG=socket.io:*", "PORT=" + PORT, "SSL=1"};
}
SSLContext createSSLContext() throws GeneralSecurityException, IOException {
KeyStore ks = KeyStore.getInstance("JKS");
File file = new File("src/test/resources/keystore.jks");
ks.load(new FileInputStream(file), "password".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
return sslContext;
}
@After
public void tearDown() {
IO.setDefaultSSLContext(null);
IO.setDefaultHostnameVerifier(null);
}
@Test(timeout = TIMEOUT)
public void connect() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
IO.Options opts = createOptions();
opts.sslContext = createSSLContext();
opts.hostnameVerifier = hostnameVerifier;
socket = client(opts);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.emit("echo");
socket.on("echoBack", new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
}
});
}
});
socket.connect();
values.take();
socket.close();
}
@Test(timeout = TIMEOUT)
public void defaultSSLContext() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
IO.setDefaultSSLContext(createSSLContext());
IO.setDefaultHostnameVerifier(hostnameVerifier);
socket = client();
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.emit("echo");
socket.on("echoBack", new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
}
});
}
});
socket.connect();
values.take();
socket.close();
}
}