blob: 3a0a5e669d3a3db2f41f2153bf9d90e73b44dd23 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone9be4a9d2011-05-16 15:44:09 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07004
5#include "shill/manager.h"
6
Jason Glasgowdf7c5532012-05-14 14:41:45 -04007#include <map>
Chris Masone6791a432011-07-12 13:23:19 -07008#include <set>
9
Chris Masone9be4a9d2011-05-16 15:44:09 -070010#include <glib.h>
11
Ben Chan11c213f2014-09-05 08:21:06 -070012#include <base/files/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080013#include <base/files/scoped_temp_dir.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050014#include <base/stl_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080015#include <base/strings/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070017#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070018#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070019
mukesh agrawal32399322011-09-01 10:53:43 -070020#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070021#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070022#include "shill/error.h"
Paul Stewart7de7e022013-08-28 09:42:50 -070023#include "shill/geolocation_info.h"
Chris Masone6515aab2011-10-12 16:19:09 -070024#include "shill/glib.h"
25#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070026#include "shill/key_value_store.h"
mukesh agrawal00752532013-05-03 15:46:55 -070027#include "shill/link_monitor.h"
Christopher Wiley3e7635e2012-08-15 09:46:17 -070028#include "shill/logging.h"
mukesh agrawal32399322011-09-01 10:53:43 -070029#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080030#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070031#include "shill/mock_control.h"
Christopher Wiley1057cd72013-02-28 15:21:29 -080032#include "shill/mock_crypto_util_proxy.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070033#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080034#include "shill/mock_device_info.h"
Paul Stewart35eff132013-04-12 12:08:40 -070035#include "shill/mock_ethernet_eap_provider.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070036#include "shill/mock_glib.h"
mukesh agrawalb94adde2013-08-22 18:17:26 -070037#include "shill/mock_log.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000038#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020039#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070040#include "shill/mock_profile.h"
Ben Chana55469d2014-01-27 16:35:29 -080041#include "shill/mock_proxy_factory.h"
Paul Stewart4d5efb72012-09-17 12:24:34 -070042#include "shill/mock_resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070043#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070044#include "shill/mock_store.h"
Paul Stewart3c504012013-01-17 17:49:58 -080045#include "shill/mock_wifi_provider.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070046#include "shill/mock_wifi_service.h"
mukesh agrawal00752532013-05-03 15:46:55 -070047#include "shill/portal_detector.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070048#include "shill/property_store_unittest.h"
mukesh agrawal00752532013-05-03 15:46:55 -070049#include "shill/resolver.h"
Chris Masone6515aab2011-10-12 16:19:09 -070050#include "shill/service_under_test.h"
Paul Stewartf2860342014-05-09 14:29:16 -070051#include "shill/testing.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070052#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020053#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070054
Christopher Wiley1057cd72013-02-28 15:21:29 -080055using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080056using base::FilePath;
Paul Stewart5ad16062013-02-21 18:10:48 -080057using base::ScopedTempDir;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070058using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070059using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060using std::string;
61using std::vector;
62
Chris Masone9be4a9d2011-05-16 15:44:09 -070063namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070064using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070065using ::testing::AnyNumber;
Paul Stewart7de7e022013-08-28 09:42:50 -070066using ::testing::AtLeast;
Gaurav Shah435de2c2011-11-17 19:01:07 -080067using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070068using ::testing::DoAll;
mukesh agrawalb94adde2013-08-22 18:17:26 -070069using ::testing::HasSubstr;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070070using ::testing::InSequence;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070071using ::testing::Invoke;
mukesh agrawal784566d2012-08-08 18:32:58 -070072using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070073using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070074using ::testing::NiceMock;
Paul Stewart967eaeb2013-04-25 19:53:07 -070075using ::testing::Ref;
Chris Masone9be4a9d2011-05-16 15:44:09 -070076using ::testing::Return;
Ben Chana55469d2014-01-27 16:35:29 -080077using ::testing::ReturnNull;
Paul Stewartce4ec192012-03-14 12:53:46 -070078using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070079using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080080using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080081using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080082using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070083using ::testing::Test;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070084using ::testing::WithArg;
Chris Masone9be4a9d2011-05-16 15:44:09 -070085
Chris Masone3bd3c8c2011-06-13 08:20:26 -070086class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070087 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070088 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020089 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080090 device_info_(new NiceMock<MockDeviceInfo>(
91 control_interface(),
92 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080093 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080094 reinterpret_cast<Manager*>(NULL))),
Paul Stewart3c504012013-01-17 17:49:58 -080095 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070096 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080097 wifi_provider_(new NiceMock<MockWiFiProvider>()),
98 crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(),
99 glib())) {
Ben Chana55469d2014-01-27 16:35:29 -0800100 ON_CALL(proxy_factory_, CreatePowerManagerProxy(_))
101 .WillByDefault(ReturnNull());
102
Paul Stewart22aa71b2011-09-16 12:15:11 -0700103 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
104 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800105 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700106 manager(),
107 "null0",
108 "addr0",
109 0));
110 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
111 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800112 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700113 manager(),
114 "null1",
115 "addr1",
116 1));
117 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
118 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800119 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700120 manager(),
121 "null2",
122 "addr2",
123 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800124 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
125 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800126 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800127 manager(),
128 "null3",
129 "addr3",
130 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700131 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800132 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800133
134 // Replace the manager's adaptor with a quieter one, and one
135 // we can do EXPECT*() against. Passes ownership.
136 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800137
Paul Stewart35eff132013-04-12 12:08:40 -0700138 // Replace the manager's Ethernet EAP provider with our mock.
139 // Passes ownership.
140 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
141
Paul Stewart3c504012013-01-17 17:49:58 -0800142 // Replace the manager's WiFi provider with our mock. Passes
143 // ownership.
144 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800145
Paul Stewartb87d22b2013-07-29 11:11:37 -0700146 // Update the manager's map from technology to provider.
147 manager()->UpdateProviderMapping();
148
Christopher Wiley1057cd72013-02-28 15:21:29 -0800149 // Replace the manager's crypto util proxy with our mock. Passes
150 // ownership.
151 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700152 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700153 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700154
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100155 void SetMetrics(Metrics *metrics) {
156 manager()->set_metrics(metrics);
157 }
158
Paul Stewartfdd16072011-09-16 12:41:35 -0700159 bool IsDeviceRegistered(const DeviceRefPtr &device,
160 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700161 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700162 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700163 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700164 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700165 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700166
Paul Stewarta849a3d2011-11-03 05:54:09 -0700167 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
168 manager->profiles_.push_back(profile);
169 }
170
Paul Stewart63864b62012-11-07 15:10:55 -0800171 void SetRunning(bool running) {
172 manager()->running_ = running;
173 }
174
Paul Stewart75225512012-01-26 22:51:33 -0800175 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
176 return manager->ephemeral_profile_;
177 }
178
Paul Stewart307c2502013-03-23 12:32:10 -0700179 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
180 return manager->profiles_;
181 }
182
Chris Masone6515aab2011-10-12 16:19:09 -0700183 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
184 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700185 FilePath final_path(storage_path());
186 final_path = final_path.Append("test.profile");
187 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
188 storage->set_path(final_path);
189 if (!storage->Open())
190 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800191 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800192 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800193 manager,
194 id,
195 "",
196 false));
197 profile->set_storage(storage.release()); // Passes ownership of "storage".
198 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700199 }
200
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700201 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700202 const string &user_identifier,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700203 const string &profile_identifier,
204 const string &service_name) {
205 GLib glib;
206 KeyFileStore store(&glib);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700207 store.set_path(temp_dir->path().Append(
208 base::StringPrintf("%s/%s.profile", user_identifier.c_str(),
209 profile_identifier.c_str())));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700210 return store.Open() &&
211 store.SetString(service_name, "rather", "irrelevant") &&
212 store.Close();
213 }
214
215 Error::Type TestCreateProfile(Manager *manager, const string &name) {
216 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800217 string path;
218 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700219 return error.type();
220 }
221
222 Error::Type TestPopAnyProfile(Manager *manager) {
223 Error error;
224 manager->PopAnyProfile(&error);
225 return error.type();
226 }
227
Paul Stewart307c2502013-03-23 12:32:10 -0700228 Error::Type TestPopAllUserProfiles(Manager *manager) {
229 Error error;
230 manager->PopAllUserProfiles(&error);
231 return error.type();
232 }
233
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700234 Error::Type TestPopProfile(Manager *manager, const string &name) {
235 Error error;
236 manager->PopProfile(name, &error);
237 return error.type();
238 }
239
240 Error::Type TestPushProfile(Manager *manager, const string &name) {
241 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800242 string path;
243 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700244 return error.type();
245 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000246
Paul Stewartf3eced92013-04-17 12:18:22 -0700247 Error::Type TestInsertUserProfile(Manager *manager,
248 const string &name,
249 const string &user_hash) {
250 Error error;
251 string path;
252 manager->InsertUserProfile(name, user_hash, &path, &error);
253 return error.type();
254 }
255
Paul Stewartd2e1c362013-03-03 19:06:07 -0800256 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
257 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700258 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800259 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800260 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200261 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700262 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800263 return profile;
264 }
265
266 void AddMockProfileToManager(Manager *manager) {
267 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700268 }
269
Paul Stewartdfa46052012-06-26 09:44:14 -0700270 void CompleteServiceSort() {
271 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
272 dispatcher()->DispatchPendingEvents();
273 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
274 }
275
Paul Stewart03e29f72013-09-26 00:49:48 -0700276 void RefreshConnectionState() {
277 manager()->RefreshConnectionState();
278 }
279
Paul Stewart49739c02012-08-08 17:24:03 -0700280 RpcIdentifier GetDefaultServiceRpcIdentifier() {
281 return manager()->GetDefaultServiceRpcIdentifier(NULL);
282 }
283
Paul Stewart4d5efb72012-09-17 12:24:34 -0700284 void SetResolver(Resolver *resolver) {
285 manager()->resolver_ = resolver;
286 }
287
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700288 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
289 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
290 }
291
292 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
293 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700294 }
295
296 const string &GetIgnoredDNSSearchPaths() {
297 return manager()->props_.ignored_dns_search_paths;
298 }
299
Paul Stewartd2e1c362013-03-03 19:06:07 -0800300 WiFiServiceRefPtr ReleaseTempMockService() {
301 // Take a reference to hold during this function.
302 WiFiServiceRefPtr temp_service = temp_mock_service_;
303 temp_mock_service_ = NULL;
304 return temp_service;
305 }
306
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700307 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000308 typedef scoped_refptr<MockService> MockServiceRefPtr;
309
Darin Petkova5e07ef2012-07-09 14:27:57 +0200310 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
311 public:
312 ServiceWatcher() {}
313 virtual ~ServiceWatcher() {}
314
315 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
316
317 private:
318 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
319 };
320
Darin Petkov3ec55342012-09-28 14:04:44 +0200321 class TerminationActionTest :
322 public base::SupportsWeakPtr<TerminationActionTest> {
323 public:
324 static const char kActionName[];
325
326 TerminationActionTest() : manager_(NULL) {}
327 virtual ~TerminationActionTest() {}
328
329 MOCK_METHOD1(Done, void(const Error &error));
330
331 void Action() {
332 manager_->TerminationActionComplete("action");
333 }
334
335 void set_manager(Manager *manager) { manager_ = manager; }
336
337 private:
338 Manager *manager_;
339 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
340 };
341
Christopher Wiley1057cd72013-02-28 15:21:29 -0800342 class DestinationVerificationTest :
343 public base::SupportsWeakPtr<DestinationVerificationTest> {
344 public:
345 DestinationVerificationTest() {}
346 virtual ~DestinationVerificationTest() {}
347
348 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
349 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
350 const string &value));
351 private:
352 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
353 };
354
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700355 class DisableTechnologyReplyHandler :
356 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
357 public:
358 DisableTechnologyReplyHandler() {}
359 virtual ~DisableTechnologyReplyHandler() {}
360
361 MOCK_METHOD1(ReportResult, void(const Error &));
362
363 private:
364 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
365 };
366
Daniel Eratfac09532014-04-17 20:25:59 -0700367 void SetSuspending(bool suspending) {
368 power_manager_->suspending_ = suspending;
Darin Petkovca621542012-07-25 14:25:56 +0200369 }
370
371 void SetPowerManager() {
372 manager()->set_power_manager(power_manager_.release());
373 }
374
Darin Petkov3ec55342012-09-28 14:04:44 +0200375 HookTable *GetTerminationActions() {
376 return &manager()->termination_actions_;
377 }
378
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700379 void OnSuspendImminent() {
380 manager()->OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +0200381 }
382
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700383 void OnSuspendDone() {
384 manager()->OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -0700385 }
386
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700387 void OnSuspendActionsComplete(const Error &error) {
388 manager()->OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200389 }
390
Paul Stewartbfb82552012-10-24 16:48:48 -0700391 vector<string> EnumerateAvailableServices() {
392 return manager()->EnumerateAvailableServices(NULL);
393 }
394
395 vector<string> EnumerateWatchedServices() {
396 return manager()->EnumerateWatchedServices(NULL);
397 }
398
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000399 MockServiceRefPtr MakeAutoConnectableService() {
400 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
401 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800402 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000403 manager());
Paul Stewart2da34c02013-10-17 15:28:56 -0700404 service->EnableAndRetainAutoConnect();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700405 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000406 return service;
407 }
408
Paul Stewart35eff132013-04-12 12:08:40 -0700409 void SetEapProviderService(const ServiceRefPtr &service) {
410 ethernet_eap_provider_->set_service(service);
411 }
412
Ben Chana55469d2014-01-27 16:35:29 -0800413 NiceMock<MockProxyFactory> proxy_factory_;
Darin Petkovca621542012-07-25 14:25:56 +0200414 scoped_ptr<MockPowerManager> power_manager_;
Ben Chane2ee5e02014-09-19 19:29:42 -0700415 vector<scoped_refptr<MockDevice>> mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800416 scoped_ptr<MockDeviceInfo> device_info_;
417
Paul Stewartd2e1c362013-03-03 19:06:07 -0800418 // This service is held for the manager, and given ownership in a mock
419 // function. This ensures that when the Manager takes ownership, there
420 // is only one reference left.
421 scoped_refptr<MockWiFiService> temp_mock_service_;
422
Paul Stewart3c504012013-01-17 17:49:58 -0800423 // These pointers are owned by the manager, and only tracked here for
424 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800425 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700426 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800427 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800428 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700429};
430
Darin Petkov3ec55342012-09-28 14:04:44 +0200431const char ManagerTest::TerminationActionTest::kActionName[] = "action";
432
Paul Stewart22aa71b2011-09-16 12:15:11 -0700433bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700434 if (!manager()->sort_services_task_.IsCancelled()) {
435 manager()->SortServicesTask();
436 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700437 return (svc0.get() == manager()->services_[0].get() &&
438 svc1.get() == manager()->services_[1].get());
439}
440
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700441void SetErrorPermissionDenied(Error *error) {
442 error->Populate(Error::kPermissionDenied);
443}
444
Arman Uguray2f352e62013-08-28 19:12:53 -0700445void SetErrorSuccess(Error *error) {
446 error->Reset();
447}
448
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700449TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700450 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700451 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700452}
453
Chris Masone9be4a9d2011-05-16 15:44:09 -0700454TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700455 ON_CALL(*mock_devices_[0].get(), technology())
456 .WillByDefault(Return(Technology::kEthernet));
457 ON_CALL(*mock_devices_[1].get(), technology())
458 .WillByDefault(Return(Technology::kWifi));
459 ON_CALL(*mock_devices_[2].get(), technology())
460 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700461
Paul Stewart22aa71b2011-09-16 12:15:11 -0700462 manager()->RegisterDevice(mock_devices_[0]);
463 manager()->RegisterDevice(mock_devices_[1]);
464 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700465
Paul Stewart22aa71b2011-09-16 12:15:11 -0700466 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
467 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
468 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700469}
470
Paul Stewarta41e38d2011-11-11 07:47:29 -0800471TEST_F(ManagerTest, DeviceRegistrationAndStart) {
472 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500473 mock_devices_[0]->enabled_persistent_ = true;
474 mock_devices_[1]->enabled_persistent_ = false;
475 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800476 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500477 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800478 .Times(0);
479 manager()->RegisterDevice(mock_devices_[0]);
480 manager()->RegisterDevice(mock_devices_[1]);
481}
482
483TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800484 MockProfile *profile =
485 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800486 DeviceRefPtr device_ref(mock_devices_[0].get());
487 AdoptProfile(manager(), profile); // Passes ownership.
488 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200489 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800490 manager()->RegisterDevice(mock_devices_[0]);
491}
492
Chris Masone9be4a9d2011-05-16 15:44:09 -0700493TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700494 ON_CALL(*mock_devices_[0].get(), technology())
495 .WillByDefault(Return(Technology::kEthernet));
496 ON_CALL(*mock_devices_[1].get(), technology())
497 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700498
Gaurav Shah435de2c2011-11-17 19:01:07 -0800499 manager()->RegisterDevice(mock_devices_[0]);
500 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700501
Paul Stewart22aa71b2011-09-16 12:15:11 -0700502 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
503 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700504
Thieu Le5133b712013-02-19 14:47:21 -0800505 MockProfile *profile =
506 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700507 AdoptProfile(manager(), profile); // Passes ownership.
508
Eric Shienbrood9a245532012-03-07 14:20:39 -0500509 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700510 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800511 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700512 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700513
Eric Shienbrood9a245532012-03-07 14:20:39 -0500514 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700515 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800516 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700517 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700518}
519
520TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700521 // It's much easier and safer to use a real GLib for this test.
522 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700523 Manager manager(control_interface(),
524 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800525 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700526 &glib,
527 run_path(),
528 storage_path(),
529 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700530 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
531 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700532 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700533
Chris Masone9be4a9d2011-05-16 15:44:09 -0700534 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700535 new NiceMock<MockService>(control_interface(),
536 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800537 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700538 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700539 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700540 new NiceMock<MockService>(control_interface(),
541 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800542 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700543 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700544
Darin Petkov457728b2013-01-09 09:49:08 +0100545 string service1_name(mock_service->unique_name());
546 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700547
548 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
549 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700550 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700551 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700552 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700553 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700554 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700555
Chris Masone9d779932011-08-25 16:33:41 -0700556 manager.RegisterService(mock_service);
557 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700558
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800559 Error error;
560 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700561 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700562 EXPECT_EQ(2, ids.size());
563 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
564 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700565
Chris Masone9d779932011-08-25 16:33:41 -0700566 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
567 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
568
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700569 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700570 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700571}
572
Chris Masone6515aab2011-10-12 16:19:09 -0700573TEST_F(ManagerTest, RegisterKnownService) {
574 // It's much easier and safer to use a real GLib for this test.
575 GLib glib;
576 Manager manager(control_interface(),
577 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800578 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700579 &glib,
580 run_path(),
581 storage_path(),
582 string());
583 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
584 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700585 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700586 {
587 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
588 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800589 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700590 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700591 ASSERT_TRUE(profile->AdoptService(service1));
592 ASSERT_TRUE(profile->ContainsService(service1));
593 } // Force destruction of service1.
594
595 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
596 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800597 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700598 &manager));
599 manager.RegisterService(service2);
600 EXPECT_EQ(service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700601
602 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700603 manager.Stop();
604}
605
606TEST_F(ManagerTest, RegisterUnknownService) {
607 // It's much easier and safer to use a real GLib for this test.
608 GLib glib;
609 Manager manager(control_interface(),
610 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800611 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700612 &glib,
613 run_path(),
614 storage_path(),
615 string());
616 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
617 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700618 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700619 {
620 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
621 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800622 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700623 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700624 ASSERT_TRUE(profile->AdoptService(service1));
625 ASSERT_TRUE(profile->ContainsService(service1));
626 } // Force destruction of service1.
627 scoped_refptr<MockService> mock_service2(
628 new NiceMock<MockService>(control_interface(),
629 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800630 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700631 &manager));
632 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100633 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700634 manager.RegisterService(mock_service2);
635 EXPECT_NE(mock_service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700636
637 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700638 manager.Stop();
639}
640
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000641TEST_F(ManagerTest, DeregisterUnregisteredService) {
642 // WiFi assumes that it can deregister a service that is not
643 // registered. (E.g. a hidden service can be deregistered when it
644 // loses its last endpoint, and again when WiFi is Stop()-ed.)
645 //
646 // So test that doing so doesn't cause a crash.
647 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
648 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800649 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000650 manager());
651 manager()->DeregisterService(service);
652}
653
Chris Masonea8a2c252011-06-27 22:16:30 -0700654TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700655 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700656 map<string, ::DBus::Variant> props;
657 Error error(Error::kInvalidProperty, "");
658 {
659 ::DBus::Error dbus_error;
660 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700661 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700662 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700663 expected,
664 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700665 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700666 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
667 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700668 expected);
669 }
670 {
671 ::DBus::Error dbus_error;
672 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700673 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700674 expected,
675 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700676 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700677 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
678 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700679 expected);
680 }
681}
682
Chris Masone3c3f6a12011-07-01 10:01:41 -0700683TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700684 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800685 manager()->RegisterDevice(mock_devices_[0]);
686 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700687 {
688 map<string, ::DBus::Variant> props;
689 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700690 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700691 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700692 vector < ::DBus::Path> devices =
Ben Chane2ee5e02014-09-19 19:29:42 -0700693 props[kDevicesProperty].operator vector<::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700694 EXPECT_EQ(2, devices.size());
695 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700696}
697
mukesh agrawal2366eed2012-03-20 18:21:50 -0700698TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700699 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700700 map<string, ::DBus::Variant> props;
701 ::DBus::Error dbus_error;
702 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
703 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700704 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700705 ASSERT_FALSE(prop == props.end());
706 const ::DBus::Variant &variant = prop->second;
707 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
708}
709
Chris Masone6791a432011-07-12 13:23:19 -0700710TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700711 Manager manager(control_interface(),
712 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800713 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700714 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700715 run_path(),
716 storage_path(),
717 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700718 scoped_refptr<MockService> s2(new MockService(control_interface(),
719 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800720 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700721 &manager));
722 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700723 {
Chris Masone6515aab2011-10-12 16:19:09 -0700724 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700725 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800726 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700727 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700728 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700729 .WillRepeatedly(Return(true));
730 EXPECT_CALL(*storage, Flush())
731 .Times(AnyNumber())
732 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700733 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700734 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700735 }
Chris Masone6515aab2011-10-12 16:19:09 -0700736 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800737 ProfileRefPtr profile(
738 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700739 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700740
Chris Masone6515aab2011-10-12 16:19:09 -0700741 // Now, move the Service |s2| to another profile.
742 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
743 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700744
745 // Force destruction of the original Profile, to ensure that the Service
746 // is kept alive and populated with data.
747 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700748 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700749 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700750 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700751}
752
Paul Stewart7f61e522012-03-22 11:13:45 -0700753TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
754 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800755 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700756 const string kProfileName("profile0");
757 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
758 .WillRepeatedly(Return(kProfileName));
759 AdoptProfile(manager(), mock_profile);
760
761 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
762 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
763 EXPECT_EQ(mock_profile.get(), profile.get());
764}
765
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800766TEST_F(ManagerTest, SetProfileForService) {
767 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800768 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800769 string profile_name0("profile0");
770 EXPECT_CALL(*profile0, GetRpcIdentifier())
771 .WillRepeatedly(Return(profile_name0));
772 AdoptProfile(manager(), profile0);
773 scoped_refptr<MockService> service(new MockService(control_interface(),
774 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800775 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800776 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700777 EXPECT_FALSE(manager()->HasService(service));
778 {
779 Error error;
780 EXPECT_CALL(*profile0, AdoptService(_))
781 .WillOnce(Return(true));
782 // Expect that setting the profile of a service that does not already
783 // have one assigned does not cause a crash.
784 manager()->SetProfileForService(service, "profile0", &error);
785 EXPECT_TRUE(error.IsSuccess());
786 }
787
788 // The service should be registered as a side-effect of the profile being
789 // set for this service.
790 EXPECT_TRUE(manager()->HasService(service));
791
792 // Since we have mocked Profile::AdoptServie() above, the service's
793 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800794 service->set_profile(profile0);
795
796 {
797 Error error;
798 manager()->SetProfileForService(service, "foo", &error);
799 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700800 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800801 }
802
803 {
804 Error error;
805 manager()->SetProfileForService(service, profile_name0, &error);
806 EXPECT_EQ(Error::kInvalidArguments, error.type());
807 EXPECT_EQ("Service is already connected to this profile", error.message());
808 }
809
810 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800811 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800812 string profile_name1("profile1");
813 EXPECT_CALL(*profile1, GetRpcIdentifier())
814 .WillRepeatedly(Return(profile_name1));
815 AdoptProfile(manager(), profile1);
816
817 {
818 Error error;
819 EXPECT_CALL(*profile1, AdoptService(_))
820 .WillOnce(Return(true));
821 EXPECT_CALL(*profile0, AbandonService(_))
822 .WillOnce(Return(true));
823 manager()->SetProfileForService(service, profile_name1, &error);
824 EXPECT_TRUE(error.IsSuccess());
825 }
826}
827
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700828TEST_F(ManagerTest, CreateProfile) {
829 // It's much easier to use real Glib here since we want the storage
830 // side-effects.
831 GLib glib;
832 ScopedTempDir temp_dir;
833 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
834
835 Manager manager(control_interface(),
836 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800837 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700838 &glib,
839 run_path(),
840 storage_path(),
841 temp_dir.path().value());
842
843 // Invalid name should be rejected.
844 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
845
Paul Stewartd0a3b812012-03-28 22:48:22 -0700846 // A profile with invalid characters in it should similarly be rejected.
847 EXPECT_EQ(Error::kInvalidArguments,
848 TestCreateProfile(&manager, "valid_profile"));
849
850 // We should be able to create a machine profile.
851 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700852
Gary Morainb672d352012-04-25 09:19:06 -0700853 // We should succeed in creating a valid user profile. Verify the returned
854 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700855 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700856 {
857 Error error;
858 string path;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700859 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Gary Morainb672d352012-04-25 09:19:06 -0700860 manager.CreateProfile(kProfile, &path, &error);
861 EXPECT_EQ(Error::kSuccess, error.type());
862 EXPECT_EQ("/profile_rpc", path);
863 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700864
865 // We should fail in creating it a second time (already exists).
866 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
867}
868
869TEST_F(ManagerTest, PushPopProfile) {
870 // It's much easier to use real Glib in creating a Manager for this
871 // test here since we want the storage side-effects.
872 GLib glib;
873 ScopedTempDir temp_dir;
874 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
875 Manager manager(control_interface(),
876 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800877 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700878 &glib,
879 run_path(),
880 storage_path(),
881 temp_dir.path().value());
mukesh agrawal92496a42014-04-08 16:04:43 -0700882 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700883
884 // Pushing an invalid profile should fail.
885 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
886
mukesh agrawal92496a42014-04-08 16:04:43 -0700887 // Create and push a default profile. Should succeed.
888 const char kDefaultProfile0[] = "default";
889 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kDefaultProfile0));
890 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kDefaultProfile0));
891 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kDefaultProfile0));
892
893 // Pushing a default profile that does not exist on disk will _not_
894 // fail, because we'll use temporary storage for it.
895 const char kMissingDefaultProfile[] = "missingdefault";
896 EXPECT_EQ(Error::kSuccess,
897 TestPushProfile(&manager, kMissingDefaultProfile));
898 EXPECT_EQ(1, profiles.size());
899 EXPECT_EQ(Error::kSuccess,
900 TestPopProfile(&manager, kMissingDefaultProfile));
901 EXPECT_EQ(0, profiles.size());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700902
903 const char kProfile0[] = "~user/profile0";
904 const char kProfile1[] = "~user/profile1";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700905 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700906
907 // Create a couple of profiles.
908 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
909 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
910
911 // Push these profiles on the stack.
912 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
913 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
914
915 // Pushing a profile a second time should fail.
916 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
917 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
918
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800919 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700920 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800921 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700922
923 // Make sure a profile name that doesn't exist fails.
924 const char kProfile2Id[] = "profile2";
925 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
926 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
927
928 // Create a new service, with a specific storage name.
929 scoped_refptr<MockService> service(
930 new NiceMock<MockService>(control_interface(),
931 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800932 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700933 &manager));
934 const char kServiceName[] = "service_storage_name";
935 EXPECT_CALL(*service.get(), GetStorageIdentifier())
936 .WillRepeatedly(Return(kServiceName));
937 EXPECT_CALL(*service.get(), Load(_))
938 .WillRepeatedly(Return(true));
939
940 // Add this service to the manager -- it should end up in the ephemeral
941 // profile.
942 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800943 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700944
945 // Create storage for a profile that contains the service storage name.
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700946 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile2Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700947 kServiceName));
948
949 // When we push the profile, the service should move away from the
950 // ephemeral profile to this new profile since it has an entry for
951 // this service.
Paul Stewart2eee6132014-05-09 13:33:26 -0700952 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700953 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800954 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700955 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
956
957 // Insert another profile that should supersede ownership of the service.
958 const char kProfile3Id[] = "profile3";
959 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700960 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile3Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700961 kServiceName));
Paul Stewart2eee6132014-05-09 13:33:26 -0700962 // We don't verify this expectation inline, since this would clear other
963 // recurring expectations on the service.
964 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700965 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
966 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
967
968 // Popping an invalid profile name should fail.
969 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
970
971 // Popping an profile that is not at the top of the stack should fail.
972 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
973
974 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700975 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700976 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
977
978 // Moreover the service should have switched profiles to profile 2.
979 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
980
981 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700982 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700983 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
984
985 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800986 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700987
mukesh agrawal92496a42014-04-08 16:04:43 -0700988 // Pop the remaining two profiles off the stack.
Paul Stewart2eee6132014-05-09 13:33:26 -0700989 EXPECT_CALL(*service, ClearExplicitlyDisconnected()).Times(2);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700990 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
991 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
Paul Stewart2eee6132014-05-09 13:33:26 -0700992 Mock::VerifyAndClearExpectations(service);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700993
994 // Next pop should fail with "stack is empty".
995 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700996
997 const char kMachineProfile0[] = "machineprofile0";
998 const char kMachineProfile1[] = "machineprofile1";
999 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1000 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1001
1002 // Should be able to push a machine profile.
1003 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1004
1005 // Should be able to push a user profile atop a machine profile.
1006 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1007
1008 // Pushing a system-wide profile on top of a user profile should fail.
1009 EXPECT_EQ(Error::kInvalidArguments,
1010 TestPushProfile(&manager, kMachineProfile1));
1011
1012 // However if we pop the user profile, we should be able stack another
1013 // machine profile on.
1014 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1015 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001016
1017 // Add two user profiles to the top of the stack.
1018 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1019 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001020 EXPECT_EQ(4, profiles.size());
1021
1022 // PopAllUserProfiles should remove both user profiles, leaving the two
1023 // machine profiles.
1024 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1025 EXPECT_EQ(2, profiles.size());
1026 EXPECT_TRUE(profiles[0]->GetUser().empty());
1027 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001028
1029 // Use InsertUserProfile() instead. Although a machine profile is valid
1030 // in this state, it cannot be added via InsertUserProfile.
1031 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1032 EXPECT_EQ(Error::kInvalidArguments,
1033 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1034 const char kUserHash0[] = "userhash0";
1035 const char kUserHash1[] = "userhash1";
1036 EXPECT_EQ(Error::kSuccess,
1037 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1038 EXPECT_EQ(Error::kSuccess,
1039 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1040 EXPECT_EQ(3, profiles.size());
1041 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1042 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001043}
1044
Paul Stewarte73d05c2012-03-29 16:26:05 -07001045TEST_F(ManagerTest, RemoveProfile) {
1046 // It's much easier to use real Glib in creating a Manager for this
1047 // test here since we want the storage side-effects.
1048 GLib glib;
1049 ScopedTempDir temp_dir;
1050 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1051 Manager manager(control_interface(),
1052 dispatcher(),
1053 metrics(),
1054 &glib,
1055 run_path(),
1056 storage_path(),
1057 temp_dir.path().value());
1058
1059 const char kProfile0[] = "profile0";
1060 FilePath profile_path(
1061 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1062
1063 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001064 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001065
1066 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1067
1068 // Remove should fail since the profile is still on the stack.
1069 {
1070 Error error;
1071 manager.RemoveProfile(kProfile0, &error);
1072 EXPECT_EQ(Error::kInvalidArguments, error.type());
1073 }
1074
1075 // Profile path should still exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001076 EXPECT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001077
1078 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1079
1080 // This should succeed now that the profile is off the stack.
1081 {
1082 Error error;
1083 manager.RemoveProfile(kProfile0, &error);
1084 EXPECT_EQ(Error::kSuccess, error.type());
1085 }
1086
1087 // Profile path should no longer exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001088 EXPECT_FALSE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001089
Ben Chana0ddf462014-02-06 11:32:42 -08001090 // Another remove succeeds, due to a foible in base::DeleteFile --
Paul Stewarte73d05c2012-03-29 16:26:05 -07001091 // it is not an error to delete a file that does not exist.
1092 {
1093 Error error;
1094 manager.RemoveProfile(kProfile0, &error);
1095 EXPECT_EQ(Error::kSuccess, error.type());
1096 }
1097
1098 // Let's create an error case that will "work". Create a non-empty
1099 // directory in the place of the profile pathname.
Ben Chana0ddf462014-02-06 11:32:42 -08001100 ASSERT_TRUE(base::CreateDirectory(profile_path.Append("foo")));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001101 {
1102 Error error;
1103 manager.RemoveProfile(kProfile0, &error);
1104 EXPECT_EQ(Error::kOperationFailed, error.type());
1105 }
1106}
1107
Paul Stewartd3d03882013-08-29 15:43:42 -07001108TEST_F(ManagerTest, RemoveService) {
1109 MockServiceRefPtr mock_service(
1110 new NiceMock<MockService>(control_interface(),
1111 dispatcher(),
1112 metrics(),
1113 manager()));
1114
1115 // Used in expectations which cannot accept a mock refptr.
1116 const ServiceRefPtr &service = mock_service;
1117
1118 manager()->RegisterService(service);
1119 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1120
1121 scoped_refptr<MockProfile> profile(
1122 new StrictMock<MockProfile>(
1123 control_interface(), metrics(), manager(), ""));
1124 AdoptProfile(manager(), profile);
1125
1126 // If service is ephemeral, it should be unloaded and left ephemeral.
1127 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1128 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1129 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1130 manager()->RemoveService(service);
1131 Mock::VerifyAndClearExpectations(mock_service);
1132 Mock::VerifyAndClearExpectations(profile);
1133 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1134 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1135
1136 // If service is not ephemeral and the Manager finds a profile to assign
1137 // the service to, the service should be re-parented. Note that since we
1138 // are using a MockProfile, ConfigureService() never actually changes the
1139 // Service's profile.
1140 service->set_profile(profile);
1141 EXPECT_CALL(*profile, AbandonService(service));
1142 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1143 EXPECT_CALL(*mock_service, Unload()).Times(0);
1144 manager()->RemoveService(service);
1145 Mock::VerifyAndClearExpectations(mock_service);
1146 Mock::VerifyAndClearExpectations(profile);
1147 EXPECT_TRUE(manager()->HasService(service));
1148 EXPECT_EQ(profile.get(), service->profile().get());
1149
1150 // If service becomes ephemeral since there is no profile to support it,
1151 // it should be unloaded.
1152 EXPECT_CALL(*profile, AbandonService(service));
1153 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1154 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1155 manager()->RemoveService(service);
1156 EXPECT_FALSE(manager()->HasService(service));
1157}
1158
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001159TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1160 // It's much easier to use real Glib in creating a Manager for this
1161 // test here since we want the storage side-effects.
1162 GLib glib;
1163 ScopedTempDir temp_dir;
1164 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1165 Manager manager(control_interface(),
1166 dispatcher(),
1167 metrics(),
1168 &glib,
1169 run_path(),
1170 storage_path(),
1171 temp_dir.path().value());
1172
1173 const char kProfile0[] = "profile0";
1174 FilePath profile_path(
1175 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1176
1177 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001178 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001179 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1180
1181 // Ensure that even if the backing filestore is removed, we still can't
1182 // create a profile twice.
Ben Chana0ddf462014-02-06 11:32:42 -08001183 ASSERT_TRUE(base::DeleteFile(profile_path, false));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001184 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1185}
1186
Paul Stewart75225512012-01-26 22:51:33 -08001187TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1188 MockServiceRefPtr s_not_in_profile(
1189 new NiceMock<MockService>(control_interface(),
1190 dispatcher(),
1191 metrics(),
1192 manager()));
1193 MockServiceRefPtr s_not_in_group(
1194 new NiceMock<MockService>(control_interface(),
1195 dispatcher(),
1196 metrics(),
1197 manager()));
1198 MockServiceRefPtr s_configure_fail(
1199 new NiceMock<MockService>(control_interface(),
1200 dispatcher(),
1201 metrics(),
1202 manager()));
1203 MockServiceRefPtr s_configure_succeed(
1204 new NiceMock<MockService>(control_interface(),
1205 dispatcher(),
1206 metrics(),
1207 manager()));
1208
1209 string entry_name("entry_name");
1210 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1211 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1212 .WillRepeatedly(Return("not_entry_name"));
1213 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1214 .WillRepeatedly(Return(entry_name));
1215 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1216 .WillRepeatedly(Return(entry_name));
1217
1218 manager()->RegisterService(s_not_in_profile);
1219 manager()->RegisterService(s_not_in_group);
1220 manager()->RegisterService(s_configure_fail);
1221 manager()->RegisterService(s_configure_succeed);
1222
1223 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001224 new StrictMock<MockProfile>(
1225 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001226 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001227 new StrictMock<MockProfile>(
1228 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001229
1230 s_not_in_group->set_profile(profile1);
1231 s_configure_fail->set_profile(profile1);
1232 s_configure_succeed->set_profile(profile1);
1233
1234 AdoptProfile(manager(), profile0);
1235 AdoptProfile(manager(), profile1);
1236
1237 // No services are a member of this profile.
1238 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1239
1240 // No services that are members of this profile have this entry name.
1241 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1242
1243 // Only services that are members of the profile and group will be abandoned.
1244 EXPECT_CALL(*profile1.get(),
1245 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1246 EXPECT_CALL(*profile1.get(),
1247 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1248 EXPECT_CALL(*profile1.get(),
1249 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1250 .WillOnce(Return(true));
1251 EXPECT_CALL(*profile1.get(),
1252 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1253 .WillOnce(Return(true));
1254
1255 // Never allow services to re-join profile1.
1256 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1257 .WillRepeatedly(Return(false));
1258
1259 // Only allow one of the members of the profile and group to successfully
1260 // join profile0.
1261 EXPECT_CALL(*profile0.get(),
1262 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1263 EXPECT_CALL(*profile0.get(),
1264 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1265 EXPECT_CALL(*profile0.get(),
1266 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1267 .WillOnce(Return(false));
1268 EXPECT_CALL(*profile0.get(),
1269 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1270 .WillOnce(Return(true));
1271
1272 // Expect the failed-to-configure service to have Unload() called on it.
1273 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1274 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1275 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1276 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1277
1278 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1279
1280 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1281 EXPECT_EQ(profile1, s_not_in_group->profile());
1282 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1283
1284 // Since we are using a MockProfile, the profile does not actually change,
1285 // since ConfigureService was not actually called on the service.
1286 EXPECT_EQ(profile1, s_configure_succeed->profile());
1287}
1288
Paul Stewart65512e12012-03-26 18:01:08 -07001289TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1290 MockServiceRefPtr s_will_remove0(
1291 new NiceMock<MockService>(control_interface(),
1292 dispatcher(),
1293 metrics(),
1294 manager()));
1295 MockServiceRefPtr s_will_remove1(
1296 new NiceMock<MockService>(control_interface(),
1297 dispatcher(),
1298 metrics(),
1299 manager()));
1300 MockServiceRefPtr s_will_not_remove0(
1301 new NiceMock<MockService>(control_interface(),
1302 dispatcher(),
1303 metrics(),
1304 manager()));
1305 MockServiceRefPtr s_will_not_remove1(
1306 new NiceMock<MockService>(control_interface(),
1307 dispatcher(),
1308 metrics(),
1309 manager()));
1310
1311 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1312 .Times(4); // Once for each registration.
1313
1314 string entry_name("entry_name");
1315 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1316 .WillRepeatedly(Return(entry_name));
1317 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1318 .WillRepeatedly(Return(entry_name));
1319 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1320 .WillRepeatedly(Return(entry_name));
1321 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1322 .WillRepeatedly(Return(entry_name));
1323
1324 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001325 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001326 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001327 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001328 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001329 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001330 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001331 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001332
1333 // One for each service added above.
1334 ASSERT_EQ(4, manager()->services_.size());
1335
1336 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001337 new StrictMock<MockProfile>(
1338 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001339
1340 s_will_remove0->set_profile(profile);
1341 s_will_remove1->set_profile(profile);
1342 s_will_not_remove0->set_profile(profile);
1343 s_will_not_remove1->set_profile(profile);
1344
1345 AdoptProfile(manager(), profile);
1346
1347 // Deny any of the services re-entry to the profile.
1348 EXPECT_CALL(*profile, ConfigureService(_))
1349 .WillRepeatedly(Return(false));
1350
1351 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1352 .WillOnce(Return(true));
1353 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1354 .WillOnce(Return(true));
1355 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1356 .WillOnce(Return(true));
1357 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1358 .WillOnce(Return(true));
1359
1360 EXPECT_CALL(*s_will_remove0, Unload())
1361 .WillOnce(Return(true));
1362 EXPECT_CALL(*s_will_remove1, Unload())
1363 .WillOnce(Return(true));
1364 EXPECT_CALL(*s_will_not_remove0, Unload())
1365 .WillOnce(Return(false));
1366 EXPECT_CALL(*s_will_not_remove1, Unload())
1367 .WillOnce(Return(false));
1368
1369
1370 // This will cause all the profiles to be unloaded.
1371 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1372
1373 // 2 of the 4 services added above should have been unregistered and
1374 // removed, leaving 2.
1375 EXPECT_EQ(2, manager()->services_.size());
1376 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1377 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1378}
1379
1380TEST_F(ManagerTest, PopProfileWithUnload) {
1381 MockServiceRefPtr s_will_remove0(
1382 new NiceMock<MockService>(control_interface(),
1383 dispatcher(),
1384 metrics(),
1385 manager()));
1386 MockServiceRefPtr s_will_remove1(
1387 new NiceMock<MockService>(control_interface(),
1388 dispatcher(),
1389 metrics(),
1390 manager()));
1391 MockServiceRefPtr s_will_not_remove0(
1392 new NiceMock<MockService>(control_interface(),
1393 dispatcher(),
1394 metrics(),
1395 manager()));
1396 MockServiceRefPtr s_will_not_remove1(
1397 new NiceMock<MockService>(control_interface(),
1398 dispatcher(),
1399 metrics(),
1400 manager()));
1401
1402 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1403 .Times(5); // Once for each registration, and one after profile pop.
1404
1405 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001406 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001407 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001408 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001409 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001410 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001411 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001412 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001413
1414 // One for each service added above.
1415 ASSERT_EQ(4, manager()->services_.size());
1416
1417 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001418 new StrictMock<MockProfile>(
1419 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001420 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001421 new StrictMock<MockProfile>(
1422 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001423
1424 s_will_remove0->set_profile(profile1);
1425 s_will_remove1->set_profile(profile1);
1426 s_will_not_remove0->set_profile(profile1);
1427 s_will_not_remove1->set_profile(profile1);
1428
1429 AdoptProfile(manager(), profile0);
1430 AdoptProfile(manager(), profile1);
1431
1432 // Deny any of the services entry to profile0, so they will all be unloaded.
1433 EXPECT_CALL(*profile0, ConfigureService(_))
1434 .WillRepeatedly(Return(false));
1435
1436 EXPECT_CALL(*s_will_remove0, Unload())
1437 .WillOnce(Return(true));
1438 EXPECT_CALL(*s_will_remove1, Unload())
1439 .WillOnce(Return(true));
1440 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001441 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001442 EXPECT_CALL(*s_will_not_remove1, Unload())
1443 .WillOnce(Return(false));
1444
Philipp Neubeck79173602012-11-13 21:10:09 +01001445 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1446 // profile list.
1447 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1448 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1449
Paul Stewart65512e12012-03-26 18:01:08 -07001450 // This will pop profile1, which should cause all our profiles to unload.
1451 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001452 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001453
1454 // 2 of the 4 services added above should have been unregistered and
1455 // removed, leaving 2.
1456 EXPECT_EQ(2, manager()->services_.size());
1457 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1458 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001459
1460 // Expect the unloaded services to lose their profile reference.
1461 EXPECT_FALSE(s_will_remove0->profile());
1462 EXPECT_FALSE(s_will_remove1->profile());
1463
1464 // If we explicitly deregister a service, the effect should be the same
1465 // with respect to the profile reference.
1466 ASSERT_TRUE(s_will_not_remove0->profile());
1467 manager()->DeregisterService(s_will_not_remove0);
1468 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001469}
1470
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001471TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001472 {
1473 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001474 ::DBus::Variant offline_mode;
1475 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001476 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001477 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001478 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001479 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001480 }
1481 {
1482 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001483 ::DBus::Variant country;
1484 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001485 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001486 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001487 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001488 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001489 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001490 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001491 {
1492 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001493 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001494 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001495 PropertyStoreTest::kBoolV,
1496 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001497 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001498 }
1499 {
1500 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001501 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001502 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001503 PropertyStoreTest::kStringV,
1504 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001505 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001506 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001507 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001508 {
1509 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001510 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001511 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001512 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001513 PropertyStoreTest::kStringsV,
1514 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001515 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001516 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001517}
1518
mukesh agrawal32399322011-09-01 10:53:43 -07001519TEST_F(ManagerTest, RequestScan) {
1520 {
1521 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001522 manager()->RegisterDevice(mock_devices_[0].get());
1523 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001524 EXPECT_CALL(*mock_devices_[0], technology())
1525 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001526 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001527 EXPECT_CALL(*mock_devices_[1], technology())
1528 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001529 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Peter Qiue783f1c2014-05-02 11:42:33 -07001530 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1531 Metrics::kUserInitiatedEventWifiScan)).Times(1);
Ben Chan923a5022013-09-20 11:23:23 -07001532 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
Peter Qiue783f1c2014-05-02 11:42:33 -07001533 manager()->DeregisterDevice(mock_devices_[0].get());
1534 manager()->DeregisterDevice(mock_devices_[1].get());
1535 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1536 Mock::VerifyAndClearExpectations(mock_devices_[1]);
1537
1538 manager()->RegisterDevice(mock_devices_[0].get());
1539 EXPECT_CALL(*mock_devices_[0], technology())
1540 .WillRepeatedly(Return(Technology::kWifi));
1541 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1542 Metrics::kUserInitiatedEventWifiScan)).Times(1);
1543 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
1544 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1545 manager()->DeregisterDevice(mock_devices_[0].get());
1546 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1547
1548 manager()->RegisterDevice(mock_devices_[0].get());
1549 EXPECT_CALL(*mock_devices_[0], technology())
1550 .WillRepeatedly(Return(Technology::kUnknown));
1551 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1552 Metrics::kUserInitiatedEventWifiScan)).Times(0);
1553 EXPECT_CALL(*mock_devices_[0], Scan(_, _, _)).Times(0);
1554 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1555 manager()->DeregisterDevice(mock_devices_[0].get());
1556 Mock::VerifyAndClearExpectations(mock_devices_[0]);
mukesh agrawal32399322011-09-01 10:53:43 -07001557 }
1558
1559 {
1560 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001561 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001562 EXPECT_EQ(Error::kInvalidArguments, error.type());
1563 }
1564}
1565
Darin Petkovb65c2452012-02-23 15:17:06 +01001566TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001567 KeyValueStore args;
1568 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001569 manager()->GetService(args, &e);
1570 EXPECT_EQ(Error::kInvalidArguments, e.type());
1571 EXPECT_EQ("must specify service type", e.message());
1572}
1573
1574TEST_F(ManagerTest, GetServiceUnknownType) {
1575 KeyValueStore args;
1576 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001577 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001578 manager()->GetService(args, &e);
1579 EXPECT_EQ(Error::kNotSupported, e.type());
1580 EXPECT_EQ("service type is unsupported", e.message());
1581}
1582
Paul Stewart35eff132013-04-12 12:08:40 -07001583TEST_F(ManagerTest, GetServiceEthernetEap) {
1584 KeyValueStore args;
1585 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001586 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1587 dispatcher(),
1588 metrics(),
1589 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001590 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001591 SetEapProviderService(service);
1592 EXPECT_EQ(service, manager()->GetService(args, &e));
1593 EXPECT_TRUE(e.IsSuccess());
1594}
1595
Darin Petkovb65c2452012-02-23 15:17:06 +01001596TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001597 KeyValueStore args;
1598 Error e;
1599 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001600 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001601 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001602 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001603 manager()->GetService(args, &e);
1604 EXPECT_TRUE(e.IsSuccess());
1605}
1606
Darin Petkov33af05c2012-02-28 10:10:30 +01001607TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1608 KeyValueStore args;
1609 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001610 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001611 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001612 new StrictMock<MockProfile>(
1613 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001614 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001615 ServiceRefPtr service = manager()->GetService(args, &e);
1616 EXPECT_EQ(Error::kNotSupported, e.type());
1617 EXPECT_FALSE(service);
1618}
1619
Darin Petkovb65c2452012-02-23 15:17:06 +01001620TEST_F(ManagerTest, GetServiceVPN) {
1621 KeyValueStore args;
1622 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001623 args.SetString(kTypeProperty, kTypeVPN);
1624 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1625 args.SetString(kProviderHostProperty, "10.8.0.1");
1626 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001627 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001628 new StrictMock<MockProfile>(
1629 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001630 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001631
1632#if defined(DISABLE_VPN)
1633
1634 ServiceRefPtr service = manager()->GetService(args, &e);
1635 EXPECT_EQ(Error::kNotSupported, e.type());
1636 EXPECT_FALSE(service);
1637
1638#else
1639
Paul Stewart7f5ad572012-06-04 15:18:54 -07001640 ServiceRefPtr updated_service;
1641 EXPECT_CALL(*profile, UpdateService(_))
1642 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1643 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001644 EXPECT_CALL(*profile, LoadService(_))
1645 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001646 EXPECT_CALL(*profile, ConfigureService(_))
1647 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001648 ServiceRefPtr service = manager()->GetService(args, &e);
1649 EXPECT_TRUE(e.IsSuccess());
1650 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001651 EXPECT_EQ(service, updated_service);
1652 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001653
1654#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001655}
1656
Ben Chan520eb172013-10-30 20:51:04 -07001657#if !defined(DISABLE_WIMAX)
1658
Darin Petkovc63dcf02012-05-24 11:51:43 +02001659TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1660 KeyValueStore args;
1661 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001662 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001663 ServiceRefPtr service = manager()->GetService(args, &e);
1664 EXPECT_EQ(Error::kInvalidArguments, e.type());
1665 EXPECT_EQ("Missing WiMAX network id.", e.message());
1666 EXPECT_FALSE(service);
1667}
1668
Darin Petkovd1cd7972012-05-22 15:26:15 +02001669TEST_F(ManagerTest, GetServiceWiMax) {
1670 KeyValueStore args;
1671 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001672 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001673 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001674 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001675 ServiceRefPtr service = manager()->GetService(args, &e);
1676 EXPECT_TRUE(e.IsSuccess());
1677 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001678}
1679
Ben Chan520eb172013-10-30 20:51:04 -07001680#endif // DISABLE_WIMAX
1681
Paul Stewart7f61e522012-03-22 11:13:45 -07001682TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1683 // Manager calls ActiveProfile() so we need at least one profile installed.
1684 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001685 new NiceMock<MockProfile>(
1686 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001687 AdoptProfile(manager(), profile);
1688
1689 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001690 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001691 Error error;
1692 manager()->ConfigureService(args, &error);
1693 EXPECT_EQ(Error::kInvalidArguments, error.type());
1694 EXPECT_EQ("Invalid profile name xxx", error.message());
1695}
1696
1697TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1698 // Manager calls ActiveProfile() so we need at least one profile installed.
1699 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001700 new NiceMock<MockProfile>(
1701 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001702 AdoptProfile(manager(), profile);
1703
1704 KeyValueStore args;
1705 Error error;
1706 manager()->ConfigureService(args, &error);
1707 EXPECT_EQ(Error::kInvalidArguments, error.type());
1708 EXPECT_EQ("must specify service type", error.message());
1709}
1710
1711// A registered service in the ephemeral profile should be moved to the
1712// active profile as a part of configuration if no profile was explicitly
1713// specified.
1714TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1715 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001716 new NiceMock<MockProfile>(
1717 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001718
1719 AdoptProfile(manager(), profile); // This is now the active profile.
1720
Paul Stewartd2e1c362013-03-03 19:06:07 -08001721 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001722 scoped_refptr<MockWiFiService> service(
1723 new NiceMock<MockWiFiService>(control_interface(),
1724 dispatcher(),
1725 metrics(),
1726 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001727 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001728 ssid,
1729 "",
1730 "",
1731 false));
1732
1733 manager()->RegisterService(service);
1734 service->set_profile(GetEphemeralProfile(manager()));
1735
Paul Stewart3c504012013-01-17 17:49:58 -08001736 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001737 .WillOnce(Return(service));
1738 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1739 .WillOnce(Return(true));
1740 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1741 .WillOnce(Return(true));
1742
1743 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001744 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001745 Error error;
1746 manager()->ConfigureService(args, &error);
1747 EXPECT_TRUE(error.IsSuccess());
1748}
1749
Paul Stewart2c575d22012-12-07 12:28:57 -08001750// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001751// specify a profile, it should be moved from the profile it was previously
1752// in to the specified profile if one was requested.
1753TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1754 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001755 new NiceMock<MockProfile>(
1756 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001757 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001758 new NiceMock<MockProfile>(
1759 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001760
1761 const string kProfileName0 = "profile0";
1762 const string kProfileName1 = "profile1";
1763
1764 EXPECT_CALL(*profile0, GetRpcIdentifier())
1765 .WillRepeatedly(Return(kProfileName0));
1766 EXPECT_CALL(*profile1, GetRpcIdentifier())
1767 .WillRepeatedly(Return(kProfileName1));
1768
1769 AdoptProfile(manager(), profile0);
1770 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1771
Paul Stewartd2e1c362013-03-03 19:06:07 -08001772 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001773 scoped_refptr<MockWiFiService> service(
1774 new NiceMock<MockWiFiService>(control_interface(),
1775 dispatcher(),
1776 metrics(),
1777 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001778 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001779 ssid,
1780 "",
1781 "",
1782 false));
1783
1784 manager()->RegisterService(service);
1785 service->set_profile(profile1);
1786
Paul Stewart3c504012013-01-17 17:49:58 -08001787 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001788 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001789 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1790 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001791 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1792 .WillOnce(Return(true));
1793 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1794 .WillOnce(Return(true));
1795 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1796 .WillOnce(Return(true));
1797
1798 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001799 args.SetString(kTypeProperty, kTypeWifi);
1800 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001801 Error error;
1802 manager()->ConfigureService(args, &error);
1803 EXPECT_TRUE(error.IsSuccess());
1804 service->set_profile(NULL); // Breaks refcounting loop.
1805}
1806
Paul Stewart2c575d22012-12-07 12:28:57 -08001807// If we configure a service that is already a member of the specified
1808// profile, the Manager should not call LoadService or AdoptService again
1809// on this service.
1810TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1811 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001812 new NiceMock<MockProfile>(
1813 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001814
1815 const string kProfileName0 = "profile0";
1816
1817 EXPECT_CALL(*profile0, GetRpcIdentifier())
1818 .WillRepeatedly(Return(kProfileName0));
1819
1820 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1821
Paul Stewartd2e1c362013-03-03 19:06:07 -08001822 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001823 scoped_refptr<MockWiFiService> service(
1824 new NiceMock<MockWiFiService>(control_interface(),
1825 dispatcher(),
1826 metrics(),
1827 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001828 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001829 ssid,
1830 "",
1831 "",
1832 false));
1833
1834 manager()->RegisterService(service);
1835 service->set_profile(profile0);
1836
Paul Stewart3c504012013-01-17 17:49:58 -08001837 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001838 .WillOnce(Return(service));
1839 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1840 .Times(0);
1841 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1842 .WillOnce(Return(true));
1843 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1844 .Times(0);
1845
1846 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001847 args.SetString(kTypeProperty, kTypeWifi);
1848 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001849 Error error;
1850 manager()->ConfigureService(args, &error);
1851 EXPECT_TRUE(error.IsSuccess());
1852 service->set_profile(NULL); // Breaks refcounting loop.
1853}
1854
Paul Stewart7f61e522012-03-22 11:13:45 -07001855// An unregistered service should remain unregistered, but its contents should
1856// be saved to the specified profile nonetheless.
1857TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1858 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001859 new NiceMock<MockProfile>(
1860 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001861 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001862 new NiceMock<MockProfile>(
1863 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001864
1865 const string kProfileName0 = "profile0";
1866 const string kProfileName1 = "profile1";
1867
1868 EXPECT_CALL(*profile0, GetRpcIdentifier())
1869 .WillRepeatedly(Return(kProfileName0));
1870 EXPECT_CALL(*profile1, GetRpcIdentifier())
1871 .WillRepeatedly(Return(kProfileName1));
1872
1873 AdoptProfile(manager(), profile0);
1874 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1875
Paul Stewartd2e1c362013-03-03 19:06:07 -08001876 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001877 scoped_refptr<MockWiFiService> service(
1878 new NiceMock<MockWiFiService>(control_interface(),
1879 dispatcher(),
1880 metrics(),
1881 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001882 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001883 ssid,
1884 "",
1885 "",
1886 false));
1887
1888 service->set_profile(profile1);
1889
Paul Stewart3c504012013-01-17 17:49:58 -08001890 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001891 .WillOnce(Return(service));
1892 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1893 .WillOnce(Return(true));
1894 EXPECT_CALL(*profile0, AdoptService(_))
1895 .Times(0);
1896 EXPECT_CALL(*profile1, AdoptService(_))
1897 .Times(0);
1898
1899 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001900 args.SetString(kTypeProperty, kTypeWifi);
1901 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001902 Error error;
1903 manager()->ConfigureService(args, &error);
1904 EXPECT_TRUE(error.IsSuccess());
1905}
1906
Paul Stewartd2e1c362013-03-03 19:06:07 -08001907TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1908 KeyValueStore args;
1909 Error error;
1910 ServiceRefPtr service =
1911 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001912 EXPECT_EQ(Error::kInvalidArguments, error.type());
1913 EXPECT_EQ("must specify service type", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001914 EXPECT_EQ(NULL, service.get());
1915}
1916
1917TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1918 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001919 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001920 Error error;
1921 ServiceRefPtr service =
1922 manager()->ConfigureServiceForProfile("", args, &error);
1923 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001924 EXPECT_EQ("service type is unsupported", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001925 EXPECT_EQ(NULL, service.get());
1926}
1927
1928TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1929 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001930 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001931 Error error;
1932 ServiceRefPtr service =
1933 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1934 EXPECT_EQ(Error::kNotFound, error.type());
1935 EXPECT_EQ("Profile specified was not found", error.message());
1936 EXPECT_EQ(NULL, service.get());
1937}
1938
1939TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1940 const string kProfileName0 = "profile0";
1941 const string kProfileName1 = "profile1";
1942 scoped_refptr<MockProfile> profile0(
1943 AddNamedMockProfileToManager(manager(), kProfileName0));
1944
1945 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001946 args.SetString(kTypeProperty, kTypeWifi);
1947 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001948 Error error;
1949 ServiceRefPtr service =
1950 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1951 EXPECT_EQ(Error::kInvalidArguments, error.type());
1952 EXPECT_EQ("Profile argument does not match that in "
1953 "the configuration arguments", error.message());
1954 EXPECT_EQ(NULL, service.get());
1955}
1956
1957TEST_F(ManagerTest,
1958 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1959 const string kProfileName0 = "profile0";
1960 scoped_refptr<MockProfile> profile0(
1961 AddNamedMockProfileToManager(manager(), kProfileName0));
1962 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001963 args.SetString(kTypeProperty, kTypeWifi);
1964 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001965
1966 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1967 .WillOnce(Return(WiFiServiceRefPtr()));
1968 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1969 .WillOnce(Return(WiFiServiceRefPtr()));
1970 Error error;
1971 ServiceRefPtr service =
1972 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1973 // Since we didn't set the error in the GetService expectation above...
1974 EXPECT_TRUE(error.IsSuccess());
1975 EXPECT_EQ(NULL, service.get());
1976}
1977
1978TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1979 const string kProfileName0 = "profile0";
1980 scoped_refptr<MockProfile> profile0(
1981 AddNamedMockProfileToManager(manager(), kProfileName0));
1982
1983 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001984 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001985
1986 scoped_refptr<MockWiFiService> mock_service(
1987 new NiceMock<MockWiFiService>(control_interface(),
1988 dispatcher(),
1989 metrics(),
1990 manager(),
1991 wifi_provider_,
1992 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07001993 kModeManaged,
1994 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08001995 false));
1996 ServiceRefPtr mock_service_generic(mock_service.get());
1997 mock_service->set_profile(profile0);
1998 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1999 .WillOnce(Return(WiFiServiceRefPtr()));
2000 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2001 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2002 .WillOnce(Return(true));
2003 Error error;
2004 ServiceRefPtr service =
2005 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2006 EXPECT_TRUE(error.IsSuccess());
2007 EXPECT_EQ(mock_service.get(), service.get());
2008 mock_service->set_profile(NULL); // Breaks reference cycle.
2009}
2010
2011TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2012 scoped_refptr<MockService> mock_service(
2013 new NiceMock<MockService>(control_interface(),
2014 dispatcher(),
2015 metrics(),
2016 manager()));
2017 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002018 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002019 manager()->RegisterService(mock_service);
2020 ServiceRefPtr mock_service_generic(mock_service.get());
2021
2022 const string kProfileName = "profile";
2023 scoped_refptr<MockProfile> profile(
2024 AddNamedMockProfileToManager(manager(), kProfileName));
2025 mock_service->set_profile(profile);
2026
2027 EXPECT_CALL(*mock_service, technology())
2028 .WillOnce(Return(Technology::kCellular))
2029 .WillOnce(Return(Technology::kWifi));
2030
2031 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2032 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2033 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2034
2035 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002036 args.SetString(kTypeProperty, kTypeWifi);
2037 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002038
2039 // The first attempt should fail because the service reports a technology
2040 // other than "WiFi".
2041 {
2042 Error error;
2043 ServiceRefPtr service =
2044 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2045 EXPECT_EQ(NULL, service.get());
2046 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002047 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002048 }
2049
2050 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2051 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2052
2053 {
2054 Error error;
2055 ServiceRefPtr service =
2056 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2057 EXPECT_TRUE(error.IsSuccess());
2058 EXPECT_EQ(mock_service.get(), service.get());
2059 EXPECT_EQ(profile.get(), service->profile().get());
2060 }
2061 mock_service->set_profile(NULL); // Breaks reference cycle.
2062}
2063
2064TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2065 const string kProfileName = "profile";
2066 scoped_refptr<MockProfile> profile(
2067 AddNamedMockProfileToManager(manager(), kProfileName));
2068
2069 scoped_refptr<MockWiFiService> mock_service(
2070 new NiceMock<MockWiFiService>(control_interface(),
2071 dispatcher(),
2072 metrics(),
2073 manager(),
2074 wifi_provider_,
2075 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002076 kModeManaged,
2077 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002078 false));
2079 mock_service->set_profile(profile);
2080 ServiceRefPtr mock_service_generic(mock_service.get());
2081
2082 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002083 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002084 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2085 .WillOnce(Return(mock_service));
2086 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2087 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2088 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2089 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2090
2091 Error error;
2092 ServiceRefPtr service =
2093 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2094 EXPECT_TRUE(error.IsSuccess());
2095 EXPECT_EQ(mock_service.get(), service.get());
2096 EXPECT_EQ(profile.get(), service->profile().get());
2097 mock_service->set_profile(NULL); // Breaks reference cycle.
2098}
2099
2100TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2101 const string kProfileName = "profile";
2102 scoped_refptr<MockProfile> profile(
2103 AddNamedMockProfileToManager(manager(), kProfileName));
2104
2105 scoped_refptr<MockWiFiService> mock_service(
2106 new NiceMock<MockWiFiService>(control_interface(),
2107 dispatcher(),
2108 metrics(),
2109 manager(),
2110 wifi_provider_,
2111 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002112 kModeManaged,
2113 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002114 false));
2115 mock_service->set_profile(GetEphemeralProfile(manager()));
2116 ServiceRefPtr mock_service_generic(mock_service.get());
2117
2118 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002119 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002120 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2121 .WillOnce(Return(mock_service));
2122 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2123 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2124 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2125
2126 Error error;
2127 ServiceRefPtr service =
2128 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2129 EXPECT_TRUE(error.IsSuccess());
2130 EXPECT_EQ(mock_service.get(), service.get());
2131 EXPECT_EQ(profile.get(), service->profile().get());
2132 mock_service->set_profile(NULL); // Breaks reference cycle.
2133}
2134
2135TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2136 const string kProfileName0 = "profile0";
2137 scoped_refptr<MockProfile> profile0(
2138 AddNamedMockProfileToManager(manager(), kProfileName0));
2139 const string kProfileName1 = "profile1";
2140 scoped_refptr<MockProfile> profile1(
2141 AddNamedMockProfileToManager(manager(), kProfileName1));
2142
2143 scoped_refptr<MockWiFiService> mock_service(
2144 new NiceMock<MockWiFiService>(control_interface(),
2145 dispatcher(),
2146 metrics(),
2147 manager(),
2148 wifi_provider_,
2149 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002150 kModeManaged,
2151 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002152 false));
2153 manager()->RegisterService(mock_service);
2154 mock_service->set_profile(profile0);
2155 ServiceRefPtr mock_service_generic(mock_service.get());
2156
2157 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002158 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002159 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2160 .WillOnce(Return(mock_service));
2161 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2162 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2163 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2164 // This happens once to make the service loadable for the ConfigureService
2165 // below, and a second time after the service is modified.
2166 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2167 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2168 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2169 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2170
2171 Error error;
2172 ServiceRefPtr service =
2173 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2174 EXPECT_TRUE(error.IsSuccess());
2175 EXPECT_EQ(mock_service.get(), service.get());
2176 mock_service->set_profile(NULL); // Breaks reference cycle.
2177}
2178
2179TEST_F(ManagerTest,
2180 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2181 const string kProfileName0 = "profile0";
2182 scoped_refptr<MockProfile> profile0(
2183 AddNamedMockProfileToManager(manager(), kProfileName0));
2184 const string kProfileName1 = "profile1";
2185 scoped_refptr<MockProfile> profile1(
2186 AddNamedMockProfileToManager(manager(), kProfileName1));
2187
2188 scoped_refptr<MockWiFiService> matching_service(
2189 new StrictMock<MockWiFiService>(control_interface(),
2190 dispatcher(),
2191 metrics(),
2192 manager(),
2193 wifi_provider_,
2194 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002195 kModeManaged,
2196 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002197 false));
2198 matching_service->set_profile(profile1);
2199
2200 // We need to get rid of our reference to this mock service as soon
2201 // as Manager::ConfigureServiceForProfile() takes a reference in its
2202 // call to WiFiProvider::CreateTemporaryService(). This way the
2203 // latter function can keep a DCHECK(service->HasOneRef() even in
2204 // unit tests.
2205 temp_mock_service_ =
2206 new NiceMock<MockWiFiService>(control_interface(),
2207 dispatcher(),
2208 metrics(),
2209 manager(),
2210 wifi_provider_,
2211 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002212 kModeManaged,
2213 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002214 false);
2215
2216 // Only hold a pointer here so we don't affect the refcount.
2217 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2218
2219 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002220 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002221 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2222 .WillOnce(Return(matching_service));
2223 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2224 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2225 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2226 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2227 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2228 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2229 .Times(1);
2230 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2231 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2232
2233 Error error;
2234 ServiceRefPtr service =
2235 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2236 EXPECT_TRUE(error.IsSuccess());
2237 EXPECT_EQ(NULL, service.get());
2238 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2239}
2240
Paul Stewart7a20aa42013-01-17 12:21:41 -08002241TEST_F(ManagerTest, FindMatchingService) {
2242 KeyValueStore args;
2243 {
2244 Error error;
2245 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2246 EXPECT_EQ(Error::kNotFound, error.type());
2247 }
2248
2249 scoped_refptr<MockService> mock_service0(
2250 new NiceMock<MockService>(control_interface(),
2251 dispatcher(),
2252 metrics(),
2253 manager()));
2254 scoped_refptr<MockService> mock_service1(
2255 new NiceMock<MockService>(control_interface(),
2256 dispatcher(),
2257 metrics(),
2258 manager()));
2259 manager()->RegisterService(mock_service0);
2260 manager()->RegisterService(mock_service1);
2261 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2262 .WillOnce(Return(true))
2263 .WillRepeatedly(Return(false));
2264 {
2265 Error error;
2266 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2267 EXPECT_TRUE(error.IsSuccess());
2268 }
2269 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2270 .WillOnce(Return(true))
2271 .WillRepeatedly(Return(false));
2272 {
2273 Error error;
2274 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2275 EXPECT_TRUE(error.IsSuccess());
2276 }
2277 {
2278 Error error;
2279 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2280 EXPECT_EQ(Error::kNotFound, error.type());
2281 }
2282}
2283
Paul Stewart22aa71b2011-09-16 12:15:11 -07002284TEST_F(ManagerTest, TechnologyOrder) {
2285 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002286 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2287 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002288 ASSERT_TRUE(error.IsSuccess());
2289 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002290 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002291
Ben Chan923a5022013-09-20 11:23:23 -07002292 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2293 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002294 ASSERT_FALSE(error.IsSuccess());
2295 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002296 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002297 manager()->GetTechnologyOrder());
2298}
2299
2300TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002301 // TODO(quiche): Some of these tests would probably fit better in
2302 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002303 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002304
mukesh agrawale37ad322013-10-08 16:33:56 -07002305 // Construct our Services so that the string comparison of
2306 // unique_name_ differs from the numerical comparison of
2307 // serial_number_.
2308 vector<scoped_refptr<MockService>> mock_services;
2309 for (size_t i = 0; i < 11; ++i) {
2310 mock_services.push_back(
2311 new NiceMock<MockService>(control_interface(),
2312 dispatcher(),
2313 metrics(),
2314 manager()));
2315 }
2316 scoped_refptr<MockService> mock_service2 = mock_services[2];
2317 scoped_refptr<MockService> mock_service10 = mock_services[10];
2318 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002319
mukesh agrawale37ad322013-10-08 16:33:56 -07002320 manager()->RegisterService(mock_service2);
2321 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002322
mukesh agrawale37ad322013-10-08 16:33:56 -07002323 // Services should already be sorted by |serial_number_|.
2324 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002325
2326 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002327 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002328 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002329
2330 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002331 mock_service10->SetStrength(1);
2332 manager()->UpdateService(mock_service10);
2333 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002334
Rebecca Silberstein6d4836b2014-07-16 14:28:28 -07002335
2336 // Sort services based on profiles. When comparing two services
2337 // with different profiles, prefer the one that is not ephemeral.
2338 // If both services have legitimate profiles, prefer the service
2339 // with the more recently applied profile.
2340 // Compare between an ephemeral and actual profile.
2341 Error error;
2342 scoped_refptr<MockProfile> default_profile(
2343 new MockProfile(control_interface(), metrics(), manager(), ""));
2344 string default_profile_name("/profile/default");
2345 EXPECT_CALL(*default_profile, GetRpcIdentifier())
2346 .WillRepeatedly(Return(default_profile_name));
2347 AdoptProfile(manager(), default_profile);
2348 mock_service2->set_profile(default_profile);
2349 manager()->UpdateService(mock_service2);
2350 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
2351
2352 // Compare between two different profiles
2353 scoped_refptr<MockProfile> user_profile(
2354 new MockProfile(control_interface(), metrics(), manager(), ""));
2355 string user_profile_name("/profile/chonos/shill");
2356 EXPECT_CALL(*user_profile, GetRpcIdentifier())
2357 .WillRepeatedly(Return(user_profile_name));
2358 AdoptProfile(manager(), user_profile);
2359 mock_service10->set_profile(user_profile);
2360 manager()->UpdateService(mock_service10);
2361 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2362
Paul Stewart22aa71b2011-09-16 12:15:11 -07002363 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002364 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2365 manager()->UpdateService(mock_service2);
2366 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002367
2368 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002369 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002370 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002371 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002372 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002373
mukesh agrawal84de5d22012-02-17 19:29:15 -08002374 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002375 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002376 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002377
Ben Chan923a5022013-09-20 11:23:23 -07002378 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2379 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002380 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002381 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002382
Gaurav Shah435de2c2011-11-17 19:01:07 -08002383 // Priority.
mukesh agrawale37ad322013-10-08 16:33:56 -07002384 mock_service2->SetPriority(1, NULL);
2385 manager()->UpdateService(mock_service2);
2386 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002387
Paul Stewart2da34c02013-10-17 15:28:56 -07002388 // HasEverConnected.
2389 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002390 manager()->UpdateService(mock_service10);
2391 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002392
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002393 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002394 mock_service2->SetAutoConnect(true);
2395 manager()->UpdateService(mock_service2);
2396 mock_service10->SetAutoConnect(false);
2397 manager()->UpdateService(mock_service10);
2398 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002399
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002400 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2401 // to any of the others below, so we reset to the default state after
2402 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002403 EXPECT_CALL(*mock_service10.get(),
2404 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002405 .WillOnce(Return(true))
2406 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002407 manager()->UpdateService(mock_service10);
2408 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2409 manager()->UpdateService(mock_service2);
2410 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002411
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002412 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002413 mock_service10->SetConnectable(true);
2414 manager()->UpdateService(mock_service10);
2415 mock_service2->SetConnectable(false);
2416 manager()->UpdateService(mock_service2);
2417 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002418
2419 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002420 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002421 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002422 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002423 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002424 manager()->UpdateService(mock_service2);
2425 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002426 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002427 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002428 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002429 manager()->UpdateService(mock_service10);
2430 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002431
2432 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002433 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002434 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002435 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002436 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002437 manager()->UpdateService(mock_service10);
2438 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002439
mukesh agrawale37ad322013-10-08 16:33:56 -07002440 // Connected-but-portalled preferred over unconnected.
2441 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002442 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002443 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002444 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002445 manager()->UpdateService(mock_service2);
2446 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002447
mukesh agrawale37ad322013-10-08 16:33:56 -07002448 // Connected preferred over connected-but-portalled.
2449 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002450 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002451 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002452 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002453 manager()->UpdateService(mock_service10);
2454 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002455
mukesh agrawale37ad322013-10-08 16:33:56 -07002456 manager()->DeregisterService(mock_service2);
2457 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002458}
2459
Peter Qiu700de642014-07-14 16:31:30 -07002460TEST_F(ManagerTest, ConnectionStatusCheck) {
2461 // Setup mock metrics and service.
2462 MockMetrics mock_metrics(dispatcher());
2463 SetMetrics(&mock_metrics);
2464 scoped_refptr<MockService> mock_service = new NiceMock<MockService>(
2465 control_interface(), dispatcher(), metrics(), manager());
2466 manager()->RegisterService(mock_service);
2467
2468 // Device not connected.
2469 EXPECT_CALL(*mock_service.get(), IsConnected())
2470 .WillOnce(Return(false));
2471 EXPECT_CALL(mock_metrics,
2472 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOffline));
Peter Qiu94d18af2014-09-11 15:54:15 -07002473 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002474
2475 // Device connected, but not online.
2476 EXPECT_CALL(*mock_service.get(), IsConnected())
2477 .WillOnce(Return(true));
2478 EXPECT_CALL(*mock_service.get(), IsOnline())
2479 .WillOnce(Return(false));
2480 EXPECT_CALL(mock_metrics,
2481 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline)).Times(0);
2482 EXPECT_CALL(mock_metrics,
2483 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002484 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002485
2486 // Device connected and online.
2487 EXPECT_CALL(*mock_service.get(), IsConnected())
2488 .WillOnce(Return(true));
2489 EXPECT_CALL(*mock_service.get(), IsOnline())
2490 .WillOnce(Return(true));
2491 EXPECT_CALL(mock_metrics,
2492 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline));
2493 EXPECT_CALL(mock_metrics,
2494 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002495 manager()->ConnectionStatusCheck();
2496}
2497
2498TEST_F(ManagerTest, DevicePresenceStatusCheck) {
2499 // Setup mock metrics and service.
2500 MockMetrics mock_metrics(dispatcher());
2501 SetMetrics(&mock_metrics);
2502
2503 manager()->RegisterDevice(mock_devices_[0]);
2504 manager()->RegisterDevice(mock_devices_[1]);
2505 manager()->RegisterDevice(mock_devices_[2]);
2506 manager()->RegisterDevice(mock_devices_[3]);
2507
2508 ON_CALL(*mock_devices_[0].get(), technology())
2509 .WillByDefault(Return(Technology::kEthernet));
2510 ON_CALL(*mock_devices_[1].get(), technology())
2511 .WillByDefault(Return(Technology::kWifi));
2512 ON_CALL(*mock_devices_[2].get(), technology())
2513 .WillByDefault(Return(Technology::kCellular));
2514 ON_CALL(*mock_devices_[3].get(), technology())
2515 .WillByDefault(Return(Technology::kWifi));
2516
2517 EXPECT_CALL(mock_metrics,
2518 NotifyDevicePresenceStatus(Technology::kEthernet, true));
2519 EXPECT_CALL(mock_metrics,
2520 NotifyDevicePresenceStatus(Technology::kWifi, true));
2521 EXPECT_CALL(mock_metrics,
2522 NotifyDevicePresenceStatus(Technology::kWiMax, false));
2523 EXPECT_CALL(mock_metrics,
2524 NotifyDevicePresenceStatus(Technology::kCellular, true));
2525 manager()->DevicePresenceStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002526}
2527
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002528TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002529 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002530 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002531
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002532 scoped_refptr<MockService> mock_service0(
2533 new NiceMock<MockService>(control_interface(),
2534 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002535 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002536 manager()));
2537 scoped_refptr<MockService> mock_service1(
2538 new NiceMock<MockService>(control_interface(),
2539 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002540 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002541 manager()));
2542
2543 scoped_refptr<MockConnection> mock_connection0(
2544 new NiceMock<MockConnection>(device_info_.get()));
2545 scoped_refptr<MockConnection> mock_connection1(
2546 new NiceMock<MockConnection>(device_info_.get()));
2547
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002548 // A single registered Service, without a connection. The
2549 // DefaultService should be NULL. If a change notification is
2550 // generated, it should reference kNullPath.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002551 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002552 EXPECT_CALL(*manager_adaptor_,
2553 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2554 DBusAdaptor::kNullPath))
2555 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002556 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002557 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002558
2559 // Adding another Service, also without a connection, does not
2560 // change DefaultService. Furthermore, we do not send a change
2561 // notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002562 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002563 EXPECT_CALL(*manager_adaptor_,
2564 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2565 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002566 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002567 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002568
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002569 // An explicit sort doesn't change anything, and does not emit a
2570 // change notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002571 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002572 EXPECT_CALL(*manager_adaptor_,
2573 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2574 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002575 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002576 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002577
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002578 // Re-ordering the unconnected Services doesn't change
2579 // DefaultService, and (hence) does not emit a change notification
2580 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002581 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002582 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002583 EXPECT_CALL(*manager_adaptor_,
2584 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2585 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002586 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002587 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002588
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002589 // Re-ordering the unconnected Services doesn't change
2590 // DefaultService, and (hence) does not emit a change notification
2591 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002592 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002593 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002594 EXPECT_CALL(*manager_adaptor_,
2595 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2596 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002597 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002598 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002599
Paul Stewartce4ec192012-03-14 12:53:46 -07002600 mock_service0->set_mock_connection(mock_connection0);
2601 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002602
mukesh agrawale37ad322013-10-08 16:33:56 -07002603 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002604 // from ServiceOrderIs. We notify others of the change in
2605 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002606 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002607 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002608 EXPECT_CALL(*manager_adaptor_,
2609 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002610 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002611 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002612
Darin Petkova5e07ef2012-07-09 14:27:57 +02002613 ServiceWatcher service_watcher;
2614 int tag =
2615 manager()->RegisterDefaultServiceCallback(
2616 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2617 service_watcher.AsWeakPtr()));
2618 EXPECT_EQ(1, tag);
2619
mukesh agrawale37ad322013-10-08 16:33:56 -07002620 // Changing the ordering causes the DefaultService to change, and
2621 // appropriate notifications are sent.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002622 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002623 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2624 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002625 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002626 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002627 EXPECT_CALL(*manager_adaptor_,
2628 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002629 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002630 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002631
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002632 // Deregistering a DefaultServiceCallback works as expected. (Later
2633 // code causes DefaultService changes, but we see no further calls
2634 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002635 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002636 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002637
2638 // Deregistering the current DefaultService causes the other Service
2639 // to become default. Appropriate notifications are sent.
2640 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002641 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002642 EXPECT_CALL(*manager_adaptor_,
2643 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002644 mock_service1->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002645 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002646 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002647
mukesh agrawale37ad322013-10-08 16:33:56 -07002648 // Deregistering the only Service causes the DefaultService to become
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002649 // NULL. Appropriate notifications are sent.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002650 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002651 EXPECT_CALL(*manager_adaptor_,
2652 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002653 mock_service0->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002654 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002655 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002656
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002657 // An explicit sort doesn't change anything, and does not generate
2658 // an external notification.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002659 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002660 EXPECT_CALL(*manager_adaptor_,
2661 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002662 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002663}
2664
Darin Petkova5e07ef2012-07-09 14:27:57 +02002665TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2666 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2667 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2668
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002669 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002670 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002671
2672 scoped_refptr<MockService> mock_service(
2673 new NiceMock<MockService>(
2674 control_interface(), dispatcher(), metrics(), manager()));
2675 ServiceRefPtr service = mock_service;
2676 ServiceRefPtr null_service;
2677
2678 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2679 manager()->NotifyDefaultServiceChanged(null_service);
2680
2681 ServiceWatcher service_watcher1;
2682 ServiceWatcher service_watcher2;
2683 int tag1 =
2684 manager()->RegisterDefaultServiceCallback(
2685 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2686 service_watcher1.AsWeakPtr()));
2687 EXPECT_EQ(1, tag1);
2688 int tag2 =
2689 manager()->RegisterDefaultServiceCallback(
2690 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2691 service_watcher2.AsWeakPtr()));
2692 EXPECT_EQ(2, tag2);
2693
2694 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2695 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2696 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2697 manager()->NotifyDefaultServiceChanged(null_service);
2698
2699 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2700 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2701 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2702 manager()->NotifyDefaultServiceChanged(mock_service);
2703
2704 manager()->DeregisterDefaultServiceCallback(tag1);
2705 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2706 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2707 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2708 manager()->NotifyDefaultServiceChanged(mock_service);
2709 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2710
2711 manager()->DeregisterDefaultServiceCallback(tag2);
2712 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2713 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2714 manager()->NotifyDefaultServiceChanged(mock_service);
2715
2716 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2717 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2718}
2719
Peter Qiu9d581932014-04-14 16:37:37 -07002720TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2721 int connection_id1 = 100;
2722 int connection_id2 = 200;
2723 scoped_refptr<MockService> mock_service1 =
2724 new NiceMock<MockService>(control_interface(), dispatcher(),
2725 metrics(), manager());
2726 mock_service1->set_connection_id(connection_id1);
2727 scoped_refptr<MockService> mock_service2 =
2728 new NiceMock<MockService>(control_interface(), dispatcher(),
2729 metrics(), manager());
2730 mock_service2->set_connection_id(connection_id1);
2731 scoped_refptr<MockService> mock_service3 =
2732 new NiceMock<MockService>(control_interface(), dispatcher(),
2733 metrics(), manager());
2734 mock_service3->set_connection_id(connection_id2);
2735
2736 manager()->RegisterService(mock_service1);
2737 manager()->RegisterService(mock_service2);
2738 manager()->RegisterService(mock_service3);
2739
2740 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2741 manager()->ReportServicesOnSameNetwork(connection_id1);
2742
2743 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2744 manager()->ReportServicesOnSameNetwork(connection_id2);
2745}
2746
Gaurav Shah435de2c2011-11-17 19:01:07 -08002747TEST_F(ManagerTest, AvailableTechnologies) {
2748 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2749 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002750 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002751 manager(),
2752 "null4",
2753 "addr4",
2754 0));
2755 manager()->RegisterDevice(mock_devices_[0]);
2756 manager()->RegisterDevice(mock_devices_[1]);
2757 manager()->RegisterDevice(mock_devices_[2]);
2758 manager()->RegisterDevice(mock_devices_[3]);
2759
2760 ON_CALL(*mock_devices_[0].get(), technology())
2761 .WillByDefault(Return(Technology::kEthernet));
2762 ON_CALL(*mock_devices_[1].get(), technology())
2763 .WillByDefault(Return(Technology::kWifi));
2764 ON_CALL(*mock_devices_[2].get(), technology())
2765 .WillByDefault(Return(Technology::kCellular));
2766 ON_CALL(*mock_devices_[3].get(), technology())
2767 .WillByDefault(Return(Technology::kWifi));
2768
2769 set<string> expected_technologies;
2770 expected_technologies.insert(Technology::NameFromIdentifier(
2771 Technology::kEthernet));
2772 expected_technologies.insert(Technology::NameFromIdentifier(
2773 Technology::kWifi));
2774 expected_technologies.insert(Technology::NameFromIdentifier(
2775 Technology::kCellular));
2776 Error error;
2777 vector<string> technologies = manager()->AvailableTechnologies(&error);
2778
2779 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2780 ContainerEq(expected_technologies));
2781}
2782
2783TEST_F(ManagerTest, ConnectedTechnologies) {
2784 scoped_refptr<MockService> connected_service1(
2785 new NiceMock<MockService>(control_interface(),
2786 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002787 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002788 manager()));
2789 scoped_refptr<MockService> connected_service2(
2790 new NiceMock<MockService>(control_interface(),
2791 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002792 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002793 manager()));
2794 scoped_refptr<MockService> disconnected_service1(
2795 new NiceMock<MockService>(control_interface(),
2796 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002797 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002798 manager()));
2799 scoped_refptr<MockService> disconnected_service2(
2800 new NiceMock<MockService>(control_interface(),
2801 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002802 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002803 manager()));
2804
2805 ON_CALL(*connected_service1.get(), IsConnected())
2806 .WillByDefault(Return(true));
2807 ON_CALL(*connected_service2.get(), IsConnected())
2808 .WillByDefault(Return(true));
2809
2810 manager()->RegisterService(connected_service1);
2811 manager()->RegisterService(connected_service2);
2812 manager()->RegisterService(disconnected_service1);
2813 manager()->RegisterService(disconnected_service2);
2814
2815 manager()->RegisterDevice(mock_devices_[0]);
2816 manager()->RegisterDevice(mock_devices_[1]);
2817 manager()->RegisterDevice(mock_devices_[2]);
2818 manager()->RegisterDevice(mock_devices_[3]);
2819
2820 ON_CALL(*mock_devices_[0].get(), technology())
2821 .WillByDefault(Return(Technology::kEthernet));
2822 ON_CALL(*mock_devices_[1].get(), technology())
2823 .WillByDefault(Return(Technology::kWifi));
2824 ON_CALL(*mock_devices_[2].get(), technology())
2825 .WillByDefault(Return(Technology::kCellular));
2826 ON_CALL(*mock_devices_[3].get(), technology())
2827 .WillByDefault(Return(Technology::kWifi));
2828
2829 mock_devices_[0]->SelectService(connected_service1);
2830 mock_devices_[1]->SelectService(disconnected_service1);
2831 mock_devices_[2]->SelectService(disconnected_service2);
2832 mock_devices_[3]->SelectService(connected_service2);
2833
2834 set<string> expected_technologies;
2835 expected_technologies.insert(Technology::NameFromIdentifier(
2836 Technology::kEthernet));
2837 expected_technologies.insert(Technology::NameFromIdentifier(
2838 Technology::kWifi));
2839 Error error;
2840
2841 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2842 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2843 ContainerEq(expected_technologies));
2844}
2845
2846TEST_F(ManagerTest, DefaultTechnology) {
2847 scoped_refptr<MockService> connected_service(
2848 new NiceMock<MockService>(control_interface(),
2849 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002850 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002851 manager()));
2852 scoped_refptr<MockService> disconnected_service(
2853 new NiceMock<MockService>(control_interface(),
2854 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002855 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002856 manager()));
2857
2858 // Connected. WiFi.
2859 ON_CALL(*connected_service.get(), IsConnected())
2860 .WillByDefault(Return(true));
2861 ON_CALL(*connected_service.get(), state())
2862 .WillByDefault(Return(Service::kStateConnected));
2863 ON_CALL(*connected_service.get(), technology())
2864 .WillByDefault(Return(Technology::kWifi));
2865
2866 // Disconnected. Ethernet.
2867 ON_CALL(*disconnected_service.get(), technology())
2868 .WillByDefault(Return(Technology::kEthernet));
2869
2870 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002871 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002872 Error error;
2873 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2874
2875
2876 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002877 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002878 // Connected service should be brought to the front now.
2879 string expected_technology =
2880 Technology::NameFromIdentifier(Technology::kWifi);
2881 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2882}
2883
Paul Stewart212d60f2012-07-12 10:59:13 -07002884TEST_F(ManagerTest, Stop) {
2885 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002886 new NiceMock<MockProfile>(
2887 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002888 AdoptProfile(manager(), profile);
2889 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002890 new NiceMock<MockService>(control_interface(),
2891 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002892 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002893 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002894 manager()->RegisterService(service);
2895 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002896 SetPowerManager();
2897 EXPECT_TRUE(manager()->power_manager());
Paul Stewart212d60f2012-07-12 10:59:13 -07002898 EXPECT_CALL(*profile.get(),
2899 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2900 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002901 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002902 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
Samuel Tan0d061192014-07-07 15:45:15 -07002903 EXPECT_CALL(*service.get(), Disconnect(_, StrEq("Stop"))).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002904 manager()->Stop();
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002905 EXPECT_FALSE(manager()->power_manager());
Thieu Le1271d682011-11-02 22:48:19 +00002906}
2907
mukesh agrawal00917ce2011-11-22 23:56:55 +00002908TEST_F(ManagerTest, UpdateServiceConnected) {
2909 scoped_refptr<MockService> mock_service(
2910 new NiceMock<MockService>(control_interface(),
2911 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002912 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002913 manager()));
2914 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002915 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002916 EXPECT_FALSE(mock_service->auto_connect());
2917
Gaurav Shah435de2c2011-11-17 19:01:07 -08002918 EXPECT_CALL(*mock_service.get(), IsConnected())
2919 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002920 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002921 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2922 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2923 // would break the SortServices test. (crbug.com/206367)
2924 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002925 EXPECT_TRUE(mock_service->auto_connect());
2926}
2927
Paul Stewart2da34c02013-10-17 15:28:56 -07002928TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002929 // This tests the case where the user connects to a service that is
2930 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002931 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002932 scoped_refptr<MockService> mock_service(
2933 new NiceMock<MockService>(control_interface(),
2934 dispatcher(),
2935 metrics(),
2936 manager()));
2937 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002938 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002939 EXPECT_FALSE(mock_service->auto_connect());
2940
Gary Moraind93615e2012-04-27 11:50:03 -07002941 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002942 new MockProfile(
2943 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002944
Gary Moraind93615e2012-04-27 11:50:03 -07002945 mock_service->set_profile(profile);
2946 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002947 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002948 EXPECT_CALL(*profile,
2949 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002950 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002951 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2952 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2953 // would break the SortServices test. (crbug.com/206367)
2954 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002955 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002956 // This releases the ref on the mock profile.
2957 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002958}
2959
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002960TEST_F(ManagerTest, SaveSuccessfulService) {
2961 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002962 new StrictMock<MockProfile>(
2963 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002964 AdoptProfile(manager(), profile);
2965 scoped_refptr<MockService> service(
2966 new NiceMock<MockService>(control_interface(),
2967 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002968 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002969 manager()));
2970
2971 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2972 ServiceRefPtr expect_service(service.get());
2973
2974 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2975 .WillOnce(Return(false));
2976 manager()->RegisterService(service);
2977
2978 EXPECT_CALL(*service.get(), state())
2979 .WillRepeatedly(Return(Service::kStateConnected));
2980 EXPECT_CALL(*service.get(), IsConnected())
2981 .WillRepeatedly(Return(true));
2982 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2983 .WillOnce(Return(true));
2984 manager()->UpdateService(service);
2985}
2986
Darin Petkove7c6ad32012-06-29 10:22:09 +02002987TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002988 MockProfile *profile0 =
2989 new MockProfile(control_interface(), metrics(), manager(), "");
2990 MockProfile *profile1 =
2991 new MockProfile(control_interface(), metrics(), manager(), "");
2992 MockProfile *profile2 =
2993 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002994 AdoptProfile(manager(), profile0); // Passes ownership.
2995 AdoptProfile(manager(), profile1); // Passes ownership.
2996 AdoptProfile(manager(), profile2); // Passes ownership.
2997 DeviceRefPtr device_ref(mock_devices_[0].get());
2998 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2999 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
3000 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
3001 manager()->UpdateDevice(mock_devices_[0]);
3002}
3003
Paul Stewart1b253142012-01-26 14:05:52 -08003004TEST_F(ManagerTest, EnumerateProfiles) {
3005 vector<string> profile_paths;
3006 for (size_t i = 0; i < 10; i++) {
3007 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003008 new StrictMock<MockProfile>(
3009 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05003010 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08003011 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
3012 .WillOnce(Return(profile_paths.back()));
3013 AdoptProfile(manager(), profile);
3014 }
3015
3016 Error error;
3017 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
3018 EXPECT_TRUE(error.IsSuccess());
3019 EXPECT_EQ(profile_paths.size(), returned_paths.size());
3020 for (size_t i = 0; i < profile_paths.size(); i++) {
3021 EXPECT_EQ(profile_paths[i], returned_paths[i]);
3022 }
3023}
3024
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003025TEST_F(ManagerTest, AutoConnectOnRegister) {
3026 MockServiceRefPtr service = MakeAutoConnectableService();
3027 EXPECT_CALL(*service.get(), AutoConnect());
3028 manager()->RegisterService(service);
3029 dispatcher()->DispatchPendingEvents();
3030}
3031
3032TEST_F(ManagerTest, AutoConnectOnUpdate) {
3033 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003034 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003035 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003036 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003037 manager()->RegisterService(service1);
3038 manager()->RegisterService(service2);
3039 dispatcher()->DispatchPendingEvents();
3040
3041 EXPECT_CALL(*service1.get(), AutoConnect());
3042 EXPECT_CALL(*service2.get(), state())
3043 .WillRepeatedly(Return(Service::kStateFailure));
3044 EXPECT_CALL(*service2.get(), IsFailed())
3045 .WillRepeatedly(Return(true));
3046 EXPECT_CALL(*service2.get(), IsConnected())
3047 .WillRepeatedly(Return(false));
3048 manager()->UpdateService(service2);
3049 dispatcher()->DispatchPendingEvents();
3050}
3051
3052TEST_F(ManagerTest, AutoConnectOnDeregister) {
3053 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003054 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003055 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003056 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003057 manager()->RegisterService(service1);
3058 manager()->RegisterService(service2);
3059 dispatcher()->DispatchPendingEvents();
3060
3061 EXPECT_CALL(*service1.get(), AutoConnect());
3062 manager()->DeregisterService(service2);
3063 dispatcher()->DispatchPendingEvents();
3064}
3065
Daniel Eratfac09532014-04-17 20:25:59 -07003066TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003067 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003068 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02003069 SetPowerManager();
3070 EXPECT_CALL(*service, AutoConnect()).Times(0);
3071 manager()->RegisterService(service);
3072 dispatcher()->DispatchPendingEvents();
3073}
3074
Daniel Eratfac09532014-04-17 20:25:59 -07003075TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02003076 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003077 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02003078 SetPowerManager();
3079 EXPECT_CALL(*service, AutoConnect());
3080 manager()->RegisterService(service);
3081 dispatcher()->DispatchPendingEvents();
3082}
3083
Paul Stewart63864b62012-11-07 15:10:55 -08003084TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
3085 SetRunning(false);
3086 MockServiceRefPtr service = MakeAutoConnectableService();
3087 EXPECT_CALL(*service, AutoConnect()).Times(0);
3088 manager()->RegisterService(service);
3089 dispatcher()->DispatchPendingEvents();
3090}
3091
Daniel Eratfac09532014-04-17 20:25:59 -07003092TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02003093 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02003094 SetPowerManager();
3095 EXPECT_CALL(*service, AutoConnect());
3096 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07003097 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003098 dispatcher()->DispatchPendingEvents();
3099
Daniel Eratfac09532014-04-17 20:25:59 -07003100 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003101 OnSuspendImminent();
Daniel Eratfac09532014-04-17 20:25:59 -07003102 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02003103 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003104 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003105
Daniel Eratfac09532014-04-17 20:25:59 -07003106 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003107 OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -07003108 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02003109 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003110 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003111}
3112
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07003113TEST_F(ManagerTest, AddTerminationAction) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003114 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3115 manager()->AddTerminationAction("action1", base::Closure());
3116 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3117 manager()->AddTerminationAction("action2", base::Closure());
3118}
3119
3120TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003121 const char kKey1[] = "action1";
3122 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02003123
Ben Chan16d3acb2014-05-13 22:08:14 -07003124 // Removing an action when the hook table is empty.
Darin Petkov3ec55342012-09-28 14:04:44 +02003125 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3126 manager()->RemoveTerminationAction("unknown");
Darin Petkov3ec55342012-09-28 14:04:44 +02003127
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07003128 // Fill hook table with two items.
Darin Petkov3ec55342012-09-28 14:04:44 +02003129 manager()->AddTerminationAction(kKey1, base::Closure());
3130 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3131 manager()->AddTerminationAction(kKey2, base::Closure());
3132
Ben Chan16d3acb2014-05-13 22:08:14 -07003133 // Removing an action that ends up with a non-empty hook table.
Darin Petkov3ec55342012-09-28 14:04:44 +02003134 manager()->RemoveTerminationAction(kKey1);
3135 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
Darin Petkov3ec55342012-09-28 14:04:44 +02003136
Ben Chan16d3acb2014-05-13 22:08:14 -07003137 // Removing the last action.
Darin Petkov3ec55342012-09-28 14:04:44 +02003138 manager()->RemoveTerminationAction(kKey2);
3139 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3140}
3141
3142TEST_F(ManagerTest, RunTerminationActions) {
3143 TerminationActionTest test_action;
3144 const string kActionName = "action";
3145
3146 EXPECT_CALL(test_action, Done(_));
3147 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3148 test_action.AsWeakPtr()));
3149
3150 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3151 Bind(&TerminationActionTest::Action,
3152 test_action.AsWeakPtr()));
3153 test_action.set_manager(manager());
3154 EXPECT_CALL(test_action, Done(_));
3155 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3156 test_action.AsWeakPtr()));
3157}
3158
Daniel Erat0818cca2012-12-14 10:16:21 -08003159TEST_F(ManagerTest, OnSuspendImminent) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003160 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003161 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003162 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003163 OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +02003164}
3165
3166TEST_F(ManagerTest, OnSuspendActionsComplete) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003167 Error error;
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003168 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003169 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003170 OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003171}
3172
Paul Stewartc681fa02012-03-02 19:40:04 -08003173TEST_F(ManagerTest, RecheckPortal) {
3174 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3175 .WillOnce(Return(false));
3176 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3177 .WillOnce(Return(true));
3178 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3179 .Times(0);
3180
3181 manager()->RegisterDevice(mock_devices_[0]);
3182 manager()->RegisterDevice(mock_devices_[1]);
3183 manager()->RegisterDevice(mock_devices_[2]);
3184
3185 manager()->RecheckPortal(NULL);
3186}
3187
Paul Stewartd215af62012-04-24 23:25:50 -07003188TEST_F(ManagerTest, RecheckPortalOnService) {
3189 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3190 dispatcher(),
3191 metrics(),
3192 manager());
3193 EXPECT_CALL(*mock_devices_[0].get(),
3194 IsConnectedToService(IsRefPtrTo(service)))
3195 .WillOnce(Return(false));
3196 EXPECT_CALL(*mock_devices_[1].get(),
3197 IsConnectedToService(IsRefPtrTo(service)))
3198 .WillOnce(Return(true));
3199 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3200 .WillOnce(Return(true));
3201 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3202 .Times(0);
3203
3204 manager()->RegisterDevice(mock_devices_[0]);
3205 manager()->RegisterDevice(mock_devices_[1]);
3206 manager()->RegisterDevice(mock_devices_[2]);
3207
3208 manager()->RecheckPortalOnService(service);
3209}
3210
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003211TEST_F(ManagerTest, GetDefaultService) {
3212 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003213 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003214
3215 scoped_refptr<MockService> mock_service(
3216 new NiceMock<MockService>(control_interface(),
3217 dispatcher(),
3218 metrics(),
3219 manager()));
3220
3221 manager()->RegisterService(mock_service);
3222 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003223 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003224
3225 scoped_refptr<MockConnection> mock_connection(
3226 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003227 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003228 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003229 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003230
Paul Stewartce4ec192012-03-14 12:53:46 -07003231 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003232 manager()->DeregisterService(mock_service);
3233}
3234
Paul Stewart13ed2252012-03-21 12:52:46 -07003235TEST_F(ManagerTest, GetServiceWithGUID) {
3236 scoped_refptr<MockService> mock_service0(
3237 new NiceMock<MockService>(control_interface(),
3238 dispatcher(),
3239 metrics(),
3240 manager()));
3241
3242 scoped_refptr<MockService> mock_service1(
3243 new NiceMock<MockService>(control_interface(),
3244 dispatcher(),
3245 metrics(),
3246 manager()));
3247
Paul Stewartcb59fed2012-03-21 21:14:46 -07003248 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3249 .Times(0);
3250 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3251 .Times(0);
3252
Paul Stewart13ed2252012-03-21 12:52:46 -07003253 manager()->RegisterService(mock_service0);
3254 manager()->RegisterService(mock_service1);
3255
3256 const string kGUID0 = "GUID0";
3257 const string kGUID1 = "GUID1";
3258
3259 {
3260 Error error;
3261 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3262 EXPECT_FALSE(error.IsSuccess());
3263 EXPECT_FALSE(service);
3264 }
3265
3266 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003267 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003268
3269 {
3270 Error error;
3271 ServiceRefPtr service = manager()->GetService(args, &error);
3272 EXPECT_EQ(Error::kInvalidArguments, error.type());
3273 EXPECT_FALSE(service);
3274 }
3275
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003276 mock_service0->SetGuid(kGUID0, NULL);
3277 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003278
3279 {
3280 Error error;
3281 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3282 EXPECT_TRUE(error.IsSuccess());
3283 EXPECT_EQ(mock_service0.get(), service.get());
3284 }
3285
3286 {
3287 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003288 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3289 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003290 ServiceRefPtr service = manager()->GetService(args, &error);
3291 EXPECT_TRUE(error.IsSuccess());
3292 EXPECT_EQ(mock_service1.get(), service.get());
3293 }
3294
3295 manager()->DeregisterService(mock_service0);
3296 manager()->DeregisterService(mock_service1);
3297}
3298
Gary Morain028545d2012-04-07 14:55:52 -07003299
3300TEST_F(ManagerTest, CalculateStateOffline) {
Peter Qiu700de642014-07-14 16:31:30 -07003301 EXPECT_FALSE(manager()->IsConnected());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003302 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3303
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003304 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003305 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003306 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3307 .Times(AnyNumber());
3308 scoped_refptr<MockService> mock_service0(
3309 new NiceMock<MockService>(control_interface(),
3310 dispatcher(),
3311 metrics(),
3312 manager()));
3313
3314 scoped_refptr<MockService> mock_service1(
3315 new NiceMock<MockService>(control_interface(),
3316 dispatcher(),
3317 metrics(),
3318 manager()));
3319
3320 EXPECT_CALL(*mock_service0.get(), IsConnected())
3321 .WillRepeatedly(Return(false));
3322 EXPECT_CALL(*mock_service1.get(), IsConnected())
3323 .WillRepeatedly(Return(false));
3324
3325 manager()->RegisterService(mock_service0);
3326 manager()->RegisterService(mock_service1);
3327
Peter Qiu700de642014-07-14 16:31:30 -07003328 EXPECT_FALSE(manager()->IsConnected());
Gary Morain028545d2012-04-07 14:55:52 -07003329 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3330
3331 manager()->DeregisterService(mock_service0);
3332 manager()->DeregisterService(mock_service1);
3333}
3334
3335TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003336 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003337 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003338 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3339 .Times(AnyNumber());
3340 scoped_refptr<MockService> mock_service0(
3341 new NiceMock<MockService>(control_interface(),
3342 dispatcher(),
3343 metrics(),
3344 manager()));
3345
3346 scoped_refptr<MockService> mock_service1(
3347 new NiceMock<MockService>(control_interface(),
3348 dispatcher(),
3349 metrics(),
3350 manager()));
3351
3352 EXPECT_CALL(*mock_service0.get(), IsConnected())
3353 .WillRepeatedly(Return(false));
3354 EXPECT_CALL(*mock_service1.get(), IsConnected())
3355 .WillRepeatedly(Return(true));
3356 EXPECT_CALL(*mock_service0.get(), state())
3357 .WillRepeatedly(Return(Service::kStateIdle));
3358 EXPECT_CALL(*mock_service1.get(), state())
3359 .WillRepeatedly(Return(Service::kStateConnected));
3360
3361 manager()->RegisterService(mock_service0);
3362 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003363 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003364
Peter Qiu700de642014-07-14 16:31:30 -07003365 EXPECT_TRUE(manager()->IsConnected());
Gary Morain028545d2012-04-07 14:55:52 -07003366 EXPECT_EQ("online", manager()->CalculateState(NULL));
3367
3368 manager()->DeregisterService(mock_service0);
3369 manager()->DeregisterService(mock_service1);
3370}
3371
Paul Stewart03e29f72013-09-26 00:49:48 -07003372TEST_F(ManagerTest, RefreshConnectionState) {
3373 EXPECT_CALL(*manager_adaptor_,
3374 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3375 RefreshConnectionState();
3376 Mock::VerifyAndClearExpectations(manager_adaptor_);
3377
3378 scoped_refptr<MockService> mock_service(
3379 new NiceMock<MockService>(control_interface(),
3380 dispatcher(),
3381 metrics(),
3382 manager()));
3383 EXPECT_CALL(*manager_adaptor_,
3384 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3385 manager()->RegisterService(mock_service);
3386 RefreshConnectionState();
3387
3388 scoped_refptr<MockConnection> mock_connection(
3389 new NiceMock<MockConnection>(device_info_.get()));
3390 mock_service->set_mock_connection(mock_connection);
3391 EXPECT_CALL(*mock_service, state())
3392 .WillOnce(Return(Service::kStateIdle));
3393 RefreshConnectionState();
3394
3395 Mock::VerifyAndClearExpectations(manager_adaptor_);
3396 EXPECT_CALL(*mock_service, state())
3397 .WillOnce(Return(Service::kStatePortal));
3398 EXPECT_CALL(*manager_adaptor_,
3399 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3400 RefreshConnectionState();
3401 Mock::VerifyAndClearExpectations(manager_adaptor_);
3402
3403 mock_service->set_mock_connection(NULL);
3404 manager()->DeregisterService(mock_service);
3405}
3406
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003407TEST_F(ManagerTest, StartupPortalList) {
3408 // Simulate loading value from the default profile.
3409 const string kProfileValue("wifi,vpn");
3410 manager()->props_.check_portal_list = kProfileValue;
3411
3412 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3413 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3414 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3415
3416 const string kStartupValue("cellular,ethernet");
3417 manager()->SetStartupPortalList(kStartupValue);
3418 // Ensure profile value is not overwritten, so when we save the default
3419 // profile, the correct value will still be written.
3420 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3421
3422 // However we should read back a different list.
3423 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3424 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3425 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3426
3427 const string kRuntimeValue("ppp");
3428 // Setting a runtime value over the control API should overwrite both
3429 // the profile value and what we read back.
3430 Error error;
3431 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003432 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003433 kRuntimeValue,
3434 &error);
3435 ASSERT_TRUE(error.IsSuccess());
3436 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3437 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3438 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3439 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3440}
3441
Paul Stewart036dba02012-08-07 12:34:41 -07003442TEST_F(ManagerTest, LinkMonitorEnabled) {
3443 const string kEnabledTechnologies("wifi,vpn");
3444 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3445 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3446 EXPECT_FALSE(
3447 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3448}
3449
Ben Chan8e6b8ef2014-07-14 21:50:18 -07003450TEST_F(ManagerTest, IsTechnologyAutoConnectDisabled) {
3451 const string kNoAutoConnectTechnologies("wifi,cellular");
3452 manager()->props_.no_auto_connect_technologies = kNoAutoConnectTechnologies;
3453 EXPECT_TRUE(manager()->IsTechnologyAutoConnectDisabled(Technology::kWifi));
3454 EXPECT_TRUE(
3455 manager()->IsTechnologyAutoConnectDisabled(Technology::kCellular));
3456 EXPECT_FALSE(
3457 manager()->IsTechnologyAutoConnectDisabled(Technology::kEthernet));
3458}
3459
Paul Stewart85aea152013-01-22 09:31:56 -08003460TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003461 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003462 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003463 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003464 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003465 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003466 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3467 AdoptProfile(manager(), profile);
3468 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3469 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3470 scoped_ptr<MockStore> store1(new MockStore);
3471 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3472}
3473
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003474TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003475 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003476 DisableTechnologyReplyHandler disable_technology_reply_handler;
3477 ResultCallback disable_technology_callback(
3478 Bind(&DisableTechnologyReplyHandler::ReportResult,
3479 disable_technology_reply_handler.AsWeakPtr()));
3480 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3481
Ben Chan923a5022013-09-20 11:23:23 -07003482 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003483 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003484 EXPECT_TRUE(error.IsSuccess());
3485
Joshua Krollda798622012-06-05 12:30:48 -07003486 ON_CALL(*mock_devices_[0], technology())
3487 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003488 ON_CALL(*mock_devices_[1], technology())
3489 .WillByDefault(Return(Technology::kCellular));
3490 ON_CALL(*mock_devices_[2], technology())
3491 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003492
3493 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003494 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003495
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003496 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003497 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3498 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003499 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003500 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003501 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003502 EXPECT_TRUE(error.IsSuccess());
3503
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003504 // Ethernet Device is enabled, and mock doesn't change error from
3505 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003506 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3507 mock_devices_[0]->enabled_ = true;
3508 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003509 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003510 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003511 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003512
3513 // Ethernet Device is disabled, and mock doesn't change error from
3514 // kOperationInitiated, so expect enable to say operation in progress.
3515 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3516 mock_devices_[0]->enabled_ = false;
3517 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003518 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003519 &error, disable_technology_callback);
3520 EXPECT_TRUE(error.IsOngoing());
3521
3522 // Cellular Device is enabled, but disable failed.
3523 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3524 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3525 mock_devices_[1]->enabled_ = true;
3526 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003527 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003528 &error, disable_technology_callback);
3529 EXPECT_EQ(Error::kPermissionDenied, error.type());
3530
3531 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3532 // if one is in progress (even if the other completed immediately).
3533 manager()->RegisterDevice(mock_devices_[2]);
3534 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3535 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3536 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3537 mock_devices_[1]->enabled_ = true;
3538 mock_devices_[2]->enabled_ = true;
3539 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003540 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003541 &error, disable_technology_callback);
3542 EXPECT_TRUE(error.IsOngoing());
3543
3544 // ...and order doesn't matter.
3545 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3546 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3547 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3548 mock_devices_[1]->enabled_ = true;
3549 mock_devices_[2]->enabled_ = true;
3550 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003551 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003552 &error, disable_technology_callback);
3553 EXPECT_TRUE(error.IsOngoing());
3554 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3555
3556 // Multiple Cellular Devices in enabled state. Even if all disable
3557 // operations complete asynchronously, we only get one call to the
3558 // DisableTechnologyReplyHandler::ReportResult.
3559 ResultCallback device1_result_callback;
3560 ResultCallback device2_result_callback;
3561 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3562 .WillOnce(SaveArg<2>(&device1_result_callback));
3563 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3564 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3565 SaveArg<2>(&device2_result_callback)));
3566 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3567 mock_devices_[1]->enabled_ = true;
3568 mock_devices_[2]->enabled_ = true;
3569 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003570 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003571 &error, disable_technology_callback);
3572 EXPECT_TRUE(error.IsOngoing());
3573 device1_result_callback.Run(Error(Error::kSuccess));
3574 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003575}
3576
Paul Stewart4d5efb72012-09-17 12:24:34 -07003577TEST_F(ManagerTest, IgnoredSearchList) {
3578 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003579 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003580 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003581
3582 const string kIgnored0 = "chromium.org";
3583 ignored_paths.push_back(kIgnored0);
3584 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003585 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003586 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3587
3588 const string kIgnored1 = "google.com";
3589 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3590 ignored_paths.push_back(kIgnored1);
3591 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003592 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003593 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3594
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003595 ignored_paths.clear();
3596 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3597 SetIgnoredDNSSearchPaths("", NULL);
3598 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3599
Paul Stewart4d5efb72012-09-17 12:24:34 -07003600 SetResolver(Resolver::GetInstance());
3601}
3602
Paul Stewartbfb82552012-10-24 16:48:48 -07003603TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3604 // Test to make sure that every service state-change causes the
3605 // Manager to emit a new service list.
3606 scoped_refptr<MockService> mock_service(
3607 new NiceMock<MockService>(control_interface(),
3608 dispatcher(),
3609 metrics(),
3610 manager()));
3611 EXPECT_CALL(*mock_service, state())
3612 .WillRepeatedly(Return(Service::kStateIdle));
3613
3614 manager()->RegisterService(mock_service);
3615 EXPECT_CALL(
3616 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003617 kServiceCompleteListProperty, _)).Times(1);
3618 EXPECT_CALL(
3619 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003620 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003621 EXPECT_CALL(
3622 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003623 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003624 CompleteServiceSort();
3625
3626 Mock::VerifyAndClearExpectations(manager_adaptor_);
3627 EXPECT_CALL(
3628 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003629 kServiceCompleteListProperty, _)).Times(1);
3630 EXPECT_CALL(
3631 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003632 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003633 EXPECT_CALL(
3634 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003635 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003636 manager()->UpdateService(mock_service.get());
3637 CompleteServiceSort();
3638
3639 manager()->DeregisterService(mock_service);
3640}
3641
3642TEST_F(ManagerTest, EnumerateServices) {
3643 scoped_refptr<MockService> mock_service(
3644 new NiceMock<MockService>(control_interface(),
3645 dispatcher(),
3646 metrics(),
3647 manager()));
3648 manager()->RegisterService(mock_service);
3649
3650 EXPECT_CALL(*mock_service, state())
3651 .WillRepeatedly(Return(Service::kStateConnected));
3652 EXPECT_CALL(*mock_service, IsVisible())
3653 .WillRepeatedly(Return(false));
3654 EXPECT_TRUE(EnumerateAvailableServices().empty());
3655 EXPECT_TRUE(EnumerateWatchedServices().empty());
3656
3657 EXPECT_CALL(*mock_service, state())
3658 .WillRepeatedly(Return(Service::kStateIdle));
3659 EXPECT_TRUE(EnumerateAvailableServices().empty());
3660 EXPECT_TRUE(EnumerateWatchedServices().empty());
3661
3662 EXPECT_CALL(*mock_service, IsVisible())
3663 .WillRepeatedly(Return(true));
3664 Service::ConnectState unwatched_states[] = {
3665 Service::kStateUnknown,
3666 Service::kStateIdle,
3667 Service::kStateFailure
3668 };
3669 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3670 EXPECT_CALL(*mock_service, state())
3671 .WillRepeatedly(Return(unwatched_states[i]));
3672 EXPECT_FALSE(EnumerateAvailableServices().empty());
3673 EXPECT_TRUE(EnumerateWatchedServices().empty());
3674 }
3675
3676 Service::ConnectState watched_states[] = {
3677 Service::kStateAssociating,
3678 Service::kStateConfiguring,
3679 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003680 Service::kStatePortal,
3681 Service::kStateOnline
3682 };
3683 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3684 EXPECT_CALL(*mock_service, state())
3685 .WillRepeatedly(Return(watched_states[i]));
3686 EXPECT_FALSE(EnumerateAvailableServices().empty());
3687 EXPECT_FALSE(EnumerateWatchedServices().empty());
3688 }
3689
3690 manager()->DeregisterService(mock_service);
3691}
3692
Paul Stewart39db5ca2013-03-18 14:15:17 -07003693TEST_F(ManagerTest, ConnectToBestServices) {
3694 scoped_refptr<MockService> wifi_service0(
3695 new NiceMock<MockService>(control_interface(),
3696 dispatcher(),
3697 metrics(),
3698 manager()));
3699 EXPECT_CALL(*wifi_service0.get(), state())
3700 .WillRepeatedly(Return(Service::kStateIdle));
3701 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3702 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003703 wifi_service0->SetConnectable(true);
3704 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003705 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3706 EXPECT_CALL(*wifi_service0.get(), technology())
3707 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003708 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3709 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003710 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3711 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003712
3713 scoped_refptr<MockService> wifi_service1(
3714 new NiceMock<MockService>(control_interface(),
3715 dispatcher(),
3716 metrics(),
3717 manager()));
3718 EXPECT_CALL(*wifi_service1.get(), state())
3719 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003720 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3721 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003722 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3723 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003724 wifi_service1->SetAutoConnect(true);
3725 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003726 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3727 EXPECT_CALL(*wifi_service1.get(), technology())
3728 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003729 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3730 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003731
3732 scoped_refptr<MockService> wifi_service2(
3733 new NiceMock<MockService>(control_interface(),
3734 dispatcher(),
3735 metrics(),
3736 manager()));
3737 EXPECT_CALL(*wifi_service2.get(), state())
3738 .WillRepeatedly(Return(Service::kStateConnected));
3739 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3740 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003741 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3742 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003743 wifi_service2->SetAutoConnect(true);
3744 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003745 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3746 EXPECT_CALL(*wifi_service2.get(), technology())
3747 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003748 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3749 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003750
3751 manager()->RegisterService(wifi_service0);
3752 manager()->RegisterService(wifi_service1);
3753 manager()->RegisterService(wifi_service2);
3754
3755 CompleteServiceSort();
3756 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3757
3758 scoped_refptr<MockService> cell_service(
3759 new NiceMock<MockService>(control_interface(),
3760 dispatcher(),
3761 metrics(),
3762 manager()));
3763
3764 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003765 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003766 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003767 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003768 EXPECT_CALL(*cell_service.get(), IsVisible())
3769 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003770 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003771 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003772 EXPECT_CALL(*cell_service.get(), technology())
3773 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003774 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3775 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003776 manager()->RegisterService(cell_service);
3777
Arman Uguray6fe4f262013-08-02 20:21:55 -07003778 scoped_refptr<MockService> wimax_service(
3779 new NiceMock<MockService>(control_interface(),
3780 dispatcher(),
3781 metrics(),
3782 manager()));
3783
3784 EXPECT_CALL(*wimax_service.get(), state())
3785 .WillRepeatedly(Return(Service::kStateConnected));
3786 EXPECT_CALL(*wimax_service.get(), IsConnected())
3787 .WillRepeatedly(Return(true));
3788 EXPECT_CALL(*wimax_service.get(), IsVisible())
3789 .WillRepeatedly(Return(true));
3790 wimax_service->SetAutoConnect(true);
3791 wimax_service->SetConnectable(true);
3792 EXPECT_CALL(*wimax_service.get(), technology())
3793 .WillRepeatedly(Return(Technology::kWiMax));
3794 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3795 .WillRepeatedly(Return(false));
3796 manager()->RegisterService(wimax_service);
3797
Paul Stewart39db5ca2013-03-18 14:15:17 -07003798 scoped_refptr<MockService> vpn_service(
3799 new NiceMock<MockService>(control_interface(),
3800 dispatcher(),
3801 metrics(),
3802 manager()));
3803
3804 EXPECT_CALL(*vpn_service.get(), state())
3805 .WillRepeatedly(Return(Service::kStateIdle));
3806 EXPECT_CALL(*vpn_service.get(), IsConnected())
3807 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003808 EXPECT_CALL(*vpn_service.get(), IsVisible())
3809 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003810 wifi_service2->SetAutoConnect(false);
3811 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003812 EXPECT_CALL(*vpn_service.get(), technology())
3813 .WillRepeatedly(Return(Technology::kVPN));
3814 manager()->RegisterService(vpn_service);
3815
3816 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003817 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003818
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003819 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3820 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003821 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003822 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3823 .Times(0); // Explicitly disconnected.
3824 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003825 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003826
3827 manager()->ConnectToBestServices(NULL);
3828 dispatcher()->DispatchPendingEvents();
3829
3830 // After this operation, since the Connect calls above are mocked and
3831 // no actual state changes have occurred, we should expect that the
3832 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003833 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003834}
3835
Rebecca Silberstein6862b382014-09-11 08:24:51 -07003836TEST_F(ManagerTest, CreateConnectivityReport) {
3837 // TODO(silberst) Test needs full implementation.
3838 manager()->CreateConnectivityReport(NULL);
3839 dispatcher()->DispatchPendingEvents();
3840}
3841
Christopher Wiley83889862013-05-02 15:55:09 -07003842TEST_F(ManagerTest, VerifyWhenNotConnected) {
3843 const string kFakeCertificate("fake cert");
3844 const string kFakePublicKey("fake public key");
3845 const string kFakeNonce("fake public key");
3846 const string kFakeSignedData("fake signed data");
3847 const string kFakeUdn("fake udn");
3848 const vector<uint8_t> kSSID(10, 87);
3849 const string kConfiguredSSID("AConfiguredDestination");
3850 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3851 kConfiguredSSID.end());
3852 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3853 scoped_refptr<MockWiFiService> mock_destination(
3854 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3855 metrics(), manager(), wifi_provider_,
3856 kSSID, "", "none", false));
3857 // Register this service, but don't mark it as connected.
3858 manager()->RegisterService(mock_destination);
3859 // Verify that if we're not connected to anything, verification fails.
3860 {
3861 LOG(INFO) << "Can't verify if not connected.";
3862 EXPECT_CALL(*crypto_util_proxy_,
3863 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3864 Error error(Error::kOperationInitiated);
3865 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3866 kFakeSignedData, kFakeUdn, "", "",
3867 ResultBoolCallback(), &error);
3868 EXPECT_TRUE(error.IsFailure());
3869 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3870 }
3871 {
3872 // However, if the destination is already configured, we might be
3873 // connected to it via something other than WiFi, and we shouldn't
3874 // enforce the WiFi check.
3875 EXPECT_CALL(*crypto_util_proxy_,
3876 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3877 kFakeSignedData, kFakeUdn,
3878 kConfiguredSSIDVector, kConfiguredBSSID,
3879 _, _)).Times(1).WillOnce(Return(true));
3880 Error error(Error::kOperationInitiated);
3881 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3882 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3883 kConfiguredBSSID, ResultBoolCallback(),
3884 &error);
3885 EXPECT_FALSE(error.IsFailure());
3886 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3887 }
3888}
3889
Christopher Wiley1057cd72013-02-28 15:21:29 -08003890TEST_F(ManagerTest, VerifyDestination) {
3891 const string kFakeCertificate("fake cert");
3892 const string kFakePublicKey("fake public key");
3893 const string kFakeNonce("fake public key");
3894 const string kFakeSignedData("fake signed data");
3895 const string kFakeUdn("fake udn");
3896 const char kSSIDStr[] = "fake ssid";
3897 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003898 const string kConfiguredSSID("AConfiguredDestination");
3899 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3900 kConfiguredSSID.end());
3901 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003902 const string kFakeData("muffin man");
3903 scoped_refptr<MockWiFiService> mock_destination(
3904 new NiceMock<MockWiFiService>(control_interface(),
3905 dispatcher(),
3906 metrics(),
3907 manager(),
3908 wifi_provider_,
3909 kSSID,
3910 "",
3911 "none",
3912 false));
3913 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003914 // Making the service look online will let service lookup in
3915 // VerifyDestinatoin succeed.
3916 EXPECT_CALL(*mock_destination.get(), IsConnected())
3917 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003918 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003919
3920 // Lead off by verifying that the basic VerifyDestination flow works.
3921 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003922 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003923 ResultBoolCallback passed_down_callback;
3924 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3925 kFakePublicKey,
3926 kFakeNonce,
3927 kFakeSignedData,
3928 kFakeUdn,
3929 kSSID,
3930 _,
3931 _,
3932 _))
3933 .Times(1)
3934 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3935 // Ask the manager to verify the current destination. This should look
3936 // up our previously registered service, and pass some metadata about
3937 // that service down to the CryptoUtilProxy to verify.
3938 Error error(Error::kOperationInitiated);
3939 ResultBoolCallback cb = Bind(
3940 &DestinationVerificationTest::ResultBoolCallbackStub,
3941 dv_test.AsWeakPtr());
3942 manager()->VerifyDestination(kFakeCertificate,
3943 kFakePublicKey,
3944 kFakeNonce,
3945 kFakeSignedData,
3946 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003947 // Ask to be verified against that service.
3948 "", "",
3949 cb,
3950 &error);
3951 // We assert here, because if the operation is not ongoing, it is
3952 // inconsistent with shim behavior to call the callback anyway.
3953 ASSERT_TRUE(error.IsOngoing());
3954 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3955 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3956 // Call the callback passed into the CryptoUtilProxy, which
3957 // should find its way into the callback passed into the manager.
3958 // In real code, that callback passed into the manager is from the
3959 // DBus adaptor.
3960 Error e;
3961 passed_down_callback.Run(e, true);
3962 Mock::VerifyAndClearExpectations(&dv_test);
3963 }
3964
Christopher Wiley1057cd72013-02-28 15:21:29 -08003965 // Now for a slightly more complex variant. When we encrypt data,
3966 // we do the same verification step but monkey with the callback to
3967 // link ourselves to an encrypt step afterward.
3968 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003969 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003970 ResultBoolCallback passed_down_callback;
3971 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3972 kFakePublicKey,
3973 kFakeNonce,
3974 kFakeSignedData,
3975 kFakeUdn,
3976 kSSID,
3977 _,
3978 _,
3979 _))
3980 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3981
3982 Error error(Error::kOperationInitiated);
3983 ResultStringCallback cb = Bind(
3984 &DestinationVerificationTest::ResultStringCallbackStub,
3985 dv_test.AsWeakPtr());
3986 manager()->VerifyAndEncryptData(kFakeCertificate,
3987 kFakePublicKey,
3988 kFakeNonce,
3989 kFakeSignedData,
3990 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003991 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003992 kFakeData,
3993 cb,
3994 &error);
3995 ASSERT_TRUE(error.IsOngoing());
3996 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3997 // Now, if we call that passed down callback, we should see encrypt being
3998 // called.
3999 ResultStringCallback second_passed_down_callback;
4000 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
4001 kFakeData,
4002 _,
4003 _))
4004 .Times(1)
4005 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
4006 Return(true)));
4007 Error e;
4008 passed_down_callback.Run(e, true);
4009 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4010 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
4011 // And if we call the second passed down callback, we should see the
4012 // original function we passed down to VerifyDestination getting called.
4013 e.Reset();
4014 second_passed_down_callback.Run(e, "");
4015 Mock::VerifyAndClearExpectations(&dv_test);
4016 }
4017
4018 // If verification fails on the way to trying to encrypt, we should ditch
4019 // without calling encrypt at all.
4020 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07004021 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08004022 ResultBoolCallback passed_down_callback;
4023 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
4024 kFakePublicKey,
4025 kFakeNonce,
4026 kFakeSignedData,
4027 kFakeUdn,
4028 kSSID,
4029 _,
4030 _,
4031 _))
4032 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
4033
4034 Error error(Error::kOperationInitiated);
4035 ResultStringCallback cb = Bind(
4036 &DestinationVerificationTest::ResultStringCallbackStub,
4037 dv_test.AsWeakPtr());
4038 manager()->VerifyAndEncryptData(kFakeCertificate,
4039 kFakePublicKey,
4040 kFakeNonce,
4041 kFakeSignedData,
4042 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004043 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08004044 kFakeData,
4045 cb,
4046 &error);
4047 ASSERT_TRUE(error.IsOngoing());
4048 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4049 Error e(Error::kOperationFailed);
4050 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
4051 // Although we're ditching, this callback is what cleans up the pending
4052 // DBus call.
4053 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
4054 passed_down_callback.Run(e, false);
4055 Mock::VerifyAndClearExpectations(&dv_test);
4056 }
4057}
4058
Paul Stewartd2e1c362013-03-03 19:06:07 -08004059TEST_F(ManagerTest, IsProfileBefore) {
4060 scoped_refptr<MockProfile> profile0(
4061 new NiceMock<MockProfile>(
4062 control_interface(), metrics(), manager(), ""));
4063 scoped_refptr<MockProfile> profile1(
4064 new NiceMock<MockProfile>(
4065 control_interface(), metrics(), manager(), ""));
4066
4067 AdoptProfile(manager(), profile0);
4068 AdoptProfile(manager(), profile1); // profile1 is after profile0.
4069 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
4070 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
4071
4072 // A few abnormal cases, but it's good to track their behavior.
4073 scoped_refptr<MockProfile> profile2(
4074 new NiceMock<MockProfile>(
4075 control_interface(), metrics(), manager(), ""));
4076 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
4077 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
4078 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
4079 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
4080}
4081
Paul Stewart967eaeb2013-04-25 19:53:07 -07004082TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
4083 MockStore storage0;
4084 MockStore storage1;
4085 MockStore storage2;
4086
4087 scoped_refptr<MockProfile> profile0(
4088 new NiceMock<MockProfile>(
4089 control_interface(), metrics(), manager(), ""));
4090 scoped_refptr<MockProfile> profile1(
4091 new NiceMock<MockProfile>(
4092 control_interface(), metrics(), manager(), ""));
4093 scoped_refptr<MockProfile> profile2(
4094 new NiceMock<MockProfile>(
4095 control_interface(), metrics(), manager(), ""));
4096
4097 AdoptProfile(manager(), profile0);
4098 AdoptProfile(manager(), profile1);
4099 AdoptProfile(manager(), profile2);
4100
4101 scoped_refptr<MockService> service(
4102 new NiceMock<MockService>(control_interface(),
4103 dispatcher(),
4104 metrics(),
4105 manager()));
4106
4107 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4108 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4109 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4110
4111 const string kEntry0("aluminum_crutch");
4112 const string kEntry2("rehashed_faces");
4113
4114 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4115 .WillOnce(Return(kEntry0));
4116 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4117 .WillOnce(Return(""));
4118 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4119 .WillOnce(Return(kEntry2));
4120
4121 const string kProfileRpc0("service_station");
4122 const string kProfileRpc2("crystal_tiaras");
4123
4124 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4125 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4126 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4127
4128 map<string, string> entries =
4129 manager()->GetLoadableProfileEntriesForService(service);
4130 EXPECT_EQ(2, entries.size());
4131 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4132 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4133 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4134 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4135}
4136
mukesh agrawal00752532013-05-03 15:46:55 -07004137TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004138 // We need a real glib here, so that profiles are persisted.
4139 GLib glib;
4140 ScopedTempDir temp_dir;
4141 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4142 Manager manager(control_interface(),
4143 dispatcher(),
4144 metrics(),
4145 &glib,
4146 run_path(),
4147 storage_path(),
4148 temp_dir.path().value());
4149 // Can't use |wifi_provider_|, because it's owned by the Manager
4150 // object in the fixture.
4151 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4152 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004153 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004154 // Give manager a valid place to write the user profile list.
4155 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4156
4157 // With no user profiles, the WiFiProvider should be called once
4158 // (for the default profile).
4159 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4160 manager.InitializeProfiles();
4161 Mock::VerifyAndClearExpectations(wifi_provider);
4162
4163 // With |n| user profiles, the WiFiProvider should be called |n+1|
4164 // times. First, create 2 user profiles...
4165 const char kProfile0[] = "~user/profile0";
4166 const char kProfile1[] = "~user/profile1";
4167 string profile_rpc_path;
4168 Error error;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004169 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawald142fd62013-05-01 16:50:57 -07004170 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4171 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4172 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4173 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4174
4175 // ... then reset manager state ...
4176 manager.profiles_.clear();
4177
4178 // ...then check that the WiFiProvider is notified about all three
4179 // profiles (one default, two user).
4180 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4181 manager.InitializeProfiles();
4182 Mock::VerifyAndClearExpectations(wifi_provider);
4183}
4184
mukesh agrawal00752532013-05-03 15:46:55 -07004185TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4186 // We need a real glib here, so that profiles are persisted.
4187 GLib glib;
4188 ScopedTempDir temp_dir;
4189 scoped_ptr<Manager> manager;
4190 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4191
4192 // Instantiate a Manager with empty persistent storage. Check that
4193 // defaults are set.
4194 //
4195 // Note that we use the same directory for default and user profiles.
4196 // This doesn't affect the test results, because we don't push a
4197 // user profile.
4198 manager.reset(new Manager(control_interface(),
4199 dispatcher(),
4200 metrics(),
4201 &glib,
4202 run_path(),
4203 temp_dir.path().value(),
4204 temp_dir.path().value()));
4205 manager->InitializeProfiles();
4206 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4207 manager->props_.check_portal_list);
4208 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4209 manager->props_.ignored_dns_search_paths);
4210 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4211 manager->props_.link_monitor_technologies);
Rebecca Silberstein3d49ea42014-08-21 11:20:50 -07004212 EXPECT_EQ(ConnectivityTrial::kDefaultURL,
mukesh agrawal00752532013-05-03 15:46:55 -07004213 manager->props_.portal_url);
4214 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4215 manager->props_.portal_check_interval_seconds);
4216
4217 // Change one of the settings.
4218 static const string kCustomCheckPortalList = "fiber0";
4219 Error error;
4220 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4221 manager->profiles_[0]->Save();
4222
4223 // Instantiate a new manager. It should have our settings for
4224 // check_portal_list, rather than the default.
4225 manager.reset(new Manager(control_interface(),
4226 dispatcher(),
4227 metrics(),
4228 &glib,
4229 run_path(),
4230 temp_dir.path().value(),
4231 temp_dir.path().value()));
4232 manager->InitializeProfiles();
4233 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4234
4235 // If we clear the persistent storage, we again get the default value.
4236 ASSERT_TRUE(temp_dir.Delete());
4237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4238 manager.reset(new Manager(control_interface(),
4239 dispatcher(),
4240 metrics(),
4241 &glib,
4242 run_path(),
4243 temp_dir.path().value(),
4244 temp_dir.path().value()));
4245 manager->InitializeProfiles();
4246 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4247 manager->props_.check_portal_list);
4248}
4249
mukesh agrawalb94adde2013-08-22 18:17:26 -07004250TEST_F(ManagerTest, ProfileStackChangeLogging) {
4251 // We use a real glib here, since Manager and Profile don't provide an
4252 // easy way to mock out KeyFileStore.
4253 GLib glib;
4254 ScopedTempDir temp_dir;
4255 scoped_ptr<Manager> manager;
4256 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4257 manager.reset(new Manager(control_interface(),
4258 dispatcher(),
4259 metrics(),
4260 &glib,
4261 run_path(),
4262 temp_dir.path().value(),
4263 temp_dir.path().value()));
4264
4265 ScopedMockLog log;
4266 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4267 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4268 manager->InitializeProfiles();
4269
4270 const char kProfile0[] = "~user/profile0";
4271 const char kProfile1[] = "~user/profile1";
4272 const char kProfile2[] = "~user/profile2";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004273 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawalb94adde2013-08-22 18:17:26 -07004274 TestCreateProfile(manager.get(), kProfile0);
4275 TestCreateProfile(manager.get(), kProfile1);
4276 TestCreateProfile(manager.get(), kProfile2);
4277
4278 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4279 TestPushProfile(manager.get(), kProfile0);
4280
4281 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4282 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4283
4284 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4285 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4286
4287 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4288 TestPopProfile(manager.get(), kProfile2);
4289
4290 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4291 TestPopAnyProfile(manager.get());
4292
4293 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4294 TestPopAllUserProfiles(manager.get());
4295}
4296
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004297// Custom property setters should return false, and make no changes, if
4298// the new value is the same as the old value.
4299TEST_F(ManagerTest, CustomSetterNoopChange) {
4300 // SetCheckPortalList
4301 {
4302 static const string kCheckPortalList = "weird-device,weirder-device";
4303 Error error;
4304 // Set to known value.
4305 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4306 EXPECT_TRUE(error.IsSuccess());
4307 // Set to same value.
4308 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4309 EXPECT_TRUE(error.IsSuccess());
4310 }
4311
4312 // SetIgnoredDNSSearchPaths
4313 {
4314 NiceMock<MockResolver> resolver;
4315 static const string kIgnoredPaths = "example.com,example.org";
4316 Error error;
4317 SetResolver(&resolver);
4318 // Set to known value.
4319 EXPECT_CALL(resolver, set_ignored_search_list(_));
4320 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4321 EXPECT_TRUE(error.IsSuccess());
4322 Mock::VerifyAndClearExpectations(&resolver);
4323 // Set to same value.
4324 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4325 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4326 EXPECT_TRUE(error.IsSuccess());
4327 Mock::VerifyAndClearExpectations(&resolver);
4328 }
4329}
4330
Paul Stewart7de7e022013-08-28 09:42:50 -07004331TEST_F(ManagerTest, GeoLocation) {
4332 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4333
4334 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4335 dispatcher(),
4336 metrics(),
4337 manager(),
4338 "null",
4339 "addr",
4340 0));
4341
4342 // Manager should ignore gelocation info from technologies it does not know.
4343 EXPECT_CALL(*device, technology())
4344 .Times(AtLeast(1))
4345 .WillRepeatedly(Return(Technology::kEthernet));
4346 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4347 manager()->OnDeviceGeolocationInfoUpdated(device);
4348 Mock::VerifyAndClearExpectations(device);
4349 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4350
4351 // Manager should add WiFi geolocation info.
4352 EXPECT_CALL(*device, technology())
4353 .Times(AtLeast(1))
4354 .WillRepeatedly(Return(Technology::kWifi));
4355 EXPECT_CALL(*device, GetGeolocationObjects())
4356 .WillOnce(Return(vector<GeolocationInfo>()));
4357 manager()->OnDeviceGeolocationInfoUpdated(device);
4358 Mock::VerifyAndClearExpectations(device);
4359 auto location_infos = manager()->GetNetworksForGeolocation();
4360 EXPECT_EQ(1, location_infos.size());
4361 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4362
4363 // Manager should inclusively add cellular info.
4364 EXPECT_CALL(*device, technology())
4365 .Times(AtLeast(1))
4366 .WillRepeatedly(Return(Technology::kCellular));
4367 EXPECT_CALL(*device, GetGeolocationObjects())
4368 .WillOnce(Return(vector<GeolocationInfo>()));
4369 manager()->OnDeviceGeolocationInfoUpdated(device);
4370 location_infos = manager()->GetNetworksForGeolocation();
4371 EXPECT_EQ(2, location_infos.size());
4372 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4373 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4374}
4375
Peter Qiu574996a2014-04-04 10:55:47 -07004376TEST_F(ManagerTest, IsWifiIdle) {
4377 // No registered service.
4378 EXPECT_FALSE(manager()->IsWifiIdle());
4379
4380 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4381 dispatcher(),
4382 metrics(),
4383 manager()));
4384
4385 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4386 dispatcher(),
4387 metrics(),
4388 manager()));
4389
4390 manager()->RegisterService(wifi_service);
4391 manager()->RegisterService(cell_service);
4392
4393 EXPECT_CALL(*wifi_service.get(), technology())
4394 .WillRepeatedly(Return(Technology::kWifi));
4395 EXPECT_CALL(*cell_service.get(), technology())
4396 .WillRepeatedly(Return(Technology::kCellular));
4397
4398 // Cellular is connected.
4399 EXPECT_CALL(*cell_service.get(), IsConnected())
4400 .WillRepeatedly(Return(true));
4401 manager()->UpdateService(cell_service);
4402
4403 // No wifi connection attempt.
4404 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4405 .WillRepeatedly(Return(false));
4406 EXPECT_CALL(*wifi_service.get(), IsConnected())
4407 .WillRepeatedly(Return(false));
4408 manager()->UpdateService(wifi_service);
4409 EXPECT_TRUE(manager()->IsWifiIdle());
4410
4411 // Attempt wifi connection.
4412 Mock::VerifyAndClearExpectations(wifi_service);
4413 EXPECT_CALL(*wifi_service.get(), technology())
4414 .WillRepeatedly(Return(Technology::kWifi));
4415 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4416 .WillRepeatedly(Return(true));
4417 EXPECT_CALL(*wifi_service.get(), IsConnected())
4418 .WillRepeatedly(Return(false));
4419 manager()->UpdateService(wifi_service);
4420 EXPECT_FALSE(manager()->IsWifiIdle());
4421
4422 // wifi connected.
4423 Mock::VerifyAndClearExpectations(wifi_service);
4424 EXPECT_CALL(*wifi_service.get(), technology())
4425 .WillRepeatedly(Return(Technology::kWifi));
4426 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4427 .WillRepeatedly(Return(false));
4428 EXPECT_CALL(*wifi_service.get(), IsConnected())
4429 .WillRepeatedly(Return(true));
4430 manager()->UpdateService(wifi_service);
4431 EXPECT_FALSE(manager()->IsWifiIdle());
4432}
4433
Chris Masone9be4a9d2011-05-16 15:44:09 -07004434} // namespace shill