2 * Copyright (C) 2017, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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"
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;
39 using namespace std::placeholders;
46 constexpr uint32_t kFakeInterfaceIndex = 12;
47 constexpr uint32_t kFakeWiphyIndex = 5;
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(
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,
61 *error_code = mock_error_code;
62 // Returing false because this helper function is used for failure case.
68 class ScannerTest : public ::testing::Test {
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_;
80 kFakeWiphyIndex, kFakeInterfaceIndex,
81 scan_capabilities_, wiphy_features_,
82 &client_interface_impl_,
83 &netlink_utils_, &scan_utils_};
86 TEST_F(ScannerTest, TestSingleScan) {
87 EXPECT_CALL(scan_utils_, Scan(_, _, _, _, _)).WillOnce(Return(true));
89 EXPECT_TRUE(scanner_.scan(SingleScanSettings(), &success).isOk());
93 TEST_F(ScannerTest, TestSingleScanFailure) {
98 ReturnErrorCodeForScanRequest, EBUSY, _1, _2, _3, _4, _5)));
100 bool success = false;
101 EXPECT_TRUE(scanner_.scan(SingleScanSettings(), &success).isOk());
102 EXPECT_FALSE(success);
105 TEST_F(ScannerTest, TestProcessAbortsOnScanReturningNoDeviceError) {
108 Scan(_, _, _, _, _)).
109 WillByDefault(Invoke(bind(
110 ReturnErrorCodeForScanRequest, ENODEV, _1, _2, _3, _4, _5)));
112 bool success_ignored;
114 scanner_.scan(SingleScanSettings(), &success_ignored),
115 "Driver is in a bad state*");
118 //TODO(b/33452931): Add more test cases for ScannerImpl.
120 } // namespace wificond
121 } // namespace android