fix #3 enable to set SSLContext as an option

This commit is contained in:
Naoyuki Kanezawa
2014-07-07 22:41:59 +09:00
parent b1e43ba1b2
commit fe2fd4413a
13 changed files with 208 additions and 36 deletions

View File

@@ -54,7 +54,7 @@ public class BinaryWSTest extends Connection {
}
@Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultiplebyteUTF8String() throws InterruptedException {
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {

View File

@@ -24,7 +24,7 @@ public abstract class Connection {
final CountDownLatch latch = new CountDownLatch(1);
serverProcess = Runtime.getRuntime().exec(
"node src/test/resources/index.js " + PORT, new String[] {"DEBUG=engine*"});
"node src/test/resources/server.js", createEnv());
serverService = Executors.newCachedThreadPool();
serverOutout = serverService.submit(new Runnable() {
@Override
@@ -70,4 +70,14 @@ public abstract class Connection {
serverService.shutdown();
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
}
Socket.Options createOptions() {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
return opts;
}
String[] createEnv() {
return new String[] {"DEBUG=engine*", "PORT=" + PORT};
}
}

View File

@@ -21,9 +21,7 @@ public class ConnectionTest extends Connection {
public void connectToLocalhost() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -45,9 +43,7 @@ public class ConnectionTest extends Connection {
public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -71,9 +67,7 @@ public class ConnectionTest extends Connection {
public void receiveEmoji() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -97,9 +91,7 @@ public class ConnectionTest extends Connection {
public void notSendPacketsIfSocketCloses() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0);
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {

View File

@@ -0,0 +1,122 @@
package com.github.nkzawa.engineio.client;
import com.github.nkzawa.emitter.Emitter;
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;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@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
Socket.Options createOptions() {
Socket.Options opts = super.createOptions();
opts.secure = true;
return opts;
}
@Override
String[] createEnv() {
return new String[] {"DEBUG=engine*", "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;
}
@Test(timeout = TIMEOUT)
public void connect() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Socket.Options opts = createOptions();
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat((String)args[0], is("hi"));
socket.close();
latch.countDown();
}
});
}
}).on("error", new Emitter.Listener() {
@Override
public void call(Object... args) {
((Exception)args[0]).printStackTrace();
}
});
socket.open();
latch.await();
}
@Test(timeout = TIMEOUT)
public void upgrade() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Socket.Options opts = createOptions();
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("hi");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat((String) args[0], is("hi"));
socket.close();
latch.countDown();
}
});
}
});
}
});
socket.open();
latch.await();
}
}

View File

@@ -30,7 +30,7 @@ public class ServerConnectionTest extends Connection {
public void openAndClose() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket("ws://localhost:" + PORT);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -53,7 +53,7 @@ public class ServerConnectionTest extends Connection {
public void messages() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket("ws://localhost:" + PORT);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -76,7 +76,7 @@ public class ServerConnectionTest extends Connection {
public void handshake() throws URISyntaxException, InterruptedException {
final Semaphore semaphore = new Semaphore(0);
socket = new Socket("ws://localhost:" + PORT);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -102,7 +102,7 @@ public class ServerConnectionTest extends Connection {
public void upgrade() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object[]> events = new LinkedBlockingQueue<Object[]>();
socket = new Socket("ws://localhost:" + PORT);
socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -136,10 +136,10 @@ public class ServerConnectionTest extends Connection {
public void pollingHeaders() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = new Socket.Options();
Socket.Options opts = createOptions();
opts.transports = new String[] {Polling.NAME};
socket = new Socket("ws://localhost:" + PORT, opts);
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -172,10 +172,10 @@ public class ServerConnectionTest extends Connection {
public void websocketHandshakeHeaders() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = new Socket.Options();
Socket.Options opts = createOptions();
opts.transports = new String[] {WebSocket.NAME};
socket = new Socket("ws://localhost:" + PORT, opts);
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
@@ -210,10 +210,7 @@ public class ServerConnectionTest extends Connection {
EventThread.exec(new Runnable() {
@Override
public void run() {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
final Socket socket = new Socket(opts);
final Socket socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
@@ -246,10 +243,7 @@ public class ServerConnectionTest extends Connection {
EventThread.exec(new Runnable() {
@Override
public void run() {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
final Socket socket = new Socket(opts);
final Socket socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override