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 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 5 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 6 | #include <exception> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 7 | #include <fstream> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <stdexcept> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | |
| 18 | class TestConfigParser : public testing::Test |
| 19 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 20 | public: |
| 21 | config::Parser parser; |
| 22 | TestConfigParser() |
| 23 | { |
| 24 | remove("/tmp/eth0.network"); |
| 25 | std::ofstream filestream("/tmp/eth0.network"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 26 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 32 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 33 | bool isValueFound(const std::vector<std::string>& values, |
| 34 | const std::string& expectedValue) |
| 35 | { |
| 36 | for (const auto& value : values) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 37 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | if (expectedValue == value) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 39 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | return true; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 41 | } |
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 false; |
| 44 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | TEST_F(TestConfigParser, ReadConfigDataFromFile) |
| 48 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 49 | config::ReturnCode rc = config::ReturnCode::SUCCESS; |
| 50 | config::ValueList values; |
| 51 | |
| 52 | std::tie(rc, values) = parser.getValues("Network", "DHCP"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 53 | std::string expectedValue = "true"; |
| 54 | bool found = isValueFound(values, expectedValue); |
| 55 | EXPECT_EQ(found, true); |
| 56 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 57 | std::tie(rc, values) = parser.getValues("DHCP", "ClientIdentifier"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 58 | expectedValue = "mac"; |
| 59 | found = isValueFound(values, expectedValue); |
| 60 | EXPECT_EQ(found, true); |
| 61 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 62 | std::tie(rc, values) = parser.getValues("Match", "Name"); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 63 | expectedValue = "eth0"; |
| 64 | found = isValueFound(values, expectedValue); |
| 65 | EXPECT_EQ(found, true); |
| 66 | } |
| 67 | |
| 68 | TEST_F(TestConfigParser, SectionNotExist) |
| 69 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 70 | 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 Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST_F(TestConfigParser, KeyNotFound) |
| 77 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 78 | 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 Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 82 | remove("/tmp/eth0.network"); |
| 83 | } |
| 84 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 85 | } // namespace network |
| 86 | } // namespace phosphor |