xref: /openbmc/libpldm/tests/dsp/bios.cpp (revision d0ba43af)
1 #include <endian.h>
2 #include <libpldm/base.h>
3 #include <libpldm/bios.h>
4 #include <libpldm/utils.h>
5 
6 #include <array>
7 #include <cstdint>
8 #include <cstring>
9 #include <memory>
10 
11 #include <gtest/gtest.h>
12 
13 constexpr auto hdrSize = sizeof(pldm_msg_hdr);
14 
TEST(GetDateTime,testEncodeRequest)15 TEST(GetDateTime, testEncodeRequest)
16 {
17     pldm_msg request{};
18 
19     auto rc = encode_get_date_time_req(0, &request);
20     EXPECT_EQ(rc, PLDM_SUCCESS);
21 }
22 
TEST(GetDateTime,testEncodeResponse)23 TEST(GetDateTime, testEncodeResponse)
24 {
25     uint8_t completionCode = 0;
26     uint8_t seconds = 50;
27     uint8_t minutes = 20;
28     uint8_t hours = 5;
29     uint8_t day = 23;
30     uint8_t month = 11;
31     uint16_t year = 2019;
32 
33     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES>
34         responseMsg{};
35 
36     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
37     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
38 
39     auto rc = encode_get_date_time_resp(0, PLDM_SUCCESS, seconds, minutes,
40                                         hours, day, month, year, response);
41 
42     EXPECT_EQ(rc, PLDM_SUCCESS);
43     EXPECT_EQ(completionCode, response->payload[0]);
44 
45     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
46                         &seconds, sizeof(seconds)));
47     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
48                             sizeof(seconds),
49                         &minutes, sizeof(minutes)));
50     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
51                             sizeof(seconds) + sizeof(minutes),
52                         &hours, sizeof(hours)));
53     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
54                             sizeof(seconds) + sizeof(minutes) + sizeof(hours),
55                         &day, sizeof(day)));
56     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
57                             sizeof(seconds) + sizeof(minutes) + sizeof(hours) +
58                             sizeof(day),
59                         &month, sizeof(month)));
60     uint16_t yearLe = htole16(year);
61     EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
62                             sizeof(seconds) + sizeof(minutes) + sizeof(hours) +
63                             sizeof(day) + sizeof(month),
64                         &yearLe, sizeof(yearLe)));
65 }
66 
TEST(GetDateTime,testDecodeResponse)67 TEST(GetDateTime, testDecodeResponse)
68 {
69     std::array<uint8_t, hdrSize + PLDM_GET_DATE_TIME_RESP_BYTES> responseMsg{};
70 
71     uint8_t completionCode = 0;
72 
73     uint8_t seconds = 55;
74     uint8_t minutes = 2;
75     uint8_t hours = 8;
76     uint8_t day = 9;
77     uint8_t month = 7;
78     uint16_t year = 2020;
79     uint16_t yearLe = htole16(year);
80 
81     uint8_t retSeconds = 0;
82     uint8_t retMinutes = 0;
83     uint8_t retHours = 0;
84     uint8_t retDay = 0;
85     uint8_t retMonth = 0;
86     uint16_t retYear = 0;
87 
88     memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &seconds,
89            sizeof(seconds));
90     memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
91                hdrSize,
92            &minutes, sizeof(minutes));
93     memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
94                sizeof(minutes) + hdrSize,
95            &hours, sizeof(hours));
96     memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
97                sizeof(minutes) + sizeof(hours) + hdrSize,
98            &day, sizeof(day));
99     memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
100                sizeof(minutes) + sizeof(hours) + sizeof(day) + hdrSize,
101            &month, sizeof(month));
102     memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
103                sizeof(minutes) + sizeof(hours) + sizeof(day) + sizeof(month) +
104                hdrSize,
105            &yearLe, sizeof(yearLe));
106 
107     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
108     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
109 
110     auto rc = decode_get_date_time_resp(
111         response, responseMsg.size() - hdrSize, &completionCode, &retSeconds,
112         &retMinutes, &retHours, &retDay, &retMonth, &retYear);
113 
114     EXPECT_EQ(rc, PLDM_SUCCESS);
115     EXPECT_EQ(seconds, retSeconds);
116     EXPECT_EQ(minutes, retMinutes);
117     EXPECT_EQ(hours, retHours);
118     EXPECT_EQ(day, retDay);
119     EXPECT_EQ(month, retMonth);
120     EXPECT_EQ(year, retYear);
121 }
122 
TEST(SetDateTime,testGoodEncodeResponse)123 TEST(SetDateTime, testGoodEncodeResponse)
124 {
125     uint8_t instanceId = 0;
126     uint8_t completionCode = PLDM_SUCCESS;
127 
128     std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
129         responseMsg{};
130     struct pldm_msg* response =
131         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
132         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
133 
134     auto rc = encode_set_date_time_resp(instanceId, completionCode, response,
135                                         responseMsg.size() - hdrSize);
136     EXPECT_EQ(rc, PLDM_SUCCESS);
137 
138     struct pldm_only_cc_resp* resp =
139         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
140         reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
141     EXPECT_EQ(completionCode, resp->completion_code);
142 }
143 
TEST(SetDateTime,testBadEncodeResponse)144 TEST(SetDateTime, testBadEncodeResponse)
145 {
146 
147     uint8_t instanceId = 10;
148     uint8_t completionCode = PLDM_SUCCESS;
149     std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
150         responseMsg{};
151     struct pldm_msg* response =
152         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
153         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
154     auto rc = encode_set_date_time_resp(instanceId, completionCode, nullptr,
155                                         responseMsg.size() - hdrSize);
156 
157     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
158     rc = encode_set_date_time_resp(instanceId, completionCode, response,
159                                    responseMsg.size() - hdrSize - 1);
160 
161     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
162 }
163 
TEST(SetDateTime,testGoodDecodeResponse)164 TEST(SetDateTime, testGoodDecodeResponse)
165 {
166     uint8_t completionCode = PLDM_SUCCESS;
167     std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
168         responseMsg{};
169     struct pldm_msg* response =
170         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
171         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
172     struct pldm_only_cc_resp* resp =
173         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
174         reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
175 
176     resp->completion_code = completionCode;
177 
178     uint8_t retCompletionCode;
179     auto rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize,
180                                         &retCompletionCode);
181 
182     EXPECT_EQ(rc, PLDM_SUCCESS);
183     EXPECT_EQ(completionCode, retCompletionCode);
184 }
185 
TEST(SetDateTime,testBadDecodeResponse)186 TEST(SetDateTime, testBadDecodeResponse)
187 {
188     uint8_t completionCode = PLDM_SUCCESS;
189 
190     std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
191         responseMsg{};
192     struct pldm_msg* response =
193         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
194         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
195     auto rc = decode_set_date_time_resp(nullptr, responseMsg.size() - hdrSize,
196                                         &completionCode);
197     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
198 
199     rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize - 1,
200                                    &completionCode);
201 
202     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
203 }
204 
TEST(SetDateTime,testGoodEncodeRequset)205 TEST(SetDateTime, testGoodEncodeRequset)
206 {
207     uint8_t instanceId = 0;
208     uint8_t seconds = 50;
209     uint8_t minutes = 20;
210     uint8_t hours = 10;
211     uint8_t day = 11;
212     uint8_t month = 11;
213     uint16_t year = 2019;
214 
215     std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
216         requestMsg{};
217 
218     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
219     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
220     auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
221                                        month, year, request,
222                                        requestMsg.size() - hdrSize);
223 
224     EXPECT_EQ(rc, PLDM_SUCCESS);
225 
226     struct pldm_set_date_time_req* req =
227         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
228         reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
229     EXPECT_EQ(seconds, bcd2dec8(req->seconds));
230     EXPECT_EQ(minutes, bcd2dec8(req->minutes));
231     EXPECT_EQ(hours, bcd2dec8(req->hours));
232     EXPECT_EQ(day, bcd2dec8(req->day));
233     EXPECT_EQ(month, bcd2dec8(req->month));
234     EXPECT_EQ(year, bcd2dec16(le16toh(req->year)));
235 }
236 
TEST(SetDateTime,testBadEncodeRequset)237 TEST(SetDateTime, testBadEncodeRequset)
238 {
239     uint8_t instanceId = 0;
240 
241     uint8_t seconds = 50;
242     uint8_t minutes = 20;
243     uint8_t hours = 10;
244     uint8_t day = 13;
245     uint8_t month = 11;
246     uint16_t year = 2019;
247 
248     uint8_t erday = 43;
249 
250     std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
251         requestMsg{};
252 
253     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
254     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
255 
256     auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
257                                        month, year, nullptr,
258                                        requestMsg.size() - hdrSize);
259     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
260 
261     rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, erday,
262                                   month, year, request,
263                                   requestMsg.size() - hdrSize);
264     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
265 
266     rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
267                                   month, year, request,
268                                   requestMsg.size() - hdrSize - 4);
269     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
270 }
271 
TEST(SetDateTime,testGoodDecodeRequest)272 TEST(SetDateTime, testGoodDecodeRequest)
273 {
274     std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
275         requestMsg{};
276     uint8_t seconds = 0x50;
277     uint8_t minutes = 0x20;
278     uint8_t hours = 0x10;
279     uint8_t day = 0x11;
280     uint8_t month = 0x11;
281     uint16_t year = 0x2019;
282 
283     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
284     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
285     struct pldm_set_date_time_req* req =
286         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
287         reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
288     req->seconds = seconds;
289     req->minutes = minutes;
290     req->hours = hours;
291     req->day = day;
292     req->month = month;
293     req->year = htole16(year);
294 
295     uint8_t retseconds;
296     uint8_t retminutes;
297     uint8_t rethours;
298     uint8_t retday;
299     uint8_t retmonth;
300     uint16_t retyear;
301 
302     auto rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize,
303                                        &retseconds, &retminutes, &rethours,
304                                        &retday, &retmonth, &retyear);
305 
306     EXPECT_EQ(rc, PLDM_SUCCESS);
307     EXPECT_EQ(retseconds, 50);
308     EXPECT_EQ(retminutes, 20);
309     EXPECT_EQ(rethours, 10);
310     EXPECT_EQ(retday, 11);
311     EXPECT_EQ(retmonth, 11);
312     EXPECT_EQ(retyear, 2019);
313 }
314 
TEST(SetDateTime,testBadDecodeRequest)315 TEST(SetDateTime, testBadDecodeRequest)
316 {
317     uint8_t seconds = 0x50;
318     uint8_t minutes = 0x20;
319     uint8_t hours = 0x10;
320     uint8_t day = 0x11;
321     uint8_t month = 0x11;
322     uint16_t year = htole16(0x2019);
323 
324     std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
325         requestMsg{};
326 
327     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
328     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
329 
330     decode_set_date_time_req(request, requestMsg.size() - hdrSize, &seconds,
331                              &minutes, &hours, &day, &month, &year);
332 
333     auto rc =
334         decode_set_date_time_req(nullptr, requestMsg.size() - hdrSize, &seconds,
335                                  &minutes, &hours, &day, &month, &year);
336     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
337 
338     rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize, nullptr,
339                                   nullptr, nullptr, nullptr, nullptr, nullptr);
340     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
341 
342     rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize - 4,
343                                   &seconds, &minutes, &hours, &day, &month,
344                                   &year);
345     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
346 }
347 
TEST(GetBIOSTable,testGoodEncodeResponse)348 TEST(GetBIOSTable, testGoodEncodeResponse)
349 {
350     std::array<uint8_t,
351                sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4>
352         responseMsg{};
353     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
354     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
355 
356     uint8_t completionCode = PLDM_SUCCESS;
357     uint32_t nextTransferHandle = 32;
358     uint8_t transferFlag = PLDM_START_AND_END;
359     std::array<uint8_t, 4> tableData{1, 2, 3, 4};
360 
361     auto rc = encode_get_bios_table_resp(
362         0, PLDM_SUCCESS, nextTransferHandle, transferFlag, tableData.data(),
363         sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4,
364         response);
365     EXPECT_EQ(rc, PLDM_SUCCESS);
366 
367     struct pldm_get_bios_table_resp* resp =
368         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
369         reinterpret_cast<struct pldm_get_bios_table_resp*>(response->payload);
370 
371     EXPECT_EQ(completionCode, resp->completion_code);
372     EXPECT_EQ(nextTransferHandle, le32toh(resp->next_transfer_handle));
373     EXPECT_EQ(transferFlag, resp->transfer_flag);
374     EXPECT_EQ(0, memcmp(tableData.data(), resp->table_data, tableData.size()));
375 }
376 
TEST(GetBIOSTable,testBadEncodeResponse)377 TEST(GetBIOSTable, testBadEncodeResponse)
378 {
379     uint32_t nextTransferHandle = 32;
380     uint8_t transferFlag = PLDM_START_AND_END;
381     std::array<uint8_t, 4> tableData{1, 2, 3, 4};
382 
383     auto rc = encode_get_bios_table_resp(
384         0, PLDM_SUCCESS, nextTransferHandle, transferFlag, tableData.data(),
385         sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4, nullptr);
386     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
387 }
388 
TEST(GetBIOSTable,testGoodEncodeRequest)389 TEST(GetBIOSTable, testGoodEncodeRequest)
390 {
391     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES>
392         requestMsg{};
393     uint32_t transferHandle = 0x0;
394     uint8_t transferOpFlag = 0x01;
395     uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
396 
397     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
398     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
399     auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
400                                         tableType, request);
401 
402     EXPECT_EQ(rc, PLDM_SUCCESS);
403 
404     struct pldm_get_bios_table_req* req =
405         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
406         reinterpret_cast<struct pldm_get_bios_table_req*>(request->payload);
407     EXPECT_EQ(transferHandle, le32toh(req->transfer_handle));
408     EXPECT_EQ(transferOpFlag, req->transfer_op_flag);
409     EXPECT_EQ(tableType, req->table_type);
410 }
411 
TEST(GetBIOSTable,testBadEncodeRequest)412 TEST(GetBIOSTable, testBadEncodeRequest)
413 {
414     uint32_t transferHandle = 0x0;
415     uint8_t transferOpFlag = 0x01;
416     uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
417 
418     auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
419                                         tableType, nullptr);
420 
421     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
422 }
423 
TEST(GetBIOSTable,testGoodDecodeRequest)424 TEST(GetBIOSTable, testGoodDecodeRequest)
425 {
426     const auto hdr_size = sizeof(pldm_msg_hdr);
427     std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
428     uint32_t transferHandle = 31;
429     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
430     uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
431     uint32_t retTransferHandle = 0;
432     uint8_t retTransferOpFlag = 0;
433     uint8_t retTableType = 0;
434 
435     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
436     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
437     struct pldm_get_bios_table_req* request =
438         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
439         reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
440 
441     request->transfer_handle = htole32(transferHandle);
442     request->transfer_op_flag = transferOpFlag;
443     request->table_type = tableType;
444 
445     auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size,
446                                         &retTransferHandle, &retTransferOpFlag,
447                                         &retTableType);
448 
449     EXPECT_EQ(rc, PLDM_SUCCESS);
450     EXPECT_EQ(transferHandle, retTransferHandle);
451     EXPECT_EQ(transferOpFlag, retTransferOpFlag);
452     EXPECT_EQ(tableType, retTableType);
453 }
TEST(GetBIOSTable,testBadDecodeRequest)454 TEST(GetBIOSTable, testBadDecodeRequest)
455 {
456     const auto hdr_size = sizeof(pldm_msg_hdr);
457     std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
458     uint32_t transferHandle = 31;
459     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
460     uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
461     uint32_t retTransferHandle = 0;
462     uint8_t retTransferOpFlag = 0;
463     uint8_t retTableType = 0;
464 
465     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
466     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
467     struct pldm_get_bios_table_req* request =
468         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
469         reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
470 
471     request->transfer_handle = htole32(transferHandle);
472     request->transfer_op_flag = transferOpFlag;
473     request->table_type = tableType;
474 
475     auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size,
476                                         &retTransferHandle, &retTransferOpFlag,
477                                         &retTableType);
478 
479     EXPECT_EQ(rc, PLDM_SUCCESS);
480     EXPECT_EQ(transferHandle, retTransferHandle);
481     EXPECT_EQ(transferOpFlag, retTransferOpFlag);
482     EXPECT_EQ(tableType, retTableType);
483 }
484 /*
485 TEST(GetBIOSTable, testBadDecodeRequest)
486 {
487     const auto hdr_size = sizeof(pldm_msg_hdr);
488     std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
489     uint32_t transferHandle = 31;
490     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
491     uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
492     uint32_t retTransferHandle = 0;
493     uint8_t retTransferOpFlag = 0;
494     uint8_t retTableType = 0;
495 
496 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
497     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
498     struct pldm_get_bios_table_req* request =
499 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
500         reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
501 
502     request->transfer_handle = htole32(transferHandle);
503     request->transfer_op_flag = transferOpFlag;
504     request->table_type = tableType;
505 
506     auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size - 3,
507                                         &retTransferHandle, &retTransferOpFlag,
508                                         &retTableType);
509 
510     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
511 }*/
512 
TEST(GetBIOSAttributeCurrentValueByHandle,testGoodDecodeRequest)513 TEST(GetBIOSAttributeCurrentValueByHandle, testGoodDecodeRequest)
514 {
515     uint32_t transferHandle = 45;
516     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
517     uint16_t attributehandle = 10;
518     uint32_t retTransferHandle = 0;
519     uint8_t retTransferOpFlag = 0;
520     uint16_t retattributehandle = 0;
521     std::array<uint8_t, hdrSize + sizeof(transferHandle) +
522                             sizeof(transferOpFlag) + sizeof(attributehandle)>
523         requestMsg{};
524 
525     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
526     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
527     struct pldm_get_bios_attribute_current_value_by_handle_req* request =
528         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
529         reinterpret_cast<
530             struct pldm_get_bios_attribute_current_value_by_handle_req*>(
531             req->payload);
532 
533     request->transfer_handle = htole32(transferHandle);
534     request->transfer_op_flag = transferOpFlag;
535     request->attribute_handle = htole16(attributehandle);
536 
537     auto rc = decode_get_bios_attribute_current_value_by_handle_req(
538         req, requestMsg.size() - hdrSize, &retTransferHandle,
539         &retTransferOpFlag, &retattributehandle);
540 
541     EXPECT_EQ(rc, PLDM_SUCCESS);
542     EXPECT_EQ(transferHandle, retTransferHandle);
543     EXPECT_EQ(transferOpFlag, retTransferOpFlag);
544     EXPECT_EQ(attributehandle, retattributehandle);
545 }
546 
TEST(GetBIOSAttributeCurrentValueByHandle,testBadDecodeRequest)547 TEST(GetBIOSAttributeCurrentValueByHandle, testBadDecodeRequest)
548 {
549 
550     uint32_t transferHandle = 0;
551     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
552     uint16_t attribute_handle = 0;
553     uint32_t retTransferHandle = 0;
554     uint8_t retTransferOpFlag = 0;
555     uint16_t retattribute_handle = 0;
556     std::array<uint8_t, hdrSize + sizeof(transferHandle) +
557                             sizeof(transferOpFlag) + sizeof(attribute_handle)>
558         requestMsg{};
559 
560     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
561     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
562     struct pldm_get_bios_attribute_current_value_by_handle_req* request =
563         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
564         reinterpret_cast<
565             struct pldm_get_bios_attribute_current_value_by_handle_req*>(
566             req->payload);
567 
568     request->transfer_handle = htole32(transferHandle);
569     request->transfer_op_flag = transferOpFlag;
570     request->attribute_handle = attribute_handle;
571 
572     auto rc = decode_get_bios_attribute_current_value_by_handle_req(
573         NULL, requestMsg.size() - hdrSize, &retTransferHandle,
574         &retTransferOpFlag, &retattribute_handle);
575     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
576 
577     transferHandle = 31;
578     request->transfer_handle = htole32(transferHandle);
579 
580     rc = decode_get_bios_attribute_current_value_by_handle_req(
581         req, 0, &retTransferHandle, &retTransferOpFlag, &retattribute_handle);
582 
583     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
584 }
585 
TEST(GetBIOSAttributeCurrentValueByHandle,testGoodEncodeRequest)586 TEST(GetBIOSAttributeCurrentValueByHandle, testGoodEncodeRequest)
587 {
588     std::array<uint8_t, sizeof(pldm_msg_hdr) +
589                             PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES>
590         requestMsg{};
591     uint32_t transferHandle = 45;
592     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
593     uint8_t attributeHandle = 10;
594 
595     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
596     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
597     auto rc = encode_get_bios_attribute_current_value_by_handle_req(
598         0, transferHandle, transferOpFlag, attributeHandle, request);
599 
600     EXPECT_EQ(rc, PLDM_SUCCESS);
601 
602     struct pldm_get_bios_attribute_current_value_by_handle_req* req =
603         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
604         reinterpret_cast<
605             struct pldm_get_bios_attribute_current_value_by_handle_req*>(
606             request->payload);
607     EXPECT_EQ(transferHandle, le32toh(req->transfer_handle));
608     EXPECT_EQ(transferOpFlag, req->transfer_op_flag);
609     EXPECT_EQ(attributeHandle, le16toh(req->attribute_handle));
610 }
611 
TEST(GetBIOSAttributeCurrentValueByHandle,testBadEncodeRequest)612 TEST(GetBIOSAttributeCurrentValueByHandle, testBadEncodeRequest)
613 {
614     uint32_t transferHandle = 0;
615     uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
616     uint8_t attributeHandle = 0;
617 
618     auto rc = encode_get_bios_attribute_current_value_by_handle_req(
619         0, transferHandle, transferOpFlag, attributeHandle, nullptr);
620 
621     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
622 }
623 
TEST(GetBIOSAttributeCurrentValueByHandle,testGoodEncodeResponse)624 TEST(GetBIOSAttributeCurrentValueByHandle, testGoodEncodeResponse)
625 {
626 
627     uint8_t instanceId = 10;
628     uint8_t completionCode = PLDM_SUCCESS;
629     uint32_t nextTransferHandle = 32;
630     uint8_t transferFlag = PLDM_START_AND_END;
631     uint8_t attributeData = 44;
632     std::array<uint8_t,
633                hdrSize +
634                    sizeof(pldm_get_bios_attribute_current_value_by_handle_resp)>
635         responseMsg{};
636     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
637     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
638 
639     auto rc = encode_get_bios_current_value_by_handle_resp(
640         instanceId, completionCode, nextTransferHandle, transferFlag,
641         &attributeData, sizeof(attributeData), response);
642 
643     EXPECT_EQ(rc, PLDM_SUCCESS);
644 
645     struct pldm_get_bios_attribute_current_value_by_handle_resp* resp =
646         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
647         reinterpret_cast<
648             struct pldm_get_bios_attribute_current_value_by_handle_resp*>(
649             response->payload);
650 
651     EXPECT_EQ(completionCode, resp->completion_code);
652     EXPECT_EQ(nextTransferHandle, le32toh(resp->next_transfer_handle));
653     EXPECT_EQ(transferFlag, resp->transfer_flag);
654     EXPECT_EQ(
655         0, memcmp(&attributeData, resp->attribute_data, sizeof(attributeData)));
656 }
657 
TEST(GetBIOSAttributeCurrentValueByHandle,testBadEncodeResponse)658 TEST(GetBIOSAttributeCurrentValueByHandle, testBadEncodeResponse)
659 {
660     uint32_t nextTransferHandle = 32;
661     uint8_t transferFlag = PLDM_START_AND_END;
662     uint8_t attributeData = 44;
663 
664     auto rc = encode_get_bios_current_value_by_handle_resp(
665         0, PLDM_SUCCESS, nextTransferHandle, transferFlag, &attributeData,
666         sizeof(attributeData), nullptr);
667     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
668 
669     std::array<uint8_t,
670                hdrSize +
671                    sizeof(pldm_get_bios_attribute_current_value_by_handle_resp)>
672         responseMsg{};
673     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
674     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
675     rc = encode_get_bios_current_value_by_handle_resp(
676         0, PLDM_SUCCESS, nextTransferHandle, transferFlag, nullptr,
677         sizeof(attributeData), response);
678     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
679 }
680 
TEST(SetBiosAttributeCurrentValue,testGoodEncodeRequest)681 TEST(SetBiosAttributeCurrentValue, testGoodEncodeRequest)
682 {
683     uint8_t instanceId = 10;
684     uint32_t transferHandle = 32;
685     uint8_t transferFlag = PLDM_START_AND_END;
686     uint32_t attributeData = 44;
687     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
688                             sizeof(attributeData)>
689         requestMsg{};
690     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
691     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
692     auto rc = encode_set_bios_attribute_current_value_req(
693         instanceId, transferHandle, transferFlag,
694         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
695         reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
696         request, requestMsg.size() - hdrSize);
697 
698     EXPECT_EQ(rc, PLDM_SUCCESS);
699 
700     struct pldm_set_bios_attribute_current_value_req* req =
701         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
702         reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
703             request->payload);
704     EXPECT_EQ(htole32(transferHandle), req->transfer_handle);
705     EXPECT_EQ(transferFlag, req->transfer_flag);
706     EXPECT_EQ(
707         0, memcmp(&attributeData, req->attribute_data, sizeof(attributeData)));
708 }
709 
TEST(SetBiosAttributeCurrentValue,testBadEncodeRequest)710 TEST(SetBiosAttributeCurrentValue, testBadEncodeRequest)
711 {
712     uint8_t instanceId = 10;
713     uint32_t transferHandle = 32;
714     uint8_t transferFlag = PLDM_START_AND_END;
715     uint32_t attributeData = 44;
716     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES>
717         requestMsg{};
718     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
719     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
720 
721     auto rc = encode_set_bios_attribute_current_value_req(
722         instanceId, transferHandle, transferFlag, nullptr, 0, nullptr, 0);
723     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
724     rc = encode_set_bios_attribute_current_value_req(
725         instanceId, transferHandle, transferFlag,
726         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
727         reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
728         request, requestMsg.size() - hdrSize);
729 
730     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
731 }
732 
TEST(SetBiosAttributeCurrentValue,testGoodDecodeRequest)733 TEST(SetBiosAttributeCurrentValue, testGoodDecodeRequest)
734 {
735     uint32_t transferHandle = 32;
736     uint8_t transferFlag = PLDM_START_AND_END;
737     uint32_t attributeData = 44;
738 
739     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
740                             sizeof(attributeData)>
741         requestMsg{};
742     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
743     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
744     struct pldm_set_bios_attribute_current_value_req* req =
745         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
746         reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
747             request->payload);
748     req->transfer_handle = htole32(transferHandle);
749     req->transfer_flag = transferFlag;
750     memcpy(req->attribute_data, &attributeData, sizeof(attributeData));
751 
752     uint32_t retTransferHandle;
753     uint8_t retTransferFlag;
754     struct variable_field attribute;
755     auto rc = decode_set_bios_attribute_current_value_req(
756         request, requestMsg.size() - hdrSize, &retTransferHandle,
757         &retTransferFlag, &attribute);
758 
759     EXPECT_EQ(rc, PLDM_SUCCESS);
760     EXPECT_EQ(retTransferHandle, transferHandle);
761     EXPECT_EQ(retTransferFlag, transferFlag);
762     EXPECT_EQ(attribute.length, sizeof(attributeData));
763     EXPECT_EQ(0, memcmp(attribute.ptr, &attributeData, sizeof(attributeData)));
764 }
765 
TEST(SetBiosAttributeCurrentValue,testBadDecodeRequest)766 TEST(SetBiosAttributeCurrentValue, testBadDecodeRequest)
767 {
768     uint32_t transferHandle = 32;
769     uint8_t transferFlag = PLDM_START_AND_END;
770     struct variable_field attribute;
771     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES - 1>
772         requestMsg{};
773     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
774     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
775 
776     auto rc = decode_set_bios_attribute_current_value_req(
777         nullptr, 0, &transferHandle, &transferFlag, &attribute);
778     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
779     rc = decode_set_bios_attribute_current_value_req(
780         request, requestMsg.size() - hdrSize, &transferHandle, &transferFlag,
781         &attribute);
782     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
783 }
784 
TEST(SetBiosAttributeCurrentValue,testGoodEncodeResponse)785 TEST(SetBiosAttributeCurrentValue, testGoodEncodeResponse)
786 {
787     uint8_t instanceId = 10;
788     uint32_t nextTransferHandle = 32;
789     uint8_t completionCode = PLDM_SUCCESS;
790 
791     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
792         responseMsg{};
793     struct pldm_msg* response =
794         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
795         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
796     auto rc = encode_set_bios_attribute_current_value_resp(
797         instanceId, completionCode, nextTransferHandle, response);
798     EXPECT_EQ(rc, PLDM_SUCCESS);
799 
800     struct pldm_set_bios_attribute_current_value_resp* resp =
801         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
802         reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
803             response->payload);
804     EXPECT_EQ(completionCode, resp->completion_code);
805     EXPECT_EQ(htole32(nextTransferHandle), resp->next_transfer_handle);
806 }
807 
TEST(SetBiosAttributeCurrentValue,testBadEncodeResponse)808 TEST(SetBiosAttributeCurrentValue, testBadEncodeResponse)
809 {
810     uint8_t instanceId = 10;
811     uint32_t nextTransferHandle = 32;
812     uint8_t completionCode = PLDM_SUCCESS;
813     auto rc = encode_set_bios_attribute_current_value_resp(
814         instanceId, completionCode, nextTransferHandle, nullptr);
815 
816     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
817 }
TEST(SetBiosAttributeCurrentValue,testGoodDecodeResponse)818 TEST(SetBiosAttributeCurrentValue, testGoodDecodeResponse)
819 {
820     uint32_t nextTransferHandle = 32;
821     uint8_t completionCode = PLDM_SUCCESS;
822     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
823         responseMsg{};
824     struct pldm_msg* response =
825         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
826         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
827     struct pldm_set_bios_attribute_current_value_resp* resp =
828         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
829         reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
830             response->payload);
831 
832     resp->completion_code = completionCode;
833     resp->next_transfer_handle = htole32(nextTransferHandle);
834 
835     uint8_t retCompletionCode;
836     uint32_t retNextTransferHandle;
837     auto rc = decode_set_bios_attribute_current_value_resp(
838         response, responseMsg.size() - hdrSize, &retCompletionCode,
839         &retNextTransferHandle);
840 
841     EXPECT_EQ(rc, PLDM_SUCCESS);
842     EXPECT_EQ(completionCode, retCompletionCode);
843     EXPECT_EQ(nextTransferHandle, retNextTransferHandle);
844 }
845 
TEST(SetBiosAttributeCurrentValue,testBadDecodeResponse)846 TEST(SetBiosAttributeCurrentValue, testBadDecodeResponse)
847 {
848     uint32_t nextTransferHandle = 32;
849     uint8_t completionCode = PLDM_SUCCESS;
850 
851     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
852         responseMsg{};
853     struct pldm_msg* response =
854         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
855         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
856     struct pldm_set_bios_attribute_current_value_resp* resp =
857         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
858         reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
859             response->payload);
860 
861     resp->completion_code = completionCode;
862     resp->next_transfer_handle = htole32(nextTransferHandle);
863 
864     auto rc = decode_set_bios_attribute_current_value_resp(
865         nullptr, 0, &completionCode, &nextTransferHandle);
866     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
867 
868     rc = decode_set_bios_attribute_current_value_resp(
869         response, responseMsg.size() - hdrSize - 1, &completionCode,
870         &nextTransferHandle);
871 
872     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
873 }
874 
TEST(GetBIOSTable,testDecodeResponse)875 TEST(GetBIOSTable, testDecodeResponse)
876 {
877     uint32_t nextTransferHandle = 32;
878     uint8_t completionCode = PLDM_SUCCESS;
879     uint8_t transfer_flag = PLDM_START_AND_END;
880 
881     std::array<uint8_t, hdrSize + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES>
882         responseMsg{};
883     struct pldm_msg* response =
884         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
885         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
886 
887     struct pldm_get_bios_table_resp* resp =
888         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
889         reinterpret_cast<struct pldm_get_bios_table_resp*>(response->payload);
890 
891     resp->completion_code = completionCode;
892     resp->next_transfer_handle = htole32(nextTransferHandle);
893     resp->transfer_flag = transfer_flag;
894     size_t biosTableOffset = sizeof(completionCode) +
895                              sizeof(nextTransferHandle) + sizeof(transfer_flag);
896 
897     uint8_t retCompletionCode;
898     uint32_t retNextTransferHandle;
899     uint8_t retransfer_flag;
900     size_t rebiosTableOffset = 0;
901     auto rc = decode_get_bios_table_resp(
902         response, responseMsg.size(), &retCompletionCode,
903         &retNextTransferHandle, &retransfer_flag, &rebiosTableOffset);
904 
905     ASSERT_EQ(rc, PLDM_SUCCESS);
906     ASSERT_EQ(completionCode, retCompletionCode);
907     ASSERT_EQ(nextTransferHandle, retNextTransferHandle);
908     ASSERT_EQ(transfer_flag, retransfer_flag);
909     ASSERT_EQ(biosTableOffset, rebiosTableOffset);
910 }
911 
TEST(GetBIOSAttributeCurrentValueByHandle,testDecodeResponse)912 TEST(GetBIOSAttributeCurrentValueByHandle, testDecodeResponse)
913 {
914     uint32_t nextTransferHandle = 32;
915     uint8_t completionCode = PLDM_SUCCESS;
916     uint8_t transfer_flag = PLDM_START_AND_END;
917     uint32_t attributeData = 44;
918 
919     std::array<uint8_t,
920                hdrSize + PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES +
921                    sizeof(attributeData)>
922         responseMsg{};
923     struct pldm_msg* response =
924         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
925         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
926 
927     struct pldm_get_bios_attribute_current_value_by_handle_resp* resp =
928         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
929         reinterpret_cast<
930             struct pldm_get_bios_attribute_current_value_by_handle_resp*>(
931             response->payload);
932 
933     resp->completion_code = completionCode;
934     resp->next_transfer_handle = htole32(nextTransferHandle);
935     resp->transfer_flag = transfer_flag;
936     memcpy(resp->attribute_data, &attributeData, sizeof(attributeData));
937 
938     uint8_t retCompletionCode;
939     uint32_t retNextTransferHandle;
940     uint8_t retransfer_flag;
941     struct variable_field retAttributeData;
942     auto rc = decode_get_bios_attribute_current_value_by_handle_resp(
943         response, responseMsg.size() - hdrSize, &retCompletionCode,
944         &retNextTransferHandle, &retransfer_flag, &retAttributeData);
945 
946     EXPECT_EQ(rc, PLDM_SUCCESS);
947     EXPECT_EQ(completionCode, retCompletionCode);
948     EXPECT_EQ(nextTransferHandle, retNextTransferHandle);
949     EXPECT_EQ(transfer_flag, retransfer_flag);
950     EXPECT_EQ(sizeof(attributeData), retAttributeData.length);
951     EXPECT_EQ(
952         0, memcmp(retAttributeData.ptr, &attributeData, sizeof(attributeData)));
953 }
954 
TEST(SetBIOSTable,testGoodEncodeRequest)955 TEST(SetBIOSTable, testGoodEncodeRequest)
956 {
957     uint8_t instanceId = 10;
958     uint32_t transferHandle = 32;
959     uint8_t transferFlag = PLDM_START_AND_END;
960     uint8_t tableType = PLDM_BIOS_STRING_TABLE;
961     uint32_t tableData = 44;
962     std::array<uint8_t,
963                hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
964         requestMsg{};
965     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
966     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
967     auto rc = encode_set_bios_table_req(
968         instanceId, transferHandle, transferFlag, tableType,
969         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
970         reinterpret_cast<uint8_t*>(&tableData), sizeof(tableData), request,
971         requestMsg.size() - hdrSize);
972 
973     EXPECT_EQ(rc, PLDM_SUCCESS);
974 
975     struct pldm_set_bios_table_req* req =
976         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
977         reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
978 
979     EXPECT_EQ(htole32(transferHandle), req->transfer_handle);
980     EXPECT_EQ(transferFlag, req->transfer_flag);
981     EXPECT_EQ(tableType, req->table_type);
982     EXPECT_EQ(0, memcmp(&tableData, req->table_data, sizeof(tableData)));
983 }
984 
TEST(SetBIOSTable,testBadEncodeRequest)985 TEST(SetBIOSTable, testBadEncodeRequest)
986 {
987     uint8_t instanceId = 10;
988     uint32_t transferHandle = 32;
989     uint8_t transferFlag = PLDM_START_AND_END;
990     uint8_t tableType = PLDM_BIOS_STRING_TABLE;
991     uint32_t tableData = 44;
992     std::array<uint8_t,
993                hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
994         requestMsg{};
995     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
996     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
997 
998     auto rc = encode_set_bios_table_req(
999         instanceId, transferHandle, transferFlag, tableType, NULL,
1000         sizeof(tableData), request, requestMsg.size() - hdrSize);
1001     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1002 
1003     rc = encode_set_bios_table_req(
1004         instanceId, transferHandle, transferFlag, tableType,
1005         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1006         reinterpret_cast<uint8_t*>(&tableData), sizeof(tableData), request,
1007         requestMsg.size() - hdrSize + 1);
1008     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1009 }
1010 
TEST(SetBIOSTable,testGoodDecodeResponse)1011 TEST(SetBIOSTable, testGoodDecodeResponse)
1012 {
1013     uint32_t nextTransferHandle = 32;
1014     uint8_t completionCode = PLDM_SUCCESS;
1015     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
1016     struct pldm_msg* response =
1017         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1018         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
1019     struct pldm_set_bios_table_resp* resp =
1020         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1021         reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
1022 
1023     resp->completion_code = completionCode;
1024     resp->next_transfer_handle = htole32(nextTransferHandle);
1025 
1026     uint8_t retCompletionCode;
1027     uint32_t retNextTransferHandle;
1028     auto rc =
1029         decode_set_bios_table_resp(response, responseMsg.size() - hdrSize,
1030                                    &retCompletionCode, &retNextTransferHandle);
1031 
1032     EXPECT_EQ(rc, PLDM_SUCCESS);
1033     EXPECT_EQ(completionCode, retCompletionCode);
1034     EXPECT_EQ(nextTransferHandle, retNextTransferHandle);
1035 }
1036 
TEST(SetBIOSTable,testBadDecodeResponse)1037 TEST(SetBIOSTable, testBadDecodeResponse)
1038 {
1039     uint32_t nextTransferHandle = 32;
1040     uint8_t completionCode = PLDM_SUCCESS;
1041     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
1042     struct pldm_msg* response =
1043         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1044         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
1045     struct pldm_set_bios_table_resp* resp =
1046         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1047         reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
1048 
1049     resp->completion_code = completionCode;
1050     resp->next_transfer_handle = htole32(nextTransferHandle);
1051 
1052     uint8_t retCompletionCode;
1053     uint32_t retNextTransferHandle;
1054 
1055     auto rc = decode_set_bios_table_resp(NULL, responseMsg.size() - hdrSize,
1056                                          NULL, NULL);
1057     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1058 
1059     rc = decode_set_bios_table_resp(response, responseMsg.size() - hdrSize + 1,
1060                                     &retCompletionCode, &retNextTransferHandle);
1061     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1062 }
1063 
TEST(SetBIOSTable,testGoodEncodeResponse)1064 TEST(SetBIOSTable, testGoodEncodeResponse)
1065 {
1066     uint8_t instanceId = 10;
1067     uint32_t nextTransferHandle = 32;
1068     uint8_t completionCode = PLDM_SUCCESS;
1069 
1070     std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
1071     struct pldm_msg* response =
1072         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1073         reinterpret_cast<struct pldm_msg*>(responseMsg.data());
1074     auto rc = encode_set_bios_table_resp(instanceId, completionCode,
1075                                          nextTransferHandle, response);
1076     EXPECT_EQ(rc, PLDM_SUCCESS);
1077 
1078     struct pldm_set_bios_table_resp* resp =
1079         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1080         reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
1081     EXPECT_EQ(completionCode, resp->completion_code);
1082     EXPECT_EQ(htole32(nextTransferHandle), resp->next_transfer_handle);
1083 }
1084 
TEST(SetBIOSTable,testBadEncodeResponse)1085 TEST(SetBIOSTable, testBadEncodeResponse)
1086 {
1087     auto rc = encode_set_bios_table_resp(0, PLDM_SUCCESS, 1, NULL);
1088     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1089 }
1090 
TEST(SetBIOSTable,testGoodDecodeRequest)1091 TEST(SetBIOSTable, testGoodDecodeRequest)
1092 {
1093     uint32_t transferHandle = 32;
1094     uint8_t transferFlag = PLDM_START_AND_END;
1095     uint8_t tableType = PLDM_BIOS_STRING_TABLE;
1096     uint32_t tableData = 44;
1097 
1098     std::array<uint8_t,
1099                hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
1100         requestMsg{};
1101     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1102     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
1103     struct pldm_set_bios_table_req* req =
1104         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1105         reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
1106     req->transfer_handle = htole32(transferHandle);
1107     req->transfer_flag = transferFlag;
1108     req->table_type = tableType;
1109     memcpy(req->table_data, &tableData, sizeof(tableData));
1110 
1111     uint32_t retTransferHandle;
1112     uint8_t retTransferFlag;
1113     uint8_t retTableType;
1114     struct variable_field table;
1115     auto rc = decode_set_bios_table_req(request, requestMsg.size() - hdrSize,
1116                                         &retTransferHandle, &retTransferFlag,
1117                                         &retTableType, &table);
1118 
1119     EXPECT_EQ(rc, PLDM_SUCCESS);
1120     EXPECT_EQ(retTransferHandle, transferHandle);
1121     EXPECT_EQ(retTransferFlag, transferFlag);
1122     EXPECT_EQ(retTableType, tableType);
1123     EXPECT_EQ(table.length, sizeof(tableData));
1124     EXPECT_EQ(0, memcmp(table.ptr, &tableData, sizeof(tableData)));
1125 }
1126 
TEST(SetBIOSTable,testBadDecodeRequest)1127 TEST(SetBIOSTable, testBadDecodeRequest)
1128 {
1129     uint32_t transferHandle = 32;
1130     uint8_t transferFlag = PLDM_START_AND_END;
1131     uint8_t tableType = PLDM_BIOS_STRING_TABLE;
1132     uint32_t tableData = 44;
1133 
1134     std::array<uint8_t,
1135                hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
1136         requestMsg{};
1137     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1138     auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
1139     struct pldm_set_bios_table_req* req =
1140         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1141         reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
1142     req->transfer_handle = htole32(transferHandle);
1143     req->transfer_flag = transferFlag;
1144     req->table_type = tableType;
1145     memcpy(req->table_data, &tableData, sizeof(tableData));
1146 
1147     uint32_t retTransferHandle;
1148     uint8_t retTransferFlag;
1149     uint8_t retTableType;
1150 
1151     auto rc = decode_set_bios_table_req(request, requestMsg.size() - hdrSize,
1152                                         &retTransferHandle, &retTransferFlag,
1153                                         &retTableType, NULL);
1154     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1155 
1156     struct variable_field table;
1157     rc = decode_set_bios_table_req(
1158         request, requestMsg.size() - hdrSize - sizeof(tableData) - 1,
1159         &retTransferHandle, &retTransferFlag, &retTableType, &table);
1160     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1161 }
1162