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