martinzink commented on a change in pull request #978:
URL: https://github.com/apache/nifi-minifi-cpp/pull/978#discussion_r566675746



##########
File path: 
extensions/standard-processors/tests/integration/TLSClientSocketSupportedProtocolsTest.cpp
##########
@@ -0,0 +1,213 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.
+ */
+#include <signal.h>
+#include <sys/stat.h>
+#include <chrono>
+#include <thread>
+#undef NDEBUG
+#include <cassert>
+#include <utility>
+#include <memory>
+#include <string>
+#include "properties/Configure.h"
+#include "io/tls/TLSSocket.h"
+
+#ifdef WIN32
+using SocketDescriptor = SOCKET;
+#else
+using SocketDescriptor = int;
+static constexpr SocketDescriptor INVALID_SOCKET = -1;
+#endif /* WIN32 */
+
+
+class SimpleSSLTestServer  {
+ public:
+  SimpleSSLTestServer(const SSL_METHOD* method, const std::string& port, const 
std::string& path)
+      : port_(port), had_connection_(false) {
+    ctx_ = SSL_CTX_new(method);
+    configureContext(path);
+    socket_descriptor_ = createSocket(std::stoi(port_));
+  }
+
+  ~SimpleSSLTestServer() {
+      SSL_shutdown(ssl_);
+      SSL_free(ssl_);
+      SSL_CTX_free(ctx_);
+  }
+
+  void waitForConnection() {
+    server_read_thread_ = std::thread([this]() -> void {
+        SocketDescriptor client = accept(socket_descriptor_, nullptr, nullptr);
+        if (client != INVALID_SOCKET) {
+            ssl_ = SSL_new(ctx_);
+            SSL_set_fd(ssl_, client);
+            had_connection_ = (SSL_accept(ssl_) == 1);
+        }
+    });
+  }
+
+  void shutdownServer() {
+#ifdef WIN32
+    shutdown(socket_descriptor_, SD_BOTH);
+    closesocket(socket_descriptor_);
+#else
+    shutdown(socket_descriptor_, SHUT_RDWR);
+    close(socket_descriptor_);
+#endif
+    server_read_thread_.join();
+  }
+
+  bool hadConnection() const {
+    return had_connection_;
+  }
+
+ private:
+  SSL_CTX *ctx_;
+  SSL* ssl_;
+  std::string port_;
+  uint16_t listeners_;
+  SocketDescriptor socket_descriptor_;
+  bool had_connection_;
+  std::thread server_read_thread_;
+
+  void configureContext(const std::string& path) {
+    SSL_CTX_set_ecdh_auto(ctx_, 1);
+    /* Set the key and cert */
+    assert(SSL_CTX_use_certificate_file(ctx_, (path + "cn.crt.pem").c_str(), 
SSL_FILETYPE_PEM) > 0);
+    assert(SSL_CTX_use_PrivateKey_file(ctx_, (path + "cn.ckey.pem").c_str(), 
SSL_FILETYPE_PEM) > 0);

Review comment:
       fixed

##########
File path: 
extensions/standard-processors/tests/integration/TLSServerSocketSupportedProtocolsTest.cpp
##########
@@ -0,0 +1,311 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.
+ */
+#ifndef WIN32
+#include <arpa/inet.h>
+#endif
+#include <signal.h>
+#include <sys/stat.h>
+#include <chrono>
+#include <thread>
+#include <cerrno>
+#include <cinttypes>
+#undef NDEBUG
+#include <cassert>
+#include <utility>
+#include <memory>
+#include <string>
+#ifdef WIN32
+#include <ws2tcpip.h>
+#include <winsock2.h>
+#endif
+#include "properties/Configure.h"
+#include "io/tls/TLSSocket.h"
+#include "io/tls/TLSServerSocket.h"
+
+#ifdef WIN32
+using SocketDescriptor = SOCKET;
+#else
+using SocketDescriptor = int;
+static constexpr SocketDescriptor INVALID_SOCKET = -1;
+#endif /* WIN32 */
+
+namespace {
+const char* str_family(int family) {
+  switch (family) {
+    case AF_INET: return "AF_INET";
+    case AF_INET6: return "AF_INET6";
+    default: return "(n/a)";
+  }
+}
+
+const char* str_socktype(int socktype) {
+  switch (socktype) {
+    case SOCK_STREAM: return "SOCK_STREAM";
+    case SOCK_DGRAM: return "SOCK_DGRAM";
+    default: return "(n/a)";
+  }
+}
+
+const char* str_proto(int protocol) {
+  switch (protocol) {
+    case IPPROTO_TCP: return "IPPROTO_TCP";
+    case IPPROTO_UDP: return "IPPROTO_UDP";
+    case IPPROTO_IP: return "IPPROTO_IP";
+    case IPPROTO_ICMP: return "IPPROTO_ICMP";
+    default: return "(n/a)";
+  }
+}
+
+std::string str_addr(const sockaddr* const sa) {
+  char buf[128] = {0};
+  switch (sa->sa_family) {
+    case AF_INET: {
+      sockaddr_in sin{};
+      memcpy(&sin, sa, sizeof(sockaddr_in));
+#ifdef WIN32
+      const auto addr_str = InetNtop(AF_INET, &sin.sin_addr, buf, sizeof(buf));
+#else
+      const auto addr_str = inet_ntop(AF_INET, &sin.sin_addr, buf, 
sizeof(buf));
+#endif
+      if (!addr_str) {
+        throw std::runtime_error{minifi::io::get_last_socket_error_message()};
+      }
+      return std::string{addr_str};
+    }
+    case AF_INET6: {
+      sockaddr_in6 sin6{};
+      memcpy(&sin6, sa, sizeof(sockaddr_in6));
+#ifdef WIN32
+      const auto addr_str = InetNtop(AF_INET, &sin6.sin6_addr, buf, 
sizeof(buf));
+#else
+      const auto addr_str = inet_ntop(AF_INET, &sin6.sin6_addr, buf, 
sizeof(buf));
+#endif
+      if (!addr_str) {
+        throw std::runtime_error{minifi::io::get_last_socket_error_message()};
+      }
+      return std::string{addr_str};
+    }
+    default: return "(n/a)";
+  }
+}
+
+void log_addrinfo(addrinfo* const ai, logging::Logger& logger) {
+  logger.log_debug(".ai_family: %d %s\n", ai->ai_family, 
str_family(ai->ai_family));
+  logger.log_debug(".ai_socktype: %d %s\n", ai->ai_socktype, 
str_socktype(ai->ai_socktype));
+  logger.log_debug(".ai_protocol: %d %s\n", ai->ai_protocol, 
str_proto(ai->ai_protocol));
+  logger.log_debug(".ai_addr: %s\n", str_addr(ai->ai_addr).c_str());
+  logger.log_debug(".ai_addrlen: %" PRIu32 "\n", ai->ai_addrlen);
+}
+}  // namespace
+
+class SimpleSSLTestClient  {
+ public:
+  SimpleSSLTestClient(const SSL_METHOD* method, const std::string& host, const 
std::string& port) :
+    host_(host),
+    port_(port) {
+      ctx_ = SSL_CTX_new(method);
+      sfd_ = openConnection(host_.c_str(), port_.c_str(), *logger_);
+      if (ctx_ != nullptr)
+        ssl_ = SSL_new(ctx_);
+      if (ssl_ != nullptr)
+        SSL_set_fd(ssl_, sfd_);
+  }
+
+  ~SimpleSSLTestClient() {
+    SSL_free(ssl_);
+#ifdef WIN32
+    closesocket(sfd_);
+#else
+    close(sfd_);
+#endif
+    SSL_CTX_free(ctx_);
+  }
+
+  bool canConnect() {
+    const int status = SSL_connect(ssl_);
+    const bool successful_connection = (status == 1);
+    return successful_connection;
+  }
+
+ private:
+  SSL_CTX *ctx_;
+  SSL* ssl_;

Review comment:
       fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to