blob: 8dfe6f19cfcf8f22d235182b83eb76ab8f05735a [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08002#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05303#include "libpldmresponder/platform.hpp"
George Liu1e44c732020-02-28 20:20:06 +08004#include "mocked_utils.hpp"
5#include "utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05306
7#include <iostream>
8
George Liu1e44c732020-02-28 20:20:06 +08009using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053010using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060011using namespace pldm::responder::platform;
12using namespace pldm::responder::pdr;
13using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053014
15TEST(getPDR, testGoodPath)
16{
17 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
18 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080019 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053020 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
21
George Liue53193f2020-02-24 09:23:26 +080022 struct pldm_get_pdr_req* request =
23 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
24 request->request_count = 100;
25
Deepak Kodihallic682fe22020-03-04 00:42:54 -060026 auto pdrRepo = pldm_pdr_init();
27 Handler handler("./pdr_jsons/state_effecter/good", pdrRepo);
28 Repo repo(pdrRepo);
29 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080030 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053031 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
32
George Liue53193f2020-02-24 09:23:26 +080033 struct pldm_get_pdr_resp* resp =
34 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
35 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
36 ASSERT_EQ(2, resp->next_record_handle);
37 ASSERT_EQ(true, resp->response_count != 0);
38
39 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
40 ASSERT_EQ(hdr->record_handle, 1);
41 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060042
43 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053044}
45
46TEST(getPDR, testShortRead)
47{
48 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
49 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080050 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053051 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
52
George Liue53193f2020-02-24 09:23:26 +080053 struct pldm_get_pdr_req* request =
54 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
55 request->request_count = 1;
56
Deepak Kodihallic682fe22020-03-04 00:42:54 -060057 auto pdrRepo = pldm_pdr_init();
58 Handler handler("./pdr_jsons/state_effecter/good", pdrRepo);
59 Repo repo(pdrRepo);
60 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080061 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053062 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080063 struct pldm_get_pdr_resp* resp =
64 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
65 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
66 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060067 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053068}
69
70TEST(getPDR, testBadRecordHandle)
71{
72 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
73 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080074 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053075 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
76
George Liue53193f2020-02-24 09:23:26 +080077 struct pldm_get_pdr_req* request =
78 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
79 request->record_handle = 100000;
80 request->request_count = 1;
81
Deepak Kodihallic682fe22020-03-04 00:42:54 -060082 auto pdrRepo = pldm_pdr_init();
83 Handler handler("./pdr_jsons/state_effecter/good", pdrRepo);
84 Repo repo(pdrRepo);
85 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080086 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053087 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
88
89 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060090
91 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053092}
93
94TEST(getPDR, testNoNextRecord)
95{
96 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
97 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080098 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053099 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
100
George Liue53193f2020-02-24 09:23:26 +0800101 struct pldm_get_pdr_req* request =
102 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
103 request->record_handle = 1;
104
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600105 auto pdrRepo = pldm_pdr_init();
106 Handler handler("./pdr_jsons/state_effecter/good", pdrRepo);
107 Repo repo(pdrRepo);
108 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800109 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530110 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800111 struct pldm_get_pdr_resp* resp =
112 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
113 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
114 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600115
116 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530117}
118
119TEST(getPDR, testFindPDR)
120{
121 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
122 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800123 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530124 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
125
George Liue53193f2020-02-24 09:23:26 +0800126 struct pldm_get_pdr_req* request =
127 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
128 request->request_count = 100;
129
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600130 auto pdrRepo = pldm_pdr_init();
131 Handler handler("./pdr_jsons/state_effecter/good", pdrRepo);
132 Repo repo(pdrRepo);
133 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800134 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530135
136 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
137 // 100
138 bool found = false;
139 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530140 while (!found)
141 {
George Liue53193f2020-02-24 09:23:26 +0800142 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800143 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530144 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800145 struct pldm_get_pdr_resp* resp =
146 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
147 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530148
George Liue53193f2020-02-24 09:23:26 +0800149 handle = resp->next_record_handle; // point to the next pdr in case
150 // current is not what we want
151
152 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600153 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800154 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530155 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
156 {
157 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800158 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600159 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530160 if (pdr->entity_type == 100)
161 {
162 found = true;
163 // Rest of the PDR can be accessed as need be
164 break;
165 }
166 }
George Liue53193f2020-02-24 09:23:26 +0800167 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530168 {
169 break;
170 }
171 }
172 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600173
174 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530175}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500176
Sampa Misraa2fa0702019-05-31 01:28:55 -0500177TEST(setStateEffecterStatesHandler, testGoodRequest)
178{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600179 auto inPDRRepo = pldm_pdr_init();
180 auto outPDRRepo = pldm_pdr_init();
181 Repo outRepo(outPDRRepo);
182 Handler handler("./pdr_jsons/state_effecter/good", inPDRRepo);
183 Repo inRepo(inPDRRepo);
184 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800185 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600186 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +0800187 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500188 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800189 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500190 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
191
192 std::vector<set_effecter_state_field> stateField;
193 stateField.push_back({PLDM_REQUEST_SET, 1});
194 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800195 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800196 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500197
198 MockdBusHandler handlerObj;
George Liu1ec85d42020-02-12 16:05:32 +0800199 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
200 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800201
202 EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500203 .Times(2);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600204 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
205 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500206 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600207
208 pldm_pdr_destroy(inPDRRepo);
209 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500210}
211
212TEST(setStateEffecterStatesHandler, testBadRequest)
213{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600214 auto inPDRRepo = pldm_pdr_init();
215 auto outPDRRepo = pldm_pdr_init();
216 Repo outRepo(outPDRRepo);
217 Handler handler("./pdr_jsons/state_effecter/good", inPDRRepo);
218 Repo inRepo(inPDRRepo);
219 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800220 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600221 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +0800222 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500223 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800224 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500225 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
226
227 std::vector<set_effecter_state_field> stateField;
228 stateField.push_back({PLDM_REQUEST_SET, 3});
229 stateField.push_back({PLDM_REQUEST_SET, 4});
230
231 MockdBusHandler handlerObj;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600232 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
233 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500234 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
235
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600236 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x9,
237 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500238 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
239
240 stateField.push_back({PLDM_REQUEST_SET, 4});
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600241 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x1,
242 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500243 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
244
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600245 pldm_pdr_destroy(inPDRRepo);
246 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247}