Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_bgscan.py
1 # bgscan 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_bgscan_simple(dev, apdev):
15     """bgscan_simple"""
16     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "bgscan" })
17     hostapd.add_ap(apdev[1]['ifname'], { "ssid": "bgscan" })
18
19     dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
20                    bgscan="simple:1:-20:2")
21     dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
22                    bgscan="simple:1:-45:2")
23
24     dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
25                    bgscan="simple:1:-45")
26     dev[2].request("REMOVE_NETWORK all")
27     dev[2].wait_disconnected()
28
29     dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
30                    bgscan="simple:0:0")
31     dev[2].request("REMOVE_NETWORK all")
32     dev[2].wait_disconnected()
33
34     dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
35                    bgscan="simple")
36     dev[2].request("REMOVE_NETWORK all")
37     dev[2].wait_disconnected()
38
39     dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
40                    bgscan="simple:1")
41     dev[2].request("REMOVE_NETWORK all")
42     dev[2].wait_disconnected()
43
44     ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
45     if ev is None:
46         raise Exception("dev0 did not indicate signal change event")
47     if "above=0" not in ev:
48         raise Exception("Unexpected signal change event contents from dev0: " + ev)
49
50     ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
51     if ev is None:
52         raise Exception("dev1 did not indicate signal change event")
53     if "above=1" not in ev:
54         raise Exception("Unexpected signal change event contents from dev1: " + ev)
55
56     ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
57     if ev is None:
58         raise Exception("dev0 did not start a scan")
59
60     ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
61     if ev is None:
62         raise Exception("dev1 did not start a scan")
63
64     ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
65     if ev is None:
66         raise Exception("dev0 did not complete a scan")
67     ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
68     if ev is None:
69         raise Exception("dev1 did not complete a scan")
70
71 def test_bgscan_learn(dev, apdev):
72     """bgscan_learn"""
73     hostapd.add_ap(apdev[0]['ifname'], { "ssid": "bgscan" })
74     hostapd.add_ap(apdev[1]['ifname'], { "ssid": "bgscan" })
75
76     try:
77         os.remove("/tmp/test_bgscan_learn.bgscan")
78     except:
79         pass
80
81     try:
82         dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
83                        bgscan="learn:1:-20:2")
84         id = dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
85                             bgscan="learn:1:-45:2:/tmp/test_bgscan_learn.bgscan")
86
87         dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
88                        bgscan="learn:1:-45")
89         dev[2].request("REMOVE_NETWORK all")
90         dev[2].wait_disconnected()
91
92         dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
93                        bgscan="learn:0:0")
94         dev[2].request("REMOVE_NETWORK all")
95         dev[2].wait_disconnected()
96
97         dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
98                        bgscan="learn")
99         dev[2].request("REMOVE_NETWORK all")
100         dev[2].wait_disconnected()
101
102         dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
103                        bgscan="learn:1")
104         dev[2].request("REMOVE_NETWORK all")
105         dev[2].wait_disconnected()
106
107         ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
108         if ev is None:
109             raise Exception("dev0 did not indicate signal change event")
110         if "above=0" not in ev:
111             raise Exception("Unexpected signal change event contents from dev0: " + ev)
112
113         ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
114         if ev is None:
115             raise Exception("dev1 did not indicate signal change event")
116         if "above=1" not in ev:
117             raise Exception("Unexpected signal change event contents from dev1: " + ev)
118
119         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
120         if ev is None:
121             raise Exception("dev0 did not start a scan")
122
123         ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
124         if ev is None:
125             raise Exception("dev1 did not start a scan")
126
127         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
128         if ev is None:
129             raise Exception("dev0 did not complete a scan")
130         ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
131         if ev is None:
132             raise Exception("dev1 did not complete a scan")
133
134         dev[0].request("DISCONNECT")
135         dev[1].request("DISCONNECT")
136         dev[0].request("REMOVE_NETWORK all")
137
138         with open("/tmp/test_bgscan_learn.bgscan", "r") as f:
139             lines = f.read().splitlines()
140         if lines[0] != "wpa_supplicant-bgscan-learn":
141             raise Exception("Unexpected bgscan header line")
142         if 'BSS 02:00:00:00:03:00 2412' not in lines:
143             raise Exception("Missing BSS1")
144         if 'BSS 02:00:00:00:04:00 2412' not in lines:
145             raise Exception("Missing BSS2")
146         if 'NEIGHBOR 02:00:00:00:03:00 02:00:00:00:04:00' not in lines:
147             raise Exception("Missing BSS1->BSS2 neighbor entry")
148         if 'NEIGHBOR 02:00:00:00:04:00 02:00:00:00:03:00' not in lines:
149             raise Exception("Missing BSS2->BSS1 neighbor entry")
150
151         dev[1].set_network(id, "scan_freq", "")
152         dev[1].connect_network(id)
153
154         ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=10)
155         if ev is None:
156             raise Exception("dev1 did not start a scan")
157
158         ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
159         if ev is None:
160             raise Exception("dev1 did not complete a scan")
161
162         dev[1].request("REMOVE_NETWORK all")
163     finally:
164         try:
165             os.remove("/tmp/test_bgscan_learn.bgscan")
166         except:
167             pass