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