blob: 0e80a94d1adf1e96b83cbd5b0db158c7b8df51d8 [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>
William A. Kennington IIIabf17352020-12-22 21:07:11 -08006#include <unordered_map>
Jason Lingc78bfc82020-11-05 18:58:16 -08007#include <vector>
8
9#include <gtest/gtest.h>
William A. Kennington IIIabf17352020-12-22 21:07:11 -080010
Jason Lingc78bfc82020-11-05 18:58:16 -080011using ::testing::_;
12using ::testing::Return;
William A. Kennington IIIabf17352020-12-22 21:07:11 -080013
Jason Lingc78bfc82020-11-05 18:58:16 -080014namespace ipmi_flash
15{
16
17class VersionOpenBlobTest : public ::testing::Test
18{
19 protected:
20 void SetUp() override
21 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080022 h = std::make_unique<VersionBlobHandler>(
23 createMockVersionConfigs(blobNames, &im, &tm));
24 for (const auto& blob : blobNames)
Jason Lingc78bfc82020-11-05 18:58:16 -080025 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080026 EXPECT_CALL(*tm.at(blob), status())
27 .WillRepeatedly(Return(ActionStatus::unknown));
Jason Lingc78bfc82020-11-05 18:58:16 -080028 }
29 }
William A. Kennington IIIabf17352020-12-22 21:07:11 -080030
Jason Lingc78bfc82020-11-05 18:58:16 -080031 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
38TEST_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
44TEST_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
58TEST_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 Lingc78bfc82020-11-05 18:58:16 -080068TEST_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
77TEST_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
87TEST_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