Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_wifi_display.py
1 # Wi-Fi Display test cases
2 # Copyright (c) 2013, 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 import threading
11 import Queue
12
13 import hwsim_utils
14 import utils
15 from test_p2p_autogo import connect_cli
16 from test_p2p_persistent import form, invite, invite_from_cli, invite_from_go
17
18 def test_wifi_display(dev):
19     """Wi-Fi Display extensions to P2P"""
20     wfd_devinfo = "00411c440028"
21     dev[0].request("SET wifi_display 1")
22     dev[0].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo)
23     if wfd_devinfo not in dev[0].request("WFD_SUBELEM_GET 0"):
24         raise Exception("Could not fetch back configured subelement")
25
26     # Associated BSSID
27     dev[0].request("WFD_SUBELEM_SET 1 0006020304050607")
28     # Coupled Sink
29     dev[0].request("WFD_SUBELEM_SET 6 000700000000000000")
30     # Session Info
31     dev[0].request("WFD_SUBELEM_SET 9 0000")
32     # WFD Extended Capability
33     dev[0].request("WFD_SUBELEM_SET 7 00020000")
34     # WFD Content Protection
35     prot = "0001" + "00"
36     dev[0].request("WFD_SUBELEM_SET 5 " + prot)
37     # WFD Video Formats
38     video = "0015" + "010203040506070809101112131415161718192021"
39     dev[0].request("WFD_SUBELEM_SET 3 " + video)
40     # WFD 3D Video Formats
41     video_3d = "0011" + "0102030405060708091011121314151617"
42     dev[0].request("WFD_SUBELEM_SET 4 " + video_3d)
43     # WFD Audio Formats
44     audio = "000f" + "010203040506070809101112131415"
45     dev[0].request("WFD_SUBELEM_SET 2 " + audio)
46
47     elems = dev[0].request("WFD_SUBELEM_GET all")
48     if wfd_devinfo not in elems:
49         raise Exception("Could not fetch back configured subelements")
50
51     wfd_devinfo2 = "00001c440028"
52     dev[1].request("SET wifi_display 1")
53     dev[1].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo2)
54     if wfd_devinfo2 not in dev[1].request("WFD_SUBELEM_GET 0"):
55         raise Exception("Could not fetch back configured subelement")
56
57     dev[0].p2p_listen()
58     if "FAIL" in dev[1].global_request("P2P_SERV_DISC_REQ " + dev[0].p2p_dev_addr() + " wifi-display [source][pri-sink] 2,3,4,5"):
59         raise Exception("Setting SD request failed")
60     dev[1].p2p_find(social=True)
61     ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
62     if ev is None:
63         raise Exception("Device discovery request not reported")
64     ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
65     if ev is None:
66         raise Exception("Device discovery timed out")
67     if "wfd_dev_info=0x" + wfd_devinfo not in ev:
68         raise Exception("Wi-Fi Display Info not in P2P-DEVICE-FOUND event")
69     if "new=1" not in ev:
70         raise Exception("new=1 flag missing from P2P-DEVICE-FOUND event")
71     ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=5)
72     if ev is None:
73         raise Exception("Service discovery timed out")
74     if prot not in ev:
75         raise Exception("WFD Content Protection missing from WSD response")
76     if video not in ev:
77         raise Exception("WFD Video Formats missing from WSD response")
78     if video_3d not in ev:
79         raise Exception("WFD 3D Video Formats missing from WSD response")
80     if audio not in ev:
81         raise Exception("WFD Audio Formats missing from WSD response")
82
83     dev[1].dump_monitor()
84     dev[0].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo2)
85     ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
86     if ev is None:
87         raise Exception("Peer info update timed out")
88     if "new=0" not in ev:
89         raise Exception("new=0 flag missing from P2P-DEVICE-FOUND event")
90     if "wfd_dev_info=0x" + wfd_devinfo2 not in ev:
91         raise Exception("Wi-Fi Display Info not in P2P-DEVICE-FOUND event")
92     dev[1].dump_monitor()
93     dev[0].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo)
94     ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
95     if ev is None:
96         raise Exception("Peer info update timed out")
97     if "new=0" not in ev:
98         raise Exception("new=0 flag missing from P2P-DEVICE-FOUND event")
99     if "wfd_dev_info=0x" + wfd_devinfo not in ev:
100         raise Exception("Wi-Fi Display Info not in P2P-DEVICE-FOUND event")
101
102     pin = dev[0].wps_read_pin()
103     dev[0].p2p_go_neg_auth(dev[1].p2p_dev_addr(), pin, 'display')
104     res1 = dev[1].p2p_go_neg_init(dev[0].p2p_dev_addr(), pin, 'enter',
105                                   timeout=20, go_intent=15, freq=2437)
106     res2 = dev[0].p2p_go_neg_auth_result()
107
108     bss = dev[0].get_bss("p2p_dev_addr=" + dev[1].p2p_dev_addr())
109     if bss['bssid'] != dev[1].p2p_interface_addr():
110         raise Exception("Unexpected BSSID in the BSS entry for the GO")
111     if wfd_devinfo2 not in bss['wfd_subelems']:
112         raise Exception("Could not see wfd_subelems in GO's BSS entry")
113     peer = dev[0].get_peer(dev[1].p2p_dev_addr())
114     if wfd_devinfo2 not in peer['wfd_subelems']:
115         raise Exception("Could not see wfd_subelems in GO's peer entry")
116     peer = dev[1].get_peer(dev[0].p2p_dev_addr())
117     if wfd_devinfo not in peer['wfd_subelems']:
118         raise Exception("Could not see wfd_subelems in client's peer entry")
119
120     wfd_devinfo3 = "00001c440028"
121     dev[2].request("SET wifi_display 1")
122     dev[2].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo3)
123     dev[2].p2p_find(social=True)
124     ev = dev[2].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
125     if ev is None:
126         raise Exception("Device discovery timed out")
127     if dev[1].p2p_dev_addr() not in ev:
128         ev = dev[2].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
129         if ev is None:
130             raise Exception("Device discovery timed out")
131         if dev[1].p2p_dev_addr() not in ev:
132             raise Exception("Could not discover GO")
133     if "wfd_dev_info=0x" + wfd_devinfo2 not in ev:
134         raise Exception("Wi-Fi Display Info not in P2P-DEVICE-FOUND event")
135     bss = dev[2].get_bss("p2p_dev_addr=" + dev[1].p2p_dev_addr())
136     if bss['bssid'] != dev[1].p2p_interface_addr():
137         raise Exception("Unexpected BSSID in the BSS entry for the GO")
138     if wfd_devinfo2 not in bss['wfd_subelems']:
139         raise Exception("Could not see wfd_subelems in GO's BSS entry")
140     peer = dev[2].get_peer(dev[1].p2p_dev_addr())
141     if wfd_devinfo2 not in peer['wfd_subelems']:
142         raise Exception("Could not see wfd_subelems in GO's peer entry")
143     dev[2].p2p_stop_find()
144
145     if dev[0].request("WFD_SUBELEM_GET 2") != audio:
146         raise Exception("Unexpected WFD_SUBELEM_GET 2 value")
147     if dev[0].request("WFD_SUBELEM_GET 3") != video:
148         raise Exception("Unexpected WFD_SUBELEM_GET 3 value")
149     if dev[0].request("WFD_SUBELEM_GET 4") != video_3d:
150         raise Exception("Unexpected WFD_SUBELEM_GET 42 value")
151     if dev[0].request("WFD_SUBELEM_GET 5") != prot:
152         raise Exception("Unexpected WFD_SUBELEM_GET 5 value")
153     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET "):
154         raise Exception("Unexpected WFD_SUBELEM_SET success")
155     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6"):
156         raise Exception("Unexpected WFD_SUBELEM_SET success")
157     if "OK" not in dev[0].request("WFD_SUBELEM_SET 6 "):
158         raise Exception("Unexpected WFD_SUBELEM_SET failure")
159     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6 0"):
160         raise Exception("Unexpected WFD_SUBELEM_SET success")
161     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6 0q"):
162         raise Exception("Unexpected WFD_SUBELEM_SET success")
163     if dev[0].request("WFD_SUBELEM_GET 6") != "":
164         raise Exception("Unexpected WFD_SUBELEM_GET 6 response")
165     if dev[0].request("WFD_SUBELEM_GET 8") != "":
166         raise Exception("Unexpected WFD_SUBELEM_GET 8 response")
167
168     if dev[0].global_request("WFD_SUBELEM_GET 2") != audio:
169         raise Exception("Unexpected WFD_SUBELEM_GET 2 value from global interface")
170     if "OK" not in dev[0].global_request("WFD_SUBELEM_SET 1 0006020304050608"):
171         raise Exception("WFD_SUBELEM_SET failed on global interface")
172     if dev[0].request("WFD_SUBELEM_GET 1") != "0006020304050608":
173         raise Exception("Unexpected WFD_SUBELEM_GET 1 value (per-interface)")
174
175     elems = dev[0].request("WFD_SUBELEM_GET all")
176     if "OK" not in dev[0].request("WFD_SUBELEM_SET all " + elems):
177         raise Exception("WFD_SUBELEM_SET all failed")
178     if dev[0].request("WFD_SUBELEM_GET all") != elems:
179         raise Exception("Mismatch in WFS_SUBELEM_SET/GET all")
180     test = "00000600411c440028"
181     if "OK" not in dev[0].request("WFD_SUBELEM_SET all " + test):
182         raise Exception("WFD_SUBELEM_SET all failed")
183     if dev[0].request("WFD_SUBELEM_GET all") != test:
184         raise Exception("Mismatch in WFS_SUBELEM_SET/GET all")
185
186     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET all qwerty"):
187         raise Exception("Invalid WFD_SUBELEM_SET all succeeded")
188     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET all 11"):
189         raise Exception("Invalid WFD_SUBELEM_SET all succeeded")
190     dev[0].request("WFD_SUBELEM_SET all 112233445566")
191     dev[0].request("WFD_SUBELEM_SET all ff0000fe0000fd00")
192
193     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 300 112233"):
194         raise Exception("Invalid WFD_SUBELEM_SET 300 succeeded")
195     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET -1 112233"):
196         raise Exception("Invalid WFD_SUBELEM_SET -1 succeeded")
197     if "FAIL" not in dev[0].request("WFD_SUBELEM_GET 300"):
198         raise Exception("Invalid WFD_SUBELEM_GET 300 succeeded")
199     if "FAIL" not in dev[0].request("WFD_SUBELEM_GET -1"):
200         raise Exception("Invalid WFD_SUBELEM_GET -1 succeeded")
201
202     dev[0].request("SET wifi_display 0")
203     dev[1].request("SET wifi_display 0")
204     dev[2].request("SET wifi_display 0")
205
206 def enable_wifi_display(dev):
207     dev.request("SET wifi_display 1")
208     dev.request("WFD_SUBELEM_SET 0 000600411c440028")
209
210 def test_wifi_display_go_invite(dev):
211     """P2P GO with Wi-Fi Display inviting a client to join"""
212     addr0 = dev[0].p2p_dev_addr()
213     addr1 = dev[1].p2p_dev_addr()
214
215     try:
216         enable_wifi_display(dev[0])
217         enable_wifi_display(dev[1])
218         enable_wifi_display(dev[2])
219
220         dev[1].p2p_listen()
221         if not dev[0].discover_peer(addr1, social=True):
222             raise Exception("Peer " + addr1 + " not found")
223         dev[0].p2p_listen()
224         if not dev[1].discover_peer(addr0, social=True):
225             raise Exception("Peer " + addr0 + " not found")
226         dev[1].p2p_listen()
227
228         logger.info("Authorize invitation")
229         pin = dev[1].wps_read_pin()
230         dev[1].global_request("P2P_CONNECT " + addr0 + " " + pin + " join auth")
231
232         dev[0].p2p_start_go(freq=2412)
233
234         # Add test client to the group
235         connect_cli(dev[0], dev[2], social=True, freq=2412)
236
237         logger.info("Invite peer to join the group")
238         dev[0].p2p_go_authorize_client(pin)
239         dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
240         ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED",
241                                        "P2P-GROUP-STARTED"], timeout=20)
242         if ev is None:
243             raise Exception("Timeout on invitation on peer")
244         if "P2P-INVITATION-RECEIVED" in ev:
245             raise Exception("Unexpected request to accept pre-authorized invitation")
246
247         dev[0].remove_group()
248         dev[1].wait_go_ending_session()
249         dev[2].wait_go_ending_session()
250
251     finally:
252         dev[0].request("SET wifi_display 0")
253         dev[1].request("SET wifi_display 0")
254         dev[2].request("SET wifi_display 0")
255
256 def test_wifi_display_persistent_group(dev):
257     """P2P persistent group formation and re-invocation with Wi-Fi Display enabled"""
258     try:
259         enable_wifi_display(dev[0])
260         enable_wifi_display(dev[1])
261         enable_wifi_display(dev[2])
262
263         form(dev[0], dev[1])
264         peer = dev[1].get_peer(dev[0].p2p_dev_addr())
265         listen_freq = peer['listen_freq']
266         invite_from_cli(dev[0], dev[1])
267         invite_from_go(dev[0], dev[1])
268
269         dev[0].dump_monitor()
270         dev[1].dump_monitor()
271         networks = dev[0].list_networks(p2p=True)
272         if len(networks) != 1:
273             raise Exception("Unexpected number of networks")
274         if "[P2P-PERSISTENT]" not in networks[0]['flags']:
275             raise Exception("Not the persistent group data")
276         if "OK" not in dev[0].global_request("P2P_GROUP_ADD persistent=" + networks[0]['id'] + " freq=" + listen_freq):
277             raise Exception("Could not start GO")
278         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=2)
279         if ev is None:
280             raise Exception("GO start up timed out")
281         dev[0].group_form_result(ev)
282
283         connect_cli(dev[0], dev[2], social=True, freq=listen_freq)
284         dev[0].dump_monitor()
285         dev[1].dump_monitor()
286         invite(dev[1], dev[0])
287         ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
288         if ev is None:
289             raise Exception("Timeout on group re-invocation (on client)")
290         dev[1].group_form_result(ev)
291
292         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=0.1)
293         if ev is not None:
294             raise Exception("Unexpected P2P-GROUP-START on GO")
295         hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
296
297     finally:
298         dev[0].request("SET wifi_display 0")
299         dev[1].request("SET wifi_display 0")
300         dev[2].request("SET wifi_display 0")
301
302 def test_wifi_display_invalid_subelem(dev):
303     """Wi-Fi Display and invalid subelement parsing"""
304     addr1 = dev[1].p2p_dev_addr()
305
306     try:
307         enable_wifi_display(dev[0])
308         enable_wifi_display(dev[1])
309         dev[1].request("WFD_SUBELEM_SET 0 ffff00411c440028")
310
311         dev[1].p2p_listen()
312         dev[0].p2p_find(social=True)
313         ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
314         if ev is None:
315             raise Exception("Device discovery timed out")
316         if "wfd_dev_info=" in ev:
317             raise Exception("Invalid WFD subelement was shown")
318
319     finally:
320         dev[0].request("SET wifi_display 0")
321         dev[1].request("SET wifi_display 0")