Skip to content

Commit

Permalink
test(httpClient): a check has been added to verify the call to SetVer…
Browse files Browse the repository at this point in the history
…ificationMode.
  • Loading branch information
Nicogp committed Dec 26, 2024
1 parent 76efee1 commit 16d5f60
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/agent/communicator/include/http_request_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace http_client
/// @param serverUrl The server URL for the request
/// @param endpoint The endpoint for the request
/// @param userAgent The user agent property for the request header
/// @param verificationMode The verification mode for the request
/// @param token Optional token for authorization
/// @param userPass Optional user credentials for basic authentication
/// @param body Optional body for the request
Expand Down
50 changes: 49 additions & 1 deletion src/agent/communicator/tests/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ TEST(CreateHttpRequestTest, AuthorizationBasic)
EXPECT_EQ(req[boost::beast::http::field::authorization], "Basic username:password");
}

TEST_F(HttpClientTest, PerformHttpRequest_Success)
TEST_F(HttpClientTest, PerformHttpRequest_Success_verification_full)
{
SetupMockResolverFactory();
SetupMockSocketFactory();

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

Expand All @@ -191,6 +192,42 @@ TEST_F(HttpClientTest, PerformHttpRequest_Success)
EXPECT_EQ(response.result(), boost::beast::http::status::ok);
}

TEST_F(HttpClientTest, PerformHttpRequest_Success_verification_certificate)
{
SetupMockResolverFactory();
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "certificate")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _)).WillOnce([](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", "certificate");
const auto response = client->PerformHttpRequest(params);

EXPECT_EQ(response.result(), boost::beast::http::status::ok);
}

TEST_F(HttpClientTest, PerformHttpRequest_Success_verification_none)
{
SetupMockResolverFactory();
SetupMockSocketFactory();

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "none")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _)).WillOnce([](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", "none");
const auto response = client->PerformHttpRequest(params);

EXPECT_EQ(response.result(), boost::beast::http::status::ok);
}

TEST_F(HttpClientTest, PerformHttpRequest_ExceptionThrown)
{
SetupMockResolverFactory();
Expand All @@ -212,6 +249,7 @@ TEST_P(HttpClientTest, Co_PerformHttpRequest_Success)
SetupMockSocketFactory();
SetupMockResolverExpectations();
SetupMockSocketConnectExpectations();
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
SetupMockSocketWriteExpectations();
SetupMockSocketReadExpectations(GetParam());

Expand Down Expand Up @@ -274,6 +312,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_CallbacksNotCalledIfCannotConnect)
SetupMockSocketFactory();
SetupMockResolverExpectations();
SetupMockSocketConnectExpectations(boost::system::errc::make_error_code(boost::system::errc::bad_address));
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);

auto getMessagesCalled = false;
auto getMessages = [&getMessagesCalled](const size_t) -> boost::asio::awaitable<std::tuple<int, std::string>>
Expand Down Expand Up @@ -320,6 +359,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_OnSuccessNotCalledIfAsyncWriteFails
SetupMockSocketFactory();
SetupMockResolverExpectations();
SetupMockSocketConnectExpectations(boost::system::errc::make_error_code(boost::system::errc::success));
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
SetupMockSocketWriteExpectations(boost::system::errc::make_error_code(boost::system::errc::bad_address));

auto getMessagesCalled = false;
Expand Down Expand Up @@ -373,6 +413,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_OnSuccessNotCalledIfAsyncReadFails)
SetupMockSocketFactory();
SetupMockResolverExpectations();
SetupMockSocketConnectExpectations(boost::system::errc::make_error_code(boost::system::errc::success));
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
SetupMockSocketWriteExpectations();
SetupMockSocketReadExpectations(boost::beast::http::status::not_found,
boost::system::errc::make_error_code(boost::system::errc::bad_address));
Expand Down Expand Up @@ -428,6 +469,7 @@ TEST_F(HttpClientTest, Co_PerformHttpRequest_UnauthorizedCalledWhenAuthorization
SetupMockSocketFactory();
SetupMockResolverExpectations();
SetupMockSocketConnectExpectations(boost::system::errc::make_error_code(boost::system::errc::success));
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
SetupMockSocketWriteExpectations();
SetupMockSocketReadExpectations(boost::beast::http::status::unauthorized);

Expand Down Expand Up @@ -483,6 +525,7 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Success)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce(
Expand All @@ -508,6 +551,7 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_Failure)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce(
Expand All @@ -530,6 +574,7 @@ TEST_F(HttpClientTest, AuthenticateWithUuidAndKey_FailureThrowsException)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce(
Expand All @@ -551,6 +596,7 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Success)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce(
Expand All @@ -576,6 +622,7 @@ TEST_F(HttpClientTest, AuthenticateWithUserPassword_Failure)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, Read(_, _))
.WillOnce([](auto& res, auto&) { res.result(boost::beast::http::status::unauthorized); });
Expand All @@ -593,6 +640,7 @@ TEST_F(HttpClientTest, PerformHttpRequestDownload_Success)

EXPECT_CALL(*mockResolver, Resolve(_, _)).WillOnce(Return(dummyResults));
EXPECT_CALL(*mockSocket, Connect(_, _)).Times(1);
EXPECT_CALL(*mockSocket, SetVerificationMode("localhost", "full")).Times(1);
EXPECT_CALL(*mockSocket, Write(_, _)).Times(1);
EXPECT_CALL(*mockSocket, ReadToFile(_, _))
.WillOnce([](boost::beast::http::response<boost::beast::http::dynamic_body>& res,
Expand Down

0 comments on commit 16d5f60

Please sign in to comment.