Skip to content

Commit

Permalink
fix(agent): fix tests after latest changes in messages format
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasTurina committed Nov 6, 2024
1 parent 3faf5bb commit 62c568d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
17 changes: 9 additions & 8 deletions src/agent/communicator/tests/communicator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -86,12 +86,13 @@ TEST(CommunicatorTest, WaitForTokenExpirationAndAuthenticate_FailedAuthenticatio
testing::Mock::AllowLeak(mockHttpClientPtr);

auto communicatorPtr =
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr);
std::make_shared<communicator::Communicator>(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<std::string>
{
Expand Down Expand Up @@ -144,10 +145,10 @@ TEST(CommunicatorTest, StatelessMessageProcessingTask_CallsWithValidToken)
testing::Mock::AllowLeak(mockHttpClientPtr);

auto communicatorPtr =
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr);
std::make_shared<communicator::Communicator>(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(_, _, _, _, _, _, _))
Expand Down Expand Up @@ -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<communicator::Communicator>(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr);
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr);

std::string groupName = "group1";
std::string dstFilePath = "/path/to/file";
Expand All @@ -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<communicator::Communicator>(std::move(mockHttpClient), "Wazuh 5.0.0", "uuid", "key", nullptr);
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "uuid", "key", nullptr, nullptr);

std::string groupName = "group1";
std::string dstFilePath = "/path/to/file";
Expand Down
34 changes: 17 additions & 17 deletions src/agent/communicator/tests/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<std::string>("token"),
reqParams,
Expand Down Expand Up @@ -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<std::string>("token"),
reqParams,
getMessages,
Expand Down Expand Up @@ -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<std::string>("token"),
reqParams,
getMessages,
Expand Down Expand Up @@ -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<std::string>("token"),
reqParams,
getMessages,
Expand Down Expand Up @@ -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<std::string>("token"),
reqParams,
getMessages,
Expand Down Expand Up @@ -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());

Expand All @@ -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());
}
Expand All @@ -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());

Expand All @@ -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());
}
Expand All @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/agent/communicator/tests/mocks/mock_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class MockHttpClient : public http_client::IHttpClient

MOCK_METHOD(std::optional<std::string>,
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<std::string>,
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<boost::beast::http::dynamic_body>,
Expand Down
20 changes: 10 additions & 10 deletions src/agent/tests/agent_registration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class MockHttpClient : public http_client::IHttpClient

MOCK_METHOD(std::optional<std::string>,
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<std::string>,
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<boost::beast::http::dynamic_body>,
Expand All @@ -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<boost::beast::http::dynamic_body> expectedResponse;
expectedResponse.result(boost::beast::http::status::ok);
Expand All @@ -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)
Expand All @@ -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<boost::beast::http::dynamic_body> expectedResponse;
Expand All @@ -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<boost::beast::http::dynamic_body> expectedResponse;
expectedResponse.result(boost::beast::http::status::ok);
Expand Down
4 changes: 3 additions & 1 deletion src/agent/tests/message_queue_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");

Expand Down

0 comments on commit 62c568d

Please sign in to comment.