blob: 4f3df8773db613835476b05c07314901db1d478f [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/platform.h"
2
Tom Joseph53279882021-04-28 06:29:13 -07003#include "common/test/mocked_utils.hpp"
George Liue53193f2020-02-24 09:23:26 +08004#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihallic682fe22020-03-04 00:42:54 -06005#include "libpldmresponder/platform.hpp"
George Liu36e81352020-07-01 14:40:30 +08006
7#include <sdbusplus/test/sdbus_mock.hpp>
Sampa Misra5fb37d52021-03-06 07:26:00 -06008#include <sdeventplus/event.hpp>
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05009
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050010#include <gtest/gtest.h>
11
12using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060013using namespace pldm::responder::platform;
14using namespace pldm::responder::pdr;
15using namespace pldm::responder::pdr_utils;
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050016
George Liu36e81352020-07-01 14:40:30 +080017using ::testing::_;
18using ::testing::Return;
19using ::testing::StrEq;
20
George Liu456c9a22020-01-13 11:36:22 +080021TEST(GeneratePDRByStateEffecter, testGoodJson)
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050022{
George Liu36e81352020-07-01 14:40:30 +080023 MockdBusHandler mockedUtils;
24 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
25 .Times(5)
26 .WillRepeatedly(Return("foo.bar"));
27
Deepak Kodihallic682fe22020-03-04 00:42:54 -060028 auto inPDRRepo = pldm_pdr_init();
29 auto outPDRRepo = pldm_pdr_init();
30 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -060031 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060032 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060033 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060034 Repo inRepo(inPDRRepo);
35 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050036
37 // 2 entries
Deepak Kodihallic682fe22020-03-04 00:42:54 -060038 ASSERT_EQ(outRepo.getRecordCount(), 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050039
40 // Check first PDR
George Liue53193f2020-02-24 09:23:26 +080041 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -050042 auto record2 = pdr::getRecordByHandle(outRepo, 2, e);
43 ASSERT_NE(record2, nullptr);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050044 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +080045 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050046
Sampa Misra12afe112020-05-25 11:40:44 -050047 ASSERT_EQ(pdr->hdr.record_handle, 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050048 ASSERT_EQ(pdr->hdr.version, 1);
49 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
50 ASSERT_EQ(pdr->hdr.record_change_num, 0);
Sampa Misraa2fa0702019-05-31 01:28:55 -050051 ASSERT_EQ(pdr->hdr.length, 23);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050052
Sampa Misra12afe112020-05-25 11:40:44 -050053 ASSERT_EQ(pdr->terminus_handle, BmcPldmTerminusHandle);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050054 ASSERT_EQ(pdr->effecter_id, 1);
55 ASSERT_EQ(pdr->entity_type, 33);
56 ASSERT_EQ(pdr->entity_instance, 0);
57 ASSERT_EQ(pdr->container_id, 0);
58 ASSERT_EQ(pdr->effecter_semantic_id, 0);
59 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
60 ASSERT_EQ(pdr->has_description_pdr, false);
Sampa Misraa2fa0702019-05-31 01:28:55 -050061 ASSERT_EQ(pdr->composite_effecter_count, 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050062 state_effecter_possible_states* states =
63 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
64 ASSERT_EQ(states->state_set_id, 196);
65 ASSERT_EQ(states->possible_states_size, 1);
66 bitfield8_t bf1{};
67 bf1.byte = 2;
68 ASSERT_EQ(states->states[0].byte, bf1.byte);
69
George Liu1ec85d42020-02-12 16:05:32 +080070 const auto& [dbusMappings1, dbusValMaps1] =
71 handler.getDbusObjMaps(pdr->effecter_id);
72 ASSERT_EQ(dbusMappings1[0].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +053073
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050074 // Check second PDR
Sampa Misra12afe112020-05-25 11:40:44 -050075 auto record3 = pdr::getRecordByHandle(outRepo, 3, e);
76 ASSERT_NE(record3, nullptr);
George Liue53193f2020-02-24 09:23:26 +080077 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050078
Sampa Misra12afe112020-05-25 11:40:44 -050079 ASSERT_EQ(pdr->hdr.record_handle, 3);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050080 ASSERT_EQ(pdr->hdr.version, 1);
81 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
82 ASSERT_EQ(pdr->hdr.record_change_num, 0);
83 ASSERT_EQ(pdr->hdr.length, 24);
84
Sampa Misra12afe112020-05-25 11:40:44 -050085 ASSERT_EQ(pdr->terminus_handle, BmcPldmTerminusHandle);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050086 ASSERT_EQ(pdr->effecter_id, 2);
87 ASSERT_EQ(pdr->entity_type, 100);
88 ASSERT_EQ(pdr->entity_instance, 0);
89 ASSERT_EQ(pdr->container_id, 0);
90 ASSERT_EQ(pdr->effecter_semantic_id, 0);
91 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
92 ASSERT_EQ(pdr->has_description_pdr, false);
93 ASSERT_EQ(pdr->composite_effecter_count, 2);
94 states =
95 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
96 ASSERT_EQ(states->state_set_id, 197);
97 ASSERT_EQ(states->possible_states_size, 1);
98 bf1.byte = 2;
99 ASSERT_EQ(states->states[0].byte, bf1.byte);
100 states = reinterpret_cast<state_effecter_possible_states*>(
101 pdr->possible_states + sizeof(state_effecter_possible_states));
102 ASSERT_EQ(states->state_set_id, 198);
103 ASSERT_EQ(states->possible_states_size, 2);
104 bitfield8_t bf2[2];
105 bf2[0].byte = 38;
106 bf2[1].byte = 128;
107 ASSERT_EQ(states->states[0].byte, bf2[0].byte);
108 ASSERT_EQ(states->states[1].byte, bf2[1].byte);
Deepak Kodihallid0782962019-05-07 16:36:47 +0530109
George Liu1ec85d42020-02-12 16:05:32 +0800110 const auto& [dbusMappings2, dbusValMaps2] =
111 handler.getDbusObjMaps(pdr->effecter_id);
112 ASSERT_EQ(dbusMappings2[0].objectPath, "/foo/bar");
George Liu36e81352020-07-01 14:40:30 +0800113 ASSERT_EQ(dbusMappings2[1].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +0530114
George Liu1ec85d42020-02-12 16:05:32 +0800115 ASSERT_THROW(handler.getDbusObjMaps(0xDEAD), std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600116
117 pldm_pdr_destroy(inPDRRepo);
118 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500119}
120
George Liu456c9a22020-01-13 11:36:22 +0800121TEST(GeneratePDRByNumericEffecter, testGoodJson)
122{
George Liu36e81352020-07-01 14:40:30 +0800123 MockdBusHandler mockedUtils;
124 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
125 .Times(5)
126 .WillRepeatedly(Return("foo.bar"));
127
George Liu456c9a22020-01-13 11:36:22 +0800128 auto inPDRRepo = pldm_pdr_init();
129 auto outPDRRepo = pldm_pdr_init();
130 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600131 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600132 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600133 nullptr, nullptr, nullptr, nullptr, event);
George Liu456c9a22020-01-13 11:36:22 +0800134 Repo inRepo(inPDRRepo);
135 getRepoByType(inRepo, outRepo, PLDM_NUMERIC_EFFECTER_PDR);
136
137 // 1 entries
138 ASSERT_EQ(outRepo.getRecordCount(), 1);
139
140 // Check first PDR
141 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500142 auto record = pdr::getRecordByHandle(outRepo, 4, e);
George Liu456c9a22020-01-13 11:36:22 +0800143 ASSERT_NE(record, nullptr);
144
145 pldm_numeric_effecter_value_pdr* pdr =
146 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
Sampa Misra12afe112020-05-25 11:40:44 -0500147 EXPECT_EQ(pdr->hdr.record_handle, 4);
George Liu456c9a22020-01-13 11:36:22 +0800148 EXPECT_EQ(pdr->hdr.version, 1);
149 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
150 EXPECT_EQ(pdr->hdr.record_change_num, 0);
151 EXPECT_EQ(pdr->hdr.length,
152 sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr));
153
154 EXPECT_EQ(pdr->effecter_id, 3);
155 EXPECT_EQ(pdr->effecter_data_size, 4);
156
157 const auto& [dbusMappings, dbusValMaps] =
158 handler.getDbusObjMaps(pdr->effecter_id);
159 EXPECT_EQ(dbusMappings[0].objectPath, "/foo/bar");
160 EXPECT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar");
161 EXPECT_EQ(dbusMappings[0].propertyName, "propertyName");
162 EXPECT_EQ(dbusMappings[0].propertyType, "uint64_t");
163
164 pldm_pdr_destroy(inPDRRepo);
165 pldm_pdr_destroy(outPDRRepo);
166}
167
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500168TEST(GeneratePDR, testMalformedJson)
169{
George Liu36e81352020-07-01 14:40:30 +0800170 MockdBusHandler mockedUtils;
171 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
172 .Times(5)
173 .WillRepeatedly(Return("foo.bar"));
174
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600175 auto inPDRRepo = pldm_pdr_init();
176 auto outPDRRepo = pldm_pdr_init();
177 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600178 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600179 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600180 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600181 Repo inRepo(inPDRRepo);
182 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
183
184 ASSERT_EQ(outRepo.getRecordCount(), 2);
George Liue53193f2020-02-24 09:23:26 +0800185 ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"),
Sampa Misraaa8ae722019-12-12 03:20:40 -0600186 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600187
188 pldm_pdr_destroy(inPDRRepo);
189 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500190}
Tom Joseph250c4752020-04-15 10:32:45 +0530191
192TEST(findStateEffecterId, goodJson)
193{
George Liu36e81352020-07-01 14:40:30 +0800194 MockdBusHandler mockedUtils;
195 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
196 .Times(5)
197 .WillRepeatedly(Return("foo.bar"));
198
Tom Joseph250c4752020-04-15 10:32:45 +0530199 auto inPDRRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600200 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600201 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600202 nullptr, nullptr, nullptr, nullptr, event);
Tom Joseph250c4752020-04-15 10:32:45 +0530203 uint16_t entityType = 33;
204 uint16_t entityInstance = 0;
205 uint16_t containerId = 0;
206 uint16_t stateSetId = 196;
207 auto effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
Sampa Misraa4a96162020-07-14 05:33:46 -0500208 containerId, stateSetId, true);
Tom Joseph250c4752020-04-15 10:32:45 +0530209 ASSERT_EQ(effecterId, 1);
210 stateSetId = 300;
211 effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
Sampa Misraa4a96162020-07-14 05:33:46 -0500212 containerId, stateSetId, true);
Tom Joseph250c4752020-04-15 10:32:45 +0530213 ASSERT_EQ(effecterId, PLDM_INVALID_EFFECTER_ID);
214 pldm_pdr_destroy(inPDRRepo);
George Liucae18662020-05-15 09:32:57 +0800215}