blob: 7c7e5d215a76625b768c3b4e3e0805975cf40704 [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
68TEST_F(VersionOpenBlobTest, VerifyDuplicateSessionNumberFails)
69{
70 EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
71 EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
72 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob1"));
73 /* the duplicate session number of 0
74 * should cause a failure for the open of a different blob
75 */
76 EXPECT_FALSE(h->open(defaultSessionNumber, blobs::read, "blob0"));
77 /* open after fail due to seq number works */
78 EXPECT_TRUE(h->open(defaultSessionNumber + 1, blobs::read, "blob0"));
79}
80
81TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
82{
83 EXPECT_CALL(*tm.at("blob1"), trigger())
84 .Times(1)
85 .WillRepeatedly(Return(true));
86 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
87 EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
88}
89
90TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
91{
92 EXPECT_CALL(*tm.at("blob1"), trigger())
93 .Times(2)
94 .WillOnce(Return(false))
95 .WillOnce(Return(true));
96 EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
97 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
98}
99
100TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
101{
102 EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
103 EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
104 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
105}
106
107} // namespace ipmi_flash