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::_;
26 using ::testing::Return;
27
28 namespace ipmi_flash
29 {
30
31 class LogStatBlobTest : public ::testing::Test
32 {
33 protected:
SetUp()34 void SetUp() override
35 {
36 h = std::make_unique<LogBlobHandler>(
37 createMockLogConfigs(blobNames, &im, &tm));
38
39 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
40 EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
41
42 blobs::BlobMeta meta;
43 EXPECT_TRUE(h->stat(0, &meta));
44 EXPECT_EQ(blobs::StateFlags::committing, meta.blobState);
45 }
46
47 std::unique_ptr<blobs::GenericBlobInterface> h;
48 std::vector<std::string> blobNames{"blob0"};
49 std::unordered_map<std::string, TriggerMock*> tm;
50 std::unordered_map<std::string, ImageHandlerMock*> im;
51 };
52
TEST_F(LogStatBlobTest,CreateError)53 TEST_F(LogStatBlobTest, CreateError)
54 {
55 EXPECT_CALL(*tm.at("blob0"), status())
56 .WillOnce(Return(ActionStatus::failed));
57 tm.at("blob0")->cb(*tm.at("blob0"));
58
59 blobs::BlobMeta meta;
60 EXPECT_TRUE(h->stat(0, &meta));
61 EXPECT_EQ(blobs::StateFlags::commit_error, meta.blobState);
62 }
63
64 class LogStatSizeBlobTest :
65 public LogStatBlobTest,
66 public ::testing::WithParamInterface<std::vector<uint8_t>>
67 {};
68
TEST_P(LogStatSizeBlobTest,StatWithSize)69 TEST_P(LogStatSizeBlobTest, StatWithSize)
70 {
71 const std::vector<uint8_t> data = GetParam();
72 EXPECT_CALL(*tm.at("blob0"), status())
73 .WillOnce(Return(ActionStatus::success));
74 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
75 EXPECT_CALL(*im.at("blob0"), read(0, ::testing::Ge(data.size())))
76 .WillOnce(Return(data));
77 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
78 tm.at("blob0")->cb(*tm.at("blob0"));
79
80 blobs::BlobMeta meta;
81 EXPECT_TRUE(h->stat(0, &meta));
82 EXPECT_EQ(blobs::StateFlags::committed | blobs::StateFlags::open_read,
83 meta.blobState);
84 EXPECT_EQ(data.size(), meta.size);
85 }
86
87 const std::vector<std::vector<uint8_t>> datas = {
88 {},
89 {0, 1, 2, 3, 4, 5, 6},
90 };
91
92 INSTANTIATE_TEST_SUITE_P(DifferentData, LogStatSizeBlobTest,
93 testing::ValuesIn(datas));
94
95 } // namespace ipmi_flash
96