From 62c568d0c6368f1c6920c2372a7a6271333c2273 Mon Sep 17 00:00:00 2001 From: Tomas Turina Date: Wed, 6 Nov 2024 20:13:29 +0000 Subject: [PATCH] fix(agent): fix tests after latest changes in messages format --- .../communicator/tests/communicator_test.cpp | 17 +++++----- .../communicator/tests/http_client_test.cpp | 34 +++++++++---------- .../tests/mocks/mock_http_client.hpp | 4 +-- src/agent/tests/agent_registration_test.cpp | 20 +++++------ src/agent/tests/message_queue_utils_test.cpp | 4 ++- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/agent/communicator/tests/communicator_test.cpp b/src/agent/communicator/tests/communicator_test.cpp index 12f916471a..d91ad80356 100644 --- a/src/agent/communicator/tests/communicator_test.cpp +++ b/src/agent/communicator/tests/communicator_test.cpp @@ -37,7 +37,7 @@ namespace TEST(CommunicatorTest, CommunicatorConstructor) { - EXPECT_NO_THROW(communicator::Communicator communicator(nullptr, "Wazuh 5.0.0", "uuid", "key", nullptr)); + EXPECT_NO_THROW(communicator::Communicator communicator(nullptr, "uuid", "key", nullptr, nullptr)); } TEST(CommunicatorTest, StatefulMessageProcessingTask_Success) @@ -69,7 +69,7 @@ TEST(CommunicatorTest, StatefulMessageProcessingTask_Success) co_return; })); - communicator::Communicator communicator(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr); + communicator::Communicator communicator(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr); auto task = communicator.StatefulMessageProcessingTask(getMessages, onSuccess); boost::asio::io_context ioContext; @@ -86,12 +86,13 @@ TEST(CommunicatorTest, WaitForTokenExpirationAndAuthenticate_FailedAuthenticatio testing::Mock::AllowLeak(mockHttpClientPtr); auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr); + std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr); // A failed authentication won't return a token - EXPECT_CALL(*mockHttpClientPtr, AuthenticateWithUuidAndKey(_, _, _)) + EXPECT_CALL(*mockHttpClientPtr, AuthenticateWithUuidAndKey(_, _, _, _)) .WillOnce(Invoke( [communicatorPtr]([[maybe_unused]] const std::string& host, + [[maybe_unused]] const std::string& userAgent, [[maybe_unused]] const std::string& uuid, [[maybe_unused]] const std::string& key) -> std::optional { @@ -144,10 +145,10 @@ TEST(CommunicatorTest, StatelessMessageProcessingTask_CallsWithValidToken) testing::Mock::AllowLeak(mockHttpClientPtr); auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr); + std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr); const auto mockedToken = CreateToken(); - EXPECT_CALL(*mockHttpClientPtr, AuthenticateWithUuidAndKey(_, _, _)).WillOnce(Return(mockedToken)); + EXPECT_CALL(*mockHttpClientPtr, AuthenticateWithUuidAndKey(_, _, _, _)).WillOnce(Return(mockedToken)); std::string capturedToken; EXPECT_CALL(*mockHttpClientPtr, Co_PerformHttpRequest(_, _, _, _, _, _, _)) @@ -196,7 +197,7 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Success) // not really a leak, as its lifetime is managed by the Communicator testing::Mock::AllowLeak(mockHttpClientPtr); auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr); + std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr); std::string groupName = "group1"; std::string dstFilePath = "/path/to/file"; @@ -217,7 +218,7 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Error) // not really a leak, as its lifetime is managed by the Communicator testing::Mock::AllowLeak(mockHttpClientPtr); auto communicatorPtr = - std::make_shared(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr); + std::make_shared(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr); std::string groupName = "group1"; std::string dstFilePath = "/path/to/file"; diff --git a/src/agent/communicator/tests/http_client_test.cpp b/src/agent/communicator/tests/http_client_test.cpp index 425c40f8d9..2b0da10fae 100644 --- a/src/agent/communicator/tests/http_client_test.cpp +++ b/src/agent/communicator/tests/http_client_test.cpp @@ -109,7 +109,7 @@ class HttpClientTest : public Test TEST(CreateHttpRequestTest, BasicGetRequest) { auto httpClient = http_client::HttpClient(); - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost", "/test"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost", "/test", "Wazuh 5.0.0"); const auto req = httpClient.CreateHttpRequest(reqParams); EXPECT_EQ(req.method(), boost::beast::http::verb::get); @@ -124,7 +124,7 @@ TEST(CreateHttpRequestTest, PostRequestWithBody) auto httpClient = http_client::HttpClient(); const std::string body = R"({"key": "value"})"; const auto reqParams = http_client::HttpRequestParams( - boost::beast::http::verb::post, "https://localhost:8080", "/submit", "", "", "Wazuh 5.0.0", body); + boost::beast::http::verb::post, "https://localhost:8080", "/submit", "Wazuh 5.0.0", "", "", body); const auto req = httpClient.CreateHttpRequest(reqParams); EXPECT_EQ(req.method(), boost::beast::http::verb::post); @@ -141,7 +141,7 @@ TEST(CreateHttpRequestTest, AuthorizationBearerToken) { auto httpClient = http_client::HttpClient(); const std::string token = "dummy_token"; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "localhost", "/secure", token); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "localhost", "/secure", "Wazuh 5.0.0", token); const auto req = httpClient.CreateHttpRequest(reqParams); EXPECT_EQ(req[boost::beast::http::field::authorization], "Bearer dummy_token"); @@ -152,7 +152,7 @@ TEST(CreateHttpRequestTest, AuthorizationBasic) auto httpClient = http_client::HttpClient(); const std::string user_pass = "username:password"; const auto reqParams = http_client::HttpRequestParams( - boost::beast::http::verb::get, "https://localhost:8080", "/secure", "", user_pass); + boost::beast::http::verb::get, "https://localhost:8080", "/secure", "Wazuh 5.0.0", "", user_pass); const auto req = httpClient.CreateHttpRequest(reqParams); EXPECT_EQ(req[boost::beast::http::field::authorization], "Basic username:password"); @@ -168,7 +168,7 @@ TEST_F(HttpClientTest, PerformHttpRequest_Success) EXPECT_CALL(*mockSocket, Write(_)).Times(1); EXPECT_CALL(*mockSocket, Read(_)).WillOnce([](auto& res) { res.result(boost::beast::http::status::ok); }); - const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/"); + const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/", "Wazuh 5.0.0"); const auto response = client->PerformHttpRequest(params); EXPECT_EQ(response.result(), boost::beast::http::status::ok); @@ -180,7 +180,7 @@ TEST_F(HttpClientTest, PerformHttpRequest_ExceptionThrown) EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Throw(std::runtime_error("Simulated resolution failure"))); - const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/"); + const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/", "Wazuh 5.0.0"); const auto response = client->PerformHttpRequest(params); EXPECT_EQ(response.result(), boost::beast::http::status::internal_server_error); @@ -217,7 +217,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_Success) unauthorizedCalled = true; }; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/", "Wazuh 5.0.0"); auto task = client->Co_PerformHttpRequest(std::make_shared("token"), reqParams, @@ -263,7 +263,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_CallbacksNotCalledIfCannotConnect) unauthorizedCalled = true; }; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/", "Wazuh 5.0.0"); auto task = client->Co_PerformHttpRequest(std::make_shared("token"), reqParams, getMessages, @@ -309,7 +309,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_OnSuccessNotCalledIfAsyncWriteFails unauthorizedCalled = true; }; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/", "Wazuh 5.0.0"); auto task = client->Co_PerformHttpRequest(std::make_shared("token"), reqParams, getMessages, @@ -357,7 +357,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_OnSuccessNotCalledIfAsyncReadFails) unauthorizedCalled = true; }; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/", "Wazuh 5.0.0"); auto task = client->Co_PerformHttpRequest(std::make_shared("token"), reqParams, getMessages, @@ -404,7 +404,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_UnauthorizedCalledWhenAuthorization unauthorizedCalled = true; }; - const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/"); + const auto reqParams = http_client::HttpRequestParams(boost::beast::http::verb::get, "https://localhost:8080", "/", "Wazuh 5.0.0"); auto task = client->Co_PerformHttpRequest(std::make_shared("token"), reqParams, getMessages, @@ -438,7 +438,7 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Success) boost::beast::ostream(res.body()) << R"({"token":"valid_token"})"; }); - const auto token = client->AuthenticateWithUuidAndKey("https://localhost:8080", "test-uuid", "test-key"); + const auto token = client->AuthenticateWithUuidAndKey("https://localhost:8080", "Wazuh 5.0.0", "test-uuid", "test-key"); ASSERT_TRUE(token.has_value()); @@ -456,7 +456,7 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Failure) EXPECT_CALL(*mockSocket, Write(_)).Times(1); EXPECT_CALL(*mockSocket, Read(_)).WillOnce([](auto& res) { res.result(boost::beast::http::status::unauthorized); }); - const auto token = client->AuthenticateWithUuidAndKey("https://localhost:8080", "test-uuid", "test-key"); + const auto token = client->AuthenticateWithUuidAndKey("https://localhost:8080", "Wazuh 5.0.0", "test-uuid", "test-key"); EXPECT_FALSE(token.has_value()); } @@ -477,7 +477,7 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Success) boost::beast::ostream(res.body()) << R"({"data":{"token":"valid_token"}})"; }); - const auto token = client->AuthenticateWithUserPassword("https://localhost:8080", "user", "password"); + const auto token = client->AuthenticateWithUserPassword("https://localhost:8080", "Wazuh 5.0.0", "user", "password"); ASSERT_TRUE(token.has_value()); @@ -495,7 +495,7 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Failure) EXPECT_CALL(*mockSocket, Write(_)).Times(1); EXPECT_CALL(*mockSocket, Read(_)).WillOnce([](auto& res) { res.result(boost::beast::http::status::unauthorized); }); - const auto token = client->AuthenticateWithUserPassword("https://localhost:8080", "user", "password"); + const auto token = client->AuthenticateWithUserPassword("https://localhost:8080", "Wazuh 5.0.0", "user", "password"); EXPECT_FALSE(token.has_value()); } @@ -512,7 +512,7 @@ TEST_F(HttpClientTest, PerformHttpRequestDownload_Success) .WillOnce([](auto& res, [[maybe_unused]] auto& dstFilePath) { res.get().result(boost::beast::http::status::ok); }); - const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/"); + const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/", "Wazuh 5.0.0"); const std::string dstFilePath = "dstFilePath"; const auto response = client->PerformHttpRequestDownload(params, dstFilePath); @@ -525,7 +525,7 @@ TEST_F(HttpClientTest, PerformHttpRequestDownload_ExceptionThrown) EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Throw(std::runtime_error("Simulated resolution failure"))); - const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/"); + const http_client::HttpRequestParams params(boost::beast::http::verb::get, "https://localhost:80", "/", "Wazuh 5.0.0"); const std::string dstFilePath = "dstFilePath"; const auto response = client->PerformHttpRequestDownload(params, dstFilePath); diff --git a/src/agent/communicator/tests/mocks/mock_http_client.hpp b/src/agent/communicator/tests/mocks/mock_http_client.hpp index 49c55a660e..3958b5c07c 100644 --- a/src/agent/communicator/tests/mocks/mock_http_client.hpp +++ b/src/agent/communicator/tests/mocks/mock_http_client.hpp @@ -28,12 +28,12 @@ class MockHttpClient : public http_client::IHttpClient MOCK_METHOD(std::optional, AuthenticateWithUuidAndKey, - (const std::string& host, const std::string& uuid, const std::string& key), + (const std::string& host, const std::string& userAgent, const std::string& uuid, const std::string& key), (override)); MOCK_METHOD(std::optional, AuthenticateWithUserPassword, - (const std::string& host, const std::string& user, const std::string& password), + (const std::string& host, const std::string& userAgent, const std::string& user, const std::string& password), (override)); MOCK_METHOD(boost::beast::http::response, diff --git a/src/agent/tests/agent_registration_test.cpp b/src/agent/tests/agent_registration_test.cpp index bd983ef7f0..e74e0dc2e8 100644 --- a/src/agent/tests/agent_registration_test.cpp +++ b/src/agent/tests/agent_registration_test.cpp @@ -40,12 +40,12 @@ class MockHttpClient : public http_client::IHttpClient MOCK_METHOD(std::optional, AuthenticateWithUuidAndKey, - (const std::string& host, const std::string& uuid, const std::string& key), + (const std::string& host, const std::string& userAgent, const std::string& uuid, const std::string& key), (override)); MOCK_METHOD(std::optional, AuthenticateWithUserPassword, - (const std::string& host, const std::string& user, const std::string& password), + (const std::string& host, const std::string& userAgent, const std::string& user, const std::string& password), (override)); MOCK_METHOD(boost::beast::http::response, @@ -71,13 +71,13 @@ TEST_F(RegisterTest, RegistrationTestSuccess) MockHttpClient mockHttpClient; - EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_)) + EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return("token")); - nlohmann::json bodyJson = {{"id", agent->GetUUID()}, {"key", agent->GetKey()}}; + nlohmann::json bodyJson = agent->GetMetadataInfo(true); http_client::HttpRequestParams reqParams( - boost::beast::http::verb::post, "https://localhost:55000", "/agents", "token", "", agent->GetHeaderInfo(), bodyJson.dump()); + boost::beast::http::verb::post, "https://localhost:55000", "/agents", agent->GetHeaderInfo(), "token", "", bodyJson.dump()); boost::beast::http::response expectedResponse; expectedResponse.result(boost::beast::http::status::ok); @@ -97,7 +97,7 @@ TEST_F(RegisterTest, RegistrationFailsIfAuthenticationFails) MockHttpClient mockHttpClient; - EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, "user", "password")) + EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, "user", "password")) .WillOnce(testing::Return(std::nullopt)); // NOLINTNEXTLINE(cppcoreguidelines-init-variables) @@ -113,7 +113,7 @@ TEST_F(RegisterTest, RegistrationFailsIfServerResponseIsNotOk) MockHttpClient mockHttpClient; - EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, "user", "password")) + EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, "user", "password")) .WillOnce(testing::Return("token")); boost::beast::http::response expectedResponse; @@ -134,13 +134,13 @@ TEST_F(RegisterTest, RegistrationTestSuccessWithEmptyKey) MockHttpClient mockHttpClient; - EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_)) + EXPECT_CALL(mockHttpClient, AuthenticateWithUserPassword(testing::_, testing::_, testing::_, testing::_)) .WillOnce(testing::Return("token")); - nlohmann::json bodyJson = {{"id", agent->GetUUID()}, {"key", agent->GetKey()}}; + nlohmann::json bodyJson = agent->GetMetadataInfo(true); http_client::HttpRequestParams reqParams( - boost::beast::http::verb::post, "https://localhost:55000", "/agents", "token", "", agent->GetHeaderInfo(), bodyJson.dump()); + boost::beast::http::verb::post, "https://localhost:55000", "/agents", agent->GetHeaderInfo(), "token", "", bodyJson.dump()); boost::beast::http::response expectedResponse; expectedResponse.result(boost::beast::http::status::ok); diff --git a/src/agent/tests/message_queue_utils_test.cpp b/src/agent/tests/message_queue_utils_test.cpp index 2669ceb432..b7cfd24ebf 100644 --- a/src/agent/tests/message_queue_utils_test.cpp +++ b/src/agent/tests/message_queue_utils_test.cpp @@ -50,7 +50,7 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueTest) io_context.restart(); auto result = boost::asio::co_spawn( - io_context, GetMessagesFromQueue(mockQueue, MessageType::STATEFUL), boost::asio::use_future); + io_context, GetMessagesFromQueue(mockQueue, MessageType::STATEFUL, nullptr), boost::asio::use_future); const auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(1); io_context.run_until(timeout); @@ -60,6 +60,8 @@ TEST_F(MessageQueueUtilsTest, GetMessagesFromQueueTest) const auto jsonResult = result.get(); nlohmann::json expectedJson; + expectedJson["agent"] = "{}"_json; + expectedJson["agent"] = nlohmann::json::object(); expectedJson["events"] = nlohmann::json::array(); expectedJson["events"].push_back("test_data");