blob: a07390b3b2fa9d82db4e697e86a80ca560b42917 [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#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
13namespace phosphor
14{
15namespace network
16{
17
18class 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
47TEST_F(TestConfigParser, ReadConfigDataFromFile)
48{
49 auto values = parser.getValues("Network", "DHCP");
50 std::string expectedValue = "true";
51 bool found = isValueFound(values, expectedValue);
52 EXPECT_EQ(found, true);
53
54 values = parser.getValues("DHCP", "ClientIdentifier");
55 expectedValue = "mac";
56 found = isValueFound(values, expectedValue);
57 EXPECT_EQ(found, true);
58
59 values = parser.getValues("Match", "Name");
60 expectedValue = "eth0";
61 found = isValueFound(values, expectedValue);
62 EXPECT_EQ(found, true);
63}
64
65TEST_F(TestConfigParser, SectionNotExist)
66{
67 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
68 bool caughtException = false;
69 try
70 {
71 parser.getValues("abc", "ipaddress");
72 }
73 catch (const std::exception& e)
74 {
75 caughtException = true;
76 }
77 EXPECT_EQ(true, caughtException);
78}
79
80TEST_F(TestConfigParser, KeyNotFound)
81{
82 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
83 bool caughtException = false;
84 try
85 {
86 parser.getValues("Network", "abc");
87 }
88 catch (const std::exception& e)
89 {
90 caughtException = true;
91 }
92 EXPECT_EQ(true, caughtException);
93 remove("/tmp/eth0.network");
94}
95
96}//namespace network
97}//namespace phosphor
98