| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 1 | #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 | 
 | 9 | namespace i2c_occ | 
 | 10 | { | 
 | 11 |  | 
 | 12 | namespace fs = std::experimental::filesystem; | 
 | 13 |  | 
 | 14 | using namespace std::string_literals; | 
 | 15 | const auto STR_4_0050 = "4-0050"s; | 
 | 16 | const auto STR_5_0051 = "5-0051"s; | 
 | 17 | const auto STR_6_0056 = "6-0056"s; | 
 | 18 | const auto STR_7_0057 = "7-0057"s; | 
 | 19 |  | 
 | 20 | const auto TEST_DIR = "test-dir/"s; | 
 | 21 | const auto BASE = TEST_DIR + "sys/bus/i2c/devices/"; | 
 | 22 | const auto I2C_0 = BASE + "i2c-0"; | 
 | 23 | const auto I2C_1 = BASE + "i2c-1"; | 
 | 24 | const auto I2C_2 = BASE + "i2c-2"; | 
 | 25 | const auto I2C_0_0068 = BASE + "0-0068"; | 
 | 26 | const auto I2C_4_0050 = BASE + STR_4_0050; | 
 | 27 | const auto I2C_5_0051 = BASE + STR_5_0051; | 
 | 28 | const auto I2C_6_0056 = BASE + STR_6_0056; | 
 | 29 | const auto I2C_7_0057 = BASE + STR_7_0057; | 
 | 30 | const auto NAME = "/name"; | 
| Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 31 | const auto OCC_MASTER_NAME = "/occ_master"; | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 32 | const auto P8_OCC_HWMON = "p8-occ-hwmon"; | 
 | 33 |  | 
 | 34 | const auto OTHER_STRING = "SomeOtherString123"s; | 
 | 35 |  | 
 | 36 |  | 
 | 37 | class TestUtilGetOccHwmonDevices : public testing::Test | 
 | 38 | { | 
 | 39 | public: | 
 | 40 |     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 YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 73 |         ofs.open(I2C_4_0050 + OCC_MASTER_NAME); | 
 | 74 |         ofs << "0\n"; // Make 4-0050 the slave occ | 
 | 75 |         ofs.close(); | 
 | 76 |  | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 77 |         ofs.open(I2C_5_0051 + NAME); | 
 | 78 |         ofs << "p8-occ-hwmon\n"; // 5-0051/name is p8-occ-hwmon | 
 | 79 |         ofs.close(); | 
 | 80 |  | 
| Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 81 |         ofs.open(I2C_5_0051 + OCC_MASTER_NAME); | 
 | 82 |         ofs << "0\n"; // Make 5-0051 the slave occ | 
 | 83 |         ofs.close(); | 
 | 84 |  | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 85 |         ofs.open(I2C_6_0056 + NAME); | 
 | 86 |         ofs << "p8-occ-hwmon\n"; // 6-0056/name is p8-occ-hwmon | 
 | 87 |         ofs.close(); | 
 | 88 |  | 
| Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 89 |         ofs.open(I2C_6_0056 + OCC_MASTER_NAME); | 
 | 90 |         ofs << "1\n"; // Make 6-0056 the master occ | 
 | 91 |         ofs.close(); | 
 | 92 |  | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 93 |         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 |  | 
 | 105 | TEST_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 YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 110 |     // 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 YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 115 |     EXPECT_EQ(STR_7_0057, ret[3]); | 
 | 116 | } | 
 | 117 |  | 
 | 118 | TEST_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 |  | 
 | 126 | TEST_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 |  | 
 | 134 | TEST(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 |  | 
 | 147 | TEST(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 |  | 
 | 160 | TEST(TestI2cDbusNames, getI2cDeviceName) | 
 | 161 | { | 
| Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 162 |     auto path = "/org/open_power/control/occ_4_0050"s; | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 163 |     auto name = getI2cDeviceName(path); | 
 | 164 |     EXPECT_EQ(STR_4_0050, name); | 
| Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 165 |  | 
 | 166 |     // With invalid occ path, the code shall assert | 
 | 167 |     path = "/org/open_power/control/SomeInvalidPath"s; | 
 | 168 |     EXPECT_DEATH(getI2cDeviceName(path), ""); | 
| Lei YU | 6c56a4a | 2017-07-14 11:07:37 +0800 | [diff] [blame] | 169 | } | 
 | 170 |  | 
 | 171 | } // namespace i2c_occ | 
 | 172 |  | 
 | 173 | #endif // I2C_OCC | 
 | 174 |  |