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 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame^] | 330 | MATCHER(WPA80211wSecurityArgs, "") { |
| 331 | return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) && |
| 332 | ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) && |
| 333 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w); |
| 334 | } |
| 335 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 336 | MATCHER(EAPSecurityArgs, "") { |
| 337 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 338 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath); |
| 339 | } |
| 340 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 341 | MATCHER_P(FrequencyArg, has_arg, "") { |
| 342 | return has_arg == |
| 343 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency); |
| 344 | } |
| 345 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 346 | TEST_F(WiFiServiceTest, ConnectTaskWPA) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 347 | vector<uint8_t> ssid(5); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 348 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 349 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 350 | metrics(), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 351 | manager(), |
| 352 | wifi(), |
| 353 | ssid, |
| 354 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 355 | flimflam::kSecurityWpa, |
| 356 | false); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 357 | EXPECT_CALL(*wifi(), |
| 358 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 359 | Error error; |
| 360 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 361 | wifi_service->Connect(NULL); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 362 | } |
| 363 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 364 | TEST_F(WiFiServiceTest, ConnectTaskRSN) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 365 | vector<uint8_t> ssid(5); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 366 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 367 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 368 | metrics(), |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 369 | manager(), |
| 370 | wifi(), |
| 371 | ssid, |
| 372 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 373 | flimflam::kSecurityRsn, |
| 374 | false); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 375 | EXPECT_CALL(*wifi(), |
| 376 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 377 | Error error; |
| 378 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 379 | wifi_service->Connect(NULL); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 382 | TEST_F(WiFiServiceTest, ConnectConditions) { |
| 383 | Error error; |
| 384 | vector<uint8_t> ssid(5); |
| 385 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 386 | dispatcher(), |
| 387 | metrics(), |
| 388 | manager(), |
| 389 | wifi(), |
| 390 | ssid, |
| 391 | flimflam::kModeManaged, |
| 392 | flimflam::kSecurityNone, |
| 393 | false); |
| 394 | scoped_refptr<MockProfile> mock_profile( |
| 395 | new NiceMock<MockProfile>(control_interface(), manager())); |
| 396 | wifi_service->set_profile(mock_profile); |
| 397 | // With nothing else going on, the service should attempt to connect. |
| 398 | EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _)); |
| 399 | wifi_service->Connect(&error); |
| 400 | Mock::VerifyAndClearExpectations(wifi()); |
| 401 | |
| 402 | // But if we're already "connecting" or "connected" then we shouldn't attempt |
| 403 | // again. |
| 404 | EXPECT_CALL(*wifi(), |
| 405 | ConnectTo(wifi_service.get(), _)).Times(0); |
| 406 | wifi_service->SetState(Service::kStateAssociating); |
| 407 | wifi_service->Connect(&error); |
| 408 | wifi_service->SetState(Service::kStateConfiguring); |
| 409 | wifi_service->Connect(&error); |
| 410 | wifi_service->SetState(Service::kStateConnected); |
| 411 | wifi_service->Connect(&error); |
| 412 | wifi_service->SetState(Service::kStatePortal); |
| 413 | wifi_service->Connect(&error); |
| 414 | wifi_service->SetState(Service::kStateOnline); |
| 415 | wifi_service->Connect(&error); |
| 416 | Mock::VerifyAndClearExpectations(wifi()); |
| 417 | } |
| 418 | |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 419 | TEST_F(WiFiServiceTest, ConnectTaskPSK) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 420 | vector<uint8_t> ssid(5); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 421 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 422 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 423 | metrics(), |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 424 | manager(), |
| 425 | wifi(), |
| 426 | ssid, |
| 427 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 428 | flimflam::kSecurityPsk, |
| 429 | false); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 430 | EXPECT_CALL(*wifi(), |
| 431 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 432 | Error error; |
| 433 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 434 | wifi_service->Connect(NULL); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 437 | TEST_F(WiFiServiceTest, ConnectTask8021x) { |
| 438 | vector<uint8_t> ssid(5); |
| 439 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 440 | dispatcher(), |
| 441 | metrics(), |
| 442 | manager(), |
| 443 | wifi(), |
| 444 | ssid, |
| 445 | flimflam::kModeManaged, |
| 446 | flimflam::kSecurity8021x, |
| 447 | false); |
| 448 | Service::EapCredentials eap; |
| 449 | eap.identity = "identity"; |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 450 | eap.password = "mumble"; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 451 | wifi_service->set_eap(eap); |
| 452 | EXPECT_CALL(*wifi(), |
| 453 | ConnectTo(wifi_service.get(), EAPSecurityArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 454 | wifi_service->Connect(NULL); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 457 | TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) { |
| 458 | vector<uint8_t> ssid(1, 'a'); |
| 459 | WiFiEndpointRefPtr endpoint_nofreq = |
| 460 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
| 461 | WiFiEndpointRefPtr endpoint_freq = |
| 462 | MakeEndpoint("a", "00:00:00:00:00:02", 2412, 0); |
| 463 | |
| 464 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 465 | dispatcher(), |
| 466 | metrics(), |
| 467 | manager(), |
| 468 | wifi(), |
| 469 | ssid, |
| 470 | flimflam::kModeManaged, |
| 471 | flimflam::kSecurityNone, |
| 472 | false); |
| 473 | wifi_service->AddEndpoint(endpoint_freq); |
| 474 | EXPECT_CALL(*wifi(), |
| 475 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
| 476 | wifi_service->Connect(NULL); |
| 477 | |
| 478 | wifi_service = new WiFiService(control_interface(), |
| 479 | dispatcher(), |
| 480 | metrics(), |
| 481 | manager(), |
| 482 | wifi(), |
| 483 | ssid, |
| 484 | flimflam::kModeAdhoc, |
| 485 | flimflam::kSecurityNone, |
| 486 | false); |
| 487 | EXPECT_CALL(*wifi(), |
| 488 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
| 489 | wifi_service->Connect(NULL); |
| 490 | |
| 491 | wifi_service = new WiFiService(control_interface(), |
| 492 | dispatcher(), |
| 493 | metrics(), |
| 494 | manager(), |
| 495 | wifi(), |
| 496 | ssid, |
| 497 | flimflam::kModeAdhoc, |
| 498 | flimflam::kSecurityNone, |
| 499 | false); |
| 500 | wifi_service->AddEndpoint(endpoint_nofreq); |
| 501 | EXPECT_CALL(*wifi(), |
| 502 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
| 503 | wifi_service->Connect(NULL); |
| 504 | |
| 505 | wifi_service = new WiFiService(control_interface(), |
| 506 | dispatcher(), |
| 507 | metrics(), |
| 508 | manager(), |
| 509 | wifi(), |
| 510 | ssid, |
| 511 | flimflam::kModeAdhoc, |
| 512 | flimflam::kSecurityNone, |
| 513 | false); |
| 514 | wifi_service->AddEndpoint(endpoint_freq); |
| 515 | EXPECT_CALL(*wifi(), |
| 516 | ConnectTo(wifi_service.get(), FrequencyArg(true))); |
| 517 | wifi_service->Connect(NULL); |
| 518 | } |
| 519 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame^] | 520 | TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) { |
| 521 | vector<uint8_t> ssid(1, 'a'); |
| 522 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 523 | dispatcher(), |
| 524 | metrics(), |
| 525 | manager(), |
| 526 | wifi(), |
| 527 | ssid, |
| 528 | flimflam::kModeManaged, |
| 529 | flimflam::kSecurityPsk, |
| 530 | false); |
| 531 | WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
| 532 | endpoint->ieee80211w_required_ = true; |
| 533 | wifi_service->AddEndpoint(endpoint); |
| 534 | Error error; |
| 535 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
| 536 | EXPECT_CALL(*wifi(), |
| 537 | ConnectTo(wifi_service.get(), WPA80211wSecurityArgs())); |
| 538 | wifi_service->Connect(NULL); |
| 539 | } |
| 540 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 541 | MATCHER(WEPSecurityArgsKeyIndex0, "") { |
| 542 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 543 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) && |
| 544 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 545 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 546 | reader().get_uint32() == 0); |
| 547 | } |
| 548 | |
| 549 | MATCHER(WEPSecurityArgsKeyIndex1, "") { |
| 550 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 551 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) && |
| 552 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 553 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 554 | reader().get_uint32() == 1); |
| 555 | } |
| 556 | |
| 557 | MATCHER(WEPSecurityArgsKeyIndex2, "") { |
| 558 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 559 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) && |
| 560 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 561 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 562 | reader().get_uint32() == 2); |
| 563 | } |
| 564 | |
| 565 | MATCHER(WEPSecurityArgsKeyIndex3, "") { |
| 566 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 567 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) && |
| 568 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 569 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 570 | reader().get_uint32() == 3); |
| 571 | } |
| 572 | |
| 573 | TEST_F(WiFiServiceTest, ConnectTaskWEP) { |
| 574 | vector<uint8_t> ssid(5); |
| 575 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 576 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 577 | metrics(), |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 578 | manager(), |
| 579 | wifi(), |
| 580 | ssid, |
| 581 | flimflam::kModeManaged, |
| 582 | flimflam::kSecurityWep, |
| 583 | false); |
| 584 | Error error; |
| 585 | wifi_service->SetPassphrase("0:abcdefghijklm", &error); |
| 586 | EXPECT_CALL(*wifi(), |
| 587 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 588 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 589 | |
| 590 | wifi_service->SetPassphrase("abcdefghijklm", &error); |
| 591 | EXPECT_CALL(*wifi(), |
| 592 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 593 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 594 | |
| 595 | wifi_service->SetPassphrase("1:abcdefghijklm", &error); |
| 596 | EXPECT_CALL(*wifi(), |
| 597 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 598 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 599 | |
| 600 | wifi_service->SetPassphrase("2:abcdefghijklm", &error); |
| 601 | EXPECT_CALL(*wifi(), |
| 602 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 603 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 604 | |
| 605 | wifi_service->SetPassphrase("3:abcdefghijklm", &error); |
| 606 | EXPECT_CALL(*wifi(), |
| 607 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 608 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 611 | |
| 612 | MATCHER(DynamicWEPArgs, "") { |
| 613 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 614 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) && |
| 615 | !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol); |
| 616 | } |
| 617 | |
| 618 | // Dynamic WEP + 802.1x. |
| 619 | TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) { |
| 620 | vector<uint8_t> ssid(5); |
| 621 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 622 | dispatcher(), |
| 623 | metrics(), |
| 624 | manager(), |
| 625 | wifi(), |
| 626 | ssid, |
| 627 | flimflam::kModeManaged, |
| 628 | flimflam::kSecurityWep, |
| 629 | false); |
| 630 | |
| 631 | Service::EapCredentials eap; |
| 632 | eap.key_management = "IEEE8021X"; |
| 633 | eap.identity = "something"; |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 634 | eap.password = "mumble"; |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 635 | wifi_service->set_eap(eap); |
| 636 | EXPECT_CALL(*wifi(), |
| 637 | ConnectTo(wifi_service.get(), DynamicWEPArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 638 | wifi_service->Connect(NULL); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 641 | TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) { |
| 642 | vector<uint8_t> ssid(5); |
| 643 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 644 | dispatcher(), |
| 645 | metrics(), |
| 646 | manager(), |
| 647 | wifi(), |
| 648 | ssid, |
| 649 | flimflam::kModeManaged, |
| 650 | flimflam::kSecurityRsn, |
| 651 | false); |
| 652 | |
| 653 | const string kPassphrase = "abcdefgh"; |
| 654 | |
| 655 | { |
| 656 | Error error; |
| 657 | // A changed passphrase should trigger cache removal. |
| 658 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 659 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 660 | Mock::VerifyAndClearExpectations(wifi()); |
| 661 | EXPECT_TRUE(error.IsSuccess()); |
| 662 | } |
| 663 | |
| 664 | { |
| 665 | Error error; |
| 666 | // An unchanged passphrase should not trigger cache removal. |
| 667 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 668 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 669 | Mock::VerifyAndClearExpectations(wifi()); |
| 670 | EXPECT_TRUE(error.IsSuccess()); |
| 671 | } |
| 672 | |
| 673 | { |
| 674 | Error error; |
| 675 | // A modified passphrase should trigger cache removal. |
| 676 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 677 | wifi_service->SetPassphrase(kPassphrase + "X", &error); |
| 678 | Mock::VerifyAndClearExpectations(wifi()); |
| 679 | EXPECT_TRUE(error.IsSuccess()); |
| 680 | } |
| 681 | |
| 682 | { |
| 683 | Error error; |
| 684 | // A cleared passphrase should also trigger cache removal. |
| 685 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 686 | wifi_service->ClearPassphrase(&error); |
| 687 | Mock::VerifyAndClearExpectations(wifi()); |
| 688 | EXPECT_TRUE(error.IsSuccess()); |
| 689 | } |
| 690 | |
| 691 | { |
| 692 | Error error; |
| 693 | // An invalid passphrase should not trigger cache removal. |
| 694 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 695 | wifi_service->SetPassphrase("", &error); |
| 696 | Mock::VerifyAndClearExpectations(wifi()); |
| 697 | EXPECT_FALSE(error.IsSuccess()); |
| 698 | } |
| 699 | |
| 700 | { |
| 701 | // Any change to EAP parameters (including a null one) will trigger cache |
| 702 | // removal. This is a lot less granular than the passphrase checks above. |
| 703 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 704 | wifi_service->set_eap(Service::EapCredentials()); |
| 705 | Mock::VerifyAndClearExpectations(wifi()); |
| 706 | } |
| 707 | } |
| 708 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 709 | TEST_F(WiFiServiceTest, LoadHidden) { |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 710 | vector<uint8_t> ssid(5); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 711 | ssid.push_back(0xff); |
| 712 | |
| 713 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 714 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 715 | metrics(), |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 716 | manager(), |
| 717 | wifi(), |
| 718 | ssid, |
| 719 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 720 | flimflam::kSecurityNone, |
| 721 | false); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 722 | ASSERT_FALSE(service->hidden_ssid_); |
| 723 | NiceMock<MockStore> mock_store; |
| 724 | const string storage_id = service->GetStorageIdentifier(); |
| 725 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 726 | .WillRepeatedly(Return(true)); |
| 727 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 728 | .WillRepeatedly(Return(false)); |
| 729 | EXPECT_CALL(mock_store, |
| 730 | GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _)) |
| 731 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 732 | EXPECT_TRUE(service->Load(&mock_store)); |
| 733 | EXPECT_TRUE(service->hidden_ssid_); |
| 734 | } |
| 735 | |
| 736 | TEST_F(WiFiServiceSecurityTest, WPAMapping) { |
| 737 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn, |
| 738 | flimflam::kSecurityPsk)); |
| 739 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa, |
| 740 | flimflam::kSecurityPsk)); |
| 741 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk, |
| 742 | flimflam::kSecurityPsk)); |
| 743 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep, |
| 744 | flimflam::kSecurityWep)); |
| 745 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone, |
| 746 | flimflam::kSecurityNone)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 747 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x, |
| 748 | flimflam::kSecurity8021x)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | TEST_F(WiFiServiceSecurityTest, LoadMapping) { |
| 752 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 753 | flimflam::kSecurityPsk, |
| 754 | true)); |
| 755 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 756 | flimflam::kSecurityRsn, |
| 757 | true)); |
| 758 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 759 | flimflam::kSecurityWpa, |
| 760 | false)); |
| 761 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 762 | flimflam::kSecurityPsk, |
| 763 | true)); |
| 764 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 765 | flimflam::kSecurityWpa, |
| 766 | true)); |
| 767 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 768 | flimflam::kSecurityRsn, |
| 769 | false)); |
| 770 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 771 | flimflam::kSecurityWep, |
| 772 | true)); |
| 773 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 774 | flimflam::kSecurityPsk, |
| 775 | false)); |
| 776 | } |
| 777 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 778 | TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) { |
| 779 | vector<uint8_t> ssid(5); |
| 780 | ssid.push_back(0xff); |
| 781 | |
| 782 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 783 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 784 | metrics(), |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 785 | manager(), |
| 786 | wifi(), |
| 787 | ssid, |
| 788 | flimflam::kModeManaged, |
| 789 | flimflam::kSecurityPsk, |
| 790 | false); |
| 791 | NiceMock<MockStore> mock_store; |
| 792 | const string storage_id = service->GetStorageIdentifier(); |
| 793 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 794 | .WillRepeatedly(Return(true)); |
| 795 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 796 | .WillRepeatedly(Return(false)); |
| 797 | const string passphrase = "passphrase"; |
| 798 | EXPECT_CALL(mock_store, |
| 799 | GetCryptedString(StrEq(storage_id), |
| 800 | WiFiService::kStoragePassphrase, _)) |
| 801 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true))); |
| 802 | EXPECT_CALL(mock_store, |
| 803 | GetCryptedString(StrEq(storage_id), |
| 804 | StrNe(WiFiService::kStoragePassphrase), _)) |
| 805 | .WillRepeatedly(Return(false)); |
| 806 | EXPECT_TRUE(service->need_passphrase_); |
| 807 | EXPECT_TRUE(service->Load(&mock_store)); |
| 808 | EXPECT_EQ(passphrase, service->passphrase_); |
| 809 | EXPECT_TRUE(service->connectable()); |
| 810 | EXPECT_FALSE(service->need_passphrase_); |
| 811 | service->Unload(); |
| 812 | EXPECT_EQ(string(""), service->passphrase_); |
| 813 | EXPECT_FALSE(service->connectable()); |
| 814 | EXPECT_TRUE(service->need_passphrase_); |
| 815 | } |
| 816 | |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 817 | TEST_F(WiFiServiceTest, ConfigureMakesConnectable) { |
| 818 | string guid("legit_guid"); |
| 819 | KeyValueStore args; |
| 820 | args.SetString(flimflam::kEapIdentityProperty, "legit_identity"); |
| 821 | args.SetString(flimflam::kEapPasswordProperty, "legit_password"); |
| 822 | args.SetString(flimflam::kEAPEAPProperty, "PEAP"); |
| 823 | args.SetString(flimflam::kGuidProperty, guid); |
| 824 | Error error; |
| 825 | vector<uint8_t> ssid(5); |
| 826 | ssid.push_back(0xff); |
| 827 | |
| 828 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 829 | dispatcher(), |
| 830 | metrics(), |
| 831 | manager(), |
| 832 | wifi(), |
| 833 | ssid, |
| 834 | flimflam::kModeManaged, |
| 835 | flimflam::kSecurity8021x, |
| 836 | false); |
| 837 | // Hack the GUID in so that we don't have to mess about with WiFi to regsiter |
| 838 | // our service. This way, Manager will handle the lookup itself. |
| 839 | service->set_guid(guid); |
| 840 | manager()->RegisterService(service); |
| 841 | EXPECT_FALSE(service->connectable()); |
| 842 | EXPECT_EQ(service.get(), manager()->GetService(args, &error).get()); |
| 843 | EXPECT_TRUE(error.IsSuccess()); |
| 844 | EXPECT_TRUE(service->connectable()); |
| 845 | } |
| 846 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 847 | TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) { |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 848 | vector<uint8_t> ssid(1, 'a'); |
| 849 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 850 | dispatcher(), |
| 851 | metrics(), |
| 852 | manager(), |
| 853 | wifi(), |
| 854 | ssid, |
| 855 | flimflam::kModeManaged, |
| 856 | flimflam::kSecurityWep, |
| 857 | false); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 858 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 859 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 860 | service->Unload(); |
| 861 | } |
| 862 | |
| 863 | TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) { |
| 864 | vector<uint8_t> ssid(1, 'a'); |
| 865 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 866 | dispatcher(), |
| 867 | metrics(), |
| 868 | manager(), |
| 869 | wifi(), |
| 870 | ssid, |
| 871 | flimflam::kModeManaged, |
| 872 | flimflam::kSecurity8021x, |
| 873 | false); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 874 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 875 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 876 | service->Unload(); |
| 877 | } |
| 878 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 879 | TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 880 | vector<uint8_t> ssid(5); |
| 881 | ssid.push_back(0xff); |
| 882 | |
| 883 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 884 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 885 | metrics(), |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 886 | manager(), |
| 887 | wifi(), |
| 888 | ssid, |
| 889 | flimflam::kModeManaged, |
| 890 | flimflam::kSecurityNone, |
| 891 | false); |
| 892 | const string storage_id = service->GetStorageIdentifier(); |
| 893 | string address; |
| 894 | string mode; |
| 895 | string security; |
| 896 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 897 | &security)); |
| 898 | EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address); |
| 899 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 900 | EXPECT_EQ(flimflam::kSecurityNone, security); |
| 901 | } |
| 902 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 903 | TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) { |
| 904 | // Do a separate test for 802.1x, since kSecurity8021x contains a "_", |
| 905 | // which needs to be dealt with specially in the parser. |
| 906 | vector<uint8_t> ssid(5); |
| 907 | ssid.push_back(0xff); |
| 908 | |
| 909 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 910 | dispatcher(), |
| 911 | metrics(), |
| 912 | manager(), |
| 913 | wifi(), |
| 914 | ssid, |
| 915 | flimflam::kModeManaged, |
| 916 | flimflam::kSecurity8021x, |
| 917 | false); |
| 918 | const string storage_id = service->GetStorageIdentifier(); |
| 919 | string address; |
| 920 | string mode; |
| 921 | string security; |
| 922 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 923 | &security)); |
| 924 | EXPECT_EQ(StringToLowerASCII(string(fake_mac)), address); |
| 925 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 926 | EXPECT_EQ(flimflam::kSecurity8021x, security); |
| 927 | } |
| 928 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 929 | TEST_F(WiFiServiceTest, Connectable) { |
| 930 | // Open network should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 931 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 932 | |
| 933 | // 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] | 934 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 935 | |
| 936 | // WEP network with passphrase set should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 937 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 938 | |
| 939 | // WEP network without passphrase set should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 940 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 941 | |
| 942 | // A bad passphrase should not make a WEP network connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 943 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 944 | |
| 945 | // Similar to WEP, for WPA. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 946 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL)); |
| 947 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL)); |
| 948 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 949 | |
| 950 | // Unconfigured 802.1x should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 951 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL)); |
| 952 | |
| 953 | Service::EapCredentials eap; |
| 954 | // Empty EAP credentials should not make a 802.1x network connectable. |
| 955 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 956 | |
| 957 | eap.identity = "something"; |
| 958 | // If client certificate is being used, a private key must exist. |
| 959 | eap.client_cert = "some client cert"; |
| 960 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 961 | eap.private_key = "some private key"; |
| 962 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 963 | |
| 964 | // Identity is always required. |
| 965 | eap.identity.clear(); |
| 966 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 967 | |
| 968 | eap.identity = "something"; |
| 969 | // For non EAP-TLS types, a password is required. |
| 970 | eap.eap = "Non-TLS"; |
| 971 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 972 | eap.password = "some password"; |
| 973 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 974 | // Dynamic WEP + 802.1X should be connectable under the same conditions. |
| 975 | eap.key_management = "IEEE8021X"; |
| 976 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 977 | } |
| 978 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 979 | TEST_F(WiFiServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 980 | const char *reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 981 | vector<uint8_t> ssid(1, 'a'); |
| 982 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 983 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 984 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 985 | manager(), |
| 986 | wifi(), |
| 987 | ssid, |
| 988 | flimflam::kModeManaged, |
| 989 | flimflam::kSecurityNone, |
| 990 | false); |
| 991 | EXPECT_CALL(*wifi(), IsIdle()) |
| 992 | .WillRepeatedly(Return(true)); |
| 993 | EXPECT_FALSE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 994 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 995 | EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 996 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 997 | reason = ""; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 998 | WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 999 | service->AddEndpoint(endpoint); |
| 1000 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1001 | .WillRepeatedly(Return(true)); |
| 1002 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1003 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
| 1004 | EXPECT_STREQ("", reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1005 | |
| 1006 | // WiFi only supports connecting to one Service at a time. So, to |
| 1007 | // avoid disrupting connectivity, we only allow auto-connection to |
| 1008 | // a WiFiService when the corresponding WiFi is idle. |
| 1009 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1010 | .WillRepeatedly(Return(false)); |
| 1011 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1012 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 1013 | EXPECT_STREQ(WiFiService::kAutoConnBusy, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | TEST_F(WiFiServiceTest, AutoConnect) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1017 | const char *reason; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1018 | vector<uint8_t> ssid(1, 'a'); |
| 1019 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 1020 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1021 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1022 | manager(), |
| 1023 | wifi(), |
| 1024 | ssid, |
| 1025 | flimflam::kModeManaged, |
| 1026 | flimflam::kSecurityNone, |
| 1027 | false); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1028 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1029 | EXPECT_CALL(*wifi(), ConnectTo(_, _)) |
| 1030 | .Times(0); |
| 1031 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1032 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1033 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1034 | WiFiEndpointRefPtr endpoint = MakeEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1035 | service->AddEndpoint(endpoint); |
| 1036 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1037 | .WillRepeatedly(Return(true)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1038 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1039 | EXPECT_CALL(*wifi(), ConnectTo(_, _)); |
| 1040 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1041 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 1042 | |
| 1043 | Error error; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 1044 | service->UserInitiatedDisconnect(&error); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1045 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1046 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1049 | TEST_F(WiFiServiceTest, Populate8021x) { |
| 1050 | vector<uint8_t> ssid(1, 'a'); |
| 1051 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 1052 | dispatcher(), |
| 1053 | metrics(), |
| 1054 | manager(), |
| 1055 | wifi(), |
| 1056 | ssid, |
| 1057 | flimflam::kModeManaged, |
| 1058 | flimflam::kSecurityNone, |
| 1059 | false); |
| 1060 | Service::EapCredentials eap; |
| 1061 | eap.identity = "testidentity"; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1062 | eap.pin = "xxxx"; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1063 | service->set_eap(eap); |
| 1064 | map<string, ::DBus::Variant> params; |
| 1065 | service->Populate8021xProperties(¶ms); |
| 1066 | // Test that only non-empty 802.1x properties are populated. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1067 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity)); |
| 1068 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 1069 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 1070 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1071 | // Test that CA path is set by default. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1072 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
| 1073 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1074 | // Test that hardware-backed security arguments are not set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1075 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 1076 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 1077 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) { |
| 1081 | vector<uint8_t> ssid(1, 'a'); |
| 1082 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 1083 | dispatcher(), |
| 1084 | metrics(), |
| 1085 | manager(), |
| 1086 | wifi(), |
| 1087 | ssid, |
| 1088 | flimflam::kModeManaged, |
| 1089 | flimflam::kSecurityNone, |
| 1090 | false); |
| 1091 | Service::EapCredentials eap; |
| 1092 | eap.identity = "testidentity"; |
| 1093 | eap.use_system_cas = false; |
| 1094 | service->set_eap(eap); |
| 1095 | map<string, ::DBus::Variant> params; |
| 1096 | service->Populate8021xProperties(¶ms); |
| 1097 | // 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] | 1098 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) { |
| 1102 | vector<uint8_t> ssid(1, 'a'); |
| 1103 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 1104 | dispatcher(), |
| 1105 | metrics(), |
| 1106 | manager(), |
| 1107 | wifi(), |
| 1108 | ssid, |
| 1109 | flimflam::kModeManaged, |
| 1110 | flimflam::kSecurityNone, |
| 1111 | false); |
| 1112 | Service::EapCredentials eap; |
| 1113 | eap.identity = "testidentity"; |
| 1114 | eap.key_id = "key_id"; |
| 1115 | eap.pin = "xxxx"; |
| 1116 | service->set_eap(eap); |
| 1117 | map<string, ::DBus::Variant> params; |
| 1118 | service->Populate8021xProperties(¶ms); |
| 1119 | // Test that EAP engine parameters set if key_id is set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1120 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 1121 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 1122 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 1123 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
| 1124 | } |
| 1125 | |
| 1126 | TEST_F(WiFiServiceTest, Populate8021xNSS) { |
| 1127 | vector<uint8_t> ssid(1, 'a'); |
| 1128 | WiFiServiceRefPtr service = new WiFiService(control_interface(), |
| 1129 | dispatcher(), |
| 1130 | metrics(), |
| 1131 | manager(), |
| 1132 | wifi(), |
| 1133 | ssid, |
| 1134 | flimflam::kModeManaged, |
| 1135 | flimflam::kSecurityNone, |
| 1136 | false); |
| 1137 | Service::EapCredentials eap; |
| 1138 | eap.ca_cert_nss = "nss_nickname"; |
| 1139 | service->set_eap(eap); |
| 1140 | MockNSS nss; |
| 1141 | service->nss_ = &nss; |
| 1142 | |
| 1143 | const string kNSSCertfile("/tmp/nss-cert"); |
| 1144 | FilePath nss_cert(kNSSCertfile); |
| 1145 | vector<char> ssid_in_chars(ssid.begin(), ssid.end()); |
| 1146 | EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars)) |
| 1147 | .WillOnce(Return(nss_cert)); |
| 1148 | |
| 1149 | map<string, ::DBus::Variant> params; |
| 1150 | service->Populate8021xProperties(¶ms); |
| 1151 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 1152 | if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) { |
| 1153 | EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert] |
| 1154 | .reader().get_string()); |
| 1155 | } |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1158 | TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) { |
| 1159 | vector<uint8_t> ssid(1, 'a'); |
| 1160 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 1161 | dispatcher(), |
| 1162 | metrics(), |
| 1163 | manager(), |
| 1164 | wifi(), |
| 1165 | ssid, |
| 1166 | flimflam::kModeManaged, |
| 1167 | flimflam::kSecurityWep, |
| 1168 | false); |
| 1169 | |
| 1170 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1171 | |
| 1172 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1173 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1174 | wifi_service->mutable_store(), |
| 1175 | flimflam::kPassphraseProperty, |
| 1176 | DBusAdaptor::StringToVariant("0:abcde"), |
| 1177 | &error)); |
| 1178 | EXPECT_EQ("0:abcde", wifi_service->passphrase_); |
| 1179 | |
| 1180 | EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(), |
| 1181 | flimflam::kPassphraseProperty, |
| 1182 | &error)); |
| 1183 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1184 | } |
| 1185 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1186 | TEST_F(WiFiServiceTest, SignalToStrength) { |
| 1187 | // Verify that our mapping is sane, in the sense that it preserves ordering. |
| 1188 | // We break the test into two domains, because we assume that positive |
| 1189 | // values aren't actually in dBm. |
| 1190 | for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) { |
| 1191 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1192 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1193 | EXPECT_LE(current_mapped, next_mapped) |
| 1194 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1195 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1196 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1197 | } |
| 1198 | for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) { |
| 1199 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1200 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1201 | EXPECT_LE(current_mapped, next_mapped) |
| 1202 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1203 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1204 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) { |
| 1209 | // If the chosen signal values don't map to distinct strength |
| 1210 | // values, then we can't expect our other tests to pass. So verify |
| 1211 | // their distinctness. |
| 1212 | EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength); |
| 1213 | EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength); |
| 1214 | EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength); |
| 1215 | } |
| 1216 | |
| 1217 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) { |
| 1218 | // Initial endpoint updates values. |
| 1219 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1220 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1221 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1222 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
| 1223 | EXPECT_CALL(adaptor, EmitUint8Changed( |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1224 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1225 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1226 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1227 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1228 | |
| 1229 | // Endpoint with stronger signal updates values. |
| 1230 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1231 | flimflam::kWifiFrequency, kGoodEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1232 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1233 | flimflam::kWifiBSsid, kGoodEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1234 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1235 | flimflam::kSignalStrengthProperty, kGoodEndpointStrength)); |
| 1236 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1237 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1238 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1239 | |
| 1240 | // Endpoint with lower signal does not change values. |
| 1241 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1242 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1243 | EXPECT_CALL(adaptor, |
| 1244 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1245 | service->AddEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1246 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1247 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1248 | |
| 1249 | // Removing non-optimal endpoint does not change values. |
| 1250 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1251 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1252 | EXPECT_CALL(adaptor, |
| 1253 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1254 | service->RemoveEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1255 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1256 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1257 | |
| 1258 | // Removing optimal endpoint updates values. |
| 1259 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1260 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1261 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1262 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1263 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1264 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1265 | service->RemoveEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1266 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1267 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1268 | |
| 1269 | // Removing last endpoint updates values (and doesn't crash). |
| 1270 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1271 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1272 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1273 | service->RemoveEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1274 | EXPECT_EQ(0, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1275 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1276 | } |
| 1277 | |
| 1278 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) { |
| 1279 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1280 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1281 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1282 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1283 | service->AddEndpoint(bad_endpoint); |
| 1284 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1285 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1286 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1287 | |
| 1288 | // Setting current endpoint forces adoption of its values, even if it |
| 1289 | // doesn't have the highest signal. |
| 1290 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1291 | flimflam::kWifiFrequency, kBadEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1292 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1293 | flimflam::kWifiBSsid, kBadEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1294 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1295 | flimflam::kSignalStrengthProperty, kBadEndpointStrength)); |
| 1296 | service->NotifyCurrentEndpoint(bad_endpoint); |
| 1297 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1298 | |
| 1299 | // Adding a better endpoint doesn't matter, when current endpoint is set. |
| 1300 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1301 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1302 | EXPECT_CALL(adaptor, |
| 1303 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1304 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1305 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1306 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1307 | |
| 1308 | // Removing a better endpoint doesn't matter, when current endpoint is set. |
| 1309 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1310 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1311 | EXPECT_CALL(adaptor, |
| 1312 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1313 | service->RemoveEndpoint(good_endpoint); |
| 1314 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1315 | |
| 1316 | // Removing the current endpoint is safe and sane. |
| 1317 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1318 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1319 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1320 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1321 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1322 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1323 | service->RemoveEndpoint(bad_endpoint); |
| 1324 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1325 | |
| 1326 | // Clearing the current endpoint (without removing it) is also safe and sane. |
| 1327 | service->NotifyCurrentEndpoint(ok_endpoint); |
| 1328 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1329 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1330 | EXPECT_CALL(adaptor, |
| 1331 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1332 | service->NotifyCurrentEndpoint(NULL); |
| 1333 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1334 | } |
| 1335 | |
| 1336 | TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) { |
| 1337 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1338 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1339 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1340 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1341 | service->AddEndpoint(ok_endpoint); |
| 1342 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1343 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1344 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1345 | |
| 1346 | // Updating sub-optimal Endpoint doesn't update Service. |
| 1347 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1348 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1349 | EXPECT_CALL(adaptor, |
| 1350 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1351 | ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2; |
| 1352 | service->NotifyEndpointUpdated(*ok_endpoint); |
| 1353 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1354 | |
| 1355 | // Updating optimal Endpoint updates appropriate Service property. |
| 1356 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1357 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1358 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1359 | good_endpoint->signal_strength_ = kGoodEndpointSignal + 1; |
| 1360 | service->NotifyEndpointUpdated(*good_endpoint); |
| 1361 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1362 | |
| 1363 | // Change in optimal Endpoint updates Service properties. |
| 1364 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1365 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1366 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1367 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1368 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1369 | ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2; |
| 1370 | service->NotifyEndpointUpdated(*ok_endpoint); |
| 1371 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1372 | } |
| 1373 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame^] | 1374 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) { |
| 1375 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1376 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
| 1377 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1378 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1379 | service->AddEndpoint(ok_endpoint); |
| 1380 | EXPECT_FALSE(service->ieee80211w_required()); |
| 1381 | good_endpoint->ieee80211w_required_ = true; |
| 1382 | service->AddEndpoint(good_endpoint); |
| 1383 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1384 | service->RemoveEndpoint(good_endpoint); |
| 1385 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1386 | } |
| 1387 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1388 | } // namespace shill |