summaryrefslogtreecommitdiff
path: root/tests/twisted/avahitest.py
blob: f9c1d7b2104a22dc7f8428ca2a1c84b8037aca2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Infrastructure for testing avahi
"""

import servicetest
from servicetest import Event, TimeoutError
import dbus
import dbus.glib
import avahi

def get_server():
  bus = dbus.SystemBus()
  server = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
            avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
  return server

def get_host_name():
  return get_server().GetHostName()

def get_host_name_fqdn():
  return get_server().GetHostNameFqdn()

def get_domain_name():
  return get_server().GetDomainName()

def txt_get_key(txt, key):
  for x in txt:
      if dbus.Byte('=') in x:
        (rkey, value) = avahi.byte_array_to_string(x).split('=', 1)
        if rkey == key:
            return value

  return None

class AvahiService:
    def __init__(self, event_queue, bus, server, interface, protocol, name,
        type, domain, aprotocol, flags):

        self.event_queue = event_queue
        self.server = server
        self.bus = bus
        self.interface = interface
        self.protocol = protocol
        self.name = name
        self.type = type
        self.domain = domain
        self.aprotocol = aprotocol
        self.flags = flags

    def resolve(self):
        resolver_path = self.server.ServiceResolverNew(self.interface,
            self.protocol, self.name, self.type, self.domain,
            self.aprotocol, self.flags)

        resolver_obj = self.bus.get_object(avahi.DBUS_NAME, resolver_path)
        resolver = dbus.Interface(resolver_obj,
            avahi.DBUS_INTERFACE_SERVICE_RESOLVER)

        resolver.connect_to_signal('Found', self._service_found)
        resolver.connect_to_signal('Failed', self._service_failed)

    def _service_found(self, interface, protocol, name, stype, domain,
        host_name, aprotocol, pt, port, txt, flags):

        e = Event('service-resolved', service = self,
          interface = interface, protocol = protocol, name = name,
          stype = stype, host_name = host_name, aprotocol = aprotocol,
          pt = pt, port = port, txt = txt, flags = flags)

        self.event_queue.append(e)

    def _service_failed(self, error):
      e = Event('service-failed', service = self, error = error)
      self.event_queue.append(e)

class AvahiListener:
    def __init__(self, event_queue):
        self.event_queue = event_queue

        self.bus = dbus.SystemBus()
        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME,
            avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
        self.browsers = []

    def _service_added_cb(self, interface, protocol, name, stype, domain,
        flags):

        service = AvahiService(self.event_queue, self.bus, self.server,
          interface, protocol, name, stype,
          domain, protocol, 0)

        e = Event ('service-added', service = service,
          interface=interface, protocol=protocol, name=name, stype=stype,
          domain=domain, flags=flags)

        self.event_queue.append(e)

    def _service_removed_cb(self, interface, protocol, name, stype, domain,
        flags):

        e = Event ('service-removed',
          interface=interface, protocol=protocol, name=name, stype=stype,
          domain=domain, flags=flags)
        self.event_queue.append(e)

    def _all_for_now_cb(self):
        self.event_queue.append(Event('service-all-for-now'))

    def listen_for_service(self, sname):
        browser_path = self.server.ServiceBrowserNew(avahi.IF_UNSPEC,
            avahi.PROTO_UNSPEC, sname, "local", dbus.UInt32(0));
        browser_obj = self.bus.get_object(avahi.DBUS_NAME, browser_path)
        browser = dbus.Interface(browser_obj,
            avahi.DBUS_INTERFACE_SERVICE_BROWSER)

        browser.connect_to_signal('ItemNew', self._service_added_cb)
        browser.connect_to_signal('ItemRemove', self._service_removed_cb)
        browser.connect_to_signal('AllForNow', self._all_for_now_cb)

        self.browsers.append(browser)
        return self

class AvahiRecordAnnouncer:
    def __init__(self, name, clazz, type, data):
        self.name = name
        self.clazz = clazz
        self.type = type
        self.data = data

        self.bus = dbus.SystemBus()
        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME,
            avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)

        entry_path = self.server.EntryGroupNew()
        entry_obj = self.bus.get_object(avahi.DBUS_NAME, entry_path)
        entry = dbus.Interface(entry_obj,
            avahi.DBUS_INTERFACE_ENTRY_GROUP)

        entry.AddRecord(avahi.IF_UNSPEC, avahi.PROTO_INET,
            dbus.UInt32(0), name, clazz, type, 120, data)

        entry.Commit()

        self.entry = entry

class AvahiAnnouncer:
    def __init__(self, name, type, port, txt, hostname = get_host_name_fqdn(),
            proto=avahi.PROTO_INET):
        self.name = name
        self.type = type
        self.port = port
        self.txt = txt
        self.proto = proto

        self.bus = dbus.SystemBus()
        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME,
            avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)

        entry_path = self.server.EntryGroupNew()
        entry_obj = self.bus.get_object(avahi.DBUS_NAME, entry_path)
        entry = dbus.Interface(entry_obj,
            avahi.DBUS_INTERFACE_ENTRY_GROUP)

        entry.AddService(avahi.IF_UNSPEC, self.proto,
            dbus.UInt32(0), name, type, get_domain_name(), hostname,
            port, avahi.dict_to_txt_array(txt))
        entry.Commit()

        self.entry = entry

    def update(self, txt):
      self.txt.update(txt)

      self.entry.UpdateServiceTxt(avahi.IF_UNSPEC, self.proto,
        dbus.UInt32(0), self.name, self.type, get_domain_name(),
        avahi.dict_to_txt_array(self.txt))

    def set(self, txt):
      self.txt = txt
      self.entry.UpdateServiceTxt(avahi.IF_UNSPEC, self.proto,
        dbus.UInt32(0), self.name, self.type, get_domain_name(),
        avahi.dict_to_txt_array(self.txt))

    def stop(self):
        self.entry.Free()

def check_ipv6_enabled(q, announcer):
    # Avahi doesn't complain if we try to announce an IPv6 service with a
    # not IPv6 enabled Avahi (http://www.avahi.org/ticket/264) so we try to
    # resolve our own service to check if it has been actually announced.
    service = AvahiService(q, announcer.bus, announcer.server,
        avahi.IF_UNSPEC, announcer.proto, announcer.name,
        announcer.type, get_domain_name(), avahi.PROTO_INET6, 0)
    service.resolve()

    try:
        q.expect('service-resolved', service=service)
        return True
    except TimeoutError:
        return False

def has_another_llxmpp():
    q = servicetest.IteratingEventQueue()

    l = AvahiListener(q)
    l.listen_for_service('_presence._tcp')

    has = False

    while True:
        e = q.wait(['service'])

        if (e.type == 'service-added' and
            e.flags & (avahi.LOOKUP_RESULT_LOCAL|avahi.LOOKUP_RESULT_OUR_OWN)):
            has = True
        elif e.type == 'service-all-for-now':
            break

    return has

def skip_if_another_llxmpp():
    if has_another_llxmpp():
        print ("SKIP: This test fails if there is another LL XMPP instance "
                "running on the machine.")
        # exiting 77 causes automake to consider the test to have been skipped
        raise SystemExit(77)

if __name__ == '__main__':
    from twisted.internet import reactor

    txtdict = { "test0": "0", "test1": "1" }

    a = AvahiAnnouncer("test", "_test._tcp", 1234, txtdict)

    q = servicetest.IteratingEventQueue()
    # Set verboseness if needed for debugging
    # q.verbose = True

    l = AvahiListener(q)
    l.listen_for_service("_test._tcp")

    while True:
      e = q.expect ('service-added', stype='_test._tcp')
      # Only care about services we announced ourselves
      if e.flags & (avahi.LOOKUP_RESULT_LOCAL|avahi.LOOKUP_RESULT_OUR_OWN):
          break

    assert "test" == e.name[0:len("test")]

    s = e.service
    s.resolve()

    e = q.expect('service-resolved', service = s)
    for (key, val ) in txtdict.iteritems():
        v = txt_get_key(e.txt, key)
        assert v == val, (key, val, v)

    txtdict["test1"] = "2"
    txtdict["test2"] = "2"

    a.update(txtdict)

    e = q.expect('service-resolved', service = s)
    for (key, val ) in txtdict.iteritems():
        v = txt_get_key(e.txt, key)
        assert v == val, (key, val, v)