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