1 #include "common/test/mocked_utils.hpp"
2 #include "common/utils.hpp"
3 #include "host-bmc/dbus_to_host_effecters.hpp"
4 
5 #include <nlohmann/json.hpp>
6 
7 #include <gtest/gtest.h>
8 
9 using namespace pldm::host_effecters;
10 using namespace pldm::utils;
11 
12 class MockHostEffecterParser : public HostEffecterParser
13 {
14   public:
15     MockHostEffecterParser(int fd, const pldm_pdr* repo,
16                            DBusHandler* const dbusHandler,
17                            const std::string& jsonPath) :
18         HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr)
19     {}
20 
21     MOCK_METHOD(int, setHostStateEffecter,
22                 (size_t, std::vector<set_effecter_state_field>&, uint16_t),
23                 (override));
24 
25     MOCK_METHOD(void, createHostEffecterMatch,
26                 (const std::string&, const std::string&, size_t, size_t,
27                  uint16_t),
28                 (override));
29 
30     const std::vector<EffecterInfo>& gethostEffecterInfo()
31     {
32         return hostEffecterInfo;
33     }
34 };
35 
36 TEST(HostEffecterParser, parseEffecterJsonGoodPath)
37 {
38     MockdBusHandler dbusHandler;
39     int sockfd{};
40     MockHostEffecterParser hostEffecterParserGood(sockfd, nullptr, &dbusHandler,
41                                                   "./host_effecter_jsons/good");
42     auto hostEffecterInfo = hostEffecterParserGood.gethostEffecterInfo();
43     ASSERT_EQ(hostEffecterInfo.size(), 1);
44     ASSERT_EQ(hostEffecterInfo[0].entityInstance, 0);
45     ASSERT_EQ(hostEffecterInfo[0].entityType, 33);
46     ASSERT_EQ(hostEffecterInfo[0].dbusInfo.size(), 1);
47     DBusEffecterMapping dbusInfo{
48         {"/xyz/openbmc_project/control/host0/boot",
49          "xyz.openbmc_project.Control.Boot.Mode", "BootMode", "string"},
50         {"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"},
51         {196, {2}}};
52     auto& temp = hostEffecterInfo[0].dbusInfo[0];
53     ASSERT_EQ(temp.dbusMap.objectPath == dbusInfo.dbusMap.objectPath, true);
54     ASSERT_EQ(temp.dbusMap.interface == dbusInfo.dbusMap.interface, true);
55     ASSERT_EQ(temp.dbusMap.propertyName == dbusInfo.dbusMap.propertyName, true);
56     ASSERT_EQ(temp.dbusMap.propertyType == dbusInfo.dbusMap.propertyType, true);
57 }
58 
59 TEST(HostEffecterParser, parseEffecterJsonBadPath)
60 {
61     MockdBusHandler dbusHandler;
62     int sockfd{};
63     MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
64                                               "./host_effecter_jsons/no_json");
65     ASSERT_THROW(
66         hostEffecterParser.parseEffecterJson("./host_effecter_jsons/no_json"),
67         std::exception);
68     ASSERT_THROW(
69         hostEffecterParser.parseEffecterJson("./host_effecter_jsons/malformed"),
70         std::exception);
71 }
72 
73 TEST(HostEffecterParser, findNewStateValue)
74 {
75     MockdBusHandler dbusHandler;
76     int sockfd{};
77     MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
78                                               "./host_effecter_jsons/good");
79 
80     PropertyValue val1{std::in_place_type<std::string>,
81                        "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"};
82     PropertyValue val2{std::in_place_type<std::string>,
83                        "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup"};
84     auto newState = hostEffecterParser.findNewStateValue(0, 0, val1);
85     ASSERT_EQ(newState, 2);
86 
87     ASSERT_THROW(hostEffecterParser.findNewStateValue(0, 0, val2),
88                  std::exception);
89 }
90