mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -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_service.h" |
| 6 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 7 | #include <map> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/string_util.h> |
| 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 16 | #include "shill/event_dispatcher.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_adaptors.h" |
| 19 | #include "shill/mock_control.h" |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 20 | #include "shill/mock_nss.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 21 | #include "shill/mock_service.h" |
| 22 | #include "shill/mock_store.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 23 | #include "shill/mock_wifi.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 25 | #include "shill/refptr_types.h" |
| 26 | #include "shill/wifi_endpoint.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 27 | #include "shill/wpa_supplicant.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 28 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 29 | using std::map; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | using std::vector; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 32 | using ::testing::_; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 33 | using ::testing::AnyNumber; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 34 | using ::testing::DoAll; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 35 | using ::testing::Mock; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 36 | using ::testing::NiceMock; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 37 | using ::testing::Return; |
| 38 | using ::testing::SetArgumentPointee; |
| 39 | using ::testing::StrEq; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 40 | using ::testing::StrNe; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 41 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 42 | namespace shill { |
| 43 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 44 | class WiFiServiceTest : public PropertyStoreTest { |
| 45 | public: |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 46 | WiFiServiceTest() : wifi_( |
| 47 | new NiceMock<MockWiFi>( |
| 48 | control_interface(), |
| 49 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 50 | metrics(), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 51 | manager(), |
| 52 | "wifi", |
| 53 | fake_mac, |
| 54 | 0)) {} |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 55 | virtual ~WiFiServiceTest() {} |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | static const char fake_mac[]; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 59 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 60 | bool CheckConnectable(const std::string &security, const char *passphrase, |
| 61 | Service::EapCredentials *eap) { |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 62 | Error error; |
| 63 | vector<uint8_t> ssid(1, 'a'); |
| 64 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 65 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 66 | metrics(), |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 67 | manager(), |
| 68 | wifi(), |
| 69 | ssid, |
| 70 | flimflam::kModeManaged, |
| 71 | security, |
| 72 | false); |
| 73 | if (passphrase) |
| 74 | service->SetPassphrase(passphrase, &error); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 75 | if (eap) { |
| 76 | service->set_eap(*eap); |
| 77 | } |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 78 | return service->connectable(); |
| 79 | } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 80 | WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid, |
| 81 | uint16 frequency, int16 signal_dbm) { |
| 82 | return WiFiEndpoint::MakeOpenEndpoint( |
| 83 | NULL, NULL, ssid, bssid, frequency, signal_dbm); |
| 84 | } |
| 85 | WiFiService *MakeGenericService() { |
| 86 | return new WiFiService(control_interface(), |
| 87 | dispatcher(), |
| 88 | metrics(), |
| 89 | manager(), |
| 90 | wifi(), |
| 91 | vector<uint8_t>(), |
| 92 | flimflam::kModeManaged, |
| 93 | flimflam::kSecurityWep, |
| 94 | false); |
| 95 | } |
| 96 | ServiceMockAdaptor *GetAdaptor(WiFiService *service) { |
| 97 | return dynamic_cast<ServiceMockAdaptor *>(service->adaptor()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 98 | } |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 99 | scoped_refptr<MockWiFi> wifi() { return wifi_; } |
| 100 | |
| 101 | private: |
| 102 | scoped_refptr<MockWiFi> wifi_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 105 | // static |
| 106 | const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF"; |
| 107 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 108 | class WiFiServiceSecurityTest : public WiFiServiceTest { |
| 109 | public: |
| 110 | WiFiServiceRefPtr CreateServiceWithSecurity(const string &security) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 111 | vector<uint8_t> ssid(5); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 112 | ssid.push_back(0xff); |
| 113 | |
| 114 | return new WiFiService(control_interface(), |
| 115 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 116 | metrics(), |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 117 | manager(), |
| 118 | wifi(), |
| 119 | ssid, |
| 120 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 121 | security, |
| 122 | false); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service, |
| 126 | const string &security) { |
| 127 | string id = wifi_service->GetStorageIdentifier(); |
| 128 | size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac))); |
| 129 | EXPECT_NE(mac_pos, string::npos); |
| 130 | size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos); |
| 131 | EXPECT_NE(mode_pos, string::npos); |
| 132 | return id.find(string(security), mode_pos) != string::npos; |
| 133 | } |
| 134 | |
| 135 | // Test that a service that is created with security |from_security| |
| 136 | // gets by default a storage identifier with |to_security| as its |
| 137 | // security component. |
| 138 | bool TestStorageMapping(const string &from_security, |
| 139 | const string &to_security) { |
| 140 | WiFiServiceRefPtr wifi_service = CreateServiceWithSecurity(from_security); |
| 141 | return TestStorageSecurityIs(wifi_service, to_security); |
| 142 | } |
| 143 | |
| 144 | // Test whether a service of type |service_security| can load from a |
| 145 | // storage interface containing an entry for |storage_security|. |
| 146 | // Make sure the result meets |expectation|. If |expectation| is |
| 147 | // true, also make sure the service storage identifier changes to |
| 148 | // match |storage_security|. |
| 149 | bool TestLoadMapping(const string &service_security, |
| 150 | const string &storage_security, |
| 151 | bool expectation) { |
| 152 | WiFiServiceRefPtr wifi_service = |
| 153 | CreateServiceWithSecurity(service_security); |
| 154 | NiceMock<MockStore> mock_store; |
| 155 | const string storage_id = |
| 156 | wifi_service->GetStorageIdentifierForSecurity(storage_security); |
| 157 | EXPECT_CALL(mock_store, ContainsGroup(_)) |
| 158 | .WillRepeatedly(Return(false)); |
| 159 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 160 | .WillRepeatedly(Return(true)); |
| 161 | bool is_loadable = wifi_service->IsLoadableFrom(&mock_store); |
| 162 | EXPECT_EQ(expectation, is_loadable); |
| 163 | bool is_loaded = wifi_service->Load(&mock_store); |
| 164 | EXPECT_EQ(expectation, is_loaded); |
| 165 | |
| 166 | if (expectation != is_loadable || expectation != is_loaded) { |
| 167 | return false; |
| 168 | } else if (!expectation) { |
| 169 | return true; |
| 170 | } else { |
| 171 | return TestStorageSecurityIs(wifi_service, storage_security); |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 176 | class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest { |
| 177 | public: |
| 178 | WiFiServiceUpdateFromEndpointsTest() |
| 179 | : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)), |
| 180 | kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)), |
| 181 | kGoodEndpointStrength( |
| 182 | WiFiService::SignalToStrength(kGoodEndpointSignal)), |
| 183 | service(MakeGenericService()), |
| 184 | adaptor(*GetAdaptor(service)) { |
| 185 | ok_endpoint = MakeEndpoint( |
| 186 | "", "00:00:00:00:01", kOkEndpointFrequency, kOkEndpointSignal); |
| 187 | good_endpoint = MakeEndpoint( |
| 188 | "", "00:00:00:00:02", kGoodEndpointFrequency, kGoodEndpointSignal); |
| 189 | bad_endpoint = MakeEndpoint( |
| 190 | "", "00:00:00:00:03", kBadEndpointFrequency, kBadEndpointSignal); |
| 191 | } |
| 192 | |
| 193 | protected: |
| 194 | static const uint16 kOkEndpointFrequency = 2422; |
| 195 | static const uint16 kBadEndpointFrequency = 2417; |
| 196 | static const uint16 kGoodEndpointFrequency = 2412; |
| 197 | static const int16 kOkEndpointSignal = -50; |
| 198 | static const int16 kBadEndpointSignal = -75; |
| 199 | static const int16 kGoodEndpointSignal = -25; |
| 200 | // Can't be both static and const (because initialization requires a |
| 201 | // function call). So choose to be just const. |
| 202 | const uint8 kOkEndpointStrength; |
| 203 | const uint8 kBadEndpointStrength; |
| 204 | const uint8 kGoodEndpointStrength; |
| 205 | WiFiEndpointRefPtr ok_endpoint; |
| 206 | WiFiEndpointRefPtr bad_endpoint; |
| 207 | WiFiEndpointRefPtr good_endpoint; |
| 208 | WiFiServiceRefPtr service; |
| 209 | ServiceMockAdaptor &adaptor; |
| 210 | }; |
| 211 | |
| 212 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 213 | TEST_F(WiFiServiceTest, StorageId) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 214 | vector<uint8_t> ssid(5); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 215 | ssid.push_back(0xff); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 216 | |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 217 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 218 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 219 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 220 | manager(), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 221 | wifi(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 222 | ssid, |
| 223 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 224 | flimflam::kSecurityNone, |
| 225 | false); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 226 | string id = wifi_service->GetStorageIdentifier(); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 227 | for (uint i = 0; i < id.length(); ++i) { |
| 228 | EXPECT_TRUE(id[i] == '_' || |
| 229 | isxdigit(id[i]) || |
| 230 | (isalpha(id[i]) && islower(id[i]))); |
| 231 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 232 | EXPECT_TRUE(wifi_service->TechnologyIs(Technology::kWifi)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 233 | size_t mac_pos = id.find(StringToLowerASCII(string(fake_mac))); |
| 234 | EXPECT_NE(mac_pos, string::npos); |
| 235 | EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos); |
| 236 | } |
| 237 | |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 238 | // Make sure the passphrase is registered as a write only property |
| 239 | // by reading and comparing all string properties returned on the store. |
| 240 | TEST_F(WiFiServiceTest, PassphraseWriteOnly) { |
| 241 | vector<uint8_t> ssid(5); |
| 242 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 243 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 244 | metrics(), |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 245 | manager(), |
| 246 | wifi(), |
| 247 | ssid, |
| 248 | flimflam::kModeManaged, |
| 249 | flimflam::kSecurityWpa, |
| 250 | false); |
| 251 | ReadablePropertyConstIterator<string> it = |
| 252 | (wifi_service->store()).GetStringPropertiesIter(); |
| 253 | for( ; !it.AtEnd(); it.Advance()) |
| 254 | EXPECT_NE(it.Key(), flimflam::kPassphraseProperty); |
| 255 | } |
| 256 | |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 257 | // Make sure setting the passphrase via D-Bus Service.SetProperty validates |
| 258 | // the passphrase. |
| 259 | TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) { |
| 260 | // We only spot check two password cases here to make sure the |
| 261 | // SetProperty code path does validation. We're not going to exhaustively |
| 262 | // test for all types of passwords. |
| 263 | vector<uint8_t> ssid(5); |
| 264 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 265 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 266 | metrics(), |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 267 | manager(), |
| 268 | wifi(), |
| 269 | ssid, |
| 270 | flimflam::kModeManaged, |
| 271 | flimflam::kSecurityWep, |
| 272 | false); |
| 273 | Error error; |
| 274 | EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty( |
| 275 | flimflam::kPassphraseProperty, "0:abcde", &error)); |
| 276 | EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty( |
| 277 | flimflam::kPassphraseProperty, "invalid", &error)); |
| 278 | EXPECT_EQ(Error::kInvalidPassphrase, error.type()); |
| 279 | } |
| 280 | |
| 281 | TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) { |
| 282 | vector<uint8_t> ssid(5); |
| 283 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 284 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 285 | metrics(), |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 286 | manager(), |
| 287 | wifi(), |
| 288 | ssid, |
| 289 | flimflam::kModeManaged, |
| 290 | flimflam::kSecurityNone, |
| 291 | false); |
| 292 | Error error; |
| 293 | EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty( |
| 294 | flimflam::kPassphraseProperty, "invalid", &error)); |
| 295 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 296 | } |
| 297 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 298 | TEST_F(WiFiServiceTest, NonUTF8SSID) { |
| 299 | vector<uint8_t> ssid; |
| 300 | |
| 301 | ssid.push_back(0xff); // not a valid UTF-8 byte-sequence |
| 302 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 303 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 304 | metrics(), |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 305 | manager(), |
| 306 | wifi(), |
| 307 | ssid, |
| 308 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 309 | flimflam::kSecurityNone, |
| 310 | false); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 311 | map<string, ::DBus::Variant> properties; |
| 312 | // if service doesn't propertly sanitize SSID, this will generate SIGABRT. |
| 313 | DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL); |
| 314 | } |
| 315 | |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 316 | MATCHER(WPASecurityArgs, "") { |
| 317 | return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) && |
| 318 | ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey); |
| 319 | } |
| 320 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 321 | MATCHER(EAPSecurityArgs, "") { |
| 322 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 323 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath); |
| 324 | } |
| 325 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 326 | TEST_F(WiFiServiceTest, ConnectTaskWPA) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 327 | vector<uint8_t> ssid(5); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 328 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 329 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 330 | metrics(), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 331 | manager(), |
| 332 | wifi(), |
| 333 | ssid, |
| 334 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 335 | flimflam::kSecurityWpa, |
| 336 | false); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 337 | EXPECT_CALL(*wifi(), |
| 338 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 339 | wifi_service->Connect(NULL); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 340 | } |
| 341 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 342 | TEST_F(WiFiServiceTest, ConnectTaskRSN) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 343 | vector<uint8_t> ssid(5); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 344 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 345 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 346 | metrics(), |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 347 | manager(), |
| 348 | wifi(), |
| 349 | ssid, |
| 350 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 351 | flimflam::kSecurityRsn, |
| 352 | false); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 353 | EXPECT_CALL(*wifi(), |
| 354 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 355 | wifi_service->Connect(NULL); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 358 | TEST_F(WiFiServiceTest, ConnectTaskPSK) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 359 | vector<uint8_t> ssid(5); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 360 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 361 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 362 | metrics(), |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 363 | manager(), |
| 364 | wifi(), |
| 365 | ssid, |
| 366 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 367 | flimflam::kSecurityPsk, |
| 368 | false); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 369 | EXPECT_CALL(*wifi(), |
| 370 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 371 | wifi_service->Connect(NULL); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 374 | TEST_F(WiFiServiceTest, ConnectTask8021x) { |
| 375 | vector<uint8_t> ssid(5); |
| 376 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 377 | dispatcher(), |
| 378 | metrics(), |
| 379 | manager(), |
| 380 | wifi(), |
| 381 | ssid, |
| 382 | flimflam::kModeManaged, |
| 383 | flimflam::kSecurity8021x, |
| 384 | false); |
| 385 | Service::EapCredentials eap; |
| 386 | eap.identity = "identity"; |
| 387 | wifi_service->set_eap(eap); |
| 388 | EXPECT_CALL(*wifi(), |
| 389 | ConnectTo(wifi_service.get(), EAPSecurityArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 390 | wifi_service->Connect(NULL); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 393 | MATCHER(WEPSecurityArgsKeyIndex0, "") { |
| 394 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 395 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) && |
| 396 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 397 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 398 | reader().get_uint32() == 0); |
| 399 | } |
| 400 | |
| 401 | MATCHER(WEPSecurityArgsKeyIndex1, "") { |
| 402 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 403 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) && |
| 404 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 405 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 406 | reader().get_uint32() == 1); |
| 407 | } |
| 408 | |
| 409 | MATCHER(WEPSecurityArgsKeyIndex2, "") { |
| 410 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 411 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) && |
| 412 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 413 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 414 | reader().get_uint32() == 2); |
| 415 | } |
| 416 | |
| 417 | MATCHER(WEPSecurityArgsKeyIndex3, "") { |
| 418 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 419 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) && |
| 420 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 421 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 422 | reader().get_uint32() == 3); |
| 423 | } |
| 424 | |
| 425 | TEST_F(WiFiServiceTest, ConnectTaskWEP) { |
| 426 | vector<uint8_t> ssid(5); |
| 427 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 428 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 429 | metrics(), |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 430 | manager(), |
| 431 | wifi(), |
| 432 | ssid, |
| 433 | flimflam::kModeManaged, |
| 434 | flimflam::kSecurityWep, |
| 435 | false); |
| 436 | Error error; |
| 437 | wifi_service->SetPassphrase("0:abcdefghijklm", &error); |
| 438 | EXPECT_CALL(*wifi(), |
| 439 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 440 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 441 | |
| 442 | wifi_service->SetPassphrase("abcdefghijklm", &error); |
| 443 | EXPECT_CALL(*wifi(), |
| 444 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 445 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 446 | |
| 447 | wifi_service->SetPassphrase("1:abcdefghijklm", &error); |
| 448 | EXPECT_CALL(*wifi(), |
| 449 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 450 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 451 | |
| 452 | wifi_service->SetPassphrase("2:abcdefghijklm", &error); |
| 453 | EXPECT_CALL(*wifi(), |
| 454 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 455 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 456 | |
| 457 | wifi_service->SetPassphrase("3:abcdefghijklm", &error); |
| 458 | EXPECT_CALL(*wifi(), |
| 459 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 460 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 463 | |
| 464 | MATCHER(DynamicWEPArgs, "") { |
| 465 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 466 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) && |
| 467 | !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol); |
| 468 | } |
| 469 | |
| 470 | // Dynamic WEP + 802.1x. |
| 471 | TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) { |
| 472 | vector<uint8_t> ssid(5); |
| 473 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 474 | dispatcher(), |
| 475 | metrics(), |
| 476 | manager(), |
| 477 | wifi(), |
| 478 | ssid, |
| 479 | flimflam::kModeManaged, |
| 480 | flimflam::kSecurityWep, |
| 481 | false); |
| 482 | |
| 483 | Service::EapCredentials eap; |
| 484 | eap.key_management = "IEEE8021X"; |
| 485 | eap.identity = "something"; |
| 486 | wifi_service->set_eap(eap); |
| 487 | EXPECT_CALL(*wifi(), |
| 488 | ConnectTo(wifi_service.get(), DynamicWEPArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 489 | wifi_service->Connect(NULL); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 492 | TEST_F(WiFiServiceTest, LoadHidden) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 493 | vector<uint8_t> ssid(5); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 494 | ssid.push_back(0xff); |
| 495 | |
| 496 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 497 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 498 | metrics(), |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 499 | manager(), |
| 500 | wifi(), |
| 501 | ssid, |
| 502 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 503 | flimflam::kSecurityNone, |
| 504 | false); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 505 | ASSERT_FALSE(service->hidden_ssid_); |
| 506 | NiceMock<MockStore> mock_store; |
| 507 | const string storage_id = service->GetStorageIdentifier(); |
| 508 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 509 | .WillRepeatedly(Return(true)); |
| 510 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 511 | .WillRepeatedly(Return(false)); |
| 512 | EXPECT_CALL(mock_store, |
| 513 | GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _)) |
| 514 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 515 | EXPECT_TRUE(service->Load(&mock_store)); |
| 516 | EXPECT_TRUE(service->hidden_ssid_); |
| 517 | } |
| 518 | |
| 519 | TEST_F(WiFiServiceSecurityTest, WPAMapping) { |
| 520 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn, |
| 521 | flimflam::kSecurityPsk)); |
| 522 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa, |
| 523 | flimflam::kSecurityPsk)); |
| 524 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk, |
| 525 | flimflam::kSecurityPsk)); |
| 526 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep, |
| 527 | flimflam::kSecurityWep)); |
| 528 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone, |
| 529 | flimflam::kSecurityNone)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 530 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x, |
| 531 | flimflam::kSecurity8021x)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | TEST_F(WiFiServiceSecurityTest, LoadMapping) { |
| 535 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 536 | flimflam::kSecurityPsk, |
| 537 | true)); |
| 538 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 539 | flimflam::kSecurityRsn, |
| 540 | true)); |
| 541 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 542 | flimflam::kSecurityWpa, |
| 543 | false)); |
| 544 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 545 | flimflam::kSecurityPsk, |
| 546 | true)); |
| 547 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 548 | flimflam::kSecurityWpa, |
| 549 | true)); |
| 550 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 551 | flimflam::kSecurityRsn, |
| 552 | false)); |
| 553 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 554 | flimflam::kSecurityWep, |
| 555 | true)); |
| 556 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 557 | flimflam::kSecurityPsk, |
| 558 | false)); |
| 559 | } |
| 560 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 561 | TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) { |
| 562 | vector<uint8_t> ssid(5); |
| 563 | ssid.push_back(0xff); |
| 564 | |
| 565 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 566 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 567 | metrics(), |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 568 | manager(), |
| 569 | wifi(), |
| 570 | ssid, |
| 571 | flimflam::kModeManaged, |
| 572 | flimflam::kSecurityPsk, |
| 573 | false); |
| 574 | NiceMock<MockStore> mock_store; |
| 575 | const string storage_id = service->GetStorageIdentifier(); |
| 576 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 577 | .WillRepeatedly(Return(true)); |
| 578 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 579 | .WillRepeatedly(Return(false)); |
| 580 | const string passphrase = "passphrase"; |
| 581 | EXPECT_CALL(mock_store, |
| 582 | GetCryptedString(StrEq(storage_id), |
| 583 | WiFiService::kStoragePassphrase, _)) |
| 584 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true))); |
| 585 | EXPECT_CALL(mock_store, |
| 586 | GetCryptedString(StrEq(storage_id), |
| 587 | StrNe(WiFiService::kStoragePassphrase), _)) |
| 588 | .WillRepeatedly(Return(false)); |
| 589 | EXPECT_TRUE(service->need_passphrase_); |
| 590 | EXPECT_TRUE(service->Load(&mock_store)); |
| 591 | EXPECT_EQ(passphrase, service->passphrase_); |
| 592 | EXPECT_TRUE(service->connectable()); |
| 593 | EXPECT_FALSE(service->need_passphrase_); |
| 594 | service->Unload(); |
| 595 | EXPECT_EQ(string(""), service->passphrase_); |
| 596 | EXPECT_FALSE(service->connectable()); |
| 597 | EXPECT_TRUE(service->need_passphrase_); |
| 598 | } |
| 599 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 600 | TEST_F(WiFiServiceTest, UnloadAndClearCacheWep) { |
| 601 | vector<uint8_t> ssid(1, 'a'); |
| 602 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 603 | dispatcher(), |
| 604 | metrics(), |
| 605 | manager(), |
| 606 | wifi(), |
| 607 | ssid, |
| 608 | flimflam::kModeManaged, |
| 609 | flimflam::kSecurityWep, |
| 610 | false); |
| 611 | // A WEP network does not incur cached credentials. |
| 612 | EXPECT_CALL(*wifi(), ClearCachedCredentials()).Times(0); |
| 613 | service->Unload(); |
| 614 | } |
| 615 | |
| 616 | TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) { |
| 617 | vector<uint8_t> ssid(1, 'a'); |
| 618 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 619 | dispatcher(), |
| 620 | metrics(), |
| 621 | manager(), |
| 622 | wifi(), |
| 623 | ssid, |
| 624 | flimflam::kModeManaged, |
| 625 | flimflam::kSecurity8021x, |
| 626 | false); |
| 627 | // An 802.1x network should clear its cached credentials. |
| 628 | EXPECT_CALL(*wifi(), ClearCachedCredentials()).Times(1); |
| 629 | service->Unload(); |
| 630 | } |
| 631 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 632 | TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 633 | vector<uint8_t> ssid(5); |
| 634 | ssid.push_back(0xff); |
| 635 | |
| 636 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 637 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 638 | metrics(), |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 639 | manager(), |
| 640 | wifi(), |
| 641 | ssid, |
| 642 | flimflam::kModeManaged, |
| 643 | flimflam::kSecurityNone, |
| 644 | false); |
| 645 | const string storage_id = service->GetStorageIdentifier(); |
| 646 | string address; |
| 647 | string mode; |
| 648 | string security; |
| 649 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 650 | &security)); |
| 651 | EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address); |
| 652 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 653 | EXPECT_EQ(flimflam::kSecurityNone, security); |
| 654 | } |
| 655 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 656 | TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) { |
| 657 | // Do a separate test for 802.1x, since kSecurity8021x contains a "_", |
| 658 | // which needs to be dealt with specially in the parser. |
| 659 | vector<uint8_t> ssid(5); |
| 660 | ssid.push_back(0xff); |
| 661 | |
| 662 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 663 | dispatcher(), |
| 664 | metrics(), |
| 665 | manager(), |
| 666 | wifi(), |
| 667 | ssid, |
| 668 | flimflam::kModeManaged, |
| 669 | flimflam::kSecurity8021x, |
| 670 | false); |
| 671 | const string storage_id = service->GetStorageIdentifier(); |
| 672 | string address; |
| 673 | string mode; |
| 674 | string security; |
| 675 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 676 | &security)); |
| 677 | EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address); |
| 678 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 679 | EXPECT_EQ(flimflam::kSecurity8021x, security); |
| 680 | } |
| 681 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 682 | TEST_F(WiFiServiceTest, Connectable) { |
| 683 | // Open network should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 684 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 685 | |
| 686 | // Open network should remain connectable if we try to set a password on it. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 687 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 688 | |
| 689 | // WEP network with passphrase set should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 690 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 691 | |
| 692 | // WEP network without passphrase set should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 693 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 694 | |
| 695 | // A bad passphrase should not make a WEP network connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 696 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 697 | |
| 698 | // Similar to WEP, for WPA. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 699 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL)); |
| 700 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL)); |
| 701 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 702 | |
| 703 | // Unconfigured 802.1x should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 704 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL)); |
| 705 | |
| 706 | Service::EapCredentials eap; |
| 707 | // Empty EAP credentials should not make a 802.1x network connectable. |
| 708 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 709 | |
| 710 | eap.identity = "something"; |
| 711 | // If client certificate is being used, a private key must exist. |
| 712 | eap.client_cert = "some client cert"; |
| 713 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 714 | eap.private_key = "some private key"; |
| 715 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 716 | |
| 717 | // Identity is always required. |
| 718 | eap.identity.clear(); |
| 719 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 720 | |
| 721 | eap.identity = "something"; |
| 722 | // For non EAP-TLS types, a password is required. |
| 723 | eap.eap = "Non-TLS"; |
| 724 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 725 | eap.password = "some password"; |
| 726 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 727 | // Dynamic WEP + 802.1X should be connectable under the same conditions. |
| 728 | eap.key_management = "IEEE8021X"; |
| 729 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 730 | } |
| 731 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 732 | TEST_F(WiFiServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 733 | const char *reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 734 | vector<uint8_t> ssid(1, 'a'); |
| 735 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 736 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 737 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 738 | manager(), |
| 739 | wifi(), |
| 740 | ssid, |
| 741 | flimflam::kModeManaged, |
| 742 | flimflam::kSecurityNone, |
| 743 | false); |
| 744 | EXPECT_CALL(*wifi(), IsIdle()) |
| 745 | .WillRepeatedly(Return(true)); |
| 746 | EXPECT_FALSE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 747 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 748 | EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 749 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 750 | reason = ""; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 751 | WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 752 | service->AddEndpoint(endpoint); |
| 753 | EXPECT_CALL(*wifi(), IsIdle()) |
| 754 | .WillRepeatedly(Return(true)); |
| 755 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 756 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
| 757 | EXPECT_STREQ("", reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 758 | |
| 759 | // WiFi only supports connecting to one Service at a time. So, to |
| 760 | // avoid disrupting connectivity, we only allow auto-connection to |
| 761 | // a WiFiService when the corresponding WiFi is idle. |
| 762 | EXPECT_CALL(*wifi(), IsIdle()) |
| 763 | .WillRepeatedly(Return(false)); |
| 764 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 765 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 766 | EXPECT_STREQ(WiFiService::kAutoConnBusy, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | TEST_F(WiFiServiceTest, AutoConnect) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 770 | const char *reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 771 | vector<uint8_t> ssid(1, 'a'); |
| 772 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 773 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 774 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 775 | manager(), |
| 776 | wifi(), |
| 777 | ssid, |
| 778 | flimflam::kModeManaged, |
| 779 | flimflam::kSecurityNone, |
| 780 | false); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 781 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 782 | EXPECT_CALL(*wifi(), ConnectTo(_, _)) |
| 783 | .Times(0); |
| 784 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 785 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 786 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 787 | WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 788 | service->AddEndpoint(endpoint); |
| 789 | EXPECT_CALL(*wifi(), IsIdle()) |
| 790 | .WillRepeatedly(Return(true)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 791 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 792 | EXPECT_CALL(*wifi(), ConnectTo(_, _)); |
| 793 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 794 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 795 | |
| 796 | Error error; |
| 797 | service->Disconnect(&error); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 798 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 799 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 802 | TEST_F(WiFiServiceTest, Populate8021x) { |
| 803 | vector<uint8_t> ssid(1, 'a'); |
| 804 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 805 | dispatcher(), |
| 806 | metrics(), |
| 807 | manager(), |
| 808 | wifi(), |
| 809 | ssid, |
| 810 | flimflam::kModeManaged, |
| 811 | flimflam::kSecurityNone, |
| 812 | false); |
| 813 | Service::EapCredentials eap; |
| 814 | eap.identity = "testidentity"; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 815 | eap.pin = "xxxx"; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 816 | service->set_eap(eap); |
| 817 | map<string, ::DBus::Variant> params; |
| 818 | service->Populate8021xProperties(¶ms); |
| 819 | // Test that only non-empty 802.1x properties are populated. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 820 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity)); |
| 821 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 822 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 823 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 824 | // Test that CA path is set by default. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 825 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
| 826 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 827 | // Test that hardware-backed security arguments are not set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 828 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 829 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 830 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) { |
| 834 | vector<uint8_t> ssid(1, 'a'); |
| 835 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 836 | dispatcher(), |
| 837 | metrics(), |
| 838 | manager(), |
| 839 | wifi(), |
| 840 | ssid, |
| 841 | flimflam::kModeManaged, |
| 842 | flimflam::kSecurityNone, |
| 843 | false); |
| 844 | Service::EapCredentials eap; |
| 845 | eap.identity = "testidentity"; |
| 846 | eap.use_system_cas = false; |
| 847 | service->set_eap(eap); |
| 848 | map<string, ::DBus::Variant> params; |
| 849 | service->Populate8021xProperties(¶ms); |
| 850 | // Test that CA path is not set if use_system_cas is explicitly false. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 851 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) { |
| 855 | vector<uint8_t> ssid(1, 'a'); |
| 856 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 857 | dispatcher(), |
| 858 | metrics(), |
| 859 | manager(), |
| 860 | wifi(), |
| 861 | ssid, |
| 862 | flimflam::kModeManaged, |
| 863 | flimflam::kSecurityNone, |
| 864 | false); |
| 865 | Service::EapCredentials eap; |
| 866 | eap.identity = "testidentity"; |
| 867 | eap.key_id = "key_id"; |
| 868 | eap.pin = "xxxx"; |
| 869 | service->set_eap(eap); |
| 870 | map<string, ::DBus::Variant> params; |
| 871 | service->Populate8021xProperties(¶ms); |
| 872 | // Test that EAP engine parameters set if key_id is set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 873 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 874 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 875 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 876 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
| 877 | } |
| 878 | |
| 879 | TEST_F(WiFiServiceTest, Populate8021xNSS) { |
| 880 | vector<uint8_t> ssid(1, 'a'); |
| 881 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 882 | dispatcher(), |
| 883 | metrics(), |
| 884 | manager(), |
| 885 | wifi(), |
| 886 | ssid, |
| 887 | flimflam::kModeManaged, |
| 888 | flimflam::kSecurityNone, |
| 889 | false); |
| 890 | Service::EapCredentials eap; |
| 891 | eap.ca_cert_nss = "nss_nickname"; |
| 892 | service->set_eap(eap); |
| 893 | MockNSS nss; |
| 894 | service->nss_ = &nss; |
| 895 | |
| 896 | const string kNSSCertfile("/tmp/nss-cert"); |
| 897 | FilePath nss_cert(kNSSCertfile); |
| 898 | vector<char> ssid_in_chars(ssid.begin(), ssid.end()); |
| 899 | EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars)) |
| 900 | .WillOnce(Return(nss_cert)); |
| 901 | |
| 902 | map<string, ::DBus::Variant> params; |
| 903 | service->Populate8021xProperties(¶ms); |
| 904 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 905 | if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) { |
| 906 | EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert] |
| 907 | .reader().get_string()); |
| 908 | } |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 909 | } |
| 910 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 911 | TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) { |
| 912 | vector<uint8_t> ssid(1, 'a'); |
| 913 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 914 | dispatcher(), |
| 915 | metrics(), |
| 916 | manager(), |
| 917 | wifi(), |
| 918 | ssid, |
| 919 | flimflam::kModeManaged, |
| 920 | flimflam::kSecurityWep, |
| 921 | false); |
| 922 | |
| 923 | EXPECT_EQ("", wifi_service->passphrase_); |
| 924 | |
| 925 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 926 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 927 | wifi_service->mutable_store(), |
| 928 | flimflam::kPassphraseProperty, |
| 929 | DBusAdaptor::StringToVariant("0:abcde"), |
| 930 | &error)); |
| 931 | EXPECT_EQ("0:abcde", wifi_service->passphrase_); |
| 932 | |
| 933 | EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(), |
| 934 | flimflam::kPassphraseProperty, |
| 935 | &error)); |
| 936 | EXPECT_EQ("", wifi_service->passphrase_); |
| 937 | } |
| 938 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 939 | TEST_F(WiFiServiceTest, SignalToStrength) { |
| 940 | // Verify that our mapping is sane, in the sense that it preserves ordering. |
| 941 | // We break the test into two domains, because we assume that positive |
| 942 | // values aren't actually in dBm. |
| 943 | for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) { |
| 944 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 945 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 946 | EXPECT_LE(current_mapped, next_mapped) |
| 947 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 948 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 949 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 950 | } |
| 951 | for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) { |
| 952 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 953 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 954 | EXPECT_LE(current_mapped, next_mapped) |
| 955 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 956 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 957 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
| 961 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) { |
| 962 | // If the chosen signal values don't map to distinct strength |
| 963 | // values, then we can't expect our other tests to pass. So verify |
| 964 | // their distinctness. |
| 965 | EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength); |
| 966 | EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength); |
| 967 | EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength); |
| 968 | } |
| 969 | |
| 970 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) { |
| 971 | // Initial endpoint updates values. |
| 972 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 973 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
| 974 | EXPECT_CALL(adaptor,EmitUint8Changed( |
| 975 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 976 | service->AddEndpoint(ok_endpoint); |
| 977 | Mock::VerifyAndClearExpectations(&adaptor); |
| 978 | |
| 979 | // Endpoint with stronger signal updates values. |
| 980 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 981 | flimflam::kWifiFrequency, kGoodEndpointFrequency)); |
| 982 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 983 | flimflam::kSignalStrengthProperty, kGoodEndpointStrength)); |
| 984 | service->AddEndpoint(good_endpoint); |
| 985 | Mock::VerifyAndClearExpectations(&adaptor); |
| 986 | |
| 987 | // Endpoint with lower signal does not change values. |
| 988 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 989 | EXPECT_CALL(adaptor, |
| 990 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 991 | service->AddEndpoint(bad_endpoint); |
| 992 | Mock::VerifyAndClearExpectations(&adaptor); |
| 993 | |
| 994 | // Removing non-optimal endpoint does not change values. |
| 995 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 996 | EXPECT_CALL(adaptor, |
| 997 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 998 | service->RemoveEndpoint(bad_endpoint); |
| 999 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1000 | |
| 1001 | // Removing optimal endpoint updates values. |
| 1002 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1003 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
| 1004 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1005 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1006 | service->RemoveEndpoint(good_endpoint); |
| 1007 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1008 | |
| 1009 | // Removing last endpoint updates values (and doesn't crash). |
| 1010 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)); |
| 1011 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1012 | service->RemoveEndpoint(ok_endpoint); |
| 1013 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1014 | } |
| 1015 | |
| 1016 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) { |
| 1017 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1018 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1019 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1020 | service->AddEndpoint(bad_endpoint); |
| 1021 | service->AddEndpoint(ok_endpoint); |
| 1022 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1023 | |
| 1024 | // Setting current endpoint forces adoption of its values, even if it |
| 1025 | // doesn't have the highest signal. |
| 1026 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1027 | flimflam::kWifiFrequency, kBadEndpointFrequency)); |
| 1028 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1029 | flimflam::kSignalStrengthProperty, kBadEndpointStrength)); |
| 1030 | service->NotifyCurrentEndpoint(bad_endpoint); |
| 1031 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1032 | |
| 1033 | // Adding a better endpoint doesn't matter, when current endpoint is set. |
| 1034 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 1035 | EXPECT_CALL(adaptor, |
| 1036 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1037 | service->AddEndpoint(good_endpoint); |
| 1038 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1039 | |
| 1040 | // Removing a better endpoint doesn't matter, when current endpoint is set. |
| 1041 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 1042 | EXPECT_CALL(adaptor, |
| 1043 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1044 | service->RemoveEndpoint(good_endpoint); |
| 1045 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1046 | |
| 1047 | // Removing the current endpoint is safe and sane. |
| 1048 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1049 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
| 1050 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1051 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1052 | service->RemoveEndpoint(bad_endpoint); |
| 1053 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1054 | |
| 1055 | // Clearing the current endpoint (without removing it) is also safe and sane. |
| 1056 | service->NotifyCurrentEndpoint(ok_endpoint); |
| 1057 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 1058 | EXPECT_CALL(adaptor, |
| 1059 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1060 | service->NotifyCurrentEndpoint(NULL); |
| 1061 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1062 | } |
| 1063 | |
| 1064 | TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) { |
| 1065 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1066 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1067 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1068 | service->AddEndpoint(ok_endpoint); |
| 1069 | service->AddEndpoint(good_endpoint); |
| 1070 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1071 | |
| 1072 | // Updating sub-optimal Endpoint doesn't update Service. |
| 1073 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 1074 | EXPECT_CALL(adaptor, |
| 1075 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1076 | ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2; |
| 1077 | service->NotifyEndpointUpdated(*ok_endpoint); |
| 1078 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1079 | |
| 1080 | // Updating optimal Endpoint updates appropriate Service property. |
| 1081 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
| 1082 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1083 | good_endpoint->signal_strength_ = kGoodEndpointSignal + 1; |
| 1084 | service->NotifyEndpointUpdated(*good_endpoint); |
| 1085 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1086 | |
| 1087 | // Change in optimal Endpoint updates Service properties. |
| 1088 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1089 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
| 1090 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1091 | ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2; |
| 1092 | service->NotifyEndpointUpdated(*ok_endpoint); |
| 1093 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1094 | } |
| 1095 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1096 | } // namespace shill |