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