-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_device.py
More file actions
368 lines (321 loc) · 12.9 KB
/
test_device.py
File metadata and controls
368 lines (321 loc) · 12.9 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import pytest
from nmcli._device import DeviceControl
from nmcli._exception import ConnectionActivateFailedException
from nmcli.data import Device, DeviceWifi, Hotspot
from .helper import DummySystemCommand
device_data_file = os.path.join(os.path.dirname(__file__), 'device_data.txt')
device_wifi_data_file = os.path.join(
os.path.dirname(__file__), 'device_wifi_data.txt')
device_wifi_data_include_bssid_file = os.path.join(os.path.dirname(__file__),
'device_wifi_data_include_bssid.txt')
def test_device():
s = DummySystemCommand('''DEVICE TYPE STATE CONNECTION
eth0 ethernet connected Default
wlan0 wifi connected MyWifi
lo loopback unmanaged --''')
device = DeviceControl(s)
r = device()
assert len(r) == 3
assert r == [
Device('eth0', 'ethernet', 'connected', 'Default'),
Device('wlan0', 'wifi', 'connected', 'MyWifi'),
Device('lo', 'loopback', 'unmanaged', None)
]
assert s.passed_parameters == ['device', 'status']
def test_status():
with open(device_data_file, encoding='utf-8') as f:
buf = f.read()
s = DummySystemCommand(buf)
device = DeviceControl(s)
r = device.status()
assert len(r) == 4
assert r == [
Device('eth0', 'ethernet', 'connected', 'Wired connection 1'),
Device('wlan0', 'wifi', 'disconnected', None),
Device('p2p-dev-wlan0', 'wifi-p2p', 'disconnected', None),
Device('lo', 'loopback', 'unmanaged', None)
]
assert s.passed_parameters == ['device', 'status']
def test_show():
d = '''GENERAL.DEVICE: lo
GENERAL.TYPE: loopback
GENERAL.HWADDR: 00:00:00:00:00:00
GENERAL.MTU: 65536
GENERAL.STATE: 10 (unmanaged)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
IP4.ADDRESS[1]: 127.0.0.1/8
IP4.GATEWAY: --
IP6.ADDRESS[1]: ::1/128
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = ::1/128, nh = ::, mt = 256'''
s1 = DummySystemCommand(d)
device = DeviceControl(s1)
assert device.show('lo') == {
'GENERAL.DEVICE': 'lo',
'GENERAL.TYPE': 'loopback',
'GENERAL.HWADDR': '00:00:00:00:00:00',
'GENERAL.MTU': '65536',
'GENERAL.STATE': '10 (unmanaged)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None,
'IP4.ADDRESS[1]': '127.0.0.1/8',
'IP4.GATEWAY': None,
'IP6.ADDRESS[1]': '::1/128',
'IP6.GATEWAY': None,
'IP6.ROUTE[1]': 'dst = ::1/128, nh = ::, mt = 256'
}
assert s1.passed_parameters == ['device', 'show', 'lo']
s2 = DummySystemCommand(d)
device = DeviceControl(s2)
assert device.show('lo', 'all') == {
'GENERAL.DEVICE': 'lo',
'GENERAL.TYPE': 'loopback',
'GENERAL.HWADDR': '00:00:00:00:00:00',
'GENERAL.MTU': '65536',
'GENERAL.STATE': '10 (unmanaged)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None,
'IP4.ADDRESS[1]': '127.0.0.1/8',
'IP4.GATEWAY': None,
'IP6.ADDRESS[1]': '::1/128',
'IP6.GATEWAY': None,
'IP6.ROUTE[1]': 'dst = ::1/128, nh = ::, mt = 256'
}
assert s2.passed_parameters == ['-f', 'all', 'device', 'show', 'lo']
def test_show_all():
d = '''GENERAL.DEVICE: wlan0
GENERAL.TYPE: wifi
GENERAL.HWADDR: 46:7B:1F:32:36:E2
GENERAL.MTU: 1500
GENERAL.STATE: 30 (disconnected)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
GENERAL.DEVICE: lo
GENERAL.TYPE: loopback
GENERAL.HWADDR: 00:00:00:00:00:00
GENERAL.MTU: 65536
GENERAL.STATE: 10 (unmanaged)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
IP4.ADDRESS[1]: 127.0.0.1/8
IP4.GATEWAY: --
IP6.ADDRESS[1]: ::1/128
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = ::1/128, nh = ::, mt = 256'''
s1 = DummySystemCommand(d)
device = DeviceControl(s1)
assert device.show_all() == [{
'GENERAL.DEVICE': 'wlan0',
'GENERAL.TYPE': 'wifi',
'GENERAL.HWADDR': '46:7B:1F:32:36:E2',
'GENERAL.MTU': '1500',
'GENERAL.STATE': '30 (disconnected)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None
}, {
'GENERAL.DEVICE': 'lo',
'GENERAL.TYPE': 'loopback',
'GENERAL.HWADDR': '00:00:00:00:00:00',
'GENERAL.MTU': '65536',
'GENERAL.STATE': '10 (unmanaged)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None,
'IP4.ADDRESS[1]': '127.0.0.1/8',
'IP4.GATEWAY': None,
'IP6.ADDRESS[1]': '::1/128',
'IP6.GATEWAY': None,
'IP6.ROUTE[1]': 'dst = ::1/128, nh = ::, mt = 256'
}]
assert s1.passed_parameters == ['device', 'show']
s2 = DummySystemCommand(d)
device = DeviceControl(s2)
assert device.show_all('all') == [{
'GENERAL.DEVICE': 'wlan0',
'GENERAL.TYPE': 'wifi',
'GENERAL.HWADDR': '46:7B:1F:32:36:E2',
'GENERAL.MTU': '1500',
'GENERAL.STATE': '30 (disconnected)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None
}, {
'GENERAL.DEVICE': 'lo',
'GENERAL.TYPE': 'loopback',
'GENERAL.HWADDR': '00:00:00:00:00:00',
'GENERAL.MTU': '65536',
'GENERAL.STATE': '10 (unmanaged)',
'GENERAL.CONNECTION': None,
'GENERAL.CON-PATH': None,
'IP4.ADDRESS[1]': '127.0.0.1/8',
'IP4.GATEWAY': None,
'IP6.ADDRESS[1]': '::1/128',
'IP6.GATEWAY': None,
'IP6.ROUTE[1]': 'dst = ::1/128, nh = ::, mt = 256'
}]
assert s2.passed_parameters == ['-f', 'all', 'device', 'show']
def test_up():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.up(ifname)
assert s.passed_parameters == ['device', 'up', ifname]
device.up(ifname, wait=10)
assert s.passed_parameters == ['--wait', '10', 'device', 'up', ifname]
def test_connect():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.connect(ifname)
assert s.passed_parameters == ['device', 'connect', ifname]
device.connect(ifname, wait=10)
assert s.passed_parameters == ['--wait', '10', 'device', 'connect', ifname]
def test_down():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.down(ifname)
assert s.passed_parameters == ['device', 'down', ifname]
device.down(ifname, wait=10)
assert s.passed_parameters == [
'--wait', '10', 'device', 'down', ifname]
def test_disconnect():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.disconnect(ifname)
assert s.passed_parameters == ['device', 'disconnect', ifname]
device.disconnect(ifname, wait=10)
assert s.passed_parameters == [
'--wait', '10', 'device', 'disconnect', ifname]
def test_reapply():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.reapply(ifname)
assert s.passed_parameters == ['device', 'reapply', ifname]
def test_delete():
s = DummySystemCommand()
device = DeviceControl(s)
ifname = 'eth0'
device.delete(ifname)
assert s.passed_parameters == ['device', 'delete', ifname]
device.delete(ifname, wait=10)
assert s.passed_parameters == ['--wait', '10', 'device', 'delete', ifname]
def test_device_wifi():
d = '''*:AP1:00\\:00\\:00\\:00\\:00\\:00:Infra:1:2400 MHz:130 Mbit/s:82:WPA1 WPA2
:AP2:00\\:00\\:00\\:00\\:00\\:01:Infra:11:2401 MHz:195 Mbit/s:74:WPA2
:AP3:00\\:00\\:00\\:00\\:00\\:02:Infra:11:2402 MHz:195 Mbit/s:72:WPA1 WPA2'''
s = DummySystemCommand(d)
device = DeviceControl(s)
r = device.wifi()
assert len(r) == 3
assert r == [
DeviceWifi(True, 'AP1', '00:00:00:00:00:00',
'Infra', 1, 2400, 130, 82, 'WPA1 WPA2'),
DeviceWifi(False, 'AP2', '00:00:00:00:00:01',
'Infra', 11, 2401, 195, 74, 'WPA2'),
DeviceWifi(False, 'AP3', '00:00:00:00:00:02',
'Infra', 11, 2402, 195, 72, 'WPA1 WPA2'),
]
assert s.passed_parameters == ['-t', '-f',
'IN-USE,SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,SECURITY',
'device', 'wifi', 'list']
with open(device_wifi_data_file, encoding='utf-8') as f:
buf = f.read()
s2 = DummySystemCommand(buf)
device = DeviceControl(s2)
ifname = 'wlan0'
r = device.wifi(ifname)
assert len(r) == 3
assert r == [
DeviceWifi(False, '', '00:00:00:00:00:00', 'Infra',
11, 2400, 195, 72, 'WPA1 WPA2'),
DeviceWifi(False, 'AP1', '00:00:00:00:00:01',
'Infra', 4, 2401, 130, 40, 'WPA1 WPA2'),
DeviceWifi(False, 'AP2', '00:00:00:00:00:02',
'Infra', 6, 2402, 65, 24, 'WPA2'),
]
assert s2.passed_parameters == ['-t', '-f',
'IN-USE,SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,SECURITY',
'device', 'wifi', 'list', 'ifname', ifname]
device.wifi(rescan=True)
assert s2.passed_parameters == ['-t', '-f',
'IN-USE,SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,SECURITY',
'device', 'wifi', 'list', '--rescan', "yes"]
device.wifi(rescan=False)
assert s2.passed_parameters == ['-t', '-f',
'IN-USE,SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,SECURITY',
'device', 'wifi', 'list', '--rescan', "no"]
device.wifi(ifname, rescan=True)
assert s2.passed_parameters == ['-t', '-f',
'IN-USE,SSID,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,SECURITY',
'device', 'wifi', 'list', 'ifname', ifname, '--rescan', "yes"]
def test_wifi_connect():
s = DummySystemCommand()
device = DeviceControl(s)
ssid = 'AP1'
password = 'abc'
ifname = 'wlan0'
device.wifi_connect(ssid, password)
assert s.passed_parameters == [
'device', 'wifi', 'connect', ssid, 'password', password]
device.wifi_connect(ssid, password, ifname)
assert s.passed_parameters == [
'device', 'wifi', 'connect', ssid, 'password', password, 'ifname', ifname]
device.wifi_connect(ssid, password, wait=10)
assert s.passed_parameters == [
'--wait', '10', 'device', 'wifi', 'connect', ssid, 'password', password]
def test_wifi_connect_when_connection_activate_failed():
s = DummySystemCommand(
'''Error: Connection activation failed: (7) Secrets were required, but not provided.
''')
device = DeviceControl(s)
ssid = 'AP1'
password = 'abc'
with pytest.raises(ConnectionActivateFailedException):
device.wifi_connect(ssid, password)
def test_wifi_hotspot():
d1 = '''Hotspot password: abcdefgh
Device 'wlan0' successfully activated with '00000000-0000-0000-0000-000000000000'.
'''
d2 = '''connection.id:Hotspot
802-11-wireless.ssid:AP1
'''
s = DummySystemCommand([d1, d2])
device = DeviceControl(s)
r = device.wifi_hotspot()
assert r == Hotspot('wlan0', 'Hotspot', 'AP1', 'abcdefgh')
assert s.parameters_history == [
['device', 'wifi', 'hotspot', '--show-secrets'],
['-t', '-f', 'connection.id,802-11-wireless.ssid',
'connection', 'show', 'uuid', '00000000-0000-0000-0000-000000000000']
]
s2 = DummySystemCommand([d1, d2])
ifname = 'wlan0'
con_name = 'MyHotspot'
ssid = 'Hot Spot'
band = 'a'
channel = 123
password = 'pass'
DeviceControl(s2).wifi_hotspot(
ifname, con_name, ssid, band, channel, password)
assert s2.parameters_history == [
['device', 'wifi', 'hotspot', '--show-secrets', 'ifname', ifname,
'con-name', con_name, 'ssid', ssid, 'band', band,
'channel', str(channel), 'password', password],
['-t', '-f', 'connection.id,802-11-wireless.ssid',
'connection', 'show', 'uuid', '00000000-0000-0000-0000-000000000000']
]
d3 = '''connection.id:Hotspot
802-11-wireless.ssid:foo:bar:baz
'''
s3 = DummySystemCommand([d1, d3])
device = DeviceControl(s3)
r = device.wifi_hotspot(ssid='foo:bar:baz')
assert r == Hotspot('wlan0', 'Hotspot', 'foo:bar:baz', 'abcdefgh')
assert s3.parameters_history == [
['device', 'wifi', 'hotspot', '--show-secrets', 'ssid', 'foo:bar:baz'],
['-t', '-f', 'connection.id,802-11-wireless.ssid',
'connection', 'show', 'uuid', '00000000-0000-0000-0000-000000000000']
]