]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_p2p_channel.py
tests: Add a helper function for clearing scan cache
[thirdparty/hostap.git] / tests / hwsim / test_p2p_channel.py
1 # P2P channel selection test cases
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import logging
8 logger = logging.getLogger()
9 import os
10 import subprocess
11 import time
12
13 import hostapd
14 import hwsim_utils
15 from wpasupplicant import WpaSupplicant
16 from hwsim import HWSimRadio
17 from test_p2p_grpform import go_neg_pin_authorized
18 from test_p2p_grpform import check_grpform_results
19 from test_p2p_grpform import remove_group
20 from test_p2p_grpform import go_neg_pbc
21 from test_p2p_autogo import autogo
22
23 def set_country(country):
24 subprocess.call(['sudo', 'iw', 'reg', 'set', country])
25 time.sleep(0.1)
26
27 def test_p2p_channel_5ghz(dev):
28 """P2P group formation with 5 GHz preference"""
29 try:
30 set_country("US")
31 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
32 r_dev=dev[1], r_intent=0,
33 test_data=False)
34 check_grpform_results(i_res, r_res)
35 freq = int(i_res['freq'])
36 if freq < 5000:
37 raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
38 remove_group(dev[0], dev[1])
39 finally:
40 set_country("00")
41
42 def test_p2p_channel_5ghz_no_vht(dev):
43 """P2P group formation with 5 GHz preference when VHT channels are disallowed"""
44 try:
45 set_country("US")
46 dev[0].request("P2P_SET disallow_freq 5180-5240")
47 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
48 r_dev=dev[1], r_intent=0,
49 test_data=False)
50 check_grpform_results(i_res, r_res)
51 freq = int(i_res['freq'])
52 if freq < 5000:
53 raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
54 remove_group(dev[0], dev[1])
55 finally:
56 set_country("00")
57 dev[0].request("P2P_SET disallow_freq ")
58
59 def test_p2p_channel_random_social(dev):
60 """P2P group formation with 5 GHz preference but all 5 GHz channels disabled"""
61 try:
62 set_country("US")
63 dev[0].request("SET p2p_oper_channel 11")
64 dev[0].request("P2P_SET disallow_freq 5000-6000,2462")
65 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
66 r_dev=dev[1], r_intent=0,
67 test_data=False)
68 check_grpform_results(i_res, r_res)
69 freq = int(i_res['freq'])
70 if freq not in [ 2412, 2437, 2462 ]:
71 raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
72 remove_group(dev[0], dev[1])
73 finally:
74 set_country("00")
75 dev[0].request("P2P_SET disallow_freq ")
76
77 def test_p2p_channel_random(dev):
78 """P2P group formation with 5 GHz preference but all 5 GHz channels and all social channels disabled"""
79 try:
80 set_country("US")
81 dev[0].request("SET p2p_oper_channel 11")
82 dev[0].request("P2P_SET disallow_freq 5000-6000,2412,2437,2462")
83 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
84 r_dev=dev[1], r_intent=0,
85 test_data=False)
86 check_grpform_results(i_res, r_res)
87 freq = int(i_res['freq'])
88 if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
89 raise Exception("Unexpected channel %d MHz" % freq)
90 remove_group(dev[0], dev[1])
91 finally:
92 set_country("00")
93 dev[0].request("P2P_SET disallow_freq ")
94
95 def test_p2p_channel_random_social_with_op_class_change(dev, apdev, params):
96 """P2P group formation using random social channel with oper class change needed"""
97 try:
98 set_country("US")
99 logger.info("Start group on 5 GHz")
100 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
101 r_dev=dev[1], r_intent=0,
102 test_data=False)
103 check_grpform_results(i_res, r_res)
104 freq = int(i_res['freq'])
105 if freq < 5000:
106 raise Exception("Unexpected channel %d MHz - did not pick 5 GHz preference" % freq)
107 remove_group(dev[0], dev[1])
108
109 logger.info("Disable 5 GHz and try to re-start group based on 5 GHz preference")
110 dev[0].request("SET p2p_oper_reg_class 115")
111 dev[0].request("SET p2p_oper_channel 36")
112 dev[0].request("P2P_SET disallow_freq 5000-6000")
113 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
114 r_dev=dev[1], r_intent=0,
115 test_data=False)
116 check_grpform_results(i_res, r_res)
117 freq = int(i_res['freq'])
118 if freq not in [ 2412, 2437, 2462 ]:
119 raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
120 remove_group(dev[0], dev[1])
121
122 try:
123 arg = [ "tshark",
124 "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
125 "-Y", "wifi_p2p.public_action.subtype == 0",
126 "-V" ]
127 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
128 stderr=open('/dev/null', 'w'))
129 except Exception, e:
130 logger.info("Could run run tshark check: " + str(e))
131 cmd = None
132 pass
133
134 if cmd:
135 last = None
136 for l in cmd.stdout.read().splitlines():
137 if "Operating Channel:" not in l:
138 continue
139 last = l
140 if last is None:
141 raise Exception("Could not find GO Negotiation Request")
142 if "Operating Class 81" not in last:
143 raise Exception("Unexpected operating class: " + last.strip())
144 finally:
145 set_country("00")
146 dev[0].request("P2P_SET disallow_freq ")
147 dev[0].request("SET p2p_oper_reg_class 0")
148 dev[0].request("SET p2p_oper_channel 0")
149
150 def test_p2p_channel_avoid(dev):
151 """P2P and avoid frequencies driver event"""
152 try:
153 set_country("US")
154 if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES 5000-6000,2412,2437,2462"):
155 raise Exception("Could not simulate driver event")
156 ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
157 if ev is None:
158 raise Exception("No CTRL-EVENT-AVOID-FREQ event")
159 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
160 r_dev=dev[1], r_intent=0,
161 test_data=False)
162 check_grpform_results(i_res, r_res)
163 freq = int(i_res['freq'])
164 if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
165 raise Exception("Unexpected channel %d MHz" % freq)
166
167 if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES"):
168 raise Exception("Could not simulate driver event(2)")
169 ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
170 if ev is None:
171 raise Exception("No CTRL-EVENT-AVOID-FREQ event")
172 ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=1)
173 if ev is not None:
174 raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP event")
175
176 if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES " + str(freq)):
177 raise Exception("Could not simulate driver event(3)")
178 ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
179 if ev is None:
180 raise Exception("No CTRL-EVENT-AVOID-FREQ event")
181 ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=10)
182 if ev is None:
183 raise Exception("No P2P-REMOVE-AND-REFORM-GROUP event")
184 finally:
185 set_country("00")
186 dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES")
187
188 def test_autogo_following_bss(dev, apdev):
189 """P2P autonomous GO operate on the same channel as station interface"""
190 if dev[0].get_mcc() > 1:
191 logger.info("test mode: MCC")
192
193 dev[0].request("SET p2p_no_group_iface 0")
194
195 channels = { 3 : "2422", 5 : "2432", 9 : "2452" }
196 for key in channels:
197 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
198 "channel" : str(key) })
199 dev[0].connect("ap-test", key_mgmt="NONE",
200 scan_freq=str(channels[key]))
201 res_go = autogo(dev[0])
202 if res_go['freq'] != channels[key]:
203 raise Exception("Group operation channel is not the same as on connected station interface")
204 hwsim_utils.test_connectivity(dev[0], hapd)
205 dev[0].remove_group(res_go['ifname'])
206
207 def test_go_neg_with_bss_connected(dev, apdev):
208 """P2P channel selection: GO negotiation when station interface is connected"""
209
210 dev[0].flush_scan_cache()
211 dev[1].flush_scan_cache()
212 dev[0].request("SET p2p_no_group_iface 0")
213
214 hapd = hostapd.add_ap(apdev[0]['ifname'],
215 { "ssid": 'bss-2.4ghz', "channel": '5' })
216 dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2432")
217 #dev[0] as GO
218 [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
219 r_intent=1)
220 check_grpform_results(i_res, r_res)
221 if i_res['role'] != "GO":
222 raise Exception("GO not selected according to go_intent")
223 if i_res['freq'] != "2432":
224 raise Exception("Group formed on a different frequency than BSS")
225 hwsim_utils.test_connectivity(dev[0], hapd)
226 dev[0].remove_group(i_res['ifname'])
227
228 if dev[0].get_mcc() > 1:
229 logger.info("Skip as-client case due to MCC being enabled")
230 return;
231
232 #dev[0] as client
233 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
234 r_intent=10)
235 check_grpform_results(i_res2, r_res2)
236 if i_res2['role'] != "client":
237 raise Exception("GO not selected according to go_intent")
238 if i_res2['freq'] != "2432":
239 raise Exception("Group formed on a different frequency than BSS")
240 hwsim_utils.test_connectivity(dev[0], hapd)
241
242 def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
243 """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
244
245 with HWSimRadio(n_channels=2) as (radio, iface):
246 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
247 wpas.interface_add(iface)
248
249 wpas.request("SET p2p_no_group_iface 0")
250
251 if wpas.get_mcc() < 2:
252 raise Exception("New radio does not support MCC")
253
254 try:
255 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
256 "channel": '1' })
257 wpas.request("P2P_SET disallow_freq 2412")
258 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
259 res = autogo(wpas)
260 if res['freq'] == "2412":
261 raise Exception("GO set on a disallowed channel")
262 hwsim_utils.test_connectivity(wpas, hapd)
263 finally:
264 wpas.request("P2P_SET disallow_freq ")
265
266 def test_go_neg_with_bss_on_disallowed_chan(dev, apdev):
267 """P2P channel selection: GO negotiation with station interface on a disallowed channel"""
268
269 with HWSimRadio(n_channels=2) as (radio, iface):
270 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
271 wpas.interface_add(iface)
272
273 wpas.request("SET p2p_no_group_iface 0")
274
275 if wpas.get_mcc() < 2:
276 raise Exception("New radio does not support MCC")
277
278 try:
279 hapd = hostapd.add_ap(apdev[0]['ifname'],
280 { "ssid": 'bss-2.4ghz', "channel": '1' })
281 # make sure PBC overlap from old test cases is not maintained
282 dev[0].flush_scan_cache()
283 dev[1].flush_scan_cache()
284 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
285 wpas.request("P2P_SET disallow_freq 2412")
286
287 #wpas as GO
288 [i_res, r_res] = go_neg_pbc(i_dev=wpas, i_intent=10, r_dev=dev[1],
289 r_intent=1)
290 check_grpform_results(i_res, r_res)
291 if i_res['role'] != "GO":
292 raise Exception("GO not selected according to go_intent")
293 if i_res['freq'] == "2412":
294 raise Exception("Group formed on a disallowed channel")
295 hwsim_utils.test_connectivity(wpas, hapd)
296 wpas.remove_group(i_res['ifname'])
297
298 #wpas as client
299 [i_res2, r_res2] = go_neg_pbc(i_dev=wpas, i_intent=1, r_dev=dev[1],
300 r_intent=10)
301 check_grpform_results(i_res2, r_res2)
302 if i_res2['role'] != "client":
303 raise Exception("GO not selected according to go_intent")
304 if i_res2['freq'] == "2412":
305 raise Exception("Group formed on a disallowed channel")
306 hwsim_utils.test_connectivity(wpas, hapd)
307 finally:
308 wpas.request("P2P_SET disallow_freq ")
309
310 def test_autogo_force_diff_channel(dev, apdev):
311 """P2P autonomous GO and station interface operate on different channels"""
312 with HWSimRadio(n_channels=2) as (radio, iface):
313 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
314 wpas.interface_add(iface)
315
316 if wpas.get_mcc() < 2:
317 raise Exception("New radio does not support MCC")
318
319 wpas.request("SET p2p_no_group_iface 0")
320
321 hapd = hostapd.add_ap(apdev[0]['ifname'],
322 {"ssid" : 'ap-test', "channel" : '1'})
323 wpas.connect("ap-test", key_mgmt = "NONE", scan_freq = "2412")
324 channels = { 2 : 2417, 5 : 2432, 9 : 2452 }
325 for key in channels:
326 res_go = autogo(wpas, channels[key])
327 hwsim_utils.test_connectivity(wpas, hapd)
328 if int(res_go['freq']) == 2412:
329 raise Exception("Group operation channel is: 2412 excepted: " + res_go['freq'])
330 wpas.remove_group(res_go['ifname'])
331
332 def test_go_neg_forced_freq_diff_than_bss_freq(dev, apdev):
333 """P2P channel selection: GO negotiation with forced freq different than station interface"""
334 with HWSimRadio(n_channels=2) as (radio, iface):
335 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
336 wpas.interface_add(iface)
337
338 if wpas.get_mcc() < 2:
339 raise Exception("New radio does not support MCC")
340
341 # Clear possible PBC session overlap from previous test case
342 dev[1].flush_scan_cache()
343
344 wpas.request("SET p2p_no_group_iface 0")
345
346 hapd = hostapd.add_ap(apdev[0]['ifname'],
347 { "country_code": 'US',
348 "ssid": 'bss-5ghz', "hw_mode": 'a',
349 "channel": '40' })
350 wpas.connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
351
352 # GO and peer force the same freq, different than BSS freq,
353 # wpas to become GO
354 [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=1, i_freq=5180,
355 r_dev=wpas, r_intent=14, r_freq=5180)
356 check_grpform_results(i_res, r_res)
357 if i_res['freq'] != "5180":
358 raise Exception("P2P group formed on unexpected frequency: " + i_res['freq'])
359 if r_res['role'] != "GO":
360 raise Exception("GO not selected according to go_intent")
361 hwsim_utils.test_connectivity(wpas, hapd)
362 wpas.remove_group(r_res['ifname'])
363
364 # GO and peer force the same freq, different than BSS freq, wpas to
365 # become client
366 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[1], i_intent=14, i_freq=2422,
367 r_dev=wpas, r_intent=1, r_freq=2422)
368 check_grpform_results(i_res2, r_res2)
369 if i_res2['freq'] != "2422":
370 raise Exception("P2P group formed on unexpected frequency: " + i_res2['freq'])
371 if r_res2['role'] != "client":
372 raise Exception("GO not selected according to go_intent")
373 hwsim_utils.test_connectivity(wpas, hapd)
374
375 def test_go_pref_chan_bss_on_diff_chan(dev, apdev):
376 """P2P channel selection: Station on different channel than GO configured pref channel"""
377
378 dev[0].request("SET p2p_no_group_iface 0")
379
380 try:
381 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
382 "channel": '1' })
383 dev[0].request("SET p2p_pref_chan 81:2")
384 dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
385 res = autogo(dev[0])
386 if res['freq'] != "2412":
387 raise Exception("GO channel did not follow BSS")
388 hwsim_utils.test_connectivity(dev[0], hapd)
389 finally:
390 dev[0].request("SET p2p_pref_chan ")
391
392 def test_go_pref_chan_bss_on_disallowed_chan(dev, apdev):
393 """P2P channel selection: Station interface on different channel than GO configured pref channel, and station channel is disallowed"""
394 with HWSimRadio(n_channels=2) as (radio, iface):
395 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
396 wpas.interface_add(iface)
397
398 if wpas.get_mcc() < 2:
399 raise Exception("New radio does not support MCC")
400
401 wpas.request("SET p2p_no_group_iface 0")
402
403 try:
404 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
405 "channel": '1' })
406 wpas.request("P2P_SET disallow_freq 2412")
407 wpas.request("SET p2p_pref_chan 81:2")
408 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
409 res2 = autogo(wpas)
410 if res2['freq'] != "2417":
411 raise Exception("GO channel did not follow pref_chan configuration")
412 hwsim_utils.test_connectivity(wpas, hapd)
413 finally:
414 wpas.request("P2P_SET disallow_freq ")
415 wpas.request("SET p2p_pref_chan ")
416
417 def test_no_go_freq(dev, apdev):
418 """P2P channel selection: no GO freq"""
419 try:
420 dev[0].request("SET p2p_no_go_freq 2412")
421 # dev[0] as client, channel 1 is ok
422 [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=1,
423 r_dev=dev[1], r_intent=14, r_freq=2412)
424 check_grpform_results(i_res, r_res)
425 if i_res['freq'] != "2412":
426 raise Exception("P2P group not formed on forced freq")
427
428 dev[1].remove_group(r_res['ifname'])
429 fail = False
430 # dev[0] as GO, channel 1 is not allowed
431 try:
432 dev[0].request("SET p2p_no_go_freq 2412")
433 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14,
434 r_dev=dev[1], r_intent=1, r_freq=2412)
435 check_grpform_results(i_res2, r_res2)
436 fail = True
437 except:
438 pass
439 if fail:
440 raise Exception("GO set on a disallowed freq")
441 finally:
442 dev[0].request("SET p2p_no_go_freq ")
443
444 def test_go_neg_peers_force_diff_freq(dev, apdev):
445 try:
446 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
447 r_dev=dev[1], r_intent=0, r_freq=5200)
448 except Exception, e:
449 return
450 raise Exception("Unexpected group formation success")
451
452 def test_autogo_random_channel(dev, apdev):
453 """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
454 freqs = []
455 go_freqs = ["2412", "2437", "2462"]
456 for i in range(0, 20):
457 result = autogo(dev[0])
458 if result['freq'] not in go_freqs:
459 raise Exception("Unexpected frequency selected: " + result['freq'])
460 if result['freq'] not in freqs:
461 freqs.append(result['freq'])
462 if len(freqs) == 3:
463 break
464 dev[0].remove_group(result['ifname'])
465 if i == 20:
466 raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
467
468 def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
469 """P2P channel selection: GO preferred channels are disallowed"""
470 try:
471 dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
472 dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
473 for i in range(0, 5):
474 res = autogo(dev[0])
475 if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
476 raise Exception("GO channel is disallowed")
477 dev[0].remove_group(res['ifname'])
478 finally:
479 dev[0].request("P2P_SET disallow_freq ")
480 dev[0].request("SET p2p_pref_chan ")
481
482 def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
483 """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
484 try:
485 set_country("US")
486 dev[0].request("SET p2p_pref_chan 124:149")
487 res = autogo(dev[0], persistent=True)
488 if res['freq'] != "5745":
489 raise Exception("Unexpected channel selected: " + res['freq'])
490 dev[0].remove_group(res['ifname'])
491
492 netw = dev[0].list_networks()
493 if len(netw) != 1:
494 raise Exception("Unexpected number of network blocks: " + str(netw))
495 id = netw[0]['id']
496
497 set_country("DE")
498 res = autogo(dev[0], persistent=id)
499 if res['freq'] == "5745":
500 raise Exception("Unexpected channel selected(2): " + res['freq'])
501 dev[0].remove_group(res['ifname'])
502 finally:
503 dev[0].request("SET p2p_pref_chan ")
504 set_country("00")