blob: bbbd15a2c38ba06b36e0541511cc005ee365f6f3 [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 Venture7b783432020-09-22 15:55:08 -070027 {}, std::move(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 Venture7b783432020-09-22 15:55:08 -070041 std::move(blobs), std::move(std::vector<DataHandlerPack>()),
42 std::move(CreateActionMap("asdf")));
Patrick Venture52854622018-11-06 12:30:00 -080043 EXPECT_EQ(handler, nullptr);
44}
Patrick Venture16265382019-11-06 19:31:36 -080045TEST(FirmwareHandlerTest, CreateEmptyActionPackVerifyFails)
46{
47 /* The ActionPack map corresponds to the firmware list passed in, but
48 * they're not checked against each other yet.
49 */
50 std::vector<DataHandlerPack> data = {
51 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
52 };
53
54 std::vector<HandlerPack> blobs;
55 blobs.push_back(
56 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
57 blobs.push_back(std::move(
58 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
59
60 ActionMap emptyMap;
61
62 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070063 std::move(blobs), std::move(data), std::move(emptyMap));
Patrick Venture16265382019-11-06 19:31:36 -080064 EXPECT_EQ(handler, nullptr);
65}
66TEST(FirmwareHandlerTest, FirmwareHandlerListRequiresAtLeastTwoEntries)
67{
68 /* The hashblob handler must be one of the entries, but it cannot be the
69 * only entry.
70 */
71 std::vector<DataHandlerPack> data = {
72 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
73 };
74
75 /* Provide a firmware list that has the hash blob, which is the required one
76 * -- tested in a different test.
77 */
78 std::vector<HandlerPack> blobs;
79 blobs.push_back(std::move(
80 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
81
82 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070083 std::move(blobs), std::move(data), std::move(CreateActionMap("asdf")));
Patrick Venture16265382019-11-06 19:31:36 -080084 EXPECT_EQ(handler, nullptr);
85
86 /* Add second firmware and it'll now work. */
87 std::vector<HandlerPack> blobs2;
88 blobs2.push_back(std::move(
89 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
90 blobs2.push_back(
91 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
92
Patrick Venture7b783432020-09-22 15:55:08 -070093 data = {
94 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
95 };
Patrick Venture16265382019-11-06 19:31:36 -080096 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070097 std::move(blobs2), std::move(data), std::move(CreateActionMap("asdf")));
Patrick Venture16265382019-11-06 19:31:36 -080098
99 auto result = handler->getBlobIds();
100 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
101 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
102}
Patrick Venture18235e62018-11-08 10:21:09 -0800103TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness)
Patrick Venture52854622018-11-06 12:30:00 -0800104{
Patrick Venture1cde5f92018-11-07 08:26:47 -0800105 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700106 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
Patrick Venture1cde5f92018-11-07 08:26:47 -0800107 };
Patrick Venturea78e39f2018-11-06 18:37:06 -0800108
Patrick Ventured4e20de2019-07-18 12:48:05 -0700109 /* This works fine only if you also pass in the hash handler. */
110 std::vector<HandlerPack> blobs;
111 blobs.push_back(
112 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
113
Patrick Venture4eb55952018-11-16 15:36:24 -0800114 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -0700115 std::move(blobs), std::move(data), std::move(CreateActionMap("asdf")));
Patrick Venture18235e62018-11-08 10:21:09 -0800116 EXPECT_EQ(handler, nullptr);
117
Patrick Ventured4e20de2019-07-18 12:48:05 -0700118 std::vector<HandlerPack> blobs2;
119 blobs2.push_back(
120 std::move(HandlerPack("asdf", std::make_unique<ImageHandlerMock>())));
121 blobs2.push_back(std::move(
122 HandlerPack(hashBlobId, std::make_unique<ImageHandlerMock>())));
Patrick Venture18235e62018-11-08 10:21:09 -0800123
Patrick Venture7b783432020-09-22 15:55:08 -0700124 data = {
125 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
126 };
Patrick Venture4eb55952018-11-16 15:36:24 -0800127 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -0700128 std::move(blobs2), std::move(data), std::move(CreateActionMap("asdf")));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700129
Patrick Venture21be45a2018-11-06 12:08:52 -0800130 auto result = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700131 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
132 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
Patrick Venture21be45a2018-11-06 12:08:52 -0800133}
Patrick Venture930c7b72019-05-24 11:11:08 -0700134
135} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700136} // namespace ipmi_flash