16c56a4a8SLei YU #include <experimental/filesystem>
26c56a4a8SLei YU #include <fstream>
36c56a4a8SLei YU #include <gtest/gtest.h>
46c56a4a8SLei YU #include <string>
56c56a4a8SLei YU 
66c56a4a8SLei YU #include "i2c_occ.hpp"
76c56a4a8SLei YU 
86c56a4a8SLei YU #ifdef I2C_OCC
96c56a4a8SLei YU namespace i2c_occ
106c56a4a8SLei YU {
116c56a4a8SLei YU 
126c56a4a8SLei YU namespace fs = std::experimental::filesystem;
136c56a4a8SLei YU 
146c56a4a8SLei YU using namespace std::string_literals;
156c56a4a8SLei YU const auto STR_4_0050 = "4-0050"s;
166c56a4a8SLei YU const auto STR_5_0051 = "5-0051"s;
176c56a4a8SLei YU const auto STR_6_0056 = "6-0056"s;
186c56a4a8SLei YU const auto STR_7_0057 = "7-0057"s;
196c56a4a8SLei YU 
206c56a4a8SLei YU const auto TEST_DIR = "test-dir/"s;
216c56a4a8SLei YU const auto BASE = TEST_DIR + "sys/bus/i2c/devices/";
226c56a4a8SLei YU const auto I2C_0 = BASE + "i2c-0";
236c56a4a8SLei YU const auto I2C_1 = BASE + "i2c-1";
246c56a4a8SLei YU const auto I2C_2 = BASE + "i2c-2";
256c56a4a8SLei YU const auto I2C_0_0068 = BASE + "0-0068";
266c56a4a8SLei YU const auto I2C_4_0050 = BASE + STR_4_0050;
276c56a4a8SLei YU const auto I2C_5_0051 = BASE + STR_5_0051;
286c56a4a8SLei YU const auto I2C_6_0056 = BASE + STR_6_0056;
296c56a4a8SLei YU const auto I2C_7_0057 = BASE + STR_7_0057;
306c56a4a8SLei YU const auto NAME = "/name";
316c56a4a8SLei YU const auto P8_OCC_HWMON = "p8-occ-hwmon";
326c56a4a8SLei YU 
336c56a4a8SLei YU const auto OTHER_STRING = "SomeOtherString123"s;
346c56a4a8SLei YU 
356c56a4a8SLei YU 
366c56a4a8SLei YU class TestUtilGetOccHwmonDevices : public testing::Test
376c56a4a8SLei YU {
386c56a4a8SLei YU public:
396c56a4a8SLei YU     TestUtilGetOccHwmonDevices()
406c56a4a8SLei YU     {
416c56a4a8SLei YU         // Prepare env for test case
426c56a4a8SLei YU         fs::create_directories(I2C_0);
436c56a4a8SLei YU         fs::create_directories(I2C_1);
446c56a4a8SLei YU         fs::create_directories(I2C_2);
456c56a4a8SLei YU         fs::create_directories(I2C_0_0068);
466c56a4a8SLei YU         fs::create_directories(I2C_4_0050);
476c56a4a8SLei YU         fs::create_directories(I2C_5_0051);
486c56a4a8SLei YU         fs::create_directories(I2C_6_0056);
496c56a4a8SLei YU         fs::create_directories(I2C_7_0057);
506c56a4a8SLei YU 
516c56a4a8SLei YU         std::ofstream ofs;
526c56a4a8SLei YU 
536c56a4a8SLei YU         ofs.open(I2C_0 + NAME); // i2c-0 has empty name
546c56a4a8SLei YU         ofs.close();
556c56a4a8SLei YU 
566c56a4a8SLei YU         ofs.open(I2C_1 + NAME);
576c56a4a8SLei YU         ofs << "some text\n"; // i2c-1/name has some text
586c56a4a8SLei YU         ofs.close();
596c56a4a8SLei YU 
606c56a4a8SLei YU         ofs.open(I2C_2 + NAME);
616c56a4a8SLei YU         ofs << "Aspped i2c"; // i2c-2/name is aspeed i2c
626c56a4a8SLei YU         ofs.close();
636c56a4a8SLei YU 
646c56a4a8SLei YU         ofs.open(I2C_0_0068 + NAME);
656c56a4a8SLei YU         ofs << "other text"; // 0-0068/name is has other text
666c56a4a8SLei YU         ofs.close();
676c56a4a8SLei YU 
686c56a4a8SLei YU         ofs.open(I2C_4_0050 + NAME);
696c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 4-0050/name is p8-occ-hwmon
706c56a4a8SLei YU         ofs.close();
716c56a4a8SLei YU 
726c56a4a8SLei YU         ofs.open(I2C_5_0051 + NAME);
736c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 5-0051/name is p8-occ-hwmon
746c56a4a8SLei YU         ofs.close();
756c56a4a8SLei YU 
766c56a4a8SLei YU         ofs.open(I2C_6_0056 + NAME);
776c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 6-0056/name is p8-occ-hwmon
786c56a4a8SLei YU         ofs.close();
796c56a4a8SLei YU 
806c56a4a8SLei YU         ofs.open(I2C_7_0057 + NAME);
816c56a4a8SLei YU         ofs << "p8-occ-hwmon\n"; // 7-0057/name is p8-occ-hwmon
826c56a4a8SLei YU         ofs.close();
836c56a4a8SLei YU     }
846c56a4a8SLei YU 
856c56a4a8SLei YU     ~TestUtilGetOccHwmonDevices()
866c56a4a8SLei YU     {
876c56a4a8SLei YU         // Cleanup test env
886c56a4a8SLei YU         fs::remove_all(TEST_DIR);
896c56a4a8SLei YU     }
906c56a4a8SLei YU };
916c56a4a8SLei YU 
926c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesOK)
936c56a4a8SLei YU {
946c56a4a8SLei YU     // With test env, it shall find all the 4 p8-occ-hwmon devices
956c56a4a8SLei YU     auto ret = getOccHwmonDevices(BASE.c_str());
966c56a4a8SLei YU     EXPECT_EQ(4u, ret.size());
976c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, ret[0]);
986c56a4a8SLei YU     EXPECT_EQ(STR_5_0051, ret[1]);
996c56a4a8SLei YU     EXPECT_EQ(STR_6_0056, ret[2]);
1006c56a4a8SLei YU     EXPECT_EQ(STR_7_0057, ret[3]);
1016c56a4a8SLei YU }
1026c56a4a8SLei YU 
1036c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesValidDirNoDevices)
1046c56a4a8SLei YU {
1056c56a4a8SLei YU     // Giving a dir without valid devices,
1066c56a4a8SLei YU     // it shall return an empty vector
1076c56a4a8SLei YU     auto ret = getOccHwmonDevices(TEST_DIR.c_str());
1086c56a4a8SLei YU     EXPECT_TRUE(ret.empty());
1096c56a4a8SLei YU }
1106c56a4a8SLei YU 
1116c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesDirNotExist)
1126c56a4a8SLei YU {
1136c56a4a8SLei YU     // Giving a dir that does not exist,
1146c56a4a8SLei YU     // it shall return an empty vector
1156c56a4a8SLei YU     auto ret = getOccHwmonDevices((TEST_DIR + "not-exist").c_str());
1166c56a4a8SLei YU     EXPECT_TRUE(ret.empty());
1176c56a4a8SLei YU }
1186c56a4a8SLei YU 
1196c56a4a8SLei YU TEST(TestI2cDbusNames, i2cToDbus)
1206c56a4a8SLei YU {
1216c56a4a8SLei YU     // It shall convert 4-0050 to 4_0050
1226c56a4a8SLei YU     auto str = STR_4_0050;
1236c56a4a8SLei YU     i2cToDbus(str);
1246c56a4a8SLei YU     EXPECT_EQ("4_0050", str);
1256c56a4a8SLei YU 
1266c56a4a8SLei YU     // It shall not modify for other strings without '-'
1276c56a4a8SLei YU     str = OTHER_STRING;
1286c56a4a8SLei YU     i2cToDbus(str);
1296c56a4a8SLei YU     EXPECT_EQ(OTHER_STRING, str);
1306c56a4a8SLei YU }
1316c56a4a8SLei YU 
1326c56a4a8SLei YU TEST(TestI2cDbusNames, dbusToI2c)
1336c56a4a8SLei YU {
1346c56a4a8SLei YU     // It shall convert 4_0050 to 4-0050
1356c56a4a8SLei YU     auto str = "4_0050"s;
1366c56a4a8SLei YU     dbusToI2c(str);
1376c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, str);
1386c56a4a8SLei YU 
1396c56a4a8SLei YU     // It shall not modify for other strings without '-'
1406c56a4a8SLei YU     str = OTHER_STRING;
1416c56a4a8SLei YU     dbusToI2c(str);
1426c56a4a8SLei YU     EXPECT_EQ(OTHER_STRING, str);
1436c56a4a8SLei YU }
1446c56a4a8SLei YU 
1456c56a4a8SLei YU TEST(TestI2cDbusNames, getI2cDeviceName)
1466c56a4a8SLei YU {
147*b5259a1eSLei YU     auto path = "/org/open_power/control/occ_4_0050"s;
1486c56a4a8SLei YU     auto name = getI2cDeviceName(path);
1496c56a4a8SLei YU     EXPECT_EQ(STR_4_0050, name);
150*b5259a1eSLei YU 
151*b5259a1eSLei YU     // With invalid occ path, the code shall assert
152*b5259a1eSLei YU     path = "/org/open_power/control/SomeInvalidPath"s;
153*b5259a1eSLei YU     EXPECT_DEATH(getI2cDeviceName(path), "");
1546c56a4a8SLei YU }
1556c56a4a8SLei YU 
1566c56a4a8SLei YU } // namespace i2c_occ
1576c56a4a8SLei YU 
1586c56a4a8SLei YU #endif // I2C_OCC
1596c56a4a8SLei YU 
160