1 #include "fw-update/package_parser.hpp"
2 
3 #include <typeinfo>
4 
5 #include <gmock/gmock.h>
6 #include <gtest/gtest.h>
7 
8 using namespace pldm::fw_update;
9 
10 TEST(PackageParser, ValidPkgSingleDescriptorSingleComponent)
11 {
12     std::vector<uint8_t> fwPkgHdr{
13         0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43, 0x98, 0x00, 0xA0, 0x2F,
14         0x05, 0x9A, 0xCA, 0x02, 0x01, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15         0x00, 0x00, 0x00, 0x19, 0x0C, 0xE5, 0x07, 0x00, 0x08, 0x00, 0x01, 0x0E,
16         0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
17         0x67, 0x31, 0x01, 0x2E, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0E,
18         0x00, 0x00, 0x01, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74,
19         0x72, 0x69, 0x6E, 0x67, 0x32, 0x02, 0x00, 0x10, 0x00, 0x16, 0x20, 0x23,
20         0xC9, 0x3E, 0xC5, 0x41, 0x15, 0x95, 0xF4, 0x48, 0x70, 0x1D, 0x49, 0xD6,
21         0x75, 0x01, 0x00, 0x0A, 0x00, 0x64, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
22         0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x01,
23         0x0E, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69,
24         0x6E, 0x67, 0x33, 0x4F, 0x96, 0xAE, 0x56};
25 
26     constexpr uintmax_t pkgSize = 166;
27     constexpr std::string_view pkgVersion{"VersionString1"};
28     auto parser = parsePkgHeader(fwPkgHdr);
29     EXPECT_EQ(typeid(*parser).name(), typeid(PackageParserV1).name());
30     EXPECT_EQ(parser->pkgHeaderSize, fwPkgHdr.size());
31     EXPECT_EQ(parser->pkgVersion, pkgVersion);
32 
33     parser->parse(fwPkgHdr, pkgSize);
34     auto outfwDeviceIDRecords = parser->getFwDeviceIDRecords();
35     FirmwareDeviceIDRecords fwDeviceIDRecords{
36         {1,
37          {0},
38          "VersionString2",
39          {{PLDM_FWUP_UUID,
40            std::vector<uint8_t>{0x16, 0x20, 0x23, 0xC9, 0x3E, 0xC5, 0x41, 0x15,
41                                 0x95, 0xF4, 0x48, 0x70, 0x1D, 0x49, 0xD6,
42                                 0x75}}},
43          {}},
44     };
45     EXPECT_EQ(outfwDeviceIDRecords, fwDeviceIDRecords);
46 
47     auto outCompImageInfos = parser->getComponentImageInfos();
48     ComponentImageInfos compImageInfos{
49         {10, 100, 0xFFFFFFFF, 0, 0, 139, 27, "VersionString3"}};
50     EXPECT_EQ(outCompImageInfos, compImageInfos);
51 }
52 
53 TEST(PackageParser, ValidPkgMultipleDescriptorsMultipleComponents)
54 {
55     std::vector<uint8_t> fwPkgHdr{
56         0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43, 0x98, 0x00, 0xA0, 0x2F,
57         0x05, 0x9A, 0xCA, 0x02, 0x01, 0x46, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
58         0x00, 0x00, 0x00, 0x19, 0x0C, 0xE5, 0x07, 0x00, 0x08, 0x00, 0x01, 0x0E,
59         0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
60         0x67, 0x31, 0x03, 0x45, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0E,
61         0x00, 0x00, 0x03, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74,
62         0x72, 0x69, 0x6E, 0x67, 0x32, 0x02, 0x00, 0x10, 0x00, 0x12, 0x44, 0xD2,
63         0x64, 0x8D, 0x7D, 0x47, 0x18, 0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D,
64         0x5B, 0x01, 0x00, 0x04, 0x00, 0x47, 0x16, 0x00, 0x00, 0xFF, 0xFF, 0x0B,
65         0x00, 0x01, 0x07, 0x4F, 0x70, 0x65, 0x6E, 0x42, 0x4D, 0x43, 0x12, 0x34,
66         0x2E, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0E, 0x00, 0x00, 0x07,
67         0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
68         0x67, 0x33, 0x02, 0x00, 0x10, 0x00, 0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D,
69         0x47, 0x18, 0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D, 0x5C, 0x2E, 0x00,
70         0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0E, 0x00, 0x00, 0x01, 0x56, 0x65,
71         0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x34,
72         0x02, 0x00, 0x10, 0x00, 0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D, 0x47, 0x18,
73         0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D, 0x5D, 0x03, 0x00, 0x0A, 0x00,
74         0x64, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x46, 0x01,
75         0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x01, 0x0E, 0x56, 0x65, 0x72, 0x73,
76         0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x35, 0x0A, 0x00,
77         0xC8, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0x00, 0x61, 0x01,
78         0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x01, 0x0E, 0x56, 0x65, 0x72, 0x73,
79         0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x36, 0x10, 0x00,
80         0x2C, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x0C, 0x00, 0x7C, 0x01,
81         0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x01, 0x0E, 0x56, 0x65, 0x72, 0x73,
82         0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x37, 0xF1, 0x90,
83         0x9C, 0x71};
84 
85     constexpr uintmax_t pkgSize = 407;
86     constexpr std::string_view pkgVersion{"VersionString1"};
87     auto parser = parsePkgHeader(fwPkgHdr);
88     EXPECT_EQ(typeid(*parser).name(), typeid(PackageParserV1).name());
89     EXPECT_EQ(parser->pkgHeaderSize, fwPkgHdr.size());
90     EXPECT_EQ(parser->pkgVersion, pkgVersion);
91 
92     parser->parse(fwPkgHdr, pkgSize);
93     auto outfwDeviceIDRecords = parser->getFwDeviceIDRecords();
94     FirmwareDeviceIDRecords fwDeviceIDRecords{
95         {1,
96          {0, 1},
97          "VersionString2",
98          {{PLDM_FWUP_UUID,
99            std::vector<uint8_t>{0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D, 0x47, 0x18,
100                                 0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D,
101                                 0x5B}},
102           {PLDM_FWUP_IANA_ENTERPRISE_ID,
103            std::vector<uint8_t>{0x47, 0x16, 0x00, 0x00}},
104           {PLDM_FWUP_VENDOR_DEFINED,
105            std::make_tuple("OpenBMC", std::vector<uint8_t>{0x12, 0x34})}},
106          {}},
107         {0,
108          {0, 1, 2},
109          "VersionString3",
110          {{PLDM_FWUP_UUID,
111            std::vector<uint8_t>{0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D, 0x47, 0x18,
112                                 0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D,
113                                 0x5C}}},
114          {}},
115         {0,
116          {0},
117          "VersionString4",
118          {{PLDM_FWUP_UUID,
119            std::vector<uint8_t>{0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D, 0x47, 0x18,
120                                 0xA0, 0x30, 0xFC, 0x8A, 0x56, 0x58, 0x7D,
121                                 0x5D}}},
122          {}},
123     };
124     EXPECT_EQ(outfwDeviceIDRecords, fwDeviceIDRecords);
125 
126     auto outCompImageInfos = parser->getComponentImageInfos();
127     ComponentImageInfos compImageInfos{
128         {10, 100, 0xFFFFFFFF, 0, 0, 326, 27, "VersionString5"},
129         {10, 200, 0xFFFFFFFF, 0, 1, 353, 27, "VersionString6"},
130         {16, 300, 0xFFFFFFFF, 1, 12, 380, 27, "VersionString7"}};
131     EXPECT_EQ(outCompImageInfos, compImageInfos);
132 }
133 
134 TEST(PackageParser, InvalidPkgHeaderInfoIncomplete)
135 {
136     std::vector<uint8_t> fwPkgHdr{0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D,
137                                   0x49, 0x43, 0x98, 0x00, 0xA0, 0x2F,
138                                   0x05, 0x9A, 0xCA, 0x02};
139 
140     auto parser = parsePkgHeader(fwPkgHdr);
141     EXPECT_EQ(parser, nullptr);
142 }
143 
144 TEST(PackageParser, InvalidPkgNotSupportedHeaderFormat)
145 {
146     std::vector<uint8_t> fwPkgHdr{
147         0x12, 0x44, 0xD2, 0x64, 0x8D, 0x7D, 0x47, 0x18, 0xA0, 0x30,
148         0xFC, 0x8A, 0x56, 0x58, 0x7D, 0x5B, 0x02, 0x8B, 0x00, 0x00,
149         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0C, 0xE5,
150         0x07, 0x00, 0x08, 0x00, 0x01, 0x0E, 0x56, 0x65, 0x72, 0x73,
151         0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x31};
152 
153     auto parser = parsePkgHeader(fwPkgHdr);
154     EXPECT_EQ(parser, nullptr);
155 }
156 
157 TEST(PackageParser, InvalidPkgBadChecksum)
158 {
159     std::vector<uint8_t> fwPkgHdr{
160         0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43, 0x98, 0x00, 0xA0, 0x2F,
161         0x05, 0x9A, 0xCA, 0x02, 0x01, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162         0x00, 0x00, 0x00, 0x19, 0x0C, 0xE5, 0x07, 0x00, 0x08, 0x00, 0x01, 0x0E,
163         0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69, 0x6E,
164         0x67, 0x31, 0x01, 0x2E, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0E,
165         0x00, 0x00, 0x01, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74,
166         0x72, 0x69, 0x6E, 0x67, 0x32, 0x02, 0x00, 0x10, 0x00, 0x16, 0x20, 0x23,
167         0xC9, 0x3E, 0xC5, 0x41, 0x15, 0x95, 0xF4, 0x48, 0x70, 0x1D, 0x49, 0xD6,
168         0x75, 0x01, 0x00, 0x0A, 0x00, 0x64, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
169         0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x01,
170         0x0E, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x53, 0x74, 0x72, 0x69,
171         0x6E, 0x67, 0x33, 0x4F, 0x96, 0xAE, 0x57};
172 
173     constexpr uintmax_t pkgSize = 166;
174     constexpr std::string_view pkgVersion{"VersionString1"};
175     auto parser = parsePkgHeader(fwPkgHdr);
176     EXPECT_EQ(typeid(*parser).name(), typeid(PackageParserV1).name());
177     EXPECT_EQ(parser->pkgHeaderSize, fwPkgHdr.size());
178     EXPECT_EQ(parser->pkgVersion, pkgVersion);
179     EXPECT_THROW(parser->parse(fwPkgHdr, pkgSize), std::exception);
180 }
181