1 #include "version_handler.hpp"
2 #include "version_mock.hpp"
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 #include <vector>
8 
9 #include <gtest/gtest.h>
10 
11 using ::testing::Return;
12 
13 namespace ipmi_flash
14 {
15 
16 class VersionOpenBlobTest : public ::testing::Test
17 {
18   protected:
19     void SetUp() override
20     {
21         h = std::make_unique<VersionBlobHandler>(
22             createMockVersionConfigs(blobNames, &im, &tm));
23     }
24 
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 {
40     for (const auto& [_, val] : tm)
41     {
42         /* set the expectation that every onOpen will be triggered */
43         EXPECT_CALL(*val, trigger()).WillOnce(Return(true));
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 {
54     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
55     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
56 
57     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
58     EXPECT_TRUE(h->close(defaultSessionNumber));
59 
60     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
61     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
62 }
63 
64 TEST_F(VersionOpenBlobTest, VerifyMultiOpenWorks)
65 {
66     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
67     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
68     EXPECT_TRUE(h->open(1, blobs::read, "blob1"));
69     EXPECT_TRUE(h->open(2, blobs::read, "blob1"));
70 }
71 
72 TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
73 {
74     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(false));
75     EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
76     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
77     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
78 }
79 
80 TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
81 {
82     EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
83     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
84     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
85 }
86 
87 } // namespace ipmi_flash
88