blob: 52e867a0086ced5439b58bbd6341fa430906b6a5 [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10302#include <libpldm/base.h>
3#include <libpldm/firmware_update.h>
4#include <libpldm/pldm_types.h>
5#include <libpldm/utils.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306
7#include <algorithm>
8#include <array>
Andrew Jeffery9c766792022-08-10 23:12:49 +09309#include <bitset>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053010#include <cstdint>
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053012#include <string>
13#include <string_view>
14#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +093015
Andrew Jeffery9c766792022-08-10 23:12:49 +093016#include <gtest/gtest.h>
17
18constexpr auto hdrSize = sizeof(pldm_msg_hdr);
19
20TEST(DecodePackageHeaderInfo, goodPath)
21{
22 // Package header identifier for Version 1.0.x
23 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
24 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -060025 0x98, 0x00, 0xa0, 0x2f, 0x05, 0x9a, 0xca, 0x02};
Andrew Jeffery9c766792022-08-10 23:12:49 +093026 // Package header version for DSP0267 version 1.0.x
27 constexpr uint8_t pkgHeaderFormatRevision = 0x01;
28 // Random PackageHeaderSize
29 constexpr uint16_t pkgHeaderSize = 303;
30 // PackageReleaseDateTime - "25/12/2021 00:00:00"
31 std::array<uint8_t, PLDM_TIMESTAMP104_SIZE> package_release_date_time{
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x00, 0x19, 0x0c, 0xe5, 0x07, 0x00};
34 constexpr uint16_t componentBitmapBitLength = 8;
35 // PackageVersionString
36 constexpr std::string_view packageVersionStr{"OpenBMCv1.0"};
37 constexpr size_t packagerHeaderSize =
38 sizeof(pldm_package_header_information) + packageVersionStr.size();
39
40 constexpr std::array<uint8_t, packagerHeaderSize> packagerHeaderInfo{
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -060041 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00, 0xa0, 0x2f,
Andrew Jeffery9c766792022-08-10 23:12:49 +093042 0x05, 0x9a, 0xca, 0x02, 0x01, 0x2f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5, 0x07, 0x00, 0x08, 0x00, 0x01, 0x0b,
44 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
45 pldm_package_header_information pkgHeader{};
46 variable_field packageVersion{};
47
48 auto rc = decode_pldm_package_header_info(packagerHeaderInfo.data(),
49 packagerHeaderInfo.size(),
50 &pkgHeader, &packageVersion);
51
52 EXPECT_EQ(rc, PLDM_SUCCESS);
53 EXPECT_EQ(true,
54 std::equal(pkgHeader.uuid, pkgHeader.uuid + PLDM_FWUP_UUID_LENGTH,
55 uuid.begin(), uuid.end()));
56 EXPECT_EQ(pkgHeader.package_header_format_version, pkgHeaderFormatRevision);
57 EXPECT_EQ(pkgHeader.package_header_size, pkgHeaderSize);
58 EXPECT_EQ(true, std::equal(pkgHeader.package_release_date_time,
59 pkgHeader.package_release_date_time +
60 PLDM_TIMESTAMP104_SIZE,
61 package_release_date_time.begin(),
62 package_release_date_time.end()));
63 EXPECT_EQ(pkgHeader.component_bitmap_bit_length, componentBitmapBitLength);
64 EXPECT_EQ(pkgHeader.package_version_string_type, PLDM_STR_TYPE_ASCII);
65 EXPECT_EQ(pkgHeader.package_version_string_length,
66 packageVersionStr.size());
67 std::string packageVersionString(
68 reinterpret_cast<const char*>(packageVersion.ptr),
69 packageVersion.length);
70 EXPECT_EQ(packageVersionString, packageVersionStr);
71}
72
73TEST(DecodePackageHeaderInfo, errorPaths)
74{
75 int rc = 0;
76 constexpr std::string_view packageVersionStr{"OpenBMCv1.0"};
77 constexpr size_t packagerHeaderSize =
78 sizeof(pldm_package_header_information) + packageVersionStr.size();
79
80 // Invalid Package Version String Type - 0x06
81 constexpr std::array<uint8_t, packagerHeaderSize>
82 invalidPackagerHeaderInfo1{
83 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -060084 0xa0, 0x2f, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
Andrew Jeffery9c766792022-08-10 23:12:49 +093085 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
86 0x07, 0x00, 0x08, 0x00, 0x06, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
87 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
88
89 pldm_package_header_information packageHeader{};
90 variable_field packageVersion{};
91
92 rc = decode_pldm_package_header_info(nullptr,
93 invalidPackagerHeaderInfo1.size(),
94 &packageHeader, &packageVersion);
95 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
96
97 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
98 invalidPackagerHeaderInfo1.size(),
99 nullptr, &packageVersion);
100 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
101
102 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
103 invalidPackagerHeaderInfo1.size(),
104 &packageHeader, nullptr);
105 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
106
107 rc = decode_pldm_package_header_info(
108 invalidPackagerHeaderInfo1.data(),
109 sizeof(pldm_package_header_information) - 1, &packageHeader,
110 &packageVersion);
111 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
112
113 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo1.data(),
114 invalidPackagerHeaderInfo1.size(),
115 &packageHeader, &packageVersion);
116 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
117
118 // Invalid Package Version String Length - 0x00
119 constexpr std::array<uint8_t, packagerHeaderSize>
120 invalidPackagerHeaderInfo2{
121 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600122 0xa0, 0x2f, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
124 0x07, 0x00, 0x08, 0x00, 0x01, 0x00, 0x4f, 0x70, 0x65, 0x6e,
125 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
126 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo2.data(),
127 invalidPackagerHeaderInfo2.size(),
128 &packageHeader, &packageVersion);
129 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
130
131 // Package version string length less than in the header information
132 constexpr std::array<uint8_t, packagerHeaderSize - 1>
133 invalidPackagerHeaderInfo3{
134 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600135 0xa0, 0x2f, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
137 0x07, 0x00, 0x08, 0x00, 0x01, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
138 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e};
139 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo3.data(),
140 invalidPackagerHeaderInfo3.size(),
141 &packageHeader, &packageVersion);
142 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
143
144 // ComponentBitmapBitLength not a multiple of 8
145 constexpr std::array<uint8_t, packagerHeaderSize>
146 invalidPackagerHeaderInfo4{
147 0xf0, 0x18, 0x87, 0x8c, 0xcb, 0x7d, 0x49, 0x43, 0x98, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600148 0xa0, 0x2f, 0x05, 0x9a, 0xca, 0x02, 0x02, 0x2f, 0x01, 0x00,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0c, 0xe5,
150 0x07, 0x00, 0x09, 0x00, 0x01, 0x0b, 0x4f, 0x70, 0x65, 0x6e,
151 0x42, 0x4d, 0x43, 0x76, 0x31, 0x2e, 0x30};
152 rc = decode_pldm_package_header_info(invalidPackagerHeaderInfo4.data(),
153 invalidPackagerHeaderInfo4.size(),
154 &packageHeader, &packageVersion);
155 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
156}
157
158TEST(DecodeFirmwareDeviceIdRecord, goodPath)
159{
160 constexpr uint8_t descriptorCount = 1;
161 // Continue component updates after failure
162 constexpr std::bitset<32> deviceUpdateFlag{1};
163 constexpr uint16_t componentBitmapBitLength = 16;
164 // Applicable Components - 1,2,5,8,9
165 std::vector<std::bitset<8>> applicableComponentsBitfield{0x93, 0x01};
166 // ComponentImageSetVersionString
167 constexpr std::string_view imageSetVersionStr{"VersionString1"};
168 // Initial descriptor - UUID
169 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
170 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
171 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
172 constexpr uint16_t fwDevicePkgDataLen = 2;
173 // FirmwareDevicePackageData
174 constexpr std::array<uint8_t, fwDevicePkgDataLen> fwDevicePkgData{0xab,
175 0xcd};
176 // Size of the firmware device ID record
177 constexpr uint16_t recordLen =
178 sizeof(pldm_firmware_device_id_record) +
179 (componentBitmapBitLength / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) +
180 imageSetVersionStr.size() + sizeof(pldm_descriptor_tlv) - 1 +
181 uuid.size() + fwDevicePkgData.size();
182 // Firmware device ID record
183 constexpr std::array<uint8_t, recordLen> record{
184 0x31, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x02,
185 0x00, 0x93, 0x01, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
186 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x02, 0x00, 0x10,
187 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18, 0xa0,
188 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b, 0xab, 0xcd};
189
190 pldm_firmware_device_id_record deviceIdRecHeader{};
191 variable_field applicableComponents{};
192 variable_field outCompImageSetVersionStr{};
193 variable_field recordDescriptors{};
194 variable_field outFwDevicePkgData{};
195
196 auto rc = decode_firmware_device_id_record(
197 record.data(), record.size(), componentBitmapBitLength,
198 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
199 &recordDescriptors, &outFwDevicePkgData);
200
201 EXPECT_EQ(rc, PLDM_SUCCESS);
202 EXPECT_EQ(deviceIdRecHeader.record_length, recordLen);
203 EXPECT_EQ(deviceIdRecHeader.descriptor_count, descriptorCount);
204 EXPECT_EQ(deviceIdRecHeader.device_update_option_flags.value,
205 deviceUpdateFlag);
206 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_type,
207 PLDM_STR_TYPE_ASCII);
208 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_length,
209 imageSetVersionStr.size());
210 EXPECT_EQ(deviceIdRecHeader.fw_device_pkg_data_length, fwDevicePkgDataLen);
211
212 EXPECT_EQ(applicableComponents.length, applicableComponentsBitfield.size());
213 EXPECT_EQ(true,
214 std::equal(applicableComponents.ptr,
215 applicableComponents.ptr + applicableComponents.length,
216 applicableComponentsBitfield.begin(),
217 applicableComponentsBitfield.end()));
218
219 EXPECT_EQ(outCompImageSetVersionStr.length, imageSetVersionStr.size());
220 std::string compImageSetVersionStr(
221 reinterpret_cast<const char*>(outCompImageSetVersionStr.ptr),
222 outCompImageSetVersionStr.length);
223 EXPECT_EQ(compImageSetVersionStr, imageSetVersionStr);
224
225 uint16_t descriptorType = 0;
226 uint16_t descriptorLen = 0;
227 variable_field descriptorData{};
228 // DescriptorCount is 1, so decode_descriptor_type_length_value called once
229 rc = decode_descriptor_type_length_value(recordDescriptors.ptr,
230 recordDescriptors.length,
231 &descriptorType, &descriptorData);
232 EXPECT_EQ(rc, PLDM_SUCCESS);
233 EXPECT_EQ(recordDescriptors.length, sizeof(descriptorType) +
234 sizeof(descriptorLen) +
235 descriptorData.length);
236 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
237 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
238 EXPECT_EQ(true, std::equal(descriptorData.ptr,
239 descriptorData.ptr + descriptorData.length,
240 uuid.begin(), uuid.end()));
241
242 EXPECT_EQ(outFwDevicePkgData.length, fwDevicePkgData.size());
243 EXPECT_EQ(true,
244 std::equal(outFwDevicePkgData.ptr,
245 outFwDevicePkgData.ptr + outFwDevicePkgData.length,
246 fwDevicePkgData.begin(), fwDevicePkgData.end()));
247}
248
249TEST(DecodeFirmwareDeviceIdRecord, goodPathNofwDevicePkgData)
250{
251 constexpr uint8_t descriptorCount = 1;
252 // Continue component updates after failure
253 constexpr std::bitset<32> deviceUpdateFlag{1};
254 constexpr uint16_t componentBitmapBitLength = 8;
255 // Applicable Components - 1,2
256 std::vector<std::bitset<8>> applicableComponentsBitfield{0x03};
257 // ComponentImageSetVersionString
258 constexpr std::string_view imageSetVersionStr{"VersionString1"};
259 // Initial descriptor - UUID
260 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
261 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
262 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
263 constexpr uint16_t fwDevicePkgDataLen = 0;
264
265 // Size of the firmware device ID record
266 constexpr uint16_t recordLen =
267 sizeof(pldm_firmware_device_id_record) +
268 (componentBitmapBitLength / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) +
269 imageSetVersionStr.size() +
270 sizeof(pldm_descriptor_tlv().descriptor_type) +
271 sizeof(pldm_descriptor_tlv().descriptor_length) + uuid.size() +
272 fwDevicePkgDataLen;
273 // Firmware device ID record
274 constexpr std::array<uint8_t, recordLen> record{
275 0x2e, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x03,
276 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e,
277 0x67, 0x31, 0x02, 0x00, 0x10, 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d,
278 0x47, 0x18, 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
279
280 pldm_firmware_device_id_record deviceIdRecHeader{};
281 variable_field applicableComponents{};
282 variable_field outCompImageSetVersionStr{};
283 variable_field recordDescriptors{};
284 variable_field outFwDevicePkgData{};
285
286 auto rc = decode_firmware_device_id_record(
287 record.data(), record.size(), componentBitmapBitLength,
288 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
289 &recordDescriptors, &outFwDevicePkgData);
290
291 EXPECT_EQ(rc, PLDM_SUCCESS);
292 EXPECT_EQ(deviceIdRecHeader.record_length, recordLen);
293 EXPECT_EQ(deviceIdRecHeader.descriptor_count, descriptorCount);
294 EXPECT_EQ(deviceIdRecHeader.device_update_option_flags.value,
295 deviceUpdateFlag);
296 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_type,
297 PLDM_STR_TYPE_ASCII);
298 EXPECT_EQ(deviceIdRecHeader.comp_image_set_version_string_length,
299 imageSetVersionStr.size());
300 EXPECT_EQ(deviceIdRecHeader.fw_device_pkg_data_length, 0);
301
302 EXPECT_EQ(applicableComponents.length, applicableComponentsBitfield.size());
303 EXPECT_EQ(true,
304 std::equal(applicableComponents.ptr,
305 applicableComponents.ptr + applicableComponents.length,
306 applicableComponentsBitfield.begin(),
307 applicableComponentsBitfield.end()));
308
309 EXPECT_EQ(outCompImageSetVersionStr.length, imageSetVersionStr.size());
310 std::string compImageSetVersionStr(
311 reinterpret_cast<const char*>(outCompImageSetVersionStr.ptr),
312 outCompImageSetVersionStr.length);
313 EXPECT_EQ(compImageSetVersionStr, imageSetVersionStr);
314
315 uint16_t descriptorType = 0;
316 uint16_t descriptorLen = 0;
317 variable_field descriptorData{};
318 // DescriptorCount is 1, so decode_descriptor_type_length_value called once
319 rc = decode_descriptor_type_length_value(recordDescriptors.ptr,
320 recordDescriptors.length,
321 &descriptorType, &descriptorData);
322 EXPECT_EQ(rc, PLDM_SUCCESS);
323 EXPECT_EQ(recordDescriptors.length, sizeof(descriptorType) +
324 sizeof(descriptorLen) +
325 descriptorData.length);
326 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
327 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
328 EXPECT_EQ(true, std::equal(descriptorData.ptr,
329 descriptorData.ptr + descriptorData.length,
330 uuid.begin(), uuid.end()));
331
332 EXPECT_EQ(outFwDevicePkgData.ptr, nullptr);
333 EXPECT_EQ(outFwDevicePkgData.length, 0);
334}
335
336TEST(DecodeFirmwareDeviceIdRecord, ErrorPaths)
337{
338 constexpr uint16_t componentBitmapBitLength = 8;
339 // Invalid ComponentImageSetVersionStringType
340 constexpr std::array<uint8_t, 11> invalidRecord1{
341 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00};
342
343 int rc = 0;
344 pldm_firmware_device_id_record deviceIdRecHeader{};
345 variable_field applicableComponents{};
346 variable_field outCompImageSetVersionStr{};
347 variable_field recordDescriptors{};
348 variable_field outFwDevicePkgData{};
349
350 rc = decode_firmware_device_id_record(
351 nullptr, invalidRecord1.size(), componentBitmapBitLength,
352 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
353 &recordDescriptors, &outFwDevicePkgData);
354 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
355
356 rc = decode_firmware_device_id_record(
357 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
358 nullptr, &applicableComponents, &outCompImageSetVersionStr,
359 &recordDescriptors, &outFwDevicePkgData);
360 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
361
362 rc = decode_firmware_device_id_record(
363 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
364 &deviceIdRecHeader, nullptr, &outCompImageSetVersionStr,
365 &recordDescriptors, &outFwDevicePkgData);
366 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
367
368 rc = decode_firmware_device_id_record(
369 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
370 &deviceIdRecHeader, &applicableComponents, nullptr, &recordDescriptors,
371 &outFwDevicePkgData);
372 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
373
374 rc = decode_firmware_device_id_record(
375 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
376 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
377 nullptr, &outFwDevicePkgData);
378 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
379
380 rc = decode_firmware_device_id_record(
381 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
382 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
383 &recordDescriptors, nullptr);
384 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
385
386 rc = decode_firmware_device_id_record(
387 invalidRecord1.data(), invalidRecord1.size() - 1,
388 componentBitmapBitLength, &deviceIdRecHeader, &applicableComponents,
389 &outCompImageSetVersionStr, &recordDescriptors, &outFwDevicePkgData);
390 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
391
392 rc = decode_firmware_device_id_record(
393 invalidRecord1.data(), invalidRecord1.size(),
394 componentBitmapBitLength + 1, &deviceIdRecHeader, &applicableComponents,
395 &outCompImageSetVersionStr, &recordDescriptors, &outFwDevicePkgData);
396 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
397
398 rc = decode_firmware_device_id_record(
399 invalidRecord1.data(), invalidRecord1.size(), componentBitmapBitLength,
400 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
401 &recordDescriptors, &outFwDevicePkgData);
402 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
403
404 // Invalid ComponentImageSetVersionStringLength
405 constexpr std::array<uint8_t, 11> invalidRecord2{
406 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
407 rc = decode_firmware_device_id_record(
408 invalidRecord2.data(), invalidRecord2.size(), componentBitmapBitLength,
409 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
410 &recordDescriptors, &outFwDevicePkgData);
411 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
412
413 // invalidRecord3 size is less than RecordLength
414 constexpr std::array<uint8_t, 11> invalidRecord3{
415 0x2e, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x00, 0x00};
416 rc = decode_firmware_device_id_record(
417 invalidRecord3.data(), invalidRecord3.size(), componentBitmapBitLength,
418 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
419 &recordDescriptors, &outFwDevicePkgData);
420 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
421
422 // RecordLength is less than the calculated RecordLength
423 constexpr std::array<uint8_t, 11> invalidRecord4{
424 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x02, 0x00};
425 rc = decode_firmware_device_id_record(
426 invalidRecord4.data(), invalidRecord4.size(), componentBitmapBitLength,
427 &deviceIdRecHeader, &applicableComponents, &outCompImageSetVersionStr,
428 &recordDescriptors, &outFwDevicePkgData);
429 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
430}
431
432TEST(DecodeDescriptors, goodPath3Descriptors)
433{
434 // In the descriptor data there are 3 descriptor entries
435 // 1) IANA enterprise ID
436 constexpr std::array<uint8_t, PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH> iana{
437 0x0a, 0x0b, 0x0c, 0xd};
438 // 2) UUID
439 constexpr std::array<uint8_t, PLDM_FWUP_UUID_LENGTH> uuid{
440 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
441 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b};
442 // 3) Vendor Defined
443 constexpr std::string_view vendorTitle{"OpenBMC"};
444 constexpr size_t vendorDescriptorLen = 2;
445 constexpr std::array<uint8_t, vendorDescriptorLen> vendorDescriptorData{
446 0x01, 0x02};
447
448 constexpr size_t vendorDefinedDescriptorLen =
449 sizeof(pldm_vendor_defined_descriptor_title_data()
450 .vendor_defined_descriptor_title_str_type) +
451 sizeof(pldm_vendor_defined_descriptor_title_data()
452 .vendor_defined_descriptor_title_str_len) +
453 vendorTitle.size() + vendorDescriptorData.size();
454
455 constexpr size_t descriptorsLength =
456 3 * (sizeof(pldm_descriptor_tlv().descriptor_type) +
457 sizeof(pldm_descriptor_tlv().descriptor_length)) +
458 iana.size() + uuid.size() + vendorDefinedDescriptorLen;
459
460 constexpr std::array<uint8_t, descriptorsLength> descriptors{
461 0x01, 0x00, 0x04, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x02, 0x00, 0x10,
462 0x00, 0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18, 0xa0, 0x30,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600463 0xfc, 0x8a, 0x56, 0x58, 0x7d, 0x5b, 0xff, 0xff, 0x0b, 0x00, 0x01,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930464 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43, 0x01, 0x02};
465
466 size_t descriptorCount = 1;
467 size_t descriptorsRemainingLength = descriptorsLength;
468 int rc = 0;
469
470 while (descriptorsRemainingLength && (descriptorCount <= 3))
471 {
472 uint16_t descriptorType = 0;
473 uint16_t descriptorLen = 0;
474 variable_field descriptorData{};
475
476 rc = decode_descriptor_type_length_value(
477 descriptors.data() + descriptorsLength - descriptorsRemainingLength,
478 descriptorsRemainingLength, &descriptorType, &descriptorData);
479 EXPECT_EQ(rc, PLDM_SUCCESS);
480
481 if (descriptorCount == 1)
482 {
483 EXPECT_EQ(descriptorType, PLDM_FWUP_IANA_ENTERPRISE_ID);
484 EXPECT_EQ(descriptorData.length,
485 PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH);
486 EXPECT_EQ(true,
487 std::equal(descriptorData.ptr,
488 descriptorData.ptr + descriptorData.length,
489 iana.begin(), iana.end()));
490 }
491 else if (descriptorCount == 2)
492 {
493 EXPECT_EQ(descriptorType, PLDM_FWUP_UUID);
494 EXPECT_EQ(descriptorData.length, PLDM_FWUP_UUID_LENGTH);
495 EXPECT_EQ(true,
496 std::equal(descriptorData.ptr,
497 descriptorData.ptr + descriptorData.length,
498 uuid.begin(), uuid.end()));
499 }
500 else if (descriptorCount == 3)
501 {
502 EXPECT_EQ(descriptorType, PLDM_FWUP_VENDOR_DEFINED);
503 EXPECT_EQ(descriptorData.length, vendorDefinedDescriptorLen);
504
505 uint8_t descriptorTitleStrType = 0;
506 variable_field descriptorTitleStr{};
507 variable_field vendorDefinedDescriptorData{};
508
509 rc = decode_vendor_defined_descriptor_value(
510 descriptorData.ptr, descriptorData.length,
511 &descriptorTitleStrType, &descriptorTitleStr,
512 &vendorDefinedDescriptorData);
513 EXPECT_EQ(rc, PLDM_SUCCESS);
514
515 EXPECT_EQ(descriptorTitleStrType, PLDM_STR_TYPE_ASCII);
516 EXPECT_EQ(descriptorTitleStr.length, vendorTitle.size());
517 std::string vendorTitleStr(
518 reinterpret_cast<const char*>(descriptorTitleStr.ptr),
519 descriptorTitleStr.length);
520 EXPECT_EQ(vendorTitleStr, vendorTitle);
521
522 EXPECT_EQ(vendorDefinedDescriptorData.length,
523 vendorDescriptorData.size());
524 EXPECT_EQ(true, std::equal(vendorDefinedDescriptorData.ptr,
525 vendorDefinedDescriptorData.ptr +
526 vendorDefinedDescriptorData.length,
527 vendorDescriptorData.begin(),
528 vendorDescriptorData.end()));
529 }
530
531 descriptorsRemainingLength -= sizeof(descriptorType) +
532 sizeof(descriptorLen) +
533 descriptorData.length;
534 descriptorCount++;
535 }
536}
537
538TEST(DecodeDescriptors, errorPathDecodeDescriptorTLV)
539{
540 int rc = 0;
541 // IANA Enterprise ID descriptor length incorrect
542 constexpr std::array<uint8_t, 7> invalidIANADescriptor1{
543 0x01, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c};
544 uint16_t descriptorType = 0;
545 variable_field descriptorData{};
546
547 rc = decode_descriptor_type_length_value(nullptr,
548 invalidIANADescriptor1.size(),
549 &descriptorType, &descriptorData);
550 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
551
552 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
553 invalidIANADescriptor1.size(),
554 nullptr, &descriptorData);
555 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
556
557 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
558 invalidIANADescriptor1.size(),
559 &descriptorType, nullptr);
560 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
561
562 rc = decode_descriptor_type_length_value(
563 invalidIANADescriptor1.data(), PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN - 1,
564 &descriptorType, &descriptorData);
565 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
566
567 rc = decode_descriptor_type_length_value(invalidIANADescriptor1.data(),
568 invalidIANADescriptor1.size(),
569 &descriptorType, &descriptorData);
570 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
571
572 // IANA Enterprise ID descriptor data less than length
573 std::array<uint8_t, 7> invalidIANADescriptor2{0x01, 0x00, 0x04, 0x00,
574 0x0a, 0x0b, 0x0c};
575 rc = decode_descriptor_type_length_value(invalidIANADescriptor2.data(),
576 invalidIANADescriptor2.size(),
577 &descriptorType, &descriptorData);
578 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
579}
580
581TEST(DecodeDescriptors, errorPathVendorDefinedDescriptor)
582{
583 int rc = 0;
584 // VendorDefinedDescriptorTitleStringType is invalid
585 constexpr std::array<uint8_t, 9> invalidVendorDescriptor1{
586 0x06, 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
587 uint8_t descriptorStringType = 0;
588 variable_field descriptorTitleStr{};
589 variable_field vendorDefinedDescriptorData{};
590
591 rc = decode_vendor_defined_descriptor_value(
592 nullptr, invalidVendorDescriptor1.size(), &descriptorStringType,
593 &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, &descriptorTitleStr,
599 &vendorDefinedDescriptorData);
600 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
601
602 rc = decode_vendor_defined_descriptor_value(
603 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
604 nullptr, &descriptorTitleStr, &vendorDefinedDescriptorData);
605 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
606
607 rc = decode_vendor_defined_descriptor_value(
608 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
609 &descriptorStringType, nullptr, &vendorDefinedDescriptorData);
610 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
611
612 rc = decode_vendor_defined_descriptor_value(
613 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
614 &descriptorStringType, &descriptorTitleStr, nullptr);
615 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
616
617 rc = decode_vendor_defined_descriptor_value(
618 invalidVendorDescriptor1.data(),
619 sizeof(pldm_vendor_defined_descriptor_title_data) - 1,
620 &descriptorStringType, &descriptorTitleStr,
621 &vendorDefinedDescriptorData);
622 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
623
624 rc = decode_vendor_defined_descriptor_value(
625 invalidVendorDescriptor1.data(), invalidVendorDescriptor1.size(),
626 &descriptorStringType, &descriptorTitleStr,
627 &vendorDefinedDescriptorData);
628 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
629
630 // VendorDefinedDescriptorTitleStringLength is 0
631 std::array<uint8_t, 9> invalidVendorDescriptor2{
632 0x01, 0x00, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
633 rc = decode_vendor_defined_descriptor_value(
634 invalidVendorDescriptor2.data(), invalidVendorDescriptor2.size(),
635 &descriptorStringType, &descriptorTitleStr,
636 &vendorDefinedDescriptorData);
637 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
638
639 // VendorDefinedDescriptorData not present in the data
640 std::array<uint8_t, 9> invalidVendorDescriptor3{
641 0x01, 0x07, 0x4f, 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43};
642 rc = decode_vendor_defined_descriptor_value(
643 invalidVendorDescriptor3.data(), invalidVendorDescriptor3.size(),
644 &descriptorStringType, &descriptorTitleStr,
645 &vendorDefinedDescriptorData);
646 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
647}
648
649TEST(DecodeComponentImageInfo, goodPath)
650{
651 // Firmware
652 constexpr uint16_t compClassification = 16;
653 constexpr uint16_t compIdentifier = 300;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600654 constexpr uint32_t compComparisonStamp = 0xffffffff;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930655 // Force update
656 constexpr std::bitset<16> compOptions{1};
657 // System reboot[Bit position 3] & Medium-specific reset[Bit position 2]
658 constexpr std::bitset<16> reqCompActivationMethod{0x0c};
659 // Random ComponentLocationOffset
660 constexpr uint32_t compLocOffset = 357;
661 // Random ComponentSize
662 constexpr uint32_t compSize = 27;
663 // ComponentVersionString
664 constexpr std::string_view compVersionStr{"VersionString1"};
665 constexpr size_t compImageInfoSize =
666 sizeof(pldm_component_image_information) + compVersionStr.size();
667
668 constexpr std::array<uint8_t, compImageInfoSize> compImageInfo{
669 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
670 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
671 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
672 pldm_component_image_information outCompImageInfo{};
673 variable_field outCompVersionStr{};
674
675 auto rc =
676 decode_pldm_comp_image_info(compImageInfo.data(), compImageInfo.size(),
677 &outCompImageInfo, &outCompVersionStr);
678
679 EXPECT_EQ(rc, PLDM_SUCCESS);
680 EXPECT_EQ(outCompImageInfo.comp_classification, compClassification);
681 EXPECT_EQ(outCompImageInfo.comp_identifier, compIdentifier);
682 EXPECT_EQ(outCompImageInfo.comp_comparison_stamp, compComparisonStamp);
683 EXPECT_EQ(outCompImageInfo.comp_options.value, compOptions);
684 EXPECT_EQ(outCompImageInfo.requested_comp_activation_method.value,
685 reqCompActivationMethod);
686 EXPECT_EQ(outCompImageInfo.comp_location_offset, compLocOffset);
687 EXPECT_EQ(outCompImageInfo.comp_size, compSize);
688 EXPECT_EQ(outCompImageInfo.comp_version_string_type, PLDM_STR_TYPE_ASCII);
689 EXPECT_EQ(outCompImageInfo.comp_version_string_length,
690 compVersionStr.size());
691
692 EXPECT_EQ(outCompVersionStr.length,
693 outCompImageInfo.comp_version_string_length);
694 std::string componentVersionString(
695 reinterpret_cast<const char*>(outCompVersionStr.ptr),
696 outCompVersionStr.length);
697 EXPECT_EQ(componentVersionString, compVersionStr);
698}
699
700TEST(DecodeComponentImageInfo, errorPaths)
701{
702 int rc = 0;
703 // ComponentVersionString
704 constexpr std::string_view compVersionStr{"VersionString1"};
705 constexpr size_t compImageInfoSize =
706 sizeof(pldm_component_image_information) + compVersionStr.size();
707 // Invalid ComponentVersionStringType - 0x06
708 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo1{
709 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
710 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x56, 0x65,
711 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
712 pldm_component_image_information outCompImageInfo{};
713 variable_field outCompVersionStr{};
714
715 rc = decode_pldm_comp_image_info(nullptr, invalidCompImageInfo1.size(),
716 &outCompImageInfo, &outCompVersionStr);
717 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
718
719 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
720 invalidCompImageInfo1.size(), nullptr,
721 &outCompVersionStr);
722 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
723
724 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
725 invalidCompImageInfo1.size(),
726 &outCompImageInfo, nullptr);
727 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
728
729 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
730 sizeof(pldm_component_image_information) -
731 1,
732 &outCompImageInfo, &outCompVersionStr);
733 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
734
735 rc = decode_pldm_comp_image_info(invalidCompImageInfo1.data(),
736 invalidCompImageInfo1.size(),
737 &outCompImageInfo, &outCompVersionStr);
738 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
739
740 // Invalid ComponentVersionStringLength - 0x00
741 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo2{
742 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
743 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x56, 0x65,
744 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
745 rc = decode_pldm_comp_image_info(invalidCompImageInfo2.data(),
746 invalidCompImageInfo2.size(),
747 &outCompImageInfo, &outCompVersionStr);
748 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
749
750 // Use Component Comparison Stamp is not set, but ComponentComparisonStamp
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600751 // is not 0xffffffff
Andrew Jeffery9c766792022-08-10 23:12:49 +0930752 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo3{
753 0x10, 0x00, 0x2c, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0c, 0x00,
754 0x65, 0x01, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
755 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
756
757 rc = decode_pldm_comp_image_info(invalidCompImageInfo3.data(),
758 invalidCompImageInfo3.size() - 1,
759 &outCompImageInfo, &outCompVersionStr);
760 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
761
762 rc = decode_pldm_comp_image_info(invalidCompImageInfo3.data(),
763 invalidCompImageInfo3.size(),
764 &outCompImageInfo, &outCompVersionStr);
765 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
766
767 // Invalid ComponentLocationOffset - 0
768 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo4{
769 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
770 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
771 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
772 rc = decode_pldm_comp_image_info(invalidCompImageInfo4.data(),
773 invalidCompImageInfo4.size(),
774 &outCompImageInfo, &outCompVersionStr);
775 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
776
777 // Invalid ComponentSize - 0
778 constexpr std::array<uint8_t, compImageInfoSize> invalidCompImageInfo5{
779 0x10, 0x00, 0x2c, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x0c, 0x00,
780 0x65, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x56, 0x65,
781 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31};
782 rc = decode_pldm_comp_image_info(invalidCompImageInfo5.data(),
783 invalidCompImageInfo5.size(),
784 &outCompImageInfo, &outCompVersionStr);
785 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
786}
787
788TEST(QueryDeviceIdentifiers, goodPathEncodeRequest)
789{
790 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
791 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
792
793 uint8_t instanceId = 0x01;
794
795 auto rc = encode_query_device_identifiers_req(
796 instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, requestPtr);
797 EXPECT_EQ(rc, PLDM_SUCCESS);
798 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
799 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
800 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
801 EXPECT_EQ(requestPtr->hdr.command, PLDM_QUERY_DEVICE_IDENTIFIERS);
802}
803
804TEST(QueryDeviceIdentifiers, goodPathDecodeResponse)
805{
806 // descriptorDataLen is not fixed here taking it as 6
807 constexpr uint8_t descriptorDataLen = 6;
808 std::array<uint8_t, hdrSize +
809 sizeof(struct pldm_query_device_identifiers_resp) +
810 descriptorDataLen>
811 responseMsg{};
812 auto inResp = reinterpret_cast<struct pldm_query_device_identifiers_resp*>(
813 responseMsg.data() + hdrSize);
814
815 inResp->completion_code = PLDM_SUCCESS;
816 inResp->device_identifiers_len = htole32(descriptorDataLen);
817 inResp->descriptor_count = 1;
818
819 // filling descriptor data
820 std::fill_n(responseMsg.data() + hdrSize +
821 sizeof(struct pldm_query_device_identifiers_resp),
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600822 descriptorDataLen, 0xff);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930823
824 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
825 uint8_t completionCode = PLDM_SUCCESS;
826 uint32_t deviceIdentifiersLen = 0;
827 uint8_t descriptorCount = 0;
828 uint8_t* outDescriptorData = nullptr;
829
830 auto rc = decode_query_device_identifiers_resp(
831 response, responseMsg.size() - hdrSize, &completionCode,
832 &deviceIdentifiersLen, &descriptorCount, &outDescriptorData);
833
834 EXPECT_EQ(rc, PLDM_SUCCESS);
835 EXPECT_EQ(completionCode, PLDM_SUCCESS);
836 EXPECT_EQ(deviceIdentifiersLen, inResp->device_identifiers_len);
837 EXPECT_EQ(descriptorCount, inResp->descriptor_count);
838 EXPECT_EQ(true,
839 std::equal(outDescriptorData,
840 outDescriptorData + deviceIdentifiersLen,
841 responseMsg.begin() + hdrSize +
842 sizeof(struct pldm_query_device_identifiers_resp),
843 responseMsg.end()));
844}
845
846TEST(GetFirmwareParameters, goodPathEncodeRequest)
847{
848 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
849 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
850 uint8_t instanceId = 0x01;
851
852 auto rc = encode_get_firmware_parameters_req(
853 instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, requestPtr);
854 EXPECT_EQ(rc, PLDM_SUCCESS);
855 EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
856 EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
857 EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
858 EXPECT_EQ(requestPtr->hdr.command, PLDM_GET_FIRMWARE_PARAMETERS);
859}
860
861TEST(GetFirmwareParameters, decodeResponse)
862{
863 // CapabilitiesDuringUpdate of the firmware device
864 // Firmware device downgrade restrictions [Bit position 8] &
865 // Firmware Device Partial Updates [Bit position 3]
866 constexpr std::bitset<32> fdCapabilities{0x00000104};
867 constexpr uint16_t compCount = 1;
868 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
869 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
870
871 // constexpr uint16_t compClassification = 16;
872 // constexpr uint16_t compIdentifier = 300;
873 // constexpr uint8_t compClassificationIndex = 20;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600874 // constexpr uint32_t activeCompComparisonStamp = 0xabcdefab;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930875 // constexpr std::array<uint8_t, 8> activeComponentReleaseData = {
876 // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
877 // constexpr uint32_t pendingCompComparisonStamp = 0x12345678;
878 // constexpr std::array<uint8_t, 8> pendingComponentReleaseData = {
879 // 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
880 constexpr std::string_view activeCompVersion{"VersionString3"};
881 constexpr std::string_view pendingCompVersion{"VersionString4"};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930882
883 constexpr size_t compParamTableSize =
884 sizeof(pldm_component_parameter_entry) + activeCompVersion.size() +
885 pendingCompVersion.size();
886
887 constexpr std::array<uint8_t, compParamTableSize> compParamTable{
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600888 0x10, 0x00, 0x2c, 0x01, 0x14, 0xab, 0xef, 0xcd, 0xab, 0x01, 0x0e, 0x01,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930889 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
890 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00, 0x02,
891 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x74,
892 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
893 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
894
895 constexpr size_t getFwParamsPayloadLen =
896 sizeof(pldm_get_firmware_parameters_resp) +
897 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size() +
898 compParamTableSize;
899
900 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
901 getFwParamsResponse{
902 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
903 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
904 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
905 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x10, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600906 0x2c, 0x01, 0x14, 0xab, 0xef, 0xcd, 0xab, 0x01, 0x0e, 0x01, 0x02,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930907 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x78, 0x56, 0x34, 0x12, 0x01,
908 0x0e, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x12, 0x00,
909 0x02, 0x00, 0x00, 0x00, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
910 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x33, 0x56, 0x65, 0x72, 0x73,
911 0x69, 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x34};
912
913 auto responseMsg =
914 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
915 pldm_get_firmware_parameters_resp outResp{};
916 variable_field outActiveCompImageSetVersion{};
917 variable_field outPendingCompImageSetVersion{};
918 variable_field outCompParameterTable{};
919
920 auto rc = decode_get_firmware_parameters_resp(
921 responseMsg, getFwParamsPayloadLen, &outResp,
922 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
923 &outCompParameterTable);
924
925 EXPECT_EQ(rc, PLDM_SUCCESS);
926 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
927 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
928 EXPECT_EQ(outResp.comp_count, compCount);
929 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
930 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
931 activeCompImageSetVersion.size());
932 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
933 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
934 pendingCompImageSetVersion.size());
935 std::string activeCompImageSetVersionStr(
936 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
937 outActiveCompImageSetVersion.length);
938 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
939 std::string pendingCompImageSetVersionStr(
940 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
941 outPendingCompImageSetVersion.length);
942 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
943 EXPECT_EQ(outCompParameterTable.length, compParamTableSize);
944 EXPECT_EQ(true, std::equal(outCompParameterTable.ptr,
945 outCompParameterTable.ptr +
946 outCompParameterTable.length,
947 compParamTable.begin(), compParamTable.end()));
948}
949
950TEST(GetFirmwareParameters, decodeResponseZeroCompCount)
951{
952 // CapabilitiesDuringUpdate of the firmware device
953 // FD Host Functionality during Firmware Update [Bit position 2] &
954 // Component Update Failure Retry Capability [Bit position 1]
955 constexpr std::bitset<32> fdCapabilities{0x06};
956 constexpr uint16_t compCount = 0;
957 constexpr std::string_view activeCompImageSetVersion{"VersionString1"};
958 constexpr std::string_view pendingCompImageSetVersion{"VersionString2"};
959
960 constexpr size_t getFwParamsPayloadLen =
961 sizeof(pldm_get_firmware_parameters_resp) +
962 activeCompImageSetVersion.size() + pendingCompImageSetVersion.size();
963
964 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
965 getFwParamsResponse{
966 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
967 0x0e, 0x01, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53,
968 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x56, 0x65, 0x72, 0x73, 0x69,
969 0x6f, 0x6e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32};
970
971 auto responseMsg =
972 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
973 pldm_get_firmware_parameters_resp outResp{};
974 variable_field outActiveCompImageSetVersion{};
975 variable_field outPendingCompImageSetVersion{};
976 variable_field outCompParameterTable{};
977
978 auto rc = decode_get_firmware_parameters_resp(
979 responseMsg, getFwParamsPayloadLen, &outResp,
980 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
981 &outCompParameterTable);
982
983 EXPECT_EQ(rc, PLDM_SUCCESS);
984 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
985 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
986 EXPECT_EQ(outResp.comp_count, compCount);
987 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
988 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
989 activeCompImageSetVersion.size());
990 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
991 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len,
992 pendingCompImageSetVersion.size());
993 std::string activeCompImageSetVersionStr(
994 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
995 outActiveCompImageSetVersion.length);
996 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
997 std::string pendingCompImageSetVersionStr(
998 reinterpret_cast<const char*>(outPendingCompImageSetVersion.ptr),
999 outPendingCompImageSetVersion.length);
1000 EXPECT_EQ(pendingCompImageSetVersionStr, pendingCompImageSetVersion);
1001 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
1002 EXPECT_EQ(outCompParameterTable.length, 0);
1003}
1004
1005TEST(GetFirmwareParameters,
1006 decodeResponseNoPendingCompImageVersionStrZeroCompCount)
1007{
1008 // CapabilitiesDuringUpdate of the firmware device
1009 // FD Host Functionality during Firmware Update [Bit position 2] &
1010 // Component Update Failure Retry Capability [Bit position 1]
1011 constexpr std::bitset<32> fdCapabilities{0x06};
1012 constexpr uint16_t compCount = 0;
1013 constexpr std::string_view activeCompImageSetVersion{"VersionString"};
1014
1015 constexpr size_t getFwParamsPayloadLen =
1016 sizeof(pldm_get_firmware_parameters_resp) +
1017 activeCompImageSetVersion.size();
1018
1019 constexpr std::array<uint8_t, hdrSize + getFwParamsPayloadLen>
1020 getFwParamsResponse{0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1021 0x00, 0x00, 0x00, 0x01, 0x0d, 0x00, 0x00,
1022 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
1023 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67};
1024
1025 auto responseMsg =
1026 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
1027 pldm_get_firmware_parameters_resp outResp{};
1028 variable_field outActiveCompImageSetVersion{};
1029 variable_field outPendingCompImageSetVersion{};
1030 variable_field outCompParameterTable{};
1031
1032 auto rc = decode_get_firmware_parameters_resp(
1033 responseMsg, getFwParamsPayloadLen, &outResp,
1034 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1035 &outCompParameterTable);
1036
1037 EXPECT_EQ(rc, PLDM_SUCCESS);
1038 EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
1039 EXPECT_EQ(outResp.capabilities_during_update.value, fdCapabilities);
1040 EXPECT_EQ(outResp.comp_count, compCount);
1041 EXPECT_EQ(outResp.active_comp_image_set_ver_str_type, PLDM_STR_TYPE_ASCII);
1042 EXPECT_EQ(outResp.active_comp_image_set_ver_str_len,
1043 activeCompImageSetVersion.size());
1044 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_type,
1045 PLDM_STR_TYPE_UNKNOWN);
1046 EXPECT_EQ(outResp.pending_comp_image_set_ver_str_len, 0);
1047 std::string activeCompImageSetVersionStr(
1048 reinterpret_cast<const char*>(outActiveCompImageSetVersion.ptr),
1049 outActiveCompImageSetVersion.length);
1050 EXPECT_EQ(activeCompImageSetVersionStr, activeCompImageSetVersion);
1051 EXPECT_EQ(outPendingCompImageSetVersion.ptr, nullptr);
1052 EXPECT_EQ(outPendingCompImageSetVersion.length, 0);
1053 EXPECT_EQ(outCompParameterTable.ptr, nullptr);
1054 EXPECT_EQ(outCompParameterTable.length, 0);
1055}
1056
1057TEST(GetFirmwareParameters, decodeResponseErrorCompletionCode)
1058{
1059 constexpr std::array<uint8_t, hdrSize + sizeof(uint8_t)>
1060 getFwParamsResponse{0x00, 0x00, 0x00, 0x01};
1061
1062 auto responseMsg =
1063 reinterpret_cast<const pldm_msg*>(getFwParamsResponse.data());
1064 pldm_get_firmware_parameters_resp outResp{};
1065 variable_field outActiveCompImageSetVersion{};
1066 variable_field outPendingCompImageSetVersion{};
1067 variable_field outCompParameterTable{};
1068
1069 auto rc = decode_get_firmware_parameters_resp(
1070 responseMsg, getFwParamsResponse.size(), &outResp,
1071 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1072 &outCompParameterTable);
1073
1074 EXPECT_EQ(rc, PLDM_SUCCESS);
1075 EXPECT_EQ(outResp.completion_code, PLDM_ERROR);
1076}
1077
1078TEST(GetFirmwareParameters, errorPathdecodeResponse)
1079{
1080 int rc = 0;
1081 // Invalid ActiveComponentImageSetVersionStringType
1082 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse1{
1083 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1084 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00};
1085
1086 auto responseMsg =
1087 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse1.data());
1088 pldm_get_firmware_parameters_resp outResp{};
1089 variable_field outActiveCompImageSetVersion{};
1090 variable_field outPendingCompImageSetVersion{};
1091 variable_field outCompParameterTable{};
1092
1093 rc = decode_get_firmware_parameters_resp(
1094 nullptr, invalidGetFwParamsResponse1.size() - hdrSize, &outResp,
1095 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1096 &outCompParameterTable);
1097 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1098
1099 rc = decode_get_firmware_parameters_resp(
1100 responseMsg, invalidGetFwParamsResponse1.size() - hdrSize, nullptr,
1101 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1102 &outCompParameterTable);
1103 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1104
1105 rc = decode_get_firmware_parameters_resp(
1106 responseMsg, invalidGetFwParamsResponse1.size() - hdrSize, &outResp,
1107 nullptr, &outPendingCompImageSetVersion, &outCompParameterTable);
1108 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1109
1110 rc = decode_get_firmware_parameters_resp(
1111 responseMsg, invalidGetFwParamsResponse1.size() - hdrSize, &outResp,
1112 &outActiveCompImageSetVersion, nullptr, &outCompParameterTable);
1113 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1114
1115 rc = decode_get_firmware_parameters_resp(
1116 responseMsg, invalidGetFwParamsResponse1.size() - hdrSize, &outResp,
1117 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion, nullptr);
1118 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1119
1120 rc = decode_get_firmware_parameters_resp(
1121 responseMsg, 0, &outResp, &outActiveCompImageSetVersion,
1122 &outPendingCompImageSetVersion, &outCompParameterTable);
1123 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1124
1125 rc = decode_get_firmware_parameters_resp(
1126 responseMsg, invalidGetFwParamsResponse1.size() - 1 - hdrSize, &outResp,
1127 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1128 &outCompParameterTable);
1129 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1130
1131 rc = decode_get_firmware_parameters_resp(
1132 responseMsg, invalidGetFwParamsResponse1.size() - hdrSize, &outResp,
1133 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1134 &outCompParameterTable);
1135 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1136
1137 // Invalid ActiveComponentImageSetVersionStringLength
1138 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse2{
1139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1140 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};
1141 responseMsg =
1142 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse2.data());
1143 rc = decode_get_firmware_parameters_resp(
1144 responseMsg, invalidGetFwParamsResponse2.size() - hdrSize, &outResp,
1145 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1146 &outCompParameterTable);
1147 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1148
1149 // Invalid PendingComponentImageSetVersionStringType &
1150 // PendingComponentImageSetVersionStringLength
1151 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse3{
1152 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1153 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00};
1154 responseMsg =
1155 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse3.data());
1156 rc = decode_get_firmware_parameters_resp(
1157 responseMsg, invalidGetFwParamsResponse3.size() - hdrSize, &outResp,
1158 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1159 &outCompParameterTable);
1160 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1161
1162 // Invalid PendingComponentImageSetVersionStringType &
1163 // PendingComponentImageSetVersionStringLength
1164 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse4{
1165 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1166 0x00, 0x00, 0x00, 0x01, 0x0e, 0x06, 0x0e};
1167 responseMsg =
1168 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse4.data());
1169 rc = decode_get_firmware_parameters_resp(
1170 responseMsg, invalidGetFwParamsResponse4.size() - hdrSize, &outResp,
1171 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1172 &outCompParameterTable);
1173 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1174
1175 // Total payload length less than expected
1176 constexpr std::array<uint8_t, 14> invalidGetFwParamsResponse5{
1177 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
1178 0x00, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x0e};
1179 responseMsg =
1180 reinterpret_cast<const pldm_msg*>(invalidGetFwParamsResponse5.data());
1181 rc = decode_get_firmware_parameters_resp(
1182 responseMsg, invalidGetFwParamsResponse5.size() - hdrSize, &outResp,
1183 &outActiveCompImageSetVersion, &outPendingCompImageSetVersion,
1184 &outCompParameterTable);
1185 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1186}
1187
1188TEST(GetFirmwareParameters, goodPathDecodeComponentParameterEntry)
1189{
1190 // Random value for component classification
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001191 constexpr uint16_t compClassification = 0x0a0b;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301192 // Random value for component classification
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001193 constexpr uint16_t compIdentifier = 0x0c0d;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301194 // Random value for component classification
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001195 constexpr uint32_t timestamp = 0x12345678;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301196 // Random value for component activation methods
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001197 constexpr uint16_t compActivationMethods = 0xbbdd;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301198 // Random value for capabilities during update
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001199 constexpr uint32_t capabilitiesDuringUpdate = 0xbadbeefe;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301200
1201 // ActiveCompImageSetVerStrLen is not fixed here taking it as 8
1202 constexpr uint8_t activeCompVerStrLen = 8;
1203 // PendingCompImageSetVerStrLen is not fixed here taking it as 8
1204 constexpr uint8_t pendingCompVerStrLen = 8;
1205 constexpr size_t entryLength =
1206 sizeof(struct pldm_component_parameter_entry) + activeCompVerStrLen +
1207 pendingCompVerStrLen;
1208 std::array<uint8_t, entryLength> entry{};
1209
1210 auto inEntry =
1211 reinterpret_cast<struct pldm_component_parameter_entry*>(entry.data());
1212
1213 inEntry->comp_classification = htole16(compClassification);
1214 inEntry->comp_identifier = htole16(compIdentifier);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001215 inEntry->comp_classification_index = 0x0f;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301216 inEntry->active_comp_comparison_stamp = htole32(timestamp);
1217 inEntry->active_comp_ver_str_type = 1;
1218 inEntry->active_comp_ver_str_len = activeCompVerStrLen;
1219 std::fill_n(inEntry->active_comp_release_date,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001220 sizeof(inEntry->active_comp_release_date), 0xff);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301221 inEntry->pending_comp_comparison_stamp = htole32(timestamp);
1222 inEntry->pending_comp_ver_str_type = 1;
1223 inEntry->pending_comp_ver_str_len = pendingCompVerStrLen;
1224 std::fill_n(inEntry->pending_comp_release_date,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001225 sizeof(inEntry->pending_comp_release_date), 0xff);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301226 inEntry->comp_activation_methods.value = htole16(compActivationMethods);
1227 inEntry->capabilities_during_update.value =
1228 htole32(capabilitiesDuringUpdate);
1229 constexpr auto activeCompVerStrPos =
1230 sizeof(struct pldm_component_parameter_entry);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001231 std::fill_n(entry.data() + activeCompVerStrPos, activeCompVerStrLen, 0xaa);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301232 constexpr auto pendingCompVerStrPos =
1233 activeCompVerStrPos + activeCompVerStrLen;
1234 std::fill_n(entry.data() + pendingCompVerStrPos, pendingCompVerStrLen,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001235 0xbb);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301236
1237 struct pldm_component_parameter_entry outEntry;
1238 struct variable_field outActiveCompVerStr;
1239 struct variable_field outPendingCompVerStr;
1240
1241 auto rc = decode_get_firmware_parameters_resp_comp_entry(
1242 entry.data(), entryLength, &outEntry, &outActiveCompVerStr,
1243 &outPendingCompVerStr);
1244
1245 EXPECT_EQ(rc, PLDM_SUCCESS);
1246
1247 EXPECT_EQ(outEntry.comp_classification, compClassification);
1248 EXPECT_EQ(outEntry.comp_identifier, compIdentifier);
1249 EXPECT_EQ(inEntry->comp_classification_index,
1250 outEntry.comp_classification_index);
1251 EXPECT_EQ(outEntry.active_comp_comparison_stamp, timestamp);
1252 EXPECT_EQ(inEntry->active_comp_ver_str_type,
1253 outEntry.active_comp_ver_str_type);
1254 EXPECT_EQ(inEntry->active_comp_ver_str_len,
1255 outEntry.active_comp_ver_str_len);
1256 EXPECT_EQ(0, memcmp(inEntry->active_comp_release_date,
1257 outEntry.active_comp_release_date,
1258 sizeof(inEntry->active_comp_release_date)));
1259 EXPECT_EQ(outEntry.pending_comp_comparison_stamp, timestamp);
1260 EXPECT_EQ(inEntry->pending_comp_ver_str_type,
1261 outEntry.pending_comp_ver_str_type);
1262 EXPECT_EQ(inEntry->pending_comp_ver_str_len,
1263 outEntry.pending_comp_ver_str_len);
1264 EXPECT_EQ(0, memcmp(inEntry->pending_comp_release_date,
1265 outEntry.pending_comp_release_date,
1266 sizeof(inEntry->pending_comp_release_date)));
1267 EXPECT_EQ(outEntry.comp_activation_methods.value, compActivationMethods);
1268 EXPECT_EQ(outEntry.capabilities_during_update.value,
1269 capabilitiesDuringUpdate);
1270
1271 EXPECT_EQ(0, memcmp(outActiveCompVerStr.ptr,
1272 entry.data() + activeCompVerStrPos,
1273 outActiveCompVerStr.length));
1274 EXPECT_EQ(0, memcmp(outPendingCompVerStr.ptr,
1275 entry.data() + pendingCompVerStrPos,
1276 outPendingCompVerStr.length));
1277}
1278
1279TEST(RequestUpdate, goodPathEncodeRequest)
1280{
1281 constexpr uint8_t instanceId = 1;
1282 constexpr uint32_t maxTransferSize = 512;
1283 constexpr uint16_t numOfComp = 3;
1284 constexpr uint8_t maxOutstandingTransferReq = 2;
1285 constexpr uint16_t pkgDataLen = 0x1234;
1286 constexpr std::string_view compImgSetVerStr = "0penBmcv1.0";
1287 constexpr uint8_t compImgSetVerStrLen =
1288 static_cast<uint8_t>(compImgSetVerStr.size());
1289 variable_field compImgSetVerStrInfo{};
1290 compImgSetVerStrInfo.ptr =
1291 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1292 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1293
1294 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1295 compImgSetVerStrLen>
1296 request{};
1297 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1298
1299 auto rc = encode_request_update_req(
1300 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1301 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1302 &compImgSetVerStrInfo, requestMsg,
1303 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1304 EXPECT_EQ(rc, PLDM_SUCCESS);
1305
1306 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1307 compImgSetVerStrLen>
1308 outRequest{0x81, 0x05, 0x10, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00,
1309 0x02, 0x34, 0x12, 0x01, 0x0b, 0x30, 0x70, 0x65, 0x6e,
1310 0x42, 0x6d, 0x63, 0x76, 0x31, 0x2e, 0x30};
1311 EXPECT_EQ(request, outRequest);
1312}
1313
1314TEST(RequestUpdate, errorPathEncodeRequest)
1315{
1316 constexpr uint8_t instanceId = 1;
1317 uint32_t maxTransferSize = 512;
1318 constexpr uint16_t numOfComp = 3;
1319 uint8_t maxOutstandingTransferReq = 2;
1320 constexpr uint16_t pkgDataLen = 0x1234;
1321 constexpr std::string_view compImgSetVerStr = "0penBmcv1.0";
1322 uint8_t compImgSetVerStrLen = static_cast<uint8_t>(compImgSetVerStr.size());
1323 variable_field compImgSetVerStrInfo{};
1324 compImgSetVerStrInfo.ptr =
1325 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1326 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1327
1328 std::array<uint8_t, hdrSize + sizeof(struct pldm_request_update_req) +
1329 compImgSetVerStr.size()>
1330 request{};
1331 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1332
1333 auto rc = encode_request_update_req(
1334 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1335 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen, nullptr,
1336 requestMsg,
1337 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1338 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1339
1340 compImgSetVerStrInfo.ptr = nullptr;
1341 rc = encode_request_update_req(
1342 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1343 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1344 &compImgSetVerStrInfo, requestMsg,
1345 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1346 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1347 compImgSetVerStrInfo.ptr =
1348 reinterpret_cast<const uint8_t*>(compImgSetVerStr.data());
1349
1350 rc = encode_request_update_req(
1351 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1352 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1353 &compImgSetVerStrInfo, nullptr,
1354 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1355 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1356
1357 rc = encode_request_update_req(instanceId, maxTransferSize, numOfComp,
1358 maxOutstandingTransferReq, pkgDataLen,
1359 PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1360 &compImgSetVerStrInfo, requestMsg, 0);
1361 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1362
1363 compImgSetVerStrLen = 0;
1364 rc = encode_request_update_req(
1365 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1366 pkgDataLen, PLDM_STR_TYPE_ASCII, 0, &compImgSetVerStrInfo, nullptr,
1367 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1368 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1369 compImgSetVerStrLen = static_cast<uint8_t>(compImgSetVerStr.size());
1370
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001371 compImgSetVerStrInfo.length = 0xffff;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301372 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 compImgSetVerStrInfo.length = compImgSetVerStrLen;
1379
1380 maxTransferSize = PLDM_FWUP_BASELINE_TRANSFER_SIZE - 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 maxTransferSize = PLDM_FWUP_BASELINE_TRANSFER_SIZE;
1388
1389 maxOutstandingTransferReq = PLDM_FWUP_MIN_OUTSTANDING_REQ - 1;
1390 rc = encode_request_update_req(
1391 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1392 pkgDataLen, PLDM_STR_TYPE_ASCII, compImgSetVerStrLen,
1393 &compImgSetVerStrInfo, nullptr,
1394 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1395 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1396 maxOutstandingTransferReq = PLDM_FWUP_MIN_OUTSTANDING_REQ;
1397
1398 rc = encode_request_update_req(
1399 instanceId, maxTransferSize, numOfComp, maxOutstandingTransferReq,
1400 pkgDataLen, PLDM_STR_TYPE_UNKNOWN, compImgSetVerStrLen,
1401 &compImgSetVerStrInfo, nullptr,
1402 sizeof(struct pldm_request_update_req) + compImgSetVerStrLen);
1403 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1404}
1405
1406TEST(RequestUpdate, goodPathDecodeResponse)
1407{
1408 constexpr uint16_t fdMetaDataLen = 1024;
1409 constexpr uint8_t fdWillSendPkgData = 1;
1410 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_request_update_resp)>
1411 requestUpdateResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01};
1412
1413 auto responseMsg1 =
1414 reinterpret_cast<const pldm_msg*>(requestUpdateResponse1.data());
1415 uint8_t outCompletionCode = 0;
1416 uint16_t outFdMetaDataLen = 0;
1417 uint8_t outFdWillSendPkgData = 0;
1418
1419 auto rc = decode_request_update_resp(
1420 responseMsg1, requestUpdateResponse1.size() - hdrSize,
1421 &outCompletionCode, &outFdMetaDataLen, &outFdWillSendPkgData);
1422 EXPECT_EQ(rc, PLDM_SUCCESS);
1423 EXPECT_EQ(outCompletionCode, PLDM_SUCCESS);
1424 EXPECT_EQ(outFdMetaDataLen, fdMetaDataLen);
1425 EXPECT_EQ(outFdWillSendPkgData, fdWillSendPkgData);
1426
1427 outCompletionCode = 0;
1428 outFdMetaDataLen = 0;
1429 outFdWillSendPkgData = 0;
1430
1431 constexpr std::array<uint8_t, hdrSize + sizeof(outCompletionCode)>
1432 requestUpdateResponse2{0x00, 0x00, 0x00, 0x81};
1433 auto responseMsg2 =
1434 reinterpret_cast<const pldm_msg*>(requestUpdateResponse2.data());
1435 rc = decode_request_update_resp(
1436 responseMsg2, requestUpdateResponse2.size() - hdrSize,
1437 &outCompletionCode, &outFdMetaDataLen, &outFdWillSendPkgData);
1438 EXPECT_EQ(rc, PLDM_SUCCESS);
1439 EXPECT_EQ(outCompletionCode, PLDM_FWUP_ALREADY_IN_UPDATE_MODE);
1440}
1441
1442TEST(RequestUpdate, errorPathDecodeResponse)
1443{
1444 constexpr std::array<uint8_t,
1445 hdrSize + sizeof(pldm_request_update_resp) - 1>
1446 requestUpdateResponse{0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
1447
1448 auto responseMsg =
1449 reinterpret_cast<const pldm_msg*>(requestUpdateResponse.data());
1450 uint8_t outCompletionCode = 0;
1451 uint16_t outFdMetaDataLen = 0;
1452 uint8_t outFdWillSendPkgData = 0;
1453
1454 auto rc = decode_request_update_resp(
1455 nullptr, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1456 &outFdMetaDataLen, &outFdWillSendPkgData);
1457 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1458
1459 rc = decode_request_update_resp(
1460 responseMsg, requestUpdateResponse.size() - hdrSize, nullptr,
1461 &outFdMetaDataLen, &outFdWillSendPkgData);
1462 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1463
1464 rc = decode_request_update_resp(
1465 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1466 nullptr, &outFdWillSendPkgData);
1467 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1468
1469 rc = decode_request_update_resp(
1470 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1471 &outFdMetaDataLen, nullptr);
1472 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1473
1474 rc = decode_request_update_resp(responseMsg, 0, &outCompletionCode,
1475 &outFdMetaDataLen, &outFdWillSendPkgData);
1476 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1477
1478 rc = decode_request_update_resp(
1479 responseMsg, requestUpdateResponse.size() - hdrSize, &outCompletionCode,
1480 &outFdMetaDataLen, &outFdWillSendPkgData);
1481 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1482}
1483
1484TEST(PassComponentTable, goodPathEncodeRequest)
1485{
1486 constexpr uint8_t instanceId = 1;
1487 constexpr uint16_t compIdentifier = 400;
1488 constexpr uint8_t compClassificationIndex = 40;
1489 constexpr uint32_t compComparisonStamp = 0x12345678;
1490 constexpr std::string_view compVerStr = "0penBmcv1.1";
1491 constexpr uint8_t compVerStrLen = static_cast<uint8_t>(compVerStr.size());
1492 variable_field compVerStrInfo{};
1493 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1494 compVerStrInfo.length = compVerStrLen;
1495
1496 std::array<uint8_t,
1497 hdrSize + sizeof(pldm_pass_component_table_req) + compVerStrLen>
1498 request{};
1499 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1500
1501 auto rc = encode_pass_component_table_req(
1502 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1503 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1504 compVerStrLen, &compVerStrInfo, requestMsg,
1505 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1506 EXPECT_EQ(rc, PLDM_SUCCESS);
1507
1508 std::array<uint8_t,
1509 hdrSize + sizeof(pldm_pass_component_table_req) + compVerStrLen>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001510 outRequest{0x81, 0x05, 0x13, 0x05, 0x0a, 0x00, 0x90, 0x01, 0x28,
1511 0x78, 0x56, 0x34, 0x12, 0x01, 0x0b, 0x30, 0x70, 0x65,
1512 0x6e, 0x42, 0x6d, 0x63, 0x76, 0x31, 0x2e, 0x31};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301513 EXPECT_EQ(request, outRequest);
1514}
1515
1516TEST(PassComponentTable, errorPathEncodeRequest)
1517{
1518 constexpr uint8_t instanceId = 1;
1519 constexpr uint16_t compIdentifier = 400;
1520 constexpr uint8_t compClassificationIndex = 40;
1521 constexpr uint32_t compComparisonStamp = 0x12345678;
1522 constexpr std::string_view compVerStr = "0penBmcv1.1";
1523 constexpr uint8_t compVerStrLen = static_cast<uint8_t>(compVerStr.size());
1524 variable_field compVerStrInfo{};
1525 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1526 compVerStrInfo.length = compVerStrLen;
1527
1528 std::array<uint8_t,
1529 hdrSize + sizeof(pldm_pass_component_table_req) + compVerStrLen>
1530 request{};
1531 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1532
1533 auto rc = encode_pass_component_table_req(
1534 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1535 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1536 compVerStrLen, nullptr, requestMsg,
1537 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1538 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1539
1540 compVerStrInfo.ptr = nullptr;
1541 rc = encode_pass_component_table_req(
1542 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1543 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1544 compVerStrLen, &compVerStrInfo, requestMsg,
1545 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1546 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1547 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1548
1549 rc = encode_pass_component_table_req(
1550 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1551 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1552 compVerStrLen, &compVerStrInfo, nullptr,
1553 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1554 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1555
1556 rc = encode_pass_component_table_req(
1557 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1558 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1559 compVerStrLen, &compVerStrInfo, requestMsg,
1560 sizeof(pldm_pass_component_table_req));
1561 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1562
1563 rc = encode_pass_component_table_req(
1564 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1565 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII, 0,
1566 &compVerStrInfo, requestMsg,
1567 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1568 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1569
1570 rc = encode_pass_component_table_req(
1571 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1572 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1573 compVerStrLen - 1, &compVerStrInfo, requestMsg,
1574 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1575 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1576
1577 rc = encode_pass_component_table_req(
1578 instanceId, PLDM_START_AND_END + 1, PLDM_COMP_FIRMWARE, compIdentifier,
1579 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_ASCII,
1580 compVerStrLen, &compVerStrInfo, requestMsg,
1581 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1582 EXPECT_EQ(rc, PLDM_INVALID_TRANSFER_OPERATION_FLAG);
1583
1584 rc = encode_pass_component_table_req(
1585 instanceId, PLDM_START_AND_END, PLDM_COMP_FIRMWARE, compIdentifier,
1586 compClassificationIndex, compComparisonStamp, PLDM_STR_TYPE_UNKNOWN,
1587 compVerStrLen, &compVerStrInfo, requestMsg,
1588 sizeof(pldm_pass_component_table_req) + compVerStrLen);
1589 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1590}
1591
1592TEST(PassComponentTable, goodPathDecodeResponse)
1593{
1594 constexpr std::array<uint8_t,
1595 hdrSize + sizeof(pldm_pass_component_table_resp)>
1596 passCompTableResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
1597 auto responseMsg1 =
1598 reinterpret_cast<const pldm_msg*>(passCompTableResponse1.data());
1599
1600 uint8_t completionCode = 0;
1601 uint8_t compResp = 0;
1602 uint8_t compRespCode = 0;
1603
1604 auto rc = decode_pass_component_table_resp(
1605 responseMsg1, sizeof(pldm_pass_component_table_resp), &completionCode,
1606 &compResp, &compRespCode);
1607
1608 EXPECT_EQ(rc, PLDM_SUCCESS);
1609 EXPECT_EQ(completionCode, PLDM_SUCCESS);
1610 EXPECT_EQ(compResp, PLDM_CR_COMP_CAN_BE_UPDATED);
1611 EXPECT_EQ(compRespCode, PLDM_CRC_COMP_COMPARISON_STAMP_IDENTICAL);
1612
1613 constexpr std::array<uint8_t,
1614 hdrSize + sizeof(pldm_pass_component_table_resp)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001615 passCompTableResponse2{0x00, 0x00, 0x00, 0x00, 0x00, 0xd0};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301616 auto responseMsg2 =
1617 reinterpret_cast<const pldm_msg*>(passCompTableResponse2.data());
1618 rc = decode_pass_component_table_resp(
1619 responseMsg2, sizeof(pldm_pass_component_table_resp), &completionCode,
1620 &compResp, &compRespCode);
1621
1622 EXPECT_EQ(rc, PLDM_SUCCESS);
1623 EXPECT_EQ(completionCode, PLDM_SUCCESS);
1624 EXPECT_EQ(compResp, PLDM_CR_COMP_CAN_BE_UPDATED);
1625 EXPECT_EQ(compRespCode, PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MIN);
1626
1627 constexpr std::array<uint8_t,
1628 hdrSize + sizeof(pldm_pass_component_table_resp)>
1629 passCompTableResponse3{0x00, 0x00, 0x00, 0x80};
1630 auto responseMsg3 =
1631 reinterpret_cast<const pldm_msg*>(passCompTableResponse3.data());
1632
1633 rc = decode_pass_component_table_resp(
1634 responseMsg3, sizeof(pldm_pass_component_table_resp), &completionCode,
1635 &compResp, &compRespCode);
1636
1637 EXPECT_EQ(rc, PLDM_SUCCESS);
1638 EXPECT_EQ(completionCode, PLDM_FWUP_NOT_IN_UPDATE_MODE);
1639}
1640
1641TEST(PassComponentTable, errorPathDecodeResponse)
1642{
1643 constexpr std::array<uint8_t,
1644 hdrSize + sizeof(pldm_pass_component_table_resp) - 1>
1645 passCompTableResponse1{0x00, 0x00, 0x00, 0x00, 0x00};
1646 auto responseMsg1 =
1647 reinterpret_cast<const pldm_msg*>(passCompTableResponse1.data());
1648
1649 uint8_t completionCode = 0;
1650 uint8_t compResp = 0;
1651 uint8_t compRespCode = 0;
1652
1653 auto rc = decode_pass_component_table_resp(
1654 nullptr, sizeof(pldm_pass_component_table_resp) - 1, &completionCode,
1655 &compResp, &compRespCode);
1656 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1657
1658 rc = decode_pass_component_table_resp(
1659 responseMsg1, sizeof(pldm_pass_component_table_resp) - 1, nullptr,
1660 &compResp, &compRespCode);
1661 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1662
1663 rc = decode_pass_component_table_resp(
1664 responseMsg1, sizeof(pldm_pass_component_table_resp) - 1,
1665 &completionCode, nullptr, &compRespCode);
1666 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1667
1668 rc = decode_pass_component_table_resp(
1669 responseMsg1, sizeof(pldm_pass_component_table_resp) - 1,
1670 &completionCode, &compResp, nullptr);
1671 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1672
1673 rc = decode_pass_component_table_resp(responseMsg1, 0, &completionCode,
1674 &compResp, &compRespCode);
1675 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1676
1677 rc = decode_pass_component_table_resp(
1678 responseMsg1, sizeof(pldm_pass_component_table_resp) - 1,
1679 &completionCode, &compResp, &compRespCode);
1680 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1681
1682 constexpr std::array<uint8_t,
1683 hdrSize + sizeof(pldm_pass_component_table_resp)>
1684 passCompTableResponse2{0x00, 0x00, 0x00, 0x00, 0x02, 0x00};
1685 auto responseMsg2 =
1686 reinterpret_cast<const pldm_msg*>(passCompTableResponse2.data());
1687 rc = decode_pass_component_table_resp(
1688 responseMsg2, sizeof(pldm_pass_component_table_resp), &completionCode,
1689 &compResp, &compRespCode);
1690 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1691
1692 constexpr std::array<uint8_t,
1693 hdrSize + sizeof(pldm_pass_component_table_resp)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001694 passCompTableResponse3{0x00, 0x00, 0x00, 0x00, 0x00, 0x0c};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301695 auto responseMsg3 =
1696 reinterpret_cast<const pldm_msg*>(passCompTableResponse3.data());
1697 rc = decode_pass_component_table_resp(
1698 responseMsg3, sizeof(pldm_pass_component_table_resp), &completionCode,
1699 &compResp, &compRespCode);
1700 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1701
1702 constexpr std::array<uint8_t,
1703 hdrSize + sizeof(pldm_pass_component_table_resp)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001704 passCompTableResponse4{0x00, 0x00, 0x00, 0x00, 0x00, 0xf0};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301705 auto responseMsg4 =
1706 reinterpret_cast<const pldm_msg*>(passCompTableResponse4.data());
1707 rc = decode_pass_component_table_resp(
1708 responseMsg4, sizeof(pldm_pass_component_table_resp), &completionCode,
1709 &compResp, &compRespCode);
1710 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1711}
1712
1713TEST(UpdateComponent, goodPathEncodeRequest)
1714{
1715 constexpr uint8_t instanceId = 2;
1716 constexpr uint16_t compIdentifier = 500;
1717 constexpr uint8_t compClassificationIndex = 50;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001718 constexpr uint32_t compComparisonStamp = 0x89abcdef;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301719 constexpr uint32_t compImageSize = 4096;
1720 constexpr bitfield32_t updateOptionFlags{1};
1721 constexpr std::string_view compVerStr = "OpenBmcv2.2";
1722 constexpr uint8_t compVerStrLen = static_cast<uint8_t>(compVerStr.size());
1723 variable_field compVerStrInfo{};
1724 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1725 compVerStrInfo.length = compVerStrLen;
1726
1727 std::array<uint8_t,
1728 hdrSize + sizeof(pldm_update_component_req) + compVerStrLen>
1729 request{};
1730 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1731
1732 auto rc = encode_update_component_req(
1733 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1734 compComparisonStamp, compImageSize, updateOptionFlags,
1735 PLDM_STR_TYPE_ASCII, compVerStrLen, &compVerStrInfo, requestMsg,
1736 sizeof(pldm_update_component_req) + compVerStrLen);
1737 EXPECT_EQ(rc, PLDM_SUCCESS);
1738
1739 std::array<uint8_t,
1740 hdrSize + sizeof(pldm_update_component_req) + compVerStrLen>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001741 outRequest{0x82, 0x05, 0x14, 0x0a, 0x00, 0xf4, 0x01, 0x32, 0xef,
1742 0xcd, 0xab, 0x89, 0x00, 0x10, 0x00, 0x00, 0x01, 0x00,
1743 0x00, 0x00, 0x01, 0x0b, 0x4f, 0x70, 0x65, 0x6e, 0x42,
1744 0x6d, 0x63, 0x76, 0x32, 0x2e, 0x32};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301745 EXPECT_EQ(request, outRequest);
1746}
1747
1748TEST(UpdateComponent, errorPathEncodeRequest)
1749{
1750 constexpr uint8_t instanceId = 2;
1751 constexpr uint16_t compIdentifier = 500;
1752 constexpr uint8_t compClassificationIndex = 50;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001753 constexpr uint32_t compComparisonStamp = 0x89abcdef;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301754 constexpr uint32_t compImageSize = 4096;
1755 constexpr bitfield32_t updateOptionFlags{1};
1756 constexpr std::string_view compVerStr = "OpenBmcv2.2";
1757 constexpr uint8_t compVerStrLen = static_cast<uint8_t>(compVerStr.size());
1758 variable_field compVerStrInfo{};
1759 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1760 compVerStrInfo.length = compVerStrLen;
1761
1762 std::array<uint8_t,
1763 hdrSize + sizeof(pldm_update_component_req) + compVerStrLen>
1764 request{};
1765 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
1766
1767 auto rc = encode_update_component_req(
1768 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1769 compComparisonStamp, compImageSize, updateOptionFlags,
1770 PLDM_STR_TYPE_ASCII, compVerStrLen, nullptr, requestMsg,
1771 sizeof(pldm_update_component_req) + compVerStrLen);
1772 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1773
1774 compVerStrInfo.ptr = nullptr;
1775 rc = encode_update_component_req(
1776 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1777 compComparisonStamp, compImageSize, updateOptionFlags,
1778 PLDM_STR_TYPE_ASCII, compVerStrLen, &compVerStrInfo, requestMsg,
1779 sizeof(pldm_update_component_req) + compVerStrLen);
1780 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1781 compVerStrInfo.ptr = reinterpret_cast<const uint8_t*>(compVerStr.data());
1782
1783 rc = encode_update_component_req(
1784 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1785 compComparisonStamp, compImageSize, updateOptionFlags,
1786 PLDM_STR_TYPE_ASCII, compVerStrLen, &compVerStrInfo, nullptr,
1787 sizeof(pldm_update_component_req) + compVerStrLen);
1788 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1789
1790 rc = encode_update_component_req(
1791 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1792 compComparisonStamp, compImageSize, updateOptionFlags,
1793 PLDM_STR_TYPE_ASCII, compVerStrLen, &compVerStrInfo, requestMsg,
1794 sizeof(pldm_update_component_req));
1795 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1796
1797 rc = encode_update_component_req(
1798 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1799 compComparisonStamp, 0, updateOptionFlags, PLDM_STR_TYPE_ASCII,
1800 compVerStrLen, &compVerStrInfo, requestMsg,
1801 sizeof(pldm_update_component_req) + compVerStrLen);
1802 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1803
1804 rc = encode_update_component_req(
1805 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1806 compComparisonStamp, compImageSize, updateOptionFlags,
1807 PLDM_STR_TYPE_ASCII, 0, &compVerStrInfo, requestMsg,
1808 sizeof(pldm_update_component_req) + compVerStrLen);
1809 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1810
1811 rc = encode_update_component_req(
1812 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1813 compComparisonStamp, compImageSize, updateOptionFlags,
1814 PLDM_STR_TYPE_ASCII, compVerStrLen - 1, &compVerStrInfo, requestMsg,
1815 sizeof(pldm_update_component_req) + compVerStrLen);
1816 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1817
1818 rc = encode_update_component_req(
1819 instanceId, PLDM_COMP_FIRMWARE, compIdentifier, compClassificationIndex,
1820 compComparisonStamp, compImageSize, updateOptionFlags,
1821 PLDM_STR_TYPE_UNKNOWN, compVerStrLen, &compVerStrInfo, requestMsg,
1822 sizeof(pldm_update_component_req) + compVerStrLen);
1823 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1824}
1825
1826TEST(UpdateComponent, goodPathDecodeResponse)
1827{
1828 constexpr std::bitset<32> forceUpdateComp{1};
1829 constexpr uint16_t timeBeforeSendingReqFwData100s = 100;
1830 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
1831 updateComponentResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1832 0x01, 0x00, 0x00, 0x00, 0x64, 0x00};
1833 auto responseMsg1 =
1834 reinterpret_cast<const pldm_msg*>(updateComponentResponse1.data());
1835
1836 uint8_t completionCode = 0;
1837 uint8_t compCompatibilityResp = 0;
1838 uint8_t compCompatibilityRespCode = 0;
1839 bitfield32_t updateOptionFlagsEnabled{};
1840 uint16_t timeBeforeReqFWData = 0;
1841
1842 auto rc = decode_update_component_resp(
1843 responseMsg1, sizeof(pldm_update_component_resp), &completionCode,
1844 &compCompatibilityResp, &compCompatibilityRespCode,
1845 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1846
1847 EXPECT_EQ(rc, PLDM_SUCCESS);
1848 EXPECT_EQ(completionCode, PLDM_SUCCESS);
1849 EXPECT_EQ(compCompatibilityResp, PLDM_CCR_COMP_CAN_BE_UPDATED);
1850 EXPECT_EQ(compCompatibilityRespCode, PLDM_CCRC_NO_RESPONSE_CODE);
1851 EXPECT_EQ(updateOptionFlagsEnabled.value, forceUpdateComp);
1852 EXPECT_EQ(timeBeforeReqFWData, timeBeforeSendingReqFwData100s);
1853
1854 constexpr std::bitset<32> noFlags{};
1855 constexpr uint16_t timeBeforeSendingReqFwData0s = 0;
1856 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
1857 updateComponentResponse2{0x00, 0x00, 0x00, 0x00, 0x01, 0x09,
1858 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1859 auto responseMsg2 =
1860 reinterpret_cast<const pldm_msg*>(updateComponentResponse2.data());
1861 rc = decode_update_component_resp(
1862 responseMsg2, sizeof(pldm_update_component_resp), &completionCode,
1863 &compCompatibilityResp, &compCompatibilityRespCode,
1864 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1865
1866 EXPECT_EQ(rc, PLDM_SUCCESS);
1867 EXPECT_EQ(completionCode, PLDM_SUCCESS);
1868 EXPECT_EQ(compCompatibilityResp, PLDM_CCR_COMP_CANNOT_BE_UPDATED);
1869 EXPECT_EQ(compCompatibilityRespCode, PLDM_CCRC_COMP_INFO_NO_MATCH);
1870 EXPECT_EQ(updateOptionFlagsEnabled.value, noFlags);
1871 EXPECT_EQ(timeBeforeReqFWData, timeBeforeSendingReqFwData0s);
1872
1873 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
1874 updateComponentResponse3{0x00, 0x00, 0x00, 0x80};
1875 auto responseMsg3 =
1876 reinterpret_cast<const pldm_msg*>(updateComponentResponse3.data());
1877
1878 rc = decode_update_component_resp(
1879 responseMsg3, sizeof(pldm_update_component_resp), &completionCode,
1880 &compCompatibilityResp, &compCompatibilityRespCode,
1881 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1882
1883 EXPECT_EQ(rc, PLDM_SUCCESS);
1884 EXPECT_EQ(completionCode, PLDM_FWUP_NOT_IN_UPDATE_MODE);
1885}
1886
1887TEST(UpdateComponent, errorPathDecodeResponse)
1888{
1889 constexpr std::array<uint8_t,
1890 hdrSize + sizeof(pldm_update_component_resp) - 1>
1891 updateComponentResponse1{0x00, 0x00, 0x00, 0x00, 0x01, 0x09,
1892 0x00, 0x00, 0x00, 0x00, 0x00};
1893 auto responseMsg1 =
1894 reinterpret_cast<const pldm_msg*>(updateComponentResponse1.data());
1895
1896 uint8_t completionCode = 0;
1897 uint8_t compCompatibilityResp = 0;
1898 uint8_t compCompatibilityRespCode = 0;
1899 bitfield32_t updateOptionFlagsEnabled{};
1900 uint16_t timeBeforeReqFWData = 0;
1901
1902 auto rc = decode_update_component_resp(
1903 nullptr, sizeof(pldm_update_component_resp) - 1, &completionCode,
1904 &compCompatibilityResp, &compCompatibilityRespCode,
1905 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1906 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1907
1908 rc = decode_update_component_resp(
1909 responseMsg1, sizeof(pldm_update_component_resp) - 1, nullptr,
1910 &compCompatibilityResp, &compCompatibilityRespCode,
1911 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1912 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1913
1914 rc = decode_update_component_resp(
1915 responseMsg1, sizeof(pldm_update_component_resp) - 1, &completionCode,
1916 nullptr, &compCompatibilityRespCode, &updateOptionFlagsEnabled,
1917 &timeBeforeReqFWData);
1918 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1919
1920 rc = decode_update_component_resp(
1921 responseMsg1, sizeof(pldm_update_component_resp) - 1, &completionCode,
1922 &compCompatibilityResp, nullptr, &updateOptionFlagsEnabled,
1923 &timeBeforeReqFWData);
1924 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1925
1926 rc = decode_update_component_resp(
1927 responseMsg1, sizeof(pldm_update_component_resp) - 1, &completionCode,
1928 &compCompatibilityResp, &compCompatibilityRespCode, nullptr,
1929 &timeBeforeReqFWData);
1930 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1931
1932 rc = decode_update_component_resp(
1933 responseMsg1, sizeof(pldm_update_component_resp) - 1, &completionCode,
1934 &compCompatibilityResp, &compCompatibilityRespCode,
1935 &updateOptionFlagsEnabled, nullptr);
1936 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1937
1938 rc = decode_update_component_resp(
1939 responseMsg1, 0, &completionCode, &compCompatibilityResp,
1940 &compCompatibilityRespCode, &updateOptionFlagsEnabled,
1941 &timeBeforeReqFWData);
1942 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1943
1944 rc = decode_update_component_resp(
1945 responseMsg1, sizeof(pldm_update_component_resp) - 1, &completionCode,
1946 &compCompatibilityResp, &compCompatibilityRespCode,
1947 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1948 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1949
1950 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
1951 updateComponentResponse2{0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
1952 0x01, 0x00, 0x00, 0x00, 0x64, 0x00};
1953 auto responseMsg2 =
1954 reinterpret_cast<const pldm_msg*>(updateComponentResponse2.data());
1955 rc = decode_update_component_resp(
1956 responseMsg2, sizeof(pldm_update_component_resp), &completionCode,
1957 &compCompatibilityResp, &compCompatibilityRespCode,
1958 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1959 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1960
1961 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001962 updateComponentResponse3{0x00, 0x00, 0x00, 0x00, 0x00, 0x0c,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301963 0x01, 0x00, 0x00, 0x00, 0x64, 0x00};
1964 auto responseMsg3 =
1965 reinterpret_cast<const pldm_msg*>(updateComponentResponse3.data());
1966 rc = decode_update_component_resp(
1967 responseMsg3, sizeof(pldm_update_component_resp), &completionCode,
1968 &compCompatibilityResp, &compCompatibilityRespCode,
1969 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1970 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1971
1972 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_update_component_resp)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001973 updateComponentResponse4{0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301974 0x01, 0x00, 0x00, 0x00, 0x64, 0x00};
1975 auto responseMsg4 =
1976 reinterpret_cast<const pldm_msg*>(updateComponentResponse4.data());
1977 rc = decode_update_component_resp(
1978 responseMsg4, sizeof(pldm_update_component_resp), &completionCode,
1979 &compCompatibilityResp, &compCompatibilityRespCode,
1980 &updateOptionFlagsEnabled, &timeBeforeReqFWData);
1981 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1982}
1983
1984TEST(RequestFirmwareData, goodPathDecodeRequest)
1985{
1986 constexpr uint32_t offset = 300;
1987 constexpr uint32_t length = 255;
1988 constexpr std::array<uint8_t,
1989 hdrSize + sizeof(pldm_request_firmware_data_req)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001990 reqFWDataReq{0x00, 0x00, 0x00, 0x2c, 0x01, 0x00,
1991 0x00, 0xff, 0x00, 0x00, 0x00};
Andrew Jeffery9c766792022-08-10 23:12:49 +09301992 auto requestMsg = reinterpret_cast<const pldm_msg*>(reqFWDataReq.data());
1993
1994 uint32_t outOffset = 0;
1995 uint32_t outLength = 0;
1996 auto rc = decode_request_firmware_data_req(
1997 requestMsg, sizeof(pldm_request_firmware_data_req), &outOffset,
1998 &outLength);
1999
2000 EXPECT_EQ(rc, PLDM_SUCCESS);
2001 EXPECT_EQ(outOffset, offset);
2002 EXPECT_EQ(outLength, length);
2003}
2004
2005TEST(RequestFirmwareData, errorPathDecodeRequest)
2006{
2007 constexpr std::array<uint8_t,
2008 hdrSize + sizeof(pldm_request_firmware_data_req)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002009 reqFWDataReq{0x00, 0x00, 0x00, 0x2c, 0x01, 0x00,
2010 0x00, 0x1f, 0x00, 0x00, 0x00};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302011 auto requestMsg = reinterpret_cast<const pldm_msg*>(reqFWDataReq.data());
2012
2013 uint32_t outOffset = 0;
2014 uint32_t outLength = 0;
2015 auto rc = decode_request_firmware_data_req(
2016 nullptr, sizeof(pldm_request_firmware_data_req), &outOffset,
2017 &outLength);
2018 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2019
2020 rc = decode_request_firmware_data_req(
2021 requestMsg, sizeof(pldm_request_firmware_data_req), nullptr,
2022 &outLength);
2023 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2024
2025 rc = decode_request_firmware_data_req(
2026 requestMsg, sizeof(pldm_request_firmware_data_req), &outOffset,
2027 nullptr);
2028 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2029
2030 rc = decode_request_firmware_data_req(
2031 requestMsg, sizeof(pldm_request_firmware_data_req) - 1, &outOffset,
2032 &outLength);
2033 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2034
2035 rc = decode_request_firmware_data_req(
2036 requestMsg, sizeof(pldm_request_firmware_data_req), &outOffset,
2037 &outLength);
2038 EXPECT_EQ(rc, PLDM_FWUP_INVALID_TRANSFER_LENGTH);
2039}
2040
2041TEST(RequestFirmwareData, goodPathEncodeResponse)
2042{
2043 constexpr uint8_t instanceId = 3;
2044 constexpr uint8_t completionCode = PLDM_SUCCESS;
2045 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode) +
2046 PLDM_FWUP_BASELINE_TRANSFER_SIZE>
2047 outReqFwDataResponse1{0x03, 0x05, 0x15, 0x00, 0x01, 0x02, 0x03, 0x04,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002048 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
2049 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
2050 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
2051 0x1d, 0x1e, 0x1f, 0x20};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302052 std::array<uint8_t, hdrSize + sizeof(completionCode) +
2053 PLDM_FWUP_BASELINE_TRANSFER_SIZE>
2054 reqFwDataResponse1{0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002055 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
2056 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
2057 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
2058 0x1d, 0x1e, 0x1f, 0x20};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302059 auto responseMsg1 = reinterpret_cast<pldm_msg*>(reqFwDataResponse1.data());
2060 auto rc = encode_request_firmware_data_resp(
2061 instanceId, completionCode, responseMsg1,
2062 sizeof(completionCode) + PLDM_FWUP_BASELINE_TRANSFER_SIZE);
2063 EXPECT_EQ(rc, PLDM_SUCCESS);
2064 EXPECT_EQ(reqFwDataResponse1, outReqFwDataResponse1);
2065
2066 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2067 outReqFwDataResponse2{0x03, 0x05, 0x15, 0x82};
2068 std::array<uint8_t, hdrSize + sizeof(completionCode)> reqFwDataResponse2{
2069 0x00, 0x00, 0x00, 0x00};
2070 auto responseMsg2 = reinterpret_cast<pldm_msg*>(reqFwDataResponse2.data());
2071 rc = encode_request_firmware_data_resp(
2072 instanceId, PLDM_FWUP_DATA_OUT_OF_RANGE, responseMsg2,
2073 sizeof(completionCode));
2074 EXPECT_EQ(rc, PLDM_SUCCESS);
2075 EXPECT_EQ(reqFwDataResponse2, outReqFwDataResponse2);
2076}
2077
2078TEST(RequestFirmwareData, errorPathEncodeResponse)
2079{
2080 std::array<uint8_t, hdrSize> reqFwDataResponse{0x00, 0x00, 0x00};
2081 auto responseMsg = reinterpret_cast<pldm_msg*>(reqFwDataResponse.data());
2082 auto rc = encode_request_firmware_data_resp(0, PLDM_SUCCESS, nullptr, 0);
2083 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2084
2085 rc = encode_request_firmware_data_resp(0, PLDM_SUCCESS, responseMsg, 0);
2086 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2087}
2088
2089TEST(TransferComplete, goodPathDecodeRequest)
2090{
2091 constexpr uint8_t transferResult = PLDM_FWUP_TRANSFER_SUCCESS;
2092 constexpr std::array<uint8_t, hdrSize + sizeof(transferResult)>
2093 transferCompleteReq1{0x00, 0x00, 0x00, 0x00};
2094 auto requestMsg1 =
2095 reinterpret_cast<const pldm_msg*>(transferCompleteReq1.data());
2096 uint8_t outTransferResult = 0;
2097
2098 auto rc = decode_transfer_complete_req(requestMsg1, sizeof(transferResult),
2099 &outTransferResult);
2100 EXPECT_EQ(rc, PLDM_SUCCESS);
2101 EXPECT_EQ(outTransferResult, transferResult);
2102
2103 constexpr std::array<uint8_t, hdrSize + sizeof(transferResult)>
2104 transferCompleteReq2{0x00, 0x00, 0x00, 0x02};
2105 auto requestMsg2 =
2106 reinterpret_cast<const pldm_msg*>(transferCompleteReq2.data());
2107 rc = decode_transfer_complete_req(requestMsg2, sizeof(transferResult),
2108 &outTransferResult);
2109 EXPECT_EQ(rc, PLDM_SUCCESS);
2110 EXPECT_EQ(outTransferResult, PLDM_FWUP_TRANSFER_ERROR_IMAGE_CORRUPT);
2111}
2112
2113TEST(TransferComplete, errorPathDecodeRequest)
2114{
2115 constexpr std::array<uint8_t, hdrSize> transferCompleteReq{0x00, 0x00,
2116 0x00};
2117 auto requestMsg =
2118 reinterpret_cast<const pldm_msg*>(transferCompleteReq.data());
2119 uint8_t outTransferResult = 0;
2120
2121 auto rc = decode_transfer_complete_req(nullptr, 0, &outTransferResult);
2122 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2123
2124 rc = decode_transfer_complete_req(requestMsg, 0, nullptr);
2125 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2126
2127 rc = decode_transfer_complete_req(requestMsg, 0, &outTransferResult);
2128 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2129}
2130
2131TEST(TransferComplete, goodPathEncodeResponse)
2132{
2133 constexpr uint8_t instanceId = 4;
2134 constexpr uint8_t completionCode = PLDM_SUCCESS;
2135 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2136 outTransferCompleteResponse1{0x04, 0x05, 0x16, 0x00};
2137 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2138 transferCompleteResponse1{0x00, 0x00, 0x00, 0x00};
2139 auto responseMsg1 =
2140 reinterpret_cast<pldm_msg*>(transferCompleteResponse1.data());
2141 auto rc = encode_transfer_complete_resp(
2142 instanceId, completionCode, responseMsg1, sizeof(completionCode));
2143 EXPECT_EQ(rc, PLDM_SUCCESS);
2144 EXPECT_EQ(transferCompleteResponse1, outTransferCompleteResponse1);
2145
2146 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2147 outTransferCompleteResponse2{0x04, 0x05, 0x16, 0x88};
2148 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2149 transferCompleteResponse2{0x00, 0x00, 0x00, 0x00};
2150 auto responseMsg2 =
2151 reinterpret_cast<pldm_msg*>(transferCompleteResponse2.data());
2152 rc = encode_transfer_complete_resp(instanceId,
2153 PLDM_FWUP_COMMAND_NOT_EXPECTED,
2154 responseMsg2, sizeof(completionCode));
2155 EXPECT_EQ(rc, PLDM_SUCCESS);
2156 EXPECT_EQ(transferCompleteResponse2, outTransferCompleteResponse2);
2157}
2158
2159TEST(TransferComplete, errorPathEncodeResponse)
2160{
2161 std::array<uint8_t, hdrSize> transferCompleteResponse{0x00, 0x00, 0x00};
2162 auto responseMsg =
2163 reinterpret_cast<pldm_msg*>(transferCompleteResponse.data());
2164 auto rc = encode_transfer_complete_resp(0, PLDM_SUCCESS, nullptr, 0);
2165 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2166
2167 rc = encode_transfer_complete_resp(0, PLDM_SUCCESS, responseMsg, 0);
2168 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2169}
2170
2171TEST(VerifyComplete, goodPathDecodeRequest)
2172{
2173 constexpr uint8_t verifyResult = PLDM_FWUP_VERIFY_SUCCESS;
2174 constexpr std::array<uint8_t, hdrSize + sizeof(verifyResult)>
2175 verifyCompleteReq1{0x00, 0x00, 0x00, 0x00};
2176 auto requestMsg1 =
2177 reinterpret_cast<const pldm_msg*>(verifyCompleteReq1.data());
2178 uint8_t outVerifyResult = 0;
2179
2180 auto rc = decode_verify_complete_req(requestMsg1, sizeof(verifyResult),
2181 &outVerifyResult);
2182 EXPECT_EQ(rc, PLDM_SUCCESS);
2183 EXPECT_EQ(outVerifyResult, verifyResult);
2184
2185 constexpr std::array<uint8_t, hdrSize + sizeof(verifyResult)>
2186 verifyCompleteReq2{0x00, 0x00, 0x00, 0x03};
2187 auto requestMsg2 =
2188 reinterpret_cast<const pldm_msg*>(verifyCompleteReq2.data());
2189 rc = decode_verify_complete_req(requestMsg2, sizeof(verifyResult),
2190 &outVerifyResult);
2191 EXPECT_EQ(rc, PLDM_SUCCESS);
2192 EXPECT_EQ(outVerifyResult, PLDM_FWUP_VERIFY_FAILED_FD_SECURITY_CHECKS);
2193}
2194
2195TEST(VerifyComplete, errorPathDecodeRequest)
2196{
2197 constexpr std::array<uint8_t, hdrSize> verifyCompleteReq{0x00, 0x00, 0x00};
2198 auto requestMsg =
2199 reinterpret_cast<const pldm_msg*>(verifyCompleteReq.data());
2200 uint8_t outVerifyResult = 0;
2201
2202 auto rc = decode_verify_complete_req(nullptr, 0, &outVerifyResult);
2203 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2204
2205 rc = decode_verify_complete_req(requestMsg, 0, nullptr);
2206 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2207
2208 rc = decode_verify_complete_req(requestMsg, 0, &outVerifyResult);
2209 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2210}
2211
2212TEST(VerifyComplete, goodPathEncodeResponse)
2213{
2214 constexpr uint8_t instanceId = 5;
2215 constexpr uint8_t completionCode = PLDM_SUCCESS;
2216 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2217 outVerifyCompleteResponse1{0x05, 0x05, 0x17, 0x00};
2218 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2219 verifyCompleteResponse1{0x00, 0x00, 0x00, 0x00};
2220 auto responseMsg1 =
2221 reinterpret_cast<pldm_msg*>(verifyCompleteResponse1.data());
2222 auto rc = encode_verify_complete_resp(instanceId, completionCode,
2223 responseMsg1, sizeof(completionCode));
2224 EXPECT_EQ(rc, PLDM_SUCCESS);
2225 EXPECT_EQ(verifyCompleteResponse1, outVerifyCompleteResponse1);
2226
2227 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2228 outVerifyCompleteResponse2{0x05, 0x05, 0x17, 0x88};
2229 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2230 verifyCompleteResponse2{0x00, 0x00, 0x00, 0x00};
2231 auto responseMsg2 =
2232 reinterpret_cast<pldm_msg*>(verifyCompleteResponse2.data());
2233 rc = encode_verify_complete_resp(instanceId, PLDM_FWUP_COMMAND_NOT_EXPECTED,
2234 responseMsg2, sizeof(completionCode));
2235 EXPECT_EQ(rc, PLDM_SUCCESS);
2236 EXPECT_EQ(verifyCompleteResponse2, outVerifyCompleteResponse2);
2237}
2238
2239TEST(VerifyComplete, errorPathEncodeResponse)
2240{
2241 std::array<uint8_t, hdrSize> verifyCompleteResponse{0x00, 0x00, 0x00};
2242 auto responseMsg =
2243 reinterpret_cast<pldm_msg*>(verifyCompleteResponse.data());
2244 auto rc = encode_verify_complete_resp(0, PLDM_SUCCESS, nullptr, 0);
2245 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2246
2247 rc = encode_verify_complete_resp(0, PLDM_SUCCESS, responseMsg, 0);
2248 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2249}
2250
2251TEST(ApplyComplete, goodPathDecodeRequest)
2252{
2253 constexpr uint8_t applyResult1 =
2254 PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD;
2255 // DC power cycle [Bit position 4] & AC power cycle [Bit position 5]
2256 constexpr std::bitset<16> compActivationModification1{0x30};
2257 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_apply_complete_req)>
2258 applyCompleteReq1{0x00, 0x00, 0x00, 0x01, 0x30, 0x00};
2259 auto requestMsg1 =
2260 reinterpret_cast<const pldm_msg*>(applyCompleteReq1.data());
2261 uint8_t outApplyResult = 0;
2262 bitfield16_t outCompActivationModification{};
2263 auto rc = decode_apply_complete_req(
2264 requestMsg1, sizeof(pldm_apply_complete_req), &outApplyResult,
2265 &outCompActivationModification);
2266 EXPECT_EQ(rc, PLDM_SUCCESS);
2267 EXPECT_EQ(outApplyResult, applyResult1);
2268 EXPECT_EQ(outCompActivationModification.value, compActivationModification1);
2269
2270 constexpr uint8_t applyResult2 = PLDM_FWUP_APPLY_SUCCESS;
2271 constexpr std::bitset<16> compActivationModification2{};
2272 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_apply_complete_req)>
2273 applyCompleteReq2{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2274 auto requestMsg2 =
2275 reinterpret_cast<const pldm_msg*>(applyCompleteReq2.data());
2276 rc = decode_apply_complete_req(requestMsg2, sizeof(pldm_apply_complete_req),
2277 &outApplyResult,
2278 &outCompActivationModification);
2279 EXPECT_EQ(rc, PLDM_SUCCESS);
2280 EXPECT_EQ(outApplyResult, applyResult2);
2281 EXPECT_EQ(outCompActivationModification.value, compActivationModification2);
2282}
2283
2284TEST(ApplyComplete, errorPathDecodeRequest)
2285{
2286 constexpr std::array<uint8_t, hdrSize> applyCompleteReq1{0x00, 0x00, 0x00};
2287 auto requestMsg1 =
2288 reinterpret_cast<const pldm_msg*>(applyCompleteReq1.data());
2289 uint8_t outApplyResult = 0;
2290 bitfield16_t outCompActivationModification{};
2291
2292 auto rc = decode_apply_complete_req(
2293 nullptr, sizeof(pldm_apply_complete_req), &outApplyResult,
2294 &outCompActivationModification);
2295 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2296
2297 rc = decode_apply_complete_req(requestMsg1, sizeof(pldm_apply_complete_req),
2298 nullptr, &outCompActivationModification);
2299 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2300
2301 rc = decode_apply_complete_req(requestMsg1, sizeof(pldm_apply_complete_req),
2302 &outApplyResult, nullptr);
2303 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2304
2305 rc = decode_apply_complete_req(requestMsg1, 0, &outApplyResult,
2306 &outCompActivationModification);
2307 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2308
2309 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_apply_complete_req)>
2310 applyCompleteReq2{0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
2311 auto requestMsg2 =
2312 reinterpret_cast<const pldm_msg*>(applyCompleteReq2.data());
2313 rc = decode_apply_complete_req(requestMsg2, sizeof(pldm_apply_complete_req),
2314 &outApplyResult,
2315 &outCompActivationModification);
2316 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2317}
2318
2319TEST(ApplyComplete, goodPathEncodeResponse)
2320{
2321 constexpr uint8_t instanceId = 6;
2322 constexpr uint8_t completionCode = PLDM_SUCCESS;
2323 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2324 outApplyCompleteResponse1{0x06, 0x05, 0x18, 0x00};
2325 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2326 applyCompleteResponse1{0x00, 0x00, 0x00, 0x00};
2327 auto responseMsg1 =
2328 reinterpret_cast<pldm_msg*>(applyCompleteResponse1.data());
2329 auto rc = encode_apply_complete_resp(instanceId, completionCode,
2330 responseMsg1, sizeof(completionCode));
2331 EXPECT_EQ(rc, PLDM_SUCCESS);
2332 EXPECT_EQ(applyCompleteResponse1, outApplyCompleteResponse1);
2333
2334 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2335 outApplyCompleteResponse2{0x06, 0x05, 0x18, 0x88};
2336 std::array<uint8_t, hdrSize + sizeof(completionCode)>
2337 applyCompleteResponse2{0x00, 0x00, 0x00, 0x00};
2338 auto responseMsg2 =
2339 reinterpret_cast<pldm_msg*>(applyCompleteResponse2.data());
2340 rc = encode_apply_complete_resp(instanceId, PLDM_FWUP_COMMAND_NOT_EXPECTED,
2341 responseMsg2, sizeof(completionCode));
2342 EXPECT_EQ(rc, PLDM_SUCCESS);
2343 EXPECT_EQ(applyCompleteResponse2, outApplyCompleteResponse2);
2344}
2345
2346TEST(ApplyComplete, errorPathEncodeResponse)
2347{
2348 std::array<uint8_t, hdrSize> applyCompleteResponse{0x00, 0x00, 0x00};
2349 auto responseMsg =
2350 reinterpret_cast<pldm_msg*>(applyCompleteResponse.data());
2351 auto rc = encode_apply_complete_resp(0, PLDM_SUCCESS, nullptr, 0);
2352 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2353
2354 rc = encode_apply_complete_resp(0, PLDM_SUCCESS, responseMsg, 0);
2355 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2356}
2357
2358TEST(ActivateFirmware, goodPathEncodeRequest)
2359{
2360 constexpr uint8_t instanceId = 7;
2361
2362 std::array<uint8_t, hdrSize + sizeof(pldm_activate_firmware_req)> request{};
2363 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2364
2365 auto rc = encode_activate_firmware_req(
2366 instanceId, PLDM_ACTIVATE_SELF_CONTAINED_COMPONENTS, requestMsg,
2367 sizeof(pldm_activate_firmware_req));
2368 EXPECT_EQ(rc, PLDM_SUCCESS);
2369
2370 std::array<uint8_t, hdrSize + sizeof(pldm_activate_firmware_req)>
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002371 outRequest{0x87, 0x05, 0x1a, 0x01};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302372 EXPECT_EQ(request, outRequest);
2373}
2374
2375TEST(ActivateFirmware, errorPathEncodeRequest)
2376{
2377 std::array<uint8_t, hdrSize + sizeof(pldm_activate_firmware_req)> request{};
2378 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2379
2380 auto rc = encode_activate_firmware_req(
2381 0, PLDM_ACTIVATE_SELF_CONTAINED_COMPONENTS, nullptr,
2382 sizeof(pldm_activate_firmware_req));
2383 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2384
2385 rc = encode_activate_firmware_req(
2386 0, PLDM_ACTIVATE_SELF_CONTAINED_COMPONENTS, requestMsg, 0);
2387 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2388
2389 rc = encode_activate_firmware_req(0, 2, requestMsg,
2390 sizeof(pldm_activate_firmware_req));
2391 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2392}
2393
2394TEST(ActivateFirmware, goodPathDecodeResponse)
2395{
2396 constexpr uint16_t estimatedTimeForActivation100s = 100;
2397 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_activate_firmware_resp)>
2398 activateFirmwareResponse1{0x00, 0x00, 0x00, 0x00, 0x64, 0x00};
2399 auto responseMsg1 =
2400 reinterpret_cast<const pldm_msg*>(activateFirmwareResponse1.data());
2401
2402 uint8_t completionCode = 0;
2403 uint16_t estimatedTimeForActivation = 0;
2404
2405 auto rc = decode_activate_firmware_resp(
2406 responseMsg1, sizeof(pldm_activate_firmware_resp), &completionCode,
2407 &estimatedTimeForActivation);
2408
2409 EXPECT_EQ(rc, PLDM_SUCCESS);
2410 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2411 EXPECT_EQ(estimatedTimeForActivation, estimatedTimeForActivation100s);
2412
2413 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2414 activateFirmwareResponse2{0x00, 0x00, 0x00, 0x85};
2415 auto responseMsg2 =
2416 reinterpret_cast<const pldm_msg*>(activateFirmwareResponse2.data());
2417
2418 rc = decode_activate_firmware_resp(responseMsg2, sizeof(completionCode),
2419 &completionCode,
2420 &estimatedTimeForActivation);
2421
2422 EXPECT_EQ(rc, PLDM_SUCCESS);
2423 EXPECT_EQ(completionCode, PLDM_FWUP_INCOMPLETE_UPDATE);
2424}
2425
2426TEST(ActivateFirmware, errorPathDecodeResponse)
2427{
2428 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_activate_firmware_resp)>
2429 activateFirmwareResponse{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2430 auto responseMsg =
2431 reinterpret_cast<const pldm_msg*>(activateFirmwareResponse.data());
2432
2433 uint8_t completionCode = 0;
2434 uint16_t estimatedTimeForActivation = 0;
2435
2436 auto rc = decode_activate_firmware_resp(
2437 nullptr, sizeof(pldm_activate_firmware_resp), &completionCode,
2438 &estimatedTimeForActivation);
2439 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2440
2441 rc = decode_activate_firmware_resp(responseMsg,
2442 sizeof(pldm_activate_firmware_resp),
2443 nullptr, &estimatedTimeForActivation);
2444 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2445
2446 rc = decode_activate_firmware_resp(responseMsg,
2447 sizeof(pldm_activate_firmware_resp),
2448 &completionCode, nullptr);
2449 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2450
2451 rc = decode_activate_firmware_resp(responseMsg, 0, &completionCode,
2452 &estimatedTimeForActivation);
2453 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2454
2455 rc = decode_activate_firmware_resp(
2456 responseMsg, sizeof(pldm_activate_firmware_resp) - 1, &completionCode,
2457 &estimatedTimeForActivation);
2458 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2459}
2460
2461TEST(GetStatus, goodPathEncodeRequest)
2462{
2463 constexpr uint8_t instanceId = 8;
2464 std::array<uint8_t, hdrSize> request{};
2465 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2466
2467 auto rc = encode_get_status_req(instanceId, requestMsg,
2468 PLDM_GET_STATUS_REQ_BYTES);
2469 EXPECT_EQ(rc, PLDM_SUCCESS);
2470
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002471 constexpr std::array<uint8_t, hdrSize> outRequest{0x88, 0x05, 0x1b};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302472 EXPECT_EQ(request, outRequest);
2473}
2474
2475TEST(GetStatus, errorPathEncodeRequest)
2476{
2477 std::array<uint8_t, hdrSize + sizeof(uint8_t)> request{};
2478 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2479
2480 auto rc = encode_get_status_req(0, nullptr, PLDM_GET_STATUS_REQ_BYTES);
2481 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2482
2483 rc = encode_get_status_req(0, requestMsg, PLDM_GET_STATUS_REQ_BYTES + 1);
2484 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2485}
2486
2487TEST(GetStatus, goodPathDecodeResponse)
2488{
2489 constexpr std::bitset<32> updateOptionFlagsEnabled1{0};
2490 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2491 getStatusResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03,
2492 0x09, 0x65, 0x05, 0x00, 0x00, 0x00, 0x00};
2493 auto responseMsg1 =
2494 reinterpret_cast<const pldm_msg*>(getStatusResponse1.data());
2495
2496 uint8_t completionCode = 0;
2497 uint8_t currentState = 0;
2498 uint8_t previousState = 0;
2499 uint8_t auxState = 0;
2500 uint8_t auxStateStatus = 0;
2501 uint8_t progressPercent = 0;
2502 uint8_t reasonCode = 0;
2503 bitfield32_t updateOptionFlagsEnabled{0};
2504
2505 auto rc = decode_get_status_resp(
2506 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2507 &currentState, &previousState, &auxState, &auxStateStatus,
2508 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2509
2510 EXPECT_EQ(rc, PLDM_SUCCESS);
2511 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2512 EXPECT_EQ(currentState, PLDM_FD_STATE_IDLE);
2513 EXPECT_EQ(previousState, PLDM_FD_STATE_DOWNLOAD);
2514 EXPECT_EQ(auxState, PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER);
2515 EXPECT_EQ(auxStateStatus, PLDM_FD_TIMEOUT);
2516 EXPECT_EQ(progressPercent, PLDM_FWUP_MAX_PROGRESS_PERCENT);
2517 EXPECT_EQ(reasonCode, PLDM_FD_TIMEOUT_DOWNLOAD);
2518 EXPECT_EQ(updateOptionFlagsEnabled.value, updateOptionFlagsEnabled1);
2519
2520 // Bit position 0 - Force update of component – FD will perform a force
2521 // update of the component.
2522 constexpr std::bitset<32> updateOptionFlagsEnabled2{1};
2523 constexpr uint8_t progressPercent2 = 50;
2524 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2525 getStatusResponse2{0x00, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00,
2526 0x70, 0x32, 0x05, 0x01, 0x00, 0x00, 0x00};
2527 auto responseMsg2 =
2528 reinterpret_cast<const pldm_msg*>(getStatusResponse2.data());
2529
2530 rc = decode_get_status_resp(
2531 responseMsg2, getStatusResponse2.size() - hdrSize, &completionCode,
2532 &currentState, &previousState, &auxState, &auxStateStatus,
2533 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2534
2535 EXPECT_EQ(rc, PLDM_SUCCESS);
2536 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2537 EXPECT_EQ(currentState, PLDM_FD_STATE_VERIFY);
2538 EXPECT_EQ(previousState, PLDM_FD_STATE_DOWNLOAD);
2539 EXPECT_EQ(auxState, PLDM_FD_OPERATION_IN_PROGRESS);
2540 EXPECT_EQ(auxStateStatus, PLDM_FD_VENDOR_DEFINED_STATUS_CODE_START);
2541 EXPECT_EQ(progressPercent, progressPercent2);
2542 EXPECT_EQ(reasonCode, PLDM_FD_TIMEOUT_DOWNLOAD);
2543 EXPECT_EQ(updateOptionFlagsEnabled.value, updateOptionFlagsEnabled2);
2544
2545 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2546 getStatusResponse3{0x00, 0x00, 0x00, 0x04};
2547 auto responseMsg3 =
2548 reinterpret_cast<const pldm_msg*>(getStatusResponse3.data());
2549 rc = decode_get_status_resp(
2550 responseMsg3, getStatusResponse3.size() - hdrSize, &completionCode,
2551 &currentState, &previousState, &auxState, &auxStateStatus,
2552 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2553 EXPECT_EQ(rc, PLDM_SUCCESS);
2554 EXPECT_EQ(completionCode, PLDM_ERROR_NOT_READY);
2555}
2556
2557TEST(GetStatus, errorPathDecodeResponse)
2558{
2559 uint8_t completionCode = 0;
2560 uint8_t currentState = 0;
2561 uint8_t previousState = 0;
2562 uint8_t auxState = 0;
2563 uint8_t auxStateStatus = 0;
2564 uint8_t progressPercent = 0;
2565 uint8_t reasonCode = 0;
2566 bitfield32_t updateOptionFlagsEnabled{0};
2567
2568 constexpr std::array<uint8_t, hdrSize> getStatusResponse1{0x00, 0x00, 0x00};
2569 auto responseMsg1 =
2570 reinterpret_cast<const pldm_msg*>(getStatusResponse1.data());
2571
2572 auto rc = decode_get_status_resp(
2573 nullptr, getStatusResponse1.size() - hdrSize, &completionCode,
2574 &currentState, &previousState, &auxState, &auxStateStatus,
2575 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2576 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2577
2578 rc = decode_get_status_resp(
2579 responseMsg1, getStatusResponse1.size() - hdrSize, nullptr,
2580 &currentState, &previousState, &auxState, &auxStateStatus,
2581 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2582 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2583
2584 rc = decode_get_status_resp(
2585 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2586 nullptr, &previousState, &auxState, &auxStateStatus, &progressPercent,
2587 &reasonCode, &updateOptionFlagsEnabled);
2588 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2589
2590 rc = decode_get_status_resp(
2591 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2592 &currentState, nullptr, &auxState, &auxStateStatus, &progressPercent,
2593 &reasonCode, &updateOptionFlagsEnabled);
2594 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2595
2596 rc = decode_get_status_resp(
2597 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2598 &currentState, &previousState, nullptr, &auxStateStatus,
2599 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2600 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2601
2602 rc = decode_get_status_resp(
2603 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2604 &currentState, &previousState, &auxState, nullptr, &progressPercent,
2605 &reasonCode, &updateOptionFlagsEnabled);
2606 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2607
2608 rc = decode_get_status_resp(
2609 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2610 &currentState, &previousState, &auxState, &auxStateStatus, nullptr,
2611 &reasonCode, &updateOptionFlagsEnabled);
2612 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2613
2614 rc = decode_get_status_resp(
2615 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2616 &currentState, &previousState, &auxState, &auxStateStatus,
2617 &progressPercent, nullptr, &updateOptionFlagsEnabled);
2618 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2619
2620 rc = decode_get_status_resp(
2621 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2622 &currentState, &previousState, &auxState, &auxStateStatus,
2623 &progressPercent, &reasonCode, nullptr);
2624 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2625
2626 rc = decode_get_status_resp(
2627 responseMsg1, getStatusResponse1.size() - hdrSize, &completionCode,
2628 &currentState, &previousState, &auxState, &auxStateStatus,
2629 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2630 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2631
2632 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp) - 1>
2633 getStatusResponse2{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2634 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2635 auto responseMsg2 =
2636 reinterpret_cast<const pldm_msg*>(getStatusResponse2.data());
2637 rc = decode_get_status_resp(
2638 responseMsg2, getStatusResponse2.size() - hdrSize, &completionCode,
2639 &currentState, &previousState, &auxState, &auxStateStatus,
2640 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2641 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2642
2643 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2644 getStatusResponse3{0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
2645 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2646 auto responseMsg3 =
2647 reinterpret_cast<const pldm_msg*>(getStatusResponse3.data());
2648 rc = decode_get_status_resp(
2649 responseMsg3, getStatusResponse3.size() - hdrSize, &completionCode,
2650 &currentState, &previousState, &auxState, &auxStateStatus,
2651 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2652 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2653
2654 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2655 getStatusResponse4{0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00,
2656 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2657 auto responseMsg4 =
2658 reinterpret_cast<const pldm_msg*>(getStatusResponse4.data());
2659 rc = decode_get_status_resp(
2660 responseMsg4, getStatusResponse4.size() - hdrSize, &completionCode,
2661 &currentState, &previousState, &auxState, &auxStateStatus,
2662 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2663 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2664
2665 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2666 getStatusResponse5{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
2667 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2668 auto responseMsg5 =
2669 reinterpret_cast<const pldm_msg*>(getStatusResponse5.data());
2670 rc = decode_get_status_resp(
2671 responseMsg5, getStatusResponse5.size() - hdrSize, &completionCode,
2672 &currentState, &previousState, &auxState, &auxStateStatus,
2673 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2674 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2675
2676 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2677 getStatusResponse6{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002678 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302679 auto responseMsg6 =
2680 reinterpret_cast<const pldm_msg*>(getStatusResponse6.data());
2681 rc = decode_get_status_resp(
2682 responseMsg6, getStatusResponse6.size() - hdrSize, &completionCode,
2683 &currentState, &previousState, &auxState, &auxStateStatus,
2684 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2685 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2686
2687 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2688 getStatusResponse7{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2689 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00};
2690 auto responseMsg7 =
2691 reinterpret_cast<const pldm_msg*>(getStatusResponse7.data());
2692 rc = decode_get_status_resp(
2693 responseMsg7, getStatusResponse7.size() - hdrSize, &completionCode,
2694 &currentState, &previousState, &auxState, &auxStateStatus,
2695 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2696 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2697
2698 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2699 getStatusResponse8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002700 0x00, 0x00, 0xc7, 0x00, 0x00, 0x00, 0x00};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302701 auto responseMsg8 =
2702 reinterpret_cast<const pldm_msg*>(getStatusResponse8.data());
2703 rc = decode_get_status_resp(
2704 responseMsg8, getStatusResponse8.size() - hdrSize, &completionCode,
2705 &currentState, &previousState, &auxState, &auxStateStatus,
2706 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2707 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2708
2709 // AuxState is not PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER when the state is
2710 // IDLE
2711 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_get_status_resp)>
2712 getStatusResponse9{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
2713 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2714 auto responseMsg9 =
2715 reinterpret_cast<const pldm_msg*>(getStatusResponse9.data());
2716 rc = decode_get_status_resp(
2717 responseMsg9, getStatusResponse9.size() - hdrSize, &completionCode,
2718 &currentState, &previousState, &auxState, &auxStateStatus,
2719 &progressPercent, &reasonCode, &updateOptionFlagsEnabled);
2720 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2721}
2722
2723TEST(CancelUpdateComponent, goodPathEncodeRequest)
2724{
2725 constexpr uint8_t instanceId = 9;
2726 std::array<uint8_t, hdrSize> request{};
2727 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2728
2729 auto rc = encode_cancel_update_component_req(
2730 instanceId, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);
2731 EXPECT_EQ(rc, PLDM_SUCCESS);
2732
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002733 constexpr std::array<uint8_t, hdrSize> outRequest{0x89, 0x05, 0x1c};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302734 EXPECT_EQ(request, outRequest);
2735}
2736
2737TEST(CancelUpdateComponent, errorPathEncodeRequest)
2738{
2739 std::array<uint8_t, hdrSize + sizeof(uint8_t)> request{};
2740 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2741
2742 auto rc = encode_cancel_update_component_req(
2743 0, nullptr, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);
2744 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2745
2746 rc = encode_cancel_update_component_req(
2747 0, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES + 1);
2748 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2749}
2750
2751TEST(CancelUpdateComponent, testGoodDecodeResponse)
2752{
2753 uint8_t completionCode = 0;
2754 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2755 cancelUpdateComponentResponse1{0x00, 0x00, 0x00, 0x00};
2756 auto responseMsg1 = reinterpret_cast<const pldm_msg*>(
2757 cancelUpdateComponentResponse1.data());
2758 auto rc = decode_cancel_update_component_resp(
2759 responseMsg1, cancelUpdateComponentResponse1.size() - hdrSize,
2760 &completionCode);
2761 EXPECT_EQ(rc, PLDM_SUCCESS);
2762 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2763
2764 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2765 cancelUpdateComponentResponse2{0x00, 0x00, 0x00, 0x86};
2766 auto responseMsg2 = reinterpret_cast<const pldm_msg*>(
2767 cancelUpdateComponentResponse2.data());
2768 rc = decode_cancel_update_component_resp(
2769 responseMsg2, cancelUpdateComponentResponse2.size() - hdrSize,
2770 &completionCode);
2771 EXPECT_EQ(rc, PLDM_SUCCESS);
2772 EXPECT_EQ(completionCode, PLDM_FWUP_BUSY_IN_BACKGROUND);
2773}
2774
2775TEST(CancelUpdateComponent, testBadDecodeResponse)
2776{
2777 uint8_t completionCode = 0;
2778 constexpr std::array<uint8_t, hdrSize> cancelUpdateComponentResponse{
2779 0x00, 0x00, 0x00};
2780 auto responseMsg =
2781 reinterpret_cast<const pldm_msg*>(cancelUpdateComponentResponse.data());
2782
2783 auto rc = decode_cancel_update_component_resp(
2784 nullptr, cancelUpdateComponentResponse.size() - hdrSize,
2785 &completionCode);
2786 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2787
2788 rc = decode_cancel_update_component_resp(
2789 responseMsg, cancelUpdateComponentResponse.size() - hdrSize, nullptr);
2790 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2791
2792 rc = decode_cancel_update_component_resp(
2793 responseMsg, cancelUpdateComponentResponse.size() - hdrSize,
2794 &completionCode);
2795 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2796}
2797
2798TEST(CancelUpdate, goodPathEncodeRequest)
2799{
2800 constexpr uint8_t instanceId = 10;
2801 std::array<uint8_t, hdrSize> request{};
2802 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2803
2804 auto rc = encode_cancel_update_req(instanceId, requestMsg,
2805 PLDM_CANCEL_UPDATE_REQ_BYTES);
2806 EXPECT_EQ(rc, PLDM_SUCCESS);
2807
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002808 constexpr std::array<uint8_t, hdrSize> outRequest{0x8a, 0x05, 0x1d};
Andrew Jeffery9c766792022-08-10 23:12:49 +09302809 EXPECT_EQ(request, outRequest);
2810}
2811
2812TEST(CancelUpdate, errorPathEncodeRequest)
2813{
2814 std::array<uint8_t, hdrSize + sizeof(uint8_t)> request{};
2815 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
2816
2817 auto rc =
2818 encode_cancel_update_req(0, nullptr, PLDM_CANCEL_UPDATE_REQ_BYTES);
2819 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2820
2821 rc = encode_cancel_update_req(0, requestMsg,
2822 PLDM_CANCEL_UPDATE_REQ_BYTES + 1);
2823 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2824}
2825
2826TEST(CancelUpdate, goodPathDecodeResponse)
2827{
2828 constexpr std::bitset<64> nonFunctioningComponentBitmap1{0};
2829 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_cancel_update_resp)>
2830 cancelUpdateResponse1{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2831 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2832 auto responseMsg1 =
2833 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse1.data());
2834 uint8_t completionCode = 0;
2835 bool8_t nonFunctioningComponentIndication = 0;
2836 bitfield64_t nonFunctioningComponentBitmap{0};
2837 auto rc = decode_cancel_update_resp(
2838 responseMsg1, cancelUpdateResponse1.size() - hdrSize, &completionCode,
2839 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2840 EXPECT_EQ(rc, PLDM_SUCCESS);
2841 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2842 EXPECT_EQ(nonFunctioningComponentIndication,
2843 PLDM_FWUP_COMPONENTS_FUNCTIONING);
2844 EXPECT_EQ(nonFunctioningComponentBitmap.value,
2845 nonFunctioningComponentBitmap1);
2846
2847 constexpr std::bitset<64> nonFunctioningComponentBitmap2{0x0101};
2848 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_cancel_update_resp)>
2849 cancelUpdateResponse2{0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
2850 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2851 auto responseMsg2 =
2852 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse2.data());
2853 rc = decode_cancel_update_resp(
2854 responseMsg2, cancelUpdateResponse2.size() - hdrSize, &completionCode,
2855 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2856 EXPECT_EQ(rc, PLDM_SUCCESS);
2857 EXPECT_EQ(completionCode, PLDM_SUCCESS);
2858 EXPECT_EQ(nonFunctioningComponentIndication,
2859 PLDM_FWUP_COMPONENTS_NOT_FUNCTIONING);
2860 EXPECT_EQ(nonFunctioningComponentBitmap.value,
2861 nonFunctioningComponentBitmap2);
2862
2863 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2864 cancelUpdateResponse3{0x00, 0x00, 0x00, 0x86};
2865 auto responseMsg3 =
2866 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse3.data());
2867 rc = decode_cancel_update_resp(
2868 responseMsg3, cancelUpdateResponse3.size() - hdrSize, &completionCode,
2869 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2870 EXPECT_EQ(rc, PLDM_SUCCESS);
2871 EXPECT_EQ(completionCode, PLDM_FWUP_BUSY_IN_BACKGROUND);
2872}
2873
2874TEST(CancelUpdate, errorPathDecodeResponse)
2875{
2876 constexpr std::array<uint8_t, hdrSize> cancelUpdateResponse1{0x00, 0x00,
2877 0x00};
2878 auto responseMsg1 =
2879 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse1.data());
2880 uint8_t completionCode = 0;
2881 bool8_t nonFunctioningComponentIndication = 0;
2882 bitfield64_t nonFunctioningComponentBitmap{0};
2883
2884 auto rc = decode_cancel_update_resp(
2885 nullptr, cancelUpdateResponse1.size() - hdrSize, &completionCode,
2886 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2887 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2888
2889 rc = decode_cancel_update_resp(
2890 responseMsg1, cancelUpdateResponse1.size() - hdrSize, nullptr,
2891 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2892 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2893
2894 rc = decode_cancel_update_resp(
2895 responseMsg1, cancelUpdateResponse1.size() - hdrSize, &completionCode,
2896 nullptr, &nonFunctioningComponentBitmap);
2897 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2898
2899 rc = decode_cancel_update_resp(
2900 responseMsg1, cancelUpdateResponse1.size() - hdrSize, &completionCode,
2901 &nonFunctioningComponentIndication, nullptr);
2902 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2903
2904 rc = decode_cancel_update_resp(
2905 responseMsg1, cancelUpdateResponse1.size() - hdrSize, &completionCode,
2906 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2907 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2908
2909 constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>
2910 cancelUpdateResponse2{0x00, 0x00, 0x00, 0x00};
2911 auto responseMsg2 =
2912 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse2.data());
2913 rc = decode_cancel_update_resp(
2914 responseMsg2, cancelUpdateResponse2.size() - hdrSize, &completionCode,
2915 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2916 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2917
2918 constexpr std::array<uint8_t, hdrSize + sizeof(pldm_cancel_update_resp)>
2919 cancelUpdateResponse3{0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
2920 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2921 auto responseMsg3 =
2922 reinterpret_cast<const pldm_msg*>(cancelUpdateResponse3.data());
2923 rc = decode_cancel_update_resp(
2924 responseMsg3, cancelUpdateResponse3.size() - hdrSize, &completionCode,
2925 &nonFunctioningComponentIndication, &nonFunctioningComponentBitmap);
2926 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2927}