blob: bee3d1167a948eb6039ad5bc9dbb80dc4356ca51 [file] [log] [blame]
Jason Lingc78bfc82020-11-05 18:58:16 -08001#include "version_handler.hpp"
William A. Kennington IIIabf17352020-12-22 21:07:11 -08002#include "version_mock.hpp"
Jason Lingc78bfc82020-11-05 18:58:16 -08003
William A. Kennington IIIabf17352020-12-22 21:07:11 -08004#include <memory>
Jason Lingc78bfc82020-11-05 18:58:16 -08005#include <string>
6#include <vector>
7
8#include <gtest/gtest.h>
9using ::testing::_;
10using ::testing::IsEmpty;
11using ::testing::Return;
12namespace ipmi_flash
13{
14
15class VersionReadBlobTest : public ::testing::Test
16{
17 protected:
18 void SetUp() override
19 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080020 h = std::make_unique<VersionBlobHandler>(
21 createMockVersionConfigs(blobNames, &im, &tm));
Jason Lingc78bfc82020-11-05 18:58:16 -080022 }
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
32TEST_F(VersionReadBlobTest, VerifyValidRead)
33{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080034 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080035 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
Jason Lingc78bfc82020-11-05 18:58:16 -080053TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails)
54{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080055 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080056 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080057 .WillOnce(Return(ActionStatus::failed));
Jason Lingc78bfc82020-11-05 18:58:16 -080058 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
59 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
60}
61
62TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
63{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080064 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080065 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080066 .WillOnce(Return(ActionStatus::success));
67 /* file path gets bound to file_handler on creation so path parameter
68 * doesn't actually matter
69 */
William A. Kennington IIIabf17352020-12-22 21:07:11 -080070 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
71 EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt));
Jason Lingc78bfc82020-11-05 18:58:16 -080072 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
73
74 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
75 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
76}
77
78TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
79{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080080 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080081 /* first call to trigger status fails, second succeeds */
82 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080083 .WillOnce(Return(ActionStatus::success));
84 /* file path gets bound to file_handler on creation so path parameter
85 * doesn't actually matter
86 */
William A. Kennington IIIabf17352020-12-22 21:07:11 -080087 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false));
Jason Lingc78bfc82020-11-05 18:58:16 -080088
89 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
90 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
91}
92
93} // namespace ipmi_flash