blob: 50801c9d36c81d63928649d91dbeee736df55f6d [file] [log] [blame]
Patrick Venture21be45a2018-11-06 12:08:52 -08001#include "firmware_handler.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -07002#include "flags.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08003#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07004#include "triggerable_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07005#include "util.hpp"
Patrick Venture21be45a2018-11-06 12:08:52 -08006
Patrick Venture52854622018-11-06 12:30:00 -08007#include <algorithm>
Patrick Ventured4e20de2019-07-18 12:48:05 -07008#include <memory>
Patrick Venturea78e39f2018-11-06 18:37:06 -08009#include <vector>
Patrick Venture21be45a2018-11-06 12:08:52 -080010
11#include <gtest/gtest.h>
12
Patrick Venture1d5a31c2019-05-20 11:38:22 -070013namespace ipmi_flash
Patrick Venture21be45a2018-11-06 12:08:52 -080014{
Patrick Venture930c7b72019-05-24 11:11:08 -070015namespace
16{
17
18using ::testing::UnorderedElementsAreArray;
Patrick Venture52854622018-11-06 12:30:00 -080019
Patrick Venture16265382019-11-06 19:31:36 -080020TEST(FirmwareHandlerTest, CreateEmptyHandlerListVerifyFails)
Patrick Venture21be45a2018-11-06 12:08:52 -080021{
Patrick Venture1cde5f92018-11-07 08:26:47 -080022 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -070023 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
Patrick Venture1cde5f92018-11-07 08:26:47 -080024 };
25
Patrick Venture4eb55952018-11-16 15:36:24 -080026 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venturefa06a5f2019-07-01 09:22:38 -070027 {}, data, std::move(CreateActionMap("abcd")));
Patrick Venture1cde5f92018-11-07 08:26:47 -080028 EXPECT_EQ(handler, nullptr);
29}
30TEST(FirmwareHandlerTest, CreateEmptyDataHandlerListFails)
31{
32 ImageHandlerMock imageMock;
33
Patrick Ventured4e20de2019-07-18 12:48:05 -070034 std::vector<HandlerPack> blobs;
35 blobs.push_back(std::move(
36 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
37 blobs.push_back(
38 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
Patrick Venture1cde5f92018-11-07 08:26:47 -080039
Patrick Venture4eb55952018-11-16 15:36:24 -080040 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -070041 std::move(blobs), {}, std::move(CreateActionMap("asdf")));
Patrick Venture52854622018-11-06 12:30:00 -080042 EXPECT_EQ(handler, nullptr);
43}
Patrick Venture16265382019-11-06 19:31:36 -080044TEST(FirmwareHandlerTest, CreateEmptyActionPackVerifyFails)
45{
46 /* The ActionPack map corresponds to the firmware list passed in, but
47 * they're not checked against each other yet.
48 */
49 std::vector<DataHandlerPack> data = {
50 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
51 };
52
53 std::vector<HandlerPack> blobs;
54 blobs.push_back(
55 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
56 blobs.push_back(std::move(
57 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
58
59 ActionMap emptyMap;
60
61 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
62 std::move(blobs), data, std::move(emptyMap));
63 EXPECT_EQ(handler, nullptr);
64}
65TEST(FirmwareHandlerTest, FirmwareHandlerListRequiresAtLeastTwoEntries)
66{
67 /* The hashblob handler must be one of the entries, but it cannot be the
68 * only entry.
69 */
70 std::vector<DataHandlerPack> data = {
71 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
72 };
73
74 /* Provide a firmware list that has the hash blob, which is the required one
75 * -- tested in a different test.
76 */
77 std::vector<HandlerPack> blobs;
78 blobs.push_back(std::move(
79 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
80
81 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
82 std::move(blobs), data, std::move(CreateActionMap("asdf")));
83 EXPECT_EQ(handler, nullptr);
84
85 /* Add second firmware and it'll now work. */
86 std::vector<HandlerPack> blobs2;
87 blobs2.push_back(std::move(
88 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
89 blobs2.push_back(
90 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
91
92 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
93 std::move(blobs2), data, std::move(CreateActionMap("asdf")));
94
95 auto result = handler->getBlobIds();
96 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
97 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
98}
Patrick Venture18235e62018-11-08 10:21:09 -080099TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness)
Patrick Venture52854622018-11-06 12:30:00 -0800100{
Patrick Venture1cde5f92018-11-07 08:26:47 -0800101 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700102 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
Patrick Venture1cde5f92018-11-07 08:26:47 -0800103 };
Patrick Venturea78e39f2018-11-06 18:37:06 -0800104
Patrick Ventured4e20de2019-07-18 12:48:05 -0700105 /* This works fine only if you also pass in the hash handler. */
106 std::vector<HandlerPack> blobs;
107 blobs.push_back(
108 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
109
Patrick Venture4eb55952018-11-16 15:36:24 -0800110 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -0700111 std::move(blobs), data, std::move(CreateActionMap("asdf")));
Patrick Venture18235e62018-11-08 10:21:09 -0800112 EXPECT_EQ(handler, nullptr);
113
Patrick Ventured4e20de2019-07-18 12:48:05 -0700114 std::vector<HandlerPack> blobs2;
115 blobs2.push_back(
116 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
117 blobs2.push_back(std::move(
118 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
Patrick Venture18235e62018-11-08 10:21:09 -0800119
Patrick Venture4eb55952018-11-16 15:36:24 -0800120 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -0700121 std::move(blobs2), data, std::move(CreateActionMap("asdf")));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700122
Patrick Venture21be45a2018-11-06 12:08:52 -0800123 auto result = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700124 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
125 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
Patrick Venture21be45a2018-11-06 12:08:52 -0800126}
Patrick Venture930c7b72019-05-24 11:11:08 -0700127
128} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700129} // namespace ipmi_flash