Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 1 | #include "version_handler.hpp" |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 2 | #include "version_mock.hpp" |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 3 | |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 4 | #include <memory> |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 5 | #include <string> |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 6 | #include <unordered_map> |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 10 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 11 | using ::testing::_; |
| 12 | using ::testing::Return; |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 13 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 14 | namespace ipmi_flash |
| 15 | { |
| 16 | |
| 17 | class VersionOpenBlobTest : public ::testing::Test |
| 18 | { |
| 19 | protected: |
| 20 | void SetUp() override |
| 21 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 22 | h = std::make_unique<VersionBlobHandler>( |
| 23 | createMockVersionConfigs(blobNames, &im, &tm)); |
| 24 | for (const auto& blob : blobNames) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 25 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 26 | EXPECT_CALL(*tm.at(blob), status()) |
| 27 | .WillRepeatedly(Return(ActionStatus::unknown)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 28 | } |
| 29 | } |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 30 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 31 | std::unique_ptr<blobs::GenericBlobInterface> h; |
| 32 | std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"}; |
| 33 | std::unordered_map<std::string, TriggerMock*> tm; |
| 34 | std::unordered_map<std::string, ImageHandlerMock*> im; |
| 35 | const std::uint16_t defaultSessionNumber{0}; |
| 36 | }; |
| 37 | |
| 38 | TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen) |
| 39 | { |
| 40 | EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true)); |
| 41 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 42 | } |
| 43 | |
| 44 | TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens) |
| 45 | { |
| 46 | for (const auto& [key, val] : tm) |
| 47 | { |
| 48 | /* set the expectation that every onOpen will be triggered */ |
| 49 | EXPECT_CALL(*val, trigger()).Times(1).WillOnce(Return(true)); |
| 50 | } |
| 51 | int i{defaultSessionNumber}; |
| 52 | for (const auto& blob : blobNames) |
| 53 | { |
| 54 | EXPECT_TRUE(h->open(i++, blobs::read, blob)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose) |
| 59 | { |
| 60 | EXPECT_CALL(*tm.at("blob0"), trigger()) |
| 61 | .Times(2) |
| 62 | .WillRepeatedly(Return(true)); |
| 63 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 64 | EXPECT_TRUE(h->close(defaultSessionNumber)); |
| 65 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 66 | } |
| 67 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 68 | TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails) |
| 69 | { |
| 70 | EXPECT_CALL(*tm.at("blob1"), trigger()) |
| 71 | .Times(1) |
| 72 | .WillRepeatedly(Return(true)); |
| 73 | EXPECT_TRUE(h->open(0, blobs::read, "blob1")); |
| 74 | EXPECT_FALSE(h->open(2, blobs::read, "blob1")); |
| 75 | } |
| 76 | |
| 77 | TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails) |
| 78 | { |
| 79 | EXPECT_CALL(*tm.at("blob1"), trigger()) |
| 80 | .Times(2) |
| 81 | .WillOnce(Return(false)) |
| 82 | .WillOnce(Return(true)); |
| 83 | EXPECT_FALSE(h->open(0, blobs::read, "blob1")); |
| 84 | EXPECT_TRUE(h->open(0, blobs::read, "blob1")); |
| 85 | } |
| 86 | |
| 87 | TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails) |
| 88 | { |
| 89 | EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true)); |
| 90 | EXPECT_FALSE(h->open(0, blobs::write, "blob1")); |
| 91 | EXPECT_TRUE(h->open(0, blobs::read, "blob1")); |
| 92 | } |
| 93 | |
| 94 | } // namespace ipmi_flash |