From 236251532465915071a90cc557a16404cf00c151 Mon Sep 17 00:00:00 2001 From: Ariel Martin Date: Wed, 18 Dec 2024 23:53:59 -0300 Subject: [PATCH] test: adds HttpSocket tests --- src/agent/communicator/tests/CMakeLists.txt | 5 + src/agent/communicator/tests/socket_test.cpp | 96 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/agent/communicator/tests/socket_test.cpp diff --git a/src/agent/communicator/tests/CMakeLists.txt b/src/agent/communicator/tests/CMakeLists.txt index 81604c072c..7182f00a81 100644 --- a/src/agent/communicator/tests/CMakeLists.txt +++ b/src/agent/communicator/tests/CMakeLists.txt @@ -11,3 +11,8 @@ add_executable(http_client_test http_client_test.cpp) configure_target(http_client_test) target_link_libraries(http_client_test PUBLIC Communicator GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) add_test(NAME HttpClientTest COMMAND http_client_test) + +add_executable(socket_test socket_test.cpp) +configure_target(socket_test) +target_link_libraries(socket_test PUBLIC Communicator GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) +add_test(NAME SocketTest COMMAND socket_test) diff --git a/src/agent/communicator/tests/socket_test.cpp b/src/agent/communicator/tests/socket_test.cpp new file mode 100644 index 0000000000..93d9a40622 --- /dev/null +++ b/src/agent/communicator/tests/socket_test.cpp @@ -0,0 +1,96 @@ +#include + +#include +#include +#include +#include + +#include +#include +#include + +class TestableHttpSocket : public http_client::HttpSocket +{ +public: + explicit TestableHttpSocket(const boost::asio::any_io_executor& ioContext) + : HttpSocket(ioContext) + { + } + + void CallAsyncConnect([[maybe_unused]] const boost::asio::ip::tcp::resolver::results_type& endpoints, + boost::system::error_code& ec, + bool& connectionSuccess) override + { + if (!m_nDelay) + { + ec.clear(); + connectionSuccess = true; + } + else + { + ec = boost::asio::error::timed_out; + } + } + + long m_nDelay {}; +}; + +// Test fixture +class HttpSocketTest : public ::testing::Test +{ +protected: + void SetUp() override + { + m_socket = std::make_unique(m_ioContext.get_executor()); + } + + void TearDown() override {} + + std::unique_ptr m_socket; + boost::asio::io_context m_ioContext; + + // Helper method to create sample endpoints + boost::asio::ip::tcp::resolver::results_type CreateMockEndpoints() + { + boost::asio::ip::tcp::resolver resolver(m_ioContext.get_executor()); + return resolver.resolve("example.com", "80"); + } +}; + +// Test Connect method +TEST_F(HttpSocketTest, ConnectSuccessful) +{ + auto endpoints = std::make_shared(CreateMockEndpoints()); + auto ec = std::make_shared(); + + std::cout << "Attempting to connect\n"; + + m_socket->m_nDelay = 0; + m_socket->Connect(m_ioContext, *endpoints, *ec, std::chrono::seconds(1)); + + std::cout << "Connection returned: " << ec->message() << " (" << ec->value() << ")\n"; + + EXPECT_EQ(ec->value(), 0) << "Connection should succeed"; +} + +TEST_F(HttpSocketTest, ConnectTimeout) +{ + auto endpoints = std::make_shared(CreateMockEndpoints()); + auto ec = std::make_shared(); + + std::cout << "Attempting to connect\n"; + + m_socket->m_nDelay = 3; + m_socket->Connect(m_ioContext, *endpoints, *ec, std::chrono::seconds(1)); + + std::cout << "Connection returned: " << ec->message() << " (" << ec->value() << ")\n"; + + EXPECT_NE(ec->value(), 0) << "Connection should fail"; +} + +// Main function +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}