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