Lei YU | e57c38e | 2019-09-20 17:38:17 +0800 | [diff] [blame] | 1 | #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 | |
| 9 | using ::testing::_; |
| 10 | using ::testing::StrEq; |
| 11 | |
| 12 | using TestInherit = |
| 13 | sdbusplus::server::object::object<sdbusplus::server::server::Test>; |
| 14 | |
| 15 | class 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 | |
| 25 | TEST_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 | |
| 49 | TEST_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 | |
| 82 | TEST_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 | } |
Patrick Williams | c67e1e8 | 2020-11-04 12:39:24 -0600 | [diff] [blame] | 106 | |
| 107 | TEST_F(Object, DoubleHasDefaultValues) |
| 108 | { |
| 109 | // Simulate the typical usage of a service |
| 110 | sdbusplus::server::manager::manager objManager(bus, objPath); |
| 111 | bus.request_name(busName); |
| 112 | |
| 113 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 114 | .Times(1); |
| 115 | EXPECT_CALL(sdbusMock, |
| 116 | sd_bus_emit_interfaces_added_strv(_, StrEq(objPath), _)) |
| 117 | .Times(0); |
| 118 | |
| 119 | auto test = std::make_unique<TestInherit>(bus, objPath); |
| 120 | EXPECT_TRUE(std::isnan(test->doubleAsNAN())); |
| 121 | EXPECT_TRUE(std::isinf(test->doubleAsInf()) && |
| 122 | !std::signbit(test->doubleAsInf())); |
| 123 | EXPECT_TRUE(std::isinf(test->doubleAsNegInf()) && |
| 124 | std::signbit(test->doubleAsNegInf())); |
| 125 | EXPECT_EQ(std::numeric_limits<double>::epsilon(), test->doubleAsEpsilon()); |
| 126 | } |