fix #6 enable to set SSLContext

This commit is contained in:
Naoyuki Kanezawa
2014-07-10 02:43:35 +09:00
parent 02bda39187
commit b7f08059d7
8 changed files with 182 additions and 15 deletions

View File

@@ -16,7 +16,7 @@ public abstract class Connection {
private Process serverProcess;
private ExecutorService serverService;
private Future serverOutout;
private Future serverOutput;
private Future serverError;
@Before
@@ -25,10 +25,9 @@ public abstract class Connection {
final CountDownLatch latch = new CountDownLatch(1);
serverProcess = Runtime.getRuntime().exec(
String.format("node src/test/resources/index.js %s %s", PORT, nsp()),
new String[] {"DEBUG=socket.io:*"});
String.format("node src/test/resources/server.js %s", nsp()), createEnv());
serverService = Executors.newCachedThreadPool();
serverOutout = serverService.submit(new Runnable() {
serverOutput = serverService.submit(new Runnable() {
@Override
public void run() {
BufferedReader reader = new BufferedReader(
@@ -67,24 +66,36 @@ public abstract class Connection {
public void stopServer() throws InterruptedException {
System.out.println("Stopping server ...");
serverProcess.destroy();
serverOutout.cancel(false);
serverOutput.cancel(false);
serverError.cancel(false);
serverService.shutdown();
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
}
protected Socket client() throws URISyntaxException {
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
Socket client() throws URISyntaxException {
return client(createOptions());
}
Socket client(IO.Options opts) throws URISyntaxException {
return IO.socket(uri() + nsp(), opts);
}
protected String uri() {
String uri() {
return "http://localhost:" + PORT;
}
protected String nsp() {
String nsp() {
return "/";
}
IO.Options createOptions() {
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
return opts;
}
String[] createEnv() {
return new String[] {"DEBUG=socket.io:*", "PORT=" + PORT};
}
}

View File

@@ -0,0 +1,116 @@
package com.github.nkzawa.socketio.client;
import com.github.nkzawa.emitter.Emitter;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
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.CountDownLatch;
@RunWith(JUnit4.class)
public class SSLConnectionTest extends Connection {
static {
// for test on localhost
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
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);
}
@Test(timeout = TIMEOUT)
public void connect() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
IO.Options opts = createOptions();
opts.sslContext = createSSLContext();
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) {
socket.close();
latch.countDown();
}
});
}
});
socket.connect();
latch.await();
}
@Test(timeout = TIMEOUT)
public void defaultSSLContext() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
IO.setDefaultSSLContext(createSSLContext());
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) {
socket.close();
latch.countDown();
}
});
}
});
socket.connect();
latch.await();
}
}