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