blob: ca2b1a460def7a0cf422133343672831038c263e [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
Tom Joseph58cc1722021-06-09 07:15:49 -0700638TEST(DecodeComponentImageInfo, goodPath)
639{
640 // Firmware
641 constexpr uint16_t compClassification = 16;
642 constexpr uint16_t compIdentifier = 300;
643 constexpr uint32_t compComparisonStamp = 0xFFFFFFFF;
644 // Force update
645 constexpr std::bitset<16> compOptions{1};
646 // System reboot[Bit position 3] & Medium-specific reset[Bit position 2]
647 constexpr std::bitset<16> reqCompActivationMethod{0x0c};
648 // Random ComponentLocationOffset
649 constexpr uint32_t compLocOffset = 357;
650 // Random ComponentSize
651 constexpr uint32_t compSize = 27;
652 // ComponentVersionString
653 constexpr std::string_view compVersionStr{"VersionString1"};
654 constexpr size_t compImageInfoSize =
655 sizeof(pldm_component_image_information) + compVersionStr.size();
656
657 constexpr std::array<uint8_t, compImageInfoSize> compImageInfo{
658 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
659 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
660 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
661 pldm_component_image_information outCompImageInfo{};
662 variable_field outCompVersionStr{};
663
664 auto rc =
665 decode_pldm_comp_image_info(compImageInfo.data(), compImageInfo.size(),
666 &outCompImageInfo, &outCompVersionStr);
667
668 EXPECT_EQ(rc, PLDM_SUCCESS);
669 EXPECT_EQ(outCompImageInfo.comp_classification, compClassification);
670 EXPECT_EQ(outCompImageInfo.comp_identifier, compIdentifier);
671 EXPECT_EQ(outCompImageInfo.comp_comparison_stamp, compComparisonStamp);
672 EXPECT_EQ(outCompImageInfo.comp_options.value, compOptions);
673 EXPECT_EQ(outCompImageInfo.requested_comp_activation_method.value,
674 reqCompActivationMethod);
675 EXPECT_EQ(outCompImageInfo.comp_location_offset, compLocOffset);
676 EXPECT_EQ(outCompImageInfo.comp_size, compSize);
677 EXPECT_EQ(outCompImageInfo.comp_version_string_type, PLDM_STR_TYPE_ASCII);
678 EXPECT_EQ(outCompImageInfo.comp_version_string_length,
679 compVersionStr.size());
680
681 EXPECT_EQ(outCompVersionStr.length,
682 outCompImageInfo.comp_version_string_length);
683 std::string componentVersionString(
684 reinterpret_cast<const char*>(outCompVersionStr.ptr),
685 outCompVersionStr.length);
686 EXPECT_EQ(componentVersionString, compVersionStr);
687}
688
689TEST(DecodeComponentImageInfo, errorPaths)
690{
691 int rc = 0;
692 // ComponentVersionString
693 constexpr std::string_view compVersionStr{"VersionString1"};
694 constexpr size_t compImageInfoSize =
695 sizeof(pldm_component_image_information) + compVersionStr.size();
696 // Invalid ComponentVersionStringType - 0x06
697 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo1{
698 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
699 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x56, 0x65,
700 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
701 pldm_component_image_information outCompImageInfo{};
702 variable_field outCompVersionStr{};
703
704 rc = decode_pldm_comp_image_info(nullptr, invalidCompImageInfo1.size(),
705 &outCompImageInfo, &outCompVersionStr);
706 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
707
708 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
709 invalidCompImageInfo1.size(), nullptr,
710 &outCompVersionStr);
711 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
712
713 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
714 invalidCompImageInfo1.size(),
715 &outCompImageInfo, nullptr);
716 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
717
718 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
719 sizeof(pldm_component_image_information) -
720 1,
721 &outCompImageInfo, &outCompVersionStr);
722 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
723
724 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
725 invalidCompImageInfo1.size(),
726 &outCompImageInfo, &outCompVersionStr);
727 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
728
729 // Invalid ComponentVersionStringLength - 0x00
730 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo2{
731 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
732 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x56, 0x65,
733 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
734 rc = decode_pldm_comp_image_info(invalidCompImageInfo2.data(),
735 invalidCompImageInfo2.size(),
736 &outCompImageInfo, &outCompVersionStr);
737 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
738
739 // Use Component Comparison Stamp is not set, but ComponentComparisonStamp
740 // is not 0xFFFFFFFF
741 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo3{
742 0x10, 0x00, 0x2c, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0c, 0x00,
743 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
744 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
745
746 rc = decode_pldm_comp_image_info(invalidCompImageInfo3.data(),
747 invalidCompImageInfo3.size() - 1,
748 &outCompImageInfo, &outCompVersionStr);
749 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
750
751 rc = decode_pldm_comp_image_info(invalidCompImageInfo3.data(),
752 invalidCompImageInfo3.size(),
753 &outCompImageInfo, &outCompVersionStr);
754 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
755
756 // Invalid ComponentLocationOffset - 0
757 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo4{
758 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
759 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
760 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
761 rc = decode_pldm_comp_image_info(invalidCompImageInfo4.data(),
762 invalidCompImageInfo4.size(),
763 &outCompImageInfo, &outCompVersionStr);
764 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
765
766 // Invalid ComponentSize - 0
767 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo5{
768 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
769 0x65, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
770 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
771 rc = decode_pldm_comp_image_info(invalidCompImageInfo5.data(),
772 invalidCompImageInfo5.size(),
773 &outCompImageInfo, &outCompVersionStr);
774 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
775}
776
gokulsanker138ceba2021-04-05 13:25:25 +0530777TEST(QueryDeviceIdentifiers, goodPathEncodeRequest)
778{
779 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
780 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
781
782 uint8_t instanceId = 0x01;
783
784 auto rc = encode_query_device_identifiers_req(
785 instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, requestPtr);
786 EXPECT_EQ(rc, PLDM_SUCCESS);
787 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
788 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
789 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
790 EXPECT_EQ(requestPtr->hdr.command, PLDM_QUERY_DEVICE_IDENTIFIERS);
791}
gokulsankereca3e192021-04-05 14:57:41 +0530792
793TEST(QueryDeviceIdentifiers, goodPathDecodeResponse)
794{
795 // descriptorDataLen is not fixed here taking it as 6
796 constexpr uint8_t descriptorDataLen = 6;
797 std::array<uint8_t, hdrSize +
798 sizeof(struct pldm_query_device_identifiers_resp) +
799 descriptorDataLen>
800 responseMsg{};
801 auto inResp = reinterpret_cast<struct pldm_query_device_identifiers_resp*>(
802 responseMsg.data() + hdrSize);
803
804 inResp->completion_code = PLDM_SUCCESS;
805 inResp->device_identifiers_len = htole32(descriptorDataLen);
806 inResp->descriptor_count = 1;
807
808 // filling descriptor data
809 std::fill_n(responseMsg.data() + hdrSize +
810 sizeof(struct pldm_query_device_identifiers_resp),
811 descriptorDataLen, 0xFF);
812
813 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
814 uint8_t completionCode = PLDM_SUCCESS;
815 uint32_t deviceIdentifiersLen = 0;
816 uint8_t descriptorCount = 0;
817 uint8_t* outDescriptorData = nullptr;
818
819 auto rc = decode_query_device_identifiers_resp(
820 response, responseMsg.size() - hdrSize, &completionCode,
821 &deviceIdentifiersLen, &descriptorCount, &outDescriptorData);
822
823 EXPECT_EQ(rc, PLDM_SUCCESS);
824 EXPECT_EQ(completionCode, PLDM_SUCCESS);
825 EXPECT_EQ(deviceIdentifiersLen, inResp->device_identifiers_len);
826 EXPECT_EQ(descriptorCount, inResp->descriptor_count);
827 EXPECT_EQ(true,
828 std::equal(outDescriptorData,
829 outDescriptorData + deviceIdentifiersLen,
830 responseMsg.begin() + hdrSize +
831 sizeof(struct pldm_query_device_identifiers_resp),
832 responseMsg.end()));
833}
gokulsanker981fbfb2021-04-05 15:17:25 +0530834
835TEST(GetFirmwareParameters, goodPathEncodeRequest)
836{
837 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
838 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
839 uint8_t instanceId = 0x01;
840
841 auto rc = encode_get_firmware_parameters_req(
842 instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, requestPtr);
843 EXPECT_EQ(rc, PLDM_SUCCESS);
844 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
845 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
846 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
847 EXPECT_EQ(requestPtr->hdr.command, PLDM_GET_FIRMWARE_PARAMETERS);
848}
gokulsanker22fbb342021-04-05 15:55:06 +0530849
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700850TEST(GetFirmwareParameters, decodeResponse)
gokulsanker22fbb342021-04-05 15:55:06 +0530851{
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700852 // CapabilitiesDuringUpdate of the firmware device
853 // Firmware device downgrade restrictions [Bit position 8] &
854 // Firmware Device Partial Updates [Bit position 3]
855 constexpr std::bitset<32> fdCapabilities{0x00000104};
856 constexpr uint16_t compCount = 1;
857 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
858 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
gokulsanker22fbb342021-04-05 15:55:06 +0530859
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700860 // constexpr uint16_t compClassification = 16;
861 // constexpr uint16_t compIdentifier = 300;
862 // constexpr uint8_t compClassificationIndex = 20;
863 // constexpr uint32_t activeCompComparisonStamp = 0xABCDEFAB;
864 // constexpr std::array<uint8_t, 8> activeComponentReleaseData = {
865 // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
866 // constexpr uint32_t pendingCompComparisonStamp = 0x12345678;
867 // constexpr std::array<uint8_t, 8> pendingComponentReleaseData = {
868 // 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
869 constexpr std::string_view activeCompVersion{"VersionString3"};
870 constexpr std::string_view pendingCompVersion{"VersionString4"};
871 // ComponentActivationMethods
872 // DC Power cycle [Bit position 4] & Self-Contained[Bit position 2]
873 constexpr std::bitset<16> compActivationMethod{0x12};
874 // CapabilitiesDuringUpdate of the firmware component
875 // Component downgrade capability [Bit position 2]
876 constexpr std::bitset<32> compCapabilities{0x02};
gokulsanker22fbb342021-04-05 15:55:06 +0530877
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700878 constexpr size_t compParamTableSize =
879 sizeof(pldm_component_parameter_entry) + activeCompVersion.size() +
880 pendingCompVersion.size();
gokulsanker22fbb342021-04-05 15:55:06 +0530881
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700882 constexpr std::array<uint8_t, compParamTableSize> compParamTable{
883 0x10, 0x00, 0x2c, 0x01, 0x14, 0xAB, 0xEF, 0xCD, 0xAB, 0x01, 0x0e, 0x01,
884 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
885 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00, 0x02,
886 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74,
887 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
888 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
gokulsanker22fbb342021-04-05 15:55:06 +0530889
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700890 constexpr size_t getFwParamsPayloadLen =
891 sizeof(pldm_get_firmware_parameters_resp) +
892 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size() +
893 compParamTableSize;
894
895 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
896 getFwParamsResponse{
897 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
898 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
899 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
900 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x10, 0x00,
901 0x2c, 0x01, 0x14, 0xAB, 0xEF, 0xCD, 0xAB, 0x01, 0x0e, 0x01, 0x02,
902 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
903 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00,
904 0x02, 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
905 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73,
906 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
907
908 auto responseMsg =
909 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
910 pldm_get_firmware_parameters_resp outResp{};
911 variable_field outActiveCompImageSetVersion{};
912 variable_field outPendingCompImageSetVersion{};
913 variable_field outCompParameterTable{};
914
915 auto rc = decode_get_firmware_parameters_resp(
916 responseMsg, getFwParamsPayloadLen, &outResp,
917 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
918 &outCompParameterTable);
gokulsanker22fbb342021-04-05 15:55:06 +0530919
920 EXPECT_EQ(rc, PLDM_SUCCESS);
921 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700922 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
923 EXPECT_EQ(outResp.comp_count, compCount);
924 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
925 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
926 activeCompImageSetVersion.size());
927 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
928 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
929 pendingCompImageSetVersion.size());
930 std::string activeCompImageSetVersionStr(
931 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
932 outActiveCompImageSetVersion.length);
933 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
934 std::string pendingCompImageSetVersionStr(
935 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
936 outPendingCompImageSetVersion.length);
937 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
938 EXPECT_EQ(outCompParameterTable.length, compParamTableSize);
939 EXPECT_EQ(true, std::equal(outCompParameterTable.ptr,
940 outCompParameterTable.ptr +
941 outCompParameterTable.length,
942 compParamTable.begin(), compParamTable.end()));
943}
gokulsanker22fbb342021-04-05 15:55:06 +0530944
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700945TEST(GetFirmwareParameters, decodeResponseZeroCompCount)
946{
947 // CapabilitiesDuringUpdate of the firmware device
948 // FD Host Functionality during Firmware Update [Bit position 2] &
949 // Component Update Failure Retry Capability [Bit position 1]
950 constexpr std::bitset<32> fdCapabilities{0x06};
951 constexpr uint16_t compCount = 0;
952 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
953 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
954
955 constexpr size_t getFwParamsPayloadLen =
956 sizeof(pldm_get_firmware_parameters_resp) +
957 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size();
958
959 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
960 getFwParamsResponse{
961 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
962 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
963 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
964 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32};
965
966 auto responseMsg =
967 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
968 pldm_get_firmware_parameters_resp outResp{};
969 variable_field outActiveCompImageSetVersion{};
970 variable_field outPendingCompImageSetVersion{};
971 variable_field outCompParameterTable{};
972
973 auto rc = decode_get_firmware_parameters_resp(
974 responseMsg, getFwParamsPayloadLen, &outResp,
975 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
976 &outCompParameterTable);
977
978 EXPECT_EQ(rc, PLDM_SUCCESS);
979 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
980 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
981 EXPECT_EQ(outResp.comp_count, compCount);
982 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
983 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
984 activeCompImageSetVersion.size());
985 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
986 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
987 pendingCompImageSetVersion.size());
988 std::string activeCompImageSetVersionStr(
989 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
990 outActiveCompImageSetVersion.length);
991 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
992 std::string pendingCompImageSetVersionStr(
993 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
994 outPendingCompImageSetVersion.length);
995 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
996 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
997 EXPECT_EQ(outCompParameterTable.length, 0);
998}
999
1000TEST(GetFirmwareParameters,
1001 decodeResponseNoPendingCompImageVersionStrZeroCompCount)
1002{
1003 // CapabilitiesDuringUpdate of the firmware device
1004 // FD Host Functionality during Firmware Update [Bit position 2] &
1005 // Component Update Failure Retry Capability [Bit position 1]
1006 constexpr std::bitset<32> fdCapabilities{0x06};
1007 constexpr uint16_t compCount = 0;
1008 constexpr std::string_view activeCompImageSetVersion{"VersionString"};
1009
1010 constexpr size_t getFwParamsPayloadLen =
1011 sizeof(pldm_get_firmware_parameters_resp) +
1012 activeCompImageSetVersion.size();
1013
1014 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
1015 getFwParamsResponse{0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1016 0x00, 0x00, 0x00, 0x01, 0x0d, 0x00, 0x00,
1017 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
1018 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67};
1019
1020 auto responseMsg =
1021 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
1022 pldm_get_firmware_parameters_resp outResp{};
1023 variable_field outActiveCompImageSetVersion{};
1024 variable_field outPendingCompImageSetVersion{};
1025 variable_field outCompParameterTable{};
1026
1027 auto rc = decode_get_firmware_parameters_resp(
1028 responseMsg, getFwParamsPayloadLen, &outResp,
1029 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1030 &outCompParameterTable);
1031
1032 EXPECT_EQ(rc, PLDM_SUCCESS);
1033 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
1034 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
1035 EXPECT_EQ(outResp.comp_count, compCount);
1036 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
1037 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
1038 activeCompImageSetVersion.size());
1039 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type,
1040 PLDM_STR_TYPE_UNKNOWN);
1041 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len, 0);
1042 std::string activeCompImageSetVersionStr(
1043 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
1044 outActiveCompImageSetVersion.length);
1045 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
1046 EXPECT_EQ(outPendingCompImageSetVersion.ptr, nullptr);
1047 EXPECT_EQ(outPendingCompImageSetVersion.length, 0);
1048 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
1049 EXPECT_EQ(outCompParameterTable.length, 0);
1050}
1051
1052TEST(GetFirmwareParameters, decodeResponseErrorCompletionCode)
1053{
1054 constexpr std::array<uint8_t,
1055 hdrSize + sizeof(pldm_get_firmware_parameters_resp)>
1056 getFwParamsResponse{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1057 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1058
1059 auto responseMsg =
1060 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
1061 pldm_get_firmware_parameters_resp outResp{};
1062 variable_field outActiveCompImageSetVersion{};
1063 variable_field outPendingCompImageSetVersion{};
1064 variable_field outCompParameterTable{};
1065
1066 auto rc = decode_get_firmware_parameters_resp(
1067 responseMsg, getFwParamsResponse.size(), &outResp,
1068 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1069 &outCompParameterTable);
1070
1071 EXPECT_EQ(rc, PLDM_SUCCESS);
1072 EXPECT_EQ(outResp.completion_code, PLDM_ERROR);
1073}
1074
1075TEST(GetFirmwareParameters, errorPathdecodeResponse)
1076{
1077 int rc = 0;
1078 // Invalid ActiveComponentImageSetVersionStringType
1079 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse1{
1080 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1081 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00};
1082
1083 auto responseMsg =
1084 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse1.data());
1085 pldm_get_firmware_parameters_resp outResp{};
1086 variable_field outActiveCompImageSetVersion{};
1087 variable_field outPendingCompImageSetVersion{};
1088 variable_field outCompParameterTable{};
1089
1090 rc = decode_get_firmware_parameters_resp(
1091 nullptr, invalidGetFwParamsResponse1.size(), &outResp,
1092 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1093 &outCompParameterTable);
1094 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1095
1096 rc = decode_get_firmware_parameters_resp(
1097 responseMsg, invalidGetFwParamsResponse1.size(), nullptr,
1098 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1099 &outCompParameterTable);
1100 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1101
1102 rc = decode_get_firmware_parameters_resp(
1103 responseMsg, invalidGetFwParamsResponse1.size(), &outResp, nullptr,
1104 &outPendingCompImageSetVersion, &outCompParameterTable);
1105 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1106
1107 rc = decode_get_firmware_parameters_resp(
1108 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
1109 &outActiveCompImageSetVersion, nullptr, &outCompParameterTable);
1110 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1111
1112 rc = decode_get_firmware_parameters_resp(
1113 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
1114 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion, nullptr);
1115 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1116
1117 rc = decode_get_firmware_parameters_resp(
1118 responseMsg, 0, &outResp, &outActiveCompImageSetVersion,
1119 &outPendingCompImageSetVersion, &outCompParameterTable);
1120 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1121
1122 rc = decode_get_firmware_parameters_resp(
1123 responseMsg, invalidGetFwParamsResponse1.size(), &outResp,
1124 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1125 &outCompParameterTable);
1126 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1127
1128 // Invalid ActiveComponentImageSetVersionStringLength
1129 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse2{
1130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1131 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
1132 responseMsg =
1133 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse2.data());
1134 rc = decode_get_firmware_parameters_resp(
1135 responseMsg, invalidGetFwParamsResponse2.size(), &outResp,
1136 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1137 &outCompParameterTable);
1138 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1139
1140 // Invalid PendingComponentImageSetVersionStringType &
1141 // PendingComponentImageSetVersionStringLength
1142 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse3{
1143 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1144 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00};
1145 responseMsg =
1146 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse3.data());
1147 rc = decode_get_firmware_parameters_resp(
1148 responseMsg, invalidGetFwParamsResponse3.size(), &outResp,
1149 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1150 &outCompParameterTable);
1151 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1152
1153 // Invalid PendingComponentImageSetVersionStringType &
1154 // PendingComponentImageSetVersionStringLength
1155 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse4{
1156 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1157 0x00, 0x00, 0x00, 0x01, 0x0e, 0x06, 0x0e};
1158 responseMsg =
1159 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse4.data());
1160 rc = decode_get_firmware_parameters_resp(
1161 responseMsg, invalidGetFwParamsResponse4.size(), &outResp,
1162 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1163 &outCompParameterTable);
1164 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1165
1166 // Total payload length less than expected
1167 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse5{
1168 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1169 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x0e};
1170 responseMsg =
1171 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse5.data());
1172 rc = decode_get_firmware_parameters_resp(
1173 responseMsg, invalidGetFwParamsResponse5.size(), &outResp,
1174 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1175 &outCompParameterTable);
1176 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
gokulsanker22fbb342021-04-05 15:55:06 +05301177}
gokulsankere1fb7a82021-04-05 16:09:29 +05301178
1179TEST(GetFirmwareParameters, goodPathDecodeComponentParameterEntry)
1180{
1181 // Random value for component classification
1182 constexpr uint16_t compClassification = 0x0A0B;
1183 // Random value for component classification
1184 constexpr uint16_t compIdentifier = 0x0C0D;
1185 // Random value for component classification
1186 constexpr uint32_t timestamp = 0X12345678;
1187 // Random value for component activation methods
1188 constexpr uint16_t compActivationMethods = 0xBBDD;
1189 // Random value for capabilities during update
1190 constexpr uint32_t capabilitiesDuringUpdate = 0xBADBEEFE;
1191
1192 // ActiveCompImageSetVerStrLen is not fixed here taking it as 8
1193 constexpr uint8_t activeCompVerStrLen = 8;
1194 // PendingCompImageSetVerStrLen is not fixed here taking it as 8
1195 constexpr uint8_t pendingCompVerStrLen = 8;
1196 constexpr size_t entryLength =
1197 sizeof(struct pldm_component_parameter_entry) + activeCompVerStrLen +
1198 pendingCompVerStrLen;
1199 std::array<uint8_t, entryLength> entry{};
1200
1201 auto inEntry =
1202 reinterpret_cast<struct pldm_component_parameter_entry*>(entry.data());
1203
1204 inEntry->comp_classification = htole16(compClassification);
1205 inEntry->comp_identifier = htole16(compIdentifier);
1206 inEntry->comp_classification_index = 0x0F;
1207 inEntry->active_comp_comparison_stamp = htole32(timestamp);
1208 inEntry->active_comp_ver_str_type = 1;
1209 inEntry->active_comp_ver_str_len = activeCompVerStrLen;
1210 std::fill_n(inEntry->active_comp_release_date,
1211 sizeof(inEntry->active_comp_release_date), 0xFF);
1212 inEntry->pending_comp_comparison_stamp = htole32(timestamp);
1213 inEntry->pending_comp_ver_str_type = 1;
1214 inEntry->pending_comp_ver_str_len = pendingCompVerStrLen;
1215 std::fill_n(inEntry->pending_comp_release_date,
1216 sizeof(inEntry->pending_comp_release_date), 0xFF);
1217 inEntry->comp_activation_methods.value = htole16(compActivationMethods);
1218 inEntry->capabilities_during_update.value =
1219 htole32(capabilitiesDuringUpdate);
1220 constexpr auto activeCompVerStrPos =
1221 sizeof(struct pldm_component_parameter_entry);
1222 std::fill_n(entry.data() + activeCompVerStrPos, activeCompVerStrLen, 0xAA);
1223 constexpr auto pendingCompVerStrPos =
1224 activeCompVerStrPos + activeCompVerStrLen;
1225 std::fill_n(entry.data() + pendingCompVerStrPos, pendingCompVerStrLen,
1226 0xBB);
1227
1228 struct pldm_component_parameter_entry outEntry;
1229 struct variable_field outActiveCompVerStr;
1230 struct variable_field outPendingCompVerStr;
1231
1232 auto rc = decode_get_firmware_parameters_resp_comp_entry(
1233 entry.data(), entryLength, &outEntry, &outActiveCompVerStr,
1234 &outPendingCompVerStr);
1235
1236 EXPECT_EQ(rc, PLDM_SUCCESS);
1237
1238 EXPECT_EQ(outEntry.comp_classification, compClassification);
1239 EXPECT_EQ(outEntry.comp_identifier, compIdentifier);
1240 EXPECT_EQ(inEntry->comp_classification_index,
1241 outEntry.comp_classification_index);
1242 EXPECT_EQ(outEntry.active_comp_comparison_stamp, timestamp);
1243 EXPECT_EQ(inEntry->active_comp_ver_str_type,
1244 outEntry.active_comp_ver_str_type);
1245 EXPECT_EQ(inEntry->active_comp_ver_str_len,
1246 outEntry.active_comp_ver_str_len);
1247 EXPECT_EQ(0, memcmp(inEntry->active_comp_release_date,
1248 outEntry.active_comp_release_date,
1249 sizeof(inEntry->active_comp_release_date)));
1250 EXPECT_EQ(outEntry.pending_comp_comparison_stamp, timestamp);
1251 EXPECT_EQ(inEntry->pending_comp_ver_str_type,
1252 outEntry.pending_comp_ver_str_type);
1253 EXPECT_EQ(inEntry->pending_comp_ver_str_len,
1254 outEntry.pending_comp_ver_str_len);
1255 EXPECT_EQ(0, memcmp(inEntry->pending_comp_release_date,
1256 outEntry.pending_comp_release_date,
1257 sizeof(inEntry->pending_comp_release_date)));
1258 EXPECT_EQ(outEntry.comp_activation_methods.value, compActivationMethods);
1259 EXPECT_EQ(outEntry.capabilities_during_update.value,
1260 capabilitiesDuringUpdate);
1261
1262 EXPECT_EQ(0, memcmp(outActiveCompVerStr.ptr,
1263 entry.data() + activeCompVerStrPos,
1264 outActiveCompVerStr.length));
1265 EXPECT_EQ(0, memcmp(outPendingCompVerStr.ptr,
1266 entry.data() + pendingCompVerStrPos,
1267 outPendingCompVerStr.length));
1268}
gokulsankerd434edc2021-04-05 16:36:04 +05301269
1270TEST(RequestUpdate, goodPathEncodeRequest)
1271{
1272 constexpr uint8_t instanceId = 1;
1273 constexpr uint32_t maxTransferSize = 512;
1274 constexpr uint16_t numOfComp = 3;
1275 constexpr uint8_t maxOutstandingTransferReq = 2;
1276 constexpr uint16_t pkgDataLen = 0x1234;
1277 constexpr std::string_view compImgSetVerStr = "0penBmcv1.0";
1278 constexpr uint8_t compImgSetVerStrLen =
1279 static_cast<uint8_t>(compImgSetVerStr.size());
1280 variable_field compImgSetVerStrInfo{};
1281 compImgSetVerStrInfo.ptr =
1282 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1283 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1284
1285 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1286 compImgSetVerStrLen>
1287 request{};
1288 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1289
1290 auto rc = encode_request_update_req(
1291 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1292 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1293 &compImgSetVerStrInfo, requestMsg,
1294 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1295 EXPECT_EQ(rc, PLDM_SUCCESS);
1296
1297 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1298 compImgSetVerStrLen>
1299 outRequest{0x81, 0x05, 0x10, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00,
1300 0x02, 0x34, 0x12, 0x01, 0x0b, 0x30, 0x70, 0x65, 0x6e,
1301 0x42, 0x6d, 0x63, 0x76, 0x31, 0x2e, 0x30};
1302 EXPECT_EQ(request, outRequest);
1303}
1304
1305TEST(RequestUpdate, errorPathEncodeRequest)
1306{
1307 constexpr uint8_t instanceId = 1;
1308 uint32_t maxTransferSize = 512;
1309 constexpr uint16_t numOfComp = 3;
1310 uint8_t maxOutstandingTransferReq = 2;
1311 constexpr uint16_t pkgDataLen = 0x1234;
1312 constexpr std::string_view compImgSetVerStr = "0penBmcv1.0";
1313 uint8_t compImgSetVerStrLen = static_cast<uint8_t>(compImgSetVerStr.size());
1314 variable_field compImgSetVerStrInfo{};
1315 compImgSetVerStrInfo.ptr =
1316 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1317 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1318
1319 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1320 compImgSetVerStr.size()>
1321 request{};
1322 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1323
1324 auto rc = encode_request_update_req(
1325 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1326 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen, nullptr,
1327 requestMsg,
1328 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1329 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1330
1331 compImgSetVerStrInfo.ptr = nullptr;
1332 rc = encode_request_update_req(
1333 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1334 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1335 &compImgSetVerStrInfo, requestMsg,
1336 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1337 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1338 compImgSetVerStrInfo.ptr =
1339 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1340
1341 rc = encode_request_update_req(
1342 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1343 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1344 &compImgSetVerStrInfo, nullptr,
1345 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1346 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1347
1348 rc = encode_request_update_req(instanceId, maxTransferSize, numOfComp,
1349 maxOutstandingTransferReq, pkgDataLen,
1350 PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1351 &compImgSetVerStrInfo, requestMsg, 0);
1352 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1353
1354 compImgSetVerStrLen = 0;
1355 rc = encode_request_update_req(
1356 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1357 pkgDataLen, PLDM_STR_TYPE_ASCII, 0, &compImgSetVerStrInfo, nullptr,
1358 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1359 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1360 compImgSetVerStrLen = static_cast<uint8_t>(compImgSetVerStr.size());
1361
1362 compImgSetVerStrInfo.length = 0xFFFF;
1363 rc = encode_request_update_req(
1364 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1365 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1366 &compImgSetVerStrInfo, nullptr,
1367 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1368 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1369 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1370
1371 maxTransferSize = PLDM_FWUP_BASELINE_TRANSFER_SIZE - 1;
1372 rc = encode_request_update_req(
1373 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1374 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1375 &compImgSetVerStrInfo, nullptr,
1376 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1377 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1378 maxTransferSize = PLDM_FWUP_BASELINE_TRANSFER_SIZE;
1379
1380 maxOutstandingTransferReq = PLDM_FWUP_MIN_OUTSTANDING_REQ - 1;
1381 rc = encode_request_update_req(
1382 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1383 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1384 &compImgSetVerStrInfo, nullptr,
1385 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1386 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1387 maxOutstandingTransferReq = PLDM_FWUP_MIN_OUTSTANDING_REQ;
1388
1389 rc = encode_request_update_req(
1390 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1391 pkgDataLen, PLDM_STR_TYPE_UNKNOWN, compImgSetVerStrLen,
1392 &compImgSetVerStrInfo, nullptr,
1393 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1394 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1395}
gokulsanker611238c2021-04-05 16:50:44 +05301396
1397TEST(RequestUpdate, goodPathDecodeResponse)
1398{
1399 constexpr uint16_t fdMetaDataLen = 1024;
1400 constexpr uint8_t fdWillSendPkgData = 1;
1401 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_request_update_resp)>
1402 requestUpdateResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01};
1403
1404 auto responseMsg1 =
1405 reinterpret_cast<const pldm_msg*>(requestUpdateResponse1.data());
1406 uint8_t outCompletionCode = 0;
1407 uint16_t outFdMetaDataLen = 0;
1408 uint8_t outFdWillSendPkgData = 0;
1409
1410 auto rc = decode_request_update_resp(
1411 responseMsg1, requestUpdateResponse1.size() - hdrSize,
1412 &outCompletionCode, &outFdMetaDataLen, &outFdWillSendPkgData);
1413 EXPECT_EQ(rc, PLDM_SUCCESS);
1414 EXPECT_EQ(outCompletionCode, PLDM_SUCCESS);
1415 EXPECT_EQ(outFdMetaDataLen, fdMetaDataLen);
1416 EXPECT_EQ(outFdWillSendPkgData, fdWillSendPkgData);
1417
1418 outCompletionCode = 0;
1419 outFdMetaDataLen = 0;
1420 outFdWillSendPkgData = 0;
1421
1422 constexpr std::array<uint8_t, hdrSize + sizeof(outCompletionCode)>
1423 requestUpdateResponse2{0x00, 0x00, 0x00, 0x81};
1424 auto responseMsg2 =
1425 reinterpret_cast<const pldm_msg*>(requestUpdateResponse2.data());
1426 rc = decode_request_update_resp(
1427 responseMsg2, requestUpdateResponse2.size() - hdrSize,
1428 &outCompletionCode, &outFdMetaDataLen, &outFdWillSendPkgData);
1429 EXPECT_EQ(rc, PLDM_SUCCESS);
1430 EXPECT_EQ(outCompletionCode, PLDM_FWUP_ALREADY_IN_UPDATE_MODE);
1431}
1432
1433TEST(RequestUpdate, errorPathDecodeResponse)
1434{
1435 constexpr std::array<uint8_t,
1436 hdrSize + sizeof(pldm_request_update_resp) - 1>
1437 requestUpdateResponse{0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
1438
1439 auto responseMsg =
1440 reinterpret_cast<const pldm_msg*>(requestUpdateResponse.data());
1441 uint8_t outCompletionCode = 0;
1442 uint16_t outFdMetaDataLen = 0;
1443 uint8_t outFdWillSendPkgData = 0;
1444
1445 auto rc = decode_request_update_resp(
1446 nullptr, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1447 &outFdMetaDataLen, &outFdWillSendPkgData);
1448 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1449
1450 rc = decode_request_update_resp(
1451 responseMsg, requestUpdateResponse.size() - hdrSize, nullptr,
1452 &outFdMetaDataLen, &outFdWillSendPkgData);
1453 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1454
1455 rc = decode_request_update_resp(
1456 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1457 nullptr, &outFdWillSendPkgData);
1458 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1459
1460 rc = decode_request_update_resp(
1461 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1462 &outFdMetaDataLen, nullptr);
1463 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1464
1465 rc = decode_request_update_resp(responseMsg, 0, &outCompletionCode,
1466 &outFdMetaDataLen, &outFdWillSendPkgData);
1467 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1468
1469 rc = decode_request_update_resp(
1470 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1471 &outFdMetaDataLen, &outFdWillSendPkgData);
1472 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1473}