Skip to content

Commit

Permalink
fix SSLErrorMessageCertificateVerificationTests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Jan 6, 2025
1 parent 5bfd4b9 commit 5620dcb
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.regex.Pattern;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -104,6 +103,7 @@ public void testMessageForRestClientHostnameVerificationFailure() throws IOExcep
}

public void testDiagnosticTrustManagerForHostnameVerificationFailure() throws Exception {

final Settings settings = getPemSSLSettings(
HTTP_SERVER_SSL,
"not-this-host.crt",
Expand Down Expand Up @@ -132,7 +132,7 @@ public void testDiagnosticTrustManagerForHostnameVerificationFailure() throws Ex
DiagnosticTrustManager.class.getName(),
Level.WARN,
"failed to establish trust with server at \\["
+ Pattern.quote(webServer.getHostName())
+ (inFipsJvm() ? "<unknown host>" : Pattern.quote(webServer.getHostName()))
+ "\\];"
+ " the server provided a certificate with subject name \\[CN=not-this-host\\],"
+ " fingerprint \\[[0-9a-f]{40}\\], no keyUsage and no extendedKeyUsage;"
Expand All @@ -153,13 +153,12 @@ public void testDiagnosticTrustManagerForHostnameVerificationFailure() throws Ex
enableHttpsHostnameChecking(clientSocket);
connect(clientSocket, webServer);
assertThat(clientSocket.isConnected(), is(true));
final SSLHandshakeException handshakeException = expectThrows(
SSLHandshakeException.class,
() -> clientSocket.getInputStream().read()
);
assertThat(handshakeException, throwableWithMessage(containsStringIgnoringCase("subject alternative names")));
assertThat(handshakeException, throwableWithMessage(containsString(webServer.getHostName())));

final Exception handshakeException = expectThrows(Exception.class, () -> clientSocket.getInputStream().read());
// Bouncy Castle throws a different exception message
if (inFipsJvm() == false) {
assertThat(handshakeException, throwableWithMessage(containsStringIgnoringCase("subject alternative names")));
assertThat(handshakeException, throwableWithMessage(containsString(webServer.getHostName())));
}
// Logging message failures are tricky to debug because you just get a "didn't find match" assertion failure.
// You should be able to check the log output for the text that was logged and compare to the regex above.
mockLog.assertAllExpectationsMatched();
Expand Down

0 comments on commit 5620dcb

Please sign in to comment.