xref: /openbmc/boost-dbus/test/avahi.cpp (revision 77e62c83d54e15716b16dcaae403b273665f0d50)
1a83e5951SBenjamin Kietzman // Copyright (c) Benjamin Kietzman (github.com/bkietz)
2a83e5951SBenjamin Kietzman //
3a83e5951SBenjamin Kietzman // Distributed under the Boost Software License, Version 1.0. (See accompanying
4a83e5951SBenjamin Kietzman // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5a83e5951SBenjamin Kietzman 
6a83e5951SBenjamin Kietzman #include <dbus/connection.hpp>
716d80fe9SBenjamin Kietzman #include <dbus/endpoint.hpp>
8a83e5951SBenjamin Kietzman #include <dbus/filter.hpp>
9a83e5951SBenjamin Kietzman #include <dbus/match.hpp>
10da3eeb6aSEd Tanous #include <dbus/message.hpp>
11da3eeb6aSEd Tanous #include <functional>
12b2c2467dSBenjamin Kietzman 
13a83e5951SBenjamin Kietzman #include <unistd.h>
14b6e8327aSEd Tanous #include <gmock/gmock.h>
15da3eeb6aSEd Tanous #include <gtest/gtest.h>
16b2c2467dSBenjamin Kietzman 
17da3eeb6aSEd Tanous TEST(AvahiTest, GetHostName) {
18da3eeb6aSEd Tanous   dbus::endpoint test_daemon("org.freedesktop.Avahi", "/",
1916d80fe9SBenjamin Kietzman                              "org.freedesktop.Avahi.Server");
20da3eeb6aSEd Tanous   boost::asio::io_service io;
21b573e22eSEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
22a83e5951SBenjamin Kietzman 
23da3eeb6aSEd Tanous   dbus::message m = dbus::message::new_call(test_daemon, "GetHostName");
24a83e5951SBenjamin Kietzman 
25b573e22eSEd Tanous   system_bus->async_send(
26da3eeb6aSEd Tanous       m, [&](const boost::system::error_code ec, dbus::message r) {
27a83e5951SBenjamin Kietzman 
28da3eeb6aSEd Tanous         std::string avahi_hostname;
29da3eeb6aSEd Tanous         std::string hostname;
30da3eeb6aSEd Tanous 
31a83e5951SBenjamin Kietzman         // get hostname from a system call
32a83e5951SBenjamin Kietzman         char c[1024];
33a83e5951SBenjamin Kietzman         gethostname(c, 1024);
34da3eeb6aSEd Tanous         hostname = c;
35a83e5951SBenjamin Kietzman 
36b2c2467dSBenjamin Kietzman         r.unpack(avahi_hostname);
37b2c2467dSBenjamin Kietzman 
38da3eeb6aSEd Tanous         // Get only the host name, not the fqdn
39da3eeb6aSEd Tanous         auto unix_hostname = hostname.substr(0, hostname.find("."));
40b2c2467dSBenjamin Kietzman         EXPECT_EQ(unix_hostname, avahi_hostname);
41b2c2467dSBenjamin Kietzman 
42b2c2467dSBenjamin Kietzman         io.stop();
43da3eeb6aSEd Tanous       });
44da3eeb6aSEd Tanous   boost::asio::deadline_timer t(io, boost::posix_time::seconds(10));
45da3eeb6aSEd Tanous   t.async_wait([&](const boost::system::error_code& /*e*/) {
46b2c2467dSBenjamin Kietzman     io.stop();
47da3eeb6aSEd Tanous     FAIL() << "Callback was never called\n";
48da3eeb6aSEd Tanous   });
49a83e5951SBenjamin Kietzman   io.run();
50a83e5951SBenjamin Kietzman }
51a83e5951SBenjamin Kietzman 
52da3eeb6aSEd Tanous TEST(AvahiTest, ServiceBrowser) {
53da3eeb6aSEd Tanous   boost::asio::io_service io;
54b573e22eSEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
55a83e5951SBenjamin Kietzman 
56da3eeb6aSEd Tanous   dbus::endpoint test_daemon("org.freedesktop.Avahi", "/",
57da3eeb6aSEd Tanous                              "org.freedesktop.Avahi.Server");
58a83e5951SBenjamin Kietzman   // create new service browser
59da3eeb6aSEd Tanous   dbus::message m1 = dbus::message::new_call(test_daemon, "ServiceBrowserNew");
60da3eeb6aSEd Tanous   m1.pack<int32_t>(-1)
61da3eeb6aSEd Tanous       .pack<int32_t>(-1)
62da3eeb6aSEd Tanous       .pack<std::string>("_http._tcp")
63da3eeb6aSEd Tanous       .pack<std::string>("local")
64da3eeb6aSEd Tanous       .pack<uint32_t>(0);
65a83e5951SBenjamin Kietzman 
66b573e22eSEd Tanous   dbus::message r = system_bus->send(m1);
67da3eeb6aSEd Tanous   std::string browser_path;
68a83e5951SBenjamin Kietzman   r.unpack(browser_path);
69da3eeb6aSEd Tanous   testing::Test::RecordProperty("browserPath", browser_path);
70a83e5951SBenjamin Kietzman 
71da3eeb6aSEd Tanous   dbus::match ma(system_bus, "type='signal',path='" + browser_path + "'");
72da3eeb6aSEd Tanous   dbus::filter f(system_bus, [](dbus::message& m) {
73da3eeb6aSEd Tanous     auto member = m.get_member();
74da3eeb6aSEd Tanous     return member == "NameAcquired";
75da3eeb6aSEd Tanous   });
76a83e5951SBenjamin Kietzman 
77da3eeb6aSEd Tanous   std::function<void(boost::system::error_code, dbus::message)> event_handler =
78da3eeb6aSEd Tanous       [&](boost::system::error_code ec, dbus::message s) {
79da3eeb6aSEd Tanous         testing::Test::RecordProperty("firstSignal", s.get_member());
80da3eeb6aSEd Tanous         std::string a = s.get_member();
81da3eeb6aSEd Tanous         std::string dude;
82da3eeb6aSEd Tanous         s.unpack(dude);
83da3eeb6aSEd Tanous         f.async_dispatch(event_handler);
84da3eeb6aSEd Tanous         io.stop();
85da3eeb6aSEd Tanous       };
86da3eeb6aSEd Tanous   f.async_dispatch(event_handler);
87b2c2467dSBenjamin Kietzman 
88da3eeb6aSEd Tanous   boost::asio::deadline_timer t(io, boost::posix_time::seconds(10));
89da3eeb6aSEd Tanous   t.async_wait([&](const boost::system::error_code& /*e*/) {
90da3eeb6aSEd Tanous     io.stop();
91da3eeb6aSEd Tanous     FAIL() << "Callback was never called\n";
92da3eeb6aSEd Tanous   });
93a83e5951SBenjamin Kietzman   io.run();
94a83e5951SBenjamin Kietzman }
95b6e8327aSEd Tanous 
96b6e8327aSEd Tanous TEST(BOOST_DBUS, ListServices) {
97b6e8327aSEd Tanous   boost::asio::io_service io;
98b6e8327aSEd Tanous   boost::asio::deadline_timer t(io, boost::posix_time::seconds(10));
99b6e8327aSEd Tanous   t.async_wait([&](const boost::system::error_code& /*e*/) {
100b6e8327aSEd Tanous     io.stop();
101b6e8327aSEd Tanous     FAIL() << "Callback was never called\n";
102b6e8327aSEd Tanous   });
103b6e8327aSEd Tanous 
104b573e22eSEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
105b6e8327aSEd Tanous 
106b6e8327aSEd Tanous   dbus::endpoint test_daemon("org.freedesktop.DBus", "/",
107b6e8327aSEd Tanous                              "org.freedesktop.DBus");
108b6e8327aSEd Tanous   // create new service browser
109b6e8327aSEd Tanous   dbus::message m = dbus::message::new_call(test_daemon, "ListNames");
110b573e22eSEd Tanous   system_bus->async_send(
111b6e8327aSEd Tanous       m, [&](const boost::system::error_code ec, dbus::message r) {
112b6e8327aSEd Tanous         io.stop();
113b6e8327aSEd Tanous         std::vector<std::string> services;
114b6e8327aSEd Tanous         r.unpack(services);
115b6e8327aSEd Tanous         // Test a couple things that should always be present.... adapt if
116b6e8327aSEd Tanous         // neccesary
117b6e8327aSEd Tanous         EXPECT_THAT(services, testing::Contains("org.freedesktop.DBus"));
118b6e8327aSEd Tanous         EXPECT_THAT(services, testing::Contains("org.freedesktop.Accounts"));
119b6e8327aSEd Tanous 
120b6e8327aSEd Tanous       });
121b6e8327aSEd Tanous 
122b6e8327aSEd Tanous   io.run();
123b6e8327aSEd Tanous }
124b6e8327aSEd Tanous 
1255d4bd2bdSEd Tanous TEST(BOOST_DBUS, SingleSensorChanged) {
126b6e8327aSEd Tanous   boost::asio::io_service io;
127b6e8327aSEd Tanous 
128b573e22eSEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
129b573e22eSEd Tanous 
130*77e62c83SEd Tanous   dbus::match ma(system_bus,
131*77e62c83SEd Tanous                  "type='signal',path_namespace='/xyz/openbmc_project/sensors'");
132b573e22eSEd Tanous 
13382a51ce2SEd Tanous   dbus::filter f(system_bus, [](dbus::message& m) {
13482a51ce2SEd Tanous     auto member = m.get_member();
13582a51ce2SEd Tanous     return member == "PropertiesChanged";
13682a51ce2SEd Tanous   });
137b6e8327aSEd Tanous 
1385d4bd2bdSEd Tanous   f.async_dispatch([&](boost::system::error_code ec, dbus::message s) {
13982a51ce2SEd Tanous     std::string object_name;
1405d4bd2bdSEd Tanous     EXPECT_EQ(s.get_path(),
1415d4bd2bdSEd Tanous               "/xyz/openbmc_project/sensors/temperature/LR_Brd_Temp");
1425d4bd2bdSEd Tanous 
14382a51ce2SEd Tanous     std::vector<std::pair<std::string, dbus::dbus_variant>> values;
1440d6f56d2SEd Tanous     s.unpack(object_name, values);
14582a51ce2SEd Tanous     EXPECT_EQ(object_name, "xyz.openbmc_project.Sensor.Value");
14682a51ce2SEd Tanous 
1475d4bd2bdSEd Tanous     EXPECT_EQ(values.size(), 1);
14882a51ce2SEd Tanous     auto expected = std::pair<std::string, dbus::dbus_variant>("Value", 42);
1495d4bd2bdSEd Tanous     EXPECT_EQ(values[0], expected);
15082a51ce2SEd Tanous 
151b6e8327aSEd Tanous     io.stop();
1525d4bd2bdSEd Tanous   });
153b6e8327aSEd Tanous 
15482a51ce2SEd Tanous   dbus::endpoint test_endpoint(
15582a51ce2SEd Tanous       "org.freedesktop.Avahi",
15682a51ce2SEd Tanous       "/xyz/openbmc_project/sensors/temperature/LR_Brd_Temp",
15782a51ce2SEd Tanous       "org.freedesktop.DBus.Properties");
158b6e8327aSEd Tanous 
15982a51ce2SEd Tanous   auto signal_name = std::string("PropertiesChanged");
16082a51ce2SEd Tanous   auto m = dbus::message::new_signal(test_endpoint, signal_name);
16182a51ce2SEd Tanous 
16282a51ce2SEd Tanous   m.pack("xyz.openbmc_project.Sensor.Value");
16382a51ce2SEd Tanous 
16482a51ce2SEd Tanous   std::vector<std::pair<std::string, dbus::dbus_variant>> map2;
16582a51ce2SEd Tanous 
16682a51ce2SEd Tanous   map2.emplace_back("Value", 42);
16782a51ce2SEd Tanous 
16882a51ce2SEd Tanous   m.pack(map2);
16982a51ce2SEd Tanous 
17082a51ce2SEd Tanous   auto removed = std::vector<uint32_t>();
17182a51ce2SEd Tanous   m.pack(removed);
172b573e22eSEd Tanous   system_bus->async_send(m,
17382a51ce2SEd Tanous                          [&](boost::system::error_code ec, dbus::message s) {});
174b6e8327aSEd Tanous 
175b6e8327aSEd Tanous   io.run();
176b6e8327aSEd Tanous }
1775d4bd2bdSEd Tanous 
1785d4bd2bdSEd Tanous TEST(BOOST_DBUS, MultipleSensorChanged) {
1795d4bd2bdSEd Tanous   boost::asio::io_service io;
180b573e22eSEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
1815d4bd2bdSEd Tanous 
1825d4bd2bdSEd Tanous   dbus::match ma(system_bus,
1835d4bd2bdSEd Tanous                  "type='signal',path_namespace='/xyz/openbmc_project/sensors'");
1845d4bd2bdSEd Tanous   dbus::filter f(system_bus, [](dbus::message& m) {
1855d4bd2bdSEd Tanous     auto member = m.get_member();
1865d4bd2bdSEd Tanous     return member == "PropertiesChanged";
1875d4bd2bdSEd Tanous   });
1885d4bd2bdSEd Tanous 
1895d4bd2bdSEd Tanous   int count = 0;
190b573e22eSEd Tanous   std::function<void(boost::system::error_code, dbus::message)> callback = [&](
191b573e22eSEd Tanous       boost::system::error_code ec, dbus::message s) {
1925d4bd2bdSEd Tanous     std::string object_name;
1935d4bd2bdSEd Tanous     EXPECT_EQ(s.get_path(),
1945d4bd2bdSEd Tanous               "/xyz/openbmc_project/sensors/temperature/LR_Brd_Temp");
1955d4bd2bdSEd Tanous 
1965d4bd2bdSEd Tanous     std::vector<std::pair<std::string, dbus::dbus_variant>> values;
1970d6f56d2SEd Tanous     s.unpack(object_name, values);
1985d4bd2bdSEd Tanous     EXPECT_EQ(object_name, "xyz.openbmc_project.Sensor.Value");
1995d4bd2bdSEd Tanous 
2005d4bd2bdSEd Tanous     EXPECT_EQ(values.size(), 1);
2015d4bd2bdSEd Tanous     auto expected = std::pair<std::string, dbus::dbus_variant>("Value", 42);
2025d4bd2bdSEd Tanous     EXPECT_EQ(values[0], expected);
2035d4bd2bdSEd Tanous     count++;
2045d4bd2bdSEd Tanous     if (count == 2) {
2055d4bd2bdSEd Tanous       io.stop();
206b573e22eSEd Tanous     } else {
207b573e22eSEd Tanous       f.async_dispatch(callback);
2085d4bd2bdSEd Tanous     }
2090d6f56d2SEd Tanous     s.unpack(object_name, values);
2105d4bd2bdSEd Tanous 
211b573e22eSEd Tanous   };
212b573e22eSEd Tanous   f.async_dispatch(callback);
2135d4bd2bdSEd Tanous 
2145d4bd2bdSEd Tanous   dbus::endpoint test_endpoint(
2155d4bd2bdSEd Tanous       "org.freedesktop.Avahi",
2165d4bd2bdSEd Tanous       "/xyz/openbmc_project/sensors/temperature/LR_Brd_Temp",
2175d4bd2bdSEd Tanous       "org.freedesktop.DBus.Properties");
2185d4bd2bdSEd Tanous 
2195d4bd2bdSEd Tanous   auto signal_name = std::string("PropertiesChanged");
2205d4bd2bdSEd Tanous 
2215d4bd2bdSEd Tanous   std::vector<std::pair<std::string, dbus::dbus_variant>> map2;
2225d4bd2bdSEd Tanous 
2235d4bd2bdSEd Tanous   map2.emplace_back("Value", 42);
2245d4bd2bdSEd Tanous 
225*77e62c83SEd Tanous   static auto removed = std::vector<uint32_t>();
2265d4bd2bdSEd Tanous 
227*77e62c83SEd Tanous   auto m = dbus::message::new_signal(test_endpoint, signal_name);
228*77e62c83SEd Tanous   m.pack("xyz.openbmc_project.Sensor.Value", map2, removed);
229*77e62c83SEd Tanous 
230*77e62c83SEd Tanous   system_bus->send(m, std::chrono::seconds(0));
231*77e62c83SEd Tanous   system_bus->send(m, std::chrono::seconds(0));
232*77e62c83SEd Tanous 
2335d4bd2bdSEd Tanous   io.run();
2345d4bd2bdSEd Tanous }
235458a9c10SVernon Mauery 
236458a9c10SVernon Mauery TEST(BOOST_DBUS, MethodCall) {
237458a9c10SVernon Mauery   boost::asio::io_service io;
238*77e62c83SEd Tanous 
239*77e62c83SEd Tanous   boost::asio::deadline_timer t(io, boost::posix_time::seconds(2));
240*77e62c83SEd Tanous   t.async_wait([&](const boost::system::error_code&) {
241458a9c10SVernon Mauery     io.stop();
242458a9c10SVernon Mauery     FAIL() << "Callback was never called\n";
243458a9c10SVernon Mauery   });
244458a9c10SVernon Mauery 
2450d6f56d2SEd Tanous   auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
2460d6f56d2SEd Tanous   std::string requested_name = system_bus->get_unique_name();
247458a9c10SVernon Mauery 
248458a9c10SVernon Mauery   dbus::filter f(system_bus, [](dbus::message& m) {
249458a9c10SVernon Mauery     return (m.get_member() == "Get" &&
250458a9c10SVernon Mauery             m.get_interface() == "org.freedesktop.DBus.Properties" &&
251458a9c10SVernon Mauery             m.get_signature() == "ss");
252458a9c10SVernon Mauery   });
253458a9c10SVernon Mauery 
254458a9c10SVernon Mauery   std::function<void(boost::system::error_code, dbus::message)> method_handler =
255458a9c10SVernon Mauery       [&](boost::system::error_code ec, dbus::message s) {
2560d6f56d2SEd Tanous         if (ec) {
2570d6f56d2SEd Tanous           FAIL() << ec;
2580d6f56d2SEd Tanous         } else {
259458a9c10SVernon Mauery           std::string intf_name, prop_name;
2600d6f56d2SEd Tanous           s.unpack(intf_name, prop_name);
261458a9c10SVernon Mauery 
262458a9c10SVernon Mauery           EXPECT_EQ(intf_name, "xyz.openbmc_project.fwupdate1");
263458a9c10SVernon Mauery           EXPECT_EQ(prop_name, "State");
264458a9c10SVernon Mauery 
265458a9c10SVernon Mauery           // send a reply so dbus doesn't get angry?
266b573e22eSEd Tanous           auto r = system_bus->reply(s);
267458a9c10SVernon Mauery           r.pack("IDLE");
268*77e62c83SEd Tanous           system_bus->async_send(
269*77e62c83SEd Tanous               r, [&](boost::system::error_code ec, dbus::message s) {});
270458a9c10SVernon Mauery           io.stop();
2710d6f56d2SEd Tanous         }
272458a9c10SVernon Mauery       };
273458a9c10SVernon Mauery   f.async_dispatch(method_handler);
274458a9c10SVernon Mauery 
275*77e62c83SEd Tanous   dbus::endpoint test_endpoint(requested_name, "/xyz/openbmc_project/fwupdate1",
276458a9c10SVernon Mauery                                "org.freedesktop.DBus.Properties");
277458a9c10SVernon Mauery 
278458a9c10SVernon Mauery   auto method_name = std::string("Get");
279458a9c10SVernon Mauery   auto m = dbus::message::new_call(test_endpoint, method_name);
280458a9c10SVernon Mauery 
2810d6f56d2SEd Tanous   m.pack("xyz.openbmc_project.fwupdate1", "State");
282*77e62c83SEd Tanous   system_bus->async_send(m, [&](boost::system::error_code ec, dbus::message s) {
283458a9c10SVernon Mauery     std::cerr << "received s: " << s << std::endl;
284458a9c10SVernon Mauery   });
285458a9c10SVernon Mauery 
286*77e62c83SEd Tanous   // system_bus->send(m, std::chrono::seconds(0));
287*77e62c83SEd Tanous 
288458a9c10SVernon Mauery   io.run();
289458a9c10SVernon Mauery }
290