Andrew Geissler | 271b7dd | 2019-02-05 11:23:30 -0600 | [diff] [blame] | 1 | #include "src/associations.hpp" |
| 2 | |
Andrew Geissler | b04f033 | 2019-02-08 14:07:56 -0600 | [diff] [blame^] | 3 | #include "src/test/util/asio_server_class.hpp" |
| 4 | |
Andrew Geissler | 271b7dd | 2019-02-05 11:23:30 -0600 | [diff] [blame] | 5 | #include <sdbusplus/asio/connection.hpp> |
| 6 | #include <sdbusplus/asio/object_server.hpp> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Andrew Geissler | b04f033 | 2019-02-08 14:07:56 -0600 | [diff] [blame^] | 10 | class TestAssociations : public AsioServerClassTest |
Andrew Geissler | 271b7dd | 2019-02-05 11:23:30 -0600 | [diff] [blame] | 11 | { |
Andrew Geissler | 271b7dd | 2019-02-05 11:23:30 -0600 | [diff] [blame] | 12 | }; |
Andrew Geissler | b04f033 | 2019-02-08 14:07:56 -0600 | [diff] [blame^] | 13 | sdbusplus::asio::object_server* TestAssociations::AsioServerClassTest::server = |
| 14 | nullptr; |
Andrew Geissler | 271b7dd | 2019-02-05 11:23:30 -0600 | [diff] [blame] | 15 | |
| 16 | const std::string DEFAULT_SOURCE_PATH = "/logging/entry/1"; |
| 17 | const std::string DEFAULT_DBUS_SVC = "xyz.openbmc_project.New.Interface"; |
| 18 | const std::string DEFAULT_FWD_PATH = {DEFAULT_SOURCE_PATH + "/" + "inventory"}; |
| 19 | const std::string DEFAULT_ENDPOINT = |
| 20 | "/xyz/openbmc_project/inventory/system/chassis"; |
| 21 | const std::string DEFAULT_REV_PATH = {DEFAULT_ENDPOINT + "/" + "error"}; |
| 22 | const std::string EXTRA_ENDPOINT = "/xyz/openbmc_project/differnt/endpoint"; |
| 23 | |
| 24 | // Create a default AssociationOwnersType object |
| 25 | AssociationOwnersType createDefaultOwnerAssociation() |
| 26 | { |
| 27 | AssociationPaths assocPathMap = {{DEFAULT_FWD_PATH, {DEFAULT_ENDPOINT}}, |
| 28 | {DEFAULT_REV_PATH, {DEFAULT_SOURCE_PATH}}}; |
| 29 | boost::container::flat_map<std::string, AssociationPaths> serviceMap = { |
| 30 | {DEFAULT_DBUS_SVC, assocPathMap}}; |
| 31 | AssociationOwnersType ownerAssoc = {{DEFAULT_SOURCE_PATH, serviceMap}}; |
| 32 | return ownerAssoc; |
| 33 | } |
| 34 | |
| 35 | // Create a default AssociationInterfaces object |
| 36 | AssociationInterfaces |
| 37 | createDefaultInterfaceAssociation(sdbusplus::asio::object_server* server) |
| 38 | { |
| 39 | AssociationInterfaces interfaceAssoc; |
| 40 | |
| 41 | auto& iface = interfaceAssoc[DEFAULT_FWD_PATH]; |
| 42 | auto& endpoints = std::get<endpointsPos>(iface); |
| 43 | endpoints.push_back(DEFAULT_ENDPOINT); |
| 44 | server->add_interface(DEFAULT_FWD_PATH, DEFAULT_DBUS_SVC); |
| 45 | |
| 46 | auto& iface2 = interfaceAssoc[DEFAULT_REV_PATH]; |
| 47 | auto& endpoints2 = std::get<endpointsPos>(iface2); |
| 48 | endpoints2.push_back(DEFAULT_SOURCE_PATH); |
| 49 | server->add_interface(DEFAULT_REV_PATH, DEFAULT_DBUS_SVC); |
| 50 | |
| 51 | return interfaceAssoc; |
| 52 | } |
| 53 | |
| 54 | // Just add an extra endpoint to the first association |
| 55 | void addEndpointToInterfaceAssociation(AssociationInterfaces& interfaceAssoc) |
| 56 | { |
| 57 | auto iface = interfaceAssoc[DEFAULT_FWD_PATH]; |
| 58 | auto endpoints = std::get<endpointsPos>(iface); |
| 59 | endpoints.push_back(EXTRA_ENDPOINT); |
| 60 | } |
| 61 | |
| 62 | // Verify call when path is not in associated owners |
| 63 | TEST_F(TestAssociations, SourcePathNotInAssociations) |
| 64 | { |
| 65 | EXPECT_NE(nullptr, server); |
| 66 | std::string sourcePath = "/xyz/openbmc_project/no/association"; |
| 67 | AssociationOwnersType assocOwners; |
| 68 | AssociationInterfaces assocInterfaces; |
| 69 | |
| 70 | removeAssociation(sourcePath, DEFAULT_DBUS_SVC, *server, assocOwners, |
| 71 | assocInterfaces); |
| 72 | } |
| 73 | |
| 74 | // Verify call when owner is not in associated owners |
| 75 | TEST_F(TestAssociations, OwnerNotInAssociations) |
| 76 | { |
| 77 | AssociationInterfaces assocInterfaces; |
| 78 | |
| 79 | auto assocOwners = createDefaultOwnerAssociation(); |
| 80 | |
| 81 | removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, |
| 82 | assocOwners, assocInterfaces); |
| 83 | } |
| 84 | |
| 85 | // Verify call when path is not in associated interfaces |
| 86 | TEST_F(TestAssociations, PathNotInAssocInterfaces) |
| 87 | { |
| 88 | AssociationInterfaces assocInterfaces; |
| 89 | |
| 90 | auto assocOwners = createDefaultOwnerAssociation(); |
| 91 | |
| 92 | removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, |
| 93 | assocOwners, assocInterfaces); |
| 94 | |
| 95 | EXPECT_TRUE(assocOwners.empty()); |
| 96 | } |
| 97 | |
| 98 | // Verify call when path is in associated interfaces |
| 99 | TEST_F(TestAssociations, PathIsInAssociatedInterfaces) |
| 100 | { |
| 101 | // Build up these objects so that an associated interface will match |
| 102 | // with the associated owner being removed |
| 103 | auto assocOwners = createDefaultOwnerAssociation(); |
| 104 | auto assocInterfaces = createDefaultInterfaceAssociation(server); |
| 105 | |
| 106 | removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, |
| 107 | assocOwners, assocInterfaces); |
| 108 | |
| 109 | // Verify owner association was deleted |
| 110 | EXPECT_TRUE(assocOwners.empty()); |
| 111 | |
| 112 | // Verify endpoint was deleted from interface association |
| 113 | auto intfEndpoints = |
| 114 | std::get<endpointsPos>(assocInterfaces[DEFAULT_FWD_PATH]); |
| 115 | EXPECT_EQ(intfEndpoints.size(), 0); |
| 116 | intfEndpoints = std::get<endpointsPos>(assocInterfaces[DEFAULT_REV_PATH]); |
| 117 | EXPECT_EQ(intfEndpoints.size(), 0); |
| 118 | } |
| 119 | |
| 120 | // Verify call when path is in associated interfaces, with extra endpoints |
| 121 | TEST_F(TestAssociations, PathIsInAssociatedInterfacesExtraEndpoints) |
| 122 | { |
| 123 | // Build up these objects so that an associated interface will match |
| 124 | // with the associated owner being removed |
| 125 | auto assocOwners = createDefaultOwnerAssociation(); |
| 126 | auto assocInterfaces = createDefaultInterfaceAssociation(server); |
| 127 | |
| 128 | // Add another endpoint to the assoc interfaces |
| 129 | addEndpointToInterfaceAssociation(assocInterfaces); |
| 130 | |
| 131 | removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, |
| 132 | assocOwners, assocInterfaces); |
| 133 | |
| 134 | // Verify owner association was deleted |
| 135 | EXPECT_TRUE(assocOwners.empty()); |
| 136 | |
| 137 | // Verify all endpoints are deleted since source path was deleted |
| 138 | auto intfEndpoints = |
| 139 | std::get<endpointsPos>(assocInterfaces[DEFAULT_FWD_PATH]); |
| 140 | EXPECT_EQ(intfEndpoints.size(), 0); |
| 141 | intfEndpoints = std::get<endpointsPos>(assocInterfaces[DEFAULT_REV_PATH]); |
| 142 | EXPECT_EQ(intfEndpoints.size(), 0); |
| 143 | } |