1 /**
2  * Copyright © 2019 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 "device.hpp"
17 #include "i2c_interface.hpp"
18 #include "id_map.hpp"
19 #include "mocked_i2c_interface.hpp"
20 #include "rail.hpp"
21 #include "rule.hpp"
22 
23 #include <exception>
24 #include <memory>
25 #include <stdexcept>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <gtest/gtest.h>
31 
32 using namespace phosphor::power::regulators;
33 
34 TEST(IDMapTests, AddDevice)
35 {
36     IDMap idMap{};
37 
38     // Create device
39     std::unique_ptr<i2c::I2CInterface> i2cInterface =
40         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
41     std::string id{"vio_reg"};
42     Device device{id, true, "/system/chassis/motherboard/vio_reg",
43                   std::move(i2cInterface)};
44 
45     // Verify device is not initially in map
46     EXPECT_THROW(idMap.getDevice(id), std::invalid_argument);
47 
48     // Add device to map
49     idMap.addDevice(device);
50 
51     // Verify device is now in map
52     try
53     {
54         Device& deviceFound = idMap.getDevice(id);
55         EXPECT_EQ(deviceFound.getID(), id);
56         EXPECT_EQ(&deviceFound, &device);
57     }
58     catch (const std::exception& error)
59     {
60         ADD_FAILURE() << "Should not have caught exception.";
61     }
62 
63     // Verify different device is not in map
64     EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
65 }
66 
67 TEST(IDMapTests, AddRail)
68 {
69     IDMap idMap{};
70 
71     // Create rail
72     std::string id{"vio0"};
73     Rail rail{id};
74 
75     // Verify rail is not initially in map
76     EXPECT_THROW(idMap.getRail(id), std::invalid_argument);
77 
78     // Add rail to map
79     idMap.addRail(rail);
80 
81     // Verify rail is now in map
82     try
83     {
84         Rail& railFound = idMap.getRail(id);
85         EXPECT_EQ(railFound.getID(), id);
86         EXPECT_EQ(&railFound, &rail);
87     }
88     catch (const std::exception& error)
89     {
90         ADD_FAILURE() << "Should not have caught exception.";
91     }
92 
93     // Verify different rail is not in map
94     EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
95 }
96 
97 TEST(IDMapTests, AddRule)
98 {
99     IDMap idMap{};
100 
101     // Create rule
102     std::string id{"set_voltage_rule"};
103     Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
104 
105     // Verify rule is not initially in map
106     EXPECT_THROW(idMap.getRule(id), std::invalid_argument);
107 
108     // Add rule to map
109     idMap.addRule(rule);
110 
111     // Verify rule is now in map
112     try
113     {
114         Rule& ruleFound = idMap.getRule(id);
115         EXPECT_EQ(ruleFound.getID(), id);
116         EXPECT_EQ(&ruleFound, &rule);
117     }
118     catch (const std::exception& error)
119     {
120         ADD_FAILURE() << "Should not have caught exception.";
121     }
122 
123     // Verify different rule is not in map
124     EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
125                  std::invalid_argument);
126 }
127 
128 TEST(IDMapTests, GetDevice)
129 {
130     IDMap idMap{};
131 
132     // Create device
133     std::unique_ptr<i2c::I2CInterface> i2cInterface =
134         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
135     std::string id{"vio_reg"};
136     Device device{id, true, "/system/chassis/motherboard/vio_reg",
137                   std::move(i2cInterface)};
138 
139     // Add a device to the map
140     idMap.addDevice(device);
141 
142     // Test where ID found in map
143     try
144     {
145         Device& deviceFound = idMap.getDevice(id);
146         EXPECT_EQ(deviceFound.getID(), id);
147         EXPECT_EQ(&deviceFound, &device);
148     }
149     catch (const std::exception& error)
150     {
151         ADD_FAILURE() << "Should not have caught exception.";
152     }
153 
154     // Test where ID not found in map
155     try
156     {
157         idMap.getDevice("vcs_reg");
158         ADD_FAILURE() << "Should not have reached this line.";
159     }
160     catch (const std::invalid_argument& ia_error)
161     {
162         EXPECT_STREQ(ia_error.what(),
163                      "Unable to find device with ID \"vcs_reg\"");
164     }
165     catch (const std::exception& error)
166     {
167         ADD_FAILURE() << "Should not have caught exception.";
168     }
169 }
170 
171 TEST(IDMapTests, GetRail)
172 {
173     IDMap idMap{};
174 
175     // Add a rail to the map
176     std::string id{"vio0"};
177     Rail rail{id};
178     idMap.addRail(rail);
179 
180     // Test where ID found in map
181     try
182     {
183         Rail& railFound = idMap.getRail(id);
184         EXPECT_EQ(railFound.getID(), id);
185         EXPECT_EQ(&railFound, &rail);
186     }
187     catch (const std::exception& error)
188     {
189         ADD_FAILURE() << "Should not have caught exception.";
190     }
191 
192     // Test where ID not found in map
193     try
194     {
195         idMap.getRail("vcs0");
196         ADD_FAILURE() << "Should not have reached this line.";
197     }
198     catch (const std::invalid_argument& ia_error)
199     {
200         EXPECT_STREQ(ia_error.what(), "Unable to find rail with ID \"vcs0\"");
201     }
202     catch (const std::exception& error)
203     {
204         ADD_FAILURE() << "Should not have caught exception.";
205     }
206 }
207 
208 TEST(IDMapTests, GetRule)
209 {
210     IDMap idMap{};
211 
212     // Add a rule to the map
213     std::string id{"set_voltage_rule"};
214     Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
215     idMap.addRule(rule);
216 
217     // Test where ID found in map
218     try
219     {
220         Rule& ruleFound = idMap.getRule(id);
221         EXPECT_EQ(ruleFound.getID(), id);
222         EXPECT_EQ(&ruleFound, &rule);
223     }
224     catch (const std::exception& error)
225     {
226         ADD_FAILURE() << "Should not have caught exception.";
227     }
228 
229     // Test where ID not found in map
230     try
231     {
232         idMap.getRule("read_sensors_rule");
233         ADD_FAILURE() << "Should not have reached this line.";
234     }
235     catch (const std::invalid_argument& ia_error)
236     {
237         EXPECT_STREQ(ia_error.what(),
238                      "Unable to find rule with ID \"read_sensors_rule\"");
239     }
240     catch (const std::exception& error)
241     {
242         ADD_FAILURE() << "Should not have caught exception.";
243     }
244 }
245