1 // Copyright (c) Benjamin Kietzman (github.com/bkietz) 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #include <dbus/connection.hpp> 7 #include <dbus/message.hpp> 8 #include <dbus/endpoint.hpp> 9 #include <dbus/filter.hpp> 10 #include <dbus/match.hpp> 11 #include <dbus/functional.hpp> 12 13 #include <gtest/gtest.h> 14 #include <unistd.h> 15 #include <utility/record_property.hpp> 16 17 18 using namespace boost::asio; 19 using namespace dbus; 20 using boost::system::error_code; 21 22 23 class AvahiTest 24 : public testing::Test 25 { 26 protected: 27 static void SetUpTestCase() 28 { 29 } 30 static io_service io; 31 static string browser_path; 32 static endpoint avahi_daemon; 33 }; 34 // It seems like these should be non-static, 35 // but I get a mysterious SEGFAULT for io 36 // ¿related: http://stackoverflow.com/questions/18009156/boost-asio-segfault-no-idea-why 37 io_service AvahiTest::io; 38 string AvahiTest::browser_path; 39 endpoint AvahiTest::avahi_daemon( 40 "org.freedesktop.Avahi", 41 "/", 42 "org.freedesktop.Avahi.Server"); 43 44 struct compare_hostnames 45 { 46 io_service& io; 47 connection& system_bus; 48 49 void operator()(error_code ec, message r) 50 { 51 string avahi_hostname; 52 string unix_hostname; 53 54 { 55 // get hostname from a system call 56 char c[1024]; 57 gethostname(c, 1024); 58 unix_hostname = c; 59 } 60 61 r.unpack(avahi_hostname); 62 63 // this is only usually accurate 64 EXPECT_EQ(unix_hostname, avahi_hostname); 65 66 // eventually, connection should stop itself 67 io.stop(); 68 } 69 }; 70 71 bool member_is_itemnew(message& m) 72 { 73 return m.get_member() == "ItemNew"; 74 } 75 76 struct handle_dispatch 77 { 78 io_service& io; 79 80 void operator()(error_code ec, message s) 81 { 82 record_property("firstSignal") << s.get_member(); 83 io.stop(); 84 } 85 }; 86 87 TEST_F(AvahiTest, GetHostName) 88 { 89 connection system_bus(io, "unix:path=/var/run/dbus/system_bus_socket"); 90 91 { 92 message m = message::new_call( 93 avahi_daemon, 94 "GetHostName"); 95 96 system_bus.async_send(m, 97 (compare_hostnames){ io, system_bus }); 98 } 99 100 io.run(); 101 /* 102 } 103 104 105 TEST_F(AvahiTest, ServiceBrowser) 106 { 107 connection system_bus(io, bus::system); 108 */ 109 110 { 111 // create new service browser 112 message m = message::new_call( 113 avahi_daemon, 114 "ServiceBrowserNew"); 115 116 m.pack<int32>(-1) 117 .pack<int32>(-1) 118 .pack<string>("_http._tcp") 119 .pack<string>("local") 120 .pack<uint32>(0); 121 122 message r = system_bus.send(m); 123 124 r.unpack(browser_path); 125 } 126 record_property("browserPath") << browser_path; 127 // RegEx match browser_path 128 // catch a possible exception 129 /* 130 } 131 132 133 134 TEST_F(AvahiTest, BrowseForHttp) 135 { 136 */ 137 io.reset(); 138 //connection system_bus1(io, bus::system); 139 140 match m(system_bus, "type='signal',path='" + browser_path + "'"); 141 filter f(system_bus, member_is_itemnew); 142 143 f.async_dispatch( 144 (handle_dispatch){ io }); 145 146 io.run(); 147 } 148