use of javax.net.ssl.HandshakeCompletedEvent 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.HandshakeCompletedEvent in project robovm by robovm.
the class HandshakeCompletedEventTest method test_Constructor.
/**
* @throws IOException
* javax.net.ssl.HandshakeCompletedEvent#HandshakeCompletedEvent(SSLSocket sock, SSLSession s)
*/
public final void test_Constructor() throws Exception {
mySSLSession session = new mySSLSession();
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
try {
new HandshakeCompletedEvent(null, null);
fail("Any exception wasn't thrown for null parameters");
} catch (Exception expected) {
}
}
use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.
the class HandshakeCompletedEventTest method test_getPeerPrincipal.
/**
* @throws IOException
* javax.net.ssl.HandshakeCompletedEvent#getPeerPrincipal()
*/
public final void test_getPeerPrincipal() throws IOException {
mySSLSession session = new mySSLSession("localhost", 1080, null);
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
assertNull(event.getPeerPrincipal());
}
use of javax.net.ssl.HandshakeCompletedEvent in project XobotOS by xamarin.
the class SSLSocketImpl method doHandshake.
/*
* Performs handshake process over this connection. The handshake
* process is directed by the handshake status code provided by
* handshake protocol. If this status is NEED_WRAP, method retrieves
* handshake message from handshake protocol and sends it to another peer.
* If this status is NEED_UNWRAP, method receives and processes handshake
* message from another peer. Each of this stages (wrap/unwrap) change
* the state of handshake protocol and this process is performed
* until handshake status is FINISHED. After handshake process is finished
* handshake completed event are sent to the registered listeners.
* For more information about the handshake process see
* TLS v1 specification (http://www.ietf.org/rfc/rfc2246.txt) p 7.3.
*/
private void doHandshake() throws IOException {
SSLEngineResult.HandshakeStatus status;
int type;
try {
while (!(status = handshakeProtocol.getStatus()).equals(SSLEngineResult.HandshakeStatus.FINISHED)) {
if (logger != null) {
String s = (status.equals(SSLEngineResult.HandshakeStatus.NEED_WRAP)) ? "NEED_WRAP" : (status.equals(SSLEngineResult.HandshakeStatus.NEED_UNWRAP)) ? "NEED_UNWRAP" : "STATUS: OTHER!";
logger.println("SSLSocketImpl: HS status: " + s + " " + status);
}
if (status.equals(SSLEngineResult.HandshakeStatus.NEED_WRAP)) {
output.write(handshakeProtocol.wrap());
} else if (status.equals(SSLEngineResult.HandshakeStatus.NEED_UNWRAP)) {
// and retrieve the type of unwrapped data
switch(type = recordProtocol.unwrap()) {
case ContentType.HANDSHAKE:
case ContentType.CHANGE_CIPHER_SPEC:
break;
case ContentType.APPLICATION_DATA:
// constraints (do not expect buffer overflow).
break;
case ContentType.ALERT:
processAlert();
if (socket_was_closed) {
return;
}
break;
default:
// will throw exception
reportFatalAlert(AlertProtocol.UNEXPECTED_MESSAGE, new SSLException("Unexpected message of type " + type + " has been got"));
}
} else {
// will throw exception
reportFatalAlert(AlertProtocol.INTERNAL_ERROR, new SSLException("Handshake passed unexpected status: " + status));
}
if (alertProtocol.hasAlert()) {
// warning alert occurred during wrap or unwrap
// (note: fatal alert causes AlertException
// to be thrown)
output.write(alertProtocol.wrap());
alertProtocol.setProcessed();
}
}
} catch (EndOfSourceException e) {
appDataIS.setEnd();
throw new IOException("Connection was closed");
} catch (AlertException e) {
// will throw exception
reportFatalAlert(e.getDescriptionCode(), e.getReason());
}
session = recordProtocol.getSession();
if (listeners != null) {
// notify the listeners
HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, session);
int size = listeners.size();
for (int i = 0; i < size; i++) {
listeners.get(i).handshakeCompleted(event);
}
}
}
use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.
the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener_RuntimeException.
public void test_SSLSocket_HandshakeCompletedListener_RuntimeException() throws Exception {
final Thread self = Thread.currentThread();
final UncaughtExceptionHandler original = self.getUncaughtExceptionHandler();
final RuntimeException expectedException = new RuntimeException("expected");
final TestUncaughtExceptionHandler test = new TestUncaughtExceptionHandler();
self.setUncaughtExceptionHandler(test);
final TestSSLContext c = TestSSLContext.create();
final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
final SSLSocket server = (SSLSocket) c.serverSocket.accept();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Void> future = executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
server.startHandshake();
return null;
}
});
executor.shutdown();
client.addHandshakeCompletedListener(new HandshakeCompletedListener() {
public void handshakeCompleted(HandshakeCompletedEvent event) {
throw expectedException;
}
});
client.startHandshake();
future.get();
client.close();
server.close();
c.close();
assertSame(expectedException, test.actualException);
self.setUncaughtExceptionHandler(original);
}
Aggregations