martinzink commented on code in PR #1419: URL: https://github.com/apache/nifi-minifi-cpp/pull/1419#discussion_r1005557987
########## extensions/standard-processors/tests/unit/PutTCPTests.cpp: ########## @@ -0,0 +1,454 @@ +/** + * + * 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 <memory> +#include <new> +#include <random> +#include <string> +#include "SingleProcessorTestController.h" +#include "Catch.h" +#include "PutTCP.h" +#include "controllers/SSLContextService.h" +#include "core/ProcessSession.h" +#include "utils/net/TcpServer.h" +#include "utils/net/SslServer.h" +#include "utils/expected.h" +#include "utils/StringUtils.h" + +using namespace std::literals::chrono_literals; + +namespace org::apache::nifi::minifi::processors { + +using controllers::SSLContextService; + +namespace { +using utils::net::TcpSession; +using utils::net::TcpServer; + +using utils::net::SslSession; +using utils::net::SslServer; + +class ISessionAwareServer { + public: + [[nodiscard]] virtual size_t getNumberOfSessions() const = 0; + virtual void closeSessions() = 0; +}; + +template<class SocketType> +class SessionAwareServer : public ISessionAwareServer { + protected: + size_t getNumberOfSessions() const override { + std::lock_guard lock_guard{mutex_}; + return sessions_.size(); + } + + void closeSessions() override { + std::lock_guard lock_guard{mutex_}; + for (const auto& session_weak : sessions_) { + if (auto session = session_weak.lock()) { + auto& socket = session->getSocket(); + if (socket.is_open()) { + socket.shutdown(asio::ip::tcp::socket::shutdown_both); + session->getSocket().close(); + } + } + } + } + + mutable std::mutex mutex_; + std::vector<std::weak_ptr<SocketType>> sessions_; +}; + +class SessionAwareTcpServer : public TcpServer, public SessionAwareServer<TcpSession> { + public: + using TcpServer::TcpServer; + + protected: + std::shared_ptr<TcpSession> createSession() override { + std::lock_guard lock_guard{mutex_}; + auto session = TcpServer::createSession(); + logger_->log_trace("SessionAwareTcpServer::createSession %p", session.get()); + sessions_.emplace_back(session); + return session; + } +}; + +class SessionAwareSslServer : public SslServer, public SessionAwareServer<SslSession> { + public: + using SslServer::SslServer; + + protected: + std::shared_ptr<SslSession> createSession() override { + std::lock_guard lock_guard{mutex_}; + auto session = SslServer::createSession(); + logger_->log_trace("SessionAwareTcpServer::createSession %p", session.get()); + sessions_.emplace_back(session); + return session; + } +}; + +utils::net::SslData createSslDataForServer() { + const std::filesystem::path executable_dir = minifi::utils::file::FileUtils::get_executable_dir(); + utils::net::SslData ssl_data; + ssl_data.ca_loc = (executable_dir / "resources/ca_A.crt").string(); + ssl_data.cert_loc = (executable_dir / "resources/localhost_by_A.pem").string(); + ssl_data.key_loc = (executable_dir / "resources/localhost_by_A.pem").string(); Review Comment: good idea, I've changed it in https://github.com/apache/nifi-minifi-cpp/pull/1419/commits/9928eaa75f607d1d337d289c7bbf84f23e20ee5a#diff-bbb56b112b398a6450c23e71e5dbbfb17bf20dad3d2fb908a1c6de26118f9ee8R108-R110 ########## extensions/standard-processors/tests/unit/PutTCPTests.cpp: ########## @@ -0,0 +1,454 @@ +/** + * + * 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 <memory> +#include <new> +#include <random> +#include <string> +#include "SingleProcessorTestController.h" +#include "Catch.h" +#include "PutTCP.h" +#include "controllers/SSLContextService.h" +#include "core/ProcessSession.h" +#include "utils/net/TcpServer.h" +#include "utils/net/SslServer.h" +#include "utils/expected.h" +#include "utils/StringUtils.h" + +using namespace std::literals::chrono_literals; + +namespace org::apache::nifi::minifi::processors { + +using controllers::SSLContextService; + +namespace { +using utils::net::TcpSession; +using utils::net::TcpServer; + +using utils::net::SslSession; +using utils::net::SslServer; + +class ISessionAwareServer { + public: + [[nodiscard]] virtual size_t getNumberOfSessions() const = 0; + virtual void closeSessions() = 0; +}; + +template<class SocketType> +class SessionAwareServer : public ISessionAwareServer { + protected: + size_t getNumberOfSessions() const override { + std::lock_guard lock_guard{mutex_}; + return sessions_.size(); + } + + void closeSessions() override { + std::lock_guard lock_guard{mutex_}; + for (const auto& session_weak : sessions_) { + if (auto session = session_weak.lock()) { + auto& socket = session->getSocket(); + if (socket.is_open()) { + socket.shutdown(asio::ip::tcp::socket::shutdown_both); + session->getSocket().close(); + } + } + } + } + + mutable std::mutex mutex_; + std::vector<std::weak_ptr<SocketType>> sessions_; +}; + +class SessionAwareTcpServer : public TcpServer, public SessionAwareServer<TcpSession> { + public: + using TcpServer::TcpServer; + + protected: + std::shared_ptr<TcpSession> createSession() override { + std::lock_guard lock_guard{mutex_}; + auto session = TcpServer::createSession(); + logger_->log_trace("SessionAwareTcpServer::createSession %p", session.get()); + sessions_.emplace_back(session); + return session; + } +}; + +class SessionAwareSslServer : public SslServer, public SessionAwareServer<SslSession> { + public: + using SslServer::SslServer; + + protected: + std::shared_ptr<SslSession> createSession() override { + std::lock_guard lock_guard{mutex_}; + auto session = SslServer::createSession(); + logger_->log_trace("SessionAwareTcpServer::createSession %p", session.get()); + sessions_.emplace_back(session); + return session; + } +}; + +utils::net::SslData createSslDataForServer() { + const std::filesystem::path executable_dir = minifi::utils::file::FileUtils::get_executable_dir(); + utils::net::SslData ssl_data; + ssl_data.ca_loc = (executable_dir / "resources/ca_A.crt").string(); + ssl_data.cert_loc = (executable_dir / "resources/localhost_by_A.pem").string(); + ssl_data.key_loc = (executable_dir / "resources/localhost_by_A.pem").string(); + return ssl_data; +} +} // namespace + +class PutTCPTestFixture { + public: + PutTCPTestFixture() { + LogTestController::getInstance().setTrace<PutTCP>(); + LogTestController::getInstance().setInfo<core::ProcessSession>(); + LogTestController::getInstance().setTrace<utils::net::Server>(); + put_tcp_->setProperty(PutTCP::Hostname, "${literal('localhost')}"); + put_tcp_->setProperty(PutTCP::Port, utils::StringUtils::join_pack("${literal('", std::to_string(port_), "')}")); + put_tcp_->setProperty(PutTCP::Timeout, "200 ms"); + put_tcp_->setProperty(PutTCP::OutgoingMessageDelimiter, "\n"); + } + + ~PutTCPTestFixture() { + stopServer(); + } + + void startTCPServer() { + gsl_Expects(!listener_ && !server_thread_.joinable()); + listener_ = std::make_unique<SessionAwareTcpServer>(std::nullopt, port_, core::logging::LoggerFactory<utils::net::Server>().getLogger()); + server_thread_ = std::thread([this]() { listener_->run(); }); + } + + void startSSLServer() { + gsl_Expects(!listener_ && !server_thread_.joinable()); + listener_ = std::make_unique<SessionAwareSslServer>(std::nullopt, + port_, + core::logging::LoggerFactory<utils::net::Server>().getLogger(), + createSslDataForServer(), + utils::net::SslServer::ClientAuthOption::REQUIRED); + server_thread_ = std::thread([this]() { listener_->run(); }); + } + + void stopServer() { + if (listener_) + listener_->stop(); + if (server_thread_.joinable()) + server_thread_.join(); + listener_.reset(); + } + + size_t getNumberOfActiveSessions() { + if (auto session_aware_listener = dynamic_cast<ISessionAwareServer*>(listener_.get())) { + return session_aware_listener->getNumberOfSessions() - 1; // There is always one inactive session waiting for a new connection + } + return -1; + } + + void closeActiveConnections() { + if (auto session_aware_listener = dynamic_cast<ISessionAwareServer*>(listener_.get())) { + session_aware_listener->closeSessions(); + } + std::this_thread::sleep_for(200ms); + } + + auto trigger(const std::string_view& message) { + return controller_.trigger(message); + } + + auto getContent(const auto& flow_file) { + return controller_.plan->getContent(flow_file); + } + + std::optional<utils::net::Message> tryDequeueReceivedMessage() { + auto timeout = 200ms; + auto interval = 10ms; + + auto start_time = std::chrono::system_clock::now(); + utils::net::Message result; + while (start_time + timeout > std::chrono::system_clock::now()) { + if (listener_->tryDequeue(result)) + return result; + std::this_thread::sleep_for(interval); + } + return std::nullopt; + } + + void addSSLContextToPutTCP(const std::filesystem::path& ca_cert, std::optional<std::filesystem::path> client_cert_key) { Review Comment: good idea changed it in https://github.com/apache/nifi-minifi-cpp/pull/1419/commits/9928eaa75f607d1d337d289c7bbf84f23e20ee5a#diff-bbb56b112b398a6450c23e71e5dbbfb17bf20dad3d2fb908a1c6de26118f9ee8R191 -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
