xref: /openbmc/phosphor-ipmi-flash/bmc/firmware-handler/test/firmware_handler_unittest.cpp (revision 84bff8be387b1755f2c9018957bb8d8d6c8b28e5)
1*84bff8beSJason Ling #include "create_action_map.hpp"
2ded66d0fSJason Ling #include "firmware_handler.hpp"
3ded66d0fSJason Ling #include "flags.hpp"
4ded66d0fSJason Ling #include "image_mock.hpp"
5ded66d0fSJason Ling #include "util.hpp"
6ded66d0fSJason Ling 
7ded66d0fSJason Ling #include <algorithm>
8ded66d0fSJason Ling #include <memory>
9ded66d0fSJason Ling #include <vector>
10ded66d0fSJason Ling 
11ded66d0fSJason Ling #include <gtest/gtest.h>
12ded66d0fSJason Ling 
13ded66d0fSJason Ling namespace ipmi_flash
14ded66d0fSJason Ling {
15ded66d0fSJason Ling namespace
16ded66d0fSJason Ling {
17ded66d0fSJason Ling 
18ded66d0fSJason Ling using ::testing::UnorderedElementsAreArray;
19ded66d0fSJason Ling 
TEST(FirmwareHandlerTest,CreateEmptyHandlerListVerifyFails)20ded66d0fSJason Ling TEST(FirmwareHandlerTest, CreateEmptyHandlerListVerifyFails)
21ded66d0fSJason Ling {
22ded66d0fSJason Ling     std::vector<DataHandlerPack> data;
23ded66d0fSJason Ling     data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
24ded66d0fSJason Ling 
25ded66d0fSJason Ling     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
26ded66d0fSJason Ling         {}, std::move(data), CreateActionMap("abcd"));
27ded66d0fSJason Ling     EXPECT_EQ(handler, nullptr);
28ded66d0fSJason Ling }
TEST(FirmwareHandlerTest,CreateEmptyDataHandlerListFails)29ded66d0fSJason Ling TEST(FirmwareHandlerTest, CreateEmptyDataHandlerListFails)
30ded66d0fSJason Ling {
31ded66d0fSJason Ling     ImageHandlerMock imageMock;
32ded66d0fSJason Ling 
33ded66d0fSJason Ling     std::vector<HandlerPack> blobs;
34ded66d0fSJason Ling     blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
35ded66d0fSJason Ling     blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
36ded66d0fSJason Ling 
37ded66d0fSJason Ling     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
38ded66d0fSJason Ling         std::move(blobs), std::vector<DataHandlerPack>(),
39ded66d0fSJason Ling         CreateActionMap("asdf"));
40ded66d0fSJason Ling     EXPECT_EQ(handler, nullptr);
41ded66d0fSJason Ling }
TEST(FirmwareHandlerTest,CreateEmptyActionPackVerifyFails)42ded66d0fSJason Ling TEST(FirmwareHandlerTest, CreateEmptyActionPackVerifyFails)
43ded66d0fSJason Ling {
44ded66d0fSJason Ling     /* The ActionPack map corresponds to the firmware list passed in, but
45ded66d0fSJason Ling      * they're not checked against each other yet.
46ded66d0fSJason Ling      */
47ded66d0fSJason Ling     std::vector<DataHandlerPack> data;
48ded66d0fSJason Ling     data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
49ded66d0fSJason Ling 
50ded66d0fSJason Ling     std::vector<HandlerPack> blobs;
51ded66d0fSJason Ling     blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
52ded66d0fSJason Ling     blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
53ded66d0fSJason Ling 
54ded66d0fSJason Ling     ActionMap emptyMap;
55ded66d0fSJason Ling 
56ded66d0fSJason Ling     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
57ded66d0fSJason Ling         std::move(blobs), std::move(data), std::move(emptyMap));
58ded66d0fSJason Ling     EXPECT_EQ(handler, nullptr);
59ded66d0fSJason Ling }
TEST(FirmwareHandlerTest,FirmwareHandlerListRequiresAtLeastTwoEntries)60ded66d0fSJason Ling TEST(FirmwareHandlerTest, FirmwareHandlerListRequiresAtLeastTwoEntries)
61ded66d0fSJason Ling {
62ded66d0fSJason Ling     /* The hashblob handler must be one of the entries, but it cannot be the
63ded66d0fSJason Ling      * only entry.
64ded66d0fSJason Ling      */
65ded66d0fSJason Ling     std::vector<DataHandlerPack> data;
66ded66d0fSJason Ling     data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
67ded66d0fSJason Ling 
68ded66d0fSJason Ling     /* Provide a firmware list that has the hash blob, which is the required one
69ded66d0fSJason Ling      * -- tested in a different test.
70ded66d0fSJason Ling      */
71ded66d0fSJason Ling     std::vector<HandlerPack> blobs;
72ded66d0fSJason Ling     blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
73ded66d0fSJason Ling 
74ded66d0fSJason Ling     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
75ded66d0fSJason Ling         std::move(blobs), std::move(data), CreateActionMap("asdf"));
76ded66d0fSJason Ling     EXPECT_EQ(handler, nullptr);
77ded66d0fSJason Ling 
78ded66d0fSJason Ling     /* Add second firmware and it'll now work. */
79ded66d0fSJason Ling     std::vector<HandlerPack> blobs2;
80ded66d0fSJason Ling     blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
81ded66d0fSJason Ling     blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
82ded66d0fSJason Ling 
83ded66d0fSJason Ling     std::vector<DataHandlerPack> data2;
84ded66d0fSJason Ling     data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
85ded66d0fSJason Ling 
86ded66d0fSJason Ling     handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
87ded66d0fSJason Ling         std::move(blobs2), std::move(data2), CreateActionMap("asdf"));
88ded66d0fSJason Ling 
89ded66d0fSJason Ling     auto result = handler->getBlobIds();
90ded66d0fSJason Ling     std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
91ded66d0fSJason Ling     EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
92ded66d0fSJason Ling }
TEST(FirmwareHandlerTest,VerifyHashRequiredForHappiness)93ded66d0fSJason Ling TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness)
94ded66d0fSJason Ling {
95ded66d0fSJason Ling     std::vector<DataHandlerPack> data;
96ded66d0fSJason Ling     data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
97ded66d0fSJason Ling 
98ded66d0fSJason Ling     /* This works fine only if you also pass in the hash handler. */
99ded66d0fSJason Ling     std::vector<HandlerPack> blobs;
100ded66d0fSJason Ling     blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
101ded66d0fSJason Ling 
102ded66d0fSJason Ling     auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
103ded66d0fSJason Ling         std::move(blobs), std::move(data), CreateActionMap("asdf"));
104ded66d0fSJason Ling     EXPECT_EQ(handler, nullptr);
105ded66d0fSJason Ling 
106ded66d0fSJason Ling     std::vector<HandlerPack> blobs2;
107ded66d0fSJason Ling     blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
108ded66d0fSJason Ling     blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
109ded66d0fSJason Ling 
110ded66d0fSJason Ling     std::vector<DataHandlerPack> data2;
111ded66d0fSJason Ling     data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
112ded66d0fSJason Ling 
113ded66d0fSJason Ling     handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
114ded66d0fSJason Ling         std::move(blobs2), std::move(data2), CreateActionMap("asdf"));
115ded66d0fSJason Ling 
116ded66d0fSJason Ling     auto result = handler->getBlobIds();
117ded66d0fSJason Ling     std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
118ded66d0fSJason Ling     EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
119ded66d0fSJason Ling }
120ded66d0fSJason Ling 
121ded66d0fSJason Ling } // namespace
122ded66d0fSJason Ling } // namespace ipmi_flash
123