|
| 1 | +package resilience.protocol; |
| 2 | + |
| 3 | +import com.arangodb.ArangoDB; |
| 4 | +import com.arangodb.Protocol; |
| 5 | +import org.junit.jupiter.api.*; |
| 6 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | +import resilience.utils.MemoryAppender; |
| 11 | + |
| 12 | +import javax.net.ssl.KeyManagerFactory; |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.TrustManagerFactory; |
| 15 | +import java.security.KeyStore; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +public class ProtocolTest { |
| 21 | + private static final String SSL_TRUSTSTORE = "/example.truststore"; |
| 22 | + private static final String SSL_TRUSTSTORE_PASSWORD = "12345678"; |
| 23 | + |
| 24 | + private MemoryAppender logs; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void init() { |
| 28 | + logs = new MemoryAppender(); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + void shutdown() { |
| 33 | + logs.stop(); |
| 34 | + } |
| 35 | + |
| 36 | + static Stream<Arguments> args() { |
| 37 | + return Stream.of( |
| 38 | + Arguments.of(Protocol.VST, "VstConnection"), |
| 39 | + Arguments.of(Protocol.HTTP_JSON, "LoggingHandler"), |
| 40 | + Arguments.of(Protocol.HTTP2_JSON, "Http2FrameLogger") |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @MethodSource("args") |
| 46 | + void shouldUseConfiguredProtocol(Protocol p, String expectedLog) { |
| 47 | + ArangoDB adb = new ArangoDB.Builder() |
| 48 | + .host("localhost", 8529) |
| 49 | + .password("test") |
| 50 | + .protocol(p) |
| 51 | + .build(); |
| 52 | + adb.getVersion(); |
| 53 | + assertThat(logs.getLogs()).anyMatch(it -> it.getLoggerName().contains(expectedLog)); |
| 54 | + adb.shutdown(); |
| 55 | + } |
| 56 | + |
| 57 | + @Tag("ssl") |
| 58 | + @EnabledIfSystemProperty(named = "SslTest", matches = "true") |
| 59 | + @ParameterizedTest |
| 60 | + @MethodSource("args") |
| 61 | + void shouldUseConfiguredProtocolWithTLS(Protocol p, String expectedLog) throws Exception { |
| 62 | + ArangoDB adb = new ArangoDB.Builder() |
| 63 | + .host("localhost", 8529) |
| 64 | + .password("test") |
| 65 | + .protocol(p) |
| 66 | + .useSsl(true) |
| 67 | + .sslContext(sslContext()) |
| 68 | + .build(); |
| 69 | + adb.getVersion(); |
| 70 | + assertThat(logs.getLogs()).anyMatch(it -> it.getLoggerName().contains(expectedLog)); |
| 71 | + adb.shutdown(); |
| 72 | + } |
| 73 | + |
| 74 | + private SSLContext sslContext() throws Exception{ |
| 75 | + final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 76 | + ks.load(this.getClass().getResourceAsStream(SSL_TRUSTSTORE), SSL_TRUSTSTORE_PASSWORD.toCharArray()); |
| 77 | + |
| 78 | + final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 79 | + kmf.init(ks, SSL_TRUSTSTORE_PASSWORD.toCharArray()); |
| 80 | + |
| 81 | + final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 82 | + tmf.init(ks); |
| 83 | + |
| 84 | + final SSLContext sc = SSLContext.getInstance("TLS"); |
| 85 | + sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 86 | + |
| 87 | + return sc; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments