b66bf42260290afe41e3106b381c0b9cdd728b5a
[mech_eap.git] / 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].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', timeout=20, go_intent=15)
105     res2 = dev[0].p2p_go_neg_auth_result()
106
107     bss = dev[0].get_bss("p2p_dev_addr=" + dev[1].p2p_dev_addr())
108     if bss['bssid'] != dev[1].p2p_interface_addr():
109         raise Exception("Unexpected BSSID in the BSS entry for the GO")
110     if wfd_devinfo2 not in bss['wfd_subelems']:
111         raise Exception("Could not see wfd_subelems in GO's BSS entry")
112     peer = dev[0].get_peer(dev[1].p2p_dev_addr())
113     if wfd_devinfo2 not in peer['wfd_subelems']:
114         raise Exception("Could not see wfd_subelems in GO's peer entry")
115     peer = dev[1].get_peer(dev[0].p2p_dev_addr())
116     if wfd_devinfo not in peer['wfd_subelems']:
117         raise Exception("Could not see wfd_subelems in client's peer entry")
118
119     wfd_devinfo3 = "00001c440028"
120     dev[2].request("SET wifi_display 1")
121     dev[2].request("WFD_SUBELEM_SET 0 0006" + wfd_devinfo3)
122     dev[2].p2p_find(social=True)
123     ev = dev[2].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
124     if ev is None:
125         raise Exception("Device discovery timed out")
126     if dev[1].p2p_dev_addr() not in ev:
127         ev = dev[2].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
128         if ev is None:
129             raise Exception("Device discovery timed out")
130         if dev[1].p2p_dev_addr() not in ev:
131             raise Exception("Could not discover GO")
132     if "wfd_dev_info=0x" + wfd_devinfo2 not in ev:
133         raise Exception("Wi-Fi Display Info not in P2P-DEVICE-FOUND event")
134     bss = dev[2].get_bss("p2p_dev_addr=" + dev[1].p2p_dev_addr())
135     if bss['bssid'] != dev[1].p2p_interface_addr():
136         raise Exception("Unexpected BSSID in the BSS entry for the GO")
137     if wfd_devinfo2 not in bss['wfd_subelems']:
138         raise Exception("Could not see wfd_subelems in GO's BSS entry")
139     peer = dev[2].get_peer(dev[1].p2p_dev_addr())
140     if wfd_devinfo2 not in peer['wfd_subelems']:
141         raise Exception("Could not see wfd_subelems in GO's peer entry")
142     dev[2].p2p_stop_find()
143
144     if dev[0].request("WFD_SUBELEM_GET 2") != audio:
145         raise Exception("Unexpected WFD_SUBELEM_GET 2 value")
146     if dev[0].request("WFD_SUBELEM_GET 3") != video:
147         raise Exception("Unexpected WFD_SUBELEM_GET 3 value")
148     if dev[0].request("WFD_SUBELEM_GET 4") != video_3d:
149         raise Exception("Unexpected WFD_SUBELEM_GET 42 value")
150     if dev[0].request("WFD_SUBELEM_GET 5") != prot:
151         raise Exception("Unexpected WFD_SUBELEM_GET 5 value")
152     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET "):
153         raise Exception("Unexpected WFD_SUBELEM_SET success")
154     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6"):
155         raise Exception("Unexpected WFD_SUBELEM_SET success")
156     if "OK" not in dev[0].request("WFD_SUBELEM_SET 6 "):
157         raise Exception("Unexpected WFD_SUBELEM_SET failure")
158     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6 0"):
159         raise Exception("Unexpected WFD_SUBELEM_SET success")
160     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 6 0q"):
161         raise Exception("Unexpected WFD_SUBELEM_SET success")
162     if dev[0].request("WFD_SUBELEM_GET 6") != "":
163         raise Exception("Unexpected WFD_SUBELEM_GET 6 response")
164     if dev[0].request("WFD_SUBELEM_GET 8") != "":
165         raise Exception("Unexpected WFD_SUBELEM_GET 8 response")
166
167     if dev[0].global_request("WFD_SUBELEM_GET 2") != audio:
168         raise Exception("Unexpected WFD_SUBELEM_GET 2 value from global interface")
169     if "OK" not in dev[0].global_request("WFD_SUBELEM_SET 1 0006020304050608"):
170         raise Exception("WFD_SUBELEM_SET failed on global interface")
171     if dev[0].request("WFD_SUBELEM_GET 1") != "0006020304050608":
172         raise Exception("Unexpected WFD_SUBELEM_GET 1 value (per-interface)")
173
174     elems = dev[0].request("WFD_SUBELEM_GET all")
175     if "OK" not in dev[0].request("WFD_SUBELEM_SET all " + elems):
176         raise Exception("WFD_SUBELEM_SET all failed")
177     if dev[0].request("WFD_SUBELEM_GET all") != elems:
178         raise Exception("Mismatch in WFS_SUBELEM_SET/GET all")
179     test = "00000600411c440028"
180     if "OK" not in dev[0].request("WFD_SUBELEM_SET all " + test):
181         raise Exception("WFD_SUBELEM_SET all failed")
182     if dev[0].request("WFD_SUBELEM_GET all") != test:
183         raise Exception("Mismatch in WFS_SUBELEM_SET/GET all")
184
185     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET all qwerty"):
186         raise Exception("Invalid WFD_SUBELEM_SET all succeeded")
187     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET all 11"):
188         raise Exception("Invalid WFD_SUBELEM_SET all succeeded")
189     dev[0].request("WFD_SUBELEM_SET all 112233445566")
190     dev[0].request("WFD_SUBELEM_SET all ff0000fe0000fd00")
191
192     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET 300 112233"):
193         raise Exception("Invalid WFD_SUBELEM_SET 300 succeeded")
194     if "FAIL" not in dev[0].request("WFD_SUBELEM_SET -1 112233"):
195         raise Exception("Invalid WFD_SUBELEM_SET -1 succeeded")
196     if "FAIL" not in dev[0].request("WFD_SUBELEM_GET 300"):
197         raise Exception("Invalid WFD_SUBELEM_GET 300 succeeded")
198     if "FAIL" not in dev[0].request("WFD_SUBELEM_GET -1"):
199         raise Exception("Invalid WFD_SUBELEM_GET -1 succeeded")
200
201     dev[0].request("SET wifi_display 0")
202     dev[1].request("SET wifi_display 0")
203     dev[2].request("SET wifi_display 0")
204
205 def enable_wifi_display(dev):
206     dev.request("SET wifi_display 1")
207     dev.request("WFD_SUBELEM_SET 0 000600411c440028")
208
209 def test_wifi_display_go_invite(dev):
210     """P2P GO with Wi-Fi Display inviting a client to join"""
211     addr0 = dev[0].p2p_dev_addr()
212     addr1 = dev[1].p2p_dev_addr()
213
214     try:
215         enable_wifi_display(dev[0])
216         enable_wifi_display(dev[1])
217         enable_wifi_display(dev[2])
218
219         dev[1].p2p_listen()
220         if not dev[0].discover_peer(addr1, social=True):
221             raise Exception("Peer " + addr1 + " not found")
222         dev[0].p2p_listen()
223         if not dev[1].discover_peer(addr0, social=True):
224             raise Exception("Peer " + addr0 + " not found")
225         dev[1].p2p_listen()
226
227         logger.info("Authorize invitation")
228         pin = dev[1].wps_read_pin()
229         dev[1].global_request("P2P_CONNECT " + addr0 + " " + pin + " join auth")
230
231         dev[0].p2p_start_go(freq=2412)
232
233         # Add test client to the group
234         connect_cli(dev[0], dev[2], social=True, freq=2412)
235
236         logger.info("Invite peer to join the group")
237         dev[0].p2p_go_authorize_client(pin)
238         dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
239         ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED",
240                                        "P2P-GROUP-STARTED"], timeout=20)
241         if ev is None:
242             raise Exception("Timeout on invitation on peer")
243         if "P2P-INVITATION-RECEIVED" in ev:
244             raise Exception("Unexpected request to accept pre-authorized invitation")
245
246         dev[0].remove_group()
247         dev[1].wait_go_ending_session()
248         dev[2].wait_go_ending_session()
249
250     finally:
251         dev[0].request("SET wifi_display 0")
252         dev[1].request("SET wifi_display 0")
253         dev[2].request("SET wifi_display 0")
254
255 def test_wifi_display_persistent_group(dev):
256     """P2P persistent group formation and re-invocation with Wi-Fi Display enabled"""
257     try:
258         enable_wifi_display(dev[0])
259         enable_wifi_display(dev[1])
260         enable_wifi_display(dev[2])
261
262         form(dev[0], dev[1])
263         peer = dev[1].get_peer(dev[0].p2p_dev_addr())
264         listen_freq = peer['listen_freq']
265         invite_from_cli(dev[0], dev[1])
266         invite_from_go(dev[0], dev[1])
267
268         dev[0].dump_monitor()
269         dev[1].dump_monitor()
270         networks = dev[0].list_networks(p2p=True)
271         if len(networks) != 1:
272             raise Exception("Unexpected number of networks")
273         if "[P2P-PERSISTENT]" not in networks[0]['flags']:
274             raise Exception("Not the persistent group data")
275         if "OK" not in dev[0].global_request("P2P_GROUP_ADD persistent=" + networks[0]['id'] + " freq=" + listen_freq):
276             raise Exception("Could not start GO")
277         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=2)
278         if ev is None:
279             raise Exception("GO start up timed out")
280         dev[0].group_form_result(ev)
281
282         connect_cli(dev[0], dev[2], social=True, freq=listen_freq)
283         dev[0].dump_monitor()
284         dev[1].dump_monitor()
285         invite(dev[1], dev[0])
286         ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
287         if ev is None:
288             raise Exception("Timeout on group re-invocation (on client)")
289         dev[1].group_form_result(ev)
290
291         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=0.1)
292         if ev is not None:
293             raise Exception("Unexpected P2P-GROUP-START on GO")
294         hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
295
296     finally:
297         dev[0].request("SET wifi_display 0")
298         dev[1].request("SET wifi_display 0")
299         dev[2].request("SET wifi_display 0")
300
301 def test_wifi_display_invalid_subelem(dev):
302     """Wi-Fi Display and invalid subelement parsing"""
303     addr1 = dev[1].p2p_dev_addr()
304
305     try:
306         enable_wifi_display(dev[0])
307         enable_wifi_display(dev[1])
308         dev[1].request("WFD_SUBELEM_SET 0 ffff00411c440028")
309
310         dev[1].p2p_listen()
311         dev[0].p2p_find(social=True)
312         ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
313         if ev is None:
314             raise Exception("Device discovery timed out")
315         if "wfd_dev_info=" in ev:
316             raise Exception("Invalid WFD subelement was shown")
317
318     finally:
319         dev[0].request("SET wifi_display 0")
320         dev[1].request("SET wifi_display 0")