diff options
author | Ratan Gupta <ratagupt@in.ibm.com> | 2017-06-15 09:07:31 +0530 |
---|---|---|
committer | Ratan Gupta <ratagupt@in.ibm.com> | 2017-06-21 15:34:32 +0530 |
commit | ed123a3a3363144b24c444be5527a3d7b56a72a4 (patch) | |
tree | 539b01aa88ff52e55f1e40dbb509450729c9d289 /test | |
parent | ef85eb9c0e1859f8c420e753341a18ddaa2f8cbb (diff) | |
download | phosphor-networkd-ed123a3a3363144b24c444be5527a3d7b56a72a4.tar.gz phosphor-networkd-ed123a3a3363144b24c444be5527a3d7b56a72a4.zip |
Implement the INI config parser
Parse the systemd.network file
to get the configuration parameter.
Change-Id: Ic9c15a46158d2f1c0948e6eca729663e87051491
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.am | 4 | ||||
-rw-r--r-- | test/test_config_parser.cpp | 98 |
2 files changed, 101 insertions, 1 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 8f951bf..bf1a935 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -8,7 +8,8 @@ test_SOURCES = \ test_util.cpp \ mock_syscall.cpp \ test_network_manager.cpp \ - test_ethernet_interface.cpp + test_ethernet_interface.cpp \ + test_config_parser.cpp test_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS) @@ -32,5 +33,6 @@ test_LDADD = $(top_builddir)/ethernet_interface.cpp \ $(top_builddir)/routing_table.cpp \ $(top_builddir)/util.cpp \ $(top_builddir)/system_configuration.cpp \ + $(top_builddir)/config_parser.cpp \ $(top_builddir)/xyz/openbmc_project/Network/VLAN/Create/phosphor_network_manager-server.o \ $(top_builddir)/xyz/openbmc_project/Network/IP/Create/phosphor_network_manager-server.o diff --git a/test/test_config_parser.cpp b/test/test_config_parser.cpp new file mode 100644 index 0000000..a07390b --- /dev/null +++ b/test/test_config_parser.cpp @@ -0,0 +1,98 @@ +#include <gtest/gtest.h> + +#include "config_parser.hpp" + +#include "xyz/openbmc_project/Common/error.hpp" +#include <phosphor-logging/elog-errors.hpp> + +#include "config.h" +#include <exception> +#include <stdexcept> +#include <fstream> + +namespace phosphor +{ +namespace network +{ + +class TestConfigParser : public testing::Test +{ + public: + config::Parser parser; + TestConfigParser() + { + remove("/tmp/eth0.network"); + std::ofstream filestream("/tmp/eth0.network"); + + filestream << "[Match]\nName=eth0\n" << + "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n"; + filestream.close(); + parser.setFile("/tmp/eth0.network"); + } + + bool isValueFound(const std::vector<std::string>& values, + const std::string& expectedValue) + { + for (const auto& value : values) + { + if (expectedValue == value) + { + return true; + } + } + return false; + } +}; + +TEST_F(TestConfigParser, ReadConfigDataFromFile) +{ + auto values = parser.getValues("Network", "DHCP"); + std::string expectedValue = "true"; + bool found = isValueFound(values, expectedValue); + EXPECT_EQ(found, true); + + values = parser.getValues("DHCP", "ClientIdentifier"); + expectedValue = "mac"; + found = isValueFound(values, expectedValue); + EXPECT_EQ(found, true); + + values = parser.getValues("Match", "Name"); + expectedValue = "eth0"; + found = isValueFound(values, expectedValue); + EXPECT_EQ(found, true); +} + +TEST_F(TestConfigParser, SectionNotExist) +{ + using namespace sdbusplus::xyz::openbmc_project::Common::Error; + bool caughtException = false; + try + { + parser.getValues("abc", "ipaddress"); + } + catch (const std::exception& e) + { + caughtException = true; + } + EXPECT_EQ(true, caughtException); +} + +TEST_F(TestConfigParser, KeyNotFound) +{ + using namespace sdbusplus::xyz::openbmc_project::Common::Error; + bool caughtException = false; + try + { + parser.getValues("Network", "abc"); + } + catch (const std::exception& e) + { + caughtException = true; + } + EXPECT_EQ(true, caughtException); + remove("/tmp/eth0.network"); +} + +}//namespace network +}//namespace phosphor + |