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