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].if_index);
461 EXPECT_EQ(kFakeWiphyIndex, interfaces[0].wiphy_index);
462 EXPECT_EQ(string(kFakeInterfaceName), interfaces[0].name);
463 EXPECT_EQ(if_mac_addr, interfaces[0].mac_address);
464 }
465
TEST_F(NetlinkUtilsTest,SkipsPseudoDevicesWhenGetInterfaces)466 TEST_F(NetlinkUtilsTest, SkipsPseudoDevicesWhenGetInterfaces) {
467 // This might be a psuedo p2p interface without any interface index/name
468 // attributes.
469 NL80211Packet psuedo_interface(
470 netlink_manager_->GetFamilyId(),
471 NL80211_CMD_NEW_INTERFACE,
472 netlink_manager_->GetSequenceNumber(),
473 getpid());
474 psuedo_interface.AddAttribute(NL80211Attr<uint64_t>(
475 NL80211_ATTR_WDEV, 0));
476
477 // This is a regular client interface.
478 NL80211Packet expected_interface(
479 netlink_manager_->GetFamilyId(),
480 NL80211_CMD_NEW_INTERFACE,
481 netlink_manager_->GetSequenceNumber(),
482 getpid());
483 expected_interface.AddAttribute(NL80211Attr<string>(
484 NL80211_ATTR_IFNAME, string(kFakeInterfaceName)));
485 expected_interface.AddAttribute(NL80211Attr<uint32_t>(
486 NL80211_ATTR_IFINDEX, kFakeInterfaceIndex));
487 std::array<uint8_t, ETH_ALEN> if_mac_addr = kFakeInterfaceMacAddress;
488 expected_interface.AddAttribute(
489 NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr));
490
491 // Kernel can send us the pseduo interface packet first
492 vector<NL80211Packet> response = {psuedo_interface, expected_interface};
493
494 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
495 WillOnce(DoAll(MakeupResponse(response), Return(true)));
496
497 vector<InterfaceInfo> interfaces;
498 EXPECT_TRUE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
499 EXPECT_TRUE(interfaces.size() == 1);
500 EXPECT_EQ(kFakeInterfaceIndex, interfaces[0].if_index);
501 EXPECT_EQ(kFakeWiphyIndex, interfaces[0].wiphy_index);
502 EXPECT_EQ(string(kFakeInterfaceName), interfaces[0].name);
503 EXPECT_EQ(if_mac_addr, interfaces[0].mac_address);
504 }
505
TEST_F(NetlinkUtilsTest,HandleP2p0WhenGetInterfaces)506 TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaces) {
507 NL80211Packet new_interface(
508 netlink_manager_->GetFamilyId(),
509 NL80211_CMD_NEW_INTERFACE,
510 netlink_manager_->GetSequenceNumber(),
511 getpid());
512 // Insert interface name attribute.
513 NL80211Attr<string> if_name_attr(NL80211_ATTR_IFNAME, string(kFakeInterfaceName));
514 new_interface.AddAttribute(if_name_attr);
515 // Insert interface index attribute.
516 new_interface.AddAttribute(
517 NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex));
518 // Insert mac address attribute.
519 std::array<uint8_t, ETH_ALEN> if_mac_addr = kFakeInterfaceMacAddress;
520 new_interface.AddAttribute(
521 NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr));
522
523 // Create a new interface packet for p2p0.
524 NL80211Packet new_interface_p2p0(
525 netlink_manager_->GetFamilyId(),
526 NL80211_CMD_NEW_INTERFACE,
527 netlink_manager_->GetSequenceNumber(),
528 getpid());
529
530 // Insert interface name attribute.
531 new_interface_p2p0.AddAttribute(
532 NL80211Attr<string>(NL80211_ATTR_IFNAME, "p2p0"));
533 // Insert interface index attribute.
534 new_interface_p2p0.AddAttribute(
535 NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex1));
536 // Insert mac address attribute.
537 std::array<uint8_t, ETH_ALEN> if_mac_addr_p2p = kFakeInterfaceMacAddress1;
538 new_interface_p2p0.AddAttribute(
539 NL80211Attr<std::array<uint8_t, ETH_ALEN>>(NL80211_ATTR_MAC, if_mac_addr_p2p));
540
541 // Mock response from kernel, including 2 interfaces.
542 vector<NL80211Packet> response = {new_interface_p2p0, new_interface};
543
544 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
545 WillOnce(DoAll(MakeupResponse(response), Return(true)));
546
547 vector<InterfaceInfo> interfaces;
548 EXPECT_TRUE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
549 EXPECT_TRUE(interfaces.size() == 2);
550
551 EXPECT_EQ(kFakeInterfaceIndex1, interfaces[0].if_index);
552 EXPECT_EQ(kFakeWiphyIndex, interfaces[0].wiphy_index);
553 EXPECT_EQ(string("p2p0"), interfaces[0].name);
554 EXPECT_EQ(if_mac_addr_p2p, interfaces[0].mac_address);
555
556 EXPECT_EQ(kFakeInterfaceIndex, interfaces[1].if_index);
557 EXPECT_EQ(kFakeWiphyIndex, interfaces[1].wiphy_index);
558 EXPECT_EQ(string(kFakeInterfaceName), interfaces[1].name);
559 EXPECT_EQ(if_mac_addr, interfaces[1].mac_address);
560 }
561
TEST_F(NetlinkUtilsTest,CanHandleGetInterfacesError)562 TEST_F(NetlinkUtilsTest, CanHandleGetInterfacesError) {
563 // Mock an error response from kernel.
564 vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
565
566 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
567 WillOnce(DoAll(MakeupResponse(response), Return(true)));
568
569 vector<InterfaceInfo> interfaces;
570 EXPECT_FALSE(netlink_utils_->GetInterfaces(kFakeWiphyIndex, &interfaces));
571 }
572
TEST_F(NetlinkUtilsTest,CanGetWiphyInfo)573 TEST_F(NetlinkUtilsTest, CanGetWiphyInfo) {
574 SetSplitWiphyDumpSupported(false);
575 NL80211Packet new_wiphy(
576 netlink_manager_->GetFamilyId(),
577 NL80211_CMD_NEW_WIPHY,
578 netlink_manager_->GetSequenceNumber(),
579 getpid());
580 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
581 kFakeWiphyIndex));
582 AppendBandInfoAttributes(&new_wiphy);
583 AppendScanCapabilitiesAttributes(&new_wiphy, true);
584 AppendWiphyFeaturesAttributes(&new_wiphy);
585 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
586
587 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
588 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
589
590 BandInfo band_info;
591 ScanCapabilities scan_capabilities;
592 WiphyFeatures wiphy_features;
593 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
594 &band_info,
595 &scan_capabilities,
596 &wiphy_features));
597 VerifyBandInfo(band_info);
598 VerifyScanCapabilities(scan_capabilities, true);
599 VerifyWiphyFeatures(wiphy_features);
600 EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
601 EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
602 EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
603 }
604
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithNoDbsParam)605 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithNoDbsParam) {
606 SetSplitWiphyDumpSupported(false);
607 NL80211Packet new_wiphy(
608 netlink_manager_->GetFamilyId(),
609 NL80211_CMD_NEW_WIPHY,
610 netlink_manager_->GetSequenceNumber(),
611 getpid());
612 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
613 kFakeWiphyIndex));
614 AppendBandInfoAttributes(&new_wiphy);
615 AppendScanCapabilitiesAttributes(&new_wiphy, false);
616 AppendWiphyFeaturesAttributes(&new_wiphy);
617 AppendWiphyExtFeaturesAttributes(&new_wiphy, false, false, false, false);
618 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
619
620 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
621 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
622
623 BandInfo band_info;
624 ScanCapabilities scan_capabilities;
625 WiphyFeatures wiphy_features;
626 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
627 &band_info,
628 &scan_capabilities,
629 &wiphy_features));
630 VerifyBandInfo(band_info);
631 VerifyScanCapabilities(scan_capabilities, false);
632 VerifyWiphyFeatures(wiphy_features);
633 EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
634 EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
635 EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
636 }
637
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithLowSpanScan)638 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithLowSpanScan) {
639 SetSplitWiphyDumpSupported(false);
640 NL80211Packet new_wiphy(
641 netlink_manager_->GetFamilyId(),
642 NL80211_CMD_NEW_WIPHY,
643 netlink_manager_->GetSequenceNumber(),
644 getpid());
645 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
646 kFakeWiphyIndex));
647 AppendBandInfoAttributes(&new_wiphy);
648 AppendScanCapabilitiesAttributes(&new_wiphy, false);
649 AppendWiphyFeaturesAttributes(&new_wiphy);
650 AppendWiphyExtFeaturesAttributes(&new_wiphy, true, false, false, false);
651 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
652
653 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
654 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
655
656 BandInfo band_info;
657 ScanCapabilities scan_capabilities;
658 WiphyFeatures wiphy_features;
659 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
660 &band_info,
661 &scan_capabilities,
662 &wiphy_features));
663 VerifyBandInfo(band_info);
664 VerifyScanCapabilities(scan_capabilities, false);
665 VerifyWiphyFeatures(wiphy_features);
666 EXPECT_TRUE(wiphy_features.supports_low_span_oneshot_scan);
667 EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
668 EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
669 }
670
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithLowPowerScan)671 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithLowPowerScan) {
672 SetSplitWiphyDumpSupported(false);
673 NL80211Packet new_wiphy(
674 netlink_manager_->GetFamilyId(),
675 NL80211_CMD_NEW_WIPHY,
676 netlink_manager_->GetSequenceNumber(),
677 getpid());
678 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
679 kFakeWiphyIndex));
680 AppendBandInfoAttributes(&new_wiphy);
681 AppendScanCapabilitiesAttributes(&new_wiphy, false);
682 AppendWiphyFeaturesAttributes(&new_wiphy);
683 AppendWiphyExtFeaturesAttributes(&new_wiphy, false, true, false, false);
684 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
685
686 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
687 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
688
689 BandInfo band_info;
690 ScanCapabilities scan_capabilities;
691 WiphyFeatures wiphy_features;
692 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
693 &band_info,
694 &scan_capabilities,
695 &wiphy_features));
696 VerifyBandInfo(band_info);
697 VerifyScanCapabilities(scan_capabilities, false);
698 VerifyWiphyFeatures(wiphy_features);
699 EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
700 EXPECT_TRUE(wiphy_features.supports_low_power_oneshot_scan);
701 EXPECT_FALSE(wiphy_features.supports_high_accuracy_oneshot_scan);
702 }
703
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithHighAccuracyScan)704 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithHighAccuracyScan) {
705 SetSplitWiphyDumpSupported(false);
706 NL80211Packet new_wiphy(
707 netlink_manager_->GetFamilyId(),
708 NL80211_CMD_NEW_WIPHY,
709 netlink_manager_->GetSequenceNumber(),
710 getpid());
711 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
712 kFakeWiphyIndex));
713 AppendBandInfoAttributes(&new_wiphy);
714 AppendScanCapabilitiesAttributes(&new_wiphy, false);
715 AppendWiphyFeaturesAttributes(&new_wiphy);
716 AppendWiphyExtFeaturesAttributes(&new_wiphy, false, false, true, false);
717 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
718
719 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
720 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
721
722 BandInfo band_info;
723 ScanCapabilities scan_capabilities;
724 WiphyFeatures wiphy_features;
725 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
726 &band_info,
727 &scan_capabilities,
728 &wiphy_features));
729 VerifyBandInfo(band_info);
730 VerifyScanCapabilities(scan_capabilities, false);
731 VerifyWiphyFeatures(wiphy_features);
732 EXPECT_FALSE(wiphy_features.supports_low_span_oneshot_scan);
733 EXPECT_FALSE(wiphy_features.supports_low_power_oneshot_scan);
734 EXPECT_TRUE(wiphy_features.supports_high_accuracy_oneshot_scan);
735 }
736
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoWithAllDbsParams)737 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoWithAllDbsParams) {
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, false, true);
750 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
751
752 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
753 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
754
755 BandInfo band_info;
756 ScanCapabilities scan_capabilities;
757 WiphyFeatures wiphy_features;
758 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
759 &band_info,
760 &scan_capabilities,
761 &wiphy_features));
762 VerifyBandInfo(band_info);
763 VerifyScanCapabilities(scan_capabilities, false);
764 VerifyWiphyFeatures(wiphy_features);
765 EXPECT_TRUE(wiphy_features.supports_low_span_oneshot_scan);
766 EXPECT_TRUE(wiphy_features.supports_low_power_oneshot_scan);
767 EXPECT_TRUE(wiphy_features.supports_high_accuracy_oneshot_scan);
768 }
769
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoScanPlanNotSupported)770 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoScanPlanNotSupported) {
771 SetSplitWiphyDumpSupported(false);
772 NL80211Packet new_wiphy(
773 netlink_manager_->GetFamilyId(),
774 NL80211_CMD_NEW_WIPHY,
775 netlink_manager_->GetSequenceNumber(),
776 getpid());
777 new_wiphy.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
778 kFakeWiphyIndex));
779 AppendBandInfoAttributes(&new_wiphy);
780 AppendScanCapabilitiesAttributes(&new_wiphy, false);
781 AppendWiphyFeaturesAttributes(&new_wiphy);
782 vector<NL80211Packet> get_wiphy_response = {new_wiphy};
783
784 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
785 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
786
787 BandInfo band_info;
788 ScanCapabilities scan_capabilities;
789 WiphyFeatures wiphy_features;
790 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
791 &band_info,
792 &scan_capabilities,
793 &wiphy_features));
794 VerifyBandInfo(band_info);
795 VerifyScanCapabilities(scan_capabilities, false);
796 VerifyWiphyFeatures(wiphy_features);
797 }
798
TEST_F(NetlinkUtilsTest,CanGetWiphyInfoSplitDump)799 TEST_F(NetlinkUtilsTest, CanGetWiphyInfoSplitDump) {
800 SetSplitWiphyDumpSupported(true);
801
802 NL80211Packet new_wiphy_packet1(
803 netlink_manager_->GetFamilyId(),
804 NL80211_CMD_NEW_WIPHY,
805 netlink_manager_->GetSequenceNumber(),
806 getpid());
807 new_wiphy_packet1.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
808 kFakeWiphyIndex));
809 new_wiphy_packet1.AddAttribute(GenerateBandsAttributeFor5gAndDfs());
810
811 NL80211Packet new_wiphy_packet2(
812 netlink_manager_->GetFamilyId(),
813 NL80211_CMD_NEW_WIPHY,
814 netlink_manager_->GetSequenceNumber(),
815 getpid());
816 new_wiphy_packet2.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
817 kFakeWiphyIndex));
818 new_wiphy_packet2.AddAttribute(GenerateBandsAttributeFor2g());
819 AppendScanCapabilitiesAttributes(&new_wiphy_packet2, false);
820 AppendWiphyFeaturesAttributes(&new_wiphy_packet2);
821
822 // Insert a packet for wiphy with index kFakeWiphyIndex1.
823 // This is unrelated and should be ingnored by |GetWiphyInfo|.
824 NL80211Packet new_wiphy_packet3(
825 netlink_manager_->GetFamilyId(),
826 NL80211_CMD_NEW_WIPHY,
827 netlink_manager_->GetSequenceNumber(),
828 getpid());
829 new_wiphy_packet3.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY,
830 kFakeWiphyIndex1));
831 AppendBandInfoAttributes(&new_wiphy_packet3);
832
833 vector<NL80211Packet> get_wiphy_response =
834 {new_wiphy_packet1, new_wiphy_packet2, new_wiphy_packet3};
835
836 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
837 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
838
839 BandInfo band_info;
840 ScanCapabilities scan_capabilities;
841 WiphyFeatures wiphy_features;
842 EXPECT_TRUE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
843 &band_info,
844 &scan_capabilities,
845 &wiphy_features));
846 VerifyBandInfo(band_info);
847 VerifyScanCapabilities(scan_capabilities, false);
848 VerifyWiphyFeatures(wiphy_features);
849 }
850
851
TEST_F(NetlinkUtilsTest,CanHandleGetWiphyInfoError)852 TEST_F(NetlinkUtilsTest, CanHandleGetWiphyInfoError) {
853 SetSplitWiphyDumpSupported(false);
854
855 // Mock an error response from kernel.
856 vector<NL80211Packet> get_wiphy_response = {CreateControlMessageError(kFakeErrorCode)};
857
858 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
859 WillOnce(DoAll(MakeupResponse(get_wiphy_response), Return(true)));
860
861 BandInfo band_info;
862 ScanCapabilities scan_capabilities;
863 WiphyFeatures wiphy_features;
864 EXPECT_FALSE(netlink_utils_->GetWiphyInfo(kFakeWiphyIndex,
865 &band_info,
866 &scan_capabilities,
867 &wiphy_features));
868 }
869
TEST_F(NetlinkUtilsTest,CanGetProtocolFeatures)870 TEST_F(NetlinkUtilsTest, CanGetProtocolFeatures) {
871 // There is no specification for the response packet id for
872 // NL80211_CMD_GET_PROTOCOL_FEATURES.
873 // Still use NL80211_CMD_GET_PROTOCOL_FEATURES here.
874 NL80211Packet get_features_response(
875 netlink_manager_->GetFamilyId(),
876 NL80211_CMD_GET_PROTOCOL_FEATURES,
877 netlink_manager_->GetSequenceNumber(),
878 getpid());
879 get_features_response.AddAttribute(
880 NL80211Attr<uint32_t>(NL80211_ATTR_PROTOCOL_FEATURES,
881 kFakeProtocolFeatures));
882 vector<NL80211Packet> response = {get_features_response};
883
884 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
885 WillOnce(DoAll(MakeupResponse(response), Return(true)));
886
887 uint32_t features;
888 EXPECT_TRUE(netlink_utils_->GetProtocolFeatures(&features));
889 EXPECT_EQ(kFakeProtocolFeatures, features);
890 }
891
TEST_F(NetlinkUtilsTest,CanHandleGetProtocolFeaturesError)892 TEST_F(NetlinkUtilsTest, CanHandleGetProtocolFeaturesError) {
893 // Mock an error response from kernel.
894 vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
895
896 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
897 WillOnce(DoAll(MakeupResponse(response), Return(true)));
898
899 uint32_t features_ignored;
900 EXPECT_FALSE(netlink_utils_->GetProtocolFeatures(&features_ignored));
901 }
902
TEST_F(NetlinkUtilsTest,CanGetCountryCode)903 TEST_F(NetlinkUtilsTest, CanGetCountryCode) {
904 // There is no specification for the response packet id for
905 // NL80211_CMD_GET_REG.
906 // Still use NL80211_CMD_GET_REG here.
907 NL80211Packet get_country_code_response(
908 netlink_manager_->GetFamilyId(),
909 NL80211_CMD_GET_REG,
910 netlink_manager_->GetSequenceNumber(),
911 getpid());
912 get_country_code_response.AddAttribute(
913 NL80211Attr<string>(NL80211_ATTR_REG_ALPHA2,
914 kFakeCountryCode));
915 vector<NL80211Packet> response = {get_country_code_response};
916
917 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
918 WillOnce(DoAll(MakeupResponse(response), Return(true)));
919
920 string country_code;
921 EXPECT_TRUE(netlink_utils_->GetCountryCode(&country_code));
922 EXPECT_EQ(kFakeCountryCode, country_code);
923 }
924
TEST_F(NetlinkUtilsTest,CanHandleGetCountryCodeError)925 TEST_F(NetlinkUtilsTest, CanHandleGetCountryCodeError) {
926 // Mock an error response from kernel.
927 vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
928
929 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
930 WillOnce(DoAll(MakeupResponse(response), Return(true)));
931
932 string country_code_ignored;
933 EXPECT_FALSE(netlink_utils_->GetCountryCode(&country_code_ignored));
934 }
935
TEST_F(NetlinkUtilsTest,CanSendMgmtFrame)936 TEST_F(NetlinkUtilsTest, CanSendMgmtFrame) {
937 // There is no specification for the response packet id for
938 // NL80211_CMD_FRAME.
939 // Still use NL80211_CMD_FRAME here.
940 NL80211Packet send_mgmt_frame_response(
941 netlink_manager_->GetFamilyId(),
942 NL80211_CMD_FRAME,
943 netlink_manager_->GetSequenceNumber(),
944 getpid());
945 send_mgmt_frame_response.AddAttribute(
946 NL80211Attr<uint64_t>(NL80211_ATTR_COOKIE, kFakeCookie));
947 vector<NL80211Packet> response = {send_mgmt_frame_response};
948
949 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _))
950 .WillOnce(DoAll(MakeupResponse(response), Return(true)));
951
952 uint64_t cookie;
953 EXPECT_TRUE(netlink_utils_->SendMgmtFrame(kFakeInterfaceIndex,
954 vector<uint8_t>(std::begin(kFakeFrame), std::end(kFakeFrame)),
955 kFakeMcs, &cookie));
956 EXPECT_EQ(kFakeCookie, cookie);
957 }
958
TEST_F(NetlinkUtilsTest,CanHandleSendMgmtFrameError)959 TEST_F(NetlinkUtilsTest, CanHandleSendMgmtFrameError) {
960 // Mock an error response from kernel.
961 vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
962 EXPECT_CALL(*netlink_manager_, SendMessageAndGetResponses(_, _)).
963 WillOnce(DoAll(MakeupResponse(response), Return(true)));
964
965 uint64_t cookie_ignored;
966 EXPECT_FALSE(netlink_utils_->SendMgmtFrame(kFakeInterfaceIndex,
967 vector<uint8_t>(std::begin(kFakeFrame), std::end(kFakeFrame)),
968 kFakeMcs, &cookie_ignored));
969 }
970
971 } // namespace wificond
972 } // namespace android
973