1 # P2P channel selection test cases
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
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
23 def set_country(country):
24 subprocess.call(['sudo', 'iw', 'reg', 'set', country])
27 def test_p2p_channel_5ghz(dev):
28 """P2P group formation with 5 GHz preference"""
31 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
32 r_dev=dev[1], r_intent=0,
34 check_grpform_results(i_res, r_res)
35 freq = int(i_res['freq'])
37 raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
38 remove_group(dev[0], dev[1])
42 def test_p2p_channel_5ghz_no_vht(dev):
43 """P2P group formation with 5 GHz preference when VHT channels are disallowed"""
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,
50 check_grpform_results(i_res, r_res)
51 freq = int(i_res['freq'])
53 raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
54 remove_group(dev[0], dev[1])
57 dev[0].request("P2P_SET disallow_freq ")
59 def test_p2p_channel_random_social(dev):
60 """P2P group formation with 5 GHz preference but all 5 GHz channels disabled"""
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,
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])
75 dev[0].request("P2P_SET disallow_freq ")
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"""
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,
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])
93 dev[0].request("P2P_SET disallow_freq ")
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"""
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,
103 check_grpform_results(i_res, r_res)
104 freq = int(i_res['freq'])
106 raise Exception("Unexpected channel %d MHz - did not pick 5 GHz preference" % freq)
107 remove_group(dev[0], dev[1])
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,
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])
124 "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
125 "-Y", "wifi_p2p.public_action.subtype == 0",
127 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
128 stderr=open('/dev/null', 'w'))
130 logger.info("Could run run tshark check: " + str(e))
136 for l in cmd.stdout.read().splitlines():
137 if "Operating Channel:" not in l:
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())
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")
150 def test_p2p_channel_avoid(dev):
151 """P2P and avoid frequencies driver event"""
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)
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,
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)
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)
171 raise Exception("No CTRL-EVENT-AVOID-FREQ event")
172 ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=1)
174 raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP event")
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)
180 raise Exception("No CTRL-EVENT-AVOID-FREQ event")
181 ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=10)
183 raise Exception("No P2P-REMOVE-AND-REFORM-GROUP event")
186 dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES")
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")
193 dev[0].request("SET p2p_no_group_iface 0")
195 channels = { 3 : "2422", 5 : "2432", 9 : "2452" }
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'])
207 def test_go_neg_with_bss_connected(dev, apdev):
208 """P2P channel selection: GO negotiation when station interface is connected"""
210 dev[0].flush_scan_cache()
211 dev[1].flush_scan_cache()
212 dev[0].request("SET p2p_no_group_iface 0")
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")
218 [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[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'])
228 if dev[0].get_mcc() > 1:
229 logger.info("Skip as-client case due to MCC being enabled")
233 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
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)
242 def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
243 """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
245 with HWSimRadio(n_channels=2) as (radio, iface):
246 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
247 wpas.interface_add(iface)
249 wpas.request("SET p2p_no_group_iface 0")
251 if wpas.get_mcc() < 2:
252 raise Exception("New radio does not support MCC")
255 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
257 wpas.request("P2P_SET disallow_freq 2412")
258 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
260 if res['freq'] == "2412":
261 raise Exception("GO set on a disallowed channel")
262 hwsim_utils.test_connectivity(wpas, hapd)
264 wpas.request("P2P_SET disallow_freq ")
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"""
269 with HWSimRadio(n_channels=2) as (radio, iface):
270 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
271 wpas.interface_add(iface)
273 wpas.request("SET p2p_no_group_iface 0")
275 if wpas.get_mcc() < 2:
276 raise Exception("New radio does not support MCC")
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")
288 [i_res, r_res] = go_neg_pbc(i_dev=wpas, i_intent=10, r_dev=dev[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'])
299 [i_res2, r_res2] = go_neg_pbc(i_dev=wpas, i_intent=1, r_dev=dev[1],
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)
308 wpas.request("P2P_SET disallow_freq ")
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)
316 if wpas.get_mcc() < 2:
317 raise Exception("New radio does not support MCC")
319 wpas.request("SET p2p_no_group_iface 0")
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 }
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'])
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)
338 if wpas.get_mcc() < 2:
339 raise Exception("New radio does not support MCC")
341 # Clear possible PBC session overlap from previous test case
342 dev[1].flush_scan_cache()
344 wpas.request("SET p2p_no_group_iface 0")
346 hapd = hostapd.add_ap(apdev[0]['ifname'],
347 { "country_code": 'US',
348 "ssid": 'bss-5ghz', "hw_mode": 'a',
350 wpas.connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
352 # GO and peer force the same freq, different than BSS freq,
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'])
364 # GO and peer force the same freq, different than BSS freq, wpas to
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)
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"""
378 dev[0].request("SET p2p_no_group_iface 0")
381 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
383 dev[0].request("SET p2p_pref_chan 81:2")
384 dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
386 if res['freq'] != "2412":
387 raise Exception("GO channel did not follow BSS")
388 hwsim_utils.test_connectivity(dev[0], hapd)
390 dev[0].request("SET p2p_pref_chan ")
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)
398 if wpas.get_mcc() < 2:
399 raise Exception("New radio does not support MCC")
401 wpas.request("SET p2p_no_group_iface 0")
404 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
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")
410 if res2['freq'] != "2417":
411 raise Exception("GO channel did not follow pref_chan configuration")
412 hwsim_utils.test_connectivity(wpas, hapd)
414 wpas.request("P2P_SET disallow_freq ")
415 wpas.request("SET p2p_pref_chan ")
417 def test_no_go_freq(dev, apdev):
418 """P2P channel selection: no GO freq"""
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")
428 dev[1].remove_group(r_res['ifname'])
430 # dev[0] as GO, channel 1 is not allowed
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)
440 raise Exception("GO set on a disallowed freq")
442 dev[0].request("SET p2p_no_go_freq ")
444 def test_go_neg_peers_force_diff_freq(dev, apdev):
445 """P2P channel selection when peers for different frequency"""
447 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
448 r_dev=dev[1], r_intent=0, r_freq=5200)
451 raise Exception("Unexpected group formation success")
453 def test_autogo_random_channel(dev, apdev):
454 """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
456 go_freqs = ["2412", "2437", "2462"]
457 for i in range(0, 20):
458 result = autogo(dev[0])
459 if result['freq'] not in go_freqs:
460 raise Exception("Unexpected frequency selected: " + result['freq'])
461 if result['freq'] not in freqs:
462 freqs.append(result['freq'])
465 dev[0].remove_group(result['ifname'])
467 raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
469 def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
470 """P2P channel selection: GO preferred channels are disallowed"""
472 dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
473 dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
474 for i in range(0, 5):
476 if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
477 raise Exception("GO channel is disallowed")
478 dev[0].remove_group(res['ifname'])
480 dev[0].request("P2P_SET disallow_freq ")
481 dev[0].request("SET p2p_pref_chan ")
483 def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
484 """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
487 dev[0].request("SET p2p_pref_chan 124:149")
488 res = autogo(dev[0], persistent=True)
489 if res['freq'] != "5745":
490 raise Exception("Unexpected channel selected: " + res['freq'])
491 dev[0].remove_group(res['ifname'])
493 netw = dev[0].list_networks()
495 raise Exception("Unexpected number of network blocks: " + str(netw))
499 res = autogo(dev[0], persistent=id)
500 if res['freq'] == "5745":
501 raise Exception("Unexpected channel selected(2): " + res['freq'])
502 dev[0].remove_group(res['ifname'])
504 dev[0].request("SET p2p_pref_chan ")