blob: 99effadee4acaa56ad83e1f805f9fcdbfc2e5aab [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
3#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05304#include "xyz/openbmc_project/Common/error.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05305
Ratan Guptaed123a32017-06-15 09:07:31 +05306#include <exception>
Ratan Guptaed123a32017-06-15 09:07:31 +05307#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -05008#include <phosphor-logging/elog-errors.hpp>
9#include <stdexcept>
10
11#include <gtest/gtest.h>
Ratan Guptaed123a32017-06-15 09:07:31 +053012
13namespace phosphor
14{
15namespace network
16{
17
18class TestConfigParser : public testing::Test
19{
Gunnar Mills57d9c502018-09-14 14:42:34 -050020 public:
21 config::Parser parser;
22 TestConfigParser()
23 {
24 remove("/tmp/eth0.network");
25 std::ofstream filestream("/tmp/eth0.network");
Ratan Guptaed123a32017-06-15 09:07:31 +053026
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 filestream << "[Match]\nName=eth0\n"
28 << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n";
29 filestream.close();
30 parser.setFile("/tmp/eth0.network");
31 }
Ratan Guptaed123a32017-06-15 09:07:31 +053032
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 bool isValueFound(const std::vector<std::string>& values,
34 const std::string& expectedValue)
35 {
36 for (const auto& value : values)
Ratan Guptaed123a32017-06-15 09:07:31 +053037 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 if (expectedValue == value)
Ratan Guptaed123a32017-06-15 09:07:31 +053039 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 return true;
Ratan Guptaed123a32017-06-15 09:07:31 +053041 }
Ratan Guptaed123a32017-06-15 09:07:31 +053042 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 return false;
44 }
Ratan Guptaed123a32017-06-15 09:07:31 +053045};
46
47TEST_F(TestConfigParser, ReadConfigDataFromFile)
48{
Ratan Guptac27170a2017-11-22 15:44:42 +053049 config::ReturnCode rc = config::ReturnCode::SUCCESS;
50 config::ValueList values;
51
52 std::tie(rc, values) = parser.getValues("Network", "DHCP");
Ratan Guptaed123a32017-06-15 09:07:31 +053053 std::string expectedValue = "true";
54 bool found = isValueFound(values, expectedValue);
55 EXPECT_EQ(found, true);
56
Ratan Guptac27170a2017-11-22 15:44:42 +053057 std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier");
Ratan Guptaed123a32017-06-15 09:07:31 +053058 expectedValue = "mac";
59 found = isValueFound(values, expectedValue);
60 EXPECT_EQ(found, true);
61
Ratan Guptac27170a2017-11-22 15:44:42 +053062 std::tie(rc, values) = parser.getValues("Match", "Name");
Ratan Guptaed123a32017-06-15 09:07:31 +053063 expectedValue = "eth0";
64 found = isValueFound(values, expectedValue);
65 EXPECT_EQ(found, true);
66}
67
68TEST_F(TestConfigParser, SectionNotExist)
69{
Ratan Guptac27170a2017-11-22 15:44:42 +053070 config::ReturnCode rc = config::ReturnCode::SUCCESS;
71 config::ValueList values;
72 std::tie(rc, values) = parser.getValues("abc", "ipaddress");
73 EXPECT_EQ(config::ReturnCode::SECTION_NOT_FOUND, rc);
Ratan Guptaed123a32017-06-15 09:07:31 +053074}
75
76TEST_F(TestConfigParser, KeyNotFound)
77{
Ratan Guptac27170a2017-11-22 15:44:42 +053078 config::ReturnCode rc = config::ReturnCode::SUCCESS;
79 config::ValueList values;
80 std::tie(rc, values) = parser.getValues("Network", "abc");
81 EXPECT_EQ(config::ReturnCode::KEY_NOT_FOUND, rc);
Ratan Guptaed123a32017-06-15 09:07:31 +053082 remove("/tmp/eth0.network");
83}
84
Gunnar Mills57d9c502018-09-14 14:42:34 -050085} // namespace network
86} // namespace phosphor