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