Jason Ling | 84bff8b | 2020-11-06 13:45:09 -0800 | [diff] [blame^] | 1 | #include "create_action_map.hpp" |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 2 | #include "firmware_handler.hpp" |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 3 | #include "flags.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 4 | #include "image_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 | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 22 | std::vector<DataHandlerPack> data; |
| 23 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 24 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 25 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 26 | {}, std::move(data), CreateActionMap("abcd")); |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 27 | EXPECT_EQ(handler, nullptr); |
| 28 | } |
| 29 | TEST(FirmwareHandlerTest, CreateEmptyDataHandlerListFails) |
| 30 | { |
| 31 | ImageHandlerMock imageMock; |
| 32 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 33 | std::vector<HandlerPack> blobs; |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 34 | blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>()); |
| 35 | blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>()); |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 36 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 37 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 38 | std::move(blobs), std::vector<DataHandlerPack>(), |
| 39 | CreateActionMap("asdf")); |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 40 | EXPECT_EQ(handler, nullptr); |
| 41 | } |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 42 | TEST(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 Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 47 | std::vector<DataHandlerPack> data; |
| 48 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 49 | |
| 50 | std::vector<HandlerPack> blobs; |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 51 | blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>()); |
| 52 | blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>()); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 53 | |
| 54 | ActionMap emptyMap; |
| 55 | |
| 56 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame] | 57 | std::move(blobs), std::move(data), std::move(emptyMap)); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 58 | EXPECT_EQ(handler, nullptr); |
| 59 | } |
| 60 | TEST(FirmwareHandlerTest, FirmwareHandlerListRequiresAtLeastTwoEntries) |
| 61 | { |
| 62 | /* The hashblob handler must be one of the entries, but it cannot be the |
| 63 | * only entry. |
| 64 | */ |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 65 | std::vector<DataHandlerPack> data; |
| 66 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 67 | |
| 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 Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 72 | blobs.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>()); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 73 | |
| 74 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 75 | std::move(blobs), std::move(data), CreateActionMap("asdf")); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 76 | EXPECT_EQ(handler, nullptr); |
| 77 | |
| 78 | /* Add second firmware and it'll now work. */ |
| 79 | std::vector<HandlerPack> blobs2; |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 80 | blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>()); |
| 81 | blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>()); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 82 | |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 83 | std::vector<DataHandlerPack> data2; |
| 84 | data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
| 85 | |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 86 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 87 | std::move(blobs2), std::move(data2), CreateActionMap("asdf")); |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 88 | |
| 89 | auto result = handler->getBlobIds(); |
| 90 | std::vector<std::string> expectedBlobs = {"asdf", hashBlobId}; |
| 91 | EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs)); |
| 92 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 93 | TEST(FirmwareHandlerTest, VerifyHashRequiredForHappiness) |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 94 | { |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 95 | std::vector<DataHandlerPack> data; |
| 96 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 97 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 98 | /* This works fine only if you also pass in the hash handler. */ |
| 99 | std::vector<HandlerPack> blobs; |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 100 | blobs.emplace_back("asdf", std::make_unique<ImageHandlerMock>()); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 101 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 102 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 103 | std::move(blobs), std::move(data), CreateActionMap("asdf")); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 104 | EXPECT_EQ(handler, nullptr); |
| 105 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 106 | std::vector<HandlerPack> blobs2; |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 107 | blobs2.emplace_back("asdf", std::make_unique<ImageHandlerMock>()); |
| 108 | blobs2.emplace_back(hashBlobId, std::make_unique<ImageHandlerMock>()); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 109 | |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 110 | std::vector<DataHandlerPack> data2; |
| 111 | data2.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
| 112 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 113 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame] | 114 | std::move(blobs2), std::move(data2), CreateActionMap("asdf")); |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 115 | |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 116 | auto result = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 117 | std::vector<std::string> expectedBlobs = {"asdf", hashBlobId}; |
| 118 | EXPECT_THAT(result, UnorderedElementsAreArray(expectedBlobs)); |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 119 | } |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 120 | |
| 121 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 122 | } // namespace ipmi_flash |