blob: 955bec6b9c22e3be1d20f7759214918052fed58d [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jefferyefb40062023-11-10 13:48:39 +10302#include <libpldm/base.h>
3#include <libpldm/bios.h>
4#include <libpldm/utils.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09305
6#include <array>
Andrew Jeffery5a706072023-04-05 19:45:31 +09307#include <cstdint>
8#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include <memory>
Andrew Jeffery9c766792022-08-10 23:12:49 +093010
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <gtest/gtest.h>
12
13constexpr auto hdrSize = sizeof(pldm_msg_hdr);
14
15TEST(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
23TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093036 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093037 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
67TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930107 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930108 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
123TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930131 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930132 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930139 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930140 reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
141 EXPECT_EQ(completionCode, resp->completion_code);
142}
143
144TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930152 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930153 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
164TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930170 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930171 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
172 struct pldm_only_cc_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930173 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930174 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
186TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930193 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930194 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
205TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930218 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930219 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930227 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930228 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
237TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930253 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930254 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
272TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930283 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930284 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
285 struct pldm_set_date_time_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930286 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930287 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
315TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930327 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930328 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
348TEST(GetBIOSTable, testGoodEncodeResponse)
349{
350 std::array<uint8_t,
351 sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4>
352 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930353 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930354 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930368 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930369 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
377TEST(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
389TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930397 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930398 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930405 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930406 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
412TEST(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
424TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930435 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930436 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
437 struct pldm_get_bios_table_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930438 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930439 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}
454TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930465 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930466 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
467 struct pldm_get_bios_table_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930468 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930469 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/*
485TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930496// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930497 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
498 struct pldm_get_bios_table_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930499// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930500 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
513TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930525 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930526 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
527 struct pldm_get_bios_attribute_current_value_by_handle_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930528 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930529 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
547TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930560 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930561 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
562 struct pldm_get_bios_attribute_current_value_by_handle_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930563 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930564 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
586TEST(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
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930595 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930596 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930603 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930604 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
612TEST(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
624TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930636 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930637 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930646 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930647 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
658TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930673 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930674 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
681TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930690 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930691 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
692 auto rc = encode_set_bios_attribute_current_value_req(
693 instanceId, transferHandle, transferFlag,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930694 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930695 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930701 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930702 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
710TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930718 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930719 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,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930726 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930727 reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
728 request, requestMsg.size() - hdrSize);
729
730 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
731}
732
733TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930742 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930743 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
744 struct pldm_set_bios_attribute_current_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930745 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930746 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
766TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930773 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930774 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
785TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930794 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930795 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930801 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930802 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
808TEST(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}
818TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930825 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930826 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
827 struct pldm_set_bios_attribute_current_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930828 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930829 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
846TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930854 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930855 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
856 struct pldm_set_bios_attribute_current_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930857 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930858 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
875TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930884 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930885 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
886
887 struct pldm_get_bios_table_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930888 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930889 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
912TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930924 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930925 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
926
927 struct pldm_get_bios_attribute_current_value_by_handle_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930928 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930929 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
955TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930965 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930966 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
967 auto rc = encode_set_bios_table_req(
968 instanceId, transferHandle, transferFlag, tableType,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930969 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930970 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930976 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930977 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
985TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930995 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930996 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,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301005 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301006 reinterpret_cast<uint8_t*>(&tableData), sizeof(tableData), request,
1007 requestMsg.size() - hdrSize + 1);
1008 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1009}
1010
1011TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301017 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301018 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
1019 struct pldm_set_bios_table_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301020 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301021 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
1037TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301043 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301044 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
1045 struct pldm_set_bios_table_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301046 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301047 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
1064TEST(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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301072 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301073 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 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301079 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301080 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
1085TEST(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
1091TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301101 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301102 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
1103 struct pldm_set_bios_table_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301104 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301105 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
1127TEST(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{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301138 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
1139 struct pldm_set_bios_table_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301140 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301141 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);
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301161}