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