1 // Copyright 2023 Google LLC
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 #include "bm_config.h"
15 
16 #include "bmc_mode_enum.hpp"
17 #include "errors.hpp"
18 #include "file_system_mock.hpp"
19 #include "handler.hpp"
20 #include "handler_impl.hpp"
21 
22 #include <filesystem>
23 #include <fstream>
24 #include <functional>
25 #include <memory>
26 #include <string>
27 
28 #include <gtest/gtest.h>
29 
30 namespace google
31 {
32 namespace ipmi
33 {
34 using ::testing::_;
35 using ::testing::Eq;
36 using ::testing::IsNull;
37 using ::testing::NotNull;
38 using ::testing::Pointee;
39 using ::testing::Return;
40 using ::testing::StrEq;
41 
42 namespace fs = std::filesystem;
43 
44 class MockFsHandler : public Handler
45 {
46   public:
MockFsHandler(std::unique_ptr<FileSystemMock> mock,const std::string & config="")47     MockFsHandler(std::unique_ptr<FileSystemMock> mock,
48                   const std::string& config = "") : Handler(config)
49     {
50         fsPtr_ = move(mock);
51     }
52 
53   protected:
getFs() const54     const std::unique_ptr<FileSystemInterface>& getFs() const override
55     {
56         return fsPtr_;
57     }
58 
59   private:
60     std::unique_ptr<FileSystemInterface> fsPtr_;
61 };
62 
TEST(BmcModeTransitionTest,DriveCleaningDoneAckFlag)63 TEST(BmcModeTransitionTest, DriveCleaningDoneAckFlag)
64 {
65     auto fsMockPtr = std::make_unique<FileSystemMock>();
66     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
67         .WillOnce(Return(true));
68 
69     MockFsHandler h(move(fsMockPtr));
70     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
71 }
72 
TEST(BmcModeTransitionTest,DriveCleaningDoneFlag)73 TEST(BmcModeTransitionTest, DriveCleaningDoneFlag)
74 {
75     auto fsMockPtr = std::make_unique<FileSystemMock>();
76     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
77         .WillOnce(Return(false));
78     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
79         .WillOnce(Return(true));
80     EXPECT_CALL(*fsMockPtr, rename(fs::path(bmDriveCleaningDoneFlagPath),
81                                    fs::path(bmDriveCleaningDoneAckFlagPath), _))
82         .Times(1);
83 
84     MockFsHandler h(move(fsMockPtr));
85     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
86 }
87 
TEST(BmcModeTransitionTest,FirstCleaningFlag)88 TEST(BmcModeTransitionTest, FirstCleaningFlag)
89 {
90     auto fsMockPtr = std::make_unique<FileSystemMock>();
91     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
92         .WillOnce(Return(false));
93     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
94         .WillOnce(Return(false));
95     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
96         .WillOnce(Return(true));
97     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningFlagPath), _))
98         .WillOnce(Return(false));
99     EXPECT_CALL(*fsMockPtr, create(StrEq(bmDriveCleaningFlagPath))).Times(1);
100 
101     MockFsHandler h(move(fsMockPtr));
102     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE));
103 }
104 
TEST(BmcModeTransitionTest,NonBmMode)105 TEST(BmcModeTransitionTest, NonBmMode)
106 {
107     auto fsMockPtr = std::make_unique<FileSystemMock>();
108     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
109         .WillOnce(Return(false));
110     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
111         .WillOnce(Return(false));
112     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
113         .WillOnce(Return(false));
114     MockFsHandler h(move(fsMockPtr));
115     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::NON_BM_MODE));
116 }
117 
118 } // namespace ipmi
119 } // namespace google
120