• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <vector>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <wifi_system_test/mock_interface_tool.h>
22 #include "android/net/wifi/IWifiScannerImpl.h"
23 #include "wificond/scanning/offload/offload_scan_utils.h"
24 #include "wificond/scanning/scanner_impl.h"
25 #include "wificond/tests/mock_client_interface_impl.h"
26 #include "wificond/tests/mock_netlink_manager.h"
27 #include "wificond/tests/mock_netlink_utils.h"
28 #include "wificond/tests/mock_offload_scan_callback_interface_impl.h"
29 #include "wificond/tests/mock_offload_scan_manager.h"
30 #include "wificond/tests/mock_offload_service_utils.h"
31 #include "wificond/tests/mock_scan_utils.h"
32 #include "wificond/tests/offload_test_utils.h"
33 
34 using ::android::binder::Status;
35 using ::android::net::wifi::IWifiScannerImpl;
36 using ::android::wifi_system::MockInterfaceTool;
37 using ::com::android::server::wifi::wificond::SingleScanSettings;
38 using ::com::android::server::wifi::wificond::PnoNetwork;
39 using ::com::android::server::wifi::wificond::PnoSettings;
40 using ::com::android::server::wifi::wificond::NativeScanResult;
41 using android::hardware::wifi::offload::V1_0::ScanResult;
42 using ::testing::Eq;
43 using ::testing::Invoke;
44 using ::testing::NiceMock;
45 using ::testing::Return;
46 using ::testing::_;
47 using std::shared_ptr;
48 using std::unique_ptr;
49 using std::vector;
50 
51 using namespace std::placeholders;
52 
53 namespace android {
54 namespace wificond {
55 
56 namespace {
57 
58 constexpr uint32_t kFakeInterfaceIndex = 12;
59 constexpr uint32_t kFakeScanIntervalMs = 10000;
60 
61 // This is a helper function to mock the behavior of ScanUtils::Scan()
62 // when we expect a error code.
63 // |interface_index_ignored|, |request_random_mac_ignored|, |ssids_ignored|,
64 // |freqs_ignored|, |error_code| are mapped to existing parameters of ScanUtils::Scan().
65 // |mock_error_code| is a additional parameter used for specifying expected error code.
ReturnErrorCodeForScanRequest(int mock_error_code,uint32_t interface_index_ignored,bool request_random_mac_ignored,int scan_type,const std::vector<std::vector<uint8_t>> & ssids_ignored,const std::vector<uint32_t> & freqs_ignored,int * error_code)66 bool ReturnErrorCodeForScanRequest(
67     int mock_error_code,
68     uint32_t interface_index_ignored,
69     bool request_random_mac_ignored,
70     int scan_type,
71     const std::vector<std::vector<uint8_t>>& ssids_ignored,
72     const std::vector<uint32_t>& freqs_ignored,
73     int* error_code) {
74   *error_code = mock_error_code;
75   // Returing false because this helper function is used for failure case.
76   return false;
77 }
78 
CaptureSchedScanIntervalSetting(uint32_t,const SchedScanIntervalSetting & interval_setting,int32_t,int32_t,const SchedScanReqFlags &,const std::vector<std::vector<uint8_t>> &,const std::vector<std::vector<uint8_t>> &,const std::vector<uint32_t> &,int *,SchedScanIntervalSetting * out_interval_setting)79 bool CaptureSchedScanIntervalSetting(
80     uint32_t /* interface_index */,
81     const SchedScanIntervalSetting&  interval_setting,
82     int32_t /* rssi_threshold_2g */,
83     int32_t /* rssi_threshold_5g */,
84     const SchedScanReqFlags&  /* req_flags */,
85     const  std::vector<std::vector<uint8_t>>& /* scan_ssids */,
86     const std::vector<std::vector<uint8_t>>& /* match_ssids */,
87     const  std::vector<uint32_t>& /* freqs */,
88     int* /* error_code */,
89     SchedScanIntervalSetting* out_interval_setting) {
90   *out_interval_setting = interval_setting;
91   return true;
92 }
93 
CaptureSchedScanReqFlags(uint32_t,const SchedScanIntervalSetting &,int32_t,int32_t,const SchedScanReqFlags & req_flags,const std::vector<std::vector<uint8_t>> &,const std::vector<std::vector<uint8_t>> &,const std::vector<uint32_t> &,int *,SchedScanReqFlags * out_req_flags)94 bool CaptureSchedScanReqFlags(
95     uint32_t /* interface_index */,
96     const SchedScanIntervalSetting&  /* interval_setting */,
97     int32_t /* rssi_threshold_2g */,
98     int32_t /* rssi_threshold_5g */,
99     const SchedScanReqFlags& req_flags,
100     const  std::vector<std::vector<uint8_t>>& /* scan_ssids */,
101     const std::vector<std::vector<uint8_t>>& /* match_ssids */,
102     const  std::vector<uint32_t>& /* freqs */,
103     int* /* error_code */,
104     SchedScanReqFlags* out_req_flags) {
105   *out_req_flags = req_flags;
106   return true;
107 }
108 
ReturnOffloadScanResults(std::vector<NativeScanResult> * native_scan_results_,const std::vector<ScanResult> & offload_scan_results)109 bool ReturnOffloadScanResults(
110     std::vector<NativeScanResult>* native_scan_results_,
111     const std::vector<ScanResult>& offload_scan_results) {
112   return OffloadScanUtils::convertToNativeScanResults(offload_scan_results,
113                                                       native_scan_results_);
114 }
115 
ReturnNetlinkScanResults(uint32_t interface_index,std::vector<NativeScanResult> * native_scan_results_,const std::vector<ScanResult> & offload_scan_results)116 bool ReturnNetlinkScanResults(
117     uint32_t interface_index,
118     std::vector<NativeScanResult>* native_scan_results_,
119     const std::vector<ScanResult>& offload_scan_results) {
120   return OffloadScanUtils::convertToNativeScanResults(offload_scan_results,
121                                                       native_scan_results_);
122 }
123 
124 }  // namespace
125 
126 class ScannerTest : public ::testing::Test {
127  protected:
SetUp()128   void SetUp() override {
129     ON_CALL(*offload_service_utils_, GetOffloadScanManager(_, _))
130         .WillByDefault(Return(offload_scan_manager_));
131     ON_CALL(*offload_service_utils_, GetOffloadScanCallbackInterface(_))
132         .WillByDefault(Return(offload_scan_callback_interface_));
133     dummy_scan_results_ = OffloadTestUtils::createOffloadScanResults();
134   }
135 
TearDown()136   void TearDown() override { dummy_scan_results_.clear(); }
137 
138   unique_ptr<ScannerImpl> scanner_impl_;
139   NiceMock<MockNetlinkManager> netlink_manager_;
140   NiceMock<MockNetlinkUtils> netlink_utils_{&netlink_manager_};
141   NiceMock<MockScanUtils> scan_utils_{&netlink_manager_};
142   NiceMock<MockInterfaceTool> if_tool_;
143   NiceMock<MockClientInterfaceImpl> client_interface_impl_{
144       &if_tool_, &netlink_utils_, &scan_utils_};
145   shared_ptr<NiceMock<MockOffloadServiceUtils>> offload_service_utils_{
146       new NiceMock<MockOffloadServiceUtils>()};
147   shared_ptr<NiceMock<MockOffloadScanCallbackInterfaceImpl>>
148       offload_scan_callback_interface_{
149           new NiceMock<MockOffloadScanCallbackInterfaceImpl>(
150               scanner_impl_.get())};
151   std::shared_ptr<NiceMock<MockOffloadScanManager>> offload_scan_manager_{
152       new NiceMock<MockOffloadScanManager>(offload_service_utils_,
153                                            offload_scan_callback_interface_)};
154   ScanCapabilities scan_capabilities_;
155   WiphyFeatures wiphy_features_;
156   std::vector<ScanResult> dummy_scan_results_;
157 };
158 
TEST_F(ScannerTest,TestSingleScan)159 TEST_F(ScannerTest, TestSingleScan) {
160   EXPECT_CALL(scan_utils_,
161               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_DEFAULT, _, _, _)).
162       WillOnce(Return(true));
163   bool success = false;
164   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
165                                       scan_capabilities_, wiphy_features_,
166                                       &client_interface_impl_,
167                                       &scan_utils_, offload_service_utils_));
168   EXPECT_TRUE(scanner_impl_->scan(SingleScanSettings(), &success).isOk());
169   EXPECT_TRUE(success);
170 }
171 
TEST_F(ScannerTest,TestSingleScanForLowSpanScan)172 TEST_F(ScannerTest, TestSingleScanForLowSpanScan) {
173   EXPECT_CALL(scan_utils_,
174               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_LOW_SPAN, _, _, _)).
175       WillOnce(Return(true));
176   wiphy_features_.supports_low_span_oneshot_scan = true;
177   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
178                            wiphy_features_, &client_interface_impl_,
179                            &scan_utils_, offload_service_utils_);
180   SingleScanSettings settings;
181   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_LOW_SPAN;
182   bool success = false;
183   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
184   EXPECT_TRUE(success);
185 }
186 
TEST_F(ScannerTest,TestSingleScanForLowPowerScan)187 TEST_F(ScannerTest, TestSingleScanForLowPowerScan) {
188   EXPECT_CALL(scan_utils_,
189               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_LOW_POWER, _, _, _)).
190       WillOnce(Return(true));
191   wiphy_features_.supports_low_power_oneshot_scan = true;
192   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
193                            wiphy_features_, &client_interface_impl_,
194                            &scan_utils_, offload_service_utils_);
195   SingleScanSettings settings;
196   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_LOW_POWER;
197   bool success = false;
198   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
199   EXPECT_TRUE(success);
200 }
201 
TEST_F(ScannerTest,TestSingleScanForHighAccuracyScan)202 TEST_F(ScannerTest, TestSingleScanForHighAccuracyScan) {
203   EXPECT_CALL(scan_utils_,
204               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY, _, _, _)).
205       WillOnce(Return(true));
206   wiphy_features_.supports_high_accuracy_oneshot_scan = true;
207   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
208                            wiphy_features_, &client_interface_impl_,
209                            &scan_utils_, offload_service_utils_);
210   SingleScanSettings settings;
211   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY;
212   bool success = false;
213   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
214   EXPECT_TRUE(success);
215 }
216 
TEST_F(ScannerTest,TestSingleScanForLowSpanScanWithNoWiphySupport)217 TEST_F(ScannerTest, TestSingleScanForLowSpanScanWithNoWiphySupport) {
218   EXPECT_CALL(scan_utils_,
219               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_DEFAULT, _, _, _)).
220       WillOnce(Return(true));
221   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
222                            wiphy_features_, &client_interface_impl_,
223                            &scan_utils_, offload_service_utils_);
224   SingleScanSettings settings;
225   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_LOW_SPAN;
226   bool success = false;
227   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
228   EXPECT_TRUE(success);
229 }
230 
TEST_F(ScannerTest,TestSingleScanForLowPowerScanWithNoWiphySupport)231 TEST_F(ScannerTest, TestSingleScanForLowPowerScanWithNoWiphySupport) {
232   EXPECT_CALL(scan_utils_,
233               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_DEFAULT, _, _, _)).
234       WillOnce(Return(true));
235   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
236                            wiphy_features_, &client_interface_impl_,
237                            &scan_utils_, offload_service_utils_);
238   SingleScanSettings settings;
239   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_LOW_POWER;
240   bool success = false;
241   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
242   EXPECT_TRUE(success);
243 }
244 
TEST_F(ScannerTest,TestSingleScanForHighAccuracyScanWithNoWiphySupport)245 TEST_F(ScannerTest, TestSingleScanForHighAccuracyScanWithNoWiphySupport) {
246   EXPECT_CALL(scan_utils_,
247               Scan(_, _, IWifiScannerImpl::SCAN_TYPE_DEFAULT, _, _, _)).
248       WillOnce(Return(true));
249   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
250                            wiphy_features_, &client_interface_impl_,
251                            &scan_utils_, offload_service_utils_);
252   SingleScanSettings settings;
253   settings.scan_type_ = IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY;
254   bool success = false;
255   EXPECT_TRUE(scanner_impl.scan(settings, &success).isOk());
256   EXPECT_TRUE(success);
257 }
258 
TEST_F(ScannerTest,TestSingleScanFailure)259 TEST_F(ScannerTest, TestSingleScanFailure) {
260   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
261                                       scan_capabilities_, wiphy_features_,
262                                       &client_interface_impl_,
263                                       &scan_utils_, offload_service_utils_));
264   EXPECT_CALL(
265       scan_utils_,
266       Scan(_, _, _, _, _, _)).
267           WillOnce(Invoke(bind(
268               ReturnErrorCodeForScanRequest, EBUSY,
269               _1, _2, _3, _4, _5, _6)));
270 
271   bool success = false;
272   EXPECT_TRUE(scanner_impl_->scan(SingleScanSettings(), &success).isOk());
273   EXPECT_FALSE(success);
274 }
275 
TEST_F(ScannerTest,TestProcessAbortsOnScanReturningNoDeviceError)276 TEST_F(ScannerTest, TestProcessAbortsOnScanReturningNoDeviceError) {
277   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
278                                       scan_capabilities_, wiphy_features_,
279                                       &client_interface_impl_,
280                                       &scan_utils_, offload_service_utils_));
281   ON_CALL(
282       scan_utils_,
283       Scan(_, _, _, _, _, _)).
284           WillByDefault(Invoke(bind(
285               ReturnErrorCodeForScanRequest, ENODEV,
286               _1, _2, _3, _4, _5, _6)));
287 
288   bool success_ignored;
289   EXPECT_DEATH(scanner_impl_->scan(SingleScanSettings(), &success_ignored),
290                "Driver is in a bad state*");
291 }
292 
TEST_F(ScannerTest,TestAbortScan)293 TEST_F(ScannerTest, TestAbortScan) {
294   bool single_scan_success = false;
295   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
296                                       scan_capabilities_, wiphy_features_,
297                                       &client_interface_impl_,
298                                       &scan_utils_, offload_service_utils_));
299   EXPECT_CALL(scan_utils_, Scan(_, _, _, _, _, _))
300       .WillOnce(Return(true));
301   EXPECT_TRUE(
302       scanner_impl_->scan(SingleScanSettings(), &single_scan_success).isOk());
303   EXPECT_TRUE(single_scan_success);
304 
305   EXPECT_CALL(scan_utils_, AbortScan(_));
306   EXPECT_TRUE(scanner_impl_->abortScan().isOk());
307 }
308 
TEST_F(ScannerTest,TestAbortScanNotIssuedIfNoOngoingScan)309 TEST_F(ScannerTest, TestAbortScanNotIssuedIfNoOngoingScan) {
310   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
311                                       scan_capabilities_, wiphy_features_,
312                                       &client_interface_impl_,
313                                       &scan_utils_, offload_service_utils_));
314   EXPECT_CALL(scan_utils_, AbortScan(_)).Times(0);
315   EXPECT_TRUE(scanner_impl_->abortScan().isOk());
316 }
317 
TEST_F(ScannerTest,TestGetScanResults)318 TEST_F(ScannerTest, TestGetScanResults) {
319   vector<NativeScanResult> scan_results;
320   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
321                                       scan_capabilities_, wiphy_features_,
322                                       &client_interface_impl_,
323                                       &scan_utils_, offload_service_utils_));
324   EXPECT_CALL(scan_utils_, GetScanResult(_, _)).WillOnce(Return(true));
325   EXPECT_TRUE(scanner_impl_->getScanResults(&scan_results).isOk());
326 }
327 
TEST_F(ScannerTest,TestStartPnoScanViaNetlink)328 TEST_F(ScannerTest, TestStartPnoScanViaNetlink) {
329   bool success = false;
330   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
331       .Times(1)
332       .WillRepeatedly(Return(false));
333   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
334                            wiphy_features_, &client_interface_impl_,
335                            &scan_utils_, offload_service_utils_);
336   EXPECT_CALL(
337       scan_utils_,
338       StartScheduledScan(_, _, _, _, _,  _, _, _, _)).
339           WillOnce(Return(true));
340   EXPECT_TRUE(scanner_impl.startPnoScan(PnoSettings(), &success).isOk());
341   EXPECT_TRUE(success);
342 }
343 
TEST_F(ScannerTest,TestStartPnoScanViaNetlinkWithLowPowerScanWiphySupport)344 TEST_F(ScannerTest, TestStartPnoScanViaNetlinkWithLowPowerScanWiphySupport) {
345   bool success = false;
346   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
347       .Times(1)
348       .WillRepeatedly(Return(false));
349   wiphy_features_.supports_low_power_oneshot_scan = true;
350   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_,
351                            wiphy_features_, &client_interface_impl_,
352                            &scan_utils_, offload_service_utils_);
353   SchedScanReqFlags req_flags = {};
354   EXPECT_CALL(
355       scan_utils_,
356       StartScheduledScan(_, _, _, _, _, _, _, _, _)).
357           WillOnce(Invoke(bind(
358               CaptureSchedScanReqFlags,
359               _1, _2, _3, _4, _5, _6, _7, _8, _9, &req_flags)));
360   EXPECT_TRUE(scanner_impl.startPnoScan(PnoSettings(), &success).isOk());
361   EXPECT_TRUE(success);
362   EXPECT_TRUE(req_flags.request_low_power);
363 }
364 
TEST_F(ScannerTest,TestStopPnoScanViaNetlink)365 TEST_F(ScannerTest, TestStopPnoScanViaNetlink) {
366   bool success = false;
367   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
368       .Times(1)
369       .WillRepeatedly(Return(false));
370   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
371                                       scan_capabilities_, wiphy_features_,
372                                       &client_interface_impl_,
373                                       &scan_utils_, offload_service_utils_));
374   // StopScheduledScan() will be called no matter if there is an ongoing
375   // scheduled scan or not. This is for making the system more robust.
376   EXPECT_CALL(scan_utils_, StopScheduledScan(_)).WillOnce(Return(true));
377   EXPECT_TRUE(scanner_impl_->stopPnoScan(&success).isOk());
378   EXPECT_TRUE(success);
379 }
380 
TEST_F(ScannerTest,TestStartScanOverOffload)381 TEST_F(ScannerTest, TestStartScanOverOffload) {
382   bool success = false;
383   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
384       .Times(1)
385       .WillRepeatedly(Return(true));
386   EXPECT_CALL(*offload_scan_manager_, startScan(_, _, _, _, _, _, _))
387       .Times(1)
388       .WillRepeatedly(Return(true));
389   EXPECT_CALL(*offload_scan_manager_, stopScan(_))
390       .Times(1)
391       .WillRepeatedly(Return(true));
392   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
393                                       scan_capabilities_, wiphy_features_,
394                                       &client_interface_impl_,
395                                       &scan_utils_, offload_service_utils_));
396   scanner_impl_->startPnoScan(PnoSettings(), &success);
397   EXPECT_TRUE(success);
398   scanner_impl_->stopPnoScan(&success);
399   EXPECT_TRUE(success);
400 }
401 
TEST_F(ScannerTest,TestStartScanOverNetlinkFallback)402 TEST_F(ScannerTest, TestStartScanOverNetlinkFallback) {
403   bool success = false;
404   ON_CALL(*offload_service_utils_, IsOffloadScanSupported())
405       .WillByDefault(Return(true));
406   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
407                                       scan_capabilities_, wiphy_features_,
408                                       &client_interface_impl_,
409                                       &scan_utils_, offload_service_utils_));
410   EXPECT_CALL(*offload_scan_manager_, startScan(_, _, _, _, _, _, _))
411       .WillOnce(Return(false));
412   EXPECT_CALL(*offload_scan_manager_, stopScan(_)).Times(0);
413   EXPECT_CALL(scan_utils_, StartScheduledScan(_, _, _, _, _, _, _, _, _))
414       .WillOnce(Return(true));
415   EXPECT_CALL(scan_utils_, StopScheduledScan(_)).WillOnce(Return(true));
416   EXPECT_TRUE(scanner_impl_->startPnoScan(PnoSettings(), &success).isOk());
417   EXPECT_TRUE(success == true);
418   scanner_impl_->stopPnoScan(&success);
419   EXPECT_TRUE(success);
420 }
421 
TEST_F(ScannerTest,TestAsyncErrorOverOffload)422 TEST_F(ScannerTest, TestAsyncErrorOverOffload) {
423   bool success = false;
424   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
425       .Times(1)
426       .WillRepeatedly(Return(true));
427   EXPECT_CALL(*offload_scan_manager_, startScan(_, _, _, _, _, _, _))
428       .Times(1)
429       .WillRepeatedly(Return(true));
430   EXPECT_CALL(*offload_scan_manager_, stopScan(_))
431       .Times(1)
432       .WillRepeatedly(Return(true));
433   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
434                                       scan_capabilities_, wiphy_features_,
435                                       &client_interface_impl_,
436                                       &scan_utils_, offload_service_utils_));
437   EXPECT_CALL(scan_utils_, StartScheduledScan(_, _, _, _, _, _, _, _, _))
438       .WillOnce(Return(true));
439   EXPECT_CALL(scan_utils_, StopScheduledScan(_)).WillOnce(Return(true));
440   scanner_impl_->startPnoScan(PnoSettings(), &success);
441   EXPECT_TRUE(success);
442   scanner_impl_->OnOffloadError(
443       OffloadScanCallbackInterface::AsyncErrorReason::REMOTE_FAILURE);
444   scanner_impl_->stopPnoScan(&success);
445   EXPECT_TRUE(success);
446 }
447 
TEST_F(ScannerTest,TestGetScanResultsFromOffload)448 TEST_F(ScannerTest, TestGetScanResultsFromOffload) {
449   bool success = false;
450   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
451       .Times(1)
452       .WillRepeatedly(Return(true));
453   EXPECT_CALL(*offload_scan_manager_, startScan(_, _, _, _, _, _, _))
454       .Times(1)
455       .WillRepeatedly(Return(true));
456   EXPECT_CALL(*offload_scan_manager_, getScanResults(_))
457       .Times(1)
458       .WillOnce(
459           Invoke(bind(ReturnOffloadScanResults, _1, dummy_scan_results_)));
460   EXPECT_CALL(*offload_scan_manager_, stopScan(_))
461       .Times(1)
462       .WillRepeatedly(Return(true));
463   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
464                                       scan_capabilities_, wiphy_features_,
465                                       &client_interface_impl_,
466                                       &scan_utils_, offload_service_utils_));
467   scanner_impl_->startPnoScan(PnoSettings(), &success);
468   EXPECT_TRUE(success);
469   scanner_impl_->OnOffloadScanResult();
470   std::vector<NativeScanResult> scan_results;
471   EXPECT_TRUE(scanner_impl_->getPnoScanResults(&scan_results).isOk());
472   EXPECT_FALSE(scan_results.empty());
473   scanner_impl_->stopPnoScan(&success);
474   EXPECT_TRUE(success);
475 }
476 
TEST_F(ScannerTest,TestGetScanResultsWhenOffloadFails)477 TEST_F(ScannerTest, TestGetScanResultsWhenOffloadFails) {
478   bool success = false;
479   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
480       .Times(1)
481       .WillRepeatedly(Return(true));
482   EXPECT_CALL(*offload_scan_manager_, startScan(_, _, _, _, _, _, _))
483       .Times(1)
484       .WillRepeatedly(Return(true));
485   EXPECT_CALL(*offload_scan_manager_, stopScan(_))
486       .Times(1)
487       .WillRepeatedly(Return(true));
488   EXPECT_CALL(*offload_scan_manager_, getScanResults(_)).Times(0);
489   EXPECT_CALL(scan_utils_, GetScanResult(_, _))
490       .Times(1)
491       .WillOnce(
492           Invoke(bind(ReturnNetlinkScanResults, _1, _2, dummy_scan_results_)));
493   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
494                                       scan_capabilities_, wiphy_features_,
495                                       &client_interface_impl_,
496                                       &scan_utils_, offload_service_utils_));
497   EXPECT_CALL(scan_utils_, StartScheduledScan(_, _, _, _, _, _, _, _, _))
498       .WillOnce(Return(true));
499   EXPECT_CALL(scan_utils_, StopScheduledScan(_)).WillOnce(Return(true));
500   EXPECT_TRUE(scanner_impl_->startPnoScan(PnoSettings(), &success).isOk());
501   EXPECT_TRUE(success);
502   scanner_impl_->OnOffloadError(
503       OffloadScanCallbackInterface::AsyncErrorReason::REMOTE_FAILURE);
504   std::vector<NativeScanResult> scan_results;
505   EXPECT_TRUE(scanner_impl_->getPnoScanResults(&scan_results).isOk());
506   EXPECT_FALSE(scan_results.empty());
507   scanner_impl_->stopPnoScan(&success);
508   EXPECT_TRUE(success);
509 }
510 
TEST_F(ScannerTest,TestGenerateScanPlansIfDeviceSupports)511 TEST_F(ScannerTest, TestGenerateScanPlansIfDeviceSupports) {
512   ScanCapabilities scan_capabilities_scan_plan_supported(
513       0 /* max_num_scan_ssids */,
514       0 /* max_num_sched_scan_ssids */,
515       0 /* max_match_sets */,
516       // Parameters above are not related to this test.
517       2 /* 1 plan for finite repeated scan and 1 plan for ininfite scan loop */,
518       kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier / 1000,
519       PnoSettings::kFastScanIterations);
520   ScannerImpl scanner(
521       kFakeInterfaceIndex,
522       scan_capabilities_scan_plan_supported, wiphy_features_,
523       &client_interface_impl_,
524       &scan_utils_, offload_service_utils_);
525 
526   PnoSettings pno_settings;
527   pno_settings.interval_ms_ = kFakeScanIntervalMs;
528 
529   SchedScanIntervalSetting interval_setting;
530   EXPECT_CALL(
531       scan_utils_,
532       StartScheduledScan(_, _, _, _, _, _, _, _, _)).
533               WillOnce(Invoke(bind(
534                   CaptureSchedScanIntervalSetting,
535                   _1, _2, _3, _4, _5, _6, _7, _8, _9, &interval_setting)));
536 
537   bool success_ignored = 0;
538   EXPECT_TRUE(scanner.startPnoScan(pno_settings, &success_ignored).isOk());
539   /* 1 plan for finite repeated scan */
540   EXPECT_EQ(1U, interval_setting.plans.size());
541   EXPECT_EQ(kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier,
542             interval_setting.final_interval_ms);
543 }
544 
TEST_F(ScannerTest,TestGenerateSingleIntervalIfDeviceDoesNotSupportScanPlan)545 TEST_F(ScannerTest, TestGenerateSingleIntervalIfDeviceDoesNotSupportScanPlan) {
546   ScanCapabilities scan_capabilities_no_scan_plan_support(
547       0 /* max_num_scan_ssids */,
548       0 /* max_num_sched_scan_ssids */,
549       0 /* max_match_sets */,
550       // Parameters above are not related to this test.
551       0 /* max_num_scan_plans */,
552       0 /* max_scan_plan_interval */,
553       0 /* max_scan_plan_iterations */);
554   ScannerImpl scanner(
555       kFakeInterfaceIndex,
556       scan_capabilities_no_scan_plan_support, wiphy_features_,
557       &client_interface_impl_,
558       &scan_utils_, offload_service_utils_);
559   PnoSettings pno_settings;
560   pno_settings.interval_ms_ = kFakeScanIntervalMs;
561 
562   SchedScanIntervalSetting interval_setting;
563   EXPECT_CALL(
564       scan_utils_,
565       StartScheduledScan(_, _, _, _, _, _, _, _, _)).
566               WillOnce(Invoke(bind(
567                   CaptureSchedScanIntervalSetting,
568                   _1, _2, _3, _4, _5, _6, _7, _8, _9, &interval_setting)));
569 
570   bool success_ignored = 0;
571   EXPECT_TRUE(scanner.startPnoScan(pno_settings, &success_ignored).isOk());
572 
573   EXPECT_EQ(0U, interval_setting.plans.size());
574   EXPECT_EQ(kFakeScanIntervalMs, interval_setting.final_interval_ms);
575 }
576 
TEST_F(ScannerTest,TestGetScanResultsOnInvalidatedScannerImpl)577 TEST_F(ScannerTest, TestGetScanResultsOnInvalidatedScannerImpl) {
578   vector<NativeScanResult> scan_results;
579   scanner_impl_.reset(new ScannerImpl(kFakeInterfaceIndex,
580                                       scan_capabilities_, wiphy_features_,
581                                       &client_interface_impl_,
582                                       &scan_utils_, offload_service_utils_));
583   scanner_impl_->Invalidate();
584   EXPECT_CALL(scan_utils_, GetScanResult(_, _))
585       .Times(0)
586       .WillOnce(Return(true));
587   EXPECT_TRUE(scanner_impl_->getScanResults(&scan_results).isOk());
588 }
589 
590 // Verify that pno scanning starts with no errors given a non-empty frequency list.
TEST_F(ScannerTest,TestStartPnoScanWithNonEmptyFrequencyList)591 TEST_F(ScannerTest, TestStartPnoScanWithNonEmptyFrequencyList) {
592   bool success = false;
593   ScanCapabilities scan_capabilities_test_frequencies(
594       1 /* max_num_scan_ssids */,
595       1 /* max_num_sched_scan_ssids */,
596       1 /* max_match_sets */,
597       0,
598       kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier / 1000,
599       PnoSettings::kFastScanIterations);
600   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
601         .Times(1)
602         .WillRepeatedly(Return(false));
603   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_test_frequencies,
604                            wiphy_features_, &client_interface_impl_,
605                            &scan_utils_, offload_service_utils_);
606 
607   PnoSettings pno_settings;
608   PnoNetwork network;
609   network.is_hidden_ = false;
610   network.frequencies_.push_back(2412);
611   pno_settings.pno_networks_.push_back(network);
612 
613   std::vector<uint32_t> expected_freqs;
614   expected_freqs.push_back(2412);
615   EXPECT_CALL(
616       scan_utils_,
617       StartScheduledScan(_, _, _, _, _, _, _, Eq(expected_freqs), _)).
618           WillOnce(Return(true));
619   EXPECT_TRUE(scanner_impl.startPnoScan(pno_settings, &success).isOk());
620   EXPECT_TRUE(success);
621 }
622 
623 // Verify that a unique set of frequencies is passed in for scanning when the input
624 // contains duplicate frequencies.
TEST_F(ScannerTest,TestStartPnoScanWithFrequencyListNoDuplicates)625 TEST_F(ScannerTest, TestStartPnoScanWithFrequencyListNoDuplicates) {
626   bool success = false;
627   ScanCapabilities scan_capabilities_test_frequencies(
628       1 /* max_num_scan_ssids */,
629       1 /* max_num_sched_scan_ssids */,
630       2 /* max_match_sets */,
631       0,
632       kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier / 1000,
633       PnoSettings::kFastScanIterations);
634   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
635         .Times(1)
636         .WillRepeatedly(Return(false));
637   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_test_frequencies,
638                            wiphy_features_, &client_interface_impl_,
639                            &scan_utils_, offload_service_utils_);
640 
641   PnoSettings pno_settings;
642   PnoNetwork network;
643   PnoNetwork network2;
644   network.is_hidden_ = false;
645   network.frequencies_.push_back(2412);
646   network.frequencies_.push_back(2437);
647   network2.is_hidden_ = false;
648   network2.frequencies_.push_back(2437);
649   network2.frequencies_.push_back(2462);
650   pno_settings.pno_networks_.push_back(network);
651   pno_settings.pno_networks_.push_back(network2);
652 
653   std::vector<uint32_t> expected_freqs;
654   expected_freqs.push_back(2412);
655   expected_freqs.push_back(2437);
656   expected_freqs.push_back(2462);
657   EXPECT_CALL(
658       scan_utils_,
659       StartScheduledScan(_, _, _, _, _, _, _, Eq(expected_freqs), _)).
660           WillOnce(Return(true));
661   EXPECT_TRUE(scanner_impl.startPnoScan(pno_settings, &success).isOk());
662   EXPECT_TRUE(success);
663 }
664 
665 // Verify that if more than 30% of networks don't have frequency data then a list of default
666 // frequencies will be added to the scan.
TEST_F(ScannerTest,TestStartPnoScanWithFrequencyListFallbackMechanism)667 TEST_F(ScannerTest, TestStartPnoScanWithFrequencyListFallbackMechanism) {
668   bool success = false;
669   ScanCapabilities scan_capabilities_test_frequencies(
670       1 /* max_num_scan_ssids */,
671       1 /* max_num_sched_scan_ssids */,
672       2 /* max_match_sets */,
673       0,
674       kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier / 1000,
675       PnoSettings::kFastScanIterations);
676   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
677         .Times(1)
678         .WillRepeatedly(Return(false));
679   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_test_frequencies,
680                            wiphy_features_, &client_interface_impl_,
681                            &scan_utils_, offload_service_utils_);
682 
683   PnoSettings pno_settings;
684   PnoNetwork network;
685   PnoNetwork network2;
686   network.is_hidden_ = false;
687   network.frequencies_.push_back(5640);
688   network2.is_hidden_ = false;
689   pno_settings.pno_networks_.push_back(network);
690   pno_settings.pno_networks_.push_back(network2);
691 
692   std::set<int32_t> default_frequencies = {2412, 2417, 2422, 2427, 2432, 2437, 2447, 2452, 2457,
693       2462, 5180, 5200, 5220, 5240, 5745, 5765, 5785, 5805};
694   default_frequencies.insert(5640); // add frequency from saved network
695   vector<uint32_t> expected_frequencies(default_frequencies.begin(), default_frequencies.end());
696   EXPECT_CALL(
697       scan_utils_,
698       StartScheduledScan(_, _, _, _, _, _, _, Eq(expected_frequencies), _)).
699           WillOnce(Return(true));
700   EXPECT_TRUE(scanner_impl.startPnoScan(pno_settings, &success).isOk());
701   EXPECT_TRUE(success);
702 }
703 
704 // Verify that when there is no frequency data all pno networks, an empty list is passed into
705 // StartScheduledScan in order to scan all frequencies.
TEST_F(ScannerTest,TestStartPnoScanEmptyList)706 TEST_F(ScannerTest, TestStartPnoScanEmptyList) {
707   bool success = false;
708   ScanCapabilities scan_capabilities_test_frequencies(
709       1 /* max_num_scan_ssids */,
710       1 /* max_num_sched_scan_ssids */,
711       2 /* max_match_sets */,
712       0,
713       kFakeScanIntervalMs * PnoSettings::kSlowScanIntervalMultiplier / 1000,
714       PnoSettings::kFastScanIterations);
715   EXPECT_CALL(*offload_service_utils_, IsOffloadScanSupported())
716         .Times(1)
717         .WillRepeatedly(Return(false));
718   ScannerImpl scanner_impl(kFakeInterfaceIndex, scan_capabilities_test_frequencies,
719                            wiphy_features_, &client_interface_impl_,
720                            &scan_utils_, offload_service_utils_);
721 
722   PnoSettings pno_settings;
723   PnoNetwork network;
724   PnoNetwork network2;
725   network.is_hidden_ = false;
726   network2.is_hidden_ = false;
727   pno_settings.pno_networks_.push_back(network);
728   pno_settings.pno_networks_.push_back(network2);
729   EXPECT_CALL(
730       scan_utils_,
731       StartScheduledScan(_, _, _, _, _, _, _, Eq(vector<uint32_t>{}), _)).
732           WillOnce(Return(true));
733   EXPECT_TRUE(scanner_impl.startPnoScan(pno_settings, &success).isOk());
734   EXPECT_TRUE(success);
735 }
736 
737 }  // namespace wificond
738 }  // namespace android
739