#include "association_manager.hpp" #include #include #include using namespace phosphor::inventory::manager::associations; namespace fs = std::filesystem; static const auto goodJson = R"( [ { "path": "system/PS0", "endpoints": [ { "types": { "rType": "inventory", "fType": "sensors" }, "paths": [ "power/ps0_input_power", "voltage/ps0_input_voltage", "current/ps0_output_current", "voltage/ps0_output_voltage" ] }, { "types": { "rType": "inventory", "fType": "fans" }, "paths": [ "fan_tach/ps0_fan" ] } ] }, { "path": "system/fan42", "endpoints": [ { "types": { "rType": "inventory", "fType": "sensors" }, "paths": [ "fan_tach/fan42" ] }, { "types": { "rType": "inventory", "fType": "led" }, "paths": [ "led/fan42" ] } ] } ])"; // Malformed JSON static const auto badJson0 = R"( "hello": world })"; // Uses 'blah' instead of 'paths' static const auto badJson1 = R"( [ { "blah": "system/PS0", "endpoints": [ { "types": { "fType": "inventory", "rType": "sensors" }, "paths": [ "ps0_input_power", ] } ] } ])"; // Uses 'blah' instead of 'rType' static const auto badJson2 = R"( [ { "paths": "system/PS0", "endpoints": [ { "types": { "blah": "inventory", "fType": "sensors" }, "paths": [ "ps0_input_power", ] } ] } ])"; // Missing the endpoints/paths array static const auto badJson3 = R"( [ { "paths": "system/PS0", "endpoints": [ { "types": { "rType": "inventory", "fType": "sensors" } } ] } ])"; class AssocsTest : public ::testing::Test { protected: AssocsTest() : ::testing::Test(), bus(sdbusplus::bus::new_default()) {} fs::path jsonDir; sdbusplus::bus_t bus; virtual void SetUp() { char dir[] = {"assocTestXXXXXX"}; jsonDir = mkdtemp(dir); } virtual void TearDown() { fs::remove_all(jsonDir); } std::string writeFile(const char* data) { fs::path path = jsonDir / "associations.json"; std::ofstream f{path}; f << data; f.close(); return path; } }; TEST_F(AssocsTest, TEST_NO_JSON) { try { Manager m{bus}; EXPECT_TRUE(false); } catch (const std::exception& e) {} } TEST_F(AssocsTest, TEST_GOOD_JSON) { auto path = writeFile(goodJson); Manager m(bus, path); const auto& a = m.getAssociationsConfig(); EXPECT_EQ(a.size(), 2); { auto x = a.find("/xyz/openbmc_project/inventory/system/PS0"); EXPECT_NE(x, a.end()); auto& endpoints = x->second; EXPECT_EQ(endpoints.size(), 2); { auto& types = std::get<0>(endpoints[0]); EXPECT_EQ(std::get<0>(types), "sensors"); EXPECT_EQ(std::get<1>(types), "inventory"); auto& paths = std::get<1>(endpoints[0]); EXPECT_EQ(paths.size(), 4); } { auto& types = std::get<0>(endpoints[1]); EXPECT_EQ(std::get<0>(types), "fans"); EXPECT_EQ(std::get<1>(types), "inventory"); auto& paths = std::get<1>(endpoints[1]); EXPECT_EQ(paths.size(), 1); } } { auto x = a.find("/xyz/openbmc_project/inventory/system/fan42"); EXPECT_NE(x, a.end()); auto& endpoints = x->second; EXPECT_EQ(endpoints.size(), 2); { auto& types = std::get<0>(endpoints[0]); EXPECT_EQ(std::get<0>(types), "sensors"); EXPECT_EQ(std::get<1>(types), "inventory"); auto& paths = std::get<1>(endpoints[0]); EXPECT_EQ(paths.size(), 1); } { auto& types = std::get<0>(endpoints[1]); EXPECT_EQ(std::get<0>(types), "led"); EXPECT_EQ(std::get<1>(types), "inventory"); auto& paths = std::get<1>(endpoints[1]); EXPECT_EQ(paths.size(), 1); } } } TEST_F(AssocsTest, TEST_BAD_JSON0) { auto path = writeFile(badJson0); try { Manager m(bus, path); EXPECT_TRUE(false); } catch (const std::exception& e) {} } TEST_F(AssocsTest, TEST_BAD_JSON1) { auto path = writeFile(badJson1); try { Manager m(bus, path); EXPECT_TRUE(false); } catch (const std::exception& e) {} } TEST_F(AssocsTest, TEST_BAD_JSON2) { auto path = writeFile(badJson2); try { Manager m(bus, path); EXPECT_TRUE(false); } catch (const std::exception& e) {} } TEST_F(AssocsTest, TEST_BAD_JSON3) { auto path = writeFile(badJson3); try { Manager m(bus, path); EXPECT_TRUE(false); } catch (const std::exception& e) {} }