Skip to content

Commit

Permalink
fix: temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Dec 20, 2024
1 parent 5592d69 commit 2978307
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 70 deletions.
33 changes: 9 additions & 24 deletions src/agent/communicator/include/https_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,37 @@ namespace http_client
/// @brief Connects the socket to the given endpoints
/// @param endpoints The endpoints to connect to
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the connection
void Connect(boost::asio::io_context& ioContext,
const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::system::error_code& ec) override;

/// @brief Asynchronous version of Connect
/// @param endpoints The endpoints to connect to
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the connection
boost::asio::awaitable<void>
AsyncConnect(const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::asio::awaitable<void> AsyncConnect(const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec) override;

/// @brief Writes the given request to the socket
/// @param ioContext The io context associated to the socket
/// @param req The request to write
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the write
void Write(boost::asio::io_context& ioContext,
const boost::beast::http::request<boost::beast::http::string_body>& req,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::system::error_code& ec) override;

/// @brief Asynchronous version of Write
/// @param req The request to write
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the write
boost::asio::awaitable<void>
AsyncWrite(const boost::beast::http::request<boost::beast::http::string_body>& req,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::asio::awaitable<void> AsyncWrite(const boost::beast::http::request<boost::beast::http::string_body>& req,
boost::system::error_code& ec) override;

/// @brief Reads a response from the socket
/// @param ioContext The io context associated to the socket
/// @param res The response
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the read
void Read(boost::asio::io_context& ioContext,
boost::beast::http::response<boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::system::error_code& ec) override;

/// @brief Reads a response from the socket and writes it to a file
/// @param res The response to read
Expand All @@ -76,11 +64,8 @@ namespace http_client
/// @brief Asynchronous version of Read
/// @param res The response to read
/// @param ec The error code, if any occurred
/// @param timeOut The timeout for the read
boost::asio::awaitable<void>
AsyncRead(boost::beast::http::response<boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec,
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override;
boost::asio::awaitable<void> AsyncRead(boost::beast::http::response<boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec) override;

/// @brief Closes the socket
void Close() override;
Expand Down
30 changes: 12 additions & 18 deletions src/agent/communicator/src/https_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace http_client

void HttpsSocket::Connect(boost::asio::io_context& ioContext,
const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
const std::chrono::seconds timeOut)
boost::system::error_code& ec)
{
try
{
Expand All @@ -41,7 +40,7 @@ namespace http_client
}
});

ioContext.run_for(timeOut);
ioContext.run_for(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));

if (connectionSuccess)
{
Expand All @@ -64,13 +63,12 @@ namespace http_client
boost::asio::awaitable<void> HttpsSocket::AsyncConnect(
const boost::asio::ip::tcp::resolver:: // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
results_type& endpoints,
boost::system::error_code& ec, // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
const std::chrono::seconds timeOut)
boost::system::error_code& ec) // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
{
using namespace boost::asio::experimental::awaitable_operators;

auto timer = std::make_shared<boost::asio::steady_timer>(co_await boost::asio::this_coro::executor);
timer->expires_after(timeOut);
timer->expires_after(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));

auto result = std::make_shared<boost::system::error_code>();
auto taskCompleted = std::make_shared<bool>(false);
Expand Down Expand Up @@ -104,8 +102,7 @@ namespace http_client

void HttpsSocket::Write(boost::asio::io_context& ioContext,
const boost::beast::http::request<boost::beast::http::string_body>& req,
boost::system::error_code& ec,
const std::chrono::seconds timeOut)
boost::system::error_code& ec)
{
ioContext.reset();
try
Expand All @@ -126,7 +123,7 @@ namespace http_client
}
});

ioContext.run_for(timeOut);
ioContext.run_for(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));
if (!writeSuccess)
{
ec = boost::asio::error::timed_out;
Expand All @@ -142,13 +139,12 @@ namespace http_client
boost::asio::awaitable<void> HttpsSocket::AsyncWrite(
const boost::beast::http::request< // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
boost::beast::http::string_body>& req,
boost::system::error_code& ec, // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
const std::chrono::seconds timeOut)
boost::system::error_code& ec) // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
{
using namespace boost::asio::experimental::awaitable_operators;

auto timer = std::make_shared<boost::asio::steady_timer>(co_await boost::asio::this_coro::executor);
timer->expires_after(timeOut);
timer->expires_after(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));

auto result = std::make_shared<boost::system::error_code>();
auto taskCompleted = std::make_shared<bool>(false);
Expand All @@ -171,8 +167,7 @@ namespace http_client

void HttpsSocket::Read(boost::asio::io_context& ioContext,
boost::beast::http::response<boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec,
const std::chrono::seconds timeOut)
boost::system::error_code& ec)
{
ioContext.reset();
try
Expand All @@ -196,7 +191,7 @@ namespace http_client
}
});

ioContext.run_for(timeOut);
ioContext.run_for(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));
if (!readSuccess)
{
ec = boost::asio::error::timed_out;
Expand Down Expand Up @@ -225,13 +220,12 @@ namespace http_client
boost::asio::awaitable<void> HttpsSocket::AsyncRead(
boost::beast::http::response< // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec, // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
const std::chrono::seconds timeOut)
boost::system::error_code& ec) // NOLINT(cppcoreguidelines-avoid-reference-coroutine-parameters)
{
using namespace boost::asio::experimental::awaitable_operators;

auto timer = std::make_shared<boost::asio::steady_timer>(co_await boost::asio::this_coro::executor);
timer->expires_after(timeOut);
timer->expires_after(std::chrono::seconds(http_client_utils::TIMEOUT_SECONDS));

auto result = std::make_shared<boost::system::error_code>();
auto taskCompleted = std::make_shared<bool>(false);
Expand Down
51 changes: 26 additions & 25 deletions src/agent/communicator/tests/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ TEST_F(HttpClientTest, PerformHttpRequest_Success)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _)).WillOnce([](auto& res, auto&) { res.result(boost::beast::http::status::ok); });
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce([](auto&, auto& res, auto&) { res.result(boost::beast::http::status::ok); });

const http_client::HttpRequestParams params(
boost::beast::http::verb::get, "https://localhost:80", "/", "Wazuh 5.0.0");
Expand Down Expand Up @@ -482,11 +483,11 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Success)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce(
[](auto& res, auto&)
[](auto&, auto& res, auto&)
{
res.result(boost::beast::http::status::ok);
boost::beast::ostream(res.body()) << R"({"token":"valid_token"})";
Expand All @@ -507,11 +508,11 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Failure)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce(
[](auto& res, auto&)
[](auto&, auto& res, auto&)
{
res.result(boost::beast::http::status::unauthorized);
boost::beast::ostream(res.body()) << R"({"message":"Try again"})";
Expand All @@ -529,11 +530,11 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_FailureThrowsException)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce(
[](auto& res, auto&)
[](auto&, auto& res, auto&)
{
res.result(boost::beast::http::status::unauthorized);
boost::beast::ostream(res.body()) << R"({"message":"Invalid key"})";
Expand All @@ -549,11 +550,11 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Success)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce(
[](auto& res, auto&)
[](auto&, auto& res, auto&)
{
res.result(boost::beast::http::status::ok);
boost::beast::ostream(res.body()) << R"({"data":{"token":"valid_token"}})";
Expand All @@ -574,10 +575,10 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Failure)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce([](auto& res, auto&) { res.result(boost::beast::http::status::unauthorized); });
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _, _))
.WillOnce([](auto&, auto& res, auto&) { res.result(boost::beast::http::status::unauthorized); });

const auto token =
client->AuthenticateWithUserPassword("https://localhost:8080", "Wazuh 5.0.0", "user", "password");
Expand All @@ -591,8 +592,8 @@ TEST_F(HttpClientTest, PerformHttpRequestDownload_Success)
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Connect(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _, _)).Times(1);
EXPECT_CALL(*mockSocket, ReadToFile(_, _))
.WillOnce([](boost::beast::http::response<boost::beast::http::dynamic_body>& res,
[[maybe_unused]] auto& dstFilePath) { res.result(boost::beast::http::status::ok); });
Expand Down
11 changes: 8 additions & 3 deletions src/agent/communicator/tests/mocks/mock_http_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class MockHttpSocket : public http_client::IHttpSocket
public:
MOCK_METHOD(void,
Connect,
(const boost::asio::ip::tcp::resolver::results_type& endpoints, boost::system::error_code& code),
(boost::asio::io_context & io_context,
const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& code),
(override));

MOCK_METHOD(boost::asio::awaitable<void>,
Expand All @@ -17,7 +19,8 @@ class MockHttpSocket : public http_client::IHttpSocket

MOCK_METHOD(void,
Write,
(const boost::beast::http::request<boost::beast::http::string_body>& req,
(boost::asio::io_context & io_context,
const boost::beast::http::request<boost::beast::http::string_body>& req,
boost::system::error_code& ec),
(override));

Expand All @@ -29,7 +32,9 @@ class MockHttpSocket : public http_client::IHttpSocket

MOCK_METHOD(void,
Read,
(boost::beast::http::response<boost::beast::http::dynamic_body> & res, boost::system::error_code& ec),
(boost::asio::io_context & io_context,
boost::beast::http::response<boost::beast::http::dynamic_body>& res,
boost::system::error_code& ec),
(override));

MOCK_METHOD(void,
Expand Down

0 comments on commit 2978307

Please sign in to comment.