blob: 11252d651b10708d28c844f3de4280ad4319e28f [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
Sampa Misra0db1dfa2019-03-19 00:15:31 -050082 uint16_t effecterId = 0x32;
83 uint8_t compEffecterCnt = 0x2;
84
85 std::array<set_effecter_state_field, 8> stateField{};
86 stateField[0] = {PLDM_REQUEST_SET, 3};
87 stateField[1] = {PLDM_REQUEST_SET, 4};
88
89 uint16_t retEffecterId = 0;
90 uint8_t retCompEffecterCnt = 0;
91
92 std::array<set_effecter_state_field, 8> retStateField{};
93
Zahed Hossain223a73d2019-07-04 12:46:18 -050094 memcpy(requestMsg.data() + hdrSize, &effecterId, sizeof(effecterId));
95 memcpy(requestMsg.data() + sizeof(effecterId) + hdrSize, &compEffecterCnt,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050096 sizeof(compEffecterCnt));
Zahed Hossain223a73d2019-07-04 12:46:18 -050097 memcpy(requestMsg.data() + sizeof(effecterId) + sizeof(compEffecterCnt) +
98 hdrSize,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050099 &stateField, sizeof(stateField));
100
Zahed Hossain223a73d2019-07-04 12:46:18 -0500101 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
102
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500103 auto rc = decode_set_state_effecter_states_req(
Zahed Hossain223a73d2019-07-04 12:46:18 -0500104 request, requestMsg.size() - hdrSize, &retEffecterId,
vkaverapa6575b82019-04-03 05:33:52 -0500105 &retCompEffecterCnt, retStateField.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500106
George Liu684a7162019-12-06 15:10:52 +0800107 EXPECT_EQ(rc, PLDM_SUCCESS);
108 EXPECT_EQ(effecterId, retEffecterId);
109 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt);
110 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request);
111 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
112 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request);
113 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500114}
115
116TEST(SetStateEffecterStates, testBadDecodeRequest)
117{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500118 const struct pldm_msg* msg = NULL;
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500119
vkaverapa6575b82019-04-03 05:33:52 -0500120 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
121 NULL, NULL);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500122
George Liu684a7162019-12-06 15:10:52 +0800123 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500124}
vkaverap98a2c192019-04-03 05:33:52 -0500125
126TEST(SetStateEffecterStates, testBadDecodeResponse)
127{
128 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
129 responseMsg{};
130
Zahed Hossain223a73d2019-07-04 12:46:18 -0500131 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
132
133 auto rc = decode_set_state_effecter_states_resp(response,
vkaverapa6575b82019-04-03 05:33:52 -0500134 responseMsg.size(), NULL);
vkaverap98a2c192019-04-03 05:33:52 -0500135
George Liu684a7162019-12-06 15:10:52 +0800136 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
vkaverap98a2c192019-04-03 05:33:52 -0500137}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500138
139TEST(GetPDR, testGoodEncodeResponse)
140{
141 uint8_t completionCode = 0;
142 uint32_t nextRecordHndl = 0x12;
143 uint32_t nextDataTransferHndl = 0x13;
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500144 uint8_t transferFlag = PLDM_END;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500145 uint16_t respCnt = 0x5;
146 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500147 uint8_t transferCRC = 6;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500148
149 // + size of record data and transfer CRC
150 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
151 recordData.size() + 1);
152 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
153
154 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
155 nextDataTransferHndl, transferFlag, respCnt,
156 recordData.data(), transferCRC, response);
157
George Liu684a7162019-12-06 15:10:52 +0800158 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500159 struct pldm_get_pdr_resp* resp =
160 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
161
George Liu684a7162019-12-06 15:10:52 +0800162 EXPECT_EQ(completionCode, resp->completion_code);
163 EXPECT_EQ(nextRecordHndl, resp->next_record_handle);
164 EXPECT_EQ(nextDataTransferHndl, resp->next_data_transfer_handle);
165 EXPECT_EQ(transferFlag, resp->transfer_flag);
166 EXPECT_EQ(respCnt, resp->response_count);
167 EXPECT_EQ(0,
Sampa Misra7fcfb662019-05-08 13:13:53 -0500168 memcmp(recordData.data(), resp->record_data, recordData.size()));
George Liu684a7162019-12-06 15:10:52 +0800169 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500170 recordData.size()),
171 transferCRC);
172
173 transferFlag = PLDM_START_AND_END; // No CRC in this case
174 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
175 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
176 nextDataTransferHndl, transferFlag, respCnt,
177 recordData.data(), transferCRC, response);
George Liu684a7162019-12-06 15:10:52 +0800178 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500179}
180
181TEST(GetPDR, testBadEncodeResponse)
182{
183 uint32_t nextRecordHndl = 0x12;
184 uint32_t nextDataTransferHndl = 0x13;
185 uint8_t transferFlag = PLDM_START_AND_END;
186 uint16_t respCnt = 0x5;
187 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
188 uint8_t transferCRC = 0;
189
190 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
191 nextDataTransferHndl, transferFlag, respCnt,
192 recordData.data(), transferCRC, nullptr);
193
George Liu684a7162019-12-06 15:10:52 +0800194 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500195}
196
197TEST(GetPDR, testGoodDecodeRequest)
198{
199 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
200
201 uint32_t recordHndl = 0x32;
202 uint32_t dataTransferHndl = 0x11;
203 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
204 uint16_t requestCnt = 0x5;
205 uint16_t recordChangeNum = 0;
206
207 uint32_t retRecordHndl = 0;
208 uint32_t retDataTransferHndl = 0;
209 uint8_t retTransferOpFlag = 0;
210 uint16_t retRequestCnt = 0;
211 uint16_t retRecordChangeNum = 0;
212
213 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
214 struct pldm_get_pdr_req* request =
215 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
216
217 request->record_handle = recordHndl;
218 request->data_transfer_handle = dataTransferHndl;
219 request->transfer_op_flag = transferOpFlag;
220 request->request_count = requestCnt;
221 request->record_change_number = recordChangeNum;
222
223 auto rc = decode_get_pdr_req(
224 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
225 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
226
George Liu684a7162019-12-06 15:10:52 +0800227 EXPECT_EQ(rc, PLDM_SUCCESS);
228 EXPECT_EQ(retRecordHndl, recordHndl);
229 EXPECT_EQ(retDataTransferHndl, dataTransferHndl);
230 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
231 EXPECT_EQ(retRequestCnt, requestCnt);
232 EXPECT_EQ(retRecordChangeNum, recordChangeNum);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500233}
234
235TEST(GetPDR, testBadDecodeRequest)
236{
237 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
238 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
239
240 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
241 NULL);
242
George Liu684a7162019-12-06 15:10:52 +0800243 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500244}
George Liu820a9a52019-11-26 14:43:59 +0800245
246TEST(GetPDR, testGoodEncodeRequest)
247{
248 uint32_t record_hndl = 0;
249 uint32_t data_transfer_hndl = 0;
250 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
251 uint16_t request_cnt = 20;
252 uint16_t record_chg_num = 0;
253
254 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
255 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
256
257 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
258 transfer_op_flag, request_cnt, record_chg_num,
259 request, PLDM_GET_PDR_REQ_BYTES);
260 EXPECT_EQ(rc, PLDM_SUCCESS);
261 struct pldm_get_pdr_req* req =
262 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
263 EXPECT_EQ(record_hndl, req->record_handle);
264 EXPECT_EQ(data_transfer_hndl, req->data_transfer_handle);
265 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
266 EXPECT_EQ(request_cnt, req->request_count);
267 EXPECT_EQ(record_chg_num, req->record_change_number);
268}
269
270TEST(GetPDR, testBadEncodeRequest)
271{
272 uint32_t record_hndl = 0;
273 uint32_t data_transfer_hndl = 0;
274 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
275 uint16_t request_cnt = 32;
276 uint16_t record_chg_num = 0;
277
278 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
279 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
280
281 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
282 transfer_op_flag, request_cnt, record_chg_num,
283 nullptr, PLDM_GET_PDR_REQ_BYTES);
284 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
285
286 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
287 transfer_op_flag, request_cnt, record_chg_num,
288 request, PLDM_GET_PDR_REQ_BYTES + 1);
289 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
290}
291
292TEST(GetPDR, testGoodDecodeResponse)
293{
294 const char* recordData = "123456789";
295 uint8_t completionCode = PLDM_SUCCESS;
296 uint32_t nextRecordHndl = 0;
297 uint32_t nextDataTransferHndl = 0;
298 uint8_t transferFlag = PLDM_END;
299 constexpr uint16_t respCnt = 9;
300 uint8_t transferCRC = 96;
301 size_t recordDataLength = 32;
302 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
303 sizeof(transferCRC)>
304 responseMsg{};
305
306 uint8_t retCompletionCode = 0;
307 uint8_t retRecordData[32] = {0};
308 uint32_t retNextRecordHndl = 0;
309 uint32_t retNextDataTransferHndl = 0;
310 uint8_t retTransferFlag = 0;
311 uint16_t retRespCnt = 0;
312 uint8_t retTransferCRC = 0;
313
314 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
315 struct pldm_get_pdr_resp* resp =
316 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
317 resp->completion_code = completionCode;
318 resp->next_record_handle = htole32(nextRecordHndl);
319 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
320 resp->transfer_flag = transferFlag;
321 resp->response_count = htole16(respCnt);
322 memcpy(resp->record_data, recordData, respCnt);
George Liu684a7162019-12-06 15:10:52 +0800323 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
George Liu820a9a52019-11-26 14:43:59 +0800324
325 auto rc = decode_get_pdr_resp(
326 response, responseMsg.size() - hdrSize, &retCompletionCode,
327 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
328 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
329 EXPECT_EQ(rc, PLDM_SUCCESS);
330 EXPECT_EQ(retCompletionCode, completionCode);
331 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
332 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
333 EXPECT_EQ(retTransferFlag, transferFlag);
334 EXPECT_EQ(retRespCnt, respCnt);
335 EXPECT_EQ(retTransferCRC, transferCRC);
336 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
337}
338
339TEST(GetPDR, testBadDecodeResponse)
340{
341 const char* recordData = "123456789";
342 uint8_t completionCode = PLDM_SUCCESS;
343 uint32_t nextRecordHndl = 0;
344 uint32_t nextDataTransferHndl = 0;
345 uint8_t transferFlag = PLDM_END;
346 constexpr uint16_t respCnt = 9;
347 uint8_t transferCRC = 96;
348 size_t recordDataLength = 32;
349 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
350 sizeof(transferCRC)>
351 responseMsg{};
352
353 uint8_t retCompletionCode = 0;
George Liu684a7162019-12-06 15:10:52 +0800354 uint8_t retRecordData[32] = {0};
George Liu820a9a52019-11-26 14:43:59 +0800355 uint32_t retNextRecordHndl = 0;
356 uint32_t retNextDataTransferHndl = 0;
357 uint8_t retTransferFlag = 0;
358 uint16_t retRespCnt = 0;
359 uint8_t retTransferCRC = 0;
360
361 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
362 struct pldm_get_pdr_resp* resp =
363 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
364 resp->completion_code = completionCode;
365 resp->next_record_handle = htole32(nextRecordHndl);
366 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
367 resp->transfer_flag = transferFlag;
368 resp->response_count = htole16(respCnt);
369 memcpy(resp->record_data, recordData, respCnt);
George Liu684a7162019-12-06 15:10:52 +0800370 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
George Liu820a9a52019-11-26 14:43:59 +0800371
372 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
373 NULL, NULL, NULL, NULL, NULL, 0, NULL);
374 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
375
376 rc = decode_get_pdr_resp(
377 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
378 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
379 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
380 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
381}
George Liu30b859f2020-01-07 15:03:22 +0800382
383TEST(SetNumericEffecterValue, testGoodDecodeRequest)
384{
385 std::array<uint8_t,
386 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3>
387 requestMsg{};
388
389 uint16_t effecter_id = 32768;
390 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32;
391 uint32_t effecter_value = 123456789;
392
393 uint16_t reteffecter_id;
394 uint8_t reteffecter_data_size;
395 uint8_t reteffecter_value[4];
396
397 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
398 struct pldm_set_numeric_effecter_value_req* request =
399 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
400 req->payload);
401
402 request->effecter_id = effecter_id;
403 request->effecter_data_size = effecter_data_size;
404 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
405
406 auto rc = decode_set_numeric_effecter_value_req(
407 req, requestMsg.size() - hdrSize, &reteffecter_id,
408 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
409
410 uint32_t value = *(reinterpret_cast<uint32_t*>(&reteffecter_value[0]));
411 EXPECT_EQ(rc, PLDM_SUCCESS);
412 EXPECT_EQ(reteffecter_id, effecter_id);
413 EXPECT_EQ(reteffecter_data_size, effecter_data_size);
414 EXPECT_EQ(value, effecter_value);
415}
416
417TEST(SetNumericEffecterValue, testBadDecodeRequest)
418{
419 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES>
420 requestMsg{};
421
422 auto rc = decode_set_numeric_effecter_value_req(
423 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL);
424 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
425
426 uint16_t effecter_id = 0x10;
427 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
428 uint8_t effecter_value = 1;
429
430 uint16_t reteffecter_id;
431 uint8_t reteffecter_data_size;
432 uint8_t reteffecter_value[4];
433
434 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
435 struct pldm_set_numeric_effecter_value_req* request =
436 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
437 req->payload);
438
439 request->effecter_id = effecter_id;
440 request->effecter_data_size = effecter_data_size;
441 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
442
443 rc = decode_set_numeric_effecter_value_req(
444 req, requestMsg.size() - hdrSize - 1, &reteffecter_id,
445 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
446 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
447}
448
449TEST(SetNumericEffecterValue, testGoodEncodeRequest)
450{
451 uint16_t effecter_id = 0;
452 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
453 uint8_t effecter_value = 1;
454
455 std::vector<uint8_t> requestMsg(
456 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
457 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
458
459 auto rc = encode_set_numeric_effecter_value_req(
460 0, effecter_id, effecter_data_size,
461 reinterpret_cast<uint8_t*>(&effecter_value), request,
462 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
463 EXPECT_EQ(rc, PLDM_SUCCESS);
464
465 struct pldm_set_numeric_effecter_value_req* req =
466 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
467 request->payload);
468 EXPECT_EQ(effecter_id, req->effecter_id);
469 EXPECT_EQ(effecter_data_size, req->effecter_data_size);
470 EXPECT_EQ(effecter_value,
471 *(reinterpret_cast<uint8_t*>(&req->effecter_value[0])));
472}
473
474TEST(SetNumericEffecterValue, testBadEncodeRequest)
475{
476 std::vector<uint8_t> requestMsg(
477 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
478 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
479
480 auto rc = encode_set_numeric_effecter_value_req(
481 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
482 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
483
484 uint16_t effecter_value;
485 rc = encode_set_numeric_effecter_value_req(
486 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
487 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
488 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
489}
490
491TEST(SetNumericEffecterValue, testGoodDecodeResponse)
492{
493 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
494 responseMsg{};
495
496 uint8_t completion_code = 0xA0;
497
498 uint8_t retcompletion_code;
499
500 memcpy(responseMsg.data() + hdrSize, &completion_code,
501 sizeof(completion_code));
502
503 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
504
505 auto rc = decode_set_numeric_effecter_value_resp(
506 response, responseMsg.size() - hdrSize, &retcompletion_code);
507
508 EXPECT_EQ(rc, PLDM_SUCCESS);
509 EXPECT_EQ(completion_code, retcompletion_code);
510}
511
512TEST(SetNumericEffecterValue, testBadDecodeResponse)
513{
514 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
515 responseMsg{};
516
517 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
518
519 auto rc = decode_set_numeric_effecter_value_resp(response,
520 responseMsg.size(), NULL);
521
522 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
523}
524
525TEST(SetNumericEffecterValue, testGoodEncodeResponse)
526{
527 std::array<uint8_t, sizeof(pldm_msg_hdr) +
528 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
529 responseMsg{};
530 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
531 uint8_t completionCode = 0;
532
533 auto rc = encode_set_numeric_effecter_value_resp(
534 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
535
536 EXPECT_EQ(rc, PLDM_SUCCESS);
537 EXPECT_EQ(completionCode, response->payload[0]);
538}
539
540TEST(SetNumericEffecterValue, testBadEncodeResponse)
541{
542 auto rc = encode_set_numeric_effecter_value_resp(
543 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
544 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
545}