1 #include "reader_test.hpp" 2 3 #include "const.hpp" 4 #include "reader_impl.hpp" 5 #include "types.hpp" 6 7 #include <nlohmann/json.hpp> 8 9 #include <fstream> 10 #include <tuple> 11 12 #include <gmock/gmock.h> 13 #include <gtest/gtest.h> 14 15 using namespace openpower::vpd::manager::reader; 16 using namespace openpower::vpd::inventory; 17 using namespace openpower::vpd::constants; 18 19 class vpdManagerReaderTest : public ::testing::Test 20 { 21 protected: 22 nlohmann::json jsonFile; 23 24 // map to hold the mapping of location code and inventory path 25 LocationCodeMap fruLocationCode; 26 27 // dummy value of node number 28 const uint8_t nodeNumber = 1; 29 30 public: 31 // constructor 32 vpdManagerReaderTest() 33 { 34 processJson(); 35 } 36 37 void processJson(); 38 }; 39 40 using namespace openpower::vpd; 41 42 void vpdManagerReaderTest::processJson() 43 { 44 // read the json file and parse it 45 std::ifstream json("vpd-manager-test/vpd_reader_test.json", 46 std::ios::binary); 47 48 if (!json) 49 { 50 throw std::runtime_error("json file not found"); 51 } 52 53 jsonFile = nlohmann::json::parse(json); 54 55 if (jsonFile.find("frus") == jsonFile.end()) 56 { 57 throw std::runtime_error("frus group not found in json"); 58 } 59 60 const nlohmann::json& groupFRUS = 61 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>(); 62 for (const auto& itemFRUS : groupFRUS.items()) 63 { 64 const std::vector<nlohmann::json>& groupEEPROM = 65 itemFRUS.value().get_ref<const nlohmann::json::array_t&>(); 66 for (const auto& itemEEPROM : groupEEPROM) 67 { 68 fruLocationCode.emplace( 69 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF] 70 ["LocationCode"] 71 .get_ref<const nlohmann::json::string_t&>(), 72 itemEEPROM["inventoryPath"] 73 .get_ref<const nlohmann::json::string_t&>()); 74 } 75 } 76 } 77 78 TEST_F(vpdManagerReaderTest, isValidLocationCode_invalid) 79 { 80 // No MTS or FCS in the collapsed location code 81 std::string unexpandedLocationCode_Invalid = "Uabc-X0"; 82 83 MockUtilCalls uCalls; 84 ReaderImpl read(uCalls); 85 EXPECT_ANY_THROW({ 86 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber, 87 fruLocationCode); 88 }); 89 90 // not starting with U 91 unexpandedLocationCode_Invalid = "Mabc-X0"; 92 EXPECT_ANY_THROW({ 93 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber, 94 fruLocationCode); 95 }); 96 } 97 98 TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Invalid) 99 { 100 std::string unexpandedLocationCode_Invalid = "Uabc-X0"; 101 102 MockUtilCalls uCalls; 103 ReaderImpl read(uCalls); 104 EXPECT_ANY_THROW({ 105 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber, 106 fruLocationCode); 107 }); 108 } 109 110 TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Valid) 111 { 112 // mock the call to read bus property 113 MockUtilCalls uCalls; 114 EXPECT_CALL(uCalls, readBusProperty(SYSTEM_OBJECT, IBM_LOCATION_CODE_INF, 115 "LocationCode")) 116 .Times(1) 117 .WillOnce(testing::Return("U78DA.ND1.1234567-P0")); 118 119 std::string unexpandedLocationCode = "Ufcs-P0"; 120 ReaderImpl read(uCalls); 121 std::string res = read.getExpandedLocationCode(unexpandedLocationCode, 122 nodeNumber, fruLocationCode); 123 124 EXPECT_EQ(res, "U78DA.ND1.1234567-P0"); 125 } 126 127 TEST_F(vpdManagerReaderTest, getFrusAtLocation_Invalid) 128 { 129 // invalid location code 130 std::string unexpandedLocationCode = "Uabc-X0"; 131 132 MockUtilCalls uCalls; 133 ReaderImpl read(uCalls); 134 EXPECT_ANY_THROW({ 135 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber, 136 fruLocationCode); 137 }); 138 139 // map to hold the mapping of location code and inventory path, empty in 140 // this case 141 LocationCodeMap mapLocationCode; 142 unexpandedLocationCode = "Ufcs-P0"; 143 EXPECT_ANY_THROW({ 144 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber, 145 mapLocationCode); 146 }); 147 } 148 149 TEST_F(vpdManagerReaderTest, getFrusAtLocation_Valid) 150 { 151 std::string LocationCode = "Ufcs-P0"; 152 153 MockUtilCalls uCalls; 154 ReaderImpl read(uCalls); 155 ListOfPaths paths = read.getFrusAtLocation(LocationCode, nodeNumber, 156 fruLocationCode); 157 std::string expected = 158 "/xyz/openbmc_project/inventory/system/chassis/motherboard"; 159 EXPECT_EQ(paths.at(0), expected); 160 } 161 162 TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_invalid) 163 { 164 // not starting from U 165 std::string locationCode = "9105.22A.SIMP10R"; 166 167 MockUtilCalls uCalls; 168 ReaderImpl read(uCalls); 169 ListOfPaths paths; 170 EXPECT_ANY_THROW({ 171 paths = read.getFRUsByExpandedLocationCode(locationCode, 172 fruLocationCode); 173 }); 174 175 // unused variable warning 176 (void)paths; 177 178 // length is les sthan 17 for expanded location code 179 locationCode = "U9105.22A.SIMP10"; 180 EXPECT_ANY_THROW({ 181 paths = read.getFRUsByExpandedLocationCode(locationCode, 182 fruLocationCode); 183 }); 184 185 // Invalid location code. No "." 186 locationCode = "U78DAND11234567-P0"; 187 188 // Mock readBUsproperty call 189 EXPECT_CALL(uCalls, 190 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC")) 191 .Times(1) 192 .WillOnce( 193 testing::Return("78DAPQRS")); // return a dummy value for FC keyword 194 195 // unused variable warning 196 (void)paths; 197 EXPECT_ANY_THROW({ 198 paths = read.getFRUsByExpandedLocationCode(locationCode, 199 fruLocationCode); 200 }); 201 } 202 203 TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_FC) 204 { 205 // valid location code with FC kwd. 206 std::string validLocationCode = "U78DA.ND1.1234567-P0"; 207 208 // Mock readBUsproperty call 209 MockUtilCalls uCalls; 210 EXPECT_CALL(uCalls, 211 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC")) 212 .WillRepeatedly( 213 testing::Return("78DAPQRS")); // return a dummy value for FC keyword 214 215 ReaderImpl read(uCalls); 216 ListOfPaths paths = read.getFRUsByExpandedLocationCode(validLocationCode, 217 fruLocationCode); 218 219 std::string expected = 220 "/xyz/openbmc_project/inventory/system/chassis/motherboard"; 221 EXPECT_EQ(paths.at(0), expected); 222 } 223 224 TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_TM) 225 { 226 // valid location code with TM kwd. 227 std::string validLocationCode = "U9105.22A.SIMP10R"; 228 229 // Mock readBUsproperty call 230 MockUtilCalls uCalls; 231 EXPECT_CALL(uCalls, 232 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC")) 233 .Times(1) 234 .WillOnce( 235 testing::Return("78DAPQRS")); // return a dummy value for FC keyword 236 237 EXPECT_CALL(uCalls, 238 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VSYS", "TM")) 239 .Times(1) 240 .WillOnce( 241 testing::Return("9105PQRS")); // return a dummy value for TM keyword 242 243 ReaderImpl read(uCalls); 244 ListOfPaths paths = read.getFRUsByExpandedLocationCode(validLocationCode, 245 fruLocationCode); 246 247 std::string expected = "/xyz/openbmc_project/inventory/system"; 248 EXPECT_EQ(paths.at(0), expected); 249 } 250 251 int main(int argc, char** argv) 252 { 253 ::testing::InitGoogleTest(&argc, argv); 254 255 return RUN_ALL_TESTS(); 256 } 257