Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 3 | #include "firmware_unittest.hpp" |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 4 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 5 | #include "triggerable_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 6 | #include "util.hpp" |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 7 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 8 | #include <memory> |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 14 | namespace ipmi_flash |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 15 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 16 | namespace |
| 17 | { |
| 18 | |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 19 | using ::testing::Eq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 20 | using ::testing::Return; |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 21 | using ::testing::StrEq; |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 22 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 23 | class FirmwareHandlerOpenTestIpmiOnly : public IpmiOnlyFirmwareTest |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 24 | { |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 27 | class FirmwareHandlerOpenTestLpc : public FakeLpcFirmwareTest |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 28 | { |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 32 | { |
| 33 | /* The client passes write set, and a transport that's supported, and a |
| 34 | * firmware image blob_id that's supported. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 35 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 36 | |
| 37 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 38 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 39 | "asdf")); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 40 | |
| 41 | /* The active image blob_id was added. */ |
| 42 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 43 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 44 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 45 | activeImageBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 48 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile) |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 49 | { |
| 50 | /* Open the hash file by blob_id. */ |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 51 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 52 | |
| 53 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 54 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 55 | hashBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 56 | |
| 57 | /* The active hash blob_id was added. */ |
| 58 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 59 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 60 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 61 | activeHashBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 64 | TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds) |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 65 | { |
| 66 | /* Attempting to open a file that has an active handler, and use that active |
| 67 | * handler method. |
| 68 | */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 69 | EXPECT_CALL(dataMock, open()).WillOnce(Return(true)); |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 70 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 71 | |
| 72 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 73 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 74 | hashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 75 | |
| 76 | /* The active hash blob_id was added. */ |
| 77 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 78 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 79 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 80 | activeHashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 83 | TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure) |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 84 | { |
| 85 | /* The data handler call returns failure on open, therefore open fails. */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 86 | EXPECT_CALL(dataMock, open()).WillOnce(Return(false)); |
| 87 | |
| 88 | EXPECT_FALSE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 89 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 90 | hashBlobId)); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 91 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 92 | /* The active hash blob_id was not added. */ |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 93 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 94 | EXPECT_EQ(2, currentBlobs.size()); |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 97 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, |
| 98 | OpenEverythingSucceedsVerifyOpenFileCheck) |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 99 | { |
| 100 | /* Verify only one file can be open at a time by opening a file, trying |
| 101 | * again, then closing, and trying again. |
| 102 | */ |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 103 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 104 | |
| 105 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 106 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 107 | "asdf")); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 108 | |
| 109 | /* The active image blob_id was added. */ |
| 110 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 111 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 112 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 113 | activeImageBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 114 | |
| 115 | /* Open the hash file (since we opened an image file). */ |
| 116 | EXPECT_FALSE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 117 | 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 118 | hashBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 119 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 120 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 121 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 122 | EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 123 | |
| 124 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 125 | 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 126 | hashBlobId)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 129 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails) |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 130 | { |
| 131 | /* Attempting to open the active image blob, when it's present will fail. |
| 132 | * |
| 133 | * TODO: We'll need another test that closes first because you can only have |
| 134 | * one file open at a time. |
| 135 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 136 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true)); |
| 137 | |
| 138 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 139 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 140 | "asdf")); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 141 | |
| 142 | /* The active image blob_id was added. */ |
| 143 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 144 | EXPECT_EQ(3, currentBlobs.size()); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 145 | EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(), |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 146 | activeImageBlobId)); |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 147 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 148 | /* Close only active session, to verify it's failing on attempt to open a |
| 149 | * specific blob_id. |
| 150 | */ |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 151 | EXPECT_TRUE(handler->close(0)); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 152 | |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 153 | EXPECT_FALSE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 154 | 1, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 155 | activeImageBlobId)); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 158 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, |
| 159 | OpenWithEverythingValidImageHandlerFails) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 160 | { |
| 161 | /* The image handler for a specific type of image is allowed to return |
| 162 | * failure on open. let's simulate that. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 163 | EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false)); |
| 164 | |
| 165 | EXPECT_FALSE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 166 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 167 | "asdf")); |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 168 | |
| 169 | /* Verify blob_id list doesn't grow. */ |
| 170 | auto currentBlobs = handler->getBlobIds(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 171 | EXPECT_EQ(2, currentBlobs.size()); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 174 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 175 | { |
| 176 | /* The client must set the file write bit. */ |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 177 | EXPECT_FALSE( |
| 178 | handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 181 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 182 | { |
| 183 | /* The client sends a request with a transport mechanism not supported. */ |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 184 | EXPECT_FALSE( |
| 185 | handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Venture | 6f09ff5 | 2019-05-17 18:09:37 -0700 | [diff] [blame] | 188 | TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId) |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 189 | { |
| 190 | /* The client sends a request with an invalid image blob_id. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 191 | EXPECT_FALSE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 192 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 193 | "bcdf")); |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* TODO: The client sends a request during verification. */ |
| 197 | /* TODO: The client sends a second request to open when there is already an open |
| 198 | * file. |
| 199 | */ |
| 200 | /* TODO: The client sends a request to open active image. */ |
| 201 | /* TODO: The client sends a request to open active hash. */ |
| 202 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 203 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 204 | } // namespace ipmi_flash |