1 #include "gpio-presence/device_presence.hpp"
2 #include "gpio-presence/gpio_presence_manager.hpp"
3
4 #include <gpiod.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <xyz/openbmc_project/Inventory/Source/DevicePresence/client.hpp>
7
8 #include <gtest/gtest.h>
9
10 using namespace gpio_presence;
11
requestStop(sdbusplus::async::context & io)12 auto requestStop(sdbusplus::async::context& io) -> sdbusplus::async::task<>
13 {
14 io.request_stop();
15 co_return;
16 }
17
TEST(GpioPresence,ConstructionSucceeds)18 TEST(GpioPresence, ConstructionSucceeds)
19 {
20 sdbusplus::async::context ctx;
21
22 gpio_presence::GPIOPresenceManager s(ctx);
23
24 ctx.spawn(requestStop(ctx));
25 ctx.run();
26 }
27
TEST(GpioPresence,AcceptConfig1Gpio)28 TEST(GpioPresence, AcceptConfig1Gpio)
29 {
30 sdbusplus::async::context ctx;
31
32 gpio_presence::GPIOPresenceManager sensor(ctx);
33
34 std::string name = "cable0";
35 std::string gpioName = "TEST_GPIO";
36
37 std::vector<std::string> gpioNames = {gpioName};
38 std::vector<uint64_t> gpioValues = {0};
39
40 auto c = std::make_unique<gpio_presence::DevicePresence>(
41 ctx, gpioNames, gpioValues, name, sensor.gpioState);
42
43 sensor.addConfig(name, std::move(c));
44
45 sensor.updatePresence(gpioName, false);
46
47 EXPECT_EQ(sensor.getPresence(name), true);
48
49 sensor.updatePresence(gpioName, true);
50
51 EXPECT_EQ(sensor.getPresence(name), false);
52
53 ctx.spawn(requestStop(ctx));
54 ctx.run();
55 }
56
testDevicePresentDbus(sdbusplus::async::context & ctx)57 auto testDevicePresentDbus(sdbusplus::async::context& ctx)
58 -> sdbusplus::async::task<>
59 {
60 gpio_presence::GPIOPresenceManager sensor(ctx);
61
62 std::string busName = sensor.setupBusName();
63
64 std::string name = "cable0";
65 std::string gpioName = "TEST_GPIO";
66
67 std::vector<std::string> gpioNames = {gpioName};
68 std::vector<uint64_t> gpioValues = {0};
69
70 auto c = std::make_unique<gpio_presence::DevicePresence>(
71 ctx, gpioNames, gpioValues, name, sensor.gpioState);
72
73 sdbusplus::message::object_path objPath = c->getObjPath();
74
75 sensor.addConfig(name, std::move(c));
76
77 sensor.updatePresence(gpioName, false);
78
79 lg2::debug("found obj path {OBJPATH}", "OBJPATH", objPath);
80
81 auto client = sdbusplus::client::xyz::openbmc_project::inventory::source::
82 DevicePresence<>(ctx)
83 .service(busName)
84 .path(objPath.str);
85
86 std::string nameFound = co_await client.name();
87
88 assert(nameFound == "cable0");
89
90 ctx.request_stop();
91
92 co_return;
93 }
94
TEST(GpioPresence,DevicePresentDbus)95 TEST(GpioPresence, DevicePresentDbus)
96 {
97 sdbusplus::async::context ctx;
98 ctx.spawn(testDevicePresentDbus(ctx));
99 ctx.run();
100 }
101
testDevicePresentThenDisappearDbus(sdbusplus::async::context & ctx)102 auto testDevicePresentThenDisappearDbus(sdbusplus::async::context& ctx)
103 -> sdbusplus::async::task<>
104 {
105 gpio_presence::GPIOPresenceManager sensor(ctx);
106
107 std::string busName = sensor.setupBusName();
108
109 std::string name = "cable0";
110 std::string gpioName = "TEST_GPIO";
111
112 std::vector<std::string> gpioNames = {gpioName};
113 std::vector<uint64_t> gpioValues = {0};
114
115 auto c = std::make_unique<gpio_presence::DevicePresence>(
116 ctx, gpioNames, gpioValues, name, sensor.gpioState);
117
118 sdbusplus::message::object_path objPath = c->getObjPath();
119
120 sensor.addConfig(name, std::move(c));
121
122 sensor.updatePresence(gpioName, false);
123
124 lg2::debug("found obj path {OBJPATH}", "OBJPATH", objPath);
125
126 auto client = sdbusplus::client::xyz::openbmc_project::inventory::source::
127 DevicePresence<>(ctx)
128 .service(busName)
129 .path(objPath.str);
130
131 std::string nameFound = co_await client.name();
132
133 assert(nameFound == "cable0");
134
135 // gpio goes high, cable 0 should disappear
136 sensor.updatePresence(gpioName, true);
137
138 try
139 {
140 co_await client.name();
141 assert(false);
142 }
143 catch (std::exception& _)
144 {
145 // expected, since cable 0 is gone.
146 // have to do something here to shut up clang-tidy
147 std::cout << "" << std::endl;
148 }
149
150 ctx.request_stop();
151
152 co_return;
153 }
154
TEST(GpioPresence,DevicePresentThenDisappearDbus)155 TEST(GpioPresence, DevicePresentThenDisappearDbus)
156 {
157 sdbusplus::async::context ctx;
158 ctx.spawn(testDevicePresentThenDisappearDbus(ctx));
159 ctx.run();
160 }
161