Updated to hostap_2_6
[mech_eap.git] / libeap / tests / hwsim / test_ap_csa.py
1 # AP CSA tests
2 # Copyright (c) 2013, Luciano Coelho <luciano.coelho@intel.com>
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 from utils import HwsimSkip
15
16 def connect(dev, apdev, **kwargs):
17     params = { "ssid": "ap-csa",
18                "channel": "1" }
19     params.update(kwargs)
20     ap = hostapd.add_ap(apdev[0], params)
21     dev.connect("ap-csa", key_mgmt="NONE", scan_freq="2412")
22     return ap
23
24 def switch_channel(ap, count, freq):
25     ap.request("CHAN_SWITCH " + str(count) + " " + str(freq))
26     ev = ap.wait_event(["AP-CSA-FINISHED"], timeout=10)
27     if ev is None:
28         raise Exception("CSA finished event timed out")
29     if "freq=" + str(freq) not in ev:
30         raise Exception("Unexpected channel in CSA finished event")
31
32 def wait_channel_switch(dev, freq):
33     ev = dev.wait_event(["CTRL-EVENT-CHANNEL-SWITCH"], timeout=5)
34     if ev is None:
35         raise Exception("Channel switch not reported")
36     if "freq=%d" % freq not in ev:
37         raise Exception("Unexpected frequency: " + ev)
38
39 # This function checks whether the provided dev, which may be either
40 # WpaSupplicant or Hostapd supports CSA.
41 def csa_supported(dev):
42     res = dev.get_driver_status()
43     if (int(res['capa.flags'], 0) & 0x80000000) == 0:
44         raise HwsimSkip("CSA not supported")
45
46 @remote_compatible
47 def test_ap_csa_1_switch(dev, apdev):
48     """AP Channel Switch, one switch"""
49     csa_supported(dev[0])
50     ap = connect(dev[0], apdev)
51
52     hwsim_utils.test_connectivity(dev[0], ap)
53     switch_channel(ap, 10, 2462)
54     wait_channel_switch(dev[0], 2462)
55     hwsim_utils.test_connectivity(dev[0], ap)
56
57 @remote_compatible
58 def test_ap_csa_2_switches(dev, apdev):
59     """AP Channel Switch, two switches"""
60     csa_supported(dev[0])
61     ap = connect(dev[0], apdev)
62
63     hwsim_utils.test_connectivity(dev[0], ap)
64     switch_channel(ap, 10, 2462)
65     wait_channel_switch(dev[0], 2462)
66     hwsim_utils.test_connectivity(dev[0], ap)
67     switch_channel(ap, 10, 2412)
68     wait_channel_switch(dev[0], 2412)
69     hwsim_utils.test_connectivity(dev[0], ap)
70
71 @remote_compatible
72 def test_ap_csa_1_switch_count_0(dev, apdev):
73     """AP Channel Switch, one switch with count 0"""
74     csa_supported(dev[0])
75     ap = connect(dev[0], apdev)
76
77     hwsim_utils.test_connectivity(dev[0], ap)
78     switch_channel(ap, 0, 2462)
79     # this does not result in CSA currently, so do not bother checking
80     # connectivity
81
82 @remote_compatible
83 def test_ap_csa_2_switches_count_0(dev, apdev):
84     """AP Channel Switch, two switches with count 0"""
85     csa_supported(dev[0])
86     ap = connect(dev[0], apdev)
87
88     hwsim_utils.test_connectivity(dev[0], ap)
89     switch_channel(ap, 0, 2462)
90     # this does not result in CSA currently, so do not bother checking
91     # connectivity
92     switch_channel(ap, 0, 2412)
93     # this does not result in CSA currently, so do not bother checking
94     # connectivity
95
96 @remote_compatible
97 def test_ap_csa_1_switch_count_1(dev, apdev):
98     """AP Channel Switch, one switch with count 1"""
99     csa_supported(dev[0])
100     ap = connect(dev[0], apdev)
101
102     hwsim_utils.test_connectivity(dev[0], ap)
103     switch_channel(ap, 1, 2462)
104     # this does not result in CSA currently, so do not bother checking
105     # connectivity
106
107 @remote_compatible
108 def test_ap_csa_2_switches_count_1(dev, apdev):
109     """AP Channel Switch, two switches with count 1"""
110     csa_supported(dev[0])
111     ap = connect(dev[0], apdev)
112
113     hwsim_utils.test_connectivity(dev[0], ap)
114     switch_channel(ap, 1, 2462)
115     # this does not result in CSA currently, so do not bother checking
116     # connectivity
117     switch_channel(ap, 1, 2412)
118     # this does not result in CSA currently, so do not bother checking
119     # connectivity
120
121 @remote_compatible
122 def test_ap_csa_1_switch_count_2(dev, apdev):
123     """AP Channel Switch, one switch with count 2"""
124     csa_supported(dev[0])
125     ap = connect(dev[0], apdev)
126
127     hwsim_utils.test_connectivity(dev[0], ap)
128     switch_channel(ap, 2, 2462)
129     wait_channel_switch(dev[0], 2462)
130     hwsim_utils.test_connectivity(dev[0], ap)
131
132 @remote_compatible
133 def test_ap_csa_ecsa_only(dev, apdev):
134     """AP Channel Switch, one switch with only ECSA IE"""
135     csa_supported(dev[0])
136     ap = connect(dev[0], apdev, ecsa_ie_only="1")
137
138     hwsim_utils.test_connectivity(dev[0], ap)
139     switch_channel(ap, 10, 2462)
140     wait_channel_switch(dev[0], 2462)
141     hwsim_utils.test_connectivity(dev[0], ap)
142
143 @remote_compatible
144 def test_ap_csa_invalid(dev, apdev):
145     """AP Channel Switch - invalid channel"""
146     csa_supported(dev[0])
147     ap = connect(dev[0], apdev)
148
149     vals = [ 2461, 4900, 4901, 5181, 5746, 5699, 5895, 5899 ]
150     for val in vals:
151         if "FAIL" not in ap.request("CHAN_SWITCH 1 %d" % val):
152             raise Exception("Invalid channel accepted: %d" % val)