Simplify test setup in Netty 4 transport tests

This commit collapes some duplicated setup code in every test in the
Netty 4 security transport tests to a common method.

Original commit: elastic/x-pack-elasticsearch@0a088db78d
This commit is contained in:
Jason Tedor 2016-08-03 11:07:03 -04:00
parent b3f8495a03
commit 832cb4fff2
1 changed files with 32 additions and 123 deletions

View File

@ -48,19 +48,30 @@ public class SecurityNetty4TransportTests extends ESTestCase {
clientSSLService = new ClientSSLService(settings, env, globalSSLConfiguration);
}
private SecurityNetty4Transport createTransport(boolean sslEnabled) {
return createTransport(sslEnabled, Settings.EMPTY);
}
private SecurityNetty4Transport createTransport(boolean sslEnabled, Settings additionalSettings) {
final Settings settings =
Settings.builder()
.put(SecurityNetty4Transport.SSL_SETTING.getKey(), sslEnabled)
.put(additionalSettings)
.build();
return new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
}
public void testThatSSLCanBeDisabledByProfile() throws Exception {
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client",
Settings.builder().put("xpack.security.ssl", false).build());
@ -69,18 +80,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
}
public void testThatSSLCanBeEnabledByProfile() throws Exception {
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), false).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(false);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client",
Settings.builder().put("xpack.security.ssl", true).build());
@ -89,18 +89,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
}
public void testThatProfileTakesDefaultSSLSetting() throws Exception {
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client", Settings.EMPTY);
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -108,18 +97,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
}
public void testDefaultClientAuth() throws Exception {
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client", Settings.EMPTY);
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -129,20 +107,8 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testRequiredClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.REQUIRED.name(), SSLClientAuth.REQUIRED.name().toLowerCase(Locale.ROOT), "true");
Settings settings = Settings.builder()
.put(SecurityNetty4Transport.SSL_SETTING.getKey(), true)
.put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
createTransport(true, Settings.builder().put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build());
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client", Settings.EMPTY);
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -152,20 +118,8 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testNoClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.NO.name(), "false", "FALSE", SSLClientAuth.NO.name().toLowerCase(Locale.ROOT));
Settings settings = Settings.builder()
.put(SecurityNetty4Transport.SSL_SETTING.getKey(), true)
.put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
createTransport(true, Settings.builder().put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build());
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client", Settings.EMPTY);
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -175,20 +129,8 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testOptionalClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.OPTIONAL.name(), SSLClientAuth.OPTIONAL.name().toLowerCase(Locale.ROOT));
Settings settings = Settings.builder()
.put(SecurityNetty4Transport.SSL_SETTING.getKey(), true)
.put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
createTransport(true, Settings.builder().put(SecurityNetty4Transport.CLIENT_AUTH_SETTING.getKey(), value).build());
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client", Settings.EMPTY);
final EmbeddedChannel ch = new EmbeddedChannel(handler);
@ -198,18 +140,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testProfileRequiredClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.REQUIRED.name(), SSLClientAuth.REQUIRED.name().toLowerCase(Locale.ROOT), "true", "TRUE");
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client",
Settings.builder().put(SecurityNetty4Transport.PROFILE_CLIENT_AUTH_SETTING, value).build());
@ -220,18 +151,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testProfileNoClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.NO.name(), "false", "FALSE", SSLClientAuth.NO.name().toLowerCase(Locale.ROOT));
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
ChannelHandler handler = transport.getServerChannelInitializer("client",
Settings.builder().put(SecurityNetty4Transport.PROFILE_CLIENT_AUTH_SETTING.getKey(), value).build());
@ -242,18 +162,7 @@ public class SecurityNetty4TransportTests extends ESTestCase {
public void testProfileOptionalClientAuth() throws Exception {
String value = randomFrom(SSLClientAuth.OPTIONAL.name(), SSLClientAuth.OPTIONAL.name().toLowerCase(Locale.ROOT));
Settings settings = Settings.builder().put(SecurityNetty4Transport.SSL_SETTING.getKey(), true).build();
SecurityNetty4Transport transport =
new SecurityNetty4Transport(
settings,
mock(ThreadPool.class),
mock(NetworkService.class),
mock(BigArrays.class),
mock(NamedWriteableRegistry.class),
mock(CircuitBreakerService.class),
null,
serverSSLService,
clientSSLService);
SecurityNetty4Transport transport = createTransport(true);
Netty4MockUtil.setOpenChannelsHandlerToMock(transport);
final ChannelHandler handler = transport.getServerChannelInitializer("client",
Settings.builder().put(SecurityNetty4Transport.PROFILE_CLIENT_AUTH_SETTING.getKey(), value).build());