1 #include "common/utils.hpp"
2 #include "fw-update/device_updater.hpp"
3 #include "fw-update/package_parser.hpp"
4 #include "pldmd/instance_id.hpp"
5 #include "requester/handler.hpp"
6 
7 #include <libpldm/firmware_update.h>
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
12 using namespace pldm;
13 using namespace pldm::fw_update;
14 
15 class DeviceUpdaterTest : public testing::Test
16 {
17   protected:
18     DeviceUpdaterTest() :
19         package("./test_pkg", std::ios::binary | std::ios::in | std::ios::ate)
20     {
21         fwDeviceIDRecord = {
22             1,
23             {0x00},
24             "VersionString2",
25             {{PLDM_FWUP_UUID,
26               std::vector<uint8_t>{0x16, 0x20, 0x23, 0xC9, 0x3E, 0xC5, 0x41,
27                                    0x15, 0x95, 0xF4, 0x48, 0x70, 0x1D, 0x49,
28                                    0xD6, 0x75}}},
29             {}};
30         compImageInfos = {
31             {10, 100, 0xFFFFFFFF, 0, 0, 139, 1024, "VersionString3"}};
32         compInfo = {{std::make_pair(10, 100), 1}};
33     }
34 
35     int fd = -1;
36     std::ifstream package;
37     FirmwareDeviceIDRecord fwDeviceIDRecord;
38     ComponentImageInfos compImageInfos;
39     ComponentInfo compInfo;
40 };
41 
42 TEST_F(DeviceUpdaterTest, validatePackage)
43 {
44     constexpr uintmax_t testPkgSize = 1163;
45     uintmax_t packageSize = package.tellg();
46     EXPECT_EQ(packageSize, testPkgSize);
47 
48     package.seekg(0);
49     std::vector<uint8_t> packageHeader(sizeof(pldm_package_header_information));
50     package.read(reinterpret_cast<char*>(packageHeader.data()),
51                  sizeof(pldm_package_header_information));
52 
53     auto pkgHeaderInfo =
54         reinterpret_cast<const pldm_package_header_information*>(
55             packageHeader.data());
56     auto pkgHeaderInfoSize = sizeof(pldm_package_header_information) +
57                              pkgHeaderInfo->package_version_string_length;
58     packageHeader.clear();
59     packageHeader.resize(pkgHeaderInfoSize);
60     package.seekg(0);
61     package.read(reinterpret_cast<char*>(packageHeader.data()),
62                  pkgHeaderInfoSize);
63 
64     auto parser = parsePkgHeader(packageHeader);
65     EXPECT_NE(parser, nullptr);
66 
67     package.seekg(0);
68     packageHeader.resize(parser->pkgHeaderSize);
69     package.read(reinterpret_cast<char*>(packageHeader.data()),
70                  parser->pkgHeaderSize);
71 
72     parser->parse(packageHeader, packageSize);
73     const auto& fwDeviceIDRecords = parser->getFwDeviceIDRecords();
74     const auto& testPkgCompImageInfos = parser->getComponentImageInfos();
75 
76     EXPECT_EQ(fwDeviceIDRecords.size(), 1);
77     EXPECT_EQ(compImageInfos.size(), 1);
78     EXPECT_EQ(fwDeviceIDRecords[0], fwDeviceIDRecord);
79     EXPECT_EQ(testPkgCompImageInfos, compImageInfos);
80 }
81 
82 TEST_F(DeviceUpdaterTest, ReadPackage512B)
83 {
84     DeviceUpdater deviceUpdater(0, package, fwDeviceIDRecord, compImageInfos,
85                                 compInfo, 512, nullptr);
86 
87     constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) +
88                                       sizeof(pldm_request_firmware_data_req)>
89         reqFwDataReq{0x8A, 0x05, 0x15, 0x00, 0x00, 0x00,
90                      0x00, 0x00, 0x02, 0x00, 0x00};
91     constexpr uint8_t instanceId = 0x0A;
92     constexpr uint8_t completionCode = PLDM_SUCCESS;
93     constexpr uint32_t length = 512;
94     auto requestMsg = reinterpret_cast<const pldm_msg*>(reqFwDataReq.data());
95     auto response = deviceUpdater.requestFwData(
96         requestMsg, sizeof(pldm_request_firmware_data_req));
97 
98     EXPECT_EQ(response.size(),
99               sizeof(pldm_msg_hdr) + sizeof(completionCode) + length);
100     auto responeMsg = reinterpret_cast<const pldm_msg*>(response.data());
101     EXPECT_EQ(responeMsg->hdr.request, PLDM_RESPONSE);
102     EXPECT_EQ(responeMsg->hdr.instance_id, instanceId);
103     EXPECT_EQ(responeMsg->hdr.type, PLDM_FWUP);
104     EXPECT_EQ(responeMsg->hdr.command, PLDM_REQUEST_FIRMWARE_DATA);
105     EXPECT_EQ(response[sizeof(pldm_msg_hdr)], completionCode);
106 
107     const std::vector<uint8_t> compFirst512B{
108         0x0A, 0x05, 0x15, 0x00, 0x48, 0xD2, 0x1E, 0x80, 0x2E, 0x77, 0x71, 0x2C,
109         0x8E, 0xE3, 0x1F, 0x6F, 0x30, 0x76, 0x65, 0x08, 0xB8, 0x1B, 0x4B, 0x03,
110         0x7E, 0x96, 0xD9, 0x2A, 0x36, 0x3A, 0xA2, 0xEE, 0x8A, 0x30, 0x21, 0x33,
111         0xFC, 0x27, 0xE7, 0x3E, 0x56, 0x79, 0x0E, 0xBD, 0xED, 0x44, 0x96, 0x2F,
112         0x84, 0xB5, 0xED, 0x19, 0x3A, 0x5E, 0x62, 0x2A, 0x6E, 0x41, 0x7E, 0xDC,
113         0x2E, 0xBB, 0x87, 0x41, 0x7F, 0xCE, 0xF0, 0xD7, 0xE4, 0x0F, 0x95, 0x33,
114         0x3B, 0xF9, 0x04, 0xF8, 0x1A, 0x92, 0x54, 0xFD, 0x33, 0xBA, 0xCD, 0xA6,
115         0x08, 0x0D, 0x32, 0x2C, 0xEB, 0x75, 0xDC, 0xEA, 0xBA, 0x30, 0x94, 0x78,
116         0x8C, 0x61, 0x58, 0xD0, 0x59, 0xF3, 0x29, 0x6D, 0x67, 0xD3, 0x26, 0x08,
117         0x25, 0x1E, 0x69, 0xBB, 0x28, 0xB0, 0x61, 0xFB, 0x96, 0xA3, 0x8C, 0xBF,
118         0x01, 0x94, 0xEB, 0x3A, 0x63, 0x6F, 0xC8, 0x0F, 0x42, 0x7F, 0xEB, 0x3D,
119         0xA7, 0x8B, 0xE5, 0xD2, 0xFB, 0xB8, 0xD3, 0x15, 0xAA, 0xDF, 0x86, 0xAB,
120         0x6E, 0x29, 0xB3, 0x12, 0x96, 0xB7, 0x86, 0xDA, 0xF9, 0xD7, 0x70, 0xAD,
121         0xB6, 0x1A, 0x29, 0xB1, 0xA4, 0x2B, 0x6F, 0x63, 0xEE, 0x05, 0x9F, 0x35,
122         0x49, 0xA1, 0xAB, 0xA2, 0x6F, 0x7C, 0xFC, 0x23, 0x09, 0x55, 0xED, 0xF7,
123         0x35, 0xD8, 0x2F, 0x8F, 0xD2, 0xBD, 0x77, 0xED, 0x0C, 0x7A, 0xE9, 0xD3,
124         0xF7, 0x90, 0xA7, 0x45, 0x97, 0xAA, 0x3A, 0x79, 0xC4, 0xF8, 0xD2, 0xFE,
125         0xFB, 0xB3, 0x25, 0x86, 0x98, 0x6B, 0x98, 0x10, 0x15, 0xB3, 0xDD, 0x43,
126         0x0B, 0x20, 0x5F, 0xE4, 0x62, 0xC8, 0xA1, 0x3E, 0x9C, 0xF3, 0xD8, 0xEA,
127         0x15, 0xA1, 0x24, 0x94, 0x1C, 0xF5, 0xB4, 0x86, 0x04, 0x30, 0x2C, 0x84,
128         0xB6, 0x29, 0xF6, 0x9D, 0x76, 0x6E, 0xD4, 0x0C, 0x1C, 0xBD, 0xF9, 0x95,
129         0x7E, 0xAF, 0x62, 0x80, 0x14, 0xE6, 0x1C, 0x43, 0x51, 0x5C, 0xCA, 0x50,
130         0xE1, 0x73, 0x3D, 0x75, 0x66, 0x52, 0x9E, 0xB6, 0x15, 0x7E, 0xF7, 0xE5,
131         0xE2, 0xAF, 0x54, 0x75, 0x82, 0x3D, 0x55, 0xC7, 0x59, 0xD7, 0xBD, 0x8C,
132         0x4B, 0x74, 0xD1, 0x3F, 0xA8, 0x1B, 0x0A, 0xF0, 0x5A, 0x32, 0x2B, 0xA7,
133         0xA4, 0xBE, 0x38, 0x18, 0xAE, 0x69, 0xDC, 0x54, 0x7C, 0x60, 0xEF, 0x4F,
134         0x0F, 0x7F, 0x5A, 0xA6, 0xC8, 0x3E, 0x59, 0xFD, 0xF5, 0x98, 0x26, 0x71,
135         0xD0, 0xEF, 0x54, 0x47, 0x38, 0x1F, 0x18, 0x9D, 0x37, 0x9D, 0xF0, 0xCD,
136         0x00, 0x73, 0x30, 0xD4, 0xB7, 0xDA, 0x2D, 0x36, 0xA1, 0xA9, 0xAD, 0x4F,
137         0x9F, 0x17, 0xA5, 0xA1, 0x62, 0x18, 0x21, 0xDD, 0x0E, 0xB6, 0x72, 0xDE,
138         0x17, 0xF0, 0x71, 0x94, 0xA9, 0x67, 0xB4, 0x75, 0xDB, 0x64, 0xF0, 0x6E,
139         0x3D, 0x4E, 0x29, 0x45, 0x42, 0xC3, 0xDA, 0x1F, 0x9E, 0x31, 0x4D, 0x1B,
140         0xA7, 0x9D, 0x07, 0xD9, 0x10, 0x75, 0x27, 0x92, 0x16, 0x35, 0xF5, 0x51,
141         0x3E, 0x14, 0x00, 0xB4, 0xBD, 0x21, 0xAF, 0x90, 0xC5, 0xE5, 0xEE, 0xD0,
142         0xB3, 0x7F, 0x61, 0xA5, 0x1B, 0x91, 0xD5, 0x66, 0x08, 0xB5, 0x16, 0x25,
143         0xC2, 0x16, 0x53, 0xDC, 0xB5, 0xF1, 0xDD, 0xCF, 0x28, 0xDD, 0x57, 0x90,
144         0x66, 0x33, 0x7B, 0x75, 0xF4, 0x8A, 0x19, 0xAC, 0x1F, 0x44, 0xC2, 0xF6,
145         0x21, 0x07, 0xE9, 0xCC, 0xDD, 0xCF, 0x4A, 0x34, 0xA1, 0x24, 0x82, 0xF8,
146         0xA1, 0x1D, 0x06, 0x90, 0x4B, 0x97, 0xB8, 0x10, 0xF2, 0x6A, 0x55, 0x30,
147         0xD9, 0x4F, 0x94, 0xE7, 0x7C, 0xBB, 0x73, 0xA3, 0x5F, 0xC6, 0xF1, 0xDB,
148         0x84, 0x3D, 0x29, 0x72, 0xD1, 0xAD, 0x2D, 0x77, 0x3F, 0x36, 0x24, 0x0F,
149         0xC4, 0x12, 0xD7, 0x3C, 0x65, 0x6C, 0xE1, 0x5A, 0x32, 0xAA, 0x0B, 0xA3,
150         0xA2, 0x72, 0x33, 0x00, 0x3C, 0x7E, 0x28, 0x36, 0x10, 0x90, 0x38, 0xFB};
151     EXPECT_EQ(response, compFirst512B);
152 }
153