blob: 997d94f5cdfba42501da20e68a666dace5aa5c78 [file] [log] [blame]
Peter Qiufb39ba42014-11-21 09:09:59 -08001// 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 Qiu8e785b92014-11-24 10:01:08 -080010#include <base/strings/stringprintf.h>
Peter Qiufb39ba42014-11-21 09:09:59 -080011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
Peter Qiu8e785b92014-11-24 10:01:08 -080013#include <shill/net/ieee80211.h>
14#include <shill/net/nl80211_attribute.h>
15#include <shill/net/nl80211_message.h>
Peter Qiufb39ba42014-11-21 09:09:59 -080016
Peter Qiu7e0ffcf2014-12-02 12:53:27 -080017#include "apmanager/mock_manager.h"
18
19using ::testing::_;
20using ::testing::Mock;
Peter Qiufb39ba42014-11-21 09:09:59 -080021using std::vector;
22
23namespace apmanager {
24
25namespace {
26
27const char kDeviceName[] = "phy0";
28const Device::WiFiInterface kApModeInterface0 = {
29 "uap0", kDeviceName, 1, NL80211_IFTYPE_AP
30};
31const Device::WiFiInterface kApModeInterface1 = {
32 "uap1", kDeviceName, 2, NL80211_IFTYPE_AP
33};
34const Device::WiFiInterface kManagedModeInterface0 = {
35 "wlan0", kDeviceName, 3, NL80211_IFTYPE_STATION
36};
37const Device::WiFiInterface kManagedModeInterface1 = {
38 "wlan1", kDeviceName, 4, NL80211_IFTYPE_STATION
39};
40const Device::WiFiInterface kMonitorModeInterface = {
41 "monitor0", kDeviceName, 5, NL80211_IFTYPE_MONITOR
42};
43
44} // namespace
45
46class DeviceTest : public testing::Test {
47 public:
Peter Qiu7e0ffcf2014-12-02 12:53:27 -080048 DeviceTest() : device_(new Device(&manager_, kDeviceName)) {}
Peter Qiufb39ba42014-11-21 09:09:59 -080049
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 Qiu8e785b92014-11-24 10:01:08 -080062 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 Qiu3d95ac72014-12-06 18:26:18 -080099 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 Qiu8e785b92014-11-24 10:01:08 -0800107 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 Qiufb39ba42014-11-21 09:09:59 -0800111 protected:
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800112 MockManager manager_;
Peter Qiufb39ba42014-11-21 09:09:59 -0800113 scoped_refptr<Device> device_;
114};
115
116TEST_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
130TEST_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
147TEST_F(DeviceTest, PreferredAPInterface) {
Peter Qiu3d95ac72014-12-06 18:26:18 -0800148 EnableApModeSupport();
149
Peter Qiufb39ba42014-11-21 09:09:59 -0800150 // 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 Qiu3d95ac72014-12-06 18:26:18 -0800190TEST_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
197TEST_F(DeviceTest, ParseWiphyCapability) {
Peter Qiu8e785b92014-11-24 10:01:08 -0800198 shill::NewWiphyMessage message;
Peter Qiu3d95ac72014-12-06 18:26:18 -0800199
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 Qiu8e785b92014-11-24 10:01:08 -0800214 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 Qiu3d95ac72014-12-06 18:26:18 -0800254 // Verify AP mode support.
255 VerifyApModeSupport(true);
256
Peter Qiu8e785b92014-11-24 10:01:08 -0800257 // 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 Qiu7e0ffcf2014-12-02 12:53:27 -0800275TEST_F(DeviceTest, ClaimAndReleaseDevice) {
Peter Qiu3d95ac72014-12-06 18:26:18 -0800276 EnableApModeSupport();
277
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800278 // 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 Qiufb39ba42014-11-21 09:09:59 -0800308} // namespace apmanager