diff options
Diffstat (limited to 'support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java')
-rw-r--r-- | support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java b/support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java index b6efdeb..709f568 100644 --- a/support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java +++ b/support/src/test/java/libcore/javax/net/ssl/TestSSLEnginePair.java @@ -47,10 +47,19 @@ public final class TestSSLEnginePair extends Assert { } public static TestSSLEnginePair create(TestSSLContext c, Hooks hooks) throws IOException { - SSLEngine[] engines = connect(c, hooks); + return create(c, hooks, null); + } + + public static TestSSLEnginePair create(TestSSLContext c, Hooks hooks, boolean[] finished) + throws IOException { + SSLEngine[] engines = connect(c, hooks, finished); return new TestSSLEnginePair(c, engines[0], engines[1]); } + public static SSLEngine[] connect(TestSSLContext c, Hooks hooks) throws IOException { + return connect(c, hooks, null); + } + /** * Create a new connected server/client engine pair within a * existing SSLContext. Optionally specify clientCipherSuites to @@ -59,11 +68,16 @@ public final class TestSSLEnginePair extends Assert { * cipher suite negotiation. */ public static SSLEngine[] connect(final TestSSLContext c, - Hooks hooks) throws IOException { + Hooks hooks, + boolean finished[]) throws IOException { if (hooks == null) { hooks = new Hooks(); } + // FINISHED state should be returned only once. + boolean[] clientFinished = new boolean[1]; + boolean[] serverFinished = new boolean[1]; + SSLSession session = c.clientContext.createSSLEngine().getSession(); int packetBufferSize = session.getPacketBufferSize(); @@ -73,7 +87,7 @@ public final class TestSSLEnginePair extends Assert { int applicationBufferSize = session.getApplicationBufferSize(); ByteBuffer scratch = ByteBuffer.allocate(applicationBufferSize); - SSLEngine client = c.clientContext.createSSLEngine(); + SSLEngine client = c.clientContext.createSSLEngine(c.host.getHostName(), c.port); SSLEngine server = c.serverContext.createSSLEngine(); client.setUseClientMode(true); server.setUseClientMode(false); @@ -93,20 +107,26 @@ public final class TestSSLEnginePair extends Assert { progress |= handshakeCompleted(client, clientToServer, serverToClient, - scratch); + scratch, + clientFinished); } if (!serverDone) { progress |= handshakeCompleted(server, serverToClient, clientToServer, - scratch); + scratch, + serverFinished); } if (!progress) { - // let caller detect the problem, but don't just hang here break; } } + if (finished != null) { + assertEquals(2, finished.length); + finished[0] = clientFinished[0]; + finished[1] = serverFinished[0]; + } return new SSLEngine[] { server, client }; } @@ -119,7 +139,8 @@ public final class TestSSLEnginePair extends Assert { private static boolean handshakeCompleted(SSLEngine engine, ByteBuffer output, ByteBuffer input, - ByteBuffer scratch) throws IOException { + ByteBuffer scratch, + boolean[] finished) throws IOException { try { // make the other side's output into our input input.flip(); @@ -127,7 +148,7 @@ public final class TestSSLEnginePair extends Assert { HandshakeStatus status = engine.getHandshakeStatus(); switch (status) { - case NEED_TASK: + case NEED_TASK: { boolean progress = false; while (true) { Runnable runnable = engine.getDelegatedTask(); @@ -137,8 +158,9 @@ public final class TestSSLEnginePair extends Assert { runnable.run(); progress = true; } + } - case NEED_UNWRAP: + case NEED_UNWRAP: { // avoid underflow if (input.remaining() == 0) { return false; @@ -146,16 +168,20 @@ public final class TestSSLEnginePair extends Assert { SSLEngineResult unwrapResult = engine.unwrap(input, scratch); assertEquals(SSLEngineResult.Status.OK, unwrapResult.getStatus()); assertEquals(0, scratch.position()); + assertFinishedOnce(finished, unwrapResult); return true; + } - case NEED_WRAP: + case NEED_WRAP: { // avoid possible overflow if (output.remaining() != output.capacity()) { return false; } SSLEngineResult wrapResult = engine.wrap(EMPTY_BYTE_BUFFER, output); assertEquals(SSLEngineResult.Status.OK, wrapResult.getStatus()); + assertFinishedOnce(finished, wrapResult); return true; + } case NOT_HANDSHAKING: // should have been checked by caller before calling @@ -170,4 +196,11 @@ public final class TestSSLEnginePair extends Assert { input.compact(); } } + + private static void assertFinishedOnce(boolean[] finishedOut, SSLEngineResult result) { + if (result.getHandshakeStatus() == HandshakeStatus.FINISHED) { + assertFalse("should only return FINISHED once", finishedOut[0]); + finishedOut[0] = true; + } + } } |