Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_ext.py
1 # P2P vendor specific extension tests
2 # Copyright (c) 2014-2015, Qualcomm Atheros, Inc.
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 os
10
11 from tshark import run_tshark
12 from test_p2p_persistent import form
13
14 def test_p2p_ext_discovery(dev):
15     """P2P device discovery with vendor specific extensions"""
16     addr0 = dev[0].p2p_dev_addr()
17     addr1 = dev[1].p2p_dev_addr()
18
19     try:
20         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 1 dd050011223344"):
21             raise Exception("VENDOR_ELEM_ADD failed")
22         res = dev[0].request("VENDOR_ELEM_GET 1")
23         if res != "dd050011223344":
24             raise Exception("Unexpected VENDOR_ELEM_GET result: " + res)
25         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 1 dd06001122335566"):
26             raise Exception("VENDOR_ELEM_ADD failed")
27         res = dev[0].request("VENDOR_ELEM_GET 1")
28         if res != "dd050011223344dd06001122335566":
29             raise Exception("Unexpected VENDOR_ELEM_GET result(2): " + res)
30         res = dev[0].request("VENDOR_ELEM_GET 2")
31         if res != "":
32             raise Exception("Unexpected VENDOR_ELEM_GET result(3): " + res)
33         if "OK" not in dev[0].request("VENDOR_ELEM_REMOVE 1 dd050011223344"):
34             raise Exception("VENDOR_ELEM_REMOVE failed")
35         res = dev[0].request("VENDOR_ELEM_GET 1")
36         if res != "dd06001122335566":
37             raise Exception("Unexpected VENDOR_ELEM_GET result(4): " + res)
38         if "OK" not in dev[0].request("VENDOR_ELEM_REMOVE 1 dd06001122335566"):
39             raise Exception("VENDOR_ELEM_REMOVE failed")
40         res = dev[0].request("VENDOR_ELEM_GET 1")
41         if res != "":
42             raise Exception("Unexpected VENDOR_ELEM_GET result(5): " + res)
43         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 1 dd050011223344dd06001122335566"):
44             raise Exception("VENDOR_ELEM_ADD failed(2)")
45
46         if "FAIL" not in dev[0].request("VENDOR_ELEM_REMOVE 1 dd051122334455"):
47             raise Exception("Unexpected VENDOR_ELEM_REMOVE success")
48         if "FAIL" not in dev[0].request("VENDOR_ELEM_REMOVE 1 dd"):
49             raise Exception("Unexpected VENDOR_ELEM_REMOVE success(2)")
50         if "FAIL" not in dev[0].request("VENDOR_ELEM_ADD 1 ddff"):
51             raise Exception("Unexpected VENDOR_ELEM_ADD success(3)")
52
53         dev[0].p2p_listen()
54         if not dev[1].discover_peer(addr0):
55             raise Exception("Device discovery timed out")
56         if not dev[0].discover_peer(addr1):
57             raise Exception("Device discovery timed out")
58
59         peer = dev[1].get_peer(addr0)
60         if peer['vendor_elems'] != "dd050011223344dd06001122335566":
61             raise Exception("Vendor elements not reported correctly")
62
63         res = dev[0].request("VENDOR_ELEM_GET 1")
64         if res != "dd050011223344dd06001122335566":
65             raise Exception("Unexpected VENDOR_ELEM_GET result(6): " + res)
66         if "OK" not in dev[0].request("VENDOR_ELEM_REMOVE 1 dd06001122335566"):
67             raise Exception("VENDOR_ELEM_REMOVE failed")
68         res = dev[0].request("VENDOR_ELEM_GET 1")
69         if res != "dd050011223344":
70             raise Exception("Unexpected VENDOR_ELEM_GET result(7): " + res)
71     finally:
72         dev[0].request("VENDOR_ELEM_REMOVE 1 *")
73
74 def test_p2p_ext_discovery_go(dev):
75     """P2P device discovery with vendor specific extensions for GO"""
76     addr0 = dev[0].p2p_dev_addr()
77     addr1 = dev[1].p2p_dev_addr()
78
79     try:
80         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 2 dd050011223344dd06001122335566"):
81             raise Exception("VENDOR_ELEM_ADD failed")
82         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 3 dd050011223344dd06001122335566"):
83             raise Exception("VENDOR_ELEM_ADD failed")
84         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 12 dd050011223344dd06001122335566"):
85             raise Exception("VENDOR_ELEM_ADD failed")
86
87         dev[0].p2p_start_go(freq="2412")
88         if not dev[1].discover_peer(addr0):
89             raise Exception("Device discovery timed out")
90         peer = dev[1].get_peer(addr0)
91         if peer['vendor_elems'] != "dd050011223344dd06001122335566":
92             print peer['vendor_elems']
93             raise Exception("Vendor elements not reported correctly")
94     finally:
95         dev[0].request("VENDOR_ELEM_REMOVE 2 *")
96         dev[0].request("VENDOR_ELEM_REMOVE 3 *")
97         dev[0].request("VENDOR_ELEM_REMOVE 12 *")
98
99 def test_p2p_ext_vendor_elem_probe_req(dev):
100     """VENDOR_ELEM in P2P Probe Request frames"""
101     try:
102         _test_p2p_ext_vendor_elem_probe_req(dev)
103     finally:
104         dev[0].request("VENDOR_ELEM_REMOVE 0 *")
105
106 def _test_p2p_ext_vendor_elem_probe_req(dev):
107     addr1 = dev[1].p2p_dev_addr()
108     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 0 dd050011223300"):
109         raise Exception("VENDOR_ELEM_ADD failed")
110     dev[1].p2p_listen()
111     if not dev[0].discover_peer(addr1):
112         raise Exception("Device discovery timed out")
113     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
114         raise Exception("Failed to enable external management frame handling")
115     ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
116     if ev is None:
117         raise Exception("MGMT-RX timeout")
118     if " 40" not in ev:
119         raise Exception("Not a Probe Request frame")
120     if "dd050011223300" not in ev:
121         raise Exception("Vendor element not found from Probe Request frame")
122     dev[0].p2p_stop_find()
123     dev[1].p2p_stop_find()
124
125 def test_p2p_ext_vendor_elem_pd_req(dev):
126     """VENDOR_ELEM in PD Request frames"""
127     try:
128         _test_p2p_ext_vendor_elem_pd_req(dev)
129     finally:
130         dev[0].request("VENDOR_ELEM_REMOVE 4 *")
131
132 def _test_p2p_ext_vendor_elem_pd_req(dev):
133     addr0 = dev[0].p2p_dev_addr()
134     addr1 = dev[1].p2p_dev_addr()
135     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 4 dd050011223301"):
136         raise Exception("VENDOR_ELEM_ADD failed")
137     dev[1].p2p_listen()
138     if not dev[0].discover_peer(addr1):
139         raise Exception("Device discovery timed out")
140     dev[0].p2p_stop_find()
141     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
142         raise Exception("Failed to enable external management frame handling")
143     dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
144     for i in range(5):
145         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
146         if ev is None:
147             raise Exception("MGMT-RX timeout")
148         if " d0" in ev:
149             break
150     if "dd050011223301" not in ev:
151         raise Exception("Vendor element not found from PD Request frame")
152     dev[1].p2p_stop_find()
153     dev[0].p2p_stop_find()
154
155 def test_p2p_ext_vendor_elem_pd_resp(dev):
156     """VENDOR_ELEM in PD Response frames"""
157     try:
158         _test_p2p_ext_vendor_elem_pd_resp(dev)
159     finally:
160         dev[0].request("VENDOR_ELEM_REMOVE 5 *")
161
162 def _test_p2p_ext_vendor_elem_pd_resp(dev):
163     addr0 = dev[0].p2p_dev_addr()
164     addr1 = dev[1].p2p_dev_addr()
165     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 5 dd050011223302"):
166         raise Exception("VENDOR_ELEM_ADD failed")
167     dev[0].p2p_listen()
168     if not dev[1].discover_peer(addr0):
169         raise Exception("Device discovery timed out")
170     dev[1].p2p_stop_find()
171     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
172         raise Exception("Failed to enable external management frame handling")
173     dev[1].global_request("P2P_PROV_DISC " + addr0 + " display")
174     for i in range(5):
175         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
176         if ev is None:
177             raise Exception("MGMT-RX timeout")
178         if " d0" in ev:
179             break
180     if "dd050011223302" not in ev:
181         raise Exception("Vendor element not found from PD Response frame")
182     dev[0].p2p_stop_find()
183     dev[1].p2p_stop_find()
184
185 def test_p2p_ext_vendor_elem_go_neg_req(dev):
186     """VENDOR_ELEM in GO Negotiation Request frames"""
187     try:
188         _test_p2p_ext_vendor_elem_go_neg_req(dev)
189     finally:
190         dev[0].request("VENDOR_ELEM_REMOVE 6 *")
191
192 def _test_p2p_ext_vendor_elem_go_neg_req(dev):
193     addr0 = dev[0].p2p_dev_addr()
194     addr1 = dev[1].p2p_dev_addr()
195     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 6 dd050011223303"):
196         raise Exception("VENDOR_ELEM_ADD failed")
197     dev[1].p2p_listen()
198     if not dev[0].discover_peer(addr1):
199         raise Exception("Device discovery timed out")
200     dev[0].p2p_stop_find()
201     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
202         raise Exception("Failed to enable external management frame handling")
203     dev[0].global_request("P2P_CONNECT " + addr1 + " 12345670 display")
204     for i in range(5):
205         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
206         if ev is None:
207             raise Exception("MGMT-RX timeout")
208         if " d0" in ev:
209             break
210     if "dd050011223303" not in ev:
211         raise Exception("Vendor element not found from GO Negotiation Request frame")
212     dev[1].p2p_stop_find()
213     dev[0].p2p_stop_find()
214
215 def test_p2p_ext_vendor_elem_go_neg_resp(dev):
216     """VENDOR_ELEM in GO Negotiation Response frames"""
217     try:
218         _test_p2p_ext_vendor_elem_go_neg_resp(dev)
219     finally:
220         dev[0].request("VENDOR_ELEM_REMOVE 7 *")
221
222 def _test_p2p_ext_vendor_elem_go_neg_resp(dev):
223     addr0 = dev[0].p2p_dev_addr()
224     addr1 = dev[1].p2p_dev_addr()
225     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 7 dd050011223304"):
226         raise Exception("VENDOR_ELEM_ADD failed")
227     dev[0].p2p_listen()
228     if not dev[1].discover_peer(addr0):
229         raise Exception("Device discovery timed out")
230     dev[1].p2p_stop_find()
231     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
232         raise Exception("Failed to enable external management frame handling")
233     dev[1].global_request("P2P_CONNECT " + addr0 + " 12345670 display")
234     for i in range(5):
235         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
236         if ev is None:
237             raise Exception("MGMT-RX timeout")
238         if " d0" in ev:
239             break
240     if "dd050011223304" not in ev:
241         raise Exception("Vendor element not found from GO Negotiation Response frame")
242     dev[0].p2p_stop_find()
243     dev[1].p2p_stop_find()
244
245 def test_p2p_ext_vendor_elem_go_neg_conf(dev, apdev, params):
246     """VENDOR_ELEM in GO Negotiation Confirm frames"""
247     try:
248         _test_p2p_ext_vendor_elem_go_neg_conf(dev, apdev, params)
249     finally:
250         dev[0].request("VENDOR_ELEM_REMOVE 8 *")
251
252 def _test_p2p_ext_vendor_elem_go_neg_conf(dev, apdev, params):
253     addr0 = dev[0].p2p_dev_addr()
254     addr1 = dev[1].p2p_dev_addr()
255     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 8 dd050011223305"):
256         raise Exception("VENDOR_ELEM_ADD failed")
257     dev[0].p2p_listen()
258     dev[1].p2p_listen()
259     dev[1].p2p_go_neg_auth(addr0, "12345670", "enter")
260     dev[0].p2p_go_neg_init(addr1, "12345678", "display")
261     dev[1].p2p_go_neg_auth_result(expect_failure=True)
262
263     out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
264                      "wifi_p2p.public_action.subtype == 2")
265     if "Vendor Specific Data: 3305" not in out:
266         raise Exception("Vendor element not found from GO Negotiation Confirm frame")
267
268 def test_p2p_ext_vendor_elem_invitation(dev):
269     """VENDOR_ELEM in Invitation frames"""
270     try:
271         _test_p2p_ext_vendor_elem_invitation(dev)
272     finally:
273         dev[0].request("VENDOR_ELEM_REMOVE 9 *")
274         dev[0].request("VENDOR_ELEM_REMOVE 10 *")
275
276 def _test_p2p_ext_vendor_elem_invitation(dev):
277     addr0 = dev[0].p2p_dev_addr()
278     addr1 = dev[1].p2p_dev_addr()
279     form(dev[0], dev[1])
280     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 9 dd050011223306"):
281         raise Exception("VENDOR_ELEM_ADD failed")
282     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 10 dd050011223307"):
283         raise Exception("VENDOR_ELEM_ADD failed")
284     dev[1].p2p_listen()
285     if not dev[0].discover_peer(addr1):
286         raise Exception("Device discovery timed out")
287     peer = dev[0].get_peer(addr1)
288     dev[0].p2p_stop_find()
289     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
290         raise Exception("Failed to enable external management frame handling")
291     dev[0].global_request("P2P_INVITE persistent=" + peer['persistent'] + " peer=" + addr1)
292     for i in range(5):
293         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
294         if ev is None:
295             raise Exception("MGMT-RX timeout")
296         if " d0" in ev:
297             break
298     if "dd050011223306" not in ev:
299         raise Exception("Vendor element not found from Invitation Request frame")
300     dev[0].p2p_stop_find()
301     dev[1].p2p_stop_find()
302
303     dev[0].p2p_listen()
304     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 0"):
305         raise Exception("Failed to disable external management frame handling")
306     if not dev[1].discover_peer(addr0):
307         raise Exception("Device discovery timed out")
308     peer = dev[1].get_peer(addr0)
309     dev[1].p2p_stop_find()
310     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
311         raise Exception("Failed to enable external management frame handling")
312     dev[1].global_request("P2P_INVITE persistent=" + peer['persistent'] + " peer=" + addr0)
313     for i in range(5):
314         ev = dev[1].wait_event(["MGMT-RX"], timeout=5)
315         if ev is None:
316             raise Exception("MGMT-RX timeout")
317         if " d0" in ev:
318             break
319     if "dd050011223307" not in ev:
320         raise Exception("Vendor element not found from Invitation Response frame")
321     dev[0].p2p_stop_find()
322     dev[1].p2p_stop_find()
323
324 def test_p2p_ext_vendor_elem_assoc(dev, apdev, params):
325     """VENDOR_ELEM in Association frames"""
326     try:
327         _test_p2p_ext_vendor_elem_assoc(dev, apdev, params)
328     finally:
329         dev[0].request("VENDOR_ELEM_REMOVE 11 *")
330         dev[1].request("VENDOR_ELEM_REMOVE 12 *")
331         dev[0].request("VENDOR_ELEM_REMOVE 13 *")
332
333 def _test_p2p_ext_vendor_elem_assoc(dev, apdev, params):
334     addr0 = dev[0].p2p_dev_addr()
335     addr1 = dev[1].p2p_dev_addr()
336     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 11 dd050011223308"):
337         raise Exception("VENDOR_ELEM_ADD failed")
338     if "OK" not in dev[1].request("VENDOR_ELEM_ADD 12 dd050011223309"):
339         raise Exception("VENDOR_ELEM_ADD failed")
340     if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 dd05001122330a"):
341         raise Exception("VENDOR_ELEM_ADD failed")
342     dev[0].p2p_listen()
343     dev[1].p2p_listen()
344     dev[1].p2p_go_neg_auth(addr0, "12345670", "enter", go_intent=15)
345     dev[0].p2p_go_neg_init(addr1, "12345670", "display", go_intent=0,
346                            timeout=15)
347     dev[1].p2p_go_neg_auth_result()
348     dev[1].remove_group()
349     dev[0].wait_go_ending_session()
350
351     out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
352                      "wlan.fc.type_subtype == 0x00", wait=False)
353     if "Vendor Specific Data: 3308" not in out:
354         raise Exception("Vendor element (P2P) not found from Association Request frame")
355     if "Vendor Specific Data: 330a" not in out:
356         raise Exception("Vendor element (non-P2P) not found from Association Request frame")
357
358     out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
359                      "wlan.fc.type_subtype == 0x01", wait=False)
360     if "Vendor Specific Data: 3309" not in out:
361         raise Exception("Vendor element not found from Association Response frame")