Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_wpas_wmm_ac.py
1 # Test cases for wpa_supplicant WMM-AC operations
2 # Copyright (c) 2014, Intel Corporation
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 struct
10
11 import hwsim_utils
12 import hostapd
13
14 def add_wmm_ap(apdev, acm_list):
15     params = { "ssid": "wmm_ac",
16                "hw_mode": "g",
17                "channel": "11",
18                "wmm_enabled" : "1"}
19
20     for ac in acm_list:
21         params["wmm_ac_%s_acm" % (ac.lower())] = "1"
22
23     return hostapd.add_ap(apdev['ifname'], params)
24
25 def test_tspec(dev, apdev):
26     """Basic addts/delts tests"""
27     # configure ap with VO and VI requiring admission-control
28     hapd = add_wmm_ap(apdev[0], ["VO", "VI"])
29     dev[0].connect("wmm_ac", key_mgmt="NONE", scan_freq="2462")
30     hwsim_utils.test_connectivity(dev[0], hapd)
31     status = dev[0].request("WMM_AC_STATUS")
32     if "WMM AC is Enabled" not in status:
33         raise Exception("WMM-AC not enabled")
34     if "TSID" in status:
35         raise Exception("Unexpected TSID info")
36     if "BK: acm=0 uapsd=0" not in status:
37         raise Exception("Unexpected BK info" + status)
38     if "BE: acm=0 uapsd=0" not in status:
39         raise Exception("Unexpected BE info" + status)
40     if "VI: acm=1 uapsd=0" not in status:
41         raise Exception("Unexpected VI info" + status)
42     if "VO: acm=1 uapsd=0" not in status:
43         raise Exception("Unexpected VO info" + status)
44
45     # no tsid --> tsid out of range
46     if "FAIL" not in dev[0].request("WMM_AC_ADDTS downlink"):
47         raise Exception("Invalid WMM_AC_ADDTS accepted")
48     # no direction
49     if "FAIL" not in dev[0].request("WMM_AC_ADDTS tsid=5"):
50         raise Exception("Invalid WMM_AC_ADDTS accepted")
51     # param out of range
52     if "FAIL" not in dev[0].request("WMM_AC_ADDTS tsid=5 downlink"):
53         raise Exception("Invalid WMM_AC_ADDTS accepted")
54
55     tsid = 5
56
57     # make sure we fail when the ac is not configured for acm
58     try:
59         dev[0].add_ts(tsid, 3)
60         raise Exception("ADDTS succeeded although it should have failed")
61     except Exception, e:
62         if not str(e).startswith("ADDTS failed"):
63             raise
64     status = dev[0].request("WMM_AC_STATUS")
65     if "TSID" in status:
66         raise Exception("Unexpected TSID info")
67
68     # add tspec for UP=6
69     dev[0].add_ts(tsid, 6)
70     status = dev[0].request("WMM_AC_STATUS")
71     if "TSID" not in status:
72         raise Exception("Missing TSID info")
73
74     # using the same tsid for a different ac is invalid
75     try:
76         dev[0].add_ts(tsid, 5)
77         raise Exception("ADDTS succeeded although it should have failed")
78     except Exception, e:
79         if not str(e).startswith("ADDTS failed"):
80             raise
81
82     # update the tspec for a different UP of the same ac
83     dev[0].add_ts(tsid, 7, extra="fixed_nominal_msdu")
84     dev[0].del_ts(tsid)
85     status = dev[0].request("WMM_AC_STATUS")
86     if "TSID" in status:
87         raise Exception("Unexpected TSID info")
88
89     # verify failure on uplink/bidi without driver support
90     tsid = 6
91     try:
92         dev[0].add_ts(tsid, 7, direction="uplink")
93         raise Exception("ADDTS succeeded although it should have failed")
94     except Exception, e:
95         if not str(e).startswith("ADDTS failed"):
96             raise
97     try:
98         dev[0].add_ts(tsid, 7, direction="bidi")
99         raise Exception("ADDTS succeeded although it should have failed")
100     except Exception, e:
101         if not str(e).startswith("ADDTS failed"):
102             raise
103
104     # attempt to delete non-existing tsid
105     try:
106         dev[0].del_ts(tsid)
107         raise Exception("DELTS succeeded although it should have failed")
108     except Exception, e:
109         if not str(e).startswith("DELTS failed"):
110             raise
111
112 def test_tspec_protocol(dev, apdev):
113     """Protocol tests for addts/delts"""
114     # configure ap with VO and VI requiring admission-control
115     hapd = add_wmm_ap(apdev[0], ["VO", "VI"])
116     dev[0].connect("wmm_ac", key_mgmt="NONE", scan_freq="2462")
117
118     dev[0].dump_monitor()
119     hapd.set("ext_mgmt_frame_handling", "1")
120
121     tsid = 6
122
123     # timeout on ADDTS response
124     dev[0].add_ts(tsid, 7, expect_failure=True)
125
126     hapd.dump_monitor()
127     req = "WMM_AC_ADDTS downlink tsid=6 up=7 nominal_msdu_size=1500 sba=9000 mean_data_rate=1500 min_phy_rate=6000000"
128     if "OK" not in dev[0].request(req):
129         raise Exception("WMM_AC_ADDTS failed")
130     # a new request while previous is still pending
131     if "FAIL" not in dev[0].request(req):
132         raise Exception("WMM_AC_ADDTS accepted while oen was still pending")
133     msg = hapd.mgmt_rx()
134     payload = msg['payload']
135     (categ, action, dialog, status) = struct.unpack('BBBB', payload[0:4])
136     if action != 0:
137         raise Exception("Unexpected Action code: %d" % action)
138
139     msg['da'] = msg['sa']
140     msg['sa'] = apdev[0]['bssid']
141
142     # unexpected dialog token
143     msg['payload'] = struct.pack('BBBB', 17, 1, (dialog + 1) & 0xff, 0) + payload[4:]
144     hapd.mgmt_tx(msg)
145
146     # valid response
147     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 0) + payload[4:]
148     hapd.mgmt_tx(msg)
149     ev = dev[0].wait_event(["TSPEC-ADDED"], timeout=10)
150     if ev is None:
151         raise Exception("Timeout on TSPEC-ADDED")
152     if "tsid=%d" % tsid not in ev:
153         raise Exception("Unexpected TSPEC-ADDED contents: " + ev)
154
155     # duplicated response
156     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 0) + payload[4:]
157     hapd.mgmt_tx(msg)
158
159     # too short ADDTS
160     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 0)
161     hapd.mgmt_tx(msg)
162
163     # invalid IE
164     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 0) + payload[4:] + struct.pack('BB', 0xdd, 100)
165     hapd.mgmt_tx(msg)
166
167     # too short WMM element
168     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 0) + payload[4:] + '\xdd\x06\x00\x50\xf2\x02\x02\x01'
169     hapd.mgmt_tx(msg)
170
171     # DELTS
172     dev[0].dump_monitor()
173     msg['payload'] = struct.pack('BBBB', 17, 2, 0, 0) + payload[4:]
174     hapd.mgmt_tx(msg)
175     ev = dev[0].wait_event(['TSPEC-REMOVED'], timeout=6)
176     if ev is None:
177         raise Exception("Timeout on TSPEC-REMOVED event")
178     if "tsid=%d" % tsid not in ev:
179         raise Exception("Unexpected TSPEC-REMOVED contents: " + ev)
180     # DELTS duplicated
181     msg['payload'] = struct.pack('BBBB', 17, 2, 0, 0) + payload[4:]
182     hapd.mgmt_tx(msg)
183
184     # start a new request
185     hapd.dump_monitor()
186     if "OK" not in dev[0].request(req):
187         raise Exception("WMM_AC_ADDTS failed")
188     msg = hapd.mgmt_rx()
189     payload = msg['payload']
190     (categ, action, dialog, status) = struct.unpack('BBBB', payload[0:4])
191     if action != 0:
192         raise Exception("Unexpected Action code: %d" % action)
193
194     msg['da'] = msg['sa']
195     msg['sa'] = apdev[0]['bssid']
196
197     # modified parameters
198     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 1) + payload[4:12] + struct.pack('B', ord(payload[12]) & ~0x60) + payload[13:]
199     hapd.mgmt_tx(msg)
200
201     # reject request
202     msg['payload'] = struct.pack('BBBB', 17, 1, dialog, 1) + payload[4:]
203     hapd.mgmt_tx(msg)
204     ev = dev[0].wait_event(["TSPEC-REQ-FAILED"], timeout=10)
205     if ev is None:
206         raise Exception("Timeout on TSPEC-REQ-FAILED")
207     if "tsid=%d" % tsid not in ev:
208         raise Exception("Unexpected TSPEC-REQ-FAILED contents: " + ev)
209
210     hapd.set("ext_mgmt_frame_handling", "0")
211
212 def test_tspec_not_enabled(dev, apdev):
213     """addts failing if AP does not support WMM"""
214     params = { "ssid": "wmm_no_ac",
215                "hw_mode": "g",
216                "channel": "11",
217                "wmm_enabled" : "0" }
218     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
219     dev[0].connect("wmm_no_ac", key_mgmt="NONE", scan_freq="2462")
220     status = dev[0].request("WMM_AC_STATUS")
221     if "Not associated to a WMM AP, WMM AC is Disabled" not in status:
222         raise Exception("Unexpected WMM_AC_STATUS: " + status)
223
224     try:
225         dev[0].add_ts(5, 6)
226         raise Exception("ADDTS succeeded although it should have failed")
227     except Exception, e:
228         if not str(e).startswith("ADDTS failed"):
229             raise
230
231     # attempt to delete non-existing tsid
232     try:
233         dev[0].del_ts(5)
234         raise Exception("DELTS succeeded although it should have failed")
235     except Exception, e:
236         if not str(e).startswith("DELTS failed"):
237             raise
238
239     # unexpected Action frame when WMM is disabled
240     MGMT_SUBTYPE_ACTION = 13
241     msg = {}
242     msg['fc'] = MGMT_SUBTYPE_ACTION << 4
243     msg['da'] = dev[0].p2p_interface_addr()
244     msg['sa'] = apdev[0]['bssid']
245     msg['bssid'] = apdev[0]['bssid']
246     msg['payload'] = struct.pack('BBBB', 17, 2, 0, 0)
247     hapd.mgmt_tx(msg)
248
249 def test_tspec_ap_roam_open(dev, apdev):
250     """Roam between two open APs while having tspecs"""
251     hapd0 = add_wmm_ap(apdev[0], ["VO", "VI"])
252     dev[0].connect("wmm_ac", key_mgmt="NONE")
253     hwsim_utils.test_connectivity(dev[0], hapd0)
254     dev[0].add_ts(5, 6)
255
256     hapd1 = add_wmm_ap(apdev[1], ["VO", "VI"])
257     dev[0].scan_for_bss(apdev[1]['bssid'], freq=2462)
258     dev[0].roam(apdev[1]['bssid'])
259     hwsim_utils.test_connectivity(dev[0], hapd1)
260     if dev[0].tspecs():
261         raise Exception("TSPECs weren't deleted on roaming")
262
263     dev[0].scan_for_bss(apdev[0]['bssid'], freq=2462)
264     dev[0].roam(apdev[0]['bssid'])
265     hwsim_utils.test_connectivity(dev[0], hapd0)
266
267 def test_tspec_reassoc(dev, apdev):
268     """Reassociation to same BSS while having tspecs"""
269     hapd0 = add_wmm_ap(apdev[0], ["VO", "VI"])
270     dev[0].connect("wmm_ac", key_mgmt="NONE")
271     hwsim_utils.test_connectivity(dev[0], hapd0)
272     dev[0].add_ts(5, 6)
273     last_tspecs = dev[0].tspecs()
274
275     dev[0].request("REASSOCIATE")
276     dev[0].wait_connected()
277
278     hwsim_utils.test_connectivity(dev[0], hapd0)
279     if dev[0].tspecs() != last_tspecs:
280         raise Exception("TSPECs weren't saved on reassociation")