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