-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bluez.py
284 lines (242 loc) · 10 KB
/
test_bluez.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import asyncio
import bluez
import dbus_next
import unittest
import unittest.mock
class BluezTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
self.bus = MockBus(self)
self.listener = unittest.mock.Mock()
self.client = self.loop.run_until_complete(bluez.connect(self.bus, self.listener))
def send_interfaces_added(self, path, interfaces):
self.bus.handler('/', 'org.freedesktop.DBus.ObjectManager', 'InterfacesAdded', [path, interfaces])
def send_interfaces_removed(self, path, interfaces):
self.bus.handler('/', 'org.freedesktop.DBus.ObjectManager', 'InterfacesRemoved', [path, interfaces])
def send_properties_changed(self, path, interface, changed):
self.bus.handler(path, 'org.freedesktop.DBus.Properties', 'PropertiesChanged', [interface, changed, []])
def assert_adapter_added(self, path):
for args, kwargs in self.listener.add_adapter.call_args_list:
if args[0].path == path:
return
self.fail(f'Call add_adapter({path}) not found in {self.listener.add_adapter.call_args_list}')
def assert_device_added(self, address, path, connected):
for args, kwargs in self.listener.add_device.call_args_list:
if args[0] == address and args[1].path == path and args[2] == connected:
return
self.fail(f'Call add_device({address}, {path}, {connected}) not found in {self.listener.add_device.call_args_list}')
def test_connect_and_disconnect(self):
self.bus.assert_call('add_message_handler', self.bus.handler)
self.bus.assert_call('call', {
'destination': 'org.freedesktop.DBus',
'path': '/',
'interface': 'org.freedesktop.DBus',
'member': 'AddMatch',
'signature': 's',
'body': ["type='signal',sender='org.bluez',path_namespace='/'"]
})
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/',
'interface': 'org.freedesktop.DBus.ObjectManager',
'member': 'GetManagedObjects',
'signature': '',
'body': []
})
self.client.disconnect()
self.bus.assert_call('remove_message_handler', self.bus.handler)
def test_initial_adapter(self):
self.assert_adapter_added('/ad')
def test_add_adapter(self):
self.send_interfaces_added('/apt', {
'org.bluez.Adapter1': {}
})
self.assert_adapter_added('/apt')
def test_initial_devices(self):
self.assert_device_added('00:11:22:33:44:55', '/ad/dev1', False)
self.assert_device_added('66:77:88:99:AA:BB', '/ad/dev2', True)
def test_ignored_message(self):
self.bus.handler('/', 'org.random', 'SomeOtherSignal', [])
def test_add_interface(self):
self.send_interfaces_added('/ad/dev3', {
'org.bluez.Device1': {
'Address': dbus_next.Variant('s', 'CC:DD:EE:FF:00:11'),
'Connected': dbus_next.Variant('b', False)
}
})
self.assert_device_added('CC:DD:EE:FF:00:11', '/ad/dev3', False)
def test_remove_interface(self):
self.send_interfaces_removed('/ad/dev1', ['org.bluez.Device1'])
self.listener.remove_device.assert_called_once_with('00:11:22:33:44:55')
def test_remove_ignored_interface(self):
self.send_interfaces_removed('/ad/dev1', ['org.random.Interface'])
self.listener.remove_device.assert_not_called()
def test_change_property(self):
self.send_properties_changed('/ad/dev1', 'org.bluez.Device1', {
'Connected': dbus_next.Variant('b', True)
})
self.listener.update_device.assert_called_once_with('00:11:22:33:44:55', True)
def test_change_ignored_property(self):
self.send_properties_changed('/ad/dev1', 'org.bluez.Device1', {
'Other': dbus_next.Variant('b', True)
})
self.listener.update_device.assert_not_called()
def test_change_property_on_ignored_interface(self):
self.send_properties_changed('/ad/dev1', 'org.random.Interface', {
'Connected': dbus_next.Variant('b', True)
})
self.listener.update_device.assert_not_called()
class BluezAdapterTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
self.bus = MockBus(self)
self.adapter = bluez.Adapter(self.bus, '/path')
def test_remove_device(self):
device = bluez.Device(self.bus, '/path/dev')
self.loop.run_until_complete(self.adapter.remove_device(device))
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Adapter1',
'member': 'RemoveDevice',
'signature': 'o',
'body': ['/path/dev']
})
def test_set_discovery_filter(self):
self.loop.run_until_complete(self.adapter.set_discovery_filter({'key': 'value'}))
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Adapter1',
'member': 'SetDiscoveryFilter',
'signature': 'a{sv}',
'body': [{'key': 'value'}]
})
def test_start_discovery(self):
self.loop.run_until_complete(self.adapter.start_discovery())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Adapter1',
'member': 'StartDiscovery',
'signature': '',
'body': []
})
def test_stop_discovery(self):
self.loop.run_until_complete(self.adapter.stop_discovery())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Adapter1',
'member': 'StopDiscovery',
'signature': '',
'body': []
})
class BluezDeviceTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
self.bus = MockBus(self)
self.adapter = bluez.Device(self.bus, '/path')
def test_pair(self):
self.loop.run_until_complete(self.adapter.pair())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Device1',
'member': 'Pair',
'signature': '',
'body': []
})
def test_connect(self):
self.loop.run_until_complete(self.adapter.connect())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Device1',
'member': 'Connect',
'signature': '',
'body': []
})
def test_disconnect(self):
self.loop.run_until_complete(self.adapter.disconnect())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.bluez.Device1',
'member': 'Disconnect',
'signature': '',
'body': []
})
def test_trust(self):
self.loop.run_until_complete(self.adapter.trust())
self.bus.assert_call('call', {
'destination': 'org.bluez',
'path': '/path',
'interface': 'org.freedesktop.DBus.Properties',
'member': 'Set',
'signature': 'ssv',
'body': ['org.bluez.Device1', 'Trusted', dbus_next.Variant('b', True)]
})
class MockBus:
def __init__(self, test):
self.test = test
self.calls = []
self.handler = None
def add_message_handler(self, handler):
self.calls.append(('add_message_handler', handler))
self.handler = handler
def remove_message_handler(self, handler):
self.calls.append(('remove_message_handler', handler))
async def call(self, destination, path, interface, member, signature='', body=[]):
self.calls.append(('call', {
'destination': destination,
'path': path,
'interface': interface,
'member': member,
'signature': signature,
'body': body
}))
if member == 'GetManagedObjects':
return [{
'/ad': {
'org.bluez.Adapter1': {}
},
'/ad/dev1': {
'org.bluez.Device1': {
'Address': dbus_next.Variant('s', '00:11:22:33:44:55'),
'Connected': dbus_next.Variant('b', False)
}
},
'/ad/dev2': {
'org.bluez.Device1': {
'Address': dbus_next.Variant('s', '66:77:88:99:AA:BB'),
'Connected': dbus_next.Variant('b', True)
}
},
}]
def assert_call(self, name, args):
self.test.assertTrue(self.calls)
self.test.assertEqual(self.calls.pop(0), (name, args))
class BluezRaceTest(unittest.TestCase):
def test_connect_callback_race(self):
loop = asyncio.new_event_loop()
bus = MockRacingBus()
listener = unittest.mock.Mock()
loop.run_until_complete(bluez.connect(bus, listener))
listener.add_device.assert_called_once()
class MockRacingBus:
def __init__(self):
self.handler = None
def add_message_handler(self, handler):
self.handler = handler
async def call(self, destination, path, interface, member, signature='', body=[]):
if member == 'GetManagedObjects':
device = '/ad/dev1'
interfaces = {
'org.bluez.Device1': {
'Address': dbus_next.Variant('s', '00:11:22:33:44:55'),
'Connected': dbus_next.Variant('b', False)
}
}
self.handler('/', 'org.freedesktop.DBus.ObjectManager', 'InterfacesAdded', [device, interfaces])
return [{device: interfaces}]