blob: 795daece941c0031ed1befcf67027a5dec6779e7 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#include <string.h>
2
3#include <array>
4
5#include "libpldm/base.h"
6#include "libpldm/bios.h"
Xiaochao Ma39ae2a92019-11-12 20:30:34 +08007#include "libpldm/utils.h"
Sampa Misra032bd502019-03-06 05:03:22 -06008
9#include <gtest/gtest.h>
10
Zahed Hossain223a73d2019-07-04 12:46:18 -050011constexpr auto hdrSize = sizeof(pldm_msg_hdr);
12
Sampa Misra032bd502019-03-06 05:03:22 -060013TEST(GetDateTime, testEncodeRequest)
14{
15 pldm_msg request{};
Sampa Misra032bd502019-03-06 05:03:22 -060016
17 auto rc = encode_get_date_time_req(0, &request);
George Liu684a7162019-12-06 15:10:52 +080018 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misra032bd502019-03-06 05:03:22 -060019}
20
21TEST(GetDateTime, testEncodeResponse)
22{
23 uint8_t completionCode = 0;
24 uint8_t seconds = 50;
25 uint8_t minutes = 20;
26 uint8_t hours = 5;
27 uint8_t day = 23;
28 uint8_t month = 11;
29 uint16_t year = 2019;
30
vkaverapa6575b82019-04-03 05:33:52 -050031 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES>
32 responseMsg{};
Sampa Misra032bd502019-03-06 05:03:22 -060033
vkaverapa6575b82019-04-03 05:33:52 -050034 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Sampa Misra032bd502019-03-06 05:03:22 -060035
36 auto rc = encode_get_date_time_resp(0, PLDM_SUCCESS, seconds, minutes,
vkaverapa6575b82019-04-03 05:33:52 -050037 hours, day, month, year, response);
Sampa Misra032bd502019-03-06 05:03:22 -060038
George Liu684a7162019-12-06 15:10:52 +080039 EXPECT_EQ(rc, PLDM_SUCCESS);
40 EXPECT_EQ(completionCode, response->payload[0]);
Sampa Misra032bd502019-03-06 05:03:22 -060041
George Liu684a7162019-12-06 15:10:52 +080042 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
vkaverapa6575b82019-04-03 05:33:52 -050043 &seconds, sizeof(seconds)));
George Liu684a7162019-12-06 15:10:52 +080044 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
vkaverapa6575b82019-04-03 05:33:52 -050045 sizeof(seconds),
Sampa Misra032bd502019-03-06 05:03:22 -060046 &minutes, sizeof(minutes)));
George Liu684a7162019-12-06 15:10:52 +080047 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
vkaverapa6575b82019-04-03 05:33:52 -050048 sizeof(seconds) + sizeof(minutes),
49 &hours, sizeof(hours)));
George Liu684a7162019-12-06 15:10:52 +080050 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
vkaverapa6575b82019-04-03 05:33:52 -050051 sizeof(seconds) + sizeof(minutes) + sizeof(hours),
52 &day, sizeof(day)));
George Liu684a7162019-12-06 15:10:52 +080053 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
vkaverapa6575b82019-04-03 05:33:52 -050054 sizeof(seconds) + sizeof(minutes) + sizeof(hours) +
55 sizeof(day),
Sampa Misra032bd502019-03-06 05:03:22 -060056 &month, sizeof(month)));
Lei YUf02454e2020-02-28 17:24:06 +080057 uint16_t yearLe = htole16(year);
George Liu684a7162019-12-06 15:10:52 +080058 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
vkaverapa6575b82019-04-03 05:33:52 -050059 sizeof(seconds) + sizeof(minutes) + sizeof(hours) +
60 sizeof(day) + sizeof(month),
Lei YUf02454e2020-02-28 17:24:06 +080061 &yearLe, sizeof(yearLe)));
Sampa Misra032bd502019-03-06 05:03:22 -060062}
63
64TEST(GetDateTime, testDecodeResponse)
65{
Zahed Hossain223a73d2019-07-04 12:46:18 -050066 std::array<uint8_t, hdrSize + PLDM_GET_DATE_TIME_RESP_BYTES> responseMsg{};
Sampa Misra032bd502019-03-06 05:03:22 -060067
68 uint8_t completionCode = 0;
69
70 uint8_t seconds = 55;
71 uint8_t minutes = 2;
72 uint8_t hours = 8;
73 uint8_t day = 9;
74 uint8_t month = 7;
75 uint16_t year = 2020;
Lei YUf02454e2020-02-28 17:24:06 +080076 uint16_t yearLe = htole16(year);
Sampa Misra032bd502019-03-06 05:03:22 -060077
78 uint8_t retSeconds = 0;
79 uint8_t retMinutes = 0;
80 uint8_t retHours = 0;
81 uint8_t retDay = 0;
82 uint8_t retMonth = 0;
83 uint16_t retYear = 0;
84
Zahed Hossain223a73d2019-07-04 12:46:18 -050085 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &seconds,
Sampa Misra032bd502019-03-06 05:03:22 -060086 sizeof(seconds));
Zahed Hossain223a73d2019-07-04 12:46:18 -050087 memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
88 hdrSize,
Sampa Misra032bd502019-03-06 05:03:22 -060089 &minutes, sizeof(minutes));
vkaverapa6575b82019-04-03 05:33:52 -050090 memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050091 sizeof(minutes) + hdrSize,
Sampa Misra032bd502019-03-06 05:03:22 -060092 &hours, sizeof(hours));
vkaverapa6575b82019-04-03 05:33:52 -050093 memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050094 sizeof(minutes) + sizeof(hours) + hdrSize,
Sampa Misra032bd502019-03-06 05:03:22 -060095 &day, sizeof(day));
vkaverapa6575b82019-04-03 05:33:52 -050096 memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050097 sizeof(minutes) + sizeof(hours) + sizeof(day) + hdrSize,
Sampa Misra032bd502019-03-06 05:03:22 -060098 &month, sizeof(month));
vkaverapa6575b82019-04-03 05:33:52 -050099 memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(seconds) +
Zahed Hossain223a73d2019-07-04 12:46:18 -0500100 sizeof(minutes) + sizeof(hours) + sizeof(day) + sizeof(month) +
101 hdrSize,
Lei YUf02454e2020-02-28 17:24:06 +0800102 &yearLe, sizeof(yearLe));
Sampa Misra032bd502019-03-06 05:03:22 -0600103
Zahed Hossain223a73d2019-07-04 12:46:18 -0500104 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
105
vkaverapa6575b82019-04-03 05:33:52 -0500106 auto rc = decode_get_date_time_resp(
Zahed Hossain223a73d2019-07-04 12:46:18 -0500107 response, responseMsg.size() - hdrSize, &completionCode, &retSeconds,
vkaverapa6575b82019-04-03 05:33:52 -0500108 &retMinutes, &retHours, &retDay, &retMonth, &retYear);
Sampa Misra032bd502019-03-06 05:03:22 -0600109
George Liu684a7162019-12-06 15:10:52 +0800110 EXPECT_EQ(rc, PLDM_SUCCESS);
111 EXPECT_EQ(seconds, retSeconds);
112 EXPECT_EQ(minutes, retMinutes);
113 EXPECT_EQ(hours, retHours);
114 EXPECT_EQ(day, retDay);
115 EXPECT_EQ(month, retMonth);
116 EXPECT_EQ(year, retYear);
Sampa Misra032bd502019-03-06 05:03:22 -0600117}
Sampa Misrab37be312019-07-03 02:26:41 -0500118
Xiaochao Ma39ae2a92019-11-12 20:30:34 +0800119TEST(SetDateTime, testGoodEncodeResponse)
120{
121 uint8_t instanceId = 0;
122 uint8_t completionCode = PLDM_SUCCESS;
123
124 std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
125 responseMsg{};
126 struct pldm_msg* response =
127 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
128
129 auto rc = encode_set_date_time_resp(instanceId, completionCode, response,
130 responseMsg.size() - hdrSize);
131 EXPECT_EQ(rc, PLDM_SUCCESS);
132
133 struct pldm_only_cc_resp* resp =
134 reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
135 EXPECT_EQ(completionCode, resp->completion_code);
136}
137
138TEST(SetDateTime, testBadEncodeResponse)
139{
140
141 uint8_t instanceId = 10;
142 uint8_t completionCode = PLDM_SUCCESS;
143 std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
144 responseMsg{};
145 struct pldm_msg* response =
146 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
147 auto rc = encode_set_date_time_resp(instanceId, completionCode, nullptr,
148 responseMsg.size() - hdrSize);
149
150 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
151 rc = encode_set_date_time_resp(instanceId, completionCode, response,
152 responseMsg.size() - hdrSize - 1);
153
154 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
155}
156
157TEST(SetDateTime, testGoodDecodeResponse)
158{
159 uint8_t completionCode = PLDM_SUCCESS;
160 std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
161 responseMsg{};
162 struct pldm_msg* response =
163 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
164 struct pldm_only_cc_resp* resp =
165 reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
166
167 resp->completion_code = completionCode;
168
169 uint8_t retCompletionCode;
170 auto rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize,
171 &retCompletionCode);
172
173 EXPECT_EQ(rc, PLDM_SUCCESS);
174 EXPECT_EQ(completionCode, retCompletionCode);
175}
176
177TEST(SetDateTime, testBadDecodeResponse)
178{
179 uint8_t completionCode = PLDM_SUCCESS;
180
181 std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
182 responseMsg{};
183 struct pldm_msg* response =
184 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
185 auto rc = decode_set_date_time_resp(nullptr, responseMsg.size() - hdrSize,
186 &completionCode);
187 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
188
189 rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize - 1,
190 &completionCode);
191
192 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
193}
194
195TEST(SetDateTime, testGoodEncodeRequset)
196{
197 uint8_t instanceId = 0;
198 uint8_t seconds = 50;
199 uint8_t minutes = 20;
200 uint8_t hours = 10;
201 uint8_t day = 11;
202 uint8_t month = 11;
203 uint16_t year = 2019;
204
205 std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
206 requestMsg{};
207
208 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
209 auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
210 month, year, request,
211 requestMsg.size() - hdrSize);
212
213 EXPECT_EQ(rc, PLDM_SUCCESS);
214
215 struct pldm_set_date_time_req* req =
216 reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
217 EXPECT_EQ(seconds, bcd2dec8(req->seconds));
218 EXPECT_EQ(minutes, bcd2dec8(req->minutes));
219 EXPECT_EQ(hours, bcd2dec8(req->hours));
220 EXPECT_EQ(day, bcd2dec8(req->day));
221 EXPECT_EQ(month, bcd2dec8(req->month));
Lei YUf02454e2020-02-28 17:24:06 +0800222 EXPECT_EQ(year, bcd2dec16(le16toh(req->year)));
Xiaochao Ma39ae2a92019-11-12 20:30:34 +0800223}
224
225TEST(SetDateTime, testBadEncodeRequset)
226{
227 uint8_t instanceId = 0;
228
229 uint8_t seconds = 50;
230 uint8_t minutes = 20;
231 uint8_t hours = 10;
232 uint8_t day = 13;
233 uint8_t month = 11;
234 uint16_t year = 2019;
235
236 uint8_t erday = 43;
237
238 std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
239 requestMsg{};
240
241 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
242
243 auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
244 month, year, nullptr,
245 requestMsg.size() - hdrSize);
246 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
247
248 rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, erday,
249 month, year, request,
250 requestMsg.size() - hdrSize);
251 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
252
253 rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
254 month, year, request,
255 requestMsg.size() - hdrSize - 4);
256 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
257}
258
259TEST(SetDateTime, testGoodDecodeRequest)
260{
261 std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
262 requestMsg{};
263 uint8_t seconds = 0x50;
264 uint8_t minutes = 0x20;
265 uint8_t hours = 0x10;
266 uint8_t day = 0x11;
267 uint8_t month = 0x11;
Lei YUf02454e2020-02-28 17:24:06 +0800268 uint16_t year = 0x2019;
Xiaochao Ma39ae2a92019-11-12 20:30:34 +0800269
270 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
271 struct pldm_set_date_time_req* req =
272 reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
273 req->seconds = seconds;
274 req->minutes = minutes;
275 req->hours = hours;
276 req->day = day;
277 req->month = month;
278 req->year = htole16(year);
279
280 uint8_t retseconds;
281 uint8_t retminutes;
282 uint8_t rethours;
283 uint8_t retday;
284 uint8_t retmonth;
285 uint16_t retyear;
286
287 auto rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize,
288 &retseconds, &retminutes, &rethours,
289 &retday, &retmonth, &retyear);
290
291 EXPECT_EQ(rc, PLDM_SUCCESS);
292 EXPECT_EQ(retseconds, 50);
293 EXPECT_EQ(retminutes, 20);
294 EXPECT_EQ(rethours, 10);
295 EXPECT_EQ(retday, 11);
296 EXPECT_EQ(retmonth, 11);
297 EXPECT_EQ(retyear, 2019);
298}
299
300TEST(SetDateTime, testBadDecodeRequest)
301{
302 uint8_t seconds = 0x50;
303 uint8_t minutes = 0x20;
304 uint8_t hours = 0x10;
305 uint8_t day = 0x11;
306 uint8_t month = 0x11;
Lei YU7cfeb8e2020-02-27 18:24:18 +0800307 uint16_t year = htole16(0x2019);
Xiaochao Ma39ae2a92019-11-12 20:30:34 +0800308
309 std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
310 requestMsg{};
311
312 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
313
314 decode_set_date_time_req(request, requestMsg.size() - hdrSize, &seconds,
315 &minutes, &hours, &day, &month, &year);
316
317 auto rc =
318 decode_set_date_time_req(nullptr, requestMsg.size() - hdrSize, &seconds,
319 &minutes, &hours, &day, &month, &year);
320 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
321
322 rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize, nullptr,
323 nullptr, nullptr, nullptr, nullptr, nullptr);
324 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
325
326 rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize - 4,
327 &seconds, &minutes, &hours, &day, &month,
328 &year);
329 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
330}
331
Sampa Misrab37be312019-07-03 02:26:41 -0500332TEST(GetBIOSTable, testGoodEncodeResponse)
333{
334 std::array<uint8_t,
335 sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4>
336 responseMsg{};
337 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
338
339 uint8_t completionCode = PLDM_SUCCESS;
340 uint32_t nextTransferHandle = 32;
341 uint8_t transferFlag = PLDM_START_AND_END;
342 std::array<uint8_t, 4> tableData{1, 2, 3, 4};
343
344 auto rc = encode_get_bios_table_resp(
345 0, PLDM_SUCCESS, nextTransferHandle, transferFlag, tableData.data(),
346 sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4,
347 response);
George Liu684a7162019-12-06 15:10:52 +0800348 EXPECT_EQ(rc, PLDM_SUCCESS);
Sampa Misrab37be312019-07-03 02:26:41 -0500349
350 struct pldm_get_bios_table_resp* resp =
351 reinterpret_cast<struct pldm_get_bios_table_resp*>(response->payload);
352
George Liu684a7162019-12-06 15:10:52 +0800353 EXPECT_EQ(completionCode, resp->completion_code);
Lei YUf02454e2020-02-28 17:24:06 +0800354 EXPECT_EQ(nextTransferHandle, le32toh(resp->next_transfer_handle));
George Liu684a7162019-12-06 15:10:52 +0800355 EXPECT_EQ(transferFlag, resp->transfer_flag);
356 EXPECT_EQ(0, memcmp(tableData.data(), resp->table_data, tableData.size()));
Sampa Misrab37be312019-07-03 02:26:41 -0500357}
358
359TEST(GetBIOSTable, testBadEncodeResponse)
360{
361 uint32_t nextTransferHandle = 32;
362 uint8_t transferFlag = PLDM_START_AND_END;
363 std::array<uint8_t, 4> tableData{1, 2, 3, 4};
364
365 auto rc = encode_get_bios_table_resp(
366 0, PLDM_SUCCESS, nextTransferHandle, transferFlag, tableData.data(),
367 sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + 4, nullptr);
George Liu684a7162019-12-06 15:10:52 +0800368 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sampa Misrab37be312019-07-03 02:26:41 -0500369}
370
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500371TEST(GetBIOSTable, testGoodEncodeRequest)
372{
373 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES>
374 requestMsg{};
375 uint32_t transferHandle = 0x0;
376 uint8_t transferOpFlag = 0x01;
377 uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
378
379 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
380 auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
381 tableType, request);
382
George Liu684a7162019-12-06 15:10:52 +0800383 EXPECT_EQ(rc, PLDM_SUCCESS);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500384
385 struct pldm_get_bios_table_req* req =
386 reinterpret_cast<struct pldm_get_bios_table_req*>(request->payload);
George Liu684a7162019-12-06 15:10:52 +0800387 EXPECT_EQ(transferHandle, le32toh(req->transfer_handle));
388 EXPECT_EQ(transferOpFlag, req->transfer_op_flag);
389 EXPECT_EQ(tableType, req->table_type);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500390}
391
392TEST(GetBIOSTable, testBadEncodeRequest)
393{
394 uint32_t transferHandle = 0x0;
395 uint8_t transferOpFlag = 0x01;
396 uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
397
398 auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
399 tableType, nullptr);
400
George Liu684a7162019-12-06 15:10:52 +0800401 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500402}
403
Sampa Misrab37be312019-07-03 02:26:41 -0500404TEST(GetBIOSTable, testGoodDecodeRequest)
405{
406 const auto hdr_size = sizeof(pldm_msg_hdr);
407 std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
408 uint32_t transferHandle = 31;
409 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
410 uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
411 uint32_t retTransferHandle = 0;
412 uint8_t retTransferOpFlag = 0;
413 uint8_t retTableType = 0;
414
415 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
416 struct pldm_get_bios_table_req* request =
417 reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
418
Lei YUf02454e2020-02-28 17:24:06 +0800419 request->transfer_handle = htole32(transferHandle);
Sampa Misrab37be312019-07-03 02:26:41 -0500420 request->transfer_op_flag = transferOpFlag;
421 request->table_type = tableType;
422
423 auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size,
424 &retTransferHandle, &retTransferOpFlag,
425 &retTableType);
426
George Liu684a7162019-12-06 15:10:52 +0800427 EXPECT_EQ(rc, PLDM_SUCCESS);
428 EXPECT_EQ(transferHandle, retTransferHandle);
429 EXPECT_EQ(transferOpFlag, retTransferOpFlag);
430 EXPECT_EQ(tableType, retTableType);
Sampa Misrab37be312019-07-03 02:26:41 -0500431}
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500432TEST(GetBIOSTable, testBadDecodeRequest)
433{
434 const auto hdr_size = sizeof(pldm_msg_hdr);
435 std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
436 uint32_t transferHandle = 31;
437 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
438 uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
439 uint32_t retTransferHandle = 0;
440 uint8_t retTransferOpFlag = 0;
441 uint8_t retTableType = 0;
Sampa Misrab37be312019-07-03 02:26:41 -0500442
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500443 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
444 struct pldm_get_bios_table_req* request =
445 reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
446
Lei YUf02454e2020-02-28 17:24:06 +0800447 request->transfer_handle = htole32(transferHandle);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500448 request->transfer_op_flag = transferOpFlag;
449 request->table_type = tableType;
450
451 auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size,
452 &retTransferHandle, &retTransferOpFlag,
453 &retTableType);
454
George Liu684a7162019-12-06 15:10:52 +0800455 EXPECT_EQ(rc, PLDM_SUCCESS);
456 EXPECT_EQ(transferHandle, retTransferHandle);
457 EXPECT_EQ(transferOpFlag, retTransferOpFlag);
458 EXPECT_EQ(tableType, retTableType);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500459}
460/*
Sampa Misrab37be312019-07-03 02:26:41 -0500461TEST(GetBIOSTable, testBadDecodeRequest)
462{
463 const auto hdr_size = sizeof(pldm_msg_hdr);
464 std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
465 uint32_t transferHandle = 31;
466 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
467 uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
468 uint32_t retTransferHandle = 0;
469 uint8_t retTransferOpFlag = 0;
470 uint8_t retTableType = 0;
471
472 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
473 struct pldm_get_bios_table_req* request =
474 reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
475
Lei YUf02454e2020-02-28 17:24:06 +0800476 request->transfer_handle = htole32(transferHandle);
Sampa Misrab37be312019-07-03 02:26:41 -0500477 request->transfer_op_flag = transferOpFlag;
478 request->table_type = tableType;
479
480 auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size - 3,
481 &retTransferHandle, &retTransferOpFlag,
482 &retTableType);
483
George Liu684a7162019-12-06 15:10:52 +0800484 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500485}*/
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500486
487TEST(GetBIOSAttributeCurrentValueByHandle, testGoodDecodeRequest)
488{
489 uint32_t transferHandle = 45;
490 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
491 uint16_t attributehandle = 10;
492 uint32_t retTransferHandle = 0;
493 uint8_t retTransferOpFlag = 0;
494 uint16_t retattributehandle = 0;
495 std::array<uint8_t, hdrSize + sizeof(transferHandle) +
496 sizeof(transferOpFlag) + sizeof(attributehandle)>
497 requestMsg{};
498
499 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
500 struct pldm_get_bios_attribute_current_value_by_handle_req* request =
501 reinterpret_cast<
502 struct pldm_get_bios_attribute_current_value_by_handle_req*>(
503 req->payload);
504
Lei YUf02454e2020-02-28 17:24:06 +0800505 request->transfer_handle = htole32(transferHandle);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500506 request->transfer_op_flag = transferOpFlag;
Lei YUf02454e2020-02-28 17:24:06 +0800507 request->attribute_handle = htole16(attributehandle);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500508
509 auto rc = decode_get_bios_attribute_current_value_by_handle_req(
510 req, requestMsg.size() - hdrSize, &retTransferHandle,
511 &retTransferOpFlag, &retattributehandle);
512
George Liu684a7162019-12-06 15:10:52 +0800513 EXPECT_EQ(rc, PLDM_SUCCESS);
514 EXPECT_EQ(transferHandle, retTransferHandle);
515 EXPECT_EQ(transferOpFlag, retTransferOpFlag);
516 EXPECT_EQ(attributehandle, retattributehandle);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500517}
518
519TEST(GetBIOSAttributeCurrentValueByHandle, testBadDecodeRequest)
520{
521
522 uint32_t transferHandle = 0;
523 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
524 uint16_t attribute_handle = 0;
525 uint32_t retTransferHandle = 0;
526 uint8_t retTransferOpFlag = 0;
527 uint16_t retattribute_handle = 0;
528 std::array<uint8_t, hdrSize + sizeof(transferHandle) +
529 sizeof(transferOpFlag) + sizeof(attribute_handle)>
530 requestMsg{};
531
532 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
533 struct pldm_get_bios_attribute_current_value_by_handle_req* request =
534 reinterpret_cast<
535 struct pldm_get_bios_attribute_current_value_by_handle_req*>(
536 req->payload);
537
Lei YUf02454e2020-02-28 17:24:06 +0800538 request->transfer_handle = htole32(transferHandle);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500539 request->transfer_op_flag = transferOpFlag;
540 request->attribute_handle = attribute_handle;
541
542 auto rc = decode_get_bios_attribute_current_value_by_handle_req(
543 NULL, requestMsg.size() - hdrSize, &retTransferHandle,
544 &retTransferOpFlag, &retattribute_handle);
George Liu684a7162019-12-06 15:10:52 +0800545 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500546
547 transferHandle = 31;
Lei YUf02454e2020-02-28 17:24:06 +0800548 request->transfer_handle = htole32(transferHandle);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500549
550 rc = decode_get_bios_attribute_current_value_by_handle_req(
551 req, 0, &retTransferHandle, &retTransferOpFlag, &retattribute_handle);
552
George Liu684a7162019-12-06 15:10:52 +0800553 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500554}
555
556TEST(GetBIOSAttributeCurrentValueByHandle, testGoodEncodeResponse)
557{
558
559 uint8_t instanceId = 10;
560 uint8_t completionCode = PLDM_SUCCESS;
561 uint32_t nextTransferHandle = 32;
562 uint8_t transferFlag = PLDM_START_AND_END;
563 uint8_t attributeData = 44;
564 std::array<uint8_t,
565 hdrSize +
566 sizeof(pldm_get_bios_attribute_current_value_by_handle_resp)>
567 responseMsg{};
568 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
569
570 auto rc = encode_get_bios_current_value_by_handle_resp(
571 instanceId, completionCode, nextTransferHandle, transferFlag,
572 &attributeData, sizeof(attributeData), response);
573
George Liu684a7162019-12-06 15:10:52 +0800574 EXPECT_EQ(rc, PLDM_SUCCESS);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500575
576 struct pldm_get_bios_attribute_current_value_by_handle_resp* resp =
577 reinterpret_cast<
578 struct pldm_get_bios_attribute_current_value_by_handle_resp*>(
579 response->payload);
580
George Liu684a7162019-12-06 15:10:52 +0800581 EXPECT_EQ(completionCode, resp->completion_code);
Lei YUf02454e2020-02-28 17:24:06 +0800582 EXPECT_EQ(nextTransferHandle, le32toh(resp->next_transfer_handle));
George Liu684a7162019-12-06 15:10:52 +0800583 EXPECT_EQ(transferFlag, resp->transfer_flag);
584 EXPECT_EQ(
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500585 0, memcmp(&attributeData, resp->attribute_data, sizeof(attributeData)));
586}
587
588TEST(GetBIOSAttributeCurrentValueByHandle, testBadEncodeResponse)
589{
590 uint32_t nextTransferHandle = 32;
591 uint8_t transferFlag = PLDM_START_AND_END;
592 uint8_t attributeData = 44;
593
594 auto rc = encode_get_bios_current_value_by_handle_resp(
595 0, PLDM_SUCCESS, nextTransferHandle, transferFlag, &attributeData,
596 sizeof(attributeData), nullptr);
George Liu684a7162019-12-06 15:10:52 +0800597 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500598
599 std::array<uint8_t,
600 hdrSize +
601 sizeof(pldm_get_bios_attribute_current_value_by_handle_resp)>
602 responseMsg{};
603 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
604 rc = encode_get_bios_current_value_by_handle_resp(
605 0, PLDM_SUCCESS, nextTransferHandle, transferFlag, nullptr,
606 sizeof(attributeData), response);
George Liu684a7162019-12-06 15:10:52 +0800607 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500608}
John Wang4d844792019-08-15 15:51:40 +0800609
610TEST(SetBiosAttributeCurrentValue, testGoodEncodeRequest)
611{
612 uint8_t instanceId = 10;
613 uint32_t transferHandle = 32;
John Wang81796c22019-09-06 11:18:57 +0800614 uint8_t transferFlag = PLDM_START_AND_END;
615 uint32_t attributeData = 44;
John Wang4d844792019-08-15 15:51:40 +0800616 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
617 sizeof(attributeData)>
618 requestMsg{};
619 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
620 auto rc = encode_set_bios_attribute_current_value_req(
621 instanceId, transferHandle, transferFlag,
622 reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
623 request, requestMsg.size() - hdrSize);
624
George Liu684a7162019-12-06 15:10:52 +0800625 EXPECT_EQ(rc, PLDM_SUCCESS);
John Wang4d844792019-08-15 15:51:40 +0800626
627 struct pldm_set_bios_attribute_current_value_req* req =
628 reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
629 request->payload);
George Liu684a7162019-12-06 15:10:52 +0800630 EXPECT_EQ(htole32(transferHandle), req->transfer_handle);
631 EXPECT_EQ(transferFlag, req->transfer_flag);
632 EXPECT_EQ(
John Wang4d844792019-08-15 15:51:40 +0800633 0, memcmp(&attributeData, req->attribute_data, sizeof(attributeData)));
634}
635
636TEST(SetBiosAttributeCurrentValue, testBadEncodeRequest)
637{
638 uint8_t instanceId = 10;
639 uint32_t transferHandle = 32;
John Wang81796c22019-09-06 11:18:57 +0800640 uint8_t transferFlag = PLDM_START_AND_END;
641 uint32_t attributeData = 44;
John Wang4d844792019-08-15 15:51:40 +0800642 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES>
643 requestMsg{};
644 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
645
646 auto rc = encode_set_bios_attribute_current_value_req(
647 instanceId, transferHandle, transferFlag, nullptr, 0, nullptr, 0);
George Liu684a7162019-12-06 15:10:52 +0800648 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Wang4d844792019-08-15 15:51:40 +0800649 rc = encode_set_bios_attribute_current_value_req(
650 instanceId, transferHandle, transferFlag,
651 reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
652 request, requestMsg.size() - hdrSize);
653
George Liu684a7162019-12-06 15:10:52 +0800654 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
John Wang4d844792019-08-15 15:51:40 +0800655}
656
657TEST(SetBiosAttributeCurrentValue, testGoodDecodeRequest)
658{
659 uint32_t transferHandle = 32;
John Wang81796c22019-09-06 11:18:57 +0800660 uint8_t transferFlag = PLDM_START_AND_END;
661 uint32_t attributeData = 44;
John Wang4d844792019-08-15 15:51:40 +0800662
663 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
664 sizeof(attributeData)>
665 requestMsg{};
666 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
667 struct pldm_set_bios_attribute_current_value_req* req =
668 reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
669 request->payload);
670 req->transfer_handle = htole32(transferHandle);
671 req->transfer_flag = transferFlag;
672 memcpy(req->attribute_data, &attributeData, sizeof(attributeData));
673
674 uint32_t retTransferHandle;
675 uint8_t retTransferFlag;
John Wang08c05952019-12-20 15:40:39 +0800676 struct variable_field attribute;
John Wang4d844792019-08-15 15:51:40 +0800677 auto rc = decode_set_bios_attribute_current_value_req(
678 request, requestMsg.size() - hdrSize, &retTransferHandle,
John Wang08c05952019-12-20 15:40:39 +0800679 &retTransferFlag, &attribute);
John Wang4d844792019-08-15 15:51:40 +0800680
George Liu684a7162019-12-06 15:10:52 +0800681 EXPECT_EQ(rc, PLDM_SUCCESS);
682 EXPECT_EQ(retTransferHandle, transferHandle);
683 EXPECT_EQ(retTransferFlag, transferFlag);
John Wang08c05952019-12-20 15:40:39 +0800684 EXPECT_EQ(attribute.length, sizeof(attributeData));
685 EXPECT_EQ(0, memcmp(attribute.ptr, &attributeData, sizeof(attributeData)));
John Wang4d844792019-08-15 15:51:40 +0800686}
687
688TEST(SetBiosAttributeCurrentValue, testBadDecodeRequest)
689{
690 uint32_t transferHandle = 32;
John Wang81796c22019-09-06 11:18:57 +0800691 uint8_t transferFlag = PLDM_START_AND_END;
John Wang08c05952019-12-20 15:40:39 +0800692 struct variable_field attribute;
John Wang4d844792019-08-15 15:51:40 +0800693 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES - 1>
694 requestMsg{};
695 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
696
697 auto rc = decode_set_bios_attribute_current_value_req(
John Wang08c05952019-12-20 15:40:39 +0800698 nullptr, 0, &transferHandle, &transferFlag, &attribute);
George Liu684a7162019-12-06 15:10:52 +0800699 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Wang4d844792019-08-15 15:51:40 +0800700 rc = decode_set_bios_attribute_current_value_req(
701 request, requestMsg.size() - hdrSize, &transferHandle, &transferFlag,
John Wang08c05952019-12-20 15:40:39 +0800702 &attribute);
George Liu684a7162019-12-06 15:10:52 +0800703 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
John Wang4d844792019-08-15 15:51:40 +0800704}
705
706TEST(SetBiosAttributeCurrentValue, testGoodEncodeResponse)
707{
708 uint8_t instanceId = 10;
709 uint32_t nextTransferHandle = 32;
710 uint8_t completionCode = PLDM_SUCCESS;
711
712 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
713 responseMsg{};
714 struct pldm_msg* response =
715 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
716 auto rc = encode_set_bios_attribute_current_value_resp(
717 instanceId, completionCode, nextTransferHandle, response);
George Liu684a7162019-12-06 15:10:52 +0800718 EXPECT_EQ(rc, PLDM_SUCCESS);
John Wang4d844792019-08-15 15:51:40 +0800719
720 struct pldm_set_bios_attribute_current_value_resp* resp =
721 reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
722 response->payload);
George Liu684a7162019-12-06 15:10:52 +0800723 EXPECT_EQ(completionCode, resp->completion_code);
724 EXPECT_EQ(htole32(nextTransferHandle), resp->next_transfer_handle);
John Wang4d844792019-08-15 15:51:40 +0800725}
726
727TEST(SetBiosAttributeCurrentValue, testBadEncodeResponse)
728{
729 uint8_t instanceId = 10;
730 uint32_t nextTransferHandle = 32;
731 uint8_t completionCode = PLDM_SUCCESS;
732 auto rc = encode_set_bios_attribute_current_value_resp(
733 instanceId, completionCode, nextTransferHandle, nullptr);
734
George Liu684a7162019-12-06 15:10:52 +0800735 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Wang4d844792019-08-15 15:51:40 +0800736}
737TEST(SetBiosAttributeCurrentValue, testGoodDecodeResponse)
738{
739 uint32_t nextTransferHandle = 32;
740 uint8_t completionCode = PLDM_SUCCESS;
741 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
742 responseMsg{};
743 struct pldm_msg* response =
744 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
745 struct pldm_set_bios_attribute_current_value_resp* resp =
746 reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
747 response->payload);
748
749 resp->completion_code = completionCode;
750 resp->next_transfer_handle = htole32(nextTransferHandle);
751
752 uint8_t retCompletionCode;
753 uint32_t retNextTransferHandle;
754 auto rc = decode_set_bios_attribute_current_value_resp(
755 response, responseMsg.size() - hdrSize, &retCompletionCode,
756 &retNextTransferHandle);
757
George Liu684a7162019-12-06 15:10:52 +0800758 EXPECT_EQ(rc, PLDM_SUCCESS);
759 EXPECT_EQ(completionCode, retCompletionCode);
760 EXPECT_EQ(nextTransferHandle, retNextTransferHandle);
John Wang4d844792019-08-15 15:51:40 +0800761}
762
763TEST(SetBiosAttributeCurrentValue, testBadDecodeResponse)
764{
765 uint32_t nextTransferHandle = 32;
766 uint8_t completionCode = PLDM_SUCCESS;
767
George Liu684a7162019-12-06 15:10:52 +0800768 std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
John Wang4d844792019-08-15 15:51:40 +0800769 responseMsg{};
770 struct pldm_msg* response =
771 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
George Liu684a7162019-12-06 15:10:52 +0800772 struct pldm_set_bios_attribute_current_value_resp* resp =
773 reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
774 response->payload);
775
776 resp->completion_code = completionCode;
777 resp->next_transfer_handle = htole32(nextTransferHandle);
778
John Wang4d844792019-08-15 15:51:40 +0800779 auto rc = decode_set_bios_attribute_current_value_resp(
780 nullptr, 0, &completionCode, &nextTransferHandle);
George Liu684a7162019-12-06 15:10:52 +0800781 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Wang4d844792019-08-15 15:51:40 +0800782
783 rc = decode_set_bios_attribute_current_value_resp(
George Liu684a7162019-12-06 15:10:52 +0800784 response, responseMsg.size() - hdrSize - 1, &completionCode,
John Wang4d844792019-08-15 15:51:40 +0800785 &nextTransferHandle);
786
George Liu684a7162019-12-06 15:10:52 +0800787 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
John Wang81796c22019-09-06 11:18:57 +0800788}
Sridevi Ramesh08efc7b2019-12-05 05:39:46 -0600789
790TEST(GetBIOSTable, testDecodeResponse)
791{
792 uint32_t nextTransferHandle = 32;
793 uint8_t completionCode = PLDM_SUCCESS;
794 uint8_t transfer_flag = PLDM_START_AND_END;
795
796 std::array<uint8_t, hdrSize + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES>
797 responseMsg{};
798 struct pldm_msg* response =
799 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
800
801 struct pldm_get_bios_table_resp* resp =
802 reinterpret_cast<struct pldm_get_bios_table_resp*>(response->payload);
803
804 resp->completion_code = completionCode;
805 resp->next_transfer_handle = htole32(nextTransferHandle);
806 resp->transfer_flag = transfer_flag;
807 size_t biosTableOffset = sizeof(completionCode) +
808 sizeof(nextTransferHandle) + sizeof(transfer_flag);
809
810 uint8_t retCompletionCode;
811 uint32_t retNextTransferHandle;
812 uint8_t retransfer_flag;
813 size_t rebiosTableOffset = 0;
814 auto rc = decode_get_bios_table_resp(
815 response, responseMsg.size(), &retCompletionCode,
816 &retNextTransferHandle, &retransfer_flag, &rebiosTableOffset);
817
818 ASSERT_EQ(rc, PLDM_SUCCESS);
819 ASSERT_EQ(completionCode, retCompletionCode);
820 ASSERT_EQ(nextTransferHandle, retNextTransferHandle);
821 ASSERT_EQ(transfer_flag, retransfer_flag);
822 ASSERT_EQ(biosTableOffset, rebiosTableOffset);
823}