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