Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
| 4 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame^] | 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | using ::testing::Return; |
| 13 | |
| 14 | TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage) |
| 15 | { |
| 16 | /* Verify the flash image returns failure on this command. It's a fairly |
| 17 | * artificial test. |
| 18 | */ |
| 19 | ImageHandlerMock imageMock1, imageMock2; |
| 20 | std::vector<HandlerPack> blobs = { |
| 21 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 22 | {"asdf", &imageMock2}, |
| 23 | }; |
| 24 | |
| 25 | std::vector<DataHandlerPack> data = { |
| 26 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 27 | }; |
| 28 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame^] | 29 | sdbusplus::SdBusMock sdbus_mock; |
| 30 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 31 | |
| 32 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 33 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 34 | |
| 35 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 36 | |
| 37 | EXPECT_TRUE(handler->open( |
| 38 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 39 | |
| 40 | EXPECT_FALSE(handler->commit(0, {})); |
| 41 | } |
| 42 | |
| 43 | TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile) |
| 44 | { |
| 45 | /* Verify the hash file returns failure on this command. It's a fairly |
| 46 | * artificial test. |
| 47 | */ |
| 48 | ImageHandlerMock imageMock1, imageMock2; |
| 49 | std::vector<HandlerPack> blobs = { |
| 50 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 51 | {"asdf", &imageMock2}, |
| 52 | }; |
| 53 | |
| 54 | std::vector<DataHandlerPack> data = { |
| 55 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 56 | }; |
| 57 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame^] | 58 | sdbusplus::SdBusMock sdbus_mock; |
| 59 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 60 | |
| 61 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 62 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 63 | |
| 64 | EXPECT_CALL(imageMock1, open(FirmwareBlobHandler::hashBlobID)) |
| 65 | .WillOnce(Return(true)); |
| 66 | |
| 67 | EXPECT_TRUE(handler->open( |
| 68 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 69 | FirmwareBlobHandler::hashBlobID)); |
| 70 | |
| 71 | EXPECT_FALSE(handler->commit(0, {})); |
| 72 | } |
| 73 | |
| 74 | TEST(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob) |
| 75 | { |
| 76 | /* Verify the verify blob lets you call this command, and it returns |
| 77 | * success. |
| 78 | */ |
| 79 | ImageHandlerMock imageMock1, imageMock2; |
| 80 | std::vector<HandlerPack> blobs = { |
| 81 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 82 | {"asdf", &imageMock2}, |
| 83 | }; |
| 84 | |
| 85 | std::vector<DataHandlerPack> data = { |
| 86 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 87 | }; |
| 88 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame^] | 89 | sdbusplus::SdBusMock sdbus_mock; |
| 90 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 91 | |
| 92 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 93 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 94 | |
| 95 | EXPECT_TRUE( |
| 96 | handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID)); |
| 97 | |
| 98 | EXPECT_TRUE(handler->commit(0, {})); |
| 99 | } |
| 100 | |
| 101 | TEST(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnce) |
| 102 | { |
| 103 | /* Verify you cannot call the commit() command once verification is started. |
| 104 | */ |
| 105 | ImageHandlerMock imageMock1, imageMock2; |
| 106 | std::vector<HandlerPack> blobs = { |
| 107 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 108 | {"asdf", &imageMock2}, |
| 109 | }; |
| 110 | |
| 111 | std::vector<DataHandlerPack> data = { |
| 112 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 113 | }; |
| 114 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame^] | 115 | sdbusplus::SdBusMock sdbus_mock; |
| 116 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 117 | |
| 118 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 119 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 120 | |
| 121 | EXPECT_TRUE( |
| 122 | handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID)); |
| 123 | |
| 124 | EXPECT_TRUE(handler->commit(0, {})); |
| 125 | EXPECT_FALSE(handler->commit(0, {})); |
| 126 | } |
| 127 | |
| 128 | } // namespace blobs |