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