1 #include "config_parser.hpp" 2 3 #include <fmt/chrono.h> 4 #include <fmt/compile.h> 5 #include <fmt/format.h> 6 7 #include <exception> 8 #include <fstream> 9 #include <phosphor-logging/elog-errors.hpp> 10 #include <stdexcept> 11 #include <stdplus/fd/atomic.hpp> 12 #include <stdplus/fd/fmt.hpp> 13 #include <stdplus/gtest/tmp.hpp> 14 #include <xyz/openbmc_project/Common/error.hpp> 15 16 #include <gmock/gmock.h> 17 #include <gtest/gtest.h> 18 19 namespace phosphor 20 { 21 namespace network 22 { 23 namespace config 24 { 25 26 using testing::ElementsAre; 27 28 class TestConfigParser : public stdplus::gtest::TestWithTmp 29 { 30 public: 31 std::string filename = fmt::format("{}/eth0.network", CaseTmpDir()); 32 Parser parser; 33 34 void WriteSampleFile() 35 { 36 std::ofstream filestream(filename); 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"; 42 filestream.close(); 43 } 44 }; 45 46 TEST_F(TestConfigParser, ReadConfigDataFromFile) 47 { 48 WriteSampleFile(); 49 parser.setFile(filename); 50 51 EXPECT_THAT(parser.getValues("Match", "Name"), ElementsAre("eth0")); 52 EXPECT_THAT(parser.getValues("DHCP", "ClientIdentifier"), 53 ElementsAre("mac")); 54 EXPECT_THAT(parser.getValues("Network", "DHCP"), 55 ElementsAre("true", "false #hi", "yes")); 56 EXPECT_THAT(parser.getValues(" SEC ", "'DHCP#'"), ElementsAre("\"#hi\"")); 57 EXPECT_THAT(parser.getValues("Blah", "nil"), ElementsAre()); 58 EXPECT_THAT(parser.getValues("Network", "nil"), ElementsAre()); 59 } 60 61 TEST_F(TestConfigParser, Perf) 62 { 63 GTEST_SKIP(); 64 stdplus::fd::AtomicWriter file(fmt::format("{}/tmp.XXXXXX", CaseTmpDir()), 65 0600); 66 stdplus::fd::FormatBuffer out(file); 67 for (size_t i = 0; i < 500; ++i) 68 { 69 out.append(FMT_COMPILE("[{:a>{}}]\n"), "", i + 1); 70 for (size_t j = 0; j < 70; j++) 71 { 72 const size_t es = i * 70 + j + 1; 73 out.append(FMT_COMPILE("{:b>{}}={:c>{}}\n"), "", es, "", es); 74 } 75 } 76 out.flush(); 77 file.commit(); 78 79 auto start = std::chrono::steady_clock::now(); 80 parser.setFile(filename); 81 fmt::print("Duration: {}\n", std::chrono::steady_clock::now() - start); 82 // Make sure this test isn't enabled 83 EXPECT_FALSE(true); 84 } 85 86 } // namespace config 87 } // namespace network 88 } // namespace phosphor 89