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