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> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | using ::testing::_; |
| 10 | using ::testing::IsEmpty; |
| 11 | using ::testing::Return; |
| 12 | namespace ipmi_flash |
| 13 | { |
| 14 | |
| 15 | class VersionReadBlobTest : public ::testing::Test |
| 16 | { |
| 17 | protected: |
| 18 | void SetUp() override |
| 19 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 20 | h = std::make_unique<VersionBlobHandler>( |
| 21 | createMockVersionConfigs(blobNames, &im, &tm)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 22 | } |
| 23 | std::unique_ptr<blobs::GenericBlobInterface> h; |
| 24 | std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"}; |
| 25 | std::unordered_map<std::string, TriggerMock*> tm; |
| 26 | std::unordered_map<std::string, ImageHandlerMock*> im; |
| 27 | const std::uint16_t defaultSessionNumber{200}; |
| 28 | std::vector<uint8_t> vector1{0xDE, 0xAD, 0xBE, 0xEF, |
| 29 | 0xBA, 0xDF, 0xEE, 0x0D}; |
| 30 | }; |
| 31 | |
| 32 | TEST_F(VersionReadBlobTest, VerifyValidRead) |
| 33 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 34 | EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 35 | EXPECT_CALL(*tm.at("blob0"), status()) |
| 36 | .Times(2) |
| 37 | .WillRepeatedly(Return(ActionStatus::success)); |
| 38 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 39 | /* file path gets bound to file_handler on creation so path parameter |
| 40 | * doesn't actually matter |
| 41 | */ |
| 42 | EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)) |
| 43 | .Times(2) |
| 44 | .WillRepeatedly(Return(true)); |
| 45 | EXPECT_CALL(*im.at("blob0"), read(0, 10)).WillOnce(Return(vector1)); |
| 46 | EXPECT_CALL(*im.at("blob0"), read(2, 10)).WillOnce(Return(vector1)); |
| 47 | EXPECT_CALL(*im.at("blob0"), close()).Times(2); |
| 48 | |
| 49 | EXPECT_EQ(h->read(defaultSessionNumber, 0, 10), vector1); |
| 50 | EXPECT_EQ(h->read(defaultSessionNumber, 2, 10), vector1); |
| 51 | } |
| 52 | |
| 53 | TEST_F(VersionReadBlobTest, VerifyUnopenedReadFails) |
| 54 | { |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 55 | EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty()); |
| 56 | } |
| 57 | |
| 58 | TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails) |
| 59 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 60 | EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 61 | EXPECT_CALL(*tm.at("blob0"), status()) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 62 | .WillOnce(Return(ActionStatus::failed)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 63 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 64 | EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty()); |
| 65 | } |
| 66 | |
| 67 | TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure) |
| 68 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 69 | EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 70 | EXPECT_CALL(*tm.at("blob0"), status()) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 71 | .WillOnce(Return(ActionStatus::success)); |
| 72 | /* file path gets bound to file_handler on creation so path parameter |
| 73 | * doesn't actually matter |
| 74 | */ |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 75 | EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true)); |
| 76 | EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 77 | EXPECT_CALL(*im.at("blob0"), close()).Times(1); |
| 78 | |
| 79 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 80 | EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty()); |
| 81 | } |
| 82 | |
| 83 | TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure) |
| 84 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 85 | EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 86 | /* first call to trigger status fails, second succeeds */ |
| 87 | EXPECT_CALL(*tm.at("blob0"), status()) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 88 | .WillOnce(Return(ActionStatus::success)); |
| 89 | /* file path gets bound to file_handler on creation so path parameter |
| 90 | * doesn't actually matter |
| 91 | */ |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame^] | 92 | EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false)); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 93 | |
| 94 | EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0")); |
| 95 | EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty()); |
| 96 | } |
| 97 | |
| 98 | } // namespace ipmi_flash |