Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
| 4 | |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | using ::testing::Return; |
| 13 | |
| 14 | TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid) |
| 15 | { |
| 16 | /* The client passes write set, and a transport that's supported, and a |
| 17 | * firmware image blob_id that's supported. */ |
| 18 | |
| 19 | ImageHandlerMock imageMock; |
| 20 | |
| 21 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 22 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 23 | {"asdf", &imageMock}, |
| 24 | }; |
| 25 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 26 | {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 30 | |
| 31 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 32 | |
| 33 | EXPECT_TRUE(handler->open( |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 34 | 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 35 | "asdf")); |
| 36 | } |
| 37 | |
| 38 | TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails) |
| 39 | { |
| 40 | /* The image handler for a specific type of image is allowed to return |
| 41 | * failure on open. let's simulate that. */ |
| 42 | |
| 43 | ImageHandlerMock imageMock; |
| 44 | |
| 45 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 46 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 47 | {"asdf", &imageMock}, |
| 48 | }; |
| 49 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 50 | {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 54 | |
| 55 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false)); |
| 56 | |
| 57 | EXPECT_FALSE(handler->open( |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 58 | 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 59 | "asdf")); |
| 60 | } |
| 61 | |
| 62 | TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails) |
| 63 | { |
| 64 | /* The client must set the file write bit. */ |
| 65 | |
| 66 | ImageHandlerMock imageMock; |
| 67 | |
| 68 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 69 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 70 | {"asdf", &imageMock}, |
| 71 | }; |
| 72 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 73 | {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 77 | |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 78 | EXPECT_FALSE(handler->open( |
| 79 | 0, FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit) |
| 83 | { |
| 84 | /* The client sends a request with a transport mechanism not supported. */ |
| 85 | |
| 86 | ImageHandlerMock imageMock; |
| 87 | |
| 88 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 89 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 90 | {"asdf", &imageMock}, |
| 91 | }; |
| 92 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 93 | {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 97 | |
| 98 | EXPECT_FALSE(handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::lpc, |
| 99 | "asdf")); |
| 100 | } |
| 101 | |
| 102 | TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId) |
| 103 | { |
| 104 | /* The client sends a request with an invalid image blob_id. */ |
| 105 | |
| 106 | ImageHandlerMock imageMock; |
| 107 | |
| 108 | std::vector<HandlerPack> blobs = { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 109 | {FirmwareBlobHandler::hashBlobID, &imageMock}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 110 | {"asdf", &imageMock}, |
| 111 | }; |
| 112 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 113 | {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr}, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data); |
| 117 | |
| 118 | EXPECT_FALSE(handler->open( |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 119 | 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 120 | "bcdf")); |
| 121 | } |
| 122 | |
| 123 | /* TODO: The client sends a request during verification. */ |
| 124 | /* TODO: The client sends a second request to open when there is already an open |
| 125 | * file. |
| 126 | */ |
| 127 | /* TODO: The client sends a request to open active image. */ |
| 128 | /* TODO: The client sends a request to open active hash. */ |
| 129 | |
| 130 | } // namespace blobs |