use of javax.net.ssl.SSLSocket in project robovm by robovm.
the class HandshakeCompletedEventTest method test_getLocalPrincipal.
/**
* @throws IOException
* javax.net.ssl.HandshakeCompletedEvent#getLocalPrincipal()
*/
public final void test_getLocalPrincipal() throws Exception {
mySSLSession session = new mySSLSession("localhost", 1080, null);
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
assertNull(event.getLocalPrincipal());
}
use of javax.net.ssl.SSLSocket in project robovm by robovm.
the class HandshakeCompletedEventTest method test_getLocalCertificates.
/**
* @throws IOException
* javax.net.ssl.HandshakeCompletedEvent#getLocalCertificates()
*/
public final void test_getLocalCertificates() throws Exception {
mySSLSession session = new mySSLSession("localhost", 1080, null);
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
assertNull(event.getLocalCertificates());
}
use of javax.net.ssl.SSLSocket in project robovm by robovm.
the class HandshakeCompletedEventTest method test_getSocket.
/**
* @throws IOException
* javax.net.ssl.HandshakeCompletedEvent#getSocket()
*/
public final void test_getSocket() throws IOException {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, null);
SSLSocket ss = event.getSocket();
assertNotNull(ss);
assertEquals(socket, ss);
}
use of javax.net.ssl.SSLSocket in project robovm by robovm.
the class MockWebServer method serveConnection.
private void serveConnection(final Socket raw) {
String name = "MockWebServer-" + raw.getRemoteSocketAddress();
executor.execute(namedRunnable(name, new Runnable() {
int sequenceNumber = 0;
public void run() {
try {
processConnection();
} catch (Exception e) {
logger.log(Level.WARNING, "MockWebServer connection failed", e);
}
}
public void processConnection() throws Exception {
Socket socket;
if (sslSocketFactory != null) {
if (tunnelProxy) {
createTunnel();
}
socket = sslSocketFactory.createSocket(raw, raw.getInetAddress().getHostAddress(), raw.getPort(), true);
((SSLSocket) socket).setUseClientMode(false);
openClientSockets.add(socket);
openClientSockets.remove(raw);
} else {
socket = raw;
}
InputStream in = new BufferedInputStream(socket.getInputStream());
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
while (!responseQueue.isEmpty() && processOneRequest(in, out, socket)) {
}
if (sequenceNumber == 0) {
logger.warning("MockWebServer connection didn't make a request");
}
in.close();
out.close();
socket.close();
if (responseQueue.isEmpty()) {
shutdown();
}
openClientSockets.remove(socket);
}
/**
* Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response
* is dispatched.
*/
private void createTunnel() throws IOException, InterruptedException {
while (true) {
MockResponse connect = responseQueue.peek();
if (!processOneRequest(raw.getInputStream(), raw.getOutputStream(), raw)) {
throw new IllegalStateException("Tunnel without any CONNECT!");
}
if (connect.getSocketPolicy() == SocketPolicy.UPGRADE_TO_SSL_AT_END) {
return;
}
}
}
/**
* Reads a request and writes its response. Returns true if a request
* was processed.
*/
private boolean processOneRequest(InputStream in, OutputStream out, Socket socket) throws IOException, InterruptedException {
RecordedRequest request = readRequest(in, sequenceNumber);
if (request == null) {
return false;
}
MockResponse response = dispatch(request);
writeResponse(out, response);
if (response.getSocketPolicy() == SocketPolicy.DISCONNECT_AT_END) {
in.close();
out.close();
} else if (response.getSocketPolicy() == SocketPolicy.SHUTDOWN_INPUT_AT_END) {
socket.shutdownInput();
} else if (response.getSocketPolicy() == SocketPolicy.SHUTDOWN_OUTPUT_AT_END) {
socket.shutdownOutput();
}
sequenceNumber++;
return true;
}
}));
}
use of javax.net.ssl.SSLSocket in project robovm by robovm.
the class TestSSLSocketPair method connect.
/**
* Create a new connected server/client socket pair within a
* existing SSLContext. Optionally specify clientCipherSuites to
* allow forcing new SSLSession to test SSLSessionContext
* caching. Optionally specify serverCipherSuites for testing
* cipher suite negotiation.
*/
public static SSLSocket[] connect(final TestSSLContext context, final String[] clientCipherSuites, final String[] serverCipherSuites) {
try {
final SSLSocket client = (SSLSocket) context.clientContext.getSocketFactory().createSocket(context.host, context.port);
final SSLSocket server = (SSLSocket) context.serverSocket.accept();
ExecutorService executor = Executors.newFixedThreadPool(2);
Future s = executor.submit(new Callable<Void>() {
public Void call() throws Exception {
if (serverCipherSuites != null) {
server.setEnabledCipherSuites(serverCipherSuites);
}
server.startHandshake();
return null;
}
});
Future c = executor.submit(new Callable<Void>() {
public Void call() throws Exception {
if (clientCipherSuites != null) {
client.setEnabledCipherSuites(clientCipherSuites);
}
client.startHandshake();
return null;
}
});
executor.shutdown();
// catch client and server exceptions separately so we can
// potentially log both.
Exception serverException;
try {
s.get(30, TimeUnit.SECONDS);
serverException = null;
} catch (Exception e) {
serverException = e;
e.printStackTrace();
}
Exception clientException;
try {
c.get(30, TimeUnit.SECONDS);
clientException = null;
} catch (Exception e) {
clientException = e;
e.printStackTrace();
}
if (serverException != null) {
throw serverException;
}
if (clientException != null) {
throw clientException;
}
return new SSLSocket[] { server, client };
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations