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"; 31*41470e56SLei YU const auto OCC_MASTER_NAME = "/occ_master"; 326c56a4a8SLei YU const auto P8_OCC_HWMON = "p8-occ-hwmon"; 336c56a4a8SLei YU 346c56a4a8SLei YU const auto OTHER_STRING = "SomeOtherString123"s; 356c56a4a8SLei YU 366c56a4a8SLei YU 376c56a4a8SLei YU class TestUtilGetOccHwmonDevices : public testing::Test 386c56a4a8SLei YU { 396c56a4a8SLei YU public: 406c56a4a8SLei YU TestUtilGetOccHwmonDevices() 416c56a4a8SLei YU { 426c56a4a8SLei YU // Prepare env for test case 436c56a4a8SLei YU fs::create_directories(I2C_0); 446c56a4a8SLei YU fs::create_directories(I2C_1); 456c56a4a8SLei YU fs::create_directories(I2C_2); 466c56a4a8SLei YU fs::create_directories(I2C_0_0068); 476c56a4a8SLei YU fs::create_directories(I2C_4_0050); 486c56a4a8SLei YU fs::create_directories(I2C_5_0051); 496c56a4a8SLei YU fs::create_directories(I2C_6_0056); 506c56a4a8SLei YU fs::create_directories(I2C_7_0057); 516c56a4a8SLei YU 526c56a4a8SLei YU std::ofstream ofs; 536c56a4a8SLei YU 546c56a4a8SLei YU ofs.open(I2C_0 + NAME); // i2c-0 has empty name 556c56a4a8SLei YU ofs.close(); 566c56a4a8SLei YU 576c56a4a8SLei YU ofs.open(I2C_1 + NAME); 586c56a4a8SLei YU ofs << "some text\n"; // i2c-1/name has some text 596c56a4a8SLei YU ofs.close(); 606c56a4a8SLei YU 616c56a4a8SLei YU ofs.open(I2C_2 + NAME); 626c56a4a8SLei YU ofs << "Aspped i2c"; // i2c-2/name is aspeed i2c 636c56a4a8SLei YU ofs.close(); 646c56a4a8SLei YU 656c56a4a8SLei YU ofs.open(I2C_0_0068 + NAME); 666c56a4a8SLei YU ofs << "other text"; // 0-0068/name is has other text 676c56a4a8SLei YU ofs.close(); 686c56a4a8SLei YU 696c56a4a8SLei YU ofs.open(I2C_4_0050 + NAME); 706c56a4a8SLei YU ofs << "p8-occ-hwmon\n"; // 4-0050/name is p8-occ-hwmon 716c56a4a8SLei YU ofs.close(); 726c56a4a8SLei YU 73*41470e56SLei YU ofs.open(I2C_4_0050 + OCC_MASTER_NAME); 74*41470e56SLei YU ofs << "0\n"; // Make 4-0050 the slave occ 75*41470e56SLei YU ofs.close(); 76*41470e56SLei YU 776c56a4a8SLei YU ofs.open(I2C_5_0051 + NAME); 786c56a4a8SLei YU ofs << "p8-occ-hwmon\n"; // 5-0051/name is p8-occ-hwmon 796c56a4a8SLei YU ofs.close(); 806c56a4a8SLei YU 81*41470e56SLei YU ofs.open(I2C_5_0051 + OCC_MASTER_NAME); 82*41470e56SLei YU ofs << "0\n"; // Make 5-0051 the slave occ 83*41470e56SLei YU ofs.close(); 84*41470e56SLei YU 856c56a4a8SLei YU ofs.open(I2C_6_0056 + NAME); 866c56a4a8SLei YU ofs << "p8-occ-hwmon\n"; // 6-0056/name is p8-occ-hwmon 876c56a4a8SLei YU ofs.close(); 886c56a4a8SLei YU 89*41470e56SLei YU ofs.open(I2C_6_0056 + OCC_MASTER_NAME); 90*41470e56SLei YU ofs << "1\n"; // Make 6-0056 the master occ 91*41470e56SLei YU ofs.close(); 92*41470e56SLei YU 936c56a4a8SLei YU ofs.open(I2C_7_0057 + NAME); 946c56a4a8SLei YU ofs << "p8-occ-hwmon\n"; // 7-0057/name is p8-occ-hwmon 956c56a4a8SLei YU ofs.close(); 966c56a4a8SLei YU } 976c56a4a8SLei YU 986c56a4a8SLei YU ~TestUtilGetOccHwmonDevices() 996c56a4a8SLei YU { 1006c56a4a8SLei YU // Cleanup test env 1016c56a4a8SLei YU fs::remove_all(TEST_DIR); 1026c56a4a8SLei YU } 1036c56a4a8SLei YU }; 1046c56a4a8SLei YU 1056c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesOK) 1066c56a4a8SLei YU { 1076c56a4a8SLei YU // With test env, it shall find all the 4 p8-occ-hwmon devices 1086c56a4a8SLei YU auto ret = getOccHwmonDevices(BASE.c_str()); 1096c56a4a8SLei YU EXPECT_EQ(4u, ret.size()); 110*41470e56SLei YU // The first one shall be master occ 111*41470e56SLei YU EXPECT_EQ(STR_6_0056, ret[0]); 112*41470e56SLei YU // The left is sorted 113*41470e56SLei YU EXPECT_EQ(STR_4_0050, ret[1]); 114*41470e56SLei YU EXPECT_EQ(STR_5_0051, ret[2]); 1156c56a4a8SLei YU EXPECT_EQ(STR_7_0057, ret[3]); 1166c56a4a8SLei YU } 1176c56a4a8SLei YU 1186c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesValidDirNoDevices) 1196c56a4a8SLei YU { 1206c56a4a8SLei YU // Giving a dir without valid devices, 1216c56a4a8SLei YU // it shall return an empty vector 1226c56a4a8SLei YU auto ret = getOccHwmonDevices(TEST_DIR.c_str()); 1236c56a4a8SLei YU EXPECT_TRUE(ret.empty()); 1246c56a4a8SLei YU } 1256c56a4a8SLei YU 1266c56a4a8SLei YU TEST_F(TestUtilGetOccHwmonDevices, getDevicesDirNotExist) 1276c56a4a8SLei YU { 1286c56a4a8SLei YU // Giving a dir that does not exist, 1296c56a4a8SLei YU // it shall return an empty vector 1306c56a4a8SLei YU auto ret = getOccHwmonDevices((TEST_DIR + "not-exist").c_str()); 1316c56a4a8SLei YU EXPECT_TRUE(ret.empty()); 1326c56a4a8SLei YU } 1336c56a4a8SLei YU 1346c56a4a8SLei YU TEST(TestI2cDbusNames, i2cToDbus) 1356c56a4a8SLei YU { 1366c56a4a8SLei YU // It shall convert 4-0050 to 4_0050 1376c56a4a8SLei YU auto str = STR_4_0050; 1386c56a4a8SLei YU i2cToDbus(str); 1396c56a4a8SLei YU EXPECT_EQ("4_0050", str); 1406c56a4a8SLei YU 1416c56a4a8SLei YU // It shall not modify for other strings without '-' 1426c56a4a8SLei YU str = OTHER_STRING; 1436c56a4a8SLei YU i2cToDbus(str); 1446c56a4a8SLei YU EXPECT_EQ(OTHER_STRING, str); 1456c56a4a8SLei YU } 1466c56a4a8SLei YU 1476c56a4a8SLei YU TEST(TestI2cDbusNames, dbusToI2c) 1486c56a4a8SLei YU { 1496c56a4a8SLei YU // It shall convert 4_0050 to 4-0050 1506c56a4a8SLei YU auto str = "4_0050"s; 1516c56a4a8SLei YU dbusToI2c(str); 1526c56a4a8SLei YU EXPECT_EQ(STR_4_0050, str); 1536c56a4a8SLei YU 1546c56a4a8SLei YU // It shall not modify for other strings without '-' 1556c56a4a8SLei YU str = OTHER_STRING; 1566c56a4a8SLei YU dbusToI2c(str); 1576c56a4a8SLei YU EXPECT_EQ(OTHER_STRING, str); 1586c56a4a8SLei YU } 1596c56a4a8SLei YU 1606c56a4a8SLei YU TEST(TestI2cDbusNames, getI2cDeviceName) 1616c56a4a8SLei YU { 162b5259a1eSLei YU auto path = "/org/open_power/control/occ_4_0050"s; 1636c56a4a8SLei YU auto name = getI2cDeviceName(path); 1646c56a4a8SLei YU EXPECT_EQ(STR_4_0050, name); 165b5259a1eSLei YU 166b5259a1eSLei YU // With invalid occ path, the code shall assert 167b5259a1eSLei YU path = "/org/open_power/control/SomeInvalidPath"s; 168b5259a1eSLei YU EXPECT_DEATH(getI2cDeviceName(path), ""); 1696c56a4a8SLei YU } 1706c56a4a8SLei YU 1716c56a4a8SLei YU } // namespace i2c_occ 1726c56a4a8SLei YU 1736c56a4a8SLei YU #endif // I2C_OCC 1746c56a4a8SLei YU 175