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