blob: 1c902d2790170ac564af4fc56635c1faa6f3c279 [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{
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
85}//namespace network
86}//namespace phosphor