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