Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 2 | |
| 3 | #include "config_parser.hpp" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 4 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 5 | #include <fmt/format.h> |
| 6 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 7 | #include <exception> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 8 | #include <fstream> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <stdexcept> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 11 | #include <stdplus/gtest/tmp.hpp> |
Patrick Venture | a973340 | 2019-01-07 13:27:01 -0800 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | |
| 14 | #include <gtest/gtest.h> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 21 | class TestConfigParser : public stdplus::gtest::TestWithTmp |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 22 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 23 | public: |
| 24 | config::Parser parser; |
| 25 | TestConfigParser() |
| 26 | { |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 27 | auto filename = fmt::format("{}/eth0.network", CaseTmpDir()); |
| 28 | std::ofstream filestream(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 29 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 30 | filestream << "[Match]\nName=eth0\n" |
| 31 | << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n"; |
| 32 | filestream.close(); |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 33 | parser.setFile(filename); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | bool isValueFound(const std::vector<std::string>& values, |
| 37 | const std::string& expectedValue) |
| 38 | { |
| 39 | for (const auto& value : values) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 40 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 41 | if (expectedValue == value) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 42 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | return true; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 44 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 45 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | return false; |
| 47 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | TEST_F(TestConfigParser, ReadConfigDataFromFile) |
| 51 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 52 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 53 | config::ValueList values; |
| 54 | |
| 55 | std::tie(rc, values) = parser.getValues("Network", "DHCP"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 56 | std::string expectedValue = "true"; |
| 57 | bool found = isValueFound(values, expectedValue); |
| 58 | EXPECT_EQ(found, true); |
| 59 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 60 | std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 61 | expectedValue = "mac"; |
| 62 | found = isValueFound(values, expectedValue); |
| 63 | EXPECT_EQ(found, true); |
| 64 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 65 | std::tie(rc, values) = parser.getValues("Match", "Name"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 66 | expectedValue = "eth0"; |
| 67 | found = isValueFound(values, expectedValue); |
| 68 | EXPECT_EQ(found, true); |
| 69 | } |
| 70 | |
| 71 | TEST_F(TestConfigParser, SectionNotExist) |
| 72 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 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); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST_F(TestConfigParser, KeyNotFound) |
| 80 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 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); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 85 | } |
| 86 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 87 | } // namespace network |
| 88 | } // namespace phosphor |