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