blob: 6aa381160b57b72a28ce5a1641de2310090c9dbd [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 III84bfe672022-07-13 14:15:30 -07003#include <fmt/format.h>
4
Ratan Guptaed123a32017-06-15 09:07:31 +05305#include <exception>
Ratan Guptaed123a32017-06-15 09:07:31 +05306#include <fstream>
Gunnar Mills57d9c502018-09-14 14:42:34 -05007#include <phosphor-logging/elog-errors.hpp>
8#include <stdexcept>
William A. Kennington III84bfe672022-07-13 14:15:30 -07009#include <stdplus/gtest/tmp.hpp>
Patrick Venturea9733402019-01-07 13:27:01 -080010#include <xyz/openbmc_project/Common/error.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050011
William A. Kennington III25511a12022-08-04 16:32:28 -070012#include <gmock/gmock.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013#include <gtest/gtest.h>
Ratan Guptaed123a32017-06-15 09:07:31 +053014
15namespace phosphor
16{
17namespace network
18{
William A. Kennington III25511a12022-08-04 16:32:28 -070019namespace config
20{
21
22using testing::ElementsAre;
Ratan Guptaed123a32017-06-15 09:07:31 +053023
William A. Kennington III84bfe672022-07-13 14:15:30 -070024class TestConfigParser : public stdplus::gtest::TestWithTmp
Ratan Guptaed123a32017-06-15 09:07:31 +053025{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
William A. Kennington III25511a12022-08-04 16:32:28 -070027 std::string filename = fmt::format("{}/eth0.network", CaseTmpDir());
28 Parser parser;
29
30 void WriteSampleFile()
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 {
William A. Kennington III84bfe672022-07-13 14:15:30 -070032 std::ofstream filestream(filename);
William A. Kennington III25511a12022-08-04 16:32:28 -070033 filestream << "\n\n\n\nBad=key\n[Match]\n # K=v \nName =eth0\n"
34 << "[Network\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n"
35 << "[Network] a\nDHCP=false #hi\n\n\nDHCP = yes \n"
36 << " [ SEC ] \n'DHCP#'=\"#hi\"\nDHCP#=ho\n[Network]\n"
37 << "Key=val\nAddress=::/0\n[]\n=\nKey";
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 filestream.close();
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 }
Ratan Guptaed123a32017-06-15 09:07:31 +053040};
41
42TEST_F(TestConfigParser, ReadConfigDataFromFile)
43{
William A. Kennington III25511a12022-08-04 16:32:28 -070044 WriteSampleFile();
45 parser.setFile(filename);
Ratan Guptac27170a2017-11-22 15:44:42 +053046
William A. Kennington III25511a12022-08-04 16:32:28 -070047 EXPECT_THAT(parser.getValues("Match", "Name"), ElementsAre("eth0"));
48 EXPECT_THAT(parser.getValues("DHCP", "ClientIdentifier"),
49 ElementsAre("mac"));
50 EXPECT_THAT(parser.getValues("Blah", "nil"), ElementsAre());
51 EXPECT_THAT(parser.getValues("Network", "nil"), ElementsAre());
Ratan Guptaed123a32017-06-15 09:07:31 +053052}
53
William A. Kennington III25511a12022-08-04 16:32:28 -070054} // namespace config
Gunnar Mills57d9c502018-09-14 14:42:34 -050055} // namespace network
56} // namespace phosphor