Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix *SslEngineTest to not throw ClassCastException and pass in all cases #9588

Merged
merged 1 commit into from Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions handler/src/test/java/io/netty/handler/ssl/OpenSslEngineTest.java
Expand Up @@ -36,6 +36,7 @@
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import java.nio.ByteBuffer;
Expand All @@ -61,6 +62,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -1131,6 +1133,19 @@ public boolean permits(
}
}

private static void runTasksIfNeeded(SSLEngine engine) {
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
for (;;) {
Runnable task = engine.getDelegatedTask();
if (task == null) {
assertNotEquals(HandshakeStatus.NEED_TASK, engine.getHandshakeStatus());
break;
}
task.run();
}
}
}

@Test
public void testExtractMasterkeyWorksCorrectly() throws Exception {
SelfSignedCertificate cert = new SelfSignedCertificate();
Expand Down Expand Up @@ -1199,17 +1214,23 @@ public void testExtractMasterkeyWorksCorrectly() throws Exception {
cTOs.flip();
sTOc.flip();

runTasksIfNeeded(clientEngine);
runTasksIfNeeded(serverEngine);

clientEngine.unwrap(sTOc, clientIn);
serverEngine.unwrap(cTOs, serverIn);

runTasksIfNeeded(clientEngine);
runTasksIfNeeded(serverEngine);

// check when the application data has fully been consumed and sent
// for both the client and server
if ((clientOut.limit() == serverIn.position()) &&
(serverOut.limit() == clientIn.position())) {
byte[] serverRandom = SSL.getServerRandom(((OpenSslEngine) serverEngine).sslPointer());
byte[] clientRandom = SSL.getClientRandom(((OpenSslEngine) clientEngine).sslPointer());
byte[] serverMasterKey = SSL.getMasterKey(((OpenSslEngine) serverEngine).sslPointer());
byte[] clientMasterKey = SSL.getMasterKey(((OpenSslEngine) clientEngine).sslPointer());
byte[] serverRandom = SSL.getServerRandom(unwrapEngine(serverEngine).sslPointer());
byte[] clientRandom = SSL.getClientRandom(unwrapEngine(clientEngine).sslPointer());
byte[] serverMasterKey = SSL.getMasterKey(unwrapEngine(serverEngine).sslPointer());
byte[] clientMasterKey = SSL.getMasterKey(unwrapEngine(clientEngine).sslPointer());

asserted = true;
assertArrayEquals(serverMasterKey, clientMasterKey);
Expand Down Expand Up @@ -1318,7 +1339,9 @@ ReferenceCountedOpenSslEngine unwrapEngine(SSLEngine engine) {

@Override
protected SslContext wrapContext(SslContext context) {
((OpenSslContext) context).setUseTasks(useTasks);
if (context instanceof OpenSslContext) {
((OpenSslContext) context).setUseTasks(useTasks);
}
return context;
}
}
Expand Up @@ -72,7 +72,9 @@ public void testNotLeakOnException() throws Exception {

@Override
protected SslContext wrapContext(SslContext context) {
((ReferenceCountedOpenSslContext) context).setUseTasks(useTasks);
if (context instanceof ReferenceCountedOpenSslContext) {
((ReferenceCountedOpenSslContext) context).setUseTasks(useTasks);
}
return context;
}
}