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