Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_concurrency.py
1 # P2P concurrency 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 logging
8 logger = logging.getLogger()
9 import subprocess
10 import time
11
12 import hwsim_utils
13 import hostapd
14 from test_p2p_grpform import go_neg_pin_authorized
15 from test_p2p_grpform import go_neg_pbc
16 from test_p2p_grpform import check_grpform_results
17 from test_p2p_grpform import remove_group
18 from test_p2p_persistent import form
19 from test_p2p_persistent import invite_from_cli
20 from test_p2p_persistent import invite_from_go
21 from test_p2p_persistent import invite
22 from test_ap_ht import clear_scan_cache
23 from utils import HwsimSkip
24
25 def test_concurrent_autogo(dev, apdev):
26     """Concurrent P2P autonomous GO"""
27     logger.info("Connect to an infrastructure AP")
28     dev[0].request("P2P_SET cross_connect 0")
29     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
30     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
31     hwsim_utils.test_connectivity(dev[0], hapd)
32
33     logger.info("Start a P2P group while associated to an AP")
34     dev[0].request("SET p2p_no_group_iface 0")
35     dev[0].p2p_start_go()
36     pin = dev[1].wps_read_pin()
37     dev[0].p2p_go_authorize_client(pin)
38     dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
39                              social=True)
40     hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
41     dev[0].remove_group()
42     dev[1].wait_go_ending_session()
43
44     logger.info("Confirm AP connection after P2P group removal")
45     hwsim_utils.test_connectivity(dev[0], hapd)
46
47 def test_concurrent_autogo_5ghz_ht40(dev, apdev):
48     """Concurrent P2P autonomous GO on 5 GHz and HT40 co-ex"""
49     clear_scan_cache(apdev[1]['ifname'])
50     try:
51         hapd = None
52         hapd2 = None
53         params = { "ssid": "ht40",
54                    "hw_mode": "a",
55                    "channel": "153",
56                    "country_code": "US",
57                    "ht_capab": "[HT40-]" }
58         hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
59
60         params = { "ssid": "test-open-5",
61                    "hw_mode": "a",
62                    "channel": "149",
63                    "country_code": "US" }
64         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
65
66         dev[0].request("P2P_SET cross_connect 0")
67         dev[0].scan_for_bss(apdev[0]['bssid'], freq=5745)
68         dev[0].scan_for_bss(apdev[1]['bssid'], freq=5765)
69         dev[0].connect("test-open-5", key_mgmt="NONE", scan_freq="5745")
70
71         dev[0].request("SET p2p_no_group_iface 0")
72         if "OK" not in dev[0].global_request("P2P_GROUP_ADD ht40"):
73             raise Exception("P2P_GROUP_ADD failed")
74         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
75         if ev is None:
76             raise Exception("GO start up timed out")
77         dev[0].group_form_result(ev)
78
79         pin = dev[1].wps_read_pin()
80         dev[0].p2p_go_authorize_client(pin)
81         dev[1].p2p_find(freq=5745)
82         addr0 = dev[0].p2p_dev_addr()
83         count = 0
84         while count < 10:
85             time.sleep(0.25)
86             count += 1
87             if dev[1].peer_known(addr0):
88                 break
89         dev[1].p2p_connect_group(addr0, pin, timeout=60)
90
91         dev[0].remove_group()
92         dev[1].wait_go_ending_session()
93     finally:
94         dev[0].request("REMOVE_NETWORK all")
95         if hapd:
96             hapd.request("DISABLE")
97         if hapd2:
98             hapd2.request("DISABLE")
99         subprocess.call(['iw', 'reg', 'set', '00'])
100         dev[0].flush_scan_cache()
101         dev[1].flush_scan_cache()
102
103 def test_concurrent_autogo_crossconnect(dev, apdev):
104     """Concurrent P2P autonomous GO"""
105     dev[0].global_request("P2P_SET cross_connect 1")
106     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
107     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
108
109     dev[0].global_request("SET p2p_no_group_iface 0")
110     dev[0].p2p_start_go(no_event_clear=True)
111     ev = dev[0].wait_global_event("P2P-CROSS-CONNECT-ENABLE", timeout=10)
112     if ev is None:
113         raise Exception("Timeout on cross connection enabled event")
114     if dev[0].group_ifname + " " + dev[0].ifname not in ev:
115         raise Exception("Unexpected interfaces: " + ev)
116     dev[0].dump_monitor()
117
118     dev[0].global_request("P2P_SET cross_connect 0")
119     ev = dev[0].wait_global_event("P2P-CROSS-CONNECT-DISABLE", timeout=10)
120     if ev is None:
121         raise Exception("Timeout on cross connection disabled event")
122     if dev[0].group_ifname + " " + dev[0].ifname not in ev:
123         raise Exception("Unexpected interfaces: " + ev)
124     dev[0].remove_group()
125
126     dev[0].global_request("P2P_SET cross_connect 1")
127     dev[0].p2p_start_go(no_event_clear=True)
128     ev = dev[0].wait_global_event("P2P-CROSS-CONNECT-ENABLE", timeout=10)
129     if ev is None:
130         raise Exception("Timeout on cross connection enabled event")
131     if dev[0].group_ifname + " " + dev[0].ifname not in ev:
132         raise Exception("Unexpected interfaces: " + ev)
133     dev[0].dump_monitor()
134     dev[0].remove_group()
135     ev = dev[0].wait_global_event("P2P-CROSS-CONNECT-DISABLE", timeout=10)
136     if ev is None:
137         raise Exception("Timeout on cross connection disabled event")
138     dev[0].global_request("P2P_SET cross_connect 0")
139
140 def test_concurrent_p2pcli(dev, apdev):
141     """Concurrent P2P client join"""
142     logger.info("Connect to an infrastructure AP")
143     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
144     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
145     hwsim_utils.test_connectivity(dev[0], hapd)
146
147     logger.info("Join a P2P group while associated to an AP")
148     dev[0].request("SET p2p_no_group_iface 0")
149     dev[1].p2p_start_go(freq=2412)
150     pin = dev[0].wps_read_pin()
151     dev[1].p2p_go_authorize_client(pin)
152     dev[0].p2p_connect_group(dev[1].p2p_dev_addr(), pin, timeout=60,
153                              social=True)
154     hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
155     dev[1].remove_group()
156     dev[0].wait_go_ending_session()
157
158     logger.info("Confirm AP connection after P2P group removal")
159     hwsim_utils.test_connectivity(dev[0], hapd)
160
161 def test_concurrent_grpform_go(dev, apdev):
162     """Concurrent P2P group formation to become GO"""
163     logger.info("Connect to an infrastructure AP")
164     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
165     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
166     hwsim_utils.test_connectivity(dev[0], hapd)
167
168     logger.info("Form a P2P group while associated to an AP")
169     dev[0].request("SET p2p_no_group_iface 0")
170
171     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
172                                            r_dev=dev[1], r_intent=0)
173     check_grpform_results(i_res, r_res)
174     remove_group(dev[0], dev[1])
175
176     logger.info("Confirm AP connection after P2P group removal")
177     hwsim_utils.test_connectivity(dev[0], hapd)
178
179 def test_concurrent_grpform_cli(dev, apdev):
180     """Concurrent P2P group formation to become P2P Client"""
181     logger.info("Connect to an infrastructure AP")
182     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
183     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
184     hwsim_utils.test_connectivity(dev[0], hapd)
185
186     logger.info("Form a P2P group while associated to an AP")
187     dev[0].request("SET p2p_no_group_iface 0")
188
189     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
190                                            r_dev=dev[1], r_intent=15)
191     check_grpform_results(i_res, r_res)
192     remove_group(dev[0], dev[1])
193
194     logger.info("Confirm AP connection after P2P group removal")
195     hwsim_utils.test_connectivity(dev[0], hapd)
196
197 def test_concurrent_grpform_while_connecting(dev, apdev):
198     """Concurrent P2P group formation while connecting to an AP"""
199     logger.info("Start connection to an infrastructure AP")
200     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
201     dev[0].connect("test-open", key_mgmt="NONE", wait_connect=False)
202
203     logger.info("Form a P2P group while connecting to an AP")
204     dev[0].request("SET p2p_no_group_iface 0")
205
206     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_freq=2412,
207                                            r_dev=dev[1], r_freq=2412)
208     check_grpform_results(i_res, r_res)
209     remove_group(dev[0], dev[1])
210
211     logger.info("Confirm AP connection after P2P group removal")
212     hwsim_utils.test_connectivity(dev[0], hapd)
213
214 def test_concurrent_grpform_while_connecting2(dev, apdev):
215     """Concurrent P2P group formation while connecting to an AP (2)"""
216     logger.info("Start connection to an infrastructure AP")
217     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
218     dev[0].connect("test-open", key_mgmt="NONE", wait_connect=False)
219     dev[1].flush_scan_cache()
220
221     logger.info("Form a P2P group while connecting to an AP")
222     dev[0].request("SET p2p_no_group_iface 0")
223
224     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, i_freq=2412,
225                                 r_dev=dev[1], r_intent=0, r_freq=2412)
226     check_grpform_results(i_res, r_res)
227     remove_group(dev[0], dev[1])
228
229     logger.info("Confirm AP connection after P2P group removal")
230     dev[0].wait_completed()
231     hwsim_utils.test_connectivity(dev[0], hapd)
232
233 def test_concurrent_grpform_while_connecting3(dev, apdev):
234     """Concurrent P2P group formation while connecting to an AP (3)"""
235     logger.info("Start connection to an infrastructure AP")
236     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
237     dev[0].connect("test-open", key_mgmt="NONE", wait_connect=False)
238
239     logger.info("Form a P2P group while connecting to an AP")
240     dev[0].request("SET p2p_no_group_iface 0")
241
242     [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=15, i_freq=2412,
243                                 r_dev=dev[0], r_intent=0, r_freq=2412)
244     check_grpform_results(i_res, r_res)
245     remove_group(dev[0], dev[1])
246
247     logger.info("Confirm AP connection after P2P group removal")
248     dev[0].wait_completed()
249     hwsim_utils.test_connectivity(dev[0], hapd)
250
251 def test_concurrent_persistent_group(dev, apdev):
252     """Concurrent P2P persistent group"""
253     logger.info("Connect to an infrastructure AP")
254     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open", "channel": "2" })
255     dev[0].global_request("SET p2p_no_group_iface 0")
256     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2417")
257
258     logger.info("Run persistent group test while associated to an AP")
259     form(dev[0], dev[1])
260     [go_res, cli_res] = invite_from_cli(dev[0], dev[1])
261     if go_res['freq'] != '2417':
262         raise Exception("Unexpected channel selected: " + go_res['freq'])
263     [go_res, cli_res] = invite_from_go(dev[0], dev[1])
264     if go_res['freq'] != '2417':
265         raise Exception("Unexpected channel selected: " + go_res['freq'])
266
267 def test_concurrent_invitation_channel_mismatch(dev, apdev):
268     """P2P persistent group invitation and channel mismatch"""
269     if dev[0].get_mcc() > 1:
270         raise HwsimSkip("Skip due to MCC being enabled")
271
272     form(dev[0], dev[1])
273     dev[0].dump_monitor()
274     dev[1].dump_monitor()
275
276     logger.info("Connect to an infrastructure AP")
277     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open", "channel": "2" })
278     dev[0].global_request("SET p2p_no_group_iface 0")
279     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2417")
280     invite(dev[1], dev[0], extra="freq=2412")
281     ev = dev[1].wait_global_event(["P2P-INVITATION-RESULT"], timeout=15)
282     if ev is None:
283         raise Exception("P2P invitation result not received")
284     if "status=7" not in ev:
285         raise Exception("Unexpected P2P invitation result: " + ev)