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