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
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <wifi_system_test/mock_interface_tool.h>
23
24 #include "android/net/wifi/nl80211/IApInterface.h"
25 #include "wificond/tests/mock_netlink_manager.h"
26 #include "wificond/tests/mock_netlink_utils.h"
27 #include "wificond/tests/mock_scan_utils.h"
28 #include "wificond/server.h"
29
30 using android::net::wifi::nl80211::IApInterface;
31 using android::net::wifi::nl80211::IClientInterface;
32 using android::wifi_system::InterfaceTool;
33 using android::wifi_system::MockInterfaceTool;
34 using std::unique_ptr;
35 using std::vector;
36 using testing::Eq;
37 using testing::Invoke;
38 using testing::NiceMock;
39 using testing::Return;
40 using testing::Sequence;
41 using testing::StrEq;
42 using testing::_;
43
44 using namespace std::placeholders;
45
46 namespace android {
47 namespace wificond {
48 namespace {
49
50 const char kFakeInterfaceName[] = "testif0";
51 const char kFakeInterfaceName1[] = "testif1";
52 const char kFakeInterfaceNameP2p[] = "testif-p2p0";
53 const char kFateInterfaceNameInvalid[] = "testif-invalid";
54 const uint32_t kFakeInterfaceIndex = 34;
55 const uint32_t kFakeInterfaceIndex1 = 36;
56 const uint32_t kFakeInterfaceIndexP2p = 36;
57 const uint32_t kFakeWiphyIndex = 0;
58 const std::array<uint8_t, ETH_ALEN> kFakeInterfaceMacAddress = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
59 const std::array<uint8_t, ETH_ALEN> kFakeInterfaceMacAddress1 = {0x05, 0x04, 0xef, 0x27, 0x12, 0xff};
60 const std::array<uint8_t, ETH_ALEN> kFakeInterfaceMacAddressP2p = {0x15, 0x24, 0xef, 0x27, 0x12, 0xff};
61
62 // This is a helper function to mock the behavior of
63 // NetlinkUtils::GetInterfaces().
64 // |wiphy_index| is mapped to first parameters of GetInterfaces().
65 // |response| is mapped to second parameters of GetInterfaces().
66 // |mock_response| and |mock_return_value| are additional parameters used
67 // for specifying expected results,
MockGetInterfacesResponse(const vector<InterfaceInfo> & mock_response,bool mock_return_value,uint32_t wiphy_index,vector<InterfaceInfo> * response)68 bool MockGetInterfacesResponse(
69 const vector<InterfaceInfo>& mock_response,
70 bool mock_return_value,
71 uint32_t wiphy_index,
72 vector<InterfaceInfo>* response) {
73 for (const auto& interface : mock_response) {
74 response->emplace_back(interface);
75 }
76 return mock_return_value;
77 }
78
79 class ServerTest : public ::testing::Test {
80 protected:
SetUp()81 void SetUp() override {
82 ON_CALL(*if_tool_, SetUpState(_, _)).WillByDefault(Return(true));
83 ON_CALL(*netlink_utils_, GetWiphyIndex(_)).WillByDefault(Return(true));
84 ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(Return(true));
85 ON_CALL(*netlink_utils_, GetInterfaces(_, _))
86 .WillByDefault(Invoke(bind(
87 MockGetInterfacesResponse, mock_interfaces, true, _1, _2)));
88 ON_CALL(*netlink_utils_, GetWiphyInfo(0, _, _, _, _))
89 .WillByDefault([](
90 uint32_t wiphy_index,
91 BandInfo* band_info,
92 ScanCapabilities* scan_capabilities,
93 WiphyFeatures* wiphy_features,
94 DriverCapabilities* driver_capabilities) {
95 band_info->band_2g = {1, 2, 3, 4, 5};
96 return true;
97 });
98 ON_CALL(*netlink_utils_, GetWiphyInfo(1, _, _, _, _))
99 .WillByDefault([](
100 uint32_t wiphy_index,
101 BandInfo* band_info,
102 ScanCapabilities* scan_capabilities,
103 WiphyFeatures* wiphy_features,
104 DriverCapabilities* driver_capabilities) {
105 band_info->band_60g = {6, 7, 8, 9, 10};
106 return true;
107 });
108 }
109
110 NiceMock<MockInterfaceTool>* if_tool_ = new NiceMock<MockInterfaceTool>;
111
112 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
113 new NiceMock<MockNetlinkManager>()};
114
115 unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
116 new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
117 unique_ptr<NiceMock<MockScanUtils>> scan_utils_{
118 new NiceMock<MockScanUtils>(netlink_manager_.get())};
119 const vector<InterfaceInfo> mock_interfaces = {
120 // Client interface
121 InterfaceInfo(
122 kFakeInterfaceIndex,
123 kFakeWiphyIndex,
124 std::string(kFakeInterfaceName),
125 std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddress)),
126 // AP Interface
127 InterfaceInfo(
128 kFakeInterfaceIndex1,
129 kFakeWiphyIndex,
130 std::string(kFakeInterfaceName1),
131 std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddress1)),
132 // p2p interface
133 InterfaceInfo(
134 kFakeInterfaceIndexP2p,
135 kFakeWiphyIndex,
136 std::string(kFakeInterfaceNameP2p),
137 std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddressP2p))
138 };
139
140 Server server_{unique_ptr<InterfaceTool>(if_tool_),
141 netlink_utils_.get(),
142 scan_utils_.get()};
143 }; // class ServerTest
144
145 } // namespace
146
TEST_F(ServerTest,CanSetUpApInterface)147 TEST_F(ServerTest, CanSetUpApInterface) {
148 sp<IApInterface> ap_if;
149 EXPECT_CALL(*netlink_utils_, SubscribeRegDomainChange(_, _));
150
151 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
152 EXPECT_NE(nullptr, ap_if.get());
153 }
154
TEST_F(ServerTest,CanSupportMultipleInterfaces)155 TEST_F(ServerTest, CanSupportMultipleInterfaces) {
156 sp<IApInterface> ap_if;
157
158 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
159 EXPECT_NE(nullptr, ap_if.get());
160
161 sp<IApInterface> second_ap_if;
162 // We won't throw on a second interface request.
163 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &second_ap_if).isOk());
164 // But this time we won't get an interface back.
165 EXPECT_NE(nullptr, second_ap_if.get());
166 }
167
TEST_F(ServerTest,CanDestroyInterfaces)168 TEST_F(ServerTest, CanDestroyInterfaces) {
169 sp<IApInterface> ap_if;
170
171 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
172
173 // When we tear down the interface, we expect the driver to be unloaded.
174 EXPECT_CALL(*netlink_utils_, UnsubscribeRegDomainChange(_));
175 EXPECT_TRUE(server_.tearDownInterfaces().isOk());
176 // After a tearDown, we should be able to create another interface.
177 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
178 }
179
TEST_F(ServerTest,CanTeardownApInterface)180 TEST_F(ServerTest, CanTeardownApInterface) {
181 sp<IApInterface> ap_if;
182
183 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
184 EXPECT_NE(nullptr, ap_if.get());
185
186 // Try to remove an invalid iface name, this should fail.
187 bool success = true;
188 EXPECT_TRUE(server_.tearDownApInterface(
189 kFateInterfaceNameInvalid, &success).isOk());
190 EXPECT_FALSE(success);
191
192 EXPECT_TRUE(server_.tearDownApInterface(kFakeInterfaceName, &success).isOk());
193 EXPECT_TRUE(success);
194 }
195
TEST_F(ServerTest,CanTeardownClientInterface)196 TEST_F(ServerTest, CanTeardownClientInterface) {
197 sp<IClientInterface> client_if;
198
199 EXPECT_TRUE(server_.createClientInterface(
200 kFakeInterfaceName, &client_if).isOk());
201 EXPECT_NE(nullptr, client_if.get());
202
203 // Try to remove an invalid iface name, this should fail.
204 bool success = true;
205 EXPECT_TRUE(server_.tearDownClientInterface(
206 kFateInterfaceNameInvalid, &success).isOk());
207 EXPECT_FALSE(success);
208
209 EXPECT_TRUE(server_.tearDownClientInterface(
210 kFakeInterfaceName, &success).isOk());
211 EXPECT_TRUE(success);
212 }
213
TEST_F(ServerTest,CanTeardownMultipleClientInterfacesOnSameWiphy)214 TEST_F(ServerTest, CanTeardownMultipleClientInterfacesOnSameWiphy) {
215 sp<IClientInterface> client_if;
216
217 // add iface 0 on wiphy 0
218 ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
219 [](uint32_t* out_wiphy_index, const std::string& iface_name) {
220 *out_wiphy_index = 0;
221 return true;
222 });
223
224 EXPECT_TRUE(server_.createClientInterface(
225 kFakeInterfaceName, &client_if).isOk());
226 EXPECT_NE(nullptr, client_if.get());
227
228 // bands non-empty
229 {
230 std::optional<std::vector<int32_t>> out_frequencies;
231 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
232 EXPECT_TRUE(out_frequencies.has_value());
233 EXPECT_FALSE(out_frequencies->empty());
234 }
235
236 sp<IClientInterface> client_if1;
237
238 // add iface 1 on wiphy 0
239 ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
240 [](uint32_t* out_wiphy_index, const std::string& iface_name) {
241 *out_wiphy_index = 0;
242 return true;
243 });
244
245 EXPECT_TRUE(server_.createClientInterface(
246 kFakeInterfaceName1, &client_if1).isOk());
247 EXPECT_NE(nullptr, client_if1.get());
248
249 // bands still non-empty
250 {
251 std::optional<std::vector<int32_t>> out_frequencies;
252 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
253 EXPECT_TRUE(out_frequencies.has_value());
254 EXPECT_FALSE(out_frequencies->empty());
255 }
256
257 // tear down iface 0
258 {
259 bool success = true;
260 EXPECT_TRUE(server_.tearDownClientInterface(
261 kFakeInterfaceName, &success).isOk());
262 EXPECT_TRUE(success);
263 }
264
265 // bands still non-empty
266 {
267 std::optional<std::vector<int32_t>> out_frequencies;
268 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
269 EXPECT_TRUE(out_frequencies.has_value());
270 EXPECT_FALSE(out_frequencies->empty());
271 }
272
273 // tear down iface 1
274 {
275 bool success = true;
276 EXPECT_TRUE(server_.tearDownClientInterface(
277 kFakeInterfaceName1, &success).isOk());
278 EXPECT_TRUE(success);
279 }
280
281 // bands now empty
282 {
283 std::optional<std::vector<int32_t>> out_frequencies;
284 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
285 EXPECT_FALSE(out_frequencies.has_value());
286 }
287 }
288
TEST_F(ServerTest,CanTeardownMultipleClientInterfacesOnDifferentWiphy)289 TEST_F(ServerTest, CanTeardownMultipleClientInterfacesOnDifferentWiphy) {
290 sp<IClientInterface> client_if;
291
292 // add iface 0 on wiphy 0, supports 2GHz
293 ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
294 [](uint32_t* out_wiphy_index, const std::string& iface_name) {
295 *out_wiphy_index = 0;
296 return true;
297 });
298
299 EXPECT_TRUE(server_.createClientInterface(
300 kFakeInterfaceName, &client_if).isOk());
301 EXPECT_NE(nullptr, client_if.get());
302
303 // 2G bands non-empty
304 {
305 std::optional<std::vector<int32_t>> out_frequencies;
306 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
307 EXPECT_TRUE(out_frequencies.has_value());
308 EXPECT_FALSE(out_frequencies->empty());
309 }
310
311 // 60G bands empty
312 {
313 std::optional<std::vector<int32_t>> out_frequencies;
314 EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
315 EXPECT_FALSE(out_frequencies.has_value());
316 }
317
318 sp<IClientInterface> client_if1;
319
320 // add iface 1 on wiphy 1, supports 60GHz
321 ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
322 [](uint32_t* out_wiphy_index, const std::string& iface_name) {
323 *out_wiphy_index = 1;
324 return true;
325 });
326
327 EXPECT_TRUE(server_.createClientInterface(
328 kFakeInterfaceName1, &client_if1).isOk());
329 EXPECT_NE(nullptr, client_if1.get());
330
331 // 2G bands still non-empty
332 {
333 std::optional<std::vector<int32_t>> out_frequencies;
334 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
335 EXPECT_TRUE(out_frequencies.has_value());
336 EXPECT_FALSE(out_frequencies->empty());
337 }
338
339 // 60G bands non-empty
340 {
341 std::optional<std::vector<int32_t>> out_frequencies;
342 EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
343 EXPECT_TRUE(out_frequencies.has_value());
344 EXPECT_FALSE(out_frequencies->empty());
345 }
346
347 // tear down iface 0
348 {
349 bool success = true;
350 EXPECT_TRUE(server_.tearDownClientInterface(
351 kFakeInterfaceName, &success).isOk());
352 EXPECT_TRUE(success);
353 }
354
355 // 2G bands now empty
356 {
357 std::optional<std::vector<int32_t>> out_frequencies;
358 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
359 EXPECT_FALSE(out_frequencies.has_value());
360 }
361
362 // 60G bands still non-empty
363 {
364 std::optional<std::vector<int32_t>> out_frequencies;
365 EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
366 EXPECT_TRUE(out_frequencies.has_value());
367 EXPECT_FALSE(out_frequencies->empty());
368 }
369
370 // tear down iface 1
371 {
372 bool success = true;
373 EXPECT_TRUE(server_.tearDownClientInterface(
374 kFakeInterfaceName1, &success).isOk());
375 EXPECT_TRUE(success);
376 }
377
378 // 2G bands still empty
379 {
380 std::optional<std::vector<int32_t>> out_frequencies;
381 EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
382 EXPECT_FALSE(out_frequencies.has_value());
383 }
384
385 // 60G bands now empty
386 {
387 std::optional<std::vector<int32_t>> out_frequencies;
388 EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
389 EXPECT_FALSE(out_frequencies.has_value());
390 }
391 }
392
TEST_F(ServerTest,CanCreateTeardownApAndClientInterface)393 TEST_F(ServerTest, CanCreateTeardownApAndClientInterface) {
394 sp<IClientInterface> client_if;
395 sp<IApInterface> ap_if;
396
397 EXPECT_TRUE(server_.createClientInterface(kFakeInterfaceName, &client_if).isOk());
398 EXPECT_NE(nullptr, client_if.get());
399
400 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
401 EXPECT_NE(nullptr, ap_if.get());
402
403 bool success = true;
404 // Try to remove an invalid iface name, this should fail.
405 EXPECT_TRUE(server_.tearDownClientInterface(
406 kFateInterfaceNameInvalid, &success).isOk());
407 EXPECT_FALSE(success);
408 EXPECT_TRUE(server_.tearDownApInterface(
409 kFateInterfaceNameInvalid, &success).isOk());
410 EXPECT_FALSE(success);
411
412 EXPECT_TRUE(server_.tearDownClientInterface(
413 kFakeInterfaceName, &success).isOk());
414 EXPECT_TRUE(success);
415
416 EXPECT_TRUE(server_.tearDownApInterface(
417 kFakeInterfaceName1, &success).isOk());
418 EXPECT_TRUE(success);
419 }
420
TEST_F(ServerTest,CanDestroyApAndClientInterfaces)421 TEST_F(ServerTest, CanDestroyApAndClientInterfaces) {
422 sp<IClientInterface> client_if;
423 sp<IApInterface> ap_if;
424
425 EXPECT_TRUE(server_.createClientInterface(
426 kFakeInterfaceName, &client_if).isOk());
427 EXPECT_NE(nullptr, client_if.get());
428
429 EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
430 EXPECT_NE(nullptr, ap_if.get());
431
432 // When we tear down the interfaces, we expect the iface to be unloaded.
433 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false))).Times(2);
434 EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName1), Eq(false))).Times(2);
435
436 EXPECT_TRUE(server_.tearDownInterfaces().isOk());
437 }
438 } // namespace wificond
439 } // namespace android
440