]>
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
)
93 def invite_from_cli(go
, cli
):
94 logger
.info("Re-invoke persistent group from client")
96 [go_res
, cli_res
] = check_result(go
, cli
)
97 hwsim_utils
.test_connectivity_p2p(go
, cli
)
98 terminate_group(go
, cli
)
99 return [go_res
, cli_res
]
101 def invite_from_go(go
, cli
):
102 logger
.info("Re-invoke persistent group from GO")
104 [go_res
, cli_res
] = check_result(go
, cli
)
105 hwsim_utils
.test_connectivity_p2p(go
, cli
)
106 terminate_group(go
, cli
)
107 return [go_res
, cli_res
]
109 def test_persistent_group(dev
):
110 """P2P persistent group formation and re-invocation"""
112 invite_from_cli(dev
[0], dev
[1])
113 invite_from_go(dev
[0], dev
[1])
115 logger
.info("Remove group on the client and try to invite from GO")
117 for n
in dev
[0].list_networks():
118 if "[P2P-PERSISTENT]" in n
['flags']:
122 raise Exception("Could not find persistent group entry")
123 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
124 if dev
[1].p2p_dev_addr() not in clients
:
125 raise Exception("Peer missing from client list")
126 if "FAIL" not in dev
[1].request("SELECT_NETWORK " + str(id)):
127 raise Exception("SELECT_NETWORK succeeded unexpectedly")
128 if "FAIL" not in dev
[1].request("SELECT_NETWORK 1234567"):
129 raise Exception("SELECT_NETWORK succeeded unexpectedly(2)")
130 if "FAIL" not in dev
[1].request("ENABLE_NETWORK " + str(id)):
131 raise Exception("ENABLE_NETWORK succeeded unexpectedly")
132 if "FAIL" not in dev
[1].request("ENABLE_NETWORK 1234567"):
133 raise Exception("ENABLE_NETWORK succeeded unexpectedly(2)")
134 if "FAIL" not in dev
[1].request("DISABLE_NETWORK " + str(id)):
135 raise Exception("DISABLE_NETWORK succeeded unexpectedly")
136 if "FAIL" not in dev
[1].request("DISABLE_NETWORK 1234567"):
137 raise Exception("DISABLE_NETWORK succeeded unexpectedly(2)")
138 if "FAIL" not in dev
[1].request("REMOVE_NETWORK 1234567"):
139 raise Exception("REMOVE_NETWORK succeeded unexpectedly")
140 dev
[1].request("REMOVE_NETWORK all")
141 if len(dev
[1].list_networks()) > 0:
142 raise Exception("Unexpected network block remaining")
143 invite(dev
[0], dev
[1])
144 ev
= dev
[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=10)
146 raise Exception("No invitation result seen")
147 if "status=8" not in ev
:
148 raise Exception("Unexpected invitation result: " + ev
)
149 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
150 if dev
[1].p2p_dev_addr() in clients
:
151 raise Exception("Peer was still in client list")
153 def test_persistent_group2(dev
):
154 """P2P persistent group formation with reverse roles"""
155 form(dev
[0], dev
[1], reverse_init
=True)
156 invite_from_cli(dev
[0], dev
[1])
157 invite_from_go(dev
[0], dev
[1])
159 def test_persistent_group3(dev
):
160 """P2P persistent group formation and re-invocation with empty BSS table"""
162 dev
[1].request("BSS_FLUSH 0")
163 invite_from_cli(dev
[0], dev
[1])
164 dev
[1].request("BSS_FLUSH 0")
165 invite_from_go(dev
[0], dev
[1])
167 def test_persistent_group_per_sta_psk(dev
):
168 """P2P persistent group formation and re-invocation using per-client PSK"""
169 addr0
= dev
[0].p2p_dev_addr()
170 addr1
= dev
[1].p2p_dev_addr()
171 addr2
= dev
[2].p2p_dev_addr()
172 dev
[0].request("P2P_SET per_sta_psk 1")
173 logger
.info("Form a persistent group")
174 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=dev
[0], i_intent
=15,
175 r_dev
=dev
[1], r_intent
=0)
176 if not i_res
['persistent'] or not r_res
['persistent']:
177 raise Exception("Formed group was not persistent")
179 logger
.info("Join another client to the group")
180 pin
= dev
[2].wps_read_pin()
181 dev
[0].p2p_go_authorize_client(pin
)
182 c_res
= dev
[2].p2p_connect_group(addr0
, pin
, timeout
=60)
183 if not c_res
['persistent']:
184 raise Exception("Joining client did not recognize persistent group")
185 if r_res
['psk'] == c_res
['psk']:
186 raise Exception("Same PSK assigned for both clients")
187 hwsim_utils
.test_connectivity_p2p_sta(dev
[1], dev
[2])
189 logger
.info("Leave persistent group and rejoin it")
190 dev
[2].remove_group()
191 ev
= dev
[2].wait_event(["P2P-GROUP-REMOVED"], timeout
=3)
193 raise Exception("Group removal event timed out")
194 if not dev
[2].discover_peer(addr0
, social
=True):
195 raise Exception("Peer " + peer
+ " not found")
196 dev
[2].dump_monitor()
197 peer
= dev
[2].get_peer(addr0
)
198 dev
[2].global_request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
199 ev
= dev
[2].wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
201 raise Exception("Timeout on group restart (on client)")
202 cli_res
= dev
[2].group_form_result(ev
)
203 if not cli_res
['persistent']:
204 raise Exception("Persistent group not restarted as persistent (cli)")
205 hwsim_utils
.test_connectivity_p2p(dev
[1], dev
[2])
207 logger
.info("Remove one of the clients from the group")
208 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr2
)
209 dev
[2].wait_go_ending_session()
211 logger
.info("Try to reconnect after having been removed from group")
212 if not dev
[2].discover_peer(addr0
, social
=True):
213 raise Exception("Peer " + peer
+ " not found")
214 dev
[2].dump_monitor()
215 peer
= dev
[2].get_peer(addr0
)
216 dev
[2].global_request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
217 ev
= dev
[2].wait_global_event(["P2P-GROUP-STARTED","WPA: 4-Way Handshake failed"], timeout
=30)
219 raise Exception("Timeout on group restart (on client)")
220 if "P2P-GROUP-STARTED" in ev
:
221 raise Exception("Client managed to connect after being removed")
223 logger
.info("Remove the remaining client from the group")
224 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr1
)
225 dev
[1].wait_go_ending_session()
227 logger
.info("Terminate persistent group")
228 dev
[0].remove_group()
229 dev
[0].dump_monitor()
231 logger
.info("Try to re-invoke persistent group from client")
232 dev
[0].request("SET persistent_reconnect 1")
234 if not dev
[1].discover_peer(addr0
, social
=True):
235 raise Exception("Peer " + peer
+ " not found")
236 dev
[1].dump_monitor()
237 peer
= dev
[1].get_peer(addr0
)
238 dev
[1].global_request("P2P_INVITE persistent=" + peer
['persistent'] + " peer=" + addr0
)
239 ev
= dev
[1].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 re-invoke after being removed")
244 dev
[0].dump_monitor()
246 logger
.info("Terminate persistent group")
247 dev
[0].remove_group()
248 dev
[0].dump_monitor()
250 def test_persistent_group_invite_removed_client(dev
):
251 """P2P persistent group client removal and re-invitation"""
252 addr0
= dev
[0].p2p_dev_addr()
253 addr1
= dev
[1].p2p_dev_addr()
254 dev
[0].request("P2P_SET per_sta_psk 1")
255 logger
.info("Form a persistent group")
256 [i_res
, r_res
] = go_neg_pin_authorized_persistent(i_dev
=dev
[0], i_intent
=15,
257 r_dev
=dev
[1], r_intent
=0)
258 if not i_res
['persistent'] or not r_res
['persistent']:
259 raise Exception("Formed group was not persistent")
261 logger
.info("Remove client from the group")
262 dev
[0].global_request("P2P_REMOVE_CLIENT " + addr1
)
263 dev
[1].wait_go_ending_session()
265 logger
.info("Re-invite the removed client to join the group")
267 if not dev
[0].discover_peer(addr1
, social
=True):
268 raise Exception("Peer " + peer
+ " not found")
269 dev
[0].global_request("P2P_INVITE group=" + dev
[0].group_ifname
+ " peer=" + addr1
)
270 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=10)
272 raise Exception("Timeout on invitation")
273 if "sa=" + addr0
+ " persistent=" not in ev
:
274 raise Exception("Unexpected invitation event")
275 [event
,addr
,persistent
] = ev
.split(' ', 2)
276 dev
[1].global_request("P2P_GROUP_ADD " + persistent
)
277 ev
= dev
[1].wait_global_event(["P2P-PERSISTENT-PSK-FAIL"], timeout
=30)
279 raise Exception("Did not receive PSK failure report")
280 [tmp
,id] = ev
.split('=', 1)
281 ev
= dev
[1].wait_global_event(["P2P-GROUP-REMOVED"], timeout
=10)
283 raise Exception("Group removal event timed out")
284 if "reason=PSK_FAILURE" not in ev
:
285 raise Exception("Unexpected group removal reason")
286 dev
[1].request("REMOVE_NETWORK " + id)
288 logger
.info("Re-invite after client removed persistent group info")
290 if not dev
[0].discover_peer(addr1
, social
=True):
291 raise Exception("Peer " + peer
+ " not found")
292 dev
[0].global_request("P2P_INVITE group=" + dev
[0].group_ifname
+ " peer=" + addr1
)
293 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=10)
295 raise Exception("Timeout on invitation")
296 if " persistent=" in ev
:
297 raise Exception("Unexpected invitation event")
298 pin
= dev
[1].wps_read_pin()
299 dev
[0].p2p_go_authorize_client(pin
)
300 c_res
= dev
[1].p2p_connect_group(addr0
, pin
, timeout
=60)
301 if not c_res
['persistent']:
302 raise Exception("Joining client did not recognize persistent group")
303 if r_res
['psk'] == c_res
['psk']:
304 raise Exception("Same PSK assigned on both times")
305 hwsim_utils
.test_connectivity_p2p(dev
[0], dev
[1])
307 terminate_group(dev
[0], dev
[1])
309 def test_persistent_group_channel(dev
):
310 """P2P persistent group re-invocation with channel selection"""
311 form(dev
[0], dev
[1], test_data
=False)
313 logger
.info("Re-invoke persistent group from client with forced channel")
314 invite(dev
[1], dev
[0], "freq=2427")
315 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
316 if go_res
['freq'] != "2427":
317 raise Exception("Persistent group client forced channel not followed")
318 terminate_group(dev
[0], dev
[1])
320 logger
.info("Re-invoke persistent group from GO with forced channel")
321 invite(dev
[0], dev
[1], "freq=2432")
322 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
323 if go_res
['freq'] != "2432":
324 raise Exception("Persistent group GO channel preference not followed")
325 terminate_group(dev
[0], dev
[1])
327 logger
.info("Re-invoke persistent group from client with channel preference")
328 invite(dev
[1], dev
[0], "pref=2417")
329 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
330 if go_res
['freq'] != "2417":
331 raise Exception("Persistent group client channel preference not followed")
332 terminate_group(dev
[0], dev
[1])
334 def test_persistent_group_and_role_change(dev
):
335 """P2P persistent group, auto GO in another role, and re-invocation"""
338 logger
.info("Start and stop autonomous GO on previous P2P client device")
339 dev
[1].p2p_start_go()
340 dev
[1].remove_group()
341 dev
[1].dump_monitor()
343 logger
.info("Re-invoke the persistent group")
344 invite_from_go(dev
[0], dev
[1])
346 def test_persistent_go_client_list(dev
):
347 """P2P GO and list of clients in persistent group"""
348 addr0
= dev
[0].p2p_dev_addr()
349 addr1
= dev
[1].p2p_dev_addr()
350 addr2
= dev
[2].p2p_dev_addr()
352 res
= dev
[0].p2p_start_go(persistent
=True)
354 for n
in dev
[0].list_networks():
355 if "[P2P-PERSISTENT]" in n
['flags']:
359 raise Exception("Could not find persistent group entry")
361 connect_cli(dev
[0], dev
[1])
362 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
364 raise Exception("Unexpected p2p_client_list entry(2): " + clients
)
365 connect_cli(dev
[0], dev
[2])
366 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
367 if clients
!= addr2
+ " " + addr1
:
368 raise Exception("Unexpected p2p_client_list entry(3): " + clients
)
370 peer
= dev
[1].get_peer(res
['go_dev_addr'])
371 dev
[1].remove_group()
372 dev
[1].request("P2P_GROUP_ADD persistent=" + peer
['persistent'])
373 ev
= dev
[1].wait_global_event(["P2P-GROUP-STARTED"], timeout
=30)
375 raise Exception("Timeout on group restart (on client)")
376 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
377 if clients
!= addr1
+ " " + addr2
:
378 raise Exception("Unexpected p2p_client_list entry(4): " + clients
)
380 dev
[2].remove_group()
381 dev
[1].remove_group()
382 dev
[0].remove_group()
384 clients
= dev
[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
385 if clients
!= addr1
+ " " + addr2
:
386 raise Exception("Unexpected p2p_client_list entry(5): " + clients
)
390 dev
[0].request("P2P_FLUSH")
391 dev
[0].discover_peer(addr1
, social
=True)
392 peer
= dev
[0].get_peer(addr1
)
393 if 'persistent' not in peer
or peer
['persistent'] != id:
394 raise Exception("Persistent group client not recognized(1)")
396 dev
[0].discover_peer(addr2
, social
=True)
397 peer
= dev
[0].get_peer(addr2
)
398 if 'persistent' not in peer
or peer
['persistent'] != id:
399 raise Exception("Persistent group client not recognized(2)")
401 def test_persistent_group_in_grpform(dev
):
402 """P2P persistent group parameters re-used in group formation"""
403 addr0
= dev
[0].p2p_dev_addr()
404 addr1
= dev
[1].p2p_dev_addr()
407 if not dev
[0].discover_peer(addr1
, social
=True):
408 raise Exception("Could not discover peer")
409 peer
= dev
[0].get_peer(addr1
)
410 if "persistent" not in peer
:
411 raise Exception("Could not map peer to a persistent group")
413 pin
= dev
[1].wps_read_pin()
414 dev
[1].p2p_go_neg_auth(addr0
, pin
, "display", go_intent
=0)
415 i_res
= dev
[0].p2p_go_neg_init(addr1
, pin
, "enter", timeout
=20,
417 persistent_id
=peer
['persistent'])
418 r_res
= dev
[1].p2p_go_neg_auth_result()
419 logger
.debug("i_res: " + str(i_res
))
420 logger
.debug("r_res: " + str(r_res
))
422 def test_persistent_group_without_persistent_reconnect(dev
):
423 """P2P persistent group re-invocation without persistent reconnect"""
425 dev
[0].dump_monitor()
426 dev
[1].dump_monitor()
428 logger
.info("Re-invoke persistent group from client")
429 invite(dev
[1], dev
[0], persistent_reconnect
=False)
431 ev
= dev
[0].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=15)
433 raise Exception("No invitation request reported");
434 if "persistent=" not in ev
:
435 raise Exception("Invalid invitation type reported: " + ev
)
437 ev2
= dev
[1].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=15)
439 raise Exception("No invitation response reported");
440 if "status=1" not in ev2
:
441 raise Exception("Unexpected status: " + ev2
)
444 exp
= r
'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*) freq=([0-9]*)'
445 s
= re
.split(exp
, ev
)
447 raise Exception("Could not parse invitation event")
451 logger
.info("Re-initiate invitation based on upper layer acceptance")
452 if "OK" not in dev
[0].global_request("P2P_INVITE persistent=" + id + " peer=" + sa
+ " freq=" + freq
):
453 raise Exception("Invitation command failed")
454 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
455 if go_res
['freq'] != freq
:
456 raise Exception("Unexpected channel on GO: {} MHz, expected {} MHz".format(go_res
['freq'], freq
))
457 if cli_res
['freq'] != freq
:
458 raise Exception("Unexpected channel on CLI: {} MHz, expected {} MHz".format(cli_res
['freq'], freq
))
459 terminate_group(dev
[0], dev
[1])
460 dev
[0].dump_monitor()
461 dev
[1].dump_monitor()
463 logger
.info("Re-invoke persistent group from GO")
464 invite(dev
[0], dev
[1], persistent_reconnect
=False)
466 ev
= dev
[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout
=15)
468 raise Exception("No invitation request reported");
469 if "persistent=" not in ev
:
470 raise Exception("Invalid invitation type reported: " + ev
)
472 ev2
= dev
[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout
=15)
474 raise Exception("No invitation response reported");
475 if "status=1" not in ev2
:
476 raise Exception("Unexpected status: " + ev2
)
479 exp
= r
'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*)'
480 s
= re
.split(exp
, ev
)
482 raise Exception("Could not parse invitation event")
485 logger
.info("Re-initiate invitation based on upper layer acceptance")
486 if "OK" not in dev
[1].global_request("P2P_INVITE persistent=" + id + " peer=" + sa
+ " freq=" + freq
):
487 raise Exception("Invitation command failed")
488 [go_res
, cli_res
] = check_result(dev
[0], dev
[1])
489 terminate_group(dev
[0], dev
[1])