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