Add test for ssl version of connection in CT 93/78493/8
authorkjaniak <kornel.janiak@nokia.com>
Thu, 14 Feb 2019 13:51:56 +0000 (14:51 +0100)
committerkjaniak <kornel.janiak@nokia.com>
Tue, 19 Feb 2019 11:15:02 +0000 (12:15 +0100)
Issue-ID: DCAEGEN2-1223
Change-Id: I0828a9637376dd6176ba07cf6f35f382d1e41070
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
services/hv-ves-client/producer/ct/src/test/java/org/onap/dcaegen2/services/sdk/services/hvves/client/producer/ct/DummyCollector.java
services/hv-ves-client/producer/ct/src/test/java/org/onap/dcaegen2/services/sdk/services/hvves/client/producer/ct/HvVesProducerIT.java
services/hv-ves-client/producer/ct/src/test/java/org/onap/dcaegen2/services/sdk/services/hvves/client/producer/ct/SystemUnderTestWrapper.java
services/hv-ves-client/producer/ct/src/test/resources/client.p12
services/hv-ves-client/producer/ct/src/test/resources/server.p12 [new file with mode: 0644]
services/hv-ves-client/producer/ct/src/test/resources/server.pass [new file with mode: 0644]
services/hv-ves-client/producer/ct/src/test/resources/trust.p12

index 46aeacc..70e9cdf 100644 (file)
 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.ct;
 
 import io.netty.buffer.ByteBuf;
+
 import java.net.InetSocketAddress;
 import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.IntStream;
+
+import io.netty.handler.ssl.SslContext;
 import org.reactivestreams.Publisher;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.ReplayProcessor;
@@ -39,6 +43,7 @@ import reactor.util.function.Tuple2;
  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
  */
 public class DummyCollector {
+    private Optional<SslContext> sslContext;
 
     private final List<ByteBuf> receivedData = Collections.synchronizedList(new ArrayList<>());
     private DisposableServer server;
@@ -48,13 +53,20 @@ public class DummyCollector {
             .map(Tuple2::getT1)
             .share();
 
+    DummyCollector(Optional<SslContext> sslContext) {
+        this.sslContext = sslContext;
+    }
+
     public InetSocketAddress start() {
-        server = TcpServer.create()
-                .host("localhost")
-                .port(6666)
-                .wiretap(true)
-                .handle(this::handleConnection)
-                .bindNow();
+        TcpServer tcpServer =
+                sslContext.map(context -> TcpServer.create()
+                        .secure(ssl -> ssl.sslContext(context)))
+                        .orElseGet(TcpServer::create)
+                        .host("localhost")
+                        .port(6666)
+                        .wiretap(true)
+                        .handle(this::handleConnection);
+        server = tcpServer.bindNow();
         return server.address();
     }
 
index 247cfad..3417557 100644 (file)
@@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import io.netty.buffer.ByteBuf;
 import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.PayloadType;
 import org.onap.ves.MeasDataCollectionOuterClass;
@@ -31,6 +30,8 @@ import org.onap.ves.VesEventOuterClass.CommonEventHeader;
 import org.onap.ves.VesEventOuterClass.VesEvent;
 import reactor.core.publisher.Flux;
 
+import java.time.Duration;
+
 /**
  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
  */
@@ -42,12 +43,7 @@ class HvVesProducerIT {
     private static final int PERIOD = 1000;
     private static final String OBJECT_INSTANCE_ID = "DH-1";
 
-    private final SystemUnderTestWrapper sut = new SystemUnderTestWrapper();
-
-    @BeforeEach
-    void setUp() {
-        sut.start();
-    }
+    private final SystemUnderTestWrapper sut = new SystemUnderTestWrapper(Duration.ofSeconds(10));
 
     @AfterEach
     void tearDown() {
@@ -55,19 +51,37 @@ class HvVesProducerIT {
     }
 
     @Test
-    void singleMessageTest() throws Exception {
+    void singleMessageTest_withUnsecureConnection() throws Exception {
         // given
+        final VesEvent sampleEvent = createSimpleVesEvent();
+        final Flux<VesEvent> input = Flux.just(sampleEvent);
+
+        // when
+        sut.start();
+        final ByteBuf receivedData = sut.blockingSend(input);
 
+        // then
+        WireProtocolDecoder decoded = WireProtocolDecoder.decode(receivedData);
+        assertThat(decoded.type).isEqualTo(PayloadType.PROTOBUF.getPayloadTypeBytes().getShort());
+        assertThat(decoded.event).isEqualTo(sampleEvent);
+
+    }
+
+    @Test
+    void singleMessageTest_withSecureConnection() throws Exception {
+        // given
         final VesEvent sampleEvent = createSimpleVesEvent();
         final Flux<VesEvent> input = Flux.just(sampleEvent);
 
         // when
+        sut.startSecure();
         final ByteBuf receivedData = sut.blockingSend(input);
 
         // then
         WireProtocolDecoder decoded = WireProtocolDecoder.decode(receivedData);
         assertThat(decoded.type).isEqualTo(PayloadType.PROTOBUF.getPayloadTypeBytes().getShort());
         assertThat(decoded.event).isEqualTo(sampleEvent);
+
     }
 
     private VesEvent createSimpleVesEvent() {
index ec16e9e..45511d7 100644 (file)
@@ -20,6 +20,7 @@
 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.ct;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.handler.ssl.SslContext;
 import io.vavr.collection.HashSet;
 import io.vavr.control.Try;
 
@@ -27,10 +28,9 @@ import java.net.InetSocketAddress;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.time.Duration;
+import java.util.Optional;
 
-import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys;
-import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore;
-import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
+import org.onap.dcaegen2.services.sdk.security.ssl.*;
 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducer;
 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducerFactory;
 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableProducerOptions;
@@ -46,9 +46,17 @@ import reactor.core.publisher.Flux;
 public class SystemUnderTestWrapper {
 
     private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5);
-    private final DummyCollector collector = new DummyCollector();
+    private static final String TRUST_CERT_PATH = "/trust.p12";
+    private static final String TRUST_PASSWORD_PATH = "/trust.pass";
+    private static final String CLIENT_CERT_PATH = "/client.p12";
+    private static final String CLIENT_PASSWORD_PATH = "/client.pass";
+    private static final String SERVER_CERT_PATH = "/server.p12";
+    private static final String SERVER_PASSWORD_PATH = "/server.pass";
+
+    private DummyCollector collector;
     private HvVesProducer cut;
     private final Duration timeout;
+    private final SslFactory sslFactory = new SslFactory();
 
     public SystemUnderTestWrapper(Duration timeout) {
         this.timeout = timeout;
@@ -59,16 +67,19 @@ public class SystemUnderTestWrapper {
     }
 
     public void startSecure() {
-        start(ImmutableProducerOptions.builder()
-                .securityKeys(ImmutableSecurityKeys.builder()
-                        .keyStore(ImmutableSecurityKeysStore.of(resource("/client.p12").get()))
-                        .keyStorePassword(Passwords.fromResource("/client.pass"))
-                        .trustStore(ImmutableSecurityKeysStore.of(resource("/trust.p12").get()))
-                        .trustStorePassword(Passwords.fromResource("/trust.pass"))
-                        .build()));
+        collector = createCollectorWithEnabledSSL();
+
+        final SecurityKeys producerSecurityKeys = ImmutableSecurityKeys.builder()
+                .keyStore(ImmutableSecurityKeysStore.of(resource(CLIENT_CERT_PATH).get()))
+                .keyStorePassword(Passwords.fromResource(CLIENT_PASSWORD_PATH))
+                .trustStore(ImmutableSecurityKeysStore.of(resource(TRUST_CERT_PATH).get()))
+                .trustStorePassword(Passwords.fromResource(TRUST_PASSWORD_PATH))
+                .build();
+        start(ImmutableProducerOptions.builder().securityKeys(producerSecurityKeys));
     }
 
     public void start() {
+        collector = new DummyCollector(Optional.empty());
         start(createDefaultOptions());
     }
 
@@ -90,6 +101,17 @@ public class SystemUnderTestWrapper {
         return collector.dataFromFirstClient();
     }
 
+    private DummyCollector createCollectorWithEnabledSSL() {
+        final SecurityKeys collectorSecurityKeys = ImmutableSecurityKeys.builder()
+                .keyStore(ImmutableSecurityKeysStore.of(resource(SERVER_CERT_PATH).get()))
+                .keyStorePassword(Passwords.fromResource(SERVER_PASSWORD_PATH))
+                .trustStore(ImmutableSecurityKeysStore.of(resource(TRUST_CERT_PATH).get()))
+                .trustStorePassword(Passwords.fromResource(TRUST_PASSWORD_PATH))
+                .build();
+        final SslContext collectorSslContext = sslFactory.createSecureServerContext(collectorSecurityKeys);
+        return new DummyCollector(Optional.of(collectorSslContext));
+    }
+
     private Builder createDefaultOptions() {
         return ImmutableProducerOptions.builder();
     }
index 68a0fb2..26b79d7 100644 (file)
Binary files a/services/hv-ves-client/producer/ct/src/test/resources/client.p12 and b/services/hv-ves-client/producer/ct/src/test/resources/client.p12 differ
diff --git a/services/hv-ves-client/producer/ct/src/test/resources/server.p12 b/services/hv-ves-client/producer/ct/src/test/resources/server.p12
new file mode 100644 (file)
index 0000000..169ecf3
Binary files /dev/null and b/services/hv-ves-client/producer/ct/src/test/resources/server.p12 differ
diff --git a/services/hv-ves-client/producer/ct/src/test/resources/server.pass b/services/hv-ves-client/producer/ct/src/test/resources/server.pass
new file mode 100644 (file)
index 0000000..e69c2de
--- /dev/null
@@ -0,0 +1 @@
+onaponap
\ No newline at end of file
index ed7f62d..1ca2f65 100644 (file)
Binary files a/services/hv-ves-client/producer/ct/src/test/resources/trust.p12 and b/services/hv-ves-client/producer/ct/src/test/resources/trust.p12 differ