• 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 #include <string>
20 #include <vector>
21 
22 #include <linux/netlink.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include "wificond/net/kernel-header-latest/nl80211.h"
27 #include "wificond/net/netlink_utils.h"
28 #include "wificond/tests/mock_netlink_manager.h"
29 
30 using std::string;
31 using std::unique_ptr;
32 using std::vector;
33 using testing::DoAll;
34 using testing::NiceMock;
35 using testing::Return;
36 using testing::_;
37 
38 namespace android {
39 namespace wificond {
40 
41 namespace {
42 
43 constexpr uint8_t kFakeMaxNumScanSSIDs = 10;
44 constexpr uint8_t kFakeMaxNumSchedScanSSIDs = 16;
45 constexpr uint8_t kFakeMaxMatchSets = 18;
46 constexpr uint8_t kFakeMaxNumScanPlans = 8;
47 constexpr uint8_t kFakeMaxScanPlanIntervals = 80;
48 constexpr uint8_t kFakeMaxScanPlanIterations = 10;
49 constexpr uint16_t kFakeFamilyId = 14;
50 constexpr uint32_t kFakeFrequency1 = 2412;
51 constexpr uint32_t kFakeFrequency2 = 2437;
52 constexpr uint32_t kFakeFrequency3 = 2484;
53 constexpr uint32_t kFakeFrequency4 = 5200;
54 constexpr uint32_t kFakeFrequency5 = 5400;
55 constexpr uint32_t kFakeFrequency6 = 5600;
56 // 802.11p channel which is not valid for wifi usage.
57 constexpr uint32_t kFakeInvalidFrequency = 5950;
58 constexpr uint32_t kFakeSequenceNumber = 162;
59 constexpr uint32_t kFakeProtocolFeatures = 0x02;
60 constexpr uint16_t kFakeWiphyIndex = 8;
61 constexpr uint16_t kFakeWiphyIndex1 = 10;
62 constexpr uint64_t kFakeCookie = 42;
63 constexpr int kFakeErrorCode = EIO;
64 constexpr int32_t kFakeMcs = 5;
65 constexpr bool kFakeSupportsRandomMacOneshotScan = true;
66 constexpr bool kFakeSupportsRandomMacSchedScan = false;
67 const char kFakeInterfaceName[] = "testif0";
68 const char kFakeCountryCode[] = "US";
69 const uint32_t kFakeInterfaceIndex = 34;
70 const uint32_t kFakeInterfaceIndex1 = 36;
71 const std::array<uint8_t, ETH_ALEN> kFakeInterfaceMacAddress = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
72 const std::array<uint8_t, ETH_ALEN> kFakeInterfaceMacAddress1 = {0x05, 0x04, 0xef, 0x27, 0x12, 0xff};
73 const uint8_t kFakeExtFeaturesForLowSpanScan[] = {0x0, 0x0, 0x40};
74 const uint8_t kFakeExtFeaturesForLowPowerScan[] = {0x0, 0x0, 0x80};
75 const uint8_t kFakeExtFeaturesForHighAccuracy[] = {0x0, 0x0, 0x0, 0x1};
76 const uint8_t kFakeExtFeaturesForAllScanType[] = {0x0, 0x0, 0xC0, 0x1};
77 const uint8_t kFakeFrame[] = {0x00, 0x01, 0x02, 0x03};
78 constexpr bool k11nSupported = true;
79 constexpr bool k11acSupported = true;
80 constexpr bool k11axSupported = true;
81 constexpr uint8_t kMaxTxStreams = 4;
82 constexpr uint8_t kMaxRxStreams = 5;
83 constexpr bool k160MHzSupported = true;
84 constexpr bool k80p80MHzSupported = false;
85 const uint8_t kHtMcsSet[] = {0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
86                              0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0};
87 const uint8_t kVhtMcsSet[] = {0xaa, 0xfe, 0xff, 0xff, 0xaa, 0xff, 0xff, 0xff};
88 const uint8_t kHeMcsSet[] = {0xaa, 0xfe, 0xaa, 0xff};
89 const uint8_t kVhtCap[] = {0x4, 0x0, 0x0, 0x0};
90 const uint8_t kHeCapPhy[] = {0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
91 
92 // Currently, control messages are only created by the kernel and sent to us.
93 // Therefore NL80211Packet doesn't have corresponding constructor.
94 // For test we manually create control messages using this helper function.
CreateControlMessageError(int error_code)95 NL80211Packet CreateControlMessageError(int error_code) {
96   vector<uint8_t> data;
97   data.resize(NLMSG_HDRLEN + NLA_ALIGN(sizeof(int)), 0);
98   // Initialize length field.
99   nlmsghdr* nl_header = reinterpret_cast<nlmsghdr*>(data.data());
100   nl_header->nlmsg_len = data.size();
101   nl_header->nlmsg_type = NLMSG_ERROR;
102   nl_header->nlmsg_seq = kFakeSequenceNumber;
103   nl_header->nlmsg_pid = getpid();
104   int* error_field = reinterpret_cast<int*>(data.data() + NLMSG_HDRLEN);
105   *error_field = -error_code;
106 
107   return NL80211Packet(data);
108 }
109 
CreateControlMessageAck()110 NL80211Packet CreateControlMessageAck() {
111   return CreateControlMessageError(0);
112 }
113 
AppendScanCapabilitiesAttributes(NL80211Packet * packet,bool supports_scan_plan)114 void AppendScanCapabilitiesAttributes(NL80211Packet* packet,
115                                       bool supports_scan_plan) {
116   packet->AddAttribute(NL80211Attr<uint8_t>(NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
117                                             kFakeMaxNumScanSSIDs));
118   packet->AddAttribute(NL80211Attr<uint8_t>(
119       NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
120       kFakeMaxNumSchedScanSSIDs));
121   packet->AddAttribute(NL80211Attr<uint8_t>(NL80211_ATTR_MAX_MATCH_SETS,
122                                             kFakeMaxMatchSets));
123   if (supports_scan_plan) {
124     packet->AddAttribute(NL80211Attr<uint32_t>(
125         NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
126         kFakeMaxNumScanPlans));
127     packet->AddAttribute(NL80211Attr<uint32_t>(
128         NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
129         kFakeMaxScanPlanIntervals));
130     packet->AddAttribute(NL80211Attr<uint32_t>(
131         NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
132         kFakeMaxScanPlanIterations));
133   }
134 }
135 
136 
GenerateBandsAttributeForHtCapa()137 NL80211Attr<std::vector<uint8_t>> GenerateBandsAttributeForHtCapa() {
138   std::vector<uint8_t> ht_cap(2, 0);
139   return NL80211Attr<std::vector<uint8_t>>(NL80211_BAND_ATTR_HT_CAPA,
140                                            ht_cap);
141 }
142 
143 
GenerateBandsAttributeForVhtCapa()144 NL80211Attr<std::vector<uint8_t>> GenerateBandsAttributeForVhtCapa() {
145   std::vector<uint8_t> vht_cap(kVhtCap, kVhtCap + sizeof(kVhtCap));
146   return NL80211Attr<std::vector<uint8_t>>(NL80211_BAND_ATTR_VHT_CAPA,
147                                            vht_cap);
148 }
149 
GenerateBandsAttributeForHtMcsSet()150 NL80211Attr<std::vector<uint8_t>> GenerateBandsAttributeForHtMcsSet() {
151   std::vector<uint8_t> ht_mcs_set(kHtMcsSet, kHtMcsSet + sizeof(kHtMcsSet));
152   return NL80211Attr<std::vector<uint8_t>>(NL80211_BAND_ATTR_HT_MCS_SET,
153                                            ht_mcs_set);
154 }
155 
GenerateBandsAttributeForVhtMcsSet()156 NL80211Attr<std::vector<uint8_t>> GenerateBandsAttributeForVhtMcsSet() {
157   std::vector<uint8_t> vht_mcs_set(kVhtMcsSet, kVhtMcsSet + sizeof(kVhtMcsSet));
158   return NL80211Attr<std::vector<uint8_t>>(NL80211_BAND_ATTR_VHT_MCS_SET,
159                                            vht_mcs_set);
160 }
161 
GenerateBandsAttributeForIfTypeData()162 NL80211NestedAttr GenerateBandsAttributeForIfTypeData() {
163   NL80211NestedAttr if_type_data(NL80211_BAND_ATTR_IFTYPE_DATA);
164 
165   NL80211NestedAttr if_type_data1(1);
166   std::vector<uint8_t> he_cap_phy(kHeCapPhy, kHeCapPhy + sizeof(kHeCapPhy));
167   std::vector<uint8_t> he_mcs_set(kHeMcsSet, kHeMcsSet + sizeof(kHeMcsSet));
168 
169   if_type_data1.AddAttribute(NL80211Attr<std::vector<uint8_t>>(
170       NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY, he_cap_phy));
171   if_type_data1.AddAttribute(NL80211Attr<std::vector<uint8_t>>(
172       NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET, he_mcs_set));
173   if_type_data.AddAttribute(if_type_data1);
174   return if_type_data;
175 }
176 
AppendBandPhyAttributes(NL80211NestedAttr * band_attr)177 void AppendBandPhyAttributes(NL80211NestedAttr* band_attr) {
178   band_attr->AddAttribute(GenerateBandsAttributeForHtCapa());
179   band_attr->AddAttribute(GenerateBandsAttributeForHtMcsSet());
180   band_attr->AddAttribute(GenerateBandsAttributeForVhtCapa());
181   band_attr->AddAttribute(GenerateBandsAttributeForVhtMcsSet());
182 
183   band_attr->AddAttribute(GenerateBandsAttributeForIfTypeData());
184 }
185 
GenerateBandsAttributeFor2g()186 NL80211NestedAttr GenerateBandsAttributeFor2g() {
187   NL80211NestedAttr freq_2g_1(1);
188   NL80211NestedAttr freq_2g_2(2);
189   NL80211NestedAttr freq_2g_3(3);
190   freq_2g_1.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
191                                                kFakeFrequency1));
192   freq_2g_2.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
193                                                kFakeFrequency2));
194   freq_2g_3.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
195                                                kFakeFrequency3));
196 
197   NL80211NestedAttr band_2g_freqs(NL80211_BAND_ATTR_FREQS);
198   band_2g_freqs.AddAttribute(freq_2g_1);
199   band_2g_freqs.AddAttribute(freq_2g_2);
200   band_2g_freqs.AddAttribute(freq_2g_3);
201 
202   NL80211NestedAttr band_2g_attr(1);
203   band_2g_attr.AddAttribute(band_2g_freqs);
204   AppendBandPhyAttributes(&band_2g_attr);
205 
206   NL80211NestedAttr band_attr(NL80211_ATTR_WIPHY_BANDS);
207   band_attr.AddAttribute(band_2g_attr);
208   return band_attr;
209 }
210 
GenerateBandsAttributeFor5gAndDfs()211 NL80211NestedAttr GenerateBandsAttributeFor5gAndDfs() {
212   NL80211NestedAttr freq_5g_1(4);
213   NL80211NestedAttr freq_5g_2(5);
214   NL80211NestedAttr freq_5g_3(6);
215   NL80211NestedAttr freq_dfs_1(7);
216   freq_5g_1.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
217                                                kFakeFrequency4));
218   freq_5g_2.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
219                                                kFakeFrequency5));
220   // This channel is passive only.
221   freq_5g_2.AddFlagAttribute(NL80211_FREQUENCY_ATTR_NO_IR);
222 
223   // This channel is not valid for wifi usage.
224   // We should not include it in the parse result.
225   freq_5g_3.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
226                                                kFakeInvalidFrequency));
227 
228   // DFS frequency.
229   freq_dfs_1.AddAttribute(NL80211Attr<uint32_t>(NL80211_FREQUENCY_ATTR_FREQ,
230                                                 kFakeFrequency6));
231   freq_dfs_1.AddAttribute(NL80211Attr<uint32_t>(
232       NL80211_FREQUENCY_ATTR_DFS_STATE,
233       NL80211_DFS_USABLE));
234 
235   NL80211NestedAttr band_5g_freqs(NL80211_BAND_ATTR_FREQS);
236   band_5g_freqs.AddAttribute(freq_5g_1);
237   band_5g_freqs.AddAttribute(freq_5g_2);
238   band_5g_freqs.AddAttribute(freq_5g_3);
239   band_5g_freqs.AddAttribute(freq_dfs_1);
240 
241   NL80211NestedAttr band_5g_attr(1);
242   band_5g_attr.AddAttribute(band_5g_freqs);
243   AppendBandPhyAttributes(&band_5g_attr);
244 
245   NL80211NestedAttr band_attr(NL80211_ATTR_WIPHY_BANDS);
246   band_attr.AddAttribute(band_5g_attr);
247   return band_attr;
248 }
249 
AppendBandInfoAttributes(NL80211Packet * packet)250 void AppendBandInfoAttributes(NL80211Packet* packet) {
251   NL80211NestedAttr attr_2g = GenerateBandsAttributeFor2g();
252   NL80211NestedAttr attr_5g_and_dfs = GenerateBandsAttributeFor5gAndDfs();
253   attr_2g.Merge(attr_5g_and_dfs);
254 
255   packet->AddAttribute(attr_2g);
256 }
257 
AppendWiphyFeaturesAttributes(NL80211Packet * packet)258 void AppendWiphyFeaturesAttributes(NL80211Packet* packet) {
259   uint32_t wiphy_features = 0;
260   if (kFakeSupportsRandomMacOneshotScan) {
261       wiphy_features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
262   }
263   if (kFakeSupportsRandomMacSchedScan) {
264       wiphy_features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
265   }
266   packet->AddAttribute(
267       NL80211Attr<uint32_t>(NL80211_ATTR_FEATURE_FLAGS, wiphy_features));
268 }
269 
AppendWiphyExtFeaturesAttributes(NL80211Packet * packet,bool support_low_span,bool support_low_power,bool support_high_accuracy,bool support_all)270 void AppendWiphyExtFeaturesAttributes(NL80211Packet* packet,
271                                       bool support_low_span,
272                                       bool support_low_power,
273                                       bool support_high_accuracy,
274                                       bool support_all) {
275   ASSERT_LE(support_low_span + support_low_power + support_high_accuracy, 1);
276   std::vector<uint8_t> ext_feature_flags_bytes;
277   if (support_low_span) {
278     ext_feature_flags_bytes =
279         std::vector<uint8_t>(kFakeExtFeaturesForLowSpanScan,
280                              kFakeExtFeaturesForLowSpanScan +
281                              sizeof(kFakeExtFeaturesForLowSpanScan));
282   } else if (support_low_power) {
283     ext_feature_flags_bytes =
284         std::vector<uint8_t>(kFakeExtFeaturesForLowPowerScan,
285                              kFakeExtFeaturesForLowPowerScan +
286                              sizeof(kFakeExtFeaturesForLowPowerScan));
287   } else if (support_high_accuracy) {
288     ext_feature_flags_bytes =
289         std::vector<uint8_t>(kFakeExtFeaturesForHighAccuracy,
290                              kFakeExtFeaturesForHighAccuracy +
291                              sizeof(kFakeExtFeaturesForHighAccuracy));
292   } else if (support_all) {
293     ext_feature_flags_bytes =
294         std::vector<uint8_t>(kFakeExtFeaturesForAllScanType,
295                              kFakeExtFeaturesForAllScanType +
296                              sizeof(kFakeExtFeaturesForAllScanType));
297   }
298   packet->AddAttribute(
299       NL80211Attr<std::vector<uint8_t>>(NL80211_ATTR_EXT_FEATURES,
300                                         ext_feature_flags_bytes));
301 }
302 
VerifyScanCapabilities(const ScanCapabilities & scan_capabilities,bool supports_scan_plan)303 void VerifyScanCapabilities(const ScanCapabilities& scan_capabilities,
304                             bool supports_scan_plan) {
305   EXPECT_EQ(scan_capabilities.max_num_scan_ssids,
306             kFakeMaxNumScanSSIDs);
307   EXPECT_EQ(scan_capabilities.max_num_sched_scan_ssids,
308             kFakeMaxNumSchedScanSSIDs);
309   EXPECT_EQ(scan_capabilities.max_match_sets,
310             kFakeMaxMatchSets);
311   if (supports_scan_plan) {
312     EXPECT_EQ(scan_capabilities.max_num_scan_plans,
313               kFakeMaxNumScanPlans);
314     EXPECT_EQ(scan_capabilities.max_scan_plan_interval,
315               kFakeMaxScanPlanIntervals);
316     EXPECT_EQ(scan_capabilities.max_scan_plan_iterations,
317               kFakeMaxScanPlanIterations);
318   } else {
319     EXPECT_EQ(scan_capabilities.max_num_scan_plans, (unsigned int) 0);
320     EXPECT_EQ(scan_capabilities.max_scan_plan_interval, (unsigned int) 0);
321     EXPECT_EQ(scan_capabilities.max_scan_plan_iterations, (unsigned int) 0);
322   }
323 }
324 
VerifyBandInfo(const BandInfo & band_info)325 void VerifyBandInfo(const BandInfo& band_info) {
326   vector<uint32_t> band_2g_expected = {kFakeFrequency1,
327       kFakeFrequency2, kFakeFrequency3};
328   vector<uint32_t> band_5g_expected = {kFakeFrequency4};
329   // Frequency5 is doesn't belong to a DFS channel. However, our convetion
330   // requires us to return any passive only channels in DFS band.
331   vector<uint32_t> band_dfs_expected = {kFakeFrequency5, kFakeFrequency6};
332   EXPECT_EQ(band_info.band_2g, band_2g_expected);
333   EXPECT_EQ(band_info.band_5g, band_5g_expected);
334   EXPECT_EQ(band_info.band_dfs, band_dfs_expected);
335   EXPECT_EQ(band_info.is_80211n_supported, k11nSupported);
336   EXPECT_EQ(band_info.is_80211ac_supported, k11acSupported);
337   EXPECT_EQ(band_info.is_80211ax_supported, k11axSupported);
338   EXPECT_EQ(band_info.is_160_mhz_supported, k160MHzSupported);
339   EXPECT_EQ(band_info.is_80p80_mhz_supported, k80p80MHzSupported);
340   EXPECT_EQ(band_info.max_tx_streams, kMaxTxStreams);
341   EXPECT_EQ(band_info.max_rx_streams, kMaxRxStreams);
342 }
343 
VerifyWiphyFeatures(const WiphyFeatures & wiphy_features)344 void VerifyWiphyFeatures(const WiphyFeatures& wiphy_features) {
345   EXPECT_TRUE(wiphy_features.supports_random_mac_oneshot_scan);
346   EXPECT_FALSE(wiphy_features.supports_random_mac_sched_scan);
347 }
348 
349 }  // namespace
350 
351 // This mocks the behavior of SendMessageAndGetResponses(), which returns a
352 // vector of NL80211Packet using passed in pointer.
ACTION_P(MakeupResponse,response)353 ACTION_P(MakeupResponse, response) {
354   // arg1 is the second parameter: vector<unique_ptr<const NL80211Packet>>* responses.
355   for (auto& pkt : response) {
356     arg1->push_back(unique_ptr<NL80211Packet>(new NL80211Packet(pkt)));
357   }
358 }
359 
360 class NetlinkUtilsTest : public ::testing::Test {
361  protected:
362   std::unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_;
363   std::unique_ptr<NetlinkUtils> netlink_utils_;
364 
SetUp()365   virtual void SetUp() {
366     netlink_manager_.reset(new NiceMock<MockNetlinkManager>());
367     netlink_utils_.reset(new NetlinkUtils(netlink_manager_.get()));
368 
369     ON_CALL(*netlink_manager_,
370             GetSequenceNumber()).WillByDefault(Return(kFakeSequenceNumber));
371     ON_CALL(*netlink_manager_,
372             GetFamilyId()).WillByDefault(Return(kFakeFamilyId));
373   }
374 
SetSplitWiphyDumpSupported(bool supported)375   void SetSplitWiphyDumpSupported(bool supported) {
376     netlink_utils_->supports_split_wiphy_dump_ = supported;
377   }
378 
379 };
380 
TEST_F(NetlinkUtilsTest,CanGetWiphyIndex)381 TEST_F(NetlinkUtilsTest, CanGetWiphyIndex) {
382   NL80211Packet new_wiphy(
383       netlink_manager_->GetFamilyId(),
384       NL80211_CMD_NEW_WIPHY,
385       netlink_manager_->GetSequenceNumber(),
386       getpid());
387   // Insert wiphy_index attribute.
388   NL80211Attr<uint32_t> wiphy_index_attr(NL80211_ATTR_WIPHY, kFakeWiphyIndex);
389   new_wiphy.AddAttribute(wiphy_index_attr);
390   // Mock a valid response from kernel.
391   vector<NL80211Packet> response = {new_wiphy};
392 
393   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
394       WillOnce(DoAll(MakeupResponse(response), Return(true)));
395 
396   uint32_t wiphy_index;
397   EXPECT_TRUE(netlink_utils_->GetWiphyIndex(&wiphy_index));
398   EXPECT_EQ(kFakeWiphyIndex, wiphy_index);
399 }
400 
TEST_F(NetlinkUtilsTest,CanHandleGetWiphyIndexError)401 TEST_F(NetlinkUtilsTest, CanHandleGetWiphyIndexError) {
402   // Mock an error response from kernel.
403   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
404 
405   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
406       WillOnce(DoAll(MakeupResponse(response), Return(true)));
407 
408   uint32_t wiphy_index;
409   EXPECT_FALSE(netlink_utils_->GetWiphyIndex(&wiphy_index));
410 }
411 
TEST_F(NetlinkUtilsTest,CanSetIntrerfaceMode)412 TEST_F(NetlinkUtilsTest, CanSetIntrerfaceMode) {
413   // Mock a ACK response from kernel.
414   vector<NL80211Packet> response = {CreateControlMessageAck()};
415 
416   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
417       WillOnce(DoAll(MakeupResponse(response), Return(true)));
418 
419   EXPECT_TRUE(netlink_utils_->SetInterfaceMode(kFakeInterfaceIndex,
420                                                NetlinkUtils::STATION_MODE));
421 }
422 
TEST_F(NetlinkUtilsTest,CanHandleSetIntrerfaceModeError)423 TEST_F(NetlinkUtilsTest, CanHandleSetIntrerfaceModeError) {
424   // Mock an error response from kernel.
425   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
426 
427   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
428       WillOnce(DoAll(MakeupResponse(response), Return(true)));
429 
430   EXPECT_FALSE(netlink_utils_->SetInterfaceMode(kFakeInterfaceIndex,
431                                                 NetlinkUtils::STATION_MODE));
432 }
433 
TEST_F(NetlinkUtilsTest,CanGetInterfaces)434 TEST_F(NetlinkUtilsTest, CanGetInterfaces) {
435   NL80211Packet new_interface(
436       netlink_manager_->GetFamilyId(),
437       NL80211_CMD_NEW_INTERFACE,
438       netlink_manager_->GetSequenceNumber(),
439       getpid());
440   // Insert interface name attribute.
441   NL80211Attr<string> if_name_attr(NL80211_ATTR_IFNAME, string(kFakeInterfaceName));
442   new_interface.AddAttribute(if_name_attr);
443   // Insert interface index attribute.
444   NL80211Attr<uint32_t> if_index_attr(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex);
445   new_interface.AddAttribute(if_index_attr);
446   // Insert mac address attribute.
447   std::array<uint8_t, ETH_ALEN> if_mac_addr = kFakeInterfaceMacAddress;
448   NL80211Attr<std::array<uint8_t, ETH_ALEN>> if_mac_attr(NL80211_ATTR_MAC, if_mac_addr);
449   new_interface.AddAttribute(if_mac_attr);
450 
451   // Mock a valid response from kernel.
452   vector<NL80211Packet> response = {new_interface};
453 
454   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
455       WillOnce(DoAll(MakeupResponse(response), Return(true)));
456 
457   vector<InterfaceInfo> interfaces;
458   EXPECT_TRUE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
459   EXPECT_TRUE(interfaces.size() == 1);
460   EXPECT_EQ(kFakeInterfaceIndex, interfaces[0].index);
461   EXPECT_EQ(string(kFakeInterfaceName), interfaces[0].name);
462   EXPECT_EQ(if_mac_addr, interfaces[0].mac_address);
463 }
464 
TEST_F(NetlinkUtilsTest,SkipsPseudoDevicesWhenGetInterfaces)465 TEST_F(NetlinkUtilsTest, SkipsPseudoDevicesWhenGetInterfaces) {
466   // This might be a psuedo p2p interface without any interface index/name
467   // attributes.
468   NL80211Packet psuedo_interface(
469       netlink_manager_->GetFamilyId(),
470       NL80211_CMD_NEW_INTERFACE,
471       netlink_manager_->GetSequenceNumber(),
472       getpid());
473   psuedo_interface.AddAttribute(NL80211Attr<uint64_t>(
474       NL80211_ATTR_WDEV, 0));
475 
476   // This is a regular client interface.
477   NL80211Packet expected_interface(
478       netlink_manager_->GetFamilyId(),
479       NL80211_CMD_NEW_INTERFACE,
480       netlink_manager_->GetSequenceNumber(),
481       getpid());
482   expected_interface.AddAttribute(NL80211Attr<string>(
483       NL80211_ATTR_IFNAME, string(kFakeInterfaceName)));
484   expected_interface.AddAttribute(NL80211Attr<uint32_t>(
485       NL80211_ATTR_IFINDEX, kFakeInterfaceIndex));
486   std::array<uint8_t, ETH_ALEN> if_mac_addr = kFakeInterfaceMacAddress;
487   expected_interface.AddAttribute(
488       NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr));
489 
490   // Kernel can send us the pseduo interface packet first
491   vector<NL80211Packet> response = {psuedo_interface, expected_interface};
492 
493   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
494       WillOnce(DoAll(MakeupResponse(response), Return(true)));
495 
496   vector<InterfaceInfo> interfaces;
497   EXPECT_TRUE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
498   EXPECT_TRUE(interfaces.size() == 1);
499   EXPECT_EQ(kFakeInterfaceIndex, interfaces[0].index);
500   EXPECT_EQ(string(kFakeInterfaceName), interfaces[0].name);
501   EXPECT_EQ(if_mac_addr, interfaces[0].mac_address);
502 }
503 
TEST_F(NetlinkUtilsTest,HandleP2p0WhenGetInterfaces)504 TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaces) {
505   NL80211Packet new_interface(
506       netlink_manager_->GetFamilyId(),
507       NL80211_CMD_NEW_INTERFACE,
508       netlink_manager_->GetSequenceNumber(),
509       getpid());
510   // Insert interface name attribute.
511   NL80211Attr<string> if_name_attr(NL80211_ATTR_IFNAME, string(kFakeInterfaceName));
512   new_interface.AddAttribute(if_name_attr);
513   // Insert interface index attribute.
514   new_interface.AddAttribute(
515       NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex));
516   // Insert mac address attribute.
517   std::array<uint8_t, ETH_ALEN> if_mac_addr = kFakeInterfaceMacAddress;
518   new_interface.AddAttribute(
519       NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr));
520 
521   // Create a new interface packet for p2p0.
522   NL80211Packet new_interface_p2p0(
523       netlink_manager_->GetFamilyId(),
524       NL80211_CMD_NEW_INTERFACE,
525       netlink_manager_->GetSequenceNumber(),
526       getpid());
527 
528   // Insert interface name attribute.
529   new_interface_p2p0.AddAttribute(
530       NL80211Attr<string>(NL80211_ATTR_IFNAME, "p2p0"));
531   // Insert interface index attribute.
532   new_interface_p2p0.AddAttribute(
533       NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex1));
534   // Insert mac address attribute.
535   std::array<uint8_t, ETH_ALEN> if_mac_addr_p2p = kFakeInterfaceMacAddress1;
536   new_interface_p2p0.AddAttribute(
537       NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr_p2p));
538 
539   // Mock response from kernel, including 2 interfaces.
540   vector<NL80211Packet> response = {new_interface_p2p0, new_interface};
541 
542   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
543       WillOnce(DoAll(MakeupResponse(response), Return(true)));
544 
545   vector<InterfaceInfo> interfaces;
546   EXPECT_TRUE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
547   EXPECT_TRUE(interfaces.size() == 2);
548 
549   EXPECT_EQ(kFakeInterfaceIndex1, interfaces[0].index);
550   EXPECT_EQ(string("p2p0"), interfaces[0].name);
551   EXPECT_EQ(if_mac_addr_p2p, interfaces[0].mac_address);
552 
553   EXPECT_EQ(kFakeInterfaceIndex, interfaces[1].index);
554   EXPECT_EQ(string(kFakeInterfaceName), interfaces[1].name);
555   EXPECT_EQ(if_mac_addr, interfaces[1].mac_address);
556 }
557 
TEST_F(NetlinkUtilsTest,CanHandleGetInterfacesError)558 TEST_F(NetlinkUtilsTest, CanHandleGetInterfacesError) {
559   // Mock an error response from kernel.
560   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
561 
562   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
563       WillOnce(DoAll(MakeupResponse(response), Return(true)));
564 
565   vector<InterfaceInfo> interfaces;
566   EXPECT_FALSE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
567 }
568 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfo)569 TEST_F(NetlinkUtilsTest, CanGetWiphyInfo) {
570   SetSplitWiphyDumpSupported(false);
571   NL80211Packet new_wiphy(
572       netlink_manager_->GetFamilyId(),
573       NL80211_CMD_NEW_WIPHY,
574       netlink_manager_->GetSequenceNumber(),
575       getpid());
576   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
577                                                kFakeWiphyIndex));
578   AppendBandInfoAttributes(&new_wiphy);
579   AppendScanCapabilitiesAttributes(&new_wiphy, true);
580   AppendWiphyFeaturesAttributes(&new_wiphy);
581   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
582 
583   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
584       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
585 
586   BandInfo band_info;
587   ScanCapabilities scan_capabilities;
588   WiphyFeatures wiphy_features;
589   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
590                                            &band_info,
591                                            &scan_capabilities,
592                                            &wiphy_features));
593   VerifyBandInfo(band_info);
594   VerifyScanCapabilities(scan_capabilities, true);
595   VerifyWiphyFeatures(wiphy_features);
596   EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
597   EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
598   EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
599 }
600 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithNoDbsParam)601 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithNoDbsParam) {
602   SetSplitWiphyDumpSupported(false);
603   NL80211Packet new_wiphy(
604       netlink_manager_->GetFamilyId(),
605       NL80211_CMD_NEW_WIPHY,
606       netlink_manager_->GetSequenceNumber(),
607       getpid());
608   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
609                                                kFakeWiphyIndex));
610   AppendBandInfoAttributes(&new_wiphy);
611   AppendScanCapabilitiesAttributes(&new_wiphy, false);
612   AppendWiphyFeaturesAttributes(&new_wiphy);
613   AppendWiphyExtFeaturesAttributes(&new_wiphy, false, false, false, false);
614   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
615 
616   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
617       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
618 
619   BandInfo band_info;
620   ScanCapabilities scan_capabilities;
621   WiphyFeatures wiphy_features;
622   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
623                                            &band_info,
624                                            &scan_capabilities,
625                                            &wiphy_features));
626   VerifyBandInfo(band_info);
627   VerifyScanCapabilities(scan_capabilities, false);
628   VerifyWiphyFeatures(wiphy_features);
629   EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
630   EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
631   EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
632 }
633 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithLowSpanScan)634 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithLowSpanScan) {
635   SetSplitWiphyDumpSupported(false);
636   NL80211Packet new_wiphy(
637       netlink_manager_->GetFamilyId(),
638       NL80211_CMD_NEW_WIPHY,
639       netlink_manager_->GetSequenceNumber(),
640       getpid());
641   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
642                                                kFakeWiphyIndex));
643   AppendBandInfoAttributes(&new_wiphy);
644   AppendScanCapabilitiesAttributes(&new_wiphy, false);
645   AppendWiphyFeaturesAttributes(&new_wiphy);
646   AppendWiphyExtFeaturesAttributes(&new_wiphy, true, false, false, false);
647   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
648 
649   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
650       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
651 
652   BandInfo band_info;
653   ScanCapabilities scan_capabilities;
654   WiphyFeatures wiphy_features;
655   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
656                                            &band_info,
657                                            &scan_capabilities,
658                                            &wiphy_features));
659   VerifyBandInfo(band_info);
660   VerifyScanCapabilities(scan_capabilities, false);
661   VerifyWiphyFeatures(wiphy_features);
662   EXPECT_TRUE(wiphy_features.supports_low_span_oneshot_scan);
663   EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
664   EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
665 }
666 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithLowPowerScan)667 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithLowPowerScan) {
668   SetSplitWiphyDumpSupported(false);
669   NL80211Packet new_wiphy(
670       netlink_manager_->GetFamilyId(),
671       NL80211_CMD_NEW_WIPHY,
672       netlink_manager_->GetSequenceNumber(),
673       getpid());
674   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
675                                                kFakeWiphyIndex));
676   AppendBandInfoAttributes(&new_wiphy);
677   AppendScanCapabilitiesAttributes(&new_wiphy, false);
678   AppendWiphyFeaturesAttributes(&new_wiphy);
679   AppendWiphyExtFeaturesAttributes(&new_wiphy, false, true, false, false);
680   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
681 
682   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
683       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
684 
685   BandInfo band_info;
686   ScanCapabilities scan_capabilities;
687   WiphyFeatures wiphy_features;
688   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
689                                            &band_info,
690                                            &scan_capabilities,
691                                            &wiphy_features));
692   VerifyBandInfo(band_info);
693   VerifyScanCapabilities(scan_capabilities, false);
694   VerifyWiphyFeatures(wiphy_features);
695   EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
696   EXPECT_TRUE(wiphy_features.supports_low_power_oneshot_scan);
697   EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
698 }
699 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithHighAccuracyScan)700 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithHighAccuracyScan) {
701   SetSplitWiphyDumpSupported(false);
702   NL80211Packet new_wiphy(
703       netlink_manager_->GetFamilyId(),
704       NL80211_CMD_NEW_WIPHY,
705       netlink_manager_->GetSequenceNumber(),
706       getpid());
707   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
708                                                kFakeWiphyIndex));
709   AppendBandInfoAttributes(&new_wiphy);
710   AppendScanCapabilitiesAttributes(&new_wiphy, false);
711   AppendWiphyFeaturesAttributes(&new_wiphy);
712   AppendWiphyExtFeaturesAttributes(&new_wiphy, false, false, true, false);
713   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
714 
715   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
716       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
717 
718   BandInfo band_info;
719   ScanCapabilities scan_capabilities;
720   WiphyFeatures wiphy_features;
721   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
722                                            &band_info,
723                                            &scan_capabilities,
724                                            &wiphy_features));
725   VerifyBandInfo(band_info);
726   VerifyScanCapabilities(scan_capabilities, false);
727   VerifyWiphyFeatures(wiphy_features);
728   EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
729   EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
730   EXPECT_TRUE(wiphy_features.supports_high_accuracy_oneshot_scan);
731 }
732 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithAllDbsParams)733 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithAllDbsParams) {
734   SetSplitWiphyDumpSupported(false);
735   NL80211Packet new_wiphy(
736       netlink_manager_->GetFamilyId(),
737       NL80211_CMD_NEW_WIPHY,
738       netlink_manager_->GetSequenceNumber(),
739       getpid());
740   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
741                                                kFakeWiphyIndex));
742   AppendBandInfoAttributes(&new_wiphy);
743   AppendScanCapabilitiesAttributes(&new_wiphy, false);
744   AppendWiphyFeaturesAttributes(&new_wiphy);
745   AppendWiphyExtFeaturesAttributes(&new_wiphy, false, false, false, true);
746   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
747 
748   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
749       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
750 
751   BandInfo band_info;
752   ScanCapabilities scan_capabilities;
753   WiphyFeatures wiphy_features;
754   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
755                                            &band_info,
756                                            &scan_capabilities,
757                                            &wiphy_features));
758   VerifyBandInfo(band_info);
759   VerifyScanCapabilities(scan_capabilities, false);
760   VerifyWiphyFeatures(wiphy_features);
761   EXPECT_TRUE(wiphy_features.supports_low_span_oneshot_scan);
762   EXPECT_TRUE(wiphy_features.supports_low_power_oneshot_scan);
763   EXPECT_TRUE(wiphy_features.supports_high_accuracy_oneshot_scan);
764 }
765 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoScanPlanNotSupported)766 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoScanPlanNotSupported) {
767   SetSplitWiphyDumpSupported(false);
768   NL80211Packet new_wiphy(
769       netlink_manager_->GetFamilyId(),
770       NL80211_CMD_NEW_WIPHY,
771       netlink_manager_->GetSequenceNumber(),
772       getpid());
773   new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
774                                                kFakeWiphyIndex));
775   AppendBandInfoAttributes(&new_wiphy);
776   AppendScanCapabilitiesAttributes(&new_wiphy, false);
777   AppendWiphyFeaturesAttributes(&new_wiphy);
778   vector<NL80211Packet> get_wiphy_response = {new_wiphy};
779 
780   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
781       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
782 
783   BandInfo band_info;
784   ScanCapabilities scan_capabilities;
785   WiphyFeatures wiphy_features;
786   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
787                                            &band_info,
788                                            &scan_capabilities,
789                                            &wiphy_features));
790   VerifyBandInfo(band_info);
791   VerifyScanCapabilities(scan_capabilities, false);
792   VerifyWiphyFeatures(wiphy_features);
793 }
794 
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoSplitDump)795 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoSplitDump) {
796   SetSplitWiphyDumpSupported(true);
797 
798   NL80211Packet new_wiphy_packet1(
799       netlink_manager_->GetFamilyId(),
800       NL80211_CMD_NEW_WIPHY,
801       netlink_manager_->GetSequenceNumber(),
802       getpid());
803   new_wiphy_packet1.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
804                                                        kFakeWiphyIndex));
805   new_wiphy_packet1.AddAttribute(GenerateBandsAttributeFor5gAndDfs());
806 
807   NL80211Packet new_wiphy_packet2(
808       netlink_manager_->GetFamilyId(),
809       NL80211_CMD_NEW_WIPHY,
810       netlink_manager_->GetSequenceNumber(),
811       getpid());
812   new_wiphy_packet2.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
813                                                        kFakeWiphyIndex));
814   new_wiphy_packet2.AddAttribute(GenerateBandsAttributeFor2g());
815   AppendScanCapabilitiesAttributes(&new_wiphy_packet2, false);
816   AppendWiphyFeaturesAttributes(&new_wiphy_packet2);
817 
818   // Insert a packet for wiphy with index kFakeWiphyIndex1.
819   // This is unrelated and should be ingnored by |GetWiphyInfo|.
820   NL80211Packet new_wiphy_packet3(
821       netlink_manager_->GetFamilyId(),
822       NL80211_CMD_NEW_WIPHY,
823       netlink_manager_->GetSequenceNumber(),
824       getpid());
825   new_wiphy_packet3.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
826                                                        kFakeWiphyIndex1));
827   AppendBandInfoAttributes(&new_wiphy_packet3);
828 
829   vector<NL80211Packet> get_wiphy_response =
830       {new_wiphy_packet1, new_wiphy_packet2, new_wiphy_packet3};
831 
832   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
833       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
834 
835   BandInfo band_info;
836   ScanCapabilities scan_capabilities;
837   WiphyFeatures wiphy_features;
838   EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
839                                            &band_info,
840                                            &scan_capabilities,
841                                            &wiphy_features));
842   VerifyBandInfo(band_info);
843   VerifyScanCapabilities(scan_capabilities, false);
844   VerifyWiphyFeatures(wiphy_features);
845 }
846 
847 
TEST_F(NetlinkUtilsTest,CanHandleGetWiphyInfoError)848 TEST_F(NetlinkUtilsTest, CanHandleGetWiphyInfoError) {
849   SetSplitWiphyDumpSupported(false);
850 
851   // Mock an error response from kernel.
852   vector<NL80211Packet> get_wiphy_response = {CreateControlMessageError(kFakeErrorCode)};
853 
854   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
855       WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
856 
857   BandInfo band_info;
858   ScanCapabilities scan_capabilities;
859   WiphyFeatures wiphy_features;
860   EXPECT_FALSE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
861                                            &band_info,
862                                            &scan_capabilities,
863                                            &wiphy_features));
864 }
865 
TEST_F(NetlinkUtilsTest,CanGetProtocolFeatures)866 TEST_F(NetlinkUtilsTest, CanGetProtocolFeatures) {
867   // There is no specification for the response packet id for
868   // NL80211_CMD_GET_PROTOCOL_FEATURES.
869   // Still use NL80211_CMD_GET_PROTOCOL_FEATURES here.
870   NL80211Packet get_features_response(
871       netlink_manager_->GetFamilyId(),
872       NL80211_CMD_GET_PROTOCOL_FEATURES,
873       netlink_manager_->GetSequenceNumber(),
874       getpid());
875   get_features_response.AddAttribute(
876       NL80211Attr<uint32_t>(NL80211_ATTR_PROTOCOL_FEATURES,
877                             kFakeProtocolFeatures));
878   vector<NL80211Packet> response = {get_features_response};
879 
880   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
881       WillOnce(DoAll(MakeupResponse(response), Return(true)));
882 
883   uint32_t features;
884   EXPECT_TRUE(netlink_utils_->GetProtocolFeatures(&features));
885   EXPECT_EQ(kFakeProtocolFeatures, features);
886 }
887 
TEST_F(NetlinkUtilsTest,CanHandleGetProtocolFeaturesError)888 TEST_F(NetlinkUtilsTest, CanHandleGetProtocolFeaturesError) {
889   // Mock an error response from kernel.
890   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
891 
892   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
893       WillOnce(DoAll(MakeupResponse(response), Return(true)));
894 
895   uint32_t features_ignored;
896   EXPECT_FALSE(netlink_utils_->GetProtocolFeatures(&features_ignored));
897 }
898 
TEST_F(NetlinkUtilsTest,CanGetCountryCode)899 TEST_F(NetlinkUtilsTest, CanGetCountryCode) {
900   // There is no specification for the response packet id for
901   // NL80211_CMD_GET_REG.
902   // Still use NL80211_CMD_GET_REG here.
903   NL80211Packet get_country_code_response(
904       netlink_manager_->GetFamilyId(),
905       NL80211_CMD_GET_REG,
906       netlink_manager_->GetSequenceNumber(),
907       getpid());
908   get_country_code_response.AddAttribute(
909       NL80211Attr<string>(NL80211_ATTR_REG_ALPHA2,
910                           kFakeCountryCode));
911   vector<NL80211Packet> response = {get_country_code_response};
912 
913   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
914       WillOnce(DoAll(MakeupResponse(response), Return(true)));
915 
916   string country_code;
917   EXPECT_TRUE(netlink_utils_->GetCountryCode(&country_code));
918   EXPECT_EQ(kFakeCountryCode, country_code);
919 }
920 
TEST_F(NetlinkUtilsTest,CanHandleGetCountryCodeError)921 TEST_F(NetlinkUtilsTest, CanHandleGetCountryCodeError) {
922   // Mock an error response from kernel.
923   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
924 
925   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
926       WillOnce(DoAll(MakeupResponse(response), Return(true)));
927 
928   string country_code_ignored;
929   EXPECT_FALSE(netlink_utils_->GetCountryCode(&country_code_ignored));
930 }
931 
TEST_F(NetlinkUtilsTest,CanSendMgmtFrame)932 TEST_F(NetlinkUtilsTest, CanSendMgmtFrame) {
933   // There is no specification for the response packet id for
934   // NL80211_CMD_FRAME.
935   // Still use NL80211_CMD_FRAME here.
936   NL80211Packet send_mgmt_frame_response(
937       netlink_manager_->GetFamilyId(),
938       NL80211_CMD_FRAME,
939       netlink_manager_->GetSequenceNumber(),
940       getpid());
941   send_mgmt_frame_response.AddAttribute(
942       NL80211Attr<uint64_t>(NL80211_ATTR_COOKIE, kFakeCookie));
943   vector<NL80211Packet> response = {send_mgmt_frame_response};
944 
945   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _))
946     .WillOnce(DoAll(MakeupResponse(response), Return(true)));
947 
948   uint64_t cookie;
949   EXPECT_TRUE(netlink_utils_->SendMgmtFrame(kFakeInterfaceIndex,
950       vector<uint8_t>(std::begin(kFakeFrame), std::end(kFakeFrame)),
951       kFakeMcs, &cookie));
952   EXPECT_EQ(kFakeCookie, cookie);
953 }
954 
TEST_F(NetlinkUtilsTest,CanHandleSendMgmtFrameError)955 TEST_F(NetlinkUtilsTest, CanHandleSendMgmtFrameError) {
956   // Mock an error response from kernel.
957   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
958   EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
959       WillOnce(DoAll(MakeupResponse(response), Return(true)));
960 
961   uint64_t cookie_ignored;
962   EXPECT_FALSE(netlink_utils_->SendMgmtFrame(kFakeInterfaceIndex,
963       vector<uint8_t>(std::begin(kFakeFrame), std::end(kFakeFrame)),
964       kFakeMcs, &cookie_ignored));
965 }
966 
967 }  // namespace wificond
968 }  // namespace android
969