blob: d19ebf5fb14e7cd2ef45b87561051d1bd17ce623 [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;
George Liuab749502020-04-15 13:16:35 +0800455 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16;
456 uint16_t effecter_value = 65534;
George Liu30b859f2020-01-07 15:03:22 +0800457
458 std::vector<uint8_t> requestMsg(
George Liuab749502020-04-15 13:16:35 +0800459 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
George Liu30b859f2020-01-07 15:03:22 +0800460 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,
George Liuab749502020-04-15 13:16:35 +0800465 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
George Liu30b859f2020-01-07 15:03:22 +0800466 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);
George Liuab749502020-04-15 13:16:35 +0800473 uint16_t* val = (uint16_t*)req->effecter_value;
474 *val = le16toh(*val);
475 EXPECT_EQ(effecter_value, *val);
George Liu30b859f2020-01-07 15:03:22 +0800476}
477
478TEST(SetNumericEffecterValue, testBadEncodeRequest)
479{
480 std::vector<uint8_t> requestMsg(
481 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
482 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
483
484 auto rc = encode_set_numeric_effecter_value_req(
485 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
486 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
487
488 uint16_t effecter_value;
489 rc = encode_set_numeric_effecter_value_req(
490 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
491 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
492 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
493}
494
495TEST(SetNumericEffecterValue, testGoodDecodeResponse)
496{
497 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
498 responseMsg{};
499
500 uint8_t completion_code = 0xA0;
501
502 uint8_t retcompletion_code;
503
504 memcpy(responseMsg.data() + hdrSize, &completion_code,
505 sizeof(completion_code));
506
507 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
508
509 auto rc = decode_set_numeric_effecter_value_resp(
510 response, responseMsg.size() - hdrSize, &retcompletion_code);
511
512 EXPECT_EQ(rc, PLDM_SUCCESS);
513 EXPECT_EQ(completion_code, retcompletion_code);
514}
515
516TEST(SetNumericEffecterValue, testBadDecodeResponse)
517{
518 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
519 responseMsg{};
520
521 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
522
523 auto rc = decode_set_numeric_effecter_value_resp(response,
524 responseMsg.size(), NULL);
525
526 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
527}
528
529TEST(SetNumericEffecterValue, testGoodEncodeResponse)
530{
531 std::array<uint8_t, sizeof(pldm_msg_hdr) +
532 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
533 responseMsg{};
534 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
535 uint8_t completionCode = 0;
536
537 auto rc = encode_set_numeric_effecter_value_resp(
538 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
539
540 EXPECT_EQ(rc, PLDM_SUCCESS);
541 EXPECT_EQ(completionCode, response->payload[0]);
542}
543
544TEST(SetNumericEffecterValue, testBadEncodeResponse)
545{
546 auto rc = encode_set_numeric_effecter_value_resp(
547 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
548 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
549}
Jolie Ku3557bad2020-03-02 16:22:57 +0800550
551TEST(GetStateSensorReadings, testGoodEncodeResponse)
552{
553 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
554 responseMsg{};
555
556 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
557 uint8_t completionCode = 0;
558 uint8_t comp_sensorCnt = 0x2;
559
560 std::array<get_sensor_state_field, 2> stateField{};
561 stateField[0] = {ENABLED, NORMAL, WARNING, UNKNOWN};
562 stateField[1] = {FAILED, UPPERFATAL, UPPERCRITICAL, FATAL};
563
564 auto rc = encode_get_state_sensor_readings_resp(
565 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response);
566
567 struct pldm_get_state_sensor_readings_resp* resp =
568 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
569 response->payload);
570
571 EXPECT_EQ(rc, PLDM_SUCCESS);
572 EXPECT_EQ(completionCode, resp->completion_code);
573 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count);
574 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state);
575 EXPECT_EQ(stateField[0].present_state, resp->field->present_state);
576 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state);
577 EXPECT_EQ(stateField[0].event_state, resp->field->event_state);
578 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state);
579 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state);
580 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state);
581 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state);
582}
583
584TEST(GetStateSensorReadings, testBadEncodeResponse)
585{
586 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr,
587 nullptr);
588
589 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
590}
591
592TEST(GetStateSensorReadings, testGoodDecodeResponse)
593{
594 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
595 responseMsg{};
596
597 uint8_t completionCode = 0;
598 uint8_t comp_sensorCnt = 2;
599
600 std::array<get_sensor_state_field, 2> stateField{};
601 stateField[0] = {DISABLED, UNKNOWN, UNKNOWN, UNKNOWN};
602 stateField[1] = {ENABLED, LOWERFATAL, LOWERCRITICAL, WARNING};
603
604 uint8_t retcompletion_code = 0;
605 uint8_t retcomp_sensorCnt = 2;
606 std::array<get_sensor_state_field, 2> retstateField{};
607
608 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
609 struct pldm_get_state_sensor_readings_resp* resp =
610 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
611 response->payload);
612
613 resp->completion_code = completionCode;
614 resp->comp_sensor_count = comp_sensorCnt;
615 memcpy(resp->field, &stateField,
616 (sizeof(get_sensor_state_field) * comp_sensorCnt));
617
618 auto rc = decode_get_state_sensor_readings_resp(
619 response, responseMsg.size() - hdrSize, &retcompletion_code,
620 &retcomp_sensorCnt, retstateField.data());
621
622 EXPECT_EQ(rc, PLDM_SUCCESS);
623 EXPECT_EQ(completionCode, retcompletion_code);
624 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt);
625 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state);
626 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state);
627 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state);
628 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state);
629 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state);
630 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state);
631 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state);
632 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state);
633}
634
635TEST(GetStateSensorReadings, testBadDecodeResponse)
636{
637 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES>
638 responseMsg{};
639
640 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
641
642 auto rc = decode_get_state_sensor_readings_resp(
643 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr);
644
645 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
646
647 uint8_t completionCode = 0;
648 uint8_t comp_sensorCnt = 1;
649
650 std::array<get_sensor_state_field, 1> stateField{};
651 stateField[0] = {ENABLED, UPPERFATAL, UPPERCRITICAL, WARNING};
652
653 uint8_t retcompletion_code = 0;
654 uint8_t retcomp_sensorCnt = 0;
655 std::array<get_sensor_state_field, 1> retstateField{};
656
657 struct pldm_get_state_sensor_readings_resp* resp =
658 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
659 response->payload);
660
661 resp->completion_code = completionCode;
662 resp->comp_sensor_count = comp_sensorCnt;
663 memcpy(resp->field, &stateField,
664 (sizeof(get_sensor_state_field) * comp_sensorCnt));
665
666 rc = decode_get_state_sensor_readings_resp(
667 response, responseMsg.size() - hdrSize, &retcompletion_code,
668 &retcomp_sensorCnt, retstateField.data());
669
670 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
671}
672
673TEST(GetStateSensorReadings, testGoodEncodeRequest)
674{
675 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
676 requestMsg{};
677
678 uint16_t sensorId = 0xAB;
679 bitfield8_t sensorRearm;
680 sensorRearm.byte = 0x03;
681
682 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
683 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0,
684 request);
685
686 struct pldm_get_state_sensor_readings_req* req =
687 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
688 request->payload);
689
690 EXPECT_EQ(rc, PLDM_SUCCESS);
691 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
692 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte);
693}
694
695TEST(GetStateSensorReadings, testBadEncodeRequest)
696{
697 bitfield8_t sensorRearm;
698 sensorRearm.byte = 0x0;
699
700 auto rc =
701 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr);
702
703 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
704}
705
706TEST(GetStateSensorReadings, testGoodDecodeRequest)
707{
708 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
709 requestMsg{};
710
711 uint16_t sensorId = 0xCD;
712 bitfield8_t sensorRearm;
713 sensorRearm.byte = 0x10;
714
715 uint16_t retsensorId;
716 bitfield8_t retsensorRearm;
717 uint8_t retreserved;
718
719 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
720
721 struct pldm_get_state_sensor_readings_req* req =
722 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
723 request->payload);
724
725 req->sensor_id = htole16(sensorId);
726 req->sensor_rearm.byte = sensorRearm.byte;
727
728 auto rc = decode_get_state_sensor_readings_req(
729 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm,
730 &retreserved);
731
732 EXPECT_EQ(rc, PLDM_SUCCESS);
733 EXPECT_EQ(sensorId, retsensorId);
734 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte);
735 EXPECT_EQ(0, retreserved);
736}
737
738TEST(GetStateSensorReadings, testBadDecodeRequest)
739{
740 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
741 requestMsg{};
742
743 auto rc = decode_get_state_sensor_readings_req(
744 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr);
745
746 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
747 uint16_t sensorId = 0x11;
748 bitfield8_t sensorRearm;
749 sensorRearm.byte = 0x04;
750
751 uint16_t retsensorId;
752 bitfield8_t retsensorRearm;
753 uint8_t retreserved;
754
755 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
756
757 struct pldm_get_state_sensor_readings_req* req =
758 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
759 request->payload);
760
761 req->sensor_id = htole16(sensorId);
762 req->sensor_rearm.byte = sensorRearm.byte;
763
764 rc = decode_get_state_sensor_readings_req(
765 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm,
766 &retreserved);
767
768 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
769}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600770
771TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
772{
773 std::array<uint8_t,
774 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
775 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES>
776 requestMsg{};
777
778 uint8_t retFormatVersion = 0;
779 uint8_t retTid = 0;
780 uint8_t retEventClass = 0;
781 size_t retEventDataOffset = 0;
782
783 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
784 struct pldm_platform_event_message_req* request =
785 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload);
786
787 uint8_t formatVersion = 0x01;
788 uint8_t tid = 0x02;
789 // Sensor Event
790 uint8_t eventClass = 0x00;
791
792 request->format_version = formatVersion;
793 request->tid = tid;
794 request->event_class = eventClass;
795 size_t eventDataOffset =
796 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass);
797
798 auto rc = decode_platform_event_message_req(
799 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid,
800 &retEventClass, &retEventDataOffset);
801
802 EXPECT_EQ(rc, PLDM_SUCCESS);
803 EXPECT_EQ(retFormatVersion, formatVersion);
804 EXPECT_EQ(retTid, tid);
805 EXPECT_EQ(retEventClass, eventClass);
806 EXPECT_EQ(retEventDataOffset, eventDataOffset);
807}
808
809TEST(PlatformEventMessage, testBadDecodeRequest)
810{
811 const struct pldm_msg* msg = NULL;
812 std::array<uint8_t,
813 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
814 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
815 requestMsg{};
816 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
817 uint8_t retFormatVersion;
818 uint8_t retTid = 0;
819 uint8_t retEventClass = 0;
820 size_t retEventDataOffset;
821
822 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL,
823 NULL, NULL);
824 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
825
826 rc = decode_platform_event_message_req(
827 req,
828 requestMsg.size() - hdrSize -
829 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES,
830 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset);
831 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
832}
833
834TEST(PlatformEventMessage, testGoodEncodeResponse)
835{
836 std::array<uint8_t,
837 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
838 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
839 responseMsg{};
840 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
841 uint8_t completionCode = 0;
842 uint8_t instanceId = 0x01;
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500843 uint8_t platformEventStatus = 0x01;
Zahed Hossaind4abab12020-02-06 03:36:43 -0600844
845 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500846 platformEventStatus, response);
Zahed Hossaind4abab12020-02-06 03:36:43 -0600847
848 EXPECT_EQ(rc, PLDM_SUCCESS);
849 EXPECT_EQ(completionCode, response->payload[0]);
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500850 EXPECT_EQ(platformEventStatus, response->payload[1]);
Zahed Hossaind4abab12020-02-06 03:36:43 -0600851}
852
853TEST(PlatformEventMessage, testBadEncodeResponse)
854{
855 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL);
856 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
857}
Zahed Hossain1c861712020-03-04 08:55:19 -0600858
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500859TEST(PlatformEventMessage, testGoodEncodeRequest)
860{
861 uint8_t formatVersion = 0x01;
862 uint8_t Tid = 0x03;
863 uint8_t eventClass = 0x00;
864 uint8_t eventData = 34;
865
866 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
867 sizeof(eventData)>
868 requestMsg{};
869
870 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
871 auto rc = encode_platform_event_message_req(
872 0, formatVersion, Tid, eventClass,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500873 reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request,
874 sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500875
876 struct pldm_platform_event_message_req* req =
877 reinterpret_cast<struct pldm_platform_event_message_req*>(
878 request->payload);
879
880 EXPECT_EQ(rc, PLDM_SUCCESS);
881 EXPECT_EQ(formatVersion, req->format_version);
882 EXPECT_EQ(Tid, req->tid);
883 EXPECT_EQ(eventClass, req->event_class);
884 EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData)));
885}
886
887TEST(PlatformEventMessage, testBadEncodeRequest)
888{
889 uint8_t Tid = 0x03;
890 uint8_t eventClass = 0x00;
891 uint8_t eventData = 34;
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500892 size_t sz_eventData = sizeof(eventData);
893 size_t payloadLen =
894 sz_eventData + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500895 uint8_t formatVersion = 0x01;
896
897 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
898 sizeof(eventData)>
899 requestMsg{};
900 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500901
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500902 auto rc = encode_platform_event_message_req(
903 0, formatVersion, Tid, eventClass,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500904 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, nullptr,
905 payloadLen);
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500906 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
907 rc = encode_platform_event_message_req(
908 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500909 sz_eventData, request, payloadLen);
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500910 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
911 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500912 nullptr, 0, request, payloadLen);
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500913 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500914 rc = encode_platform_event_message_req(
915 0, formatVersion, Tid, eventClass,
916 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, request, 0);
917 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500918}
919
920TEST(PlatformEventMessage, testGoodDecodeResponse)
921{
922 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
923 responseMsg{};
924
925 uint8_t completionCode = PLDM_SUCCESS;
926 uint8_t platformEventStatus = 0x01;
927
928 uint8_t retcompletionCode;
929 uint8_t retplatformEventStatus;
930
931 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
932 struct pldm_platform_event_message_resp* resp =
933 reinterpret_cast<struct pldm_platform_event_message_resp*>(
934 response->payload);
935
936 resp->completion_code = completionCode;
937 resp->platform_event_status = platformEventStatus;
938
939 auto rc = decode_platform_event_message_resp(
940 response, responseMsg.size() - hdrSize, &retcompletionCode,
941 &retplatformEventStatus);
942
943 EXPECT_EQ(rc, PLDM_SUCCESS);
944 EXPECT_EQ(completionCode, retcompletionCode);
945 EXPECT_EQ(platformEventStatus, retplatformEventStatus);
946}
947
948TEST(PlatformEventMessage, testBadDecodeResponse)
949{
950 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
951 responseMsg{};
952
953 uint8_t completionCode = PLDM_SUCCESS;
954 uint8_t platformEventStatus = 0x01;
955
956 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
957 struct pldm_platform_event_message_resp* resp =
958 reinterpret_cast<struct pldm_platform_event_message_resp*>(
959 response->payload);
960 resp->completion_code = completionCode;
961 resp->platform_event_status = platformEventStatus;
962
963 auto rc = decode_platform_event_message_resp(
964 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
965
966 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
967
968 rc = decode_platform_event_message_resp(
969 response, responseMsg.size() - hdrSize - 1, &completionCode,
970 &platformEventStatus);
971
972 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
973}
974
Zahed Hossain1c861712020-03-04 08:55:19 -0600975TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
976{
977 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +
978 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
979 eventDataArr{};
980 uint16_t sensorId = 0x1234;
981 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE;
982
983 struct pldm_sensor_event_data* eventData =
984 (struct pldm_sensor_event_data*)eventDataArr.data();
985 eventData->sensor_id = sensorId;
986 eventData->sensor_event_class_type = sensorEventClassType;
987
988 size_t retSensorOpDataOffset;
989 uint16_t retSensorId = 0;
990 uint8_t retSensorEventClassType;
991 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType);
992 auto rc = decode_sensor_event_data(
993 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(),
994 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
995 EXPECT_EQ(rc, PLDM_SUCCESS);
996 EXPECT_EQ(retSensorId, sensorId);
997 EXPECT_EQ(retSensorEventClassType, sensorEventClassType);
998 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset);
999}
1000
1001TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest)
1002{
1003
1004 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH +
1005 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
1006 eventDataArr{};
1007
1008 struct pldm_sensor_event_data* eventData =
1009 (struct pldm_sensor_event_data*)eventDataArr.data();
1010
1011 size_t retSensorOpDataOffset;
1012 uint16_t retSensorId = 0;
1013 uint8_t retSensorEventClassType;
1014 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId,
1015 &retSensorEventClassType,
1016 &retSensorOpDataOffset);
1017 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1018
1019 rc = decode_sensor_event_data(
1020 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1021 eventDataArr.size() -
1022 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH,
1023 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1024 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1025
1026 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
1027
1028 rc = decode_sensor_event_data(
1029 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1030 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1031 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1032
1033 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
1034 rc = decode_sensor_event_data(
1035 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1036 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1037 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1038
1039 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE;
1040 rc = decode_sensor_event_data(
1041 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1042 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType,
1043 &retSensorOpDataOffset);
1044 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1045}
1046
1047TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
1048{
1049 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1050 eventDataArr{};
1051
1052 struct pldm_sensor_event_sensor_op_state* sensorData =
1053 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
1054 uint8_t presentState = PLDM_SENSOR_ENABLED;
1055 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
1056 sensorData->present_op_state = presentState;
1057 sensorData->previous_op_state = previousState;
1058
1059 uint8_t retPresentState;
1060 uint8_t retPreviousState;
1061 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
1062 eventDataArr.size(), &retPresentState,
1063 &retPreviousState);
1064 EXPECT_EQ(rc, PLDM_SUCCESS);
1065 EXPECT_EQ(retPresentState, presentState);
1066 EXPECT_EQ(retPreviousState, previousState);
1067}
1068
1069TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
1070{
1071 uint8_t presentOpState;
1072 uint8_t previousOpState;
1073 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
1074 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
1075 &previousOpState);
1076 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1077
1078 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1079 sensorData{};
1080 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1081 sensorDataLength + 1, &presentOpState,
1082 &previousOpState);
1083 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1084
1085 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1086 sensorDataLength, nullptr, &previousOpState);
1087 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1088}
1089
1090TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
1091{
1092 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1093 eventDataArr{};
1094
1095 struct pldm_sensor_event_state_sensor_state* sensorData =
1096 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
1097 uint8_t sensorOffset = 0x02;
1098 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1099 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1100 sensorData->sensor_offset = sensorOffset;
1101 sensorData->event_state = eventState;
1102 sensorData->previous_event_state = previousEventState;
1103
1104 uint8_t retSensorOffset;
1105 uint8_t retEventState;
1106 uint8_t retPreviousState;
1107 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1108 eventDataArr.size(), &retSensorOffset,
1109 &retEventState, &retPreviousState);
1110 EXPECT_EQ(rc, PLDM_SUCCESS);
1111 EXPECT_EQ(retSensorOffset, sensorOffset);
1112 EXPECT_EQ(retEventState, eventState);
1113 EXPECT_EQ(retPreviousState, previousEventState);
1114}
1115
1116TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
1117{
1118 uint8_t sensorOffset;
1119 uint8_t eventState;
1120 uint8_t previousEventState;
1121 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
1122 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
1123 &eventState, &previousEventState);
1124 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1125
1126 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1127 sensorData{};
1128 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1129 sensorDataLength - 1, &sensorOffset,
1130 &eventState, &previousEventState);
1131 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1132
1133 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1134 sensorDataLength, &sensorOffset, nullptr,
1135 &previousEventState);
1136 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1137}
1138
1139TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
1140{
1141 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1142 eventDataArr{};
1143 struct pldm_sensor_event_numeric_sensor_state* sensorData =
1144 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
1145
1146 size_t sensorDataLength =
1147 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
1148 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1149 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1150 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
1151 uint32_t presentReading = 305441741;
1152 sensorData->event_state = eventState;
1153 sensorData->previous_event_state = previousEventState;
1154 sensorData->sensor_data_size = sensorDataSize;
1155 sensorData->present_reading[3] =
1156 static_cast<uint8_t>(htole32(presentReading) & (0x000000ff));
1157 sensorData->present_reading[2] =
1158 static_cast<uint8_t>((htole32(presentReading) & (0x0000ff00)) >> 8);
1159 sensorData->present_reading[1] =
1160 static_cast<uint8_t>((htole32(presentReading) & (0x00ff0000)) >> 16);
1161 sensorData->present_reading[0] =
1162 static_cast<uint8_t>((htole32(presentReading) & (0xff000000)) >> 24);
1163
1164 uint8_t retEventState;
1165 uint8_t retPreviousEventState;
1166 uint8_t retSensorDataSize;
1167 uint32_t retPresentReading;
1168
1169 auto rc = decode_numeric_sensor_data(
1170 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
1171 &retEventState, &retPreviousEventState, &retSensorDataSize,
1172 &retPresentReading);
1173 EXPECT_EQ(rc, PLDM_SUCCESS);
1174 EXPECT_EQ(retEventState, eventState);
1175 EXPECT_EQ(retPreviousEventState, previousEventState);
1176 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1177 EXPECT_EQ(retPresentReading, presentReading);
1178
1179 int16_t presentReadingNew = -31432;
1180 sensorData->present_reading[1] =
1181 static_cast<uint8_t>(htole16(presentReadingNew) & (0x000000ff));
1182 sensorData->present_reading[0] =
1183 static_cast<uint8_t>((htole16(presentReadingNew) & (0x0000ff00)) >> 8);
1184 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
1185 sensorData->sensor_data_size = sensorDataSize;
1186 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
1187
1188 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1189 sensorDataLength, &retEventState,
1190 &retPreviousEventState, &retSensorDataSize,
1191 &retPresentReading);
1192 EXPECT_EQ(rc, PLDM_SUCCESS);
1193 EXPECT_EQ(retEventState, eventState);
1194 EXPECT_EQ(retPreviousEventState, previousEventState);
1195 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1196 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
1197}
1198
1199TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
1200{
1201 uint8_t eventState;
1202 uint8_t previousEventState;
1203 uint8_t sensorDataSize;
1204 uint32_t presentReading;
1205 size_t sensorDataLength =
1206 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
1207 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
1208 &previousEventState, &sensorDataSize,
1209 &presentReading);
1210 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1211
1212 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1213 sensorData{};
1214 rc = decode_numeric_sensor_data(
1215 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
1216 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1217 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1218
1219 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
1220 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
1221 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
1222 rc = decode_numeric_sensor_data(
1223 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1224 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1225 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1226
1227 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
1228 rc = decode_numeric_sensor_data(
1229 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1230 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1231 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1232
1233 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT32;
1234 rc = decode_numeric_sensor_data(
1235 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
1236 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1237 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1238}
Jolie Ku6787f172020-03-19 11:15:53 +08001239
1240TEST(GetNumericEffecterValue, testGoodEncodeRequest)
1241{
1242 std::vector<uint8_t> requestMsg(hdrSize +
1243 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
1244
1245 uint16_t effecter_id = 0xAB01;
1246
1247 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1248
1249 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
1250
1251 struct pldm_get_numeric_effecter_value_req* req =
1252 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1253 request->payload);
1254
1255 EXPECT_EQ(rc, PLDM_SUCCESS);
1256 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
1257}
1258
1259TEST(GetNumericEffecterValue, testBadEncodeRequest)
1260{
1261 std::vector<uint8_t> requestMsg(
1262 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
1263
1264 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
1265 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1266}
1267
1268TEST(GetNumericEffecterValue, testGoodDecodeRequest)
1269{
1270 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1271 requestMsg{};
1272
1273 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1274 struct pldm_get_numeric_effecter_value_req* req =
1275 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1276 request->payload);
1277
1278 uint16_t effecter_id = 0x12AB;
1279 req->effecter_id = htole16(effecter_id);
1280
1281 uint16_t reteffecter_id;
1282
1283 auto rc = decode_get_numeric_effecter_value_req(
1284 request, requestMsg.size() - hdrSize, &reteffecter_id);
1285
1286 EXPECT_EQ(rc, PLDM_SUCCESS);
1287 EXPECT_EQ(effecter_id, reteffecter_id);
1288}
1289
1290TEST(GetNumericEffecterValue, testBadDecodeRequest)
1291{
1292 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1293 requestMsg{};
1294
1295 auto rc = decode_get_numeric_effecter_value_req(
1296 nullptr, requestMsg.size() - hdrSize, nullptr);
1297
1298 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1299
1300 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1301 struct pldm_set_numeric_effecter_value_req* req =
1302 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
1303 request->payload);
1304
1305 uint16_t effecter_id = 0x1A;
1306 req->effecter_id = htole16(effecter_id);
1307 uint16_t reteffecter_id;
1308
1309 rc = decode_get_numeric_effecter_value_req(
1310 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
1311
1312 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1313}
1314
1315TEST(GetNumericEffecterValue, testGoodEncodeResponse)
1316{
1317 uint8_t completionCode = 0;
1318 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
1319 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1320 uint32_t pendingValue = 0x12345678;
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001321 uint32_t presentValue = 0xABCDEF11;
Jolie Ku6787f172020-03-19 11:15:53 +08001322
1323 std::array<uint8_t,
1324 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1325 responseMsg{};
1326 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1327
Jolie Ku6787f172020-03-19 11:15:53 +08001328 auto rc = encode_get_numeric_effecter_value_resp(
1329 0, completionCode, effecter_dataSize, effecter_operState,
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001330 reinterpret_cast<uint8_t*>(&pendingValue),
1331 reinterpret_cast<uint8_t*>(&presentValue), response,
Jolie Ku6787f172020-03-19 11:15:53 +08001332 responseMsg.size() - hdrSize);
1333
1334 struct pldm_get_numeric_effecter_value_resp* resp =
1335 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1336 response->payload);
1337
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001338 uint32_t* val_pending = (uint32_t*)(&resp->pending_and_present_values[0]);
1339 *val_pending = le32toh(*val_pending);
1340 uint32_t* val_present = (uint32_t*)(&resp->pending_and_present_values[4]);
1341 *val_present = le32toh(*val_present);
1342
Jolie Ku6787f172020-03-19 11:15:53 +08001343 EXPECT_EQ(rc, PLDM_SUCCESS);
1344 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
1345 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001346 EXPECT_EQ(pendingValue, *val_pending);
1347 EXPECT_EQ(presentValue, *val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001348}
1349
1350TEST(GetNumericEffecterValue, testBadEncodeResponse)
1351{
1352 std::array<uint8_t,
1353 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1354 responseMsg{};
1355 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1356
1357 uint8_t pendingValue = 0x01;
1358 uint8_t presentValue = 0x02;
1359
1360 auto rc = encode_get_numeric_effecter_value_resp(
1361 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
1362 responseMsg.size() - hdrSize);
1363 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1364
1365 rc = encode_get_numeric_effecter_value_resp(
1366 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
1367 reinterpret_cast<uint8_t*>(&presentValue), response,
1368 responseMsg.size() - hdrSize);
1369 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1370
1371 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1372 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
1373
1374 rc = encode_get_numeric_effecter_value_resp(
1375 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
1376 reinterpret_cast<uint8_t*>(&pendingValue),
1377 reinterpret_cast<uint8_t*>(&presentValue), response,
1378 responseMsg.size() - hdrSize);
1379 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1380}
1381
1382TEST(GetNumericEffecterValue, testGoodDecodeResponse)
1383{
1384 std::array<uint8_t,
1385 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1386 responseMsg{};
1387
1388 uint8_t completionCode = 0;
1389 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
1390 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1391 uint16_t pendingValue = 0x4321;
1392 uint16_t presentValue = 0xDCBA;
1393
1394 uint8_t retcompletionCode;
1395 uint8_t reteffecter_dataSize;
1396 uint8_t reteffecter_operState;
1397 uint8_t retpendingValue[2];
1398 uint8_t retpresentValue[2];
1399
1400 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1401 struct pldm_get_numeric_effecter_value_resp* resp =
1402 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1403 response->payload);
1404
1405 resp->completion_code = completionCode;
1406 resp->effecter_data_size = effecter_dataSize;
1407 resp->effecter_oper_state = effecter_operState;
1408
1409 uint16_t pendingValue_le = htole16(pendingValue);
1410 memcpy(resp->pending_and_present_values, &pendingValue_le,
1411 sizeof(pendingValue_le));
1412 uint16_t presentValue_le = htole16(presentValue);
1413 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1414 sizeof(presentValue_le));
1415
1416 auto rc = decode_get_numeric_effecter_value_resp(
1417 response, responseMsg.size() - hdrSize, &retcompletionCode,
1418 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1419 retpresentValue);
1420
Jolie Ku6787f172020-03-19 11:15:53 +08001421 EXPECT_EQ(rc, PLDM_SUCCESS);
1422 EXPECT_EQ(completionCode, retcompletionCode);
1423 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
1424 EXPECT_EQ(effecter_operState, reteffecter_operState);
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001425 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
1426 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
Jolie Ku6787f172020-03-19 11:15:53 +08001427}
1428
1429TEST(GetNumericEffecterValue, testBadDecodeResponse)
1430{
1431 std::array<uint8_t,
1432 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1433 responseMsg{};
1434
1435 auto rc = decode_get_numeric_effecter_value_resp(
1436 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
1437 nullptr, nullptr);
1438
1439 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1440
1441 uint8_t completionCode = 0;
1442 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
1443 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
1444 uint16_t pendingValue = 0x5678;
1445 uint16_t presentValue = 0xCDEF;
1446
1447 uint8_t retcompletionCode;
1448 uint8_t reteffecter_dataSize;
1449 uint8_t reteffecter_operState;
1450 uint8_t retpendingValue[2];
1451 uint8_t retpresentValue[2];
1452
1453 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1454 struct pldm_get_numeric_effecter_value_resp* resp =
1455 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1456 response->payload);
1457
1458 resp->completion_code = completionCode;
1459 resp->effecter_data_size = effecter_dataSize;
1460 resp->effecter_oper_state = effecter_operState;
1461
1462 uint16_t pendingValue_le = htole16(pendingValue);
1463 memcpy(resp->pending_and_present_values, &pendingValue_le,
1464 sizeof(pendingValue_le));
1465 uint16_t presentValue_le = htole16(presentValue);
1466 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1467 sizeof(presentValue_le));
1468
1469 rc = decode_get_numeric_effecter_value_resp(
1470 response, responseMsg.size() - hdrSize, &retcompletionCode,
1471 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1472 retpresentValue);
1473
1474 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1475}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001476
1477TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
1478{
1479 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
1480 const uint8_t numberOfChangeRecords = 2;
1481 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
1482 const uint8_t numberOfChangeEntries1 = 2;
1483 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
1484 {0x00000000, 0x12345678}};
1485 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
1486 const uint8_t numberOfChangeEntries2 = 5;
1487 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
1488 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
1489 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
1490 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
1491 numberOfChangeRecords +
1492 (numberOfChangeEntries1 + numberOfChangeEntries2) *
1493 sizeof(uint32_t)>
1494 eventDataArr{};
1495
1496 struct pldm_pdr_repository_chg_event_data* eventData =
1497 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
1498 eventDataArr.data());
1499 eventData->event_data_format = eventDataFormat;
1500 eventData->number_of_change_records = numberOfChangeRecords;
1501 struct pldm_pdr_repository_change_record_data* changeRecord1 =
1502 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1503 eventData->change_records);
1504 changeRecord1->event_data_operation = eventDataOperation1;
1505 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
1506 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
1507 changeRecordArr1.size() * sizeof(uint32_t));
1508 struct pldm_pdr_repository_change_record_data* changeRecord2 =
1509 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1510 eventData->change_records +
1511 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
1512 (changeRecordArr1.size() * sizeof(uint32_t)));
1513 changeRecord2->event_data_operation = eventDataOperation2;
1514 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
1515 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
1516 changeRecordArr2.size() * sizeof(uint32_t));
1517
1518 uint8_t retEventDataFormat{};
1519 uint8_t retNumberOfChangeRecords{};
1520 size_t retChangeRecordDataOffset{0};
1521 auto rc = decode_pldm_pdr_repository_chg_event_data(
1522 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
1523 &retEventDataFormat, &retNumberOfChangeRecords,
1524 &retChangeRecordDataOffset);
1525 EXPECT_EQ(rc, PLDM_SUCCESS);
1526 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
1527 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
1528
1529 const uint8_t* changeRecordData =
1530 reinterpret_cast<const uint8_t*>(changeRecord1);
1531 size_t changeRecordDataSize =
1532 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
1533 uint8_t retEventDataOperation;
1534 uint8_t retNumberOfChangeEntries;
1535 size_t retChangeEntryDataOffset;
1536
1537 rc = decode_pldm_pdr_repository_change_record_data(
1538 reinterpret_cast<const uint8_t*>(changeRecordData),
1539 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1540 &retChangeEntryDataOffset);
1541 EXPECT_EQ(rc, PLDM_SUCCESS);
1542 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
1543 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
1544 changeRecordData += retChangeEntryDataOffset;
1545 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
1546 sizeof(uint32_t) * retNumberOfChangeEntries));
1547
1548 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
1549 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
1550 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
1551 rc = decode_pldm_pdr_repository_change_record_data(
1552 reinterpret_cast<const uint8_t*>(changeRecordData),
1553 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1554 &retChangeEntryDataOffset);
1555 EXPECT_EQ(rc, PLDM_SUCCESS);
1556 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
1557 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
1558 changeRecordData += retChangeEntryDataOffset;
1559 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
1560 sizeof(uint32_t) * retNumberOfChangeEntries));
1561}
1562
1563TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
1564{
1565 uint8_t eventDataFormat{};
1566 uint8_t numberOfChangeRecords{};
1567 size_t changeRecordDataOffset{};
1568 auto rc = decode_pldm_pdr_repository_chg_event_data(
1569 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
1570 &changeRecordDataOffset);
1571 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1572
1573 std::array<uint8_t, 2> eventData{};
1574 rc = decode_pldm_pdr_repository_chg_event_data(
1575 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
1576 &numberOfChangeRecords, &changeRecordDataOffset);
1577 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1578
1579 uint8_t eventDataOperation{};
1580 uint8_t numberOfChangeEntries{};
1581 size_t changeEntryDataOffset{};
1582 rc = decode_pldm_pdr_repository_change_record_data(
1583 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
1584 &changeEntryDataOffset);
1585 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1586
1587 std::array<uint8_t, 2> changeRecord{};
1588 rc = decode_pldm_pdr_repository_change_record_data(
1589 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
1590 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
1591 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1592}
Jolie Kuf798c8f2020-04-14 11:18:06 +08001593
1594TEST(GetSensorReading, testGoodEncodeRequest)
1595{
1596 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
1597 requestMsg{};
1598
1599 uint16_t sensorId = 0x1234;
1600 bool8_t rearmEventState = 0x01;
1601
1602 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1603 auto rc =
1604 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request);
1605
1606 struct pldm_get_sensor_reading_req* req =
1607 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
1608
1609 EXPECT_EQ(rc, PLDM_SUCCESS);
1610 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
1611 EXPECT_EQ(rearmEventState, req->rearm_event_state);
1612}
1613
1614TEST(GetSensorReading, testBadEncodeRequest)
1615{
1616 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr);
1617
1618 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1619}
1620
1621TEST(GetSensorReading, testGoodDecodeRequest)
1622{
1623 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
1624 requestMsg{};
1625
1626 uint16_t sensorId = 0xABCD;
1627 bool8_t rearmEventState = 0xA;
1628
1629 uint16_t retsensorId;
1630 bool8_t retrearmEventState;
1631
1632 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1633
1634 struct pldm_get_sensor_reading_req* req =
1635 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
1636
1637 req->sensor_id = htole16(sensorId);
1638 req->rearm_event_state = rearmEventState;
1639
1640 auto rc =
1641 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize,
1642 &retsensorId, &retrearmEventState);
1643
1644 EXPECT_EQ(rc, PLDM_SUCCESS);
1645 EXPECT_EQ(sensorId, retsensorId);
1646 EXPECT_EQ(rearmEventState, retrearmEventState);
1647}
1648
1649TEST(GetSensorReading, testBadDecodeRequest)
1650{
1651 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
1652 requestMsg{};
1653
1654 auto rc = decode_get_sensor_reading_req(
1655 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr);
1656 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1657
1658 uint16_t sensorId = 0xABCD;
1659 bool8_t rearmEventState = 0xA;
1660
1661 uint16_t retsensorId;
1662 bool8_t retrearmEventState;
1663
1664 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1665
1666 struct pldm_get_sensor_reading_req* req =
1667 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
1668
1669 req->sensor_id = htole16(sensorId);
1670 req->rearm_event_state = rearmEventState;
1671
1672 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1,
1673 &retsensorId, &retrearmEventState);
1674
1675 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1676}
1677
1678TEST(GetSensorReading, testGoodEncodeResponse)
1679{
1680 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES>
1681 responseMsg{};
1682
1683 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1684
1685 uint8_t completionCode = 0;
1686 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1687 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED;
1688 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION;
1689 uint8_t presentState = NORMAL;
1690 uint8_t previousState = WARNING;
1691 uint8_t eventState = UPPERWARNING;
1692 uint8_t presentReading = 0x21;
1693
1694 auto rc = encode_get_sensor_reading_resp(
1695 0, completionCode, sensor_dataSize, sensor_operationalState,
1696 sensor_event_messageEnable, presentState, previousState, eventState,
1697 reinterpret_cast<uint8_t*>(&presentReading), response,
1698 responseMsg.size() - hdrSize);
1699
1700 struct pldm_get_sensor_reading_resp* resp =
1701 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
1702 response->payload);
1703
1704 EXPECT_EQ(rc, PLDM_SUCCESS);
1705 EXPECT_EQ(completionCode, resp->completion_code);
1706 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size);
1707 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state);
1708 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable);
1709 EXPECT_EQ(presentState, resp->present_state);
1710 EXPECT_EQ(previousState, resp->previous_state);
1711 EXPECT_EQ(eventState, resp->event_state);
1712 EXPECT_EQ(presentReading,
1713 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0])));
1714}
1715
1716TEST(GetSensorReading, testBadEncodeResponse)
1717{
1718 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
1719 responseMsg{};
1720
1721 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1722
1723 uint8_t presentReading = 0x1;
1724
1725 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0,
1726 nullptr, nullptr,
1727 responseMsg.size() - hdrSize);
1728 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1729
1730 rc = encode_get_sensor_reading_resp(
1731 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1,
1732 reinterpret_cast<uint8_t*>(&presentReading), response,
1733 responseMsg.size() - hdrSize);
1734 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1735
1736 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1737
1738 rc = encode_get_sensor_reading_resp(
1739 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1,
1740 reinterpret_cast<uint8_t*>(&presentReading), response,
1741 responseMsg.size() - hdrSize);
1742 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1743}
1744
1745TEST(GetSensorReading, testGoodDecodeResponse)
1746{
1747 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
1748 responseMsg{};
1749
1750 uint8_t completionCode = 0;
1751 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
1752 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN;
1753 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED;
1754 uint8_t presentState = CRITICAL;
1755 uint8_t previousState = UPPERCRITICAL;
1756 uint8_t eventState = WARNING;
1757 uint32_t presentReading = 0xABCDEF11;
1758
1759 uint8_t retcompletionCode;
1760 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
1761 uint8_t retsensor_operationalState;
1762 uint8_t retsensor_event_messageEnable;
1763 uint8_t retpresentState;
1764 uint8_t retpreviousState;
1765 uint8_t reteventState;
1766 uint8_t retpresentReading[4];
1767
1768 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1769 struct pldm_get_sensor_reading_resp* resp =
1770 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
1771 response->payload);
1772
1773 resp->completion_code = completionCode;
1774 resp->sensor_data_size = sensor_dataSize;
1775 resp->sensor_operational_state = sensor_operationalState;
1776 resp->sensor_event_message_enable = sensor_event_messageEnable;
1777 resp->present_state = presentState;
1778 resp->previous_state = previousState;
1779 resp->event_state = eventState;
1780
1781 uint32_t presentReading_le = htole32(presentReading);
1782 memcpy(resp->present_reading, &presentReading_le,
1783 sizeof(presentReading_le));
1784
1785 auto rc = decode_get_sensor_reading_resp(
1786 response, responseMsg.size() - hdrSize, &retcompletionCode,
1787 &retsensor_dataSize, &retsensor_operationalState,
1788 &retsensor_event_messageEnable, &retpresentState, &retpreviousState,
1789 &reteventState, retpresentReading);
1790
1791 EXPECT_EQ(rc, PLDM_SUCCESS);
1792 EXPECT_EQ(completionCode, retcompletionCode);
1793 EXPECT_EQ(sensor_dataSize, retsensor_dataSize);
1794 EXPECT_EQ(sensor_operationalState, retsensor_operationalState);
1795 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable);
1796 EXPECT_EQ(presentState, retpresentState);
1797 EXPECT_EQ(previousState, retpreviousState);
1798 EXPECT_EQ(eventState, reteventState);
1799 EXPECT_EQ(presentReading,
1800 *(reinterpret_cast<uint32_t*>(retpresentReading)));
1801}
1802
1803TEST(GetSensorReading, testBadDecodeResponse)
1804{
1805 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1>
1806 responseMsg{};
1807
1808 auto rc = decode_get_sensor_reading_resp(
1809 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
1810 nullptr, nullptr, nullptr, nullptr, nullptr);
1811
1812 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1813
1814 uint8_t completionCode = 0;
1815 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1816 uint8_t sensor_operationalState = INTEST;
1817 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED;
1818 uint8_t presentState = FATAL;
1819 uint8_t previousState = UPPERFATAL;
1820 uint8_t eventState = WARNING;
1821 uint8_t presentReading = 0xA;
1822
1823 uint8_t retcompletionCode;
1824 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
1825 uint8_t retsensor_operationalState;
1826 uint8_t retsensor_event_messageEnable;
1827 uint8_t retpresent_state;
1828 uint8_t retprevious_state;
1829 uint8_t retevent_state;
1830 uint8_t retpresentReading;
1831
1832 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1833 struct pldm_get_sensor_reading_resp* resp =
1834 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
1835 response->payload);
1836
1837 resp->completion_code = completionCode;
1838 resp->sensor_data_size = sensor_dataSize;
1839 resp->sensor_operational_state = sensor_operationalState;
1840 resp->sensor_event_message_enable = sensor_event_messageEnable;
1841 resp->present_state = presentState;
1842 resp->previous_state = previousState;
1843 resp->event_state = eventState;
1844 resp->present_reading[0] = presentReading;
1845
1846 rc = decode_get_sensor_reading_resp(
1847 response, responseMsg.size() - hdrSize, &retcompletionCode,
1848 &retsensor_dataSize, &retsensor_operationalState,
1849 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state,
1850 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading));
1851
1852 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1853}