Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 1 | // 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 Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 9 | #include <limits> |
| 10 | #include <set> |
| 11 | #include <vector> |
| 12 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 17 | #include "shill/mock_event_dispatcher.h" |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 18 | #include "shill/mock_netlink_manager.h" |
| 19 | #include "shill/netlink_manager.h" |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 20 | #include "shill/netlink_message_matchers.h" |
| 21 | #include "shill/nl80211_message.h" |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 22 | |
| 23 | using std::set; |
| 24 | using std::vector; |
| 25 | using testing::_; |
| 26 | using testing::ContainerEq; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 27 | using testing::Test; |
| 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | static const uint16_t kExpectedFreq5640 = 5640; |
| 32 | static const uint16_t kExpectedFreq5600 = 5600; |
| 33 | static const uint16_t kExpectedFreq5580 = 5580; |
| 34 | static const uint16_t kExpectedFreq5560 = 5560; |
| 35 | static const uint16_t kExpectedFreq5620 = 5620; |
| 36 | |
| 37 | static 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 | |
| 45 | static const uint16_t kExpectedFreq2432 = 2432; |
| 46 | static const uint16_t kExpectedFreq2427 = 2427; |
| 47 | static const uint16_t kExpectedFreq2422 = 2422; |
| 48 | static const uint16_t kExpectedFreq2417 = 2417; |
| 49 | static const uint16_t kExpectedFreq2412 = 2412; |
| 50 | |
| 51 | static uint16_t kUnconnectedFrequencies[] = { |
| 52 | kExpectedFreq2432, |
| 53 | kExpectedFreq2427, |
| 54 | kExpectedFreq2422, |
| 55 | kExpectedFreq2417, |
| 56 | kExpectedFreq2412 |
| 57 | }; |
| 58 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 59 | static const uint16_t kNl80211FamilyId = 0x13; |
| 60 | |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 61 | class ScanSessionTest : public Test { |
| 62 | public: |
| 63 | // Test set of "all the other frequencies this device can support" in |
| 64 | // sorted order. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 65 | ScanSessionTest() : weak_ptr_factory_(this) { |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 66 | WiFiProvider::FrequencyCountList connected_frequencies( |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 67 | kConnectedFrequencies, |
| 68 | kConnectedFrequencies + arraysize(kConnectedFrequencies)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 69 | |
| 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 75 | scan_session_.reset(new ScanSession(&netlink_manager_, |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 76 | &dispatcher_, |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 77 | connected_frequencies, |
| 78 | unconnected_frequencies, |
| 79 | 0, |
| 80 | ScanSession::FractionList(), |
| 81 | kArbitraryMinimum, |
| 82 | kArbitraryMaximum, |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 83 | Bind(&ScanSessionTest::OnScanError, |
Wade Guthrie | f22681f | 2013-05-31 11:46:31 -0700 | [diff] [blame^] | 84 | weak_ptr_factory_.GetWeakPtr()), |
| 85 | NULL)); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 88 | 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 Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 96 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 112 | MockNetlinkManager netlink_manager_; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 113 | scoped_ptr<ScanSession> scan_session_; |
| 114 | base::WeakPtrFactory<ScanSessionTest> weak_ptr_factory_; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | // Test that we can get a bunch of frequencies up to a specified fraction. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 118 | TEST_F(ScanSessionTest, Fraction) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 119 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 127 | result = GetScanFrequencies(.83, 1, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 128 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 129 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Get the next 4 values. |
| 133 | { |
| 134 | vector<uint16_t> expected; |
| 135 | expected.push_back(kExpectedFreq5560); |
| 136 | expected.push_back(kExpectedFreq5620); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 137 | expected.push_back(kExpectedFreq2412); |
| 138 | expected.push_back(kExpectedFreq2417); |
| 139 | result = GetScanFrequencies(ScanSession::kAllFrequencies, 1, 4); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 140 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // And, get the remaining list. |
| 145 | { |
| 146 | vector<uint16_t> expected; |
| 147 | expected.push_back(kExpectedFreq2422); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 148 | expected.push_back(kExpectedFreq2427); |
| 149 | expected.push_back(kExpectedFreq2432); |
| 150 | result = GetScanFrequencies(ScanSession::kAllFrequencies, 20, |
| 151 | std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 152 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 153 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 154 | } |
| 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 Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 159 | TEST_F(ScanSessionTest, TwoFractions) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 160 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 167 | result = GetScanFrequencies(.60, 0, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 168 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 169 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 170 | } |
| 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 177 | result = GetScanFrequencies(.32, 0, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 178 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 179 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // And, get the remaining list. |
| 183 | { |
| 184 | vector<uint16_t> expected; |
| 185 | expected.push_back(kExpectedFreq5620); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 186 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 187 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 194 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 195 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 196 | } |
| 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 Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 201 | TEST_F(ScanSessionTest, Min) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 202 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 210 | result = GetScanFrequencies(.30, 3, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 211 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 212 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Get the next value by requensting a minimum of 1. |
| 216 | { |
| 217 | vector<uint16_t> expected; |
| 218 | expected.push_back(kExpectedFreq5560); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 219 | result = GetScanFrequencies(0.0, 1, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 220 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 221 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // And, get the remaining list. |
| 225 | { |
| 226 | vector<uint16_t> expected; |
| 227 | expected.push_back(kExpectedFreq5620); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 228 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 229 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 236 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 237 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | // Test that we can get up to a specified maximum number of frequencies. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 242 | TEST_F(ScanSessionTest, Max) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 243 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 253 | expected.push_back(kExpectedFreq2412); |
| 254 | expected.push_back(kExpectedFreq2417); |
| 255 | result = GetScanFrequencies(ScanSession::kAllFrequencies, 1, 7); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 256 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 257 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // And, get the remaining list. |
| 261 | { |
| 262 | vector<uint16_t> expected; |
| 263 | expected.push_back(kExpectedFreq2422); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 264 | expected.push_back(kExpectedFreq2427); |
| 265 | expected.push_back(kExpectedFreq2432); |
| 266 | result = GetScanFrequencies(ScanSession::kAllFrequencies, 20, |
| 267 | std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 268 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 269 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | // Test that we can get exactly the seen frequencies and exactly the unseen |
| 274 | // ones. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 275 | TEST_F(ScanSessionTest, Exact) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 276 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 286 | result = GetScanFrequencies(ScanSession::kAllFrequencies, 5, 5); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 287 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 288 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // And, get the last 5. |
| 292 | { |
| 293 | vector<uint16_t> expected; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 294 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 295 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 300 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 301 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | // Test that we can get everything in one read. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 306 | TEST_F(ScanSessionTest, AllOneRead) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 307 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 313 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 314 | expected.push_back(kExpectedFreq2417); |
| 315 | expected.push_back(kExpectedFreq2422); |
| 316 | expected.push_back(kExpectedFreq2427); |
| 317 | expected.push_back(kExpectedFreq2432); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 318 | vector<uint16_t> result; |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 319 | result = GetScanFrequencies(ScanSession::kAllFrequencies, |
| 320 | std::numeric_limits<size_t>::max(), |
| 321 | std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 322 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 323 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Test that we can get all the previously seen frequencies (and only the |
| 327 | // previously seen frequencies) via the requested fraction. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 328 | TEST_F(ScanSessionTest, EverythingFraction) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 329 | 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 Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 339 | result = GetScanFrequencies(1.0, 0, std::numeric_limits<size_t>::max()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 340 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 341 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // And, get the remaining list. |
| 345 | { |
| 346 | vector<uint16_t> expected; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 347 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 348 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 355 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 356 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | // Test that we can get each value individually. |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 361 | TEST_F(ScanSessionTest, IndividualReads) { |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 362 | vector<uint16_t> result; |
| 363 | static const float kArbitraryFraction = 0.83; |
| 364 | |
| 365 | { |
| 366 | vector<uint16_t> expected; |
| 367 | expected.push_back(kExpectedFreq5640); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 368 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 369 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 370 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 371 | } |
| 372 | { |
| 373 | vector<uint16_t> expected; |
| 374 | expected.push_back(kExpectedFreq5600); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 375 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 376 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 377 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 378 | } |
| 379 | { |
| 380 | vector<uint16_t> expected; |
| 381 | expected.push_back(kExpectedFreq5580); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 382 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 383 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 384 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 385 | } |
| 386 | { |
| 387 | vector<uint16_t> expected; |
| 388 | expected.push_back(kExpectedFreq5560); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 389 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 390 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 391 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 392 | } |
| 393 | { |
| 394 | vector<uint16_t> expected; |
| 395 | expected.push_back(kExpectedFreq5620); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 396 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 397 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 398 | EXPECT_TRUE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 399 | } |
| 400 | { |
| 401 | vector<uint16_t> expected; |
| 402 | expected.push_back(kExpectedFreq2412); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 403 | result = GetScanFrequencies(kArbitraryFraction, 1, 1); |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 404 | EXPECT_THAT(result, ContainerEq(expected)); |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 405 | 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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 437 | TEST_F(ScanSessionTest, OnTriggerScanResponse) { |
| 438 | Nl80211Message::SetMessageType(kNl80211FamilyId); |
| 439 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 440 | EXPECT_CALL(*netlink_manager(), SendNl80211Message( |
| 441 | IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 442 | 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 | |
| 449 | TEST_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 Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 457 | EXPECT_CALL(*netlink_manager(), SendNl80211Message( |
| 458 | IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 459 | scan_session()->InitiateScan(); |
| 460 | } |
| 461 | |
| 462 | EXPECT_FALSE(scan_session()->HasMoreFrequencies()); |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 463 | EXPECT_CALL(*netlink_manager(), SendNl80211Message( |
| 464 | IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _)) |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 465 | .Times(0); |
| 466 | scan_session()->InitiateScan(); |
| 467 | } |
| 468 | |
| 469 | TEST_F(ScanSessionTest, OnError) { |
| 470 | Nl80211Message::SetMessageType(kNl80211FamilyId); |
| 471 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 472 | EXPECT_CALL(*netlink_manager(), SendNl80211Message( |
| 473 | IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 474 | scan_session()->InitiateScan(); |
| 475 | |
| 476 | EXPECT_CALL(*this, OnScanError()); |
| 477 | ErrorAckMessage error_message(-EINTR); |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 478 | scan_session()->OnTriggerScanErrorResponse(&error_message); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST_F(ScanSessionTest, EBusy) { |
| 482 | const size_t kSmallRetryNumber = 3; |
| 483 | Nl80211Message::SetMessageType(kNl80211FamilyId); |
| 484 | scan_session()->scan_tries_left_ = kSmallRetryNumber; |
| 485 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 486 | EXPECT_CALL(*netlink_manager(), SendNl80211Message( |
| 487 | IsNl80211Command(kNl80211FamilyId, NL80211_CMD_TRIGGER_SCAN), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 488 | 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 Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 494 | scan_session()->OnTriggerScanErrorResponse(&error_message); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | EXPECT_CALL(*this, OnScanError()); |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 498 | scan_session()->OnTriggerScanErrorResponse(&error_message); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | TEST_F(ScanSessionTest, ScanHidden) { |
| 502 | scan_session_->AddSsid(ByteString("a", 1)); |
| 503 | EXPECT_CALL(netlink_manager_, |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 504 | SendNl80211Message(HasHiddenSSID(kNl80211FamilyId), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 505 | scan_session()->InitiateScan(); |
| 506 | } |
| 507 | |
| 508 | TEST_F(ScanSessionTest, ScanNoHidden) { |
| 509 | EXPECT_CALL(netlink_manager_, |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 510 | SendNl80211Message(HasNoHiddenSSID(kNl80211FamilyId), _, _)); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 511 | scan_session()->InitiateScan(); |
| 512 | } |
| 513 | |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 514 | } // namespace shill |