Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Dec 9, 2024
1 parent cb8acf5 commit 783ec8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 783ec8c

Please sign in to comment.