From 783ec8cb3305332a764961bcc07d2b61911d271f Mon Sep 17 00:00:00 2001 From: Hakky54 Date: Tue, 10 Dec 2024 00:50:43 +0100 Subject: [PATCH] Added tests --- .../socket/FenixSSLServerSocketFactory.java | 9 ++------- .../ssl/util/TrustManagerUtilsShould.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/socket/FenixSSLServerSocketFactory.java b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/socket/FenixSSLServerSocketFactory.java index e81e9616..9ee2de82 100644 --- a/sslcontext-kickstart/src/main/java/nl/altindag/ssl/socket/FenixSSLServerSocketFactory.java +++ b/sslcontext-kickstart/src/main/java/nl/altindag/ssl/socket/FenixSSLServerSocketFactory.java @@ -15,7 +15,6 @@ */ package nl.altindag.ssl.socket; -import nl.altindag.ssl.exception.GenericIOException; import nl.altindag.ssl.sslparameters.HotSwappableSSLParameters; import nl.altindag.ssl.util.SSLParametersUtils; @@ -81,17 +80,13 @@ public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddr return withSslParameters(serverSocket); } - private ServerSocket withSslParameters(ServerSocket socket) { + private ServerSocket withSslParameters(ServerSocket socket) throws IOException { if (socket instanceof SSLServerSocket) { SSLServerSocket sslSocket = (SSLServerSocket) socket; sslSocket.setSSLParameters(SSLParametersUtils.copy(sslParameters)); if (sslParameters instanceof HotSwappableSSLParameters) { - try { - return new FenixSSLServerSocket(sslSocket, sslParameters); - } catch (IOException e) { - throw new GenericIOException(e); - } + return new FenixSSLServerSocket(sslSocket, sslParameters); } } return socket; diff --git a/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/TrustManagerUtilsShould.java b/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/TrustManagerUtilsShould.java index 4e6501fe..22fda2a0 100644 --- a/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/TrustManagerUtilsShould.java +++ b/sslcontext-kickstart/src/test/java/nl/altindag/ssl/util/TrustManagerUtilsShould.java @@ -46,6 +46,7 @@ import java.security.Provider; import java.security.Security; import java.security.cert.CertPathBuilder; +import java.security.cert.CertificateException; import java.security.cert.PKIXBuilderParameters; import java.security.cert.PKIXRevocationChecker; import java.security.cert.X509CertSelector; @@ -317,6 +318,24 @@ void trustManagerShouldNotSwapWhenLoggingTrustManagerDoesNotContainSwappableTrus "and should be an instance of [nl.altindag.ssl.trustmanager.HotSwappableX509ExtendedTrustManager]."); } + @Test + void createEnhanceableTrustManagerDoesSkipCallingBaseTrustManagerWhenCustomValidatorReturnsTrue() throws CertificateException { + X509ExtendedTrustManager baseTrustManager = mock(X509ExtendedTrustManager.class); + X509ExtendedTrustManager enhanceableTrustManager = TrustManagerUtils.createEnhanceableTrustManager(baseTrustManager, trustManagerParameters -> "RSA".equals(trustManagerParameters.getAuthType())); + + enhanceableTrustManager.checkServerTrusted(null, "RSA"); + verify(baseTrustManager, times(0)).checkServerTrusted(null, "RSA"); + } + + @Test + void createEnhanceableTrustManagerDoesCallsBaseTrustManagerWhenCustomValidatorReturnsFalse() throws CertificateException { + X509ExtendedTrustManager baseTrustManager = mock(X509ExtendedTrustManager.class); + X509ExtendedTrustManager enhanceableTrustManager = TrustManagerUtils.createEnhanceableTrustManager(baseTrustManager, trustManagerParameters -> "RSA".equals(trustManagerParameters.getAuthType())); + + enhanceableTrustManager.checkServerTrusted(null, "ASR"); + verify(baseTrustManager, times(1)).checkServerTrusted(null, "ASR"); + } + @Test void trustManagerShouldSwapEvenThoughItContainsAnEnhanceableTrustManager() { X509ExtendedTrustManager trustManager = TrustManagerUtils.trustManagerBuilder()