summaryrefslogtreecommitdiffstats
path: root/test/server/object.cpp
blob: 8faa6686c9610d433227efa53ef5c9a13f448032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Test/server.hpp"

#include <sdbusplus/bus.hpp>
#include <sdbusplus/server/manager.hpp>
#include <sdbusplus/test/sdbus_mock.hpp>

#include <gtest/gtest.h>

using ::testing::_;
using ::testing::StrEq;

using TestInherit =
    sdbusplus::server::object::object<sdbusplus::server::server::Test>;

class Object : public ::testing::Test
{
  protected:
    sdbusplus::SdBusMock sdbusMock;
    sdbusplus::bus::bus bus = sdbusplus::get_mocked_new(&sdbusMock);

    static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Object";
    static constexpr auto objPath = "/xyz/openbmc_project/sdbusplus/test";
};

TEST_F(Object, InterfaceAddedOnly)
{
    // Simulate the typical usage of a service
    sdbusplus::server::manager::manager objManager(bus, objPath);
    bus.request_name(busName);

    EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
        .Times(0);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
        .Times(1);

    // This only add interface to the existing object
    auto test = std::make_unique<TestInherit>(
        bus, objPath, TestInherit::action::emit_interface_added);

    // After destruction, the interface shall be removed
    EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
        .Times(0);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
        .Times(1);
}

TEST_F(Object, DeferAddInterface)
{
    // Simulate the typical usage of a service
    sdbusplus::server::manager::manager objManager(bus, objPath);
    bus.request_name(busName);

    EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
        .Times(0);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
        .Times(0);

    // It defers emit_object_added()
    auto test = std::make_unique<TestInherit>(bus, objPath,
                                              TestInherit::action::defer_emit);

    EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
        .Times(1);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
        .Times(0);

    // Now invoke emit_object_added()
    test->emit_object_added();

    // After destruction, the object will be removed
    EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
        .Times(1);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
        .Times(0);
}

TEST_F(Object, ObjectAdded)
{
    // Simulate the typical usage of a service
    sdbusplus::server::manager::manager objManager(bus, objPath);
    bus.request_name(busName);

    EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
        .Times(1);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _))
        .Times(0);

    // Note: this is the wrong usage!
    // With the below code, the interface is added as expected, but after
    // destruction of TestInherit, the whole object will be removed from DBus.
    // Refer to InterfaceAddedOnly case for the correct usage.
    auto test = std::make_unique<TestInherit>(bus, objPath);

    EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath)))
        .Times(1);
    EXPECT_CALL(sdbusMock,
                sd_bus_emit_interfaces_removed_strv(_, StrEq(objPath), _))
        .Times(0);
}
OpenPOWER on IntegriCloud