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