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 | d9e2d72 | 2022-08-18 15:21:08 -0700 | [diff] [blame^] | 3 | #include <fmt/chrono.h> |
| 4 | #include <fmt/compile.h> |
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 | d9e2d72 | 2022-08-18 15:21:08 -0700 | [diff] [blame^] | 11 | #include <stdplus/fd/atomic.hpp> |
| 12 | #include <stdplus/fd/fmt.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 13 | #include <stdplus/gtest/tmp.hpp> |
Patrick Venture | a973340 | 2019-01-07 13:27:01 -0800 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 15 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 18 | |
| 19 | namespace phosphor |
| 20 | { |
| 21 | namespace network |
| 22 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 23 | namespace config |
| 24 | { |
| 25 | |
| 26 | using testing::ElementsAre; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 27 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 28 | class TestConfigParser : public stdplus::gtest::TestWithTmp |
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 | public: |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 31 | std::string filename = fmt::format("{}/eth0.network", CaseTmpDir()); |
| 32 | Parser parser; |
| 33 | |
| 34 | void WriteSampleFile() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | { |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 36 | std::ofstream filestream(filename); |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 37 | filestream << "\n\n\n\nBad=key\n[Match]\n # K=v \nName =eth0\n" |
| 38 | << "[Network\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n" |
| 39 | << "[Network] a\nDHCP=false #hi\n\n\nDHCP = yes \n" |
| 40 | << " [ SEC ] \n'DHCP#'=\"#hi\"\nDHCP#=ho\n[Network]\n" |
| 41 | << "Key=val\nAddress=::/0\n[]\n=\nKey"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 42 | filestream.close(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | TEST_F(TestConfigParser, ReadConfigDataFromFile) |
| 47 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 48 | WriteSampleFile(); |
| 49 | parser.setFile(filename); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 50 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 51 | EXPECT_THAT(parser.getValues("Match", "Name"), ElementsAre("eth0")); |
| 52 | EXPECT_THAT(parser.getValues("DHCP", "ClientIdentifier"), |
| 53 | ElementsAre("mac")); |
| 54 | EXPECT_THAT(parser.getValues("Blah", "nil"), ElementsAre()); |
| 55 | EXPECT_THAT(parser.getValues("Network", "nil"), ElementsAre()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 56 | } |
| 57 | |
William A. Kennington III | d9e2d72 | 2022-08-18 15:21:08 -0700 | [diff] [blame^] | 58 | TEST_F(TestConfigParser, Perf) |
| 59 | { |
| 60 | GTEST_SKIP(); |
| 61 | stdplus::fd::AtomicWriter file(fmt::format("{}/tmp.XXXXXX", CaseTmpDir()), |
| 62 | 0600); |
| 63 | stdplus::fd::FormatBuffer out(file); |
| 64 | for (size_t i = 0; i < 500; ++i) |
| 65 | { |
| 66 | out.append(FMT_COMPILE("[{:a>{}}]\n"), "", i + 1); |
| 67 | for (size_t j = 0; j < 70; j++) |
| 68 | { |
| 69 | const size_t es = i * 70 + j + 1; |
| 70 | out.append(FMT_COMPILE("{:b>{}}={:c>{}}\n"), "", es, "", es); |
| 71 | } |
| 72 | } |
| 73 | out.flush(); |
| 74 | file.commit(); |
| 75 | |
| 76 | auto start = std::chrono::steady_clock::now(); |
| 77 | parser.setFile(filename); |
| 78 | fmt::print("Duration: {}\n", std::chrono::steady_clock::now() - start); |
| 79 | // Make sure this test isn't enabled |
| 80 | EXPECT_FALSE(true); |
| 81 | } |
| 82 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 83 | } // namespace config |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 84 | } // namespace network |
| 85 | } // namespace phosphor |