xref: /openbmc/phosphor-networkd/test/test_config_parser.cpp (revision d8bad197fb6d7a18c52d19f472676ecae8ec900d)
1 #include <gtest/gtest.h>
2 
3 #include "config_parser.hpp"
4 
5 #include "xyz/openbmc_project/Common/error.hpp"
6 #include <phosphor-logging/elog-errors.hpp>
7 
8 #include "config.h"
9 #include <exception>
10 #include <stdexcept>
11 #include <fstream>
12 
13 namespace phosphor
14 {
15 namespace network
16 {
17 
18 class TestConfigParser : public testing::Test
19 {
20     public:
21         config::Parser parser;
22         TestConfigParser()
23         {
24             remove("/tmp/eth0.network");
25             std::ofstream filestream("/tmp/eth0.network");
26 
27             filestream << "[Match]\nName=eth0\n" <<
28                           "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n";
29             filestream.close();
30             parser.setFile("/tmp/eth0.network");
31         }
32 
33         bool isValueFound(const std::vector<std::string>& values,
34                           const std::string& expectedValue)
35         {
36             for (const auto& value : values)
37             {
38                 if (expectedValue == value)
39                 {
40                     return  true;
41                 }
42             }
43             return false;
44         }
45 };
46 
47 TEST_F(TestConfigParser, ReadConfigDataFromFile)
48 {
49     config::ReturnCode rc = config::ReturnCode::SUCCESS;
50     config::ValueList values;
51 
52     std::tie(rc, values) = parser.getValues("Network", "DHCP");
53     std::string expectedValue = "true";
54     bool found = isValueFound(values, expectedValue);
55     EXPECT_EQ(found, true);
56 
57     std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier");
58     expectedValue = "mac";
59     found = isValueFound(values, expectedValue);
60     EXPECT_EQ(found, true);
61 
62     std::tie(rc, values) = parser.getValues("Match", "Name");
63     expectedValue = "eth0";
64     found = isValueFound(values, expectedValue);
65     EXPECT_EQ(found, true);
66 }
67 
68 TEST_F(TestConfigParser, SectionNotExist)
69 {
70     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);
74 }
75 
76 TEST_F(TestConfigParser, KeyNotFound)
77 {
78     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);
82     remove("/tmp/eth0.network");
83 }
84 
85 }//namespace network
86 }//namespace phosphor
87