Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 2 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 3 | #include <fmt/format.h> |
| 4 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 5 | #include <exception> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 6 | #include <fstream> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <stdexcept> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 9 | #include <stdplus/gtest/tmp.hpp> |
Patrick Venture | a973340 | 2019-01-07 13:27:01 -0800 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 11 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 12 | #include <gmock/gmock.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 19 | namespace config |
| 20 | { |
| 21 | |
| 22 | using testing::ElementsAre; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 24 | class TestConfigParser : public stdplus::gtest::TestWithTmp |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 25 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | public: |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 27 | std::string filename = fmt::format("{}/eth0.network", CaseTmpDir()); |
| 28 | Parser parser; |
| 29 | |
| 30 | void WriteSampleFile() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 31 | { |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 32 | std::ofstream filestream(filename); |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 33 | filestream << "\n\n\n\nBad=key\n[Match]\n # K=v \nName =eth0\n" |
| 34 | << "[Network\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n" |
| 35 | << "[Network] a\nDHCP=false #hi\n\n\nDHCP = yes \n" |
| 36 | << " [ SEC ] \n'DHCP#'=\"#hi\"\nDHCP#=ho\n[Network]\n" |
| 37 | << "Key=val\nAddress=::/0\n[]\n=\nKey"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | filestream.close(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | TEST_F(TestConfigParser, ReadConfigDataFromFile) |
| 43 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 44 | WriteSampleFile(); |
| 45 | parser.setFile(filename); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 46 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 47 | EXPECT_THAT(parser.getValues("Match", "Name"), ElementsAre("eth0")); |
| 48 | EXPECT_THAT(parser.getValues("DHCP", "ClientIdentifier"), |
| 49 | ElementsAre("mac")); |
| 50 | EXPECT_THAT(parser.getValues("Blah", "nil"), ElementsAre()); |
| 51 | EXPECT_THAT(parser.getValues("Network", "nil"), ElementsAre()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 52 | } |
| 53 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame^] | 54 | } // namespace config |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | } // namespace network |
| 56 | } // namespace phosphor |