blob: d16c5c63f146538015a4972cb4a3d0aa3b0316b9 [file] [log] [blame]
Wade Guthriea60a11c2013-04-12 17:47:34 -07001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/scan_session.h"
6
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -07007#include <errno.h>
8
Wade Guthriea60a11c2013-04-12 17:47:34 -07009#include <limits>
10#include <set>
11#include <vector>
12
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070013#include <base/memory/weak_ptr.h>
Wade Guthriea60a11c2013-04-12 17:47:34 -070014#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070017#include "shill/mock_event_dispatcher.h"
Wade Guthriea60a11c2013-04-12 17:47:34 -070018#include "shill/mock_netlink_manager.h"
19#include "shill/netlink_manager.h"
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070020#include "shill/netlink_message_matchers.h"
21#include "shill/nl80211_message.h"
Wade Guthriea60a11c2013-04-12 17:47:34 -070022
23using std::set;
24using std::vector;
25using testing::_;
26using testing::ContainerEq;
Wade Guthriea60a11c2013-04-12 17:47:34 -070027using testing::Test;
28
29namespace shill {
30
31static const uint16_t kExpectedFreq5640 = 5640;
32static const uint16_t kExpectedFreq5600 = 5600;
33static const uint16_t kExpectedFreq5580 = 5580;
34static const uint16_t kExpectedFreq5560 = 5560;
35static const uint16_t kExpectedFreq5620 = 5620;
36
37static WiFiProvider::FrequencyCount kConnectedFrequencies[] = {
38 WiFiProvider::FrequencyCount(kExpectedFreq5640, 40), // 40th percentile.
39 WiFiProvider::FrequencyCount(kExpectedFreq5600, 25), // 65th percentile.
40 WiFiProvider::FrequencyCount(kExpectedFreq5580, 20), // 85th percentile.
41 WiFiProvider::FrequencyCount(kExpectedFreq5560, 10), // 95th percentile.
42 WiFiProvider::FrequencyCount(kExpectedFreq5620, 5) // 100th percentile.
43};
44
45static const uint16_t kExpectedFreq2432 = 2432;
46static const uint16_t kExpectedFreq2427 = 2427;
47static const uint16_t kExpectedFreq2422 = 2422;
48static const uint16_t kExpectedFreq2417 = 2417;
49static const uint16_t kExpectedFreq2412 = 2412;
50
51static uint16_t kUnconnectedFrequencies[] = {
52 kExpectedFreq2432,
53 kExpectedFreq2427,
54 kExpectedFreq2422,
55 kExpectedFreq2417,
56 kExpectedFreq2412
57};
58
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070059static const uint16_t kNl80211FamilyId = 0x13;
60
Wade Guthriea60a11c2013-04-12 17:47:34 -070061class ScanSessionTest : public Test {
62 public:
63 // Test set of "all the other frequencies this device can support" in
64 // sorted order.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070065 ScanSessionTest() : weak_ptr_factory_(this) {
Wade Guthrieb9c3feb2013-04-25 16:31:19 -070066 WiFiProvider::FrequencyCountList connected_frequencies(
Wade Guthriea60a11c2013-04-12 17:47:34 -070067 kConnectedFrequencies,
68 kConnectedFrequencies + arraysize(kConnectedFrequencies));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -070069
70 set<uint16_t> unconnected_frequencies(
71 kUnconnectedFrequencies,
72 kUnconnectedFrequencies + arraysize(kUnconnectedFrequencies));
73 const int kArbitraryMinimum = 1;
74 const int kArbitraryMaximum = std::numeric_limits<int>::max();
Wade Guthrieb9c3feb2013-04-25 16:31:19 -070075 scan_session_.reset(new ScanSession(&netlink_manager_,
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070076 &dispatcher_,
Wade Guthrieb9c3feb2013-04-25 16:31:19 -070077 connected_frequencies,
78 unconnected_frequencies,
79 0,
80 ScanSession::FractionList(),
81 kArbitraryMinimum,
82 kArbitraryMaximum,
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070083 Bind(&ScanSessionTest::OnScanError,
Wade Guthrief22681f2013-05-31 11:46:31 -070084 weak_ptr_factory_.GetWeakPtr()),
85 NULL));
Wade Guthriea60a11c2013-04-12 17:47:34 -070086 }
87
Wade Guthrieb9c3feb2013-04-25 16:31:19 -070088 virtual std::vector<uint16_t> GetScanFrequencies(float scan_fraction,
89 size_t min_frequencies,
90 size_t max_frequencies) {
91 return scan_session_->GetScanFrequencies(scan_fraction, min_frequencies,
92 max_frequencies);
93 }
94 ScanSession *scan_session() { return scan_session_.get(); }
95
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -070096 void SetScanSize(size_t min_frequencies, size_t max_frequencies) {
97 scan_session_->min_frequencies_ = min_frequencies;
98 scan_session_->max_frequencies_ = max_frequencies;
99 }
100
101 size_t GetScanFrequencyCount() {
102 return arraysize(kConnectedFrequencies) +
103 arraysize(kUnconnectedFrequencies);
104 }
105
106 protected:
107 MOCK_METHOD0(OnScanError, void());
108 MockNetlinkManager *netlink_manager() { return &netlink_manager_; }
109 MockEventDispatcher *dispatcher() { return &dispatcher_; }
110
111 MockEventDispatcher dispatcher_;
Wade Guthriea60a11c2013-04-12 17:47:34 -0700112 MockNetlinkManager netlink_manager_;
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700113 scoped_ptr<ScanSession> scan_session_;
114 base::WeakPtrFactory<ScanSessionTest> weak_ptr_factory_;
Wade Guthriea60a11c2013-04-12 17:47:34 -0700115};
116
117// Test that we can get a bunch of frequencies up to a specified fraction.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700118TEST_F(ScanSessionTest, Fraction) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700119 vector<uint16_t> result;
120
121 // Get the first 83% of the connected values.
122 {
123 vector<uint16_t> expected;
124 expected.push_back(kExpectedFreq5640);
125 expected.push_back(kExpectedFreq5600);
126 expected.push_back(kExpectedFreq5580);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700127 result = GetScanFrequencies(.83, 1, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700128 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700129 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700130 }
131
132 // Get the next 4 values.
133 {
134 vector<uint16_t> expected;
135 expected.push_back(kExpectedFreq5560);
136 expected.push_back(kExpectedFreq5620);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700137 expected.push_back(kExpectedFreq2412);
138 expected.push_back(kExpectedFreq2417);
139 result = GetScanFrequencies(ScanSession::kAllFrequencies, 1, 4);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700140 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700141 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700142 }
143
144 // And, get the remaining list.
145 {
146 vector<uint16_t> expected;
147 expected.push_back(kExpectedFreq2422);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700148 expected.push_back(kExpectedFreq2427);
149 expected.push_back(kExpectedFreq2432);
150 result = GetScanFrequencies(ScanSession::kAllFrequencies, 20,
151 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700152 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700153 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700154 }
155}
156
157// Test that we can get a bunch of frequencies up to a specified fraction,
158// followed by another group up to a specified fraction.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700159TEST_F(ScanSessionTest, TwoFractions) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700160 vector<uint16_t> result;
161
162 // Get the first 60% of the connected values.
163 {
164 vector<uint16_t> expected;
165 expected.push_back(kExpectedFreq5640);
166 expected.push_back(kExpectedFreq5600);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700167 result = GetScanFrequencies(.60, 0, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700168 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700169 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700170 }
171
172 // Get the next 32% of the connected values.
173 {
174 vector<uint16_t> expected;
175 expected.push_back(kExpectedFreq5580);
176 expected.push_back(kExpectedFreq5560);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700177 result = GetScanFrequencies(.32, 0, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700178 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700179 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700180 }
181
182 // And, get the remaining list.
183 {
184 vector<uint16_t> expected;
185 expected.push_back(kExpectedFreq5620);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700186 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700187 expected.push_back(kExpectedFreq2417);
188 expected.push_back(kExpectedFreq2422);
189 expected.push_back(kExpectedFreq2427);
190 expected.push_back(kExpectedFreq2432);
191 result = GetScanFrequencies(ScanSession::kAllFrequencies,
192 std::numeric_limits<size_t>::max(),
193 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700194 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700195 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700196 }
197}
198
199// Test that we can get a bunch of frequencies up to a minimum count, even
200// when the requested fraction has already been reached.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700201TEST_F(ScanSessionTest, Min) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700202 vector<uint16_t> result;
203
204 // Get the first 3 previously seen values.
205 {
206 vector<uint16_t> expected;
207 expected.push_back(kExpectedFreq5640);
208 expected.push_back(kExpectedFreq5600);
209 expected.push_back(kExpectedFreq5580);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700210 result = GetScanFrequencies(.30, 3, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700211 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700212 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700213 }
214
215 // Get the next value by requensting a minimum of 1.
216 {
217 vector<uint16_t> expected;
218 expected.push_back(kExpectedFreq5560);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700219 result = GetScanFrequencies(0.0, 1, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700220 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700221 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700222 }
223
224 // And, get the remaining list.
225 {
226 vector<uint16_t> expected;
227 expected.push_back(kExpectedFreq5620);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700228 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700229 expected.push_back(kExpectedFreq2417);
230 expected.push_back(kExpectedFreq2422);
231 expected.push_back(kExpectedFreq2427);
232 expected.push_back(kExpectedFreq2432);
233 result = GetScanFrequencies(ScanSession::kAllFrequencies,
234 std::numeric_limits<size_t>::max(),
235 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700236 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700237 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700238 }
239}
240
241// Test that we can get up to a specified maximum number of frequencies.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700242TEST_F(ScanSessionTest, Max) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700243 vector<uint16_t> result;
244
245 // Get the first 7 values (crosses seen/unseen boundary).
246 {
247 vector<uint16_t> expected;
248 expected.push_back(kExpectedFreq5640);
249 expected.push_back(kExpectedFreq5600);
250 expected.push_back(kExpectedFreq5580);
251 expected.push_back(kExpectedFreq5560);
252 expected.push_back(kExpectedFreq5620);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700253 expected.push_back(kExpectedFreq2412);
254 expected.push_back(kExpectedFreq2417);
255 result = GetScanFrequencies(ScanSession::kAllFrequencies, 1, 7);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700256 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700257 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700258 }
259
260 // And, get the remaining list.
261 {
262 vector<uint16_t> expected;
263 expected.push_back(kExpectedFreq2422);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700264 expected.push_back(kExpectedFreq2427);
265 expected.push_back(kExpectedFreq2432);
266 result = GetScanFrequencies(ScanSession::kAllFrequencies, 20,
267 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700268 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700269 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700270 }
271}
272
273// Test that we can get exactly the seen frequencies and exactly the unseen
274// ones.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700275TEST_F(ScanSessionTest, Exact) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700276 vector<uint16_t> result;
277
278 // Get the first 5 values -- exectly on the seen/unseen border.
279 {
280 vector<uint16_t> expected;
281 expected.push_back(kExpectedFreq5640);
282 expected.push_back(kExpectedFreq5600);
283 expected.push_back(kExpectedFreq5580);
284 expected.push_back(kExpectedFreq5560);
285 expected.push_back(kExpectedFreq5620);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700286 result = GetScanFrequencies(ScanSession::kAllFrequencies, 5, 5);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700287 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700288 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700289 }
290
291 // And, get the last 5.
292 {
293 vector<uint16_t> expected;
Wade Guthriea60a11c2013-04-12 17:47:34 -0700294 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700295 expected.push_back(kExpectedFreq2417);
296 expected.push_back(kExpectedFreq2422);
297 expected.push_back(kExpectedFreq2427);
298 expected.push_back(kExpectedFreq2432);
299 result = GetScanFrequencies(ScanSession::kAllFrequencies, 5, 5);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700300 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700301 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700302 }
303}
304
305// Test that we can get everything in one read.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700306TEST_F(ScanSessionTest, AllOneRead) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700307 vector<uint16_t> expected;
308 expected.push_back(kExpectedFreq5640);
309 expected.push_back(kExpectedFreq5600);
310 expected.push_back(kExpectedFreq5580);
311 expected.push_back(kExpectedFreq5560);
312 expected.push_back(kExpectedFreq5620);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700313 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700314 expected.push_back(kExpectedFreq2417);
315 expected.push_back(kExpectedFreq2422);
316 expected.push_back(kExpectedFreq2427);
317 expected.push_back(kExpectedFreq2432);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700318 vector<uint16_t> result;
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700319 result = GetScanFrequencies(ScanSession::kAllFrequencies,
320 std::numeric_limits<size_t>::max(),
321 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700322 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700323 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700324}
325
326// Test that we can get all the previously seen frequencies (and only the
327// previously seen frequencies) via the requested fraction.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700328TEST_F(ScanSessionTest, EverythingFraction) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700329 vector<uint16_t> result;
330
331 // Get the first 100% of the connected values.
332 {
333 vector<uint16_t> expected;
334 expected.push_back(kExpectedFreq5640);
335 expected.push_back(kExpectedFreq5600);
336 expected.push_back(kExpectedFreq5580);
337 expected.push_back(kExpectedFreq5560);
338 expected.push_back(kExpectedFreq5620);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700339 result = GetScanFrequencies(1.0, 0, std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700340 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700341 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700342 }
343
344 // And, get the remaining list.
345 {
346 vector<uint16_t> expected;
Wade Guthriea60a11c2013-04-12 17:47:34 -0700347 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700348 expected.push_back(kExpectedFreq2417);
349 expected.push_back(kExpectedFreq2422);
350 expected.push_back(kExpectedFreq2427);
351 expected.push_back(kExpectedFreq2432);
352 result = GetScanFrequencies(ScanSession::kAllFrequencies,
353 std::numeric_limits<size_t>::max(),
354 std::numeric_limits<size_t>::max());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700355 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700356 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700357 }
358}
359
360// Test that we can get each value individually.
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700361TEST_F(ScanSessionTest, IndividualReads) {
Wade Guthriea60a11c2013-04-12 17:47:34 -0700362 vector<uint16_t> result;
363 static const float kArbitraryFraction = 0.83;
364
365 {
366 vector<uint16_t> expected;
367 expected.push_back(kExpectedFreq5640);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700368 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700369 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700370 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700371 }
372 {
373 vector<uint16_t> expected;
374 expected.push_back(kExpectedFreq5600);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700375 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700376 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700377 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700378 }
379 {
380 vector<uint16_t> expected;
381 expected.push_back(kExpectedFreq5580);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700382 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700383 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700384 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700385 }
386 {
387 vector<uint16_t> expected;
388 expected.push_back(kExpectedFreq5560);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700389 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700390 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700391 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700392 }
393 {
394 vector<uint16_t> expected;
395 expected.push_back(kExpectedFreq5620);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700396 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700397 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700398 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700399 }
400 {
401 vector<uint16_t> expected;
402 expected.push_back(kExpectedFreq2412);
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700403 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
Wade Guthriea60a11c2013-04-12 17:47:34 -0700404 EXPECT_THAT(result, ContainerEq(expected));
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700405 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
406 }
407 {
408 vector<uint16_t> expected;
409 expected.push_back(kExpectedFreq2417);
410 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
411 EXPECT_THAT(result, ContainerEq(expected));
412 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
413 }
414 {
415 vector<uint16_t> expected;
416 expected.push_back(kExpectedFreq2422);
417 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
418 EXPECT_THAT(result, ContainerEq(expected));
419 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
420 }
421 {
422 vector<uint16_t> expected;
423 expected.push_back(kExpectedFreq2427);
424 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
425 EXPECT_THAT(result, ContainerEq(expected));
426 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
427 }
428 {
429 vector<uint16_t> expected;
430 expected.push_back(kExpectedFreq2432);
431 result = GetScanFrequencies(kArbitraryFraction, 1, 1);
432 EXPECT_THAT(result, ContainerEq(expected));
433 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthriea60a11c2013-04-12 17:47:34 -0700434 }
435}
436
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700437TEST_F(ScanSessionTest, OnTriggerScanResponse) {
438 Nl80211Message::SetMessageType(kNl80211FamilyId);
439
Wade Guthrie7347bf22013-04-30 11:21:51 -0700440 EXPECT_CALL(*netlink_manager(), SendNl80211Message(
441 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700442 scan_session()->InitiateScan();
443
444 EXPECT_CALL(*this, OnScanError());
445 NewScanResultsMessage not_supposed_to_get_this_message;
446 scan_session()->OnTriggerScanResponse(not_supposed_to_get_this_message);
447}
448
449TEST_F(ScanSessionTest, ExhaustFrequencies) {
450 // Set min & max scan frequency count to 1 so each scan will be of a single
451 // frequency.
452 SetScanSize(1, 1);
453
454 // Perform all the progressive scans until the frequencies are exhausted.
455 for (size_t i = 0; i < GetScanFrequencyCount(); ++i) {
456 EXPECT_TRUE(scan_session()->HasMoreFrequencies());
Wade Guthrie7347bf22013-04-30 11:21:51 -0700457 EXPECT_CALL(*netlink_manager(), SendNl80211Message(
458 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700459 scan_session()->InitiateScan();
460 }
461
462 EXPECT_FALSE(scan_session()->HasMoreFrequencies());
Wade Guthrie7347bf22013-04-30 11:21:51 -0700463 EXPECT_CALL(*netlink_manager(), SendNl80211Message(
464 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _))
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700465 .Times(0);
466 scan_session()->InitiateScan();
467}
468
469TEST_F(ScanSessionTest, OnError) {
470 Nl80211Message::SetMessageType(kNl80211FamilyId);
471
Wade Guthrie7347bf22013-04-30 11:21:51 -0700472 EXPECT_CALL(*netlink_manager(), SendNl80211Message(
473 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700474 scan_session()->InitiateScan();
475
476 EXPECT_CALL(*this, OnScanError());
477 ErrorAckMessage error_message(-EINTR);
Wade Guthrie7347bf22013-04-30 11:21:51 -0700478 scan_session()->OnTriggerScanErrorResponse(&error_message);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700479}
480
481TEST_F(ScanSessionTest, EBusy) {
482 const size_t kSmallRetryNumber = 3;
483 Nl80211Message::SetMessageType(kNl80211FamilyId);
484 scan_session()->scan_tries_left_ = kSmallRetryNumber;
485
Wade Guthrie7347bf22013-04-30 11:21:51 -0700486 EXPECT_CALL(*netlink_manager(), SendNl80211Message(
487 IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700488 scan_session()->InitiateScan();
489
490 ErrorAckMessage error_message(-EBUSY);
491 for (size_t i = 0; i < kSmallRetryNumber; ++i) {
492 EXPECT_CALL(*this, OnScanError()).Times(0);
493 EXPECT_CALL(*dispatcher(), PostDelayedTask(_, _));
Wade Guthrie7347bf22013-04-30 11:21:51 -0700494 scan_session()->OnTriggerScanErrorResponse(&error_message);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700495 }
496
497 EXPECT_CALL(*this, OnScanError());
Wade Guthrie7347bf22013-04-30 11:21:51 -0700498 scan_session()->OnTriggerScanErrorResponse(&error_message);
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700499}
500
501TEST_F(ScanSessionTest, ScanHidden) {
502 scan_session_->AddSsid(ByteString("a", 1));
503 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -0700504 SendNl80211Message(HasHiddenSSID(kNl80211FamilyId), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700505 scan_session()->InitiateScan();
506}
507
508TEST_F(ScanSessionTest, ScanNoHidden) {
509 EXPECT_CALL(netlink_manager_,
Wade Guthrie7347bf22013-04-30 11:21:51 -0700510 SendNl80211Message(HasNoHiddenSSID(kNl80211FamilyId), _, _));
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700511 scan_session()->InitiateScan();
512}
513
Wade Guthriea60a11c2013-04-12 17:47:34 -0700514} // namespace shill