blob: f8b9bc7b622ee75630bbd6787b8f0b6b7207b57c [file] [log] [blame]
Sampa Misra0db1dfa2019-03-19 00:15:31 -05001#include <string.h>
2
3#include <array>
4
5#include "libpldm/base.h"
6#include "libpldm/platform.h"
7
8#include <gtest/gtest.h>
9
Zahed Hossain223a73d2019-07-04 12:46:18 -050010constexpr auto hdrSize = sizeof(pldm_msg_hdr);
11
Sampa Misra0db1dfa2019-03-19 00:15:31 -050012TEST(SetStateEffecterStates, testEncodeResponse)
13{
vkaverapa6575b82019-04-03 05:33:52 -050014 std::array<uint8_t,
15 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
16 responseMsg{};
17 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -050018 uint8_t completionCode = 0;
19
vkaverapa6575b82019-04-03 05:33:52 -050020 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response);
Sampa Misra0db1dfa2019-03-19 00:15:31 -050021
George Liu684a7162019-12-06 15:10:52 +080022 EXPECT_EQ(rc, PLDM_SUCCESS);
23 EXPECT_EQ(completionCode, response->payload[0]);
Sampa Misra0db1dfa2019-03-19 00:15:31 -050024}
25
vkaverap98a2c192019-04-03 05:33:52 -050026TEST(SetStateEffecterStates, testEncodeRequest)
27{
vkaverapa6575b82019-04-03 05:33:52 -050028 std::array<uint8_t,
29 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
30 requestMsg{};
31 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
32
vkaverap98a2c192019-04-03 05:33:52 -050033 uint16_t effecterId = 0x0A;
34 uint8_t compEffecterCnt = 0x2;
35 std::array<set_effecter_state_field, 8> stateField{};
36 stateField[0] = {PLDM_REQUEST_SET, 2};
37 stateField[1] = {PLDM_REQUEST_SET, 3};
38
vkaverap98a2c192019-04-03 05:33:52 -050039 auto rc = encode_set_state_effecter_states_req(
vkaverapa6575b82019-04-03 05:33:52 -050040 0, effecterId, compEffecterCnt, stateField.data(), request);
vkaverap98a2c192019-04-03 05:33:52 -050041
George Liu684a7162019-12-06 15:10:52 +080042 EXPECT_EQ(rc, PLDM_SUCCESS);
43 EXPECT_EQ(effecterId, request->payload[0]);
44 EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
45 EXPECT_EQ(stateField[0].set_request,
vkaverapa6575b82019-04-03 05:33:52 -050046 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
George Liu684a7162019-12-06 15:10:52 +080047 EXPECT_EQ(stateField[0].effecter_state,
vkaverapa6575b82019-04-03 05:33:52 -050048 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
49 sizeof(stateField[0].set_request)]);
George Liu684a7162019-12-06 15:10:52 +080050 EXPECT_EQ(stateField[1].set_request,
vkaverapa6575b82019-04-03 05:33:52 -050051 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
52 sizeof(stateField[0])]);
George Liu684a7162019-12-06 15:10:52 +080053 EXPECT_EQ(stateField[1].effecter_state,
vkaverapa6575b82019-04-03 05:33:52 -050054 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
55 sizeof(stateField[0]) +
56 sizeof(stateField[1].set_request)]);
vkaverap98a2c192019-04-03 05:33:52 -050057}
58
59TEST(SetStateEffecterStates, testGoodDecodeResponse)
60{
Zahed Hossain223a73d2019-07-04 12:46:18 -050061 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
vkaverap98a2c192019-04-03 05:33:52 -050062 responseMsg{};
63
vkaverap98a2c192019-04-03 05:33:52 -050064 uint8_t retcompletion_code = 0;
65
George Liu684a7162019-12-06 15:10:52 +080066 responseMsg[hdrSize] = PLDM_SUCCESS;
Zahed Hossain223a73d2019-07-04 12:46:18 -050067
68 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
vkaverap98a2c192019-04-03 05:33:52 -050069
vkaverapa6575b82019-04-03 05:33:52 -050070 auto rc = decode_set_state_effecter_states_resp(
Zahed Hossain223a73d2019-07-04 12:46:18 -050071 response, responseMsg.size() - hdrSize, &retcompletion_code);
vkaverap98a2c192019-04-03 05:33:52 -050072
George Liu684a7162019-12-06 15:10:52 +080073 EXPECT_EQ(rc, PLDM_SUCCESS);
74 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
vkaverap98a2c192019-04-03 05:33:52 -050075}
76
Sampa Misra0db1dfa2019-03-19 00:15:31 -050077TEST(SetStateEffecterStates, testGoodDecodeRequest)
78{
Zahed Hossain223a73d2019-07-04 12:46:18 -050079 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
80 requestMsg{};
Sampa Misra0db1dfa2019-03-19 00:15:31 -050081
Lei YU065cf762020-02-28 15:31:28 +080082 uint16_t effecterId = 0x32;
83 uint16_t effecterIdLE = htole16(effecterId);
Sampa Misra0db1dfa2019-03-19 00:15:31 -050084 uint8_t compEffecterCnt = 0x2;
85
86 std::array<set_effecter_state_field, 8> stateField{};
87 stateField[0] = {PLDM_REQUEST_SET, 3};
88 stateField[1] = {PLDM_REQUEST_SET, 4};
89
90 uint16_t retEffecterId = 0;
91 uint8_t retCompEffecterCnt = 0;
92
93 std::array<set_effecter_state_field, 8> retStateField{};
94
Lei YU065cf762020-02-28 15:31:28 +080095 memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE));
96 memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050097 sizeof(compEffecterCnt));
Lei YU065cf762020-02-28 15:31:28 +080098 memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050099 hdrSize,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500100 &stateField, sizeof(stateField));
101
Zahed Hossain223a73d2019-07-04 12:46:18 -0500102 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
103
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500104 auto rc = decode_set_state_effecter_states_req(
Zahed Hossain223a73d2019-07-04 12:46:18 -0500105 request, requestMsg.size() - hdrSize, &retEffecterId,
vkaverapa6575b82019-04-03 05:33:52 -0500106 &retCompEffecterCnt, retStateField.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500107
George Liu684a7162019-12-06 15:10:52 +0800108 EXPECT_EQ(rc, PLDM_SUCCESS);
109 EXPECT_EQ(effecterId, retEffecterId);
110 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt);
111 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request);
112 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
113 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request);
114 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500115}
116
117TEST(SetStateEffecterStates, testBadDecodeRequest)
118{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500119 const struct pldm_msg* msg = NULL;
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500120
vkaverapa6575b82019-04-03 05:33:52 -0500121 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
122 NULL, NULL);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500123
George Liu684a7162019-12-06 15:10:52 +0800124 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500125}
vkaverap98a2c192019-04-03 05:33:52 -0500126
127TEST(SetStateEffecterStates, testBadDecodeResponse)
128{
129 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
130 responseMsg{};
131
Zahed Hossain223a73d2019-07-04 12:46:18 -0500132 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
133
134 auto rc = decode_set_state_effecter_states_resp(response,
vkaverapa6575b82019-04-03 05:33:52 -0500135 responseMsg.size(), NULL);
vkaverap98a2c192019-04-03 05:33:52 -0500136
George Liu684a7162019-12-06 15:10:52 +0800137 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
vkaverap98a2c192019-04-03 05:33:52 -0500138}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500139
140TEST(GetPDR, testGoodEncodeResponse)
141{
142 uint8_t completionCode = 0;
143 uint32_t nextRecordHndl = 0x12;
144 uint32_t nextDataTransferHndl = 0x13;
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500145 uint8_t transferFlag = PLDM_END;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500146 uint16_t respCnt = 0x5;
147 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500148 uint8_t transferCRC = 6;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500149
150 // + size of record data and transfer CRC
151 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
152 recordData.size() + 1);
153 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
154
155 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
156 nextDataTransferHndl, transferFlag, respCnt,
157 recordData.data(), transferCRC, response);
158
George Liu684a7162019-12-06 15:10:52 +0800159 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500160 struct pldm_get_pdr_resp* resp =
161 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
162
George Liu684a7162019-12-06 15:10:52 +0800163 EXPECT_EQ(completionCode, resp->completion_code);
Lei YU065cf762020-02-28 15:31:28 +0800164 EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle));
165 EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle));
George Liu684a7162019-12-06 15:10:52 +0800166 EXPECT_EQ(transferFlag, resp->transfer_flag);
Lei YU065cf762020-02-28 15:31:28 +0800167 EXPECT_EQ(respCnt, le16toh(resp->response_count));
George Liu684a7162019-12-06 15:10:52 +0800168 EXPECT_EQ(0,
Sampa Misra7fcfb662019-05-08 13:13:53 -0500169 memcmp(recordData.data(), resp->record_data, recordData.size()));
George Liu684a7162019-12-06 15:10:52 +0800170 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500171 recordData.size()),
172 transferCRC);
173
174 transferFlag = PLDM_START_AND_END; // No CRC in this case
175 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
176 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
177 nextDataTransferHndl, transferFlag, respCnt,
178 recordData.data(), transferCRC, response);
George Liu684a7162019-12-06 15:10:52 +0800179 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500180}
181
182TEST(GetPDR, testBadEncodeResponse)
183{
184 uint32_t nextRecordHndl = 0x12;
185 uint32_t nextDataTransferHndl = 0x13;
186 uint8_t transferFlag = PLDM_START_AND_END;
187 uint16_t respCnt = 0x5;
188 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
189 uint8_t transferCRC = 0;
190
191 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
192 nextDataTransferHndl, transferFlag, respCnt,
193 recordData.data(), transferCRC, nullptr);
194
George Liu684a7162019-12-06 15:10:52 +0800195 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500196}
197
198TEST(GetPDR, testGoodDecodeRequest)
199{
200 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
201
202 uint32_t recordHndl = 0x32;
203 uint32_t dataTransferHndl = 0x11;
204 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
205 uint16_t requestCnt = 0x5;
Lei YU065cf762020-02-28 15:31:28 +0800206 uint16_t recordChangeNum = 0x01;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500207
208 uint32_t retRecordHndl = 0;
209 uint32_t retDataTransferHndl = 0;
210 uint8_t retTransferOpFlag = 0;
211 uint16_t retRequestCnt = 0;
212 uint16_t retRecordChangeNum = 0;
213
214 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
215 struct pldm_get_pdr_req* request =
216 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
217
Lei YU065cf762020-02-28 15:31:28 +0800218 request->record_handle = htole32(recordHndl);
219 request->data_transfer_handle = htole32(dataTransferHndl);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500220 request->transfer_op_flag = transferOpFlag;
Lei YU065cf762020-02-28 15:31:28 +0800221 request->request_count = htole16(requestCnt);
222 request->record_change_number = htole16(recordChangeNum);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500223
224 auto rc = decode_get_pdr_req(
225 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
226 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
227
George Liu684a7162019-12-06 15:10:52 +0800228 EXPECT_EQ(rc, PLDM_SUCCESS);
229 EXPECT_EQ(retRecordHndl, recordHndl);
230 EXPECT_EQ(retDataTransferHndl, dataTransferHndl);
231 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
232 EXPECT_EQ(retRequestCnt, requestCnt);
233 EXPECT_EQ(retRecordChangeNum, recordChangeNum);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500234}
235
236TEST(GetPDR, testBadDecodeRequest)
237{
238 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
239 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
240
241 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
242 NULL);
243
George Liu684a7162019-12-06 15:10:52 +0800244 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500245}
George Liu820a9a52019-11-26 14:43:59 +0800246
247TEST(GetPDR, testGoodEncodeRequest)
248{
249 uint32_t record_hndl = 0;
250 uint32_t data_transfer_hndl = 0;
251 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
252 uint16_t request_cnt = 20;
253 uint16_t record_chg_num = 0;
254
255 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
256 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
257
258 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
259 transfer_op_flag, request_cnt, record_chg_num,
260 request, PLDM_GET_PDR_REQ_BYTES);
261 EXPECT_EQ(rc, PLDM_SUCCESS);
262 struct pldm_get_pdr_req* req =
263 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
Lei YU065cf762020-02-28 15:31:28 +0800264 EXPECT_EQ(record_hndl, le32toh(req->record_handle));
265 EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle));
George Liu820a9a52019-11-26 14:43:59 +0800266 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
Lei YU065cf762020-02-28 15:31:28 +0800267 EXPECT_EQ(request_cnt, le16toh(req->request_count));
268 EXPECT_EQ(record_chg_num, le16toh(req->record_change_number));
George Liu820a9a52019-11-26 14:43:59 +0800269}
270
271TEST(GetPDR, testBadEncodeRequest)
272{
273 uint32_t record_hndl = 0;
274 uint32_t data_transfer_hndl = 0;
275 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
276 uint16_t request_cnt = 32;
277 uint16_t record_chg_num = 0;
278
279 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
280 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
281
282 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
283 transfer_op_flag, request_cnt, record_chg_num,
284 nullptr, PLDM_GET_PDR_REQ_BYTES);
285 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
286
287 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
288 transfer_op_flag, request_cnt, record_chg_num,
289 request, PLDM_GET_PDR_REQ_BYTES + 1);
290 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
291}
292
293TEST(GetPDR, testGoodDecodeResponse)
294{
295 const char* recordData = "123456789";
296 uint8_t completionCode = PLDM_SUCCESS;
297 uint32_t nextRecordHndl = 0;
298 uint32_t nextDataTransferHndl = 0;
299 uint8_t transferFlag = PLDM_END;
300 constexpr uint16_t respCnt = 9;
301 uint8_t transferCRC = 96;
302 size_t recordDataLength = 32;
303 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
304 sizeof(transferCRC)>
305 responseMsg{};
306
307 uint8_t retCompletionCode = 0;
308 uint8_t retRecordData[32] = {0};
309 uint32_t retNextRecordHndl = 0;
310 uint32_t retNextDataTransferHndl = 0;
311 uint8_t retTransferFlag = 0;
312 uint16_t retRespCnt = 0;
313 uint8_t retTransferCRC = 0;
314
315 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
316 struct pldm_get_pdr_resp* resp =
317 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
318 resp->completion_code = completionCode;
319 resp->next_record_handle = htole32(nextRecordHndl);
320 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
321 resp->transfer_flag = transferFlag;
322 resp->response_count = htole16(respCnt);
323 memcpy(resp->record_data, recordData, respCnt);
George Liu684a7162019-12-06 15:10:52 +0800324 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
George Liu820a9a52019-11-26 14:43:59 +0800325
326 auto rc = decode_get_pdr_resp(
327 response, responseMsg.size() - hdrSize, &retCompletionCode,
328 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
329 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
330 EXPECT_EQ(rc, PLDM_SUCCESS);
331 EXPECT_EQ(retCompletionCode, completionCode);
332 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
333 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
334 EXPECT_EQ(retTransferFlag, transferFlag);
335 EXPECT_EQ(retRespCnt, respCnt);
336 EXPECT_EQ(retTransferCRC, transferCRC);
337 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
338}
339
340TEST(GetPDR, testBadDecodeResponse)
341{
342 const char* recordData = "123456789";
343 uint8_t completionCode = PLDM_SUCCESS;
344 uint32_t nextRecordHndl = 0;
345 uint32_t nextDataTransferHndl = 0;
346 uint8_t transferFlag = PLDM_END;
347 constexpr uint16_t respCnt = 9;
348 uint8_t transferCRC = 96;
349 size_t recordDataLength = 32;
350 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
351 sizeof(transferCRC)>
352 responseMsg{};
353
354 uint8_t retCompletionCode = 0;
George Liu684a7162019-12-06 15:10:52 +0800355 uint8_t retRecordData[32] = {0};
George Liu820a9a52019-11-26 14:43:59 +0800356 uint32_t retNextRecordHndl = 0;
357 uint32_t retNextDataTransferHndl = 0;
358 uint8_t retTransferFlag = 0;
359 uint16_t retRespCnt = 0;
360 uint8_t retTransferCRC = 0;
361
362 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
363 struct pldm_get_pdr_resp* resp =
364 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
365 resp->completion_code = completionCode;
366 resp->next_record_handle = htole32(nextRecordHndl);
367 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
368 resp->transfer_flag = transferFlag;
369 resp->response_count = htole16(respCnt);
370 memcpy(resp->record_data, recordData, respCnt);
George Liu684a7162019-12-06 15:10:52 +0800371 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
George Liu820a9a52019-11-26 14:43:59 +0800372
373 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
374 NULL, NULL, NULL, NULL, NULL, 0, NULL);
375 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
376
377 rc = decode_get_pdr_resp(
378 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
379 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
380 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
381 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
382}
George Liu30b859f2020-01-07 15:03:22 +0800383
384TEST(SetNumericEffecterValue, testGoodDecodeRequest)
385{
386 std::array<uint8_t,
387 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3>
388 requestMsg{};
389
390 uint16_t effecter_id = 32768;
391 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32;
392 uint32_t effecter_value = 123456789;
393
394 uint16_t reteffecter_id;
395 uint8_t reteffecter_data_size;
396 uint8_t reteffecter_value[4];
397
398 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
399 struct pldm_set_numeric_effecter_value_req* request =
400 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
401 req->payload);
402
Lei YU065cf762020-02-28 15:31:28 +0800403 request->effecter_id = htole16(effecter_id);
George Liu30b859f2020-01-07 15:03:22 +0800404 request->effecter_data_size = effecter_data_size;
Lei YU065cf762020-02-28 15:31:28 +0800405 uint32_t effecter_value_le = htole32(effecter_value);
406 memcpy(request->effecter_value, &effecter_value_le,
407 sizeof(effecter_value_le));
George Liu30b859f2020-01-07 15:03:22 +0800408
409 auto rc = decode_set_numeric_effecter_value_req(
410 req, requestMsg.size() - hdrSize, &reteffecter_id,
Lei YU065cf762020-02-28 15:31:28 +0800411 &reteffecter_data_size, reteffecter_value);
George Liu30b859f2020-01-07 15:03:22 +0800412
George Liuda75abe2020-04-09 13:36:13 +0800413 uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value));
George Liu30b859f2020-01-07 15:03:22 +0800414 EXPECT_EQ(rc, PLDM_SUCCESS);
415 EXPECT_EQ(reteffecter_id, effecter_id);
416 EXPECT_EQ(reteffecter_data_size, effecter_data_size);
417 EXPECT_EQ(value, effecter_value);
418}
419
420TEST(SetNumericEffecterValue, testBadDecodeRequest)
421{
422 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES>
423 requestMsg{};
424
425 auto rc = decode_set_numeric_effecter_value_req(
426 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL);
427 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
428
429 uint16_t effecter_id = 0x10;
430 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
431 uint8_t effecter_value = 1;
432
433 uint16_t reteffecter_id;
434 uint8_t reteffecter_data_size;
435 uint8_t reteffecter_value[4];
436
437 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
438 struct pldm_set_numeric_effecter_value_req* request =
439 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
440 req->payload);
441
442 request->effecter_id = effecter_id;
443 request->effecter_data_size = effecter_data_size;
444 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
445
446 rc = decode_set_numeric_effecter_value_req(
447 req, requestMsg.size() - hdrSize - 1, &reteffecter_id,
448 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
449 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
450}
451
452TEST(SetNumericEffecterValue, testGoodEncodeRequest)
453{
454 uint16_t effecter_id = 0;
455 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
456 uint8_t effecter_value = 1;
457
458 std::vector<uint8_t> requestMsg(
459 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
460 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
461
462 auto rc = encode_set_numeric_effecter_value_req(
463 0, effecter_id, effecter_data_size,
464 reinterpret_cast<uint8_t*>(&effecter_value), request,
465 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
466 EXPECT_EQ(rc, PLDM_SUCCESS);
467
468 struct pldm_set_numeric_effecter_value_req* req =
469 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
470 request->payload);
471 EXPECT_EQ(effecter_id, req->effecter_id);
472 EXPECT_EQ(effecter_data_size, req->effecter_data_size);
473 EXPECT_EQ(effecter_value,
474 *(reinterpret_cast<uint8_t*>(&req->effecter_value[0])));
475}
476
477TEST(SetNumericEffecterValue, testBadEncodeRequest)
478{
479 std::vector<uint8_t> requestMsg(
480 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
481 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
482
483 auto rc = encode_set_numeric_effecter_value_req(
484 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
485 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
486
487 uint16_t effecter_value;
488 rc = encode_set_numeric_effecter_value_req(
489 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
490 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
491 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
492}
493
494TEST(SetNumericEffecterValue, testGoodDecodeResponse)
495{
496 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
497 responseMsg{};
498
499 uint8_t completion_code = 0xA0;
500
501 uint8_t retcompletion_code;
502
503 memcpy(responseMsg.data() + hdrSize, &completion_code,
504 sizeof(completion_code));
505
506 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
507
508 auto rc = decode_set_numeric_effecter_value_resp(
509 response, responseMsg.size() - hdrSize, &retcompletion_code);
510
511 EXPECT_EQ(rc, PLDM_SUCCESS);
512 EXPECT_EQ(completion_code, retcompletion_code);
513}
514
515TEST(SetNumericEffecterValue, testBadDecodeResponse)
516{
517 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
518 responseMsg{};
519
520 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
521
522 auto rc = decode_set_numeric_effecter_value_resp(response,
523 responseMsg.size(), NULL);
524
525 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
526}
527
528TEST(SetNumericEffecterValue, testGoodEncodeResponse)
529{
530 std::array<uint8_t, sizeof(pldm_msg_hdr) +
531 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
532 responseMsg{};
533 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
534 uint8_t completionCode = 0;
535
536 auto rc = encode_set_numeric_effecter_value_resp(
537 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
538
539 EXPECT_EQ(rc, PLDM_SUCCESS);
540 EXPECT_EQ(completionCode, response->payload[0]);
541}
542
543TEST(SetNumericEffecterValue, testBadEncodeResponse)
544{
545 auto rc = encode_set_numeric_effecter_value_resp(
546 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
547 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
548}
Jolie Ku3557bad2020-03-02 16:22:57 +0800549
550TEST(GetStateSensorReadings, testGoodEncodeResponse)
551{
552 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
553 responseMsg{};
554
555 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
556 uint8_t completionCode = 0;
557 uint8_t comp_sensorCnt = 0x2;
558
559 std::array<get_sensor_state_field, 2> stateField{};
560 stateField[0] = {ENABLED, NORMAL, WARNING, UNKNOWN};
561 stateField[1] = {FAILED, UPPERFATAL, UPPERCRITICAL, FATAL};
562
563 auto rc = encode_get_state_sensor_readings_resp(
564 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response);
565
566 struct pldm_get_state_sensor_readings_resp* resp =
567 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
568 response->payload);
569
570 EXPECT_EQ(rc, PLDM_SUCCESS);
571 EXPECT_EQ(completionCode, resp->completion_code);
572 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count);
573 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state);
574 EXPECT_EQ(stateField[0].present_state, resp->field->present_state);
575 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state);
576 EXPECT_EQ(stateField[0].event_state, resp->field->event_state);
577 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state);
578 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state);
579 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state);
580 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state);
581}
582
583TEST(GetStateSensorReadings, testBadEncodeResponse)
584{
585 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr,
586 nullptr);
587
588 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
589}
590
591TEST(GetStateSensorReadings, testGoodDecodeResponse)
592{
593 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
594 responseMsg{};
595
596 uint8_t completionCode = 0;
597 uint8_t comp_sensorCnt = 2;
598
599 std::array<get_sensor_state_field, 2> stateField{};
600 stateField[0] = {DISABLED, UNKNOWN, UNKNOWN, UNKNOWN};
601 stateField[1] = {ENABLED, LOWERFATAL, LOWERCRITICAL, WARNING};
602
603 uint8_t retcompletion_code = 0;
604 uint8_t retcomp_sensorCnt = 2;
605 std::array<get_sensor_state_field, 2> retstateField{};
606
607 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
608 struct pldm_get_state_sensor_readings_resp* resp =
609 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
610 response->payload);
611
612 resp->completion_code = completionCode;
613 resp->comp_sensor_count = comp_sensorCnt;
614 memcpy(resp->field, &stateField,
615 (sizeof(get_sensor_state_field) * comp_sensorCnt));
616
617 auto rc = decode_get_state_sensor_readings_resp(
618 response, responseMsg.size() - hdrSize, &retcompletion_code,
619 &retcomp_sensorCnt, retstateField.data());
620
621 EXPECT_EQ(rc, PLDM_SUCCESS);
622 EXPECT_EQ(completionCode, retcompletion_code);
623 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt);
624 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state);
625 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state);
626 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state);
627 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state);
628 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state);
629 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state);
630 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state);
631 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state);
632}
633
634TEST(GetStateSensorReadings, testBadDecodeResponse)
635{
636 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
637 responseMsg{};
638
639 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
640
641 auto rc = decode_get_state_sensor_readings_resp(
642 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr);
643
644 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
645
646 uint8_t completionCode = 0;
647 uint8_t comp_sensorCnt = 1;
648
649 std::array<get_sensor_state_field, 1> stateField{};
650 stateField[0] = {ENABLED, UPPERFATAL, UPPERCRITICAL, WARNING};
651
652 uint8_t retcompletion_code = 0;
653 uint8_t retcomp_sensorCnt = 0;
654 std::array<get_sensor_state_field, 1> retstateField{};
655
656 struct pldm_get_state_sensor_readings_resp* resp =
657 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
658 response->payload);
659
660 resp->completion_code = completionCode;
661 resp->comp_sensor_count = comp_sensorCnt;
662 memcpy(resp->field, &stateField,
663 (sizeof(get_sensor_state_field) * comp_sensorCnt));
664
665 rc = decode_get_state_sensor_readings_resp(
666 response, responseMsg.size() - hdrSize, &retcompletion_code,
667 &retcomp_sensorCnt, retstateField.data());
668
669 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
670}
671
672TEST(GetStateSensorReadings, testGoodEncodeRequest)
673{
674 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
675 requestMsg{};
676
677 uint16_t sensorId = 0xAB;
678 bitfield8_t sensorRearm;
679 sensorRearm.byte = 0x03;
680
681 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
682 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0,
683 request);
684
685 struct pldm_get_state_sensor_readings_req* req =
686 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
687 request->payload);
688
689 EXPECT_EQ(rc, PLDM_SUCCESS);
690 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
691 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte);
692}
693
694TEST(GetStateSensorReadings, testBadEncodeRequest)
695{
696 bitfield8_t sensorRearm;
697 sensorRearm.byte = 0x0;
698
699 auto rc =
700 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr);
701
702 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
703}
704
705TEST(GetStateSensorReadings, testGoodDecodeRequest)
706{
707 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
708 requestMsg{};
709
710 uint16_t sensorId = 0xCD;
711 bitfield8_t sensorRearm;
712 sensorRearm.byte = 0x10;
713
714 uint16_t retsensorId;
715 bitfield8_t retsensorRearm;
716 uint8_t retreserved;
717
718 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
719
720 struct pldm_get_state_sensor_readings_req* req =
721 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
722 request->payload);
723
724 req->sensor_id = htole16(sensorId);
725 req->sensor_rearm.byte = sensorRearm.byte;
726
727 auto rc = decode_get_state_sensor_readings_req(
728 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm,
729 &retreserved);
730
731 EXPECT_EQ(rc, PLDM_SUCCESS);
732 EXPECT_EQ(sensorId, retsensorId);
733 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte);
734 EXPECT_EQ(0, retreserved);
735}
736
737TEST(GetStateSensorReadings, testBadDecodeRequest)
738{
739 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
740 requestMsg{};
741
742 auto rc = decode_get_state_sensor_readings_req(
743 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr);
744
745 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
746 uint16_t sensorId = 0x11;
747 bitfield8_t sensorRearm;
748 sensorRearm.byte = 0x04;
749
750 uint16_t retsensorId;
751 bitfield8_t retsensorRearm;
752 uint8_t retreserved;
753
754 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
755
756 struct pldm_get_state_sensor_readings_req* req =
757 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
758 request->payload);
759
760 req->sensor_id = htole16(sensorId);
761 req->sensor_rearm.byte = sensorRearm.byte;
762
763 rc = decode_get_state_sensor_readings_req(
764 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm,
765 &retreserved);
766
767 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
768}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600769
770TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
771{
772 std::array<uint8_t,
773 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
774 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES>
775 requestMsg{};
776
777 uint8_t retFormatVersion = 0;
778 uint8_t retTid = 0;
779 uint8_t retEventClass = 0;
780 size_t retEventDataOffset = 0;
781
782 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
783 struct pldm_platform_event_message_req* request =
784 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload);
785
786 uint8_t formatVersion = 0x01;
787 uint8_t tid = 0x02;
788 // Sensor Event
789 uint8_t eventClass = 0x00;
790
791 request->format_version = formatVersion;
792 request->tid = tid;
793 request->event_class = eventClass;
794 size_t eventDataOffset =
795 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass);
796
797 auto rc = decode_platform_event_message_req(
798 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid,
799 &retEventClass, &retEventDataOffset);
800
801 EXPECT_EQ(rc, PLDM_SUCCESS);
802 EXPECT_EQ(retFormatVersion, formatVersion);
803 EXPECT_EQ(retTid, tid);
804 EXPECT_EQ(retEventClass, eventClass);
805 EXPECT_EQ(retEventDataOffset, eventDataOffset);
806}
807
808TEST(PlatformEventMessage, testBadDecodeRequest)
809{
810 const struct pldm_msg* msg = NULL;
811 std::array<uint8_t,
812 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
813 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
814 requestMsg{};
815 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
816 uint8_t retFormatVersion;
817 uint8_t retTid = 0;
818 uint8_t retEventClass = 0;
819 size_t retEventDataOffset;
820
821 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL,
822 NULL, NULL);
823 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
824
825 rc = decode_platform_event_message_req(
826 req,
827 requestMsg.size() - hdrSize -
828 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES,
829 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset);
830 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
831}
832
833TEST(PlatformEventMessage, testGoodEncodeResponse)
834{
835 std::array<uint8_t,
836 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
837 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
838 responseMsg{};
839 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
840 uint8_t completionCode = 0;
841 uint8_t instanceId = 0x01;
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500842 uint8_t platformEventStatus = 0x01;
Zahed Hossaind4abab12020-02-06 03:36:43 -0600843
844 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500845 platformEventStatus, response);
Zahed Hossaind4abab12020-02-06 03:36:43 -0600846
847 EXPECT_EQ(rc, PLDM_SUCCESS);
848 EXPECT_EQ(completionCode, response->payload[0]);
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500849 EXPECT_EQ(platformEventStatus, response->payload[1]);
Zahed Hossaind4abab12020-02-06 03:36:43 -0600850}
851
852TEST(PlatformEventMessage, testBadEncodeResponse)
853{
854 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL);
855 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
856}
Zahed Hossain1c861712020-03-04 08:55:19 -0600857
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500858TEST(PlatformEventMessage, testGoodEncodeRequest)
859{
860 uint8_t formatVersion = 0x01;
861 uint8_t Tid = 0x03;
862 uint8_t eventClass = 0x00;
863 uint8_t eventData = 34;
864
865 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
866 sizeof(eventData)>
867 requestMsg{};
868
869 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
870 auto rc = encode_platform_event_message_req(
871 0, formatVersion, Tid, eventClass,
872 reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request);
873
874 struct pldm_platform_event_message_req* req =
875 reinterpret_cast<struct pldm_platform_event_message_req*>(
876 request->payload);
877
878 EXPECT_EQ(rc, PLDM_SUCCESS);
879 EXPECT_EQ(formatVersion, req->format_version);
880 EXPECT_EQ(Tid, req->tid);
881 EXPECT_EQ(eventClass, req->event_class);
882 EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData)));
883}
884
885TEST(PlatformEventMessage, testBadEncodeRequest)
886{
887 uint8_t Tid = 0x03;
888 uint8_t eventClass = 0x00;
889 uint8_t eventData = 34;
890 uint8_t formatVersion = 0x01;
891
892 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
893 sizeof(eventData)>
894 requestMsg{};
895 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
896 auto rc = encode_platform_event_message_req(
897 0, formatVersion, Tid, eventClass,
898 reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), nullptr);
899
900 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
901 rc = encode_platform_event_message_req(
902 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
903 sizeof(eventData), request);
904 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
905 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
906 nullptr, 0, request);
907 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
908}
909
910TEST(PlatformEventMessage, testGoodDecodeResponse)
911{
912 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
913 responseMsg{};
914
915 uint8_t completionCode = PLDM_SUCCESS;
916 uint8_t platformEventStatus = 0x01;
917
918 uint8_t retcompletionCode;
919 uint8_t retplatformEventStatus;
920
921 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
922 struct pldm_platform_event_message_resp* resp =
923 reinterpret_cast<struct pldm_platform_event_message_resp*>(
924 response->payload);
925
926 resp->completion_code = completionCode;
927 resp->platform_event_status = platformEventStatus;
928
929 auto rc = decode_platform_event_message_resp(
930 response, responseMsg.size() - hdrSize, &retcompletionCode,
931 &retplatformEventStatus);
932
933 EXPECT_EQ(rc, PLDM_SUCCESS);
934 EXPECT_EQ(completionCode, retcompletionCode);
935 EXPECT_EQ(platformEventStatus, retplatformEventStatus);
936}
937
938TEST(PlatformEventMessage, testBadDecodeResponse)
939{
940 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
941 responseMsg{};
942
943 uint8_t completionCode = PLDM_SUCCESS;
944 uint8_t platformEventStatus = 0x01;
945
946 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
947 struct pldm_platform_event_message_resp* resp =
948 reinterpret_cast<struct pldm_platform_event_message_resp*>(
949 response->payload);
950 resp->completion_code = completionCode;
951 resp->platform_event_status = platformEventStatus;
952
953 auto rc = decode_platform_event_message_resp(
954 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
955
956 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
957
958 rc = decode_platform_event_message_resp(
959 response, responseMsg.size() - hdrSize - 1, &completionCode,
960 &platformEventStatus);
961
962 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
963}
964
Zahed Hossain1c861712020-03-04 08:55:19 -0600965TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
966{
967 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +
968 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
969 eventDataArr{};
970 uint16_t sensorId = 0x1234;
971 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE;
972
973 struct pldm_sensor_event_data* eventData =
974 (struct pldm_sensor_event_data*)eventDataArr.data();
975 eventData->sensor_id = sensorId;
976 eventData->sensor_event_class_type = sensorEventClassType;
977
978 size_t retSensorOpDataOffset;
979 uint16_t retSensorId = 0;
980 uint8_t retSensorEventClassType;
981 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType);
982 auto rc = decode_sensor_event_data(
983 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(),
984 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
985 EXPECT_EQ(rc, PLDM_SUCCESS);
986 EXPECT_EQ(retSensorId, sensorId);
987 EXPECT_EQ(retSensorEventClassType, sensorEventClassType);
988 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset);
989}
990
991TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest)
992{
993
994 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH +
995 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
996 eventDataArr{};
997
998 struct pldm_sensor_event_data* eventData =
999 (struct pldm_sensor_event_data*)eventDataArr.data();
1000
1001 size_t retSensorOpDataOffset;
1002 uint16_t retSensorId = 0;
1003 uint8_t retSensorEventClassType;
1004 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId,
1005 &retSensorEventClassType,
1006 &retSensorOpDataOffset);
1007 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1008
1009 rc = decode_sensor_event_data(
1010 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1011 eventDataArr.size() -
1012 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH,
1013 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1014 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1015
1016 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
1017
1018 rc = decode_sensor_event_data(
1019 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1020 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1021 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1022
1023 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
1024 rc = decode_sensor_event_data(
1025 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1026 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1027 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1028
1029 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE;
1030 rc = decode_sensor_event_data(
1031 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1032 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType,
1033 &retSensorOpDataOffset);
1034 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1035}
1036
1037TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
1038{
1039 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1040 eventDataArr{};
1041
1042 struct pldm_sensor_event_sensor_op_state* sensorData =
1043 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
1044 uint8_t presentState = PLDM_SENSOR_ENABLED;
1045 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
1046 sensorData->present_op_state = presentState;
1047 sensorData->previous_op_state = previousState;
1048
1049 uint8_t retPresentState;
1050 uint8_t retPreviousState;
1051 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
1052 eventDataArr.size(), &retPresentState,
1053 &retPreviousState);
1054 EXPECT_EQ(rc, PLDM_SUCCESS);
1055 EXPECT_EQ(retPresentState, presentState);
1056 EXPECT_EQ(retPreviousState, previousState);
1057}
1058
1059TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
1060{
1061 uint8_t presentOpState;
1062 uint8_t previousOpState;
1063 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
1064 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
1065 &previousOpState);
1066 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1067
1068 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1069 sensorData{};
1070 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1071 sensorDataLength + 1, &presentOpState,
1072 &previousOpState);
1073 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1074
1075 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1076 sensorDataLength, nullptr, &previousOpState);
1077 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1078}
1079
1080TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
1081{
1082 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1083 eventDataArr{};
1084
1085 struct pldm_sensor_event_state_sensor_state* sensorData =
1086 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
1087 uint8_t sensorOffset = 0x02;
1088 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1089 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1090 sensorData->sensor_offset = sensorOffset;
1091 sensorData->event_state = eventState;
1092 sensorData->previous_event_state = previousEventState;
1093
1094 uint8_t retSensorOffset;
1095 uint8_t retEventState;
1096 uint8_t retPreviousState;
1097 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1098 eventDataArr.size(), &retSensorOffset,
1099 &retEventState, &retPreviousState);
1100 EXPECT_EQ(rc, PLDM_SUCCESS);
1101 EXPECT_EQ(retSensorOffset, sensorOffset);
1102 EXPECT_EQ(retEventState, eventState);
1103 EXPECT_EQ(retPreviousState, previousEventState);
1104}
1105
1106TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
1107{
1108 uint8_t sensorOffset;
1109 uint8_t eventState;
1110 uint8_t previousEventState;
1111 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
1112 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
1113 &eventState, &previousEventState);
1114 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1115
1116 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1117 sensorData{};
1118 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1119 sensorDataLength - 1, &sensorOffset,
1120 &eventState, &previousEventState);
1121 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1122
1123 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1124 sensorDataLength, &sensorOffset, nullptr,
1125 &previousEventState);
1126 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1127}
1128
1129TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
1130{
1131 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1132 eventDataArr{};
1133 struct pldm_sensor_event_numeric_sensor_state* sensorData =
1134 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
1135
1136 size_t sensorDataLength =
1137 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
1138 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1139 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1140 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
1141 uint32_t presentReading = 305441741;
1142 sensorData->event_state = eventState;
1143 sensorData->previous_event_state = previousEventState;
1144 sensorData->sensor_data_size = sensorDataSize;
1145 sensorData->present_reading[3] =
1146 static_cast<uint8_t>(htole32(presentReading) & (0x000000ff));
1147 sensorData->present_reading[2] =
1148 static_cast<uint8_t>((htole32(presentReading) & (0x0000ff00)) >> 8);
1149 sensorData->present_reading[1] =
1150 static_cast<uint8_t>((htole32(presentReading) & (0x00ff0000)) >> 16);
1151 sensorData->present_reading[0] =
1152 static_cast<uint8_t>((htole32(presentReading) & (0xff000000)) >> 24);
1153
1154 uint8_t retEventState;
1155 uint8_t retPreviousEventState;
1156 uint8_t retSensorDataSize;
1157 uint32_t retPresentReading;
1158
1159 auto rc = decode_numeric_sensor_data(
1160 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
1161 &retEventState, &retPreviousEventState, &retSensorDataSize,
1162 &retPresentReading);
1163 EXPECT_EQ(rc, PLDM_SUCCESS);
1164 EXPECT_EQ(retEventState, eventState);
1165 EXPECT_EQ(retPreviousEventState, previousEventState);
1166 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1167 EXPECT_EQ(retPresentReading, presentReading);
1168
1169 int16_t presentReadingNew = -31432;
1170 sensorData->present_reading[1] =
1171 static_cast<uint8_t>(htole16(presentReadingNew) & (0x000000ff));
1172 sensorData->present_reading[0] =
1173 static_cast<uint8_t>((htole16(presentReadingNew) & (0x0000ff00)) >> 8);
1174 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
1175 sensorData->sensor_data_size = sensorDataSize;
1176 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
1177
1178 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1179 sensorDataLength, &retEventState,
1180 &retPreviousEventState, &retSensorDataSize,
1181 &retPresentReading);
1182 EXPECT_EQ(rc, PLDM_SUCCESS);
1183 EXPECT_EQ(retEventState, eventState);
1184 EXPECT_EQ(retPreviousEventState, previousEventState);
1185 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1186 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
1187}
1188
1189TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
1190{
1191 uint8_t eventState;
1192 uint8_t previousEventState;
1193 uint8_t sensorDataSize;
1194 uint32_t presentReading;
1195 size_t sensorDataLength =
1196 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
1197 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
1198 &previousEventState, &sensorDataSize,
1199 &presentReading);
1200 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1201
1202 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1203 sensorData{};
1204 rc = decode_numeric_sensor_data(
1205 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
1206 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1207 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1208
1209 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
1210 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
1211 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
1212 rc = decode_numeric_sensor_data(
1213 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1214 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1215 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1216
1217 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
1218 rc = decode_numeric_sensor_data(
1219 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1220 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1221 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1222
1223 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT32;
1224 rc = decode_numeric_sensor_data(
1225 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
1226 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1227 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1228}
Jolie Ku6787f172020-03-19 11:15:53 +08001229
1230TEST(GetNumericEffecterValue, testGoodEncodeRequest)
1231{
1232 std::vector<uint8_t> requestMsg(hdrSize +
1233 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
1234
1235 uint16_t effecter_id = 0xAB01;
1236
1237 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1238
1239 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
1240
1241 struct pldm_get_numeric_effecter_value_req* req =
1242 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1243 request->payload);
1244
1245 EXPECT_EQ(rc, PLDM_SUCCESS);
1246 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
1247}
1248
1249TEST(GetNumericEffecterValue, testBadEncodeRequest)
1250{
1251 std::vector<uint8_t> requestMsg(
1252 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
1253
1254 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
1255 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1256}
1257
1258TEST(GetNumericEffecterValue, testGoodDecodeRequest)
1259{
1260 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1261 requestMsg{};
1262
1263 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1264 struct pldm_get_numeric_effecter_value_req* req =
1265 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1266 request->payload);
1267
1268 uint16_t effecter_id = 0x12AB;
1269 req->effecter_id = htole16(effecter_id);
1270
1271 uint16_t reteffecter_id;
1272
1273 auto rc = decode_get_numeric_effecter_value_req(
1274 request, requestMsg.size() - hdrSize, &reteffecter_id);
1275
1276 EXPECT_EQ(rc, PLDM_SUCCESS);
1277 EXPECT_EQ(effecter_id, reteffecter_id);
1278}
1279
1280TEST(GetNumericEffecterValue, testBadDecodeRequest)
1281{
1282 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1283 requestMsg{};
1284
1285 auto rc = decode_get_numeric_effecter_value_req(
1286 nullptr, requestMsg.size() - hdrSize, nullptr);
1287
1288 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1289
1290 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1291 struct pldm_set_numeric_effecter_value_req* req =
1292 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
1293 request->payload);
1294
1295 uint16_t effecter_id = 0x1A;
1296 req->effecter_id = htole16(effecter_id);
1297 uint16_t reteffecter_id;
1298
1299 rc = decode_get_numeric_effecter_value_req(
1300 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
1301
1302 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1303}
1304
1305TEST(GetNumericEffecterValue, testGoodEncodeResponse)
1306{
1307 uint8_t completionCode = 0;
1308 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
1309 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1310 uint32_t pendingValue = 0x12345678;
1311 uint32_t presentValue = 0xABCDEF00;
1312
1313 std::array<uint8_t,
1314 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1315 responseMsg{};
1316 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1317
1318 uint32_t pendingValue_le = htole32(pendingValue);
1319 uint32_t presentValue_le = htole32(presentValue);
1320
1321 auto rc = encode_get_numeric_effecter_value_resp(
1322 0, completionCode, effecter_dataSize, effecter_operState,
1323 reinterpret_cast<uint8_t*>(&pendingValue_le),
1324 reinterpret_cast<uint8_t*>(&presentValue_le), response,
1325 responseMsg.size() - hdrSize);
1326
1327 struct pldm_get_numeric_effecter_value_resp* resp =
1328 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1329 response->payload);
1330
1331 EXPECT_EQ(rc, PLDM_SUCCESS);
1332 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
1333 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
1334 EXPECT_EQ(pendingValue, le32toh(*(reinterpret_cast<uint32_t*>(
1335 &resp->pending_and_present_values[0]))));
1336 EXPECT_EQ(presentValue, le32toh(*(reinterpret_cast<uint32_t*>(
1337 &resp->pending_and_present_values[4]))));
1338}
1339
1340TEST(GetNumericEffecterValue, testBadEncodeResponse)
1341{
1342 std::array<uint8_t,
1343 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1344 responseMsg{};
1345 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1346
1347 uint8_t pendingValue = 0x01;
1348 uint8_t presentValue = 0x02;
1349
1350 auto rc = encode_get_numeric_effecter_value_resp(
1351 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
1352 responseMsg.size() - hdrSize);
1353 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1354
1355 rc = encode_get_numeric_effecter_value_resp(
1356 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
1357 reinterpret_cast<uint8_t*>(&presentValue), response,
1358 responseMsg.size() - hdrSize);
1359 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1360
1361 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1362 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
1363
1364 rc = encode_get_numeric_effecter_value_resp(
1365 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
1366 reinterpret_cast<uint8_t*>(&pendingValue),
1367 reinterpret_cast<uint8_t*>(&presentValue), response,
1368 responseMsg.size() - hdrSize);
1369 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1370}
1371
1372TEST(GetNumericEffecterValue, testGoodDecodeResponse)
1373{
1374 std::array<uint8_t,
1375 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1376 responseMsg{};
1377
1378 uint8_t completionCode = 0;
1379 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
1380 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1381 uint16_t pendingValue = 0x4321;
1382 uint16_t presentValue = 0xDCBA;
1383
1384 uint8_t retcompletionCode;
1385 uint8_t reteffecter_dataSize;
1386 uint8_t reteffecter_operState;
1387 uint8_t retpendingValue[2];
1388 uint8_t retpresentValue[2];
1389
1390 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1391 struct pldm_get_numeric_effecter_value_resp* resp =
1392 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1393 response->payload);
1394
1395 resp->completion_code = completionCode;
1396 resp->effecter_data_size = effecter_dataSize;
1397 resp->effecter_oper_state = effecter_operState;
1398
1399 uint16_t pendingValue_le = htole16(pendingValue);
1400 memcpy(resp->pending_and_present_values, &pendingValue_le,
1401 sizeof(pendingValue_le));
1402 uint16_t presentValue_le = htole16(presentValue);
1403 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1404 sizeof(presentValue_le));
1405
1406 auto rc = decode_get_numeric_effecter_value_resp(
1407 response, responseMsg.size() - hdrSize, &retcompletionCode,
1408 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1409 retpresentValue);
1410
1411 uint16_t retpending_value =
1412 le16toh(*(reinterpret_cast<uint16_t*>(retpendingValue)));
1413 uint16_t retpresent_value =
1414 le16toh(*(reinterpret_cast<uint16_t*>(retpresentValue)));
1415
1416 EXPECT_EQ(rc, PLDM_SUCCESS);
1417 EXPECT_EQ(completionCode, retcompletionCode);
1418 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
1419 EXPECT_EQ(effecter_operState, reteffecter_operState);
1420 EXPECT_EQ(pendingValue, retpending_value);
1421 EXPECT_EQ(presentValue, retpresent_value);
1422}
1423
1424TEST(GetNumericEffecterValue, testBadDecodeResponse)
1425{
1426 std::array<uint8_t,
1427 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1428 responseMsg{};
1429
1430 auto rc = decode_get_numeric_effecter_value_resp(
1431 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
1432 nullptr, nullptr);
1433
1434 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1435
1436 uint8_t completionCode = 0;
1437 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
1438 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
1439 uint16_t pendingValue = 0x5678;
1440 uint16_t presentValue = 0xCDEF;
1441
1442 uint8_t retcompletionCode;
1443 uint8_t reteffecter_dataSize;
1444 uint8_t reteffecter_operState;
1445 uint8_t retpendingValue[2];
1446 uint8_t retpresentValue[2];
1447
1448 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1449 struct pldm_get_numeric_effecter_value_resp* resp =
1450 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1451 response->payload);
1452
1453 resp->completion_code = completionCode;
1454 resp->effecter_data_size = effecter_dataSize;
1455 resp->effecter_oper_state = effecter_operState;
1456
1457 uint16_t pendingValue_le = htole16(pendingValue);
1458 memcpy(resp->pending_and_present_values, &pendingValue_le,
1459 sizeof(pendingValue_le));
1460 uint16_t presentValue_le = htole16(presentValue);
1461 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1462 sizeof(presentValue_le));
1463
1464 rc = decode_get_numeric_effecter_value_resp(
1465 response, responseMsg.size() - hdrSize, &retcompletionCode,
1466 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1467 retpresentValue);
1468
1469 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1470}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001471
1472TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
1473{
1474 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
1475 const uint8_t numberOfChangeRecords = 2;
1476 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
1477 const uint8_t numberOfChangeEntries1 = 2;
1478 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
1479 {0x00000000, 0x12345678}};
1480 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
1481 const uint8_t numberOfChangeEntries2 = 5;
1482 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
1483 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
1484 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
1485 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
1486 numberOfChangeRecords +
1487 (numberOfChangeEntries1 + numberOfChangeEntries2) *
1488 sizeof(uint32_t)>
1489 eventDataArr{};
1490
1491 struct pldm_pdr_repository_chg_event_data* eventData =
1492 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
1493 eventDataArr.data());
1494 eventData->event_data_format = eventDataFormat;
1495 eventData->number_of_change_records = numberOfChangeRecords;
1496 struct pldm_pdr_repository_change_record_data* changeRecord1 =
1497 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1498 eventData->change_records);
1499 changeRecord1->event_data_operation = eventDataOperation1;
1500 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
1501 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
1502 changeRecordArr1.size() * sizeof(uint32_t));
1503 struct pldm_pdr_repository_change_record_data* changeRecord2 =
1504 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1505 eventData->change_records +
1506 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
1507 (changeRecordArr1.size() * sizeof(uint32_t)));
1508 changeRecord2->event_data_operation = eventDataOperation2;
1509 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
1510 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
1511 changeRecordArr2.size() * sizeof(uint32_t));
1512
1513 uint8_t retEventDataFormat{};
1514 uint8_t retNumberOfChangeRecords{};
1515 size_t retChangeRecordDataOffset{0};
1516 auto rc = decode_pldm_pdr_repository_chg_event_data(
1517 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
1518 &retEventDataFormat, &retNumberOfChangeRecords,
1519 &retChangeRecordDataOffset);
1520 EXPECT_EQ(rc, PLDM_SUCCESS);
1521 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
1522 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
1523
1524 const uint8_t* changeRecordData =
1525 reinterpret_cast<const uint8_t*>(changeRecord1);
1526 size_t changeRecordDataSize =
1527 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
1528 uint8_t retEventDataOperation;
1529 uint8_t retNumberOfChangeEntries;
1530 size_t retChangeEntryDataOffset;
1531
1532 rc = decode_pldm_pdr_repository_change_record_data(
1533 reinterpret_cast<const uint8_t*>(changeRecordData),
1534 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1535 &retChangeEntryDataOffset);
1536 EXPECT_EQ(rc, PLDM_SUCCESS);
1537 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
1538 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
1539 changeRecordData += retChangeEntryDataOffset;
1540 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
1541 sizeof(uint32_t) * retNumberOfChangeEntries));
1542
1543 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
1544 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
1545 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
1546 rc = decode_pldm_pdr_repository_change_record_data(
1547 reinterpret_cast<const uint8_t*>(changeRecordData),
1548 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1549 &retChangeEntryDataOffset);
1550 EXPECT_EQ(rc, PLDM_SUCCESS);
1551 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
1552 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
1553 changeRecordData += retChangeEntryDataOffset;
1554 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
1555 sizeof(uint32_t) * retNumberOfChangeEntries));
1556}
1557
1558TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
1559{
1560 uint8_t eventDataFormat{};
1561 uint8_t numberOfChangeRecords{};
1562 size_t changeRecordDataOffset{};
1563 auto rc = decode_pldm_pdr_repository_chg_event_data(
1564 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
1565 &changeRecordDataOffset);
1566 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1567
1568 std::array<uint8_t, 2> eventData{};
1569 rc = decode_pldm_pdr_repository_chg_event_data(
1570 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
1571 &numberOfChangeRecords, &changeRecordDataOffset);
1572 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1573
1574 uint8_t eventDataOperation{};
1575 uint8_t numberOfChangeEntries{};
1576 size_t changeEntryDataOffset{};
1577 rc = decode_pldm_pdr_repository_change_record_data(
1578 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
1579 &changeEntryDataOffset);
1580 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1581
1582 std::array<uint8_t, 2> changeRecord{};
1583 rc = decode_pldm_pdr_repository_change_record_data(
1584 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
1585 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
1586 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1587}