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 | { |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 12 | using ::testing::_; |
| 13 | using ::testing::IsNull; |
| 14 | using ::testing::NotNull; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 15 | using ::testing::Return; |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 16 | using ::testing::StrEq; |
| 17 | using ::testing::StrictMock; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 18 | |
| 19 | TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage) |
| 20 | { |
| 21 | /* Verify the flash image returns failure on this command. It's a fairly |
| 22 | * artificial test. |
| 23 | */ |
| 24 | ImageHandlerMock imageMock1, imageMock2; |
| 25 | std::vector<HandlerPack> blobs = { |
| 26 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 27 | {"asdf", &imageMock2}, |
| 28 | }; |
| 29 | |
| 30 | std::vector<DataHandlerPack> data = { |
| 31 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 32 | }; |
| 33 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 34 | sdbusplus::SdBusMock sdbus_mock; |
| 35 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 36 | |
| 37 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 38 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 39 | |
| 40 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 41 | |
| 42 | EXPECT_TRUE(handler->open( |
| 43 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 44 | |
| 45 | EXPECT_FALSE(handler->commit(0, {})); |
| 46 | } |
| 47 | |
| 48 | TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile) |
| 49 | { |
| 50 | /* Verify the hash file returns failure on this command. It's a fairly |
| 51 | * artificial test. |
| 52 | */ |
| 53 | ImageHandlerMock imageMock1, imageMock2; |
| 54 | std::vector<HandlerPack> blobs = { |
| 55 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 56 | {"asdf", &imageMock2}, |
| 57 | }; |
| 58 | |
| 59 | std::vector<DataHandlerPack> data = { |
| 60 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 61 | }; |
| 62 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 63 | sdbusplus::SdBusMock sdbus_mock; |
| 64 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 65 | |
| 66 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 67 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 68 | |
| 69 | EXPECT_CALL(imageMock1, open(FirmwareBlobHandler::hashBlobID)) |
| 70 | .WillOnce(Return(true)); |
| 71 | |
| 72 | EXPECT_TRUE(handler->open( |
| 73 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 74 | FirmwareBlobHandler::hashBlobID)); |
| 75 | |
| 76 | EXPECT_FALSE(handler->commit(0, {})); |
| 77 | } |
| 78 | |
| 79 | TEST(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob) |
| 80 | { |
| 81 | /* Verify the verify blob lets you call this command, and it returns |
| 82 | * success. |
| 83 | */ |
| 84 | ImageHandlerMock imageMock1, imageMock2; |
| 85 | std::vector<HandlerPack> blobs = { |
| 86 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 87 | {"asdf", &imageMock2}, |
| 88 | }; |
| 89 | |
| 90 | std::vector<DataHandlerPack> data = { |
| 91 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 92 | }; |
| 93 | |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 94 | sdbusplus::SdBusMock sdbus_mock; |
| 95 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 96 | |
| 97 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 98 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 99 | |
| 100 | EXPECT_TRUE( |
| 101 | handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID)); |
| 102 | |
| 103 | EXPECT_TRUE(handler->commit(0, {})); |
| 104 | } |
| 105 | |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame^] | 106 | TEST(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 107 | { |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame^] | 108 | /* Verify you cannot call the commit() command once verification is |
| 109 | * started, after which it will just return true. |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 110 | */ |
| 111 | ImageHandlerMock imageMock1, imageMock2; |
| 112 | std::vector<HandlerPack> blobs = { |
| 113 | {FirmwareBlobHandler::hashBlobID, &imageMock1}, |
| 114 | {"asdf", &imageMock2}, |
| 115 | }; |
| 116 | |
| 117 | std::vector<DataHandlerPack> data = { |
| 118 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 119 | }; |
| 120 | |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 121 | StrictMock<sdbusplus::SdBusMock> sdbus_mock; |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 122 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 123 | |
| 124 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
| 125 | std::move(bus_mock), blobs, data); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 126 | |
| 127 | EXPECT_TRUE( |
| 128 | handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID)); |
| 129 | |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 130 | /* Note: I used StrictMock<> here to just catch all the calls. However, the |
| 131 | * unit-tests pass if we don't use StrictMock and ignore the calls. |
| 132 | */ |
| 133 | EXPECT_CALL(sdbus_mock, |
| 134 | sd_bus_message_new_method_call( |
| 135 | IsNull(), NotNull(), StrEq("org.freedesktop.systemd1"), |
| 136 | StrEq("/org/freedesktop/systemd1"), |
| 137 | StrEq("org.freedesktop.systemd1.Manager"), |
| 138 | StrEq("StartUnit"))) |
| 139 | .WillOnce(Return(0)); |
| 140 | EXPECT_CALL(sdbus_mock, |
| 141 | sd_bus_message_append_basic(IsNull(), 's', NotNull())) |
| 142 | .Times(2) |
| 143 | .WillRepeatedly(Return(0)); |
| 144 | EXPECT_CALL(sdbus_mock, sd_bus_call(_, _, _, _, _)).WillOnce(Return(0)); |
| 145 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 146 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame^] | 147 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | } // namespace blobs |