blob: c9cf1e530e8aca6b8adf5ccc94ee3b0aade78208 [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()
Ben Chancc225ef2014-09-30 13:26:51 -070089 : power_manager_(new MockPowerManager(nullptr, &proxy_factory_)),
90 device_info_(new NiceMock<MockDeviceInfo>(control_interface(),
91 nullptr,
92 nullptr,
93 nullptr)),
Paul Stewart3c504012013-01-17 17:49:58 -080094 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070095 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080096 wifi_provider_(new NiceMock<MockWiFiProvider>()),
Ben Chancc225ef2014-09-30 13:26:51 -070097 crypto_util_proxy_(
98 new NiceMock<MockCryptoUtilProxy>(dispatcher(), glib())) {
Ben Chana55469d2014-01-27 16:35:29 -080099 ON_CALL(proxy_factory_, CreatePowerManagerProxy(_))
100 .WillByDefault(ReturnNull());
101
Paul Stewart22aa71b2011-09-16 12:15:11 -0700102 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
103 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800104 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700105 manager(),
106 "null0",
107 "addr0",
108 0));
109 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
110 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800111 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700112 manager(),
113 "null1",
114 "addr1",
115 1));
116 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
117 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800118 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700119 manager(),
120 "null2",
121 "addr2",
122 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800123 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
124 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800125 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800126 manager(),
127 "null3",
128 "addr3",
129 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700130 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800131 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800132
133 // Replace the manager's adaptor with a quieter one, and one
134 // we can do EXPECT*() against. Passes ownership.
135 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800136
Paul Stewart35eff132013-04-12 12:08:40 -0700137 // Replace the manager's Ethernet EAP provider with our mock.
138 // Passes ownership.
139 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
140
Paul Stewart3c504012013-01-17 17:49:58 -0800141 // Replace the manager's WiFi provider with our mock. Passes
142 // ownership.
143 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800144
Paul Stewartb87d22b2013-07-29 11:11:37 -0700145 // Update the manager's map from technology to provider.
146 manager()->UpdateProviderMapping();
147
Christopher Wiley1057cd72013-02-28 15:21:29 -0800148 // Replace the manager's crypto util proxy with our mock. Passes
149 // ownership.
150 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700151 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700152 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700153
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100154 void SetMetrics(Metrics *metrics) {
155 manager()->set_metrics(metrics);
156 }
157
Paul Stewartfdd16072011-09-16 12:41:35 -0700158 bool IsDeviceRegistered(const DeviceRefPtr &device,
159 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700160 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700161 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700162 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700163 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700164 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700165
Paul Stewarta849a3d2011-11-03 05:54:09 -0700166 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
167 manager->profiles_.push_back(profile);
168 }
169
Paul Stewart63864b62012-11-07 15:10:55 -0800170 void SetRunning(bool running) {
171 manager()->running_ = running;
172 }
173
Paul Stewart75225512012-01-26 22:51:33 -0800174 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
175 return manager->ephemeral_profile_;
176 }
177
Paul Stewart307c2502013-03-23 12:32:10 -0700178 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
179 return manager->profiles_;
180 }
181
Chris Masone6515aab2011-10-12 16:19:09 -0700182 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
183 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700184 FilePath final_path(storage_path());
185 final_path = final_path.Append("test.profile");
186 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
187 storage->set_path(final_path);
188 if (!storage->Open())
Ben Chancc225ef2014-09-30 13:26:51 -0700189 return nullptr;
Paul Stewart5ad16062013-02-21 18:10:48 -0800190 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800191 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800192 manager,
193 id,
194 "",
195 false));
196 profile->set_storage(storage.release()); // Passes ownership of "storage".
197 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700198 }
199
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700200 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700201 const string &user_identifier,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700202 const string &profile_identifier,
203 const string &service_name) {
204 GLib glib;
205 KeyFileStore store(&glib);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700206 store.set_path(temp_dir->path().Append(
207 base::StringPrintf("%s/%s.profile", user_identifier.c_str(),
208 profile_identifier.c_str())));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700209 return store.Open() &&
210 store.SetString(service_name, "rather", "irrelevant") &&
211 store.Close();
212 }
213
214 Error::Type TestCreateProfile(Manager *manager, const string &name) {
215 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800216 string path;
217 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700218 return error.type();
219 }
220
221 Error::Type TestPopAnyProfile(Manager *manager) {
222 Error error;
223 manager->PopAnyProfile(&error);
224 return error.type();
225 }
226
Paul Stewart307c2502013-03-23 12:32:10 -0700227 Error::Type TestPopAllUserProfiles(Manager *manager) {
228 Error error;
229 manager->PopAllUserProfiles(&error);
230 return error.type();
231 }
232
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700233 Error::Type TestPopProfile(Manager *manager, const string &name) {
234 Error error;
235 manager->PopProfile(name, &error);
236 return error.type();
237 }
238
239 Error::Type TestPushProfile(Manager *manager, const string &name) {
240 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800241 string path;
242 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700243 return error.type();
244 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000245
Paul Stewartf3eced92013-04-17 12:18:22 -0700246 Error::Type TestInsertUserProfile(Manager *manager,
247 const string &name,
248 const string &user_hash) {
249 Error error;
250 string path;
251 manager->InsertUserProfile(name, user_hash, &path, &error);
252 return error.type();
253 }
254
Paul Stewartd2e1c362013-03-03 19:06:07 -0800255 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
256 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700257 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800258 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800259 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200260 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700261 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800262 return profile;
263 }
264
265 void AddMockProfileToManager(Manager *manager) {
266 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700267 }
268
Paul Stewartdfa46052012-06-26 09:44:14 -0700269 void CompleteServiceSort() {
Paul Stewartfbca6bc2014-10-07 08:26:03 -0700270 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewartdfa46052012-06-26 09:44:14 -0700271 dispatcher()->DispatchPendingEvents();
Paul Stewartfbca6bc2014-10-07 08:26:03 -0700272 EXPECT_FALSE(IsSortServicesTaskPending());
273 }
274
275 bool IsSortServicesTaskPending() {
276 return !manager()->sort_services_task_.IsCancelled();
Paul Stewartdfa46052012-06-26 09:44:14 -0700277 }
278
Paul Stewart03e29f72013-09-26 00:49:48 -0700279 void RefreshConnectionState() {
280 manager()->RefreshConnectionState();
281 }
282
Paul Stewart49739c02012-08-08 17:24:03 -0700283 RpcIdentifier GetDefaultServiceRpcIdentifier() {
Ben Chancc225ef2014-09-30 13:26:51 -0700284 return manager()->GetDefaultServiceRpcIdentifier(nullptr);
Paul Stewart49739c02012-08-08 17:24:03 -0700285 }
286
Paul Stewart4d5efb72012-09-17 12:24:34 -0700287 void SetResolver(Resolver *resolver) {
288 manager()->resolver_ = resolver;
289 }
290
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700291 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
292 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
293 }
294
295 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
296 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700297 }
298
299 const string &GetIgnoredDNSSearchPaths() {
300 return manager()->props_.ignored_dns_search_paths;
301 }
302
Paul Stewartd2e1c362013-03-03 19:06:07 -0800303 WiFiServiceRefPtr ReleaseTempMockService() {
304 // Take a reference to hold during this function.
305 WiFiServiceRefPtr temp_service = temp_mock_service_;
Ben Chancc225ef2014-09-30 13:26:51 -0700306 temp_mock_service_ = nullptr;
Paul Stewartd2e1c362013-03-03 19:06:07 -0800307 return temp_service;
308 }
309
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700310 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000311 typedef scoped_refptr<MockService> MockServiceRefPtr;
312
Darin Petkova5e07ef2012-07-09 14:27:57 +0200313 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
314 public:
315 ServiceWatcher() {}
316 virtual ~ServiceWatcher() {}
317
318 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
319
320 private:
321 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
322 };
323
Darin Petkov3ec55342012-09-28 14:04:44 +0200324 class TerminationActionTest :
325 public base::SupportsWeakPtr<TerminationActionTest> {
326 public:
327 static const char kActionName[];
328
Ben Chancc225ef2014-09-30 13:26:51 -0700329 TerminationActionTest() : manager_(nullptr) {}
Darin Petkov3ec55342012-09-28 14:04:44 +0200330 virtual ~TerminationActionTest() {}
331
332 MOCK_METHOD1(Done, void(const Error &error));
333
334 void Action() {
335 manager_->TerminationActionComplete("action");
336 }
337
338 void set_manager(Manager *manager) { manager_ = manager; }
339
340 private:
341 Manager *manager_;
342 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
343 };
344
Christopher Wiley1057cd72013-02-28 15:21:29 -0800345 class DestinationVerificationTest :
346 public base::SupportsWeakPtr<DestinationVerificationTest> {
347 public:
348 DestinationVerificationTest() {}
349 virtual ~DestinationVerificationTest() {}
350
351 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
352 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
353 const string &value));
354 private:
355 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
356 };
357
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700358 class DisableTechnologyReplyHandler :
359 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
360 public:
361 DisableTechnologyReplyHandler() {}
362 virtual ~DisableTechnologyReplyHandler() {}
363
364 MOCK_METHOD1(ReportResult, void(const Error &));
365
366 private:
367 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
368 };
369
Daniel Eratfac09532014-04-17 20:25:59 -0700370 void SetSuspending(bool suspending) {
371 power_manager_->suspending_ = suspending;
Darin Petkovca621542012-07-25 14:25:56 +0200372 }
373
374 void SetPowerManager() {
375 manager()->set_power_manager(power_manager_.release());
376 }
377
Darin Petkov3ec55342012-09-28 14:04:44 +0200378 HookTable *GetTerminationActions() {
379 return &manager()->termination_actions_;
380 }
381
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700382 void OnSuspendImminent() {
383 manager()->OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +0200384 }
385
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700386 void OnSuspendDone() {
387 manager()->OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -0700388 }
389
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700390 void OnSuspendActionsComplete(const Error &error) {
391 manager()->OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200392 }
393
Paul Stewartbfb82552012-10-24 16:48:48 -0700394 vector<string> EnumerateAvailableServices() {
Ben Chancc225ef2014-09-30 13:26:51 -0700395 return manager()->EnumerateAvailableServices(nullptr);
Paul Stewartbfb82552012-10-24 16:48:48 -0700396 }
397
398 vector<string> EnumerateWatchedServices() {
Ben Chancc225ef2014-09-30 13:26:51 -0700399 return manager()->EnumerateWatchedServices(nullptr);
Paul Stewartbfb82552012-10-24 16:48:48 -0700400 }
401
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000402 MockServiceRefPtr MakeAutoConnectableService() {
403 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
404 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800405 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000406 manager());
Paul Stewart2da34c02013-10-17 15:28:56 -0700407 service->EnableAndRetainAutoConnect();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700408 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000409 return service;
410 }
411
Paul Stewart35eff132013-04-12 12:08:40 -0700412 void SetEapProviderService(const ServiceRefPtr &service) {
413 ethernet_eap_provider_->set_service(service);
414 }
415
Ben Chana55469d2014-01-27 16:35:29 -0800416 NiceMock<MockProxyFactory> proxy_factory_;
Darin Petkovca621542012-07-25 14:25:56 +0200417 scoped_ptr<MockPowerManager> power_manager_;
Ben Chane2ee5e02014-09-19 19:29:42 -0700418 vector<scoped_refptr<MockDevice>> mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800419 scoped_ptr<MockDeviceInfo> device_info_;
420
Paul Stewartd2e1c362013-03-03 19:06:07 -0800421 // This service is held for the manager, and given ownership in a mock
422 // function. This ensures that when the Manager takes ownership, there
423 // is only one reference left.
424 scoped_refptr<MockWiFiService> temp_mock_service_;
425
Paul Stewart3c504012013-01-17 17:49:58 -0800426 // These pointers are owned by the manager, and only tracked here for
427 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800428 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700429 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800430 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800431 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700432};
433
Darin Petkov3ec55342012-09-28 14:04:44 +0200434const char ManagerTest::TerminationActionTest::kActionName[] = "action";
435
Paul Stewart22aa71b2011-09-16 12:15:11 -0700436bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700437 if (!manager()->sort_services_task_.IsCancelled()) {
438 manager()->SortServicesTask();
439 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700440 return (svc0.get() == manager()->services_[0].get() &&
441 svc1.get() == manager()->services_[1].get());
442}
443
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700444void SetErrorPermissionDenied(Error *error) {
445 error->Populate(Error::kPermissionDenied);
446}
447
Arman Uguray2f352e62013-08-28 19:12:53 -0700448void SetErrorSuccess(Error *error) {
449 error->Reset();
450}
451
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700452TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700453 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700454 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700455}
456
Chris Masone9be4a9d2011-05-16 15:44:09 -0700457TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700458 ON_CALL(*mock_devices_[0].get(), technology())
459 .WillByDefault(Return(Technology::kEthernet));
460 ON_CALL(*mock_devices_[1].get(), technology())
461 .WillByDefault(Return(Technology::kWifi));
462 ON_CALL(*mock_devices_[2].get(), technology())
463 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700464
Paul Stewart22aa71b2011-09-16 12:15:11 -0700465 manager()->RegisterDevice(mock_devices_[0]);
466 manager()->RegisterDevice(mock_devices_[1]);
467 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700468
Paul Stewart22aa71b2011-09-16 12:15:11 -0700469 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
470 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
471 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700472}
473
Paul Stewarta41e38d2011-11-11 07:47:29 -0800474TEST_F(ManagerTest, DeviceRegistrationAndStart) {
475 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500476 mock_devices_[0]->enabled_persistent_ = true;
477 mock_devices_[1]->enabled_persistent_ = false;
478 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800479 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500480 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800481 .Times(0);
482 manager()->RegisterDevice(mock_devices_[0]);
483 manager()->RegisterDevice(mock_devices_[1]);
484}
485
486TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800487 MockProfile *profile =
488 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800489 DeviceRefPtr device_ref(mock_devices_[0].get());
490 AdoptProfile(manager(), profile); // Passes ownership.
491 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200492 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800493 manager()->RegisterDevice(mock_devices_[0]);
494}
495
Chris Masone9be4a9d2011-05-16 15:44:09 -0700496TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700497 ON_CALL(*mock_devices_[0].get(), technology())
498 .WillByDefault(Return(Technology::kEthernet));
499 ON_CALL(*mock_devices_[1].get(), technology())
500 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700501
Gaurav Shah435de2c2011-11-17 19:01:07 -0800502 manager()->RegisterDevice(mock_devices_[0]);
503 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700504
Paul Stewart22aa71b2011-09-16 12:15:11 -0700505 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
506 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700507
Thieu Le5133b712013-02-19 14:47:21 -0800508 MockProfile *profile =
509 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700510 AdoptProfile(manager(), profile); // Passes ownership.
511
Eric Shienbrood9a245532012-03-07 14:20:39 -0500512 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700513 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800514 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700515 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700516
Eric Shienbrood9a245532012-03-07 14:20:39 -0500517 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700518 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800519 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700520 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700521}
522
523TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700524 // It's much easier and safer to use a real GLib for this test.
525 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700526 Manager manager(control_interface(),
527 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800528 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700529 &glib,
530 run_path(),
531 storage_path(),
532 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700533 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
534 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700535 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700536
Chris Masone9be4a9d2011-05-16 15:44:09 -0700537 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700538 new NiceMock<MockService>(control_interface(),
539 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800540 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700541 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700542 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700543 new NiceMock<MockService>(control_interface(),
544 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800545 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700546 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700547
Darin Petkov457728b2013-01-09 09:49:08 +0100548 string service1_name(mock_service->unique_name());
549 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700550
551 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
552 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700553 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700554 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700555 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700556 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700557 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700558
Chris Masone9d779932011-08-25 16:33:41 -0700559 manager.RegisterService(mock_service);
560 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700561
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800562 Error error;
563 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700564 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700565 EXPECT_EQ(2, ids.size());
566 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
567 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700568
Ben Chancc225ef2014-09-30 13:26:51 -0700569 EXPECT_NE(nullptr, manager.FindService(service1_name).get());
570 EXPECT_NE(nullptr, manager.FindService(service2_name).get());
Chris Masone9d779932011-08-25 16:33:41 -0700571
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700572 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700573 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700574}
575
Chris Masone6515aab2011-10-12 16:19:09 -0700576TEST_F(ManagerTest, RegisterKnownService) {
577 // It's much easier and safer to use a real GLib for this test.
578 GLib glib;
579 Manager manager(control_interface(),
580 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800581 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700582 &glib,
583 run_path(),
584 storage_path(),
585 string());
586 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
587 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700588 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700589 {
590 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
591 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800592 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700593 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700594 ASSERT_TRUE(profile->AdoptService(service1));
595 ASSERT_TRUE(profile->ContainsService(service1));
596 } // Force destruction of service1.
597
598 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
599 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800600 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700601 &manager));
602 manager.RegisterService(service2);
603 EXPECT_EQ(service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700604
605 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700606 manager.Stop();
607}
608
609TEST_F(ManagerTest, RegisterUnknownService) {
610 // It's much easier and safer to use a real GLib for this test.
611 GLib glib;
612 Manager manager(control_interface(),
613 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800614 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700615 &glib,
616 run_path(),
617 storage_path(),
618 string());
619 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
620 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700621 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700622 {
623 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
624 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800625 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700626 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700627 ASSERT_TRUE(profile->AdoptService(service1));
628 ASSERT_TRUE(profile->ContainsService(service1));
629 } // Force destruction of service1.
630 scoped_refptr<MockService> mock_service2(
631 new NiceMock<MockService>(control_interface(),
632 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800633 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700634 &manager));
635 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100636 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700637 manager.RegisterService(mock_service2);
638 EXPECT_NE(mock_service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700639
640 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700641 manager.Stop();
642}
643
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000644TEST_F(ManagerTest, DeregisterUnregisteredService) {
645 // WiFi assumes that it can deregister a service that is not
646 // registered. (E.g. a hidden service can be deregistered when it
647 // loses its last endpoint, and again when WiFi is Stop()-ed.)
648 //
649 // So test that doing so doesn't cause a crash.
650 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
651 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800652 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000653 manager());
654 manager()->DeregisterService(service);
655}
656
Chris Masonea8a2c252011-06-27 22:16:30 -0700657TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700658 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700659 map<string, ::DBus::Variant> props;
660 Error error(Error::kInvalidProperty, "");
661 {
662 ::DBus::Error dbus_error;
663 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700664 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700665 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700666 expected,
667 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700668 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700669 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
670 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700671 expected);
672 }
673 {
674 ::DBus::Error dbus_error;
675 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700676 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700677 expected,
678 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700679 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700680 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
681 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700682 expected);
683 }
684}
685
Chris Masone3c3f6a12011-07-01 10:01:41 -0700686TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700687 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800688 manager()->RegisterDevice(mock_devices_[0]);
689 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700690 {
691 map<string, ::DBus::Variant> props;
692 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700693 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700694 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700695 vector < ::DBus::Path> devices =
Ben Chane2ee5e02014-09-19 19:29:42 -0700696 props[kDevicesProperty].operator vector<::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700697 EXPECT_EQ(2, devices.size());
698 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700699}
700
mukesh agrawal2366eed2012-03-20 18:21:50 -0700701TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700702 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700703 map<string, ::DBus::Variant> props;
704 ::DBus::Error dbus_error;
705 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
706 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700707 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700708 ASSERT_FALSE(prop == props.end());
709 const ::DBus::Variant &variant = prop->second;
710 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
711}
712
Chris Masone6791a432011-07-12 13:23:19 -0700713TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700714 Manager manager(control_interface(),
715 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800716 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700717 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700718 run_path(),
719 storage_path(),
720 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700721 scoped_refptr<MockService> s2(new MockService(control_interface(),
722 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800723 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700724 &manager));
725 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700726 {
Chris Masone6515aab2011-10-12 16:19:09 -0700727 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700728 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800729 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700730 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700731 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700732 .WillRepeatedly(Return(true));
733 EXPECT_CALL(*storage, Flush())
734 .Times(AnyNumber())
735 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700736 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700737 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700738 }
Chris Masone6515aab2011-10-12 16:19:09 -0700739 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800740 ProfileRefPtr profile(
741 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700742 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700743
Chris Masone6515aab2011-10-12 16:19:09 -0700744 // Now, move the Service |s2| to another profile.
745 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
746 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700747
748 // Force destruction of the original Profile, to ensure that the Service
749 // is kept alive and populated with data.
Ben Chancc225ef2014-09-30 13:26:51 -0700750 profile = nullptr;
Chris Masone6515aab2011-10-12 16:19:09 -0700751 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700752 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700753 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700754}
755
Paul Stewart7f61e522012-03-22 11:13:45 -0700756TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
757 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800758 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700759 const string kProfileName("profile0");
760 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
761 .WillRepeatedly(Return(kProfileName));
762 AdoptProfile(manager(), mock_profile);
763
764 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
765 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
766 EXPECT_EQ(mock_profile.get(), profile.get());
767}
768
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800769TEST_F(ManagerTest, SetProfileForService) {
770 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800771 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800772 string profile_name0("profile0");
773 EXPECT_CALL(*profile0, GetRpcIdentifier())
774 .WillRepeatedly(Return(profile_name0));
775 AdoptProfile(manager(), profile0);
776 scoped_refptr<MockService> service(new MockService(control_interface(),
777 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800778 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800779 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700780 EXPECT_FALSE(manager()->HasService(service));
781 {
782 Error error;
783 EXPECT_CALL(*profile0, AdoptService(_))
784 .WillOnce(Return(true));
785 // Expect that setting the profile of a service that does not already
786 // have one assigned does not cause a crash.
787 manager()->SetProfileForService(service, "profile0", &error);
788 EXPECT_TRUE(error.IsSuccess());
789 }
790
791 // The service should be registered as a side-effect of the profile being
792 // set for this service.
793 EXPECT_TRUE(manager()->HasService(service));
794
795 // Since we have mocked Profile::AdoptServie() above, the service's
796 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800797 service->set_profile(profile0);
798
799 {
800 Error error;
801 manager()->SetProfileForService(service, "foo", &error);
802 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700803 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800804 }
805
806 {
807 Error error;
808 manager()->SetProfileForService(service, profile_name0, &error);
809 EXPECT_EQ(Error::kInvalidArguments, error.type());
810 EXPECT_EQ("Service is already connected to this profile", error.message());
811 }
812
813 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800814 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800815 string profile_name1("profile1");
816 EXPECT_CALL(*profile1, GetRpcIdentifier())
817 .WillRepeatedly(Return(profile_name1));
818 AdoptProfile(manager(), profile1);
819
820 {
821 Error error;
822 EXPECT_CALL(*profile1, AdoptService(_))
823 .WillOnce(Return(true));
824 EXPECT_CALL(*profile0, AbandonService(_))
825 .WillOnce(Return(true));
826 manager()->SetProfileForService(service, profile_name1, &error);
827 EXPECT_TRUE(error.IsSuccess());
828 }
829}
830
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700831TEST_F(ManagerTest, CreateProfile) {
832 // It's much easier to use real Glib here since we want the storage
833 // side-effects.
834 GLib glib;
835 ScopedTempDir temp_dir;
836 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
837
838 Manager manager(control_interface(),
839 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800840 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700841 &glib,
842 run_path(),
843 storage_path(),
844 temp_dir.path().value());
845
846 // Invalid name should be rejected.
847 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
848
Paul Stewartd0a3b812012-03-28 22:48:22 -0700849 // A profile with invalid characters in it should similarly be rejected.
850 EXPECT_EQ(Error::kInvalidArguments,
851 TestCreateProfile(&manager, "valid_profile"));
852
853 // We should be able to create a machine profile.
854 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700855
Gary Morainb672d352012-04-25 09:19:06 -0700856 // We should succeed in creating a valid user profile. Verify the returned
857 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700858 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700859 {
860 Error error;
861 string path;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700862 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Gary Morainb672d352012-04-25 09:19:06 -0700863 manager.CreateProfile(kProfile, &path, &error);
864 EXPECT_EQ(Error::kSuccess, error.type());
865 EXPECT_EQ("/profile_rpc", path);
866 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700867
868 // We should fail in creating it a second time (already exists).
869 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
870}
871
872TEST_F(ManagerTest, PushPopProfile) {
873 // It's much easier to use real Glib in creating a Manager for this
874 // test here since we want the storage side-effects.
875 GLib glib;
876 ScopedTempDir temp_dir;
877 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
878 Manager manager(control_interface(),
879 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800880 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700881 &glib,
882 run_path(),
883 storage_path(),
884 temp_dir.path().value());
mukesh agrawal92496a42014-04-08 16:04:43 -0700885 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700886
887 // Pushing an invalid profile should fail.
888 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
889
mukesh agrawal92496a42014-04-08 16:04:43 -0700890 // Create and push a default profile. Should succeed.
891 const char kDefaultProfile0[] = "default";
892 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kDefaultProfile0));
893 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kDefaultProfile0));
894 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kDefaultProfile0));
895
896 // Pushing a default profile that does not exist on disk will _not_
897 // fail, because we'll use temporary storage for it.
898 const char kMissingDefaultProfile[] = "missingdefault";
899 EXPECT_EQ(Error::kSuccess,
900 TestPushProfile(&manager, kMissingDefaultProfile));
901 EXPECT_EQ(1, profiles.size());
902 EXPECT_EQ(Error::kSuccess,
903 TestPopProfile(&manager, kMissingDefaultProfile));
904 EXPECT_EQ(0, profiles.size());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700905
906 const char kProfile0[] = "~user/profile0";
907 const char kProfile1[] = "~user/profile1";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700908 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700909
910 // Create a couple of profiles.
911 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
912 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
913
914 // Push these profiles on the stack.
915 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
916 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
917
918 // Pushing a profile a second time should fail.
919 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
920 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
921
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800922 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700923 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800924 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700925
926 // Make sure a profile name that doesn't exist fails.
927 const char kProfile2Id[] = "profile2";
928 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
929 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
930
931 // Create a new service, with a specific storage name.
932 scoped_refptr<MockService> service(
933 new NiceMock<MockService>(control_interface(),
934 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800935 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700936 &manager));
937 const char kServiceName[] = "service_storage_name";
938 EXPECT_CALL(*service.get(), GetStorageIdentifier())
939 .WillRepeatedly(Return(kServiceName));
940 EXPECT_CALL(*service.get(), Load(_))
941 .WillRepeatedly(Return(true));
942
943 // Add this service to the manager -- it should end up in the ephemeral
944 // profile.
945 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800946 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700947
948 // Create storage for a profile that contains the service storage name.
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700949 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile2Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700950 kServiceName));
951
952 // When we push the profile, the service should move away from the
953 // ephemeral profile to this new profile since it has an entry for
954 // this service.
Paul Stewart2eee6132014-05-09 13:33:26 -0700955 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700956 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800957 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700958 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
959
960 // Insert another profile that should supersede ownership of the service.
961 const char kProfile3Id[] = "profile3";
962 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700963 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile3Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700964 kServiceName));
Paul Stewart2eee6132014-05-09 13:33:26 -0700965 // We don't verify this expectation inline, since this would clear other
966 // recurring expectations on the service.
967 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700968 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
969 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
970
971 // Popping an invalid profile name should fail.
972 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
973
974 // Popping an profile that is not at the top of the stack should fail.
975 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
976
977 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700978 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700979 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
980
981 // Moreover the service should have switched profiles to profile 2.
982 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
983
984 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700985 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700986 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
987
988 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800989 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700990
mukesh agrawal92496a42014-04-08 16:04:43 -0700991 // Pop the remaining two profiles off the stack.
Paul Stewart2eee6132014-05-09 13:33:26 -0700992 EXPECT_CALL(*service, ClearExplicitlyDisconnected()).Times(2);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700993 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
994 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
Paul Stewart2eee6132014-05-09 13:33:26 -0700995 Mock::VerifyAndClearExpectations(service);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700996
997 // Next pop should fail with "stack is empty".
998 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700999
1000 const char kMachineProfile0[] = "machineprofile0";
1001 const char kMachineProfile1[] = "machineprofile1";
1002 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1003 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1004
1005 // Should be able to push a machine profile.
1006 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1007
1008 // Should be able to push a user profile atop a machine profile.
1009 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1010
1011 // Pushing a system-wide profile on top of a user profile should fail.
1012 EXPECT_EQ(Error::kInvalidArguments,
1013 TestPushProfile(&manager, kMachineProfile1));
1014
1015 // However if we pop the user profile, we should be able stack another
1016 // machine profile on.
1017 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1018 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001019
1020 // Add two user profiles to the top of the stack.
1021 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1022 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001023 EXPECT_EQ(4, profiles.size());
1024
1025 // PopAllUserProfiles should remove both user profiles, leaving the two
1026 // machine profiles.
1027 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1028 EXPECT_EQ(2, profiles.size());
1029 EXPECT_TRUE(profiles[0]->GetUser().empty());
1030 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001031
1032 // Use InsertUserProfile() instead. Although a machine profile is valid
1033 // in this state, it cannot be added via InsertUserProfile.
1034 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1035 EXPECT_EQ(Error::kInvalidArguments,
1036 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1037 const char kUserHash0[] = "userhash0";
1038 const char kUserHash1[] = "userhash1";
1039 EXPECT_EQ(Error::kSuccess,
1040 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1041 EXPECT_EQ(Error::kSuccess,
1042 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1043 EXPECT_EQ(3, profiles.size());
1044 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1045 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001046}
1047
Paul Stewarte73d05c2012-03-29 16:26:05 -07001048TEST_F(ManagerTest, RemoveProfile) {
1049 // It's much easier to use real Glib in creating a Manager for this
1050 // test here since we want the storage side-effects.
1051 GLib glib;
1052 ScopedTempDir temp_dir;
1053 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1054 Manager manager(control_interface(),
1055 dispatcher(),
1056 metrics(),
1057 &glib,
1058 run_path(),
1059 storage_path(),
1060 temp_dir.path().value());
1061
1062 const char kProfile0[] = "profile0";
1063 FilePath profile_path(
1064 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1065
1066 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001067 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001068
1069 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1070
1071 // Remove should fail since the profile is still on the stack.
1072 {
1073 Error error;
1074 manager.RemoveProfile(kProfile0, &error);
1075 EXPECT_EQ(Error::kInvalidArguments, error.type());
1076 }
1077
1078 // Profile path should still exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001079 EXPECT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001080
1081 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1082
1083 // This should succeed now that the profile is off the stack.
1084 {
1085 Error error;
1086 manager.RemoveProfile(kProfile0, &error);
1087 EXPECT_EQ(Error::kSuccess, error.type());
1088 }
1089
1090 // Profile path should no longer exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001091 EXPECT_FALSE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001092
Ben Chana0ddf462014-02-06 11:32:42 -08001093 // Another remove succeeds, due to a foible in base::DeleteFile --
Paul Stewarte73d05c2012-03-29 16:26:05 -07001094 // it is not an error to delete a file that does not exist.
1095 {
1096 Error error;
1097 manager.RemoveProfile(kProfile0, &error);
1098 EXPECT_EQ(Error::kSuccess, error.type());
1099 }
1100
1101 // Let's create an error case that will "work". Create a non-empty
1102 // directory in the place of the profile pathname.
Ben Chana0ddf462014-02-06 11:32:42 -08001103 ASSERT_TRUE(base::CreateDirectory(profile_path.Append("foo")));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001104 {
1105 Error error;
1106 manager.RemoveProfile(kProfile0, &error);
1107 EXPECT_EQ(Error::kOperationFailed, error.type());
1108 }
1109}
1110
Paul Stewartd3d03882013-08-29 15:43:42 -07001111TEST_F(ManagerTest, RemoveService) {
1112 MockServiceRefPtr mock_service(
1113 new NiceMock<MockService>(control_interface(),
1114 dispatcher(),
1115 metrics(),
1116 manager()));
1117
1118 // Used in expectations which cannot accept a mock refptr.
1119 const ServiceRefPtr &service = mock_service;
1120
1121 manager()->RegisterService(service);
1122 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1123
1124 scoped_refptr<MockProfile> profile(
1125 new StrictMock<MockProfile>(
1126 control_interface(), metrics(), manager(), ""));
1127 AdoptProfile(manager(), profile);
1128
1129 // If service is ephemeral, it should be unloaded and left ephemeral.
1130 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1131 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1132 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1133 manager()->RemoveService(service);
1134 Mock::VerifyAndClearExpectations(mock_service);
1135 Mock::VerifyAndClearExpectations(profile);
1136 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1137 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1138
1139 // If service is not ephemeral and the Manager finds a profile to assign
1140 // the service to, the service should be re-parented. Note that since we
1141 // are using a MockProfile, ConfigureService() never actually changes the
1142 // Service's profile.
1143 service->set_profile(profile);
1144 EXPECT_CALL(*profile, AbandonService(service));
1145 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1146 EXPECT_CALL(*mock_service, Unload()).Times(0);
1147 manager()->RemoveService(service);
1148 Mock::VerifyAndClearExpectations(mock_service);
1149 Mock::VerifyAndClearExpectations(profile);
1150 EXPECT_TRUE(manager()->HasService(service));
1151 EXPECT_EQ(profile.get(), service->profile().get());
1152
1153 // If service becomes ephemeral since there is no profile to support it,
1154 // it should be unloaded.
1155 EXPECT_CALL(*profile, AbandonService(service));
1156 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1157 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1158 manager()->RemoveService(service);
1159 EXPECT_FALSE(manager()->HasService(service));
1160}
1161
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001162TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1163 // It's much easier to use real Glib in creating a Manager for this
1164 // test here since we want the storage side-effects.
1165 GLib glib;
1166 ScopedTempDir temp_dir;
1167 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1168 Manager manager(control_interface(),
1169 dispatcher(),
1170 metrics(),
1171 &glib,
1172 run_path(),
1173 storage_path(),
1174 temp_dir.path().value());
1175
1176 const char kProfile0[] = "profile0";
1177 FilePath profile_path(
1178 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1179
1180 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001181 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001182 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1183
1184 // Ensure that even if the backing filestore is removed, we still can't
1185 // create a profile twice.
Ben Chana0ddf462014-02-06 11:32:42 -08001186 ASSERT_TRUE(base::DeleteFile(profile_path, false));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001187 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1188}
1189
Paul Stewart75225512012-01-26 22:51:33 -08001190TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1191 MockServiceRefPtr s_not_in_profile(
1192 new NiceMock<MockService>(control_interface(),
1193 dispatcher(),
1194 metrics(),
1195 manager()));
1196 MockServiceRefPtr s_not_in_group(
1197 new NiceMock<MockService>(control_interface(),
1198 dispatcher(),
1199 metrics(),
1200 manager()));
1201 MockServiceRefPtr s_configure_fail(
1202 new NiceMock<MockService>(control_interface(),
1203 dispatcher(),
1204 metrics(),
1205 manager()));
1206 MockServiceRefPtr s_configure_succeed(
1207 new NiceMock<MockService>(control_interface(),
1208 dispatcher(),
1209 metrics(),
1210 manager()));
1211
1212 string entry_name("entry_name");
1213 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1214 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1215 .WillRepeatedly(Return("not_entry_name"));
1216 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1217 .WillRepeatedly(Return(entry_name));
1218 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1219 .WillRepeatedly(Return(entry_name));
1220
1221 manager()->RegisterService(s_not_in_profile);
1222 manager()->RegisterService(s_not_in_group);
1223 manager()->RegisterService(s_configure_fail);
1224 manager()->RegisterService(s_configure_succeed);
1225
1226 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001227 new StrictMock<MockProfile>(
1228 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001229 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001230 new StrictMock<MockProfile>(
1231 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001232
1233 s_not_in_group->set_profile(profile1);
1234 s_configure_fail->set_profile(profile1);
1235 s_configure_succeed->set_profile(profile1);
1236
1237 AdoptProfile(manager(), profile0);
1238 AdoptProfile(manager(), profile1);
1239
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001240 CompleteServiceSort();
1241
Paul Stewart75225512012-01-26 22:51:33 -08001242 // No services are a member of this profile.
1243 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001244 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001245
1246 // No services that are members of this profile have this entry name.
1247 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001248 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001249
1250 // Only services that are members of the profile and group will be abandoned.
1251 EXPECT_CALL(*profile1.get(),
1252 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1253 EXPECT_CALL(*profile1.get(),
1254 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1255 EXPECT_CALL(*profile1.get(),
1256 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1257 .WillOnce(Return(true));
1258 EXPECT_CALL(*profile1.get(),
1259 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1260 .WillOnce(Return(true));
1261
1262 // Never allow services to re-join profile1.
1263 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1264 .WillRepeatedly(Return(false));
1265
1266 // Only allow one of the members of the profile and group to successfully
1267 // join profile0.
1268 EXPECT_CALL(*profile0.get(),
1269 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1270 EXPECT_CALL(*profile0.get(),
1271 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1272 EXPECT_CALL(*profile0.get(),
1273 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1274 .WillOnce(Return(false));
1275 EXPECT_CALL(*profile0.get(),
1276 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1277 .WillOnce(Return(true));
1278
1279 // Expect the failed-to-configure service to have Unload() called on it.
1280 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1281 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1282 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1283 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1284
1285 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001286 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001287
1288 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1289 EXPECT_EQ(profile1, s_not_in_group->profile());
1290 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1291
1292 // Since we are using a MockProfile, the profile does not actually change,
1293 // since ConfigureService was not actually called on the service.
1294 EXPECT_EQ(profile1, s_configure_succeed->profile());
1295}
1296
Paul Stewart65512e12012-03-26 18:01:08 -07001297TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1298 MockServiceRefPtr s_will_remove0(
1299 new NiceMock<MockService>(control_interface(),
1300 dispatcher(),
1301 metrics(),
1302 manager()));
1303 MockServiceRefPtr s_will_remove1(
1304 new NiceMock<MockService>(control_interface(),
1305 dispatcher(),
1306 metrics(),
1307 manager()));
1308 MockServiceRefPtr s_will_not_remove0(
1309 new NiceMock<MockService>(control_interface(),
1310 dispatcher(),
1311 metrics(),
1312 manager()));
1313 MockServiceRefPtr s_will_not_remove1(
1314 new NiceMock<MockService>(control_interface(),
1315 dispatcher(),
1316 metrics(),
1317 manager()));
1318
Ben Chancc225ef2014-09-30 13:26:51 -07001319 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(nullptr))
Paul Stewart65512e12012-03-26 18:01:08 -07001320 .Times(4); // Once for each registration.
1321
1322 string entry_name("entry_name");
1323 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1324 .WillRepeatedly(Return(entry_name));
1325 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1326 .WillRepeatedly(Return(entry_name));
1327 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1328 .WillRepeatedly(Return(entry_name));
1329 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1330 .WillRepeatedly(Return(entry_name));
1331
1332 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001333 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001334 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001335 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001336 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001337 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001338 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001339 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001340
1341 // One for each service added above.
1342 ASSERT_EQ(4, manager()->services_.size());
1343
1344 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001345 new StrictMock<MockProfile>(
1346 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001347
1348 s_will_remove0->set_profile(profile);
1349 s_will_remove1->set_profile(profile);
1350 s_will_not_remove0->set_profile(profile);
1351 s_will_not_remove1->set_profile(profile);
1352
1353 AdoptProfile(manager(), profile);
1354
1355 // Deny any of the services re-entry to the profile.
1356 EXPECT_CALL(*profile, ConfigureService(_))
1357 .WillRepeatedly(Return(false));
1358
1359 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1360 .WillOnce(Return(true));
1361 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1362 .WillOnce(Return(true));
1363 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1364 .WillOnce(Return(true));
1365 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1366 .WillOnce(Return(true));
1367
1368 EXPECT_CALL(*s_will_remove0, Unload())
1369 .WillOnce(Return(true));
1370 EXPECT_CALL(*s_will_remove1, Unload())
1371 .WillOnce(Return(true));
1372 EXPECT_CALL(*s_will_not_remove0, Unload())
1373 .WillOnce(Return(false));
1374 EXPECT_CALL(*s_will_not_remove1, Unload())
1375 .WillOnce(Return(false));
1376
1377
1378 // This will cause all the profiles to be unloaded.
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001379 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart65512e12012-03-26 18:01:08 -07001380 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001381 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewart65512e12012-03-26 18:01:08 -07001382
1383 // 2 of the 4 services added above should have been unregistered and
1384 // removed, leaving 2.
1385 EXPECT_EQ(2, manager()->services_.size());
1386 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1387 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1388}
1389
1390TEST_F(ManagerTest, PopProfileWithUnload) {
1391 MockServiceRefPtr s_will_remove0(
1392 new NiceMock<MockService>(control_interface(),
1393 dispatcher(),
1394 metrics(),
1395 manager()));
1396 MockServiceRefPtr s_will_remove1(
1397 new NiceMock<MockService>(control_interface(),
1398 dispatcher(),
1399 metrics(),
1400 manager()));
1401 MockServiceRefPtr s_will_not_remove0(
1402 new NiceMock<MockService>(control_interface(),
1403 dispatcher(),
1404 metrics(),
1405 manager()));
1406 MockServiceRefPtr s_will_not_remove1(
1407 new NiceMock<MockService>(control_interface(),
1408 dispatcher(),
1409 metrics(),
1410 manager()));
1411
Ben Chancc225ef2014-09-30 13:26:51 -07001412 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(nullptr))
Paul Stewart65512e12012-03-26 18:01:08 -07001413 .Times(5); // Once for each registration, and one after profile pop.
1414
1415 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001416 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001417 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001418 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001419 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001420 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001421 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001422 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001423
1424 // One for each service added above.
1425 ASSERT_EQ(4, manager()->services_.size());
1426
1427 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001428 new StrictMock<MockProfile>(
1429 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001430 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001431 new StrictMock<MockProfile>(
1432 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001433
1434 s_will_remove0->set_profile(profile1);
1435 s_will_remove1->set_profile(profile1);
1436 s_will_not_remove0->set_profile(profile1);
1437 s_will_not_remove1->set_profile(profile1);
1438
1439 AdoptProfile(manager(), profile0);
1440 AdoptProfile(manager(), profile1);
1441
1442 // Deny any of the services entry to profile0, so they will all be unloaded.
1443 EXPECT_CALL(*profile0, ConfigureService(_))
1444 .WillRepeatedly(Return(false));
1445
1446 EXPECT_CALL(*s_will_remove0, Unload())
1447 .WillOnce(Return(true));
1448 EXPECT_CALL(*s_will_remove1, Unload())
1449 .WillOnce(Return(true));
1450 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001451 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001452 EXPECT_CALL(*s_will_not_remove1, Unload())
1453 .WillOnce(Return(false));
1454
Philipp Neubeck79173602012-11-13 21:10:09 +01001455 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1456 // profile list.
1457 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1458 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1459
Paul Stewart65512e12012-03-26 18:01:08 -07001460 // This will pop profile1, which should cause all our profiles to unload.
1461 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001462 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001463
1464 // 2 of the 4 services added above should have been unregistered and
1465 // removed, leaving 2.
1466 EXPECT_EQ(2, manager()->services_.size());
1467 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1468 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001469
1470 // Expect the unloaded services to lose their profile reference.
1471 EXPECT_FALSE(s_will_remove0->profile());
1472 EXPECT_FALSE(s_will_remove1->profile());
1473
1474 // If we explicitly deregister a service, the effect should be the same
1475 // with respect to the profile reference.
1476 ASSERT_TRUE(s_will_not_remove0->profile());
1477 manager()->DeregisterService(s_will_not_remove0);
1478 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001479}
1480
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001481TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001482 {
1483 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001484 ::DBus::Variant offline_mode;
1485 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001486 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001487 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001488 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001489 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001490 }
1491 {
1492 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001493 ::DBus::Variant country;
1494 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001495 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001496 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001497 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001498 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001499 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001500 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001501 {
1502 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001503 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001504 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001505 PropertyStoreTest::kBoolV,
1506 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001507 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001508 }
1509 {
1510 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001511 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001512 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001513 PropertyStoreTest::kStringV,
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 Masoneb925cc82011-06-22 15:39:57 -07001517 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001518 {
1519 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001520 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001521 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001522 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001523 PropertyStoreTest::kStringsV,
1524 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001525 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001526 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001527}
1528
mukesh agrawal32399322011-09-01 10:53:43 -07001529TEST_F(ManagerTest, RequestScan) {
1530 {
1531 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001532 manager()->RegisterDevice(mock_devices_[0].get());
1533 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001534 EXPECT_CALL(*mock_devices_[0], technology())
1535 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001536 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001537 EXPECT_CALL(*mock_devices_[1], technology())
1538 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001539 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Peter Qiue783f1c2014-05-02 11:42:33 -07001540 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1541 Metrics::kUserInitiatedEventWifiScan)).Times(1);
Ben Chan923a5022013-09-20 11:23:23 -07001542 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
Peter Qiue783f1c2014-05-02 11:42:33 -07001543 manager()->DeregisterDevice(mock_devices_[0].get());
1544 manager()->DeregisterDevice(mock_devices_[1].get());
1545 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1546 Mock::VerifyAndClearExpectations(mock_devices_[1]);
1547
1548 manager()->RegisterDevice(mock_devices_[0].get());
1549 EXPECT_CALL(*mock_devices_[0], technology())
1550 .WillRepeatedly(Return(Technology::kWifi));
1551 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1552 Metrics::kUserInitiatedEventWifiScan)).Times(1);
1553 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
1554 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1555 manager()->DeregisterDevice(mock_devices_[0].get());
1556 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1557
1558 manager()->RegisterDevice(mock_devices_[0].get());
1559 EXPECT_CALL(*mock_devices_[0], technology())
1560 .WillRepeatedly(Return(Technology::kUnknown));
1561 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1562 Metrics::kUserInitiatedEventWifiScan)).Times(0);
1563 EXPECT_CALL(*mock_devices_[0], Scan(_, _, _)).Times(0);
1564 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1565 manager()->DeregisterDevice(mock_devices_[0].get());
1566 Mock::VerifyAndClearExpectations(mock_devices_[0]);
mukesh agrawal32399322011-09-01 10:53:43 -07001567 }
1568
1569 {
1570 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001571 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001572 EXPECT_EQ(Error::kInvalidArguments, error.type());
1573 }
1574}
1575
Darin Petkovb65c2452012-02-23 15:17:06 +01001576TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001577 KeyValueStore args;
1578 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001579 manager()->GetService(args, &e);
1580 EXPECT_EQ(Error::kInvalidArguments, e.type());
1581 EXPECT_EQ("must specify service type", e.message());
1582}
1583
1584TEST_F(ManagerTest, GetServiceUnknownType) {
1585 KeyValueStore args;
1586 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001587 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001588 manager()->GetService(args, &e);
1589 EXPECT_EQ(Error::kNotSupported, e.type());
1590 EXPECT_EQ("service type is unsupported", e.message());
1591}
1592
Paul Stewart35eff132013-04-12 12:08:40 -07001593TEST_F(ManagerTest, GetServiceEthernetEap) {
1594 KeyValueStore args;
1595 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001596 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1597 dispatcher(),
1598 metrics(),
1599 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001600 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001601 SetEapProviderService(service);
1602 EXPECT_EQ(service, manager()->GetService(args, &e));
1603 EXPECT_TRUE(e.IsSuccess());
1604}
1605
Darin Petkovb65c2452012-02-23 15:17:06 +01001606TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001607 KeyValueStore args;
1608 Error e;
1609 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001610 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001611 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001612 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001613 manager()->GetService(args, &e);
1614 EXPECT_TRUE(e.IsSuccess());
1615}
1616
Darin Petkov33af05c2012-02-28 10:10:30 +01001617TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1618 KeyValueStore args;
1619 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001620 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001621 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001622 new StrictMock<MockProfile>(
1623 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001624 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001625 ServiceRefPtr service = manager()->GetService(args, &e);
1626 EXPECT_EQ(Error::kNotSupported, e.type());
1627 EXPECT_FALSE(service);
1628}
1629
Darin Petkovb65c2452012-02-23 15:17:06 +01001630TEST_F(ManagerTest, GetServiceVPN) {
1631 KeyValueStore args;
1632 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001633 args.SetString(kTypeProperty, kTypeVPN);
1634 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1635 args.SetString(kProviderHostProperty, "10.8.0.1");
1636 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001637 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001638 new StrictMock<MockProfile>(
1639 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001640 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001641
1642#if defined(DISABLE_VPN)
1643
1644 ServiceRefPtr service = manager()->GetService(args, &e);
1645 EXPECT_EQ(Error::kNotSupported, e.type());
1646 EXPECT_FALSE(service);
1647
1648#else
1649
Paul Stewart7f5ad572012-06-04 15:18:54 -07001650 ServiceRefPtr updated_service;
1651 EXPECT_CALL(*profile, UpdateService(_))
1652 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1653 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001654 EXPECT_CALL(*profile, LoadService(_))
1655 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001656 EXPECT_CALL(*profile, ConfigureService(_))
1657 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001658 ServiceRefPtr service = manager()->GetService(args, &e);
1659 EXPECT_TRUE(e.IsSuccess());
1660 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001661 EXPECT_EQ(service, updated_service);
1662 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001663
1664#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001665}
1666
Ben Chan520eb172013-10-30 20:51:04 -07001667#if !defined(DISABLE_WIMAX)
1668
Darin Petkovc63dcf02012-05-24 11:51:43 +02001669TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
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 ServiceRefPtr service = manager()->GetService(args, &e);
1674 EXPECT_EQ(Error::kInvalidArguments, e.type());
1675 EXPECT_EQ("Missing WiMAX network id.", e.message());
1676 EXPECT_FALSE(service);
1677}
1678
Darin Petkovd1cd7972012-05-22 15:26:15 +02001679TEST_F(ManagerTest, GetServiceWiMax) {
1680 KeyValueStore args;
1681 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001682 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001683 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001684 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001685 ServiceRefPtr service = manager()->GetService(args, &e);
1686 EXPECT_TRUE(e.IsSuccess());
1687 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001688}
1689
Ben Chan520eb172013-10-30 20:51:04 -07001690#endif // DISABLE_WIMAX
1691
Paul Stewart7f61e522012-03-22 11:13:45 -07001692TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1693 // Manager calls ActiveProfile() so we need at least one profile installed.
1694 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001695 new NiceMock<MockProfile>(
1696 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001697 AdoptProfile(manager(), profile);
1698
1699 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001700 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001701 Error error;
1702 manager()->ConfigureService(args, &error);
1703 EXPECT_EQ(Error::kInvalidArguments, error.type());
1704 EXPECT_EQ("Invalid profile name xxx", error.message());
1705}
1706
1707TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1708 // Manager calls ActiveProfile() so we need at least one profile installed.
1709 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001710 new NiceMock<MockProfile>(
1711 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001712 AdoptProfile(manager(), profile);
1713
1714 KeyValueStore args;
1715 Error error;
1716 manager()->ConfigureService(args, &error);
1717 EXPECT_EQ(Error::kInvalidArguments, error.type());
1718 EXPECT_EQ("must specify service type", error.message());
1719}
1720
1721// A registered service in the ephemeral profile should be moved to the
1722// active profile as a part of configuration if no profile was explicitly
1723// specified.
1724TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1725 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001726 new NiceMock<MockProfile>(
1727 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001728
1729 AdoptProfile(manager(), profile); // This is now the active profile.
1730
Paul Stewartd2e1c362013-03-03 19:06:07 -08001731 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001732 scoped_refptr<MockWiFiService> service(
1733 new NiceMock<MockWiFiService>(control_interface(),
1734 dispatcher(),
1735 metrics(),
1736 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001737 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001738 ssid,
1739 "",
1740 "",
1741 false));
1742
1743 manager()->RegisterService(service);
1744 service->set_profile(GetEphemeralProfile(manager()));
1745
Paul Stewart3c504012013-01-17 17:49:58 -08001746 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001747 .WillOnce(Return(service));
1748 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1749 .WillOnce(Return(true));
1750 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1751 .WillOnce(Return(true));
1752
1753 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001754 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001755 Error error;
1756 manager()->ConfigureService(args, &error);
1757 EXPECT_TRUE(error.IsSuccess());
1758}
1759
Paul Stewart2c575d22012-12-07 12:28:57 -08001760// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001761// specify a profile, it should be moved from the profile it was previously
1762// in to the specified profile if one was requested.
1763TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1764 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001765 new NiceMock<MockProfile>(
1766 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001767 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001768 new NiceMock<MockProfile>(
1769 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001770
1771 const string kProfileName0 = "profile0";
1772 const string kProfileName1 = "profile1";
1773
1774 EXPECT_CALL(*profile0, GetRpcIdentifier())
1775 .WillRepeatedly(Return(kProfileName0));
1776 EXPECT_CALL(*profile1, GetRpcIdentifier())
1777 .WillRepeatedly(Return(kProfileName1));
1778
1779 AdoptProfile(manager(), profile0);
1780 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1781
Paul Stewartd2e1c362013-03-03 19:06:07 -08001782 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001783 scoped_refptr<MockWiFiService> service(
1784 new NiceMock<MockWiFiService>(control_interface(),
1785 dispatcher(),
1786 metrics(),
1787 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001788 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001789 ssid,
1790 "",
1791 "",
1792 false));
1793
1794 manager()->RegisterService(service);
1795 service->set_profile(profile1);
1796
Paul Stewart3c504012013-01-17 17:49:58 -08001797 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001798 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001799 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1800 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001801 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1802 .WillOnce(Return(true));
1803 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1804 .WillOnce(Return(true));
1805 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1806 .WillOnce(Return(true));
1807
1808 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001809 args.SetString(kTypeProperty, kTypeWifi);
1810 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001811 Error error;
1812 manager()->ConfigureService(args, &error);
1813 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001814 service->set_profile(nullptr); // Breaks refcounting loop.
Paul Stewart7f61e522012-03-22 11:13:45 -07001815}
1816
Paul Stewart2c575d22012-12-07 12:28:57 -08001817// If we configure a service that is already a member of the specified
1818// profile, the Manager should not call LoadService or AdoptService again
1819// on this service.
1820TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1821 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001822 new NiceMock<MockProfile>(
1823 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001824
1825 const string kProfileName0 = "profile0";
1826
1827 EXPECT_CALL(*profile0, GetRpcIdentifier())
1828 .WillRepeatedly(Return(kProfileName0));
1829
1830 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1831
Paul Stewartd2e1c362013-03-03 19:06:07 -08001832 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001833 scoped_refptr<MockWiFiService> service(
1834 new NiceMock<MockWiFiService>(control_interface(),
1835 dispatcher(),
1836 metrics(),
1837 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001838 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001839 ssid,
1840 "",
1841 "",
1842 false));
1843
1844 manager()->RegisterService(service);
1845 service->set_profile(profile0);
1846
Paul Stewart3c504012013-01-17 17:49:58 -08001847 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001848 .WillOnce(Return(service));
1849 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1850 .Times(0);
1851 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1852 .WillOnce(Return(true));
1853 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1854 .Times(0);
1855
1856 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001857 args.SetString(kTypeProperty, kTypeWifi);
1858 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001859 Error error;
1860 manager()->ConfigureService(args, &error);
1861 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001862 service->set_profile(nullptr); // Breaks refcounting loop.
Paul Stewart2c575d22012-12-07 12:28:57 -08001863}
1864
Paul Stewart7f61e522012-03-22 11:13:45 -07001865// An unregistered service should remain unregistered, but its contents should
1866// be saved to the specified profile nonetheless.
1867TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1868 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001869 new NiceMock<MockProfile>(
1870 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001871 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001872 new NiceMock<MockProfile>(
1873 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001874
1875 const string kProfileName0 = "profile0";
1876 const string kProfileName1 = "profile1";
1877
1878 EXPECT_CALL(*profile0, GetRpcIdentifier())
1879 .WillRepeatedly(Return(kProfileName0));
1880 EXPECT_CALL(*profile1, GetRpcIdentifier())
1881 .WillRepeatedly(Return(kProfileName1));
1882
1883 AdoptProfile(manager(), profile0);
1884 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1885
Paul Stewartd2e1c362013-03-03 19:06:07 -08001886 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001887 scoped_refptr<MockWiFiService> service(
1888 new NiceMock<MockWiFiService>(control_interface(),
1889 dispatcher(),
1890 metrics(),
1891 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001892 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001893 ssid,
1894 "",
1895 "",
1896 false));
1897
1898 service->set_profile(profile1);
1899
Paul Stewart3c504012013-01-17 17:49:58 -08001900 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001901 .WillOnce(Return(service));
1902 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1903 .WillOnce(Return(true));
1904 EXPECT_CALL(*profile0, AdoptService(_))
1905 .Times(0);
1906 EXPECT_CALL(*profile1, AdoptService(_))
1907 .Times(0);
1908
1909 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001910 args.SetString(kTypeProperty, kTypeWifi);
1911 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001912 Error error;
1913 manager()->ConfigureService(args, &error);
1914 EXPECT_TRUE(error.IsSuccess());
1915}
1916
Paul Stewartd2e1c362013-03-03 19:06:07 -08001917TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1918 KeyValueStore args;
1919 Error error;
1920 ServiceRefPtr service =
1921 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001922 EXPECT_EQ(Error::kInvalidArguments, error.type());
1923 EXPECT_EQ("must specify service type", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001924 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001925}
1926
1927TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1928 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001929 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001930 Error error;
1931 ServiceRefPtr service =
1932 manager()->ConfigureServiceForProfile("", args, &error);
1933 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001934 EXPECT_EQ("service type is unsupported", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001935 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001936}
1937
1938TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1939 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001940 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001941 Error error;
1942 ServiceRefPtr service =
1943 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1944 EXPECT_EQ(Error::kNotFound, error.type());
1945 EXPECT_EQ("Profile specified was not found", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001946 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001947}
1948
1949TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1950 const string kProfileName0 = "profile0";
1951 const string kProfileName1 = "profile1";
1952 scoped_refptr<MockProfile> profile0(
1953 AddNamedMockProfileToManager(manager(), kProfileName0));
1954
1955 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001956 args.SetString(kTypeProperty, kTypeWifi);
1957 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001958 Error error;
1959 ServiceRefPtr service =
1960 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1961 EXPECT_EQ(Error::kInvalidArguments, error.type());
1962 EXPECT_EQ("Profile argument does not match that in "
1963 "the configuration arguments", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001964 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001965}
1966
1967TEST_F(ManagerTest,
1968 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1969 const string kProfileName0 = "profile0";
1970 scoped_refptr<MockProfile> profile0(
1971 AddNamedMockProfileToManager(manager(), kProfileName0));
1972 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001973 args.SetString(kTypeProperty, kTypeWifi);
1974 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001975
1976 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1977 .WillOnce(Return(WiFiServiceRefPtr()));
1978 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1979 .WillOnce(Return(WiFiServiceRefPtr()));
1980 Error error;
1981 ServiceRefPtr service =
1982 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1983 // Since we didn't set the error in the GetService expectation above...
1984 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001985 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001986}
1987
1988TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1989 const string kProfileName0 = "profile0";
1990 scoped_refptr<MockProfile> profile0(
1991 AddNamedMockProfileToManager(manager(), kProfileName0));
1992
1993 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001994 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001995
1996 scoped_refptr<MockWiFiService> mock_service(
1997 new NiceMock<MockWiFiService>(control_interface(),
1998 dispatcher(),
1999 metrics(),
2000 manager(),
2001 wifi_provider_,
2002 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002003 kModeManaged,
2004 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002005 false));
2006 ServiceRefPtr mock_service_generic(mock_service.get());
2007 mock_service->set_profile(profile0);
2008 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2009 .WillOnce(Return(WiFiServiceRefPtr()));
2010 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2011 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2012 .WillOnce(Return(true));
2013 Error error;
2014 ServiceRefPtr service =
2015 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2016 EXPECT_TRUE(error.IsSuccess());
2017 EXPECT_EQ(mock_service.get(), service.get());
Ben Chancc225ef2014-09-30 13:26:51 -07002018 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002019}
2020
2021TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2022 scoped_refptr<MockService> mock_service(
2023 new NiceMock<MockService>(control_interface(),
2024 dispatcher(),
2025 metrics(),
2026 manager()));
2027 const string kGUID = "a guid";
Ben Chancc225ef2014-09-30 13:26:51 -07002028 mock_service->SetGuid(kGUID, nullptr);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002029 manager()->RegisterService(mock_service);
2030 ServiceRefPtr mock_service_generic(mock_service.get());
2031
2032 const string kProfileName = "profile";
2033 scoped_refptr<MockProfile> profile(
2034 AddNamedMockProfileToManager(manager(), kProfileName));
2035 mock_service->set_profile(profile);
2036
2037 EXPECT_CALL(*mock_service, technology())
2038 .WillOnce(Return(Technology::kCellular))
2039 .WillOnce(Return(Technology::kWifi));
2040
2041 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2042 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2043 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2044
2045 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002046 args.SetString(kTypeProperty, kTypeWifi);
2047 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002048
2049 // The first attempt should fail because the service reports a technology
2050 // other than "WiFi".
2051 {
2052 Error error;
2053 ServiceRefPtr service =
2054 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
Ben Chancc225ef2014-09-30 13:26:51 -07002055 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002056 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002057 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002058 }
2059
2060 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2061 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2062
2063 {
2064 Error error;
2065 ServiceRefPtr service =
2066 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2067 EXPECT_TRUE(error.IsSuccess());
2068 EXPECT_EQ(mock_service.get(), service.get());
2069 EXPECT_EQ(profile.get(), service->profile().get());
2070 }
Ben Chancc225ef2014-09-30 13:26:51 -07002071 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002072}
2073
2074TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2075 const string kProfileName = "profile";
2076 scoped_refptr<MockProfile> profile(
2077 AddNamedMockProfileToManager(manager(), kProfileName));
2078
2079 scoped_refptr<MockWiFiService> mock_service(
2080 new NiceMock<MockWiFiService>(control_interface(),
2081 dispatcher(),
2082 metrics(),
2083 manager(),
2084 wifi_provider_,
2085 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002086 kModeManaged,
2087 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002088 false));
2089 mock_service->set_profile(profile);
2090 ServiceRefPtr mock_service_generic(mock_service.get());
2091
2092 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002093 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002094 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2095 .WillOnce(Return(mock_service));
2096 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2097 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2098 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2099 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2100
2101 Error error;
2102 ServiceRefPtr service =
2103 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2104 EXPECT_TRUE(error.IsSuccess());
2105 EXPECT_EQ(mock_service.get(), service.get());
2106 EXPECT_EQ(profile.get(), service->profile().get());
Ben Chancc225ef2014-09-30 13:26:51 -07002107 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002108}
2109
2110TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2111 const string kProfileName = "profile";
2112 scoped_refptr<MockProfile> profile(
2113 AddNamedMockProfileToManager(manager(), kProfileName));
2114
2115 scoped_refptr<MockWiFiService> mock_service(
2116 new NiceMock<MockWiFiService>(control_interface(),
2117 dispatcher(),
2118 metrics(),
2119 manager(),
2120 wifi_provider_,
2121 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002122 kModeManaged,
2123 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002124 false));
2125 mock_service->set_profile(GetEphemeralProfile(manager()));
2126 ServiceRefPtr mock_service_generic(mock_service.get());
2127
2128 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002129 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002130 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2131 .WillOnce(Return(mock_service));
2132 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2133 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2134 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2135
2136 Error error;
2137 ServiceRefPtr service =
2138 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2139 EXPECT_TRUE(error.IsSuccess());
2140 EXPECT_EQ(mock_service.get(), service.get());
2141 EXPECT_EQ(profile.get(), service->profile().get());
Ben Chancc225ef2014-09-30 13:26:51 -07002142 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002143}
2144
2145TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2146 const string kProfileName0 = "profile0";
2147 scoped_refptr<MockProfile> profile0(
2148 AddNamedMockProfileToManager(manager(), kProfileName0));
2149 const string kProfileName1 = "profile1";
2150 scoped_refptr<MockProfile> profile1(
2151 AddNamedMockProfileToManager(manager(), kProfileName1));
2152
2153 scoped_refptr<MockWiFiService> mock_service(
2154 new NiceMock<MockWiFiService>(control_interface(),
2155 dispatcher(),
2156 metrics(),
2157 manager(),
2158 wifi_provider_,
2159 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002160 kModeManaged,
2161 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002162 false));
2163 manager()->RegisterService(mock_service);
2164 mock_service->set_profile(profile0);
2165 ServiceRefPtr mock_service_generic(mock_service.get());
2166
2167 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002168 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002169 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2170 .WillOnce(Return(mock_service));
2171 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2172 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2173 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2174 // This happens once to make the service loadable for the ConfigureService
2175 // below, and a second time after the service is modified.
2176 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2177 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2178 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2179 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2180
2181 Error error;
2182 ServiceRefPtr service =
2183 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2184 EXPECT_TRUE(error.IsSuccess());
2185 EXPECT_EQ(mock_service.get(), service.get());
Ben Chancc225ef2014-09-30 13:26:51 -07002186 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002187}
2188
2189TEST_F(ManagerTest,
2190 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2191 const string kProfileName0 = "profile0";
2192 scoped_refptr<MockProfile> profile0(
2193 AddNamedMockProfileToManager(manager(), kProfileName0));
2194 const string kProfileName1 = "profile1";
2195 scoped_refptr<MockProfile> profile1(
2196 AddNamedMockProfileToManager(manager(), kProfileName1));
2197
2198 scoped_refptr<MockWiFiService> matching_service(
2199 new StrictMock<MockWiFiService>(control_interface(),
2200 dispatcher(),
2201 metrics(),
2202 manager(),
2203 wifi_provider_,
2204 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002205 kModeManaged,
2206 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002207 false));
2208 matching_service->set_profile(profile1);
2209
2210 // We need to get rid of our reference to this mock service as soon
2211 // as Manager::ConfigureServiceForProfile() takes a reference in its
2212 // call to WiFiProvider::CreateTemporaryService(). This way the
2213 // latter function can keep a DCHECK(service->HasOneRef() even in
2214 // unit tests.
2215 temp_mock_service_ =
2216 new NiceMock<MockWiFiService>(control_interface(),
2217 dispatcher(),
2218 metrics(),
2219 manager(),
2220 wifi_provider_,
2221 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002222 kModeManaged,
2223 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002224 false);
2225
2226 // Only hold a pointer here so we don't affect the refcount.
2227 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2228
2229 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002230 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002231 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2232 .WillOnce(Return(matching_service));
2233 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2234 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2235 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2236 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2237 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2238 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2239 .Times(1);
2240 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2241 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2242
2243 Error error;
2244 ServiceRefPtr service =
2245 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2246 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07002247 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002248 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2249}
2250
Paul Stewart7a20aa42013-01-17 12:21:41 -08002251TEST_F(ManagerTest, FindMatchingService) {
2252 KeyValueStore args;
2253 {
2254 Error error;
2255 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2256 EXPECT_EQ(Error::kNotFound, error.type());
2257 }
2258
2259 scoped_refptr<MockService> mock_service0(
2260 new NiceMock<MockService>(control_interface(),
2261 dispatcher(),
2262 metrics(),
2263 manager()));
2264 scoped_refptr<MockService> mock_service1(
2265 new NiceMock<MockService>(control_interface(),
2266 dispatcher(),
2267 metrics(),
2268 manager()));
2269 manager()->RegisterService(mock_service0);
2270 manager()->RegisterService(mock_service1);
2271 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2272 .WillOnce(Return(true))
2273 .WillRepeatedly(Return(false));
2274 {
2275 Error error;
2276 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2277 EXPECT_TRUE(error.IsSuccess());
2278 }
2279 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2280 .WillOnce(Return(true))
2281 .WillRepeatedly(Return(false));
2282 {
2283 Error error;
2284 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2285 EXPECT_TRUE(error.IsSuccess());
2286 }
2287 {
2288 Error error;
2289 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2290 EXPECT_EQ(Error::kNotFound, error.type());
2291 }
2292}
2293
Paul Stewart22aa71b2011-09-16 12:15:11 -07002294TEST_F(ManagerTest, TechnologyOrder) {
2295 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002296 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2297 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002298 ASSERT_TRUE(error.IsSuccess());
2299 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002300 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002301
Ben Chan923a5022013-09-20 11:23:23 -07002302 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2303 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002304 ASSERT_FALSE(error.IsSuccess());
2305 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002306 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002307 manager()->GetTechnologyOrder());
2308}
2309
2310TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002311 // TODO(quiche): Some of these tests would probably fit better in
2312 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002313 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002314
mukesh agrawale37ad322013-10-08 16:33:56 -07002315 // Construct our Services so that the string comparison of
2316 // unique_name_ differs from the numerical comparison of
2317 // serial_number_.
2318 vector<scoped_refptr<MockService>> mock_services;
2319 for (size_t i = 0; i < 11; ++i) {
2320 mock_services.push_back(
2321 new NiceMock<MockService>(control_interface(),
2322 dispatcher(),
2323 metrics(),
2324 manager()));
2325 }
2326 scoped_refptr<MockService> mock_service2 = mock_services[2];
2327 scoped_refptr<MockService> mock_service10 = mock_services[10];
2328 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002329
mukesh agrawale37ad322013-10-08 16:33:56 -07002330 manager()->RegisterService(mock_service2);
2331 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002332
mukesh agrawale37ad322013-10-08 16:33:56 -07002333 // Services should already be sorted by |serial_number_|.
2334 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002335
2336 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002337 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002338 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002339
2340 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002341 mock_service10->SetStrength(1);
2342 manager()->UpdateService(mock_service10);
2343 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002344
Rebecca Silberstein6d4836b2014-07-16 14:28:28 -07002345
2346 // Sort services based on profiles. When comparing two services
2347 // with different profiles, prefer the one that is not ephemeral.
2348 // If both services have legitimate profiles, prefer the service
2349 // with the more recently applied profile.
2350 // Compare between an ephemeral and actual profile.
2351 Error error;
2352 scoped_refptr<MockProfile> default_profile(
2353 new MockProfile(control_interface(), metrics(), manager(), ""));
2354 string default_profile_name("/profile/default");
2355 EXPECT_CALL(*default_profile, GetRpcIdentifier())
2356 .WillRepeatedly(Return(default_profile_name));
2357 AdoptProfile(manager(), default_profile);
2358 mock_service2->set_profile(default_profile);
2359 manager()->UpdateService(mock_service2);
2360 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
2361
2362 // Compare between two different profiles
2363 scoped_refptr<MockProfile> user_profile(
2364 new MockProfile(control_interface(), metrics(), manager(), ""));
2365 string user_profile_name("/profile/chonos/shill");
2366 EXPECT_CALL(*user_profile, GetRpcIdentifier())
2367 .WillRepeatedly(Return(user_profile_name));
2368 AdoptProfile(manager(), user_profile);
2369 mock_service10->set_profile(user_profile);
2370 manager()->UpdateService(mock_service10);
2371 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2372
Paul Stewart22aa71b2011-09-16 12:15:11 -07002373 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002374 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2375 manager()->UpdateService(mock_service2);
2376 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002377
2378 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002379 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002380 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002381 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002382 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002383
mukesh agrawal84de5d22012-02-17 19:29:15 -08002384 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002385 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002386 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002387
Ben Chan923a5022013-09-20 11:23:23 -07002388 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2389 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002390 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002391 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002392
Gaurav Shah435de2c2011-11-17 19:01:07 -08002393 // Priority.
Ben Chancc225ef2014-09-30 13:26:51 -07002394 mock_service2->SetPriority(1, nullptr);
mukesh agrawale37ad322013-10-08 16:33:56 -07002395 manager()->UpdateService(mock_service2);
2396 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002397
Paul Stewart2da34c02013-10-17 15:28:56 -07002398 // HasEverConnected.
2399 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002400 manager()->UpdateService(mock_service10);
2401 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002402
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002403 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002404 mock_service2->SetAutoConnect(true);
2405 manager()->UpdateService(mock_service2);
2406 mock_service10->SetAutoConnect(false);
2407 manager()->UpdateService(mock_service10);
2408 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002409
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002410 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2411 // to any of the others below, so we reset to the default state after
2412 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002413 EXPECT_CALL(*mock_service10.get(),
2414 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002415 .WillOnce(Return(true))
2416 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002417 manager()->UpdateService(mock_service10);
2418 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2419 manager()->UpdateService(mock_service2);
2420 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002421
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002422 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002423 mock_service10->SetConnectable(true);
2424 manager()->UpdateService(mock_service10);
2425 mock_service2->SetConnectable(false);
2426 manager()->UpdateService(mock_service2);
2427 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002428
2429 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002430 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002431 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002432 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002433 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002434 manager()->UpdateService(mock_service2);
2435 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002436 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002437 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002438 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002439 manager()->UpdateService(mock_service10);
2440 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002441
2442 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002443 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002444 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002445 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002446 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002447 manager()->UpdateService(mock_service10);
2448 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002449
mukesh agrawale37ad322013-10-08 16:33:56 -07002450 // Connected-but-portalled preferred over unconnected.
2451 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002452 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002453 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002454 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002455 manager()->UpdateService(mock_service2);
2456 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002457
mukesh agrawale37ad322013-10-08 16:33:56 -07002458 // Connected preferred over connected-but-portalled.
2459 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002460 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002461 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002462 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002463 manager()->UpdateService(mock_service10);
2464 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002465
mukesh agrawale37ad322013-10-08 16:33:56 -07002466 manager()->DeregisterService(mock_service2);
2467 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002468}
2469
Peter Qiu700de642014-07-14 16:31:30 -07002470TEST_F(ManagerTest, ConnectionStatusCheck) {
2471 // Setup mock metrics and service.
2472 MockMetrics mock_metrics(dispatcher());
2473 SetMetrics(&mock_metrics);
2474 scoped_refptr<MockService> mock_service = new NiceMock<MockService>(
2475 control_interface(), dispatcher(), metrics(), manager());
2476 manager()->RegisterService(mock_service);
2477
2478 // Device not connected.
2479 EXPECT_CALL(*mock_service.get(), IsConnected())
2480 .WillOnce(Return(false));
2481 EXPECT_CALL(mock_metrics,
2482 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOffline));
Peter Qiu94d18af2014-09-11 15:54:15 -07002483 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002484
2485 // Device connected, but not online.
2486 EXPECT_CALL(*mock_service.get(), IsConnected())
2487 .WillOnce(Return(true));
2488 EXPECT_CALL(*mock_service.get(), IsOnline())
2489 .WillOnce(Return(false));
2490 EXPECT_CALL(mock_metrics,
2491 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline)).Times(0);
2492 EXPECT_CALL(mock_metrics,
2493 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002494 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002495
2496 // Device connected and online.
2497 EXPECT_CALL(*mock_service.get(), IsConnected())
2498 .WillOnce(Return(true));
2499 EXPECT_CALL(*mock_service.get(), IsOnline())
2500 .WillOnce(Return(true));
2501 EXPECT_CALL(mock_metrics,
2502 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline));
2503 EXPECT_CALL(mock_metrics,
2504 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002505 manager()->ConnectionStatusCheck();
2506}
2507
2508TEST_F(ManagerTest, DevicePresenceStatusCheck) {
2509 // Setup mock metrics and service.
2510 MockMetrics mock_metrics(dispatcher());
2511 SetMetrics(&mock_metrics);
2512
2513 manager()->RegisterDevice(mock_devices_[0]);
2514 manager()->RegisterDevice(mock_devices_[1]);
2515 manager()->RegisterDevice(mock_devices_[2]);
2516 manager()->RegisterDevice(mock_devices_[3]);
2517
2518 ON_CALL(*mock_devices_[0].get(), technology())
2519 .WillByDefault(Return(Technology::kEthernet));
2520 ON_CALL(*mock_devices_[1].get(), technology())
2521 .WillByDefault(Return(Technology::kWifi));
2522 ON_CALL(*mock_devices_[2].get(), technology())
2523 .WillByDefault(Return(Technology::kCellular));
2524 ON_CALL(*mock_devices_[3].get(), technology())
2525 .WillByDefault(Return(Technology::kWifi));
2526
2527 EXPECT_CALL(mock_metrics,
2528 NotifyDevicePresenceStatus(Technology::kEthernet, true));
2529 EXPECT_CALL(mock_metrics,
2530 NotifyDevicePresenceStatus(Technology::kWifi, true));
2531 EXPECT_CALL(mock_metrics,
2532 NotifyDevicePresenceStatus(Technology::kWiMax, false));
2533 EXPECT_CALL(mock_metrics,
2534 NotifyDevicePresenceStatus(Technology::kCellular, true));
2535 manager()->DevicePresenceStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002536}
2537
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002538TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002539 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002540 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002541
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002542 scoped_refptr<MockService> mock_service0(
2543 new NiceMock<MockService>(control_interface(),
2544 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002545 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002546 manager()));
2547 scoped_refptr<MockService> mock_service1(
2548 new NiceMock<MockService>(control_interface(),
2549 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002550 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002551 manager()));
2552
2553 scoped_refptr<MockConnection> mock_connection0(
2554 new NiceMock<MockConnection>(device_info_.get()));
2555 scoped_refptr<MockConnection> mock_connection1(
2556 new NiceMock<MockConnection>(device_info_.get()));
2557
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002558 // A single registered Service, without a connection. The
Ben Chancc225ef2014-09-30 13:26:51 -07002559 // DefaultService should be nullptr. If a change notification is
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002560 // generated, it should reference kNullPath.
Ben Chancc225ef2014-09-30 13:26:51 -07002561 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002562 EXPECT_CALL(*manager_adaptor_,
2563 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2564 DBusAdaptor::kNullPath))
2565 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002566 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002567 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002568
2569 // Adding another Service, also without a connection, does not
2570 // change DefaultService. Furthermore, we do not send a change
2571 // notification for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002572 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002573 EXPECT_CALL(*manager_adaptor_,
2574 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2575 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002576 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002577 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002578
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002579 // An explicit sort doesn't change anything, and does not emit a
2580 // change notification for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002581 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002582 EXPECT_CALL(*manager_adaptor_,
2583 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2584 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002585 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002586 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002587
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002588 // Re-ordering the unconnected Services doesn't change
2589 // DefaultService, and (hence) does not emit a change notification
2590 // for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002591 mock_service1->SetPriority(1, nullptr);
2592 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002593 EXPECT_CALL(*manager_adaptor_,
2594 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2595 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002596 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002597 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002598
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002599 // Re-ordering the unconnected Services doesn't change
2600 // DefaultService, and (hence) does not emit a change notification
2601 // for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002602 mock_service1->SetPriority(0, nullptr);
2603 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002604 EXPECT_CALL(*manager_adaptor_,
2605 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2606 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002607 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002608 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002609
Paul Stewartce4ec192012-03-14 12:53:46 -07002610 mock_service0->set_mock_connection(mock_connection0);
2611 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002612
mukesh agrawale37ad322013-10-08 16:33:56 -07002613 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002614 // from ServiceOrderIs. We notify others of the change in
2615 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002616 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002617 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002618 EXPECT_CALL(*manager_adaptor_,
2619 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002620 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002621 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002622
Darin Petkova5e07ef2012-07-09 14:27:57 +02002623 ServiceWatcher service_watcher;
2624 int tag =
2625 manager()->RegisterDefaultServiceCallback(
2626 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2627 service_watcher.AsWeakPtr()));
2628 EXPECT_EQ(1, tag);
2629
mukesh agrawale37ad322013-10-08 16:33:56 -07002630 // Changing the ordering causes the DefaultService to change, and
2631 // appropriate notifications are sent.
Ben Chancc225ef2014-09-30 13:26:51 -07002632 mock_service1->SetPriority(1, nullptr);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002633 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2634 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002635 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002636 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002637 EXPECT_CALL(*manager_adaptor_,
2638 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002639 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002640 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002641
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002642 // Deregistering a DefaultServiceCallback works as expected. (Later
2643 // code causes DefaultService changes, but we see no further calls
2644 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002645 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002646 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002647
2648 // Deregistering the current DefaultService causes the other Service
2649 // to become default. Appropriate notifications are sent.
2650 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002651 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002652 EXPECT_CALL(*manager_adaptor_,
2653 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Ben Chancc225ef2014-09-30 13:26:51 -07002654 mock_service1->set_mock_connection(nullptr); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002655 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002656 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002657
mukesh agrawale37ad322013-10-08 16:33:56 -07002658 // Deregistering the only Service causes the DefaultService to become
Ben Chancc225ef2014-09-30 13:26:51 -07002659 // nullptr. Appropriate notifications are sent.
2660 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002661 EXPECT_CALL(*manager_adaptor_,
2662 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Ben Chancc225ef2014-09-30 13:26:51 -07002663 mock_service0->set_mock_connection(nullptr); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002664 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002665 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002666
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002667 // An explicit sort doesn't change anything, and does not generate
2668 // an external notification.
Ben Chancc225ef2014-09-30 13:26:51 -07002669 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002670 EXPECT_CALL(*manager_adaptor_,
2671 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002672 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002673}
2674
Darin Petkova5e07ef2012-07-09 14:27:57 +02002675TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2676 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2677 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2678
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002679 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002680 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002681
2682 scoped_refptr<MockService> mock_service(
2683 new NiceMock<MockService>(
2684 control_interface(), dispatcher(), metrics(), manager()));
2685 ServiceRefPtr service = mock_service;
2686 ServiceRefPtr null_service;
2687
Ben Chancc225ef2014-09-30 13:26:51 -07002688 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002689 manager()->NotifyDefaultServiceChanged(null_service);
2690
2691 ServiceWatcher service_watcher1;
2692 ServiceWatcher service_watcher2;
2693 int tag1 =
2694 manager()->RegisterDefaultServiceCallback(
2695 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2696 service_watcher1.AsWeakPtr()));
2697 EXPECT_EQ(1, tag1);
2698 int tag2 =
2699 manager()->RegisterDefaultServiceCallback(
2700 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2701 service_watcher2.AsWeakPtr()));
2702 EXPECT_EQ(2, tag2);
2703
2704 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2705 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
Ben Chancc225ef2014-09-30 13:26:51 -07002706 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002707 manager()->NotifyDefaultServiceChanged(null_service);
2708
2709 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2710 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2711 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2712 manager()->NotifyDefaultServiceChanged(mock_service);
2713
2714 manager()->DeregisterDefaultServiceCallback(tag1);
2715 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2716 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2717 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2718 manager()->NotifyDefaultServiceChanged(mock_service);
2719 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2720
2721 manager()->DeregisterDefaultServiceCallback(tag2);
2722 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2723 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2724 manager()->NotifyDefaultServiceChanged(mock_service);
2725
2726 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2727 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2728}
2729
Peter Qiu9d581932014-04-14 16:37:37 -07002730TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2731 int connection_id1 = 100;
2732 int connection_id2 = 200;
2733 scoped_refptr<MockService> mock_service1 =
2734 new NiceMock<MockService>(control_interface(), dispatcher(),
2735 metrics(), manager());
2736 mock_service1->set_connection_id(connection_id1);
2737 scoped_refptr<MockService> mock_service2 =
2738 new NiceMock<MockService>(control_interface(), dispatcher(),
2739 metrics(), manager());
2740 mock_service2->set_connection_id(connection_id1);
2741 scoped_refptr<MockService> mock_service3 =
2742 new NiceMock<MockService>(control_interface(), dispatcher(),
2743 metrics(), manager());
2744 mock_service3->set_connection_id(connection_id2);
2745
2746 manager()->RegisterService(mock_service1);
2747 manager()->RegisterService(mock_service2);
2748 manager()->RegisterService(mock_service3);
2749
2750 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2751 manager()->ReportServicesOnSameNetwork(connection_id1);
2752
2753 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2754 manager()->ReportServicesOnSameNetwork(connection_id2);
2755}
2756
Gaurav Shah435de2c2011-11-17 19:01:07 -08002757TEST_F(ManagerTest, AvailableTechnologies) {
2758 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2759 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002760 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002761 manager(),
2762 "null4",
2763 "addr4",
2764 0));
2765 manager()->RegisterDevice(mock_devices_[0]);
2766 manager()->RegisterDevice(mock_devices_[1]);
2767 manager()->RegisterDevice(mock_devices_[2]);
2768 manager()->RegisterDevice(mock_devices_[3]);
2769
2770 ON_CALL(*mock_devices_[0].get(), technology())
2771 .WillByDefault(Return(Technology::kEthernet));
2772 ON_CALL(*mock_devices_[1].get(), technology())
2773 .WillByDefault(Return(Technology::kWifi));
2774 ON_CALL(*mock_devices_[2].get(), technology())
2775 .WillByDefault(Return(Technology::kCellular));
2776 ON_CALL(*mock_devices_[3].get(), technology())
2777 .WillByDefault(Return(Technology::kWifi));
2778
2779 set<string> expected_technologies;
2780 expected_technologies.insert(Technology::NameFromIdentifier(
2781 Technology::kEthernet));
2782 expected_technologies.insert(Technology::NameFromIdentifier(
2783 Technology::kWifi));
2784 expected_technologies.insert(Technology::NameFromIdentifier(
2785 Technology::kCellular));
2786 Error error;
2787 vector<string> technologies = manager()->AvailableTechnologies(&error);
2788
2789 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2790 ContainerEq(expected_technologies));
2791}
2792
2793TEST_F(ManagerTest, ConnectedTechnologies) {
2794 scoped_refptr<MockService> connected_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> connected_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 scoped_refptr<MockService> disconnected_service1(
2805 new NiceMock<MockService>(control_interface(),
2806 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002807 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002808 manager()));
2809 scoped_refptr<MockService> disconnected_service2(
2810 new NiceMock<MockService>(control_interface(),
2811 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002812 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002813 manager()));
2814
2815 ON_CALL(*connected_service1.get(), IsConnected())
2816 .WillByDefault(Return(true));
2817 ON_CALL(*connected_service2.get(), IsConnected())
2818 .WillByDefault(Return(true));
2819
2820 manager()->RegisterService(connected_service1);
2821 manager()->RegisterService(connected_service2);
2822 manager()->RegisterService(disconnected_service1);
2823 manager()->RegisterService(disconnected_service2);
2824
2825 manager()->RegisterDevice(mock_devices_[0]);
2826 manager()->RegisterDevice(mock_devices_[1]);
2827 manager()->RegisterDevice(mock_devices_[2]);
2828 manager()->RegisterDevice(mock_devices_[3]);
2829
2830 ON_CALL(*mock_devices_[0].get(), technology())
2831 .WillByDefault(Return(Technology::kEthernet));
2832 ON_CALL(*mock_devices_[1].get(), technology())
2833 .WillByDefault(Return(Technology::kWifi));
2834 ON_CALL(*mock_devices_[2].get(), technology())
2835 .WillByDefault(Return(Technology::kCellular));
2836 ON_CALL(*mock_devices_[3].get(), technology())
2837 .WillByDefault(Return(Technology::kWifi));
2838
2839 mock_devices_[0]->SelectService(connected_service1);
2840 mock_devices_[1]->SelectService(disconnected_service1);
2841 mock_devices_[2]->SelectService(disconnected_service2);
2842 mock_devices_[3]->SelectService(connected_service2);
2843
2844 set<string> expected_technologies;
2845 expected_technologies.insert(Technology::NameFromIdentifier(
2846 Technology::kEthernet));
2847 expected_technologies.insert(Technology::NameFromIdentifier(
2848 Technology::kWifi));
2849 Error error;
2850
2851 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2852 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2853 ContainerEq(expected_technologies));
2854}
2855
2856TEST_F(ManagerTest, DefaultTechnology) {
2857 scoped_refptr<MockService> connected_service(
2858 new NiceMock<MockService>(control_interface(),
2859 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002860 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002861 manager()));
2862 scoped_refptr<MockService> disconnected_service(
2863 new NiceMock<MockService>(control_interface(),
2864 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002865 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002866 manager()));
2867
2868 // Connected. WiFi.
2869 ON_CALL(*connected_service.get(), IsConnected())
2870 .WillByDefault(Return(true));
2871 ON_CALL(*connected_service.get(), state())
2872 .WillByDefault(Return(Service::kStateConnected));
2873 ON_CALL(*connected_service.get(), technology())
2874 .WillByDefault(Return(Technology::kWifi));
2875
2876 // Disconnected. Ethernet.
2877 ON_CALL(*disconnected_service.get(), technology())
2878 .WillByDefault(Return(Technology::kEthernet));
2879
2880 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002881 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002882 Error error;
2883 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2884
2885
2886 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002887 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002888 // Connected service should be brought to the front now.
2889 string expected_technology =
2890 Technology::NameFromIdentifier(Technology::kWifi);
2891 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2892}
2893
Paul Stewart212d60f2012-07-12 10:59:13 -07002894TEST_F(ManagerTest, Stop) {
2895 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002896 new NiceMock<MockProfile>(
2897 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002898 AdoptProfile(manager(), profile);
2899 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002900 new NiceMock<MockService>(control_interface(),
2901 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002902 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002903 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002904 manager()->RegisterService(service);
2905 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002906 SetPowerManager();
2907 EXPECT_TRUE(manager()->power_manager());
Paul Stewart212d60f2012-07-12 10:59:13 -07002908 EXPECT_CALL(*profile.get(),
2909 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2910 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002911 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002912 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
Samuel Tan0d061192014-07-07 15:45:15 -07002913 EXPECT_CALL(*service.get(), Disconnect(_, StrEq("Stop"))).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002914 manager()->Stop();
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002915 EXPECT_FALSE(manager()->power_manager());
Thieu Le1271d682011-11-02 22:48:19 +00002916}
2917
mukesh agrawal00917ce2011-11-22 23:56:55 +00002918TEST_F(ManagerTest, UpdateServiceConnected) {
2919 scoped_refptr<MockService> mock_service(
2920 new NiceMock<MockService>(control_interface(),
2921 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002922 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002923 manager()));
2924 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002925 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002926 EXPECT_FALSE(mock_service->auto_connect());
2927
Gaurav Shah435de2c2011-11-17 19:01:07 -08002928 EXPECT_CALL(*mock_service.get(), IsConnected())
2929 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002930 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002931 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2932 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2933 // would break the SortServices test. (crbug.com/206367)
2934 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002935 EXPECT_TRUE(mock_service->auto_connect());
2936}
2937
Paul Stewart2da34c02013-10-17 15:28:56 -07002938TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002939 // This tests the case where the user connects to a service that is
2940 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002941 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002942 scoped_refptr<MockService> mock_service(
2943 new NiceMock<MockService>(control_interface(),
2944 dispatcher(),
2945 metrics(),
2946 manager()));
2947 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002948 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002949 EXPECT_FALSE(mock_service->auto_connect());
2950
Gary Moraind93615e2012-04-27 11:50:03 -07002951 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002952 new MockProfile(
2953 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002954
Gary Moraind93615e2012-04-27 11:50:03 -07002955 mock_service->set_profile(profile);
2956 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002957 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002958 EXPECT_CALL(*profile,
2959 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002960 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002961 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2962 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2963 // would break the SortServices test. (crbug.com/206367)
2964 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002965 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002966 // This releases the ref on the mock profile.
Ben Chancc225ef2014-09-30 13:26:51 -07002967 mock_service->set_profile(nullptr);
Thieu Led4e9e552012-02-16 16:26:07 -08002968}
2969
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002970TEST_F(ManagerTest, SaveSuccessfulService) {
2971 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002972 new StrictMock<MockProfile>(
2973 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002974 AdoptProfile(manager(), profile);
2975 scoped_refptr<MockService> service(
2976 new NiceMock<MockService>(control_interface(),
2977 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002978 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002979 manager()));
2980
2981 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2982 ServiceRefPtr expect_service(service.get());
2983
2984 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2985 .WillOnce(Return(false));
2986 manager()->RegisterService(service);
2987
2988 EXPECT_CALL(*service.get(), state())
2989 .WillRepeatedly(Return(Service::kStateConnected));
2990 EXPECT_CALL(*service.get(), IsConnected())
2991 .WillRepeatedly(Return(true));
2992 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2993 .WillOnce(Return(true));
2994 manager()->UpdateService(service);
2995}
2996
Darin Petkove7c6ad32012-06-29 10:22:09 +02002997TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002998 MockProfile *profile0 =
2999 new MockProfile(control_interface(), metrics(), manager(), "");
3000 MockProfile *profile1 =
3001 new MockProfile(control_interface(), metrics(), manager(), "");
3002 MockProfile *profile2 =
3003 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02003004 AdoptProfile(manager(), profile0); // Passes ownership.
3005 AdoptProfile(manager(), profile1); // Passes ownership.
3006 AdoptProfile(manager(), profile2); // Passes ownership.
3007 DeviceRefPtr device_ref(mock_devices_[0].get());
3008 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
3009 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
3010 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
3011 manager()->UpdateDevice(mock_devices_[0]);
3012}
3013
Paul Stewart1b253142012-01-26 14:05:52 -08003014TEST_F(ManagerTest, EnumerateProfiles) {
3015 vector<string> profile_paths;
3016 for (size_t i = 0; i < 10; i++) {
3017 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003018 new StrictMock<MockProfile>(
3019 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05003020 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08003021 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
3022 .WillOnce(Return(profile_paths.back()));
3023 AdoptProfile(manager(), profile);
3024 }
3025
3026 Error error;
3027 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
3028 EXPECT_TRUE(error.IsSuccess());
3029 EXPECT_EQ(profile_paths.size(), returned_paths.size());
3030 for (size_t i = 0; i < profile_paths.size(); i++) {
3031 EXPECT_EQ(profile_paths[i], returned_paths[i]);
3032 }
3033}
3034
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003035TEST_F(ManagerTest, AutoConnectOnRegister) {
3036 MockServiceRefPtr service = MakeAutoConnectableService();
3037 EXPECT_CALL(*service.get(), AutoConnect());
3038 manager()->RegisterService(service);
3039 dispatcher()->DispatchPendingEvents();
3040}
3041
3042TEST_F(ManagerTest, AutoConnectOnUpdate) {
3043 MockServiceRefPtr service1 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07003044 service1->SetPriority(1, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003045 MockServiceRefPtr service2 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07003046 service2->SetPriority(2, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003047 manager()->RegisterService(service1);
3048 manager()->RegisterService(service2);
3049 dispatcher()->DispatchPendingEvents();
3050
3051 EXPECT_CALL(*service1.get(), AutoConnect());
3052 EXPECT_CALL(*service2.get(), state())
3053 .WillRepeatedly(Return(Service::kStateFailure));
3054 EXPECT_CALL(*service2.get(), IsFailed())
3055 .WillRepeatedly(Return(true));
3056 EXPECT_CALL(*service2.get(), IsConnected())
3057 .WillRepeatedly(Return(false));
3058 manager()->UpdateService(service2);
3059 dispatcher()->DispatchPendingEvents();
3060}
3061
3062TEST_F(ManagerTest, AutoConnectOnDeregister) {
3063 MockServiceRefPtr service1 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07003064 service1->SetPriority(1, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003065 MockServiceRefPtr service2 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07003066 service2->SetPriority(2, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00003067 manager()->RegisterService(service1);
3068 manager()->RegisterService(service2);
3069 dispatcher()->DispatchPendingEvents();
3070
3071 EXPECT_CALL(*service1.get(), AutoConnect());
3072 manager()->DeregisterService(service2);
3073 dispatcher()->DispatchPendingEvents();
3074}
3075
Daniel Eratfac09532014-04-17 20:25:59 -07003076TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003077 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003078 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02003079 SetPowerManager();
3080 EXPECT_CALL(*service, AutoConnect()).Times(0);
3081 manager()->RegisterService(service);
3082 dispatcher()->DispatchPendingEvents();
3083}
3084
Daniel Eratfac09532014-04-17 20:25:59 -07003085TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02003086 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003087 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02003088 SetPowerManager();
3089 EXPECT_CALL(*service, AutoConnect());
3090 manager()->RegisterService(service);
3091 dispatcher()->DispatchPendingEvents();
3092}
3093
Paul Stewart63864b62012-11-07 15:10:55 -08003094TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
3095 SetRunning(false);
3096 MockServiceRefPtr service = MakeAutoConnectableService();
3097 EXPECT_CALL(*service, AutoConnect()).Times(0);
3098 manager()->RegisterService(service);
3099 dispatcher()->DispatchPendingEvents();
3100}
3101
Daniel Eratfac09532014-04-17 20:25:59 -07003102TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02003103 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02003104 SetPowerManager();
3105 EXPECT_CALL(*service, AutoConnect());
3106 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07003107 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003108 dispatcher()->DispatchPendingEvents();
3109
Samuel Tanfbe8d2b2014-09-15 20:23:59 -07003110 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend(_));
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003111 OnSuspendImminent();
Daniel Eratfac09532014-04-17 20:25:59 -07003112 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02003113 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003114 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003115
Daniel Eratfac09532014-04-17 20:25:59 -07003116 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003117 OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -07003118 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02003119 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003120 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003121}
3122
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07003123TEST_F(ManagerTest, AddTerminationAction) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003124 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3125 manager()->AddTerminationAction("action1", base::Closure());
3126 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3127 manager()->AddTerminationAction("action2", base::Closure());
3128}
3129
3130TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003131 const char kKey1[] = "action1";
3132 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02003133
Ben Chan16d3acb2014-05-13 22:08:14 -07003134 // Removing an action when the hook table is empty.
Darin Petkov3ec55342012-09-28 14:04:44 +02003135 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3136 manager()->RemoveTerminationAction("unknown");
Darin Petkov3ec55342012-09-28 14:04:44 +02003137
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07003138 // Fill hook table with two items.
Darin Petkov3ec55342012-09-28 14:04:44 +02003139 manager()->AddTerminationAction(kKey1, base::Closure());
3140 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3141 manager()->AddTerminationAction(kKey2, base::Closure());
3142
Ben Chan16d3acb2014-05-13 22:08:14 -07003143 // Removing an action that ends up with a non-empty hook table.
Darin Petkov3ec55342012-09-28 14:04:44 +02003144 manager()->RemoveTerminationAction(kKey1);
3145 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
Darin Petkov3ec55342012-09-28 14:04:44 +02003146
Ben Chan16d3acb2014-05-13 22:08:14 -07003147 // Removing the last action.
Darin Petkov3ec55342012-09-28 14:04:44 +02003148 manager()->RemoveTerminationAction(kKey2);
3149 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3150}
3151
3152TEST_F(ManagerTest, RunTerminationActions) {
3153 TerminationActionTest test_action;
3154 const string kActionName = "action";
3155
3156 EXPECT_CALL(test_action, Done(_));
3157 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3158 test_action.AsWeakPtr()));
3159
3160 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3161 Bind(&TerminationActionTest::Action,
3162 test_action.AsWeakPtr()));
3163 test_action.set_manager(manager());
3164 EXPECT_CALL(test_action, Done(_));
3165 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3166 test_action.AsWeakPtr()));
3167}
3168
Samuel Tanfbe8d2b2014-09-15 20:23:59 -07003169TEST_F(ManagerTest, OnSuspendImminentDevicesPresent) {
3170 EXPECT_CALL(*mock_devices_[0].get(), OnBeforeSuspend(_));
3171 EXPECT_CALL(*mock_devices_[1].get(), OnBeforeSuspend(_));
3172 EXPECT_CALL(*mock_devices_[2].get(), OnBeforeSuspend(_));
3173 manager()->RegisterDevice(mock_devices_[0]);
3174 manager()->RegisterDevice(mock_devices_[1]);
3175 manager()->RegisterDevice(mock_devices_[2]);
3176 SetPowerManager();
3177 OnSuspendImminent();
3178}
3179
3180TEST_F(ManagerTest, OnSuspendImminentNoDevicesPresent) {
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003181 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003182 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003183 OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +02003184}
3185
3186TEST_F(ManagerTest, OnSuspendActionsComplete) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003187 Error error;
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003188 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003189 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003190 OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003191}
3192
Paul Stewartc681fa02012-03-02 19:40:04 -08003193TEST_F(ManagerTest, RecheckPortal) {
3194 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3195 .WillOnce(Return(false));
3196 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3197 .WillOnce(Return(true));
3198 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3199 .Times(0);
3200
3201 manager()->RegisterDevice(mock_devices_[0]);
3202 manager()->RegisterDevice(mock_devices_[1]);
3203 manager()->RegisterDevice(mock_devices_[2]);
3204
Ben Chancc225ef2014-09-30 13:26:51 -07003205 manager()->RecheckPortal(nullptr);
Paul Stewartc681fa02012-03-02 19:40:04 -08003206}
3207
Paul Stewartd215af62012-04-24 23:25:50 -07003208TEST_F(ManagerTest, RecheckPortalOnService) {
3209 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3210 dispatcher(),
3211 metrics(),
3212 manager());
3213 EXPECT_CALL(*mock_devices_[0].get(),
3214 IsConnectedToService(IsRefPtrTo(service)))
3215 .WillOnce(Return(false));
3216 EXPECT_CALL(*mock_devices_[1].get(),
3217 IsConnectedToService(IsRefPtrTo(service)))
3218 .WillOnce(Return(true));
3219 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3220 .WillOnce(Return(true));
3221 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3222 .Times(0);
3223
3224 manager()->RegisterDevice(mock_devices_[0]);
3225 manager()->RegisterDevice(mock_devices_[1]);
3226 manager()->RegisterDevice(mock_devices_[2]);
3227
3228 manager()->RecheckPortalOnService(service);
3229}
3230
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003231TEST_F(ManagerTest, GetDefaultService) {
3232 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003233 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003234
3235 scoped_refptr<MockService> mock_service(
3236 new NiceMock<MockService>(control_interface(),
3237 dispatcher(),
3238 metrics(),
3239 manager()));
3240
3241 manager()->RegisterService(mock_service);
3242 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003243 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003244
3245 scoped_refptr<MockConnection> mock_connection(
3246 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003247 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003248 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003249 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003250
Ben Chancc225ef2014-09-30 13:26:51 -07003251 mock_service->set_mock_connection(nullptr);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003252 manager()->DeregisterService(mock_service);
3253}
3254
Paul Stewart13ed2252012-03-21 12:52:46 -07003255TEST_F(ManagerTest, GetServiceWithGUID) {
3256 scoped_refptr<MockService> mock_service0(
3257 new NiceMock<MockService>(control_interface(),
3258 dispatcher(),
3259 metrics(),
3260 manager()));
3261
3262 scoped_refptr<MockService> mock_service1(
3263 new NiceMock<MockService>(control_interface(),
3264 dispatcher(),
3265 metrics(),
3266 manager()));
3267
Paul Stewartcb59fed2012-03-21 21:14:46 -07003268 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3269 .Times(0);
3270 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3271 .Times(0);
3272
Paul Stewart13ed2252012-03-21 12:52:46 -07003273 manager()->RegisterService(mock_service0);
3274 manager()->RegisterService(mock_service1);
3275
3276 const string kGUID0 = "GUID0";
3277 const string kGUID1 = "GUID1";
3278
3279 {
3280 Error error;
3281 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3282 EXPECT_FALSE(error.IsSuccess());
3283 EXPECT_FALSE(service);
3284 }
3285
3286 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003287 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003288
3289 {
3290 Error error;
3291 ServiceRefPtr service = manager()->GetService(args, &error);
3292 EXPECT_EQ(Error::kInvalidArguments, error.type());
3293 EXPECT_FALSE(service);
3294 }
3295
Ben Chancc225ef2014-09-30 13:26:51 -07003296 mock_service0->SetGuid(kGUID0, nullptr);
3297 mock_service1->SetGuid(kGUID1, nullptr);
Paul Stewart13ed2252012-03-21 12:52:46 -07003298
3299 {
3300 Error error;
3301 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3302 EXPECT_TRUE(error.IsSuccess());
3303 EXPECT_EQ(mock_service0.get(), service.get());
3304 }
3305
3306 {
3307 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003308 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3309 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003310 ServiceRefPtr service = manager()->GetService(args, &error);
3311 EXPECT_TRUE(error.IsSuccess());
3312 EXPECT_EQ(mock_service1.get(), service.get());
3313 }
3314
3315 manager()->DeregisterService(mock_service0);
3316 manager()->DeregisterService(mock_service1);
3317}
3318
Gary Morain028545d2012-04-07 14:55:52 -07003319
3320TEST_F(ManagerTest, CalculateStateOffline) {
Peter Qiu700de642014-07-14 16:31:30 -07003321 EXPECT_FALSE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003322 EXPECT_EQ("offline", manager()->CalculateState(nullptr));
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003323
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003324 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003325 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003326 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3327 .Times(AnyNumber());
3328 scoped_refptr<MockService> mock_service0(
3329 new NiceMock<MockService>(control_interface(),
3330 dispatcher(),
3331 metrics(),
3332 manager()));
3333
3334 scoped_refptr<MockService> mock_service1(
3335 new NiceMock<MockService>(control_interface(),
3336 dispatcher(),
3337 metrics(),
3338 manager()));
3339
3340 EXPECT_CALL(*mock_service0.get(), IsConnected())
3341 .WillRepeatedly(Return(false));
3342 EXPECT_CALL(*mock_service1.get(), IsConnected())
3343 .WillRepeatedly(Return(false));
3344
3345 manager()->RegisterService(mock_service0);
3346 manager()->RegisterService(mock_service1);
3347
Peter Qiu700de642014-07-14 16:31:30 -07003348 EXPECT_FALSE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003349 EXPECT_EQ("offline", manager()->CalculateState(nullptr));
Gary Morain028545d2012-04-07 14:55:52 -07003350
3351 manager()->DeregisterService(mock_service0);
3352 manager()->DeregisterService(mock_service1);
3353}
3354
3355TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003356 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003357 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003358 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3359 .Times(AnyNumber());
3360 scoped_refptr<MockService> mock_service0(
3361 new NiceMock<MockService>(control_interface(),
3362 dispatcher(),
3363 metrics(),
3364 manager()));
3365
3366 scoped_refptr<MockService> mock_service1(
3367 new NiceMock<MockService>(control_interface(),
3368 dispatcher(),
3369 metrics(),
3370 manager()));
3371
3372 EXPECT_CALL(*mock_service0.get(), IsConnected())
3373 .WillRepeatedly(Return(false));
3374 EXPECT_CALL(*mock_service1.get(), IsConnected())
3375 .WillRepeatedly(Return(true));
3376 EXPECT_CALL(*mock_service0.get(), state())
3377 .WillRepeatedly(Return(Service::kStateIdle));
3378 EXPECT_CALL(*mock_service1.get(), state())
3379 .WillRepeatedly(Return(Service::kStateConnected));
3380
3381 manager()->RegisterService(mock_service0);
3382 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003383 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003384
Peter Qiu700de642014-07-14 16:31:30 -07003385 EXPECT_TRUE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003386 EXPECT_EQ("online", manager()->CalculateState(nullptr));
Gary Morain028545d2012-04-07 14:55:52 -07003387
3388 manager()->DeregisterService(mock_service0);
3389 manager()->DeregisterService(mock_service1);
3390}
3391
Paul Stewart03e29f72013-09-26 00:49:48 -07003392TEST_F(ManagerTest, RefreshConnectionState) {
3393 EXPECT_CALL(*manager_adaptor_,
3394 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3395 RefreshConnectionState();
3396 Mock::VerifyAndClearExpectations(manager_adaptor_);
3397
3398 scoped_refptr<MockService> mock_service(
3399 new NiceMock<MockService>(control_interface(),
3400 dispatcher(),
3401 metrics(),
3402 manager()));
3403 EXPECT_CALL(*manager_adaptor_,
3404 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3405 manager()->RegisterService(mock_service);
3406 RefreshConnectionState();
3407
3408 scoped_refptr<MockConnection> mock_connection(
3409 new NiceMock<MockConnection>(device_info_.get()));
3410 mock_service->set_mock_connection(mock_connection);
3411 EXPECT_CALL(*mock_service, state())
3412 .WillOnce(Return(Service::kStateIdle));
3413 RefreshConnectionState();
3414
3415 Mock::VerifyAndClearExpectations(manager_adaptor_);
3416 EXPECT_CALL(*mock_service, state())
3417 .WillOnce(Return(Service::kStatePortal));
3418 EXPECT_CALL(*manager_adaptor_,
3419 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3420 RefreshConnectionState();
3421 Mock::VerifyAndClearExpectations(manager_adaptor_);
3422
Ben Chancc225ef2014-09-30 13:26:51 -07003423 mock_service->set_mock_connection(nullptr);
Paul Stewart03e29f72013-09-26 00:49:48 -07003424 manager()->DeregisterService(mock_service);
3425}
3426
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003427TEST_F(ManagerTest, StartupPortalList) {
3428 // Simulate loading value from the default profile.
3429 const string kProfileValue("wifi,vpn");
3430 manager()->props_.check_portal_list = kProfileValue;
3431
Ben Chancc225ef2014-09-30 13:26:51 -07003432 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003433 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3434 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3435
3436 const string kStartupValue("cellular,ethernet");
3437 manager()->SetStartupPortalList(kStartupValue);
3438 // Ensure profile value is not overwritten, so when we save the default
3439 // profile, the correct value will still be written.
3440 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3441
3442 // However we should read back a different list.
Ben Chancc225ef2014-09-30 13:26:51 -07003443 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003444 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3445 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3446
3447 const string kRuntimeValue("ppp");
3448 // Setting a runtime value over the control API should overwrite both
3449 // the profile value and what we read back.
3450 Error error;
3451 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003452 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003453 kRuntimeValue,
3454 &error);
3455 ASSERT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07003456 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003457 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3458 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3459 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3460}
3461
Paul Stewart036dba02012-08-07 12:34:41 -07003462TEST_F(ManagerTest, LinkMonitorEnabled) {
3463 const string kEnabledTechnologies("wifi,vpn");
3464 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3465 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3466 EXPECT_FALSE(
3467 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3468}
3469
Ben Chan8e6b8ef2014-07-14 21:50:18 -07003470TEST_F(ManagerTest, IsTechnologyAutoConnectDisabled) {
3471 const string kNoAutoConnectTechnologies("wifi,cellular");
3472 manager()->props_.no_auto_connect_technologies = kNoAutoConnectTechnologies;
3473 EXPECT_TRUE(manager()->IsTechnologyAutoConnectDisabled(Technology::kWifi));
3474 EXPECT_TRUE(
3475 manager()->IsTechnologyAutoConnectDisabled(Technology::kCellular));
3476 EXPECT_FALSE(
3477 manager()->IsTechnologyAutoConnectDisabled(Technology::kEthernet));
3478}
3479
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003480TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003481 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003482 DisableTechnologyReplyHandler disable_technology_reply_handler;
3483 ResultCallback disable_technology_callback(
3484 Bind(&DisableTechnologyReplyHandler::ReportResult,
3485 disable_technology_reply_handler.AsWeakPtr()));
3486 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3487
Ben Chan923a5022013-09-20 11:23:23 -07003488 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003489 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003490 EXPECT_TRUE(error.IsSuccess());
3491
Joshua Krollda798622012-06-05 12:30:48 -07003492 ON_CALL(*mock_devices_[0], technology())
3493 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003494 ON_CALL(*mock_devices_[1], technology())
3495 .WillByDefault(Return(Technology::kCellular));
3496 ON_CALL(*mock_devices_[2], technology())
3497 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003498
3499 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003500 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003501
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003502 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003503 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3504 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003505 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003506 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003507 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003508 EXPECT_TRUE(error.IsSuccess());
3509
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003510 // Ethernet Device is enabled, and mock doesn't change error from
3511 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003512 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3513 mock_devices_[0]->enabled_ = true;
3514 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003515 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003516 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003517 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003518
3519 // Ethernet Device is disabled, and mock doesn't change error from
3520 // kOperationInitiated, so expect enable to say operation in progress.
3521 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3522 mock_devices_[0]->enabled_ = false;
3523 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003524 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003525 &error, disable_technology_callback);
3526 EXPECT_TRUE(error.IsOngoing());
3527
3528 // Cellular Device is enabled, but disable failed.
3529 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3530 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3531 mock_devices_[1]->enabled_ = true;
3532 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003533 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003534 &error, disable_technology_callback);
3535 EXPECT_EQ(Error::kPermissionDenied, error.type());
3536
3537 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3538 // if one is in progress (even if the other completed immediately).
3539 manager()->RegisterDevice(mock_devices_[2]);
3540 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3541 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3542 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3543 mock_devices_[1]->enabled_ = true;
3544 mock_devices_[2]->enabled_ = true;
3545 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003546 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003547 &error, disable_technology_callback);
3548 EXPECT_TRUE(error.IsOngoing());
3549
3550 // ...and order doesn't matter.
3551 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3552 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3553 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3554 mock_devices_[1]->enabled_ = true;
3555 mock_devices_[2]->enabled_ = true;
3556 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003557 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003558 &error, disable_technology_callback);
3559 EXPECT_TRUE(error.IsOngoing());
3560 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3561
3562 // Multiple Cellular Devices in enabled state. Even if all disable
3563 // operations complete asynchronously, we only get one call to the
3564 // DisableTechnologyReplyHandler::ReportResult.
3565 ResultCallback device1_result_callback;
3566 ResultCallback device2_result_callback;
3567 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3568 .WillOnce(SaveArg<2>(&device1_result_callback));
3569 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3570 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3571 SaveArg<2>(&device2_result_callback)));
3572 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3573 mock_devices_[1]->enabled_ = true;
3574 mock_devices_[2]->enabled_ = true;
3575 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003576 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003577 &error, disable_technology_callback);
3578 EXPECT_TRUE(error.IsOngoing());
3579 device1_result_callback.Run(Error(Error::kSuccess));
3580 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003581}
3582
Paul Stewart4d5efb72012-09-17 12:24:34 -07003583TEST_F(ManagerTest, IgnoredSearchList) {
3584 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003585 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003586 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003587
3588 const string kIgnored0 = "chromium.org";
3589 ignored_paths.push_back(kIgnored0);
3590 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003591 SetIgnoredDNSSearchPaths(kIgnored0, nullptr);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003592 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3593
3594 const string kIgnored1 = "google.com";
3595 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3596 ignored_paths.push_back(kIgnored1);
3597 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003598 SetIgnoredDNSSearchPaths(kIgnoredSum, nullptr);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003599 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3600
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003601 ignored_paths.clear();
3602 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003603 SetIgnoredDNSSearchPaths("", nullptr);
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003604 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3605
Paul Stewart4d5efb72012-09-17 12:24:34 -07003606 SetResolver(Resolver::GetInstance());
3607}
3608
Paul Stewartbfb82552012-10-24 16:48:48 -07003609TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3610 // Test to make sure that every service state-change causes the
3611 // Manager to emit a new service list.
3612 scoped_refptr<MockService> mock_service(
3613 new NiceMock<MockService>(control_interface(),
3614 dispatcher(),
3615 metrics(),
3616 manager()));
3617 EXPECT_CALL(*mock_service, state())
3618 .WillRepeatedly(Return(Service::kStateIdle));
3619
3620 manager()->RegisterService(mock_service);
3621 EXPECT_CALL(
3622 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003623 kServiceCompleteListProperty, _)).Times(1);
3624 EXPECT_CALL(
3625 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003626 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003627 EXPECT_CALL(
3628 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003629 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003630 CompleteServiceSort();
3631
3632 Mock::VerifyAndClearExpectations(manager_adaptor_);
3633 EXPECT_CALL(
3634 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003635 kServiceCompleteListProperty, _)).Times(1);
3636 EXPECT_CALL(
3637 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003638 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003639 EXPECT_CALL(
3640 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003641 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003642 manager()->UpdateService(mock_service.get());
3643 CompleteServiceSort();
3644
3645 manager()->DeregisterService(mock_service);
3646}
3647
3648TEST_F(ManagerTest, EnumerateServices) {
3649 scoped_refptr<MockService> mock_service(
3650 new NiceMock<MockService>(control_interface(),
3651 dispatcher(),
3652 metrics(),
3653 manager()));
3654 manager()->RegisterService(mock_service);
3655
3656 EXPECT_CALL(*mock_service, state())
3657 .WillRepeatedly(Return(Service::kStateConnected));
3658 EXPECT_CALL(*mock_service, IsVisible())
3659 .WillRepeatedly(Return(false));
3660 EXPECT_TRUE(EnumerateAvailableServices().empty());
3661 EXPECT_TRUE(EnumerateWatchedServices().empty());
3662
3663 EXPECT_CALL(*mock_service, state())
3664 .WillRepeatedly(Return(Service::kStateIdle));
3665 EXPECT_TRUE(EnumerateAvailableServices().empty());
3666 EXPECT_TRUE(EnumerateWatchedServices().empty());
3667
3668 EXPECT_CALL(*mock_service, IsVisible())
3669 .WillRepeatedly(Return(true));
3670 Service::ConnectState unwatched_states[] = {
3671 Service::kStateUnknown,
3672 Service::kStateIdle,
3673 Service::kStateFailure
3674 };
3675 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3676 EXPECT_CALL(*mock_service, state())
3677 .WillRepeatedly(Return(unwatched_states[i]));
3678 EXPECT_FALSE(EnumerateAvailableServices().empty());
3679 EXPECT_TRUE(EnumerateWatchedServices().empty());
3680 }
3681
3682 Service::ConnectState watched_states[] = {
3683 Service::kStateAssociating,
3684 Service::kStateConfiguring,
3685 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003686 Service::kStatePortal,
3687 Service::kStateOnline
3688 };
3689 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3690 EXPECT_CALL(*mock_service, state())
3691 .WillRepeatedly(Return(watched_states[i]));
3692 EXPECT_FALSE(EnumerateAvailableServices().empty());
3693 EXPECT_FALSE(EnumerateWatchedServices().empty());
3694 }
3695
3696 manager()->DeregisterService(mock_service);
3697}
3698
Paul Stewart39db5ca2013-03-18 14:15:17 -07003699TEST_F(ManagerTest, ConnectToBestServices) {
3700 scoped_refptr<MockService> wifi_service0(
3701 new NiceMock<MockService>(control_interface(),
3702 dispatcher(),
3703 metrics(),
3704 manager()));
3705 EXPECT_CALL(*wifi_service0.get(), state())
3706 .WillRepeatedly(Return(Service::kStateIdle));
3707 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3708 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003709 wifi_service0->SetConnectable(true);
3710 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003711 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3712 EXPECT_CALL(*wifi_service0.get(), technology())
3713 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003714 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3715 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003716 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3717 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003718
3719 scoped_refptr<MockService> wifi_service1(
3720 new NiceMock<MockService>(control_interface(),
3721 dispatcher(),
3722 metrics(),
3723 manager()));
3724 EXPECT_CALL(*wifi_service1.get(), state())
3725 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003726 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3727 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003728 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3729 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003730 wifi_service1->SetAutoConnect(true);
3731 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003732 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3733 EXPECT_CALL(*wifi_service1.get(), technology())
3734 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003735 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3736 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003737
3738 scoped_refptr<MockService> wifi_service2(
3739 new NiceMock<MockService>(control_interface(),
3740 dispatcher(),
3741 metrics(),
3742 manager()));
3743 EXPECT_CALL(*wifi_service2.get(), state())
3744 .WillRepeatedly(Return(Service::kStateConnected));
3745 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3746 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003747 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3748 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003749 wifi_service2->SetAutoConnect(true);
3750 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003751 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3752 EXPECT_CALL(*wifi_service2.get(), technology())
3753 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003754 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3755 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003756
3757 manager()->RegisterService(wifi_service0);
3758 manager()->RegisterService(wifi_service1);
3759 manager()->RegisterService(wifi_service2);
3760
3761 CompleteServiceSort();
3762 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3763
3764 scoped_refptr<MockService> cell_service(
3765 new NiceMock<MockService>(control_interface(),
3766 dispatcher(),
3767 metrics(),
3768 manager()));
3769
3770 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003771 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003772 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003773 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003774 EXPECT_CALL(*cell_service.get(), IsVisible())
3775 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003776 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003777 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003778 EXPECT_CALL(*cell_service.get(), technology())
3779 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003780 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3781 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003782 manager()->RegisterService(cell_service);
3783
Arman Uguray6fe4f262013-08-02 20:21:55 -07003784 scoped_refptr<MockService> wimax_service(
3785 new NiceMock<MockService>(control_interface(),
3786 dispatcher(),
3787 metrics(),
3788 manager()));
3789
3790 EXPECT_CALL(*wimax_service.get(), state())
3791 .WillRepeatedly(Return(Service::kStateConnected));
3792 EXPECT_CALL(*wimax_service.get(), IsConnected())
3793 .WillRepeatedly(Return(true));
3794 EXPECT_CALL(*wimax_service.get(), IsVisible())
3795 .WillRepeatedly(Return(true));
3796 wimax_service->SetAutoConnect(true);
3797 wimax_service->SetConnectable(true);
3798 EXPECT_CALL(*wimax_service.get(), technology())
3799 .WillRepeatedly(Return(Technology::kWiMax));
3800 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3801 .WillRepeatedly(Return(false));
3802 manager()->RegisterService(wimax_service);
3803
Paul Stewart39db5ca2013-03-18 14:15:17 -07003804 scoped_refptr<MockService> vpn_service(
3805 new NiceMock<MockService>(control_interface(),
3806 dispatcher(),
3807 metrics(),
3808 manager()));
3809
3810 EXPECT_CALL(*vpn_service.get(), state())
3811 .WillRepeatedly(Return(Service::kStateIdle));
3812 EXPECT_CALL(*vpn_service.get(), IsConnected())
3813 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003814 EXPECT_CALL(*vpn_service.get(), IsVisible())
3815 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003816 wifi_service2->SetAutoConnect(false);
3817 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003818 EXPECT_CALL(*vpn_service.get(), technology())
3819 .WillRepeatedly(Return(Technology::kVPN));
3820 manager()->RegisterService(vpn_service);
3821
3822 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003823 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003824
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003825 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3826 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003827 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003828 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3829 .Times(0); // Explicitly disconnected.
3830 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003831 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003832
Ben Chancc225ef2014-09-30 13:26:51 -07003833 manager()->ConnectToBestServices(nullptr);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003834 dispatcher()->DispatchPendingEvents();
3835
3836 // After this operation, since the Connect calls above are mocked and
3837 // no actual state changes have occurred, we should expect that the
3838 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003839 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003840}
3841
Rebecca Silberstein6862b382014-09-11 08:24:51 -07003842TEST_F(ManagerTest, CreateConnectivityReport) {
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -07003843 // Add devices
3844 // WiFi
3845 auto wifi_device = make_scoped_refptr(
3846 new NiceMock<MockDevice>(control_interface(),
3847 dispatcher(),
3848 metrics(),
3849 manager(),
3850 "null",
3851 "addr",
3852 0));
3853 manager()->RegisterDevice(wifi_device);
3854 // Cell
3855 auto cell_device = make_scoped_refptr(
3856 new NiceMock<MockDevice>(control_interface(),
3857 dispatcher(),
3858 metrics(),
3859 manager(),
3860 "null",
3861 "addr",
3862 1));
3863 manager()->RegisterDevice(cell_device);
3864 // WiMax
3865 auto wimax_device = make_scoped_refptr(
3866 new NiceMock<MockDevice>(control_interface(),
3867 dispatcher(),
3868 metrics(),
3869 manager(),
3870 "null",
3871 "addr",
3872 2));
3873 manager()->RegisterDevice(wimax_device);
3874 // Ethernet
3875 auto eth_device = make_scoped_refptr(
3876 new NiceMock<MockDevice>(control_interface(),
3877 dispatcher(),
3878 metrics(),
3879 manager(),
3880 "null",
3881 "addr",
3882 3));
3883 manager()->RegisterDevice(eth_device);
3884 // VPN Device -- base device for a service that will not be connected
3885 auto vpn_device = make_scoped_refptr(
3886 new NiceMock<MockDevice>(control_interface(),
3887 dispatcher(),
3888 metrics(),
3889 manager(),
3890 "null",
3891 "addr",
3892 4));
3893 manager()->RegisterDevice(vpn_device);
3894
3895 // Add service for multiple devices
3896 // WiFi
3897 MockServiceRefPtr wifi_service =
3898 new NiceMock<MockService>(control_interface(),
3899 dispatcher(),
3900 metrics(),
3901 manager());
3902 manager()->RegisterService(wifi_service);
3903 EXPECT_CALL(*wifi_service.get(), state())
3904 .WillRepeatedly(Return(Service::kStateConnected));
3905 EXPECT_CALL(*wifi_service.get(), IsConnected())
3906 .WillRepeatedly(Return(true));
3907 EXPECT_CALL(*wifi_device.get(),
3908 IsConnectedToService(_)).WillRepeatedly(Return(false));
3909 EXPECT_CALL(*wifi_device.get(),
3910 IsConnectedToService(IsRefPtrTo(wifi_service)))
3911 .WillRepeatedly(Return(true));
3912
3913 // Cell
3914 MockServiceRefPtr cell_service =
3915 new NiceMock<MockService>(control_interface(),
3916 dispatcher(),
3917 metrics(),
3918 manager());
3919 manager()->RegisterService(cell_service);
3920 EXPECT_CALL(*cell_service.get(), state())
3921 .WillRepeatedly(Return(Service::kStateConnected));
3922 EXPECT_CALL(*cell_service.get(), IsConnected())
3923 .WillRepeatedly(Return(true));
3924 EXPECT_CALL(*cell_device.get(),
3925 IsConnectedToService(_)).WillRepeatedly(Return(false));
3926 EXPECT_CALL(*cell_device.get(),
3927 IsConnectedToService(IsRefPtrTo(cell_service)))
3928 .WillRepeatedly(Return(true));
3929
3930 // WiMax
3931 MockServiceRefPtr wimax_service =
3932 new NiceMock<MockService>(control_interface(),
3933 dispatcher(),
3934 metrics(),
3935 manager());
3936 manager()->RegisterService(wimax_service);
3937 EXPECT_CALL(*wimax_service.get(), state())
3938 .WillRepeatedly(Return(Service::kStateConnected));
3939 EXPECT_CALL(*wimax_service.get(), IsConnected())
3940 .WillRepeatedly(Return(true));
3941
3942 EXPECT_CALL(*wimax_device.get(),
3943 IsConnectedToService(_)).WillRepeatedly(Return(false));
3944 EXPECT_CALL(*wimax_device.get(),
3945 IsConnectedToService(IsRefPtrTo(wimax_service)))
3946 .WillRepeatedly(Return(true));
3947
3948 // Ethernet
3949 MockServiceRefPtr eth_service =
3950 new NiceMock<MockService>(control_interface(),
3951 dispatcher(),
3952 metrics(),
3953 manager());
3954 manager()->RegisterService(eth_service);
3955 EXPECT_CALL(*eth_service.get(), state())
3956 .WillRepeatedly(Return(Service::kStateConnected));
3957 EXPECT_CALL(*eth_service.get(), IsConnected())
3958 .WillRepeatedly(Return(true));
3959 EXPECT_CALL(*eth_device.get(),
3960 IsConnectedToService(_)).WillRepeatedly(Return(false));
3961 EXPECT_CALL(*eth_device.get(),
3962 IsConnectedToService(IsRefPtrTo(eth_service)))
3963 .WillRepeatedly(Return(true));
3964
3965 // VPN: Service exists but is not connected and will not trigger a
3966 // connectivity report.
3967 MockServiceRefPtr vpn_service =
3968 new NiceMock<MockService>(control_interface(),
3969 dispatcher(),
3970 metrics(),
3971 manager());
3972 manager()->RegisterService(vpn_service);
3973 EXPECT_CALL(*vpn_service.get(), state())
3974 .WillRepeatedly(Return(Service::kStateIdle));
3975 EXPECT_CALL(*vpn_service.get(), IsConnected())
3976 .WillRepeatedly(Return(false));
3977
3978 EXPECT_CALL(*wifi_device.get(), StartConnectivityTest())
3979 .WillOnce(Return(true));
3980 EXPECT_CALL(*cell_device.get(), StartConnectivityTest())
3981 .WillOnce(Return(true));
3982 EXPECT_CALL(*wimax_device.get(), StartConnectivityTest())
3983 .WillOnce(Return(true));
3984 EXPECT_CALL(*eth_device.get(), StartConnectivityTest())
3985 .WillOnce(Return(true));
3986 EXPECT_CALL(*vpn_device.get(), StartConnectivityTest()).Times(0);
Ben Chancc225ef2014-09-30 13:26:51 -07003987 manager()->CreateConnectivityReport(nullptr);
Rebecca Silberstein6862b382014-09-11 08:24:51 -07003988 dispatcher()->DispatchPendingEvents();
3989}
3990
Christopher Wiley83889862013-05-02 15:55:09 -07003991TEST_F(ManagerTest, VerifyWhenNotConnected) {
3992 const string kFakeCertificate("fake cert");
3993 const string kFakePublicKey("fake public key");
3994 const string kFakeNonce("fake public key");
3995 const string kFakeSignedData("fake signed data");
3996 const string kFakeUdn("fake udn");
3997 const vector<uint8_t> kSSID(10, 87);
3998 const string kConfiguredSSID("AConfiguredDestination");
3999 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
4000 kConfiguredSSID.end());
4001 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
4002 scoped_refptr<MockWiFiService> mock_destination(
4003 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
4004 metrics(), manager(), wifi_provider_,
4005 kSSID, "", "none", false));
4006 // Register this service, but don't mark it as connected.
4007 manager()->RegisterService(mock_destination);
4008 // Verify that if we're not connected to anything, verification fails.
4009 {
4010 LOG(INFO) << "Can't verify if not connected.";
4011 EXPECT_CALL(*crypto_util_proxy_,
4012 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
4013 Error error(Error::kOperationInitiated);
4014 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
4015 kFakeSignedData, kFakeUdn, "", "",
4016 ResultBoolCallback(), &error);
4017 EXPECT_TRUE(error.IsFailure());
4018 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4019 }
4020 {
4021 // However, if the destination is already configured, we might be
4022 // connected to it via something other than WiFi, and we shouldn't
4023 // enforce the WiFi check.
4024 EXPECT_CALL(*crypto_util_proxy_,
4025 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
4026 kFakeSignedData, kFakeUdn,
4027 kConfiguredSSIDVector, kConfiguredBSSID,
4028 _, _)).Times(1).WillOnce(Return(true));
4029 Error error(Error::kOperationInitiated);
4030 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
4031 kFakeSignedData, kFakeUdn, kConfiguredSSID,
4032 kConfiguredBSSID, ResultBoolCallback(),
4033 &error);
4034 EXPECT_FALSE(error.IsFailure());
4035 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4036 }
4037}
4038
Christopher Wiley1057cd72013-02-28 15:21:29 -08004039TEST_F(ManagerTest, VerifyDestination) {
4040 const string kFakeCertificate("fake cert");
4041 const string kFakePublicKey("fake public key");
4042 const string kFakeNonce("fake public key");
4043 const string kFakeSignedData("fake signed data");
4044 const string kFakeUdn("fake udn");
4045 const char kSSIDStr[] = "fake ssid";
4046 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07004047 const string kConfiguredSSID("AConfiguredDestination");
4048 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
4049 kConfiguredSSID.end());
4050 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08004051 const string kFakeData("muffin man");
4052 scoped_refptr<MockWiFiService> mock_destination(
4053 new NiceMock<MockWiFiService>(control_interface(),
4054 dispatcher(),
4055 metrics(),
4056 manager(),
4057 wifi_provider_,
4058 kSSID,
4059 "",
4060 "none",
4061 false));
4062 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08004063 // Making the service look online will let service lookup in
4064 // VerifyDestinatoin succeed.
4065 EXPECT_CALL(*mock_destination.get(), IsConnected())
4066 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07004067 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08004068
4069 // Lead off by verifying that the basic VerifyDestination flow works.
4070 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07004071 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08004072 ResultBoolCallback passed_down_callback;
4073 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
4074 kFakePublicKey,
4075 kFakeNonce,
4076 kFakeSignedData,
4077 kFakeUdn,
4078 kSSID,
4079 _,
4080 _,
4081 _))
4082 .Times(1)
4083 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
4084 // Ask the manager to verify the current destination. This should look
4085 // up our previously registered service, and pass some metadata about
4086 // that service down to the CryptoUtilProxy to verify.
4087 Error error(Error::kOperationInitiated);
4088 ResultBoolCallback cb = Bind(
4089 &DestinationVerificationTest::ResultBoolCallbackStub,
4090 dv_test.AsWeakPtr());
4091 manager()->VerifyDestination(kFakeCertificate,
4092 kFakePublicKey,
4093 kFakeNonce,
4094 kFakeSignedData,
4095 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004096 // Ask to be verified against that service.
4097 "", "",
4098 cb,
4099 &error);
4100 // We assert here, because if the operation is not ongoing, it is
4101 // inconsistent with shim behavior to call the callback anyway.
4102 ASSERT_TRUE(error.IsOngoing());
4103 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4104 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
4105 // Call the callback passed into the CryptoUtilProxy, which
4106 // should find its way into the callback passed into the manager.
4107 // In real code, that callback passed into the manager is from the
4108 // DBus adaptor.
4109 Error e;
4110 passed_down_callback.Run(e, true);
4111 Mock::VerifyAndClearExpectations(&dv_test);
4112 }
4113
Christopher Wiley1057cd72013-02-28 15:21:29 -08004114 // Now for a slightly more complex variant. When we encrypt data,
4115 // we do the same verification step but monkey with the callback to
4116 // link ourselves to an encrypt step afterward.
4117 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07004118 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08004119 ResultBoolCallback passed_down_callback;
4120 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
4121 kFakePublicKey,
4122 kFakeNonce,
4123 kFakeSignedData,
4124 kFakeUdn,
4125 kSSID,
4126 _,
4127 _,
4128 _))
4129 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
4130
4131 Error error(Error::kOperationInitiated);
4132 ResultStringCallback cb = Bind(
4133 &DestinationVerificationTest::ResultStringCallbackStub,
4134 dv_test.AsWeakPtr());
4135 manager()->VerifyAndEncryptData(kFakeCertificate,
4136 kFakePublicKey,
4137 kFakeNonce,
4138 kFakeSignedData,
4139 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004140 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08004141 kFakeData,
4142 cb,
4143 &error);
4144 ASSERT_TRUE(error.IsOngoing());
4145 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4146 // Now, if we call that passed down callback, we should see encrypt being
4147 // called.
4148 ResultStringCallback second_passed_down_callback;
4149 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
4150 kFakeData,
4151 _,
4152 _))
4153 .Times(1)
4154 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
4155 Return(true)));
4156 Error e;
4157 passed_down_callback.Run(e, true);
4158 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4159 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
4160 // And if we call the second passed down callback, we should see the
4161 // original function we passed down to VerifyDestination getting called.
4162 e.Reset();
4163 second_passed_down_callback.Run(e, "");
4164 Mock::VerifyAndClearExpectations(&dv_test);
4165 }
4166
4167 // If verification fails on the way to trying to encrypt, we should ditch
4168 // without calling encrypt at all.
4169 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07004170 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08004171 ResultBoolCallback passed_down_callback;
4172 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
4173 kFakePublicKey,
4174 kFakeNonce,
4175 kFakeSignedData,
4176 kFakeUdn,
4177 kSSID,
4178 _,
4179 _,
4180 _))
4181 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
4182
4183 Error error(Error::kOperationInitiated);
4184 ResultStringCallback cb = Bind(
4185 &DestinationVerificationTest::ResultStringCallbackStub,
4186 dv_test.AsWeakPtr());
4187 manager()->VerifyAndEncryptData(kFakeCertificate,
4188 kFakePublicKey,
4189 kFakeNonce,
4190 kFakeSignedData,
4191 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004192 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08004193 kFakeData,
4194 cb,
4195 &error);
4196 ASSERT_TRUE(error.IsOngoing());
4197 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4198 Error e(Error::kOperationFailed);
4199 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
4200 // Although we're ditching, this callback is what cleans up the pending
4201 // DBus call.
4202 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
4203 passed_down_callback.Run(e, false);
4204 Mock::VerifyAndClearExpectations(&dv_test);
4205 }
4206}
4207
Paul Stewartd2e1c362013-03-03 19:06:07 -08004208TEST_F(ManagerTest, IsProfileBefore) {
4209 scoped_refptr<MockProfile> profile0(
4210 new NiceMock<MockProfile>(
4211 control_interface(), metrics(), manager(), ""));
4212 scoped_refptr<MockProfile> profile1(
4213 new NiceMock<MockProfile>(
4214 control_interface(), metrics(), manager(), ""));
4215
4216 AdoptProfile(manager(), profile0);
4217 AdoptProfile(manager(), profile1); // profile1 is after profile0.
4218 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
4219 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
4220
4221 // A few abnormal cases, but it's good to track their behavior.
4222 scoped_refptr<MockProfile> profile2(
4223 new NiceMock<MockProfile>(
4224 control_interface(), metrics(), manager(), ""));
4225 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
4226 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
4227 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
4228 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
4229}
4230
Paul Stewart967eaeb2013-04-25 19:53:07 -07004231TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
4232 MockStore storage0;
4233 MockStore storage1;
4234 MockStore storage2;
4235
4236 scoped_refptr<MockProfile> profile0(
4237 new NiceMock<MockProfile>(
4238 control_interface(), metrics(), manager(), ""));
4239 scoped_refptr<MockProfile> profile1(
4240 new NiceMock<MockProfile>(
4241 control_interface(), metrics(), manager(), ""));
4242 scoped_refptr<MockProfile> profile2(
4243 new NiceMock<MockProfile>(
4244 control_interface(), metrics(), manager(), ""));
4245
4246 AdoptProfile(manager(), profile0);
4247 AdoptProfile(manager(), profile1);
4248 AdoptProfile(manager(), profile2);
4249
4250 scoped_refptr<MockService> service(
4251 new NiceMock<MockService>(control_interface(),
4252 dispatcher(),
4253 metrics(),
4254 manager()));
4255
4256 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4257 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4258 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4259
4260 const string kEntry0("aluminum_crutch");
4261 const string kEntry2("rehashed_faces");
4262
4263 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4264 .WillOnce(Return(kEntry0));
4265 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4266 .WillOnce(Return(""));
4267 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4268 .WillOnce(Return(kEntry2));
4269
4270 const string kProfileRpc0("service_station");
4271 const string kProfileRpc2("crystal_tiaras");
4272
4273 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4274 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4275 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4276
4277 map<string, string> entries =
4278 manager()->GetLoadableProfileEntriesForService(service);
4279 EXPECT_EQ(2, entries.size());
4280 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4281 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4282 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4283 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4284}
4285
mukesh agrawal00752532013-05-03 15:46:55 -07004286TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004287 // We need a real glib here, so that profiles are persisted.
4288 GLib glib;
4289 ScopedTempDir temp_dir;
4290 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4291 Manager manager(control_interface(),
4292 dispatcher(),
4293 metrics(),
4294 &glib,
4295 run_path(),
4296 storage_path(),
4297 temp_dir.path().value());
4298 // Can't use |wifi_provider_|, because it's owned by the Manager
4299 // object in the fixture.
4300 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4301 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004302 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004303 // Give manager a valid place to write the user profile list.
4304 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4305
4306 // With no user profiles, the WiFiProvider should be called once
4307 // (for the default profile).
4308 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4309 manager.InitializeProfiles();
4310 Mock::VerifyAndClearExpectations(wifi_provider);
4311
4312 // With |n| user profiles, the WiFiProvider should be called |n+1|
4313 // times. First, create 2 user profiles...
4314 const char kProfile0[] = "~user/profile0";
4315 const char kProfile1[] = "~user/profile1";
4316 string profile_rpc_path;
4317 Error error;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004318 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawald142fd62013-05-01 16:50:57 -07004319 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4320 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4321 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4322 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4323
4324 // ... then reset manager state ...
4325 manager.profiles_.clear();
4326
4327 // ...then check that the WiFiProvider is notified about all three
4328 // profiles (one default, two user).
4329 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4330 manager.InitializeProfiles();
4331 Mock::VerifyAndClearExpectations(wifi_provider);
4332}
4333
mukesh agrawal00752532013-05-03 15:46:55 -07004334TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4335 // We need a real glib here, so that profiles are persisted.
4336 GLib glib;
4337 ScopedTempDir temp_dir;
4338 scoped_ptr<Manager> manager;
4339 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4340
4341 // Instantiate a Manager with empty persistent storage. Check that
4342 // defaults are set.
4343 //
4344 // Note that we use the same directory for default and user profiles.
4345 // This doesn't affect the test results, because we don't push a
4346 // user profile.
4347 manager.reset(new Manager(control_interface(),
4348 dispatcher(),
4349 metrics(),
4350 &glib,
4351 run_path(),
4352 temp_dir.path().value(),
4353 temp_dir.path().value()));
4354 manager->InitializeProfiles();
4355 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4356 manager->props_.check_portal_list);
4357 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4358 manager->props_.ignored_dns_search_paths);
4359 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4360 manager->props_.link_monitor_technologies);
Rebecca Silberstein3d49ea42014-08-21 11:20:50 -07004361 EXPECT_EQ(ConnectivityTrial::kDefaultURL,
mukesh agrawal00752532013-05-03 15:46:55 -07004362 manager->props_.portal_url);
4363 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4364 manager->props_.portal_check_interval_seconds);
4365
4366 // Change one of the settings.
4367 static const string kCustomCheckPortalList = "fiber0";
4368 Error error;
4369 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4370 manager->profiles_[0]->Save();
4371
4372 // Instantiate a new manager. It should have our settings for
4373 // check_portal_list, rather than the default.
4374 manager.reset(new Manager(control_interface(),
4375 dispatcher(),
4376 metrics(),
4377 &glib,
4378 run_path(),
4379 temp_dir.path().value(),
4380 temp_dir.path().value()));
4381 manager->InitializeProfiles();
4382 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4383
4384 // If we clear the persistent storage, we again get the default value.
4385 ASSERT_TRUE(temp_dir.Delete());
4386 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4387 manager.reset(new Manager(control_interface(),
4388 dispatcher(),
4389 metrics(),
4390 &glib,
4391 run_path(),
4392 temp_dir.path().value(),
4393 temp_dir.path().value()));
4394 manager->InitializeProfiles();
4395 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4396 manager->props_.check_portal_list);
4397}
4398
mukesh agrawalb94adde2013-08-22 18:17:26 -07004399TEST_F(ManagerTest, ProfileStackChangeLogging) {
4400 // We use a real glib here, since Manager and Profile don't provide an
4401 // easy way to mock out KeyFileStore.
4402 GLib glib;
4403 ScopedTempDir temp_dir;
4404 scoped_ptr<Manager> manager;
4405 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4406 manager.reset(new Manager(control_interface(),
4407 dispatcher(),
4408 metrics(),
4409 &glib,
4410 run_path(),
4411 temp_dir.path().value(),
4412 temp_dir.path().value()));
4413
4414 ScopedMockLog log;
4415 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4416 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4417 manager->InitializeProfiles();
4418
4419 const char kProfile0[] = "~user/profile0";
4420 const char kProfile1[] = "~user/profile1";
4421 const char kProfile2[] = "~user/profile2";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004422 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawalb94adde2013-08-22 18:17:26 -07004423 TestCreateProfile(manager.get(), kProfile0);
4424 TestCreateProfile(manager.get(), kProfile1);
4425 TestCreateProfile(manager.get(), kProfile2);
4426
4427 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4428 TestPushProfile(manager.get(), kProfile0);
4429
4430 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4431 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4432
4433 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4434 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4435
4436 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4437 TestPopProfile(manager.get(), kProfile2);
4438
4439 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4440 TestPopAnyProfile(manager.get());
4441
4442 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4443 TestPopAllUserProfiles(manager.get());
4444}
4445
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004446// Custom property setters should return false, and make no changes, if
4447// the new value is the same as the old value.
4448TEST_F(ManagerTest, CustomSetterNoopChange) {
4449 // SetCheckPortalList
4450 {
4451 static const string kCheckPortalList = "weird-device,weirder-device";
4452 Error error;
4453 // Set to known value.
4454 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4455 EXPECT_TRUE(error.IsSuccess());
4456 // Set to same value.
4457 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4458 EXPECT_TRUE(error.IsSuccess());
4459 }
4460
4461 // SetIgnoredDNSSearchPaths
4462 {
4463 NiceMock<MockResolver> resolver;
4464 static const string kIgnoredPaths = "example.com,example.org";
4465 Error error;
4466 SetResolver(&resolver);
4467 // Set to known value.
4468 EXPECT_CALL(resolver, set_ignored_search_list(_));
4469 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4470 EXPECT_TRUE(error.IsSuccess());
4471 Mock::VerifyAndClearExpectations(&resolver);
4472 // Set to same value.
4473 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4474 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4475 EXPECT_TRUE(error.IsSuccess());
4476 Mock::VerifyAndClearExpectations(&resolver);
4477 }
4478}
4479
Paul Stewart7de7e022013-08-28 09:42:50 -07004480TEST_F(ManagerTest, GeoLocation) {
4481 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4482
4483 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4484 dispatcher(),
4485 metrics(),
4486 manager(),
4487 "null",
4488 "addr",
4489 0));
4490
4491 // Manager should ignore gelocation info from technologies it does not know.
4492 EXPECT_CALL(*device, technology())
4493 .Times(AtLeast(1))
4494 .WillRepeatedly(Return(Technology::kEthernet));
4495 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4496 manager()->OnDeviceGeolocationInfoUpdated(device);
4497 Mock::VerifyAndClearExpectations(device);
4498 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4499
4500 // Manager should add WiFi geolocation info.
4501 EXPECT_CALL(*device, technology())
4502 .Times(AtLeast(1))
4503 .WillRepeatedly(Return(Technology::kWifi));
4504 EXPECT_CALL(*device, GetGeolocationObjects())
4505 .WillOnce(Return(vector<GeolocationInfo>()));
4506 manager()->OnDeviceGeolocationInfoUpdated(device);
4507 Mock::VerifyAndClearExpectations(device);
4508 auto location_infos = manager()->GetNetworksForGeolocation();
4509 EXPECT_EQ(1, location_infos.size());
4510 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4511
4512 // Manager should inclusively add cellular info.
4513 EXPECT_CALL(*device, technology())
4514 .Times(AtLeast(1))
4515 .WillRepeatedly(Return(Technology::kCellular));
4516 EXPECT_CALL(*device, GetGeolocationObjects())
4517 .WillOnce(Return(vector<GeolocationInfo>()));
4518 manager()->OnDeviceGeolocationInfoUpdated(device);
4519 location_infos = manager()->GetNetworksForGeolocation();
4520 EXPECT_EQ(2, location_infos.size());
4521 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4522 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4523}
4524
Peter Qiu574996a2014-04-04 10:55:47 -07004525TEST_F(ManagerTest, IsWifiIdle) {
4526 // No registered service.
4527 EXPECT_FALSE(manager()->IsWifiIdle());
4528
4529 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4530 dispatcher(),
4531 metrics(),
4532 manager()));
4533
4534 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4535 dispatcher(),
4536 metrics(),
4537 manager()));
4538
4539 manager()->RegisterService(wifi_service);
4540 manager()->RegisterService(cell_service);
4541
4542 EXPECT_CALL(*wifi_service.get(), technology())
4543 .WillRepeatedly(Return(Technology::kWifi));
4544 EXPECT_CALL(*cell_service.get(), technology())
4545 .WillRepeatedly(Return(Technology::kCellular));
4546
4547 // Cellular is connected.
4548 EXPECT_CALL(*cell_service.get(), IsConnected())
4549 .WillRepeatedly(Return(true));
4550 manager()->UpdateService(cell_service);
4551
4552 // No wifi connection attempt.
4553 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4554 .WillRepeatedly(Return(false));
4555 EXPECT_CALL(*wifi_service.get(), IsConnected())
4556 .WillRepeatedly(Return(false));
4557 manager()->UpdateService(wifi_service);
4558 EXPECT_TRUE(manager()->IsWifiIdle());
4559
4560 // Attempt wifi connection.
4561 Mock::VerifyAndClearExpectations(wifi_service);
4562 EXPECT_CALL(*wifi_service.get(), technology())
4563 .WillRepeatedly(Return(Technology::kWifi));
4564 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4565 .WillRepeatedly(Return(true));
4566 EXPECT_CALL(*wifi_service.get(), IsConnected())
4567 .WillRepeatedly(Return(false));
4568 manager()->UpdateService(wifi_service);
4569 EXPECT_FALSE(manager()->IsWifiIdle());
4570
4571 // wifi connected.
4572 Mock::VerifyAndClearExpectations(wifi_service);
4573 EXPECT_CALL(*wifi_service.get(), technology())
4574 .WillRepeatedly(Return(Technology::kWifi));
4575 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4576 .WillRepeatedly(Return(false));
4577 EXPECT_CALL(*wifi_service.get(), IsConnected())
4578 .WillRepeatedly(Return(true));
4579 manager()->UpdateService(wifi_service);
4580 EXPECT_FALSE(manager()->IsWifiIdle());
4581}
4582
Chris Masone9be4a9d2011-05-16 15:44:09 -07004583} // namespace shill