2 * Copyright (C) 2016, 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.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <wifi_system_test/mock_hostapd_manager.h>
23 #include <wifi_system_test/mock_interface_tool.h>
25 #include "wificond/tests/mock_netlink_manager.h"
26 #include "wificond/tests/mock_netlink_utils.h"
28 #include "wificond/ap_interface_impl.h"
30 using android::wifi_system::HostapdManager;
31 using android::wifi_system::MockHostapdManager;
32 using android::wifi_system::MockInterfaceTool;
33 using std::placeholders::_1;
34 using std::placeholders::_2;
35 using std::unique_ptr;
37 using testing::NiceMock;
38 using testing::Invoke;
39 using testing::Return;
40 using testing::Sequence;
48 const char kTestInterfaceName[] = "testwifi0";
49 const uint32_t kTestInterfaceIndex = 42;
50 const uint8_t kFakeMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
52 void CaptureStationEventHandler(
53 OnStationEventHandler* out_handler,
54 uint32_t interface_index,
55 OnStationEventHandler handler) {
56 *out_handler = handler;
59 class ApInterfaceImplTest : public ::testing::Test {
61 unique_ptr<NiceMock<MockInterfaceTool>> if_tool_{
62 new NiceMock<MockInterfaceTool>};
63 unique_ptr<NiceMock<MockHostapdManager>> hostapd_manager_{
64 new NiceMock<MockHostapdManager>};
65 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
66 new NiceMock<MockNetlinkManager>()};
67 unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
68 new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
70 unique_ptr<ApInterfaceImpl> ap_interface_;
72 void SetUp() override {
73 ap_interface_.reset(new ApInterfaceImpl(
78 hostapd_manager_.get()));
80 }; // class ApInterfaceImplTest
84 TEST_F(ApInterfaceImplTest, ShouldReportStartFailure) {
85 EXPECT_CALL(*hostapd_manager_, StartHostapd())
86 .WillOnce(Return(false));
87 EXPECT_FALSE(ap_interface_->StartHostapd());
90 TEST_F(ApInterfaceImplTest, ShouldReportStartSuccess) {
91 EXPECT_CALL(*hostapd_manager_, StartHostapd())
92 .WillOnce(Return(true));
93 EXPECT_TRUE(ap_interface_->StartHostapd());
96 TEST_F(ApInterfaceImplTest, ShouldReportStopFailure) {
97 EXPECT_CALL(*hostapd_manager_, StopHostapd())
98 .WillOnce(Return(false));
99 EXPECT_FALSE(ap_interface_->StopHostapd());
102 TEST_F(ApInterfaceImplTest, ShouldReportStopSuccess) {
103 EXPECT_CALL(*hostapd_manager_, StopHostapd())
104 .WillOnce(Return(true));
105 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kTestInterfaceName), false))
106 .WillOnce(Return(true));
107 EXPECT_CALL(*netlink_utils_, SetInterfaceMode(
109 NetlinkUtils::STATION_MODE)).WillOnce(Return(true));
110 EXPECT_TRUE(ap_interface_->StopHostapd());
111 testing::Mock::VerifyAndClearExpectations(if_tool_.get());
114 TEST_F(ApInterfaceImplTest, ShouldRejectInvalidConfig) {
115 EXPECT_CALL(*hostapd_manager_, CreateHostapdConfig(_, _, _, _, _, _))
116 .WillOnce(Return(""));
117 EXPECT_CALL(*hostapd_manager_, WriteHostapdConfig(_)).Times(0);
118 EXPECT_FALSE(ap_interface_->WriteHostapdConfig(
122 HostapdManager::EncryptionType::kWpa2,
126 TEST_F(ApInterfaceImplTest, CanGetNumberOfAssociatedStations) {
127 OnStationEventHandler handler;
128 EXPECT_CALL(*netlink_utils_,
129 SubscribeStationEvent(kTestInterfaceIndex, _)).
130 WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2)));
132 ap_interface_.reset(new ApInterfaceImpl(
135 netlink_utils_.get(),
137 hostapd_manager_.get()));
139 vector<uint8_t> fake_mac_address(kFakeMacAddress,
140 kFakeMacAddress + sizeof(kFakeMacAddress));
141 EXPECT_EQ(0, ap_interface_->GetNumberOfAssociatedStations());
142 handler(NEW_STATION, fake_mac_address);
143 EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations());
144 handler(NEW_STATION, fake_mac_address);
145 EXPECT_EQ(2, ap_interface_->GetNumberOfAssociatedStations());
146 handler(DEL_STATION, fake_mac_address);
147 EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations());
150 } // namespace wificond
151 } // namespace android