194df8c90SGunnar Mills #include "i2c_occ.hpp"
294df8c90SGunnar Mills 
3*bcef3b48SGeorge Liu #include <filesystem>
46c56a4a8SLei YU #include <fstream>
56c56a4a8SLei YU #include <string>
66c56a4a8SLei YU 
794df8c90SGunnar Mills #include <gtest/gtest.h>
86c56a4a8SLei YU 
96c56a4a8SLei YU #ifdef I2C_OCC
106c56a4a8SLei YU namespace i2c_occ
116c56a4a8SLei YU {
126c56a4a8SLei YU 
13*bcef3b48SGeorge Liu namespace fs = std::filesystem;
146c56a4a8SLei YU 
156c56a4a8SLei YU using namespace std::string_literals;
166c56a4a8SLei YU const auto STR_4_0050 = "4-0050"s;
176c56a4a8SLei YU const auto STR_5_0051 = "5-0051"s;
186c56a4a8SLei YU const auto STR_6_0056 = "6-0056"s;
196c56a4a8SLei YU const auto STR_7_0057 = "7-0057"s;
206c56a4a8SLei YU 
216c56a4a8SLei YU const auto TEST_DIR = "test-dir/"s;
226c56a4a8SLei YU const auto BASE = TEST_DIR + "sys/bus/i2c/devices/";
236c56a4a8SLei YU const auto I2C_0 = BASE + "i2c-0";
246c56a4a8SLei YU const auto I2C_1 = BASE + "i2c-1";
256c56a4a8SLei YU const auto I2C_2 = BASE + "i2c-2";
266c56a4a8SLei YU const auto I2C_0_0068 = BASE + "0-0068";
276c56a4a8SLei YU const auto I2C_4_0050 = BASE + STR_4_0050;
286c56a4a8SLei YU const auto I2C_5_0051 = BASE + STR_5_0051;
296c56a4a8SLei YU const auto I2C_6_0056 = BASE + STR_6_0056;
306c56a4a8SLei YU const auto I2C_7_0057 = BASE + STR_7_0057;
316c56a4a8SLei YU const auto NAME = "/name";
3241470e56SLei YU const auto OCC_MASTER_NAME = "/occ_master";
336c56a4a8SLei YU const auto P8_OCC_HWMON = "p8-occ-hwmon";
346c56a4a8SLei YU 
356c56a4a8SLei YU const auto OTHER_STRING = "SomeOtherString123"s;
366c56a4a8SLei YU 
376c56a4a8SLei YU class TestUtilGetOccHwmonDevices : public testing::Test
386c56a4a8SLei YU {
396c56a4a8SLei YU   public:
TestUtilGetOccHwmonDevices()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 
7341470e56SLei YU         ofs.open(I2C_4_0050 + OCC_MASTER_NAME);
7441470e56SLei YU         ofs << "0\n"; // Make 4-0050 the slave occ
7541470e56SLei YU         ofs.close();
7641470e56SLei 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 
8141470e56SLei YU         ofs.open(I2C_5_0051 + OCC_MASTER_NAME);
8241470e56SLei YU         ofs << "0\n"; // Make 5-0051 the slave occ
8341470e56SLei YU         ofs.close();
8441470e56SLei 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 
8941470e56SLei YU         ofs.open(I2C_6_0056 + OCC_MASTER_NAME);
9041470e56SLei YU         ofs << "1\n"; // Make 6-0056 the master occ
9141470e56SLei YU         ofs.close();
9241470e56SLei 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 
~TestUtilGetOccHwmonDevices()986c56a4a8SLei YU     ~TestUtilGetOccHwmonDevices()
996c56a4a8SLei YU     {
1006c56a4a8SLei YU         // Cleanup test env
1016c56a4a8SLei YU         fs::remove_all(TEST_DIR);
1026c56a4a8SLei YU     }
1036c56a4a8SLei YU };
1046c56a4a8SLei YU 
TEST_F(TestUtilGetOccHwmonDevices,getDevicesOK)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());
11041470e56SLei YU     // The first one shall be master occ
11141470e56SLei YU     EXPECT_EQ(STR_6_0056, ret[0]);
11241470e56SLei YU     // The left is sorted
11341470e56SLei YU     EXPECT_EQ(STR_4_0050, ret[1]);
11441470e56SLei YU     EXPECT_EQ(STR_5_0051, ret[2]);
1156c56a4a8SLei YU     EXPECT_EQ(STR_7_0057, ret[3]);
1166c56a4a8SLei YU }
1176c56a4a8SLei YU 
TEST_F(TestUtilGetOccHwmonDevices,getDevicesValidDirNoDevices)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 
TEST_F(TestUtilGetOccHwmonDevices,getDevicesDirNotExist)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 
TEST(TestI2cDbusNames,i2cToDbus)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 
TEST(TestI2cDbusNames,dbusToI2c)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 
TEST(TestI2cDbusNames,getI2cDeviceName)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
174