mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 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 "shill/wifi_endpoint.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/stl_util.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
| 16 | |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 17 | #include "shill/ieee80211.h" |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 18 | #include "shill/mock_log.h" |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 19 | #include "shill/mock_wifi.h" |
| 20 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 21 | #include "shill/refptr_types.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 22 | #include "shill/wpa_supplicant.h" |
| 23 | |
| 24 | using std::map; |
| 25 | using std::set; |
| 26 | using std::string; |
| 27 | using std::vector; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 28 | using ::testing::_; |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 29 | using testing::HasSubstr; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 30 | using ::testing::NiceMock; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 31 | |
| 32 | namespace shill { |
| 33 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 34 | class WiFiEndpointTest : public PropertyStoreTest { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 35 | public: |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 36 | WiFiEndpointTest() : wifi_( |
| 37 | new NiceMock<MockWiFi>( |
| 38 | control_interface(), |
| 39 | dispatcher(), |
| 40 | metrics(), |
| 41 | manager(), |
| 42 | "wifi", |
| 43 | "aabbccddeeff", // fake mac |
| 44 | 0)) {} |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 45 | virtual ~WiFiEndpointTest() {} |
| 46 | |
| 47 | protected: |
| 48 | vector<string> make_string_vector1(const string &str1) { |
| 49 | vector<string> strvec; |
| 50 | strvec.push_back(str1); |
| 51 | return strvec; |
| 52 | } |
| 53 | |
| 54 | vector<string> make_string_vector2(const string &str1, const string &str2) { |
| 55 | vector<string> strvec; |
| 56 | strvec.push_back(str1); |
| 57 | strvec.push_back(str2); |
| 58 | return strvec; |
| 59 | } |
| 60 | |
| 61 | map<string, ::DBus::Variant> make_key_management_args( |
| 62 | vector<string> key_management_method_strings) { |
| 63 | map<string, ::DBus::Variant> args; |
| 64 | ::DBus::MessageIter writer; |
| 65 | writer = |
| 66 | args[wpa_supplicant::kSecurityMethodPropertyKeyManagement].writer(); |
| 67 | writer << key_management_method_strings; |
| 68 | return args; |
| 69 | } |
| 70 | |
| 71 | map<string, ::DBus::Variant> make_security_args( |
| 72 | const string &security_protocol, |
| 73 | const string &key_management_method) { |
| 74 | map<string, ::DBus::Variant> args; |
| 75 | ::DBus::MessageIter writer; |
| 76 | writer = args[security_protocol].writer(); |
| 77 | writer << |
| 78 | make_key_management_args(make_string_vector1(key_management_method)); |
| 79 | return args; |
| 80 | } |
| 81 | |
| 82 | const char *ParseSecurity( |
| 83 | const map<string, ::DBus::Variant> &properties) { |
| 84 | return WiFiEndpoint::ParseSecurity(properties); |
| 85 | } |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 86 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 87 | void AddIEWithData(uint8_t type, vector<uint8_t> data, vector<uint8_t> *ies) { |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 88 | ies->push_back(type); // type |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 89 | ies->push_back(data.size()); // length |
| 90 | ies->insert(ies->end(), data.begin(), data.end()); |
| 91 | } |
| 92 | |
| 93 | void AddIE(uint8_t type, vector<uint8_t> *ies) { |
| 94 | AddIEWithData(type, vector<uint8_t>(1), ies); |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 95 | } |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 96 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 97 | void AddVendorIE(uint32_t oui, uint8_t vendor_type, |
| 98 | const vector<uint8_t> &data, |
| 99 | vector<uint8_t> *ies) { |
| 100 | ies->push_back(IEEE_80211::kElemIdVendor); // type |
| 101 | ies->push_back(4 + data.size()); // length |
| 102 | ies->push_back((oui >> 16) & 0xff); // OUI MSByte |
| 103 | ies->push_back((oui >> 8) & 0xff); // OUI middle octet |
| 104 | ies->push_back(oui & 0xff); // OUI LSByte |
| 105 | ies->push_back(vendor_type); // OUI Type |
| 106 | ies->insert(ies->end(), data.begin(), data.end()); |
| 107 | } |
| 108 | |
| 109 | void AddWPSElement(uint16_t type, const string &value, |
| 110 | vector<uint8_t> *wps) { |
| 111 | wps->push_back(type >> 8); // type MSByte |
| 112 | wps->push_back(type); // type LSByte |
| 113 | CHECK(value.size() < kuint16max); |
| 114 | wps->push_back((value.size() >> 8) & 0xff); // length MSByte |
| 115 | wps->push_back(value.size() & 0xff); // length LSByte |
| 116 | wps->insert(wps->end(), value.begin(), value.end()); |
| 117 | } |
| 118 | |
| 119 | map<string, ::DBus::Variant> MakeBSSPropertiesWithIEs( |
| 120 | const vector<uint8_t> &ies) { |
| 121 | map<string, ::DBus::Variant> properties; |
| 122 | ::DBus::MessageIter writer = |
| 123 | properties[wpa_supplicant::kBSSPropertyIEs].writer(); |
| 124 | writer << ies; |
| 125 | return properties; |
| 126 | } |
| 127 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 128 | // Creates the RSN properties string (which still requires an information |
| 129 | // element prefix). |
| 130 | vector<uint8_t> MakeRSNProperties(uint16 pairwise_count, |
| 131 | uint16 authkey_count, |
| 132 | uint16 capabilities) { |
| 133 | vector<uint8_t> rsn(IEEE_80211::kRSNIECipherCountOffset + |
| 134 | IEEE_80211::kRSNIECipherCountLen * 2 + |
| 135 | IEEE_80211::kRSNIESelectorLen * |
| 136 | (pairwise_count + authkey_count) + |
| 137 | IEEE_80211::kRSNIECapabilitiesLen); |
| 138 | |
| 139 | // Set both cipher counts in little endian. |
| 140 | rsn[IEEE_80211::kRSNIECipherCountOffset] = pairwise_count & 0xff; |
| 141 | rsn[IEEE_80211::kRSNIECipherCountOffset + 1] = pairwise_count >> 8; |
| 142 | size_t authkey_offset = IEEE_80211::kRSNIECipherCountOffset + |
| 143 | IEEE_80211::kRSNIECipherCountLen + |
| 144 | pairwise_count * IEEE_80211::kRSNIESelectorLen; |
| 145 | rsn[authkey_offset] = authkey_count & 0xff; |
| 146 | rsn[authkey_offset + 1] = authkey_count >> 8; |
| 147 | |
| 148 | // Set the little-endian capabilities field. |
| 149 | size_t capabilities_offset = rsn.size() - 2; |
| 150 | rsn[capabilities_offset] = capabilities & 0xff; |
| 151 | rsn[capabilities_offset + 1] = capabilities >> 8; |
| 152 | |
| 153 | return rsn; |
| 154 | } |
| 155 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 156 | WiFiEndpoint *MakeOpenEndpoint(ProxyFactory *proxy_factory, |
| 157 | const WiFiRefPtr &wifi, |
| 158 | const std::string &ssid, |
| 159 | const std::string &bssid) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 160 | return WiFiEndpoint::MakeOpenEndpoint( |
| 161 | proxy_factory, wifi, ssid, bssid, 0, 0); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | scoped_refptr<MockWiFi> wifi() { return wifi_; } |
| 165 | |
| 166 | private: |
| 167 | scoped_refptr<MockWiFi> wifi_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | TEST_F(WiFiEndpointTest, ParseKeyManagementMethodsEAP) { |
| 171 | set<WiFiEndpoint::KeyManagement> parsed_methods; |
| 172 | WiFiEndpoint::ParseKeyManagementMethods( |
| 173 | make_key_management_args(make_string_vector1("something-eap")), |
| 174 | &parsed_methods); |
| 175 | EXPECT_TRUE( |
| 176 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagement802_1x)); |
| 177 | EXPECT_FALSE( |
| 178 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagementPSK)); |
| 179 | } |
| 180 | |
| 181 | TEST_F(WiFiEndpointTest, ParseKeyManagementMethodsPSK) { |
| 182 | set<WiFiEndpoint::KeyManagement> parsed_methods; |
| 183 | WiFiEndpoint::ParseKeyManagementMethods( |
| 184 | make_key_management_args(make_string_vector1("something-psk")), |
| 185 | &parsed_methods); |
| 186 | EXPECT_TRUE( |
| 187 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagementPSK)); |
| 188 | EXPECT_FALSE( |
| 189 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagement802_1x)); |
| 190 | } |
| 191 | |
| 192 | TEST_F(WiFiEndpointTest, ParseKeyManagementMethodsEAPAndPSK) { |
| 193 | set<WiFiEndpoint::KeyManagement> parsed_methods; |
| 194 | WiFiEndpoint::ParseKeyManagementMethods( |
| 195 | make_key_management_args( |
| 196 | make_string_vector2("something-eap", "something-psk")), |
| 197 | &parsed_methods); |
| 198 | EXPECT_TRUE( |
| 199 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagement802_1x)); |
| 200 | EXPECT_TRUE( |
| 201 | ContainsKey(parsed_methods, WiFiEndpoint::kKeyManagementPSK)); |
| 202 | } |
| 203 | |
| 204 | TEST_F(WiFiEndpointTest, ParseSecurityRSN802_1x) { |
| 205 | EXPECT_STREQ(flimflam::kSecurity8021x, |
| 206 | ParseSecurity(make_security_args("RSN", "something-eap"))); |
| 207 | } |
| 208 | |
| 209 | TEST_F(WiFiEndpointTest, ParseSecurityWPA802_1x) { |
| 210 | EXPECT_STREQ(flimflam::kSecurity8021x, |
| 211 | ParseSecurity(make_security_args("WPA", "something-eap"))); |
| 212 | } |
| 213 | |
| 214 | TEST_F(WiFiEndpointTest, ParseSecurityRSNPSK) { |
| 215 | EXPECT_STREQ(flimflam::kSecurityRsn, |
| 216 | ParseSecurity(make_security_args("RSN", "something-psk"))); |
| 217 | } |
| 218 | |
| 219 | TEST_F(WiFiEndpointTest, ParseSecurityWPAPSK) { |
| 220 | EXPECT_STREQ(flimflam::kSecurityWpa, |
| 221 | ParseSecurity(make_security_args("WPA", "something-psk"))); |
| 222 | } |
| 223 | |
| 224 | TEST_F(WiFiEndpointTest, ParseSecurityWEP) { |
| 225 | map<string, ::DBus::Variant> top_params; |
| 226 | top_params[wpa_supplicant::kPropertyPrivacy].writer().append_bool(true); |
| 227 | EXPECT_STREQ(flimflam::kSecurityWep, ParseSecurity(top_params)); |
| 228 | } |
| 229 | |
| 230 | TEST_F(WiFiEndpointTest, ParseSecurityNone) { |
| 231 | map<string, ::DBus::Variant> top_params; |
| 232 | EXPECT_STREQ(flimflam::kSecurityNone, ParseSecurity(top_params)); |
| 233 | } |
| 234 | |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 235 | TEST_F(WiFiEndpointTest, SSIDWithNull) { |
| 236 | WiFiEndpointRefPtr endpoint = |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 237 | MakeOpenEndpoint(NULL, NULL, string(1, 0), "00:00:00:00:00:01"); |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 238 | EXPECT_EQ("?", endpoint->ssid_string()); |
| 239 | } |
| 240 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 241 | TEST_F(WiFiEndpointTest, DeterminePhyModeFromFrequency) { |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 242 | { |
| 243 | map<string, ::DBus::Variant> properties; |
| 244 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11a, |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 245 | WiFiEndpoint::DeterminePhyModeFromFrequency(properties, 3200)); |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 246 | } |
| 247 | { |
| 248 | map<string, ::DBus::Variant> properties; |
| 249 | vector<uint32_t> rates(1, 22000000); |
| 250 | ::DBus::MessageIter writer = |
| 251 | properties[wpa_supplicant::kBSSPropertyRates].writer(); |
| 252 | writer << rates; |
| 253 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11b, |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 254 | WiFiEndpoint::DeterminePhyModeFromFrequency(properties, 2400)); |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 255 | } |
| 256 | { |
| 257 | map<string, ::DBus::Variant> properties; |
| 258 | vector<uint32_t> rates(1, 54000000); |
| 259 | ::DBus::MessageIter writer = |
| 260 | properties[wpa_supplicant::kBSSPropertyRates].writer(); |
| 261 | writer << rates; |
| 262 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11g, |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 263 | WiFiEndpoint::DeterminePhyModeFromFrequency(properties, 2400)); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | TEST_F(WiFiEndpointTest, ParseIEs) { |
| 268 | { |
| 269 | vector<uint8_t> ies; |
| 270 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 271 | WiFiEndpoint::VendorInformation vendor_information; |
| 272 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 273 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 274 | EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, phy_mode); |
| 275 | } |
| 276 | { |
| 277 | vector<uint8_t> ies; |
| 278 | AddIE(IEEE_80211::kElemIdErp, &ies); |
| 279 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 280 | WiFiEndpoint::VendorInformation vendor_information; |
| 281 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 282 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 283 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11g, phy_mode); |
| 284 | } |
| 285 | { |
| 286 | vector<uint8_t> ies; |
| 287 | AddIE(IEEE_80211::kElemIdHTCap, &ies); |
| 288 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 289 | WiFiEndpoint::VendorInformation vendor_information; |
| 290 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 291 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 292 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, phy_mode); |
| 293 | } |
| 294 | { |
| 295 | vector<uint8_t> ies; |
| 296 | AddIE(IEEE_80211::kElemIdHTInfo, &ies); |
| 297 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 298 | WiFiEndpoint::VendorInformation vendor_information; |
| 299 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 300 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 301 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, phy_mode); |
| 302 | } |
| 303 | { |
| 304 | vector<uint8_t> ies; |
| 305 | AddIE(IEEE_80211::kElemIdErp, &ies); |
| 306 | AddIE(IEEE_80211::kElemIdHTCap, &ies); |
| 307 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 308 | WiFiEndpoint::VendorInformation vendor_information; |
| 309 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 310 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 311 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, phy_mode); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | TEST_F(WiFiEndpointTest, ParseVendorIEs) { |
| 316 | { |
| 317 | ScopedMockLog log; |
| 318 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 319 | HasSubstr("no room in IE for OUI and type field."))) |
| 320 | .Times(1); |
| 321 | vector<uint8_t> ies; |
| 322 | AddIE(IEEE_80211::kElemIdVendor, &ies); |
| 323 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 324 | WiFiEndpoint::VendorInformation vendor_information; |
| 325 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 326 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 327 | } |
| 328 | { |
| 329 | vector<uint8_t> ies; |
| 330 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 331 | WiFiEndpoint::VendorInformation vendor_information; |
| 332 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 333 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 334 | EXPECT_EQ("", vendor_information.wps_manufacturer); |
| 335 | EXPECT_EQ("", vendor_information.wps_model_name); |
| 336 | EXPECT_EQ("", vendor_information.wps_model_number); |
| 337 | EXPECT_EQ("", vendor_information.wps_device_name); |
| 338 | EXPECT_EQ(0, vendor_information.oui_list.size()); |
| 339 | } |
| 340 | { |
| 341 | ScopedMockLog log; |
| 342 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 343 | HasSubstr("IE extends past containing PDU"))).Times(1); |
| 344 | vector<uint8_t> ies; |
| 345 | AddVendorIE(0, 0, vector<uint8_t>(), &ies); |
| 346 | ies.resize(ies.size() - 1); // Cause an underrun in the data. |
| 347 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 348 | WiFiEndpoint::VendorInformation vendor_information; |
| 349 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 350 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 351 | } |
| 352 | { |
| 353 | vector<uint8_t> ies; |
| 354 | const uint32_t kVendorOUI = 0xaabbcc; |
| 355 | AddVendorIE(kVendorOUI, 0, vector<uint8_t>(), &ies); |
| 356 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, 0, vector<uint8_t>(), &ies); |
| 357 | AddVendorIE(IEEE_80211::kOUIVendorEpigram, 0, vector<uint8_t>(), &ies); |
| 358 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 359 | WiFiEndpoint::VendorInformation vendor_information; |
| 360 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 361 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 362 | EXPECT_EQ("", vendor_information.wps_manufacturer); |
| 363 | EXPECT_EQ("", vendor_information.wps_model_name); |
| 364 | EXPECT_EQ("", vendor_information.wps_model_number); |
| 365 | EXPECT_EQ("", vendor_information.wps_device_name); |
| 366 | EXPECT_EQ(1, vendor_information.oui_list.size()); |
| 367 | EXPECT_FALSE(vendor_information.oui_list.find(kVendorOUI) == |
| 368 | vendor_information.oui_list.end()); |
| 369 | |
| 370 | WiFiEndpointRefPtr endpoint = |
| 371 | MakeOpenEndpoint(NULL, NULL, string(1, 0), "00:00:00:00:00:01"); |
| 372 | endpoint->vendor_information_ = vendor_information; |
| 373 | map<string, string> vendor_stringmap(endpoint->GetVendorInformation()); |
| 374 | EXPECT_FALSE(ContainsKey(vendor_stringmap, kVendorWPSManufacturerProperty)); |
| 375 | EXPECT_FALSE(ContainsKey(vendor_stringmap, kVendorWPSModelNameProperty)); |
| 376 | EXPECT_FALSE(ContainsKey(vendor_stringmap, kVendorWPSModelNumberProperty)); |
| 377 | EXPECT_FALSE(ContainsKey(vendor_stringmap, kVendorWPSDeviceNameProperty)); |
| 378 | EXPECT_EQ("aa-bb-cc", vendor_stringmap[kVendorOUIListProperty]); |
| 379 | } |
| 380 | { |
| 381 | ScopedMockLog log; |
| 382 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 383 | HasSubstr("WPS element extends past containing PDU"))) |
| 384 | .Times(1); |
| 385 | vector<uint8_t> ies; |
| 386 | vector<uint8_t> wps; |
| 387 | AddWPSElement(IEEE_80211::kWPSElementManufacturer, "foo", &wps); |
| 388 | wps.resize(wps.size() - 1); // Cause an underrun in the data. |
| 389 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, |
| 390 | IEEE_80211::kOUIMicrosoftWPS, wps, &ies); |
| 391 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 392 | WiFiEndpoint::VendorInformation vendor_information; |
| 393 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 394 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 395 | EXPECT_EQ("", vendor_information.wps_manufacturer); |
| 396 | } |
| 397 | { |
| 398 | vector<uint8_t> ies; |
| 399 | vector<uint8_t> wps; |
| 400 | const string kManufacturer("manufacturer"); |
| 401 | const string kModelName("modelname"); |
| 402 | const string kModelNumber("modelnumber"); |
| 403 | const string kDeviceName("devicename"); |
| 404 | AddWPSElement(IEEE_80211::kWPSElementManufacturer, kManufacturer, &wps); |
| 405 | AddWPSElement(IEEE_80211::kWPSElementModelName, kModelName, &wps); |
| 406 | AddWPSElement(IEEE_80211::kWPSElementModelNumber, kModelNumber, &wps); |
| 407 | AddWPSElement(IEEE_80211::kWPSElementDeviceName, kDeviceName, &wps); |
| 408 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, |
| 409 | IEEE_80211::kOUIMicrosoftWPS, wps, &ies); |
| 410 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 411 | WiFiEndpoint::VendorInformation vendor_information; |
| 412 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 413 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 414 | EXPECT_EQ(kManufacturer, vendor_information.wps_manufacturer); |
| 415 | EXPECT_EQ(kModelName, vendor_information.wps_model_name); |
| 416 | EXPECT_EQ(kModelNumber, vendor_information.wps_model_number); |
| 417 | EXPECT_EQ(kDeviceName, vendor_information.wps_device_name); |
| 418 | |
| 419 | WiFiEndpointRefPtr endpoint = |
| 420 | MakeOpenEndpoint(NULL, NULL, string(1, 0), "00:00:00:00:00:01"); |
| 421 | endpoint->vendor_information_ = vendor_information; |
| 422 | map<string, string> vendor_stringmap(endpoint->GetVendorInformation()); |
| 423 | EXPECT_EQ(kManufacturer, vendor_stringmap[kVendorWPSManufacturerProperty]); |
| 424 | EXPECT_EQ(kModelName, vendor_stringmap[kVendorWPSModelNameProperty]); |
| 425 | EXPECT_EQ(kModelNumber, vendor_stringmap[kVendorWPSModelNumberProperty]); |
| 426 | EXPECT_EQ(kDeviceName, vendor_stringmap[kVendorWPSDeviceNameProperty]); |
| 427 | EXPECT_FALSE(ContainsKey(vendor_stringmap, kVendorOUIListProperty)); |
| 428 | } |
| 429 | { |
| 430 | vector<uint8_t> ies; |
| 431 | vector<uint8_t> wps; |
| 432 | const string kManufacturer("manufacturer"); |
| 433 | const string kModelName("modelname"); |
| 434 | AddWPSElement(IEEE_80211::kWPSElementManufacturer, kManufacturer, &wps); |
| 435 | wps.resize(wps.size() - 1); // Insert a non-ASCII character in the WPS. |
| 436 | wps.push_back(0x80); |
| 437 | AddWPSElement(IEEE_80211::kWPSElementModelName, kModelName, &wps); |
| 438 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, |
| 439 | IEEE_80211::kOUIMicrosoftWPS, wps, &ies); |
| 440 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 441 | WiFiEndpoint::VendorInformation vendor_information; |
| 442 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 443 | &phy_mode, &vendor_information, NULL); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 444 | EXPECT_EQ("", vendor_information.wps_manufacturer); |
| 445 | EXPECT_EQ(kModelName, vendor_information.wps_model_name); |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 449 | TEST_F(WiFiEndpointTest, ParseWPACapabilities) { |
| 450 | { |
| 451 | vector<uint8_t> ies; |
| 452 | vector<uint8_t> rsn; |
| 453 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, IEEE_80211::kOUIMicrosoftWPA, |
| 454 | rsn, &ies); |
| 455 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn, &ies); |
| 456 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 457 | WiFiEndpoint::VendorInformation vendor_information; |
| 458 | bool ieee80211w_required = false; |
| 459 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 460 | &phy_mode, &vendor_information, |
| 461 | &ieee80211w_required); |
| 462 | EXPECT_FALSE(ieee80211w_required); |
| 463 | } |
| 464 | { |
| 465 | vector<uint8_t> ies; |
| 466 | vector<uint8_t> rsn = MakeRSNProperties( |
| 467 | 2, 3, ~IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 468 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, IEEE_80211::kOUIMicrosoftWPA, |
| 469 | rsn, &ies); |
| 470 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn, &ies); |
| 471 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 472 | WiFiEndpoint::VendorInformation vendor_information; |
| 473 | bool ieee80211w_required = false; |
| 474 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 475 | &phy_mode, &vendor_information, |
| 476 | &ieee80211w_required); |
| 477 | EXPECT_FALSE(ieee80211w_required); |
| 478 | } |
| 479 | { |
| 480 | vector<uint8_t> ies; |
| 481 | vector<uint8_t> rsn = MakeRSNProperties( |
| 482 | 2, 3, IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 483 | AddVendorIE(IEEE_80211::kOUIVendorMicrosoft, IEEE_80211::kOUIMicrosoftWPA, |
| 484 | rsn, &ies); |
| 485 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 486 | WiFiEndpoint::VendorInformation vendor_information; |
| 487 | bool ieee80211w_required = false; |
| 488 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 489 | &phy_mode, &vendor_information, |
| 490 | &ieee80211w_required); |
| 491 | EXPECT_TRUE(ieee80211w_required); |
| 492 | } |
| 493 | { |
| 494 | vector<uint8_t> ies; |
| 495 | vector<uint8_t> rsn = MakeRSNProperties( |
| 496 | 8, 2, IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 497 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn, &ies); |
| 498 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 499 | WiFiEndpoint::VendorInformation vendor_information; |
| 500 | bool ieee80211w_required = false; |
| 501 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 502 | &phy_mode, &vendor_information, |
| 503 | &ieee80211w_required); |
| 504 | EXPECT_TRUE(ieee80211w_required); |
| 505 | } |
| 506 | { |
| 507 | vector<uint8_t> ies; |
| 508 | vector<uint8_t> rsn = MakeRSNProperties( |
| 509 | 8, 2, IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 510 | rsn.resize(rsn.size() + 1); |
| 511 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn, &ies); |
| 512 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 513 | WiFiEndpoint::VendorInformation vendor_information; |
| 514 | bool ieee80211w_required = false; |
| 515 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 516 | &phy_mode, &vendor_information, |
| 517 | &ieee80211w_required); |
| 518 | EXPECT_TRUE(ieee80211w_required); |
| 519 | } |
| 520 | { |
| 521 | vector<uint8_t> ies; |
| 522 | vector<uint8_t> rsn = MakeRSNProperties( |
| 523 | 8, 2, IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 524 | rsn.resize(rsn.size() - 1); |
| 525 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn, &ies); |
| 526 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 527 | WiFiEndpoint::VendorInformation vendor_information; |
| 528 | bool ieee80211w_required = false; |
| 529 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 530 | &phy_mode, &vendor_information, |
| 531 | &ieee80211w_required); |
| 532 | EXPECT_FALSE(ieee80211w_required); |
| 533 | } |
| 534 | { |
| 535 | vector<uint8_t> ies; |
| 536 | vector<uint8_t> rsn0 = MakeRSNProperties( |
| 537 | 1, 1, IEEE_80211::kRSNCapabilityFrameProtectionRequired); |
| 538 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn0, &ies); |
| 539 | vector<uint8_t> rsn1 = MakeRSNProperties(1, 1, 0); |
| 540 | AddIEWithData(IEEE_80211::kElemIdRSN, rsn1, &ies); |
| 541 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 542 | WiFiEndpoint::VendorInformation vendor_information; |
| 543 | bool ieee80211w_required = false; |
| 544 | WiFiEndpoint::ParseIEs(MakeBSSPropertiesWithIEs(ies), |
| 545 | &phy_mode, &vendor_information, |
| 546 | &ieee80211w_required); |
| 547 | EXPECT_TRUE(ieee80211w_required); |
| 548 | } |
| 549 | } |
| 550 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 551 | TEST_F(WiFiEndpointTest, PropertiesChanged) { |
| 552 | WiFiEndpointRefPtr endpoint = |
| 553 | MakeOpenEndpoint(NULL, wifi(), "ssid", "00:00:00:00:00:01"); |
| 554 | map<string, ::DBus::Variant> changed_properties; |
| 555 | ::DBus::MessageIter writer; |
| 556 | int16_t signal_strength = 10; |
| 557 | |
| 558 | EXPECT_NE(signal_strength, endpoint->signal_strength()); |
| 559 | writer = |
| 560 | changed_properties[wpa_supplicant::kBSSPropertySignal].writer(); |
| 561 | writer << signal_strength; |
| 562 | |
| 563 | EXPECT_CALL(*wifi(), NotifyEndpointChanged(_)); |
| 564 | endpoint->PropertiesChanged(changed_properties); |
| 565 | EXPECT_EQ(signal_strength, endpoint->signal_strength()); |
| 566 | } |
| 567 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 568 | } // namespace shill |