1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "action.hpp"
17 #include "chassis.hpp"
18 #include "configuration.hpp"
19 #include "device.hpp"
20 #include "i2c_interface.hpp"
21 #include "mock_action.hpp"
22 #include "phase_fault_detection.hpp"
23 #include "presence_detection.hpp"
24 #include "rail.hpp"
25 #include "rule.hpp"
26 
27 #include <filesystem>
28 #include <fstream>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace phosphor::power::regulators::test_utils
35 {
36 
37 namespace fs = std::filesystem;
38 
39 /**
40  * Create an I2CInterface object with hard-coded bus and address values.
41  *
42  * @return I2CInterface object wrapped in a unique_ptr
43  */
44 inline std::unique_ptr<i2c::I2CInterface> createI2CInterface()
45 {
46     return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
47 }
48 
49 /**
50  * Creates a Device object with the specified ID.
51  *
52  * Creates Rail objects within the Device if railIDs is specified.
53  *
54  * @param id device ID
55  * @param railIDs rail IDs (optional)
56  * @return Device object
57  */
58 inline std::unique_ptr<Device>
59     createDevice(const std::string& id,
60                  const std::vector<std::string>& railIDs = {})
61 {
62     // Create Rails (if any)
63     std::vector<std::unique_ptr<Rail>> rails{};
64     for (const std::string& railID : railIDs)
65     {
66         rails.emplace_back(std::make_unique<Rail>(railID));
67     }
68 
69     // Create Device
70     bool isRegulator = true;
71     std::string fru =
72         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1";
73     std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
74     std::unique_ptr<PresenceDetection> presenceDetection{};
75     std::unique_ptr<Configuration> configuration{};
76     std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
77     return std::make_unique<Device>(
78         id, isRegulator, fru, std::move(i2cInterface),
79         std::move(presenceDetection), std::move(configuration),
80         std::move(phaseFaultDetection), std::move(rails));
81 }
82 
83 /**
84  * Creates a Rule object with the specified ID.
85  *
86  * @param id rule ID
87  * @return Rule object
88  */
89 inline std::unique_ptr<Rule> createRule(const std::string& id)
90 {
91     // Create actions
92     std::vector<std::unique_ptr<Action>> actions{};
93     actions.emplace_back(std::make_unique<MockAction>());
94 
95     // Create Rule
96     return std::make_unique<Rule>(id, std::move(actions));
97 }
98 
99 /**
100  * Modify the specified file so that fs::remove() fails with an exception.
101  *
102  * The file will be renamed and can be restored by calling makeFileRemovable().
103  *
104  * @param path path to the file
105  */
106 inline void makeFileUnRemovable(const fs::path& path)
107 {
108     // Rename the file to save its contents
109     fs::path savePath{path.native() + ".save"};
110     fs::rename(path, savePath);
111 
112     // Create a directory at the original file path
113     fs::create_directory(path);
114 
115     // Create a file within the directory.  fs::remove() will throw an exception
116     // if the path is a non-empty directory.
117     std::ofstream childFile{path / "childFile"};
118 }
119 
120 /**
121  * Modify the specified file so that fs::remove() can successfully delete it.
122  *
123  * Undo the modifications from an earlier call to makeFileUnRemovable().
124  *
125  * @param path path to the file
126  */
127 inline void makeFileRemovable(const fs::path& path)
128 {
129     // makeFileUnRemovable() creates a directory at the file path.  Remove the
130     // directory and all of its contents.
131     fs::remove_all(path);
132 
133     // Rename the file back to the original path to restore its contents
134     fs::path savePath{path.native() + ".save"};
135     fs::rename(savePath, path);
136 }
137 
138 } // namespace phosphor::power::regulators::test_utils
139