]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_p2p_persistent.py
1 # P2P persistent group test cases
2 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger
= logging
.getLogger()
13 from test_p2p_autogo
import connect_cli
15 def go_neg_pin_authorized_persistent(i_dev
, r_dev
, i_intent
=None, r_intent
=None, i_method
='enter', r_method
='display', test_data
=True):
18 pin
= r_dev
.wps_read_pin()
19 logger
.info("Start GO negotiation " + i_dev
.ifname
+ " -> " + r_dev
.ifname
)
20 r_dev
.p2p_go_neg_auth(i_dev
.p2p_dev_addr(), pin
, r_method
,
21 go_intent
=r_intent
, persistent
=True)
22 i_res
= i_dev
.p2p_go_neg_init(r_dev
.p2p_dev_addr(), pin
, i_method
,
23 timeout
=20, go_intent
=i_intent
,
25 r_res
= r_dev
.p2p_go_neg_auth_result()
26 logger
.debug("i_res: " + str(i_res
))
27 logger
.debug("r_res: " + str(r_res
))
30 logger
.info("Group formed")
32 hwsim_utils
.test_connectivity_p2p(r_dev
, i_dev
)
35 def terminate_group(go
, cli
):
36 logger
.info("Terminate persistent group")
38 cli
.wait_go_ending_session()
40 def invite(inv
, resp
, extra
=None, persistent_reconnect
=True):
41 addr
= resp
.p2p_dev_addr()
42 if persistent_reconnect
:
43 resp
.request("SET persistent_reconnect 1")
45 resp
.request("SET persistent_reconnect 0")
47 if not inv
.discover_peer(addr
, social
=True):
48 raise Exception("Peer " + addr
+ " not found")
50 peer
= inv
.get_peer(addr
)
51 cmd
= "P2P_INVITE persistent=" + peer
['persistent'] + " peer=" + addr
53 cmd
= cmd
+ " " + extra
;
54 inv
.global_request(cmd
)
56 def check_result(go
, cli
):
57 ev
= go
.wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
59 raise Exception("Timeout on group re-invocation (on GO)")
60 if "[PERSISTENT]" not in ev
:
61 raise Exception("Re-invoked group not marked persistent")
62 go_res
= go
.group_form_result(ev
)
63 if go_res
['role'] != 'GO':
64 raise Exception("Persistent group GO did not become GO")
65 if not go_res
['persistent']:
66 raise Exception("Persistent group not re-invoked as persistent (GO)")
67 ev
= cli
.wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
69 raise Exception("Timeout on group re-invocation (on client)")
70 if "[PERSISTENT]" not in ev
:
71 raise Exception("Re-invoked group not marked persistent")
72 cli_res
= cli
.group_form_result(ev
)
73 if cli_res
['role'] != 'client':
74 raise Exception("Persistent group client did not become client")
75 if not cli_res
['persistent']:
76 raise Exception("Persistent group not re-invoked as persistent (cli)")
77 return [go_res
, cli_res
]
79 def form(go
, cli
, test_data
=True, reverse_init
=False):
80 logger
.info("Form a persistent group")
82 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=cli
, i_intent
=0,
83 r_dev
=go
, r_intent
=15,
86 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=go
, i_intent
=15,
87 r_dev
=cli
, r_intent
=0,
89 if not i_res
['persistent'] or not r_res
['persistent']:
90 raise Exception("Formed group was not persistent")
91 terminate_group(go
, cli
)
97 def invite_from_cli(go
, cli
):
98 logger
.info("Re-invoke persistent group from client")
100 [go_res
, cli_res
] = check_result(go
, cli
)
101 hwsim_utils
.test_connectivity_p2p(go
, cli
)
102 terminate_group(go
, cli
)
103 return [go_res
, cli_res
]
105 def invite_from_go(go
, cli
):
106 logger
.info("Re-invoke persistent group from GO")
108 [go_res
, cli_res
] = check_result(go
, cli
)
109 hwsim_utils
.test_connectivity_p2p(go
, cli
)
110 terminate_group(go
, cli
)
111 return [go_res
, cli_res
]
113 def test_persistent_group(dev
):
114 """P2P persistent group formation and re-invocation"""
116 invite_from_cli(dev
[0], dev
[1])
117 invite_from_go(dev
[0], dev
[1])
119 logger
.info("Remove group on the client and try to invite from GO")
121 for n
in dev
[0].list_networks():
122 if "[P2P-PERSISTENT]" in n
['flags']:
126 raise Exception("Could not find persistent group entry")
127 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
128 if dev
[1].p2p_dev_addr() not in clients
:
129 raise Exception("Peer missing from client list")
130 if "FAIL" not in dev
[1].request("SELECT_NETWORK " + str(id)):
131 raise Exception("SELECT_NETWORK succeeded unexpectedly")
132 if "FAIL" not in dev
[1].request("SELECT_NETWORK 1234567"):
133 raise Exception("SELECT_NETWORK succeeded unexpectedly(2)")
134 if "FAIL" not in dev
[1].request("ENABLE_NETWORK " + str(id)):
135 raise Exception("ENABLE_NETWORK succeeded unexpectedly")
136 if "FAIL" not in dev
[1].request("ENABLE_NETWORK 1234567"):
137 raise Exception("ENABLE_NETWORK succeeded unexpectedly(2)")
138 if "FAIL" not in dev
[1].request("DISABLE_NETWORK " + str(id)):
139 raise Exception("DISABLE_NETWORK succeeded unexpectedly")
140 if "FAIL" not in dev
[1].request("DISABLE_NETWORK 1234567"):
141 raise Exception("DISABLE_NETWORK succeeded unexpectedly(2)")
142 if "FAIL" not in dev
[1].request("REMOVE_NETWORK 1234567"):
143 raise Exception("REMOVE_NETWORK succeeded unexpectedly")
144 dev
[1].request("REMOVE_NETWORK all")
145 if len(dev
[1].list_networks()) > 0:
146 raise Exception("Unexpected network block remaining")
147 invite(dev
[0], dev
[1])
148 ev
= dev
[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=10)
150 raise Exception("No invitation result seen")
151 if "status=8" not in ev
:
152 raise Exception("Unexpected invitation result: " + ev
)
153 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
154 if dev
[1].p2p_dev_addr() in clients
:
155 raise Exception("Peer was still in client list")
157 def test_persistent_group2(dev
):
158 """P2P persistent group formation with reverse roles"""
159 form(dev
[0], dev
[1], reverse_init
=True)
160 invite_from_cli(dev
[0], dev
[1])
161 invite_from_go(dev
[0], dev
[1])
163 def test_persistent_group3(dev
):
164 """P2P persistent group formation and re-invocation with empty BSS table"""
166 dev
[1].request("BSS_FLUSH 0")
167 invite_from_cli(dev
[0], dev
[1])
168 dev
[1].request("BSS_FLUSH 0")
169 invite_from_go(dev
[0], dev
[1])
171 def test_persistent_group_per_sta_psk(dev
):
172 """P2P persistent group formation and re-invocation using per-client PSK"""
173 addr0
= dev
[0].p2p_dev_addr()
174 addr1
= dev
[1].p2p_dev_addr()
175 addr2
= dev
[2].p2p_dev_addr()
176 dev
[0].request("P2P_SET per_sta_psk 1")
177 logger
.info("Form a persistent group")
178 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=dev
[0], i_intent
=15,
179 r_dev
=dev
[1], r_intent
=0)
180 if not i_res
['persistent'] or not r_res
['persistent']:
181 raise Exception("Formed group was not persistent")
183 logger
.info("Join another client to the group")
184 pin
= dev
[2].wps_read_pin()
185 dev
[0].p2p_go_authorize_client(pin
)
186 c_res
= dev
[2].p2p_connect_group(addr0
, pin
, timeout
=60)
187 if not c_res
['persistent']:
188 raise Exception("Joining client did not recognize persistent group")
189 if r_res
['psk'] == c_res
['psk']:
190 raise Exception("Same PSK assigned for both clients")
191 hwsim_utils
.test_connectivity_p2p_sta(dev
[1], dev
[2])
193 logger
.info("Remove persistent group and re-start it manually")
194 dev
[0].remove_group()
195 dev
[1].wait_go_ending_session()
196 dev
[2].wait_go_ending_session()
197 dev
[0].dump_monitor()
199 for i
in range(0, 3):
200 networks
= dev
[i
].list_networks()
201 if len(networks
) != 1:
202 raise Exception("Unexpected number of networks")
203 if "[P2P-PERSISTENT]" not in networks
[0]['flags']:
204 raise Exception("Not the persistent group data")
205 if "OK" not in dev
[i
].global_request("P2P_GROUP_ADD persistent=" + networks
[0]['id'] + " freq=2412"):
206 raise Exception("Could not re-start persistent group")
207 ev
= dev
[i
].wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
209 raise Exception("Timeout on group restart")
211 logger
.info("Leave persistent group and rejoin it")
212 dev
[2].remove_group()
213 ev
= dev
[2].wait_event(["P2P-GROUP-REMOVED"], timeout
=3)
215 raise Exception("Group removal event timed out")
216 if not dev
[2].discover_peer(addr0
, social
=True):
217 raise Exception("Peer " + peer
+ " not found")
218 dev
[2].dump_monitor()
219 peer
= dev
[2].get_peer(addr0
)
220 dev
[2].global_request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
221 ev
= dev
[2].wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
223 raise Exception("Timeout on group restart (on client)")
224 cli_res
= dev
[2].group_form_result(ev
)
225 if not cli_res
['persistent']:
226 raise Exception("Persistent group not restarted as persistent (cli)")
227 hwsim_utils
.test_connectivity_p2p(dev
[1], dev
[2])
229 logger
.info("Remove one of the clients from the group")
230 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr2
)
231 dev
[2].wait_go_ending_session()
233 logger
.info("Try to reconnect after having been removed from group")
234 if not dev
[2].discover_peer(addr0
, social
=True):
235 raise Exception("Peer " + peer
+ " not found")
236 dev
[2].dump_monitor()
237 peer
= dev
[2].get_peer(addr0
)
238 dev
[2].global_request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
239 ev
= dev
[2].wait_global_event(["P2P-GROUP-STARTED","WPA: 4-Way Handshake failed"], timeout
=30)
241 raise Exception("Timeout on group restart (on client)")
242 if "P2P-GROUP-STARTED" in ev
:
243 raise Exception("Client managed to connect after being removed")
245 logger
.info("Remove the remaining client from the group")
246 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr1
)
247 dev
[1].wait_go_ending_session()
249 logger
.info("Terminate persistent group")
250 dev
[0].remove_group()
251 dev
[0].dump_monitor()
253 logger
.info("Try to re-invoke persistent group from client")
254 dev
[0].request("SET persistent_reconnect 1")
256 if not dev
[1].discover_peer(addr0
, social
=True):
257 raise Exception("Peer " + peer
+ " not found")
258 dev
[1].dump_monitor()
259 peer
= dev
[1].get_peer(addr0
)
260 dev
[1].global_request("P2P_INVITE persistent=" + peer
['persistent'] + " peer=" + addr0
)
261 ev
= dev
[1].wait_global_event(["P2P-GROUP-STARTED","WPA: 4-Way Handshake failed"], timeout
=30)
263 raise Exception("Timeout on group restart (on client)")
264 if "P2P-GROUP-STARTED" in ev
:
265 raise Exception("Client managed to re-invoke after being removed")
266 dev
[0].dump_monitor()
268 logger
.info("Terminate persistent group")
269 dev
[0].remove_group()
270 dev
[0].dump_monitor()
272 def test_persistent_group_invite_removed_client(dev
):
273 """P2P persistent group client removal and re-invitation"""
274 addr0
= dev
[0].p2p_dev_addr()
275 addr1
= dev
[1].p2p_dev_addr()
276 dev
[0].request("P2P_SET per_sta_psk 1")
277 logger
.info("Form a persistent group")
278 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=dev
[0], i_intent
=15,
279 r_dev
=dev
[1], r_intent
=0)
280 if not i_res
['persistent'] or not r_res
['persistent']:
281 raise Exception("Formed group was not persistent")
283 logger
.info("Remove client from the group")
284 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr1
)
285 dev
[1].wait_go_ending_session()
287 logger
.info("Re-invite the removed client to join the group")
289 if not dev
[0].discover_peer(addr1
, social
=True):
290 raise Exception("Peer " + peer
+ " not found")
291 dev
[0].global_request("P2P_INVITE group=" + dev
[0].group_ifname
+ " peer=" + addr1
)
292 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=10)
294 raise Exception("Timeout on invitation")
295 if "sa=" + addr0
+ " persistent=" not in ev
:
296 raise Exception("Unexpected invitation event")
297 [event
,addr
,persistent
] = ev
.split(' ', 2)
298 dev
[1].global_request("P2P_GROUP_ADD " + persistent
)
299 ev
= dev
[1].wait_global_event(["P2P-PERSISTENT-PSK-FAIL"], timeout
=30)
301 raise Exception("Did not receive PSK failure report")
302 [tmp
,id] = ev
.split('=', 1)
303 ev
= dev
[1].wait_global_event(["P2P-GROUP-REMOVED"], timeout
=10)
305 raise Exception("Group removal event timed out")
306 if "reason=PSK_FAILURE" not in ev
:
307 raise Exception("Unexpected group removal reason")
308 dev
[1].request("REMOVE_NETWORK " + id)
310 logger
.info("Re-invite after client removed persistent group info")
312 if not dev
[0].discover_peer(addr1
, social
=True):
313 raise Exception("Peer " + peer
+ " not found")
314 dev
[0].global_request("P2P_INVITE group=" + dev
[0].group_ifname
+ " peer=" + addr1
)
315 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=10)
317 raise Exception("Timeout on invitation")
318 if " persistent=" in ev
:
319 raise Exception("Unexpected invitation event")
320 pin
= dev
[1].wps_read_pin()
321 dev
[0].p2p_go_authorize_client(pin
)
322 c_res
= dev
[1].p2p_connect_group(addr0
, pin
, timeout
=60)
323 if not c_res
['persistent']:
324 raise Exception("Joining client did not recognize persistent group")
325 if r_res
['psk'] == c_res
['psk']:
326 raise Exception("Same PSK assigned on both times")
327 hwsim_utils
.test_connectivity_p2p(dev
[0], dev
[1])
329 terminate_group(dev
[0], dev
[1])
331 def test_persistent_group_channel(dev
):
332 """P2P persistent group re-invocation with channel selection"""
333 form(dev
[0], dev
[1], test_data
=False)
335 logger
.info("Re-invoke persistent group from client with forced channel")
336 invite(dev
[1], dev
[0], "freq=2427")
337 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
338 if go_res
['freq'] != "2427":
339 raise Exception("Persistent group client forced channel not followed")
340 terminate_group(dev
[0], dev
[1])
342 logger
.info("Re-invoke persistent group from GO with forced channel")
343 invite(dev
[0], dev
[1], "freq=2432")
344 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
345 if go_res
['freq'] != "2432":
346 raise Exception("Persistent group GO channel preference not followed")
347 terminate_group(dev
[0], dev
[1])
349 logger
.info("Re-invoke persistent group from client with channel preference")
350 invite(dev
[1], dev
[0], "pref=2417")
351 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
352 if go_res
['freq'] != "2417":
353 raise Exception("Persistent group client channel preference not followed")
354 terminate_group(dev
[0], dev
[1])
356 def test_persistent_group_and_role_change(dev
):
357 """P2P persistent group, auto GO in another role, and re-invocation"""
360 logger
.info("Start and stop autonomous GO on previous P2P client device")
361 dev
[1].p2p_start_go()
362 dev
[1].remove_group()
363 dev
[1].dump_monitor()
365 logger
.info("Re-invoke the persistent group")
366 invite_from_go(dev
[0], dev
[1])
368 def test_persistent_go_client_list(dev
):
369 """P2P GO and list of clients in persistent group"""
370 addr0
= dev
[0].p2p_dev_addr()
371 addr1
= dev
[1].p2p_dev_addr()
372 addr2
= dev
[2].p2p_dev_addr()
374 res
= dev
[0].p2p_start_go(persistent
=True)
376 for n
in dev
[0].list_networks():
377 if "[P2P-PERSISTENT]" in n
['flags']:
381 raise Exception("Could not find persistent group entry")
383 connect_cli(dev
[0], dev
[1])
384 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
386 raise Exception("Unexpected p2p_client_list entry(2): " + clients
)
387 connect_cli(dev
[0], dev
[2])
388 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
389 if clients
!= addr2
+ " " + addr1
:
390 raise Exception("Unexpected p2p_client_list entry(3): " + clients
)
392 peer
= dev
[1].get_peer(res
['go_dev_addr'])
393 dev
[1].remove_group()
394 dev
[1].request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
395 ev
= dev
[1].wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
397 raise Exception("Timeout on group restart (on client)")
398 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
399 if clients
!= addr1
+ " " + addr2
:
400 raise Exception("Unexpected p2p_client_list entry(4): " + clients
)
402 dev
[2].remove_group()
403 dev
[1].remove_group()
404 dev
[0].remove_group()
406 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
407 if clients
!= addr1
+ " " + addr2
:
408 raise Exception("Unexpected p2p_client_list entry(5): " + clients
)
412 dev
[0].request("P2P_FLUSH")
413 dev
[0].discover_peer(addr1
, social
=True)
414 peer
= dev
[0].get_peer(addr1
)
415 if 'persistent' not in peer
or peer
['persistent'] != id:
416 raise Exception("Persistent group client not recognized(1)")
418 dev
[0].discover_peer(addr2
, social
=True)
419 peer
= dev
[0].get_peer(addr2
)
420 if 'persistent' not in peer
or peer
['persistent'] != id:
421 raise Exception("Persistent group client not recognized(2)")
423 def test_persistent_group_in_grpform(dev
):
424 """P2P persistent group parameters re-used in group formation"""
425 addr0
= dev
[0].p2p_dev_addr()
426 addr1
= dev
[1].p2p_dev_addr()
429 if not dev
[0].discover_peer(addr1
, social
=True):
430 raise Exception("Could not discover peer")
431 peer
= dev
[0].get_peer(addr1
)
432 if "persistent" not in peer
:
433 raise Exception("Could not map peer to a persistent group")
435 pin
= dev
[1].wps_read_pin()
436 dev
[1].p2p_go_neg_auth(addr0
, pin
, "display", go_intent
=0)
437 i_res
= dev
[0].p2p_go_neg_init(addr1
, pin
, "enter", timeout
=20,
439 persistent_id
=peer
['persistent'])
440 r_res
= dev
[1].p2p_go_neg_auth_result()
441 logger
.debug("i_res: " + str(i_res
))
442 logger
.debug("r_res: " + str(r_res
))
444 def test_persistent_group_without_persistent_reconnect(dev
):
445 """P2P persistent group re-invocation without persistent reconnect"""
447 dev
[0].dump_monitor()
448 dev
[1].dump_monitor()
450 logger
.info("Re-invoke persistent group from client")
451 invite(dev
[1], dev
[0], persistent_reconnect
=False)
453 ev
= dev
[0].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=15)
455 raise Exception("No invitation request reported");
456 if "persistent=" not in ev
:
457 raise Exception("Invalid invitation type reported: " + ev
)
459 ev2
= dev
[1].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=15)
461 raise Exception("No invitation response reported");
462 if "status=1" not in ev2
:
463 raise Exception("Unexpected status: " + ev2
)
466 exp
= r
'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*) freq=([0-9]*)'
467 s
= re
.split(exp
, ev
)
469 raise Exception("Could not parse invitation event")
473 logger
.info("Re-initiate invitation based on upper layer acceptance")
474 if "OK" not in dev
[0].global_request("P2P_INVITE persistent=" + id + " peer=" + sa
+ " freq=" + freq
):
475 raise Exception("Invitation command failed")
476 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
477 if go_res
['freq'] != freq
:
478 raise Exception("Unexpected channel on GO: {} MHz, expected {} MHz".format(go_res
['freq'], freq
))
479 if cli_res
['freq'] != freq
:
480 raise Exception("Unexpected channel on CLI: {} MHz, expected {} MHz".format(cli_res
['freq'], freq
))
481 terminate_group(dev
[0], dev
[1])
482 dev
[0].dump_monitor()
483 dev
[1].dump_monitor()
485 logger
.info("Re-invoke persistent group from GO")
486 invite(dev
[0], dev
[1], persistent_reconnect
=False)
488 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=15)
490 raise Exception("No invitation request reported");
491 if "persistent=" not in ev
:
492 raise Exception("Invalid invitation type reported: " + ev
)
494 ev2
= dev
[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=15)
496 raise Exception("No invitation response reported");
497 if "status=1" not in ev2
:
498 raise Exception("Unexpected status: " + ev2
)
501 exp
= r
'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*)'
502 s
= re
.split(exp
, ev
)
504 raise Exception("Could not parse invitation event")
507 logger
.info("Re-initiate invitation based on upper layer acceptance")
508 if "OK" not in dev
[1].global_request("P2P_INVITE persistent=" + id + " peer=" + sa
+ " freq=" + freq
):
509 raise Exception("Invitation command failed")
510 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
511 terminate_group(dev
[0], dev
[1])
513 def test_persistent_group_already_running(dev
):
514 """P2P persistent group formation and invitation while GO already running"""
516 peer
= dev
[1].get_peer(dev
[0].p2p_dev_addr())
517 listen_freq
= peer
['listen_freq']
518 dev
[0].dump_monitor()
519 dev
[1].dump_monitor()
520 networks
= dev
[0].list_networks()
521 if len(networks
) != 1:
522 raise Exception("Unexpected number of networks")
523 if "[P2P-PERSISTENT]" not in networks
[0]['flags']:
524 raise Exception("Not the persistent group data")
525 if "OK" not in dev
[0].global_request("P2P_GROUP_ADD persistent=" + networks
[0]['id'] + " freq=" + listen_freq
):
526 raise Exception("Could not state GO")
527 invite_from_cli(dev
[0], dev
[1])