tests: Remove ignore_old_scan_res workaround
[mech_eap.git] / tests / hwsim / test_p2p_autogo.py
1 #!/usr/bin/python
2 #
3 # P2P autonomous GO 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 time
10 import subprocess
11 import logging
12 logger = logging.getLogger()
13
14 import hwsim_utils
15 import utils
16 from wlantest import Wlantest
17
18 def autogo(go, freq=None):
19     logger.info("Start autonomous GO " + go.ifname)
20     res = go.p2p_start_go(freq=freq)
21     logger.debug("res: " + str(res))
22     return res
23
24 def connect_cli(go, client):
25     logger.info("Try to connect the client to the GO")
26     pin = client.wps_read_pin()
27     go.p2p_go_authorize_client(pin)
28     res = client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60)
29     logger.info("Client connected")
30     hwsim_utils.test_connectivity_p2p(go, client)
31     return res
32
33 def test_autogo(dev):
34     """P2P autonomous GO and client joining group"""
35     res = autogo(dev[0])
36     if "p2p-wlan" in res['ifname']:
37         raise Exception("Unexpected group interface name on GO")
38     res = connect_cli(dev[0], dev[1])
39     if "p2p-wlan" in res['ifname']:
40         raise Exception("Unexpected group interface name on client")
41     bss = dev[1].get_bss("p2p_dev_addr=" + dev[0].p2p_dev_addr())
42     if bss['bssid'] != dev[0].p2p_interface_addr():
43         raise Exception("Unexpected BSSID in the BSS entry for the GO")
44     id = bss['id']
45     bss = dev[1].get_bss("ID-" + id)
46     if bss['id'] != id:
47         raise Exception("Could not find BSS entry based on id")
48     res = dev[1].request("BSS RANGE=" + id + "- MASK=0x1")
49     if "id=" + id not in res:
50         raise Exception("Could not find BSS entry based on id range")
51
52     # Presence request to increase testing coverage
53     if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
54         raise Exception("Could not send presence request")
55     ev = dev[1].wait_event(["P2P-PRESENCE-RESPONSE"])
56     if ev is None:
57         raise Exception("Timeout while waiting for Presence Response")
58
59     dev[0].remove_group()
60     dev[1].wait_go_ending_session()
61
62 def test_autogo2(dev):
63     """P2P autonomous GO with a separate group interface and client joining group"""
64     dev[0].request("SET p2p_no_group_iface 0")
65     res = autogo(dev[0])
66     if "p2p-wlan" not in res['ifname']:
67         raise Exception("Unexpected group interface name on GO")
68     if res['ifname'] not in utils.get_ifnames():
69         raise Exception("Could not find group interface netdev")
70     connect_cli(dev[0], dev[1])
71     dev[0].remove_group()
72     dev[1].wait_go_ending_session()
73     if res['ifname'] in utils.get_ifnames():
74         raise Exception("Group interface netdev was not removed")
75
76 def test_autogo3(dev):
77     """P2P autonomous GO and client with a separate group interface joining group"""
78     dev[1].request("SET p2p_no_group_iface 0")
79     autogo(dev[0])
80     res = connect_cli(dev[0], dev[1])
81     if "p2p-wlan" not in res['ifname']:
82         raise Exception("Unexpected group interface name on client")
83     if res['ifname'] not in utils.get_ifnames():
84         raise Exception("Could not find group interface netdev")
85     dev[0].remove_group()
86     dev[1].wait_go_ending_session()
87     dev[1].ping()
88     if res['ifname'] in utils.get_ifnames():
89         raise Exception("Group interface netdev was not removed")
90
91 def test_autogo4(dev):
92     """P2P autonomous GO and client joining group (both with a separate group interface)"""
93     dev[0].request("SET p2p_no_group_iface 0")
94     dev[1].request("SET p2p_no_group_iface 0")
95     res1 = autogo(dev[0])
96     res2 = connect_cli(dev[0], dev[1])
97     if "p2p-wlan" not in res1['ifname']:
98         raise Exception("Unexpected group interface name on GO")
99     if "p2p-wlan" not in res2['ifname']:
100         raise Exception("Unexpected group interface name on client")
101     ifnames = utils.get_ifnames()
102     if res1['ifname'] not in ifnames:
103         raise Exception("Could not find GO group interface netdev")
104     if res2['ifname'] not in ifnames:
105         raise Exception("Could not find client group interface netdev")
106     dev[0].remove_group()
107     dev[1].wait_go_ending_session()
108     dev[1].ping()
109     ifnames = utils.get_ifnames()
110     if res1['ifname'] in ifnames:
111         raise Exception("GO group interface netdev was not removed")
112     if res2['ifname'] in ifnames:
113         raise Exception("Client group interface netdev was not removed")
114
115 def test_autogo_2cli(dev):
116     """P2P autonomous GO and two clients joining group"""
117     autogo(dev[0])
118     connect_cli(dev[0], dev[1])
119     connect_cli(dev[0], dev[2])
120     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
121     dev[0].global_request("P2P_REMOVE_CLIENT " + dev[1].p2p_dev_addr())
122     dev[1].wait_go_ending_session()
123     dev[0].remove_group()
124     dev[2].wait_go_ending_session()
125
126 def test_autogo_tdls(dev):
127     """P2P autonomous GO and two clients using TDLS"""
128     wt = Wlantest()
129     go = dev[0]
130     logger.info("Start autonomous GO with fixed parameters " + go.ifname)
131     id = go.add_network()
132     go.set_network_quoted(id, "ssid", "DIRECT-tdls")
133     go.set_network_quoted(id, "psk", "12345678")
134     go.set_network(id, "mode", "3")
135     go.set_network(id, "disabled", "2")
136     res = go.p2p_start_go(persistent=id)
137     logger.debug("res: " + str(res))
138     wt.flush()
139     wt.add_passphrase("12345678")
140     connect_cli(go, dev[1])
141     connect_cli(go, dev[2])
142     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
143     bssid = dev[0].p2p_interface_addr()
144     addr1 = dev[1].p2p_interface_addr()
145     addr2 = dev[2].p2p_interface_addr()
146     dev[1].tdls_setup(addr2)
147     time.sleep(1)
148     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
149     conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr1, addr2);
150     if conf == 0:
151         raise Exception("No TDLS Setup Confirm (success) seen")
152     dl = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
153     if dl == 0:
154         raise Exception("No valid frames through direct link")
155     wt.tdls_clear(bssid, addr1, addr2);
156     dev[1].tdls_teardown(addr2)
157     time.sleep(1)
158     teardown = wt.get_tdls_counter("teardown", bssid, addr1, addr2);
159     if teardown == 0:
160         raise Exception("No TDLS Setup Teardown seen")
161     wt.tdls_clear(bssid, addr1, addr2);
162     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
163     ap_path = wt.get_tdls_counter("valid_ap_path", bssid, addr1, addr2);
164     if ap_path == 0:
165         raise Exception("No valid frames via AP path")
166     direct_link = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
167     if direct_link > 0:
168         raise Exception("Unexpected frames through direct link")
169     idirect_link = wt.get_tdls_counter("invalid_direct_link", bssid, addr1,
170                                        addr2);
171     if idirect_link > 0:
172         raise Exception("Unexpected frames through direct link (invalid)")
173     dev[2].remove_group()
174     dev[1].remove_group()
175     dev[0].remove_group()
176
177 def test_autogo_legacy(dev):
178     """P2P autonomous GO and legacy clients"""
179     res = autogo(dev[0])
180
181     logger.info("Connect P2P client")
182     connect_cli(dev[0], dev[1])
183
184     logger.info("Connect legacy WPS client")
185     pin = dev[2].wps_read_pin()
186     dev[0].p2p_go_authorize_client(pin)
187     dev[2].request("P2P_SET disabled 1")
188     dev[2].dump_monitor()
189     dev[2].request("WPS_PIN any " + pin)
190     ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
191     if ev is None:
192         raise Exception("Association with the GO timed out")
193     status = dev[2].get_status()
194     if status['wpa_state'] != 'COMPLETED':
195         raise Exception("Not fully connected")
196     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
197     dev[2].request("DISCONNECT")
198
199     logger.info("Connect legacy non-WPS client")
200     dev[2].request("FLUSH")
201     dev[2].request("P2P_SET disabled 1")
202     dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
203                    key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
204                    scan_freq=res['freq'])
205     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
206     dev[2].request("DISCONNECT")
207
208     dev[0].remove_group()
209     dev[1].wait_go_ending_session()
210
211 def test_autogo_chan_switch(dev):
212     """P2P autonomous GO switching channels"""
213     autogo(dev[0], freq=2417)
214     connect_cli(dev[0], dev[1])
215     res = dev[0].request("CHAN_SWITCH 5 2422")
216     if "FAIL" in res:
217         # for now, skip test since mac80211_hwsim support is not yet widely
218         # deployed
219         return 'skip'
220     ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
221     if ev is None:
222         raise Exception("CSA finished event timed out")
223     if "freq=2422" not in ev:
224         raise Exception("Unexpected cahnnel in CSA finished event")
225     dev[0].dump_monitor()
226     dev[1].dump_monitor()
227     time.sleep(0.1)
228     hwsim_utils.test_connectivity_p2p(dev[0], dev[1])