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 | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 27 | {}, 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 | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 41 | std::move(blobs), {}, std::move(CreateActionMap("asdf"))); |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 42 | EXPECT_EQ(handler, nullptr); |
| 43 | } |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 44 | TEST(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 | } |
| 65 | TEST(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 Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 99 | TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness) |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 100 | { |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 101 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 102 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 103 | }; |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 104 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 105 | /* 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 Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 110 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 111 | std::move(blobs), data, std::move(CreateActionMap("asdf"))); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 112 | EXPECT_EQ(handler, nullptr); |
| 113 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 114 | 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 Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 119 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 120 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 121 | std::move(blobs2), data, std::move(CreateActionMap("asdf"))); |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 122 | |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 123 | auto result = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 124 | std::vector<std::string> expectedBlobs = {"asdf", hashBlobId}; |
| 125 | EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs)); |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 126 | } |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 127 | |
| 128 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 129 | } // namespace ipmi_flash |