Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_autoscan.py
1 # autoscan tests
2 # Copyright (c) 2014, 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 time
8 import logging
9 logger = logging.getLogger()
10 import os
11
12 import hostapd
13
14 def test_autoscan_periodic(dev, apdev):
15     """autoscan_periodic"""
16     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "autoscan" })
17
18     try:
19         if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
20             raise Exception("Failed to set autoscan")
21         id = dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
22                             wait_connect=False)
23         times = {}
24         for i in range(0, 3):
25             logger.info("Waiting for scan to start")
26             start = os.times()[4]
27             ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
28             if ev is None:
29                 raise Exception("did not start a scan")
30             stop = os.times()[4]
31             times[i] = stop - start
32             logger.info("Waiting for scan to complete")
33             ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
34             if ev is None:
35                 raise Exception("did not complete a scan")
36         if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
37             raise Exception("Unexpected scan timing: " + str(times))
38
39         # scan some more channels to allow some more time for reseting AUTOSCAN
40         # while a scan is in progress
41         dev[0].set_network(id, "scan_freq", "2412 2437 2462 5180 5200 5220 5240")
42         dev[0].dump_monitor()
43         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
44         if ev is None:
45             raise Exception("did not start a scan")
46         if "OK" not in dev[0].request("AUTOSCAN periodic:2"):
47             raise Exception("Failed to (re)set autoscan")
48         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
49         if ev is None:
50             raise Exception("did not complete a scan")
51     finally:
52         dev[0].request("AUTOSCAN ")
53
54 def test_autoscan_exponential(dev, apdev):
55     """autoscan_exponential"""
56     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "autoscan" })
57
58     try:
59         if "OK" not in dev[0].request("AUTOSCAN exponential:2:10"):
60             raise Exception("Failed to set autoscan")
61         dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
62                        wait_connect=False)
63         times = {}
64         for i in range(0, 3):
65             logger.info("Waiting for scan to start")
66             start = os.times()[4]
67             ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
68             if ev is None:
69                 raise Exception("did not start a scan")
70             stop = os.times()[4]
71             times[i] = stop - start
72             logger.info("Waiting for scan to complete")
73             ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
74             if ev is None:
75                 raise Exception("did not complete a scan")
76         if times[0] > 1 or times[1] < 1 or times[1] > 3 or times[2] < 3 or times[2] > 5:
77             raise Exception("Unexpected scan timing: " + str(times))
78     finally:
79         dev[0].request("AUTOSCAN ")