blob: bd89b810d63f02d7810be2a436ed36a39f44d20b [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone34af2182011-08-22 11:59:36 -07002// 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 agrawald835b202011-10-07 15:26:47 -07007#include <map>
Paul Stewart85aea152013-01-22 09:31:56 -08008#include <set>
Chris Masone34af2182011-08-22 11:59:36 -07009#include <string>
10#include <vector>
11
Paul Stewart85aea152013-01-22 09:31:56 -080012#include <base/stringprintf.h>
Paul Stewart71a4d3b2013-01-18 18:12:56 -080013#include <base/string_number_conversions.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <base/string_util.h>
15#include <chromeos/dbus/service_constants.h>
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Chris Masone34af2182011-08-22 11:59:36 -070020#include "shill/manager.h"
mukesh agrawalf6b32092013-04-10 15:49:55 -070021#include "shill/metrics.h"
Chris Masone34af2182011-08-22 11:59:36 -070022#include "shill/mock_adaptors.h"
Paul Stewart5baebb72013-03-14 11:43:29 -070023#include "shill/mock_certificate_file.h"
Chris Masone34af2182011-08-22 11:59:36 -070024#include "shill/mock_control.h"
Paul Stewartc43cbbe2013-04-11 06:29:30 -070025#include "shill/mock_eap_credentials.h"
Paul Stewart71a4d3b2013-01-18 18:12:56 -080026#include "shill/mock_log.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070027#include "shill/mock_nss.h"
Christopher Wiley1ce658d2012-10-10 10:02:03 -070028#include "shill/mock_profile.h"
Chris Masone34af2182011-08-22 11:59:36 -070029#include "shill/mock_service.h"
30#include "shill/mock_store.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070031#include "shill/mock_wifi.h"
Paul Stewart3c504012013-01-17 17:49:58 -080032#include "shill/mock_wifi_provider.h"
Chris Masone34af2182011-08-22 11:59:36 -070033#include "shill/property_store_unittest.h"
mukesh agrawal8a3188d2011-12-01 20:56:44 +000034#include "shill/refptr_types.h"
35#include "shill/wifi_endpoint.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070036#include "shill/wpa_supplicant.h"
Chris Masone34af2182011-08-22 11:59:36 -070037
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080038using base::FilePath;
mukesh agrawald835b202011-10-07 15:26:47 -070039using std::map;
Paul Stewart85aea152013-01-22 09:31:56 -080040using std::set;
Chris Masone34af2182011-08-22 11:59:36 -070041using std::string;
42using std::vector;
Paul Stewartd08f4432011-11-04 07:48:20 -070043using ::testing::_;
mukesh agrawale1d90e92012-02-15 17:36:08 -080044using ::testing::AnyNumber;
Paul Stewartd08f4432011-11-04 07:48:20 -070045using ::testing::DoAll;
Paul Stewart71a4d3b2013-01-18 18:12:56 -080046using ::testing::EndsWith;
Paul Stewart3c504012013-01-17 17:49:58 -080047using ::testing::HasSubstr;
mukesh agrawale1d90e92012-02-15 17:36:08 -080048using ::testing::Mock;
mukesh agrawal6e277772011-09-29 15:04:23 -070049using ::testing::NiceMock;
Paul Stewartd08f4432011-11-04 07:48:20 -070050using ::testing::Return;
Paul Stewartc43cbbe2013-04-11 06:29:30 -070051using ::testing::ReturnRef;
Paul Stewartd08f4432011-11-04 07:48:20 -070052using ::testing::SetArgumentPointee;
53using ::testing::StrEq;
Paul Stewartd8ad3c42012-01-09 12:39:38 -080054using ::testing::StrNe;
Paul Stewart85aea152013-01-22 09:31:56 -080055using ::testing::StrictMock;
Chris Masone34af2182011-08-22 11:59:36 -070056
mukesh agrawalb20776f2012-02-10 16:00:36 -080057namespace shill {
58
Chris Masone34af2182011-08-22 11:59:36 -070059class WiFiServiceTest : public PropertyStoreTest {
60 public:
Paul Stewart3c504012013-01-17 17:49:58 -080061 WiFiServiceTest()
62 : wifi_(new NiceMock<MockWiFi>(
63 control_interface(),
64 dispatcher(),
65 metrics(),
66 manager(),
67 "wifi",
68 fake_mac,
69 0)),
70 simple_ssid_(1, 'a'),
71 simple_ssid_string_("a") {}
Chris Masone34af2182011-08-22 11:59:36 -070072 virtual ~WiFiServiceTest() {}
mukesh agrawal6e277772011-09-29 15:04:23 -070073
74 protected:
75 static const char fake_mac[];
mukesh agrawale1d90e92012-02-15 17:36:08 -080076
Paul Stewartc43cbbe2013-04-11 06:29:30 -070077 MockEapCredentials *SetMockEap(
78 const WiFiServiceRefPtr &service) {
79 MockEapCredentials *eap = new MockEapCredentials();
80 service->eap_.reset(eap); // Passes ownership.
81 return eap;
82 }
Gaurav Shah10109f22011-11-11 20:16:22 -080083 bool CheckConnectable(const std::string &security, const char *passphrase,
Paul Stewartc43cbbe2013-04-11 06:29:30 -070084 bool is_1x_connectable) {
mukesh agrawal29c13a12011-11-24 00:09:19 +000085 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -080086 WiFiServiceRefPtr service = MakeSimpleService(security);
mukesh agrawal29c13a12011-11-24 00:09:19 +000087 if (passphrase)
88 service->SetPassphrase(passphrase, &error);
Paul Stewartc43cbbe2013-04-11 06:29:30 -070089 MockEapCredentials *eap = SetMockEap(service);
90 EXPECT_CALL(*eap, IsConnectable())
91 .WillRepeatedly(Return(is_1x_connectable));
92 const string kKeyManagement8021x(WPASupplicant::kKeyManagementIeee8021X);
93 if (security == flimflam::kSecurityWep && is_1x_connectable) {
94 EXPECT_CALL(*eap, key_management())
95 .WillRepeatedly(ReturnRef(kKeyManagement8021x));
Gaurav Shah10109f22011-11-11 20:16:22 -080096 }
Paul Stewartc43cbbe2013-04-11 06:29:30 -070097 service->OnEapCredentialsChanged();
mukesh agrawal29c13a12011-11-24 00:09:19 +000098 return service->connectable();
99 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800100 WiFiEndpoint *MakeEndpoint(const string &ssid, const string &bssid,
mukesh agrawal43970a22013-02-15 16:00:07 -0800101 uint16 frequency, int16 signal_dbm,
102 bool has_wpa_property, bool has_rsn_property) {
103 return WiFiEndpoint::MakeEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -0700104 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
mukesh agrawal43970a22013-02-15 16:00:07 -0800105 frequency, signal_dbm, has_wpa_property, has_rsn_property);
106 }
107 WiFiEndpoint *MakeOpenEndpoint(const string &ssid, const string &bssid,
108 uint16 frequency, int16 signal_dbm) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800109 return WiFiEndpoint::MakeOpenEndpoint(
Paul Stewart0654ece2013-03-26 15:21:26 -0700110 NULL, wifi(), ssid, bssid, WPASupplicant::kNetworkModeInfrastructure,
Paul Stewart3c504012013-01-17 17:49:58 -0800111 frequency, signal_dbm);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800112 }
Paul Stewart3c504012013-01-17 17:49:58 -0800113 WiFiServiceRefPtr MakeSimpleService(const string &security) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800114 return new WiFiService(control_interface(),
115 dispatcher(),
116 metrics(),
117 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800118 &provider_,
119 simple_ssid_,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800120 flimflam::kModeManaged,
Paul Stewart3c504012013-01-17 17:49:58 -0800121 security,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800122 false);
123 }
Paul Stewart3c504012013-01-17 17:49:58 -0800124 WiFiServiceRefPtr MakeGenericService() {
125 return MakeSimpleService(flimflam::kSecurityWep);
126 }
127 void SetWiFiForService(WiFiServiceRefPtr service, WiFiRefPtr wifi) {
128 service->wifi_ = wifi;
129 }
130 WiFiServiceRefPtr MakeServiceWithWiFi(const string &security) {
131 WiFiServiceRefPtr service = MakeSimpleService(security);
132 SetWiFiForService(service, wifi_);
133 return service;
134 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800135 ServiceMockAdaptor *GetAdaptor(WiFiService *service) {
136 return dynamic_cast<ServiceMockAdaptor *>(service->adaptor());
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000137 }
Paul Stewart3c504012013-01-17 17:49:58 -0800138 Error::Type TestConfigurePassphrase(const string &security,
139 const char *passphrase) {
140 WiFiServiceRefPtr service = MakeSimpleService(security);
141 KeyValueStore args;
142 if (passphrase) {
143 args.SetString(flimflam::kPassphraseProperty, passphrase);
144 }
145 Error error;
146 service->Configure(args, &error);
147 return error.type();
148 }
mukesh agrawal6e277772011-09-29 15:04:23 -0700149 scoped_refptr<MockWiFi> wifi() { return wifi_; }
Paul Stewart3c504012013-01-17 17:49:58 -0800150 MockWiFiProvider *provider() { return &provider_; }
151 string GetAnyDeviceAddress() { return WiFiService::kAnyDeviceAddress; }
152 const vector<uint8_t> &simple_ssid() { return simple_ssid_; }
153 const string &simple_ssid_string() { return simple_ssid_string_; }
mukesh agrawal6e277772011-09-29 15:04:23 -0700154
155 private:
156 scoped_refptr<MockWiFi> wifi_;
Paul Stewart3c504012013-01-17 17:49:58 -0800157 MockWiFiProvider provider_;
158 const vector<uint8_t> simple_ssid_;
159 const string simple_ssid_string_;
Chris Masone34af2182011-08-22 11:59:36 -0700160};
161
mukesh agrawal6e277772011-09-29 15:04:23 -0700162// static
163const char WiFiServiceTest::fake_mac[] = "AaBBcCDDeeFF";
164
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800165MATCHER_P3(ContainsWiFiProperties, ssid, mode, security, "") {
166 string hex_ssid = base::HexEncode(ssid.data(), ssid.size());
167 return
168 arg.ContainsString(WiFiService::kStorageType) &&
169 arg.GetString(WiFiService::kStorageType) == flimflam::kTypeWifi &&
170 arg.ContainsString(WiFiService::kStorageSSID) &&
171 arg.GetString(WiFiService::kStorageSSID) == hex_ssid &&
172 arg.ContainsString(WiFiService::kStorageMode) &&
173 arg.GetString(WiFiService::kStorageMode) == mode &&
174 arg.ContainsString(WiFiService::kStorageSecurityClass) &&
175 arg.GetString(WiFiService::kStorageSecurityClass) == security;
176}
177
Paul Stewartd08f4432011-11-04 07:48:20 -0700178class WiFiServiceSecurityTest : public WiFiServiceTest {
179 public:
Paul Stewartd08f4432011-11-04 07:48:20 -0700180 bool TestStorageSecurityIs(WiFiServiceRefPtr wifi_service,
181 const string &security) {
182 string id = wifi_service->GetStorageIdentifier();
Paul Stewart3c504012013-01-17 17:49:58 -0800183 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Paul Stewartd08f4432011-11-04 07:48:20 -0700184 EXPECT_NE(mac_pos, string::npos);
185 size_t mode_pos = id.find(string(flimflam::kModeManaged), mac_pos);
186 EXPECT_NE(mode_pos, string::npos);
187 return id.find(string(security), mode_pos) != string::npos;
188 }
189
190 // Test that a service that is created with security |from_security|
191 // gets by default a storage identifier with |to_security| as its
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800192 // security component, and that when saved, it sets the Security
193 // property in to |to_security| as well.
Paul Stewartd08f4432011-11-04 07:48:20 -0700194 bool TestStorageMapping(const string &from_security,
195 const string &to_security) {
Paul Stewart3c504012013-01-17 17:49:58 -0800196 WiFiServiceRefPtr wifi_service = MakeSimpleService(from_security);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800197 NiceMock<MockStore> mock_store;
198 EXPECT_CALL(mock_store, SetString(_, _, _)).WillRepeatedly(Return(true));
199 EXPECT_CALL(mock_store,
200 SetString(_, WiFiService::kStorageSecurity, from_security))
201 .Times(1);
202 EXPECT_CALL(mock_store,
203 SetString(_, WiFiService::kStorageSecurityClass, to_security))
204 .Times(1);
205 wifi_service->Save(&mock_store);
Paul Stewartd08f4432011-11-04 07:48:20 -0700206 return TestStorageSecurityIs(wifi_service, to_security);
207 }
208
209 // Test whether a service of type |service_security| can load from a
210 // storage interface containing an entry for |storage_security|.
211 // Make sure the result meets |expectation|. If |expectation| is
212 // true, also make sure the service storage identifier changes to
213 // match |storage_security|.
214 bool TestLoadMapping(const string &service_security,
215 const string &storage_security,
216 bool expectation) {
Paul Stewart3c504012013-01-17 17:49:58 -0800217 WiFiServiceRefPtr wifi_service = MakeSimpleService(service_security);
Paul Stewartd08f4432011-11-04 07:48:20 -0700218 NiceMock<MockStore> mock_store;
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800219 EXPECT_CALL(mock_store, GetGroupsWithProperties(_))
220 .WillRepeatedly(Return(set<string>()));
221 const string kStorageId = "storage_id";
222 EXPECT_CALL(mock_store, ContainsGroup(kStorageId))
Paul Stewartd08f4432011-11-04 07:48:20 -0700223 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800224 set<string> groups;
225 groups.insert(kStorageId);
226 EXPECT_CALL(mock_store, GetGroupsWithProperties(
227 ContainsWiFiProperties(wifi_service->ssid(),
228 flimflam::kModeManaged,
229 storage_security)))
230 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700231 bool is_loadable = wifi_service->IsLoadableFrom(&mock_store);
232 EXPECT_EQ(expectation, is_loadable);
233 bool is_loaded = wifi_service->Load(&mock_store);
234 EXPECT_EQ(expectation, is_loaded);
235
236 if (expectation != is_loadable || expectation != is_loaded) {
237 return false;
238 } else if (!expectation) {
239 return true;
240 } else {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800241 return wifi_service->GetStorageIdentifier() == kStorageId;
Paul Stewartd08f4432011-11-04 07:48:20 -0700242 }
243 }
244};
245
mukesh agrawale1d90e92012-02-15 17:36:08 -0800246class WiFiServiceUpdateFromEndpointsTest : public WiFiServiceTest {
247 public:
248 WiFiServiceUpdateFromEndpointsTest()
249 : kOkEndpointStrength(WiFiService::SignalToStrength(kOkEndpointSignal)),
250 kBadEndpointStrength(WiFiService::SignalToStrength(kBadEndpointSignal)),
251 kGoodEndpointStrength(
252 WiFiService::SignalToStrength(kGoodEndpointSignal)),
253 service(MakeGenericService()),
254 adaptor(*GetAdaptor(service)) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800255 ok_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800256 simple_ssid_string(), kOkEndpointBssId, kOkEndpointFrequency,
257 kOkEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800258 good_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800259 simple_ssid_string(), kGoodEndpointBssId, kGoodEndpointFrequency,
260 kGoodEndpointSignal);
mukesh agrawal43970a22013-02-15 16:00:07 -0800261 bad_endpoint = MakeOpenEndpoint(
Paul Stewart3c504012013-01-17 17:49:58 -0800262 simple_ssid_string(), kBadEndpointBssId, kBadEndpointFrequency,
263 kBadEndpointSignal);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800264 }
265
266 protected:
267 static const uint16 kOkEndpointFrequency = 2422;
268 static const uint16 kBadEndpointFrequency = 2417;
269 static const uint16 kGoodEndpointFrequency = 2412;
270 static const int16 kOkEndpointSignal = -50;
271 static const int16 kBadEndpointSignal = -75;
272 static const int16 kGoodEndpointSignal = -25;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700273 static const char *kOkEndpointBssId;
274 static const char *kGoodEndpointBssId;
275 static const char *kBadEndpointBssId;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800276 // Can't be both static and const (because initialization requires a
277 // function call). So choose to be just const.
278 const uint8 kOkEndpointStrength;
279 const uint8 kBadEndpointStrength;
280 const uint8 kGoodEndpointStrength;
281 WiFiEndpointRefPtr ok_endpoint;
282 WiFiEndpointRefPtr bad_endpoint;
283 WiFiEndpointRefPtr good_endpoint;
284 WiFiServiceRefPtr service;
285 ServiceMockAdaptor &adaptor;
286};
287
mukesh agrawal923f14f2012-06-04 16:46:08 -0700288const char *WiFiServiceUpdateFromEndpointsTest::kOkEndpointBssId =
289 "00:00:00:00:00:01";
290const char *WiFiServiceUpdateFromEndpointsTest::kGoodEndpointBssId =
291 "00:00:00:00:00:02";
292const char *WiFiServiceUpdateFromEndpointsTest::kBadEndpointBssId =
293 "00:00:00:00:00:03";
mukesh agrawale1d90e92012-02-15 17:36:08 -0800294
Paul Stewart85aea152013-01-22 09:31:56 -0800295class WiFiServiceFixupStorageTest : public WiFiServiceTest {
296 protected:
297 void AddGroup(string group_name) {
298 groups_.insert(group_name);
299 }
300
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800301 void AddServiceEntry(bool has_type, bool has_mode, bool has_security,
302 bool has_security_class) {
Paul Stewart85aea152013-01-22 09:31:56 -0800303 int index = groups_.size();
304 string id = base::StringPrintf("%s_%d_%d_%s_%s", flimflam::kTypeWifi,
305 index, index, flimflam::kModeManaged,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800306 flimflam::kSecurityWpa);
Paul Stewart85aea152013-01-22 09:31:56 -0800307 AddGroup(id);
308 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageType, _))
309 .WillOnce(Return(has_type));
310 if (!has_type) {
311 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageType,
312 flimflam::kTypeWifi));
313 }
314 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageMode, _))
315 .WillOnce(Return(has_mode));
316 if (!has_mode) {
317 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageMode,
318 flimflam::kModeManaged));
319 }
320 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurity, _))
321 .WillOnce(Return(has_security));
322 if (!has_security) {
323 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurity,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800324 flimflam::kSecurityWpa));
325 }
326 EXPECT_CALL(store_, GetString(id, WiFiService::kStorageSecurityClass, _))
327 .WillOnce(Return(has_security_class));
328 if (!has_security_class) {
329 EXPECT_CALL(store_, SetString(id, WiFiService::kStorageSecurityClass,
330 flimflam::kSecurityPsk));
Paul Stewart85aea152013-01-22 09:31:56 -0800331 }
332 }
333
334 bool FixupServiceEntries() {
335 EXPECT_CALL(store_, GetGroups()).WillOnce(Return(groups_));
336 return WiFiService::FixupServiceEntries(&store_);
337 }
338
339 private:
340 StrictMock<MockStore> store_;
341 set<string> groups_;
342};
343
Chris Masone34af2182011-08-22 11:59:36 -0700344TEST_F(WiFiServiceTest, StorageId) {
Paul Stewart3c504012013-01-17 17:49:58 -0800345 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Chris Masone9d779932011-08-25 16:33:41 -0700346 string id = wifi_service->GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700347 for (uint i = 0; i < id.length(); ++i) {
348 EXPECT_TRUE(id[i] == '_' ||
349 isxdigit(id[i]) ||
350 (isalpha(id[i]) && islower(id[i])));
351 }
Paul Stewart3c504012013-01-17 17:49:58 -0800352 size_t mac_pos = id.find(StringToLowerASCII(GetAnyDeviceAddress()));
Chris Masone34af2182011-08-22 11:59:36 -0700353 EXPECT_NE(mac_pos, string::npos);
354 EXPECT_NE(id.find(string(flimflam::kModeManaged), mac_pos), string::npos);
355}
356
Gaurav Shahda6218a2011-11-11 12:09:33 -0800357// Make sure the passphrase is registered as a write only property
358// by reading and comparing all string properties returned on the store.
359TEST_F(WiFiServiceTest, PassphraseWriteOnly) {
Paul Stewart3c504012013-01-17 17:49:58 -0800360 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWpa);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800361 ReadablePropertyConstIterator<string> it =
362 (wifi_service->store()).GetStringPropertiesIter();
363 for( ; !it.AtEnd(); it.Advance())
364 EXPECT_NE(it.Key(), flimflam::kPassphraseProperty);
365}
366
Thieu Lef7709452011-11-15 01:13:19 +0000367// Make sure setting the passphrase via D-Bus Service.SetProperty validates
368// the passphrase.
369TEST_F(WiFiServiceTest, PassphraseSetPropertyValidation) {
370 // We only spot check two password cases here to make sure the
371 // SetProperty code path does validation. We're not going to exhaustively
372 // test for all types of passwords.
Paul Stewart3c504012013-01-17 17:49:58 -0800373 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
Thieu Lef7709452011-11-15 01:13:19 +0000374 Error error;
375 EXPECT_TRUE(wifi_service->mutable_store()->SetStringProperty(
376 flimflam::kPassphraseProperty, "0:abcde", &error));
377 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
378 flimflam::kPassphraseProperty, "invalid", &error));
379 EXPECT_EQ(Error::kInvalidPassphrase, error.type());
380}
381
382TEST_F(WiFiServiceTest, PassphraseSetPropertyOpenNetwork) {
Paul Stewart3c504012013-01-17 17:49:58 -0800383 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityNone);
Thieu Lef7709452011-11-15 01:13:19 +0000384 Error error;
385 EXPECT_FALSE(wifi_service->mutable_store()->SetStringProperty(
386 flimflam::kPassphraseProperty, "invalid", &error));
387 EXPECT_EQ(Error::kNotSupported, error.type());
388}
389
mukesh agrawald835b202011-10-07 15:26:47 -0700390TEST_F(WiFiServiceTest, NonUTF8SSID) {
391 vector<uint8_t> ssid;
392
393 ssid.push_back(0xff); // not a valid UTF-8 byte-sequence
394 WiFiServiceRefPtr wifi_service = new WiFiService(control_interface(),
395 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800396 metrics(),
mukesh agrawald835b202011-10-07 15:26:47 -0700397 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800398 provider(),
mukesh agrawald835b202011-10-07 15:26:47 -0700399 ssid,
400 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -0800401 flimflam::kSecurityNone,
402 false);
mukesh agrawald835b202011-10-07 15:26:47 -0700403 map<string, ::DBus::Variant> properties;
404 // if service doesn't propertly sanitize SSID, this will generate SIGABRT.
405 DBusAdaptor::GetProperties(wifi_service->store(), &properties, NULL);
406}
407
Paul Stewart4108db92013-03-11 12:13:24 -0700408MATCHER(PSKSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700409 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
410 arg.find(WPASupplicant::kPropertySecurityProtocol)->second.
Paul Stewart4108db92013-03-11 12:13:24 -0700411 reader().get_string() == string("WPA RSN") &&
Paul Stewart0654ece2013-03-26 15:21:26 -0700412 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey);
Gaurav Shahda6218a2011-11-11 12:09:33 -0800413}
414
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800415MATCHER(WPA80211wSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700416 return ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol) &&
417 ContainsKey(arg, WPASupplicant::kPropertyPreSharedKey) &&
418 ContainsKey(arg, WPASupplicant::kNetworkPropertyIeee80211w);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800419}
420
Gaurav Shah10109f22011-11-11 20:16:22 -0800421MATCHER(EAPSecurityArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700422 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
423 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath);
Gaurav Shah10109f22011-11-11 20:16:22 -0800424}
425
Paul Stewarte2d7c502012-07-16 16:35:10 -0700426MATCHER_P(FrequencyArg, has_arg, "") {
427 return has_arg ==
Paul Stewart0654ece2013-03-26 15:21:26 -0700428 ContainsKey(arg, WPASupplicant::kNetworkPropertyFrequency);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700429}
430
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700431TEST_F(WiFiServiceTest, ConnectTaskWPA) {
Paul Stewart3c504012013-01-17 17:49:58 -0800432 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWpa);
Paul Stewart4108db92013-03-11 12:13:24 -0700433 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700434 Error error;
435 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700436 wifi_service->Connect(NULL, "in test");
mukesh agrawal6e277772011-09-29 15:04:23 -0700437}
438
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700439TEST_F(WiFiServiceTest, ConnectTaskRSN) {
Paul Stewart3c504012013-01-17 17:49:58 -0800440 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart4108db92013-03-11 12:13:24 -0700441 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700442 Error error;
443 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700444 wifi_service->Connect(NULL, "in test");
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700445}
446
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700447TEST_F(WiFiServiceTest, ConnectConditions) {
448 Error error;
Paul Stewart3c504012013-01-17 17:49:58 -0800449 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700450 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800451 new NiceMock<MockProfile>(control_interface(), metrics(), manager()));
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700452 wifi_service->set_profile(mock_profile);
453 // With nothing else going on, the service should attempt to connect.
454 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700455 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700456 Mock::VerifyAndClearExpectations(wifi());
457
458 // But if we're already "connecting" or "connected" then we shouldn't attempt
459 // again.
460 EXPECT_CALL(*wifi(),
461 ConnectTo(wifi_service.get(), _)).Times(0);
462 wifi_service->SetState(Service::kStateAssociating);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700463 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700464 wifi_service->SetState(Service::kStateConfiguring);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700465 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700466 wifi_service->SetState(Service::kStateConnected);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700467 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700468 wifi_service->SetState(Service::kStatePortal);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700469 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700470 wifi_service->SetState(Service::kStateOnline);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700471 wifi_service->Connect(&error, "in test");
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700472 Mock::VerifyAndClearExpectations(wifi());
473}
474
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800475TEST_F(WiFiServiceTest, ConnectTaskPSK) {
Paul Stewart3c504012013-01-17 17:49:58 -0800476 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
Paul Stewart4108db92013-03-11 12:13:24 -0700477 EXPECT_CALL(*wifi(), ConnectTo(wifi_service.get(), PSKSecurityArgs()));
Wade Guthrie005bd342012-05-02 09:37:07 -0700478 Error error;
479 wifi_service->SetPassphrase("0:mumblemumblem", &error);
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700480 wifi_service->Connect(NULL, "in test");
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800481}
482
Gaurav Shah10109f22011-11-11 20:16:22 -0800483TEST_F(WiFiServiceTest, ConnectTask8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -0800484 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700485 service->mutable_eap()->set_identity("identity");
486 service->mutable_eap()->set_password("mumble");
487 service->OnEapCredentialsChanged();
Paul Stewart3c504012013-01-17 17:49:58 -0800488 EXPECT_CALL(*wifi(), ConnectTo(service.get(), EAPSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700489 service->Connect(NULL, "in test");
Gaurav Shah10109f22011-11-11 20:16:22 -0800490}
491
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700492TEST_F(WiFiServiceTest, ConnectTask8021xWithMockEap) {
493 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
494 MockEapCredentials *eap = SetMockEap(service);
495 EXPECT_CALL(*eap, IsConnectable()).WillOnce(Return(true));
496 service->OnEapCredentialsChanged();
497 EXPECT_CALL(*eap, PopulateSupplicantProperties(_, _, _, _));
498 // The mocked function does not actually set EAP parameters so we cannot
499 // expect them to be set.
500 EXPECT_CALL(*wifi(), ConnectTo(service.get(), _));
501 service->Connect(NULL, "in test");
502}
503
Paul Stewarte2d7c502012-07-16 16:35:10 -0700504TEST_F(WiFiServiceTest, ConnectTaskAdHocFrequency) {
505 vector<uint8_t> ssid(1, 'a');
506 WiFiEndpointRefPtr endpoint_nofreq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800507 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700508 WiFiEndpointRefPtr endpoint_freq =
mukesh agrawal43970a22013-02-15 16:00:07 -0800509 MakeOpenEndpoint("a", "00:00:00:00:00:02", 2412, 0);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700510
Paul Stewart3c504012013-01-17 17:49:58 -0800511 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewarte2d7c502012-07-16 16:35:10 -0700512 wifi_service->AddEndpoint(endpoint_freq);
513 EXPECT_CALL(*wifi(),
514 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700515 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700516
517 wifi_service = new WiFiService(control_interface(),
518 dispatcher(),
519 metrics(),
520 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800521 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700522 ssid,
523 flimflam::kModeAdhoc,
524 flimflam::kSecurityNone,
525 false);
526 EXPECT_CALL(*wifi(),
527 ConnectTo(wifi_service.get(), FrequencyArg(false)));
Paul Stewart3c504012013-01-17 17:49:58 -0800528 SetWiFiForService(wifi_service, wifi());
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700529 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700530
531 wifi_service = new WiFiService(control_interface(),
532 dispatcher(),
533 metrics(),
534 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800535 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700536 ssid,
537 flimflam::kModeAdhoc,
538 flimflam::kSecurityNone,
539 false);
540 wifi_service->AddEndpoint(endpoint_nofreq);
Paul Stewart3c504012013-01-17 17:49:58 -0800541 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700542 EXPECT_CALL(*wifi(),
543 ConnectTo(wifi_service.get(), FrequencyArg(false)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700544 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700545
546 wifi_service = new WiFiService(control_interface(),
547 dispatcher(),
548 metrics(),
549 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -0800550 provider(),
Paul Stewarte2d7c502012-07-16 16:35:10 -0700551 ssid,
552 flimflam::kModeAdhoc,
553 flimflam::kSecurityNone,
554 false);
555 wifi_service->AddEndpoint(endpoint_freq);
Paul Stewart3c504012013-01-17 17:49:58 -0800556 SetWiFiForService(wifi_service, wifi());
Paul Stewarte2d7c502012-07-16 16:35:10 -0700557 EXPECT_CALL(*wifi(),
558 ConnectTo(wifi_service.get(), FrequencyArg(true)));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700559 wifi_service->Connect(NULL, "in test");
Paul Stewarte2d7c502012-07-16 16:35:10 -0700560}
561
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800562TEST_F(WiFiServiceTest, ConnectTaskWPA80211w) {
Paul Stewart3c504012013-01-17 17:49:58 -0800563 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityPsk);
mukesh agrawal43970a22013-02-15 16:00:07 -0800564 WiFiEndpointRefPtr endpoint =
565 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800566 endpoint->ieee80211w_required_ = true;
567 wifi_service->AddEndpoint(endpoint);
568 Error error;
569 wifi_service->SetPassphrase("0:mumblemumblem", &error);
570 EXPECT_CALL(*wifi(),
571 ConnectTo(wifi_service.get(), WPA80211wSecurityArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700572 wifi_service->Connect(NULL, "in test");
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800573}
574
Thieu Lef4cbda92011-11-10 23:41:24 +0000575MATCHER(WEPSecurityArgsKeyIndex0, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700576 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
577 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("0")) &&
578 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
579 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000580 reader().get_uint32() == 0);
581}
582
583MATCHER(WEPSecurityArgsKeyIndex1, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700584 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
585 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("1")) &&
586 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
587 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000588 reader().get_uint32() == 1);
589}
590
591MATCHER(WEPSecurityArgsKeyIndex2, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700592 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
593 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("2")) &&
594 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
595 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000596 reader().get_uint32() == 2);
597}
598
599MATCHER(WEPSecurityArgsKeyIndex3, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700600 return ContainsKey(arg, WPASupplicant::kPropertyAuthAlg) &&
601 ContainsKey(arg, WPASupplicant::kPropertyWEPKey + std::string("3")) &&
602 ContainsKey(arg, WPASupplicant::kPropertyWEPTxKeyIndex) &&
603 (arg.find(WPASupplicant::kPropertyWEPTxKeyIndex)->second.
Thieu Lef4cbda92011-11-10 23:41:24 +0000604 reader().get_uint32() == 3);
605}
606
607TEST_F(WiFiServiceTest, ConnectTaskWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800608 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Thieu Lef4cbda92011-11-10 23:41:24 +0000609 Error error;
610 wifi_service->SetPassphrase("0:abcdefghijklm", &error);
611 EXPECT_CALL(*wifi(),
612 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700613 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000614
615 wifi_service->SetPassphrase("abcdefghijklm", &error);
616 EXPECT_CALL(*wifi(),
617 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex0()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700618 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000619
620 wifi_service->SetPassphrase("1:abcdefghijklm", &error);
621 EXPECT_CALL(*wifi(),
622 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex1()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700623 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000624
625 wifi_service->SetPassphrase("2:abcdefghijklm", &error);
626 EXPECT_CALL(*wifi(),
627 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex2()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700628 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000629
630 wifi_service->SetPassphrase("3:abcdefghijklm", &error);
631 EXPECT_CALL(*wifi(),
632 ConnectTo(wifi_service.get(), WEPSecurityArgsKeyIndex3()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700633 wifi_service->Connect(NULL, "in test");
Thieu Lef4cbda92011-11-10 23:41:24 +0000634}
635
Gaurav Shah29d68882012-01-30 19:06:42 -0800636
637MATCHER(DynamicWEPArgs, "") {
Paul Stewart0654ece2013-03-26 15:21:26 -0700638 return ContainsKey(arg, WPASupplicant::kNetworkPropertyEapIdentity) &&
639 ContainsKey(arg, WPASupplicant::kNetworkPropertyCaPath) &&
640 !ContainsKey(arg, WPASupplicant::kPropertySecurityProtocol);
Gaurav Shah29d68882012-01-30 19:06:42 -0800641}
642
643// Dynamic WEP + 802.1x.
644TEST_F(WiFiServiceTest, ConnectTaskDynamicWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -0800645 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Gaurav Shah29d68882012-01-30 19:06:42 -0800646
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700647 wifi_service->mutable_eap()->SetKeyManagement("IEEE8021X", NULL);
648 wifi_service->mutable_eap()->set_identity("something");
649 wifi_service->mutable_eap()->set_password("mumble");
650 wifi_service->OnEapCredentialsChanged();
Gaurav Shah29d68882012-01-30 19:06:42 -0800651 EXPECT_CALL(*wifi(),
652 ConnectTo(wifi_service.get(), DynamicWEPArgs()));
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700653 wifi_service->Connect(NULL, "in test");
Gaurav Shah29d68882012-01-30 19:06:42 -0800654}
655
Paul Stewart835934a2012-12-06 19:27:09 -0800656TEST_F(WiFiServiceTest, SetPassphraseRemovesCachedCredentials) {
657 vector<uint8_t> ssid(5);
Paul Stewart3c504012013-01-17 17:49:58 -0800658 WiFiServiceRefPtr wifi_service = MakeServiceWithWiFi(flimflam::kSecurityRsn);
Paul Stewart835934a2012-12-06 19:27:09 -0800659
660 const string kPassphrase = "abcdefgh";
661
662 {
663 Error error;
664 // A changed passphrase should trigger cache removal.
665 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
666 wifi_service->SetPassphrase(kPassphrase, &error);
667 Mock::VerifyAndClearExpectations(wifi());
668 EXPECT_TRUE(error.IsSuccess());
669 }
670
671 {
672 Error error;
673 // An unchanged passphrase should not trigger cache removal.
674 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
675 wifi_service->SetPassphrase(kPassphrase, &error);
676 Mock::VerifyAndClearExpectations(wifi());
677 EXPECT_TRUE(error.IsSuccess());
678 }
679
680 {
681 Error error;
682 // A modified passphrase should trigger cache removal.
683 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
684 wifi_service->SetPassphrase(kPassphrase + "X", &error);
685 Mock::VerifyAndClearExpectations(wifi());
686 EXPECT_TRUE(error.IsSuccess());
687 }
688
689 {
690 Error error;
691 // A cleared passphrase should also trigger cache removal.
692 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
693 wifi_service->ClearPassphrase(&error);
694 Mock::VerifyAndClearExpectations(wifi());
695 EXPECT_TRUE(error.IsSuccess());
696 }
697
698 {
699 Error error;
700 // An invalid passphrase should not trigger cache removal.
701 EXPECT_CALL(*wifi(), ClearCachedCredentials(_)).Times(0);
702 wifi_service->SetPassphrase("", &error);
703 Mock::VerifyAndClearExpectations(wifi());
704 EXPECT_FALSE(error.IsSuccess());
705 }
706
707 {
708 // Any change to EAP parameters (including a null one) will trigger cache
709 // removal. This is a lot less granular than the passphrase checks above.
710 EXPECT_CALL(*wifi(), ClearCachedCredentials(wifi_service.get()));
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700711 wifi_service->OnEapCredentialsChanged();
Paul Stewart835934a2012-12-06 19:27:09 -0800712 Mock::VerifyAndClearExpectations(wifi());
713 }
714}
715
Paul Stewartd08f4432011-11-04 07:48:20 -0700716TEST_F(WiFiServiceTest, LoadHidden) {
Paul Stewart3c504012013-01-17 17:49:58 -0800717 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewartd08f4432011-11-04 07:48:20 -0700718 ASSERT_FALSE(service->hidden_ssid_);
719 NiceMock<MockStore> mock_store;
720 const string storage_id = service->GetStorageIdentifier();
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800721 set<string> groups;
722 groups.insert(storage_id);
Paul Stewartd08f4432011-11-04 07:48:20 -0700723 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
724 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800725 EXPECT_CALL(mock_store, GetGroupsWithProperties(
726 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800727 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800728 .WillRepeatedly(Return(groups));
Paul Stewartd08f4432011-11-04 07:48:20 -0700729 EXPECT_CALL(mock_store, GetBool(_, _, _))
730 .WillRepeatedly(Return(false));
731 EXPECT_CALL(mock_store,
732 GetBool(StrEq(storage_id), WiFiService::kStorageHiddenSSID, _))
733 .WillRepeatedly(DoAll(SetArgumentPointee<2>(true), Return(true)));
734 EXPECT_TRUE(service->Load(&mock_store));
735 EXPECT_TRUE(service->hidden_ssid_);
736}
737
Paul Stewartfa013ab2013-04-11 07:12:03 -0700738TEST_F(WiFiServiceTest, LoadPassphraseForNonPassphraseService) {
739 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
740 NiceMock<MockStore> mock_store;
741 const string storage_id = service->GetStorageIdentifier();
742 set<string> groups;
743 groups.insert(storage_id);
744 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
745 .WillRepeatedly(Return(true));
746 EXPECT_CALL(mock_store, GetGroupsWithProperties(
747 ContainsWiFiProperties(
748 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
749 .WillRepeatedly(Return(groups));
750 EXPECT_CALL(mock_store,
751 GetCryptedString(StrEq(storage_id),
752 StrNe(WiFiService::kStoragePassphrase), _))
753 .WillRepeatedly(Return(false));
754 EXPECT_CALL(mock_store,
755 GetCryptedString(StrEq(storage_id),
756 WiFiService::kStoragePassphrase, _))
757 .WillOnce(DoAll(SetArgumentPointee<2>(string("password")), Return(true)))
758 .WillOnce(DoAll(SetArgumentPointee<2>(string()), Return(true)));
759 ScopedMockLog log;
760 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
761 EndsWith("Passphrase could not be set: "
762 "org.chromium.flimflam.Error.NotSupported")))
763 .Times(1);
764 EXPECT_TRUE(service->Load(&mock_store));
765 Mock::VerifyAndClearExpectations(&log);
766 EXPECT_CALL(log, Log(logging::LOG_ERROR, _, _)).Times(0);
767 EXPECT_TRUE(service->Load(&mock_store));
768}
769
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800770TEST_F(WiFiServiceTest, LoadMultipleMatchingGroups) {
Paul Stewart3c504012013-01-17 17:49:58 -0800771 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800772 set<string> groups;
773 groups.insert("id0");
774 groups.insert("id1");
775 // Make sure we retain the first matched group in the same way that
776 // WiFiService::Load() will.
777 string first_group = *groups.begin();
778
779 NiceMock<MockStore> mock_store;
780 EXPECT_CALL(mock_store, GetGroupsWithProperties(
781 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800782 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityNone)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800783 .WillRepeatedly(Return(groups));
784 EXPECT_CALL(mock_store, ContainsGroup(first_group))
785 .WillRepeatedly(Return(true));
786 EXPECT_CALL(mock_store, ContainsGroup(StrNe(first_group))).Times(0);
787 EXPECT_CALL(mock_store, GetBool(first_group, _, _))
788 .WillRepeatedly(Return(false));
789 EXPECT_CALL(mock_store, GetBool(StrNe(first_group), _, _)).Times(0);
790 ScopedMockLog log;
791 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
792 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
793 EndsWith("choosing the first.")));
794 EXPECT_TRUE(service->Load(&mock_store));
795}
796
Paul Stewartd08f4432011-11-04 07:48:20 -0700797TEST_F(WiFiServiceSecurityTest, WPAMapping) {
798 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityRsn,
799 flimflam::kSecurityPsk));
800 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWpa,
801 flimflam::kSecurityPsk));
802 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityPsk,
803 flimflam::kSecurityPsk));
804 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityWep,
805 flimflam::kSecurityWep));
806 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurityNone,
807 flimflam::kSecurityNone));
Gaurav Shah10109f22011-11-11 20:16:22 -0800808 EXPECT_TRUE(TestStorageMapping(flimflam::kSecurity8021x,
809 flimflam::kSecurity8021x));
Paul Stewartd08f4432011-11-04 07:48:20 -0700810}
811
812TEST_F(WiFiServiceSecurityTest, LoadMapping) {
813 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
814 flimflam::kSecurityPsk,
815 true));
816 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
817 flimflam::kSecurityRsn,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800818 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700819 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityRsn,
820 flimflam::kSecurityWpa,
821 false));
822 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
823 flimflam::kSecurityPsk,
824 true));
825 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
826 flimflam::kSecurityWpa,
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800827 false));
Paul Stewartd08f4432011-11-04 07:48:20 -0700828 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWpa,
829 flimflam::kSecurityRsn,
830 false));
831 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
832 flimflam::kSecurityWep,
833 true));
834 EXPECT_TRUE(TestLoadMapping(flimflam::kSecurityWep,
835 flimflam::kSecurityPsk,
836 false));
837}
838
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800839TEST_F(WiFiServiceTest, LoadAndUnloadPassphrase) {
Paul Stewart3c504012013-01-17 17:49:58 -0800840 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800841 NiceMock<MockStore> mock_store;
842 const string storage_id = service->GetStorageIdentifier();
843 EXPECT_CALL(mock_store, ContainsGroup(StrEq(storage_id)))
844 .WillRepeatedly(Return(true));
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800845 set<string> groups;
846 groups.insert(storage_id);
847 EXPECT_CALL(mock_store, GetGroupsWithProperties(
848 ContainsWiFiProperties(
Paul Stewart3c504012013-01-17 17:49:58 -0800849 simple_ssid(), flimflam::kModeManaged, flimflam::kSecurityPsk)))
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800850 .WillRepeatedly(Return(groups));
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800851 EXPECT_CALL(mock_store, GetBool(_, _, _))
852 .WillRepeatedly(Return(false));
853 const string passphrase = "passphrase";
854 EXPECT_CALL(mock_store,
855 GetCryptedString(StrEq(storage_id),
856 WiFiService::kStoragePassphrase, _))
857 .WillRepeatedly(DoAll(SetArgumentPointee<2>(passphrase), Return(true)));
858 EXPECT_CALL(mock_store,
859 GetCryptedString(StrEq(storage_id),
860 StrNe(WiFiService::kStoragePassphrase), _))
861 .WillRepeatedly(Return(false));
862 EXPECT_TRUE(service->need_passphrase_);
863 EXPECT_TRUE(service->Load(&mock_store));
864 EXPECT_EQ(passphrase, service->passphrase_);
865 EXPECT_TRUE(service->connectable());
866 EXPECT_FALSE(service->need_passphrase_);
867 service->Unload();
868 EXPECT_EQ(string(""), service->passphrase_);
869 EXPECT_FALSE(service->connectable());
870 EXPECT_TRUE(service->need_passphrase_);
871}
872
Christopher Wiley27b47232012-11-02 13:13:00 -0700873TEST_F(WiFiServiceTest, ConfigureMakesConnectable) {
874 string guid("legit_guid");
875 KeyValueStore args;
876 args.SetString(flimflam::kEapIdentityProperty, "legit_identity");
877 args.SetString(flimflam::kEapPasswordProperty, "legit_password");
878 args.SetString(flimflam::kEAPEAPProperty, "PEAP");
879 args.SetString(flimflam::kGuidProperty, guid);
880 Error error;
Christopher Wiley27b47232012-11-02 13:13:00 -0700881
Paul Stewart3c504012013-01-17 17:49:58 -0800882 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Christopher Wiley27b47232012-11-02 13:13:00 -0700883 // Hack the GUID in so that we don't have to mess about with WiFi to regsiter
884 // our service. This way, Manager will handle the lookup itself.
885 service->set_guid(guid);
886 manager()->RegisterService(service);
887 EXPECT_FALSE(service->connectable());
888 EXPECT_EQ(service.get(), manager()->GetService(args, &error).get());
889 EXPECT_TRUE(error.IsSuccess());
890 EXPECT_TRUE(service->connectable());
891}
892
Paul Stewart3c504012013-01-17 17:49:58 -0800893TEST_F(WiFiServiceTest, ConfigurePassphrase) {
894 EXPECT_EQ(Error::kNotSupported,
895 TestConfigurePassphrase(flimflam::kSecurityNone, ""));
896 EXPECT_EQ(Error::kNotSupported,
897 TestConfigurePassphrase(flimflam::kSecurityNone, "foo"));
898 EXPECT_EQ(Error::kSuccess,
899 TestConfigurePassphrase(flimflam::kSecurityWep, NULL));
900 EXPECT_EQ(Error::kInvalidPassphrase,
901 TestConfigurePassphrase(flimflam::kSecurityWep, ""));
902 EXPECT_EQ(Error::kInvalidPassphrase,
903 TestConfigurePassphrase(flimflam::kSecurityWep, "abcd"));
904 EXPECT_EQ(Error::kSuccess,
905 TestConfigurePassphrase(flimflam::kSecurityWep, "abcde"));
906 EXPECT_EQ(Error::kSuccess,
907 TestConfigurePassphrase(flimflam::kSecurityWep, "abcdefghijklm"));
908 EXPECT_EQ(Error::kSuccess,
909 TestConfigurePassphrase(flimflam::kSecurityWep, "0:abcdefghijklm"));
910 EXPECT_EQ(Error::kSuccess,
911 TestConfigurePassphrase(flimflam::kSecurityWep, "0102030405"));
912 EXPECT_EQ(Error::kInvalidPassphrase,
913 TestConfigurePassphrase(flimflam::kSecurityWep, "0x0102030405"));
914 EXPECT_EQ(Error::kInvalidPassphrase,
915 TestConfigurePassphrase(flimflam::kSecurityWep, "O102030405"));
916 EXPECT_EQ(Error::kInvalidPassphrase,
917 TestConfigurePassphrase(flimflam::kSecurityWep, "1:O102030405"));
918 EXPECT_EQ(Error::kInvalidPassphrase,
919 TestConfigurePassphrase(flimflam::kSecurityWep, "1:0xO102030405"));
920 EXPECT_EQ(Error::kInvalidPassphrase,
921 TestConfigurePassphrase(flimflam::kSecurityWep, "0xO102030405"));
922 EXPECT_EQ(Error::kSuccess,
923 TestConfigurePassphrase(flimflam::kSecurityWep,
924 "0102030405060708090a0b0c0d"));
925 EXPECT_EQ(Error::kSuccess,
926 TestConfigurePassphrase(flimflam::kSecurityWep,
927 "0102030405060708090A0B0C0D"));
928 EXPECT_EQ(Error::kSuccess,
929 TestConfigurePassphrase(flimflam::kSecurityWep,
930 "0:0102030405060708090a0b0c0d"));
931 EXPECT_EQ(Error::kSuccess,
932 TestConfigurePassphrase(flimflam::kSecurityWep,
933 "0:0x0102030405060708090a0b0c0d"));
934 EXPECT_EQ(Error::kSuccess,
935 TestConfigurePassphrase(flimflam::kSecurityWpa, NULL));
936 EXPECT_EQ(Error::kSuccess,
937 TestConfigurePassphrase(flimflam::kSecurityWpa, "secure password"));
938 EXPECT_EQ(Error::kInvalidPassphrase,
939 TestConfigurePassphrase(flimflam::kSecurityWpa, ""));
940 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
941 flimflam::kSecurityWpa,
942 string(IEEE_80211::kWPAAsciiMinLen, 'Z').c_str()));
943 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
944 flimflam::kSecurityWpa,
945 string(IEEE_80211::kWPAAsciiMaxLen, 'Z').c_str()));
946 // subtle: invalid length for hex key, but valid as ascii passphrase
947 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
948 flimflam::kSecurityWpa,
949 string(IEEE_80211::kWPAHexLen-1, '1').c_str()));
950 EXPECT_EQ(Error::kSuccess, TestConfigurePassphrase(
951 flimflam::kSecurityWpa,
952 string(IEEE_80211::kWPAHexLen, '1').c_str()));
953 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
954 flimflam::kSecurityWpa,
955 string(IEEE_80211::kWPAAsciiMinLen-1, 'Z').c_str()));
956 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
957 flimflam::kSecurityWpa,
958 string(IEEE_80211::kWPAAsciiMaxLen+1, 'Z').c_str()));
959 EXPECT_EQ(Error::kInvalidPassphrase, TestConfigurePassphrase(
960 flimflam::kSecurityWpa,
961 string(IEEE_80211::kWPAHexLen+1, '1').c_str()));
962}
963
964TEST_F(WiFiServiceTest, ConfigureRedundantProperties) {
965 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
966 KeyValueStore args;
967 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
968 args.SetString(flimflam::kSSIDProperty, simple_ssid_string());
969 args.SetString(flimflam::kSecurityProperty, flimflam::kSecurityNone);
970 const string kGUID = "aguid";
971 args.SetString(flimflam::kGuidProperty, kGUID);
972
973 EXPECT_EQ("", service->guid());
974 Error error;
975 service->Configure(args, &error);
976 EXPECT_TRUE(error.IsSuccess());
977 EXPECT_EQ(kGUID, service->guid());
978}
979
980TEST_F(WiFiServiceTest, DisconnectWithWiFi) {
981 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
982 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
983 Error error;
984 service->Disconnect(&error);
985}
986
987TEST_F(WiFiServiceTest, DisconnectWithoutWiFi) {
988 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
989 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
990 Error error;
991 service->Disconnect(&error);
992 EXPECT_EQ(Error::kOperationFailed, error.type());
993}
994
995TEST_F(WiFiServiceTest, DisconnectWithoutWiFiWhileAssociating) {
996 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
997 EXPECT_CALL(*wifi(), DisconnectFrom(_)).Times(0);
998 service->SetState(Service::kStateAssociating);
999 ScopedMockLog log;
1000 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1001 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1002 HasSubstr("WiFi endpoints do not (yet) exist.")));
1003 Error error;
1004 service->Disconnect(&error);
1005 EXPECT_EQ(Error::kOperationFailed, error.type());
1006}
1007
Paul Stewart835934a2012-12-06 19:27:09 -08001008TEST_F(WiFiServiceTest, UnloadAndClearCacheWEP) {
Paul Stewart3c504012013-01-17 17:49:58 -08001009 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityWep);
Paul Stewart835934a2012-12-06 19:27:09 -08001010 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
1011 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -08001012 service->Unload();
1013}
1014
1015TEST_F(WiFiServiceTest, UnloadAndClearCache8021x) {
Paul Stewart3c504012013-01-17 17:49:58 -08001016 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurity8021x);
Paul Stewart835934a2012-12-06 19:27:09 -08001017 EXPECT_CALL(*wifi(), ClearCachedCredentials(service.get())).Times(1);
1018 EXPECT_CALL(*wifi(), DisconnectFrom(service.get())).Times(1);
Paul Stewart66c86002012-01-30 18:00:52 -08001019 service->Unload();
1020}
1021
Paul Stewart0756db92012-01-27 08:34:47 -08001022TEST_F(WiFiServiceTest, ParseStorageIdentifierNone) {
Paul Stewart3c504012013-01-17 17:49:58 -08001023 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001024 const string storage_id = service->GetStorageIdentifier();
1025 string address;
1026 string mode;
1027 string security;
1028 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
1029 &security));
Paul Stewart3c504012013-01-17 17:49:58 -08001030 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewarta41e38d2011-11-11 07:47:29 -08001031 EXPECT_EQ(flimflam::kModeManaged, mode);
1032 EXPECT_EQ(flimflam::kSecurityNone, security);
1033}
1034
Paul Stewart0756db92012-01-27 08:34:47 -08001035TEST_F(WiFiServiceTest, ParseStorageIdentifier8021x) {
1036 // Do a separate test for 802.1x, since kSecurity8021x contains a "_",
1037 // which needs to be dealt with specially in the parser.
Paul Stewart3c504012013-01-17 17:49:58 -08001038 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
Paul Stewart0756db92012-01-27 08:34:47 -08001039 const string storage_id = service->GetStorageIdentifier();
1040 string address;
1041 string mode;
1042 string security;
1043 EXPECT_TRUE(service->ParseStorageIdentifier(storage_id, &address, &mode,
1044 &security));
Paul Stewart3c504012013-01-17 17:49:58 -08001045 EXPECT_EQ(StringToLowerASCII(GetAnyDeviceAddress()), address);
Paul Stewart0756db92012-01-27 08:34:47 -08001046 EXPECT_EQ(flimflam::kModeManaged, mode);
1047 EXPECT_EQ(flimflam::kSecurity8021x, security);
1048}
1049
Paul Stewart85aea152013-01-22 09:31:56 -08001050TEST_F(WiFiServiceFixupStorageTest, FixedEntries) {
1051 const string kNonWiFiId = "vpn_foo";
1052 const string kUnparsableWiFiId = "wifi_foo";
1053
1054 AddGroup(kNonWiFiId);
1055 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001056 AddServiceEntry(true, true, true, true);
1057 AddServiceEntry(false, false, false, false);
1058 AddServiceEntry(true, true, true, true);
1059 AddServiceEntry(false, false, false, false);
Paul Stewart85aea152013-01-22 09:31:56 -08001060 EXPECT_TRUE(FixupServiceEntries());
1061}
1062
1063TEST_F(WiFiServiceFixupStorageTest, NoFixedEntries) {
1064 const string kNonWiFiId = "vpn_foo";
1065 const string kUnparsableWiFiId = "wifi_foo";
1066
1067 AddGroup(kNonWiFiId);
1068 AddGroup(kUnparsableWiFiId);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001069 AddServiceEntry(true, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001070 EXPECT_FALSE(FixupServiceEntries());
1071}
1072
1073TEST_F(WiFiServiceFixupStorageTest, MissingTypeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001074 AddServiceEntry(false, true, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001075 EXPECT_TRUE(FixupServiceEntries());
1076}
1077
1078TEST_F(WiFiServiceFixupStorageTest, MissingModeProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001079 AddServiceEntry(true, false, true, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001080 EXPECT_TRUE(FixupServiceEntries());
1081}
1082
1083TEST_F(WiFiServiceFixupStorageTest, MissingSecurityProperty) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001084 AddServiceEntry(true, true, false, true);
Paul Stewart85aea152013-01-22 09:31:56 -08001085 EXPECT_TRUE(FixupServiceEntries());
1086}
1087
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001088TEST_F(WiFiServiceFixupStorageTest, MissingSecurityClassProperty) {
1089 AddServiceEntry(true, true, true, false);
1090 EXPECT_TRUE(FixupServiceEntries());
1091}
Paul Stewart85aea152013-01-22 09:31:56 -08001092
mukesh agrawal29c13a12011-11-24 00:09:19 +00001093TEST_F(WiFiServiceTest, Connectable) {
1094 // Open network should be connectable.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001095 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, NULL, false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001096
1097 // Open network should remain connectable if we try to set a password on it.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001098 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityNone, "abcde", false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001099
1100 // WEP network with passphrase set should be connectable.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001101 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, "abcde", false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001102
1103 // WEP network without passphrase set should NOT be connectable.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001104 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, NULL, false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001105
1106 // A bad passphrase should not make a WEP network connectable.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001107 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWep, "a", false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001108
1109 // Similar to WEP, for WPA.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001110 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWpa, "abcdefgh", false));
1111 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, NULL, false));
1112 EXPECT_FALSE(CheckConnectable(flimflam::kSecurityWpa, "a", false));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001113
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001114 // 802.1x without connectable EAP credentials should NOT be connectable.
1115 EXPECT_FALSE(CheckConnectable(flimflam::kSecurity8021x, NULL, false));
Gaurav Shah10109f22011-11-11 20:16:22 -08001116
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001117 // 802.1x with connectable EAP credentials should be connectable.
1118 EXPECT_TRUE(CheckConnectable(flimflam::kSecurity8021x, NULL, true));
Gaurav Shah10109f22011-11-11 20:16:22 -08001119
Gaurav Shah29d68882012-01-30 19:06:42 -08001120 // Dynamic WEP + 802.1X should be connectable under the same conditions.
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001121 EXPECT_TRUE(CheckConnectable(flimflam::kSecurityWep, NULL, true));
mukesh agrawal29c13a12011-11-24 00:09:19 +00001122}
1123
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001124TEST_F(WiFiServiceTest, IsAutoConnectable) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001125 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001126 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001127 EXPECT_CALL(*wifi(), IsIdle())
1128 .WillRepeatedly(Return(true));
1129 EXPECT_FALSE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001130 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1131 EXPECT_STREQ(WiFiService::kAutoConnNoEndpoint, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001132
mukesh agrawalbf14e942012-03-02 14:36:34 -08001133 reason = "";
mukesh agrawal43970a22013-02-15 16:00:07 -08001134 WiFiEndpointRefPtr endpoint =
1135 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001136 service->AddEndpoint(endpoint);
1137 EXPECT_CALL(*wifi(), IsIdle())
1138 .WillRepeatedly(Return(true));
1139 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001140 EXPECT_TRUE(service->IsAutoConnectable(&reason));
1141 EXPECT_STREQ("", reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001142
1143 // WiFi only supports connecting to one Service at a time. So, to
1144 // avoid disrupting connectivity, we only allow auto-connection to
1145 // a WiFiService when the corresponding WiFi is idle.
1146 EXPECT_CALL(*wifi(), IsIdle())
1147 .WillRepeatedly(Return(false));
1148 EXPECT_TRUE(service->HasEndpoints());
mukesh agrawalbf14e942012-03-02 14:36:34 -08001149 EXPECT_FALSE(service->IsAutoConnectable(&reason));
1150 EXPECT_STREQ(WiFiService::kAutoConnBusy, reason);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001151}
1152
1153TEST_F(WiFiServiceTest, AutoConnect) {
mukesh agrawalbf14e942012-03-02 14:36:34 -08001154 const char *reason;
Paul Stewart3c504012013-01-17 17:49:58 -08001155 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
mukesh agrawalbf14e942012-03-02 14:36:34 -08001156 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001157 EXPECT_CALL(*wifi(), ConnectTo(_, _))
1158 .Times(0);
1159 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001160 dispatcher()->DispatchPendingEvents();
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001161
mukesh agrawal43970a22013-02-15 16:00:07 -08001162 WiFiEndpointRefPtr endpoint =
1163 MakeOpenEndpoint("a", "00:00:00:00:00:01", 0, 0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001164 service->AddEndpoint(endpoint);
1165 EXPECT_CALL(*wifi(), IsIdle())
1166 .WillRepeatedly(Return(true));
mukesh agrawalbf14e942012-03-02 14:36:34 -08001167 EXPECT_TRUE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001168 EXPECT_CALL(*wifi(), ConnectTo(_, _));
1169 service->AutoConnect();
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001170 dispatcher()->DispatchPendingEvents();
mukesh agrawaladb68482012-01-17 16:31:51 -08001171
1172 Error error;
Christopher Wileyabd3b502012-09-26 13:08:52 -07001173 service->UserInitiatedDisconnect(&error);
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001174 dispatcher()->DispatchPendingEvents();
mukesh agrawalbf14e942012-03-02 14:36:34 -08001175 EXPECT_FALSE(service->IsAutoConnectable(&reason));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001176}
1177
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001178TEST_F(WiFiServiceTest, ClearWriteOnlyDerivedProperty) {
Paul Stewart3c504012013-01-17 17:49:58 -08001179 WiFiServiceRefPtr wifi_service = MakeSimpleService(flimflam::kSecurityWep);
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001180
1181 EXPECT_EQ("", wifi_service->passphrase_);
1182
1183 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001184 EXPECT_TRUE(DBusAdaptor::SetProperty(
mukesh agrawal8abd2f62012-01-30 14:56:14 -08001185 wifi_service->mutable_store(),
1186 flimflam::kPassphraseProperty,
1187 DBusAdaptor::StringToVariant("0:abcde"),
1188 &error));
1189 EXPECT_EQ("0:abcde", wifi_service->passphrase_);
1190
1191 EXPECT_TRUE(DBusAdaptor::ClearProperty(wifi_service->mutable_store(),
1192 flimflam::kPassphraseProperty,
1193 &error));
1194 EXPECT_EQ("", wifi_service->passphrase_);
1195}
1196
mukesh agrawale1d90e92012-02-15 17:36:08 -08001197TEST_F(WiFiServiceTest, SignalToStrength) {
1198 // Verify that our mapping is sane, in the sense that it preserves ordering.
1199 // We break the test into two domains, because we assume that positive
1200 // values aren't actually in dBm.
1201 for (int16 i = std::numeric_limits<int16>::min(); i < 0; ++i) {
1202 int16 current_mapped = WiFiService::SignalToStrength(i);
1203 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1204 EXPECT_LE(current_mapped, next_mapped)
1205 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001206 EXPECT_GE(current_mapped, Service::kStrengthMin);
1207 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001208 }
1209 for (int16 i = 1; i < std::numeric_limits<int16>::max(); ++i) {
1210 int16 current_mapped = WiFiService::SignalToStrength(i);
1211 int16 next_mapped = WiFiService::SignalToStrength(i+1);
1212 EXPECT_LE(current_mapped, next_mapped)
1213 << "(original values " << i << " " << i+1 << ")";
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001214 EXPECT_GE(current_mapped, Service::kStrengthMin);
1215 EXPECT_LE(current_mapped, Service::kStrengthMax);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001216 }
1217}
1218
1219TEST_F(WiFiServiceUpdateFromEndpointsTest, Strengths) {
1220 // If the chosen signal values don't map to distinct strength
1221 // values, then we can't expect our other tests to pass. So verify
1222 // their distinctness.
1223 EXPECT_TRUE(kOkEndpointStrength != kBadEndpointStrength);
1224 EXPECT_TRUE(kOkEndpointStrength != kGoodEndpointStrength);
1225 EXPECT_TRUE(kGoodEndpointStrength != kBadEndpointStrength);
1226}
1227
1228TEST_F(WiFiServiceUpdateFromEndpointsTest, Floating) {
1229 // Initial endpoint updates values.
1230 EXPECT_CALL(adaptor, EmitUint16Changed(
1231 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001232 EXPECT_CALL(adaptor, EmitStringChanged(
1233 flimflam::kWifiBSsid, kOkEndpointBssId));
1234 EXPECT_CALL(adaptor, EmitUint8Changed(
mukesh agrawale1d90e92012-02-15 17:36:08 -08001235 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001236 EXPECT_CALL(adaptor, EmitUint16Changed(
1237 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyMode11b));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001238 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001239 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001240 Mock::VerifyAndClearExpectations(&adaptor);
1241
1242 // Endpoint with stronger signal updates values.
1243 EXPECT_CALL(adaptor, EmitUint16Changed(
1244 flimflam::kWifiFrequency, kGoodEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001245 EXPECT_CALL(adaptor, EmitStringChanged(
1246 flimflam::kWifiBSsid, kGoodEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001247 EXPECT_CALL(adaptor, EmitUint8Changed(
1248 flimflam::kSignalStrengthProperty, kGoodEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001249 // However, both endpoints are 11b.
1250 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001251 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001252 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001253 Mock::VerifyAndClearExpectations(&adaptor);
1254
1255 // Endpoint with lower signal does not change values.
1256 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001257 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001258 EXPECT_CALL(adaptor,
1259 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001260 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001261 service->AddEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001262 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001263 Mock::VerifyAndClearExpectations(&adaptor);
1264
1265 // Removing non-optimal endpoint does not change values.
1266 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001267 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001268 EXPECT_CALL(adaptor,
1269 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
mukesh agrawalf6b32092013-04-10 15:49:55 -07001270 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001271 service->RemoveEndpoint(bad_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001272 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001273 Mock::VerifyAndClearExpectations(&adaptor);
1274
1275 // Removing optimal endpoint updates values.
1276 EXPECT_CALL(adaptor, EmitUint16Changed(
1277 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001278 EXPECT_CALL(adaptor, EmitStringChanged(
1279 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001280 EXPECT_CALL(adaptor, EmitUint8Changed(
1281 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001282 // However, both endpoints are 11b.
1283 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiPhyMode, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001284 service->RemoveEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001285 EXPECT_EQ(1, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001286 Mock::VerifyAndClearExpectations(&adaptor);
1287
1288 // Removing last endpoint updates values (and doesn't crash).
1289 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001290 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001291 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
mukesh agrawalf6b32092013-04-10 15:49:55 -07001292 EXPECT_CALL(adaptor, EmitUint16Changed(
1293 flimflam::kWifiPhyMode, Metrics::kWiFiNetworkPhyModeUndef));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001294 service->RemoveEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001295 EXPECT_EQ(0, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001296 Mock::VerifyAndClearExpectations(&adaptor);
1297}
1298
1299TEST_F(WiFiServiceUpdateFromEndpointsTest, Connected) {
1300 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001301 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001302 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1303 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1304 service->AddEndpoint(bad_endpoint);
1305 service->AddEndpoint(ok_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001306 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001307 Mock::VerifyAndClearExpectations(&adaptor);
1308
1309 // Setting current endpoint forces adoption of its values, even if it
1310 // doesn't have the highest signal.
1311 EXPECT_CALL(adaptor, EmitUint16Changed(
1312 flimflam::kWifiFrequency, kBadEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001313 EXPECT_CALL(adaptor, EmitStringChanged(
1314 flimflam::kWifiBSsid, kBadEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001315 EXPECT_CALL(adaptor, EmitUint8Changed(
1316 flimflam::kSignalStrengthProperty, kBadEndpointStrength));
1317 service->NotifyCurrentEndpoint(bad_endpoint);
1318 Mock::VerifyAndClearExpectations(&adaptor);
1319
1320 // Adding a better endpoint doesn't matter, when current endpoint is set.
1321 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001322 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001323 EXPECT_CALL(adaptor,
1324 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1325 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001326 EXPECT_EQ(3, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001327 Mock::VerifyAndClearExpectations(&adaptor);
1328
1329 // Removing a better endpoint doesn't matter, when current endpoint is set.
1330 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001331 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001332 EXPECT_CALL(adaptor,
1333 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1334 service->RemoveEndpoint(good_endpoint);
1335 Mock::VerifyAndClearExpectations(&adaptor);
1336
1337 // Removing the current endpoint is safe and sane.
1338 EXPECT_CALL(adaptor, EmitUint16Changed(
1339 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001340 EXPECT_CALL(adaptor, EmitStringChanged(
1341 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001342 EXPECT_CALL(adaptor, EmitUint8Changed(
1343 flimflam::kSignalStrengthProperty, kOkEndpointStrength));
1344 service->RemoveEndpoint(bad_endpoint);
1345 Mock::VerifyAndClearExpectations(&adaptor);
1346
1347 // Clearing the current endpoint (without removing it) is also safe and sane.
1348 service->NotifyCurrentEndpoint(ok_endpoint);
1349 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001350 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001351 EXPECT_CALL(adaptor,
1352 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1353 service->NotifyCurrentEndpoint(NULL);
1354 Mock::VerifyAndClearExpectations(&adaptor);
1355}
1356
1357TEST_F(WiFiServiceUpdateFromEndpointsTest, EndpointModified) {
1358 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
mukesh agrawal923f14f2012-06-04 16:46:08 -07001359 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001360 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1361 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1362 service->AddEndpoint(ok_endpoint);
1363 service->AddEndpoint(good_endpoint);
Darin Petkov4a66cc52012-06-15 10:08:29 +02001364 EXPECT_EQ(2, service->GetEndpointCount());
mukesh agrawale1d90e92012-02-15 17:36:08 -08001365 Mock::VerifyAndClearExpectations(&adaptor);
1366
1367 // Updating sub-optimal Endpoint doesn't update Service.
1368 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001369 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001370 EXPECT_CALL(adaptor,
1371 EmitUint8Changed(flimflam::kSignalStrengthProperty, _)).Times(0);
1372 ok_endpoint->signal_strength_ = (kOkEndpointSignal + kGoodEndpointSignal) / 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001373 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001374 Mock::VerifyAndClearExpectations(&adaptor);
1375
1376 // Updating optimal Endpoint updates appropriate Service property.
1377 EXPECT_CALL(adaptor, EmitUint16Changed(flimflam::kWifiFrequency, _)).Times(0);
mukesh agrawal923f14f2012-06-04 16:46:08 -07001378 EXPECT_CALL(adaptor, EmitStringChanged(flimflam::kWifiBSsid, _)).Times(0);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001379 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1380 good_endpoint->signal_strength_ = kGoodEndpointSignal + 1;
Paul Stewart3c504012013-01-17 17:49:58 -08001381 service->NotifyEndpointUpdated(good_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001382 Mock::VerifyAndClearExpectations(&adaptor);
1383
1384 // Change in optimal Endpoint updates Service properties.
1385 EXPECT_CALL(adaptor, EmitUint16Changed(
1386 flimflam::kWifiFrequency, kOkEndpointFrequency));
mukesh agrawal923f14f2012-06-04 16:46:08 -07001387 EXPECT_CALL(adaptor, EmitStringChanged(
1388 flimflam::kWifiBSsid, kOkEndpointBssId));
mukesh agrawale1d90e92012-02-15 17:36:08 -08001389 EXPECT_CALL(adaptor, EmitUint8Changed(flimflam::kSignalStrengthProperty, _));
1390 ok_endpoint->signal_strength_ = kGoodEndpointSignal + 2;
Paul Stewart3c504012013-01-17 17:49:58 -08001391 service->NotifyEndpointUpdated(ok_endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -08001392 Mock::VerifyAndClearExpectations(&adaptor);
1393}
1394
Paul Stewarta5e7d5f2013-01-09 18:06:15 -08001395TEST_F(WiFiServiceUpdateFromEndpointsTest, Ieee80211w) {
1396 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1397 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1398 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1399 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1400 service->AddEndpoint(ok_endpoint);
1401 EXPECT_FALSE(service->ieee80211w_required());
1402 good_endpoint->ieee80211w_required_ = true;
1403 service->AddEndpoint(good_endpoint);
1404 EXPECT_TRUE(service->ieee80211w_required());
1405 service->RemoveEndpoint(good_endpoint);
1406 EXPECT_TRUE(service->ieee80211w_required());
1407}
1408
mukesh agrawalf6b32092013-04-10 15:49:55 -07001409TEST_F(WiFiServiceUpdateFromEndpointsTest, PhysicalMode) {
1410 EXPECT_CALL(adaptor, EmitUint16Changed(_, _)).Times(AnyNumber());
1411 EXPECT_CALL(adaptor, EmitStringChanged(_, _)).Times(AnyNumber());
1412 EXPECT_CALL(adaptor, EmitUint8Changed(_, _)).Times(AnyNumber());
1413 EXPECT_CALL(adaptor, EmitBoolChanged(_, _)).Times(AnyNumber());
1414
1415 // No endpoints -> undef.
1416 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1417
1418 // Endpoint has unknown physical mode -> undef.
1419 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyModeUndef;
1420 service->AddEndpoint(ok_endpoint);
1421 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1422
1423 // New endpoint with 802.11a -> 802.11a.
1424 good_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a;
1425 service->AddEndpoint(good_endpoint);
1426 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11a, service->physical_mode());
1427
1428 // Remove 802.11a endpoint -> undef.
1429 service->RemoveEndpoint(good_endpoint);
1430 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1431
1432 // Change endpoint -> take endpoint's new value.
1433 ok_endpoint->physical_mode_ = Metrics::kWiFiNetworkPhyMode11n;
1434 service->NotifyEndpointUpdated(ok_endpoint);
1435 EXPECT_EQ(Metrics::kWiFiNetworkPhyMode11n, service->physical_mode());
1436
1437 // No endpoints -> undef.
1438 service->RemoveEndpoint(ok_endpoint);
1439 EXPECT_EQ(Metrics::kWiFiNetworkPhyModeUndef, service->physical_mode());
1440}
1441
Paul Stewart8653f462013-02-06 12:21:05 -08001442TEST_F(WiFiServiceUpdateFromEndpointsTest, WarningOnDisconnect) {
1443 service->AddEndpoint(ok_endpoint);
1444 service->SetState(Service::kStateAssociating);
1445 ScopedMockLog log;
1446 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1447 EXPECT_CALL(log, Log(logging::LOG_WARNING, _,
1448 EndsWith("disconnect due to no remaining endpoints.")));
1449 service->RemoveEndpoint(ok_endpoint);
1450}
1451
Paul Stewart6df20bd2013-03-13 19:31:25 -07001452TEST_F(WiFiServiceTest, SecurityFromCurrentEndpoint) {
1453 WiFiServiceRefPtr service(MakeSimpleService(flimflam::kSecurityPsk));
1454 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1455 WiFiEndpoint *endpoint = MakeOpenEndpoint(
1456 simple_ssid_string(), "00:00:00:00:00:00", 0, 0);
1457 service->AddEndpoint(endpoint);
1458 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1459 service->NotifyCurrentEndpoint(endpoint);
1460 EXPECT_EQ(flimflam::kSecurityNone, service->GetSecurity(NULL));
1461 service->NotifyCurrentEndpoint(NULL);
1462 EXPECT_EQ(flimflam::kSecurityPsk, service->GetSecurity(NULL));
1463}
1464
mukesh agrawal43970a22013-02-15 16:00:07 -08001465TEST_F(WiFiServiceTest, UpdateSecurity) {
1466 // Cleartext and pre-shared-key crypto.
1467 {
1468 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityNone);
1469 EXPECT_EQ(Service::kCryptoNone, service->crypto_algorithm());
1470 EXPECT_FALSE(service->key_rotation());
1471 EXPECT_FALSE(service->endpoint_auth());
1472 }
1473 {
1474 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1475 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1476 EXPECT_FALSE(service->key_rotation());
1477 EXPECT_FALSE(service->endpoint_auth());
1478 }
1479 {
1480 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityPsk);
1481 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1482 EXPECT_TRUE(service->key_rotation());
1483 EXPECT_FALSE(service->endpoint_auth());
1484 }
1485 {
1486 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWpa);
1487 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1488 EXPECT_TRUE(service->key_rotation());
1489 EXPECT_FALSE(service->endpoint_auth());
1490 }
1491 {
1492 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityRsn);
1493 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1494 EXPECT_TRUE(service->key_rotation());
1495 EXPECT_FALSE(service->endpoint_auth());
1496 }
1497
1498 // Crypto with 802.1X key management.
1499 {
1500 // WEP
1501 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurityWep);
1502 service->SetEAPKeyManagement("IEEE8021X");
1503 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1504 EXPECT_TRUE(service->key_rotation());
1505 EXPECT_TRUE(service->endpoint_auth());
1506 }
1507 {
1508 // WPA
1509 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1510 WiFiEndpointRefPtr endpoint =
1511 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false);
1512 service->AddEndpoint(endpoint);
1513 EXPECT_EQ(Service::kCryptoRc4, service->crypto_algorithm());
1514 EXPECT_TRUE(service->key_rotation());
1515 EXPECT_TRUE(service->endpoint_auth());
1516 }
1517 {
1518 // RSN
1519 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1520 WiFiEndpointRefPtr endpoint =
1521 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true);
1522 service->AddEndpoint(endpoint);
1523 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1524 EXPECT_TRUE(service->key_rotation());
1525 EXPECT_TRUE(service->endpoint_auth());
1526 }
1527 {
1528 // AP supports both WPA and RSN.
1529 WiFiServiceRefPtr service = MakeSimpleService(flimflam::kSecurity8021x);
1530 WiFiEndpointRefPtr endpoint =
1531 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true);
1532 service->AddEndpoint(endpoint);
1533 EXPECT_EQ(Service::kCryptoAes, service->crypto_algorithm());
1534 EXPECT_TRUE(service->key_rotation());
1535 EXPECT_TRUE(service->endpoint_auth());
1536 }
1537}
1538
1539TEST_F(WiFiServiceTest, ComputeCipher8021x) {
1540 // No endpoints.
1541 {
1542 const set<WiFiEndpointConstRefPtr> endpoints;
1543 EXPECT_EQ(Service::kCryptoNone,
1544 WiFiService::ComputeCipher8021x(endpoints));
1545 }
1546
1547 // Single endpoint, various configs.
1548 {
1549 set<WiFiEndpointConstRefPtr> endpoints;
1550 endpoints.insert(
1551 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1552 EXPECT_EQ(Service::kCryptoNone,
1553 WiFiService::ComputeCipher8021x(endpoints));
1554 }
1555 {
1556 set<WiFiEndpointConstRefPtr> endpoints;
1557 endpoints.insert(
1558 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1559 EXPECT_EQ(Service::kCryptoRc4,
1560 WiFiService::ComputeCipher8021x(endpoints));
1561 }
1562 {
1563 set<WiFiEndpointConstRefPtr> endpoints;
1564 endpoints.insert(
1565 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1566 EXPECT_EQ(Service::kCryptoAes,
1567 WiFiService::ComputeCipher8021x(endpoints));
1568 }
1569 {
1570 set<WiFiEndpointConstRefPtr> endpoints;
1571 endpoints.insert(
1572 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1573 EXPECT_EQ(Service::kCryptoAes,
1574 WiFiService::ComputeCipher8021x(endpoints));
1575 }
1576
1577 // Multiple endpoints.
1578 {
1579 set<WiFiEndpointConstRefPtr> endpoints;
1580 endpoints.insert(
1581 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1582 endpoints.insert(
1583 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, false));
1584 EXPECT_EQ(Service::kCryptoNone,
1585 WiFiService::ComputeCipher8021x(endpoints));
1586 }
1587 {
1588 set<WiFiEndpointConstRefPtr> endpoints;
1589 endpoints.insert(
1590 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, false));
1591 endpoints.insert(
1592 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1593 EXPECT_EQ(Service::kCryptoNone,
1594 WiFiService::ComputeCipher8021x(endpoints));
1595 }
1596 {
1597 set<WiFiEndpointConstRefPtr> endpoints;
1598 endpoints.insert(
1599 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1600 endpoints.insert(
1601 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, false));
1602 EXPECT_EQ(Service::kCryptoRc4,
1603 WiFiService::ComputeCipher8021x(endpoints));
1604 }
1605 {
1606 set<WiFiEndpointConstRefPtr> endpoints;
1607 endpoints.insert(
1608 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, false));
1609 endpoints.insert(
1610 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1611 EXPECT_EQ(Service::kCryptoRc4,
1612 WiFiService::ComputeCipher8021x(endpoints));
1613 }
1614 {
1615 set<WiFiEndpointConstRefPtr> endpoints;
1616 endpoints.insert(
1617 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, false, true));
1618 endpoints.insert(
1619 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, false, true));
1620 EXPECT_EQ(Service::kCryptoAes,
1621 WiFiService::ComputeCipher8021x(endpoints));
1622 }
1623 {
1624 set<WiFiEndpointConstRefPtr> endpoints;
1625 endpoints.insert(
1626 MakeEndpoint("a", "00:00:00:00:00:01", 0, 0, true, true));
1627 endpoints.insert(
1628 MakeEndpoint("a", "00:00:00:00:00:02", 0, 0, true, true));
1629 EXPECT_EQ(Service::kCryptoAes,
1630 WiFiService::ComputeCipher8021x(endpoints));
1631 }
1632}
1633
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08001634TEST_F(WiFiServiceTest, Unload) {
1635 WiFiServiceRefPtr service = MakeServiceWithWiFi(flimflam::kSecurityNone);
1636 EXPECT_CALL(*wifi(), DestroyIPConfigLease(service->GetStorageIdentifier())).
1637 Times(1);
1638 service->Unload();
1639}
1640
1641
Chris Masone34af2182011-08-22 11:59:36 -07001642} // namespace shill