blob: 5c62e31f381dbaddfcd5d36e73bff5cc53bd29cd [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
7#include <iostream>
8
9#include <gtest/gtest.h>
10
11using namespace pldm::host_effecters;
12using namespace pldm::utils;
Tom Joseph250c4752020-04-15 10:32:45 +053013
14class MockHostEffecterParser : public HostEffecterParser
15{
16 public:
17 MockHostEffecterParser(int fd, const pldm_pdr* repo,
18 DBusHandler* const dbusHandler,
19 const std::string& jsonPath) :
Sampa Misrac0c79482021-06-02 08:01:54 -050020 HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr)
Tom Joseph250c4752020-04-15 10:32:45 +053021 {}
22
23 MOCK_METHOD(void, setHostStateEffecter,
24 (size_t, std::vector<set_effecter_state_field>&, uint16_t),
25 (const override));
26
27 MOCK_METHOD(void, createHostEffecterMatch,
28 (const std::string&, const std::string&, size_t, size_t,
29 uint16_t),
Andrew Jefferyc3d27e02023-04-28 12:44:29 +093030 (override));
Tom Joseph250c4752020-04-15 10:32:45 +053031
32 const std::vector<EffecterInfo>& gethostEffecterInfo()
33 {
34 return hostEffecterInfo;
35 }
36};
37
38TEST(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
61TEST(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
75TEST(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}