1*6c56a4a8SLei YU #include <experimental/filesystem>
2*6c56a4a8SLei YU #include <fstream>
3*6c56a4a8SLei YU #include <gtest/gtest.h>
4*6c56a4a8SLei YU #include <string>
5*6c56a4a8SLei YU 
6*6c56a4a8SLei YU #include "i2c_occ.hpp"
7*6c56a4a8SLei YU 
8*6c56a4a8SLei YU #ifdef I2C_OCC
9*6c56a4a8SLei YU namespace i2c_occ
10*6c56a4a8SLei YU {
11*6c56a4a8SLei YU 
12*6c56a4a8SLei YU namespace fs = std::experimental::filesystem;
13*6c56a4a8SLei YU 
14*6c56a4a8SLei YU using namespace std::string_literals;
15*6c56a4a8SLei YU const auto STR_4_0050 = "4-0050"s;
16*6c56a4a8SLei YU const auto STR_5_0051 = "5-0051"s;
17*6c56a4a8SLei YU const auto STR_6_0056 = "6-0056"s;
18*6c56a4a8SLei YU const auto STR_7_0057 = "7-0057"s;
19*6c56a4a8SLei YU 
20*6c56a4a8SLei YU const auto TEST_DIR = "test-dir/"s;
21*6c56a4a8SLei YU const auto BASE = TEST_DIR + "sys/bus/i2c/devices/";
22*6c56a4a8SLei YU const auto I2C_0 = BASE + "i2c-0";
23*6c56a4a8SLei YU const auto I2C_1 = BASE + "i2c-1";
24*6c56a4a8SLei YU const auto I2C_2 = BASE + "i2c-2";
25*6c56a4a8SLei YU const auto I2C_0_0068 = BASE + "0-0068";
26*6c56a4a8SLei YU const auto I2C_4_0050 = BASE + STR_4_0050;
27*6c56a4a8SLei YU const auto I2C_5_0051 = BASE + STR_5_0051;
28*6c56a4a8SLei YU const auto I2C_6_0056 = BASE + STR_6_0056;
29*6c56a4a8SLei YU const auto I2C_7_0057 = BASE + STR_7_0057;
30*6c56a4a8SLei YU const auto NAME = "/name";
31*6c56a4a8SLei YU const auto P8_OCC_HWMON = "p8-occ-hwmon";
32*6c56a4a8SLei YU 
33*6c56a4a8SLei YU const auto OTHER_STRING = "SomeOtherString123"s;
34*6c56a4a8SLei YU 
35*6c56a4a8SLei YU 
36*6c56a4a8SLei YU class TestUtilGetOccHwmonDevices : public testing::Test
37*6c56a4a8SLei YU {
38*6c56a4a8SLei YU public:
39*6c56a4a8SLei YU     TestUtilGetOccHwmonDevices()
40*6c56a4a8SLei YU     {
41*6c56a4a8SLei YU         // Prepare env for test case
42*6c56a4a8SLei YU         fs::create_directories(I2C_0);
43*6c56a4a8SLei YU         fs::create_directories(I2C_1);
44*6c56a4a8SLei YU         fs::create_directories(I2C_2);
45*6c56a4a8SLei YU         fs::create_directories(I2C_0_0068);
46*6c56a4a8SLei YU         fs::create_directories(I2C_4_0050);
47*6c56a4a8SLei YU         fs::create_directories(I2C_5_0051);
48*6c56a4a8SLei YU         fs::create_directories(I2C_6_0056);
49*6c56a4a8SLei YU         fs::create_directories(I2C_7_0057);
50*6c56a4a8SLei YU 
51*6c56a4a8SLei YU         std::ofstream ofs;
52*6c56a4a8SLei YU 
53*6c56a4a8SLei YU         ofs.open(I2C_0 + NAME); // i2c-0 has empty name
54*6c56a4a8SLei YU         ofs.close();
55*6c56a4a8SLei YU 
56*6c56a4a8SLei YU         ofs.open(I2C_1 + NAME);
57*6c56a4a8SLei YU         ofs << "some text\n"; // i2c-1/name has some text
58*6c56a4a8SLei YU         ofs.close();
59*6c56a4a8SLei YU 
60*6c56a4a8SLei YU         ofs.open(I2C_2 + NAME);
61*6c56a4a8SLei YU         ofs << "Aspped i2c"; // i2c-2/name is aspeed i2c
62*6c56a4a8SLei YU         ofs.close();
63*6c56a4a8SLei YU 
64*6c56a4a8SLei YU         ofs.open(I2C_0_0068 + NAME);
65*6c56a4a8SLei YU         ofs << "other text"; // 0-0068/name is has other text
66*6c56a4a8SLei YU         ofs.close();
67*6c56a4a8SLei YU 
68*6c56a4a8SLei YU         ofs.open(I2C_4_0050 + NAME);
69*6c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 4-0050/name is p8-occ-hwmon
70*6c56a4a8SLei YU         ofs.close();
71*6c56a4a8SLei YU 
72*6c56a4a8SLei YU         ofs.open(I2C_5_0051 + NAME);
73*6c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 5-0051/name is p8-occ-hwmon
74*6c56a4a8SLei YU         ofs.close();
75*6c56a4a8SLei YU 
76*6c56a4a8SLei YU         ofs.open(I2C_6_0056 + NAME);
77*6c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 6-0056/name is p8-occ-hwmon
78*6c56a4a8SLei YU         ofs.close();
79*6c56a4a8SLei YU 
80*6c56a4a8SLei YU         ofs.open(I2C_7_0057 + NAME);
81*6c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 7-0057/name is p8-occ-hwmon
82*6c56a4a8SLei YU         ofs.close();
83*6c56a4a8SLei YU     }
84*6c56a4a8SLei YU 
85*6c56a4a8SLei YU     ~TestUtilGetOccHwmonDevices()
86*6c56a4a8SLei YU     {
87*6c56a4a8SLei YU         // Cleanup test env
88*6c56a4a8SLei YU         fs::remove_all(TEST_DIR);
89*6c56a4a8SLei YU     }
90*6c56a4a8SLei YU };
91*6c56a4a8SLei YU 
92*6c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesOK)
93*6c56a4a8SLei YU {
94*6c56a4a8SLei YU     // With test env, it shall find all the 4 p8-occ-hwmon devices
95*6c56a4a8SLei YU     auto ret = getOccHwmonDevices(BASE.c_str());
96*6c56a4a8SLei YU     EXPECT_EQ(4u, ret.size());
97*6c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, ret[0]);
98*6c56a4a8SLei YU     EXPECT_EQ(STR_5_0051, ret[1]);
99*6c56a4a8SLei YU     EXPECT_EQ(STR_6_0056, ret[2]);
100*6c56a4a8SLei YU     EXPECT_EQ(STR_7_0057, ret[3]);
101*6c56a4a8SLei YU }
102*6c56a4a8SLei YU 
103*6c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesValidDirNoDevices)
104*6c56a4a8SLei YU {
105*6c56a4a8SLei YU     // Giving a dir without valid devices,
106*6c56a4a8SLei YU     // it shall return an empty vector
107*6c56a4a8SLei YU     auto ret = getOccHwmonDevices(TEST_DIR.c_str());
108*6c56a4a8SLei YU     EXPECT_TRUE(ret.empty());
109*6c56a4a8SLei YU }
110*6c56a4a8SLei YU 
111*6c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesDirNotExist)
112*6c56a4a8SLei YU {
113*6c56a4a8SLei YU     // Giving a dir that does not exist,
114*6c56a4a8SLei YU     // it shall return an empty vector
115*6c56a4a8SLei YU     auto ret = getOccHwmonDevices((TEST_DIR + "not-exist").c_str());
116*6c56a4a8SLei YU     EXPECT_TRUE(ret.empty());
117*6c56a4a8SLei YU }
118*6c56a4a8SLei YU 
119*6c56a4a8SLei YU TEST(TestI2cDbusNames, i2cToDbus)
120*6c56a4a8SLei YU {
121*6c56a4a8SLei YU     // It shall convert 4-0050 to 4_0050
122*6c56a4a8SLei YU     auto str = STR_4_0050;
123*6c56a4a8SLei YU     i2cToDbus(str);
124*6c56a4a8SLei YU     EXPECT_EQ("4_0050", str);
125*6c56a4a8SLei YU 
126*6c56a4a8SLei YU     // It shall not modify for other strings without '-'
127*6c56a4a8SLei YU     str = OTHER_STRING;
128*6c56a4a8SLei YU     i2cToDbus(str);
129*6c56a4a8SLei YU     EXPECT_EQ(OTHER_STRING, str);
130*6c56a4a8SLei YU }
131*6c56a4a8SLei YU 
132*6c56a4a8SLei YU TEST(TestI2cDbusNames, dbusToI2c)
133*6c56a4a8SLei YU {
134*6c56a4a8SLei YU     // It shall convert 4_0050 to 4-0050
135*6c56a4a8SLei YU     auto str = "4_0050"s;
136*6c56a4a8SLei YU     dbusToI2c(str);
137*6c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, str);
138*6c56a4a8SLei YU 
139*6c56a4a8SLei YU     // It shall not modify for other strings without '-'
140*6c56a4a8SLei YU     str = OTHER_STRING;
141*6c56a4a8SLei YU     dbusToI2c(str);
142*6c56a4a8SLei YU     EXPECT_EQ(OTHER_STRING, str);
143*6c56a4a8SLei YU }
144*6c56a4a8SLei YU 
145*6c56a4a8SLei YU TEST(TestI2cDbusNames, getI2cDeviceName)
146*6c56a4a8SLei YU {
147*6c56a4a8SLei YU     auto path = "/org/open_power/control/4_0050"s;
148*6c56a4a8SLei YU     auto name = getI2cDeviceName(path);
149*6c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, name);
150*6c56a4a8SLei YU }
151*6c56a4a8SLei YU 
152*6c56a4a8SLei YU } // namespace i2c_occ
153*6c56a4a8SLei YU 
154*6c56a4a8SLei YU #endif // I2C_OCC
155*6c56a4a8SLei YU 
156