xref: /openbmc/boost-dbus/test/avahi.cpp (revision a83e59511d9309d34475c90e2cf4ea15e11cd817)
1*a83e5951SBenjamin Kietzman // Copyright (c) Benjamin Kietzman (github.com/bkietz)
2*a83e5951SBenjamin Kietzman //
3*a83e5951SBenjamin Kietzman // Distributed under the Boost Software License, Version 1.0. (See accompanying
4*a83e5951SBenjamin Kietzman // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*a83e5951SBenjamin Kietzman 
6*a83e5951SBenjamin Kietzman #include <gtest/gtest.h>
7*a83e5951SBenjamin Kietzman #include <dbus/connection.hpp>
8*a83e5951SBenjamin Kietzman #include <dbus/message.hpp>
9*a83e5951SBenjamin Kietzman #include <dbus/filter.hpp>
10*a83e5951SBenjamin Kietzman #include <dbus/match.hpp>
11*a83e5951SBenjamin Kietzman #include <dbus/functional.hpp>
12*a83e5951SBenjamin Kietzman #include <unistd.h>
13*a83e5951SBenjamin Kietzman 
14*a83e5951SBenjamin Kietzman 
15*a83e5951SBenjamin Kietzman class AvahiTest
16*a83e5951SBenjamin Kietzman   : public testing::Test
17*a83e5951SBenjamin Kietzman {
18*a83e5951SBenjamin Kietzman protected:
19*a83e5951SBenjamin Kietzman   static void SetUpTestCase()
20*a83e5951SBenjamin Kietzman   {
21*a83e5951SBenjamin Kietzman   }
22*a83e5951SBenjamin Kietzman   static boost::asio::io_service io;
23*a83e5951SBenjamin Kietzman   static dbus::connection system_bus;
24*a83e5951SBenjamin Kietzman   static dbus::string browser_path;
25*a83e5951SBenjamin Kietzman };
26*a83e5951SBenjamin Kietzman // It seems like these should be non-static,
27*a83e5951SBenjamin Kietzman // but I get a mysterious SEGFAULT for io
28*a83e5951SBenjamin Kietzman //   ¿related: http://stackoverflow.com/questions/18009156/boost-asio-segfault-no-idea-why
29*a83e5951SBenjamin Kietzman // and a C++ exception with description
30*a83e5951SBenjamin Kietzman // "assign: File exists" for system_bus
31*a83e5951SBenjamin Kietzman // (probably indicates I should upgrade connection's constructor)
32*a83e5951SBenjamin Kietzman boost::asio::io_service AvahiTest::io;
33*a83e5951SBenjamin Kietzman dbus::connection AvahiTest::system_bus(io, dbus::bus::system);
34*a83e5951SBenjamin Kietzman dbus::string AvahiTest::browser_path;
35*a83e5951SBenjamin Kietzman 
36*a83e5951SBenjamin Kietzman 
37*a83e5951SBenjamin Kietzman TEST_F(AvahiTest, GetHostName)
38*a83e5951SBenjamin Kietzman {
39*a83e5951SBenjamin Kietzman   using namespace boost::asio;
40*a83e5951SBenjamin Kietzman   using namespace dbus;
41*a83e5951SBenjamin Kietzman   using boost::system::error_code;
42*a83e5951SBenjamin Kietzman 
43*a83e5951SBenjamin Kietzman   string avahi_hostname;
44*a83e5951SBenjamin Kietzman   string unix_hostname;
45*a83e5951SBenjamin Kietzman 
46*a83e5951SBenjamin Kietzman   {
47*a83e5951SBenjamin Kietzman     // get hostname from a system call
48*a83e5951SBenjamin Kietzman     char c[1024];
49*a83e5951SBenjamin Kietzman     gethostname(c, 1024);
50*a83e5951SBenjamin Kietzman     unix_hostname = c;
51*a83e5951SBenjamin Kietzman   }
52*a83e5951SBenjamin Kietzman 
53*a83e5951SBenjamin Kietzman   // get hostname from the Avahi daemon
54*a83e5951SBenjamin Kietzman   message m = message::new_call(
55*a83e5951SBenjamin Kietzman     "org.freedesktop.Avahi",
56*a83e5951SBenjamin Kietzman     "/",
57*a83e5951SBenjamin Kietzman     "org.freedesktop.Avahi.Server",
58*a83e5951SBenjamin Kietzman     "GetHostName");
59*a83e5951SBenjamin Kietzman 
60*a83e5951SBenjamin Kietzman   system_bus.async_send(m, [&](error_code ec, message r){
61*a83e5951SBenjamin Kietzman     r.unpack(avahi_hostname);
62*a83e5951SBenjamin Kietzman 
63*a83e5951SBenjamin Kietzman     // this is only usually accurate
64*a83e5951SBenjamin Kietzman     ASSERT_EQ(unix_hostname, avahi_hostname);
65*a83e5951SBenjamin Kietzman 
66*a83e5951SBenjamin Kietzman     // eventually, connection should stop itself
67*a83e5951SBenjamin Kietzman     io.stop();
68*a83e5951SBenjamin Kietzman   });
69*a83e5951SBenjamin Kietzman 
70*a83e5951SBenjamin Kietzman   io.run();
71*a83e5951SBenjamin Kietzman }
72*a83e5951SBenjamin Kietzman 
73*a83e5951SBenjamin Kietzman 
74*a83e5951SBenjamin Kietzman TEST_F(AvahiTest, ServiceBrowser)
75*a83e5951SBenjamin Kietzman {
76*a83e5951SBenjamin Kietzman   using namespace boost::asio;
77*a83e5951SBenjamin Kietzman   using namespace dbus;
78*a83e5951SBenjamin Kietzman   using boost::system::error_code;
79*a83e5951SBenjamin Kietzman 
80*a83e5951SBenjamin Kietzman   // create new service browser
81*a83e5951SBenjamin Kietzman   message m = message::new_call(
82*a83e5951SBenjamin Kietzman     "org.freedesktop.Avahi",
83*a83e5951SBenjamin Kietzman     "/",
84*a83e5951SBenjamin Kietzman     "org.freedesktop.Avahi.Server",
85*a83e5951SBenjamin Kietzman     "ServiceBrowserNew");
86*a83e5951SBenjamin Kietzman 
87*a83e5951SBenjamin Kietzman   m.pack<int32>(-1)
88*a83e5951SBenjamin Kietzman    .pack<int32>(-1)
89*a83e5951SBenjamin Kietzman    .pack<string>("_http._tcp")
90*a83e5951SBenjamin Kietzman    .pack<string>("local")
91*a83e5951SBenjamin Kietzman    .pack<uint32>(0);
92*a83e5951SBenjamin Kietzman 
93*a83e5951SBenjamin Kietzman   message r = system_bus.send(m);
94*a83e5951SBenjamin Kietzman 
95*a83e5951SBenjamin Kietzman   r.unpack(browser_path);
96*a83e5951SBenjamin Kietzman 
97*a83e5951SBenjamin Kietzman   // RegEx match browser_path
98*a83e5951SBenjamin Kietzman   // catch a possible exception
99*a83e5951SBenjamin Kietzman }
100*a83e5951SBenjamin Kietzman 
101*a83e5951SBenjamin Kietzman 
102*a83e5951SBenjamin Kietzman TEST_F(AvahiTest, BrowseForHttp)
103*a83e5951SBenjamin Kietzman {
104*a83e5951SBenjamin Kietzman   using namespace boost::asio;
105*a83e5951SBenjamin Kietzman   using namespace dbus;
106*a83e5951SBenjamin Kietzman   using boost::system::error_code;
107*a83e5951SBenjamin Kietzman 
108*a83e5951SBenjamin Kietzman   match m(system_bus, "type='signal',path='" + browser_path + "'");
109*a83e5951SBenjamin Kietzman   filter f(system_bus, [](message& m){
110*a83e5951SBenjamin Kietzman     return m.get_member() == "ItemNew"; });
111*a83e5951SBenjamin Kietzman 
112*a83e5951SBenjamin Kietzman   function<void(error_code, message)> h;
113*a83e5951SBenjamin Kietzman   h = [&] (error_code ec, message m) {};
114*a83e5951SBenjamin Kietzman   f.async_dispatch(h);
115*a83e5951SBenjamin Kietzman   io.run();
116*a83e5951SBenjamin Kietzman }
117