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 | |
Gaurav Shah | da6218a | 2011-11-11 12:09:33 -0800 | [diff] [blame] | 391 | MATCHER(WPASecurityArgs, "") { |
| 392 | return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) && |
| 393 | ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey); |
| 394 | } |
| 395 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 396 | MATCHER(WPA80211wSecurityArgs, "") { |
| 397 | return ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol) && |
| 398 | ContainsKey(arg, wpa_supplicant::kPropertyPreSharedKey) && |
| 399 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyIeee80211w); |
| 400 | } |
| 401 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 402 | MATCHER(EAPSecurityArgs, "") { |
| 403 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 404 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath); |
| 405 | } |
| 406 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 407 | MATCHER_P(FrequencyArg, has_arg, "") { |
| 408 | return has_arg == |
| 409 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyFrequency); |
| 410 | } |
| 411 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 412 | TEST_F(WiFiServiceTest, ConnectTaskWPA) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 413 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 414 | EXPECT_CALL(*wifi(), |
| 415 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 416 | Error error; |
| 417 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 418 | wifi_service->Connect(NULL); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 419 | } |
| 420 | |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 421 | TEST_F(WiFiServiceTest, ConnectTaskRSN) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 422 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn); |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 423 | EXPECT_CALL(*wifi(), |
| 424 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
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( |
| 434 | new NiceMock<MockProfile>(control_interface(), manager())); |
| 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); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 460 | EXPECT_CALL(*wifi(), |
| 461 | ConnectTo(wifi_service.get(), WPASecurityArgs())); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 462 | Error error; |
| 463 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 464 | wifi_service->Connect(NULL); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 467 | TEST_F(WiFiServiceTest, ConnectTask8021x) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 468 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 469 | Service::EapCredentials eap; |
| 470 | eap.identity = "identity"; |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 471 | eap.password = "mumble"; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 472 | service->set_eap(eap); |
| 473 | EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs())); |
| 474 | service->Connect(NULL); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 477 | TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) { |
| 478 | vector<uint8_t> ssid(1, 'a'); |
| 479 | WiFiEndpointRefPtr endpoint_nofreq = |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 480 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 481 | WiFiEndpointRefPtr endpoint_freq = |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 482 | MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 483 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 484 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 485 | wifi_service->AddEndpoint(endpoint_freq); |
| 486 | EXPECT_CALL(*wifi(), |
| 487 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
| 488 | wifi_service->Connect(NULL); |
| 489 | |
| 490 | wifi_service = new WiFiService(control_interface(), |
| 491 | dispatcher(), |
| 492 | metrics(), |
| 493 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 494 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 495 | ssid, |
| 496 | flimflam::kModeAdhoc, |
| 497 | flimflam::kSecurityNone, |
| 498 | false); |
| 499 | EXPECT_CALL(*wifi(), |
| 500 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 501 | SetWiFiForService(wifi_service, wifi()); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 502 | wifi_service->Connect(NULL); |
| 503 | |
| 504 | wifi_service = new WiFiService(control_interface(), |
| 505 | dispatcher(), |
| 506 | metrics(), |
| 507 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 508 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 509 | ssid, |
| 510 | flimflam::kModeAdhoc, |
| 511 | flimflam::kSecurityNone, |
| 512 | false); |
| 513 | wifi_service->AddEndpoint(endpoint_nofreq); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 514 | SetWiFiForService(wifi_service, wifi()); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 515 | EXPECT_CALL(*wifi(), |
| 516 | ConnectTo(wifi_service.get(), FrequencyArg(false))); |
| 517 | wifi_service->Connect(NULL); |
| 518 | |
| 519 | wifi_service = new WiFiService(control_interface(), |
| 520 | dispatcher(), |
| 521 | metrics(), |
| 522 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 523 | provider(), |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 524 | ssid, |
| 525 | flimflam::kModeAdhoc, |
| 526 | flimflam::kSecurityNone, |
| 527 | false); |
| 528 | wifi_service->AddEndpoint(endpoint_freq); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 529 | SetWiFiForService(wifi_service, wifi()); |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 530 | EXPECT_CALL(*wifi(), |
| 531 | ConnectTo(wifi_service.get(), FrequencyArg(true))); |
| 532 | wifi_service->Connect(NULL); |
| 533 | } |
| 534 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 535 | TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 536 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 537 | WiFiEndpointRefPtr endpoint = |
| 538 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 539 | endpoint->ieee80211w_required_ = true; |
| 540 | wifi_service->AddEndpoint(endpoint); |
| 541 | Error error; |
| 542 | wifi_service->SetPassphrase("0:mumblemumblem", &error); |
| 543 | EXPECT_CALL(*wifi(), |
| 544 | ConnectTo(wifi_service.get(), WPA80211wSecurityArgs())); |
| 545 | wifi_service->Connect(NULL); |
| 546 | } |
| 547 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 548 | MATCHER(WEPSecurityArgsKeyIndex0, "") { |
| 549 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 550 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("0")) && |
| 551 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 552 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 553 | reader().get_uint32() == 0); |
| 554 | } |
| 555 | |
| 556 | MATCHER(WEPSecurityArgsKeyIndex1, "") { |
| 557 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 558 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("1")) && |
| 559 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 560 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 561 | reader().get_uint32() == 1); |
| 562 | } |
| 563 | |
| 564 | MATCHER(WEPSecurityArgsKeyIndex2, "") { |
| 565 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 566 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("2")) && |
| 567 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 568 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 569 | reader().get_uint32() == 2); |
| 570 | } |
| 571 | |
| 572 | MATCHER(WEPSecurityArgsKeyIndex3, "") { |
| 573 | return ContainsKey(arg, wpa_supplicant::kPropertyAuthAlg) && |
| 574 | ContainsKey(arg, wpa_supplicant::kPropertyWEPKey + std::string("3")) && |
| 575 | ContainsKey(arg, wpa_supplicant::kPropertyWEPTxKeyIndex) && |
| 576 | (arg.find(wpa_supplicant::kPropertyWEPTxKeyIndex)->second. |
| 577 | reader().get_uint32() == 3); |
| 578 | } |
| 579 | |
| 580 | TEST_F(WiFiServiceTest, ConnectTaskWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 581 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 582 | Error error; |
| 583 | wifi_service->SetPassphrase("0:abcdefghijklm", &error); |
| 584 | EXPECT_CALL(*wifi(), |
| 585 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 586 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 587 | |
| 588 | wifi_service->SetPassphrase("abcdefghijklm", &error); |
| 589 | EXPECT_CALL(*wifi(), |
| 590 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 591 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 592 | |
| 593 | wifi_service->SetPassphrase("1:abcdefghijklm", &error); |
| 594 | EXPECT_CALL(*wifi(), |
| 595 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 596 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 597 | |
| 598 | wifi_service->SetPassphrase("2:abcdefghijklm", &error); |
| 599 | EXPECT_CALL(*wifi(), |
| 600 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 601 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 602 | |
| 603 | wifi_service->SetPassphrase("3:abcdefghijklm", &error); |
| 604 | EXPECT_CALL(*wifi(), |
| 605 | ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 606 | wifi_service->Connect(NULL); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 609 | |
| 610 | MATCHER(DynamicWEPArgs, "") { |
| 611 | return ContainsKey(arg, wpa_supplicant::kNetworkPropertyEapIdentity) && |
| 612 | ContainsKey(arg, wpa_supplicant::kNetworkPropertyCaPath) && |
| 613 | !ContainsKey(arg, wpa_supplicant::kPropertySecurityProtocol); |
| 614 | } |
| 615 | |
| 616 | // Dynamic WEP + 802.1x. |
| 617 | TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 618 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 619 | |
| 620 | Service::EapCredentials eap; |
| 621 | eap.key_management = "IEEE8021X"; |
| 622 | eap.identity = "something"; |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 623 | eap.password = "mumble"; |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 624 | wifi_service->set_eap(eap); |
| 625 | EXPECT_CALL(*wifi(), |
| 626 | ConnectTo(wifi_service.get(), DynamicWEPArgs())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 627 | wifi_service->Connect(NULL); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 630 | TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) { |
| 631 | vector<uint8_t> ssid(5); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 632 | WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 633 | |
| 634 | const string kPassphrase = "abcdefgh"; |
| 635 | |
| 636 | { |
| 637 | Error error; |
| 638 | // A changed passphrase should trigger cache removal. |
| 639 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 640 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 641 | Mock::VerifyAndClearExpectations(wifi()); |
| 642 | EXPECT_TRUE(error.IsSuccess()); |
| 643 | } |
| 644 | |
| 645 | { |
| 646 | Error error; |
| 647 | // An unchanged passphrase should not trigger cache removal. |
| 648 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 649 | wifi_service->SetPassphrase(kPassphrase, &error); |
| 650 | Mock::VerifyAndClearExpectations(wifi()); |
| 651 | EXPECT_TRUE(error.IsSuccess()); |
| 652 | } |
| 653 | |
| 654 | { |
| 655 | Error error; |
| 656 | // A modified passphrase should trigger cache removal. |
| 657 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 658 | wifi_service->SetPassphrase(kPassphrase + "X", &error); |
| 659 | Mock::VerifyAndClearExpectations(wifi()); |
| 660 | EXPECT_TRUE(error.IsSuccess()); |
| 661 | } |
| 662 | |
| 663 | { |
| 664 | Error error; |
| 665 | // A cleared passphrase should also trigger cache removal. |
| 666 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 667 | wifi_service->ClearPassphrase(&error); |
| 668 | Mock::VerifyAndClearExpectations(wifi()); |
| 669 | EXPECT_TRUE(error.IsSuccess()); |
| 670 | } |
| 671 | |
| 672 | { |
| 673 | Error error; |
| 674 | // An invalid passphrase should not trigger cache removal. |
| 675 | EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0); |
| 676 | wifi_service->SetPassphrase("", &error); |
| 677 | Mock::VerifyAndClearExpectations(wifi()); |
| 678 | EXPECT_FALSE(error.IsSuccess()); |
| 679 | } |
| 680 | |
| 681 | { |
| 682 | // Any change to EAP parameters (including a null one) will trigger cache |
| 683 | // removal. This is a lot less granular than the passphrase checks above. |
| 684 | EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get())); |
| 685 | wifi_service->set_eap(Service::EapCredentials()); |
| 686 | Mock::VerifyAndClearExpectations(wifi()); |
| 687 | } |
| 688 | } |
| 689 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 690 | TEST_F(WiFiServiceTest, LoadHidden) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 691 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 692 | ASSERT_FALSE(service->hidden_ssid_); |
| 693 | NiceMock<MockStore> mock_store; |
| 694 | const string storage_id = service->GetStorageIdentifier(); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 695 | set<string> groups; |
| 696 | groups.insert(storage_id); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 697 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 698 | .WillRepeatedly(Return(true)); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 699 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 700 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 701 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 702 | .WillRepeatedly(Return(groups)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 703 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 704 | .WillRepeatedly(Return(false)); |
| 705 | EXPECT_CALL(mock_store, |
| 706 | GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _)) |
| 707 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true))); |
| 708 | EXPECT_TRUE(service->Load(&mock_store)); |
| 709 | EXPECT_TRUE(service->hidden_ssid_); |
| 710 | } |
| 711 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 712 | TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 713 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 714 | set<string> groups; |
| 715 | groups.insert("id0"); |
| 716 | groups.insert("id1"); |
| 717 | // Make sure we retain the first matched group in the same way that |
| 718 | // WiFiService::Load() will. |
| 719 | string first_group = *groups.begin(); |
| 720 | |
| 721 | NiceMock<MockStore> mock_store; |
| 722 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 723 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 724 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 725 | .WillRepeatedly(Return(groups)); |
| 726 | EXPECT_CALL(mock_store, ContainsGroup(first_group)) |
| 727 | .WillRepeatedly(Return(true)); |
| 728 | EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0); |
| 729 | EXPECT_CALL(mock_store, GetBool(first_group, _, _)) |
| 730 | .WillRepeatedly(Return(false)); |
| 731 | EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0); |
| 732 | ScopedMockLog log; |
| 733 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 734 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 735 | EndsWith("choosing the first."))); |
| 736 | EXPECT_TRUE(service->Load(&mock_store)); |
| 737 | } |
| 738 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 739 | TEST_F(WiFiServiceSecurityTest, WPAMapping) { |
| 740 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn, |
| 741 | flimflam::kSecurityPsk)); |
| 742 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa, |
| 743 | flimflam::kSecurityPsk)); |
| 744 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk, |
| 745 | flimflam::kSecurityPsk)); |
| 746 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep, |
| 747 | flimflam::kSecurityWep)); |
| 748 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone, |
| 749 | flimflam::kSecurityNone)); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 750 | EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x, |
| 751 | flimflam::kSecurity8021x)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | TEST_F(WiFiServiceSecurityTest, LoadMapping) { |
| 755 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 756 | flimflam::kSecurityPsk, |
| 757 | true)); |
| 758 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 759 | flimflam::kSecurityRsn, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 760 | false)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 761 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn, |
| 762 | flimflam::kSecurityWpa, |
| 763 | false)); |
| 764 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 765 | flimflam::kSecurityPsk, |
| 766 | true)); |
| 767 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 768 | flimflam::kSecurityWpa, |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 769 | false)); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 770 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa, |
| 771 | flimflam::kSecurityRsn, |
| 772 | false)); |
| 773 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 774 | flimflam::kSecurityWep, |
| 775 | true)); |
| 776 | EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep, |
| 777 | flimflam::kSecurityPsk, |
| 778 | false)); |
| 779 | } |
| 780 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 781 | TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 782 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 783 | NiceMock<MockStore> mock_store; |
| 784 | const string storage_id = service->GetStorageIdentifier(); |
| 785 | EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id))) |
| 786 | .WillRepeatedly(Return(true)); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 787 | set<string> groups; |
| 788 | groups.insert(storage_id); |
| 789 | EXPECT_CALL(mock_store, GetGroupsWithProperties( |
| 790 | ContainsWiFiProperties( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 791 | simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk))) |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 792 | .WillRepeatedly(Return(groups)); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 793 | EXPECT_CALL(mock_store, GetBool(_, _, _)) |
| 794 | .WillRepeatedly(Return(false)); |
| 795 | const string passphrase = "passphrase"; |
| 796 | EXPECT_CALL(mock_store, |
| 797 | GetCryptedString(StrEq(storage_id), |
| 798 | WiFiService::kStoragePassphrase, _)) |
| 799 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true))); |
| 800 | EXPECT_CALL(mock_store, |
| 801 | GetCryptedString(StrEq(storage_id), |
| 802 | StrNe(WiFiService::kStoragePassphrase), _)) |
| 803 | .WillRepeatedly(Return(false)); |
| 804 | EXPECT_TRUE(service->need_passphrase_); |
| 805 | EXPECT_TRUE(service->Load(&mock_store)); |
| 806 | EXPECT_EQ(passphrase, service->passphrase_); |
| 807 | EXPECT_TRUE(service->connectable()); |
| 808 | EXPECT_FALSE(service->need_passphrase_); |
| 809 | service->Unload(); |
| 810 | EXPECT_EQ(string(""), service->passphrase_); |
| 811 | EXPECT_FALSE(service->connectable()); |
| 812 | EXPECT_TRUE(service->need_passphrase_); |
| 813 | } |
| 814 | |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 815 | TEST_F(WiFiServiceTest, ConfigureMakesConnectable) { |
| 816 | string guid("legit_guid"); |
| 817 | KeyValueStore args; |
| 818 | args.SetString(flimflam::kEapIdentityProperty, "legit_identity"); |
| 819 | args.SetString(flimflam::kEapPasswordProperty, "legit_password"); |
| 820 | args.SetString(flimflam::kEAPEAPProperty, "PEAP"); |
| 821 | args.SetString(flimflam::kGuidProperty, guid); |
| 822 | Error error; |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 823 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 824 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
Christopher Wiley | 27b4723 | 2012-11-02 13:13:00 -0700 | [diff] [blame] | 825 | // Hack the GUID in so that we don't have to mess about with WiFi to regsiter |
| 826 | // our service. This way, Manager will handle the lookup itself. |
| 827 | service->set_guid(guid); |
| 828 | manager()->RegisterService(service); |
| 829 | EXPECT_FALSE(service->connectable()); |
| 830 | EXPECT_EQ(service.get(), manager()->GetService(args, &error).get()); |
| 831 | EXPECT_TRUE(error.IsSuccess()); |
| 832 | EXPECT_TRUE(service->connectable()); |
| 833 | } |
| 834 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 835 | TEST_F(WiFiServiceTest, ConfigurePassphrase) { |
| 836 | EXPECT_EQ(Error::kNotSupported, |
| 837 | TestConfigurePassphrase(flimflam::kSecurityNone, "")); |
| 838 | EXPECT_EQ(Error::kNotSupported, |
| 839 | TestConfigurePassphrase(flimflam::kSecurityNone, "foo")); |
| 840 | EXPECT_EQ(Error::kSuccess, |
| 841 | TestConfigurePassphrase(flimflam::kSecurityWep, NULL)); |
| 842 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 843 | TestConfigurePassphrase(flimflam::kSecurityWep, "")); |
| 844 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 845 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcd")); |
| 846 | EXPECT_EQ(Error::kSuccess, |
| 847 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcde")); |
| 848 | EXPECT_EQ(Error::kSuccess, |
| 849 | TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm")); |
| 850 | EXPECT_EQ(Error::kSuccess, |
| 851 | TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm")); |
| 852 | EXPECT_EQ(Error::kSuccess, |
| 853 | TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405")); |
| 854 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 855 | TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405")); |
| 856 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 857 | TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405")); |
| 858 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 859 | TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405")); |
| 860 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 861 | TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405")); |
| 862 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 863 | TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405")); |
| 864 | EXPECT_EQ(Error::kSuccess, |
| 865 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 866 | "0102030405060708090a0b0c0d")); |
| 867 | EXPECT_EQ(Error::kSuccess, |
| 868 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 869 | "0102030405060708090A0B0C0D")); |
| 870 | EXPECT_EQ(Error::kSuccess, |
| 871 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 872 | "0:0102030405060708090a0b0c0d")); |
| 873 | EXPECT_EQ(Error::kSuccess, |
| 874 | TestConfigurePassphrase(flimflam::kSecurityWep, |
| 875 | "0:0x0102030405060708090a0b0c0d")); |
| 876 | EXPECT_EQ(Error::kSuccess, |
| 877 | TestConfigurePassphrase(flimflam::kSecurityWpa, NULL)); |
| 878 | EXPECT_EQ(Error::kSuccess, |
| 879 | TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password")); |
| 880 | EXPECT_EQ(Error::kInvalidPassphrase, |
| 881 | TestConfigurePassphrase(flimflam::kSecurityWpa, "")); |
| 882 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 883 | flimflam::kSecurityWpa, |
| 884 | string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str())); |
| 885 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 886 | flimflam::kSecurityWpa, |
| 887 | string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str())); |
| 888 | // subtle: invalid length for hex key, but valid as ascii passphrase |
| 889 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 890 | flimflam::kSecurityWpa, |
| 891 | string(IEEE_80211::kWPAHexLen-1, '1').c_str())); |
| 892 | EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase( |
| 893 | flimflam::kSecurityWpa, |
| 894 | string(IEEE_80211::kWPAHexLen, '1').c_str())); |
| 895 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 896 | flimflam::kSecurityWpa, |
| 897 | string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str())); |
| 898 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 899 | flimflam::kSecurityWpa, |
| 900 | string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str())); |
| 901 | EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase( |
| 902 | flimflam::kSecurityWpa, |
| 903 | string(IEEE_80211::kWPAHexLen+1, '1').c_str())); |
| 904 | } |
| 905 | |
| 906 | TEST_F(WiFiServiceTest, ConfigureRedundantProperties) { |
| 907 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
| 908 | KeyValueStore args; |
| 909 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 910 | args.SetString(flimflam::kSSIDProperty, simple_ssid_string()); |
| 911 | args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone); |
| 912 | const string kGUID = "aguid"; |
| 913 | args.SetString(flimflam::kGuidProperty, kGUID); |
| 914 | |
| 915 | EXPECT_EQ("", service->guid()); |
| 916 | Error error; |
| 917 | service->Configure(args, &error); |
| 918 | EXPECT_TRUE(error.IsSuccess()); |
| 919 | EXPECT_EQ(kGUID, service->guid()); |
| 920 | } |
| 921 | |
| 922 | TEST_F(WiFiServiceTest, DisconnectWithWiFi) { |
| 923 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
| 924 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
| 925 | Error error; |
| 926 | service->Disconnect(&error); |
| 927 | } |
| 928 | |
| 929 | TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) { |
| 930 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 931 | EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0); |
| 932 | Error error; |
| 933 | service->Disconnect(&error); |
| 934 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 935 | } |
| 936 | |
| 937 | TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) { |
| 938 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 939 | EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0); |
| 940 | service->SetState(Service::kStateAssociating); |
| 941 | ScopedMockLog log; |
| 942 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 943 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 944 | HasSubstr("WiFi endpoints do not (yet) exist."))); |
| 945 | Error error; |
| 946 | service->Disconnect(&error); |
| 947 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 948 | } |
| 949 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 950 | TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 951 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 952 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 953 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 954 | service->Unload(); |
| 955 | } |
| 956 | |
| 957 | TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 958 | WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 959 | EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1); |
| 960 | EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 961 | service->Unload(); |
| 962 | } |
| 963 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 964 | TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 965 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 966 | const string storage_id = service->GetStorageIdentifier(); |
| 967 | string address; |
| 968 | string mode; |
| 969 | string security; |
| 970 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 971 | &security)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 972 | EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 973 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 974 | EXPECT_EQ(flimflam::kSecurityNone, security); |
| 975 | } |
| 976 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 977 | TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) { |
| 978 | // Do a separate test for 802.1x, since kSecurity8021x contains a "_", |
| 979 | // which needs to be dealt with specially in the parser. |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 980 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 981 | const string storage_id = service->GetStorageIdentifier(); |
| 982 | string address; |
| 983 | string mode; |
| 984 | string security; |
| 985 | EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode, |
| 986 | &security)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 987 | EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 988 | EXPECT_EQ(flimflam::kModeManaged, mode); |
| 989 | EXPECT_EQ(flimflam::kSecurity8021x, security); |
| 990 | } |
| 991 | |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 992 | TEST_F(WiFiServiceFixupStorageTest, FixedEntries) { |
| 993 | const string kNonWiFiId = "vpn_foo"; |
| 994 | const string kUnparsableWiFiId = "wifi_foo"; |
| 995 | |
| 996 | AddGroup(kNonWiFiId); |
| 997 | AddGroup(kUnparsableWiFiId); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 998 | AddServiceEntry(true, true, true, true); |
| 999 | AddServiceEntry(false, false, false, false); |
| 1000 | AddServiceEntry(true, true, true, true); |
| 1001 | AddServiceEntry(false, false, false, false); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1002 | EXPECT_TRUE(FixupServiceEntries()); |
| 1003 | } |
| 1004 | |
| 1005 | TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) { |
| 1006 | const string kNonWiFiId = "vpn_foo"; |
| 1007 | const string kUnparsableWiFiId = "wifi_foo"; |
| 1008 | |
| 1009 | AddGroup(kNonWiFiId); |
| 1010 | AddGroup(kUnparsableWiFiId); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1011 | AddServiceEntry(true, true, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1012 | EXPECT_FALSE(FixupServiceEntries()); |
| 1013 | } |
| 1014 | |
| 1015 | TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1016 | AddServiceEntry(false, true, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1017 | EXPECT_TRUE(FixupServiceEntries()); |
| 1018 | } |
| 1019 | |
| 1020 | TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1021 | AddServiceEntry(true, false, true, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1022 | EXPECT_TRUE(FixupServiceEntries()); |
| 1023 | } |
| 1024 | |
| 1025 | TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1026 | AddServiceEntry(true, true, false, true); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1027 | EXPECT_TRUE(FixupServiceEntries()); |
| 1028 | } |
| 1029 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1030 | TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) { |
| 1031 | AddServiceEntry(true, true, true, false); |
| 1032 | EXPECT_TRUE(FixupServiceEntries()); |
| 1033 | } |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1034 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1035 | TEST_F(WiFiServiceTest, Connectable) { |
| 1036 | // Open network should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1037 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1038 | |
| 1039 | // 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] | 1040 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1041 | |
| 1042 | // WEP network with passphrase set should be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1043 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1044 | |
| 1045 | // WEP network without passphrase set should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1046 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1047 | |
| 1048 | // A bad passphrase should not make a WEP network connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1049 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Similar to WEP, for WPA. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1052 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", NULL)); |
| 1053 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, NULL)); |
| 1054 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", NULL)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Unconfigured 802.1x should NOT be connectable. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1057 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, NULL)); |
| 1058 | |
| 1059 | Service::EapCredentials eap; |
| 1060 | // Empty EAP credentials should not make a 802.1x network connectable. |
| 1061 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 1062 | |
| 1063 | eap.identity = "something"; |
| 1064 | // If client certificate is being used, a private key must exist. |
| 1065 | eap.client_cert = "some client cert"; |
| 1066 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 1067 | eap.private_key = "some private key"; |
| 1068 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 1069 | |
| 1070 | // Identity is always required. |
| 1071 | eap.identity.clear(); |
| 1072 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 1073 | |
| 1074 | eap.identity = "something"; |
| 1075 | // For non EAP-TLS types, a password is required. |
| 1076 | eap.eap = "Non-TLS"; |
| 1077 | EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
| 1078 | eap.password = "some password"; |
| 1079 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, &eap)); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 1080 | // Dynamic WEP + 802.1X should be connectable under the same conditions. |
| 1081 | eap.key_management = "IEEE8021X"; |
| 1082 | EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, &eap)); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1085 | TEST_F(WiFiServiceTest, IsAutoConnectable) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1086 | const char *reason; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1087 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1088 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1089 | .WillRepeatedly(Return(true)); |
| 1090 | EXPECT_FALSE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1091 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 1092 | EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1093 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1094 | reason = ""; |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 1095 | WiFiEndpointRefPtr endpoint = |
| 1096 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1097 | service->AddEndpoint(endpoint); |
| 1098 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1099 | .WillRepeatedly(Return(true)); |
| 1100 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1101 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
| 1102 | EXPECT_STREQ("", reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1103 | |
| 1104 | // WiFi only supports connecting to one Service at a time. So, to |
| 1105 | // avoid disrupting connectivity, we only allow auto-connection to |
| 1106 | // a WiFiService when the corresponding WiFi is idle. |
| 1107 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1108 | .WillRepeatedly(Return(false)); |
| 1109 | EXPECT_TRUE(service->HasEndpoints()); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1110 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
| 1111 | EXPECT_STREQ(WiFiService::kAutoConnBusy, reason); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | TEST_F(WiFiServiceTest, AutoConnect) { |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1115 | const char *reason; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1116 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1117 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1118 | EXPECT_CALL(*wifi(), ConnectTo(_, _)) |
| 1119 | .Times(0); |
| 1120 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1121 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1122 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 1123 | WiFiEndpointRefPtr endpoint = |
| 1124 | MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1125 | service->AddEndpoint(endpoint); |
| 1126 | EXPECT_CALL(*wifi(), IsIdle()) |
| 1127 | .WillRepeatedly(Return(true)); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1128 | EXPECT_TRUE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1129 | EXPECT_CALL(*wifi(), ConnectTo(_, _)); |
| 1130 | service->AutoConnect(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1131 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 1132 | |
| 1133 | Error error; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 1134 | service->UserInitiatedDisconnect(&error); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1135 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1136 | EXPECT_FALSE(service->IsAutoConnectable(&reason)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1139 | TEST_F(WiFiServiceTest, Populate8021x) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1140 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1141 | Service::EapCredentials eap; |
| 1142 | eap.identity = "testidentity"; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1143 | eap.pin = "xxxx"; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1144 | service->set_eap(eap); |
| 1145 | map<string, ::DBus::Variant> params; |
| 1146 | service->Populate8021xProperties(¶ms); |
| 1147 | // Test that only non-empty 802.1x properties are populated. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1148 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapIdentity)); |
| 1149 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 1150 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 1151 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1152 | // Test that CA path is set by default. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1153 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
| 1154 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1155 | // Test that hardware-backed security arguments are not set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1156 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 1157 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 1158 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | TEST_F(WiFiServiceTest, Populate8021xNoSystemCAs) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1162 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1163 | Service::EapCredentials eap; |
| 1164 | eap.identity = "testidentity"; |
| 1165 | eap.use_system_cas = false; |
| 1166 | service->set_eap(eap); |
| 1167 | map<string, ::DBus::Variant> params; |
| 1168 | service->Populate8021xProperties(¶ms); |
| 1169 | // 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] | 1170 | EXPECT_FALSE(ContainsKey(params, wpa_supplicant::kNetworkPropertyCaPath)); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | TEST_F(WiFiServiceTest, Populate8021xUsingHardwareAuth) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1174 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 1175 | Service::EapCredentials eap; |
| 1176 | eap.identity = "testidentity"; |
| 1177 | eap.key_id = "key_id"; |
| 1178 | eap.pin = "xxxx"; |
| 1179 | service->set_eap(eap); |
| 1180 | map<string, ::DBus::Variant> params; |
| 1181 | service->Populate8021xProperties(¶ms); |
| 1182 | // Test that EAP engine parameters set if key_id is set. |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1183 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapPin)); |
| 1184 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapKeyId)); |
| 1185 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngine)); |
| 1186 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEngineId)); |
| 1187 | } |
| 1188 | |
| 1189 | TEST_F(WiFiServiceTest, Populate8021xNSS) { |
| 1190 | vector<uint8_t> ssid(1, 'a'); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1191 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 1192 | Service::EapCredentials eap; |
| 1193 | eap.ca_cert_nss = "nss_nickname"; |
| 1194 | service->set_eap(eap); |
| 1195 | MockNSS nss; |
| 1196 | service->nss_ = &nss; |
| 1197 | |
| 1198 | const string kNSSCertfile("/tmp/nss-cert"); |
| 1199 | FilePath nss_cert(kNSSCertfile); |
| 1200 | vector<char> ssid_in_chars(ssid.begin(), ssid.end()); |
| 1201 | EXPECT_CALL(nss, GetDERCertfile(eap.ca_cert_nss, ssid_in_chars)) |
| 1202 | .WillOnce(Return(nss_cert)); |
| 1203 | |
| 1204 | map<string, ::DBus::Variant> params; |
| 1205 | service->Populate8021xProperties(¶ms); |
| 1206 | EXPECT_TRUE(ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)); |
| 1207 | if (ContainsKey(params, wpa_supplicant::kNetworkPropertyEapCaCert)) { |
| 1208 | EXPECT_EQ(kNSSCertfile, params[wpa_supplicant::kNetworkPropertyEapCaCert] |
| 1209 | .reader().get_string()); |
| 1210 | } |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1213 | TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1214 | WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1215 | |
| 1216 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1217 | |
| 1218 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1219 | EXPECT_TRUE(DBusAdaptor::SetProperty( |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 1220 | wifi_service->mutable_store(), |
| 1221 | flimflam::kPassphraseProperty, |
| 1222 | DBusAdaptor::StringToVariant("0:abcde"), |
| 1223 | &error)); |
| 1224 | EXPECT_EQ("0:abcde", wifi_service->passphrase_); |
| 1225 | |
| 1226 | EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(), |
| 1227 | flimflam::kPassphraseProperty, |
| 1228 | &error)); |
| 1229 | EXPECT_EQ("", wifi_service->passphrase_); |
| 1230 | } |
| 1231 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1232 | TEST_F(WiFiServiceTest, SignalToStrength) { |
| 1233 | // Verify that our mapping is sane, in the sense that it preserves ordering. |
| 1234 | // We break the test into two domains, because we assume that positive |
| 1235 | // values aren't actually in dBm. |
| 1236 | for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) { |
| 1237 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1238 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1239 | EXPECT_LE(current_mapped, next_mapped) |
| 1240 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1241 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1242 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1243 | } |
| 1244 | for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) { |
| 1245 | int16 current_mapped = WiFiService::SignalToStrength(i); |
| 1246 | int16 next_mapped = WiFiService::SignalToStrength(i+1); |
| 1247 | EXPECT_LE(current_mapped, next_mapped) |
| 1248 | << "(original values " << i << " " << i+1 << ")"; |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1249 | EXPECT_GE(current_mapped, Service::kStrengthMin); |
| 1250 | EXPECT_LE(current_mapped, Service::kStrengthMax); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) { |
| 1255 | // If the chosen signal values don't map to distinct strength |
| 1256 | // values, then we can't expect our other tests to pass. So verify |
| 1257 | // their distinctness. |
| 1258 | EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength); |
| 1259 | EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength); |
| 1260 | EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength); |
| 1261 | } |
| 1262 | |
| 1263 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) { |
| 1264 | // Initial endpoint updates values. |
| 1265 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1266 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1267 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1268 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
| 1269 | EXPECT_CALL(adaptor, EmitUint8Changed( |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1270 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1271 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1272 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1273 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1274 | |
| 1275 | // Endpoint with stronger signal updates values. |
| 1276 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1277 | flimflam::kWifiFrequency, kGoodEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1278 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1279 | flimflam::kWifiBSsid, kGoodEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1280 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1281 | flimflam::kSignalStrengthProperty, kGoodEndpointStrength)); |
| 1282 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1283 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1284 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1285 | |
| 1286 | // Endpoint with lower signal does not change values. |
| 1287 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1288 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1289 | EXPECT_CALL(adaptor, |
| 1290 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1291 | service->AddEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1292 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1293 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1294 | |
| 1295 | // Removing non-optimal endpoint does not change values. |
| 1296 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1297 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1298 | EXPECT_CALL(adaptor, |
| 1299 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1300 | service->RemoveEndpoint(bad_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1301 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1302 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1303 | |
| 1304 | // Removing optimal endpoint updates values. |
| 1305 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1306 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1307 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1308 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1309 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1310 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1311 | service->RemoveEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1312 | EXPECT_EQ(1, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1313 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1314 | |
| 1315 | // Removing last endpoint updates values (and doesn't crash). |
| 1316 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1317 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1318 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1319 | service->RemoveEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1320 | EXPECT_EQ(0, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1321 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1322 | } |
| 1323 | |
| 1324 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) { |
| 1325 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1326 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1327 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1328 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1329 | service->AddEndpoint(bad_endpoint); |
| 1330 | service->AddEndpoint(ok_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1331 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1332 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1333 | |
| 1334 | // Setting current endpoint forces adoption of its values, even if it |
| 1335 | // doesn't have the highest signal. |
| 1336 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1337 | flimflam::kWifiFrequency, kBadEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1338 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1339 | flimflam::kWifiBSsid, kBadEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1340 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1341 | flimflam::kSignalStrengthProperty, kBadEndpointStrength)); |
| 1342 | service->NotifyCurrentEndpoint(bad_endpoint); |
| 1343 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1344 | |
| 1345 | // Adding a better endpoint doesn't matter, when current endpoint is set. |
| 1346 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1347 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1348 | EXPECT_CALL(adaptor, |
| 1349 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1350 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1351 | EXPECT_EQ(3, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1352 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1353 | |
| 1354 | // Removing a better endpoint doesn't matter, when current endpoint is set. |
| 1355 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1356 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1357 | EXPECT_CALL(adaptor, |
| 1358 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1359 | service->RemoveEndpoint(good_endpoint); |
| 1360 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1361 | |
| 1362 | // Removing the current endpoint is safe and sane. |
| 1363 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1364 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1365 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1366 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1367 | EXPECT_CALL(adaptor, EmitUint8Changed( |
| 1368 | flimflam::kSignalStrengthProperty, kOkEndpointStrength)); |
| 1369 | service->RemoveEndpoint(bad_endpoint); |
| 1370 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1371 | |
| 1372 | // Clearing the current endpoint (without removing it) is also safe and sane. |
| 1373 | service->NotifyCurrentEndpoint(ok_endpoint); |
| 1374 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1375 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1376 | EXPECT_CALL(adaptor, |
| 1377 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1378 | service->NotifyCurrentEndpoint(NULL); |
| 1379 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1380 | } |
| 1381 | |
| 1382 | TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) { |
| 1383 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1384 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1385 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1386 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1387 | service->AddEndpoint(ok_endpoint); |
| 1388 | service->AddEndpoint(good_endpoint); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 1389 | EXPECT_EQ(2, service->GetEndpointCount()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1390 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1391 | |
| 1392 | // Updating sub-optimal Endpoint doesn't update Service. |
| 1393 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1394 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1395 | EXPECT_CALL(adaptor, |
| 1396 | EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0); |
| 1397 | ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1398 | service->NotifyEndpointUpdated(ok_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1399 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1400 | |
| 1401 | // Updating optimal Endpoint updates appropriate Service property. |
| 1402 | EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1403 | EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1404 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1405 | good_endpoint->signal_strength_ = kGoodEndpointSignal + 1; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1406 | service->NotifyEndpointUpdated(good_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1407 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1408 | |
| 1409 | // Change in optimal Endpoint updates Service properties. |
| 1410 | EXPECT_CALL(adaptor, EmitUint16Changed( |
| 1411 | flimflam::kWifiFrequency, kOkEndpointFrequency)); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 1412 | EXPECT_CALL(adaptor, EmitStringChanged( |
| 1413 | flimflam::kWifiBSsid, kOkEndpointBssId)); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1414 | EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _)); |
| 1415 | ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1416 | service->NotifyEndpointUpdated(ok_endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1417 | Mock::VerifyAndClearExpectations(&adaptor); |
| 1418 | } |
| 1419 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 1420 | TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) { |
| 1421 | EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber()); |
| 1422 | EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber()); |
| 1423 | EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber()); |
| 1424 | EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber()); |
| 1425 | service->AddEndpoint(ok_endpoint); |
| 1426 | EXPECT_FALSE(service->ieee80211w_required()); |
| 1427 | good_endpoint->ieee80211w_required_ = true; |
| 1428 | service->AddEndpoint(good_endpoint); |
| 1429 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1430 | service->RemoveEndpoint(good_endpoint); |
| 1431 | EXPECT_TRUE(service->ieee80211w_required()); |
| 1432 | } |
| 1433 | |
Paul Stewart | 8653f46 | 2013-02-06 12:21:05 -0800 | [diff] [blame] | 1434 | TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) { |
| 1435 | service->AddEndpoint(ok_endpoint); |
| 1436 | service->SetState(Service::kStateAssociating); |
| 1437 | ScopedMockLog log; |
| 1438 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 1439 | EXPECT_CALL(log, Log(logging::LOG_WARNING, _, |
| 1440 | EndsWith("disconnect due to no remaining endpoints."))); |
| 1441 | service->RemoveEndpoint(ok_endpoint); |
| 1442 | } |
| 1443 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame^] | 1444 | TEST_F(WiFiServiceTest, UpdateSecurity) { |
| 1445 | // Cleartext and pre-shared-key crypto. |
| 1446 | { |
| 1447 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone); |
| 1448 | EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm()); |
| 1449 | EXPECT_FALSE(service->key_rotation()); |
| 1450 | EXPECT_FALSE(service->endpoint_auth()); |
| 1451 | } |
| 1452 | { |
| 1453 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1454 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1455 | EXPECT_FALSE(service->key_rotation()); |
| 1456 | EXPECT_FALSE(service->endpoint_auth()); |
| 1457 | } |
| 1458 | { |
| 1459 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk); |
| 1460 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1461 | EXPECT_TRUE(service->key_rotation()); |
| 1462 | EXPECT_FALSE(service->endpoint_auth()); |
| 1463 | } |
| 1464 | { |
| 1465 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa); |
| 1466 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1467 | EXPECT_TRUE(service->key_rotation()); |
| 1468 | EXPECT_FALSE(service->endpoint_auth()); |
| 1469 | } |
| 1470 | { |
| 1471 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn); |
| 1472 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1473 | EXPECT_TRUE(service->key_rotation()); |
| 1474 | EXPECT_FALSE(service->endpoint_auth()); |
| 1475 | } |
| 1476 | |
| 1477 | // Crypto with 802.1X key management. |
| 1478 | { |
| 1479 | // WEP |
| 1480 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep); |
| 1481 | service->SetEAPKeyManagement("IEEE8021X"); |
| 1482 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1483 | EXPECT_TRUE(service->key_rotation()); |
| 1484 | EXPECT_TRUE(service->endpoint_auth()); |
| 1485 | } |
| 1486 | { |
| 1487 | // WPA |
| 1488 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1489 | WiFiEndpointRefPtr endpoint = |
| 1490 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false); |
| 1491 | service->AddEndpoint(endpoint); |
| 1492 | EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm()); |
| 1493 | EXPECT_TRUE(service->key_rotation()); |
| 1494 | EXPECT_TRUE(service->endpoint_auth()); |
| 1495 | } |
| 1496 | { |
| 1497 | // RSN |
| 1498 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1499 | WiFiEndpointRefPtr endpoint = |
| 1500 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true); |
| 1501 | service->AddEndpoint(endpoint); |
| 1502 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1503 | EXPECT_TRUE(service->key_rotation()); |
| 1504 | EXPECT_TRUE(service->endpoint_auth()); |
| 1505 | } |
| 1506 | { |
| 1507 | // AP supports both WPA and RSN. |
| 1508 | WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x); |
| 1509 | WiFiEndpointRefPtr endpoint = |
| 1510 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true); |
| 1511 | service->AddEndpoint(endpoint); |
| 1512 | EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm()); |
| 1513 | EXPECT_TRUE(service->key_rotation()); |
| 1514 | EXPECT_TRUE(service->endpoint_auth()); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | TEST_F(WiFiServiceTest, ComputeCipher8021x) { |
| 1519 | // No endpoints. |
| 1520 | { |
| 1521 | const set<WiFiEndpointConstRefPtr> endpoints; |
| 1522 | EXPECT_EQ(Service::kCryptoNone, |
| 1523 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1524 | } |
| 1525 | |
| 1526 | // Single endpoint, various configs. |
| 1527 | { |
| 1528 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1529 | endpoints.insert( |
| 1530 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1531 | EXPECT_EQ(Service::kCryptoNone, |
| 1532 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1533 | } |
| 1534 | { |
| 1535 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1536 | endpoints.insert( |
| 1537 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1538 | EXPECT_EQ(Service::kCryptoRc4, |
| 1539 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1540 | } |
| 1541 | { |
| 1542 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1543 | endpoints.insert( |
| 1544 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true)); |
| 1545 | EXPECT_EQ(Service::kCryptoAes, |
| 1546 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1547 | } |
| 1548 | { |
| 1549 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1550 | endpoints.insert( |
| 1551 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true)); |
| 1552 | EXPECT_EQ(Service::kCryptoAes, |
| 1553 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1554 | } |
| 1555 | |
| 1556 | // Multiple endpoints. |
| 1557 | { |
| 1558 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1559 | endpoints.insert( |
| 1560 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1561 | endpoints.insert( |
| 1562 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false)); |
| 1563 | EXPECT_EQ(Service::kCryptoNone, |
| 1564 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1565 | } |
| 1566 | { |
| 1567 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1568 | endpoints.insert( |
| 1569 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false)); |
| 1570 | endpoints.insert( |
| 1571 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false)); |
| 1572 | EXPECT_EQ(Service::kCryptoNone, |
| 1573 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1574 | } |
| 1575 | { |
| 1576 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1577 | endpoints.insert( |
| 1578 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1579 | endpoints.insert( |
| 1580 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false)); |
| 1581 | EXPECT_EQ(Service::kCryptoRc4, |
| 1582 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1583 | } |
| 1584 | { |
| 1585 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1586 | endpoints.insert( |
| 1587 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false)); |
| 1588 | endpoints.insert( |
| 1589 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true)); |
| 1590 | EXPECT_EQ(Service::kCryptoRc4, |
| 1591 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1592 | } |
| 1593 | { |
| 1594 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1595 | endpoints.insert( |
| 1596 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true)); |
| 1597 | endpoints.insert( |
| 1598 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true)); |
| 1599 | EXPECT_EQ(Service::kCryptoAes, |
| 1600 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1601 | } |
| 1602 | { |
| 1603 | set<WiFiEndpointConstRefPtr> endpoints; |
| 1604 | endpoints.insert( |
| 1605 | MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true)); |
| 1606 | endpoints.insert( |
| 1607 | MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true)); |
| 1608 | EXPECT_EQ(Service::kCryptoAes, |
| 1609 | WiFiService::ComputeCipher8021x(endpoints)); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1613 | } // namespace shill |