OSDN Git Service

Create unittest for ScannerImpl
[android-x86/system-connectivity-wificond.git] / tests / scanner_unittest.cpp
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 <wifi_system_test/mock_supplicant_manager.h>
23
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_scan_utils.h"
29
30 using ::android::binder::Status;
31 using ::android::wifi_system::MockInterfaceTool;
32 using ::android::wifi_system::MockSupplicantManager;
33 using ::com::android::server::wifi::wificond::SingleScanSettings;
34 using ::testing::Invoke;
35 using ::testing::NiceMock;
36 using ::testing::Return;
37 using ::testing::_;
38
39 using namespace std::placeholders;
40
41 namespace android {
42 namespace wificond {
43
44 namespace {
45
46 constexpr uint32_t kFakeInterfaceIndex = 12;
47 constexpr uint32_t kFakeWiphyIndex = 5;
48
49 // This is a helper function to mock the behavior of ScanUtils::Scan()
50 // when we expect a error code.
51 // |interface_index_ignored|, |request_random_mac_ignored|, |ssids_ignored|,
52 // |freqs_ignored|, |error_code| are mapped to existing parameters of ScanUtils::Scan().
53 // |mock_error_code| is a additional parameter used for specifying expected error code.
54 bool ReturnErrorCodeForScanRequest(
55     int mock_error_code,
56     uint32_t interface_index_ignored,
57     bool request_random_mac_ignored,
58     const std::vector<std::vector<uint8_t>>& ssids_ignored,
59     const std::vector<uint32_t>& freqs_ignored,
60     int* error_code) {
61   *error_code = mock_error_code;
62   // Returing false because this helper function is used for failure case.
63   return false;
64 }
65
66 }  // namespace
67
68 class ScannerTest : public ::testing::Test {
69  protected:
70   NiceMock<MockNetlinkManager> netlink_manager_;
71   NiceMock<MockNetlinkUtils> netlink_utils_{&netlink_manager_};
72   NiceMock<MockScanUtils> scan_utils_{&netlink_manager_};
73   NiceMock<MockInterfaceTool> if_tool_;
74   NiceMock<MockSupplicantManager> supplicant_manager_;
75   NiceMock<MockClientInterfaceImpl> client_interface_impl_{
76       &if_tool_, &supplicant_manager_, &netlink_utils_, &scan_utils_};
77   ScanCapabilities scan_capabilities_;
78   WiphyFeatures wiphy_features_;
79   ScannerImpl scanner_{
80     kFakeWiphyIndex, kFakeInterfaceIndex,
81     scan_capabilities_, wiphy_features_,
82     &client_interface_impl_,
83     &netlink_utils_, &scan_utils_};
84 };
85
86 TEST_F(ScannerTest, TestSingleScan) {
87   EXPECT_CALL(scan_utils_, Scan(_, _, _, _, _)).WillOnce(Return(true));
88   bool success = false;
89   EXPECT_TRUE(scanner_.scan(SingleScanSettings(), &success).isOk());
90   EXPECT_TRUE(success);
91 }
92
93 TEST_F(ScannerTest, TestSingleScanFailure) {
94   EXPECT_CALL(
95       scan_utils_,
96       Scan(_, _, _, _, _)).
97           WillOnce(Invoke(bind(
98               ReturnErrorCodeForScanRequest, EBUSY, _1, _2, _3, _4, _5)));
99
100   bool success = false;
101   EXPECT_TRUE(scanner_.scan(SingleScanSettings(), &success).isOk());
102   EXPECT_FALSE(success);
103 }
104
105 TEST_F(ScannerTest, TestProcessAbortsOnScanReturningNoDeviceError) {
106   ON_CALL(
107       scan_utils_,
108       Scan(_, _, _, _, _)).
109           WillByDefault(Invoke(bind(
110               ReturnErrorCodeForScanRequest, ENODEV, _1, _2, _3, _4, _5)));
111
112   bool success_ignored;
113   EXPECT_DEATH(
114       scanner_.scan(SingleScanSettings(), &success_ignored),
115       "Driver is in a bad state*");
116 }
117
118 //TODO(b/33452931): Add more test cases for ScannerImpl.
119
120 }  // namespace wificond
121 }  // namespace android