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("Leave persistent group and rejoin it")
194 dev[2].remove_group()
195 ev = dev[2].wait_event(["P2P-GROUP-REMOVED"], timeout=3)
197 raise Exception("Group removal event timed out")
198 if not dev[2].discover_peer(addr0, social=True):
199 raise Exception("Peer " + peer + " not found")
200 dev[2].dump_monitor()
201 peer = dev[2].get_peer(addr0)
202 dev[2].global_request("P2P_GROUP_ADD persistent=" + peer['persistent'])
203 ev = dev[2].wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
205 raise Exception("Timeout on group restart (on client)")
206 cli_res = dev[2].group_form_result(ev)
207 if not cli_res['persistent']:
208 raise Exception("Persistent group not restarted as persistent (cli)")
209 hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
211 logger.info("Remove one of the clients from the group")
212 dev[0].global_request("P2P_REMOVE_CLIENT " + addr2)
213 dev[2].wait_go_ending_session()
215 logger.info("Try to reconnect after having been removed from group")
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","WPA: 4-Way Handshake failed"], timeout=30)
223 raise Exception("Timeout on group restart (on client)")
224 if "P2P-GROUP-STARTED" in ev:
225 raise Exception("Client managed to connect after being removed")
227 logger.info("Remove the remaining client from the group")
228 dev[0].global_request("P2P_REMOVE_CLIENT " + addr1)
229 dev[1].wait_go_ending_session()
231 logger.info("Terminate persistent group")
232 dev[0].remove_group()
233 dev[0].dump_monitor()
235 logger.info("Try to re-invoke persistent group from client")
236 dev[0].request("SET persistent_reconnect 1")
238 if not dev[1].discover_peer(addr0, social=True):
239 raise Exception("Peer " + peer + " not found")
240 dev[1].dump_monitor()
241 peer = dev[1].get_peer(addr0)
242 dev[1].global_request("P2P_INVITE persistent=" + peer['persistent'] + " peer=" + addr0)
243 ev = dev[1].wait_global_event(["P2P-GROUP-STARTED","WPA: 4-Way Handshake failed"], timeout=30)
245 raise Exception("Timeout on group restart (on client)")
246 if "P2P-GROUP-STARTED" in ev:
247 raise Exception("Client managed to re-invoke after being removed")
248 dev[0].dump_monitor()
250 logger.info("Terminate persistent group")
251 dev[0].remove_group()
252 dev[0].dump_monitor()
254 def test_persistent_group_invite_removed_client(dev):
255 """P2P persistent group client removal and re-invitation"""
256 addr0 = dev[0].p2p_dev_addr()
257 addr1 = dev[1].p2p_dev_addr()
258 dev[0].request("P2P_SET per_sta_psk 1")
259 logger.info("Form a persistent group")
260 [i_res, r_res] = go_neg_pin_authorized_persistent(i_dev=dev[0], i_intent=15,
261 r_dev=dev[1], r_intent=0)
262 if not i_res['persistent'] or not r_res['persistent']:
263 raise Exception("Formed group was not persistent")
265 logger.info("Remove client from the group")
266 dev[0].global_request("P2P_REMOVE_CLIENT " + addr1)
267 dev[1].wait_go_ending_session()
269 logger.info("Re-invite the removed client to join the group")
271 if not dev[0].discover_peer(addr1, social=True):
272 raise Exception("Peer " + peer + " not found")
273 dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
274 ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=10)
276 raise Exception("Timeout on invitation")
277 if "sa=" + addr0 + " persistent=" not in ev:
278 raise Exception("Unexpected invitation event")
279 [event,addr,persistent] = ev.split(' ', 2)
280 dev[1].global_request("P2P_GROUP_ADD " + persistent)
281 ev = dev[1].wait_global_event(["P2P-PERSISTENT-PSK-FAIL"], timeout=30)
283 raise Exception("Did not receive PSK failure report")
284 [tmp,id] = ev.split('=', 1)
285 ev = dev[1].wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
287 raise Exception("Group removal event timed out")
288 if "reason=PSK_FAILURE" not in ev:
289 raise Exception("Unexpected group removal reason")
290 dev[1].request("REMOVE_NETWORK " + id)
292 logger.info("Re-invite after client removed persistent group info")
294 if not dev[0].discover_peer(addr1, social=True):
295 raise Exception("Peer " + peer + " not found")
296 dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
297 ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=10)
299 raise Exception("Timeout on invitation")
300 if " persistent=" in ev:
301 raise Exception("Unexpected invitation event")
302 pin = dev[1].wps_read_pin()
303 dev[0].p2p_go_authorize_client(pin)
304 c_res = dev[1].p2p_connect_group(addr0, pin, timeout=60)
305 if not c_res['persistent']:
306 raise Exception("Joining client did not recognize persistent group")
307 if r_res['psk'] == c_res['psk']:
308 raise Exception("Same PSK assigned on both times")
309 hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
311 terminate_group(dev[0], dev[1])
313 def test_persistent_group_channel(dev):
314 """P2P persistent group re-invocation with channel selection"""
315 form(dev[0], dev[1], test_data=False)
317 logger.info("Re-invoke persistent group from client with forced channel")
318 invite(dev[1], dev[0], "freq=2427")
319 [go_res, cli_res] = check_result(dev[0], dev[1])
320 if go_res['freq'] != "2427":
321 raise Exception("Persistent group client forced channel not followed")
322 terminate_group(dev[0], dev[1])
324 logger.info("Re-invoke persistent group from GO with forced channel")
325 invite(dev[0], dev[1], "freq=2432")
326 [go_res, cli_res] = check_result(dev[0], dev[1])
327 if go_res['freq'] != "2432":
328 raise Exception("Persistent group GO channel preference not followed")
329 terminate_group(dev[0], dev[1])
331 logger.info("Re-invoke persistent group from client with channel preference")
332 invite(dev[1], dev[0], "pref=2417")
333 [go_res, cli_res] = check_result(dev[0], dev[1])
334 if go_res['freq'] != "2417":
335 raise Exception("Persistent group client channel preference not followed")
336 terminate_group(dev[0], dev[1])
338 def test_persistent_group_and_role_change(dev):
339 """P2P persistent group, auto GO in another role, and re-invocation"""
342 logger.info("Start and stop autonomous GO on previous P2P client device")
343 dev[1].p2p_start_go()
344 dev[1].remove_group()
345 dev[1].dump_monitor()
347 logger.info("Re-invoke the persistent group")
348 invite_from_go(dev[0], dev[1])
350 def test_persistent_go_client_list(dev):
351 """P2P GO and list of clients in persistent group"""
352 addr0 = dev[0].p2p_dev_addr()
353 addr1 = dev[1].p2p_dev_addr()
354 addr2 = dev[2].p2p_dev_addr()
356 res = dev[0].p2p_start_go(persistent=True)
358 for n in dev[0].list_networks():
359 if "[P2P-PERSISTENT]" in n['flags']:
363 raise Exception("Could not find persistent group entry")
365 connect_cli(dev[0], dev[1])
366 clients = dev[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
368 raise Exception("Unexpected p2p_client_list entry(2): " + clients)
369 connect_cli(dev[0], dev[2])
370 clients = dev[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
371 if clients != addr2 + " " + addr1:
372 raise Exception("Unexpected p2p_client_list entry(3): " + clients)
374 peer = dev[1].get_peer(res['go_dev_addr'])
375 dev[1].remove_group()
376 dev[1].request("P2P_GROUP_ADD persistent=" + peer['persistent'])
377 ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
379 raise Exception("Timeout on group restart (on client)")
380 clients = dev[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
381 if clients != addr1 + " " + addr2:
382 raise Exception("Unexpected p2p_client_list entry(4): " + clients)
384 dev[2].remove_group()
385 dev[1].remove_group()
386 dev[0].remove_group()
388 clients = dev[0].request("GET_NETWORK " + id + " p2p_client_list").rstrip()
389 if clients != addr1 + " " + addr2:
390 raise Exception("Unexpected p2p_client_list entry(5): " + clients)
394 dev[0].request("P2P_FLUSH")
395 dev[0].discover_peer(addr1, social=True)
396 peer = dev[0].get_peer(addr1)
397 if 'persistent' not in peer or peer['persistent'] != id:
398 raise Exception("Persistent group client not recognized(1)")
400 dev[0].discover_peer(addr2, social=True)
401 peer = dev[0].get_peer(addr2)
402 if 'persistent' not in peer or peer['persistent'] != id:
403 raise Exception("Persistent group client not recognized(2)")
405 def test_persistent_group_in_grpform(dev):
406 """P2P persistent group parameters re-used in group formation"""
407 addr0 = dev[0].p2p_dev_addr()
408 addr1 = dev[1].p2p_dev_addr()
411 if not dev[0].discover_peer(addr1, social=True):
412 raise Exception("Could not discover peer")
413 peer = dev[0].get_peer(addr1)
414 if "persistent" not in peer:
415 raise Exception("Could not map peer to a persistent group")
417 pin = dev[1].wps_read_pin()
418 dev[1].p2p_go_neg_auth(addr0, pin, "display", go_intent=0)
419 i_res = dev[0].p2p_go_neg_init(addr1, pin, "enter", timeout=20,
421 persistent_id=peer['persistent'])
422 r_res = dev[1].p2p_go_neg_auth_result()
423 logger.debug("i_res: " + str(i_res))
424 logger.debug("r_res: " + str(r_res))
426 def test_persistent_group_without_persistent_reconnect(dev):
427 """P2P persistent group re-invocation without persistent reconnect"""
429 dev[0].dump_monitor()
430 dev[1].dump_monitor()
432 logger.info("Re-invoke persistent group from client")
433 invite(dev[1], dev[0], persistent_reconnect=False)
435 ev = dev[0].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=15)
437 raise Exception("No invitation request reported");
438 if "persistent=" not in ev:
439 raise Exception("Invalid invitation type reported: " + ev)
441 ev2 = dev[1].wait_global_event(["P2P-INVITATION-RESULT"], timeout=15)
443 raise Exception("No invitation response reported");
444 if "status=1" not in ev2:
445 raise Exception("Unexpected status: " + ev2)
448 exp = r'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*) freq=([0-9]*)'
449 s = re.split(exp, ev)
451 raise Exception("Could not parse invitation event")
455 logger.info("Re-initiate invitation based on upper layer acceptance")
456 if "OK" not in dev[0].global_request("P2P_INVITE persistent=" + id + " peer=" + sa + " freq=" + freq):
457 raise Exception("Invitation command failed")
458 [go_res, cli_res] = check_result(dev[0], dev[1])
459 if go_res['freq'] != freq:
460 raise Exception("Unexpected channel on GO: {} MHz, expected {} MHz".format(go_res['freq'], freq))
461 if cli_res['freq'] != freq:
462 raise Exception("Unexpected channel on CLI: {} MHz, expected {} MHz".format(cli_res['freq'], freq))
463 terminate_group(dev[0], dev[1])
464 dev[0].dump_monitor()
465 dev[1].dump_monitor()
467 logger.info("Re-invoke persistent group from GO")
468 invite(dev[0], dev[1], persistent_reconnect=False)
470 ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=15)
472 raise Exception("No invitation request reported");
473 if "persistent=" not in ev:
474 raise Exception("Invalid invitation type reported: " + ev)
476 ev2 = dev[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout=15)
478 raise Exception("No invitation response reported");
479 if "status=1" not in ev2:
480 raise Exception("Unexpected status: " + ev2)
483 exp = r'<.>(P2P-INVITATION-RECEIVED) sa=([0-9a-f:]*) persistent=([0-9]*)'
484 s = re.split(exp, ev)
486 raise Exception("Could not parse invitation event")
489 logger.info("Re-initiate invitation based on upper layer acceptance")
490 if "OK" not in dev[1].global_request("P2P_INVITE persistent=" + id + " peer=" + sa + " freq=" + freq):
491 raise Exception("Invitation command failed")
492 [go_res, cli_res] = check_result(dev[0], dev[1])
493 terminate_group(dev[0], dev[1])
495 def test_persistent_group_already_running(dev):
496 """P2P persistent group formation and invitation while GO already running"""
498 peer = dev[1].get_peer(dev[0].p2p_dev_addr())
499 listen_freq = peer['listen_freq']
500 dev[0].dump_monitor()
501 dev[1].dump_monitor()
502 networks = dev[0].list_networks()
503 if len(networks) != 1:
504 raise Exception("Unexpected number of networks")
505 if "[P2P-PERSISTENT]" not in networks[0]['flags']:
506 raise Exception("Not the persistent group data")
507 if "OK" not in dev[0].global_request("P2P_GROUP_ADD persistent=" + networks[0]['id'] + " freq=" + listen_freq):
508 raise Exception("Could not state GO")
509 invite_from_cli(dev[0], dev[1])