1 // Copyright 2021 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "log_handler.hpp"
16 #include "log_mock.hpp"
17
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22
23 #include <gtest/gtest.h>
24
25 using ::testing::Return;
26
27 namespace ipmi_flash
28 {
29
30 class LogOpenBlobTest : public ::testing::Test
31 {
32 protected:
SetUp()33 void SetUp() override
34 {
35 h = std::make_unique<LogBlobHandler>(
36 createMockLogConfigs(blobNames, &im, &tm));
37 }
38
39 std::unique_ptr<blobs::GenericBlobInterface> h;
40 std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
41 std::unordered_map<std::string, TriggerMock*> tm;
42 std::unordered_map<std::string, ImageHandlerMock*> im;
43 const std::uint16_t defaultSessionNumber{0};
44 };
45
TEST_F(LogOpenBlobTest,VerifySingleBlobOpen)46 TEST_F(LogOpenBlobTest, VerifySingleBlobOpen)
47 {
48 EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
49 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
50 }
51
TEST_F(LogOpenBlobTest,VerifyMultipleBlobOpens)52 TEST_F(LogOpenBlobTest, VerifyMultipleBlobOpens)
53 {
54 for (const auto& [_, val] : tm)
55 {
56 /* set the expectation that every onOpen will be triggered */
57 EXPECT_CALL(*val, trigger()).WillOnce(Return(true));
58 }
59 int i{defaultSessionNumber};
60 for (const auto& blob : blobNames)
61 {
62 EXPECT_TRUE(h->open(i++, blobs::read, blob));
63 }
64 }
65
TEST_F(LogOpenBlobTest,VerifyOpenAfterClose)66 TEST_F(LogOpenBlobTest, VerifyOpenAfterClose)
67 {
68 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
69 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
70
71 EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
72 EXPECT_TRUE(h->close(defaultSessionNumber));
73
74 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
75 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
76 }
77
TEST_F(LogOpenBlobTest,VerifyMultiOpenWorks)78 TEST_F(LogOpenBlobTest, VerifyMultiOpenWorks)
79 {
80 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
81 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
82 EXPECT_TRUE(h->open(1, blobs::read, "blob1"));
83 EXPECT_TRUE(h->open(2, blobs::read, "blob1"));
84 }
85
TEST_F(LogOpenBlobTest,VerifyFailedTriggerFails)86 TEST_F(LogOpenBlobTest, VerifyFailedTriggerFails)
87 {
88 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(false));
89 EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
90 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
91 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
92 }
93
TEST_F(LogOpenBlobTest,VerifyUnsupportedOpenFlagsFails)94 TEST_F(LogOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
95 {
96 EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
97 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
98 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
99 }
100
101 } // namespace ipmi_flash
102