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> |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 8 | #include <set> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 12 | #include <base/stringprintf.h> |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 13 | #include <base/string_number_conversions.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 14 | #include <base/string_util.h> |
| 15 | #include <chromeos/dbus/service_constants.h> |
| 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 21 | #include "shill/metrics.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 22 | #include "shill/mock_adaptors.h" |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 23 | #include "shill/mock_certificate_file.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 24 | #include "shill/mock_control.h" |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 25 | #include "shill/mock_eap_credentials.h" |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 26 | #include "shill/mock_log.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 27 | #include "shill/mock_manager.h" |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 28 | #include "shill/mock_nss.h" |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 29 | #include "shill/mock_profile.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 30 | #include "shill/mock_service.h" |
| 31 | #include "shill/mock_store.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 32 | #include "shill/mock_wifi.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 33 | #include "shill/mock_wifi_provider.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 34 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 35 | #include "shill/refptr_types.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 36 | #include "shill/service_property_change_test.h" |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 37 | #include "shill/wifi_endpoint.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 38 | #include "shill/wpa_supplicant.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 39 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 40 | using base::FilePath; |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 41 | using std::map; |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 42 | using std::set; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | using std::vector; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 45 | using ::testing::_; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 46 | using ::testing::AnyNumber; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 47 | using ::testing::DoAll; |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 48 | using ::testing::EndsWith; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 49 | using ::testing::HasSubstr; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 50 | using ::testing::Mock; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 51 | using ::testing::NiceMock; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 52 | using ::testing::Return; |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 53 | using ::testing::ReturnRef; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 54 | using ::testing::SetArgumentPointee; |
| 55 | using ::testing::StrEq; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 56 | using ::testing::StrNe; |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 57 | using ::testing::StrictMock; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 58 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 59 | namespace shill { |
| 60 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 61 | class WiFiServiceTest : public PropertyStoreTest { |
| 62 | public: |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 63 | WiFiServiceTest() |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 64 | : mock_manager_(control_interface(), dispatcher(), metrics(), glib()), |
| 65 | wifi_( |
| 66 | new NiceMock<MockWiFi>(control_interface(), |
| 67 | dispatcher(), |
| 68 | metrics(), |
| 69 | manager(), |
| 70 | "wifi", |
| 71 | fake_mac, |
| 72 | 0)), |
| 73 | simple_ssid_(1, 'a'), |
| 74 | simple_ssid_string_("a") {} |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 75 | virtual ~WiFiServiceTest() {} |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 76 | |
| 77 | protected: |
| 78 | static const char fake_mac[]; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 79 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 80 | MockEapCredentials *SetMockEap( |
| 81 | const WiFiServiceRefPtr &service) { |
| 82 | MockEapCredentials *eap = new MockEapCredentials(); |
| 83 | service->eap_.reset(eap); // Passes ownership. |
| 84 | return eap; |
| 85 | } |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 86 | bool CheckConnectable(const std::string &security, const char *passphrase, |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 87 | bool is_1x_connectable) { |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 88 | Error error; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 89 | WiFiServiceRefPtr service = MakeSimpleService(security); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 90 | if (passphrase) |
| 91 | service->SetPassphrase(passphrase, &error); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 92 | MockEapCredentials *eap = SetMockEap(service); |
| 93 | EXPECT_CALL(*eap, IsConnectable()) |
| 94 | .WillRepeatedly(Return(is_1x_connectable)); |
| 95 | const string kKeyManagement8021x(WPASupplicant::kKeyManagementIeee8021X); |
| 96 | if (security == flimflam::kSecurityWep && is_1x_connectable) { |
| 97 | EXPECT_CALL(*eap, key_management()) |
| 98 | .WillRepeatedly(ReturnRef(kKeyManagement8021x)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 99 | } |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 100 | service->OnEapCredentialsChanged(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 101 | return service->connectable(); |
| 102 | } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 103 | WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid, |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 104 | uint16 frequency, int16 signal_dbm, |
| 105 | bool has_wpa_property, bool has_rsn_property) { |
| 106 | return WiFiEndpoint::MakeEndpoint( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 107 | NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure, |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 108 | frequency, signal_dbm, has_wpa_property, has_rsn_property); |
| 109 | } |
| 110 | WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid, |
| 111 | uint16 frequency, int16 signal_dbm) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 112 | return WiFiEndpoint::MakeOpenEndpoint( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 113 | NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 114 | frequency, signal_dbm); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 115 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 116 | WiFiServiceRefPtr MakeSimpleService(const string &security) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 117 | return new WiFiService(control_interface(), |
| 118 | dispatcher(), |
| 119 | metrics(), |
| 120 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 121 | &provider_, |
| 122 | simple_ssid_, |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 123 | flimflam::kModeManaged, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 124 | security, |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 125 | false); |
| 126 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 127 | WiFiServiceRefPtr MakeGenericService() { |
| 128 | return MakeSimpleService(flimflam::kSecurityWep); |
| 129 | } |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 130 | void SetWiFi(WiFiServiceRefPtr service, WiFiRefPtr wifi) { |
| 131 | service->SetWiFi(wifi); // Has side-effects. |
| 132 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 133 | void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) { |
| 134 | service->wifi_ = wifi; |
| 135 | } |
| 136 | WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) { |
| 137 | WiFiServiceRefPtr service = MakeSimpleService(security); |
| 138 | SetWiFiForService(service, wifi_); |
| 139 | return service; |
| 140 | } |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 141 | WiFiServiceRefPtr MakeServiceWithMockManager() { |
| 142 | return new WiFiService(control_interface(), |
| 143 | dispatcher(), |
| 144 | metrics(), |
| 145 | &mock_manager_, |
| 146 | &provider_, |
| 147 | simple_ssid_, |
| 148 | flimflam::kModeManaged, |
| 149 | flimflam::kSecurityWep, |
| 150 | false); |
| 151 | } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 152 | ServiceMockAdaptor *GetAdaptor(WiFiService *service) { |
| 153 | return dynamic_cast<ServiceMockAdaptor *>(service->adaptor()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 154 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 155 | Error::Type TestConfigurePassphrase(const string &security, |
| 156 | const char *passphrase) { |
| 157 | WiFiServiceRefPtr service = MakeSimpleService(security); |
| 158 | KeyValueStore args; |
| 159 | if (passphrase) { |
| 160 | args.SetString(flimflam::kPassphraseProperty, passphrase); |
| 161 | } |
| 162 | Error error; |
| 163 | service->Configure(args, &error); |
| 164 | return error.type(); |
| 165 | } |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 166 | scoped_refptr<MockWiFi> wifi() { return wifi_; } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 167 | MockManager *mock_manager() { return &mock_manager_; } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 168 | MockWiFiProvider *provider() { return &provider_; } |
| 169 | string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; } |
| 170 | const vector<uint8_t> &simple_ssid() { return simple_ssid_; } |
| 171 | const string &simple_ssid_string() { return simple_ssid_string_; } |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 172 | |
| 173 | private: |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 174 | MockManager mock_manager_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 175 | scoped_refptr<MockWiFi> wifi_; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 176 | MockWiFiProvider provider_; |
| 177 | const vector<uint8_t> simple_ssid_; |
| 178 | const string simple_ssid_string_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 181 | // static |
| 182 | const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF"; |
| 183 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 184 | MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") { |
| 185 | string hex_ssid = base::HexEncode(ssid.data(), ssid.size()); |
| 186 | return |
| 187 | arg.ContainsString(WiFiService::kStorageType) && |
| 188 | arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi && |
| 189 | arg.ContainsString(WiFiService::kStorageSSID) && |
| 190 | arg.GetString(WiFiService::kStorageSSID) == hex_ssid && |
| 191 | arg.ContainsString(WiFiService::kStorageMode) && |
| 192 | arg.GetString(WiFiService::kStorageMode) == mode && |
| 193 | arg.ContainsString(WiFiService::kStorageSecurityClass) && |
| 194 | arg.GetString(WiFiService::kStorageSecurityClass) == security; |
| 195 | } |
| 196 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 197 | class WiFiServiceSecurityTest : public WiFiServiceTest { |
| 198 | public: |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 199 | bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service, |
| 200 | const string &security) { |
| 201 | string id = wifi_service->GetStorageIdentifier(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 202 | size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress())); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 203 | EXPECT_NE(mac_pos, string::npos); |
| 204 | size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos); |
| 205 | EXPECT_NE(mode_pos, string::npos); |
| 206 | return id.find(string(security), mode_pos) != string::npos; |
| 207 | } |
| 208 | |
| 209 | // Test that a service that is created with security |from_security| |
| 210 | // gets by default a storage identifier with |to_security| as its |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 211 | // security component, and that when saved, it sets the Security |
| 212 | // property in to |to_security| as well. |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 213 | bool TestStorageMapping(const string &from_security, |
| 214 | const string &to_security) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 215 | WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 216 | NiceMock<MockStore> mock_store; |
| 217 | EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true)); |
| 218 | EXPECT_CALL(mock_store, |
| 219 | SetString(_, WiFiService::kStorageSecurity, from_security)) |
| 220 | .Times(1); |
| 221 | EXPECT_CALL(mock_store, |
| 222 | SetString(_, WiFiService::kStorageSecurityClass, to_security)) |
| 223 | .Times(1); |
| 224 | wifi_service->Save(&mock_store); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 225 | return TestStorageSecurityIs(wifi_service, to_security); |
| 226 | } |
| 227 | |
| 228 | // Test whether a service of type |service_security| can load from a |
| 229 | // storage interface containing an entry for |storage_security|. |
| 230 | // Make sure the result meets |expectation|. If |expectation| is |
| 231 | // true, also make sure the service storage identifier changes to |
| 232 | // match |storage_security|. |
| 233 | bool TestLoadMapping(const string &service_security, |
| 234 | const string &storage_security, |
| 235 | bool expectation) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 236 | WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 237 | NiceMock<MockStore> mock_store; |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 238 | EXPECT_CALL(mock_store, GetGroupsWithProperties(_)) |
| 239 | .WillRepeatedly(Return(set<string>())); |
| 240 | const string kStorageId = "storage_id"; |
| 241 | EXPECT_CALL(mock_store, ContainsGroup(kStorageId)) |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 242 | .WillRepeatedly(Return(true)); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 243 | set<string> groups; |
| 244 | groups.insert(kStorageId); |
| 245 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 246 | ContainsWiFiProperties(wifi_service->ssid(), |
| 247 | flimflam::kModeManaged, |
| 248 | storage_security))) |
| 249 | .WillRepeatedly(Return(groups)); |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 250 | bool is_loadable = wifi_service->IsLoadableFrom(mock_store); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 251 | EXPECT_EQ(expectation, is_loadable); |
| 252 | bool is_loaded = wifi_service->Load(&mock_store); |
| 253 | EXPECT_EQ(expectation, is_loaded); |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 254 | const string expected_identifier(expectation ? kStorageId : ""); |
| 255 | EXPECT_EQ(expected_identifier, |
| 256 | wifi_service->GetLoadableStorageIdentifier(mock_store)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 257 | |
| 258 | if (expectation != is_loadable || expectation != is_loaded) { |
| 259 | return false; |
| 260 | } else if (!expectation) { |
| 261 | return true; |
| 262 | } else { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 263 | return wifi_service->GetStorageIdentifier() == kStorageId; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | }; |
| 267 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 268 | class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest { |
| 269 | public: |
| 270 | WiFiServiceUpdateFromEndpointsTest() |
| 271 | : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)), |
| 272 | kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)), |
| 273 | kGoodEndpointStrength( |
| 274 | WiFiService::SignalToStrength(kGoodEndpointSignal)), |
| 275 | service(MakeGenericService()), |
| 276 | adaptor(*GetAdaptor(service)) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 277 | ok_endpoint = MakeOpenEndpoint( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 278 | simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency, |
| 279 | kOkEndpointSignal); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 280 | good_endpoint = MakeOpenEndpoint( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 281 | simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency, |
| 282 | kGoodEndpointSignal); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 283 | bad_endpoint = MakeOpenEndpoint( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 284 | simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency, |
| 285 | kBadEndpointSignal); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | protected: |
| 289 | static const uint16 kOkEndpointFrequency = 2422; |
| 290 | static const uint16 kBadEndpointFrequency = 2417; |
| 291 | static const uint16 kGoodEndpointFrequency = 2412; |
| 292 | static const int16 kOkEndpointSignal = -50; |
| 293 | static const int16 kBadEndpointSignal = -75; |
| 294 | static const int16 kGoodEndpointSignal = -25; |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 295 | static const char *kOkEndpointBssId; |
| 296 | static const char *kGoodEndpointBssId; |
| 297 | static const char *kBadEndpointBssId; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 298 | // Can't be both static and const (because initialization requires a |
| 299 | // function call). So choose to be just const. |
| 300 | const uint8 kOkEndpointStrength; |
| 301 | const uint8 kBadEndpointStrength; |
| 302 | const uint8 kGoodEndpointStrength; |
| 303 | WiFiEndpointRefPtr ok_endpoint; |
| 304 | WiFiEndpointRefPtr bad_endpoint; |
| 305 | WiFiEndpointRefPtr good_endpoint; |
| 306 | WiFiServiceRefPtr service; |
| 307 | ServiceMockAdaptor &adaptor; |
| 308 | }; |
| 309 | |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 310 | const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId = |
| 311 | "00:00:00:00:00:01"; |
| 312 | const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId = |
| 313 | "00:00:00:00:00:02"; |
| 314 | const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId = |
| 315 | "00:00:00:00:00:03"; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 316 | |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 317 | class WiFiServiceFixupStorageTest : public WiFiServiceTest { |
| 318 | protected: |
| 319 | void AddGroup(string group_name) { |
| 320 | groups_.insert(group_name); |
| 321 | } |
| 322 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 323 | void AddServiceEntry(bool has_type, bool has_mode, bool has_security, |
| 324 | bool has_security_class) { |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 325 | int index = groups_.size(); |
| 326 | string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi, |
| 327 | index, index, flimflam::kModeManaged, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 328 | flimflam::kSecurityWpa); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 329 | AddGroup(id); |
| 330 | EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _)) |
| 331 | .WillOnce(Return(has_type)); |
| 332 | if (!has_type) { |
| 333 | EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType, |
| 334 | flimflam::kTypeWifi)); |
| 335 | } |
| 336 | EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _)) |
| 337 | .WillOnce(Return(has_mode)); |
| 338 | if (!has_mode) { |
| 339 | EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode, |
| 340 | flimflam::kModeManaged)); |
| 341 | } |
| 342 | EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _)) |
| 343 | .WillOnce(Return(has_security)); |
| 344 | if (!has_security) { |
| 345 | EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 346 | flimflam::kSecurityWpa)); |
| 347 | } |
| 348 | EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _)) |
| 349 | .WillOnce(Return(has_security_class)); |
| 350 | if (!has_security_class) { |
| 351 | EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass, |
| 352 | flimflam::kSecurityPsk)); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | bool FixupServiceEntries() { |
| 357 | EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_)); |
| 358 | return WiFiService::FixupServiceEntries(&store_); |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | StrictMock<MockStore> store_; |
| 363 | set<string> groups_; |
| 364 | }; |
| 365 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 366 | TEST_F(WiFiServiceTest, StorageId) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 367 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 368 | string id = wifi_service->GetStorageIdentifier(); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 369 | for (uint i = 0; i < id.length(); ++i) { |
| 370 | EXPECT_TRUE(id[i] == '_' || |
| 371 | isxdigit(id[i]) || |
| 372 | (isalpha(id[i]) && islower(id[i]))); |
| 373 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 374 | size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress())); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 375 | EXPECT_NE(mac_pos, string::npos); |
| 376 | EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos); |
| 377 | } |
| 378 | |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 379 | // Make sure the passphrase is registered as a write only property |
| 380 | // by reading and comparing all string properties returned on the store. |
| 381 | TEST_F(WiFiServiceTest, PassphraseWriteOnly) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 382 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa); |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 383 | ReadablePropertyConstIterator<string> it = |
| 384 | (wifi_service->store()).GetStringPropertiesIter(); |
Wade Guthrie | f54872f | 2013-04-11 15:11:50 -0700 | [diff] [blame] | 385 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 386 | EXPECT_NE(it.Key(), flimflam::kPassphraseProperty); |
| 387 | } |
| 388 | |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 389 | // Make sure setting the passphrase via D-Bus Service.SetProperty validates |
| 390 | // the passphrase. |
| 391 | TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) { |
| 392 | // We only spot check two password cases here to make sure the |
| 393 | // SetProperty code path does validation. We're not going to exhaustively |
| 394 | // test for all types of passwords. |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 395 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 396 | Error error; |
| 397 | EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty( |
| 398 | flimflam::kPassphraseProperty, "0:abcde", &error)); |
| 399 | EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty( |
| 400 | flimflam::kPassphraseProperty, "invalid", &error)); |
| 401 | EXPECT_EQ(Error::kInvalidPassphrase, error.type()); |
| 402 | } |
| 403 | |
| 404 | TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 405 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 406 | Error error; |
| 407 | EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty( |
| 408 | flimflam::kPassphraseProperty, "invalid", &error)); |
| 409 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 410 | } |
| 411 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 412 | TEST_F(WiFiServiceTest, NonUTF8SSID) { |
| 413 | vector<uint8_t> ssid; |
| 414 | |
| 415 | ssid.push_back(0xff); // not a valid UTF-8 byte-sequence |
| 416 | WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(), |
| 417 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 418 | metrics(), |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 419 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 420 | provider(), |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 421 | ssid, |
| 422 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 423 | flimflam::kSecurityNone, |
| 424 | false); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 425 | map<string, ::DBus::Variant> properties; |
| 426 | // if service doesn't propertly sanitize SSID, this will generate SIGABRT. |
| 427 | DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL); |
| 428 | } |
| 429 | |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 430 | MATCHER(PSKSecurityArgs, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 431 | return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) && |
| 432 | arg.find(WPASupplicant::kPropertySecurityProtocol)->second. |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 433 | reader().get_string() == string("WPA RSN") && |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 434 | ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey); |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 437 | MATCHER(WPA80211wSecurityArgs, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 438 | return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) && |
| 439 | ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey) && |
| 440 | ContainsKey(arg, WPASupplicant::kNetworkPropertyIeee80211w); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 443 | MATCHER(EAPSecurityArgs, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 444 | return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) && |
| 445 | ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 448 | MATCHER_P(FrequencyArg, has_arg, "") { |
| 449 | return has_arg == |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 450 | ContainsKey(arg, WPASupplicant::kNetworkPropertyFrequency); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 453 | TEST_F(WiFiServiceTest, ConnectTaskWPA) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 454 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa); |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 455 | EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 456 | Error error; |
| 457 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 458 | wifi_service->Connect(NULL, "in test"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 459 | } |
| 460 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 461 | TEST_F(WiFiServiceTest, ConnectTaskRSN) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 462 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn); |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 463 | EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 464 | Error error; |
| 465 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 466 | wifi_service->Connect(NULL, "in test"); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 469 | TEST_F(WiFiServiceTest, ConnectConditions) { |
| 470 | Error error; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 471 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 472 | scoped_refptr<MockProfile> mock_profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 473 | new NiceMock<MockProfile>(control_interface(), metrics(), manager())); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 474 | wifi_service->set_profile(mock_profile); |
| 475 | // With nothing else going on, the service should attempt to connect. |
| 476 | EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _)); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 477 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 478 | Mock::VerifyAndClearExpectations(wifi()); |
| 479 | |
| 480 | // But if we're already "connecting" or "connected" then we shouldn't attempt |
| 481 | // again. |
| 482 | EXPECT_CALL(*wifi(), |
| 483 | ConnectTo(wifi_service.get(), _)).Times(0); |
| 484 | wifi_service->SetState(Service::kStateAssociating); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 485 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 486 | wifi_service->SetState(Service::kStateConfiguring); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 487 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 488 | wifi_service->SetState(Service::kStateConnected); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 489 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 490 | wifi_service->SetState(Service::kStatePortal); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 491 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 492 | wifi_service->SetState(Service::kStateOnline); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 493 | wifi_service->Connect(&error, "in test"); |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 494 | Mock::VerifyAndClearExpectations(wifi()); |
| 495 | } |
| 496 | |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 497 | TEST_F(WiFiServiceTest, ConnectTaskPSK) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 498 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk); |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 499 | EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 500 | Error error; |
| 501 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 502 | wifi_service->Connect(NULL, "in test"); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 505 | TEST_F(WiFiServiceTest, ConnectTask8021x) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 506 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 507 | service->mutable_eap()->set_identity("identity"); |
| 508 | service->mutable_eap()->set_password("mumble"); |
| 509 | service->OnEapCredentialsChanged(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 510 | EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 511 | service->Connect(NULL, "in test"); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 514 | TEST_F(WiFiServiceTest, ConnectTask8021xWithMockEap) { |
| 515 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x); |
| 516 | MockEapCredentials *eap = SetMockEap(service); |
| 517 | EXPECT_CALL(*eap, IsConnectable()).WillOnce(Return(true)); |
| 518 | service->OnEapCredentialsChanged(); |
| 519 | EXPECT_CALL(*eap, PopulateSupplicantProperties(_, _, _, _)); |
| 520 | // The mocked function does not actually set EAP parameters so we cannot |
| 521 | // expect them to be set. |
| 522 | EXPECT_CALL(*wifi(), ConnectTo(service.get(), _)); |
| 523 | service->Connect(NULL, "in test"); |
| 524 | } |
| 525 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 526 | TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) { |
| 527 | vector<uint8_t> ssid(1, 'a'); |
| 528 | WiFiEndpointRefPtr endpoint_nofreq = |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 529 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 530 | WiFiEndpointRefPtr endpoint_freq = |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 531 | MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 532 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 533 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 534 | wifi_service->AddEndpoint(endpoint_freq); |
| 535 | EXPECT_CALL(*wifi(), |
| 536 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 537 | wifi_service->Connect(NULL, "in test"); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 538 | |
| 539 | wifi_service = new WiFiService(control_interface(), |
| 540 | dispatcher(), |
| 541 | metrics(), |
| 542 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 543 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 544 | ssid, |
| 545 | flimflam::kModeAdhoc, |
| 546 | flimflam::kSecurityNone, |
| 547 | false); |
| 548 | EXPECT_CALL(*wifi(), |
| 549 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 550 | SetWiFiForService(wifi_service, wifi()); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 551 | wifi_service->Connect(NULL, "in test"); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 552 | |
| 553 | wifi_service = new WiFiService(control_interface(), |
| 554 | dispatcher(), |
| 555 | metrics(), |
| 556 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 557 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 558 | ssid, |
| 559 | flimflam::kModeAdhoc, |
| 560 | flimflam::kSecurityNone, |
| 561 | false); |
| 562 | wifi_service->AddEndpoint(endpoint_nofreq); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 563 | SetWiFiForService(wifi_service, wifi()); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 564 | EXPECT_CALL(*wifi(), |
| 565 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 566 | wifi_service->Connect(NULL, "in test"); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 567 | |
| 568 | wifi_service = new WiFiService(control_interface(), |
| 569 | dispatcher(), |
| 570 | metrics(), |
| 571 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 572 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 573 | ssid, |
| 574 | flimflam::kModeAdhoc, |
| 575 | flimflam::kSecurityNone, |
| 576 | false); |
| 577 | wifi_service->AddEndpoint(endpoint_freq); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 578 | SetWiFiForService(wifi_service, wifi()); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 579 | EXPECT_CALL(*wifi(), |
| 580 | ConnectTo(wifi_service.get(), FrequencyArg(true))); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 581 | wifi_service->Connect(NULL, "in test"); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 584 | TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 585 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 586 | WiFiEndpointRefPtr endpoint = |
| 587 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 588 | endpoint->ieee80211w_required_ = true; |
| 589 | wifi_service->AddEndpoint(endpoint); |
| 590 | Error error; |
| 591 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
| 592 | EXPECT_CALL(*wifi(), |
| 593 | ConnectTo(wifi_service.get(), WPA80211wSecurityArgs())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 594 | wifi_service->Connect(NULL, "in test"); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 597 | MATCHER(WEPSecurityArgsKeyIndex0, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 598 | return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) && |
| 599 | ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("0")) && |
| 600 | ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) && |
| 601 | (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second. |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 602 | reader().get_uint32() == 0); |
| 603 | } |
| 604 | |
| 605 | MATCHER(WEPSecurityArgsKeyIndex1, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 606 | return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) && |
| 607 | ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("1")) && |
| 608 | ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) && |
| 609 | (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second. |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 610 | reader().get_uint32() == 1); |
| 611 | } |
| 612 | |
| 613 | MATCHER(WEPSecurityArgsKeyIndex2, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 614 | return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) && |
| 615 | ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("2")) && |
| 616 | ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) && |
| 617 | (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second. |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 618 | reader().get_uint32() == 2); |
| 619 | } |
| 620 | |
| 621 | MATCHER(WEPSecurityArgsKeyIndex3, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 622 | return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) && |
| 623 | ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("3")) && |
| 624 | ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) && |
| 625 | (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second. |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 626 | reader().get_uint32() == 3); |
| 627 | } |
| 628 | |
| 629 | TEST_F(WiFiServiceTest, ConnectTaskWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 630 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 631 | Error error; |
| 632 | wifi_service->SetPassphrase("0:abcdefghijklm", &error); |
| 633 | EXPECT_CALL(*wifi(), |
| 634 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 635 | wifi_service->Connect(NULL, "in test"); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 636 | |
| 637 | wifi_service->SetPassphrase("abcdefghijklm", &error); |
| 638 | EXPECT_CALL(*wifi(), |
| 639 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 640 | wifi_service->Connect(NULL, "in test"); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 641 | |
| 642 | wifi_service->SetPassphrase("1:abcdefghijklm", &error); |
| 643 | EXPECT_CALL(*wifi(), |
| 644 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 645 | wifi_service->Connect(NULL, "in test"); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 646 | |
| 647 | wifi_service->SetPassphrase("2:abcdefghijklm", &error); |
| 648 | EXPECT_CALL(*wifi(), |
| 649 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 650 | wifi_service->Connect(NULL, "in test"); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 651 | |
| 652 | wifi_service->SetPassphrase("3:abcdefghijklm", &error); |
| 653 | EXPECT_CALL(*wifi(), |
| 654 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 655 | wifi_service->Connect(NULL, "in test"); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 658 | |
| 659 | MATCHER(DynamicWEPArgs, "") { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 660 | return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) && |
| 661 | ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath) && |
| 662 | !ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // Dynamic WEP + 802.1x. |
| 666 | TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 667 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 668 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 669 | wifi_service->mutable_eap()->SetKeyManagement("IEEE8021X", NULL); |
| 670 | wifi_service->mutable_eap()->set_identity("something"); |
| 671 | wifi_service->mutable_eap()->set_password("mumble"); |
| 672 | wifi_service->OnEapCredentialsChanged(); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 673 | EXPECT_CALL(*wifi(), |
| 674 | ConnectTo(wifi_service.get(), DynamicWEPArgs())); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 675 | wifi_service->Connect(NULL, "in test"); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 678 | TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 679 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 680 | |
| 681 | const string kPassphrase = "abcdefgh"; |
| 682 | |
| 683 | { |
| 684 | Error error; |
| 685 | // A changed passphrase should trigger cache removal. |
| 686 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 687 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 688 | Mock::VerifyAndClearExpectations(wifi()); |
| 689 | EXPECT_TRUE(error.IsSuccess()); |
| 690 | } |
| 691 | |
| 692 | { |
| 693 | Error error; |
| 694 | // An unchanged passphrase should not trigger cache removal. |
| 695 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 696 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 697 | Mock::VerifyAndClearExpectations(wifi()); |
| 698 | EXPECT_TRUE(error.IsSuccess()); |
| 699 | } |
| 700 | |
| 701 | { |
| 702 | Error error; |
| 703 | // A modified passphrase should trigger cache removal. |
| 704 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 705 | wifi_service->SetPassphrase(kPassphrase + "X", &error); |
| 706 | Mock::VerifyAndClearExpectations(wifi()); |
| 707 | EXPECT_TRUE(error.IsSuccess()); |
| 708 | } |
| 709 | |
| 710 | { |
| 711 | Error error; |
| 712 | // A cleared passphrase should also trigger cache removal. |
| 713 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 714 | wifi_service->ClearPassphrase(&error); |
| 715 | Mock::VerifyAndClearExpectations(wifi()); |
| 716 | EXPECT_TRUE(error.IsSuccess()); |
| 717 | } |
| 718 | |
| 719 | { |
| 720 | Error error; |
| 721 | // An invalid passphrase should not trigger cache removal. |
| 722 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 723 | wifi_service->SetPassphrase("", &error); |
| 724 | Mock::VerifyAndClearExpectations(wifi()); |
| 725 | EXPECT_FALSE(error.IsSuccess()); |
| 726 | } |
| 727 | |
| 728 | { |
| 729 | // Any change to EAP parameters (including a null one) will trigger cache |
| 730 | // removal. This is a lot less granular than the passphrase checks above. |
| 731 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 732 | wifi_service->OnEapCredentialsChanged(); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 733 | Mock::VerifyAndClearExpectations(wifi()); |
| 734 | } |
| 735 | } |
| 736 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 737 | // This test is somewhat redundant, since: |
| 738 | // |
| 739 | // a) we test that generic property setters return false on a null |
| 740 | // change (e.g. in PropertyAccessorTest.SignedIntCorrectness) |
| 741 | // b) we test that custom EAP property setters return false on a null |
| 742 | // change in EapCredentialsTest.CustomSetterNoopChange |
| 743 | // c) we test that the various custom accessors pass through the |
| 744 | // return value of custom setters |
| 745 | // (e.g. PropertyAccessorTest.CustomAccessorCorrectness) |
| 746 | // d) we test that PropertyStore skips the change callback when a |
| 747 | // property setter return false (PropertyStoreTypedTest.SetProperty) |
| 748 | // |
| 749 | // Nonetheless, I think it's worth testing the WiFi+EAP case directly. |
| 750 | TEST_F(WiFiServiceTest, EapAuthPropertyChangeClearsCachedCredentials) { |
| 751 | WiFiServiceRefPtr wifi_service = |
| 752 | MakeServiceWithWiFi(flimflam::kSecurity8021x); |
| 753 | PropertyStore &property_store(*wifi_service->mutable_store()); |
| 754 | |
| 755 | // Property with custom accessor. |
| 756 | const string kPassword = "abcdefgh"; |
| 757 | { |
| 758 | Error error; |
| 759 | // A changed passphrase should trigger cache removal. |
| 760 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 761 | EXPECT_TRUE(property_store.SetStringProperty( |
| 762 | flimflam::kEapPasswordProperty, kPassword, &error)); |
| 763 | Mock::VerifyAndClearExpectations(wifi()); |
| 764 | EXPECT_TRUE(error.IsSuccess()); |
| 765 | } |
| 766 | { |
| 767 | Error error; |
| 768 | // An unchanged passphrase should not trigger cache removal. |
| 769 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 770 | EXPECT_FALSE(property_store.SetStringProperty( |
| 771 | flimflam::kEapPasswordProperty, kPassword, &error)); |
| 772 | Mock::VerifyAndClearExpectations(wifi()); |
| 773 | EXPECT_TRUE(error.IsSuccess()); |
| 774 | } |
| 775 | { |
| 776 | Error error; |
| 777 | // A modified passphrase should trigger cache removal. |
| 778 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 779 | EXPECT_TRUE(property_store.SetStringProperty( |
| 780 | flimflam::kEapPasswordProperty, kPassword + "X", &error)); |
| 781 | Mock::VerifyAndClearExpectations(wifi()); |
| 782 | EXPECT_TRUE(error.IsSuccess()); |
| 783 | } |
| 784 | |
| 785 | // Property with generic accessor. |
| 786 | const string kCertId = "abcdefgh"; |
| 787 | { |
| 788 | Error error; |
| 789 | // A changed cert id should trigger cache removal. |
| 790 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 791 | EXPECT_TRUE(property_store.SetStringProperty( |
| 792 | flimflam::kEapCertIdProperty, kCertId, &error)); |
| 793 | Mock::VerifyAndClearExpectations(wifi()); |
| 794 | EXPECT_TRUE(error.IsSuccess()); |
| 795 | } |
| 796 | { |
| 797 | Error error; |
| 798 | // An unchanged cert id should not trigger cache removal. |
| 799 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 800 | EXPECT_FALSE(property_store.SetStringProperty( |
| 801 | flimflam::kEapCertIdProperty, kCertId, &error)); |
| 802 | Mock::VerifyAndClearExpectations(wifi()); |
| 803 | EXPECT_TRUE(error.IsSuccess()); |
| 804 | } |
| 805 | { |
| 806 | Error error; |
| 807 | // A modified cert id should trigger cache removal. |
| 808 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 809 | EXPECT_TRUE(property_store.SetStringProperty( |
| 810 | flimflam::kEapCertIdProperty, kCertId + "X", &error)); |
| 811 | Mock::VerifyAndClearExpectations(wifi()); |
| 812 | EXPECT_TRUE(error.IsSuccess()); |
| 813 | } |
| 814 | } |
| 815 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 816 | TEST_F(WiFiServiceTest, LoadHidden) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 817 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 818 | ASSERT_FALSE(service->hidden_ssid_); |
| 819 | NiceMock<MockStore> mock_store; |
| 820 | const string storage_id = service->GetStorageIdentifier(); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 821 | set<string> groups; |
| 822 | groups.insert(storage_id); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 823 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 824 | .WillRepeatedly(Return(true)); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 825 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 826 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 827 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 828 | .WillRepeatedly(Return(groups)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 829 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 830 | .WillRepeatedly(Return(false)); |
| 831 | EXPECT_CALL(mock_store, |
| 832 | GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _)) |
| 833 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 834 | EXPECT_TRUE(service->Load(&mock_store)); |
| 835 | EXPECT_TRUE(service->hidden_ssid_); |
| 836 | } |
| 837 | |
Paul Stewart | fa013ab | 2013-04-11 07:12:03 -0700 | [diff] [blame] | 838 | TEST_F(WiFiServiceTest, LoadPassphraseForNonPassphraseService) { |
| 839 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
| 840 | NiceMock<MockStore> mock_store; |
| 841 | const string storage_id = service->GetStorageIdentifier(); |
| 842 | set<string> groups; |
| 843 | groups.insert(storage_id); |
| 844 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 845 | .WillRepeatedly(Return(true)); |
| 846 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 847 | ContainsWiFiProperties( |
| 848 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone))) |
| 849 | .WillRepeatedly(Return(groups)); |
| 850 | EXPECT_CALL(mock_store, |
| 851 | GetCryptedString(StrEq(storage_id), |
| 852 | StrNe(WiFiService::kStoragePassphrase), _)) |
| 853 | .WillRepeatedly(Return(false)); |
| 854 | EXPECT_CALL(mock_store, |
| 855 | GetCryptedString(StrEq(storage_id), |
| 856 | WiFiService::kStoragePassphrase, _)) |
| 857 | .WillOnce(DoAll(SetArgumentPointee<2>(string("password")), Return(true))) |
| 858 | .WillOnce(DoAll(SetArgumentPointee<2>(string()), Return(true))); |
| 859 | ScopedMockLog log; |
| 860 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 861 | EndsWith("Passphrase could not be set: " |
| 862 | "org.chromium.flimflam.Error.NotSupported"))) |
| 863 | .Times(1); |
| 864 | EXPECT_TRUE(service->Load(&mock_store)); |
| 865 | Mock::VerifyAndClearExpectations(&log); |
| 866 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, _)).Times(0); |
| 867 | EXPECT_TRUE(service->Load(&mock_store)); |
| 868 | } |
| 869 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 870 | TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 871 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 872 | set<string> groups; |
| 873 | groups.insert("id0"); |
| 874 | groups.insert("id1"); |
| 875 | // Make sure we retain the first matched group in the same way that |
| 876 | // WiFiService::Load() will. |
| 877 | string first_group = *groups.begin(); |
| 878 | |
| 879 | NiceMock<MockStore> mock_store; |
| 880 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 881 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 882 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 883 | .WillRepeatedly(Return(groups)); |
| 884 | EXPECT_CALL(mock_store, ContainsGroup(first_group)) |
| 885 | .WillRepeatedly(Return(true)); |
| 886 | EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0); |
| 887 | EXPECT_CALL(mock_store, GetBool(first_group, _, _)) |
| 888 | .WillRepeatedly(Return(false)); |
| 889 | EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0); |
| 890 | ScopedMockLog log; |
| 891 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 892 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 893 | EndsWith("choosing the first."))); |
| 894 | EXPECT_TRUE(service->Load(&mock_store)); |
| 895 | } |
| 896 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 897 | TEST_F(WiFiServiceSecurityTest, WPAMapping) { |
| 898 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn, |
| 899 | flimflam::kSecurityPsk)); |
| 900 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa, |
| 901 | flimflam::kSecurityPsk)); |
| 902 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk, |
| 903 | flimflam::kSecurityPsk)); |
| 904 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep, |
| 905 | flimflam::kSecurityWep)); |
| 906 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone, |
| 907 | flimflam::kSecurityNone)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 908 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x, |
| 909 | flimflam::kSecurity8021x)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | TEST_F(WiFiServiceSecurityTest, LoadMapping) { |
| 913 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 914 | flimflam::kSecurityPsk, |
| 915 | true)); |
| 916 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 917 | flimflam::kSecurityRsn, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 918 | false)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 919 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 920 | flimflam::kSecurityWpa, |
| 921 | false)); |
| 922 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 923 | flimflam::kSecurityPsk, |
| 924 | true)); |
| 925 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 926 | flimflam::kSecurityWpa, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 927 | false)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 928 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 929 | flimflam::kSecurityRsn, |
| 930 | false)); |
| 931 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 932 | flimflam::kSecurityWep, |
| 933 | true)); |
| 934 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 935 | flimflam::kSecurityPsk, |
| 936 | false)); |
| 937 | } |
| 938 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 939 | TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 940 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 941 | NiceMock<MockStore> mock_store; |
| 942 | const string storage_id = service->GetStorageIdentifier(); |
| 943 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 944 | .WillRepeatedly(Return(true)); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 945 | set<string> groups; |
| 946 | groups.insert(storage_id); |
| 947 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 948 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 949 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 950 | .WillRepeatedly(Return(groups)); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 951 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 952 | .WillRepeatedly(Return(false)); |
| 953 | const string passphrase = "passphrase"; |
| 954 | EXPECT_CALL(mock_store, |
| 955 | GetCryptedString(StrEq(storage_id), |
| 956 | WiFiService::kStoragePassphrase, _)) |
| 957 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true))); |
| 958 | EXPECT_CALL(mock_store, |
| 959 | GetCryptedString(StrEq(storage_id), |
| 960 | StrNe(WiFiService::kStoragePassphrase), _)) |
| 961 | .WillRepeatedly(Return(false)); |
| 962 | EXPECT_TRUE(service->need_passphrase_); |
| 963 | EXPECT_TRUE(service->Load(&mock_store)); |
| 964 | EXPECT_EQ(passphrase, service->passphrase_); |
| 965 | EXPECT_TRUE(service->connectable()); |
| 966 | EXPECT_FALSE(service->need_passphrase_); |
| 967 | service->Unload(); |
| 968 | EXPECT_EQ(string(""), service->passphrase_); |
| 969 | EXPECT_FALSE(service->connectable()); |
| 970 | EXPECT_TRUE(service->need_passphrase_); |
| 971 | } |
| 972 | |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 973 | TEST_F(WiFiServiceTest, ConfigureMakesConnectable) { |
| 974 | string guid("legit_guid"); |
| 975 | KeyValueStore args; |
| 976 | args.SetString(flimflam::kEapIdentityProperty, "legit_identity"); |
| 977 | args.SetString(flimflam::kEapPasswordProperty, "legit_password"); |
| 978 | args.SetString(flimflam::kEAPEAPProperty, "PEAP"); |
| 979 | args.SetString(flimflam::kGuidProperty, guid); |
| 980 | Error error; |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 981 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 982 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 983 | // Hack the GUID in so that we don't have to mess about with WiFi to regsiter |
| 984 | // our service. This way, Manager will handle the lookup itself. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 985 | service->SetGuid(guid, NULL); |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 986 | manager()->RegisterService(service); |
| 987 | EXPECT_FALSE(service->connectable()); |
| 988 | EXPECT_EQ(service.get(), manager()->GetService(args, &error).get()); |
| 989 | EXPECT_TRUE(error.IsSuccess()); |
| 990 | EXPECT_TRUE(service->connectable()); |
| 991 | } |
| 992 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 993 | TEST_F(WiFiServiceTest, ConfigurePassphrase) { |
| 994 | EXPECT_EQ(Error::kNotSupported, |
| 995 | TestConfigurePassphrase(flimflam::kSecurityNone, "")); |
| 996 | EXPECT_EQ(Error::kNotSupported, |
| 997 | TestConfigurePassphrase(flimflam::kSecurityNone, "foo")); |
| 998 | EXPECT_EQ(Error::kSuccess, |
| 999 | TestConfigurePassphrase(flimflam::kSecurityWep, NULL)); |
| 1000 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1001 | TestConfigurePassphrase(flimflam::kSecurityWep, "")); |
| 1002 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1003 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcd")); |
| 1004 | EXPECT_EQ(Error::kSuccess, |
| 1005 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcde")); |
| 1006 | EXPECT_EQ(Error::kSuccess, |
| 1007 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm")); |
| 1008 | EXPECT_EQ(Error::kSuccess, |
| 1009 | TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm")); |
| 1010 | EXPECT_EQ(Error::kSuccess, |
| 1011 | TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405")); |
| 1012 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1013 | TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405")); |
| 1014 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1015 | TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405")); |
| 1016 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1017 | TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405")); |
| 1018 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1019 | TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405")); |
| 1020 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1021 | TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405")); |
| 1022 | EXPECT_EQ(Error::kSuccess, |
| 1023 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 1024 | "0102030405060708090a0b0c0d")); |
| 1025 | EXPECT_EQ(Error::kSuccess, |
| 1026 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 1027 | "0102030405060708090A0B0C0D")); |
| 1028 | EXPECT_EQ(Error::kSuccess, |
| 1029 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 1030 | "0:0102030405060708090a0b0c0d")); |
| 1031 | EXPECT_EQ(Error::kSuccess, |
| 1032 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 1033 | "0:0x0102030405060708090a0b0c0d")); |
| 1034 | EXPECT_EQ(Error::kSuccess, |
| 1035 | TestConfigurePassphrase(flimflam::kSecurityWpa, NULL)); |
| 1036 | EXPECT_EQ(Error::kSuccess, |
| 1037 | TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password")); |
| 1038 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 1039 | TestConfigurePassphrase(flimflam::kSecurityWpa, "")); |
| 1040 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 1041 | flimflam::kSecurityWpa, |
| 1042 | string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str())); |
| 1043 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 1044 | flimflam::kSecurityWpa, |
| 1045 | string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str())); |
| 1046 | // subtle: invalid length for hex key, but valid as ascii passphrase |
| 1047 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 1048 | flimflam::kSecurityWpa, |
| 1049 | string(IEEE_80211::kWPAHexLen-1, '1').c_str())); |
| 1050 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 1051 | flimflam::kSecurityWpa, |
| 1052 | string(IEEE_80211::kWPAHexLen, '1').c_str())); |
| 1053 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 1054 | flimflam::kSecurityWpa, |
| 1055 | string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str())); |
| 1056 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 1057 | flimflam::kSecurityWpa, |
| 1058 | string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str())); |
| 1059 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 1060 | flimflam::kSecurityWpa, |
| 1061 | string(IEEE_80211::kWPAHexLen+1, '1').c_str())); |
| 1062 | } |
| 1063 | |
| 1064 | TEST_F(WiFiServiceTest, ConfigureRedundantProperties) { |
| 1065 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
| 1066 | KeyValueStore args; |
| 1067 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1068 | args.SetString(flimflam::kSSIDProperty, simple_ssid_string()); |
| 1069 | args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone); |
| 1070 | const string kGUID = "aguid"; |
| 1071 | args.SetString(flimflam::kGuidProperty, kGUID); |
| 1072 | |
| 1073 | EXPECT_EQ("", service->guid()); |
| 1074 | Error error; |
| 1075 | service->Configure(args, &error); |
| 1076 | EXPECT_TRUE(error.IsSuccess()); |
| 1077 | EXPECT_EQ(kGUID, service->guid()); |
| 1078 | } |
| 1079 | |
| 1080 | TEST_F(WiFiServiceTest, DisconnectWithWiFi) { |
| 1081 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
| 1082 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
| 1083 | Error error; |
| 1084 | service->Disconnect(&error); |
| 1085 | } |
| 1086 | |
| 1087 | TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) { |
| 1088 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1089 | EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0); |
| 1090 | Error error; |
| 1091 | service->Disconnect(&error); |
| 1092 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 1093 | } |
| 1094 | |
| 1095 | TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) { |
| 1096 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1097 | EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0); |
| 1098 | service->SetState(Service::kStateAssociating); |
| 1099 | ScopedMockLog log; |
| 1100 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 1101 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1102 | HasSubstr("WiFi endpoints do not (yet) exist."))); |
| 1103 | Error error; |
| 1104 | service->Disconnect(&error); |
| 1105 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 1106 | } |
| 1107 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1108 | TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1109 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1110 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 1111 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 1112 | service->Unload(); |
| 1113 | } |
| 1114 | |
| 1115 | TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1116 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1117 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 1118 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 1119 | service->Unload(); |
| 1120 | } |
| 1121 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 1122 | TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1123 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1124 | const string storage_id = service->GetStorageIdentifier(); |
| 1125 | string address; |
| 1126 | string mode; |
| 1127 | string security; |
| 1128 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 1129 | &security)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1130 | EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1131 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 1132 | EXPECT_EQ(flimflam::kSecurityNone, security); |
| 1133 | } |
| 1134 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 1135 | TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) { |
| 1136 | // Do a separate test for 802.1x, since kSecurity8021x contains a "_", |
| 1137 | // which needs to be dealt with specially in the parser. |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1138 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 1139 | const string storage_id = service->GetStorageIdentifier(); |
| 1140 | string address; |
| 1141 | string mode; |
| 1142 | string security; |
| 1143 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 1144 | &security)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1145 | EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 1146 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 1147 | EXPECT_EQ(flimflam::kSecurity8021x, security); |
| 1148 | } |
| 1149 | |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1150 | TEST_F(WiFiServiceFixupStorageTest, FixedEntries) { |
| 1151 | const string kNonWiFiId = "vpn_foo"; |
| 1152 | const string kUnparsableWiFiId = "wifi_foo"; |
| 1153 | |
| 1154 | AddGroup(kNonWiFiId); |
| 1155 | AddGroup(kUnparsableWiFiId); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1156 | AddServiceEntry(true, true, true, true); |
| 1157 | AddServiceEntry(false, false, false, false); |
| 1158 | AddServiceEntry(true, true, true, true); |
| 1159 | AddServiceEntry(false, false, false, false); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1160 | EXPECT_TRUE(FixupServiceEntries()); |
| 1161 | } |
| 1162 | |
| 1163 | TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) { |
| 1164 | const string kNonWiFiId = "vpn_foo"; |
| 1165 | const string kUnparsableWiFiId = "wifi_foo"; |
| 1166 | |
| 1167 | AddGroup(kNonWiFiId); |
| 1168 | AddGroup(kUnparsableWiFiId); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1169 | AddServiceEntry(true, true, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1170 | EXPECT_FALSE(FixupServiceEntries()); |
| 1171 | } |
| 1172 | |
| 1173 | TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1174 | AddServiceEntry(false, true, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1175 | EXPECT_TRUE(FixupServiceEntries()); |
| 1176 | } |
| 1177 | |
| 1178 | TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1179 | AddServiceEntry(true, false, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1180 | EXPECT_TRUE(FixupServiceEntries()); |
| 1181 | } |
| 1182 | |
| 1183 | TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1184 | AddServiceEntry(true, true, false, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1185 | EXPECT_TRUE(FixupServiceEntries()); |
| 1186 | } |
| 1187 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1188 | TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) { |
| 1189 | AddServiceEntry(true, true, true, false); |
| 1190 | EXPECT_TRUE(FixupServiceEntries()); |
| 1191 | } |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1192 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1193 | TEST_F(WiFiServiceTest, Connectable) { |
| 1194 | // Open network should be connectable. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1195 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Open network should remain connectable if we try to set a password on it. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1198 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1199 | |
| 1200 | // WEP network with passphrase set should be connectable. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1201 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1202 | |
| 1203 | // WEP network without passphrase set should NOT be connectable. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1204 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1205 | |
| 1206 | // A bad passphrase should not make a WEP network connectable. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1207 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Similar to WEP, for WPA. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1210 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", false)); |
| 1211 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, false)); |
| 1212 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", false)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1213 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1214 | // 802.1x without connectable EAP credentials should NOT be connectable. |
| 1215 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, false)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1216 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1217 | // 802.1x with connectable EAP credentials should be connectable. |
| 1218 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, true)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1219 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 1220 | // Dynamic WEP + 802.1X should be connectable under the same conditions. |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1221 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, true)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1224 | TEST_F(WiFiServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1225 | const char *reason; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1226 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1227 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1228 | .WillRepeatedly(Return(true)); |
| 1229 | EXPECT_FALSE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1230 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 1231 | EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1232 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1233 | reason = ""; |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1234 | WiFiEndpointRefPtr endpoint = |
| 1235 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1236 | service->AddEndpoint(endpoint); |
| 1237 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1238 | .WillRepeatedly(Return(true)); |
| 1239 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1240 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
| 1241 | EXPECT_STREQ("", reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1242 | |
| 1243 | // WiFi only supports connecting to one Service at a time. So, to |
| 1244 | // avoid disrupting connectivity, we only allow auto-connection to |
| 1245 | // a WiFiService when the corresponding WiFi is idle. |
| 1246 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1247 | .WillRepeatedly(Return(false)); |
| 1248 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1249 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 1250 | EXPECT_STREQ(WiFiService::kAutoConnBusy, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | TEST_F(WiFiServiceTest, AutoConnect) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1254 | const char *reason; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1255 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1256 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1257 | EXPECT_CALL(*wifi(), ConnectTo(_, _)) |
| 1258 | .Times(0); |
| 1259 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1260 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1261 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1262 | WiFiEndpointRefPtr endpoint = |
| 1263 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1264 | service->AddEndpoint(endpoint); |
| 1265 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1266 | .WillRepeatedly(Return(true)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1267 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1268 | EXPECT_CALL(*wifi(), ConnectTo(_, _)); |
| 1269 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1270 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 1271 | |
| 1272 | Error error; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 1273 | service->UserInitiatedDisconnect(&error); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1274 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1275 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1278 | TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1279 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1280 | |
| 1281 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1282 | |
| 1283 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1284 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1285 | wifi_service->mutable_store(), |
| 1286 | flimflam::kPassphraseProperty, |
| 1287 | DBusAdaptor::StringToVariant("0:abcde"), |
| 1288 | &error)); |
| 1289 | EXPECT_EQ("0:abcde", wifi_service->passphrase_); |
| 1290 | |
| 1291 | EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(), |
| 1292 | flimflam::kPassphraseProperty, |
| 1293 | &error)); |
| 1294 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1295 | } |
| 1296 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1297 | TEST_F(WiFiServiceTest, SignalToStrength) { |
| 1298 | // Verify that our mapping is sane, in the sense that it preserves ordering. |
| 1299 | // We break the test into two domains, because we assume that positive |
| 1300 | // values aren't actually in dBm. |
| 1301 | for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) { |
| 1302 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1303 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1304 | EXPECT_LE(current_mapped, next_mapped) |
| 1305 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1306 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1307 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1308 | } |
| 1309 | for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) { |
| 1310 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1311 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1312 | EXPECT_LE(current_mapped, next_mapped) |
| 1313 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1314 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1315 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) { |
| 1320 | // If the chosen signal values don't map to distinct strength |
| 1321 | // values, then we can't expect our other tests to pass. So verify |
| 1322 | // their distinctness. |
| 1323 | EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength); |
| 1324 | EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength); |
| 1325 | EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength); |
| 1326 | } |
| 1327 | |
| 1328 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) { |
| 1329 | // Initial endpoint updates values. |
| 1330 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1331 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1332 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1333 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
| 1334 | EXPECT_CALL(adaptor, EmitUint8Changed( |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1335 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1336 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1337 | flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyMode11b)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1338 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1339 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1340 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1341 | |
| 1342 | // Endpoint with stronger signal updates values. |
| 1343 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1344 | flimflam::kWifiFrequency, kGoodEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1345 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1346 | flimflam::kWifiBSsid, kGoodEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1347 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1348 | flimflam::kSignalStrengthProperty, kGoodEndpointStrength)); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1349 | // However, both endpoints are 11b. |
| 1350 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1351 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1352 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1353 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1354 | |
| 1355 | // Endpoint with lower signal does not change values. |
| 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, |
| 1359 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1360 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1361 | service->AddEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1362 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1363 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1364 | |
| 1365 | // Removing non-optimal endpoint does not change values. |
| 1366 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1367 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1368 | EXPECT_CALL(adaptor, |
| 1369 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1370 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1371 | service->RemoveEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1372 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1373 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1374 | |
| 1375 | // Removing optimal endpoint updates values. |
| 1376 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1377 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1378 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1379 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1380 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1381 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1382 | // However, both endpoints are 11b. |
| 1383 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1384 | service->RemoveEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1385 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1386 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1387 | |
| 1388 | // Removing last endpoint updates values (and doesn't crash). |
| 1389 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1390 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1391 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1392 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1393 | flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyModeUndef)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1394 | service->RemoveEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1395 | EXPECT_EQ(0, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1396 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1397 | } |
| 1398 | |
| 1399 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) { |
| 1400 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1401 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1402 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1403 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1404 | service->AddEndpoint(bad_endpoint); |
| 1405 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1406 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1407 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1408 | |
| 1409 | // Setting current endpoint forces adoption of its values, even if it |
| 1410 | // doesn't have the highest signal. |
| 1411 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1412 | flimflam::kWifiFrequency, kBadEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1413 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1414 | flimflam::kWifiBSsid, kBadEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1415 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1416 | flimflam::kSignalStrengthProperty, kBadEndpointStrength)); |
| 1417 | service->NotifyCurrentEndpoint(bad_endpoint); |
| 1418 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1419 | |
| 1420 | // Adding a better endpoint doesn't matter, when current endpoint is set. |
| 1421 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1422 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1423 | EXPECT_CALL(adaptor, |
| 1424 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1425 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1426 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1427 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1428 | |
| 1429 | // Removing a better endpoint doesn't matter, when current endpoint is set. |
| 1430 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1431 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1432 | EXPECT_CALL(adaptor, |
| 1433 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1434 | service->RemoveEndpoint(good_endpoint); |
| 1435 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1436 | |
| 1437 | // Removing the current endpoint is safe and sane. |
| 1438 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1439 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1440 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1441 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1442 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1443 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1444 | service->RemoveEndpoint(bad_endpoint); |
| 1445 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1446 | |
| 1447 | // Clearing the current endpoint (without removing it) is also safe and sane. |
| 1448 | service->NotifyCurrentEndpoint(ok_endpoint); |
| 1449 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1450 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1451 | EXPECT_CALL(adaptor, |
| 1452 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1453 | service->NotifyCurrentEndpoint(NULL); |
| 1454 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1455 | } |
| 1456 | |
| 1457 | TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) { |
| 1458 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1459 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1460 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1461 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1462 | service->AddEndpoint(ok_endpoint); |
| 1463 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1464 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1465 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1466 | |
| 1467 | // Updating sub-optimal Endpoint doesn't update Service. |
| 1468 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1469 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1470 | EXPECT_CALL(adaptor, |
| 1471 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1472 | ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1473 | service->NotifyEndpointUpdated(ok_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1474 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1475 | |
| 1476 | // Updating optimal Endpoint updates appropriate Service property. |
| 1477 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1478 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1479 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1480 | good_endpoint->signal_strength_ = kGoodEndpointSignal + 1; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1481 | service->NotifyEndpointUpdated(good_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1482 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1483 | |
| 1484 | // Change in optimal Endpoint updates Service properties. |
| 1485 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1486 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1487 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1488 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1489 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1490 | ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1491 | service->NotifyEndpointUpdated(ok_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1492 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1493 | } |
| 1494 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 1495 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) { |
| 1496 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1497 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
| 1498 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1499 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1500 | service->AddEndpoint(ok_endpoint); |
| 1501 | EXPECT_FALSE(service->ieee80211w_required()); |
| 1502 | good_endpoint->ieee80211w_required_ = true; |
| 1503 | service->AddEndpoint(good_endpoint); |
| 1504 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1505 | service->RemoveEndpoint(good_endpoint); |
| 1506 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1507 | } |
| 1508 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 1509 | TEST_F(WiFiServiceUpdateFromEndpointsTest, PhysicalMode) { |
| 1510 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1511 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
| 1512 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1513 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1514 | |
| 1515 | // No endpoints -> undef. |
| 1516 | EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode()); |
| 1517 | |
| 1518 | // Endpoint has unknown physical mode -> undef. |
| 1519 | ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyModeUndef; |
| 1520 | service->AddEndpoint(ok_endpoint); |
| 1521 | EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode()); |
| 1522 | |
| 1523 | // New endpoint with 802.11a -> 802.11a. |
| 1524 | good_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a; |
| 1525 | service->AddEndpoint(good_endpoint); |
| 1526 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11a, service->physical_mode()); |
| 1527 | |
| 1528 | // Remove 802.11a endpoint -> undef. |
| 1529 | service->RemoveEndpoint(good_endpoint); |
| 1530 | EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode()); |
| 1531 | |
| 1532 | // Change endpoint -> take endpoint's new value. |
| 1533 | ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11n; |
| 1534 | service->NotifyEndpointUpdated(ok_endpoint); |
| 1535 | EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, service->physical_mode()); |
| 1536 | |
| 1537 | // No endpoints -> undef. |
| 1538 | service->RemoveEndpoint(ok_endpoint); |
| 1539 | EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode()); |
| 1540 | } |
| 1541 | |
Paul Stewart | 8653f46 | 2013-02-06 12:21:05 -0800 | [diff] [blame] | 1542 | TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) { |
| 1543 | service->AddEndpoint(ok_endpoint); |
| 1544 | service->SetState(Service::kStateAssociating); |
| 1545 | ScopedMockLog log; |
| 1546 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 1547 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 1548 | EndsWith("disconnect due to no remaining endpoints."))); |
| 1549 | service->RemoveEndpoint(ok_endpoint); |
| 1550 | } |
| 1551 | |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 1552 | MATCHER_P(IsSetwiseEqual, expected_set, "") { |
| 1553 | set<uint16> arg_set(arg.begin(), arg.end()); |
| 1554 | return arg_set == expected_set; |
| 1555 | } |
| 1556 | |
| 1557 | TEST_F(WiFiServiceUpdateFromEndpointsTest, FrequencyList) { |
| 1558 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1559 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
| 1560 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1561 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1562 | |
| 1563 | // No endpoints -> empty list. |
| 1564 | EXPECT_EQ(vector<uint16>(), service->frequency_list()); |
| 1565 | |
| 1566 | // Add endpoint -> endpoint's frequency in list. |
| 1567 | EXPECT_CALL(adaptor, EmitUint16sChanged( |
| 1568 | kWifiFrequencyListProperty, vector<uint16>{kGoodEndpointFrequency})); |
| 1569 | service->AddEndpoint(good_endpoint); |
| 1570 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1571 | |
| 1572 | // Add another endpoint -> both frequencies in list. |
| 1573 | // Order doesn't matter. |
| 1574 | set<uint16> expected_frequencies{kGoodEndpointFrequency, |
| 1575 | kOkEndpointFrequency}; |
| 1576 | EXPECT_CALL(adaptor, EmitUint16sChanged( |
| 1577 | kWifiFrequencyListProperty, IsSetwiseEqual(expected_frequencies))); |
| 1578 | service->AddEndpoint(ok_endpoint); |
| 1579 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1580 | |
| 1581 | // Remove endpoint -> other endpoint's frequency remains. |
| 1582 | EXPECT_CALL(adaptor, EmitUint16sChanged( |
| 1583 | kWifiFrequencyListProperty, vector<uint16>{kOkEndpointFrequency})); |
| 1584 | service->RemoveEndpoint(good_endpoint); |
| 1585 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1586 | |
| 1587 | // Endpoint with same frequency -> frequency remains. |
| 1588 | // Notification may or may not occur -- don't care. |
| 1589 | // Frequency may or may not be repeated in list -- don't care. |
| 1590 | WiFiEndpointRefPtr same_freq_as_ok_endpoint = MakeOpenEndpoint( |
| 1591 | simple_ssid_string(), "aa:bb:cc:dd:ee:ff", ok_endpoint->frequency(), 0); |
| 1592 | service->AddEndpoint(same_freq_as_ok_endpoint); |
| 1593 | EXPECT_THAT(service->frequency_list(), |
| 1594 | IsSetwiseEqual(set<uint16>{kOkEndpointFrequency})); |
| 1595 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1596 | |
| 1597 | // Remove endpoint with same frequency -> frequency remains. |
| 1598 | // Notification may or may not occur -- don't care. |
| 1599 | service->RemoveEndpoint(ok_endpoint); |
| 1600 | EXPECT_EQ(vector<uint16>{same_freq_as_ok_endpoint->frequency()}, |
| 1601 | service->frequency_list()); |
| 1602 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1603 | |
| 1604 | // Remove last endpoint. Frequency list goes empty. |
| 1605 | EXPECT_CALL(adaptor, EmitUint16sChanged( |
| 1606 | kWifiFrequencyListProperty, vector<uint16>{})); |
| 1607 | service->RemoveEndpoint(same_freq_as_ok_endpoint); |
| 1608 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1609 | } |
| 1610 | |
Paul Stewart | 6df20bd | 2013-03-13 19:31:25 -0700 | [diff] [blame] | 1611 | TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) { |
| 1612 | WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk)); |
| 1613 | EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL)); |
| 1614 | WiFiEndpoint *endpoint = MakeOpenEndpoint( |
| 1615 | simple_ssid_string(), "00:00:00:00:00:00", 0, 0); |
| 1616 | service->AddEndpoint(endpoint); |
| 1617 | EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL)); |
| 1618 | service->NotifyCurrentEndpoint(endpoint); |
| 1619 | EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL)); |
| 1620 | service->NotifyCurrentEndpoint(NULL); |
| 1621 | EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL)); |
| 1622 | } |
| 1623 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1624 | TEST_F(WiFiServiceTest, UpdateSecurity) { |
| 1625 | // Cleartext and pre-shared-key crypto. |
| 1626 | { |
| 1627 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
| 1628 | EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm()); |
| 1629 | EXPECT_FALSE(service->key_rotation()); |
| 1630 | EXPECT_FALSE(service->endpoint_auth()); |
| 1631 | } |
| 1632 | { |
| 1633 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1634 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1635 | EXPECT_FALSE(service->key_rotation()); |
| 1636 | EXPECT_FALSE(service->endpoint_auth()); |
| 1637 | } |
| 1638 | { |
| 1639 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk); |
| 1640 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1641 | EXPECT_TRUE(service->key_rotation()); |
| 1642 | EXPECT_FALSE(service->endpoint_auth()); |
| 1643 | } |
| 1644 | { |
| 1645 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa); |
| 1646 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1647 | EXPECT_TRUE(service->key_rotation()); |
| 1648 | EXPECT_FALSE(service->endpoint_auth()); |
| 1649 | } |
| 1650 | { |
| 1651 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn); |
| 1652 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1653 | EXPECT_TRUE(service->key_rotation()); |
| 1654 | EXPECT_FALSE(service->endpoint_auth()); |
| 1655 | } |
| 1656 | |
| 1657 | // Crypto with 802.1X key management. |
| 1658 | { |
| 1659 | // WEP |
| 1660 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1661 | service->SetEAPKeyManagement("IEEE8021X"); |
| 1662 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1663 | EXPECT_TRUE(service->key_rotation()); |
| 1664 | EXPECT_TRUE(service->endpoint_auth()); |
| 1665 | } |
| 1666 | { |
| 1667 | // WPA |
| 1668 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1669 | WiFiEndpointRefPtr endpoint = |
| 1670 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false); |
| 1671 | service->AddEndpoint(endpoint); |
| 1672 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1673 | EXPECT_TRUE(service->key_rotation()); |
| 1674 | EXPECT_TRUE(service->endpoint_auth()); |
| 1675 | } |
| 1676 | { |
| 1677 | // RSN |
| 1678 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1679 | WiFiEndpointRefPtr endpoint = |
| 1680 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true); |
| 1681 | service->AddEndpoint(endpoint); |
| 1682 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1683 | EXPECT_TRUE(service->key_rotation()); |
| 1684 | EXPECT_TRUE(service->endpoint_auth()); |
| 1685 | } |
| 1686 | { |
| 1687 | // AP supports both WPA and RSN. |
| 1688 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1689 | WiFiEndpointRefPtr endpoint = |
| 1690 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true); |
| 1691 | service->AddEndpoint(endpoint); |
| 1692 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1693 | EXPECT_TRUE(service->key_rotation()); |
| 1694 | EXPECT_TRUE(service->endpoint_auth()); |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | TEST_F(WiFiServiceTest, ComputeCipher8021x) { |
| 1699 | // No endpoints. |
| 1700 | { |
| 1701 | const set<WiFiEndpointConstRefPtr> endpoints; |
| 1702 | EXPECT_EQ(Service::kCryptoNone, |
| 1703 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1704 | } |
| 1705 | |
| 1706 | // Single endpoint, various configs. |
| 1707 | { |
| 1708 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1709 | endpoints.insert( |
| 1710 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1711 | EXPECT_EQ(Service::kCryptoNone, |
| 1712 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1713 | } |
| 1714 | { |
| 1715 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1716 | endpoints.insert( |
| 1717 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1718 | EXPECT_EQ(Service::kCryptoRc4, |
| 1719 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1720 | } |
| 1721 | { |
| 1722 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1723 | endpoints.insert( |
| 1724 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true)); |
| 1725 | EXPECT_EQ(Service::kCryptoAes, |
| 1726 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1727 | } |
| 1728 | { |
| 1729 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1730 | endpoints.insert( |
| 1731 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true)); |
| 1732 | EXPECT_EQ(Service::kCryptoAes, |
| 1733 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1734 | } |
| 1735 | |
| 1736 | // Multiple endpoints. |
| 1737 | { |
| 1738 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1739 | endpoints.insert( |
| 1740 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1741 | endpoints.insert( |
| 1742 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false)); |
| 1743 | EXPECT_EQ(Service::kCryptoNone, |
| 1744 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1745 | } |
| 1746 | { |
| 1747 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1748 | endpoints.insert( |
| 1749 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1750 | endpoints.insert( |
| 1751 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false)); |
| 1752 | EXPECT_EQ(Service::kCryptoNone, |
| 1753 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1754 | } |
| 1755 | { |
| 1756 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1757 | endpoints.insert( |
| 1758 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1759 | endpoints.insert( |
| 1760 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false)); |
| 1761 | EXPECT_EQ(Service::kCryptoRc4, |
| 1762 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1763 | } |
| 1764 | { |
| 1765 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1766 | endpoints.insert( |
| 1767 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1768 | endpoints.insert( |
| 1769 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true)); |
| 1770 | EXPECT_EQ(Service::kCryptoRc4, |
| 1771 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1772 | } |
| 1773 | { |
| 1774 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1775 | endpoints.insert( |
| 1776 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true)); |
| 1777 | endpoints.insert( |
| 1778 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true)); |
| 1779 | EXPECT_EQ(Service::kCryptoAes, |
| 1780 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1781 | } |
| 1782 | { |
| 1783 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1784 | endpoints.insert( |
| 1785 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true)); |
| 1786 | endpoints.insert( |
| 1787 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true)); |
| 1788 | EXPECT_EQ(Service::kCryptoAes, |
| 1789 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1790 | } |
| 1791 | } |
| 1792 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 1793 | TEST_F(WiFiServiceTest, Unload) { |
| 1794 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
| 1795 | EXPECT_CALL(*wifi(), DestroyIPConfigLease(service->GetStorageIdentifier())). |
| 1796 | Times(1); |
| 1797 | service->Unload(); |
| 1798 | } |
| 1799 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1800 | TEST_F(WiFiServiceTest, PropertyChanges) { |
| 1801 | WiFiServiceRefPtr service = MakeServiceWithMockManager(); |
| 1802 | ServiceMockAdaptor *adaptor = GetAdaptor(service); |
| 1803 | TestCommonPropertyChanges(service, adaptor); |
| 1804 | TestAutoConnectPropertyChange(service, adaptor); |
| 1805 | |
| 1806 | EXPECT_CALL(*adaptor, |
| 1807 | EmitRpcIdentifierChanged(flimflam::kDeviceProperty, _)); |
| 1808 | SetWiFi(service, wifi()); |
| 1809 | Mock::VerifyAndClearExpectations(adaptor); |
| 1810 | |
| 1811 | EXPECT_CALL(*adaptor, |
| 1812 | EmitRpcIdentifierChanged(flimflam::kDeviceProperty, _)); |
| 1813 | service->ResetWiFi(); |
| 1814 | Mock::VerifyAndClearExpectations(adaptor); |
| 1815 | } |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 1816 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1817 | // Custom property setters should return false, and make no changes, if |
| 1818 | // the new value is the same as the old value. |
| 1819 | TEST_F(WiFiServiceTest, CustomSetterNoopChange) { |
| 1820 | WiFiServiceRefPtr service = MakeServiceWithMockManager(); |
| 1821 | TestCustomSetterNoopChange(service, mock_manager()); |
| 1822 | } |
| 1823 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1824 | } // namespace shill |