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
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <wifi_system_test/mock_hostapd_manager.h>
22 #include <wifi_system_test/mock_interface_tool.h>
23 #include <wifi_system_test/mock_supplicant_manager.h>
24
25 #include "android/net/wifi/IApInterface.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 #include "wificond/server.h"
30
31 using android::net::wifi::IApInterface;
32 using android::net::wifi::IClientInterface;
33 using android::wifi_system::HostapdManager;
34 using android::wifi_system::InterfaceTool;
35 using android::wifi_system::MockHostapdManager;
36 using android::wifi_system::MockInterfaceTool;
37 using android::wifi_system::MockSupplicantManager;
38 using android::wifi_system::SupplicantManager;
39 using std::unique_ptr;
40 using std::vector;
41 using testing::Eq;
42 using testing::Invoke;
43 using testing::NiceMock;
44 using testing::Return;
45 using testing::Sequence;
46 using testing::StrEq;
47 using testing::_;
48
49 using namespace std::placeholders;
50
51 namespace android {
52 namespace wificond {
53 namespace {
54
55 const char kFakeInterfaceName[] = "testif0";
56 const char kFakeInterfaceName1[] = "testif1";
57 const char kFakeInterfaceNameP2p[] = "testif-p2p0";
58 const char kFateInterfaceNameInvalid[] = "testif-invalid";
59 const uint32_t kFakeInterfaceIndex = 34;
60 const uint32_t kFakeInterfaceIndex1 = 36;
61 const uint32_t kFakeInterfaceIndexP2p = 36;
62 const uint8_t kFakeInterfaceMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
63 const uint8_t kFakeInterfaceMacAddress1[] = {0x05, 0x04, 0xef, 0x27, 0x12, 0xff};
64 const uint8_t kFakeInterfaceMacAddressP2p[] = {0x15, 0x24, 0xef, 0x27, 0x12, 0xff};
65
66 // This is a helper function to mock the behavior of
67 // NetlinkUtils::GetInterfaces().
68 // |wiphy_index| is mapped to first parameters of GetInterfaces().
69 // |response| is mapped to second parameters of GetInterfaces().
70 // |mock_response| and |mock_return_value| are additional parameters used
71 // for specifying expected results,
MockGetInterfacesResponse(const vector<InterfaceInfo> & mock_response,bool mock_return_value,uint32_t wiphy_index,vector<InterfaceInfo> * response)72 bool MockGetInterfacesResponse(
73 const vector<InterfaceInfo>& mock_response,
74 bool mock_return_value,
75 uint32_t wiphy_index,
76 vector<InterfaceInfo>* response) {
77 for (auto interface : mock_response) {
78 response->emplace_back(interface);
79 }
80 return mock_return_value;
81 }
82
83 class ServerTest : public ::testing::Test {
84 protected:
SetUp()85 void SetUp() override {
86 ON_CALL(*if_tool_, SetUpState(_, _)).WillByDefault(Return(true));
87 ON_CALL(*netlink_utils_, GetWiphyIndex(_)).WillByDefault(Return(true));
88 ON_CALL(*netlink_utils_, GetInterfaces(_, _))
89 .WillByDefault(Invoke(bind(
90 MockGetInterfacesResponse, mock_interfaces, true, _1, _2)));
91 }
92
93 NiceMock<MockInterfaceTool>* if_tool_ = new NiceMock<MockInterfaceTool>;
94 NiceMock<MockSupplicantManager>* supplicant_manager_ =
95 new NiceMock<MockSupplicantManager>;
96 NiceMock<MockHostapdManager>* hostapd_manager_ =
97 new NiceMock<MockHostapdManager>;
98
99 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
100 new NiceMock<MockNetlinkManager>()};
101
102 unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
103 new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
104 unique_ptr<NiceMock<MockScanUtils>> scan_utils_{
105 new NiceMock<MockScanUtils>(netlink_manager_.get())};
106 const vector<InterfaceInfo> mock_interfaces = {
107 // Client interface
108 InterfaceInfo(
109 kFakeInterfaceIndex,
110 std::string(kFakeInterfaceName),
111 vector<uint8_t>(
112 kFakeInterfaceMacAddress,
113 kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress))),
114 // AP Interface
115 InterfaceInfo(
116 kFakeInterfaceIndex1,
117 std::string(kFakeInterfaceName1),
118 vector<uint8_t>(
119 kFakeInterfaceMacAddress1,
120 kFakeInterfaceMacAddress1 + sizeof(kFakeInterfaceMacAddress1))),
121 // p2p interface
122 InterfaceInfo(
123 kFakeInterfaceIndexP2p,
124 std::string(kFakeInterfaceNameP2p),
125 vector<uint8_t>(
126 kFakeInterfaceMacAddressP2p,
127 kFakeInterfaceMacAddressP2p + sizeof(kFakeInterfaceMacAddressP2p)))
128 };
129
130 Server server_{unique_ptr<InterfaceTool>(if_tool_),
131 unique_ptr<SupplicantManager>(supplicant_manager_),
132 unique_ptr<HostapdManager>(hostapd_manager_),
133 netlink_utils_.get(),
134 scan_utils_.get()};
135 }; // class ServerTest
136
137 } // namespace
138
TEST_F(ServerTest,CanSetUpApInterface)139 TEST_F(ServerTest, CanSetUpApInterface) {
140 sp<IApInterface> ap_if;
141 EXPECT_CALL(*netlink_utils_, SubscribeRegDomainChange(_, _));
142
143 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
144 EXPECT_NE(nullptr, ap_if.get());
145 }
146
TEST_F(ServerTest,CanSupportMultipleInterfaces)147 TEST_F(ServerTest, CanSupportMultipleInterfaces) {
148 sp<IApInterface> ap_if;
149
150 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
151 EXPECT_NE(nullptr, ap_if.get());
152
153 sp<IApInterface> second_ap_if;
154 // We won't throw on a second interface request.
155 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &second_ap_if).isOk());
156 // But this time we won't get an interface back.
157 EXPECT_NE(nullptr, second_ap_if.get());
158 }
159
TEST_F(ServerTest,CanDestroyInterfaces)160 TEST_F(ServerTest, CanDestroyInterfaces) {
161 sp<IApInterface> ap_if;
162
163 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
164
165 // When we tear down the interface, we expect the driver to be unloaded.
166 EXPECT_CALL(*netlink_utils_, UnsubscribeRegDomainChange(_));
167 EXPECT_TRUE(server_.tearDownInterfaces().isOk());
168 // After a tearDown, we should be able to create another interface.
169 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
170 }
171
TEST_F(ServerTest,CanTeardownApInterface)172 TEST_F(ServerTest, CanTeardownApInterface) {
173 sp<IApInterface> ap_if;
174
175 // When we tear down the interface, we expect the iface to be unloaded.
176 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false)));
177
178 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
179 EXPECT_NE(nullptr, ap_if.get());
180
181 // Try to remove an invalid iface name, this should fail.
182 bool success = true;
183 EXPECT_TRUE(server_.tearDownApInterface(
184 kFateInterfaceNameInvalid, &success).isOk());
185 EXPECT_FALSE(success);
186
187 EXPECT_TRUE(server_.tearDownApInterface(kFakeInterfaceName, &success).isOk());
188 EXPECT_TRUE(success);
189 }
190
TEST_F(ServerTest,CanTeardownClientInterface)191 TEST_F(ServerTest, CanTeardownClientInterface) {
192 sp<IClientInterface> client_if;
193
194 // When we tear down the interface, we expect the iface to be unloaded.
195 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false)));
196
197 EXPECT_TRUE(server_.createClientInterface(
198 kFakeInterfaceName, &client_if).isOk());
199 EXPECT_NE(nullptr, client_if.get());
200
201 // Try to remove an invalid iface name, this should fail.
202 bool success = true;
203 EXPECT_TRUE(server_.tearDownClientInterface(
204 kFateInterfaceNameInvalid, &success).isOk());
205 EXPECT_FALSE(success);
206
207 EXPECT_TRUE(server_.tearDownClientInterface(
208 kFakeInterfaceName, &success).isOk());
209 EXPECT_TRUE(success);
210 }
211
TEST_F(ServerTest,ShouldReportEnableFailure)212 TEST_F(ServerTest, ShouldReportEnableFailure) {
213 EXPECT_CALL(*supplicant_manager_, StartSupplicant())
214 .WillOnce(Return(false));
215 bool success = true;
216 EXPECT_TRUE(server_.enableSupplicant(&success).isOk());
217 EXPECT_FALSE(success);
218 }
219
TEST_F(ServerTest,ShouldReportenableSuccess)220 TEST_F(ServerTest, ShouldReportenableSuccess) {
221 EXPECT_CALL(*supplicant_manager_, StartSupplicant())
222 .WillOnce(Return(true));
223 bool success = false;
224 EXPECT_TRUE(server_.enableSupplicant(&success).isOk());
225 EXPECT_TRUE(success);
226 }
227
TEST_F(ServerTest,ShouldReportDisableFailure)228 TEST_F(ServerTest, ShouldReportDisableFailure) {
229 EXPECT_CALL(*supplicant_manager_, StopSupplicant())
230 .WillOnce(Return(false));
231 bool success = true;
232 EXPECT_TRUE(server_.disableSupplicant(&success).isOk());
233 EXPECT_FALSE(success);
234 }
235
TEST_F(ServerTest,ShouldReportDisableSuccess)236 TEST_F(ServerTest, ShouldReportDisableSuccess) {
237 EXPECT_CALL(*supplicant_manager_, StopSupplicant())
238 .WillOnce(Return(true));
239 bool success = false;
240 EXPECT_TRUE(server_.disableSupplicant(&success).isOk());
241 EXPECT_TRUE(success);
242 }
243
TEST_F(ServerTest,CanCreateTeardownApAndClientInterface)244 TEST_F(ServerTest, CanCreateTeardownApAndClientInterface) {
245 sp<IClientInterface> client_if;
246 sp<IApInterface> ap_if;
247
248 EXPECT_TRUE(server_.createClientInterface(kFakeInterfaceName, &client_if).isOk());
249 EXPECT_NE(nullptr, client_if.get());
250
251 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
252 EXPECT_NE(nullptr, ap_if.get());
253
254 // When we tear down the interfaces, we expect the iface to be unloaded.
255 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false)));
256 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName1), Eq(false)));
257
258 bool success = true;
259 // Try to remove an invalid iface name, this should fail.
260 EXPECT_TRUE(server_.tearDownClientInterface(
261 kFateInterfaceNameInvalid, &success).isOk());
262 EXPECT_FALSE(success);
263 EXPECT_TRUE(server_.tearDownApInterface(
264 kFateInterfaceNameInvalid, &success).isOk());
265 EXPECT_FALSE(success);
266
267 EXPECT_TRUE(server_.tearDownClientInterface(
268 kFakeInterfaceName, &success).isOk());
269 EXPECT_TRUE(success);
270
271 EXPECT_TRUE(server_.tearDownApInterface(
272 kFakeInterfaceName1, &success).isOk());
273 EXPECT_TRUE(success);
274 }
275
TEST_F(ServerTest,CanDestroyApAndClientInterfaces)276 TEST_F(ServerTest, CanDestroyApAndClientInterfaces) {
277 sp<IClientInterface> client_if;
278 sp<IApInterface> ap_if;
279
280 EXPECT_TRUE(server_.createClientInterface(
281 kFakeInterfaceName, &client_if).isOk());
282 EXPECT_NE(nullptr, client_if.get());
283
284 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
285 EXPECT_NE(nullptr, ap_if.get());
286
287 // When we tear down the interfaces, we expect the iface to be unloaded.
288 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false))).Times(2);
289 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName1), Eq(false))).Times(2);
290 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceNameP2p), Eq(false)));
291
292 EXPECT_TRUE(server_.tearDownInterfaces().isOk());
293 }
294 } // namespace wificond
295 } // namespace android
296