tests: Association timeout
[mech_eap.git] / tests / hwsim / test_ap_open.py
1 # Open mode AP tests
2 # Copyright (c) 2014, Qualcomm Atheros, Inc.
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 hostapd
12 import hwsim_utils
13
14 def test_ap_open(dev, apdev):
15     """AP with open mode (no security) configuration"""
16     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
17     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
18     hwsim_utils.test_connectivity(dev[0].ifname, apdev[0]['ifname'])
19     ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
20     if ev is None:
21         raise Exception("No connection event received from hostapd")
22     dev[0].request("DISCONNECT")
23     ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
24     if ev is None:
25         raise Exception("No disconnection event received from hostapd")
26
27 def test_ap_open_packet_loss(dev, apdev):
28     """AP with open mode configuration and large packet loss"""
29     params = { "ssid": "open",
30                "ignore_probe_probability": "0.5",
31                "ignore_auth_probability": "0.5",
32                "ignore_assoc_probability": "0.5",
33                "ignore_reassoc_probability": "0.5" }
34     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
35     for i in range(0, 3):
36         dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
37                        wait_connect=False)
38     for i in range(0, 3):
39         ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
40         if ev is None:
41             raise Exception("Association with the AP timed out")
42
43 def test_ap_open_unknown_action(dev, apdev):
44     """AP with open mode configuration and unknown Action frame"""
45     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
46     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
47     bssid = apdev[0]['bssid']
48     cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
49     if "FAIL" in dev[0].request(cmd):
50         raise Exception("Could not send test Action frame")
51     ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
52     if ev is None:
53         raise Exception("Timeout on MGMT-TX-STATUS")
54     if "result=SUCCESS" not in ev:
55         raise Exception("AP did not ack Action frame")
56
57 def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
58     """Reconnect to open mode AP after inactivity related disconnection"""
59     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
60     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
61     hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
62     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
63     if ev is None:
64         raise Exception("Timeout on disconnection")
65     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=2)
66     if ev is None:
67         raise Exception("Timeout on reconnection")
68
69 def test_ap_open_assoc_timeout(dev, apdev):
70     """AP timing out association"""
71     ssid = "test"
72     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
73     dev[0].scan(freq="2412")
74     hapd.set("ext_mgmt_frame_handling", "1")
75     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
76                    wait_connect=False)
77     for i in range(0, 10):
78         req = hapd.mgmt_rx()
79         if req is None:
80             raise Exception("MGMT RX wait timed out")
81         if req['subtype'] == 11:
82             break
83         req = None
84     if not req:
85         raise Exception("Authentication frame not received")
86
87     resp = {}
88     resp['fc'] = req['fc']
89     resp['da'] = req['sa']
90     resp['sa'] = req['da']
91     resp['bssid'] = req['bssid']
92     resp['payload'] = struct.pack('<HHH', 0, 2, 0)
93     hapd.mgmt_tx(resp)
94
95     assoc = 0
96     for i in range(0, 10):
97         req = hapd.mgmt_rx()
98         if req is None:
99             raise Exception("MGMT RX wait timed out")
100         if req['subtype'] == 0:
101             assoc += 1
102             if assoc == 3:
103                 break
104     if assoc != 3:
105         raise Exception("Association Request frames not received: assoc=%d" % assoc)
106     hapd.set("ext_mgmt_frame_handling", "0")
107     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
108     if ev is None:
109         raise Exception("Timeout on connection")