Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_device.py
1 # cfg80211 P2P Device
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 time
10
11 from wpasupplicant import WpaSupplicant
12 from test_p2p_grpform import go_neg_pin_authorized
13 from test_p2p_grpform import check_grpform_results
14 from test_p2p_grpform import remove_group
15 from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
16 from hwsim import HWSimRadio
17 import hostapd
18 import hwsim_utils
19
20 def test_p2p_device_grpform(dev, apdev):
21     """P2P group formation with driver using cfg80211 P2P Device"""
22     with HWSimRadio(use_p2p_device=True) as (radio, iface):
23         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
24         wpas.interface_add(iface)
25         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
26                                                r_dev=wpas, r_intent=0)
27         check_grpform_results(i_res, r_res)
28         remove_group(dev[0], wpas)
29
30         res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
31         lines = res.splitlines()
32         found = False
33         for l in lines:
34             try:
35                 [name,value] = l.split('=', 1)
36                 if name == "wdev_id":
37                     found = True
38                     break
39             except ValueError:
40                 pass
41         if not found:
42             raise Exception("wdev_id not found")
43
44 def test_p2p_device_grpform2(dev, apdev):
45     """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
46     with HWSimRadio(use_p2p_device=True) as (radio, iface):
47         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
48         wpas.interface_add(iface)
49         [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
50                                                r_dev=dev[0], r_intent=0)
51         check_grpform_results(i_res, r_res)
52         remove_group(wpas, dev[0])
53
54 def test_p2p_device_group_remove(dev, apdev):
55     """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
56     with HWSimRadio(use_p2p_device=True) as (radio, iface):
57         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
58         wpas.interface_add(iface)
59         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
60                                                r_dev=wpas, r_intent=0)
61         check_grpform_results(i_res, r_res)
62         # Issue the remove request on the interface which will be removed
63         p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
64         res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
65         if "OK" not in res:
66             raise Exception("Failed to remove P2P group")
67         ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
68         if ev is None:
69             raise Exception("Group removal event not received")
70         if not wpas.global_ping():
71             raise Exception("Could not ping global ctrl_iface after group removal")
72
73 def test_p2p_device_concurrent_scan(dev, apdev):
74     """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
75     with HWSimRadio(use_p2p_device=True) as (radio, iface):
76         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
77         wpas.interface_add(iface)
78         wpas.p2p_find()
79         time.sleep(0.1)
80         wpas.request("SCAN")
81         ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
82         if ev is None:
83             raise Exception("Station mode scan did not start")
84
85 def test_p2p_device_nfc_invite(dev, apdev):
86     """P2P NFC invitiation with driver using cfg80211 P2P Device"""
87     with HWSimRadio(use_p2p_device=True) as (radio, iface):
88         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
89         wpas.interface_add(iface)
90
91         set_ip_addr_info(dev[0])
92         logger.info("Start autonomous GO")
93         dev[0].p2p_start_go()
94
95         logger.info("Write NFC Tag on the P2P Client")
96         res = wpas.global_request("P2P_LISTEN")
97         if "FAIL" in res:
98             raise Exception("Failed to start Listen mode")
99         pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
100         if "FAIL" in pw:
101             raise Exception("Failed to generate password token")
102         res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
103         if "FAIL" in res:
104             raise Exception("Failed to enable NFC Tag for P2P static handover")
105         sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
106         if "FAIL" in sel:
107             raise Exception("Failed to generate NFC connection handover select")
108
109         logger.info("Read NFC Tag on the GO to trigger invitation")
110         res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
111         if "FAIL" in res:
112             raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
113
114         ev = wpas.wait_global_event(grpform_events, timeout=20)
115         if ev is None:
116             raise Exception("Joining the group timed out")
117         res = wpas.group_form_result(ev)
118         hwsim_utils.test_connectivity_p2p(dev[0], wpas)
119         check_ip_addr(res)
120
121 def test_p2p_device_misuses(dev, apdev):
122     """cfg80211 P2P Device misuses"""
123     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
124     with HWSimRadio(use_p2p_device=True) as (radio, iface):
125         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
126         wpas.interface_add(iface)
127
128         # Add a normal network profile to the P2P Device management only
129         # interface to verify that it does not get used.
130         id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
131         wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
132         wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
133         wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
134
135         # Scan requests get ignored on p2p-dev
136         wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
137
138         dev[0].p2p_start_go(freq=2412)
139         addr = dev[0].p2p_interface_addr()
140         wpas.scan_for_bss(addr, freq=2412)
141         wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
142         hwsim_utils.test_connectivity(wpas, hapd)
143
144         pin = wpas.wps_read_pin()
145         dev[0].p2p_go_authorize_client(pin)
146         res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
147                                      social=True, freq=2412)
148         hwsim_utils.test_connectivity_p2p(dev[0], wpas)
149
150         # Optimize scan-after-disconnect
151         wpas.group_request("SET_NETWORK 0 scan_freq 2412")
152
153         dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
154         ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
155         if ev is None:
156             raise Exception("Did not see disconnect event on P2P group interface")
157         dev[0].remove_group()
158
159         ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
160         if ev is None:
161             raise Exception("Scan not started")
162         ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
163         if ev is None:
164             raise Exception("Scan not completed")
165         time.sleep(1)
166         hwsim_utils.test_connectivity(wpas, hapd)
167
168         ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
169         if ev is not None:
170             raise Exception("Unexpected disconnection event received from hostapd")
171         ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
172         if ev is not None:
173             raise Exception("Unexpected disconnection event received from wpa_supplicant")
174
175         wpas.request("DISCONNECT")
176         wpas.wait_disconnected()
177
178 def test_p2p_device_incorrect_command_interface(dev, apdev):
179     """cfg80211 P2P Device and P2P_* command on incorrect interface"""
180     with HWSimRadio(use_p2p_device=True) as (radio, iface):
181         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
182         wpas.interface_add(iface)
183
184         dev[0].p2p_listen()
185         wpas.request('P2P_FIND type=social')
186         ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
187         if ev is None:
188             raise Exception("Peer not found")
189         ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
190         if ev is not None:
191             raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
192
193         pin = wpas.wps_read_pin()
194         dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
195                                freq=2412)
196         wpas.request('P2P_STOP_FIND')
197         if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
198             raise Exception("P2P_CONNECT failed")
199
200         ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
201         if ev is None:
202             raise Exception("Group formation timed out")
203         wpas.group_form_result(ev)
204
205         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
206         if ev is None:
207             raise Exception("Group formation timed out(2)")
208         dev[0].group_form_result(ev)
209
210         dev[0].remove_group()
211         wpas.wait_go_ending_session()
212
213 def test_p2p_device_incorrect_command_interface2(dev, apdev):
214     """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
215     with HWSimRadio(use_p2p_device=True) as (radio, iface):
216         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
217         wpas.interface_add(iface)
218
219         print wpas.request('P2P_GROUP_ADD')
220         ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
221         if ev is None:
222             raise Exception("Group formation timed out")
223         res = wpas.group_form_result(ev)
224         logger.info("Group results: " + str(res))
225         wpas.remove_group()
226         if not res['ifname'].startswith('p2p-' + iface + '-'):
227             raise Exception("Unexpected group ifname: " + res['ifname'])