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 81")
148 dev[0].request("SET p2p_oper_channel 11")
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].request("SET p2p_no_group_iface 0")
212 hapd = hostapd.add_ap(apdev[0]['ifname'],
213 { "ssid": 'bss-2.4ghz', "channel": '5' })
214 dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2432")
216 [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
218 check_grpform_results(i_res, r_res)
219 if i_res['role'] != "GO":
220 raise Exception("GO not selected according to go_intent")
221 if i_res['freq'] != "2432":
222 raise Exception("Group formed on a different frequency than BSS")
223 hwsim_utils.test_connectivity(dev[0], hapd)
224 dev[0].remove_group(i_res['ifname'])
226 if dev[0].get_mcc() > 1:
227 logger.info("Skip as-client case due to MCC being enabled")
231 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
233 check_grpform_results(i_res2, r_res2)
234 if i_res2['role'] != "client":
235 raise Exception("GO not selected according to go_intent")
236 if i_res2['freq'] != "2432":
237 raise Exception("Group formed on a different frequency than BSS")
238 hwsim_utils.test_connectivity(dev[0], hapd)
240 def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
241 """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
243 with HWSimRadio(n_channels=2) as (radio, iface):
244 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
245 wpas.interface_add(iface)
247 wpas.request("SET p2p_no_group_iface 0")
249 if wpas.get_mcc() < 2:
250 raise Exception("New radio does not support MCC")
253 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
255 wpas.request("P2P_SET disallow_freq 2412")
256 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
258 if res['freq'] == "2412":
259 raise Exception("GO set on a disallowed channel")
260 hwsim_utils.test_connectivity(wpas, hapd)
262 wpas.request("P2P_SET disallow_freq ")
264 def test_go_neg_with_bss_on_disallowed_chan(dev, apdev):
265 """P2P channel selection: GO negotiation with station interface on a disallowed channel"""
267 with HWSimRadio(n_channels=2) as (radio, iface):
268 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
269 wpas.interface_add(iface)
271 wpas.request("SET p2p_no_group_iface 0")
273 if wpas.get_mcc() < 2:
274 raise Exception("New radio does not support MCC")
277 hapd = hostapd.add_ap(apdev[0]['ifname'],
278 { "ssid": 'bss-2.4ghz', "channel": '1' })
279 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
280 wpas.request("P2P_SET disallow_freq 2412")
283 [i_res, r_res] = go_neg_pbc(i_dev=wpas, i_intent=10, r_dev=dev[1],
285 check_grpform_results(i_res, r_res)
286 if i_res['role'] != "GO":
287 raise Exception("GO not selected according to go_intent")
288 if i_res['freq'] == "2412":
289 raise Exception("Group formed on a disallowed channel")
290 hwsim_utils.test_connectivity(wpas, hapd)
291 wpas.remove_group(i_res['ifname'])
294 [i_res2, r_res2] = go_neg_pbc(i_dev=wpas, i_intent=1, r_dev=dev[1],
296 check_grpform_results(i_res2, r_res2)
297 if i_res2['role'] != "client":
298 raise Exception("GO not selected according to go_intent")
299 if i_res2['freq'] == "2412":
300 raise Exception("Group formed on a disallowed channel")
301 hwsim_utils.test_connectivity(wpas, hapd)
303 wpas.request("P2P_SET disallow_freq ")
305 def test_autogo_force_diff_channel(dev, apdev):
306 """P2P autonomous GO and station interface operate on different channels"""
307 with HWSimRadio(n_channels=2) as (radio, iface):
308 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
309 wpas.interface_add(iface)
311 if wpas.get_mcc() < 2:
312 raise Exception("New radio does not support MCC")
314 wpas.request("SET p2p_no_group_iface 0")
316 hapd = hostapd.add_ap(apdev[0]['ifname'],
317 {"ssid" : 'ap-test', "channel" : '1'})
318 wpas.connect("ap-test", key_mgmt = "NONE", scan_freq = "2412")
319 channels = { 2 : 2417, 5 : 2432, 9 : 2452 }
321 res_go = autogo(wpas, channels[key])
322 hwsim_utils.test_connectivity(wpas, hapd)
323 if int(res_go['freq']) == 2412:
324 raise Exception("Group operation channel is: 2412 excepted: " + res_go['freq'])
325 wpas.remove_group(res_go['ifname'])
327 def test_go_neg_forced_freq_diff_than_bss_freq(dev, apdev):
328 """P2P channel selection: GO negotiation with forced freq different than station interface"""
329 with HWSimRadio(n_channels=2) as (radio, iface):
330 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
331 wpas.interface_add(iface)
333 if wpas.get_mcc() < 2:
334 raise Exception("New radio does not support MCC")
336 wpas.request("SET p2p_no_group_iface 0")
338 hapd = hostapd.add_ap(apdev[0]['ifname'],
339 { "country_code": 'US',
340 "ssid": 'bss-5ghz', "hw_mode": 'a',
342 wpas.connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
344 # GO and peer force the same freq, different than BSS freq,
346 [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=1, i_freq=5180,
347 r_dev=wpas, r_intent=14, r_freq=5180)
348 check_grpform_results(i_res, r_res)
349 if i_res['freq'] != "5180":
350 raise Exception("P2P group formed on unexpected frequency: " + i_res['freq'])
351 if r_res['role'] != "GO":
352 raise Exception("GO not selected according to go_intent")
353 hwsim_utils.test_connectivity(wpas, hapd)
354 wpas.remove_group(r_res['ifname'])
356 # GO and peer force the same freq, different than BSS freq, wpas to
358 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[1], i_intent=14, i_freq=2422,
359 r_dev=wpas, r_intent=1, r_freq=2422)
360 check_grpform_results(i_res2, r_res2)
361 if i_res2['freq'] != "2422":
362 raise Exception("P2P group formed on unexpected frequency: " + i_res2['freq'])
363 if r_res2['role'] != "client":
364 raise Exception("GO not selected according to go_intent")
365 hwsim_utils.test_connectivity(wpas, hapd)
367 def test_go_pref_chan_bss_on_diff_chan(dev, apdev):
368 """P2P channel selection: Station on different channel than GO configured pref channel"""
370 dev[0].request("SET p2p_no_group_iface 0")
373 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
375 dev[0].request("SET p2p_pref_chan 81:2")
376 dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
378 if res['freq'] != "2412":
379 raise Exception("GO channel did not follow BSS")
380 hwsim_utils.test_connectivity(dev[0], hapd)
382 dev[0].request("SET p2p_pref_chan ")
384 def test_go_pref_chan_bss_on_disallowed_chan(dev, apdev):
385 """P2P channel selection: Station interface on different channel than GO configured pref channel, and station channel is disallowed"""
386 with HWSimRadio(n_channels=2) as (radio, iface):
387 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
388 wpas.interface_add(iface)
390 if wpas.get_mcc() < 2:
391 raise Exception("New radio does not support MCC")
393 wpas.request("SET p2p_no_group_iface 0")
396 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
398 wpas.request("P2P_SET disallow_freq 2412")
399 wpas.request("SET p2p_pref_chan 81:2")
400 wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
402 if res2['freq'] != "2417":
403 raise Exception("GO channel did not follow pref_chan configuration")
404 hwsim_utils.test_connectivity(wpas, hapd)
406 wpas.request("P2P_SET disallow_freq ")
407 wpas.request("SET p2p_pref_chan ")
409 def test_no_go_freq(dev, apdev):
410 """P2P channel selection: no GO freq"""
412 dev[0].request("SET p2p_no_go_freq 2412")
413 # dev[0] as client, channel 1 is ok
414 [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=1,
415 r_dev=dev[1], r_intent=14, r_freq=2412)
416 check_grpform_results(i_res, r_res)
417 if i_res['freq'] != "2412":
418 raise Exception("P2P group not formed on forced freq")
420 dev[1].remove_group(r_res['ifname'])
422 # dev[0] as GO, channel 1 is not allowed
424 dev[0].request("SET p2p_no_go_freq 2412")
425 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14,
426 r_dev=dev[1], r_intent=1, r_freq=2412)
427 check_grpform_results(i_res2, r_res2)
432 raise Exception("GO set on a disallowed freq")
434 dev[0].request("SET p2p_no_go_freq ")
436 def test_go_neg_peers_force_diff_freq(dev, apdev):
438 [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
439 r_dev=dev[1], r_intent=0, r_freq=5200)
442 raise Exception("Unexpected group formation success")
444 def test_autogo_random_channel(dev, apdev):
445 """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
447 go_freqs = ["2412", "2437", "2462"]
448 for i in range(0, 20):
449 result = autogo(dev[0])
450 if result['freq'] not in go_freqs:
451 raise Exception("Unexpected frequency selected: " + result['freq'])
452 if result['freq'] not in freqs:
453 freqs.append(result['freq'])
456 dev[0].remove_group(result['ifname'])
458 raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
460 def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
461 """P2P channel selection: GO preferred channels are disallowed"""
463 dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
464 dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
465 for i in range(0, 5):
467 if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
468 raise Exception("GO channel is disallowed")
469 dev[0].remove_group(res['ifname'])
471 dev[0].request("P2P_SET disallow_freq ")
472 dev[0].request("SET p2p_pref_chan ")
474 def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
475 """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
478 dev[0].request("SET p2p_pref_chan 124:149")
479 res = autogo(dev[0], persistent=True)
480 if res['freq'] != "5745":
481 raise Exception("Unexpected channel selected: " + res['freq'])
482 dev[0].remove_group(res['ifname'])
484 netw = dev[0].list_networks()
486 raise Exception("Unexpected number of network blocks: " + str(netw))
490 res = autogo(dev[0], persistent=id)
491 if res['freq'] == "5745":
492 raise Exception("Unexpected channel selected(2): " + res['freq'])
493 dev[0].remove_group(res['ifname'])
495 dev[0].request("SET p2p_pref_chan ")