Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 1 | #include "firmware_handler.hpp" |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 2 | #include "flags.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 3 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 4 | #include "triggerable_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 6 | |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 7 | #include <algorithm> |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 8 | #include <memory> |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 9 | #include <vector> |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 13 | namespace ipmi_flash |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 14 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 19 | |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 20 | TEST(FirmwareHandlerTest, CreateEmptyHandlerListVerifyFails) |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 21 | { |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 22 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 23 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 26 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 27 | {}, std::move(data), std::move(CreateActionMap("abcd"))); |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 28 | EXPECT_EQ(handler, nullptr); |
| 29 | } |
| 30 | TEST(FirmwareHandlerTest, CreateEmptyDataHandlerListFails) |
| 31 | { |
| 32 | ImageHandlerMock imageMock; |
| 33 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 34 | 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 Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 39 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 40 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 41 | std::move(blobs), std::move(std::vector<DataHandlerPack>()), |
| 42 | std::move(CreateActionMap("asdf"))); |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 43 | EXPECT_EQ(handler, nullptr); |
| 44 | } |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 45 | TEST(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 Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 63 | std::move(blobs), std::move(data), std::move(emptyMap)); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 64 | EXPECT_EQ(handler, nullptr); |
| 65 | } |
| 66 | TEST(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 Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 83 | std::move(blobs), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 84 | 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 Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 93 | data = { |
| 94 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
| 95 | }; |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 96 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 97 | std::move(blobs2), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 98 | |
| 99 | auto result = handler->getBlobIds(); |
| 100 | std::vector<std::string> expectedBlobs = {"asdf", hashBlobId}; |
| 101 | EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs)); |
| 102 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 103 | TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness) |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 104 | { |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 105 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 106 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 107 | }; |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 108 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 109 | /* 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 Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 114 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 115 | std::move(blobs), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 116 | EXPECT_EQ(handler, nullptr); |
| 117 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 118 | 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 Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 123 | |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 124 | data = { |
| 125 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
| 126 | }; |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 127 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 128 | std::move(blobs2), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 129 | |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 130 | auto result = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 131 | std::vector<std::string> expectedBlobs = {"asdf", hashBlobId}; |
| 132 | EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs)); |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 133 | } |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 134 | |
| 135 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 136 | } // namespace ipmi_flash |