Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_autogo.py
1 # P2P autonomous GO test cases
2 # Copyright (c) 2013-2015, 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 time
8 import subprocess
9 import logging
10 logger = logging.getLogger()
11
12 import hwsim_utils
13 import utils
14 from utils import HwsimSkip
15 from wlantest import Wlantest
16 from wpasupplicant import WpaSupplicant
17
18 def autogo(go, freq=None, persistent=None):
19     logger.info("Start autonomous GO " + go.ifname)
20     res = go.p2p_start_go(freq=freq, persistent=persistent)
21     logger.debug("res: " + str(res))
22     return res
23
24 def connect_cli(go, client, social=False, freq=None):
25     logger.info("Try to connect the client to the GO")
26     pin = client.wps_read_pin()
27     go.p2p_go_authorize_client(pin)
28     res = client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60,
29                                    social=social, freq=freq)
30     logger.info("Client connected")
31     hwsim_utils.test_connectivity_p2p(go, client)
32     return res
33
34 def test_autogo(dev):
35     """P2P autonomous GO and client joining group"""
36     addr0 = dev[0].p2p_dev_addr()
37     addr2 = dev[2].p2p_dev_addr()
38     res = autogo(dev[0])
39     if "p2p-wlan" in res['ifname']:
40         raise Exception("Unexpected group interface name on GO")
41     res = connect_cli(dev[0], dev[1])
42     if "p2p-wlan" in res['ifname']:
43         raise Exception("Unexpected group interface name on client")
44     bss = dev[1].get_bss("p2p_dev_addr=" + addr0, res['ifname'])
45     if not bss or bss['bssid'] != dev[0].p2p_interface_addr():
46         raise Exception("Unexpected BSSID in the BSS entry for the GO")
47     id = bss['id']
48     bss = dev[1].get_bss("ID-" + id, res['ifname'])
49     if not bss or bss['id'] != id:
50         raise Exception("Could not find BSS entry based on id")
51     res = dev[1].group_request("BSS RANGE=" + id + "- MASK=0x1")
52     if "id=" + id not in res:
53         raise Exception("Could not find BSS entry based on id range")
54
55     res = dev[1].request("SCAN_RESULTS")
56     if "[P2P]" not in res:
57         raise Exception("P2P flag missing from scan results: " + res)
58
59     # Presence request to increase testing coverage
60     if "FAIL" not in dev[1].group_request("P2P_PRESENCE_REQ 30000"):
61         raise Exception("Invald P2P_PRESENCE_REQ accepted")
62     if "FAIL" not in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400 30001"):
63         raise Exception("Invald P2P_PRESENCE_REQ accepted")
64     if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
65         raise Exception("Could not send presence request")
66     ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"], 10)
67     if ev is None:
68         raise Exception("Timeout while waiting for Presence Response")
69     if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400 20000 102400"):
70         raise Exception("Could not send presence request")
71     ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
72     if ev is None:
73         raise Exception("Timeout while waiting for Presence Response")
74     if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ"):
75         raise Exception("Could not send presence request")
76     ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
77     if ev is None:
78         raise Exception("Timeout while waiting for Presence Response")
79
80     if not dev[2].discover_peer(addr0):
81         raise Exception("Could not discover GO")
82     dev[0].dump_monitor()
83     dev[2].global_request("P2P_PROV_DISC " + addr0 + " display join")
84     ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=10)
85     if ev is None:
86         raise Exception("GO did not report P2P-PROV-DISC-SHOW-PIN")
87     if "p2p_dev_addr=" + addr2 not in ev:
88         raise Exception("Unexpected P2P Device Address in event: " + ev)
89     if "group=" + dev[0].group_ifname not in ev:
90         raise Exception("Unexpected group interface in event: " + ev)
91     ev = dev[2].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=10)
92     if ev is None:
93         raise Exception("P2P-PROV-DISC-ENTER-PIN not reported")
94
95     dev[0].remove_group()
96     dev[1].wait_go_ending_session()
97
98 def test_autogo2(dev):
99     """P2P autonomous GO with a separate group interface and client joining group"""
100     dev[0].request("SET p2p_no_group_iface 0")
101     res = autogo(dev[0], freq=2437)
102     if "p2p-wlan" not in res['ifname']:
103         raise Exception("Unexpected group interface name on GO")
104     if res['ifname'] not in utils.get_ifnames():
105         raise Exception("Could not find group interface netdev")
106     connect_cli(dev[0], dev[1], social=True, freq=2437)
107     dev[0].remove_group()
108     dev[1].wait_go_ending_session()
109     if res['ifname'] in utils.get_ifnames():
110         raise Exception("Group interface netdev was not removed")
111
112 def test_autogo3(dev):
113     """P2P autonomous GO and client with a separate group interface joining group"""
114     dev[1].request("SET p2p_no_group_iface 0")
115     autogo(dev[0], freq=2462)
116     res = connect_cli(dev[0], dev[1], social=True, freq=2462)
117     if "p2p-wlan" not in res['ifname']:
118         raise Exception("Unexpected group interface name on client")
119     if res['ifname'] not in utils.get_ifnames():
120         raise Exception("Could not find group interface netdev")
121     dev[0].remove_group()
122     dev[1].wait_go_ending_session()
123     dev[1].ping()
124     if res['ifname'] in utils.get_ifnames():
125         raise Exception("Group interface netdev was not removed")
126
127 def test_autogo4(dev):
128     """P2P autonomous GO and client joining group (both with a separate group interface)"""
129     dev[0].request("SET p2p_no_group_iface 0")
130     dev[1].request("SET p2p_no_group_iface 0")
131     res1 = autogo(dev[0], freq=2412)
132     res2 = connect_cli(dev[0], dev[1], social=True, freq=2412)
133     if "p2p-wlan" not in res1['ifname']:
134         raise Exception("Unexpected group interface name on GO")
135     if "p2p-wlan" not in res2['ifname']:
136         raise Exception("Unexpected group interface name on client")
137     ifnames = utils.get_ifnames()
138     if res1['ifname'] not in ifnames:
139         raise Exception("Could not find GO group interface netdev")
140     if res2['ifname'] not in ifnames:
141         raise Exception("Could not find client group interface netdev")
142     dev[0].remove_group()
143     dev[1].wait_go_ending_session()
144     dev[1].ping()
145     ifnames = utils.get_ifnames()
146     if res1['ifname'] in ifnames:
147         raise Exception("GO group interface netdev was not removed")
148     if res2['ifname'] in ifnames:
149         raise Exception("Client group interface netdev was not removed")
150
151 def test_autogo_m2d(dev):
152     """P2P autonomous GO and clients not authorized"""
153     autogo(dev[0], freq=2412)
154     go_addr = dev[0].p2p_dev_addr()
155
156     dev[1].request("SET p2p_no_group_iface 0")
157     if not dev[1].discover_peer(go_addr, social=True):
158         raise Exception("GO " + go_addr + " not found")
159     dev[1].dump_monitor()
160
161     if not dev[2].discover_peer(go_addr, social=True):
162         raise Exception("GO " + go_addr + " not found")
163     dev[2].dump_monitor()
164
165     logger.info("Trying to join the group when GO has not authorized the client")
166     pin = dev[1].wps_read_pin()
167     cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
168     if "OK" not in dev[1].global_request(cmd):
169         raise Exception("P2P_CONNECT join failed")
170
171     pin = dev[2].wps_read_pin()
172     cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
173     if "OK" not in dev[2].global_request(cmd):
174         raise Exception("P2P_CONNECT join failed")
175
176     ev = dev[1].wait_global_event(["WPS-M2D"], timeout=16)
177     if ev is None:
178         raise Exception("No global M2D event")
179     ifaces = dev[1].request("INTERFACES").splitlines()
180     iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
181     wpas = WpaSupplicant(ifname=iface)
182     ev = wpas.wait_event(["WPS-M2D"], timeout=10)
183     if ev is None:
184         raise Exception("No M2D event on group interface")
185
186     ev = dev[2].wait_global_event(["WPS-M2D"], timeout=10)
187     if ev is None:
188         raise Exception("No global M2D event (2)")
189     ev = dev[2].wait_event(["WPS-M2D"], timeout=10)
190     if ev is None:
191         raise Exception("No M2D event on group interface (2)")
192
193 def test_autogo_fail(dev):
194     """P2P autonomous GO and incorrect PIN"""
195     autogo(dev[0], freq=2412)
196     go_addr = dev[0].p2p_dev_addr()
197     dev[0].p2p_go_authorize_client("00000000")
198
199     dev[1].request("SET p2p_no_group_iface 0")
200     if not dev[1].discover_peer(go_addr, social=True):
201         raise Exception("GO " + go_addr + " not found")
202     dev[1].dump_monitor()
203
204     logger.info("Trying to join the group when GO has not authorized the client")
205     pin = dev[1].wps_read_pin()
206     cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
207     if "OK" not in dev[1].global_request(cmd):
208         raise Exception("P2P_CONNECT join failed")
209
210     ev = dev[1].wait_global_event(["WPS-FAIL"], timeout=10)
211     if ev is None:
212         raise Exception("No global WPS-FAIL event")
213
214 def test_autogo_2cli(dev):
215     """P2P autonomous GO and two clients joining group"""
216     autogo(dev[0], freq=2412)
217     connect_cli(dev[0], dev[1], social=True, freq=2412)
218     connect_cli(dev[0], dev[2], social=True, freq=2412)
219     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
220     dev[0].global_request("P2P_REMOVE_CLIENT " + dev[1].p2p_dev_addr())
221     dev[1].wait_go_ending_session()
222     dev[0].global_request("P2P_REMOVE_CLIENT iface=" + dev[2].p2p_interface_addr())
223     dev[2].wait_go_ending_session()
224     if "FAIL" not in dev[0].global_request("P2P_REMOVE_CLIENT foo"):
225         raise Exception("Invalid P2P_REMOVE_CLIENT command accepted")
226     dev[0].remove_group()
227
228 def test_autogo_pbc(dev):
229     """P2P autonomous GO and PBC"""
230     dev[1].global_request("SET p2p_no_group_iface 0")
231     autogo(dev[0], freq=2412)
232     if "FAIL" not in dev[0].group_request("WPS_PBC p2p_dev_addr=00:11:22:33:44"):
233         raise Exception("Invalid WPS_PBC succeeded")
234     if "OK" not in dev[0].group_request("WPS_PBC p2p_dev_addr=" + dev[1].p2p_dev_addr()):
235         raise Exception("WPS_PBC failed")
236     dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=0,
237                              social=True)
238     ev = dev[2].wait_global_event(["WPS-M2D"], timeout=15)
239     if ev is None:
240         raise Exception("WPS-M2D not reported")
241     if "config_error=12" not in ev:
242         raise Exception("Unexpected config_error: " + ev)
243     dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=15,
244                              social=True)
245
246 def test_autogo_tdls(dev):
247     """P2P autonomous GO and two clients using TDLS"""
248     wt = Wlantest()
249     go = dev[0]
250     logger.info("Start autonomous GO with fixed parameters " + go.ifname)
251     id = go.add_network()
252     go.set_network_quoted(id, "ssid", "DIRECT-tdls")
253     go.set_network_quoted(id, "psk", "12345678")
254     go.set_network(id, "mode", "3")
255     go.set_network(id, "disabled", "2")
256     res = go.p2p_start_go(persistent=id, freq="2462")
257     logger.debug("res: " + str(res))
258     wt.flush()
259     wt.add_passphrase("12345678")
260     connect_cli(go, dev[1], social=True, freq=2462)
261     connect_cli(go, dev[2], social=True, freq=2462)
262     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
263     bssid = dev[0].p2p_interface_addr()
264     addr1 = dev[1].p2p_interface_addr()
265     addr2 = dev[2].p2p_interface_addr()
266     dev[1].tdls_setup(addr2)
267     time.sleep(1)
268     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
269     conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr1, addr2);
270     if conf == 0:
271         raise Exception("No TDLS Setup Confirm (success) seen")
272     dl = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
273     if dl == 0:
274         raise Exception("No valid frames through direct link")
275     wt.tdls_clear(bssid, addr1, addr2);
276     dev[1].tdls_teardown(addr2)
277     time.sleep(1)
278     teardown = wt.get_tdls_counter("teardown", bssid, addr1, addr2);
279     if teardown == 0:
280         raise Exception("No TDLS Setup Teardown seen")
281     wt.tdls_clear(bssid, addr1, addr2);
282     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
283     ap_path = wt.get_tdls_counter("valid_ap_path", bssid, addr1, addr2);
284     if ap_path == 0:
285         raise Exception("No valid frames via AP path")
286     direct_link = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
287     if direct_link > 0:
288         raise Exception("Unexpected frames through direct link")
289     idirect_link = wt.get_tdls_counter("invalid_direct_link", bssid, addr1,
290                                        addr2);
291     if idirect_link > 0:
292         raise Exception("Unexpected frames through direct link (invalid)")
293     dev[2].remove_group()
294     dev[1].remove_group()
295     dev[0].remove_group()
296
297 def test_autogo_legacy(dev):
298     """P2P autonomous GO and legacy clients"""
299     res = autogo(dev[0], freq=2462)
300     if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
301         raise Exception("passphrase mismatch")
302     if dev[0].group_request("P2P_GET_PASSPHRASE") != res['passphrase']:
303         raise Exception("passphrase mismatch(2)")
304
305     logger.info("Connect P2P client")
306     connect_cli(dev[0], dev[1], social=True, freq=2462)
307
308     if "FAIL" not in dev[1].request("P2P_GET_PASSPHRASE"):
309         raise Exception("P2P_GET_PASSPHRASE succeeded on P2P Client")
310
311     logger.info("Connect legacy WPS client")
312     pin = dev[2].wps_read_pin()
313     dev[0].p2p_go_authorize_client(pin)
314     dev[2].request("P2P_SET disabled 1")
315     dev[2].dump_monitor()
316     dev[2].request("WPS_PIN any " + pin)
317     dev[2].wait_connected(timeout=30)
318     status = dev[2].get_status()
319     if status['wpa_state'] != 'COMPLETED':
320         raise Exception("Not fully connected")
321     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
322     dev[2].request("DISCONNECT")
323
324     logger.info("Connect legacy non-WPS client")
325     dev[2].request("FLUSH")
326     dev[2].request("P2P_SET disabled 1")
327     dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
328                    key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
329                    scan_freq=res['freq'])
330     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
331     dev[2].request("DISCONNECT")
332
333     dev[0].remove_group()
334     dev[1].wait_go_ending_session()
335
336 def test_autogo_chan_switch(dev):
337     """P2P autonomous GO switching channels"""
338     autogo(dev[0], freq=2417)
339     connect_cli(dev[0], dev[1])
340     res = dev[0].request("CHAN_SWITCH 5 2422")
341     if "FAIL" in res:
342         # for now, skip test since mac80211_hwsim support is not yet widely
343         # deployed
344         raise HwsimSkip("Assume mac80211_hwsim did not support channel switching")
345     ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
346     if ev is None:
347         raise Exception("CSA finished event timed out")
348     if "freq=2422" not in ev:
349         raise Exception("Unexpected cahnnel in CSA finished event")
350     dev[0].dump_monitor()
351     dev[1].dump_monitor()
352     time.sleep(0.1)
353     hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
354
355 def test_autogo_extra_cred(dev):
356     """P2P autonomous GO sending two WPS credentials"""
357     if "FAIL" in dev[0].request("SET wps_testing_dummy_cred 1"):
358         raise Exception("Failed to enable test mode")
359     autogo(dev[0], freq=2412)
360     connect_cli(dev[0], dev[1], social=True, freq=2412)
361     dev[0].remove_group()
362     dev[1].wait_go_ending_session()
363
364 def test_autogo_ifdown(dev):
365     """P2P autonomous GO and external ifdown"""
366     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
367     wpas.interface_add("wlan5")
368     res = autogo(wpas)
369     wpas.dump_monitor()
370     wpas.interface_remove("wlan5")
371     wpas.interface_add("wlan5")
372     res = autogo(wpas)
373     wpas.dump_monitor()
374     subprocess.call(['ifconfig', res['ifname'], 'down'])
375     ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
376     if ev is None:
377         raise Exception("Group removal not reported")
378     if res['ifname'] not in ev:
379         raise Exception("Unexpected group removal event: " + ev)
380
381 def test_autogo_start_during_scan(dev):
382     """P2P autonomous GO started during ongoing manual scan"""
383     try:
384         # use autoscan to set scan_req = MANUAL_SCAN_REQ
385         if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
386             raise Exception("Failed to set autoscan")
387         autogo(dev[0], freq=2462)
388         connect_cli(dev[0], dev[1], social=True, freq=2462)
389         dev[0].remove_group()
390         dev[1].wait_go_ending_session()
391     finally:
392         dev[0].request("AUTOSCAN ")
393
394 def test_autogo_passphrase_len(dev):
395     """P2P autonomous GO and longer passphrase"""
396     try:
397         if "OK" not in dev[0].request("SET p2p_passphrase_len 13"):
398             raise Exception("Failed to set passphrase length")
399         res = autogo(dev[0], freq=2412)
400         if len(res['passphrase']) != 13:
401             raise Exception("Unexpected passphrase length")
402         if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
403             raise Exception("passphrase mismatch")
404
405         logger.info("Connect P2P client")
406         connect_cli(dev[0], dev[1], social=True, freq=2412)
407
408         logger.info("Connect legacy WPS client")
409         pin = dev[2].wps_read_pin()
410         dev[0].p2p_go_authorize_client(pin)
411         dev[2].request("P2P_SET disabled 1")
412         dev[2].dump_monitor()
413         dev[2].request("WPS_PIN any " + pin)
414         dev[2].wait_connected(timeout=30)
415         status = dev[2].get_status()
416         if status['wpa_state'] != 'COMPLETED':
417             raise Exception("Not fully connected")
418         dev[2].request("DISCONNECT")
419
420         logger.info("Connect legacy non-WPS client")
421         dev[2].request("FLUSH")
422         dev[2].request("P2P_SET disabled 1")
423         dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
424                        key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
425                        scan_freq=res['freq'])
426         hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
427         dev[2].request("DISCONNECT")
428
429         dev[0].remove_group()
430         dev[1].wait_go_ending_session()
431     finally:
432         dev[0].request("SET p2p_passphrase_len 8")
433
434 def test_autogo_bridge(dev):
435     """P2P autonomous GO in a bridge"""
436     try:
437         # use autoscan to set scan_req = MANUAL_SCAN_REQ
438         if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
439             raise Exception("Failed to set autoscan")
440         autogo(dev[0])
441         ifname = dev[0].get_group_ifname()
442         subprocess.call(['brctl', 'addbr', 'p2p-br0'])
443         subprocess.call(['brctl', 'setfd', 'p2p-br0', '0'])
444         subprocess.call(['brctl', 'addif', 'p2p-br0', ifname])
445         subprocess.call(['ip', 'link', 'set', 'dev', 'p2p-br0', 'up'])
446         time.sleep(0.1)
447         subprocess.call(['brctl', 'delif', 'p2p-br0', ifname])
448         time.sleep(0.1)
449         subprocess.call(['ip', 'link', 'set', 'dev', 'p2p-br0', 'down'])
450         time.sleep(0.1)
451         subprocess.call(['brctl', 'delbr', 'p2p-br0'])
452         ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=1)
453         if ev is not None:
454             raise Exception("P2P group removed unexpectedly")
455         if dev[0].get_group_status_field('wpa_state') != "COMPLETED":
456             raise Exception("Unexpected wpa_state")
457         dev[0].remove_group()
458     finally:
459         dev[0].request("AUTOSCAN ")
460         subprocess.Popen(['brctl', 'delif', 'p2p-br0', ifname],
461                          stderr=open('/dev/null', 'w'))
462         subprocess.Popen(['ip', 'link', 'set', 'dev', 'p2p-br0', 'down'],
463                          stderr=open('/dev/null', 'w'))
464         subprocess.Popen(['brctl', 'delbr', 'p2p-br0'],
465                          stderr=open('/dev/null', 'w'))
466
467 def test_presence_req_on_group_interface(dev):
468     """P2P_PRESENCE_REQ on group interface"""
469     dev[1].request("SET p2p_no_group_iface 0")
470     res = autogo(dev[0], freq=2437)
471     res = connect_cli(dev[0], dev[1], social=True, freq=2437)
472     if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
473         raise Exception("Could not send presence request")
474     ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
475     if ev is None:
476         raise Exception("Timeout while waiting for Presence Response")
477     dev[0].remove_group()
478     dev[1].wait_go_ending_session()
479
480 def test_autogo_join_auto_go_not_found(dev):
481     """P2P_CONNECT-auto not finding GO"""
482     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
483     wpas.interface_add("wlan5")
484     wpas.request("P2P_SET listen_channel 1")
485     wpas.global_request("SET p2p_no_group_iface 0")
486     autogo(wpas, freq=2412)
487     addr = wpas.p2p_dev_addr()
488     bssid = wpas.p2p_interface_addr()
489
490     dev[1].global_request("SET p2p_no_group_iface 0")
491     dev[1].scan_for_bss(bssid, freq=2412)
492     # This makes the GO not show up in the scan iteration following the
493     # P2P_CONNECT command by stopping beaconing and handling Probe Request
494     # frames externally (but not really replying to them). P2P listen mode is
495     # needed to keep the GO listening on the operating channel for the PD
496     # exchange.
497     if "OK" not in wpas.group_request("STOP_AP"):
498         raise Exception("STOP_AP failed")
499     wpas.group_request("SET ext_mgmt_frame_handling 1")
500     wpas.p2p_listen()
501     time.sleep(0.02)
502     dev[1].global_request("P2P_CONNECT " + addr + " pbc auto")
503
504     ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG-ENABLED"], 15)
505     if ev is None:
506         raise Exception("Could not trigger old-scan-only case")
507         return
508
509     ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], 15)
510     wpas.remove_group()
511     if ev is None:
512         raise Exception("Fallback to GO Negotiation not seen")
513     if "reason=GO-not-found" not in ev:
514         raise Exception("Unexpected reason for fallback: " + ev)
515
516 def test_autogo_join_auto(dev):
517     """P2P_CONNECT-auto joining a group"""
518     autogo(dev[0])
519     addr = dev[0].p2p_dev_addr()
520     if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
521         raise Exception("P2P_CONNECT failed")
522
523     ev = dev[0].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout=15)
524     if ev is None:
525         raise Exception("Timeout on P2P-PROV-DISC-PBC-REQ")
526     if "group=" + dev[0].group_ifname not in ev:
527         raise Exception("Unexpected PD event contents: " + ev)
528     dev[0].group_request("WPS_PBC")
529
530     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
531     if ev is None:
532         raise Exception("Joining the group timed out")
533     dev[1].group_form_result(ev)
534
535     dev[0].remove_group()
536     dev[1].wait_go_ending_session()
537     dev[1].flush_scan_cache()
538
539 def test_autogo_join_auto_go_neg(dev):
540     """P2P_CONNECT-auto fallback to GO Neg"""
541     dev[1].flush_scan_cache()
542     dev[0].p2p_listen()
543     addr = dev[0].p2p_dev_addr()
544     if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
545         raise Exception("P2P_CONNECT failed")
546
547     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
548     if ev is None:
549         raise Exception("Timeout on P2P-GO-NEG-REQUEST")
550     peer = ev.split(' ')[1]
551     dev[0].p2p_go_neg_init(peer, None, "pbc", timeout=15, go_intent=15)
552
553     ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
554     if ev is None:
555         raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
556     if "P2P-FALLBACK-TO-GO-NEG-ENABLED" in ev:
557         ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
558         if ev is None:
559             raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
560     if "reason=peer-not-running-GO" not in ev:
561         raise Exception("Unexpected reason: " + ev)
562
563     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
564     if ev is None:
565         raise Exception("Joining the group timed out")
566     dev[1].group_form_result(ev)
567
568     dev[0].remove_group()
569     dev[1].wait_go_ending_session()
570     dev[1].flush_scan_cache()
571
572 def test_autogo_join_auto_go_neg_after_seeing_go(dev):
573     """P2P_CONNECT-auto fallback to GO Neg after seeing GO"""
574     autogo(dev[0], freq=2412)
575     addr = dev[0].p2p_dev_addr()
576     bssid = dev[0].p2p_interface_addr()
577     dev[1].scan_for_bss(bssid, freq=2412)
578     dev[0].remove_group()
579     dev[0].p2p_listen()
580
581     if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
582         raise Exception("P2P_CONNECT failed")
583
584     ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG-ENABLED"],
585                                   timeout=15)
586     if ev is None:
587         raise Exception("No P2P-FALLBACK-TO-GO-NEG-ENABLED event seen")
588
589     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
590     if ev is None:
591         raise Exception("Timeout on P2P-GO-NEG-REQUEST")
592     peer = ev.split(' ')[1]
593     dev[0].p2p_go_neg_init(peer, None, "pbc", timeout=15, go_intent=15)
594
595     ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
596     if ev is None:
597         raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
598     if "reason=no-ACK-to-PD-Req" not in ev and "reason=PD-failed" not in ev:
599         raise Exception("Unexpected reason: " + ev)
600
601     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
602     if ev is None:
603         raise Exception("Joining the group timed out")
604     dev[1].group_form_result(ev)
605
606     dev[0].remove_group()
607     dev[1].wait_go_ending_session()
608     dev[1].flush_scan_cache()
609
610 def test_go_search_non_social(dev):
611     """P2P_FIND with freq parameter to scan a single channel"""
612     addr0 = dev[0].p2p_dev_addr()
613     autogo(dev[0], freq=2422)
614     dev[1].p2p_find(freq=2422)
615     ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=3.5)
616     if ev is None:
617         raise Exception("Did not find GO quickly enough")
618     dev[2].p2p_listen()
619     ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
620     if ev is None:
621         raise Exception("Did not find peer")
622     dev[2].p2p_stop_find()
623     dev[1].p2p_stop_find()
624     dev[0].remove_group()
625
626 def test_autogo_many(dev):
627     """P2P autonomous GO with large number of GO instances"""
628     dev[0].request("SET p2p_no_group_iface 0")
629     for i in range(100):
630         if "OK" not in dev[0].global_request("P2P_GROUP_ADD freq=2412"):
631             logger.info("Was able to add %d groups" % i)
632             if i < 5:
633                 raise Exception("P2P_GROUP_ADD failed")
634             stop_ev = dev[0].wait_global_event(["P2P-GROUP-REMOVE"], timeout=1)
635             if stop_ev is not None:
636                 raise Exception("Unexpected P2P-GROUP-REMOVE event")
637             break
638         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
639         if ev is None:
640             raise Exception("GO start up timed out")
641         dev[0].group_form_result(ev)
642
643     for i in dev[0].global_request("INTERFACES").splitlines():
644         dev[0].request("P2P_GROUP_REMOVE " + i)
645         dev[0].dump_monitor()
646     dev[0].request("P2P_GROUP_REMOVE *")
647
648 def test_autogo_many_clients(dev):
649     """P2P autonomous GO and many clients (P2P IE fragmentation)"""
650     try:
651         _test_autogo_many_clients(dev)
652     finally:
653         dev[0].global_request("SET device_name Device A")
654         dev[1].global_request("SET device_name Device B")
655         dev[2].global_request("SET device_name Device C")
656
657 def _test_autogo_many_clients(dev):
658     # These long device names will push the P2P IE contents beyond the limit
659     # that requires fragmentation.
660     name0 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
661     name1 = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
662     name2 = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
663     name3 = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
664     dev[0].global_request("SET device_name " + name0)
665     dev[1].global_request("SET device_name " + name1)
666     dev[2].global_request("SET device_name " + name2)
667
668     addr0 = dev[0].p2p_dev_addr()
669     res = autogo(dev[0], freq=2412)
670     bssid = dev[0].p2p_interface_addr()
671
672     connect_cli(dev[0], dev[1], social=True, freq=2412)
673     dev[0].dump_monitor()
674     connect_cli(dev[0], dev[2], social=True, freq=2412)
675     dev[0].dump_monitor()
676
677     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
678     wpas.interface_add("wlan5")
679     wpas.global_request("SET device_name " + name3)
680     wpas.global_request("SET sec_device_type 1-11111111-1")
681     wpas.global_request("SET sec_device_type 2-22222222-2")
682     wpas.global_request("SET sec_device_type 3-33333333-3")
683     wpas.global_request("SET sec_device_type 4-44444444-4")
684     wpas.global_request("SET sec_device_type 5-55555555-5")
685     connect_cli(dev[0], wpas, social=True, freq=2412)
686     dev[0].dump_monitor()
687
688     dev[1].dump_monitor()
689     dev[1].p2p_find(freq=2412)
690     ev1 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
691     if ev1 is None:
692         raise Exception("Could not find peer (1)")
693     ev2 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
694     if ev2 is None:
695         raise Exception("Could not find peer (2)")
696     ev3 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
697     if ev3 is None:
698         raise Exception("Could not find peer (3)")
699     dev[1].p2p_stop_find()
700
701     for i in [ name0, name2, name3 ]:
702         if i not in ev1 and i not in ev2 and i not in ev3:
703             raise Exception('name "%s" not found' % i)