Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "apmanager/device.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 10 | #include <base/strings/stringprintf.h> |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 13 | #include <shill/net/ieee80211.h> |
| 14 | #include <shill/net/nl80211_attribute.h> |
| 15 | #include <shill/net/nl80211_message.h> |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 16 | |
Peter Qiu | 7e0ffcf | 2014-12-02 12:53:27 -0800 | [diff] [blame] | 17 | #include "apmanager/mock_manager.h" |
| 18 | |
| 19 | using ::testing::_; |
| 20 | using ::testing::Mock; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 21 | using std::vector; |
| 22 | |
| 23 | namespace apmanager { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const char kDeviceName[] = "phy0"; |
| 28 | const Device::WiFiInterface kApModeInterface0 = { |
| 29 | "uap0", kDeviceName, 1, NL80211_IFTYPE_AP |
| 30 | }; |
| 31 | const Device::WiFiInterface kApModeInterface1 = { |
| 32 | "uap1", kDeviceName, 2, NL80211_IFTYPE_AP |
| 33 | }; |
| 34 | const Device::WiFiInterface kManagedModeInterface0 = { |
| 35 | "wlan0", kDeviceName, 3, NL80211_IFTYPE_STATION |
| 36 | }; |
| 37 | const Device::WiFiInterface kManagedModeInterface1 = { |
| 38 | "wlan1", kDeviceName, 4, NL80211_IFTYPE_STATION |
| 39 | }; |
| 40 | const Device::WiFiInterface kMonitorModeInterface = { |
| 41 | "monitor0", kDeviceName, 5, NL80211_IFTYPE_MONITOR |
| 42 | }; |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | class DeviceTest : public testing::Test { |
| 47 | public: |
Peter Qiu | 7e0ffcf | 2014-12-02 12:53:27 -0800 | [diff] [blame] | 48 | DeviceTest() : device_(new Device(&manager_, kDeviceName)) {} |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 49 | |
| 50 | void VerifyInterfaceList( |
| 51 | const vector<Device::WiFiInterface>& interface_list) { |
| 52 | EXPECT_EQ(interface_list.size(), device_->interface_list_.size()); |
| 53 | for (size_t i = 0; i < interface_list.size(); i++) { |
| 54 | EXPECT_TRUE(interface_list[i].Equals(device_->interface_list_[i])); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void VerifyPreferredApInterface(const std::string& interface_name) { |
| 59 | EXPECT_EQ(interface_name, device_->GetPreferredApInterface()); |
| 60 | } |
| 61 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 62 | void AddWiphyBandAttribute(shill::AttributeListRefPtr wiphy_bands, |
| 63 | const std::string& band_name, |
| 64 | int band_id, |
| 65 | std::vector<uint32_t> frequency_list, |
| 66 | uint16_t ht_cap_mask) { |
| 67 | // Band attribute. |
| 68 | shill::AttributeListRefPtr wiphy_band; |
| 69 | wiphy_bands->CreateNestedAttribute(band_id, band_name.c_str()); |
| 70 | wiphy_bands->GetNestedAttributeList(band_id, &wiphy_band); |
| 71 | // Frequencies attribute. |
| 72 | shill::AttributeListRefPtr frequencies; |
| 73 | wiphy_band->CreateNestedAttribute(NL80211_BAND_ATTR_FREQS, |
| 74 | "NL80211_BAND_ATTR_FREQS"); |
| 75 | wiphy_band->GetNestedAttributeList(NL80211_BAND_ATTR_FREQS, |
| 76 | &frequencies); |
| 77 | // Frequency attribute. |
| 78 | for (size_t i = 0; i < frequency_list.size(); i++) { |
| 79 | shill::AttributeListRefPtr frequency; |
| 80 | frequencies->CreateNestedAttribute( |
| 81 | i, base::StringPrintf("Frequency %d", frequency_list[i]).c_str()); |
| 82 | frequencies->GetNestedAttributeList(i, &frequency); |
| 83 | frequency->CreateU32Attribute(NL80211_FREQUENCY_ATTR_FREQ, |
| 84 | "NL80211_FREQUENCY_ATTR_FREQ"); |
| 85 | frequency->SetU32AttributeValue(NL80211_FREQUENCY_ATTR_FREQ, |
| 86 | frequency_list[i]); |
| 87 | frequencies->SetNestedAttributeHasAValue(i); |
| 88 | } |
| 89 | wiphy_band->SetNestedAttributeHasAValue(NL80211_BAND_ATTR_FREQS); |
| 90 | |
| 91 | // HT Capability attribute. |
| 92 | wiphy_band->CreateU16Attribute(NL80211_BAND_ATTR_HT_CAPA, |
| 93 | "NL80211_BAND_ATTR_HT_CAPA"); |
| 94 | wiphy_band->SetU16AttributeValue(NL80211_BAND_ATTR_HT_CAPA, ht_cap_mask); |
| 95 | |
| 96 | wiphy_bands->SetNestedAttributeHasAValue(band_id); |
| 97 | } |
| 98 | |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 99 | void EnableApModeSupport() { |
| 100 | device_->supports_ap_mode_ = true; |
| 101 | } |
| 102 | |
| 103 | void VerifyApModeSupport(bool supports_ap_mode) { |
| 104 | EXPECT_EQ(supports_ap_mode, device_->supports_ap_mode_); |
| 105 | } |
| 106 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 107 | void VerifyFrequencyList(int band_id, std::vector<uint32_t> frequency_list) { |
| 108 | EXPECT_EQ(frequency_list, device_->band_capability_[band_id].frequencies); |
| 109 | } |
| 110 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 111 | protected: |
Peter Qiu | 7e0ffcf | 2014-12-02 12:53:27 -0800 | [diff] [blame] | 112 | MockManager manager_; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 113 | scoped_refptr<Device> device_; |
| 114 | }; |
| 115 | |
| 116 | TEST_F(DeviceTest, RegisterInterface) { |
| 117 | vector<Device::WiFiInterface> interface_list; |
| 118 | interface_list.push_back(kApModeInterface0); |
| 119 | interface_list.push_back(kManagedModeInterface0); |
| 120 | interface_list.push_back(kMonitorModeInterface); |
| 121 | |
| 122 | device_->RegisterInterface(kApModeInterface0); |
| 123 | device_->RegisterInterface(kManagedModeInterface0); |
| 124 | device_->RegisterInterface(kMonitorModeInterface); |
| 125 | |
| 126 | // Verify result interface list. |
| 127 | VerifyInterfaceList(interface_list); |
| 128 | } |
| 129 | |
| 130 | TEST_F(DeviceTest, DeregisterInterface) { |
| 131 | vector<Device::WiFiInterface> interface_list; |
| 132 | interface_list.push_back(kApModeInterface0); |
| 133 | interface_list.push_back(kManagedModeInterface0); |
| 134 | |
| 135 | // Register all interfaces, then deregister monitor0 and wlan1 interfaces. |
| 136 | device_->RegisterInterface(kApModeInterface0); |
| 137 | device_->RegisterInterface(kMonitorModeInterface); |
| 138 | device_->RegisterInterface(kManagedModeInterface0); |
| 139 | device_->RegisterInterface(kManagedModeInterface1); |
| 140 | device_->DeregisterInterface(kMonitorModeInterface); |
| 141 | device_->DeregisterInterface(kManagedModeInterface1); |
| 142 | |
| 143 | // Verify result interface list. |
| 144 | VerifyInterfaceList(interface_list); |
| 145 | } |
| 146 | |
| 147 | TEST_F(DeviceTest, PreferredAPInterface) { |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 148 | EnableApModeSupport(); |
| 149 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 150 | // Register a monitor mode interface, no preferred AP mode interface. |
| 151 | device_->RegisterInterface(kMonitorModeInterface); |
| 152 | VerifyPreferredApInterface(""); |
| 153 | |
| 154 | // Register a managed mode interface, should be set to preferred AP interface. |
| 155 | device_->RegisterInterface(kManagedModeInterface0); |
| 156 | VerifyPreferredApInterface(kManagedModeInterface0.iface_name); |
| 157 | |
| 158 | // Register a ap mode interface, should be set to preferred AP interface. |
| 159 | device_->RegisterInterface(kApModeInterface0); |
| 160 | VerifyPreferredApInterface(kApModeInterface0.iface_name); |
| 161 | |
| 162 | // Register another ap mode interface "uap1" and managed mode interface |
| 163 | // "wlan1", preferred AP interface should still be set to the first detected |
| 164 | // ap mode interface "uap0". |
| 165 | device_->RegisterInterface(kApModeInterface1); |
| 166 | device_->RegisterInterface(kManagedModeInterface1); |
| 167 | VerifyPreferredApInterface(kApModeInterface0.iface_name); |
| 168 | |
| 169 | // Deregister the first ap mode interface, preferred AP interface should be |
| 170 | // set to the second ap mode interface. |
| 171 | device_->DeregisterInterface(kApModeInterface0); |
| 172 | VerifyPreferredApInterface(kApModeInterface1.iface_name); |
| 173 | |
| 174 | // Deregister the second ap mode interface, preferred AP interface should be |
| 175 | // set the first managed mode interface. |
| 176 | device_->DeregisterInterface(kApModeInterface1); |
| 177 | VerifyPreferredApInterface(kManagedModeInterface0.iface_name); |
| 178 | |
| 179 | // Deregister the first managed mode interface, preferred AP interface |
| 180 | // should be set to the second managed mode interface. |
| 181 | device_->DeregisterInterface(kManagedModeInterface0); |
| 182 | VerifyPreferredApInterface(kManagedModeInterface1.iface_name); |
| 183 | |
| 184 | // Deregister the second managed mode interface, preferred AP interface |
| 185 | // should be set to empty string. |
| 186 | device_->DeregisterInterface(kManagedModeInterface1); |
| 187 | VerifyPreferredApInterface(""); |
| 188 | } |
| 189 | |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 190 | TEST_F(DeviceTest, DeviceWithoutAPModeSupport) { |
| 191 | // AP mode support is not enabled for the device, so no preferred AP |
| 192 | // mode interface. |
| 193 | device_->RegisterInterface(kApModeInterface0); |
| 194 | VerifyPreferredApInterface(""); |
| 195 | } |
| 196 | |
| 197 | TEST_F(DeviceTest, ParseWiphyCapability) { |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 198 | shill::NewWiphyMessage message; |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 199 | |
| 200 | // Supported interface types attribute. |
| 201 | message.attributes()->CreateNestedAttribute( |
| 202 | NL80211_ATTR_SUPPORTED_IFTYPES, "NL80211_ATTR_SUPPORTED_IFTYPES"); |
| 203 | shill::AttributeListRefPtr supported_iftypes; |
| 204 | message.attributes()->GetNestedAttributeList( |
| 205 | NL80211_ATTR_SUPPORTED_IFTYPES, &supported_iftypes); |
| 206 | // Add support for AP mode interface. |
| 207 | supported_iftypes->CreateFlagAttribute( |
| 208 | NL80211_IFTYPE_AP, "NL80211_IFTYPE_AP"); |
| 209 | supported_iftypes->SetFlagAttributeValue(NL80211_IFTYPE_AP, true); |
| 210 | message.attributes()->SetNestedAttributeHasAValue( |
| 211 | NL80211_ATTR_SUPPORTED_IFTYPES); |
| 212 | |
| 213 | // Wiphy bands attribute. |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 214 | message.attributes()->CreateNestedAttribute( |
| 215 | NL80211_ATTR_WIPHY_BANDS, "NL80211_ATTR_WIPHY_BANDS"); |
| 216 | shill::AttributeListRefPtr wiphy_bands; |
| 217 | message.attributes()->GetNestedAttributeList( |
| 218 | NL80211_ATTR_WIPHY_BANDS, &wiphy_bands); |
| 219 | |
| 220 | // 2.4GHz band capability. |
| 221 | const uint32_t kBand24GHzFrequencies[] = { |
| 222 | 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467}; |
| 223 | const uint16_t kBand24GHzHTCapMask = shill::IEEE_80211::kHTCapMaskLdpcCoding | |
| 224 | shill::IEEE_80211::kHTCapMaskGrnFld | |
| 225 | shill::IEEE_80211::kHTCapMaskSgi20; |
| 226 | std::vector<uint32_t> band_24ghz_freq_list( |
| 227 | kBand24GHzFrequencies, |
| 228 | kBand24GHzFrequencies + sizeof(kBand24GHzFrequencies) / |
| 229 | sizeof(kBand24GHzFrequencies[0])); |
| 230 | AddWiphyBandAttribute( |
| 231 | wiphy_bands, "2.4GHz band", 0, band_24ghz_freq_list, |
| 232 | kBand24GHzHTCapMask); |
| 233 | |
| 234 | // 5GHz band capability. |
| 235 | const uint32_t kBand5GHzFrequencies[] = { |
| 236 | 5180, 5190, 5200, 5210, 5220, 5230, 5240, 5260, 5280, 5300, 5320}; |
| 237 | const uint16_t kBand5GHzHTCapMask = |
| 238 | shill::IEEE_80211::kHTCapMaskLdpcCoding | |
| 239 | shill::IEEE_80211::kHTCapMaskSupWidth2040 | |
| 240 | shill::IEEE_80211::kHTCapMaskGrnFld | |
| 241 | shill::IEEE_80211::kHTCapMaskSgi20 | |
| 242 | shill::IEEE_80211::kHTCapMaskSgi40; |
| 243 | std::vector<uint32_t> band_5ghz_freq_list( |
| 244 | kBand5GHzFrequencies, |
| 245 | kBand5GHzFrequencies + sizeof(kBand5GHzFrequencies) / |
| 246 | sizeof(kBand5GHzFrequencies[0])); |
| 247 | AddWiphyBandAttribute( |
| 248 | wiphy_bands, "5GHz band", 1, band_5ghz_freq_list, kBand5GHzHTCapMask); |
| 249 | |
| 250 | message.attributes()->SetNestedAttributeHasAValue(NL80211_ATTR_WIPHY_BANDS); |
| 251 | |
| 252 | device_->ParseWiphyCapability(message); |
| 253 | |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 254 | // Verify AP mode support. |
| 255 | VerifyApModeSupport(true); |
| 256 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 257 | // Verify frequency list for both bands. |
| 258 | VerifyFrequencyList(0, band_24ghz_freq_list); |
| 259 | VerifyFrequencyList(1, band_5ghz_freq_list); |
| 260 | |
| 261 | // Verify HT Capablity for 2.4GHz band. |
| 262 | const char kBand24GHzHTCapability[] = "[LDPC SMPS-STATIC GF SHORT-GI-20]"; |
| 263 | std::string band_24ghz_cap; |
| 264 | EXPECT_TRUE(device_->GetHTCapability(6, &band_24ghz_cap)); |
| 265 | EXPECT_EQ(kBand24GHzHTCapability, band_24ghz_cap); |
| 266 | |
| 267 | // Verify HT Capablity for 5GHz band. |
| 268 | const char kBand5GHzHTCapability[] = |
| 269 | "[LDPC HT40+ SMPS-STATIC GF SHORT-GI-20 SHORT-GI-40]"; |
| 270 | std::string band_5ghz_cap; |
| 271 | EXPECT_TRUE(device_->GetHTCapability(36, &band_5ghz_cap)); |
| 272 | EXPECT_EQ(kBand5GHzHTCapability, band_5ghz_cap); |
| 273 | } |
| 274 | |
Peter Qiu | 7e0ffcf | 2014-12-02 12:53:27 -0800 | [diff] [blame] | 275 | TEST_F(DeviceTest, ClaimAndReleaseDevice) { |
Peter Qiu | 3d95ac7 | 2014-12-06 18:26:18 -0800 | [diff] [blame] | 276 | EnableApModeSupport(); |
| 277 | |
Peter Qiu | 7e0ffcf | 2014-12-02 12:53:27 -0800 | [diff] [blame] | 278 | // Register multiple interfaces. |
| 279 | device_->RegisterInterface(kApModeInterface1); |
| 280 | device_->RegisterInterface(kManagedModeInterface1); |
| 281 | |
| 282 | // Claim the device should claim all interfaces registered on this device. |
| 283 | EXPECT_CALL(manager_, ClaimInterface(kApModeInterface1.iface_name)).Times(1); |
| 284 | EXPECT_CALL(manager_, |
| 285 | ClaimInterface(kManagedModeInterface1.iface_name)).Times(1); |
| 286 | EXPECT_TRUE(device_->ClaimDevice()); |
| 287 | Mock::VerifyAndClearExpectations(&manager_); |
| 288 | |
| 289 | // Claim the device when it is already claimed. |
| 290 | EXPECT_CALL(manager_, ClaimInterface(_)).Times(0); |
| 291 | EXPECT_FALSE(device_->ClaimDevice()); |
| 292 | Mock::VerifyAndClearExpectations(&manager_); |
| 293 | |
| 294 | // Release the device should release all interfaces registered on this device. |
| 295 | EXPECT_CALL(manager_, |
| 296 | ReleaseInterface(kApModeInterface1.iface_name)).Times(1); |
| 297 | EXPECT_CALL(manager_, |
| 298 | ReleaseInterface(kManagedModeInterface1.iface_name)).Times(1); |
| 299 | EXPECT_TRUE(device_->ReleaseDevice()); |
| 300 | Mock::VerifyAndClearExpectations(&manager_); |
| 301 | |
| 302 | // Release the device when it is not claimed. |
| 303 | EXPECT_CALL(manager_, ReleaseInterface(_)).Times(0); |
| 304 | EXPECT_FALSE(device_->ReleaseDevice()); |
| 305 | Mock::VerifyAndClearExpectations(&manager_); |
| 306 | } |
| 307 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 308 | } // namespace apmanager |