• 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 <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             band_info->band_2g = {1, 2, 3, 4, 5};
95             return true;
96           });
97     ON_CALL(*netlink_utils_, GetWiphyInfo(1, _, _, _))
98           .WillByDefault([](
99               uint32_t wiphy_index,
100               BandInfo* band_info,
101               ScanCapabilities* scan_capabilities,
102               WiphyFeatures* wiphy_features) {
103             band_info->band_60g = {6, 7, 8, 9, 10};
104             return true;
105           });
106   }
107 
108   NiceMock<MockInterfaceTool>* if_tool_ = new NiceMock<MockInterfaceTool>;
109 
110   unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
111       new NiceMock<MockNetlinkManager>()};
112 
113   unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
114       new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
115   unique_ptr<NiceMock<MockScanUtils>> scan_utils_{
116       new NiceMock<MockScanUtils>(netlink_manager_.get())};
117   const vector<InterfaceInfo> mock_interfaces = {
118       // Client interface
119       InterfaceInfo(
120           kFakeInterfaceIndex,
121           kFakeWiphyIndex,
122           std::string(kFakeInterfaceName),
123           std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddress)),
124       // AP Interface
125       InterfaceInfo(
126           kFakeInterfaceIndex1,
127           kFakeWiphyIndex,
128           std::string(kFakeInterfaceName1),
129           std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddress1)),
130       // p2p interface
131       InterfaceInfo(
132           kFakeInterfaceIndexP2p,
133           kFakeWiphyIndex,
134           std::string(kFakeInterfaceNameP2p),
135           std::array<uint8_t, ETH_ALEN>(kFakeInterfaceMacAddressP2p))
136   };
137 
138   Server server_{unique_ptr<InterfaceTool>(if_tool_),
139                  netlink_utils_.get(),
140                  scan_utils_.get()};
141 };  // class ServerTest
142 
143 }  // namespace
144 
TEST_F(ServerTest,CanSetUpApInterface)145 TEST_F(ServerTest, CanSetUpApInterface) {
146   sp<IApInterface> ap_if;
147   EXPECT_CALL(*netlink_utils_, SubscribeRegDomainChange(_, _));
148 
149   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
150   EXPECT_NE(nullptr, ap_if.get());
151 }
152 
TEST_F(ServerTest,CanSupportMultipleInterfaces)153 TEST_F(ServerTest, CanSupportMultipleInterfaces) {
154   sp<IApInterface> ap_if;
155 
156   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
157   EXPECT_NE(nullptr, ap_if.get());
158 
159   sp<IApInterface> second_ap_if;
160   // We won't throw on a second interface request.
161   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &second_ap_if).isOk());
162   // But this time we won't get an interface back.
163   EXPECT_NE(nullptr, second_ap_if.get());
164 }
165 
TEST_F(ServerTest,CanDestroyInterfaces)166 TEST_F(ServerTest, CanDestroyInterfaces) {
167   sp<IApInterface> ap_if;
168 
169   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
170 
171   // When we tear down the interface, we expect the driver to be unloaded.
172   EXPECT_CALL(*netlink_utils_, UnsubscribeRegDomainChange(_));
173   EXPECT_TRUE(server_.tearDownInterfaces().isOk());
174   // After a tearDown, we should be able to create another interface.
175   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
176 }
177 
TEST_F(ServerTest,CanTeardownApInterface)178 TEST_F(ServerTest, CanTeardownApInterface) {
179   sp<IApInterface> ap_if;
180 
181   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName, &ap_if).isOk());
182   EXPECT_NE(nullptr, ap_if.get());
183 
184   // Try to remove an invalid iface name, this should fail.
185   bool success = true;
186   EXPECT_TRUE(server_.tearDownApInterface(
187       kFateInterfaceNameInvalid, &success).isOk());
188   EXPECT_FALSE(success);
189 
190   EXPECT_TRUE(server_.tearDownApInterface(kFakeInterfaceName, &success).isOk());
191   EXPECT_TRUE(success);
192 }
193 
TEST_F(ServerTest,CanTeardownClientInterface)194 TEST_F(ServerTest, CanTeardownClientInterface) {
195   sp<IClientInterface> client_if;
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,CanTeardownMultipleClientInterfacesOnSameWiphy)212 TEST_F(ServerTest, CanTeardownMultipleClientInterfacesOnSameWiphy) {
213   sp<IClientInterface> client_if;
214 
215   // add iface 0 on wiphy 0
216   ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
217       [](uint32_t* out_wiphy_index, const std::string& iface_name) {
218         *out_wiphy_index = 0;
219         return true;
220       });
221 
222   EXPECT_TRUE(server_.createClientInterface(
223       kFakeInterfaceName, &client_if).isOk());
224   EXPECT_NE(nullptr, client_if.get());
225 
226   // bands non-empty
227   {
228      std::optional<std::vector<int32_t>> out_frequencies;
229      EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
230      EXPECT_TRUE(out_frequencies.has_value());
231      EXPECT_FALSE(out_frequencies->empty());
232   }
233 
234   sp<IClientInterface> client_if1;
235 
236   // add iface 1 on wiphy 0
237   ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
238       [](uint32_t* out_wiphy_index, const std::string& iface_name) {
239         *out_wiphy_index = 0;
240         return true;
241       });
242 
243   EXPECT_TRUE(server_.createClientInterface(
244       kFakeInterfaceName1, &client_if1).isOk());
245   EXPECT_NE(nullptr, client_if1.get());
246 
247   // bands still non-empty
248   {
249      std::optional<std::vector<int32_t>> out_frequencies;
250      EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
251      EXPECT_TRUE(out_frequencies.has_value());
252      EXPECT_FALSE(out_frequencies->empty());
253   }
254 
255   // tear down iface 0
256   {
257     bool success = true;
258     EXPECT_TRUE(server_.tearDownClientInterface(
259         kFakeInterfaceName, &success).isOk());
260     EXPECT_TRUE(success);
261   }
262 
263   // bands still non-empty
264   {
265     std::optional<std::vector<int32_t>> out_frequencies;
266     EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
267     EXPECT_TRUE(out_frequencies.has_value());
268     EXPECT_FALSE(out_frequencies->empty());
269   }
270 
271   // tear down iface 1
272   {
273     bool success = true;
274     EXPECT_TRUE(server_.tearDownClientInterface(
275         kFakeInterfaceName1, &success).isOk());
276     EXPECT_TRUE(success);
277   }
278 
279   // bands now empty
280   {
281     std::optional<std::vector<int32_t>> out_frequencies;
282     EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
283     EXPECT_FALSE(out_frequencies.has_value());
284   }
285 }
286 
TEST_F(ServerTest,CanTeardownMultipleClientInterfacesOnDifferentWiphy)287 TEST_F(ServerTest, CanTeardownMultipleClientInterfacesOnDifferentWiphy) {
288   sp<IClientInterface> client_if;
289 
290   // add iface 0 on wiphy 0, supports 2GHz
291   ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
292       [](uint32_t* out_wiphy_index, const std::string& iface_name) {
293         *out_wiphy_index = 0;
294         return true;
295       });
296 
297   EXPECT_TRUE(server_.createClientInterface(
298       kFakeInterfaceName, &client_if).isOk());
299   EXPECT_NE(nullptr, client_if.get());
300 
301   // 2G bands non-empty
302   {
303      std::optional<std::vector<int32_t>> out_frequencies;
304      EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
305      EXPECT_TRUE(out_frequencies.has_value());
306      EXPECT_FALSE(out_frequencies->empty());
307   }
308 
309   // 60G bands empty
310   {
311      std::optional<std::vector<int32_t>> out_frequencies;
312      EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
313      EXPECT_FALSE(out_frequencies.has_value());
314   }
315 
316   sp<IClientInterface> client_if1;
317 
318   // add iface 1 on wiphy 1, supports 60GHz
319   ON_CALL(*netlink_utils_, GetWiphyIndex(_, _)).WillByDefault(
320       [](uint32_t* out_wiphy_index, const std::string& iface_name) {
321         *out_wiphy_index = 1;
322         return true;
323       });
324 
325   EXPECT_TRUE(server_.createClientInterface(
326       kFakeInterfaceName1, &client_if1).isOk());
327   EXPECT_NE(nullptr, client_if1.get());
328 
329   // 2G bands still non-empty
330   {
331      std::optional<std::vector<int32_t>> out_frequencies;
332      EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
333      EXPECT_TRUE(out_frequencies.has_value());
334      EXPECT_FALSE(out_frequencies->empty());
335   }
336 
337   // 60G bands non-empty
338   {
339      std::optional<std::vector<int32_t>> out_frequencies;
340      EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
341      EXPECT_TRUE(out_frequencies.has_value());
342      EXPECT_FALSE(out_frequencies->empty());
343   }
344 
345   // tear down iface 0
346   {
347     bool success = true;
348     EXPECT_TRUE(server_.tearDownClientInterface(
349         kFakeInterfaceName, &success).isOk());
350     EXPECT_TRUE(success);
351   }
352 
353   // 2G bands now empty
354   {
355     std::optional<std::vector<int32_t>> out_frequencies;
356     EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
357     EXPECT_FALSE(out_frequencies.has_value());
358   }
359 
360   // 60G bands still non-empty
361   {
362      std::optional<std::vector<int32_t>> out_frequencies;
363      EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
364      EXPECT_TRUE(out_frequencies.has_value());
365      EXPECT_FALSE(out_frequencies->empty());
366   }
367 
368   // tear down iface 1
369   {
370     bool success = true;
371     EXPECT_TRUE(server_.tearDownClientInterface(
372         kFakeInterfaceName1, &success).isOk());
373     EXPECT_TRUE(success);
374   }
375 
376   // 2G bands still empty
377   {
378     std::optional<std::vector<int32_t>> out_frequencies;
379     EXPECT_TRUE(server_.getAvailable2gChannels(&out_frequencies).isOk());
380     EXPECT_FALSE(out_frequencies.has_value());
381   }
382 
383   // 60G bands now empty
384   {
385      std::optional<std::vector<int32_t>> out_frequencies;
386      EXPECT_TRUE(server_.getAvailable60gChannels(&out_frequencies).isOk());
387      EXPECT_FALSE(out_frequencies.has_value());
388   }
389 }
390 
TEST_F(ServerTest,CanCreateTeardownApAndClientInterface)391 TEST_F(ServerTest, CanCreateTeardownApAndClientInterface) {
392   sp<IClientInterface> client_if;
393   sp<IApInterface> ap_if;
394 
395   EXPECT_TRUE(server_.createClientInterface(kFakeInterfaceName, &client_if).isOk());
396   EXPECT_NE(nullptr, client_if.get());
397 
398   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
399   EXPECT_NE(nullptr, ap_if.get());
400 
401   bool success = true;
402   // Try to remove an invalid iface name, this should fail.
403   EXPECT_TRUE(server_.tearDownClientInterface(
404       kFateInterfaceNameInvalid, &success).isOk());
405   EXPECT_FALSE(success);
406   EXPECT_TRUE(server_.tearDownApInterface(
407       kFateInterfaceNameInvalid, &success).isOk());
408   EXPECT_FALSE(success);
409 
410   EXPECT_TRUE(server_.tearDownClientInterface(
411       kFakeInterfaceName, &success).isOk());
412   EXPECT_TRUE(success);
413 
414   EXPECT_TRUE(server_.tearDownApInterface(
415       kFakeInterfaceName1, &success).isOk());
416   EXPECT_TRUE(success);
417 }
418 
TEST_F(ServerTest,CanDestroyApAndClientInterfaces)419 TEST_F(ServerTest, CanDestroyApAndClientInterfaces) {
420   sp<IClientInterface> client_if;
421   sp<IApInterface> ap_if;
422 
423   EXPECT_TRUE(server_.createClientInterface(
424       kFakeInterfaceName, &client_if).isOk());
425   EXPECT_NE(nullptr, client_if.get());
426 
427   EXPECT_TRUE(server_.createApInterface(kFakeInterfaceName1, &ap_if).isOk());
428   EXPECT_NE(nullptr, ap_if.get());
429 
430   // When we tear down the interfaces, we expect the iface to be unloaded.
431   EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName), Eq(false))).Times(2);
432   EXPECT_CALL(*if_tool_, SetUpState(StrEq(kFakeInterfaceName1), Eq(false))).Times(2);
433 
434   EXPECT_TRUE(server_.tearDownInterfaces().isOk());
435 }
436 }  // namespace wificond
437 }  // namespace android
438