Updated to hostap_2_6
[mech_eap.git] / libeap / tests / hwsim / test_hostapd_oom.py
1 # hostapd and out-of-memory error paths
2 # Copyright (c) 2015, Jouni Malinen
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 logging
9 logger = logging.getLogger()
10 import time
11
12 import hostapd
13 from utils import HwsimSkip
14
15 def hostapd_oom_loop(apdev, params, start_func="main"):
16     hapd = hostapd.add_ap(apdev[0], { "ssid": "ctrl" })
17
18     count = 0
19     for i in range(1, 1000):
20         if "OK" not in hapd.request("TEST_ALLOC_FAIL %d:%s" % (i, start_func)):
21             raise HwsimSkip("TEST_ALLOC_FAIL not supported")
22         try:
23             hostapd.add_ap(apdev[1], params, timeout=2.5)
24             logger.info("Iteration %d - success" % i)
25             hostapd.remove_bss(apdev[1])
26
27             state = hapd.request('GET_ALLOC_FAIL')
28             logger.info("GET_ALLOC_FAIL: " + state)
29             hapd.request("TEST_ALLOC_FAIL 0:")
30             if i < 3:
31                 raise Exception("AP setup succeeded during out-of-memory")
32             if state.startswith('0:'):
33                 count = 0
34             else:
35                 count += 1
36                 if count == 5:
37                     break
38         except Exception, e:
39             logger.info("Iteration %d - %s" % (i, str(e)))
40
41 @remote_compatible
42 def test_hostapd_oom_open(dev, apdev):
43     """hostapd failing to setup open mode due to OOM"""
44     params = { "ssid": "open" }
45     hostapd_oom_loop(apdev, params)
46
47 def test_hostapd_oom_wpa2_psk(dev, apdev):
48     """hostapd failing to setup WPA2-PSK mode due to OOM"""
49     params = hostapd.wpa2_params(ssid="test", passphrase="12345678")
50     params['wpa_psk_file'] = 'hostapd.wpa_psk'
51     hostapd_oom_loop(apdev, params)
52
53 @remote_compatible
54 def test_hostapd_oom_wpa2_eap(dev, apdev):
55     """hostapd failing to setup WPA2-EAP mode due to OOM"""
56     params = hostapd.wpa2_eap_params(ssid="test")
57     params['acct_server_addr'] = "127.0.0.1"
58     params['acct_server_port'] = "1813"
59     params['acct_server_shared_secret'] = "radius"
60     hostapd_oom_loop(apdev, params)
61
62 @remote_compatible
63 def test_hostapd_oom_wpa2_eap_radius(dev, apdev):
64     """hostapd failing to setup WPA2-EAP mode due to OOM in RADIUS"""
65     params = hostapd.wpa2_eap_params(ssid="test")
66     params['acct_server_addr'] = "127.0.0.1"
67     params['acct_server_port'] = "1813"
68     params['acct_server_shared_secret'] = "radius"
69     hostapd_oom_loop(apdev, params, start_func="accounting_init")
70
71 def test_hostapd_oom_wpa2_psk_connect(dev, apdev):
72     """hostapd failing during WPA2-PSK mode connection due to OOM"""
73     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
74     hapd = hostapd.add_ap(apdev[0], params)
75
76     dev[0].request("SCAN_INTERVAL 1")
77     count = 0
78     for i in range(1, 1000):
79         logger.info("Iteration %d" % i)
80         if "OK" not in hapd.request("TEST_ALLOC_FAIL %d:main" % i):
81             raise HwsimSkip("TEST_ALLOC_FAIL not supported")
82         id = dev[0].connect("test-wpa2-psk", psk="12345678",
83                             scan_freq="2412", wait_connect=False)
84         ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
85                                 "CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=5)
86         if ev is None:
87             logger.info("Timeout while waiting for connection in iteration %d" % i)
88             dev[0].request("REMOVE_NETWORK all")
89             time.sleep(0.1)
90         else:
91             if "CTRL-EVENT-SSID-TEMP-DISABLED" in ev:
92                 logger.info("Re-select to avoid long wait for temp disavle")
93                 dev[0].select_network(id)
94                 dev[0].wait_connected()
95             dev[0].request("REMOVE_NETWORK all")
96             dev[0].wait_disconnected()
97         for i in range(3):
98             dev[i].dump_monitor()
99         hapd.dump_monitor()
100
101         state = hapd.request('GET_ALLOC_FAIL')
102         logger.info("GET_ALLOC_FAIL: " + state)
103         hapd.request("TEST_ALLOC_FAIL 0:")
104         if state.startswith('0:'):
105             count = 0
106         else:
107             count += 1
108             if count == 5:
109                 break
110     dev[0].request("SCAN_INTERVAL 5")
111
112 def test_hostapd_oom_wpa2_eap_connect(dev, apdev, params):
113     """hostapd failing during WPA2-EAP mode connection due to OOM"""
114     if not params['long']:
115         raise HwsimSkip("Skip test case with long duration due to --long not specified")
116     params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
117     params['acct_server_addr'] = "127.0.0.1"
118     params['acct_server_port'] = "1813"
119     params['acct_server_shared_secret'] = "radius"
120     hapd = hostapd.add_ap(apdev[0], params)
121
122     dev[0].request("SCAN_INTERVAL 1")
123     count = 0
124     for i in range(1, 1000):
125         logger.info("Iteration %d" % i)
126         if "OK" not in hapd.request("TEST_ALLOC_FAIL %d:main" % i):
127             raise HwsimSkip("TEST_ALLOC_FAIL not supported")
128         id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
129                             eap="GPSK", identity="gpsk user",
130                             password="abcdefghijklmnop0123456789abcdef",
131                             scan_freq="2412", wait_connect=False)
132         ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
133                                 "CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=5)
134         if ev is None:
135             logger.info("Timeout while waiting for connection in iteration %d" % i)
136             dev[0].request("REMOVE_NETWORK all")
137             time.sleep(0.1)
138         else:
139             if "CTRL-EVENT-SSID-TEMP-DISABLED" in ev:
140                 logger.info("Re-select to avoid long wait for temp disavle")
141                 dev[0].select_network(id)
142                 dev[0].wait_connected()
143             dev[0].request("REMOVE_NETWORK all")
144             dev[0].wait_disconnected()
145         for i in range(3):
146             dev[i].dump_monitor()
147         hapd.dump_monitor()
148
149         state = hapd.request('GET_ALLOC_FAIL')
150         logger.info("GET_ALLOC_FAIL: " + state)
151         hapd.request("TEST_ALLOC_FAIL 0:")
152         if state.startswith('0:'):
153             count = 0
154         else:
155             count += 1
156             if count == 5:
157                 break
158     dev[0].request("SCAN_INTERVAL 5")