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:
47     MockFsHandler(std::unique_ptr<FileSystemMock> mock,
48                   const std::string& config = "") :
49         Handler(config)
50     {
51         fsPtr_ = move(mock);
52     }
53 
54   protected:
55     const std::unique_ptr<FileSystemInterface>& getFs() const override
56     {
57         return fsPtr_;
58     }
59 
60   private:
61     std::unique_ptr<FileSystemInterface> fsPtr_;
62 };
63 
64 TEST(BmcModeTransitionTest, DriveCleaningDoneAckFlag)
65 {
66     auto fsMockPtr = std::make_unique<FileSystemMock>();
67     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
68         .WillOnce(Return(true));
69 
70     MockFsHandler h(move(fsMockPtr));
71     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
72 }
73 
74 TEST(BmcModeTransitionTest, DriveCleaningDoneFlag)
75 {
76     auto fsMockPtr = std::make_unique<FileSystemMock>();
77     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
78         .WillOnce(Return(false));
79     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
80         .WillOnce(Return(true));
81     EXPECT_CALL(*fsMockPtr, rename(fs::path(bmDriveCleaningDoneFlagPath),
82                                    fs::path(bmDriveCleaningDoneAckFlagPath), _))
83         .Times(1);
84 
85     MockFsHandler h(move(fsMockPtr));
86     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
87 }
88 
89 TEST(BmcModeTransitionTest, FirstCleaningFlag)
90 {
91     auto fsMockPtr = std::make_unique<FileSystemMock>();
92     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
93         .WillOnce(Return(false));
94     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
95         .WillOnce(Return(false));
96     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
97         .WillOnce(Return(true));
98     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningFlagPath), _))
99         .WillOnce(Return(false));
100     EXPECT_CALL(*fsMockPtr, create(StrEq(bmDriveCleaningFlagPath))).Times(1);
101 
102     MockFsHandler h(move(fsMockPtr));
103     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE));
104 }
105 
106 TEST(BmcModeTransitionTest, NonBmMode)
107 {
108     auto fsMockPtr = std::make_unique<FileSystemMock>();
109     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
110         .WillOnce(Return(false));
111     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
112         .WillOnce(Return(false));
113     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
114         .WillOnce(Return(false));
115     MockFsHandler h(move(fsMockPtr));
116     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::NON_BM_MODE));
117 }
118 
119 } // namespace ipmi
120 } // namespace google
121