Skip to content

Commit

Permalink
Merge pull request #617 from wazuh/enhancement/616-enable-misc-const-…
Browse files Browse the repository at this point in the history
…correctness-in-clang-tidy

Enable "misc-const-correctness" in clang tidy
  • Loading branch information
TomasTurina authored Feb 21, 2025
2 parents 7a3d279 + cfbb67c commit 8568b29
Show file tree
Hide file tree
Showing 70 changed files with 706 additions and 884 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ InsertNewlineAtEOF: 'true'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PointerAlignment: Left
QualifierAlignment: Left
SeparateDefinitionBlocks: 'Always'
SortIncludes: 'true'
SpaceAfterCStyleCast: 'false'
Expand Down
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Checks: 'clang-diagnostic-*,
google-explicit-constructor,
hicpp-multiway-paths-covered,
hicpp-exception-baseclass,
misc-const-correctness,
misc-unconventional-assign-operator,
misc-misplaced-const,
misc-new-delete-overloads,
Expand Down
4 changes: 2 additions & 2 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void AgentInfo::LoadEndpointInfo()
{
if (m_getOSInfo != nullptr)
{
nlohmann::json osInfo = m_getOSInfo();
const nlohmann::json osInfo = m_getOSInfo();
m_endpointInfo["hostname"] = osInfo.value("hostname", "Unknown");
m_endpointInfo["architecture"] = osInfo.value("architecture", "Unknown");
m_endpointInfo["os"] = nlohmann::json::object();
Expand All @@ -235,7 +235,7 @@ void AgentInfo::LoadEndpointInfo()

if (m_getNetworksInfo != nullptr)
{
nlohmann::json networksInfo = m_getNetworksInfo();
const nlohmann::json networksInfo = m_getNetworksInfo();
m_endpointInfo["ip"] = GetActiveIPAddresses(networksInfo);
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/agent/agent_info/tests/agent_info_persistance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST_F(AgentInfoPersistanceTest, TestConstruction)

TEST_F(AgentInfoPersistanceTest, TestGetNameValue)
{
std::vector<column::Row> mockRowName = {{column::ColumnValue("name", column::ColumnType::TEXT, "name_test")}};
const std::vector<column::Row> mockRowName = {{column::ColumnValue("name", column::ColumnType::TEXT, "name_test")}};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowName));
Expand All @@ -45,7 +45,7 @@ TEST_F(AgentInfoPersistanceTest, TestGetNameValue)

TEST_F(AgentInfoPersistanceTest, TestGetNameNotValue)
{
std::vector<column::Row> mockRowName = {};
const std::vector<column::Row> mockRowName = {};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowName));
Expand All @@ -62,7 +62,7 @@ TEST_F(AgentInfoPersistanceTest, TestGetNameCatch)

TEST_F(AgentInfoPersistanceTest, TestGetKeyValue)
{
std::vector<column::Row> mockRowKey = {{column::ColumnValue("key", column::ColumnType::TEXT, "key_test")}};
const std::vector<column::Row> mockRowKey = {{column::ColumnValue("key", column::ColumnType::TEXT, "key_test")}};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowKey));
Expand All @@ -71,7 +71,7 @@ TEST_F(AgentInfoPersistanceTest, TestGetKeyValue)

TEST_F(AgentInfoPersistanceTest, TestGetKeyNotValue)
{
std::vector<column::Row> mockRowKey = {};
const std::vector<column::Row> mockRowKey = {};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowKey));
Expand All @@ -88,7 +88,7 @@ TEST_F(AgentInfoPersistanceTest, TestGetKeyCatch)

TEST_F(AgentInfoPersistanceTest, TestGetUUIDValue)
{
std::vector<column::Row> mockRowUUID = {{column::ColumnValue("uuid", column::ColumnType::TEXT, "uuid_test")}};
const std::vector<column::Row> mockRowUUID = {{column::ColumnValue("uuid", column::ColumnType::TEXT, "uuid_test")}};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowUUID));
Expand All @@ -97,7 +97,7 @@ TEST_F(AgentInfoPersistanceTest, TestGetUUIDValue)

TEST_F(AgentInfoPersistanceTest, TestGetUUIDNotValue)
{
std::vector<column::Row> mockRowUUID = {};
const std::vector<column::Row> mockRowUUID = {};
EXPECT_CALL(*mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowUUID));
Expand All @@ -114,24 +114,24 @@ TEST_F(AgentInfoPersistanceTest, TestGetUUIDCatch)

TEST_F(AgentInfoPersistanceTest, TestGetGroupsValue)
{
std::vector<column::Row> mockRowGroups = {{column::ColumnValue("name", column::ColumnType::TEXT, "group_1")},
{column::ColumnValue("name", column::ColumnType::TEXT, "group_2")}};
const std::vector<column::Row> mockRowGroups = {{column::ColumnValue("name", column::ColumnType::TEXT, "group_1")},
{column::ColumnValue("name", column::ColumnType::TEXT, "group_2")}};
EXPECT_CALL(*mockPersistence,
Select("agent_group", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowGroups));

std::vector<std::string> expectedGroups = {"group_1", "group_2"};
const std::vector<std::string> expectedGroups = {"group_1", "group_2"};
EXPECT_EQ(agentInfoPersistance->GetGroups(), expectedGroups);
}

TEST_F(AgentInfoPersistanceTest, TestGetGroupsNotValue)
{
std::vector<column::Row> mockRowGroups = {};
const std::vector<column::Row> mockRowGroups = {};
EXPECT_CALL(*mockPersistence,
Select("agent_group", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Return(mockRowGroups));

std::vector<std::string> expectedGroups = {};
const std::vector<std::string> expectedGroups = {};
EXPECT_EQ(agentInfoPersistance->GetGroups(), expectedGroups);
}

Expand All @@ -141,14 +141,14 @@ TEST_F(AgentInfoPersistanceTest, TestGetGroupsCatch)
Select("agent_group", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.WillOnce(testing::Throw(std::runtime_error("Error Select")));

std::vector<std::string> expectedGroups = {};
const std::vector<std::string> expectedGroups = {};
EXPECT_EQ(agentInfoPersistance->GetGroups(), expectedGroups);
}

TEST_F(AgentInfoPersistanceTest, TestSetName)
{
std::string expectedColumn = "name";
std::string newName = "new_name";
const std::string expectedColumn = "name";
const std::string newName = "new_name";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand All @@ -164,8 +164,8 @@ TEST_F(AgentInfoPersistanceTest, TestSetName)

TEST_F(AgentInfoPersistanceTest, TestSetNameCatch)
{
std::string expectedColumn = "name";
std::string newName = "new_name";
const std::string expectedColumn = "name";
const std::string newName = "new_name";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand All @@ -181,8 +181,8 @@ TEST_F(AgentInfoPersistanceTest, TestSetNameCatch)

TEST_F(AgentInfoPersistanceTest, TestSetKey)
{
std::string expectedColumn = "key";
std::string newKey = "new_key";
const std::string expectedColumn = "key";
const std::string newKey = "new_key";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand All @@ -198,8 +198,8 @@ TEST_F(AgentInfoPersistanceTest, TestSetKey)

TEST_F(AgentInfoPersistanceTest, TestSetKeyCatch)
{
std::string expectedColumn = "key";
std::string newKey = "new_key";
const std::string expectedColumn = "key";
const std::string newKey = "new_key";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand All @@ -215,8 +215,8 @@ TEST_F(AgentInfoPersistanceTest, TestSetKeyCatch)

TEST_F(AgentInfoPersistanceTest, TestSetUUID)
{
std::string expectedColumn = "uuid";
std::string newUUID = "new_uuid";
const std::string expectedColumn = "uuid";
const std::string newUUID = "new_uuid";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand All @@ -232,8 +232,8 @@ TEST_F(AgentInfoPersistanceTest, TestSetUUID)

TEST_F(AgentInfoPersistanceTest, TestSetUUIDCatch)
{
std::string expectedColumn = "uuid";
std::string newUUID = "new_uuid";
const std::string expectedColumn = "uuid";
const std::string newUUID = "new_uuid";

EXPECT_CALL(*mockPersistence,
Update(testing::Eq("agent_info"),
Expand Down Expand Up @@ -301,7 +301,7 @@ TEST_F(AgentInfoPersistanceTest, TestSetGroupsInsertFails2)
EXPECT_CALL(*mockPersistence, BeginTransaction()).Times(1);
EXPECT_CALL(*mockPersistence, Remove("agent_group", testing::_, testing::_)).Times(1);

testing::Sequence seq;
const testing::Sequence seq;
EXPECT_CALL(*mockPersistence, Insert("agent_group", testing::_))
.InSequence(seq)
.WillOnce(testing::Return())
Expand Down
15 changes: 9 additions & 6 deletions src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ class AgentInfoTest : public ::testing::Test

void SetUpAgentInfoInitialization()
{
std::vector<column::Row> mockRowName = {{column::ColumnValue("name", column::ColumnType::TEXT, "name_test")}};
std::vector<column::Row> mockRowKey = {{column::ColumnValue("key", column::ColumnType::TEXT, "key_test")}};
std::vector<column::Row> mockRowUUID = {{column::ColumnValue("uuid", column::ColumnType::TEXT, "uuid_test")}};
std::vector<column::Row> mockRowGroup = {{}};

testing::Sequence seq;
const std::vector<column::Row> mockRowName = {
{column::ColumnValue("name", column::ColumnType::TEXT, "name_test")}};
const std::vector<column::Row> mockRowKey = {
{column::ColumnValue("key", column::ColumnType::TEXT, "key_test")}};
const std::vector<column::Row> mockRowUUID = {
{column::ColumnValue("uuid", column::ColumnType::TEXT, "uuid_test")}};
const std::vector<column::Row> mockRowGroup = {{}};

const testing::Sequence seq;
EXPECT_CALL(*m_mockPersistence,
Select("agent_info", testing::_, testing::_, testing::_, testing::_, testing::_, testing::_))
.InSequence(seq)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<int> distribution(MIN_VALUE, MAX_VALUE);
int random = distribution(generator);
const int random = distribution(generator);

auto now = std::chrono::high_resolution_clock::now();
auto timestamp = now.time_since_epoch().count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace

TEST(CentralizedConfiguration, Constructor)
{
EXPECT_NO_THROW(CentralizedConfiguration centralizedConfiguration(
EXPECT_NO_THROW(const CentralizedConfiguration centralizedConfiguration(
[](const std::vector<std::string>&) { return true; },
[]() { return std::vector<std::string> {}; },
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; },
Expand All @@ -49,7 +49,7 @@ TEST(CentralizedConfiguration, Constructor)

TEST(CentralizedConfiguration, ConstructorNoGetGroups)
{
EXPECT_THROW(CentralizedConfiguration centralizedConfiguration(
EXPECT_THROW(const CentralizedConfiguration centralizedConfiguration(
nullptr,
[]() { return std::vector<std::string> {}; },
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; },
Expand All @@ -61,7 +61,7 @@ TEST(CentralizedConfiguration, ConstructorNoGetGroups)

TEST(CentralizedConfiguration, ConstructorNoSetGroups)
{
EXPECT_THROW(CentralizedConfiguration centralizedConfiguration(
EXPECT_THROW(const CentralizedConfiguration centralizedConfiguration(
[](const std::vector<std::string>&) { return true; },
nullptr,
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; },
Expand All @@ -73,18 +73,19 @@ TEST(CentralizedConfiguration, ConstructorNoSetGroups)

TEST(CentralizedConfiguration, ConstructorNoDownloadGroups)
{
EXPECT_THROW(CentralizedConfiguration centralizedConfiguration([](const std::vector<std::string>&) { return true; },
[]() { return std::vector<std::string> {}; },
nullptr,
[](const std::filesystem::path&) { return true; },
[]() {},
nullptr),
std::runtime_error);
EXPECT_THROW(
const CentralizedConfiguration centralizedConfiguration([](const std::vector<std::string>&) { return true; },
[]() { return std::vector<std::string> {}; },
nullptr,
[](const std::filesystem::path&) { return true; },
[]() {},
nullptr),
std::runtime_error);
}

TEST(CentralizedConfiguration, ConstructorNoValidateGroups)
{
EXPECT_THROW(CentralizedConfiguration centralizedConfiguration(
EXPECT_THROW(const CentralizedConfiguration centralizedConfiguration(
[](const std::vector<std::string>&) { return true; },
[]() { return std::vector<std::string> {}; },
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; },
Expand All @@ -96,7 +97,7 @@ TEST(CentralizedConfiguration, ConstructorNoValidateGroups)

TEST(CentralizedConfiguration, ConstructorNoReloadModules)
{
EXPECT_THROW(CentralizedConfiguration centralizedConfiguration(
EXPECT_THROW(const CentralizedConfiguration centralizedConfiguration(
[](const std::vector<std::string>&) { return true; },
[]() { return std::vector<std::string> {}; },
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; },
Expand Down
4 changes: 2 additions & 2 deletions src/agent/command_handler/tests/command_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ TEST(CommandHandlerTest, CommandHandlerConstructor)
{
auto configurationParser = std::make_shared<configuration::ConfigurationParser>();

EXPECT_NO_THROW(command_handler::CommandHandler cm(configurationParser));
EXPECT_NO_THROW(const command_handler::CommandHandler cm(configurationParser));
}

TEST(CommandHandlerTest, CommandHandlerConstructorNoConfigParser)
{
EXPECT_THROW(command_handler::CommandHandler cm(nullptr), std::runtime_error);
EXPECT_THROW(const command_handler::CommandHandler cm(nullptr), std::runtime_error);
}

int main(int argc, char** argv)
Expand Down
Loading

0 comments on commit 8568b29

Please sign in to comment.