blob: 1c3cc588034287423d4599389f7f655b87a1011c [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 Venture4934daa2020-09-22 16:37:44 -070022 std::vector<DataHandlerPack> data;
23 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
Patrick Venture1cde5f92018-11-07 08:26:47 -080024
Patrick Venture4eb55952018-11-16 15:36:24 -080025 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -070026 {}, std::move(data), CreateActionMap("abcd"));
Patrick Venture1cde5f92018-11-07 08:26:47 -080027 EXPECT_EQ(handler, nullptr);
28}
29TEST(FirmwareHandlerTest, CreateEmptyDataHandlerListFails)
30{
31 ImageHandlerMock imageMock;
32
Patrick Ventured4e20de2019-07-18 12:48:05 -070033 std::vector<HandlerPack> blobs;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070034 blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
35 blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
Patrick Venture1cde5f92018-11-07 08:26:47 -080036
Patrick Venture4eb55952018-11-16 15:36:24 -080037 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -070038 std::move(blobs), std::vector<DataHandlerPack>(),
39 CreateActionMap("asdf"));
Patrick Venture52854622018-11-06 12:30:00 -080040 EXPECT_EQ(handler, nullptr);
41}
Patrick Venture16265382019-11-06 19:31:36 -080042TEST(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 */
Patrick Venture4934daa2020-09-22 16:37:44 -070047 std::vector<DataHandlerPack> data;
48 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
Patrick Venture16265382019-11-06 19:31:36 -080049
50 std::vector<HandlerPack> blobs;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070051 blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
52 blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
Patrick Venture16265382019-11-06 19:31:36 -080053
54 ActionMap emptyMap;
55
56 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070057 std::move(blobs), std::move(data), std::move(emptyMap));
Patrick Venture16265382019-11-06 19:31:36 -080058 EXPECT_EQ(handler, nullptr);
59}
60TEST(FirmwareHandlerTest, FirmwareHandlerListRequiresAtLeastTwoEntries)
61{
62 /* The hashblob handler must be one of the entries, but it cannot be the
63 * only entry.
64 */
Patrick Venture4934daa2020-09-22 16:37:44 -070065 std::vector<DataHandlerPack> data;
66 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
Patrick Venture16265382019-11-06 19:31:36 -080067
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;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070072 blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
Patrick Venture16265382019-11-06 19:31:36 -080073
74 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -070075 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture16265382019-11-06 19:31:36 -080076 EXPECT_EQ(handler, nullptr);
77
78 /* Add second firmware and it'll now work. */
79 std::vector<HandlerPack> blobs2;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070080 blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
81 blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
Patrick Venture16265382019-11-06 19:31:36 -080082
Patrick Venture4934daa2020-09-22 16:37:44 -070083 std::vector<DataHandlerPack> data2;
84 data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
85
Patrick Venture16265382019-11-06 19:31:36 -080086 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -070087 std::move(blobs2), std::move(data2), CreateActionMap("asdf"));
Patrick Venture16265382019-11-06 19:31:36 -080088
89 auto result = handler->getBlobIds();
90 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
91 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
92}
Patrick Venture18235e62018-11-08 10:21:09 -080093TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness)
Patrick Venture52854622018-11-06 12:30:00 -080094{
Patrick Venture4934daa2020-09-22 16:37:44 -070095 std::vector<DataHandlerPack> data;
96 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
Patrick Venturea78e39f2018-11-06 18:37:06 -080097
Patrick Ventured4e20de2019-07-18 12:48:05 -070098 /* This works fine only if you also pass in the hash handler. */
99 std::vector<HandlerPack> blobs;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700100 blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
Patrick Ventured4e20de2019-07-18 12:48:05 -0700101
Patrick Venture4eb55952018-11-16 15:36:24 -0800102 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700103 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture18235e62018-11-08 10:21:09 -0800104 EXPECT_EQ(handler, nullptr);
105
Patrick Ventured4e20de2019-07-18 12:48:05 -0700106 std::vector<HandlerPack> blobs2;
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700107 blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>());
108 blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>());
Patrick Venture18235e62018-11-08 10:21:09 -0800109
Patrick Venture4934daa2020-09-22 16:37:44 -0700110 std::vector<DataHandlerPack> data2;
111 data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
112
Patrick Venture4eb55952018-11-16 15:36:24 -0800113 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700114 std::move(blobs2), std::move(data2), CreateActionMap("asdf"));
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700115
Patrick Venture21be45a2018-11-06 12:08:52 -0800116 auto result = handler->getBlobIds();
Patrick Venture930c7b72019-05-24 11:11:08 -0700117 std::vector<std::string> expectedBlobs = {"asdf", hashBlobId};
118 EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs));
Patrick Venture21be45a2018-11-06 12:08:52 -0800119}
Patrick Venture930c7b72019-05-24 11:11:08 -0700120
121} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700122} // namespace ipmi_flash