Skip to content

Commit f0640af

Browse files
committed
Add TLS tests with SNI
1 parent d7647e7 commit f0640af

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/test/java/com/rabbitmq/stream/impl/TlsTest.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import com.rabbitmq.stream.impl.TestUtils.DisabledIfAuthMechanismSslNotEnabled;
3131
import com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled;
3232
import com.rabbitmq.stream.sasl.DefaultSaslConfiguration;
33-
import io.netty.channel.Channel;
3433
import io.netty.handler.ssl.SslContext;
3534
import io.netty.handler.ssl.SslContextBuilder;
36-
import io.netty.handler.ssl.SslHandler;
3735
import java.io.File;
3836
import java.io.FileInputStream;
3937
import java.net.InetAddress;
@@ -50,12 +48,10 @@
5048
import java.util.Collections;
5149
import java.util.UUID;
5250
import java.util.concurrent.CountDownLatch;
53-
import java.util.function.Consumer;
5451
import java.util.stream.IntStream;
5552
import javax.net.ssl.SNIHostName;
5653
import javax.net.ssl.SSLException;
5754
import javax.net.ssl.SSLHandshakeException;
58-
import javax.net.ssl.SSLParameters;
5955
import org.junit.jupiter.api.Test;
6056
import org.junit.jupiter.api.extension.ExtendWith;
6157

@@ -192,26 +188,32 @@ void unverifiedConnection() {
192188
}
193189

194190
@Test
195-
void unverifiedConnectionWithSni() {
196-
Consumer<Channel> channelCustomizer =
197-
ch -> {
198-
SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
199-
if (sslHandler != null) {
200-
SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
201-
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("localhost")));
202-
sslHandler.engine().setSSLParameters(sslParameters);
203-
}
204-
};
205-
cf.get(
206-
new ClientParameters()
207-
.sslContext(alwaysTrustSslContext())
208-
.channelCustomizer(channelCustomizer));
191+
void verifiedConnectionWithCorrectServerCertificate() throws Exception {
192+
// in server certificate SAN
193+
String hostname = "localhost";
194+
SslContext context = SslContextBuilder.forClient().trustManager(caCertificate()).build();
195+
cf.get(new ClientParameters().host(hostname).sslContext(context));
209196
}
210197

211198
@Test
212-
void verifiedConnectionWithCorrectServerCertificate() throws Exception {
199+
void verifiedConnectionWithCorrectServerCertificateWithSni() throws Exception {
200+
// not in server certificate SAN, but setting SNI makes it work
201+
String hostname = "127.0.0.1";
202+
SslContext context =
203+
SslContextBuilder.forClient()
204+
.trustManager(caCertificate())
205+
.serverName(new SNIHostName("localhost"))
206+
.build();
207+
cf.get(new ClientParameters().host(hostname).sslContext(context));
208+
}
209+
210+
@Test
211+
void verifiedConnectionWithCorrectServerCertificateFailsIfHostnameNotInSan() throws Exception {
212+
// not in server certificate SAN
213+
String hostname = "127.0.0.1";
213214
SslContext context = SslContextBuilder.forClient().trustManager(caCertificate()).build();
214-
cf.get(new ClientParameters().sslContext(context));
215+
assertThatThrownBy(() -> cf.get(new ClientParameters().host(hostname).sslContext(context)))
216+
.hasCauseInstanceOf(SSLHandshakeException.class);
215217
}
216218

217219
@Test

0 commit comments

Comments
 (0)