blob: 7866f87aaefcdf26a663a061400b7bcf66ab28af [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
3#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05304
William A. Kennington III84bfe672022-07-13 14:15:30 -07005#include <fmt/format.h>
6
Ratan Guptaed123a32017-06-15 09:07:31 +05307#include <exception>
Ratan Guptaed123a32017-06-15 09:07:31 +05308#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -05009#include <phosphor-logging/elog-errors.hpp>
10#include <stdexcept>
William A. Kennington III84bfe672022-07-13 14:15:30 -070011#include <stdplus/gtest/tmp.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080012#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013
14#include <gtest/gtest.h>
Ratan Guptaed123a32017-06-15 09:07:31 +053015
16namespace phosphor
17{
18namespace network
19{
20
William A. Kennington III84bfe672022-07-13 14:15:30 -070021class TestConfigParser : public stdplus::gtest::TestWithTmp
Ratan Guptaed123a32017-06-15 09:07:31 +053022{
Gunnar Mills57d9c502018-09-14 14:42:34 -050023 public:
24 config::Parser parser;
25 TestConfigParser()
26 {
William A. Kennington III84bfe672022-07-13 14:15:30 -070027 auto filename = fmt::format("{}/eth0.network", CaseTmpDir());
28 std::ofstream filestream(filename);
Ratan Guptaed123a32017-06-15 09:07:31 +053029
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 filestream << "[Match]\nName=eth0\n"
31 << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n";
32 filestream.close();
William A. Kennington III84bfe672022-07-13 14:15:30 -070033 parser.setFile(filename);
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 }
Ratan Guptaed123a32017-06-15 09:07:31 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 bool isValueFound(const std::vector<std::string>& values,
37 const std::string& expectedValue)
38 {
39 for (const auto& value : values)
Ratan Guptaed123a32017-06-15 09:07:31 +053040 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 if (expectedValue == value)
Ratan Guptaed123a32017-06-15 09:07:31 +053042 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 return true;
Ratan Guptaed123a32017-06-15 09:07:31 +053044 }
Ratan Guptaed123a32017-06-15 09:07:31 +053045 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 return false;
47 }
Ratan Guptaed123a32017-06-15 09:07:31 +053048};
49
50TEST_F(TestConfigParser, ReadConfigDataFromFile)
51{
Ratan Guptac27170a2017-11-22 15:44:42 +053052 config::ReturnCode rc = config::ReturnCode::SUCCESS;
53 config::ValueList values;
54
55 std::tie(rc, values) = parser.getValues("Network", "DHCP");
Ratan Guptaed123a32017-06-15 09:07:31 +053056 std::string expectedValue = "true";
57 bool found = isValueFound(values, expectedValue);
58 EXPECT_EQ(found, true);
59
Ratan Guptac27170a2017-11-22 15:44:42 +053060 std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier");
Ratan Guptaed123a32017-06-15 09:07:31 +053061 expectedValue = "mac";
62 found = isValueFound(values, expectedValue);
63 EXPECT_EQ(found, true);
64
Ratan Guptac27170a2017-11-22 15:44:42 +053065 std::tie(rc, values) = parser.getValues("Match", "Name");
Ratan Guptaed123a32017-06-15 09:07:31 +053066 expectedValue = "eth0";
67 found = isValueFound(values, expectedValue);
68 EXPECT_EQ(found, true);
69}
70
71TEST_F(TestConfigParser, SectionNotExist)
72{
Ratan Guptac27170a2017-11-22 15:44:42 +053073 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);
Ratan Guptaed123a32017-06-15 09:07:31 +053077}
78
79TEST_F(TestConfigParser, KeyNotFound)
80{
Ratan Guptac27170a2017-11-22 15:44:42 +053081 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);
Ratan Guptaed123a32017-06-15 09:07:31 +053085}
86
Gunnar Mills57d9c502018-09-14 14:42:34 -050087} // namespace network
88} // namespace phosphor