summaryrefslogtreecommitdiffstats
path: root/test/test_config_parser.cpp
blob: 61432c255b50a1a22198a93108341041d539a840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "config.h"

#include "config_parser.hpp"

#include <exception>
#include <fstream>
#include <phosphor-logging/elog-errors.hpp>
#include <stdexcept>
#include <xyz/openbmc_project/Common/error.hpp>

#include <gtest/gtest.h>

namespace phosphor
{
namespace network
{

class TestConfigParser : public testing::Test
{
  public:
    config::Parser parser;
    TestConfigParser()
    {
        remove("/tmp/eth0.network");
        std::ofstream filestream("/tmp/eth0.network");

        filestream << "[Match]\nName=eth0\n"
                   << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n";
        filestream.close();
        parser.setFile("/tmp/eth0.network");
    }

    bool isValueFound(const std::vector<std::string>& values,
                      const std::string& expectedValue)
    {
        for (const auto& value : values)
        {
            if (expectedValue == value)
            {
                return true;
            }
        }
        return false;
    }
};

TEST_F(TestConfigParser, ReadConfigDataFromFile)
{
    config::ReturnCode rc = config::ReturnCode::SUCCESS;
    config::ValueList values;

    std::tie(rc, values) = parser.getValues("Network", "DHCP");
    std::string expectedValue = "true";
    bool found = isValueFound(values, expectedValue);
    EXPECT_EQ(found, true);

    std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier");
    expectedValue = "mac";
    found = isValueFound(values, expectedValue);
    EXPECT_EQ(found, true);

    std::tie(rc, values) = parser.getValues("Match", "Name");
    expectedValue = "eth0";
    found = isValueFound(values, expectedValue);
    EXPECT_EQ(found, true);
}

TEST_F(TestConfigParser, SectionNotExist)
{
    config::ReturnCode rc = config::ReturnCode::SUCCESS;
    config::ValueList values;
    std::tie(rc, values) = parser.getValues("abc", "ipaddress");
    EXPECT_EQ(config::ReturnCode::SECTION_NOT_FOUND, rc);
}

TEST_F(TestConfigParser, KeyNotFound)
{
    config::ReturnCode rc = config::ReturnCode::SUCCESS;
    config::ValueList values;
    std::tie(rc, values) = parser.getValues("Network", "abc");
    EXPECT_EQ(config::ReturnCode::KEY_NOT_FOUND, rc);
    remove("/tmp/eth0.network");
}

} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud