forked from ukBaz/python-bluezero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dbus_tools_mock.py
193 lines (164 loc) · 8.46 KB
/
test_dbus_tools_mock.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
import dbus
import dbusmock
import io
from pathlib import Path
import subprocess
from unittest import mock
from unittest.mock import patch
from tests.mock_eventloop import MockAsync
# Module under test
from bluezero import dbus_tools
class TestAdapterExample(dbusmock.DBusTestCase):
"""
Test mocking bluetoothd
"""
@classmethod
def setUpClass(cls):
here = Path(__file__).parent
template = str(here.joinpath('dbusmock_templates', 'bluez_scan.py'))
cls.start_system_bus()
cls.dbus_con = cls.get_dbus(True)
(cls.p_mock, cls.obj_bluez) = cls.spawn_server_template(
template, {}, stdout=subprocess.PIPE)
def setUp(self):
self.obj_bluez.Reset()
self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')
@classmethod
def tearDownClass(cls):
cls.stop_dbus(cls.system_bus_pid)
cls.p_mock.terminate()
cls.p_mock.wait()
def test_dbus_string(self):
result = dbus_tools.dbus_to_python(dbus.String("This is a test"))
self.assertEqual(result, "This is a test")
self.assertIsInstance(result, str)
def test_dbus_int64(self):
self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
result = dbus_tools.dbus_to_python(dbus.Int64(12))
self.assertTrue(isinstance(result, int))
def test_dbus_read_value(self):
result = dbus_tools.dbus_to_python(dbus.Array([dbus.Byte(66),
dbus.Byte(97),
dbus.Byte(122)],
signature=dbus.Signature('y')))
self.assertEqual(result, b"Baz")
def test_dbus_dict(self):
result = dbus_tools.dbus_to_python(dbus.Dictionary(
{dbus.String('device'): dbus.ObjectPath('/org/bluez/hci0/dev_67_63_13_0D_37_01', variant_level=1),
dbus.String('link'): dbus.String('LE', variant_level=1),
dbus.String('mtu'): dbus.UInt16(512, variant_level=1)},
signature=dbus.Signature('sv'))
)
self.assertDictEqual({'device': '/org/bluez/hci0/dev_67_63_13_0D_37_01',
'link': 'LE', 'mtu': 512},
result)
def test_dbus_dict_adapter_props(self):
dongle_props = dbus.Dictionary({
dbus.String('Address'): dbus.String('11:22:33:44:55:66', variant_level=1),
dbus.String('AddressType'): dbus.String('public', variant_level=1),
dbus.String('Name'): dbus.String('test_machine', variant_level=1),
dbus.String('Alias'): dbus.String('test_machine', variant_level=1),
dbus.String('Class'): dbus.UInt32(786700, variant_level=1),
dbus.String('Powered'): dbus.Boolean(True, variant_level=1),
dbus.String('Discoverable'): dbus.Boolean(False, variant_level=1),
dbus.String('DiscoverableTimeout'): dbus.UInt32(190, variant_level=1),
dbus.String('Pairable'): dbus.Boolean(False, variant_level=1),
dbus.String('PairableTimeout'): dbus.UInt32(120, variant_level=1),
dbus.String('Discovering'): dbus.Boolean(False, variant_level=1),
dbus.String('UUIDs'): dbus.Array(
[dbus.String('0000110e-0000-1000-8000-00805f9b34fb'),
dbus.String('0000110a-0000-1000-8000-00805f9b34fb'),
dbus.String('00001200-0000-1000-8000-00805f9b34fb'),
dbus.String('00001112-0000-1000-8000-00805f9b34fb'),
dbus.String('0000110b-0000-1000-8000-00805f9b34fb'),
dbus.String('0000110c-0000-1000-8000-00805f9b34fb'),
dbus.String('00001800-0000-1000-8000-00805f9b34fb'),
dbus.String('00001108-0000-1000-8000-00805f9b34fb'),
dbus.String('00001801-0000-1000-8000-00805f9b34fb')],
signature=dbus.Signature('s'), variant_level=1),
dbus.String('Modalias'): dbus.String('usb:v1D6Bp0246d0535', variant_level=1)},
signature=dbus.Signature('sv'))
expected = {'Address': '11:22:33:44:55:66',
'AddressType': 'public',
'Alias': 'test_machine',
'Class': 786700,
'Discoverable': False,
'DiscoverableTimeout': 190,
'Discovering': False,
'Modalias': 'usb:v1D6Bp0246d0535',
'Name': 'test_machine',
'Pairable': False,
'PairableTimeout': 120,
'Powered': True,
'UUIDs': ['0000110e-0000-1000-8000-00805f9b34fb',
'0000110a-0000-1000-8000-00805f9b34fb',
'00001200-0000-1000-8000-00805f9b34fb',
'00001112-0000-1000-8000-00805f9b34fb',
'0000110b-0000-1000-8000-00805f9b34fb',
'0000110c-0000-1000-8000-00805f9b34fb',
'00001800-0000-1000-8000-00805f9b34fb',
'00001108-0000-1000-8000-00805f9b34fb',
'00001801-0000-1000-8000-00805f9b34fb']}
result = dbus_tools.dbus_to_python(dongle_props)
self.assertEqual(expected, result)
def test_dbus_bool(self):
result = dbus_tools.dbus_to_python(dbus.Boolean(True))
self.assertTrue(isinstance(result, bool))
def test_dbus_double(self):
result = dbus_tools.dbus_to_python(dbus.Double(12.3))
self.assertTrue(12.3, result)
def test_dbus_byte(self):
data = dbus.Byte(255)
result = dbus_tools.dbus_to_python(data)
self.assertEqual(0xFF, result)
def test_dbus_byte_array1(self):
data = dbus.Array([dbus.Byte(29)],
signature=dbus.Signature('y'))
result = dbus_tools.dbus_to_python(data)
self.assertEqual(b'\x1d', result)
def test_str_to_dbusarray(self):
expected = dbus.Array([dbus.Byte(70), dbus.Byte(111), dbus.Byte(120)],
signature=dbus.Signature('y'))
result = dbus_tools.str_to_dbusarray('Fox')
self.assertEqual(expected, result)
def test_bytes_to_dbusarray(self):
expected = dbus.Array([dbus.Byte(70), dbus.Byte(111), dbus.Byte(120)],
signature=dbus.Signature('y'))
result = dbus_tools.bytes_to_dbusarray(b'Fox')
self.assertEqual(expected, result)
def test_bytes_to_dbusarray2(self):
expected = dbus.Array([dbus.Byte(0), dbus.Byte(1), dbus.Byte(2)],
signature=dbus.Signature('y'))
result = dbus_tools.bytes_to_dbusarray(b'\x00\x01\x02')
self.assertEqual(expected, result)
def test_get_service(self):
result = dbus_tools.get_services('some/rubbish')
self.assertListEqual([], result)
def test_interfaces_added(self):
with patch('logging.Logger.debug') as logger:
result = dbus_tools.interfaces_added('/org/bluez/hci1/dev_00_11_22_33_44_55',
'org.bluez.Device1')
logger.assert_called_once()
def test_properties_changed(self):
with patch('logging.Logger.debug') as logger:
result = dbus_tools.properties_changed('org.bluez.Device1',
{'Connected': True},
{}, '/org/bluez/hci0')
print(result)
logger.assert_called_once()
def test_get_property(self):
self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0')
result = dbus_tools.get(path_obj, 'org.bluez.Adapter1', 'Address')
self.assertEqual('00:01:02:03:04:05', result)
def test_get_property_exception(self):
self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0')
with self.assertRaises(dbus.exceptions.DBusException):
result = dbus_tools.get(path_obj, 'org.bluez.AdapterXX', 'Address')
def test_get_property_default(self):
self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0')
result = dbus_tools.get(path_obj, 'org.bluez.Adapter1', 'address', 'xx')
self.assertEqual('xx', result)