blob: 04198263b04044ad4d6d36ea8a3780acfb693312 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include <string.h>
2
3#include <array>
4#include <cstring>
5
6#include "libpldm/base.h"
7#include "libpldm/fru.h"
8
9#include <gtest/gtest.h>
10
11TEST(GetFruRecordTableMetadata, testGoodEncodeRequest)
12{
13 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
14 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
15 auto rc = encode_get_fru_record_table_metadata_req(
16 0, requestPtr, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
17 ASSERT_EQ(rc, PLDM_SUCCESS);
18 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
19 ASSERT_EQ(requestPtr->hdr.instance_id, 0u);
20 ASSERT_EQ(requestPtr->hdr.type, PLDM_FRU);
21 ASSERT_EQ(requestPtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE_METADATA);
22}
23
24TEST(GetFruRecordTableMetadata, testBadEncodeRequest)
25{
26 auto rc = encode_get_fru_record_table_metadata_req(0, NULL, 0);
27 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
28 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
29 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
30 rc = encode_get_fru_record_table_metadata_req(0, requestPtr, 1);
31 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
32}
33
34TEST(GetFruRecordTableMetadata, testGoodDecodeResponse)
35{
36
37 std::vector<uint8_t> responseMsg(
38 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES);
39 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
40 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
41 auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
42 responsePtr->payload);
43
44 responsePtr->hdr.request = PLDM_RESPONSE;
45 responsePtr->hdr.instance_id = 0;
46 responsePtr->hdr.type = PLDM_FRU;
47 responsePtr->hdr.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
48 response->completion_code = PLDM_SUCCESS;
49 response->fru_data_major_version = 0x12;
50 response->fru_data_minor_version = 0x21;
51 response->fru_table_maximum_size = htole32(0x1234ABCD);
52 response->fru_table_length = htole32(0x56781234);
53 response->total_record_set_identifiers = htole16(0x34EF);
54 response->total_table_records = htole16(0xEEEF);
55 response->checksum = htole32(0x6543FA71);
56
57 uint8_t completion_code = 0xFF;
58 uint8_t fru_data_major_version = 0x00;
59 uint8_t fru_data_minor_version = 0x00;
60 uint32_t fru_table_maximum_size = htole32(0x00000000);
61 uint32_t fru_table_length = htole32(0x00000000);
62 uint16_t total_record_set_identifiers = htole16(0x0000);
63 uint16_t total_table_records = htole16(0x0000);
64 uint32_t checksum = htole32(0x00000000);
65
66 auto rc = decode_get_fru_record_table_metadata_resp(
67 responsePtr, payload_length, &completion_code, &fru_data_major_version,
68 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
69 &total_record_set_identifiers, &total_table_records, &checksum);
70 ASSERT_EQ(rc, PLDM_SUCCESS);
71 ASSERT_EQ(completion_code, PLDM_SUCCESS);
72 ASSERT_EQ(fru_data_major_version, 0x12u);
73 ASSERT_EQ(fru_data_minor_version, 0x21u);
74 ASSERT_EQ(fru_table_maximum_size, 0x1234ABCDu);
75 ASSERT_EQ(fru_table_length, 0x56781234u);
76 ASSERT_EQ(total_record_set_identifiers, 0x34EFu);
77 ASSERT_EQ(total_table_records, 0xEEEFu);
78 ASSERT_EQ(checksum, 0x6543FA71u);
79
80 response->fru_data_major_version = 0x00;
81 response->fru_data_minor_version = 0x00;
82 response->fru_table_maximum_size = htole32(0x00000000);
83 response->fru_table_length = htole32(0x00000000);
84 response->total_record_set_identifiers = htole16(0x0000);
85 response->total_table_records = htole16(0x0000);
86 response->checksum = htole32(0x00000000);
87 fru_data_major_version = 0x00;
88 fru_data_minor_version = 0x00;
89 fru_table_maximum_size = htole32(0x00000000);
90 fru_table_length = htole32(0x00000000);
91 total_record_set_identifiers = htole16(0x0000);
92 total_table_records = htole16(0x0000);
93 checksum = htole32(0x00000000);
94 response->completion_code = PLDM_ERROR_INVALID_LENGTH;
95 rc = decode_get_fru_record_table_metadata_resp(
96 responsePtr, payload_length, &completion_code, &fru_data_major_version,
97 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
98 &total_record_set_identifiers, &total_table_records, &checksum);
99 ASSERT_EQ(rc, PLDM_SUCCESS);
100 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
101 ASSERT_EQ(responsePtr->hdr.instance_id, 0u);
102 ASSERT_EQ(responsePtr->hdr.type, PLDM_FRU);
103 ASSERT_EQ(responsePtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE_METADATA);
104 ASSERT_EQ(completion_code, PLDM_ERROR_INVALID_LENGTH);
105 ASSERT_EQ(fru_data_major_version, 0x00u);
106 ASSERT_EQ(fru_data_minor_version, 0x00u);
107 ASSERT_EQ(fru_table_maximum_size, 0x00000000u);
108 ASSERT_EQ(fru_table_length, 0x00000000u);
109 ASSERT_EQ(total_record_set_identifiers, 0x0000u);
110 ASSERT_EQ(total_table_records, 0x0000u);
111 ASSERT_EQ(checksum, 0x00000000u);
112}
113
114TEST(GetFruRecordTableMetadata, testBadDecodeResponse)
115{
116 std::vector<uint8_t> responseMsg(
117 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES);
118 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
119 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
120 auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
121 responsePtr->payload);
122
123 response->completion_code = PLDM_SUCCESS;
124 response->fru_data_major_version = 0x12;
125 response->fru_data_minor_version = 0x21;
126 response->fru_table_maximum_size = htole32(0x1234ABCD);
127 response->fru_table_length = htole32(0x56781234);
128 response->total_record_set_identifiers = htole16(0x34EF);
129 response->total_table_records = htole16(0xEEEF);
130 response->checksum = htole32(0x6543FA71);
131
132 uint8_t completion_code = 0xFF;
133 uint8_t fru_data_major_version = 0x00;
134 uint8_t fru_data_minor_version = 0x00;
135 uint32_t fru_table_maximum_size = htole32(0x00000000);
136 uint32_t fru_table_length = htole32(0x00000000);
137 uint16_t total_record_set_identifiers = htole16(0x0000);
138 uint16_t total_table_records = htole16(0x0000);
139 uint32_t checksum = htole32(0x00000000);
140
141 auto rc = decode_get_fru_record_table_metadata_resp(
142 responsePtr, PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES + 2,
143 &completion_code, &fru_data_major_version, &fru_data_minor_version,
144 &fru_table_maximum_size, &fru_table_length,
145 &total_record_set_identifiers, &total_table_records, &checksum);
146 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
147
148 rc = decode_get_fru_record_table_metadata_resp(
149 responsePtr, payload_length, &completion_code, NULL,
150 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
151 &total_record_set_identifiers, &total_table_records, &checksum);
152 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
153
154 rc = decode_get_fru_record_table_metadata_resp(
155 responsePtr, payload_length, &completion_code, &fru_data_major_version,
156 NULL, &fru_table_maximum_size, &fru_table_length,
157 &total_record_set_identifiers, &total_table_records, &checksum);
158 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
159
160 rc = decode_get_fru_record_table_metadata_resp(
161 responsePtr, payload_length, &completion_code, &fru_data_major_version,
162 &fru_data_minor_version, NULL, &fru_table_length,
163 &total_record_set_identifiers, &total_table_records, &checksum);
164 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
165
166 rc = decode_get_fru_record_table_metadata_resp(
167 responsePtr, payload_length, &completion_code, &fru_data_major_version,
168 &fru_data_minor_version, &fru_table_maximum_size, NULL,
169 &total_record_set_identifiers, &total_table_records, &checksum);
170 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
171
172 rc = decode_get_fru_record_table_metadata_resp(
173 responsePtr, payload_length, &completion_code, &fru_data_major_version,
174 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
175 NULL, &total_table_records, &checksum);
176 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
177
178 rc = decode_get_fru_record_table_metadata_resp(
179 responsePtr, payload_length, &completion_code, &fru_data_major_version,
180 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
181 &total_record_set_identifiers, NULL, &checksum);
182 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
183
184 rc = decode_get_fru_record_table_metadata_resp(
185 responsePtr, payload_length, &completion_code, &fru_data_major_version,
186 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
187 &total_record_set_identifiers, &total_table_records, NULL);
188 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
189}
190
191TEST(GetFruRecordTableMetadata, testGoodEncodeResponse)
192{
193
194 std::array<uint8_t, sizeof(pldm_msg_hdr) +
195 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES>
196 responseMsg{};
197 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
198
199 responsePtr->hdr.request = PLDM_RESPONSE;
200 responsePtr->hdr.instance_id = 0;
201 responsePtr->hdr.type = PLDM_FRU;
202 responsePtr->hdr.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
203
204 uint8_t completion_code = PLDM_SUCCESS;
205 uint8_t fru_data_major_version = 0x12;
206 uint8_t fru_data_minor_version = 0x21;
207 uint32_t fru_table_maximum_size = htole32(0x1234ABCD);
208 uint32_t fru_table_length = htole32(0x56781234);
209 uint16_t total_record_set_identifiers = htole16(0x34EF);
210 uint16_t total_table_records = htole16(0xEEEF);
211 uint32_t checksum = htole32(0x6543FA71);
212
213 auto rc = encode_get_fru_record_table_metadata_resp(
214 0, completion_code, fru_data_major_version, fru_data_minor_version,
215 fru_table_maximum_size, fru_table_length, total_record_set_identifiers,
216 total_table_records, checksum, responsePtr);
217
218 auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
219 responsePtr->payload);
220
221 ASSERT_EQ(rc, PLDM_SUCCESS);
222 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
223 ASSERT_EQ(responsePtr->hdr.instance_id, 0u);
224 ASSERT_EQ(responsePtr->hdr.type, PLDM_FRU);
225 ASSERT_EQ(responsePtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE_METADATA);
226 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
227 ASSERT_EQ(response->fru_data_major_version, 0x12u);
228 ASSERT_EQ(response->fru_data_minor_version, 0x21u);
229 ASSERT_EQ(response->fru_table_maximum_size, 0x1234ABCDu);
230 ASSERT_EQ(response->fru_table_length, 0x56781234u);
231 ASSERT_EQ(response->total_record_set_identifiers, 0x34EFu);
232 ASSERT_EQ(response->total_table_records, 0xEEEFu);
233 ASSERT_EQ(response->checksum, 0x6543FA71u);
234
235 response->fru_data_major_version = 0;
236 response->fru_data_major_version = 0x00;
237 response->fru_data_minor_version = 0x00;
238 response->fru_table_maximum_size = htole32(0x00000000);
239 response->fru_table_length = htole32(0x00000000);
240 response->total_record_set_identifiers = htole16(0x0000);
241 response->total_table_records = htole16(0x0000);
242 response->checksum = htole32(0x00000000);
243 completion_code = PLDM_ERROR_INVALID_DATA;
244 rc = encode_get_fru_record_table_metadata_resp(
245 0, completion_code, fru_data_major_version, fru_data_minor_version,
246 fru_table_maximum_size, fru_table_length, total_record_set_identifiers,
247 total_table_records, checksum, responsePtr);
248
249 ASSERT_EQ(rc, PLDM_SUCCESS);
250 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
251 ASSERT_EQ(responsePtr->hdr.instance_id, 0u);
252 ASSERT_EQ(responsePtr->hdr.type, PLDM_FRU);
253 ASSERT_EQ(responsePtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE_METADATA);
254 ASSERT_EQ(completion_code, PLDM_ERROR_INVALID_DATA);
255 ASSERT_EQ(response->fru_data_major_version, 0x00u);
256 ASSERT_EQ(response->fru_data_minor_version, 0x00u);
257 ASSERT_EQ(response->fru_table_maximum_size, 0x00000000u);
258 ASSERT_EQ(response->fru_table_length, 0x00000000u);
259 ASSERT_EQ(response->total_record_set_identifiers, 0x0000u);
260 ASSERT_EQ(response->total_table_records, 0x0000u);
261 ASSERT_EQ(response->checksum, 0x00000000u);
262}
263
264TEST(GetFruRecordTableMetadata, testBadEncodeResponse)
265{
266 std::array<uint8_t, sizeof(pldm_msg_hdr) +
267 PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES>
268 responseMsg{};
269 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
270
271 uint8_t completion_code = PLDM_SUCCESS;
272 uint8_t fru_data_major_version = 0x12;
273 uint8_t fru_data_minor_version = 0x21;
274 uint32_t fru_table_maximum_size = htole32(0x1234ABCD);
275 uint32_t fru_table_length = htole32(0x56781234);
276 uint16_t total_record_set_identifiers = htole16(0x34EF);
277 uint16_t total_table_records = htole16(0xEEEF);
278 uint32_t checksum = htole32(0x6543FA71);
279
280 auto rc = encode_get_fru_record_table_metadata_resp(
281 0, completion_code, fru_data_major_version, fru_data_minor_version,
282 fru_table_maximum_size, fru_table_length, total_record_set_identifiers,
283 total_table_records, checksum, NULL);
284
285 auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
286 responsePtr->payload);
287
288 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
289 ASSERT_EQ(completion_code, PLDM_SUCCESS);
290 ASSERT_EQ(response->fru_data_major_version, 0x00u);
291 ASSERT_EQ(response->fru_data_minor_version, 0x00u);
292 ASSERT_EQ(response->fru_table_maximum_size, 0x00000000u);
293 ASSERT_EQ(response->fru_table_length, 0x00000000u);
294 ASSERT_EQ(response->total_record_set_identifiers, 0x0000u);
295 ASSERT_EQ(response->total_table_records, 0x0000u);
296 ASSERT_EQ(response->checksum, 0x00000000u);
297}
298
299TEST(GetFruRecordTable, testGoodDecodeRequest)
300{
301 uint32_t data_transfer_handle = 31;
302 uint8_t transfer_operation_flag = PLDM_GET_FIRSTPART;
303 std::array<uint8_t,
304 PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES + sizeof(pldm_msg_hdr)>
305 requestMsg{};
306 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
307 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
308 auto request =
309 reinterpret_cast<pldm_get_fru_record_table_req*>(requestPtr->payload);
310
311 request->data_transfer_handle = htole32(data_transfer_handle);
312 request->transfer_operation_flag = transfer_operation_flag;
313
314 uint32_t ret_data_transfer_handle = 0;
315 uint8_t ret_transfer_operation_flag = 0;
316
317 // Invoke decode get FRU record table request api
318 auto rc = decode_get_fru_record_table_req(requestPtr, payload_length,
319 &ret_data_transfer_handle,
320 &ret_transfer_operation_flag);
321
322 ASSERT_EQ(rc, PLDM_SUCCESS);
323 ASSERT_EQ(data_transfer_handle, ret_data_transfer_handle);
324 ASSERT_EQ(transfer_operation_flag, ret_transfer_operation_flag);
325}
326
327TEST(GetFruRecordTable, testBadDecodeRequest)
328{
329 uint32_t data_transfer_handle = 0x0;
330 uint8_t transfer_operation_flag = PLDM_GET_FIRSTPART;
331
332 std::array<uint8_t,
333 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES>
334 requestMsg{};
335 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
336
337 // Payload message is missing
338 auto rc = decode_get_fru_record_table_req(NULL, 0, &data_transfer_handle,
339 &transfer_operation_flag);
340 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
341
342 // Payload length is invalid
343 rc = decode_get_fru_record_table_req(requestPtr, 0, &data_transfer_handle,
344 &transfer_operation_flag);
345 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
346}
347
348TEST(GetFruRecordTable, testGoodEncodeResponse)
349{
350 uint8_t completion_code = 0;
351 uint32_t next_data_transfer_handle = 32;
352 uint8_t transfer_flag = PLDM_START_AND_END;
353
354 std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
355 PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES);
356
357 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
358 auto response =
359 reinterpret_cast<pldm_get_fru_record_table_resp*>(responsePtr->payload);
360
361 // Invoke encode get FRU record table response api
362 auto rc = encode_get_fru_record_table_resp(0, completion_code,
363 next_data_transfer_handle,
364 transfer_flag, responsePtr);
365
366 ASSERT_EQ(rc, PLDM_SUCCESS);
367 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
368 ASSERT_EQ(responsePtr->hdr.instance_id, 0u);
369 ASSERT_EQ(responsePtr->hdr.type, PLDM_FRU);
370 ASSERT_EQ(responsePtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE);
371 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
372 ASSERT_EQ(le32toh(response->next_data_transfer_handle),
373 next_data_transfer_handle);
374 ASSERT_EQ(response->transfer_flag, transfer_flag);
375}
376
377TEST(GetFruRecordTable, testBadEncodeResponse)
378{
379 uint32_t next_data_transfer_handle = 32;
380 uint8_t transfer_flag = PLDM_START_AND_END;
381
382 std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
383 PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES);
384
385 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
386 auto rc = encode_get_fru_record_table_resp(
387 0, PLDM_ERROR, next_data_transfer_handle, transfer_flag, responsePtr);
388
389 ASSERT_EQ(rc, PLDM_SUCCESS);
390 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
391 ASSERT_EQ(responsePtr->hdr.instance_id, 0u);
392 ASSERT_EQ(responsePtr->hdr.type, PLDM_FRU);
393 ASSERT_EQ(responsePtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE);
394 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
395
396 rc = encode_get_fru_record_table_resp(
397 0, PLDM_SUCCESS, next_data_transfer_handle, transfer_flag, nullptr);
398
399 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
400}
401
402TEST(GetFruRecordTable, testGoodEncodeRequest)
403
404{
405 std::array<uint8_t,
406 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES>
407 requestMsg{};
408
409 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
410 auto request =
411 reinterpret_cast<pldm_get_fru_record_table_req*>(requestPtr->payload);
412
413 // Random value for data transfer handle and transfer operation flag
414 uint32_t data_transfer_handle = 32;
415 uint8_t transfer_operation_flag = PLDM_GET_FIRSTPART;
416
417 // Invoke encode get FRU record table request api
418 auto rc = encode_get_fru_record_table_req(
419 0, data_transfer_handle, transfer_operation_flag, requestPtr,
420 requestMsg.size() - sizeof(pldm_msg_hdr));
421
422 ASSERT_EQ(rc, PLDM_SUCCESS);
423 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
424 ASSERT_EQ(requestPtr->hdr.instance_id, 0u);
425 ASSERT_EQ(requestPtr->hdr.type, PLDM_FRU);
426 ASSERT_EQ(requestPtr->hdr.command, PLDM_GET_FRU_RECORD_TABLE);
427 ASSERT_EQ(le32toh(data_transfer_handle), request->data_transfer_handle);
428 ASSERT_EQ(transfer_operation_flag, request->transfer_operation_flag);
429}
430
431TEST(GetFruRecordTable, testBadEncodeRequest)
432
433{
434 uint32_t data_transfer_handle = 0x0;
435 uint8_t transfer_operation_flag = PLDM_GET_FIRSTPART;
436
437 std::array<uint8_t,
438 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES>
439 requestMsg{};
440 auto rc = encode_get_fru_record_table_req(
441 0, data_transfer_handle, transfer_operation_flag, NULL,
442 requestMsg.size() - sizeof(pldm_msg_hdr));
443
444 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
445}
446
447TEST(GetFruRecordTable, testGoodDecodeResponse)
448{
449 uint8_t completion_code = PLDM_SUCCESS;
450 uint32_t next_data_transfer_handle = 0x16;
451 uint8_t transfer_flag = PLDM_START_AND_END;
452 std::vector<uint8_t> fru_record_table_data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
453
454 std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
455 PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES +
456 fru_record_table_data.size());
457
458 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
459 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
460 auto response =
461 reinterpret_cast<pldm_get_fru_record_table_resp*>(responsePtr->payload);
462
463 response->completion_code = completion_code;
464 response->next_data_transfer_handle = htole32(next_data_transfer_handle);
465 response->transfer_flag = transfer_flag;
466 memcpy(response->fru_record_table_data, fru_record_table_data.data(),
467 fru_record_table_data.size());
468
469 uint8_t ret_completion_code = 0;
470 uint32_t ret_next_data_transfer_handle = 0;
471 uint8_t ret_transfer_flag = 0;
472 std::vector<uint8_t> ret_fru_record_table_data(9, 0);
473 size_t ret_fru_record_table_length = 0;
474
475 // Invoke decode get FRU record table response api
476 auto rc = decode_get_fru_record_table_resp(
477 responsePtr, payload_length, &ret_completion_code,
478 &ret_next_data_transfer_handle, &ret_transfer_flag,
479 ret_fru_record_table_data.data(), &ret_fru_record_table_length);
480 ASSERT_EQ(rc, PLDM_SUCCESS);
481 ASSERT_EQ(completion_code, ret_completion_code);
482 ASSERT_EQ(next_data_transfer_handle, ret_next_data_transfer_handle);
483 ASSERT_EQ(transfer_flag, ret_transfer_flag);
484 ASSERT_EQ(0, memcmp(fru_record_table_data.data(),
485 ret_fru_record_table_data.data(),
486 ret_fru_record_table_length));
487 ASSERT_EQ(fru_record_table_data.size(), ret_fru_record_table_length);
488}
489
490TEST(GetFruRecordTable, testBadDecodeResponse)
491{
492 uint8_t completion_code = 0;
493 uint32_t next_data_transfer_handle = 0;
494 uint8_t transfer_flag = PLDM_START_AND_END;
495 std::vector<uint8_t> fru_record_table_data(9, 0);
496 size_t fru_record_table_length = 0;
497
498 std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
499 PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES +
500 fru_record_table_data.size());
501
502 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
503
504 // Payload message is missing
505 auto rc = decode_get_fru_record_table_resp(
506 NULL, 0, &completion_code, &next_data_transfer_handle, &transfer_flag,
507 fru_record_table_data.data(), &fru_record_table_length);
508
509 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
510
511 // Payload length is invalid
512 rc = decode_get_fru_record_table_resp(
513 responsePtr, 0, &completion_code, &next_data_transfer_handle,
514 &transfer_flag, fru_record_table_data.data(), &fru_record_table_length);
515
516 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
517}
518
519TEST(GetFRURecordByOption, testGoodEncodeRequest)
520{
521 uint8_t instanceId = 2;
522 uint32_t dataTransferHandle = 3;
523 uint16_t fruTableHandle = 4;
524 uint16_t recordSetIdentifier = 5;
525 uint8_t recordType = 6;
526 uint8_t fieldType = 7;
527 uint8_t transferOpFlag = 0;
528
529 constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
530
531 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
532 auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
533
534 auto rc = encode_get_fru_record_by_option_req(
535 instanceId, dataTransferHandle, fruTableHandle, recordSetIdentifier,
536 recordType, fieldType, transferOpFlag, reqMsg, payLoadLength);
537
538 EXPECT_EQ(rc, PLDM_SUCCESS);
539 EXPECT_EQ(instanceId, reqMsg->hdr.instance_id);
540
541 auto payLoadMsg =
542 reinterpret_cast<pldm_get_fru_record_by_option_req*>(reqMsg->payload);
543
544 EXPECT_EQ(le32toh(payLoadMsg->data_transfer_handle), dataTransferHandle);
545 EXPECT_EQ(le16toh(payLoadMsg->fru_table_handle), fruTableHandle);
546 EXPECT_EQ(le16toh(payLoadMsg->record_set_identifier), recordSetIdentifier);
547 EXPECT_EQ(payLoadMsg->record_type, recordType);
548 EXPECT_EQ(payLoadMsg->field_type, fieldType);
549 EXPECT_EQ(payLoadMsg->transfer_op_flag, transferOpFlag);
550}
551
552TEST(GetFRURecordByOption, testBadEncodeRequest)
553{
554
555 constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
556
557 auto rc = encode_get_fru_record_by_option_req(1, 2, 3, 4, 5, 6, 0, nullptr,
558 payLoadLength);
559
560 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
561 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
562 auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
563
564 rc = encode_get_fru_record_by_option_req(1, 2, 3, 4, 5, 6, 0, reqMsg,
565 payLoadLength - 1);
566
567 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
568}
569
570TEST(GetFRURecordByOption, testGoodDecodeRequest)
571{
572 uint8_t instanceId = 2;
573 uint32_t dataTransferHandle = 3;
574 uint16_t fruTableHandle = 4;
575 uint16_t recordSetIdentifier = 5;
576 uint8_t recordType = 6;
577 uint8_t fieldType = 7;
578 uint8_t transferOpFlag = 0;
579
580 constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
581
582 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
583 auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
584
585 auto rc = encode_get_fru_record_by_option_req(
586 instanceId, dataTransferHandle, fruTableHandle, recordSetIdentifier,
587 recordType, fieldType, transferOpFlag, reqMsg, payLoadLength);
588
589 EXPECT_EQ(rc, PLDM_SUCCESS);
590
591 uint32_t retDataTransferHandle{};
592 uint16_t retFruTableHandle{};
593 uint16_t retRecordSetIdentifier{};
594 uint8_t retRecordType{};
595 uint8_t retFieldType{};
596 uint8_t retTransferOpFlag{};
597
598 rc = decode_get_fru_record_by_option_req(
599 reqMsg, payLoadLength, &retDataTransferHandle, &retFruTableHandle,
600 &retRecordSetIdentifier, &retRecordType, &retFieldType,
601 &retTransferOpFlag);
602
603 EXPECT_EQ(rc, PLDM_SUCCESS);
604 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
605 EXPECT_EQ(retFruTableHandle, fruTableHandle);
606 EXPECT_EQ(retRecordSetIdentifier, recordSetIdentifier);
607 EXPECT_EQ(retRecordType, recordType);
608 EXPECT_EQ(retFieldType, fieldType);
609 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
610}
611
612TEST(GetFRURecordByOption, testBadDecodeRequest)
613{
614 constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
615 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request{};
616 auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
617
618 uint32_t retDataTransferHandle{};
619 uint16_t retFruTableHandle{};
620 uint16_t retRecordSetIdentifier{};
621 uint8_t retRecordType{};
622 uint8_t retFieldType{};
623 uint8_t retTransferOpFlag{};
624
625 auto rc = decode_get_fru_record_by_option_req(
626 reqMsg, payLoadLength - 1, &retDataTransferHandle, &retFruTableHandle,
627 &retRecordSetIdentifier, &retRecordType, &retFieldType,
628 &retTransferOpFlag);
629
630 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
631
632 rc = decode_get_fru_record_by_option_req(
633 reqMsg, payLoadLength - 1, nullptr, &retFruTableHandle,
634 &retRecordSetIdentifier, &retRecordType, &retFieldType,
635 &retTransferOpFlag);
636
637 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
638}
639
640TEST(GetFruRecordByOption, testGoodEncodeResponse)
641{
642 uint8_t completionCode = PLDM_SUCCESS;
643 uint8_t instanceId = 2;
644 uint32_t dataTransferHandle = 3;
645 uint8_t transferFlag = 5;
646
647 std::array<uint8_t, 5> fruData = {1, 2, 3, 4, 5};
648 constexpr auto payLoadLength =
649 sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
650
651 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
652 auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
653
654 auto rc = encode_get_fru_record_by_option_resp(
655 instanceId, completionCode, dataTransferHandle, transferFlag,
656 fruData.data(), fruData.size(), respMsg, payLoadLength);
657
658 auto payLoadMsg =
659 reinterpret_cast<pldm_get_fru_record_by_option_resp*>(respMsg->payload);
660
661 EXPECT_EQ(rc, PLDM_SUCCESS);
662 EXPECT_EQ(payLoadMsg->completion_code, completionCode);
663 EXPECT_EQ(payLoadMsg->next_data_transfer_handle,
664 htole32(dataTransferHandle));
665 EXPECT_EQ(payLoadMsg->transfer_flag, transferFlag);
666
667 EXPECT_EQ(std::memcmp(payLoadMsg->fru_structure_data, fruData.data(),
668 fruData.size()),
669 0);
670}
671
672TEST(GetFruRecordByOption, testBadEncodeResponse)
673{
674 uint8_t completionCode = PLDM_SUCCESS;
675 uint8_t instanceId = 2;
676 uint32_t dataTransferHandle = 3;
677 uint8_t transferFlag = 5;
678
679 std::array<uint8_t, 5> fruData = {1, 2, 3, 4, 5};
680 constexpr auto payLoadLength =
681 sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
682
683 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
684 auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
685
686 auto rc = encode_get_fru_record_by_option_resp(
687 instanceId, completionCode, dataTransferHandle, transferFlag, nullptr,
688 fruData.size(), respMsg, payLoadLength);
689
690 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
691
692 rc = encode_get_fru_record_by_option_resp(
693 instanceId, completionCode, dataTransferHandle, transferFlag,
694 fruData.data(), fruData.size(), respMsg, payLoadLength - 1);
695
696 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
697}
698
699TEST(GetFruRecordByOption, testGoodDecodeResponse)
700{
701 uint8_t completionCode = PLDM_SUCCESS;
702 uint8_t instanceId = 2;
703 uint32_t dataTransferHandle = 3;
704 uint8_t transferFlag = 5;
705
706 std::array<uint8_t, 5> fruData = {1, 2, 3, 4, 5};
707 constexpr auto payLoadLength =
708 sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
709
710 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
711 auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
712
713 auto rc = encode_get_fru_record_by_option_resp(
714 instanceId, completionCode, dataTransferHandle, transferFlag,
715 fruData.data(), fruData.size(), respMsg, payLoadLength);
716
717 EXPECT_EQ(rc, PLDM_SUCCESS);
718
719 uint8_t retCompletionCode;
720 uint32_t retDataTransferHandle;
721 uint8_t retTransferFlag;
722 variable_field retFruData;
723
724 rc = decode_get_fru_record_by_option_resp(
725 respMsg, payLoadLength, &retCompletionCode, &retDataTransferHandle,
726 &retTransferFlag, &retFruData);
727
728 EXPECT_EQ(rc, PLDM_SUCCESS);
729 EXPECT_EQ(retCompletionCode, completionCode);
730 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
731 EXPECT_EQ(retTransferFlag, transferFlag);
732 EXPECT_EQ(retFruData.length, fruData.size());
733 EXPECT_EQ(std::memcmp(fruData.data(), retFruData.ptr, fruData.size()), 0);
734}
735
736TEST(GetFruRecordByOption, testBadDecodeResponse)
737{
738
739 uint8_t completionCode = PLDM_SUCCESS;
740 uint8_t instanceId = 2;
741 uint32_t dataTransferHandle = 3;
742 uint8_t transferFlag = 5;
743
744 std::array<uint8_t, 5> fruData = {1, 2, 3, 4, 5};
745 constexpr auto payLoadLength =
746 sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
747
748 std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
749 auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
750
751 auto rc = encode_get_fru_record_by_option_resp(
752 instanceId, completionCode, dataTransferHandle, transferFlag,
753 fruData.data(), fruData.size(), respMsg, payLoadLength);
754
755 EXPECT_EQ(rc, PLDM_SUCCESS);
756
757 uint8_t retCompletionCode;
758 uint32_t retDataTransferHandle;
759 uint8_t retTransferFlag;
760 variable_field retFruData;
761
762 rc = decode_get_fru_record_by_option_resp(respMsg, payLoadLength, nullptr,
763 &retDataTransferHandle,
764 &retTransferFlag, &retFruData);
765
766 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
767
768 rc = decode_get_fru_record_by_option_resp(
769 respMsg, PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES - 1,
770 &retCompletionCode, &retDataTransferHandle, &retTransferFlag,
771 &retFruData);
772
773 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
774}
775
776TEST(SetFRURecordTable, testGoodEncodeResponse)
777{
778 uint8_t instanceId = 2;
779 uint8_t completionCode = PLDM_SUCCESS;
780 uint32_t nextDataTransferHandle = 32;
781
782 std::array<uint8_t,
783 sizeof(pldm_msg_hdr) + PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES>
784 responseMsg{};
785 struct pldm_msg* response =
786 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
787 auto rc = encode_set_fru_record_table_resp(
788 instanceId, completionCode, nextDataTransferHandle,
789 responseMsg.size() - sizeof(pldm_msg_hdr), response);
790 EXPECT_EQ(rc, PLDM_SUCCESS);
791
792 struct pldm_set_fru_record_table_resp* resp =
793 reinterpret_cast<struct pldm_set_fru_record_table_resp*>(
794 response->payload);
795 EXPECT_EQ(completionCode, resp->completion_code);
796 EXPECT_EQ(htole32(nextDataTransferHandle), resp->next_data_transfer_handle);
797}
798
799TEST(SetFRURecordTable, testBadEncodeResponse)
800{
801 uint8_t instanceId = 0;
802 uint8_t completionCode = PLDM_SUCCESS;
803 uint32_t nextDataTransferHandle = 1;
804
805 std::array<uint8_t,
806 sizeof(pldm_msg_hdr) + PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES>
807 responseMsg{};
808 struct pldm_msg* response =
809 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
810
811 auto rc = encode_set_fru_record_table_resp(
812 instanceId, completionCode, nextDataTransferHandle,
813 responseMsg.size() - sizeof(pldm_msg_hdr), NULL);
814 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
815
816 rc = encode_set_fru_record_table_resp(
817 instanceId, completionCode, nextDataTransferHandle,
818 responseMsg.size() - sizeof(pldm_msg_hdr) - 1, response);
819 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
820}
821
822TEST(SetFRURecordTable, testGoodDecodeRequest)
823{
824 uint32_t transferHandle = 1;
825 uint8_t transferFlag = PLDM_GET_FIRSTPART;
826 uint32_t tableData = 44;
827
828 std::array<uint8_t, sizeof(pldm_msg_hdr) +
829 PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES +
830 sizeof(tableData)>
831 requestMsg{};
832 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
833 struct pldm_set_fru_record_table_req* req =
834 reinterpret_cast<struct pldm_set_fru_record_table_req*>(
835 request->payload);
836 req->data_transfer_handle = htole32(transferHandle);
837 req->transfer_flag = transferFlag;
838 memcpy(req->fru_record_table_data, &tableData, sizeof(tableData));
839
840 uint32_t retTransferHandle;
841 uint8_t retTransferFlag;
842 struct variable_field table;
843
844 auto rc = decode_set_fru_record_table_req(
845 request, requestMsg.size() - sizeof(pldm_msg_hdr), &retTransferHandle,
846 &retTransferFlag, &table);
847
848 EXPECT_EQ(rc, PLDM_SUCCESS);
849 EXPECT_EQ(retTransferHandle, transferHandle);
850 EXPECT_EQ(retTransferFlag, transferFlag);
851 EXPECT_EQ(table.length, sizeof(tableData));
852 EXPECT_EQ(0, memcmp(table.ptr, &tableData, sizeof(tableData)));
853}
854
855TEST(SetFRURecordTable, testBadDecodeRequest)
856{
857 uint32_t transferHandle = 1;
858 uint8_t transferFlag = PLDM_GET_FIRSTPART;
859 uint32_t tableData = 44;
860
861 std::array<uint8_t, sizeof(pldm_msg_hdr) +
862 PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES +
863 sizeof(tableData)>
864 requestMsg{};
865 auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
866 struct pldm_set_fru_record_table_req* req =
867 reinterpret_cast<struct pldm_set_fru_record_table_req*>(
868 request->payload);
869 req->data_transfer_handle = htole32(transferHandle);
870 req->transfer_flag = transferFlag;
871 memcpy(req->fru_record_table_data, &tableData, sizeof(tableData));
872
873 uint32_t retTransferHandle;
874 uint8_t retTransferFlag;
875
876 auto rc = decode_set_fru_record_table_req(
877 request, requestMsg.size() - sizeof(pldm_msg_hdr), &retTransferHandle,
878 &retTransferFlag, NULL);
879 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
880
881 struct variable_field table;
882 rc = decode_set_fru_record_table_req(
883 request,
884 requestMsg.size() - sizeof(pldm_msg_hdr) - sizeof(tableData) - 1,
885 &retTransferHandle, &retTransferFlag, &table);
886 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
887}