1 #include "libpldm++/firmware_update.hpp"
2 #include "libpldm++/types.hpp"
3
4 #include <expected>
5 #include <span>
6
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9
10 using namespace pldm::fw_update;
11
12 namespace pldm
13 {
14 namespace fw_update
15 {
16
17 #if HAVE_LIBPLDM_API_TESTING
18
19 const std::vector<uint8_t> fwPkgHdrSingleComponent{
20 // clang-format off
21 0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43,
22 0x98, 0x00, 0xA0, 0x2F, 0x05, 0x9A, 0xCA, 0x02, // UUID
23
24 0x01, // pkg header format revision
25 0x8b, 0x00, // pkg header size
26
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28 0x00, 0x00, 0x19, 0x0C, 0xE5, 0x07,
29 0x00, // pkg release date time (13 bytes, timestamp104)
30
31 0x08, 0x00, // component bitmap bit length
32
33 0x01, // package version string type
34
35 0x0E, // package version string length
36
37 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F,
38 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
39 0x67, 0x31, // package version string
40
41 0x01, // device id record count
42
43 0x2E, 0x00, // record 0: record length
44
45 0x01, // record 0: descriptor count
46 0x01, 0x00, 0x00, 0x00, // record 0: device update options flags
47
48 0x01, // record 0: component image set version string type
49 0x0E, // record 0: component image set version string length
50 0x00, 0x00, // record 0: firmware device package data length
51
52 0x01, // applicable components
53
54 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E,
55 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67,
56 0x32, // component image set version string (14 bytes)
57
58 // record descriptors below
59 0x02, 0x00, // record 0: descriptor type: UUID
60
61 0x10, 0x00, // record 0: InitialDescriptorLength (16 bytes)
62
63 0x16, 0x20, 0x23, 0xC9, 0x3E, 0xC5, 0x41,
64 0x15, 0x95, 0xF4, 0x48, 0x70, 0x1D, 0x49,
65 0xD6, 0x75, // record 0: InitialDescriptorData (UUID)
66
67 // firmware device package data (empty here)
68
69 // component image info area
70 0x01, 0x00, // component image count
71
72 0x0A, 0x00, // component classification
73 0x64, 0x00, // component identifier
74 0xFF, 0xFF, 0xFF, 0xFF, // component comparison stamp
75 0x00, 0x00, // component options
76
77 0x00, 0x00, // requested component activation method
78 0x8B, 0x00, 0x00, 0x00, // component location offset
79 0x01, 0x00, 0x00, 0x00, // component size
80 0x01, // component version string type
81 0x0E, // component version string length
82
83 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F,
84 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
85 0x67, 0x33, // component version string
86
87 0x54, 0x9d, 0x7d, 0xe1, // package header checksum
88
89 0x00, // component image
90 // clang-format on
91 };
92
93 #endif
94
95 #if HAVE_LIBPLDM_API_TESTING
96
TEST(PackageParserTest,ValidPkgSingleDescriptorSingleComponent)97 TEST(PackageParserTest, ValidPkgSingleDescriptorSingleComponent)
98 {
99
100 uintmax_t pkgSize = fwPkgHdrSingleComponent.size();
101
102 std::cout << pkgSize << std::endl;
103
104 DEFINE_PLDM_PACKAGE_FORMAT_PIN_FR01H(pin);
105 auto span = std::span<const uint8_t>(fwPkgHdrSingleComponent);
106 auto res = PackageParser::parse(span, pin);
107
108 if (!res.has_value())
109 {
110 std::cout << res.error().msg << std::endl;
111 }
112
113 ASSERT_TRUE(res.has_value());
114
115 auto outfwDeviceIDRecords = res.value()->firmwareDeviceIdRecords;
116
117 std::vector<uint8_t> dd1Data{0x16, 0x20, 0x23, 0xC9, 0x3E, 0xC5,
118 0x41, 0x15, 0x95, 0xF4, 0x48, 0x70,
119 0x1D, 0x49, 0xD6, 0x75};
120
121 ASSERT_EQ(outfwDeviceIDRecords.size(), 1);
122
123 // we cannot compare the applicable components here since their memory
124 // address will be different
125 EXPECT_EQ(outfwDeviceIDRecords[0].deviceUpdateOptionFlags,
126 std::bitset<32>(1));
127 EXPECT_EQ(outfwDeviceIDRecords[0].componentImageSetVersionString,
128 "VersionString2");
129
130 EXPECT_EQ(outfwDeviceIDRecords[0].getDescriptorTypes(),
131 std::vector<uint16_t>({PLDM_FWUP_UUID}));
132
133 // assert for descriptor type PLDM_FWUP_UUID
134 const auto& d1 =
135 outfwDeviceIDRecords[0].recordDescriptors.at(PLDM_FWUP_UUID);
136 EXPECT_EQ(d1->data, dd1Data);
137 EXPECT_EQ(d1->vendorDefinedDescriptorTitle, std::nullopt);
138
139 EXPECT_EQ(outfwDeviceIDRecords[0].firmwareDevicePackageData,
140 std::vector<uint8_t>{});
141
142 auto outCompImageInfos = res.value()->componentImageInformation;
143
144 ASSERT_EQ(outCompImageInfos.size(), 1);
145
146 EXPECT_EQ(outCompImageInfos[0].componentClassification, 10);
147 EXPECT_EQ(outCompImageInfos[0].componentIdentifier, 100);
148 EXPECT_EQ(outCompImageInfos[0].compComparisonStamp, 0xFFFFFFFF);
149 EXPECT_EQ(outCompImageInfos[0].componentOptions, 0);
150 EXPECT_EQ(outCompImageInfos[0].requestedComponentActivationMethod, 0);
151 EXPECT_EQ(outCompImageInfos[0].componentLocation.length, 1);
152 EXPECT_EQ(outCompImageInfos[0].componentVersion, "VersionString3");
153 }
154
155 #endif
156
157 } // namespace fw_update
158 } // namespace pldm
159