Skip to content

Commit

Permalink
Merge pull request #493 from Hakky54/feature/get-hostname-port-from-t…
Browse files Browse the repository at this point in the history
…rustmanager-parameters

Added support for hostname and port in TrustManagerParameters
  • Loading branch information
Hakky54 authored May 6, 2024
2 parents b81821e + 186ee7d commit cdd4d78
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.net.Socket;
import java.security.cert.X509Certificate;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
* @author Hakan Altindag
Expand Down Expand Up @@ -53,4 +55,37 @@ public Optional<SSLEngine> getSslEngine() {
return Optional.ofNullable(sslEngine);
}

public Optional<String> getHostname() {
return findFirst(getHostnameFromSslEngine(), getHostnameFromSocket());
}

private Supplier<Optional<String>> getHostnameFromSslEngine() {
return () -> getSslEngine().map(SSLEngine::getPeerHost);
}

private Supplier<Optional<String>> getHostnameFromSocket() {
return () -> getSocket().map(socket -> socket.getInetAddress().getHostName());
}

public Optional<Integer> getPort() {
return findFirst(getPortFromSslEngine(), getPortFromSocket());
}

private Supplier<Optional<Integer>> getPortFromSslEngine() {
return () -> getSslEngine().map(SSLEngine::getPeerPort);
}

private Supplier<Optional<Integer>> getPortFromSocket() {
return () -> getSocket().map(Socket::getPort);
}

@SafeVarargs
private static <T> Optional<T> findFirst(Supplier<Optional<T>>... suppliers) {
return Stream.of(suppliers)
.map(Supplier::get)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2019 Thunderberry.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.altindag.ssl.model;

import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSocket;

import java.net.Inet4Address;
import java.net.InetAddress;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Hakan Altindag
*/
class TrustManagerParametersShould {

@Test
void getHostnameFromSslEngineIfAvailable() {
SSLEngine sslEngine = mock(SSLEngine.class);
when(sslEngine.getPeerHost()).thenReturn("localhost");

TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, null, sslEngine);
assertThat(trustManagerParameters.getHostname()).hasValue("localhost");
}

@Test
void getHostnameFromSocketIfAvailable() {
SSLSocket socket = mock(SSLSocket.class);
InetAddress inetAddress = mock(Inet4Address.class);

when(inetAddress.getHostName()).thenReturn("localhost");
when(socket.getInetAddress()).thenReturn(inetAddress);

TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, socket, null);
assertThat(trustManagerParameters.getHostname()).hasValue("localhost");
}

@Test
void getHostnameIsAbsentWhenNoSSLEngineAndSocketIsPresent() {
TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, null, null);
assertThat(trustManagerParameters.getHostname()).isEmpty();
}

@Test
void getPortFromSslEngineIfAvailable() {
SSLEngine sslEngine = mock(SSLEngine.class);
when(sslEngine.getPeerPort()).thenReturn(8443);

TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, null, sslEngine);
assertThat(trustManagerParameters.getPort()).hasValue(8443);
}

@Test
void getPortFromSocketIfAvailable() {
SSLSocket socket = mock(SSLSocket.class);
when(socket.getPort()).thenReturn(8443);

TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, socket, null);
assertThat(trustManagerParameters.getPort()).hasValue(8443);
}

@Test
void getPortIsAbsentWhenNoSSLEngineAndSocketIsPresent() {
TrustManagerParameters trustManagerParameters = new TrustManagerParameters(null, null, null, null);
assertThat(trustManagerParameters.getPort()).isEmpty();
}

}

0 comments on commit cdd4d78

Please sign in to comment.