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