blob: cb5d22e7443a672d3aada8aa88ff03906bbc065f [file] [log] [blame]
Tom Joseph3fd3eb82021-06-18 04:13:29 -07001#include <bitset>
Tom Joseph763b51e2021-06-05 04:50:47 -07002#include <cstring>
3
gokulsanker138ceba2021-04-05 13:25:25 +05304#include "libpldm/base.h"
5#include "libpldm/firmware_update.h"
6
7#include <gtest/gtest.h>
8
9constexpr auto hdrSize = sizeof(pldm_msg_hdr);
10
Tom Joseph568e4702021-06-07 22:15:49 -070011TEST(DecodePackageHeaderInfo, goodPath)
12{
13 // Package header identifier for Version 1.0.x
14 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
15 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43,
16 0x98, 0x00, 0xa0, 0x2F, 0x05, 0x9a, 0xca, 0x02};
17 // Package header version for DSP0267 version 1.0.x
18 constexpr uint8_t pkgHeaderFormatRevision = 0x01;
19 // Random PackageHeaderSize
20 constexpr uint16_t pkgHeaderSize = 303;
21 // PackageReleaseDateTime - "25/12/2021 00:00:00"
22 std::array<uint8_t, PLDM_TIMESTAMP104_SIZE> timestamp104{
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0x00, 0x19, 0x0c, 0xe5, 0x07, 0x00};
25 constexpr uint16_t componentBitmapBitLength = 8;
26 // PackageVersionString
27 constexpr std::string_view packageVersionStr{"OpenBMCv1.0"};
28 constexpr size_t packagerHeaderSize =
29 sizeof(pldm_package_header_information) + packageVersionStr.size();
30
31 constexpr std::array<uint8_t, packagerHeaderSize> packagerHeaderInfo{
32 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00, 0xa0, 0x2F,
33 0x05, 0x9a, 0xca, 0x02, 0x01, 0x2f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5, 0x07, 0x00, 0x08, 0x00, 0x01, 0x0b,
35 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
36 pldm_package_header_information pkgHeader{};
37 variable_field packageVersion{};
38
39 auto rc = decode_pldm_package_header_info(packagerHeaderInfo.data(),
40 packagerHeaderInfo.size(),
41 &pkgHeader, &packageVersion);
42
43 EXPECT_EQ(rc, PLDM_SUCCESS);
44 EXPECT_EQ(true,
45 std::equal(pkgHeader.uuid, pkgHeader.uuid + PLDM_FWUP_UUID_LENGTH,
46 uuid.begin(), uuid.end()));
47 EXPECT_EQ(pkgHeader.package_header_format_version, pkgHeaderFormatRevision);
48 EXPECT_EQ(pkgHeader.package_header_size, pkgHeaderSize);
49 EXPECT_EQ(true, std::equal(pkgHeader.timestamp104,
50 pkgHeader.timestamp104 + PLDM_TIMESTAMP104_SIZE,
51 timestamp104.begin(), timestamp104.end()));
52 EXPECT_EQ(pkgHeader.component_bitmap_bit_length, componentBitmapBitLength);
53 EXPECT_EQ(pkgHeader.package_version_string_type, PLDM_STR_TYPE_ASCII);
54 EXPECT_EQ(pkgHeader.package_version_string_length,
55 packageVersionStr.size());
56 std::string packageVersionString(
57 reinterpret_cast<const char*>(packageVersion.ptr),
58 packageVersion.length);
59 EXPECT_EQ(packageVersionString, packageVersionStr);
60}
61
62TEST(DecodePackageHeaderInfo, errorPaths)
63{
64 int rc = 0;
65 constexpr std::string_view packageVersionStr{"OpenBMCv1.0"};
66 constexpr size_t packagerHeaderSize =
67 sizeof(pldm_package_header_information) + packageVersionStr.size();
68
69 // Invalid Package Version String Type - 0x06
70 constexpr std::array<uint8_t, packagerHeaderSize>
71 invalidPackagerHeaderInfo1{
72 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
73 0xa0, 0x2F, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
75 0x07, 0x00, 0x08, 0x00, 0x06, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
76 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
77
78 pldm_package_header_information packageHeader{};
79 variable_field packageVersion{};
80
81 rc = decode_pldm_package_header_info(nullptr,
82 invalidPackagerHeaderInfo1.size(),
83 &packageHeader, &packageVersion);
84 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
85
86 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
87 invalidPackagerHeaderInfo1.size(),
88 nullptr, &packageVersion);
89 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
90
91 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
92 invalidPackagerHeaderInfo1.size(),
93 &packageHeader, nullptr);
94 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
95
96 rc = decode_pldm_package_header_info(
97 invalidPackagerHeaderInfo1.data(),
98 sizeof(pldm_package_header_information) - 1, &packageHeader,
99 &packageVersion);
100 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
101
102 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
103 invalidPackagerHeaderInfo1.size(),
104 &packageHeader, &packageVersion);
105 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
106
107 // Invalid Package Version String Length - 0x00
108 constexpr std::array<uint8_t, packagerHeaderSize>
109 invalidPackagerHeaderInfo2{
110 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
111 0xa0, 0x2F, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
113 0x07, 0x00, 0x08, 0x00, 0x01, 0x00, 0x4f, 0x70, 0x65, 0x6e,
114 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
115 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo2.data(),
116 invalidPackagerHeaderInfo2.size(),
117 &packageHeader, &packageVersion);
118 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
119
120 // Package version string length less than in the header information
121 constexpr std::array<uint8_t, packagerHeaderSize - 1>
122 invalidPackagerHeaderInfo3{
123 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
124 0xa0, 0x2F, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
126 0x07, 0x00, 0x08, 0x00, 0x01, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
127 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e};
128 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo3.data(),
129 invalidPackagerHeaderInfo3.size(),
130 &packageHeader, &packageVersion);
131 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
132
133 // ComponentBitmapBitLength not a multiple of 8
134 constexpr std::array<uint8_t, packagerHeaderSize>
135 invalidPackagerHeaderInfo4{
136 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
137 0xa0, 0x2F, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
139 0x07, 0x00, 0x09, 0x00, 0x01, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
140 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
141 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo4.data(),
142 invalidPackagerHeaderInfo4.size(),
143 &packageHeader, &packageVersion);
144 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
145}
146
Tom Joseph64af3452021-06-08 04:05:28 -0700147TEST(DecodeFirmwareDeviceIdRecord, goodPath)
148{
149 constexpr uint8_t descriptorCount = 1;
150 // Continue component updates after failure
151 constexpr std::bitset<32> deviceUpdateFlag{1};
152 constexpr uint16_t componentBitmapBitLength = 16;
153 // Applicable Components - 1,2,5,8,9
154 std::vector<std::bitset<8>> applicableComponentsBitfield{0x93, 0x01};
155 // ComponentImageSetVersionString
156 constexpr std::string_view imageSetVersionStr{"VersionString1"};
157 // Initial descriptor - UUID
158 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
159 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
160 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
161 constexpr uint16_t fwDevicePkgDataLen = 2;
162 // FirmwareDevicePackageData
163 constexpr std::array<uint8_t, fwDevicePkgDataLen> fwDevicePkgData{0xab,
164 0xcd};
165 // Size of the firmware device ID record
166 constexpr uint16_t recordLen =
167 sizeof(pldm_firmware_device_id_record) +
168 (componentBitmapBitLength / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) +
169 imageSetVersionStr.size() + sizeof(pldm_descriptor_tlv) - 1 +
170 uuid.size() + fwDevicePkgData.size();
171 // Firmware device ID record
172 constexpr std::array<uint8_t, recordLen> record{
173 0x31, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x02,
174 0x00, 0x93, 0x01, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
175 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x02, 0x00, 0x10,
176 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18, 0xa0,
177 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b, 0xab, 0xcd};
178
179 pldm_firmware_device_id_record deviceIdRecHeader{};
180 variable_field applicableComponents{};
181 variable_field outCompImageSetVersionStr{};
182 variable_field recordDescriptors{};
183 variable_field outFwDevicePkgData{};
184
185 auto rc = decode_firmware_device_id_record(
186 record.data(), record.size(), componentBitmapBitLength,
187 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
188 &recordDescriptors, &outFwDevicePkgData);
189
190 EXPECT_EQ(rc, PLDM_SUCCESS);
191 EXPECT_EQ(deviceIdRecHeader.record_length, recordLen);
192 EXPECT_EQ(deviceIdRecHeader.descriptor_count, descriptorCount);
193 EXPECT_EQ(deviceIdRecHeader.device_update_option_flags.value,
194 deviceUpdateFlag);
195 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_type,
196 PLDM_STR_TYPE_ASCII);
197 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_length,
198 imageSetVersionStr.size());
199 EXPECT_EQ(deviceIdRecHeader.fw_device_pkg_data_length, fwDevicePkgDataLen);
200
201 EXPECT_EQ(applicableComponents.length, applicableComponentsBitfield.size());
202 EXPECT_EQ(true,
203 std::equal(applicableComponents.ptr,
204 applicableComponents.ptr + applicableComponents.length,
205 applicableComponentsBitfield.begin(),
206 applicableComponentsBitfield.end()));
207
208 EXPECT_EQ(outCompImageSetVersionStr.length, imageSetVersionStr.size());
209 std::string compImageSetVersionStr(
210 reinterpret_cast<const char*>(outCompImageSetVersionStr.ptr),
211 outCompImageSetVersionStr.length);
212 EXPECT_EQ(compImageSetVersionStr, imageSetVersionStr);
213
214 uint16_t descriptorType = 0;
215 uint16_t descriptorLen = 0;
216 variable_field descriptorData{};
217 // DescriptorCount is 1, so decode_descriptor_type_length_value called once
218 rc = decode_descriptor_type_length_value(recordDescriptors.ptr,
219 recordDescriptors.length,
220 &descriptorType, &descriptorData);
221 EXPECT_EQ(rc, PLDM_SUCCESS);
222 EXPECT_EQ(recordDescriptors.length, sizeof(descriptorType) +
223 sizeof(descriptorLen) +
224 descriptorData.length);
225 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
226 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
227 EXPECT_EQ(true, std::equal(descriptorData.ptr,
228 descriptorData.ptr + descriptorData.length,
229 uuid.begin(), uuid.end()));
230
231 EXPECT_EQ(outFwDevicePkgData.length, fwDevicePkgData.size());
232 EXPECT_EQ(true,
233 std::equal(outFwDevicePkgData.ptr,
234 outFwDevicePkgData.ptr + outFwDevicePkgData.length,
235 fwDevicePkgData.begin(), fwDevicePkgData.end()));
236}
237
238TEST(DecodeFirmwareDeviceIdRecord, goodPathNofwDevicePkgData)
239{
240 constexpr uint8_t descriptorCount = 1;
241 // Continue component updates after failure
242 constexpr std::bitset<32> deviceUpdateFlag{1};
243 constexpr uint16_t componentBitmapBitLength = 8;
244 // Applicable Components - 1,2
245 std::vector<std::bitset<8>> applicableComponentsBitfield{0x03};
246 // ComponentImageSetVersionString
247 constexpr std::string_view imageSetVersionStr{"VersionString1"};
248 // Initial descriptor - UUID
249 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
250 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
251 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
252 constexpr uint16_t fwDevicePkgDataLen = 0;
253
254 // Size of the firmware device ID record
255 constexpr uint16_t recordLen =
256 sizeof(pldm_firmware_device_id_record) +
257 (componentBitmapBitLength / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) +
258 imageSetVersionStr.size() +
259 sizeof(pldm_descriptor_tlv().descriptor_type) +
260 sizeof(pldm_descriptor_tlv().descriptor_length) + uuid.size() +
261 fwDevicePkgDataLen;
262 // Firmware device ID record
263 constexpr std::array<uint8_t, recordLen> record{
264 0x2e, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x03,
265 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e,
266 0x67, 0x31, 0x02, 0x00, 0x10, 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d,
267 0x47, 0x18, 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
268
269 pldm_firmware_device_id_record deviceIdRecHeader{};
270 variable_field applicableComponents{};
271 variable_field outCompImageSetVersionStr{};
272 variable_field recordDescriptors{};
273 variable_field outFwDevicePkgData{};
274
275 auto rc = decode_firmware_device_id_record(
276 record.data(), record.size(), componentBitmapBitLength,
277 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
278 &recordDescriptors, &outFwDevicePkgData);
279
280 EXPECT_EQ(rc, PLDM_SUCCESS);
281 EXPECT_EQ(deviceIdRecHeader.record_length, recordLen);
282 EXPECT_EQ(deviceIdRecHeader.descriptor_count, descriptorCount);
283 EXPECT_EQ(deviceIdRecHeader.device_update_option_flags.value,
284 deviceUpdateFlag);
285 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_type,
286 PLDM_STR_TYPE_ASCII);
287 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_length,
288 imageSetVersionStr.size());
289 EXPECT_EQ(deviceIdRecHeader.fw_device_pkg_data_length, 0);
290
291 EXPECT_EQ(applicableComponents.length, applicableComponentsBitfield.size());
292 EXPECT_EQ(true,
293 std::equal(applicableComponents.ptr,
294 applicableComponents.ptr + applicableComponents.length,
295 applicableComponentsBitfield.begin(),
296 applicableComponentsBitfield.end()));
297
298 EXPECT_EQ(outCompImageSetVersionStr.length, imageSetVersionStr.size());
299 std::string compImageSetVersionStr(
300 reinterpret_cast<const char*>(outCompImageSetVersionStr.ptr),
301 outCompImageSetVersionStr.length);
302 EXPECT_EQ(compImageSetVersionStr, imageSetVersionStr);
303
304 uint16_t descriptorType = 0;
305 uint16_t descriptorLen = 0;
306 variable_field descriptorData{};
307 // DescriptorCount is 1, so decode_descriptor_type_length_value called once
308 rc = decode_descriptor_type_length_value(recordDescriptors.ptr,
309 recordDescriptors.length,
310 &descriptorType, &descriptorData);
311 EXPECT_EQ(rc, PLDM_SUCCESS);
312 EXPECT_EQ(recordDescriptors.length, sizeof(descriptorType) +
313 sizeof(descriptorLen) +
314 descriptorData.length);
315 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
316 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
317 EXPECT_EQ(true, std::equal(descriptorData.ptr,
318 descriptorData.ptr + descriptorData.length,
319 uuid.begin(), uuid.end()));
320
321 EXPECT_EQ(outFwDevicePkgData.ptr, nullptr);
322 EXPECT_EQ(outFwDevicePkgData.length, 0);
323}
324
325TEST(DecodeFirmwareDeviceIdRecord, ErrorPaths)
326{
327 constexpr uint16_t componentBitmapBitLength = 8;
328 // Invalid ComponentImageSetVersionStringType
329 constexpr std::array<uint8_t, 11> invalidRecord1{
330 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00};
331
332 int rc = 0;
333 pldm_firmware_device_id_record deviceIdRecHeader{};
334 variable_field applicableComponents{};
335 variable_field outCompImageSetVersionStr{};
336 variable_field recordDescriptors{};
337 variable_field outFwDevicePkgData{};
338
339 rc = decode_firmware_device_id_record(
340 nullptr, invalidRecord1.size(), componentBitmapBitLength,
341 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
342 &recordDescriptors, &outFwDevicePkgData);
343 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
344
345 rc = decode_firmware_device_id_record(
346 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
347 nullptr, &applicableComponents, &outCompImageSetVersionStr,
348 &recordDescriptors, &outFwDevicePkgData);
349 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
350
351 rc = decode_firmware_device_id_record(
352 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
353 &deviceIdRecHeader, nullptr, &outCompImageSetVersionStr,
354 &recordDescriptors, &outFwDevicePkgData);
355 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
356
357 rc = decode_firmware_device_id_record(
358 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
359 &deviceIdRecHeader, &applicableComponents, nullptr, &recordDescriptors,
360 &outFwDevicePkgData);
361 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
362
363 rc = decode_firmware_device_id_record(
364 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
365 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
366 nullptr, &outFwDevicePkgData);
367 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
368
369 rc = decode_firmware_device_id_record(
370 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
371 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
372 &recordDescriptors, nullptr);
373 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
374
375 rc = decode_firmware_device_id_record(
376 invalidRecord1.data(), invalidRecord1.size() - 1,
377 componentBitmapBitLength, &deviceIdRecHeader, &applicableComponents,
378 &outCompImageSetVersionStr, &recordDescriptors, &outFwDevicePkgData);
379 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
380
381 rc = decode_firmware_device_id_record(
382 invalidRecord1.data(), invalidRecord1.size(),
383 componentBitmapBitLength + 1, &deviceIdRecHeader, &applicableComponents,
384 &outCompImageSetVersionStr, &recordDescriptors, &outFwDevicePkgData);
385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
386
387 rc = decode_firmware_device_id_record(
388 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
389 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
390 &recordDescriptors, &outFwDevicePkgData);
391 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
392
393 // Invalid ComponentImageSetVersionStringLength
394 constexpr std::array<uint8_t, 11> invalidRecord2{
395 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
396 rc = decode_firmware_device_id_record(
397 invalidRecord2.data(), invalidRecord2.size(), componentBitmapBitLength,
398 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
399 &recordDescriptors, &outFwDevicePkgData);
400 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
401
402 // invalidRecord3 size is less than RecordLength
403 constexpr std::array<uint8_t, 11> invalidRecord3{
404 0x2e, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x00, 0x00};
405 rc = decode_firmware_device_id_record(
406 invalidRecord3.data(), invalidRecord3.size(), componentBitmapBitLength,
407 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
408 &recordDescriptors, &outFwDevicePkgData);
409 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
410
411 // RecordLength is less than the calculated RecordLength
412 constexpr std::array<uint8_t, 11> invalidRecord4{
413 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x02, 0x00};
414 rc = decode_firmware_device_id_record(
415 invalidRecord4.data(), invalidRecord4.size(), componentBitmapBitLength,
416 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
417 &recordDescriptors, &outFwDevicePkgData);
418 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
419}
420
Tom Joseph763b51e2021-06-05 04:50:47 -0700421TEST(DecodeDescriptors, goodPath3Descriptors)
422{
423 // In the descriptor data there are 3 descriptor entries
424 // 1) IANA enterprise ID
425 constexpr std::array<uint8_t, PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH> iana{
426 0x0a, 0x0b, 0x0c, 0xd};
427 // 2) UUID
428 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
429 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
430 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
431 // 3) Vendor Defined
432 constexpr std::string_view vendorTitle{"OpenBMC"};
433 constexpr size_t vendorDescriptorLen = 2;
434 constexpr std::array<uint8_t, vendorDescriptorLen> vendorDescriptorData{
435 0x01, 0x02};
436
437 constexpr size_t vendorDefinedDescriptorLen =
438 sizeof(pldm_vendor_defined_descriptor_title_data()
439 .vendor_defined_descriptor_title_str_type) +
440 sizeof(pldm_vendor_defined_descriptor_title_data()
441 .vendor_defined_descriptor_title_str_len) +
442 vendorTitle.size() + vendorDescriptorData.size();
443
444 constexpr size_t descriptorsLength =
445 3 * (sizeof(pldm_descriptor_tlv().descriptor_type) +
446 sizeof(pldm_descriptor_tlv().descriptor_length)) +
447 iana.size() + uuid.size() + vendorDefinedDescriptorLen;
448
449 constexpr std::array<uint8_t, descriptorsLength> descriptors{
450 0x01, 0x00, 0x04, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x02, 0x00, 0x10,
451 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18, 0xa0, 0x30,
452 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b, 0xFF, 0xFF, 0x0B, 0x00, 0x01,
453 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43, 0x01, 0x02};
454
455 size_t descriptorCount = 1;
456 size_t descriptorsRemainingLength = descriptorsLength;
457 int rc = 0;
458
459 while (descriptorsRemainingLength && (descriptorCount <= 3))
460 {
461 uint16_t descriptorType = 0;
462 uint16_t descriptorLen = 0;
463 variable_field descriptorData{};
464
465 rc = decode_descriptor_type_length_value(
466 descriptors.data() + descriptorsLength - descriptorsRemainingLength,
467 descriptorsRemainingLength, &descriptorType, &descriptorData);
468 EXPECT_EQ(rc, PLDM_SUCCESS);
469
470 if (descriptorCount == 1)
471 {
472 EXPECT_EQ(descriptorType, PLDM_FWUP_IANA_ENTERPRISE_ID);
473 EXPECT_EQ(descriptorData.length,
474 PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH);
475 EXPECT_EQ(true,
476 std::equal(descriptorData.ptr,
477 descriptorData.ptr + descriptorData.length,
478 iana.begin(), iana.end()));
479 }
480 else if (descriptorCount == 2)
481 {
482 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
483 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
484 EXPECT_EQ(true,
485 std::equal(descriptorData.ptr,
486 descriptorData.ptr + descriptorData.length,
487 uuid.begin(), uuid.end()));
488 }
489 else if (descriptorCount == 3)
490 {
491 EXPECT_EQ(descriptorType, PLDM_FWUP_VENDOR_DEFINED);
492 EXPECT_EQ(descriptorData.length, vendorDefinedDescriptorLen);
493
494 uint8_t descriptorTitleStrType = 0;
495 variable_field descriptorTitleStr{};
496 variable_field vendorDefinedDescriptorData{};
497
498 rc = decode_vendor_defined_descriptor_value(
499 descriptorData.ptr, descriptorData.length,
500 &descriptorTitleStrType, &descriptorTitleStr,
501 &vendorDefinedDescriptorData);
502 EXPECT_EQ(rc, PLDM_SUCCESS);
503
504 EXPECT_EQ(descriptorTitleStrType, PLDM_STR_TYPE_ASCII);
505 EXPECT_EQ(descriptorTitleStr.length, vendorTitle.size());
506 std::string vendorTitleStr(
507 reinterpret_cast<const char*>(descriptorTitleStr.ptr),
508 descriptorTitleStr.length);
509 EXPECT_EQ(vendorTitleStr, vendorTitle);
510
511 EXPECT_EQ(vendorDefinedDescriptorData.length,
512 vendorDescriptorData.size());
513 EXPECT_EQ(true, std::equal(vendorDefinedDescriptorData.ptr,
514 vendorDefinedDescriptorData.ptr +
515 vendorDefinedDescriptorData.length,
516 vendorDescriptorData.begin(),
517 vendorDescriptorData.end()));
518 }
519
520 descriptorsRemainingLength -= sizeof(descriptorType) +
521 sizeof(descriptorLen) +
522 descriptorData.length;
523 descriptorCount++;
524 }
525}
526
527TEST(DecodeDescriptors, errorPathDecodeDescriptorTLV)
528{
529 int rc = 0;
530 // IANA Enterprise ID descriptor length incorrect
531 constexpr std::array<uint8_t, 7> invalidIANADescriptor1{
532 0x01, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c};
533 uint16_t descriptorType = 0;
534 variable_field descriptorData{};
535
536 rc = decode_descriptor_type_length_value(nullptr,
537 invalidIANADescriptor1.size(),
538 &descriptorType, &descriptorData);
539 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
540
541 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
542 invalidIANADescriptor1.size(),
543 nullptr, &descriptorData);
544 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
545
546 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
547 invalidIANADescriptor1.size(),
548 &descriptorType, nullptr);
549 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
550
551 rc = decode_descriptor_type_length_value(
552 invalidIANADescriptor1.data(), PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN - 1,
553 &descriptorType, &descriptorData);
554 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
555
556 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
557 invalidIANADescriptor1.size(),
558 &descriptorType, &descriptorData);
559 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
560
561 // IANA Enterprise ID descriptor data less than length
562 std::array<uint8_t, 7> invalidIANADescriptor2{0x01, 0x00, 0x04, 0x00,
563 0x0a, 0x0b, 0x0c};
564 rc = decode_descriptor_type_length_value(invalidIANADescriptor2.data(),
565 invalidIANADescriptor2.size(),
566 &descriptorType, &descriptorData);
567 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
568}
569
570TEST(DecodeDescriptors, errorPathVendorDefinedDescriptor)
571{
572 int rc = 0;
573 // VendorDefinedDescriptorTitleStringType is invalid
574 constexpr std::array<uint8_t, 9> invalidVendorDescriptor1{
575 0x06, 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
576 uint8_t descriptorStringType = 0;
577 variable_field descriptorTitleStr{};
578 variable_field vendorDefinedDescriptorData{};
579
580 rc = decode_vendor_defined_descriptor_value(
581 nullptr, invalidVendorDescriptor1.size(), &descriptorStringType,
582 &descriptorTitleStr, &vendorDefinedDescriptorData);
583 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
584
585 rc = decode_vendor_defined_descriptor_value(
586 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
587 &descriptorStringType, &descriptorTitleStr,
588 &vendorDefinedDescriptorData);
589 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
590
591 rc = decode_vendor_defined_descriptor_value(
592 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
593 nullptr, &descriptorTitleStr, &vendorDefinedDescriptorData);
594 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
595
596 rc = decode_vendor_defined_descriptor_value(
597 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
598 &descriptorStringType, nullptr, &vendorDefinedDescriptorData);
599 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
600
601 rc = decode_vendor_defined_descriptor_value(
602 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
603 &descriptorStringType, &descriptorTitleStr, nullptr);
604 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
605
606 rc = decode_vendor_defined_descriptor_value(
607 invalidVendorDescriptor1.data(),
608 sizeof(pldm_vendor_defined_descriptor_title_data) - 1,
609 &descriptorStringType, &descriptorTitleStr,
610 &vendorDefinedDescriptorData);
611 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
612
613 rc = decode_vendor_defined_descriptor_value(
614 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
615 &descriptorStringType, &descriptorTitleStr,
616 &vendorDefinedDescriptorData);
617 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
618
619 // VendorDefinedDescriptorTitleStringLength is 0
620 std::array<uint8_t, 9> invalidVendorDescriptor2{
621 0x01, 0x00, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
622 rc = decode_vendor_defined_descriptor_value(
623 invalidVendorDescriptor2.data(), invalidVendorDescriptor2.size(),
624 &descriptorStringType, &descriptorTitleStr,
625 &vendorDefinedDescriptorData);
626 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
627
628 // VendorDefinedDescriptorData not present in the data
629 std::array<uint8_t, 9> invalidVendorDescriptor3{
630 0x01, 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
631 rc = decode_vendor_defined_descriptor_value(
632 invalidVendorDescriptor3.data(), invalidVendorDescriptor3.size(),
633 &descriptorStringType, &descriptorTitleStr,
634 &vendorDefinedDescriptorData);
635 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
636}
637
gokulsanker138ceba2021-04-05 13:25:25 +0530638TEST(QueryDeviceIdentifiers, goodPathEncodeRequest)
639{
640 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
641 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
642
643 uint8_t instanceId = 0x01;
644
645 auto rc = encode_query_device_identifiers_req(
646 instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, requestPtr);
647 EXPECT_EQ(rc, PLDM_SUCCESS);
648 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
649 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
650 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
651 EXPECT_EQ(requestPtr->hdr.command, PLDM_QUERY_DEVICE_IDENTIFIERS);
652}
gokulsankereca3e192021-04-05 14:57:41 +0530653
654TEST(QueryDeviceIdentifiers, goodPathDecodeResponse)
655{
656 // descriptorDataLen is not fixed here taking it as 6
657 constexpr uint8_t descriptorDataLen = 6;
658 std::array<uint8_t, hdrSize +
659 sizeof(struct pldm_query_device_identifiers_resp) +
660 descriptorDataLen>
661 responseMsg{};
662 auto inResp = reinterpret_cast<struct pldm_query_device_identifiers_resp*>(
663 responseMsg.data() + hdrSize);
664
665 inResp->completion_code = PLDM_SUCCESS;
666 inResp->device_identifiers_len = htole32(descriptorDataLen);
667 inResp->descriptor_count = 1;
668
669 // filling descriptor data
670 std::fill_n(responseMsg.data() + hdrSize +
671 sizeof(struct pldm_query_device_identifiers_resp),
672 descriptorDataLen, 0xFF);
673
674 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
675 uint8_t completionCode = PLDM_SUCCESS;
676 uint32_t deviceIdentifiersLen = 0;
677 uint8_t descriptorCount = 0;
678 uint8_t* outDescriptorData = nullptr;
679
680 auto rc = decode_query_device_identifiers_resp(
681 response, responseMsg.size() - hdrSize, &completionCode,
682 &deviceIdentifiersLen, &descriptorCount, &outDescriptorData);
683
684 EXPECT_EQ(rc, PLDM_SUCCESS);
685 EXPECT_EQ(completionCode, PLDM_SUCCESS);
686 EXPECT_EQ(deviceIdentifiersLen, inResp->device_identifiers_len);
687 EXPECT_EQ(descriptorCount, inResp->descriptor_count);
688 EXPECT_EQ(true,
689 std::equal(outDescriptorData,
690 outDescriptorData + deviceIdentifiersLen,
691 responseMsg.begin() + hdrSize +
692 sizeof(struct pldm_query_device_identifiers_resp),
693 responseMsg.end()));
694}
gokulsanker981fbfb2021-04-05 15:17:25 +0530695
696TEST(GetFirmwareParameters, goodPathEncodeRequest)
697{
698 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
699 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
700 uint8_t instanceId = 0x01;
701
702 auto rc = encode_get_firmware_parameters_req(
703 instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, requestPtr);
704 EXPECT_EQ(rc, PLDM_SUCCESS);
705 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
706 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
707 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
708 EXPECT_EQ(requestPtr->hdr.command, PLDM_GET_FIRMWARE_PARAMETERS);
709}
gokulsanker22fbb342021-04-05 15:55:06 +0530710
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700711TEST(GetFirmwareParameters, decodeResponse)
gokulsanker22fbb342021-04-05 15:55:06 +0530712{
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700713 // CapabilitiesDuringUpdate of the firmware device
714 // Firmware device downgrade restrictions [Bit position 8] &
715 // Firmware Device Partial Updates [Bit position 3]
716 constexpr std::bitset<32> fdCapabilities{0x00000104};
717 constexpr uint16_t compCount = 1;
718 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
719 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
gokulsanker22fbb342021-04-05 15:55:06 +0530720
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700721 // constexpr uint16_t compClassification = 16;
722 // constexpr uint16_t compIdentifier = 300;
723 // constexpr uint8_t compClassificationIndex = 20;
724 // constexpr uint32_t activeCompComparisonStamp = 0xABCDEFAB;
725 // constexpr std::array<uint8_t, 8> activeComponentReleaseData = {
726 // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
727 // constexpr uint32_t pendingCompComparisonStamp = 0x12345678;
728 // constexpr std::array<uint8_t, 8> pendingComponentReleaseData = {
729 // 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
730 constexpr std::string_view activeCompVersion{"VersionString3"};
731 constexpr std::string_view pendingCompVersion{"VersionString4"};
732 // ComponentActivationMethods
733 // DC Power cycle [Bit position 4] & Self-Contained[Bit position 2]
734 constexpr std::bitset<16> compActivationMethod{0x12};
735 // CapabilitiesDuringUpdate of the firmware component
736 // Component downgrade capability [Bit position 2]
737 constexpr std::bitset<32> compCapabilities{0x02};
gokulsanker22fbb342021-04-05 15:55:06 +0530738
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700739 constexpr size_t compParamTableSize =
740 sizeof(pldm_component_parameter_entry) + activeCompVersion.size() +
741 pendingCompVersion.size();
gokulsanker22fbb342021-04-05 15:55:06 +0530742
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700743 constexpr std::array<uint8_t, compParamTableSize> compParamTable{
744 0x10, 0x00, 0x2c, 0x01, 0x14, 0xAB, 0xEF, 0xCD, 0xAB, 0x01, 0x0e, 0x01,
745 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
746 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00, 0x02,
747 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74,
748 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
749 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
gokulsanker22fbb342021-04-05 15:55:06 +0530750
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700751 constexpr size_t getFwParamsPayloadLen =
752 sizeof(pldm_get_firmware_parameters_resp) +
753 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size() +
754 compParamTableSize;
755
756 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
757 getFwParamsResponse{
758 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
759 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
760 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
761 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x10, 0x00,
762 0x2c, 0x01, 0x14, 0xAB, 0xEF, 0xCD, 0xAB, 0x01, 0x0e, 0x01, 0x02,
763 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
764 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00,
765 0x02, 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
766 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73,
767 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
768
769 auto responseMsg =
770 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
771 pldm_get_firmware_parameters_resp outResp{};
772 variable_field outActiveCompImageSetVersion{};
773 variable_field outPendingCompImageSetVersion{};
774 variable_field outCompParameterTable{};
775
776 auto rc = decode_get_firmware_parameters_resp(
777 responseMsg, getFwParamsPayloadLen, &outResp,
778 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
779 &outCompParameterTable);
gokulsanker22fbb342021-04-05 15:55:06 +0530780
781 EXPECT_EQ(rc, PLDM_SUCCESS);
782 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700783 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
784 EXPECT_EQ(outResp.comp_count, compCount);
785 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
786 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
787 activeCompImageSetVersion.size());
788 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
789 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
790 pendingCompImageSetVersion.size());
791 std::string activeCompImageSetVersionStr(
792 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
793 outActiveCompImageSetVersion.length);
794 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
795 std::string pendingCompImageSetVersionStr(
796 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
797 outPendingCompImageSetVersion.length);
798 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
799 EXPECT_EQ(outCompParameterTable.length, compParamTableSize);
800 EXPECT_EQ(true, std::equal(outCompParameterTable.ptr,
801 outCompParameterTable.ptr +
802 outCompParameterTable.length,
803 compParamTable.begin(), compParamTable.end()));
804}
gokulsanker22fbb342021-04-05 15:55:06 +0530805
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700806TEST(GetFirmwareParameters, decodeResponseZeroCompCount)
807{
808 // CapabilitiesDuringUpdate of the firmware device
809 // FD Host Functionality during Firmware Update [Bit position 2] &
810 // Component Update Failure Retry Capability [Bit position 1]
811 constexpr std::bitset<32> fdCapabilities{0x06};
812 constexpr uint16_t compCount = 0;
813 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
814 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
815
816 constexpr size_t getFwParamsPayloadLen =
817 sizeof(pldm_get_firmware_parameters_resp) +
818 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size();
819
820 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
821 getFwParamsResponse{
822 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
823 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
824 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
825 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32};
826
827 auto responseMsg =
828 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
829 pldm_get_firmware_parameters_resp outResp{};
830 variable_field outActiveCompImageSetVersion{};
831 variable_field outPendingCompImageSetVersion{};
832 variable_field outCompParameterTable{};
833
834 auto rc = decode_get_firmware_parameters_resp(
835 responseMsg, getFwParamsPayloadLen, &outResp,
836 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
837 &outCompParameterTable);
838
839 EXPECT_EQ(rc, PLDM_SUCCESS);
840 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
841 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
842 EXPECT_EQ(outResp.comp_count, compCount);
843 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
844 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
845 activeCompImageSetVersion.size());
846 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
847 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
848 pendingCompImageSetVersion.size());
849 std::string activeCompImageSetVersionStr(
850 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
851 outActiveCompImageSetVersion.length);
852 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
853 std::string pendingCompImageSetVersionStr(
854 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
855 outPendingCompImageSetVersion.length);
856 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
857 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
858 EXPECT_EQ(outCompParameterTable.length, 0);
859}
860
861TEST(GetFirmwareParameters,
862 decodeResponseNoPendingCompImageVersionStrZeroCompCount)
863{
864 // CapabilitiesDuringUpdate of the firmware device
865 // FD Host Functionality during Firmware Update [Bit position 2] &
866 // Component Update Failure Retry Capability [Bit position 1]
867 constexpr std::bitset<32> fdCapabilities{0x06};
868 constexpr uint16_t compCount = 0;
869 constexpr std::string_view activeCompImageSetVersion{"VersionString"};
870
871 constexpr size_t getFwParamsPayloadLen =
872 sizeof(pldm_get_firmware_parameters_resp) +
873 activeCompImageSetVersion.size();
874
875 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
876 getFwParamsResponse{0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
877 0x00, 0x00, 0x00, 0x01, 0x0d, 0x00, 0x00,
878 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
879 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67};
880
881 auto responseMsg =
882 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
883 pldm_get_firmware_parameters_resp outResp{};
884 variable_field outActiveCompImageSetVersion{};
885 variable_field outPendingCompImageSetVersion{};
886 variable_field outCompParameterTable{};
887
888 auto rc = decode_get_firmware_parameters_resp(
889 responseMsg, getFwParamsPayloadLen, &outResp,
890 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
891 &outCompParameterTable);
892
893 EXPECT_EQ(rc, PLDM_SUCCESS);
894 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
895 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
896 EXPECT_EQ(outResp.comp_count, compCount);
897 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
898 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
899 activeCompImageSetVersion.size());
900 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type,
901 PLDM_STR_TYPE_UNKNOWN);
902 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len, 0);
903 std::string activeCompImageSetVersionStr(
904 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
905 outActiveCompImageSetVersion.length);
906 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
907 EXPECT_EQ(outPendingCompImageSetVersion.ptr, nullptr);
908 EXPECT_EQ(outPendingCompImageSetVersion.length, 0);
909 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
910 EXPECT_EQ(outCompParameterTable.length, 0);
911}
912
913TEST(GetFirmwareParameters, decodeResponseErrorCompletionCode)
914{
915 constexpr std::array<uint8_t,
916 hdrSize + sizeof(pldm_get_firmware_parameters_resp)>
917 getFwParamsResponse{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
918 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
919
920 auto responseMsg =
921 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
922 pldm_get_firmware_parameters_resp outResp{};
923 variable_field outActiveCompImageSetVersion{};
924 variable_field outPendingCompImageSetVersion{};
925 variable_field outCompParameterTable{};
926
927 auto rc = decode_get_firmware_parameters_resp(
928 responseMsg, getFwParamsResponse.size(), &outResp,
929 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
930 &outCompParameterTable);
931
932 EXPECT_EQ(rc, PLDM_SUCCESS);
933 EXPECT_EQ(outResp.completion_code, PLDM_ERROR);
934}
935
936TEST(GetFirmwareParameters, errorPathdecodeResponse)
937{
938 int rc = 0;
939 // Invalid ActiveComponentImageSetVersionStringType
940 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse1{
941 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
942 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00};
943
944 auto responseMsg =
945 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse1.data());
946 pldm_get_firmware_parameters_resp outResp{};
947 variable_field outActiveCompImageSetVersion{};
948 variable_field outPendingCompImageSetVersion{};
949 variable_field outCompParameterTable{};
950
951 rc = decode_get_firmware_parameters_resp(
952 nullptr, invalidGetFwParamsResponse1.size(), &outResp,
953 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
954 &outCompParameterTable);
955 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
956
957 rc = decode_get_firmware_parameters_resp(
958 responseMsg, invalidGetFwParamsResponse1.size(), nullptr,
959 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
960 &outCompParameterTable);
961 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
962
963 rc = decode_get_firmware_parameters_resp(
964 responseMsg, invalidGetFwParamsResponse1.size(), &outResp, nullptr,
965 &outPendingCompImageSetVersion, &outCompParameterTable);
966 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
967
968 rc = decode_get_firmware_parameters_resp(
969 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
970 &outActiveCompImageSetVersion, nullptr, &outCompParameterTable);
971 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
972
973 rc = decode_get_firmware_parameters_resp(
974 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
975 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion, nullptr);
976 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
977
978 rc = decode_get_firmware_parameters_resp(
979 responseMsg, 0, &outResp, &outActiveCompImageSetVersion,
980 &outPendingCompImageSetVersion, &outCompParameterTable);
981 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
982
983 rc = decode_get_firmware_parameters_resp(
984 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
985 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
986 &outCompParameterTable);
987 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
988
989 // Invalid ActiveComponentImageSetVersionStringLength
990 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse2{
991 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
992 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
993 responseMsg =
994 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse2.data());
995 rc = decode_get_firmware_parameters_resp(
996 responseMsg, invalidGetFwParamsResponse2.size(), &outResp,
997 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
998 &outCompParameterTable);
999 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1000
1001 // Invalid PendingComponentImageSetVersionStringType &
1002 // PendingComponentImageSetVersionStringLength
1003 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse3{
1004 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1005 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00};
1006 responseMsg =
1007 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse3.data());
1008 rc = decode_get_firmware_parameters_resp(
1009 responseMsg, invalidGetFwParamsResponse3.size(), &outResp,
1010 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1011 &outCompParameterTable);
1012 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1013
1014 // Invalid PendingComponentImageSetVersionStringType &
1015 // PendingComponentImageSetVersionStringLength
1016 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse4{
1017 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1018 0x00, 0x00, 0x00, 0x01, 0x0e, 0x06, 0x0e};
1019 responseMsg =
1020 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse4.data());
1021 rc = decode_get_firmware_parameters_resp(
1022 responseMsg, invalidGetFwParamsResponse4.size(), &outResp,
1023 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1024 &outCompParameterTable);
1025 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1026
1027 // Total payload length less than expected
1028 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse5{
1029 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1030 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x0e};
1031 responseMsg =
1032 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse5.data());
1033 rc = decode_get_firmware_parameters_resp(
1034 responseMsg, invalidGetFwParamsResponse5.size(), &outResp,
1035 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1036 &outCompParameterTable);
1037 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
gokulsanker22fbb342021-04-05 15:55:06 +05301038}
gokulsankere1fb7a82021-04-05 16:09:29 +05301039
1040TEST(GetFirmwareParameters, goodPathDecodeComponentParameterEntry)
1041{
1042 // Random value for component classification
1043 constexpr uint16_t compClassification = 0x0A0B;
1044 // Random value for component classification
1045 constexpr uint16_t compIdentifier = 0x0C0D;
1046 // Random value for component classification
1047 constexpr uint32_t timestamp = 0X12345678;
1048 // Random value for component activation methods
1049 constexpr uint16_t compActivationMethods = 0xBBDD;
1050 // Random value for capabilities during update
1051 constexpr uint32_t capabilitiesDuringUpdate = 0xBADBEEFE;
1052
1053 // ActiveCompImageSetVerStrLen is not fixed here taking it as 8
1054 constexpr uint8_t activeCompVerStrLen = 8;
1055 // PendingCompImageSetVerStrLen is not fixed here taking it as 8
1056 constexpr uint8_t pendingCompVerStrLen = 8;
1057 constexpr size_t entryLength =
1058 sizeof(struct pldm_component_parameter_entry) + activeCompVerStrLen +
1059 pendingCompVerStrLen;
1060 std::array<uint8_t, entryLength> entry{};
1061
1062 auto inEntry =
1063 reinterpret_cast<struct pldm_component_parameter_entry*>(entry.data());
1064
1065 inEntry->comp_classification = htole16(compClassification);
1066 inEntry->comp_identifier = htole16(compIdentifier);
1067 inEntry->comp_classification_index = 0x0F;
1068 inEntry->active_comp_comparison_stamp = htole32(timestamp);
1069 inEntry->active_comp_ver_str_type = 1;
1070 inEntry->active_comp_ver_str_len = activeCompVerStrLen;
1071 std::fill_n(inEntry->active_comp_release_date,
1072 sizeof(inEntry->active_comp_release_date), 0xFF);
1073 inEntry->pending_comp_comparison_stamp = htole32(timestamp);
1074 inEntry->pending_comp_ver_str_type = 1;
1075 inEntry->pending_comp_ver_str_len = pendingCompVerStrLen;
1076 std::fill_n(inEntry->pending_comp_release_date,
1077 sizeof(inEntry->pending_comp_release_date), 0xFF);
1078 inEntry->comp_activation_methods.value = htole16(compActivationMethods);
1079 inEntry->capabilities_during_update.value =
1080 htole32(capabilitiesDuringUpdate);
1081 constexpr auto activeCompVerStrPos =
1082 sizeof(struct pldm_component_parameter_entry);
1083 std::fill_n(entry.data() + activeCompVerStrPos, activeCompVerStrLen, 0xAA);
1084 constexpr auto pendingCompVerStrPos =
1085 activeCompVerStrPos + activeCompVerStrLen;
1086 std::fill_n(entry.data() + pendingCompVerStrPos, pendingCompVerStrLen,
1087 0xBB);
1088
1089 struct pldm_component_parameter_entry outEntry;
1090 struct variable_field outActiveCompVerStr;
1091 struct variable_field outPendingCompVerStr;
1092
1093 auto rc = decode_get_firmware_parameters_resp_comp_entry(
1094 entry.data(), entryLength, &outEntry, &outActiveCompVerStr,
1095 &outPendingCompVerStr);
1096
1097 EXPECT_EQ(rc, PLDM_SUCCESS);
1098
1099 EXPECT_EQ(outEntry.comp_classification, compClassification);
1100 EXPECT_EQ(outEntry.comp_identifier, compIdentifier);
1101 EXPECT_EQ(inEntry->comp_classification_index,
1102 outEntry.comp_classification_index);
1103 EXPECT_EQ(outEntry.active_comp_comparison_stamp, timestamp);
1104 EXPECT_EQ(inEntry->active_comp_ver_str_type,
1105 outEntry.active_comp_ver_str_type);
1106 EXPECT_EQ(inEntry->active_comp_ver_str_len,
1107 outEntry.active_comp_ver_str_len);
1108 EXPECT_EQ(0, memcmp(inEntry->active_comp_release_date,
1109 outEntry.active_comp_release_date,
1110 sizeof(inEntry->active_comp_release_date)));
1111 EXPECT_EQ(outEntry.pending_comp_comparison_stamp, timestamp);
1112 EXPECT_EQ(inEntry->pending_comp_ver_str_type,
1113 outEntry.pending_comp_ver_str_type);
1114 EXPECT_EQ(inEntry->pending_comp_ver_str_len,
1115 outEntry.pending_comp_ver_str_len);
1116 EXPECT_EQ(0, memcmp(inEntry->pending_comp_release_date,
1117 outEntry.pending_comp_release_date,
1118 sizeof(inEntry->pending_comp_release_date)));
1119 EXPECT_EQ(outEntry.comp_activation_methods.value, compActivationMethods);
1120 EXPECT_EQ(outEntry.capabilities_during_update.value,
1121 capabilitiesDuringUpdate);
1122
1123 EXPECT_EQ(0, memcmp(outActiveCompVerStr.ptr,
1124 entry.data() + activeCompVerStrPos,
1125 outActiveCompVerStr.length));
1126 EXPECT_EQ(0, memcmp(outPendingCompVerStr.ptr,
1127 entry.data() + pendingCompVerStrPos,
1128 outPendingCompVerStr.length));
1129}