blob: d666502666004bc4698ea27805fd35bd4844a076 [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
Gunnar Mills57d9c502018-09-14 14:42:34 -05003#include <phosphor-logging/elog-errors.hpp>
William A. Kennington IIId9e2d722022-08-18 15:21:08 -07004#include <stdplus/fd/atomic.hpp>
5#include <stdplus/fd/fmt.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -07006#include <stdplus/gtest/tmp.hpp>
William A. Kennington IIIb7401962023-07-21 16:36:11 -07007#include <stdplus/print.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -08008#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -05009
Patrick Williams89d734b2023-05-10 07:50:25 -050010#include <exception>
11#include <fstream>
12#include <stdexcept>
13
William A. Kennington III25511a12022-08-04 16:32:28 -070014#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050015#include <gtest/gtest.h>
Ratan Guptaed123a32017-06-15 09:07:31 +053016
17namespace phosphor
18{
19namespace network
20{
William A. Kennington III25511a12022-08-04 16:32:28 -070021namespace config
22{
23
24using testing::ElementsAre;
William A. Kennington III86642522023-07-24 17:55:55 -070025using std::literals::string_view_literals::operator""sv;
Ratan Guptaed123a32017-06-15 09:07:31 +053026
William A. Kennington III150753f2022-08-05 15:20:54 -070027TEST(TestConvert, iCaseEq)
28{
29 EXPECT_TRUE(icaseeq("VaL", "val"));
30 EXPECT_TRUE(icaseeq("[ab1", "[ab1"));
31}
32
33TEST(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 III0dd09372022-08-18 14:57:07 -070051TEST(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
59TEST(TestTypeChecking, Value)
60{
61 Value("");
62 Value("=fds1!'\"#=");
63 Value("fds]sf'' #");
64 EXPECT_THROW(Value("g\ng"), std::invalid_argument);
65}
66
67TEST(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 III84bfe672022-07-13 14:15:30 -070076class TestConfigParser : public stdplus::gtest::TestWithTmp
Ratan Guptaed123a32017-06-15 09:07:31 +053077{
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 public:
William A. Kennington III25511a12022-08-04 16:32:28 -070079 std::string filename = fmt::format("{}/eth0.network", CaseTmpDir());
80 Parser parser;
81
82 void WriteSampleFile()
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 {
William A. Kennington III84bfe672022-07-13 14:15:30 -070084 std::ofstream filestream(filename);
William A. Kennington III25511a12022-08-04 16:32:28 -070085 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 Mills57d9c502018-09-14 14:42:34 -050090 filestream.close();
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 }
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070092
93 void ValidateSectionMap()
94 {
95 EXPECT_THAT(
William A. Kennington III34bb3e22022-08-18 15:17:22 -070096 parser.map,
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070097 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 Guptaed123a32017-06-15 09:07:31 +0530110};
111
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700112TEST_F(TestConfigParser, EmptyObject)
113{
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800114 EXPECT_FALSE(parser.getFileExists());
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700115 EXPECT_TRUE(parser.getFilename().empty());
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700116 EXPECT_EQ(0, parser.getWarnings().size());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700117 EXPECT_EQ(SectionMap(), parser.map);
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700118}
119
120TEST_F(TestConfigParser, ReadDirectory)
121{
122 parser.setFile("/");
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800123 EXPECT_FALSE(parser.getFileExists());
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700124 EXPECT_EQ("/", parser.getFilename());
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700125 EXPECT_EQ(1, parser.getWarnings().size());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700126 EXPECT_EQ(SectionMap(), parser.map);
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700127}
128
129TEST_F(TestConfigParser, ReadConfigDataMissingFile)
130{
131 parser.setFile("/no-such-path");
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800132 EXPECT_FALSE(parser.getFileExists());
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700133 EXPECT_EQ("/no-such-path", parser.getFilename());
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700134 EXPECT_EQ(1, parser.getWarnings().size());
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700135 EXPECT_EQ(SectionMap(), parser.map);
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700136}
137
Ratan Guptaed123a32017-06-15 09:07:31 +0530138TEST_F(TestConfigParser, ReadConfigDataFromFile)
139{
William A. Kennington III25511a12022-08-04 16:32:28 -0700140 WriteSampleFile();
141 parser.setFile(filename);
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800142 EXPECT_TRUE(parser.getFileExists());
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700143 EXPECT_EQ(filename, parser.getFilename());
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700144 EXPECT_EQ(4, parser.getWarnings().size());
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700145 ValidateSectionMap();
Ratan Guptac27170a2017-11-22 15:44:42 +0530146
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700147 const auto& map = parser.map;
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700148
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 III25511a12022-08-04 16:32:28 -0700157 ElementsAre("mac"));
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700158 EXPECT_THAT(map.getValueStrings("Network", "DHCP"),
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700159 ElementsAre("true", "false #hi", "yes"));
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700160 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 Guptaed123a32017-06-15 09:07:31 +0530164}
165
William A. Kennington III409f1a62022-08-11 15:44:37 -0700166TEST_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 IIId9e2d722022-08-18 15:21:08 -0700180TEST_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 III86642522023-07-24 17:55:55 -0700186 std::string obj(500, 'a');
187 std::string kv(500 * 70, 'b');
William A. Kennington IIId9e2d722022-08-18 15:21:08 -0700188 for (size_t i = 0; i < 500; ++i)
189 {
William A. Kennington III86642522023-07-24 17:55:55 -0700190 out.appends("["sv, std::string_view{obj}.substr(0, i + 1), "[\n"sv);
William A. Kennington IIId9e2d722022-08-18 15:21:08 -0700191 for (size_t j = 0; j < 70; j++)
192 {
William A. Kennington III86642522023-07-24 17:55:55 -0700193 auto sv = std::string_view(kv).substr(0, i * 70 + j + 1);
194 out.appends(sv, "="sv, sv, "\n"sv);
William A. Kennington IIId9e2d722022-08-18 15:21:08 -0700195 }
196 }
197 out.flush();
198 file.commit();
199
200 auto start = std::chrono::steady_clock::now();
201 parser.setFile(filename);
William A. Kennington IIIb7401962023-07-21 16:36:11 -0700202 stdplus::print("Duration: {}\n", std::chrono::steady_clock::now() - start);
William A. Kennington IIId9e2d722022-08-18 15:21:08 -0700203 // Make sure this test isn't enabled
204 EXPECT_FALSE(true);
205}
206
William A. Kennington III25511a12022-08-04 16:32:28 -0700207} // namespace config
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208} // namespace network
209} // namespace phosphor