Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #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 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | |
| 18 | class 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 | |
| 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 | |
| 85 | }//namespace network |
| 86 | }//namespace phosphor |