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 | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 28 | TEST(TestConvert, iCaseEq) |
| 29 | { |
| 30 | EXPECT_TRUE(icaseeq("VaL", "val")); |
| 31 | EXPECT_TRUE(icaseeq("[ab1", "[ab1")); |
| 32 | } |
| 33 | |
| 34 | TEST(TestConvert, ParseBool) |
| 35 | { |
| 36 | EXPECT_TRUE(parseBool("tRue").value()); |
| 37 | EXPECT_FALSE(parseBool("tru").has_value()); |
| 38 | EXPECT_TRUE(parseBool("t").value()); |
| 39 | EXPECT_TRUE(parseBool("Yes").value()); |
| 40 | EXPECT_FALSE(parseBool("ye").has_value()); |
| 41 | EXPECT_TRUE(parseBool("y").value()); |
| 42 | EXPECT_TRUE(parseBool("oN").value()); |
| 43 | |
| 44 | EXPECT_FALSE(parseBool("fAlse").value()); |
| 45 | EXPECT_FALSE(parseBool("fal").has_value()); |
| 46 | EXPECT_FALSE(parseBool("f").value()); |
| 47 | EXPECT_FALSE(parseBool("No").value()); |
| 48 | EXPECT_FALSE(parseBool("n").value()); |
| 49 | EXPECT_FALSE(parseBool("oFf").value()); |
| 50 | } |
| 51 | |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 52 | TEST(TestTypeChecking, Section) |
| 53 | { |
| 54 | Section(""); |
| 55 | Section("fds#1!'\""); |
| 56 | EXPECT_THROW(Section("fds]sf"), std::invalid_argument); |
| 57 | EXPECT_THROW(Section("g\ng"), std::invalid_argument); |
| 58 | } |
| 59 | |
| 60 | TEST(TestTypeChecking, Value) |
| 61 | { |
| 62 | Value(""); |
| 63 | Value("=fds1!'\"#="); |
| 64 | Value("fds]sf'' #"); |
| 65 | EXPECT_THROW(Value("g\ng"), std::invalid_argument); |
| 66 | } |
| 67 | |
| 68 | TEST(TestTypeChecking, Key) |
| 69 | { |
| 70 | Key(""); |
| 71 | Key("fds1!'\"#"); |
| 72 | Key("fds]sf'' #"); |
| 73 | EXPECT_THROW(Key("fds]sf'='"), std::invalid_argument); |
| 74 | EXPECT_THROW(Key("g\ng"), std::invalid_argument); |
| 75 | } |
| 76 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 77 | class TestConfigParser : public stdplus::gtest::TestWithTmp |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 78 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 79 | public: |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 80 | std::string filename = fmt::format("{}/eth0.network", CaseTmpDir()); |
| 81 | Parser parser; |
| 82 | |
| 83 | void WriteSampleFile() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 84 | { |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 85 | std::ofstream filestream(filename); |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 86 | filestream << "\n\n\n\nBad=key\n[Match]\n # K=v \nName =eth0\n" |
| 87 | << "[Network\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n" |
| 88 | << "[Network] a\nDHCP=false #hi\n\n\nDHCP = yes \n" |
| 89 | << " [ SEC ] \n'DHCP#'=\"#hi\"\nDHCP#=ho\n[Network]\n" |
| 90 | << "Key=val\nAddress=::/0\n[]\n=\nKey"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | filestream.close(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | } |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 93 | |
| 94 | void ValidateSectionMap() |
| 95 | { |
| 96 | EXPECT_THAT( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 97 | parser.map, |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 98 | testing::ContainerEq(SectionMap(SectionMapInt{ |
| 99 | {"Match", {{{"Name", {"eth0"}}}}}, |
| 100 | {"Network", |
| 101 | { |
| 102 | {{"DHCP", {"true"}}}, |
| 103 | {{"DHCP", {"false #hi", "yes"}}}, |
| 104 | {{"Key", {"val"}}, {"Address", {"::/0"}}}, |
| 105 | }}, |
| 106 | {"DHCP", {{{"ClientIdentifier", {"mac"}}}}}, |
| 107 | {" SEC ", {{{"'DHCP#'", {"\"#hi\""}}, {"DHCP#", {"ho"}}}}}, |
| 108 | {"", {{{"", {""}}}}}, |
| 109 | }))); |
| 110 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 111 | }; |
| 112 | |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 113 | TEST_F(TestConfigParser, EmptyObject) |
| 114 | { |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(parser.getFilename().empty()); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 116 | EXPECT_EQ(0, parser.getWarnings().size()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 117 | EXPECT_EQ(SectionMap(), parser.map); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST_F(TestConfigParser, ReadDirectory) |
| 121 | { |
| 122 | parser.setFile("/"); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 123 | EXPECT_EQ("/", parser.getFilename()); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 124 | EXPECT_EQ(1, parser.getWarnings().size()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 125 | EXPECT_EQ(SectionMap(), parser.map); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(TestConfigParser, ReadConfigDataMissingFile) |
| 129 | { |
| 130 | parser.setFile("/no-such-path"); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 131 | EXPECT_EQ("/no-such-path", parser.getFilename()); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 132 | EXPECT_EQ(1, parser.getWarnings().size()); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 133 | EXPECT_EQ(SectionMap(), parser.map); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 136 | TEST_F(TestConfigParser, ReadConfigDataFromFile) |
| 137 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 138 | WriteSampleFile(); |
| 139 | parser.setFile(filename); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 140 | EXPECT_EQ(filename, parser.getFilename()); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 141 | EXPECT_EQ(4, parser.getWarnings().size()); |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 142 | ValidateSectionMap(); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 143 | |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 144 | const auto& map = parser.map; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 145 | |
| 146 | EXPECT_EQ("eth0", *map.getLastValueString("Match", "Name")); |
| 147 | EXPECT_EQ("yes", *map.getLastValueString("Network", "DHCP")); |
| 148 | EXPECT_EQ(nullptr, map.getLastValueString("Match", "BadKey")); |
| 149 | EXPECT_EQ(nullptr, map.getLastValueString("BadSec", "Name")); |
| 150 | EXPECT_EQ(nullptr, map.getLastValueString("BadSec", "Name")); |
| 151 | |
| 152 | EXPECT_THAT(map.getValueStrings("Match", "Name"), ElementsAre("eth0")); |
| 153 | EXPECT_THAT(map.getValueStrings("DHCP", "ClientIdentifier"), |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 154 | ElementsAre("mac")); |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 155 | EXPECT_THAT(map.getValueStrings("Network", "DHCP"), |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 156 | ElementsAre("true", "false #hi", "yes")); |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 157 | EXPECT_THAT(map.getValueStrings(" SEC ", "'DHCP#'"), |
| 158 | ElementsAre("\"#hi\"")); |
| 159 | EXPECT_THAT(map.getValueStrings("Blah", "nil"), ElementsAre()); |
| 160 | EXPECT_THAT(map.getValueStrings("Network", "nil"), ElementsAre()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 161 | } |
| 162 | |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 163 | TEST_F(TestConfigParser, WriteConfigFile) |
| 164 | { |
| 165 | WriteSampleFile(); |
| 166 | parser.setFile(filename); |
| 167 | EXPECT_EQ(4, parser.getWarnings().size()); |
| 168 | ValidateSectionMap(); |
| 169 | |
| 170 | parser.writeFile(); |
| 171 | |
| 172 | parser.setFile(filename); |
| 173 | EXPECT_EQ(0, parser.getWarnings().size()); |
| 174 | ValidateSectionMap(); |
| 175 | } |
| 176 | |
William A. Kennington III | d9e2d72 | 2022-08-18 15:21:08 -0700 | [diff] [blame] | 177 | TEST_F(TestConfigParser, Perf) |
| 178 | { |
| 179 | GTEST_SKIP(); |
| 180 | stdplus::fd::AtomicWriter file(fmt::format("{}/tmp.XXXXXX", CaseTmpDir()), |
| 181 | 0600); |
| 182 | stdplus::fd::FormatBuffer out(file); |
| 183 | for (size_t i = 0; i < 500; ++i) |
| 184 | { |
| 185 | out.append(FMT_COMPILE("[{:a>{}}]\n"), "", i + 1); |
| 186 | for (size_t j = 0; j < 70; j++) |
| 187 | { |
| 188 | const size_t es = i * 70 + j + 1; |
| 189 | out.append(FMT_COMPILE("{:b>{}}={:c>{}}\n"), "", es, "", es); |
| 190 | } |
| 191 | } |
| 192 | out.flush(); |
| 193 | file.commit(); |
| 194 | |
| 195 | auto start = std::chrono::steady_clock::now(); |
| 196 | parser.setFile(filename); |
| 197 | fmt::print("Duration: {}\n", std::chrono::steady_clock::now() - start); |
| 198 | // Make sure this test isn't enabled |
| 199 | EXPECT_FALSE(true); |
| 200 | } |
| 201 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 202 | } // namespace config |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | } // namespace network |
| 204 | } // namespace phosphor |