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