• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016, 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 <memory>
18 #include <vector>
19 
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>
24 
25 #include "wificond/tests/mock_ap_interface_event_callback.h"
26 #include "wificond/tests/mock_netlink_manager.h"
27 #include "wificond/tests/mock_netlink_utils.h"
28 
29 #include "wificond/ap_interface_impl.h"
30 
31 using android::wifi_system::HostapdManager;
32 using android::wifi_system::MockHostapdManager;
33 using android::wifi_system::MockInterfaceTool;
34 using std::placeholders::_1;
35 using std::placeholders::_2;
36 using std::unique_ptr;
37 using std::vector;
38 using testing::NiceMock;
39 using testing::Invoke;
40 using testing::Return;
41 using testing::Sequence;
42 using testing::StrEq;
43 using testing::_;
44 
45 namespace android {
46 namespace wificond {
47 namespace {
48 
49 const char kTestInterfaceName[] = "testwifi0";
50 const uint32_t kTestInterfaceIndex = 42;
51 const uint8_t kFakeMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
52 
CaptureStationEventHandler(OnStationEventHandler * out_handler,uint32_t interface_index,OnStationEventHandler handler)53 void CaptureStationEventHandler(
54     OnStationEventHandler* out_handler,
55     uint32_t interface_index,
56     OnStationEventHandler handler) {
57   *out_handler = handler;
58 }
59 
CaptureChannelSwitchEventHandler(OnChannelSwitchEventHandler * out_handler,uint32_t interface_index,OnChannelSwitchEventHandler handler)60 void CaptureChannelSwitchEventHandler(
61     OnChannelSwitchEventHandler* out_handler,
62     uint32_t interface_index,
63     OnChannelSwitchEventHandler handler) {
64   *out_handler = handler;
65 }
66 
67 class ApInterfaceImplTest : public ::testing::Test {
68  protected:
69   unique_ptr<NiceMock<MockInterfaceTool>> if_tool_{
70       new NiceMock<MockInterfaceTool>};
71   unique_ptr<NiceMock<MockHostapdManager>> hostapd_manager_{
72       new NiceMock<MockHostapdManager>};
73   unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
74       new NiceMock<MockNetlinkManager>()};
75   unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
76       new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
77 
78   unique_ptr<ApInterfaceImpl> ap_interface_;
79 
SetUp()80   void SetUp() override {
81     ap_interface_.reset(new ApInterfaceImpl(
82         kTestInterfaceName,
83         kTestInterfaceIndex,
84         netlink_utils_.get(),
85         if_tool_.get(),
86         hostapd_manager_.get()));
87   }
88 };  // class ApInterfaceImplTest
89 
90 }  // namespace
91 
TEST_F(ApInterfaceImplTest,ShouldReportStartFailure)92 TEST_F(ApInterfaceImplTest, ShouldReportStartFailure) {
93   EXPECT_CALL(*hostapd_manager_, StartHostapd())
94       .WillOnce(Return(false));
95   EXPECT_FALSE(ap_interface_->StartHostapd());
96 }
97 
TEST_F(ApInterfaceImplTest,ShouldReportStartSuccess)98 TEST_F(ApInterfaceImplTest, ShouldReportStartSuccess) {
99   EXPECT_CALL(*hostapd_manager_, StartHostapd())
100       .WillOnce(Return(true));
101   EXPECT_TRUE(ap_interface_->StartHostapd());
102 }
103 
TEST_F(ApInterfaceImplTest,ShouldReportStopFailure)104 TEST_F(ApInterfaceImplTest, ShouldReportStopFailure) {
105   EXPECT_CALL(*hostapd_manager_, StopHostapd())
106       .WillOnce(Return(false));
107   EXPECT_FALSE(ap_interface_->StopHostapd());
108 }
109 
TEST_F(ApInterfaceImplTest,ShouldReportStopSuccess)110 TEST_F(ApInterfaceImplTest, ShouldReportStopSuccess) {
111   EXPECT_CALL(*hostapd_manager_, StopHostapd())
112       .WillOnce(Return(true));
113   EXPECT_CALL(*if_tool_, SetUpState(StrEq(kTestInterfaceName), false))
114       .WillOnce(Return(true));
115   EXPECT_CALL(*netlink_utils_, SetInterfaceMode(
116       kTestInterfaceIndex,
117       NetlinkUtils::STATION_MODE)).WillOnce(Return(true));
118   EXPECT_TRUE(ap_interface_->StopHostapd());
119   testing::Mock::VerifyAndClearExpectations(if_tool_.get());
120 }
121 
TEST_F(ApInterfaceImplTest,CanGetNumberOfAssociatedStations)122 TEST_F(ApInterfaceImplTest, CanGetNumberOfAssociatedStations) {
123   OnStationEventHandler handler;
124   EXPECT_CALL(*netlink_utils_,
125       SubscribeStationEvent(kTestInterfaceIndex, _)).
126           WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2)));
127 
128   ap_interface_.reset(new ApInterfaceImpl(
129         kTestInterfaceName,
130         kTestInterfaceIndex,
131         netlink_utils_.get(),
132         if_tool_.get(),
133         hostapd_manager_.get()));
134 
135   vector<uint8_t> fake_mac_address(kFakeMacAddress,
136                                    kFakeMacAddress + sizeof(kFakeMacAddress));
137   EXPECT_EQ(0, ap_interface_->GetNumberOfAssociatedStations());
138   handler(NEW_STATION, fake_mac_address);
139   EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations());
140   handler(NEW_STATION, fake_mac_address);
141   EXPECT_EQ(2, ap_interface_->GetNumberOfAssociatedStations());
142   handler(DEL_STATION, fake_mac_address);
143   EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations());
144 }
145 
TEST_F(ApInterfaceImplTest,CallbackIsCalledOnNumAssociatedStationsChanged)146 TEST_F(ApInterfaceImplTest, CallbackIsCalledOnNumAssociatedStationsChanged) {
147   OnStationEventHandler handler;
148   EXPECT_CALL(*netlink_utils_, SubscribeStationEvent(kTestInterfaceIndex, _))
149       .WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2)));
150   ap_interface_.reset(new ApInterfaceImpl(
151       kTestInterfaceName, kTestInterfaceIndex, netlink_utils_.get(),
152       if_tool_.get(), hostapd_manager_.get()));
153 
154   EXPECT_CALL(*hostapd_manager_, StartHostapd()).WillOnce(Return(true));
155   auto binder = ap_interface_->GetBinder();
156   sp<MockApInterfaceEventCallback> callback(new MockApInterfaceEventCallback());
157   bool out_success = false;
158   EXPECT_TRUE(binder->startHostapd(callback, &out_success).isOk());
159   EXPECT_TRUE(out_success);
160 
161   vector<uint8_t> fake_mac_address(kFakeMacAddress,
162                                    kFakeMacAddress + sizeof(kFakeMacAddress));
163   EXPECT_CALL(*callback, onNumAssociatedStationsChanged(1));
164   handler(NEW_STATION, fake_mac_address);
165   EXPECT_CALL(*callback, onNumAssociatedStationsChanged(2));
166   handler(NEW_STATION, fake_mac_address);
167   EXPECT_CALL(*callback, onNumAssociatedStationsChanged(1));
168   handler(DEL_STATION, fake_mac_address);
169 }
170 
TEST_F(ApInterfaceImplTest,CallbackIsCalledOnSoftApChannelSwitched)171 TEST_F(ApInterfaceImplTest, CallbackIsCalledOnSoftApChannelSwitched) {
172   OnChannelSwitchEventHandler handler;
173   EXPECT_CALL(*netlink_utils_, SubscribeChannelSwitchEvent(kTestInterfaceIndex, _))
174       .WillOnce(Invoke(bind(CaptureChannelSwitchEventHandler, &handler, _1, _2)));
175   ap_interface_.reset(new ApInterfaceImpl(
176       kTestInterfaceName, kTestInterfaceIndex, netlink_utils_.get(),
177       if_tool_.get(), hostapd_manager_.get()));
178 
179   EXPECT_CALL(*hostapd_manager_, StartHostapd()).WillOnce(Return(true));
180   auto binder = ap_interface_->GetBinder();
181   sp<MockApInterfaceEventCallback> callback(new MockApInterfaceEventCallback());
182   bool out_success = false;
183   EXPECT_TRUE(binder->startHostapd(callback, &out_success).isOk());
184   EXPECT_TRUE(out_success);
185 
186   const uint32_t kTestChannelFrequency = 2437;
187   const ChannelBandwidth kTestChannelBandwidth = ChannelBandwidth::BW_20;
188   EXPECT_CALL(*callback, onSoftApChannelSwitched(kTestChannelFrequency,
189                                                  kTestChannelBandwidth));
190   handler(kTestChannelFrequency, kTestChannelBandwidth);
191 }
192 
193 }  // namespace wificond
194 }  // namespace android
195