blob: 92028fdbcbad1d7051d036ecc08eb459f8f61d7d [file] [log] [blame]
Gunnar Mills94df8c92018-09-14 14:50:03 -05001#include "i2c_occ.hpp"
2
Lei YU6c56a4a2017-07-14 11:07:37 +08003#include <experimental/filesystem>
4#include <fstream>
Lei YU6c56a4a2017-07-14 11:07:37 +08005#include <string>
6
Gunnar Mills94df8c92018-09-14 14:50:03 -05007#include <gtest/gtest.h>
Lei YU6c56a4a2017-07-14 11:07:37 +08008
9#ifdef I2C_OCC
10namespace i2c_occ
11{
12
13namespace fs = std::experimental::filesystem;
14
15using namespace std::string_literals;
16const auto STR_4_0050 = "4-0050"s;
17const auto STR_5_0051 = "5-0051"s;
18const auto STR_6_0056 = "6-0056"s;
19const auto STR_7_0057 = "7-0057"s;
20
21const auto TEST_DIR = "test-dir/"s;
22const auto BASE = TEST_DIR + "sys/bus/i2c/devices/";
23const auto I2C_0 = BASE + "i2c-0";
24const auto I2C_1 = BASE + "i2c-1";
25const auto I2C_2 = BASE + "i2c-2";
26const auto I2C_0_0068 = BASE + "0-0068";
27const auto I2C_4_0050 = BASE + STR_4_0050;
28const auto I2C_5_0051 = BASE + STR_5_0051;
29const auto I2C_6_0056 = BASE + STR_6_0056;
30const auto I2C_7_0057 = BASE + STR_7_0057;
31const auto NAME = "/name";
Lei YU41470e52017-11-30 16:03:50 +080032const auto OCC_MASTER_NAME = "/occ_master";
Lei YU6c56a4a2017-07-14 11:07:37 +080033const auto P8_OCC_HWMON = "p8-occ-hwmon";
34
35const auto OTHER_STRING = "SomeOtherString123"s;
36
Lei YU6c56a4a2017-07-14 11:07:37 +080037class TestUtilGetOccHwmonDevices : public testing::Test
38{
Gunnar Mills94df8c92018-09-14 14:50:03 -050039 public:
Lei YU6c56a4a2017-07-14 11:07:37 +080040 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
Lei YU41470e52017-11-30 16:03:50 +080073 ofs.open(I2C_4_0050 + OCC_MASTER_NAME);
74 ofs << "0\n"; // Make 4-0050 the slave occ
75 ofs.close();
76
Lei YU6c56a4a2017-07-14 11:07:37 +080077 ofs.open(I2C_5_0051 + NAME);
78 ofs << "p8-occ-hwmon\n"; // 5-0051/name is p8-occ-hwmon
79 ofs.close();
80
Lei YU41470e52017-11-30 16:03:50 +080081 ofs.open(I2C_5_0051 + OCC_MASTER_NAME);
82 ofs << "0\n"; // Make 5-0051 the slave occ
83 ofs.close();
84
Lei YU6c56a4a2017-07-14 11:07:37 +080085 ofs.open(I2C_6_0056 + NAME);
86 ofs << "p8-occ-hwmon\n"; // 6-0056/name is p8-occ-hwmon
87 ofs.close();
88
Lei YU41470e52017-11-30 16:03:50 +080089 ofs.open(I2C_6_0056 + OCC_MASTER_NAME);
90 ofs << "1\n"; // Make 6-0056 the master occ
91 ofs.close();
92
Lei YU6c56a4a2017-07-14 11:07:37 +080093 ofs.open(I2C_7_0057 + NAME);
94 ofs << "p8-occ-hwmon\n"; // 7-0057/name is p8-occ-hwmon
95 ofs.close();
96 }
97
98 ~TestUtilGetOccHwmonDevices()
99 {
100 // Cleanup test env
101 fs::remove_all(TEST_DIR);
102 }
103};
104
105TEST_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());
Lei YU41470e52017-11-30 16:03:50 +0800110 // 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]);
Lei YU6c56a4a2017-07-14 11:07:37 +0800115 EXPECT_EQ(STR_7_0057, ret[3]);
116}
117
118TEST_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
126TEST_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
134TEST(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
147TEST(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
160TEST(TestI2cDbusNames, getI2cDeviceName)
161{
Lei YUb5259a12017-09-01 16:22:40 +0800162 auto path = "/org/open_power/control/occ_4_0050"s;
Lei YU6c56a4a2017-07-14 11:07:37 +0800163 auto name = getI2cDeviceName(path);
164 EXPECT_EQ(STR_4_0050, name);
Lei YUb5259a12017-09-01 16:22:40 +0800165
166 // With invalid occ path, the code shall assert
167 path = "/org/open_power/control/SomeInvalidPath"s;
168 EXPECT_DEATH(getI2cDeviceName(path), "");
Lei YU6c56a4a2017-07-14 11:07:37 +0800169}
170
171} // namespace i2c_occ
172
173#endif // I2C_OCC