tests: Ignore old scan results in test_grpform_pbc
[mech_eap.git] / tests / hwsim / test_p2p_grpform.py
1 #!/usr/bin/python
2 #
3 # P2P group formation test cases
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import logging
10 logger = logging.getLogger()
11 import time
12 import threading
13 import Queue
14
15 import hwsim_utils
16
17 def check_grpform_results(i_res, r_res):
18     if i_res['result'] != 'success' or r_res['result'] != 'success':
19         raise Exception("Failed group formation")
20     if i_res['ssid'] != r_res['ssid']:
21         raise Exception("SSID mismatch")
22     if i_res['freq'] != r_res['freq']:
23         raise Exception("freq mismatch")
24     if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
25         raise Exception("go_neg_freq mismatch")
26     if i_res['freq'] != i_res['go_neg_freq']:
27         raise Exception("freq/go_neg_freq mismatch")
28     if i_res['role'] != i_res['go_neg_role']:
29         raise Exception("role/go_neg_role mismatch")
30     if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
31         raise Exception("role/go_neg_role mismatch")
32     if i_res['go_dev_addr'] != r_res['go_dev_addr']:
33         raise Exception("GO Device Address mismatch")
34
35 def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
36     logger.debug("Initiate GO Negotiation from i_dev")
37     i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
38     logger.debug("i_res: " + str(i_res))
39     res.put(i_res)
40
41 def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
42     r_dev.p2p_listen()
43     i_dev.p2p_listen()
44     pin = r_dev.wps_read_pin()
45     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
46     r_dev.dump_monitor()
47     res = Queue.Queue()
48     t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
49     t.start()
50     logger.debug("Wait for GO Negotiation Request on r_dev")
51     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
52     if ev is None:
53         raise Exception("GO Negotiation timed out")
54     r_dev.dump_monitor()
55     logger.debug("Re-initiate GO Negotiation from r_dev")
56     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
57     logger.debug("r_res: " + str(r_res))
58     r_dev.dump_monitor()
59     t.join()
60     i_res = res.get()
61     logger.debug("i_res: " + str(i_res))
62     logger.info("Group formed")
63     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
64     i_dev.dump_monitor()
65
66 def go_neg_pin_authorized(i_dev, r_dev, i_intent=None, r_intent=None, expect_failure=False, i_go_neg_status=None, i_method='enter', r_method='display', test_data=True, i_freq=None, r_freq=None):
67     r_dev.p2p_listen()
68     i_dev.p2p_listen()
69     pin = r_dev.wps_read_pin()
70     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
71     r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, freq=r_freq)
72     i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent, expect_failure=expect_failure, freq=i_freq)
73     r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
74     logger.debug("i_res: " + str(i_res))
75     logger.debug("r_res: " + str(r_res))
76     r_dev.dump_monitor()
77     i_dev.dump_monitor()
78     if i_go_neg_status:
79         if i_res['result'] != 'go-neg-failed':
80             raise Exception("Expected GO Negotiation failure not reported")
81         if i_res['status'] != i_go_neg_status:
82             raise Exception("Expected GO Negotiation status not seen")
83     if expect_failure:
84         return
85     logger.info("Group formed")
86     if test_data:
87         hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
88     return [i_res, r_res]
89
90 def go_neg_init_pbc(i_dev, r_dev, i_intent, res):
91     logger.debug("Initiate GO Negotiation from i_dev")
92     i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc", timeout=20, go_intent=i_intent)
93     logger.debug("i_res: " + str(i_res))
94     res.put(i_res)
95
96 def go_neg_pbc(i_dev, r_dev, i_intent=None, r_intent=None):
97     r_dev.p2p_find(social=True)
98     i_dev.p2p_find(social=True)
99     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
100     r_dev.dump_monitor()
101     res = Queue.Queue()
102     t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res))
103     t.start()
104     logger.debug("Wait for GO Negotiation Request on r_dev")
105     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
106     if ev is None:
107         raise Exception("GO Negotiation timed out")
108     r_dev.dump_monitor()
109     logger.debug("Re-initiate GO Negotiation from r_dev")
110     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc", go_intent=r_intent, timeout=20)
111     logger.debug("r_res: " + str(r_res))
112     r_dev.dump_monitor()
113     t.join()
114     i_res = res.get()
115     logger.debug("i_res: " + str(i_res))
116     logger.info("Group formed")
117     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
118     i_dev.dump_monitor()
119     return [i_res, r_res]
120
121 def remove_group(dev1, dev2):
122     dev1.remove_group()
123     try:
124         dev2.remove_group()
125     except:
126         pass
127
128 def test_grpform(dev):
129     """P2P group formation using PIN and authorized connection (init -> GO)"""
130     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
131                                            r_dev=dev[1], r_intent=0)
132     check_grpform_results(i_res, r_res)
133     remove_group(dev[0], dev[1])
134
135 def test_grpform2(dev):
136     """P2P group formation using PIN and authorized connection (resp -> GO)"""
137     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
138     remove_group(dev[0], dev[1])
139
140 def test_grpform3(dev):
141     """P2P group formation using PIN and re-init GO Negotiation"""
142     go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
143     remove_group(dev[0], dev[1])
144
145 def test_grpform_pbc(dev):
146     """P2P group formation using PBC and re-init GO Negotiation"""
147     dev[0].request("SET ignore_old_scan_res 1")
148     dev[1].request("SET ignore_old_scan_res 1")
149     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
150     check_grpform_results(i_res, r_res)
151     if i_res['role'] != 'GO' or r_res['role'] != 'client':
152         raise Exception("Unexpected device roles")
153     remove_group(dev[0], dev[1])
154
155 def test_both_go_intent_15(dev):
156     """P2P GO Negotiation with both devices using GO intent 15"""
157     go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=15, expect_failure=True, i_go_neg_status=9)
158
159 def test_both_go_neg_display(dev):
160     """P2P GO Negotiation with both devices trying to display PIN"""
161     go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='display', r_method='display')
162
163 def test_both_go_neg_enter(dev):
164     """P2P GO Negotiation with both devices trying to enter PIN"""
165     go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='enter', r_method='enter')
166
167 def test_grpform_per_sta_psk(dev):
168     """P2P group formation with per-STA PSKs"""
169     dev[0].request("P2P_SET per_sta_psk 1")
170     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
171     check_grpform_results(i_res, r_res)
172
173     pin = dev[2].wps_read_pin()
174     dev[0].p2p_go_authorize_client(pin)
175     c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
176     check_grpform_results(i_res, c_res)
177
178     if r_res['psk'] == c_res['psk']:
179         raise Exception("Same PSK assigned for both clients")
180
181     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
182
183     dev[0].remove_group()
184     dev[1].wait_go_ending_session()
185     dev[2].wait_go_ending_session()
186
187 def test_grpform_per_sta_psk_wps(dev):
188     """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
189     dev[0].request("P2P_SET per_sta_psk 1")
190     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
191     check_grpform_results(i_res, r_res)
192
193     dev[0].p2p_go_authorize_client_pbc()
194     dev[2].request("WPS_PBC")
195     ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
196     if ev is None:
197         raise Exception("Association with the GO timed out")
198
199     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
200
201     dev[0].remove_group()
202     dev[2].request("DISCONNECT")
203     dev[1].wait_go_ending_session()
204
205 def test_grpform_force_chan_go(dev):
206     """P2P group formation forced channel selection by GO"""
207     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
208                                            i_freq=2432,
209                                            r_dev=dev[1], r_intent=0,
210                                            test_data=False)
211     check_grpform_results(i_res, r_res)
212     if i_res['freq'] != "2432":
213         raise Exception("Unexpected channel - did not follow GO's forced channel")
214     remove_group(dev[0], dev[1])
215
216 def test_grpform_force_chan_cli(dev):
217     """P2P group formation forced channel selection by client"""
218     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
219                                            i_freq=2417,
220                                            r_dev=dev[1], r_intent=15,
221                                            test_data=False)
222     check_grpform_results(i_res, r_res)
223     if i_res['freq'] != "2417":
224         raise Exception("Unexpected channel - did not follow GO's forced channel")
225     remove_group(dev[0], dev[1])
226
227 def test_grpform_force_chan_conflict(dev):
228     """P2P group formation fails due to forced channel mismatch"""
229     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
230                           r_dev=dev[1], r_intent=15, r_freq=2427,
231                           expect_failure=True, i_go_neg_status=7)
232
233 def test_grpform_pref_chan_go(dev):
234     """P2P group formation preferred channel selection by GO"""
235     dev[0].request("SET p2p_pref_chan 81:7")
236     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
237                                            r_dev=dev[1], r_intent=0,
238                                            test_data=False)
239     check_grpform_results(i_res, r_res)
240     if i_res['freq'] != "2442":
241         raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
242     remove_group(dev[0], dev[1])
243
244 def test_grpform_pref_chan_go_overridden(dev):
245     """P2P group formation preferred channel selection by GO overridden by client"""
246     dev[1].request("SET p2p_pref_chan 81:7")
247     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
248                                            i_freq=2422,
249                                            r_dev=dev[1], r_intent=15,
250                                            test_data=False)
251     check_grpform_results(i_res, r_res)
252     if i_res['freq'] != "2422":
253         raise Exception("Unexpected channel - did not follow client's forced channel")
254     remove_group(dev[0], dev[1])
255
256 def test_grpform_no_go_freq_forcing_chan(dev):
257     """P2P group formation with no-GO freq forcing channel"""
258     dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
259     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
260                                            r_dev=dev[1], r_intent=15,
261                                            test_data=False)
262     check_grpform_results(i_res, r_res)
263     if int(i_res['freq']) > 4000:
264         raise Exception("Unexpected channel - did not follow no-GO freq")
265     remove_group(dev[0], dev[1])
266
267 def test_grpform_no_go_freq_conflict(dev):
268     """P2P group formation fails due to no-GO range forced by client"""
269     dev[1].request("SET p2p_no_go_freq 2000-3000")
270     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
271                           r_dev=dev[1], r_intent=15,
272                           expect_failure=True, i_go_neg_status=7)
273
274 def test_grpform_no_5ghz_world_roaming(dev):
275     """P2P group formation with world roaming regulatory"""
276     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
277                                            r_dev=dev[1], r_intent=15,
278                                            test_data=False)
279     check_grpform_results(i_res, r_res)
280     if int(i_res['freq']) > 4000:
281         raise Exception("Unexpected channel - did not follow world roaming rules")
282     remove_group(dev[0], dev[1])
283
284 def test_grpform_no_5ghz_add_cli(dev):
285     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
286     dev[0].request("SET p2p_add_cli_chan 1")
287     dev[1].request("SET p2p_add_cli_chan 1")
288     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
289                                            r_dev=dev[1], r_intent=14,
290                                            test_data=False)
291     check_grpform_results(i_res, r_res)
292     if int(i_res['freq']) > 4000:
293         raise Exception("Unexpected channel - did not follow world roaming rules")
294     remove_group(dev[0], dev[1])
295
296 def test_grpform_no_5ghz_add_cli2(dev):
297     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
298     dev[0].request("SET p2p_add_cli_chan 1")
299     dev[1].request("SET p2p_add_cli_chan 1")
300     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
301                                            r_dev=dev[1], r_intent=0,
302                                            test_data=False)
303     check_grpform_results(i_res, r_res)
304     if int(i_res['freq']) > 4000:
305         raise Exception("Unexpected channel - did not follow world roaming rules")
306     remove_group(dev[0], dev[1])
307
308 def test_grpform_no_5ghz_add_cli3(dev):
309     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
310     dev[0].request("SET p2p_add_cli_chan 1")
311     dev[1].request("SET p2p_add_cli_chan 1")
312     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
313                                            r_dev=dev[1], r_intent=15,
314                                            test_data=False)
315     check_grpform_results(i_res, r_res)
316     if int(i_res['freq']) > 4000:
317         raise Exception("Unexpected channel - did not follow world roaming rules")
318     remove_group(dev[0], dev[1])
319
320 def test_grpform_no_5ghz_add_cli4(dev):
321     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
322     dev[0].request("SET p2p_add_cli_chan 1")
323     dev[1].request("SET p2p_add_cli_chan 1")
324     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
325                                            r_dev=dev[1], r_intent=0,
326                                            test_data=False)
327     check_grpform_results(i_res, r_res)
328     if int(i_res['freq']) > 4000:
329         raise Exception("Unexpected channel - did not follow world roaming rules")
330     remove_group(dev[0], dev[1])