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 <array>
18 #include <memory>
19 #include <vector>
20
21 #include <linux/if_ether.h>
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <wifi_system_test/mock_interface_tool.h>
26
27 #include "wificond/logging_utils.h"
28 #include "wificond/tests/mock_ap_interface_event_callback.h"
29 #include "wificond/tests/mock_netlink_manager.h"
30 #include "wificond/tests/mock_netlink_utils.h"
31
32 #include "wificond/ap_interface_impl.h"
33
34 using android::wifi_system::MockInterfaceTool;
35 using android::net::wifi::nl80211::NativeWifiClient;
36 using std::array;
37 using std::placeholders::_1;
38 using std::placeholders::_2;
39 using std::unique_ptr;
40 using std::vector;
41 using testing::NiceMock;
42 using testing::Invoke;
43 using testing::Return;
44 using testing::Sequence;
45 using testing::StrEq;
46 using testing::_;
47
48 namespace android {
49 namespace wificond {
50 namespace {
51
52 const char kTestInterfaceName[] = "testwifi0";
53 const uint32_t kTestInterfaceIndex = 42;
54 const array<uint8_t, ETH_ALEN> kFakeMacAddress01 = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
55 const array<uint8_t, ETH_ALEN> kFakeMacAddress02 = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf7};
56
CaptureStationEventHandler(OnStationEventHandler * out_handler,uint32_t interface_index,OnStationEventHandler handler)57 void CaptureStationEventHandler(
58 OnStationEventHandler* out_handler,
59 uint32_t interface_index,
60 OnStationEventHandler handler) {
61 *out_handler = handler;
62 }
63
CaptureChannelSwitchEventHandler(OnChannelSwitchEventHandler * out_handler,uint32_t interface_index,OnChannelSwitchEventHandler handler)64 void CaptureChannelSwitchEventHandler(
65 OnChannelSwitchEventHandler* out_handler,
66 uint32_t interface_index,
67 OnChannelSwitchEventHandler handler) {
68 *out_handler = handler;
69 }
70
71 class ApInterfaceImplTest : public ::testing::Test {
72 protected:
73 unique_ptr<NiceMock<MockInterfaceTool>> if_tool_{
74 new NiceMock<MockInterfaceTool>};
75 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
76 new NiceMock<MockNetlinkManager>()};
77 unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
78 new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
79
80 unique_ptr<ApInterfaceImpl> ap_interface_;
81
SetUp()82 void SetUp() override {
83 ap_interface_.reset(new ApInterfaceImpl(
84 kTestInterfaceName,
85 kTestInterfaceIndex,
86 netlink_utils_.get(),
87 if_tool_.get()));
88 }
89 }; // class ApInterfaceImplTest
90
91 } // namespace
92
TEST_F(ApInterfaceImplTest,CallbackIsCalledOnConnectedClientsChanged)93 TEST_F(ApInterfaceImplTest, CallbackIsCalledOnConnectedClientsChanged) {
94 OnStationEventHandler handler;
95 EXPECT_CALL(*netlink_utils_, SubscribeStationEvent(kTestInterfaceIndex, _))
96 .WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2)));
97 ap_interface_.reset(new ApInterfaceImpl(
98 kTestInterfaceName, kTestInterfaceIndex, netlink_utils_.get(),
99 if_tool_.get()));
100
101 auto binder = ap_interface_->GetBinder();
102 sp<MockApInterfaceEventCallback> callback(new MockApInterfaceEventCallback());
103 bool out_success = false;
104 EXPECT_TRUE(binder->registerCallback(callback, &out_success).isOk());
105 EXPECT_TRUE(out_success);
106
107 array<uint8_t, ETH_ALEN> fake_mac_address_01 = kFakeMacAddress01;
108 array<uint8_t, ETH_ALEN> fake_mac_address_02 = kFakeMacAddress02;
109 EXPECT_CALL(*callback, onConnectedClientsChanged(_,_)).Times(3);
110 handler(NEW_STATION, fake_mac_address_01);
111 handler(NEW_STATION, fake_mac_address_02);
112 handler(DEL_STATION, fake_mac_address_01);
113 }
114
TEST_F(ApInterfaceImplTest,CallbackIsCalledOnConnectedClientsChangedOnlyOnDiff)115 TEST_F(ApInterfaceImplTest, CallbackIsCalledOnConnectedClientsChangedOnlyOnDiff) {
116 OnStationEventHandler handler;
117 EXPECT_CALL(*netlink_utils_, SubscribeStationEvent(kTestInterfaceIndex, _))
118 .WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2)));
119 ap_interface_.reset(new ApInterfaceImpl(
120 kTestInterfaceName, kTestInterfaceIndex, netlink_utils_.get(),
121 if_tool_.get()));
122
123 auto binder = ap_interface_->GetBinder();
124 sp<MockApInterfaceEventCallback> callback(new MockApInterfaceEventCallback());
125 bool out_success = false;
126 EXPECT_TRUE(binder->registerCallback(callback, &out_success).isOk());
127 EXPECT_TRUE(out_success);
128
129 array<uint8_t, ETH_ALEN> fake_mac_address_01 = kFakeMacAddress01;
130 EXPECT_CALL(*callback, onConnectedClientsChanged(_,_)).Times(4);
131 handler(NEW_STATION, fake_mac_address_01);
132 handler(NEW_STATION, fake_mac_address_01);
133 handler(DEL_STATION, fake_mac_address_01);
134 handler(DEL_STATION, fake_mac_address_01);
135 }
136
TEST_F(ApInterfaceImplTest,CallbackIsCalledOnSoftApChannelSwitched)137 TEST_F(ApInterfaceImplTest, CallbackIsCalledOnSoftApChannelSwitched) {
138 OnChannelSwitchEventHandler handler;
139 EXPECT_CALL(*netlink_utils_, SubscribeChannelSwitchEvent(kTestInterfaceIndex, _))
140 .WillOnce(Invoke(bind(CaptureChannelSwitchEventHandler, &handler, _1, _2)));
141 ap_interface_.reset(new ApInterfaceImpl(
142 kTestInterfaceName, kTestInterfaceIndex, netlink_utils_.get(),
143 if_tool_.get()));
144
145 auto binder = ap_interface_->GetBinder();
146 sp<MockApInterfaceEventCallback> callback(new MockApInterfaceEventCallback());
147 bool out_success = false;
148 EXPECT_TRUE(binder->registerCallback(callback, &out_success).isOk());
149 EXPECT_TRUE(out_success);
150
151 const uint32_t kTestChannelFrequency = 2437;
152 const ChannelBandwidth kTestChannelBandwidth = ChannelBandwidth::BW_20;
153 EXPECT_CALL(*callback, onSoftApChannelSwitched(kTestChannelFrequency,
154 kTestChannelBandwidth));
155 handler(kTestChannelFrequency, kTestChannelBandwidth);
156 }
157
158 } // namespace wificond
159 } // namespace android
160