blob: ec90a5fc02760a0bd8c99292cc712f62dfdc850f [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
William A. Kennington IIId9e2d722022-08-18 15:21:08 -07003#include <fmt/chrono.h>
4#include <fmt/compile.h>
William A. Kennington III84bfe672022-07-13 14:15:30 -07005#include <fmt/format.h>
6
Ratan Guptaed123a32017-06-15 09:07:31 +05307#include <exception>
Ratan Guptaed123a32017-06-15 09:07:31 +05308#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -05009#include <phosphor-logging/elog-errors.hpp>
10#include <stdexcept>
William A. Kennington IIId9e2d722022-08-18 15:21:08 -070011#include <stdplus/fd/atomic.hpp>
12#include <stdplus/fd/fmt.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070013#include <stdplus/gtest/tmp.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080014#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050015
William A. Kennington III25511a12022-08-04 16:32:28 -070016#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050017#include <gtest/gtest.h>
Ratan Guptaed123a32017-06-15 09:07:31 +053018
19namespace phosphor
20{
21namespace network
22{
William A. Kennington III25511a12022-08-04 16:32:28 -070023namespace config
24{
25
26using testing::ElementsAre;
Ratan Guptaed123a32017-06-15 09:07:31 +053027
William A. Kennington III84bfe672022-07-13 14:15:30 -070028class TestConfigParser : public stdplus::gtest::TestWithTmp
Ratan Guptaed123a32017-06-15 09:07:31 +053029{
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 public:
William A. Kennington III25511a12022-08-04 16:32:28 -070031 std::string filename = fmt::format("{}/eth0.network", CaseTmpDir());
32 Parser parser;
33
34 void WriteSampleFile()
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 {
William A. Kennington III84bfe672022-07-13 14:15:30 -070036 std::ofstream filestream(filename);
William A. Kennington III25511a12022-08-04 16:32:28 -070037 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";
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 filestream.close();
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 }
Ratan Guptaed123a32017-06-15 09:07:31 +053044};
45
46TEST_F(TestConfigParser, ReadConfigDataFromFile)
47{
William A. Kennington III25511a12022-08-04 16:32:28 -070048 WriteSampleFile();
49 parser.setFile(filename);
Ratan Guptac27170a2017-11-22 15:44:42 +053050
William A. Kennington III25511a12022-08-04 16:32:28 -070051 EXPECT_THAT(parser.getValues("Match", "Name"), ElementsAre("eth0"));
52 EXPECT_THAT(parser.getValues("DHCP", "ClientIdentifier"),
53 ElementsAre("mac"));
54 EXPECT_THAT(parser.getValues("Blah", "nil"), ElementsAre());
55 EXPECT_THAT(parser.getValues("Network", "nil"), ElementsAre());
Ratan Guptaed123a32017-06-15 09:07:31 +053056}
57
William A. Kennington IIId9e2d722022-08-18 15:21:08 -070058TEST_F(TestConfigParser, Perf)
59{
60 GTEST_SKIP();
61 stdplus::fd::AtomicWriter file(fmt::format("{}/tmp.XXXXXX", CaseTmpDir()),
62 0600);
63 stdplus::fd::FormatBuffer out(file);
64 for (size_t i = 0; i < 500; ++i)
65 {
66 out.append(FMT_COMPILE("[{:a>{}}]\n"), "", i + 1);
67 for (size_t j = 0; j < 70; j++)
68 {
69 const size_t es = i * 70 + j + 1;
70 out.append(FMT_COMPILE("{:b>{}}={:c>{}}\n"), "", es, "", es);
71 }
72 }
73 out.flush();
74 file.commit();
75
76 auto start = std::chrono::steady_clock::now();
77 parser.setFile(filename);
78 fmt::print("Duration: {}\n", std::chrono::steady_clock::now() - start);
79 // Make sure this test isn't enabled
80 EXPECT_FALSE(true);
81}
82
William A. Kennington III25511a12022-08-04 16:32:28 -070083} // namespace config
Gunnar Mills57d9c502018-09-14 14:42:34 -050084} // namespace network
85} // namespace phosphor