diff --git a/src/agent/configuration_parser/include/configuration_parser.hpp b/src/agent/configuration_parser/include/configuration_parser.hpp index b32d9ec2d3..a9779aac68 100644 --- a/src/agent/configuration_parser/include/configuration_parser.hpp +++ b/src/agent/configuration_parser/include/configuration_parser.hpp @@ -19,7 +19,7 @@ namespace configuration { private: /// @brief Holds the parsed YAML configuration. - YAML::Node config; + YAML::Node m_config; public: /// @brief Default constructor. Loads configuration from a default file path. @@ -53,7 +53,7 @@ namespace configuration { try { - YAML::Node current = YAML::Clone(config); + YAML::Node current = YAML::Clone(m_config); ( [¤t](const auto& key) diff --git a/src/agent/configuration_parser/src/configuration_parser.cpp b/src/agent/configuration_parser/src/configuration_parser.cpp index 5b74bfcae7..5c2716c6ef 100644 --- a/src/agent/configuration_parser/src/configuration_parser.cpp +++ b/src/agent/configuration_parser/src/configuration_parser.cpp @@ -12,7 +12,7 @@ namespace configuration { try { - config = YAML::LoadFile(configFile.string()); + m_config = YAML::LoadFile(configFile.string()); } catch (const std::exception& e) { @@ -42,7 +42,7 @@ namespace configuration file_wait: 500 )"; - config = YAML::Load(yamlStr); + m_config = YAML::Load(yamlStr); } } @@ -55,7 +55,7 @@ namespace configuration { try { - config = YAML::Load(stringToParse); + m_config = YAML::Load(stringToParse); } catch (const std::exception& e) { diff --git a/src/agent/configuration_parser/tests/configuration_parser_test.cpp b/src/agent/configuration_parser/tests/configuration_parser_test.cpp index 1ee8cf73f4..04458d52ac 100644 --- a/src/agent/configuration_parser/tests/configuration_parser_test.cpp +++ b/src/agent/configuration_parser/tests/configuration_parser_test.cpp @@ -12,13 +12,13 @@ using namespace configuration; class ConfigurationParserFileTest : public ::testing::Test { protected: - std::filesystem::path tempConfigFilePath; + std::filesystem::path m_tempConfigFilePath; void SetUp() override { - tempConfigFilePath = "temp_wazuh-agent.yml"; + m_tempConfigFilePath = "temp_wazuh-agent.yml"; - std::ofstream outFile(tempConfigFilePath); + std::ofstream outFile(m_tempConfigFilePath); outFile << R"( agent: server_url: https://myserver:28000 @@ -39,20 +39,22 @@ class ConfigurationParserFileTest : public ::testing::Test void TearDown() override { - std::filesystem::remove(tempConfigFilePath); + std::filesystem::remove(m_tempConfigFilePath); } }; class ConfigurationParserInvalidYamlFileTest : public ::testing::Test { protected: - std::filesystem::path tempConfigFilePath; + std::filesystem::path m_tempConfigFilePath; void SetUp() override { - tempConfigFilePath = "temp_wazuh-agent.yml"; + m_tempConfigFilePath = "temp_wazuh-agent.yml"; - std::ofstream outFile(tempConfigFilePath); + std::ofstream outFile(m_tempConfigFilePath); + // This string does not respect the yaml format in the line of the file_wait field, the field is misaligned. + // With this case we want it to fail when parsing the file. outFile << R"( agent: server_url: https://myserver:28000 @@ -73,7 +75,7 @@ class ConfigurationParserInvalidYamlFileTest : public ::testing::Test void TearDown() override { - std::filesystem::remove(tempConfigFilePath); + std::filesystem::remove(m_tempConfigFilePath); } }; @@ -265,24 +267,42 @@ TEST(ConfigurationParser, ConfigurationParserStringMisaligned) TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly) { - const auto parser = std::make_unique(tempConfigFilePath); + try + { + const auto parser = std::make_unique(m_tempConfigFilePath); - EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://myserver:28000"); - EXPECT_FALSE(parser->GetConfig("inventory", "enabled")); - EXPECT_EQ(parser->GetConfig("inventory", "interval"), 7200); - EXPECT_FALSE(parser->GetConfig("logcollector", "enabled")); - EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 1000); + EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://myserver:28000"); + EXPECT_FALSE(parser->GetConfig("inventory", "enabled")); + EXPECT_EQ(parser->GetConfig("inventory", "interval"), 7200); + EXPECT_FALSE(parser->GetConfig("logcollector", "enabled")); + EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 1000); + } + catch (const std::exception& e) + { + std::cerr << "Error: " << e.what() << '\n'; + std::filesystem::remove(m_tempConfigFilePath); + throw; + } } TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault) { - const auto parser = std::make_unique(tempConfigFilePath); + try + { + const auto parser = std::make_unique(m_tempConfigFilePath); - EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://localhost:27000"); - EXPECT_TRUE(parser->GetConfig("inventory", "enabled")); - EXPECT_EQ(parser->GetConfig("inventory", "interval"), 3600); - EXPECT_TRUE(parser->GetConfig("logcollector", "enabled")); - EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 500); + EXPECT_EQ(parser->GetConfig("agent", "server_url"), "https://localhost:27000"); + EXPECT_TRUE(parser->GetConfig("inventory", "enabled")); + EXPECT_EQ(parser->GetConfig("inventory", "interval"), 3600); + EXPECT_TRUE(parser->GetConfig("logcollector", "enabled")); + EXPECT_EQ(parser->GetConfig("logcollector", "file_wait"), 500); + } + catch (const std::exception& e) + { + std::cerr << "Error: " << e.what() << '\n'; + std::filesystem::remove(m_tempConfigFilePath); + throw; + } } int main(int argc, char** argv)