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