blob: 82501e9cf881b79ed6029849ba83d4d65993d7a3 [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
22 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -050023 ASSERT_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
42 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -050043 ASSERT_EQ(effecterId, request->payload[0]);
44 ASSERT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
45 ASSERT_EQ(stateField[0].set_request,
46 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
47 ASSERT_EQ(stateField[0].effecter_state,
48 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
49 sizeof(stateField[0].set_request)]);
50 ASSERT_EQ(stateField[1].set_request,
51 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
52 sizeof(stateField[0])]);
53 ASSERT_EQ(stateField[1].effecter_state,
54 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 completion_code = 0xA0;
65
66 uint8_t retcompletion_code = 0;
67
Zahed Hossain223a73d2019-07-04 12:46:18 -050068 memcpy(responseMsg.data() + hdrSize, &completion_code,
69 sizeof(completion_code));
70
71 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
vkaverap98a2c192019-04-03 05:33:52 -050072
vkaverapa6575b82019-04-03 05:33:52 -050073 auto rc = decode_set_state_effecter_states_resp(
Zahed Hossain223a73d2019-07-04 12:46:18 -050074 response, responseMsg.size() - hdrSize, &retcompletion_code);
vkaverap98a2c192019-04-03 05:33:52 -050075
76 ASSERT_EQ(rc, PLDM_SUCCESS);
77 ASSERT_EQ(completion_code, retcompletion_code);
78}
79
Sampa Misra0db1dfa2019-03-19 00:15:31 -050080TEST(SetStateEffecterStates, testGoodDecodeRequest)
81{
Zahed Hossain223a73d2019-07-04 12:46:18 -050082 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
83 requestMsg{};
Sampa Misra0db1dfa2019-03-19 00:15:31 -050084
Sampa Misra0db1dfa2019-03-19 00:15:31 -050085 uint16_t effecterId = 0x32;
86 uint8_t compEffecterCnt = 0x2;
87
88 std::array<set_effecter_state_field, 8> stateField{};
89 stateField[0] = {PLDM_REQUEST_SET, 3};
90 stateField[1] = {PLDM_REQUEST_SET, 4};
91
92 uint16_t retEffecterId = 0;
93 uint8_t retCompEffecterCnt = 0;
94
95 std::array<set_effecter_state_field, 8> retStateField{};
96
Zahed Hossain223a73d2019-07-04 12:46:18 -050097 memcpy(requestMsg.data() + hdrSize, &effecterId, sizeof(effecterId));
98 memcpy(requestMsg.data() + sizeof(effecterId) + hdrSize, &compEffecterCnt,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050099 sizeof(compEffecterCnt));
Zahed Hossain223a73d2019-07-04 12:46:18 -0500100 memcpy(requestMsg.data() + sizeof(effecterId) + sizeof(compEffecterCnt) +
101 hdrSize,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500102 &stateField, sizeof(stateField));
103
Zahed Hossain223a73d2019-07-04 12:46:18 -0500104 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
105
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500106 auto rc = decode_set_state_effecter_states_req(
Zahed Hossain223a73d2019-07-04 12:46:18 -0500107 request, requestMsg.size() - hdrSize, &retEffecterId,
vkaverapa6575b82019-04-03 05:33:52 -0500108 &retCompEffecterCnt, retStateField.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500109
110 ASSERT_EQ(rc, PLDM_SUCCESS);
111 ASSERT_EQ(effecterId, retEffecterId);
112 ASSERT_EQ(retCompEffecterCnt, compEffecterCnt);
113 ASSERT_EQ(retStateField[0].set_request, stateField[0].set_request);
114 ASSERT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
115 ASSERT_EQ(retStateField[1].set_request, stateField[1].set_request);
116 ASSERT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
117}
118
119TEST(SetStateEffecterStates, testBadDecodeRequest)
120{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500121 const struct pldm_msg* msg = NULL;
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500122
vkaverapa6575b82019-04-03 05:33:52 -0500123 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
124 NULL, NULL);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500125
126 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
127}
vkaverap98a2c192019-04-03 05:33:52 -0500128
129TEST(SetStateEffecterStates, testBadDecodeResponse)
130{
131 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
132 responseMsg{};
133
Zahed Hossain223a73d2019-07-04 12:46:18 -0500134 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
135
136 auto rc = decode_set_state_effecter_states_resp(response,
vkaverapa6575b82019-04-03 05:33:52 -0500137 responseMsg.size(), NULL);
vkaverap98a2c192019-04-03 05:33:52 -0500138
139 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
140}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500141
142TEST(GetPDR, testGoodEncodeResponse)
143{
144 uint8_t completionCode = 0;
145 uint32_t nextRecordHndl = 0x12;
146 uint32_t nextDataTransferHndl = 0x13;
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500147 uint8_t transferFlag = PLDM_END;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500148 uint16_t respCnt = 0x5;
149 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500150 uint8_t transferCRC = 6;
Sampa Misra7fcfb662019-05-08 13:13:53 -0500151
152 // + size of record data and transfer CRC
153 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
154 recordData.size() + 1);
155 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
156
157 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
158 nextDataTransferHndl, transferFlag, respCnt,
159 recordData.data(), transferCRC, response);
160
161 ASSERT_EQ(rc, PLDM_SUCCESS);
162 struct pldm_get_pdr_resp* resp =
163 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
164
165 ASSERT_EQ(completionCode, resp->completion_code);
166 ASSERT_EQ(nextRecordHndl, resp->next_record_handle);
167 ASSERT_EQ(nextDataTransferHndl, resp->next_data_transfer_handle);
168 ASSERT_EQ(transferFlag, resp->transfer_flag);
169 ASSERT_EQ(respCnt, resp->response_count);
170 ASSERT_EQ(0,
171 memcmp(recordData.data(), resp->record_data, recordData.size()));
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500172 ASSERT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
173 recordData.size()),
174 transferCRC);
175
176 transferFlag = PLDM_START_AND_END; // No CRC in this case
177 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
178 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
179 nextDataTransferHndl, transferFlag, respCnt,
180 recordData.data(), transferCRC, response);
181 ASSERT_EQ(rc, PLDM_SUCCESS);
Sampa Misra7fcfb662019-05-08 13:13:53 -0500182}
183
184TEST(GetPDR, testBadEncodeResponse)
185{
186 uint32_t nextRecordHndl = 0x12;
187 uint32_t nextDataTransferHndl = 0x13;
188 uint8_t transferFlag = PLDM_START_AND_END;
189 uint16_t respCnt = 0x5;
190 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
191 uint8_t transferCRC = 0;
192
193 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
194 nextDataTransferHndl, transferFlag, respCnt,
195 recordData.data(), transferCRC, nullptr);
196
197 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
198}
199
200TEST(GetPDR, testGoodDecodeRequest)
201{
202 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
203
204 uint32_t recordHndl = 0x32;
205 uint32_t dataTransferHndl = 0x11;
206 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
207 uint16_t requestCnt = 0x5;
208 uint16_t recordChangeNum = 0;
209
210 uint32_t retRecordHndl = 0;
211 uint32_t retDataTransferHndl = 0;
212 uint8_t retTransferOpFlag = 0;
213 uint16_t retRequestCnt = 0;
214 uint16_t retRecordChangeNum = 0;
215
216 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
217 struct pldm_get_pdr_req* request =
218 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
219
220 request->record_handle = recordHndl;
221 request->data_transfer_handle = dataTransferHndl;
222 request->transfer_op_flag = transferOpFlag;
223 request->request_count = requestCnt;
224 request->record_change_number = recordChangeNum;
225
226 auto rc = decode_get_pdr_req(
227 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
228 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
229
230 ASSERT_EQ(rc, PLDM_SUCCESS);
231 ASSERT_EQ(retRecordHndl, recordHndl);
232 ASSERT_EQ(retDataTransferHndl, dataTransferHndl);
233 ASSERT_EQ(retTransferOpFlag, transferOpFlag);
234 ASSERT_EQ(retRequestCnt, requestCnt);
235 ASSERT_EQ(retRecordChangeNum, recordChangeNum);
236}
237
238TEST(GetPDR, testBadDecodeRequest)
239{
240 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
241 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
242
243 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
244 NULL);
245
246 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
247}
George Liu820a9a52019-11-26 14:43:59 +0800248
249TEST(GetPDR, testGoodEncodeRequest)
250{
251 uint32_t record_hndl = 0;
252 uint32_t data_transfer_hndl = 0;
253 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
254 uint16_t request_cnt = 20;
255 uint16_t record_chg_num = 0;
256
257 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
258 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
259
260 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
261 transfer_op_flag, request_cnt, record_chg_num,
262 request, PLDM_GET_PDR_REQ_BYTES);
263 EXPECT_EQ(rc, PLDM_SUCCESS);
264 struct pldm_get_pdr_req* req =
265 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
266 EXPECT_EQ(record_hndl, req->record_handle);
267 EXPECT_EQ(data_transfer_hndl, req->data_transfer_handle);
268 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
269 EXPECT_EQ(request_cnt, req->request_count);
270 EXPECT_EQ(record_chg_num, req->record_change_number);
271}
272
273TEST(GetPDR, testBadEncodeRequest)
274{
275 uint32_t record_hndl = 0;
276 uint32_t data_transfer_hndl = 0;
277 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
278 uint16_t request_cnt = 32;
279 uint16_t record_chg_num = 0;
280
281 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
282 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
283
284 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
285 transfer_op_flag, request_cnt, record_chg_num,
286 nullptr, PLDM_GET_PDR_REQ_BYTES);
287 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
288
289 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
290 transfer_op_flag, request_cnt, record_chg_num,
291 request, PLDM_GET_PDR_REQ_BYTES + 1);
292 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
293}
294
295TEST(GetPDR, testGoodDecodeResponse)
296{
297 const char* recordData = "123456789";
298 uint8_t completionCode = PLDM_SUCCESS;
299 uint32_t nextRecordHndl = 0;
300 uint32_t nextDataTransferHndl = 0;
301 uint8_t transferFlag = PLDM_END;
302 constexpr uint16_t respCnt = 9;
303 uint8_t transferCRC = 96;
304 size_t recordDataLength = 32;
305 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
306 sizeof(transferCRC)>
307 responseMsg{};
308
309 uint8_t retCompletionCode = 0;
310 uint8_t retRecordData[32] = {0};
311 uint32_t retNextRecordHndl = 0;
312 uint32_t retNextDataTransferHndl = 0;
313 uint8_t retTransferFlag = 0;
314 uint16_t retRespCnt = 0;
315 uint8_t retTransferCRC = 0;
316
317 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
318 struct pldm_get_pdr_resp* resp =
319 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
320 resp->completion_code = completionCode;
321 resp->next_record_handle = htole32(nextRecordHndl);
322 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
323 resp->transfer_flag = transferFlag;
324 resp->response_count = htole16(respCnt);
325 memcpy(resp->record_data, recordData, respCnt);
326 memcpy(response->payload + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt,
327 &transferCRC, 1);
328
329 auto rc = decode_get_pdr_resp(
330 response, responseMsg.size() - hdrSize, &retCompletionCode,
331 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
332 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
333 EXPECT_EQ(rc, PLDM_SUCCESS);
334 EXPECT_EQ(retCompletionCode, completionCode);
335 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
336 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
337 EXPECT_EQ(retTransferFlag, transferFlag);
338 EXPECT_EQ(retRespCnt, respCnt);
339 EXPECT_EQ(retTransferCRC, transferCRC);
340 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
341}
342
343TEST(GetPDR, testBadDecodeResponse)
344{
345 const char* recordData = "123456789";
346 uint8_t completionCode = PLDM_SUCCESS;
347 uint32_t nextRecordHndl = 0;
348 uint32_t nextDataTransferHndl = 0;
349 uint8_t transferFlag = PLDM_END;
350 constexpr uint16_t respCnt = 9;
351 uint8_t transferCRC = 96;
352 size_t recordDataLength = 32;
353 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
354 sizeof(transferCRC)>
355 responseMsg{};
356
357 uint8_t retCompletionCode = 0;
358 uint8_t retRecordData[32];
359 uint32_t retNextRecordHndl = 0;
360 uint32_t retNextDataTransferHndl = 0;
361 uint8_t retTransferFlag = 0;
362 uint16_t retRespCnt = 0;
363 uint8_t retTransferCRC = 0;
364
365 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
366 struct pldm_get_pdr_resp* resp =
367 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
368 resp->completion_code = completionCode;
369 resp->next_record_handle = htole32(nextRecordHndl);
370 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
371 resp->transfer_flag = transferFlag;
372 resp->response_count = htole16(respCnt);
373 memcpy(resp->record_data, recordData, respCnt);
374 memcpy(response->payload + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt,
375 &transferCRC, 1);
376
377 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
378 NULL, NULL, NULL, NULL, NULL, 0, NULL);
379 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
380
381 rc = decode_get_pdr_resp(
382 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
383 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
384 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
386}