0da1c7f2a260027ec40d1cd4369c95c313141be4
[mech_eap.git] / tests / hwsim / test_ap_roam.py
1 # Roaming tests
2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 from remotehost import remote_compatible
8 import time
9 import logging
10 logger = logging.getLogger()
11
12 import hwsim_utils
13 import hostapd
14
15 @remote_compatible
16 def test_ap_roam_open(dev, apdev):
17     """Roam between two open APs"""
18     hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
19     dev[0].connect("test-open", key_mgmt="NONE")
20     hwsim_utils.test_connectivity(dev[0], hapd0)
21     hapd1 = hostapd.add_ap(apdev[1], { "ssid": "test-open" })
22     dev[0].scan(type="ONLY")
23     dev[0].roam(apdev[1]['bssid'])
24     hwsim_utils.test_connectivity(dev[0], hapd1)
25     dev[0].roam(apdev[0]['bssid'])
26     hwsim_utils.test_connectivity(dev[0], hapd0)
27
28 @remote_compatible
29 def test_ap_roam_open_failed(dev, apdev):
30     """Roam failure due to rejected authentication"""
31     hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
32     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
33     hwsim_utils.test_connectivity(dev[0], hapd0)
34     params = { "ssid": "test-open", "max_num_sta" : "0" }
35     hapd1 = hostapd.add_ap(apdev[1], params)
36     bssid = hapd1.own_addr()
37
38     dev[0].scan_for_bss(bssid, freq=2412)
39     dev[0].dump_monitor()
40     if "OK" not in dev[0].request("ROAM " + bssid):
41         raise Exception("ROAM failed")
42
43     ev = dev[0].wait_event(["CTRL-EVENT-AUTH-REJECT"], 1)
44     if not ev:
45         raise Exception("CTRL-EVENT-AUTH-REJECT was not seen")
46
47     dev[0].wait_connected(timeout=5)
48     hwsim_utils.test_connectivity(dev[0], hapd0)
49
50 @remote_compatible
51 def test_ap_roam_wpa2_psk(dev, apdev):
52     """Roam between two WPA2-PSK APs"""
53     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
54     hapd0 = hostapd.add_ap(apdev[0], params)
55     dev[0].connect("test-wpa2-psk", psk="12345678")
56     hwsim_utils.test_connectivity(dev[0], hapd0)
57     hapd1 = hostapd.add_ap(apdev[1], params)
58     dev[0].scan(type="ONLY")
59     dev[0].roam(apdev[1]['bssid'])
60     hwsim_utils.test_connectivity(dev[0], hapd1)
61     dev[0].roam(apdev[0]['bssid'])
62     hwsim_utils.test_connectivity(dev[0], hapd0)
63
64 def test_ap_roam_wpa2_psk_failed(dev, apdev, params):
65     """Roam failure with WPA2-PSK AP due to wrong passphrase"""
66     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
67     hapd0 = hostapd.add_ap(apdev[0], params)
68     id = dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
69     hwsim_utils.test_connectivity(dev[0], hapd0)
70     params['wpa_passphrase'] = "22345678"
71     hapd1 = hostapd.add_ap(apdev[1], params)
72     bssid = hapd1.own_addr()
73     dev[0].scan_for_bss(bssid, freq=2412)
74
75     dev[0].dump_monitor()
76     if "OK" not in dev[0].request("ROAM " + bssid):
77         raise Exception("ROAM failed")
78
79     ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED",
80                             "CTRL-EVENT-CONNECTED"], 5)
81     if "CTRL-EVENT-CONNECTED" in ev:
82         raise Exception("Got unexpected CTRL-EVENT-CONNECTED")
83     if "CTRL-EVENT-SSID-TEMP-DISABLED" not in ev:
84         raise Exception("CTRL-EVENT-SSID-TEMP-DISABLED not seen")
85
86     if "OK" not in dev[0].request("SELECT_NETWORK id=" + str(id)):
87         raise Exception("SELECT_NETWORK failed")
88
89     ev = dev[0].wait_event(["CTRL-EVENT-SSID-REENABLED"], 3)
90     if not ev:
91         raise Exception("CTRL-EVENT-SSID-REENABLED not seen")
92
93     dev[0].wait_connected(timeout=5)
94     hwsim_utils.test_connectivity(dev[0], hapd0)
95
96 @remote_compatible
97 def test_ap_reassociation_to_same_bss(dev, apdev):
98     """Reassociate to the same BSS"""
99     hapd = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
100     dev[0].connect("test-open", key_mgmt="NONE")
101
102     dev[0].request("REASSOCIATE")
103     dev[0].wait_connected(timeout=10, error="Reassociation timed out")
104     hwsim_utils.test_connectivity(dev[0], hapd)
105
106     dev[0].request("REATTACH")
107     dev[0].wait_connected(timeout=10, error="Reattach timed out")
108     hwsim_utils.test_connectivity(dev[0], hapd)
109
110     # Wait for previous scan results to expire to trigger new scan
111     time.sleep(5)
112     dev[0].request("REATTACH")
113     dev[0].wait_connected(timeout=10, error="Reattach timed out")
114     hwsim_utils.test_connectivity(dev[0], hapd)
115
116 @remote_compatible
117 def test_ap_roam_set_bssid(dev, apdev):
118     """Roam control"""
119     hostapd.add_ap(apdev[0], { "ssid": "test-open" })
120     hostapd.add_ap(apdev[1], { "ssid": "test-open" })
121     id = dev[0].connect("test-open", key_mgmt="NONE", bssid=apdev[1]['bssid'],
122                         scan_freq="2412")
123     if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
124         raise Exception("Unexpected BSS")
125     # for now, these are just verifying that the code path to indicate
126     # within-ESS roaming changes can be executed; the actual results of those
127     # operations are not currently verified (that would require a test driver
128     # that does BSS selection)
129     dev[0].set_network(id, "bssid", "")
130     dev[0].set_network(id, "bssid", apdev[0]['bssid'])
131     dev[0].set_network(id, "bssid", apdev[1]['bssid'])
132
133 @remote_compatible
134 def test_ap_roam_wpa2_psk_race(dev, apdev):
135     """Roam between two WPA2-PSK APs and try to hit a disconnection race"""
136     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
137     hapd0 = hostapd.add_ap(apdev[0], params)
138     dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
139     hwsim_utils.test_connectivity(dev[0], hapd0)
140
141     params['channel'] = '2'
142     hapd1 = hostapd.add_ap(apdev[1], params)
143     dev[0].scan_for_bss(apdev[1]['bssid'], freq=2417)
144     dev[0].roam(apdev[1]['bssid'])
145     hwsim_utils.test_connectivity(dev[0], hapd1)
146     dev[0].roam(apdev[0]['bssid'])
147     hwsim_utils.test_connectivity(dev[0], hapd0)
148     # Wait at least two seconds to trigger the previous issue with the
149     # disconnection callback.
150     for i in range(3):
151         time.sleep(0.8)
152         hwsim_utils.test_connectivity(dev[0], hapd0)