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