blob: 8faa6686c9610d433227efa53ef5c9a13f448032 [file] [log] [blame]
Lei YUe57c38e2019-09-20 17:38:17 +08001#include "Test/server.hpp"
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/server/manager.hpp>
5#include <sdbusplus/test/sdbus_mock.hpp>
6
7#include <gtest/gtest.h>
8
9using ::testing::_;
10using ::testing::StrEq;
11
12using TestInherit =
13 sdbusplus::server::object::object<sdbusplus::server::server::Test>;
14
15class Object : public ::testing::Test
16{
17 protected:
18 sdbusplus::SdBusMock sdbusMock;
19 sdbusplus::bus::bus bus = sdbusplus::get_mocked_new(&sdbusMock);
20
21 static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Object";
22 static constexpr auto objPath = "/xyz/openbmc_project/sdbusplus/test";
23};
24
25TEST_F(Object, InterfaceAddedOnly)
26{
27 // Simulate the typical usage of a service
28 sdbusplus::server::manager::manager objManager(bus, objPath);
29 bus.request_name(busName);
30
31 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
32 .Times(0);
33 EXPECT_CALL(sdbusMock,
34 sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
35 .Times(1);
36
37 // This only add interface to the existing object
38 auto test = std::make_unique<TestInherit>(
39 bus, objPath, TestInherit::action::emit_interface_added);
40
41 // After destruction, the interface shall be removed
42 EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
43 .Times(0);
44 EXPECT_CALL(sdbusMock,
45 sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
46 .Times(1);
47}
48
49TEST_F(Object, DeferAddInterface)
50{
51 // Simulate the typical usage of a service
52 sdbusplus::server::manager::manager objManager(bus, objPath);
53 bus.request_name(busName);
54
55 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
56 .Times(0);
57 EXPECT_CALL(sdbusMock,
58 sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
59 .Times(0);
60
61 // It defers emit_object_added()
62 auto test = std::make_unique<TestInherit>(bus, objPath,
63 TestInherit::action::defer_emit);
64
65 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
66 .Times(1);
67 EXPECT_CALL(sdbusMock,
68 sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
69 .Times(0);
70
71 // Now invoke emit_object_added()
72 test->emit_object_added();
73
74 // After destruction, the object will be removed
75 EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
76 .Times(1);
77 EXPECT_CALL(sdbusMock,
78 sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
79 .Times(0);
80}
81
82TEST_F(Object, ObjectAdded)
83{
84 // Simulate the typical usage of a service
85 sdbusplus::server::manager::manager objManager(bus, objPath);
86 bus.request_name(busName);
87
88 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
89 .Times(1);
90 EXPECT_CALL(sdbusMock,
91 sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
92 .Times(0);
93
94 // Note: this is the wrong usage!
95 // With the below code, the interface is added as expected, but after
96 // destruction of TestInherit, the whole object will be removed from DBus.
97 // Refer to InterfaceAddedOnly case for the correct usage.
98 auto test = std::make_unique<TestInherit>(bus, objPath);
99
100 EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
101 .Times(1);
102 EXPECT_CALL(sdbusMock,
103 sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
104 .Times(0);
105}