blob: 00653ced4a947a74689a862a40a47efdb7b18ad4 [file] [log] [blame]
Tom Joseph53279882021-04-28 06:29:13 -07001#include "common/test/mocked_utils.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05002#include "common/utils.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -05003#include "host-bmc/dbus_to_host_effecters.hpp"
Tom Joseph250c4752020-04-15 10:32:45 +05304
5#include <nlohmann/json.hpp>
6
Tom Joseph250c4752020-04-15 10:32:45 +05307#include <gtest/gtest.h>
8
9using namespace pldm::host_effecters;
10using namespace pldm::utils;
Tom Joseph250c4752020-04-15 10:32:45 +053011
12class MockHostEffecterParser : public HostEffecterParser
13{
14 public:
15 MockHostEffecterParser(int fd, const pldm_pdr* repo,
16 DBusHandler* const dbusHandler,
17 const std::string& jsonPath) :
Sampa Misrac0c79482021-06-02 08:01:54 -050018 HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr)
Tom Joseph250c4752020-04-15 10:32:45 +053019 {}
20
Pavithra Barithaya2af90d32023-07-10 04:47:32 -050021 MOCK_METHOD(int, setHostStateEffecter,
Tom Joseph250c4752020-04-15 10:32:45 +053022 (size_t, std::vector<set_effecter_state_field>&, uint16_t),
Pavithra Barithaya2af90d32023-07-10 04:47:32 -050023 (override));
Tom Joseph250c4752020-04-15 10:32:45 +053024
25 MOCK_METHOD(void, createHostEffecterMatch,
26 (const std::string&, const std::string&, size_t, size_t,
27 uint16_t),
Andrew Jefferyc3d27e02023-04-28 12:44:29 +093028 (override));
Tom Joseph250c4752020-04-15 10:32:45 +053029
30 const std::vector<EffecterInfo>& gethostEffecterInfo()
31 {
32 return hostEffecterInfo;
33 }
34};
35
36TEST(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
59TEST(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
73TEST(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}