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