Updated to hostap_2_6
[mech_eap.git] / libeap / tests / hwsim / test_ap_acs.py
1 # Test cases for automatic channel selection with hostapd
2 # Copyright (c) 2013-2015, 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 logging
8 logger = logging.getLogger()
9 import time
10
11 import hostapd
12 from utils import skip_with_fips
13 from test_ap_ht import clear_scan_cache
14
15 def force_prev_ap_on_24g(ap):
16     # For now, make sure the last operating channel was on 2.4 GHz band to get
17     # sufficient survey data from mac80211_hwsim.
18     hostapd.add_ap(ap, { "ssid": "open" })
19     time.sleep(0.1)
20     hostapd.remove_bss(ap)
21
22 def force_prev_ap_on_5g(ap):
23     # For now, make sure the last operating channel was on 5 GHz band to get
24     # sufficient survey data from mac80211_hwsim.
25     hostapd.add_ap(ap, { "ssid": "open", "hw_mode": "a",
26                          "channel": "36", "country_code": "US" })
27     time.sleep(0.1)
28     hostapd.remove_bss(ap)
29
30 def wait_acs(hapd):
31     ev = hapd.wait_event(["ACS-STARTED", "ACS-COMPLETED", "ACS-FAILED",
32                           "AP-ENABLED", "AP-DISABLED"], timeout=5)
33     if not ev:
34         raise Exception("ACS start timed out")
35     if "ACS-STARTED" not in ev:
36         raise Exception("Unexpected ACS event: " + ev)
37
38     state = hapd.get_status_field("state")
39     if state != "ACS":
40         raise Exception("Unexpected interface state")
41
42     ev = hapd.wait_event(["ACS-COMPLETED", "ACS-FAILED", "AP-ENABLED",
43                           "AP-DISABLED"], timeout=20)
44     if not ev:
45         raise Exception("ACS timed out")
46     if "ACS-COMPLETED" not in ev:
47         raise Exception("Unexpected ACS event: " + ev)
48
49     ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=5)
50     if not ev:
51         raise Exception("AP setup timed out")
52     if "AP-ENABLED" not in ev:
53         raise Exception("Unexpected ACS event: " + ev)
54
55     state = hapd.get_status_field("state")
56     if state != "ENABLED":
57         raise Exception("Unexpected interface state")
58
59 def test_ap_acs(dev, apdev):
60     """Automatic channel selection"""
61     force_prev_ap_on_24g(apdev[0])
62     params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
63     params['channel'] = '0'
64     hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
65     wait_acs(hapd)
66
67     freq = hapd.get_status_field("freq")
68     if int(freq) < 2400:
69         raise Exception("Unexpected frequency")
70
71     dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
72
73 def test_ap_acs_chanlist(dev, apdev):
74     """Automatic channel selection with chanlist set"""
75     force_prev_ap_on_24g(apdev[0])
76     params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
77     params['channel'] = '0'
78     params['chanlist'] = '1 6 11'
79     hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
80     wait_acs(hapd)
81
82     freq = hapd.get_status_field("freq")
83     if int(freq) < 2400:
84         raise Exception("Unexpected frequency")
85
86     dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
87
88 def test_ap_multi_bss_acs(dev, apdev):
89     """hostapd start with a multi-BSS configuration file using ACS"""
90     skip_with_fips(dev[0])
91     force_prev_ap_on_24g(apdev[0])
92
93     # start the actual test
94     hapd = hostapd.add_iface(apdev[0], 'multi-bss-acs.conf')
95     hapd.enable()
96     wait_acs(hapd)
97
98     freq = hapd.get_status_field("freq")
99     if int(freq) < 2400:
100         raise Exception("Unexpected frequency")
101
102     dev[0].connect("bss-1", key_mgmt="NONE", scan_freq=freq)
103     dev[1].connect("bss-2", psk="12345678", scan_freq=freq)
104     dev[2].connect("bss-3", psk="qwertyuiop", scan_freq=freq)
105
106 def test_ap_acs_40mhz(dev, apdev):
107     """Automatic channel selection for 40 MHz channel"""
108     clear_scan_cache(apdev[0])
109     force_prev_ap_on_24g(apdev[0])
110     params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
111     params['channel'] = '0'
112     params['ht_capab'] = '[HT40+]'
113     hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
114     wait_acs(hapd)
115
116     freq = hapd.get_status_field("freq")
117     if int(freq) < 2400:
118         raise Exception("Unexpected frequency")
119     sec = hapd.get_status_field("secondary_channel")
120     if int(sec) == 0:
121         raise Exception("Secondary channel not set")
122
123     dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
124
125 def test_ap_acs_5ghz(dev, apdev):
126     """Automatic channel selection on 5 GHz"""
127     try:
128         hapd = None
129         force_prev_ap_on_5g(apdev[0])
130         params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
131         params['hw_mode'] = 'a'
132         params['channel'] = '0'
133         params['country_code'] = 'US'
134         hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
135         wait_acs(hapd)
136         freq = hapd.get_status_field("freq")
137         if int(freq) < 5000:
138             raise Exception("Unexpected frequency")
139
140         dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
141
142     finally:
143         dev[0].request("DISCONNECT")
144         if hapd:
145             hapd.request("DISABLE")
146         hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
147         dev[0].flush_scan_cache()
148
149 def test_ap_acs_5ghz_40mhz(dev, apdev):
150     """Automatic channel selection on 5 GHz for 40 MHz channel"""
151     try:
152         hapd = None
153         force_prev_ap_on_5g(apdev[0])
154         params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
155         params['hw_mode'] = 'a'
156         params['channel'] = '0'
157         params['ht_capab'] = '[HT40+]'
158         params['country_code'] = 'US'
159         hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
160         wait_acs(hapd)
161         freq = hapd.get_status_field("freq")
162         if int(freq) < 5000:
163             raise Exception("Unexpected frequency")
164
165         sec = hapd.get_status_field("secondary_channel")
166         if int(sec) == 0:
167             raise Exception("Secondary channel not set")
168
169         dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
170
171     finally:
172         dev[0].request("DISCONNECT")
173         if hapd:
174             hapd.request("DISABLE")
175         hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
176         dev[0].flush_scan_cache()
177
178 def test_ap_acs_vht(dev, apdev):
179     """Automatic channel selection for VHT"""
180     try:
181         hapd = None
182         force_prev_ap_on_5g(apdev[0])
183         params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
184         params['hw_mode'] = 'a'
185         params['channel'] = '0'
186         params['ht_capab'] = '[HT40+]'
187         params['country_code'] = 'US'
188         params['ieee80211ac'] = '1'
189         params['vht_oper_chwidth'] = '1'
190         hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
191         wait_acs(hapd)
192         freq = hapd.get_status_field("freq")
193         if int(freq) < 5000:
194             raise Exception("Unexpected frequency")
195
196         sec = hapd.get_status_field("secondary_channel")
197         if int(sec) == 0:
198             raise Exception("Secondary channel not set")
199
200         dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
201
202     finally:
203         dev[0].request("DISCONNECT")
204         if hapd:
205             hapd.request("DISABLE")
206         hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
207         dev[0].flush_scan_cache()
208
209 def test_ap_acs_bias(dev, apdev):
210     """Automatic channel selection with bias values"""
211     force_prev_ap_on_24g(apdev[0])
212     params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
213     params['channel'] = '0'
214     params['acs_chan_bias'] = '1:0.8 3:1.2 6:0.7 11:0.8'
215     hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
216     wait_acs(hapd)
217
218     freq = hapd.get_status_field("freq")
219     if int(freq) < 2400:
220         raise Exception("Unexpected frequency")
221
222     dev[0].connect("test-acs", psk="12345678", scan_freq=freq)