lordgamez commented on a change in pull request #978: URL: https://github.com/apache/nifi-minifi-cpp/pull/978#discussion_r564556332
########## File path: extensions/standard-processors/tests/integration/TLSServerSocketSupportedProtocolsTest.cpp ########## @@ -0,0 +1,233 @@ +/** + * + * 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> +#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 */ + +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()); + 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_; + SocketDescriptor sfd_; + std::string host_; + std::string port_; + + static SocketDescriptor openConnection(const char *host_name, const char *port) { + struct hostent *host; + assert((host = gethostbyname(host_name)) != nullptr); + struct addrinfo hints = {0}, *addrs; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + const int status = getaddrinfo(host_name, port, &hints, &addrs); + assert(status == 0); + SocketDescriptor sfd = INVALID_SOCKET; + for (struct addrinfo *addr = addrs; addr != nullptr; addr = addr->ai_next) { + sfd = socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); + if (sfd == INVALID_SOCKET) { + continue; + } + if (connect(sfd, addr->ai_addr, addr->ai_addrlen) == 0) { + break; + } + sfd = INVALID_SOCKET; +#ifdef WIN32 + closesocket(sfd); +#else + close(sfd); +#endif Review comment: Minor indent issues ########## 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), has_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); + has_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 hasConnection() const { + return has_connection_; + } + + private: + SSL_CTX *ctx_; + SSL* ssl_; + std::string port_; + uint16_t listeners_; + SocketDescriptor socket_descriptor_; + bool has_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: Minor indent issue ---------------------------------------------------------------- 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: [email protected]
