blob: 077f3e9a3061353a65cc93db3a2ac82a86757715 [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
Paul Stewarte73d05c2012-03-29 16:26:05 -070012#include <base/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"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070051#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020052#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070053
Christopher Wiley1057cd72013-02-28 15:21:29 -080054using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080055using base::FilePath;
Paul Stewart5ad16062013-02-21 18:10:48 -080056using base::ScopedTempDir;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070058using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070059using std::string;
60using std::vector;
61
Chris Masone9be4a9d2011-05-16 15:44:09 -070062namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070063using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070064using ::testing::AnyNumber;
Paul Stewart7de7e022013-08-28 09:42:50 -070065using ::testing::AtLeast;
Gaurav Shah435de2c2011-11-17 19:01:07 -080066using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070067using ::testing::DoAll;
mukesh agrawalb94adde2013-08-22 18:17:26 -070068using ::testing::HasSubstr;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070069using ::testing::InSequence;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070070using ::testing::Invoke;
mukesh agrawal784566d2012-08-08 18:32:58 -070071using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070072using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070073using ::testing::NiceMock;
Paul Stewart967eaeb2013-04-25 19:53:07 -070074using ::testing::Ref;
Chris Masone9be4a9d2011-05-16 15:44:09 -070075using ::testing::Return;
Ben Chana55469d2014-01-27 16:35:29 -080076using ::testing::ReturnNull;
Paul Stewartce4ec192012-03-14 12:53:46 -070077using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070078using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080079using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080080using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080081using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070082using ::testing::Test;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070083using ::testing::WithArg;
Chris Masone9be4a9d2011-05-16 15:44:09 -070084
Chris Masone3bd3c8c2011-06-13 08:20:26 -070085class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070086 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070087 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020088 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080089 device_info_(new NiceMock<MockDeviceInfo>(
90 control_interface(),
91 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080093 reinterpret_cast<Manager*>(NULL))),
Paul Stewart3c504012013-01-17 17:49:58 -080094 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070095 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080096 wifi_provider_(new NiceMock<MockWiFiProvider>()),
97 crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(),
98 glib())) {
Ben Chana55469d2014-01-27 16:35:29 -080099 ON_CALL(proxy_factory_, CreatePowerManagerProxy(_))
100 .WillByDefault(ReturnNull());
101
Paul Stewart22aa71b2011-09-16 12:15:11 -0700102 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
103 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800104 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700105 manager(),
106 "null0",
107 "addr0",
108 0));
109 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
110 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800111 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700112 manager(),
113 "null1",
114 "addr1",
115 1));
116 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
117 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800118 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700119 manager(),
120 "null2",
121 "addr2",
122 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800123 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
124 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800125 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800126 manager(),
127 "null3",
128 "addr3",
129 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700130 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800131 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800132
133 // Replace the manager's adaptor with a quieter one, and one
134 // we can do EXPECT*() against. Passes ownership.
135 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800136
Paul Stewart35eff132013-04-12 12:08:40 -0700137 // Replace the manager's Ethernet EAP provider with our mock.
138 // Passes ownership.
139 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
140
Paul Stewart3c504012013-01-17 17:49:58 -0800141 // Replace the manager's WiFi provider with our mock. Passes
142 // ownership.
143 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800144
Paul Stewartb87d22b2013-07-29 11:11:37 -0700145 // Update the manager's map from technology to provider.
146 manager()->UpdateProviderMapping();
147
Christopher Wiley1057cd72013-02-28 15:21:29 -0800148 // Replace the manager's crypto util proxy with our mock. Passes
149 // ownership.
150 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700151 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700152 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700153
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100154 void SetMetrics(Metrics *metrics) {
155 manager()->set_metrics(metrics);
156 }
157
Paul Stewartfdd16072011-09-16 12:41:35 -0700158 bool IsDeviceRegistered(const DeviceRefPtr &device,
159 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700160 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700161 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700162 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700163 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700164 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700165
Paul Stewarta849a3d2011-11-03 05:54:09 -0700166 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
167 manager->profiles_.push_back(profile);
168 }
169
Paul Stewart63864b62012-11-07 15:10:55 -0800170 void SetRunning(bool running) {
171 manager()->running_ = running;
172 }
173
Paul Stewart75225512012-01-26 22:51:33 -0800174 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
175 return manager->ephemeral_profile_;
176 }
177
Paul Stewart307c2502013-03-23 12:32:10 -0700178 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
179 return manager->profiles_;
180 }
181
Chris Masone6515aab2011-10-12 16:19:09 -0700182 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
183 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700184 FilePath final_path(storage_path());
185 final_path = final_path.Append("test.profile");
186 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
187 storage->set_path(final_path);
188 if (!storage->Open())
189 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800190 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800191 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800192 manager,
193 id,
194 "",
195 false));
196 profile->set_storage(storage.release()); // Passes ownership of "storage".
197 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700198 }
199
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700200 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700201 const string &user_identifier,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700202 const string &profile_identifier,
203 const string &service_name) {
204 GLib glib;
205 KeyFileStore store(&glib);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700206 store.set_path(temp_dir->path().Append(
207 base::StringPrintf("%s/%s.profile", user_identifier.c_str(),
208 profile_identifier.c_str())));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700209 return store.Open() &&
210 store.SetString(service_name, "rather", "irrelevant") &&
211 store.Close();
212 }
213
214 Error::Type TestCreateProfile(Manager *manager, const string &name) {
215 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800216 string path;
217 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700218 return error.type();
219 }
220
221 Error::Type TestPopAnyProfile(Manager *manager) {
222 Error error;
223 manager->PopAnyProfile(&error);
224 return error.type();
225 }
226
Paul Stewart307c2502013-03-23 12:32:10 -0700227 Error::Type TestPopAllUserProfiles(Manager *manager) {
228 Error error;
229 manager->PopAllUserProfiles(&error);
230 return error.type();
231 }
232
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700233 Error::Type TestPopProfile(Manager *manager, const string &name) {
234 Error error;
235 manager->PopProfile(name, &error);
236 return error.type();
237 }
238
239 Error::Type TestPushProfile(Manager *manager, const string &name) {
240 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800241 string path;
242 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700243 return error.type();
244 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000245
Paul Stewartf3eced92013-04-17 12:18:22 -0700246 Error::Type TestInsertUserProfile(Manager *manager,
247 const string &name,
248 const string &user_hash) {
249 Error error;
250 string path;
251 manager->InsertUserProfile(name, user_hash, &path, &error);
252 return error.type();
253 }
254
Paul Stewartd2e1c362013-03-03 19:06:07 -0800255 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
256 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700257 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800258 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800259 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200260 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700261 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800262 return profile;
263 }
264
265 void AddMockProfileToManager(Manager *manager) {
266 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700267 }
268
Paul Stewartdfa46052012-06-26 09:44:14 -0700269 void CompleteServiceSort() {
270 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
271 dispatcher()->DispatchPendingEvents();
272 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
273 }
274
Paul Stewart03e29f72013-09-26 00:49:48 -0700275 void RefreshConnectionState() {
276 manager()->RefreshConnectionState();
277 }
278
Paul Stewart49739c02012-08-08 17:24:03 -0700279 RpcIdentifier GetDefaultServiceRpcIdentifier() {
280 return manager()->GetDefaultServiceRpcIdentifier(NULL);
281 }
282
Paul Stewart4d5efb72012-09-17 12:24:34 -0700283 void SetResolver(Resolver *resolver) {
284 manager()->resolver_ = resolver;
285 }
286
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700287 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
288 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
289 }
290
291 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
292 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700293 }
294
295 const string &GetIgnoredDNSSearchPaths() {
296 return manager()->props_.ignored_dns_search_paths;
297 }
298
Paul Stewartd2e1c362013-03-03 19:06:07 -0800299 WiFiServiceRefPtr ReleaseTempMockService() {
300 // Take a reference to hold during this function.
301 WiFiServiceRefPtr temp_service = temp_mock_service_;
302 temp_mock_service_ = NULL;
303 return temp_service;
304 }
305
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700306 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000307 typedef scoped_refptr<MockService> MockServiceRefPtr;
308
Darin Petkova5e07ef2012-07-09 14:27:57 +0200309 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
310 public:
311 ServiceWatcher() {}
312 virtual ~ServiceWatcher() {}
313
314 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
315
316 private:
317 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
318 };
319
Darin Petkov3ec55342012-09-28 14:04:44 +0200320 class TerminationActionTest :
321 public base::SupportsWeakPtr<TerminationActionTest> {
322 public:
323 static const char kActionName[];
324
325 TerminationActionTest() : manager_(NULL) {}
326 virtual ~TerminationActionTest() {}
327
328 MOCK_METHOD1(Done, void(const Error &error));
329
330 void Action() {
331 manager_->TerminationActionComplete("action");
332 }
333
334 void set_manager(Manager *manager) { manager_ = manager; }
335
336 private:
337 Manager *manager_;
338 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
339 };
340
Christopher Wiley1057cd72013-02-28 15:21:29 -0800341 class DestinationVerificationTest :
342 public base::SupportsWeakPtr<DestinationVerificationTest> {
343 public:
344 DestinationVerificationTest() {}
345 virtual ~DestinationVerificationTest() {}
346
347 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
348 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
349 const string &value));
350 private:
351 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
352 };
353
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700354 class DisableTechnologyReplyHandler :
355 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
356 public:
357 DisableTechnologyReplyHandler() {}
358 virtual ~DisableTechnologyReplyHandler() {}
359
360 MOCK_METHOD1(ReportResult, void(const Error &));
361
362 private:
363 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
364 };
365
Daniel Eratfac09532014-04-17 20:25:59 -0700366 void SetSuspending(bool suspending) {
367 power_manager_->suspending_ = suspending;
Darin Petkovca621542012-07-25 14:25:56 +0200368 }
369
370 void SetPowerManager() {
371 manager()->set_power_manager(power_manager_.release());
372 }
373
Darin Petkov3ec55342012-09-28 14:04:44 +0200374 HookTable *GetTerminationActions() {
375 return &manager()->termination_actions_;
376 }
377
Daniel Erat0818cca2012-12-14 10:16:21 -0800378 void OnSuspendImminent(int suspend_id) {
379 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200380 }
381
Daniel Eratfac09532014-04-17 20:25:59 -0700382 void OnSuspendDone(int suspend_id) {
383 manager()->OnSuspendDone(suspend_id);
384 }
385
Daniel Erat0818cca2012-12-14 10:16:21 -0800386 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
387 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200388 }
389
Paul Stewartbfb82552012-10-24 16:48:48 -0700390 vector<string> EnumerateAvailableServices() {
391 return manager()->EnumerateAvailableServices(NULL);
392 }
393
394 vector<string> EnumerateWatchedServices() {
395 return manager()->EnumerateWatchedServices(NULL);
396 }
397
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000398 MockServiceRefPtr MakeAutoConnectableService() {
399 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
400 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800401 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000402 manager());
Paul Stewart2da34c02013-10-17 15:28:56 -0700403 service->EnableAndRetainAutoConnect();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700404 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000405 return service;
406 }
407
Paul Stewart35eff132013-04-12 12:08:40 -0700408 void SetEapProviderService(const ServiceRefPtr &service) {
409 ethernet_eap_provider_->set_service(service);
410 }
411
Ben Chana55469d2014-01-27 16:35:29 -0800412 NiceMock<MockProxyFactory> proxy_factory_;
Darin Petkovca621542012-07-25 14:25:56 +0200413 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700414 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800415 scoped_ptr<MockDeviceInfo> device_info_;
416
Paul Stewartd2e1c362013-03-03 19:06:07 -0800417 // This service is held for the manager, and given ownership in a mock
418 // function. This ensures that when the Manager takes ownership, there
419 // is only one reference left.
420 scoped_refptr<MockWiFiService> temp_mock_service_;
421
Paul Stewart3c504012013-01-17 17:49:58 -0800422 // These pointers are owned by the manager, and only tracked here for
423 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800424 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700425 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800426 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800427 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700428};
429
Darin Petkov3ec55342012-09-28 14:04:44 +0200430const char ManagerTest::TerminationActionTest::kActionName[] = "action";
431
Paul Stewart22aa71b2011-09-16 12:15:11 -0700432bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700433 if (!manager()->sort_services_task_.IsCancelled()) {
434 manager()->SortServicesTask();
435 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700436 return (svc0.get() == manager()->services_[0].get() &&
437 svc1.get() == manager()->services_[1].get());
438}
439
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700440void SetErrorPermissionDenied(Error *error) {
441 error->Populate(Error::kPermissionDenied);
442}
443
Arman Uguray2f352e62013-08-28 19:12:53 -0700444void SetErrorSuccess(Error *error) {
445 error->Reset();
446}
447
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700448TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700449 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700450 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700451}
452
Chris Masone9be4a9d2011-05-16 15:44:09 -0700453TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700454 ON_CALL(*mock_devices_[0].get(), technology())
455 .WillByDefault(Return(Technology::kEthernet));
456 ON_CALL(*mock_devices_[1].get(), technology())
457 .WillByDefault(Return(Technology::kWifi));
458 ON_CALL(*mock_devices_[2].get(), technology())
459 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700460
Paul Stewart22aa71b2011-09-16 12:15:11 -0700461 manager()->RegisterDevice(mock_devices_[0]);
462 manager()->RegisterDevice(mock_devices_[1]);
463 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700464
Paul Stewart22aa71b2011-09-16 12:15:11 -0700465 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
466 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
467 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700468}
469
Paul Stewarta41e38d2011-11-11 07:47:29 -0800470TEST_F(ManagerTest, DeviceRegistrationAndStart) {
471 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500472 mock_devices_[0]->enabled_persistent_ = true;
473 mock_devices_[1]->enabled_persistent_ = false;
474 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800475 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500476 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800477 .Times(0);
478 manager()->RegisterDevice(mock_devices_[0]);
479 manager()->RegisterDevice(mock_devices_[1]);
480}
481
482TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800483 MockProfile *profile =
484 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800485 DeviceRefPtr device_ref(mock_devices_[0].get());
486 AdoptProfile(manager(), profile); // Passes ownership.
487 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200488 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800489 manager()->RegisterDevice(mock_devices_[0]);
490}
491
Chris Masone9be4a9d2011-05-16 15:44:09 -0700492TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700493 ON_CALL(*mock_devices_[0].get(), technology())
494 .WillByDefault(Return(Technology::kEthernet));
495 ON_CALL(*mock_devices_[1].get(), technology())
496 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700497
Gaurav Shah435de2c2011-11-17 19:01:07 -0800498 manager()->RegisterDevice(mock_devices_[0]);
499 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700500
Paul Stewart22aa71b2011-09-16 12:15:11 -0700501 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
502 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700503
Thieu Le5133b712013-02-19 14:47:21 -0800504 MockProfile *profile =
505 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700506 AdoptProfile(manager(), profile); // Passes ownership.
507
Eric Shienbrood9a245532012-03-07 14:20:39 -0500508 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700509 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800510 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700511 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700512
Eric Shienbrood9a245532012-03-07 14:20:39 -0500513 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700514 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800515 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700516 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700517}
518
519TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700520 // It's much easier and safer to use a real GLib for this test.
521 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700522 Manager manager(control_interface(),
523 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800524 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700525 &glib,
526 run_path(),
527 storage_path(),
528 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700529 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
530 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700531 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700532
Chris Masone9be4a9d2011-05-16 15:44:09 -0700533 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700534 new NiceMock<MockService>(control_interface(),
535 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800536 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700537 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700538 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700539 new NiceMock<MockService>(control_interface(),
540 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800541 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700542 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700543
Darin Petkov457728b2013-01-09 09:49:08 +0100544 string service1_name(mock_service->unique_name());
545 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700546
547 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
548 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700549 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700550 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700551 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700552 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700553 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700554
Chris Masone9d779932011-08-25 16:33:41 -0700555 manager.RegisterService(mock_service);
556 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700557
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800558 Error error;
559 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700560 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700561 EXPECT_EQ(2, ids.size());
562 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
563 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700564
Chris Masone9d779932011-08-25 16:33:41 -0700565 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
566 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
567
568 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700569}
570
Chris Masone6515aab2011-10-12 16:19:09 -0700571TEST_F(ManagerTest, RegisterKnownService) {
572 // It's much easier and safer to use a real GLib for this test.
573 GLib glib;
574 Manager manager(control_interface(),
575 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800576 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700577 &glib,
578 run_path(),
579 storage_path(),
580 string());
581 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
582 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700583 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700584 {
585 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
586 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800587 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700588 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700589 ASSERT_TRUE(profile->AdoptService(service1));
590 ASSERT_TRUE(profile->ContainsService(service1));
591 } // Force destruction of service1.
592
593 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
594 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800595 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700596 &manager));
597 manager.RegisterService(service2);
598 EXPECT_EQ(service2->profile().get(), profile.get());
599 manager.Stop();
600}
601
602TEST_F(ManagerTest, RegisterUnknownService) {
603 // It's much easier and safer to use a real GLib for this test.
604 GLib glib;
605 Manager manager(control_interface(),
606 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800607 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700608 &glib,
609 run_path(),
610 storage_path(),
611 string());
612 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
613 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700614 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700615 {
616 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
617 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800618 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700619 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700620 ASSERT_TRUE(profile->AdoptService(service1));
621 ASSERT_TRUE(profile->ContainsService(service1));
622 } // Force destruction of service1.
623 scoped_refptr<MockService> mock_service2(
624 new NiceMock<MockService>(control_interface(),
625 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800626 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700627 &manager));
628 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100629 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700630 manager.RegisterService(mock_service2);
631 EXPECT_NE(mock_service2->profile().get(), profile.get());
632 manager.Stop();
633}
634
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000635TEST_F(ManagerTest, DeregisterUnregisteredService) {
636 // WiFi assumes that it can deregister a service that is not
637 // registered. (E.g. a hidden service can be deregistered when it
638 // loses its last endpoint, and again when WiFi is Stop()-ed.)
639 //
640 // So test that doing so doesn't cause a crash.
641 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
642 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800643 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000644 manager());
645 manager()->DeregisterService(service);
646}
647
Chris Masonea8a2c252011-06-27 22:16:30 -0700648TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700649 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700650 map<string, ::DBus::Variant> props;
651 Error error(Error::kInvalidProperty, "");
652 {
653 ::DBus::Error dbus_error;
654 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700655 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700656 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700657 expected,
658 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700659 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700660 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
661 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700662 expected);
663 }
664 {
665 ::DBus::Error dbus_error;
666 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700667 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700668 expected,
669 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700670 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700671 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
672 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700673 expected);
674 }
675}
676
Chris Masone3c3f6a12011-07-01 10:01:41 -0700677TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700678 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800679 manager()->RegisterDevice(mock_devices_[0]);
680 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700681 {
682 map<string, ::DBus::Variant> props;
683 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700684 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700685 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700686 vector < ::DBus::Path> devices =
Ben Chan923a5022013-09-20 11:23:23 -0700687 props[kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700688 EXPECT_EQ(2, devices.size());
689 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700690}
691
mukesh agrawal2366eed2012-03-20 18:21:50 -0700692TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700693 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700694 map<string, ::DBus::Variant> props;
695 ::DBus::Error dbus_error;
696 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
697 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700698 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700699 ASSERT_FALSE(prop == props.end());
700 const ::DBus::Variant &variant = prop->second;
701 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
702}
703
Chris Masone6791a432011-07-12 13:23:19 -0700704TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700705 Manager manager(control_interface(),
706 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800707 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700708 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700709 run_path(),
710 storage_path(),
711 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700712 scoped_refptr<MockService> s2(new MockService(control_interface(),
713 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800714 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700715 &manager));
716 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700717 {
Chris Masone6515aab2011-10-12 16:19:09 -0700718 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700719 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800720 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700721 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700722 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700723 .WillRepeatedly(Return(true));
724 EXPECT_CALL(*storage, Flush())
725 .Times(AnyNumber())
726 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700727 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700728 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700729 }
Chris Masone6515aab2011-10-12 16:19:09 -0700730 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800731 ProfileRefPtr profile(
732 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700733 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700734
Chris Masone6515aab2011-10-12 16:19:09 -0700735 // Now, move the Service |s2| to another profile.
736 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
737 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700738
739 // Force destruction of the original Profile, to ensure that the Service
740 // is kept alive and populated with data.
741 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700742 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700743 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700744}
745
Paul Stewart7f61e522012-03-22 11:13:45 -0700746TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
747 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800748 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700749 const string kProfileName("profile0");
750 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
751 .WillRepeatedly(Return(kProfileName));
752 AdoptProfile(manager(), mock_profile);
753
754 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
755 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
756 EXPECT_EQ(mock_profile.get(), profile.get());
757}
758
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800759TEST_F(ManagerTest, SetProfileForService) {
760 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800761 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800762 string profile_name0("profile0");
763 EXPECT_CALL(*profile0, GetRpcIdentifier())
764 .WillRepeatedly(Return(profile_name0));
765 AdoptProfile(manager(), profile0);
766 scoped_refptr<MockService> service(new MockService(control_interface(),
767 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800768 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800769 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700770 EXPECT_FALSE(manager()->HasService(service));
771 {
772 Error error;
773 EXPECT_CALL(*profile0, AdoptService(_))
774 .WillOnce(Return(true));
775 // Expect that setting the profile of a service that does not already
776 // have one assigned does not cause a crash.
777 manager()->SetProfileForService(service, "profile0", &error);
778 EXPECT_TRUE(error.IsSuccess());
779 }
780
781 // The service should be registered as a side-effect of the profile being
782 // set for this service.
783 EXPECT_TRUE(manager()->HasService(service));
784
785 // Since we have mocked Profile::AdoptServie() above, the service's
786 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800787 service->set_profile(profile0);
788
789 {
790 Error error;
791 manager()->SetProfileForService(service, "foo", &error);
792 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700793 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800794 }
795
796 {
797 Error error;
798 manager()->SetProfileForService(service, profile_name0, &error);
799 EXPECT_EQ(Error::kInvalidArguments, error.type());
800 EXPECT_EQ("Service is already connected to this profile", error.message());
801 }
802
803 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800804 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800805 string profile_name1("profile1");
806 EXPECT_CALL(*profile1, GetRpcIdentifier())
807 .WillRepeatedly(Return(profile_name1));
808 AdoptProfile(manager(), profile1);
809
810 {
811 Error error;
812 EXPECT_CALL(*profile1, AdoptService(_))
813 .WillOnce(Return(true));
814 EXPECT_CALL(*profile0, AbandonService(_))
815 .WillOnce(Return(true));
816 manager()->SetProfileForService(service, profile_name1, &error);
817 EXPECT_TRUE(error.IsSuccess());
818 }
819}
820
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700821TEST_F(ManagerTest, CreateProfile) {
822 // It's much easier to use real Glib here since we want the storage
823 // side-effects.
824 GLib glib;
825 ScopedTempDir temp_dir;
826 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
827
828 Manager manager(control_interface(),
829 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800830 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700831 &glib,
832 run_path(),
833 storage_path(),
834 temp_dir.path().value());
835
836 // Invalid name should be rejected.
837 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
838
Paul Stewartd0a3b812012-03-28 22:48:22 -0700839 // A profile with invalid characters in it should similarly be rejected.
840 EXPECT_EQ(Error::kInvalidArguments,
841 TestCreateProfile(&manager, "valid_profile"));
842
843 // We should be able to create a machine profile.
844 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700845
Gary Morainb672d352012-04-25 09:19:06 -0700846 // We should succeed in creating a valid user profile. Verify the returned
847 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700848 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700849 {
850 Error error;
851 string path;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700852 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Gary Morainb672d352012-04-25 09:19:06 -0700853 manager.CreateProfile(kProfile, &path, &error);
854 EXPECT_EQ(Error::kSuccess, error.type());
855 EXPECT_EQ("/profile_rpc", path);
856 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700857
858 // We should fail in creating it a second time (already exists).
859 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
860}
861
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700862// We receive PopProfile when a user logs out, and it should always trigger a
863// MemoryLog Clear() call.
864TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
865 GLib glib;
866 ScopedTempDir temp_dir;
867 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
868 Manager manager(control_interface(),
869 dispatcher(),
870 metrics(),
871 &glib,
872 run_path(),
873 storage_path(),
874 temp_dir.path().value());
875 const char kProfile0[] = "~user/profile0";
876 const char kPurgedMessage[] = "This message should be purged";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700877
878 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
879
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700880 // Create a profile and push it on the stack, leave one uncreated
881 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
882 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
883
884 // Popping a profile which isn't on top should still clear the log.
885 LOG(INFO) << kPurgedMessage;
886 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
887 kPurgedMessage));
888 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
889 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
890 kPurgedMessage));
891
892 // Popping an invalid profile name should do the same thing.
893 LOG(INFO) << kPurgedMessage;
894 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
895 kPurgedMessage));
896 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
897 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
898 kPurgedMessage));
899
900 // Successful pops also purge the message log.
901 LOG(INFO) << kPurgedMessage;
902 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
903 kPurgedMessage));
904 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
905 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
906 kPurgedMessage));
907}
908
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700909TEST_F(ManagerTest, PushPopProfile) {
910 // It's much easier to use real Glib in creating a Manager for this
911 // test here since we want the storage side-effects.
912 GLib glib;
913 ScopedTempDir temp_dir;
914 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
915 Manager manager(control_interface(),
916 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800917 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700918 &glib,
919 run_path(),
920 storage_path(),
921 temp_dir.path().value());
mukesh agrawal92496a42014-04-08 16:04:43 -0700922 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700923
924 // Pushing an invalid profile should fail.
925 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
926
mukesh agrawal92496a42014-04-08 16:04:43 -0700927 // Create and push a default profile. Should succeed.
928 const char kDefaultProfile0[] = "default";
929 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kDefaultProfile0));
930 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kDefaultProfile0));
931 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kDefaultProfile0));
932
933 // Pushing a default profile that does not exist on disk will _not_
934 // fail, because we'll use temporary storage for it.
935 const char kMissingDefaultProfile[] = "missingdefault";
936 EXPECT_EQ(Error::kSuccess,
937 TestPushProfile(&manager, kMissingDefaultProfile));
938 EXPECT_EQ(1, profiles.size());
939 EXPECT_EQ(Error::kSuccess,
940 TestPopProfile(&manager, kMissingDefaultProfile));
941 EXPECT_EQ(0, profiles.size());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700942
943 const char kProfile0[] = "~user/profile0";
944 const char kProfile1[] = "~user/profile1";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700945 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700946
947 // Create a couple of profiles.
948 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
949 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
950
951 // Push these profiles on the stack.
952 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
953 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
954
955 // Pushing a profile a second time should fail.
956 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
957 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
958
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800959 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700960 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800961 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700962
963 // Make sure a profile name that doesn't exist fails.
964 const char kProfile2Id[] = "profile2";
965 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
966 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
967
968 // Create a new service, with a specific storage name.
969 scoped_refptr<MockService> service(
970 new NiceMock<MockService>(control_interface(),
971 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800972 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700973 &manager));
974 const char kServiceName[] = "service_storage_name";
975 EXPECT_CALL(*service.get(), GetStorageIdentifier())
976 .WillRepeatedly(Return(kServiceName));
977 EXPECT_CALL(*service.get(), Load(_))
978 .WillRepeatedly(Return(true));
979
980 // Add this service to the manager -- it should end up in the ephemeral
981 // profile.
982 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800983 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700984
985 // Create storage for a profile that contains the service storage name.
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700986 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile2Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700987 kServiceName));
988
989 // When we push the profile, the service should move away from the
990 // ephemeral profile to this new profile since it has an entry for
991 // this service.
992 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800993 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700994 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
995
996 // Insert another profile that should supersede ownership of the service.
997 const char kProfile3Id[] = "profile3";
998 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700999 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile3Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001000 kServiceName));
1001 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
1002 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
1003
1004 // Popping an invalid profile name should fail.
1005 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
1006
1007 // Popping an profile that is not at the top of the stack should fail.
1008 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
1009
1010 // Popping the top profile should succeed.
1011 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
1012
1013 // Moreover the service should have switched profiles to profile 2.
1014 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
1015
1016 // Popping the top profile should succeed.
1017 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1018
1019 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -08001020 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001021
mukesh agrawal92496a42014-04-08 16:04:43 -07001022 // Pop the remaining two profiles off the stack.
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001023 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1024 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1025
1026 // Next pop should fail with "stack is empty".
1027 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -07001028
1029 const char kMachineProfile0[] = "machineprofile0";
1030 const char kMachineProfile1[] = "machineprofile1";
1031 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1032 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1033
1034 // Should be able to push a machine profile.
1035 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1036
1037 // Should be able to push a user profile atop a machine profile.
1038 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1039
1040 // Pushing a system-wide profile on top of a user profile should fail.
1041 EXPECT_EQ(Error::kInvalidArguments,
1042 TestPushProfile(&manager, kMachineProfile1));
1043
1044 // However if we pop the user profile, we should be able stack another
1045 // machine profile on.
1046 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1047 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001048
1049 // Add two user profiles to the top of the stack.
1050 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1051 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001052 EXPECT_EQ(4, profiles.size());
1053
1054 // PopAllUserProfiles should remove both user profiles, leaving the two
1055 // machine profiles.
1056 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1057 EXPECT_EQ(2, profiles.size());
1058 EXPECT_TRUE(profiles[0]->GetUser().empty());
1059 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001060
1061 // Use InsertUserProfile() instead. Although a machine profile is valid
1062 // in this state, it cannot be added via InsertUserProfile.
1063 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1064 EXPECT_EQ(Error::kInvalidArguments,
1065 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1066 const char kUserHash0[] = "userhash0";
1067 const char kUserHash1[] = "userhash1";
1068 EXPECT_EQ(Error::kSuccess,
1069 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1070 EXPECT_EQ(Error::kSuccess,
1071 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1072 EXPECT_EQ(3, profiles.size());
1073 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1074 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001075}
1076
Paul Stewarte73d05c2012-03-29 16:26:05 -07001077TEST_F(ManagerTest, RemoveProfile) {
1078 // It's much easier to use real Glib in creating a Manager for this
1079 // test here since we want the storage side-effects.
1080 GLib glib;
1081 ScopedTempDir temp_dir;
1082 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1083 Manager manager(control_interface(),
1084 dispatcher(),
1085 metrics(),
1086 &glib,
1087 run_path(),
1088 storage_path(),
1089 temp_dir.path().value());
1090
1091 const char kProfile0[] = "profile0";
1092 FilePath profile_path(
1093 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1094
1095 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001096 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001097
1098 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1099
1100 // Remove should fail since the profile is still on the stack.
1101 {
1102 Error error;
1103 manager.RemoveProfile(kProfile0, &error);
1104 EXPECT_EQ(Error::kInvalidArguments, error.type());
1105 }
1106
1107 // Profile path should still exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001108 EXPECT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001109
1110 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1111
1112 // This should succeed now that the profile is off the stack.
1113 {
1114 Error error;
1115 manager.RemoveProfile(kProfile0, &error);
1116 EXPECT_EQ(Error::kSuccess, error.type());
1117 }
1118
1119 // Profile path should no longer exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001120 EXPECT_FALSE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001121
Ben Chana0ddf462014-02-06 11:32:42 -08001122 // Another remove succeeds, due to a foible in base::DeleteFile --
Paul Stewarte73d05c2012-03-29 16:26:05 -07001123 // it is not an error to delete a file that does not exist.
1124 {
1125 Error error;
1126 manager.RemoveProfile(kProfile0, &error);
1127 EXPECT_EQ(Error::kSuccess, error.type());
1128 }
1129
1130 // Let's create an error case that will "work". Create a non-empty
1131 // directory in the place of the profile pathname.
Ben Chana0ddf462014-02-06 11:32:42 -08001132 ASSERT_TRUE(base::CreateDirectory(profile_path.Append("foo")));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001133 {
1134 Error error;
1135 manager.RemoveProfile(kProfile0, &error);
1136 EXPECT_EQ(Error::kOperationFailed, error.type());
1137 }
1138}
1139
Paul Stewartd3d03882013-08-29 15:43:42 -07001140TEST_F(ManagerTest, RemoveService) {
1141 MockServiceRefPtr mock_service(
1142 new NiceMock<MockService>(control_interface(),
1143 dispatcher(),
1144 metrics(),
1145 manager()));
1146
1147 // Used in expectations which cannot accept a mock refptr.
1148 const ServiceRefPtr &service = mock_service;
1149
1150 manager()->RegisterService(service);
1151 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1152
1153 scoped_refptr<MockProfile> profile(
1154 new StrictMock<MockProfile>(
1155 control_interface(), metrics(), manager(), ""));
1156 AdoptProfile(manager(), profile);
1157
1158 // If service is ephemeral, it should be unloaded and left ephemeral.
1159 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1160 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1161 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1162 manager()->RemoveService(service);
1163 Mock::VerifyAndClearExpectations(mock_service);
1164 Mock::VerifyAndClearExpectations(profile);
1165 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1166 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1167
1168 // If service is not ephemeral and the Manager finds a profile to assign
1169 // the service to, the service should be re-parented. Note that since we
1170 // are using a MockProfile, ConfigureService() never actually changes the
1171 // Service's profile.
1172 service->set_profile(profile);
1173 EXPECT_CALL(*profile, AbandonService(service));
1174 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1175 EXPECT_CALL(*mock_service, Unload()).Times(0);
1176 manager()->RemoveService(service);
1177 Mock::VerifyAndClearExpectations(mock_service);
1178 Mock::VerifyAndClearExpectations(profile);
1179 EXPECT_TRUE(manager()->HasService(service));
1180 EXPECT_EQ(profile.get(), service->profile().get());
1181
1182 // If service becomes ephemeral since there is no profile to support it,
1183 // it should be unloaded.
1184 EXPECT_CALL(*profile, AbandonService(service));
1185 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1186 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1187 manager()->RemoveService(service);
1188 EXPECT_FALSE(manager()->HasService(service));
1189}
1190
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001191TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1192 // It's much easier to use real Glib in creating a Manager for this
1193 // test here since we want the storage side-effects.
1194 GLib glib;
1195 ScopedTempDir temp_dir;
1196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1197 Manager manager(control_interface(),
1198 dispatcher(),
1199 metrics(),
1200 &glib,
1201 run_path(),
1202 storage_path(),
1203 temp_dir.path().value());
1204
1205 const char kProfile0[] = "profile0";
1206 FilePath profile_path(
1207 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1208
1209 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001210 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001211 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1212
1213 // Ensure that even if the backing filestore is removed, we still can't
1214 // create a profile twice.
Ben Chana0ddf462014-02-06 11:32:42 -08001215 ASSERT_TRUE(base::DeleteFile(profile_path, false));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001216 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1217}
1218
Paul Stewart75225512012-01-26 22:51:33 -08001219// Use this matcher instead of passing RefPtrs directly into the arguments
1220// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1221// system teardown.
1222MATCHER_P(IsRefPtrTo, ref_address, "") {
1223 return arg.get() == ref_address;
1224}
1225
1226TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1227 MockServiceRefPtr s_not_in_profile(
1228 new NiceMock<MockService>(control_interface(),
1229 dispatcher(),
1230 metrics(),
1231 manager()));
1232 MockServiceRefPtr s_not_in_group(
1233 new NiceMock<MockService>(control_interface(),
1234 dispatcher(),
1235 metrics(),
1236 manager()));
1237 MockServiceRefPtr s_configure_fail(
1238 new NiceMock<MockService>(control_interface(),
1239 dispatcher(),
1240 metrics(),
1241 manager()));
1242 MockServiceRefPtr s_configure_succeed(
1243 new NiceMock<MockService>(control_interface(),
1244 dispatcher(),
1245 metrics(),
1246 manager()));
1247
1248 string entry_name("entry_name");
1249 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1250 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1251 .WillRepeatedly(Return("not_entry_name"));
1252 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1253 .WillRepeatedly(Return(entry_name));
1254 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1255 .WillRepeatedly(Return(entry_name));
1256
1257 manager()->RegisterService(s_not_in_profile);
1258 manager()->RegisterService(s_not_in_group);
1259 manager()->RegisterService(s_configure_fail);
1260 manager()->RegisterService(s_configure_succeed);
1261
1262 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001263 new StrictMock<MockProfile>(
1264 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001265 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001266 new StrictMock<MockProfile>(
1267 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001268
1269 s_not_in_group->set_profile(profile1);
1270 s_configure_fail->set_profile(profile1);
1271 s_configure_succeed->set_profile(profile1);
1272
1273 AdoptProfile(manager(), profile0);
1274 AdoptProfile(manager(), profile1);
1275
1276 // No services are a member of this profile.
1277 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1278
1279 // No services that are members of this profile have this entry name.
1280 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1281
1282 // Only services that are members of the profile and group will be abandoned.
1283 EXPECT_CALL(*profile1.get(),
1284 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1285 EXPECT_CALL(*profile1.get(),
1286 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1287 EXPECT_CALL(*profile1.get(),
1288 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1289 .WillOnce(Return(true));
1290 EXPECT_CALL(*profile1.get(),
1291 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1292 .WillOnce(Return(true));
1293
1294 // Never allow services to re-join profile1.
1295 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1296 .WillRepeatedly(Return(false));
1297
1298 // Only allow one of the members of the profile and group to successfully
1299 // join profile0.
1300 EXPECT_CALL(*profile0.get(),
1301 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1302 EXPECT_CALL(*profile0.get(),
1303 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1304 EXPECT_CALL(*profile0.get(),
1305 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1306 .WillOnce(Return(false));
1307 EXPECT_CALL(*profile0.get(),
1308 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1309 .WillOnce(Return(true));
1310
1311 // Expect the failed-to-configure service to have Unload() called on it.
1312 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1313 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1314 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1315 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1316
1317 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1318
1319 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1320 EXPECT_EQ(profile1, s_not_in_group->profile());
1321 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1322
1323 // Since we are using a MockProfile, the profile does not actually change,
1324 // since ConfigureService was not actually called on the service.
1325 EXPECT_EQ(profile1, s_configure_succeed->profile());
1326}
1327
Paul Stewart65512e12012-03-26 18:01:08 -07001328TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1329 MockServiceRefPtr s_will_remove0(
1330 new NiceMock<MockService>(control_interface(),
1331 dispatcher(),
1332 metrics(),
1333 manager()));
1334 MockServiceRefPtr s_will_remove1(
1335 new NiceMock<MockService>(control_interface(),
1336 dispatcher(),
1337 metrics(),
1338 manager()));
1339 MockServiceRefPtr s_will_not_remove0(
1340 new NiceMock<MockService>(control_interface(),
1341 dispatcher(),
1342 metrics(),
1343 manager()));
1344 MockServiceRefPtr s_will_not_remove1(
1345 new NiceMock<MockService>(control_interface(),
1346 dispatcher(),
1347 metrics(),
1348 manager()));
1349
1350 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1351 .Times(4); // Once for each registration.
1352
1353 string entry_name("entry_name");
1354 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1355 .WillRepeatedly(Return(entry_name));
1356 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1357 .WillRepeatedly(Return(entry_name));
1358 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1359 .WillRepeatedly(Return(entry_name));
1360 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1361 .WillRepeatedly(Return(entry_name));
1362
1363 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001364 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001365 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001366 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001367 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001368 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001369 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001370 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001371
1372 // One for each service added above.
1373 ASSERT_EQ(4, manager()->services_.size());
1374
1375 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001376 new StrictMock<MockProfile>(
1377 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001378
1379 s_will_remove0->set_profile(profile);
1380 s_will_remove1->set_profile(profile);
1381 s_will_not_remove0->set_profile(profile);
1382 s_will_not_remove1->set_profile(profile);
1383
1384 AdoptProfile(manager(), profile);
1385
1386 // Deny any of the services re-entry to the profile.
1387 EXPECT_CALL(*profile, ConfigureService(_))
1388 .WillRepeatedly(Return(false));
1389
1390 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1391 .WillOnce(Return(true));
1392 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1393 .WillOnce(Return(true));
1394 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1395 .WillOnce(Return(true));
1396 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1397 .WillOnce(Return(true));
1398
1399 EXPECT_CALL(*s_will_remove0, Unload())
1400 .WillOnce(Return(true));
1401 EXPECT_CALL(*s_will_remove1, Unload())
1402 .WillOnce(Return(true));
1403 EXPECT_CALL(*s_will_not_remove0, Unload())
1404 .WillOnce(Return(false));
1405 EXPECT_CALL(*s_will_not_remove1, Unload())
1406 .WillOnce(Return(false));
1407
1408
1409 // This will cause all the profiles to be unloaded.
1410 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1411
1412 // 2 of the 4 services added above should have been unregistered and
1413 // removed, leaving 2.
1414 EXPECT_EQ(2, manager()->services_.size());
1415 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1416 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1417}
1418
1419TEST_F(ManagerTest, PopProfileWithUnload) {
1420 MockServiceRefPtr s_will_remove0(
1421 new NiceMock<MockService>(control_interface(),
1422 dispatcher(),
1423 metrics(),
1424 manager()));
1425 MockServiceRefPtr s_will_remove1(
1426 new NiceMock<MockService>(control_interface(),
1427 dispatcher(),
1428 metrics(),
1429 manager()));
1430 MockServiceRefPtr s_will_not_remove0(
1431 new NiceMock<MockService>(control_interface(),
1432 dispatcher(),
1433 metrics(),
1434 manager()));
1435 MockServiceRefPtr s_will_not_remove1(
1436 new NiceMock<MockService>(control_interface(),
1437 dispatcher(),
1438 metrics(),
1439 manager()));
1440
1441 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1442 .Times(5); // Once for each registration, and one after profile pop.
1443
1444 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001445 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001446 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001447 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001448 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001449 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001450 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001451 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001452
1453 // One for each service added above.
1454 ASSERT_EQ(4, manager()->services_.size());
1455
1456 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001457 new StrictMock<MockProfile>(
1458 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001459 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001460 new StrictMock<MockProfile>(
1461 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001462
1463 s_will_remove0->set_profile(profile1);
1464 s_will_remove1->set_profile(profile1);
1465 s_will_not_remove0->set_profile(profile1);
1466 s_will_not_remove1->set_profile(profile1);
1467
1468 AdoptProfile(manager(), profile0);
1469 AdoptProfile(manager(), profile1);
1470
1471 // Deny any of the services entry to profile0, so they will all be unloaded.
1472 EXPECT_CALL(*profile0, ConfigureService(_))
1473 .WillRepeatedly(Return(false));
1474
1475 EXPECT_CALL(*s_will_remove0, Unload())
1476 .WillOnce(Return(true));
1477 EXPECT_CALL(*s_will_remove1, Unload())
1478 .WillOnce(Return(true));
1479 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001480 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001481 EXPECT_CALL(*s_will_not_remove1, Unload())
1482 .WillOnce(Return(false));
1483
Philipp Neubeck79173602012-11-13 21:10:09 +01001484 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1485 // profile list.
1486 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1487 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1488
Paul Stewart65512e12012-03-26 18:01:08 -07001489 // This will pop profile1, which should cause all our profiles to unload.
1490 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001491 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001492
1493 // 2 of the 4 services added above should have been unregistered and
1494 // removed, leaving 2.
1495 EXPECT_EQ(2, manager()->services_.size());
1496 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1497 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001498
1499 // Expect the unloaded services to lose their profile reference.
1500 EXPECT_FALSE(s_will_remove0->profile());
1501 EXPECT_FALSE(s_will_remove1->profile());
1502
1503 // If we explicitly deregister a service, the effect should be the same
1504 // with respect to the profile reference.
1505 ASSERT_TRUE(s_will_not_remove0->profile());
1506 manager()->DeregisterService(s_will_not_remove0);
1507 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001508}
1509
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001510TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001511 {
1512 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001513 ::DBus::Variant offline_mode;
1514 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001515 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001516 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001517 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001518 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001519 }
1520 {
1521 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001522 ::DBus::Variant country;
1523 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001524 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001525 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001526 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001527 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001528 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001529 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001530 {
1531 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001532 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001533 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001534 PropertyStoreTest::kBoolV,
1535 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001536 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001537 }
1538 {
1539 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001540 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001541 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001542 PropertyStoreTest::kStringV,
1543 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001544 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001545 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001546 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001547 {
1548 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001549 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001550 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001551 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001552 PropertyStoreTest::kStringsV,
1553 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001554 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001555 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001556}
1557
mukesh agrawal32399322011-09-01 10:53:43 -07001558TEST_F(ManagerTest, RequestScan) {
1559 {
1560 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001561 manager()->RegisterDevice(mock_devices_[0].get());
1562 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001563 EXPECT_CALL(*mock_devices_[0], technology())
1564 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001565 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001566 EXPECT_CALL(*mock_devices_[1], technology())
1567 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001568 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Ben Chan923a5022013-09-20 11:23:23 -07001569 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001570 }
1571
1572 {
1573 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001574 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001575 EXPECT_EQ(Error::kInvalidArguments, error.type());
1576 }
1577}
1578
Darin Petkovb65c2452012-02-23 15:17:06 +01001579TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001580 KeyValueStore args;
1581 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001582 manager()->GetService(args, &e);
1583 EXPECT_EQ(Error::kInvalidArguments, e.type());
1584 EXPECT_EQ("must specify service type", e.message());
1585}
1586
1587TEST_F(ManagerTest, GetServiceUnknownType) {
1588 KeyValueStore args;
1589 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001590 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001591 manager()->GetService(args, &e);
1592 EXPECT_EQ(Error::kNotSupported, e.type());
1593 EXPECT_EQ("service type is unsupported", e.message());
1594}
1595
Paul Stewart35eff132013-04-12 12:08:40 -07001596TEST_F(ManagerTest, GetServiceEthernetEap) {
1597 KeyValueStore args;
1598 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001599 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1600 dispatcher(),
1601 metrics(),
1602 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001603 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001604 SetEapProviderService(service);
1605 EXPECT_EQ(service, manager()->GetService(args, &e));
1606 EXPECT_TRUE(e.IsSuccess());
1607}
1608
Darin Petkovb65c2452012-02-23 15:17:06 +01001609TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001610 KeyValueStore args;
1611 Error e;
1612 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001613 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001614 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001615 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001616 manager()->GetService(args, &e);
1617 EXPECT_TRUE(e.IsSuccess());
1618}
1619
Darin Petkov33af05c2012-02-28 10:10:30 +01001620TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1621 KeyValueStore args;
1622 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001623 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001624 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001625 new StrictMock<MockProfile>(
1626 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001627 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001628 ServiceRefPtr service = manager()->GetService(args, &e);
1629 EXPECT_EQ(Error::kNotSupported, e.type());
1630 EXPECT_FALSE(service);
1631}
1632
Darin Petkovb65c2452012-02-23 15:17:06 +01001633TEST_F(ManagerTest, GetServiceVPN) {
1634 KeyValueStore args;
1635 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001636 args.SetString(kTypeProperty, kTypeVPN);
1637 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1638 args.SetString(kProviderHostProperty, "10.8.0.1");
1639 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001640 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001641 new StrictMock<MockProfile>(
1642 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001643 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001644
1645#if defined(DISABLE_VPN)
1646
1647 ServiceRefPtr service = manager()->GetService(args, &e);
1648 EXPECT_EQ(Error::kNotSupported, e.type());
1649 EXPECT_FALSE(service);
1650
1651#else
1652
Paul Stewart7f5ad572012-06-04 15:18:54 -07001653 ServiceRefPtr updated_service;
1654 EXPECT_CALL(*profile, UpdateService(_))
1655 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1656 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001657 EXPECT_CALL(*profile, LoadService(_))
1658 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001659 EXPECT_CALL(*profile, ConfigureService(_))
1660 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001661 ServiceRefPtr service = manager()->GetService(args, &e);
1662 EXPECT_TRUE(e.IsSuccess());
1663 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001664 EXPECT_EQ(service, updated_service);
1665 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001666
1667#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001668}
1669
Ben Chan520eb172013-10-30 20:51:04 -07001670#if !defined(DISABLE_WIMAX)
1671
Darin Petkovc63dcf02012-05-24 11:51:43 +02001672TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1673 KeyValueStore args;
1674 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001675 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001676 ServiceRefPtr service = manager()->GetService(args, &e);
1677 EXPECT_EQ(Error::kInvalidArguments, e.type());
1678 EXPECT_EQ("Missing WiMAX network id.", e.message());
1679 EXPECT_FALSE(service);
1680}
1681
Darin Petkovd1cd7972012-05-22 15:26:15 +02001682TEST_F(ManagerTest, GetServiceWiMax) {
1683 KeyValueStore args;
1684 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001685 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001686 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001687 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001688 ServiceRefPtr service = manager()->GetService(args, &e);
1689 EXPECT_TRUE(e.IsSuccess());
1690 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001691}
1692
Ben Chan520eb172013-10-30 20:51:04 -07001693#endif // DISABLE_WIMAX
1694
Paul Stewart7f61e522012-03-22 11:13:45 -07001695TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1696 // Manager calls ActiveProfile() so we need at least one profile installed.
1697 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001698 new NiceMock<MockProfile>(
1699 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001700 AdoptProfile(manager(), profile);
1701
1702 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001703 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001704 Error error;
1705 manager()->ConfigureService(args, &error);
1706 EXPECT_EQ(Error::kInvalidArguments, error.type());
1707 EXPECT_EQ("Invalid profile name xxx", error.message());
1708}
1709
1710TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1711 // Manager calls ActiveProfile() so we need at least one profile installed.
1712 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001713 new NiceMock<MockProfile>(
1714 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001715 AdoptProfile(manager(), profile);
1716
1717 KeyValueStore args;
1718 Error error;
1719 manager()->ConfigureService(args, &error);
1720 EXPECT_EQ(Error::kInvalidArguments, error.type());
1721 EXPECT_EQ("must specify service type", error.message());
1722}
1723
1724// A registered service in the ephemeral profile should be moved to the
1725// active profile as a part of configuration if no profile was explicitly
1726// specified.
1727TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1728 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001729 new NiceMock<MockProfile>(
1730 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001731
1732 AdoptProfile(manager(), profile); // This is now the active profile.
1733
Paul Stewartd2e1c362013-03-03 19:06:07 -08001734 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001735 scoped_refptr<MockWiFiService> service(
1736 new NiceMock<MockWiFiService>(control_interface(),
1737 dispatcher(),
1738 metrics(),
1739 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001740 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001741 ssid,
1742 "",
1743 "",
1744 false));
1745
1746 manager()->RegisterService(service);
1747 service->set_profile(GetEphemeralProfile(manager()));
1748
Paul Stewart3c504012013-01-17 17:49:58 -08001749 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001750 .WillOnce(Return(service));
1751 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1752 .WillOnce(Return(true));
1753 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1754 .WillOnce(Return(true));
1755
1756 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001757 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001758 Error error;
1759 manager()->ConfigureService(args, &error);
1760 EXPECT_TRUE(error.IsSuccess());
1761}
1762
Paul Stewart2c575d22012-12-07 12:28:57 -08001763// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001764// specify a profile, it should be moved from the profile it was previously
1765// in to the specified profile if one was requested.
1766TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1767 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001768 new NiceMock<MockProfile>(
1769 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001770 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001771 new NiceMock<MockProfile>(
1772 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001773
1774 const string kProfileName0 = "profile0";
1775 const string kProfileName1 = "profile1";
1776
1777 EXPECT_CALL(*profile0, GetRpcIdentifier())
1778 .WillRepeatedly(Return(kProfileName0));
1779 EXPECT_CALL(*profile1, GetRpcIdentifier())
1780 .WillRepeatedly(Return(kProfileName1));
1781
1782 AdoptProfile(manager(), profile0);
1783 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1784
Paul Stewartd2e1c362013-03-03 19:06:07 -08001785 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001786 scoped_refptr<MockWiFiService> service(
1787 new NiceMock<MockWiFiService>(control_interface(),
1788 dispatcher(),
1789 metrics(),
1790 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001791 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001792 ssid,
1793 "",
1794 "",
1795 false));
1796
1797 manager()->RegisterService(service);
1798 service->set_profile(profile1);
1799
Paul Stewart3c504012013-01-17 17:49:58 -08001800 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001801 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001802 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1803 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001804 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1805 .WillOnce(Return(true));
1806 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1807 .WillOnce(Return(true));
1808 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1809 .WillOnce(Return(true));
1810
1811 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001812 args.SetString(kTypeProperty, kTypeWifi);
1813 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001814 Error error;
1815 manager()->ConfigureService(args, &error);
1816 EXPECT_TRUE(error.IsSuccess());
1817 service->set_profile(NULL); // Breaks refcounting loop.
1818}
1819
Paul Stewart2c575d22012-12-07 12:28:57 -08001820// If we configure a service that is already a member of the specified
1821// profile, the Manager should not call LoadService or AdoptService again
1822// on this service.
1823TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1824 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001825 new NiceMock<MockProfile>(
1826 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001827
1828 const string kProfileName0 = "profile0";
1829
1830 EXPECT_CALL(*profile0, GetRpcIdentifier())
1831 .WillRepeatedly(Return(kProfileName0));
1832
1833 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1834
Paul Stewartd2e1c362013-03-03 19:06:07 -08001835 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001836 scoped_refptr<MockWiFiService> service(
1837 new NiceMock<MockWiFiService>(control_interface(),
1838 dispatcher(),
1839 metrics(),
1840 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001841 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001842 ssid,
1843 "",
1844 "",
1845 false));
1846
1847 manager()->RegisterService(service);
1848 service->set_profile(profile0);
1849
Paul Stewart3c504012013-01-17 17:49:58 -08001850 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001851 .WillOnce(Return(service));
1852 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1853 .Times(0);
1854 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1855 .WillOnce(Return(true));
1856 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1857 .Times(0);
1858
1859 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001860 args.SetString(kTypeProperty, kTypeWifi);
1861 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001862 Error error;
1863 manager()->ConfigureService(args, &error);
1864 EXPECT_TRUE(error.IsSuccess());
1865 service->set_profile(NULL); // Breaks refcounting loop.
1866}
1867
Paul Stewart7f61e522012-03-22 11:13:45 -07001868// An unregistered service should remain unregistered, but its contents should
1869// be saved to the specified profile nonetheless.
1870TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1871 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001872 new NiceMock<MockProfile>(
1873 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001874 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001875 new NiceMock<MockProfile>(
1876 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001877
1878 const string kProfileName0 = "profile0";
1879 const string kProfileName1 = "profile1";
1880
1881 EXPECT_CALL(*profile0, GetRpcIdentifier())
1882 .WillRepeatedly(Return(kProfileName0));
1883 EXPECT_CALL(*profile1, GetRpcIdentifier())
1884 .WillRepeatedly(Return(kProfileName1));
1885
1886 AdoptProfile(manager(), profile0);
1887 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1888
Paul Stewartd2e1c362013-03-03 19:06:07 -08001889 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001890 scoped_refptr<MockWiFiService> service(
1891 new NiceMock<MockWiFiService>(control_interface(),
1892 dispatcher(),
1893 metrics(),
1894 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001895 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001896 ssid,
1897 "",
1898 "",
1899 false));
1900
1901 service->set_profile(profile1);
1902
Paul Stewart3c504012013-01-17 17:49:58 -08001903 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001904 .WillOnce(Return(service));
1905 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1906 .WillOnce(Return(true));
1907 EXPECT_CALL(*profile0, AdoptService(_))
1908 .Times(0);
1909 EXPECT_CALL(*profile1, AdoptService(_))
1910 .Times(0);
1911
1912 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001913 args.SetString(kTypeProperty, kTypeWifi);
1914 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001915 Error error;
1916 manager()->ConfigureService(args, &error);
1917 EXPECT_TRUE(error.IsSuccess());
1918}
1919
Paul Stewartd2e1c362013-03-03 19:06:07 -08001920TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1921 KeyValueStore args;
1922 Error error;
1923 ServiceRefPtr service =
1924 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001925 EXPECT_EQ(Error::kInvalidArguments, error.type());
1926 EXPECT_EQ("must specify service type", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001927 EXPECT_EQ(NULL, service.get());
1928}
1929
1930TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1931 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001932 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001933 Error error;
1934 ServiceRefPtr service =
1935 manager()->ConfigureServiceForProfile("", args, &error);
1936 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001937 EXPECT_EQ("service type is unsupported", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001938 EXPECT_EQ(NULL, service.get());
1939}
1940
1941TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1942 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001943 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001944 Error error;
1945 ServiceRefPtr service =
1946 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1947 EXPECT_EQ(Error::kNotFound, error.type());
1948 EXPECT_EQ("Profile specified was not found", error.message());
1949 EXPECT_EQ(NULL, service.get());
1950}
1951
1952TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1953 const string kProfileName0 = "profile0";
1954 const string kProfileName1 = "profile1";
1955 scoped_refptr<MockProfile> profile0(
1956 AddNamedMockProfileToManager(manager(), kProfileName0));
1957
1958 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001959 args.SetString(kTypeProperty, kTypeWifi);
1960 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001961 Error error;
1962 ServiceRefPtr service =
1963 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1964 EXPECT_EQ(Error::kInvalidArguments, error.type());
1965 EXPECT_EQ("Profile argument does not match that in "
1966 "the configuration arguments", error.message());
1967 EXPECT_EQ(NULL, service.get());
1968}
1969
1970TEST_F(ManagerTest,
1971 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1972 const string kProfileName0 = "profile0";
1973 scoped_refptr<MockProfile> profile0(
1974 AddNamedMockProfileToManager(manager(), kProfileName0));
1975 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001976 args.SetString(kTypeProperty, kTypeWifi);
1977 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001978
1979 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1980 .WillOnce(Return(WiFiServiceRefPtr()));
1981 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1982 .WillOnce(Return(WiFiServiceRefPtr()));
1983 Error error;
1984 ServiceRefPtr service =
1985 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1986 // Since we didn't set the error in the GetService expectation above...
1987 EXPECT_TRUE(error.IsSuccess());
1988 EXPECT_EQ(NULL, service.get());
1989}
1990
1991TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1992 const string kProfileName0 = "profile0";
1993 scoped_refptr<MockProfile> profile0(
1994 AddNamedMockProfileToManager(manager(), kProfileName0));
1995
1996 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001997 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001998
1999 scoped_refptr<MockWiFiService> mock_service(
2000 new NiceMock<MockWiFiService>(control_interface(),
2001 dispatcher(),
2002 metrics(),
2003 manager(),
2004 wifi_provider_,
2005 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002006 kModeManaged,
2007 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002008 false));
2009 ServiceRefPtr mock_service_generic(mock_service.get());
2010 mock_service->set_profile(profile0);
2011 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2012 .WillOnce(Return(WiFiServiceRefPtr()));
2013 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2014 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2015 .WillOnce(Return(true));
2016 Error error;
2017 ServiceRefPtr service =
2018 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2019 EXPECT_TRUE(error.IsSuccess());
2020 EXPECT_EQ(mock_service.get(), service.get());
2021 mock_service->set_profile(NULL); // Breaks reference cycle.
2022}
2023
2024TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2025 scoped_refptr<MockService> mock_service(
2026 new NiceMock<MockService>(control_interface(),
2027 dispatcher(),
2028 metrics(),
2029 manager()));
2030 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002031 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002032 manager()->RegisterService(mock_service);
2033 ServiceRefPtr mock_service_generic(mock_service.get());
2034
2035 const string kProfileName = "profile";
2036 scoped_refptr<MockProfile> profile(
2037 AddNamedMockProfileToManager(manager(), kProfileName));
2038 mock_service->set_profile(profile);
2039
2040 EXPECT_CALL(*mock_service, technology())
2041 .WillOnce(Return(Technology::kCellular))
2042 .WillOnce(Return(Technology::kWifi));
2043
2044 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2045 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2046 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2047
2048 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002049 args.SetString(kTypeProperty, kTypeWifi);
2050 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002051
2052 // The first attempt should fail because the service reports a technology
2053 // other than "WiFi".
2054 {
2055 Error error;
2056 ServiceRefPtr service =
2057 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2058 EXPECT_EQ(NULL, service.get());
2059 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002060 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002061 }
2062
2063 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2064 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2065
2066 {
2067 Error error;
2068 ServiceRefPtr service =
2069 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2070 EXPECT_TRUE(error.IsSuccess());
2071 EXPECT_EQ(mock_service.get(), service.get());
2072 EXPECT_EQ(profile.get(), service->profile().get());
2073 }
2074 mock_service->set_profile(NULL); // Breaks reference cycle.
2075}
2076
2077TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2078 const string kProfileName = "profile";
2079 scoped_refptr<MockProfile> profile(
2080 AddNamedMockProfileToManager(manager(), kProfileName));
2081
2082 scoped_refptr<MockWiFiService> mock_service(
2083 new NiceMock<MockWiFiService>(control_interface(),
2084 dispatcher(),
2085 metrics(),
2086 manager(),
2087 wifi_provider_,
2088 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002089 kModeManaged,
2090 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002091 false));
2092 mock_service->set_profile(profile);
2093 ServiceRefPtr mock_service_generic(mock_service.get());
2094
2095 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002096 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002097 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2098 .WillOnce(Return(mock_service));
2099 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2100 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2101 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2102 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2103
2104 Error error;
2105 ServiceRefPtr service =
2106 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2107 EXPECT_TRUE(error.IsSuccess());
2108 EXPECT_EQ(mock_service.get(), service.get());
2109 EXPECT_EQ(profile.get(), service->profile().get());
2110 mock_service->set_profile(NULL); // Breaks reference cycle.
2111}
2112
2113TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2114 const string kProfileName = "profile";
2115 scoped_refptr<MockProfile> profile(
2116 AddNamedMockProfileToManager(manager(), kProfileName));
2117
2118 scoped_refptr<MockWiFiService> mock_service(
2119 new NiceMock<MockWiFiService>(control_interface(),
2120 dispatcher(),
2121 metrics(),
2122 manager(),
2123 wifi_provider_,
2124 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002125 kModeManaged,
2126 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002127 false));
2128 mock_service->set_profile(GetEphemeralProfile(manager()));
2129 ServiceRefPtr mock_service_generic(mock_service.get());
2130
2131 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002132 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002133 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2134 .WillOnce(Return(mock_service));
2135 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2136 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2137 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2138
2139 Error error;
2140 ServiceRefPtr service =
2141 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2142 EXPECT_TRUE(error.IsSuccess());
2143 EXPECT_EQ(mock_service.get(), service.get());
2144 EXPECT_EQ(profile.get(), service->profile().get());
2145 mock_service->set_profile(NULL); // Breaks reference cycle.
2146}
2147
2148TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2149 const string kProfileName0 = "profile0";
2150 scoped_refptr<MockProfile> profile0(
2151 AddNamedMockProfileToManager(manager(), kProfileName0));
2152 const string kProfileName1 = "profile1";
2153 scoped_refptr<MockProfile> profile1(
2154 AddNamedMockProfileToManager(manager(), kProfileName1));
2155
2156 scoped_refptr<MockWiFiService> mock_service(
2157 new NiceMock<MockWiFiService>(control_interface(),
2158 dispatcher(),
2159 metrics(),
2160 manager(),
2161 wifi_provider_,
2162 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002163 kModeManaged,
2164 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002165 false));
2166 manager()->RegisterService(mock_service);
2167 mock_service->set_profile(profile0);
2168 ServiceRefPtr mock_service_generic(mock_service.get());
2169
2170 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002171 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002172 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2173 .WillOnce(Return(mock_service));
2174 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2175 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2176 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2177 // This happens once to make the service loadable for the ConfigureService
2178 // below, and a second time after the service is modified.
2179 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2180 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2181 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2182 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2183
2184 Error error;
2185 ServiceRefPtr service =
2186 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2187 EXPECT_TRUE(error.IsSuccess());
2188 EXPECT_EQ(mock_service.get(), service.get());
2189 mock_service->set_profile(NULL); // Breaks reference cycle.
2190}
2191
2192TEST_F(ManagerTest,
2193 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2194 const string kProfileName0 = "profile0";
2195 scoped_refptr<MockProfile> profile0(
2196 AddNamedMockProfileToManager(manager(), kProfileName0));
2197 const string kProfileName1 = "profile1";
2198 scoped_refptr<MockProfile> profile1(
2199 AddNamedMockProfileToManager(manager(), kProfileName1));
2200
2201 scoped_refptr<MockWiFiService> matching_service(
2202 new StrictMock<MockWiFiService>(control_interface(),
2203 dispatcher(),
2204 metrics(),
2205 manager(),
2206 wifi_provider_,
2207 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002208 kModeManaged,
2209 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002210 false));
2211 matching_service->set_profile(profile1);
2212
2213 // We need to get rid of our reference to this mock service as soon
2214 // as Manager::ConfigureServiceForProfile() takes a reference in its
2215 // call to WiFiProvider::CreateTemporaryService(). This way the
2216 // latter function can keep a DCHECK(service->HasOneRef() even in
2217 // unit tests.
2218 temp_mock_service_ =
2219 new NiceMock<MockWiFiService>(control_interface(),
2220 dispatcher(),
2221 metrics(),
2222 manager(),
2223 wifi_provider_,
2224 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002225 kModeManaged,
2226 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002227 false);
2228
2229 // Only hold a pointer here so we don't affect the refcount.
2230 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2231
2232 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002233 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002234 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2235 .WillOnce(Return(matching_service));
2236 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2237 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2238 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2239 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2240 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2241 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2242 .Times(1);
2243 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2244 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2245
2246 Error error;
2247 ServiceRefPtr service =
2248 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2249 EXPECT_TRUE(error.IsSuccess());
2250 EXPECT_EQ(NULL, service.get());
2251 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2252}
2253
Paul Stewart7a20aa42013-01-17 12:21:41 -08002254TEST_F(ManagerTest, FindMatchingService) {
2255 KeyValueStore args;
2256 {
2257 Error error;
2258 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2259 EXPECT_EQ(Error::kNotFound, error.type());
2260 }
2261
2262 scoped_refptr<MockService> mock_service0(
2263 new NiceMock<MockService>(control_interface(),
2264 dispatcher(),
2265 metrics(),
2266 manager()));
2267 scoped_refptr<MockService> mock_service1(
2268 new NiceMock<MockService>(control_interface(),
2269 dispatcher(),
2270 metrics(),
2271 manager()));
2272 manager()->RegisterService(mock_service0);
2273 manager()->RegisterService(mock_service1);
2274 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2275 .WillOnce(Return(true))
2276 .WillRepeatedly(Return(false));
2277 {
2278 Error error;
2279 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2280 EXPECT_TRUE(error.IsSuccess());
2281 }
2282 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2283 .WillOnce(Return(true))
2284 .WillRepeatedly(Return(false));
2285 {
2286 Error error;
2287 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2288 EXPECT_TRUE(error.IsSuccess());
2289 }
2290 {
2291 Error error;
2292 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2293 EXPECT_EQ(Error::kNotFound, error.type());
2294 }
2295}
2296
Paul Stewart22aa71b2011-09-16 12:15:11 -07002297TEST_F(ManagerTest, TechnologyOrder) {
2298 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002299 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2300 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002301 ASSERT_TRUE(error.IsSuccess());
2302 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002303 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002304
Ben Chan923a5022013-09-20 11:23:23 -07002305 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2306 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002307 ASSERT_FALSE(error.IsSuccess());
2308 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002309 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002310 manager()->GetTechnologyOrder());
2311}
2312
2313TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002314 // TODO(quiche): Some of these tests would probably fit better in
2315 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002316 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002317
mukesh agrawale37ad322013-10-08 16:33:56 -07002318 // Construct our Services so that the string comparison of
2319 // unique_name_ differs from the numerical comparison of
2320 // serial_number_.
2321 vector<scoped_refptr<MockService>> mock_services;
2322 for (size_t i = 0; i < 11; ++i) {
2323 mock_services.push_back(
2324 new NiceMock<MockService>(control_interface(),
2325 dispatcher(),
2326 metrics(),
2327 manager()));
2328 }
2329 scoped_refptr<MockService> mock_service2 = mock_services[2];
2330 scoped_refptr<MockService> mock_service10 = mock_services[10];
2331 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002332
mukesh agrawale37ad322013-10-08 16:33:56 -07002333 manager()->RegisterService(mock_service2);
2334 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002335
mukesh agrawale37ad322013-10-08 16:33:56 -07002336 // Services should already be sorted by |serial_number_|.
2337 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002338
2339 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002340 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002341 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002342
2343 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002344 mock_service10->SetStrength(1);
2345 manager()->UpdateService(mock_service10);
2346 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002347
2348 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002349 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2350 manager()->UpdateService(mock_service2);
2351 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002352
2353 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002354 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002355 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002356 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002357 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002358
2359 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002360 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002361 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002362 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002363
Ben Chan923a5022013-09-20 11:23:23 -07002364 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2365 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002366 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002367 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002368
Gaurav Shah435de2c2011-11-17 19:01:07 -08002369 // Priority.
mukesh agrawale37ad322013-10-08 16:33:56 -07002370 mock_service2->SetPriority(1, NULL);
2371 manager()->UpdateService(mock_service2);
2372 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002373
Paul Stewart2da34c02013-10-17 15:28:56 -07002374 // HasEverConnected.
2375 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002376 manager()->UpdateService(mock_service10);
2377 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002378
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002379 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002380 mock_service2->SetAutoConnect(true);
2381 manager()->UpdateService(mock_service2);
2382 mock_service10->SetAutoConnect(false);
2383 manager()->UpdateService(mock_service10);
2384 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002385
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002386 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2387 // to any of the others below, so we reset to the default state after
2388 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002389 EXPECT_CALL(*mock_service10.get(),
2390 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002391 .WillOnce(Return(true))
2392 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002393 manager()->UpdateService(mock_service10);
2394 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2395 manager()->UpdateService(mock_service2);
2396 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002397
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002398 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002399 mock_service10->SetConnectable(true);
2400 manager()->UpdateService(mock_service10);
2401 mock_service2->SetConnectable(false);
2402 manager()->UpdateService(mock_service2);
2403 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002404
2405 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002406 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002407 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002408 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002409 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002410 manager()->UpdateService(mock_service2);
2411 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002412 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002413 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002414 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002415 manager()->UpdateService(mock_service10);
2416 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002417
2418 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002419 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002420 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002421 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002422 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002423 manager()->UpdateService(mock_service10);
2424 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002425
mukesh agrawale37ad322013-10-08 16:33:56 -07002426 // Connected-but-portalled preferred over unconnected.
2427 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002428 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002429 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002430 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002431 manager()->UpdateService(mock_service2);
2432 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002433
mukesh agrawale37ad322013-10-08 16:33:56 -07002434 // Connected preferred over connected-but-portalled.
2435 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002436 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002437 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002438 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002439 manager()->UpdateService(mock_service10);
2440 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002441
mukesh agrawale37ad322013-10-08 16:33:56 -07002442 manager()->DeregisterService(mock_service2);
2443 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002444}
2445
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002446TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002447 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002448 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002449
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002450 scoped_refptr<MockService> mock_service0(
2451 new NiceMock<MockService>(control_interface(),
2452 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002453 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002454 manager()));
2455 scoped_refptr<MockService> mock_service1(
2456 new NiceMock<MockService>(control_interface(),
2457 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002458 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002459 manager()));
2460
2461 scoped_refptr<MockConnection> mock_connection0(
2462 new NiceMock<MockConnection>(device_info_.get()));
2463 scoped_refptr<MockConnection> mock_connection1(
2464 new NiceMock<MockConnection>(device_info_.get()));
2465
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002466 // A single registered Service, without a connection. The
2467 // DefaultService should be NULL. If a change notification is
2468 // generated, it should reference kNullPath.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002469 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002470 EXPECT_CALL(*manager_adaptor_,
2471 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2472 DBusAdaptor::kNullPath))
2473 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002474 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002475 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002476
2477 // Adding another Service, also without a connection, does not
2478 // change DefaultService. Furthermore, we do not send a change
2479 // notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002480 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002481 EXPECT_CALL(*manager_adaptor_,
2482 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2483 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002484 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002485 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002486
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002487 // An explicit sort doesn't change anything, and does not emit a
2488 // change notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002489 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002490 EXPECT_CALL(*manager_adaptor_,
2491 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2492 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002493 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002494 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002495
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002496 // Re-ordering the unconnected Services doesn't change
2497 // DefaultService, and (hence) does not emit a change notification
2498 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002499 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002500 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002501 EXPECT_CALL(*manager_adaptor_,
2502 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2503 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002504 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002505 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002506
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002507 // Re-ordering the unconnected Services doesn't change
2508 // DefaultService, and (hence) does not emit a change notification
2509 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002510 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002511 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002512 EXPECT_CALL(*manager_adaptor_,
2513 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2514 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002515 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002516 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002517
Paul Stewartce4ec192012-03-14 12:53:46 -07002518 mock_service0->set_mock_connection(mock_connection0);
2519 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002520
mukesh agrawale37ad322013-10-08 16:33:56 -07002521 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002522 // from ServiceOrderIs. We notify others of the change in
2523 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002524 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002525 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002526 EXPECT_CALL(*manager_adaptor_,
2527 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002528 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002529 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002530
Darin Petkova5e07ef2012-07-09 14:27:57 +02002531 ServiceWatcher service_watcher;
2532 int tag =
2533 manager()->RegisterDefaultServiceCallback(
2534 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2535 service_watcher.AsWeakPtr()));
2536 EXPECT_EQ(1, tag);
2537
mukesh agrawale37ad322013-10-08 16:33:56 -07002538 // Changing the ordering causes the DefaultService to change, and
2539 // appropriate notifications are sent.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002540 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002541 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2542 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002543 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002544 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002545 EXPECT_CALL(*manager_adaptor_,
2546 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002547 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002548 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002549
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002550 // Deregistering a DefaultServiceCallback works as expected. (Later
2551 // code causes DefaultService changes, but we see no further calls
2552 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002553 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002554 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002555
2556 // Deregistering the current DefaultService causes the other Service
2557 // to become default. Appropriate notifications are sent.
2558 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002559 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002560 EXPECT_CALL(*manager_adaptor_,
2561 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002562 mock_service1->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002563 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002564 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002565
mukesh agrawale37ad322013-10-08 16:33:56 -07002566 // Deregistering the only Service causes the DefaultService to become
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002567 // NULL. Appropriate notifications are sent.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002568 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002569 EXPECT_CALL(*manager_adaptor_,
2570 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002571 mock_service0->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002572 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002573 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002574
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002575 // An explicit sort doesn't change anything, and does not generate
2576 // an external notification.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002577 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002578 EXPECT_CALL(*manager_adaptor_,
2579 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002580 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002581}
2582
Darin Petkova5e07ef2012-07-09 14:27:57 +02002583TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2584 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2585 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2586
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002587 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002588 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002589
2590 scoped_refptr<MockService> mock_service(
2591 new NiceMock<MockService>(
2592 control_interface(), dispatcher(), metrics(), manager()));
2593 ServiceRefPtr service = mock_service;
2594 ServiceRefPtr null_service;
2595
2596 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2597 manager()->NotifyDefaultServiceChanged(null_service);
2598
2599 ServiceWatcher service_watcher1;
2600 ServiceWatcher service_watcher2;
2601 int tag1 =
2602 manager()->RegisterDefaultServiceCallback(
2603 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2604 service_watcher1.AsWeakPtr()));
2605 EXPECT_EQ(1, tag1);
2606 int tag2 =
2607 manager()->RegisterDefaultServiceCallback(
2608 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2609 service_watcher2.AsWeakPtr()));
2610 EXPECT_EQ(2, tag2);
2611
2612 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2613 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2614 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2615 manager()->NotifyDefaultServiceChanged(null_service);
2616
2617 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2618 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2619 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2620 manager()->NotifyDefaultServiceChanged(mock_service);
2621
2622 manager()->DeregisterDefaultServiceCallback(tag1);
2623 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2624 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2625 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2626 manager()->NotifyDefaultServiceChanged(mock_service);
2627 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2628
2629 manager()->DeregisterDefaultServiceCallback(tag2);
2630 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2631 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2632 manager()->NotifyDefaultServiceChanged(mock_service);
2633
2634 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2635 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2636}
2637
Peter Qiu9d581932014-04-14 16:37:37 -07002638TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2639 int connection_id1 = 100;
2640 int connection_id2 = 200;
2641 scoped_refptr<MockService> mock_service1 =
2642 new NiceMock<MockService>(control_interface(), dispatcher(),
2643 metrics(), manager());
2644 mock_service1->set_connection_id(connection_id1);
2645 scoped_refptr<MockService> mock_service2 =
2646 new NiceMock<MockService>(control_interface(), dispatcher(),
2647 metrics(), manager());
2648 mock_service2->set_connection_id(connection_id1);
2649 scoped_refptr<MockService> mock_service3 =
2650 new NiceMock<MockService>(control_interface(), dispatcher(),
2651 metrics(), manager());
2652 mock_service3->set_connection_id(connection_id2);
2653
2654 manager()->RegisterService(mock_service1);
2655 manager()->RegisterService(mock_service2);
2656 manager()->RegisterService(mock_service3);
2657
2658 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2659 manager()->ReportServicesOnSameNetwork(connection_id1);
2660
2661 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2662 manager()->ReportServicesOnSameNetwork(connection_id2);
2663}
2664
Gaurav Shah435de2c2011-11-17 19:01:07 -08002665TEST_F(ManagerTest, AvailableTechnologies) {
2666 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2667 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002668 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002669 manager(),
2670 "null4",
2671 "addr4",
2672 0));
2673 manager()->RegisterDevice(mock_devices_[0]);
2674 manager()->RegisterDevice(mock_devices_[1]);
2675 manager()->RegisterDevice(mock_devices_[2]);
2676 manager()->RegisterDevice(mock_devices_[3]);
2677
2678 ON_CALL(*mock_devices_[0].get(), technology())
2679 .WillByDefault(Return(Technology::kEthernet));
2680 ON_CALL(*mock_devices_[1].get(), technology())
2681 .WillByDefault(Return(Technology::kWifi));
2682 ON_CALL(*mock_devices_[2].get(), technology())
2683 .WillByDefault(Return(Technology::kCellular));
2684 ON_CALL(*mock_devices_[3].get(), technology())
2685 .WillByDefault(Return(Technology::kWifi));
2686
2687 set<string> expected_technologies;
2688 expected_technologies.insert(Technology::NameFromIdentifier(
2689 Technology::kEthernet));
2690 expected_technologies.insert(Technology::NameFromIdentifier(
2691 Technology::kWifi));
2692 expected_technologies.insert(Technology::NameFromIdentifier(
2693 Technology::kCellular));
2694 Error error;
2695 vector<string> technologies = manager()->AvailableTechnologies(&error);
2696
2697 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2698 ContainerEq(expected_technologies));
2699}
2700
2701TEST_F(ManagerTest, ConnectedTechnologies) {
2702 scoped_refptr<MockService> connected_service1(
2703 new NiceMock<MockService>(control_interface(),
2704 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002705 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002706 manager()));
2707 scoped_refptr<MockService> connected_service2(
2708 new NiceMock<MockService>(control_interface(),
2709 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002710 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002711 manager()));
2712 scoped_refptr<MockService> disconnected_service1(
2713 new NiceMock<MockService>(control_interface(),
2714 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002715 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002716 manager()));
2717 scoped_refptr<MockService> disconnected_service2(
2718 new NiceMock<MockService>(control_interface(),
2719 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002720 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002721 manager()));
2722
2723 ON_CALL(*connected_service1.get(), IsConnected())
2724 .WillByDefault(Return(true));
2725 ON_CALL(*connected_service2.get(), IsConnected())
2726 .WillByDefault(Return(true));
2727
2728 manager()->RegisterService(connected_service1);
2729 manager()->RegisterService(connected_service2);
2730 manager()->RegisterService(disconnected_service1);
2731 manager()->RegisterService(disconnected_service2);
2732
2733 manager()->RegisterDevice(mock_devices_[0]);
2734 manager()->RegisterDevice(mock_devices_[1]);
2735 manager()->RegisterDevice(mock_devices_[2]);
2736 manager()->RegisterDevice(mock_devices_[3]);
2737
2738 ON_CALL(*mock_devices_[0].get(), technology())
2739 .WillByDefault(Return(Technology::kEthernet));
2740 ON_CALL(*mock_devices_[1].get(), technology())
2741 .WillByDefault(Return(Technology::kWifi));
2742 ON_CALL(*mock_devices_[2].get(), technology())
2743 .WillByDefault(Return(Technology::kCellular));
2744 ON_CALL(*mock_devices_[3].get(), technology())
2745 .WillByDefault(Return(Technology::kWifi));
2746
2747 mock_devices_[0]->SelectService(connected_service1);
2748 mock_devices_[1]->SelectService(disconnected_service1);
2749 mock_devices_[2]->SelectService(disconnected_service2);
2750 mock_devices_[3]->SelectService(connected_service2);
2751
2752 set<string> expected_technologies;
2753 expected_technologies.insert(Technology::NameFromIdentifier(
2754 Technology::kEthernet));
2755 expected_technologies.insert(Technology::NameFromIdentifier(
2756 Technology::kWifi));
2757 Error error;
2758
2759 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2760 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2761 ContainerEq(expected_technologies));
2762}
2763
2764TEST_F(ManagerTest, DefaultTechnology) {
2765 scoped_refptr<MockService> connected_service(
2766 new NiceMock<MockService>(control_interface(),
2767 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002768 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002769 manager()));
2770 scoped_refptr<MockService> disconnected_service(
2771 new NiceMock<MockService>(control_interface(),
2772 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002773 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002774 manager()));
2775
2776 // Connected. WiFi.
2777 ON_CALL(*connected_service.get(), IsConnected())
2778 .WillByDefault(Return(true));
2779 ON_CALL(*connected_service.get(), state())
2780 .WillByDefault(Return(Service::kStateConnected));
2781 ON_CALL(*connected_service.get(), technology())
2782 .WillByDefault(Return(Technology::kWifi));
2783
2784 // Disconnected. Ethernet.
2785 ON_CALL(*disconnected_service.get(), technology())
2786 .WillByDefault(Return(Technology::kEthernet));
2787
2788 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002789 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002790 Error error;
2791 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2792
2793
2794 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002795 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002796 // Connected service should be brought to the front now.
2797 string expected_technology =
2798 Technology::NameFromIdentifier(Technology::kWifi);
2799 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2800}
2801
Paul Stewart212d60f2012-07-12 10:59:13 -07002802TEST_F(ManagerTest, Stop) {
2803 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002804 new NiceMock<MockProfile>(
2805 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002806 AdoptProfile(manager(), profile);
2807 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002808 new NiceMock<MockService>(control_interface(),
2809 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002810 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002811 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002812 manager()->RegisterService(service);
2813 manager()->RegisterDevice(mock_devices_[0]);
2814 EXPECT_CALL(*profile.get(),
2815 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2816 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002817 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002818 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2819 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002820 manager()->Stop();
2821}
2822
mukesh agrawal00917ce2011-11-22 23:56:55 +00002823TEST_F(ManagerTest, UpdateServiceConnected) {
2824 scoped_refptr<MockService> mock_service(
2825 new NiceMock<MockService>(control_interface(),
2826 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002827 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002828 manager()));
2829 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002830 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002831 EXPECT_FALSE(mock_service->auto_connect());
2832
Gaurav Shah435de2c2011-11-17 19:01:07 -08002833 EXPECT_CALL(*mock_service.get(), IsConnected())
2834 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002835 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002836 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2837 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2838 // would break the SortServices test. (crbug.com/206367)
2839 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002840 EXPECT_TRUE(mock_service->auto_connect());
2841}
2842
Paul Stewart2da34c02013-10-17 15:28:56 -07002843TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002844 // This tests the case where the user connects to a service that is
2845 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002846 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002847 scoped_refptr<MockService> mock_service(
2848 new NiceMock<MockService>(control_interface(),
2849 dispatcher(),
2850 metrics(),
2851 manager()));
2852 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002853 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002854 EXPECT_FALSE(mock_service->auto_connect());
2855
Gary Moraind93615e2012-04-27 11:50:03 -07002856 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002857 new MockProfile(
2858 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002859
Gary Moraind93615e2012-04-27 11:50:03 -07002860 mock_service->set_profile(profile);
2861 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002862 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002863 EXPECT_CALL(*profile,
2864 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002865 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002866 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2867 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2868 // would break the SortServices test. (crbug.com/206367)
2869 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002870 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002871 // This releases the ref on the mock profile.
2872 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002873}
2874
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002875TEST_F(ManagerTest, SaveSuccessfulService) {
2876 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002877 new StrictMock<MockProfile>(
2878 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002879 AdoptProfile(manager(), profile);
2880 scoped_refptr<MockService> service(
2881 new NiceMock<MockService>(control_interface(),
2882 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002883 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002884 manager()));
2885
2886 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2887 ServiceRefPtr expect_service(service.get());
2888
2889 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2890 .WillOnce(Return(false));
2891 manager()->RegisterService(service);
2892
2893 EXPECT_CALL(*service.get(), state())
2894 .WillRepeatedly(Return(Service::kStateConnected));
2895 EXPECT_CALL(*service.get(), IsConnected())
2896 .WillRepeatedly(Return(true));
2897 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2898 .WillOnce(Return(true));
2899 manager()->UpdateService(service);
2900}
2901
Darin Petkove7c6ad32012-06-29 10:22:09 +02002902TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002903 MockProfile *profile0 =
2904 new MockProfile(control_interface(), metrics(), manager(), "");
2905 MockProfile *profile1 =
2906 new MockProfile(control_interface(), metrics(), manager(), "");
2907 MockProfile *profile2 =
2908 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002909 AdoptProfile(manager(), profile0); // Passes ownership.
2910 AdoptProfile(manager(), profile1); // Passes ownership.
2911 AdoptProfile(manager(), profile2); // Passes ownership.
2912 DeviceRefPtr device_ref(mock_devices_[0].get());
2913 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2914 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2915 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2916 manager()->UpdateDevice(mock_devices_[0]);
2917}
2918
Paul Stewart1b253142012-01-26 14:05:52 -08002919TEST_F(ManagerTest, EnumerateProfiles) {
2920 vector<string> profile_paths;
2921 for (size_t i = 0; i < 10; i++) {
2922 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002923 new StrictMock<MockProfile>(
2924 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002925 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002926 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2927 .WillOnce(Return(profile_paths.back()));
2928 AdoptProfile(manager(), profile);
2929 }
2930
2931 Error error;
2932 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2933 EXPECT_TRUE(error.IsSuccess());
2934 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2935 for (size_t i = 0; i < profile_paths.size(); i++) {
2936 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2937 }
2938}
2939
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002940TEST_F(ManagerTest, AutoConnectOnRegister) {
2941 MockServiceRefPtr service = MakeAutoConnectableService();
2942 EXPECT_CALL(*service.get(), AutoConnect());
2943 manager()->RegisterService(service);
2944 dispatcher()->DispatchPendingEvents();
2945}
2946
2947TEST_F(ManagerTest, AutoConnectOnUpdate) {
2948 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002949 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002950 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002951 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002952 manager()->RegisterService(service1);
2953 manager()->RegisterService(service2);
2954 dispatcher()->DispatchPendingEvents();
2955
2956 EXPECT_CALL(*service1.get(), AutoConnect());
2957 EXPECT_CALL(*service2.get(), state())
2958 .WillRepeatedly(Return(Service::kStateFailure));
2959 EXPECT_CALL(*service2.get(), IsFailed())
2960 .WillRepeatedly(Return(true));
2961 EXPECT_CALL(*service2.get(), IsConnected())
2962 .WillRepeatedly(Return(false));
2963 manager()->UpdateService(service2);
2964 dispatcher()->DispatchPendingEvents();
2965}
2966
2967TEST_F(ManagerTest, AutoConnectOnDeregister) {
2968 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002969 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002970 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002971 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002972 manager()->RegisterService(service1);
2973 manager()->RegisterService(service2);
2974 dispatcher()->DispatchPendingEvents();
2975
2976 EXPECT_CALL(*service1.get(), AutoConnect());
2977 manager()->DeregisterService(service2);
2978 dispatcher()->DispatchPendingEvents();
2979}
2980
Daniel Eratfac09532014-04-17 20:25:59 -07002981TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02002982 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002983 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02002984 SetPowerManager();
2985 EXPECT_CALL(*service, AutoConnect()).Times(0);
2986 manager()->RegisterService(service);
2987 dispatcher()->DispatchPendingEvents();
2988}
2989
Daniel Eratfac09532014-04-17 20:25:59 -07002990TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02002991 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002992 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02002993 SetPowerManager();
2994 EXPECT_CALL(*service, AutoConnect());
2995 manager()->RegisterService(service);
2996 dispatcher()->DispatchPendingEvents();
2997}
2998
Paul Stewart63864b62012-11-07 15:10:55 -08002999TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
3000 SetRunning(false);
3001 MockServiceRefPtr service = MakeAutoConnectableService();
3002 EXPECT_CALL(*service, AutoConnect()).Times(0);
3003 manager()->RegisterService(service);
3004 dispatcher()->DispatchPendingEvents();
3005}
3006
Daniel Eratfac09532014-04-17 20:25:59 -07003007TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02003008 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02003009 SetPowerManager();
3010 EXPECT_CALL(*service, AutoConnect());
3011 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07003012 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003013 dispatcher()->DispatchPendingEvents();
3014
Daniel Eratfac09532014-04-17 20:25:59 -07003015 const int kSuspendId = 1;
3016 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
3017 OnSuspendImminent(kSuspendId);
3018 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02003019 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003020 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003021
Daniel Eratfac09532014-04-17 20:25:59 -07003022 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
3023 OnSuspendDone(kSuspendId);
3024 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02003025 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003026 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003027}
3028
Darin Petkov3ec55342012-09-28 14:04:44 +02003029TEST_F(ManagerTest, AddTerminationAction) {
Daniel Eratfac09532014-04-17 20:25:59 -07003030 EXPECT_CALL(*power_manager_, AddSuspendDelay(_, _, _, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02003031 SetPowerManager();
3032 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3033 manager()->AddTerminationAction("action1", base::Closure());
3034 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3035 manager()->AddTerminationAction("action2", base::Closure());
3036}
3037
3038TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003039 const char kKey1[] = "action1";
3040 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02003041
3042 MockPowerManager &power_manager = *power_manager_;
3043 SetPowerManager();
3044
3045 // Removing an action when the hook table is empty should not result in any
3046 // calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003047 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003048 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3049 manager()->RemoveTerminationAction("unknown");
3050 Mock::VerifyAndClearExpectations(&power_manager);
3051
Daniel Eratfac09532014-04-17 20:25:59 -07003052 EXPECT_CALL(power_manager, AddSuspendDelay(_, _, _, _, _))
3053 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003054 manager()->AddTerminationAction(kKey1, base::Closure());
3055 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3056 manager()->AddTerminationAction(kKey2, base::Closure());
Daniel Eratfac09532014-04-17 20:25:59 -07003057 Mock::VerifyAndClearExpectations(&power_manager);
Darin Petkov3ec55342012-09-28 14:04:44 +02003058
3059 // Removing an action that ends up with a non-empty hook table should not
3060 // result in any calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003061 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003062 manager()->RemoveTerminationAction(kKey1);
3063 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3064 Mock::VerifyAndClearExpectations(&power_manager);
3065
3066 // Removing the last action should trigger unregistering from the power
3067 // manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003068 EXPECT_CALL(power_manager, RemoveSuspendDelay(_))
Daniel Erat0818cca2012-12-14 10:16:21 -08003069 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003070 manager()->RemoveTerminationAction(kKey2);
3071 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3072}
3073
3074TEST_F(ManagerTest, RunTerminationActions) {
3075 TerminationActionTest test_action;
3076 const string kActionName = "action";
3077
3078 EXPECT_CALL(test_action, Done(_));
3079 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3080 test_action.AsWeakPtr()));
3081
3082 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3083 Bind(&TerminationActionTest::Action,
3084 test_action.AsWeakPtr()));
3085 test_action.set_manager(manager());
3086 EXPECT_CALL(test_action, Done(_));
3087 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3088 test_action.AsWeakPtr()));
3089}
3090
Daniel Erat0818cca2012-12-14 10:16:21 -08003091TEST_F(ManagerTest, OnSuspendImminent) {
3092 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02003093 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Eratfac09532014-04-17 20:25:59 -07003094 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003095 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003096 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02003097}
3098
3099TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003100 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02003101 Error error;
Daniel Eratfac09532014-04-17 20:25:59 -07003102 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003103 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003104 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003105}
3106
Paul Stewartc681fa02012-03-02 19:40:04 -08003107TEST_F(ManagerTest, RecheckPortal) {
3108 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3109 .WillOnce(Return(false));
3110 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3111 .WillOnce(Return(true));
3112 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3113 .Times(0);
3114
3115 manager()->RegisterDevice(mock_devices_[0]);
3116 manager()->RegisterDevice(mock_devices_[1]);
3117 manager()->RegisterDevice(mock_devices_[2]);
3118
3119 manager()->RecheckPortal(NULL);
3120}
3121
Paul Stewartd215af62012-04-24 23:25:50 -07003122TEST_F(ManagerTest, RecheckPortalOnService) {
3123 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3124 dispatcher(),
3125 metrics(),
3126 manager());
3127 EXPECT_CALL(*mock_devices_[0].get(),
3128 IsConnectedToService(IsRefPtrTo(service)))
3129 .WillOnce(Return(false));
3130 EXPECT_CALL(*mock_devices_[1].get(),
3131 IsConnectedToService(IsRefPtrTo(service)))
3132 .WillOnce(Return(true));
3133 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3134 .WillOnce(Return(true));
3135 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3136 .Times(0);
3137
3138 manager()->RegisterDevice(mock_devices_[0]);
3139 manager()->RegisterDevice(mock_devices_[1]);
3140 manager()->RegisterDevice(mock_devices_[2]);
3141
3142 manager()->RecheckPortalOnService(service);
3143}
3144
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003145TEST_F(ManagerTest, GetDefaultService) {
3146 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003147 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003148
3149 scoped_refptr<MockService> mock_service(
3150 new NiceMock<MockService>(control_interface(),
3151 dispatcher(),
3152 metrics(),
3153 manager()));
3154
3155 manager()->RegisterService(mock_service);
3156 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003157 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003158
3159 scoped_refptr<MockConnection> mock_connection(
3160 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003161 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003162 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003163 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003164
Paul Stewartce4ec192012-03-14 12:53:46 -07003165 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003166 manager()->DeregisterService(mock_service);
3167}
3168
Paul Stewart13ed2252012-03-21 12:52:46 -07003169TEST_F(ManagerTest, GetServiceWithGUID) {
3170 scoped_refptr<MockService> mock_service0(
3171 new NiceMock<MockService>(control_interface(),
3172 dispatcher(),
3173 metrics(),
3174 manager()));
3175
3176 scoped_refptr<MockService> mock_service1(
3177 new NiceMock<MockService>(control_interface(),
3178 dispatcher(),
3179 metrics(),
3180 manager()));
3181
Paul Stewartcb59fed2012-03-21 21:14:46 -07003182 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3183 .Times(0);
3184 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3185 .Times(0);
3186
Paul Stewart13ed2252012-03-21 12:52:46 -07003187 manager()->RegisterService(mock_service0);
3188 manager()->RegisterService(mock_service1);
3189
3190 const string kGUID0 = "GUID0";
3191 const string kGUID1 = "GUID1";
3192
3193 {
3194 Error error;
3195 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3196 EXPECT_FALSE(error.IsSuccess());
3197 EXPECT_FALSE(service);
3198 }
3199
3200 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003201 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003202
3203 {
3204 Error error;
3205 ServiceRefPtr service = manager()->GetService(args, &error);
3206 EXPECT_EQ(Error::kInvalidArguments, error.type());
3207 EXPECT_FALSE(service);
3208 }
3209
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003210 mock_service0->SetGuid(kGUID0, NULL);
3211 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003212
3213 {
3214 Error error;
3215 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3216 EXPECT_TRUE(error.IsSuccess());
3217 EXPECT_EQ(mock_service0.get(), service.get());
3218 }
3219
3220 {
3221 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003222 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3223 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003224 ServiceRefPtr service = manager()->GetService(args, &error);
3225 EXPECT_TRUE(error.IsSuccess());
3226 EXPECT_EQ(mock_service1.get(), service.get());
3227 }
3228
3229 manager()->DeregisterService(mock_service0);
3230 manager()->DeregisterService(mock_service1);
3231}
3232
Gary Morain028545d2012-04-07 14:55:52 -07003233
3234TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003235 EXPECT_FALSE(manager()->IsOnline());
3236 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3237
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003238 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003239 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003240 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3241 .Times(AnyNumber());
3242 scoped_refptr<MockService> mock_service0(
3243 new NiceMock<MockService>(control_interface(),
3244 dispatcher(),
3245 metrics(),
3246 manager()));
3247
3248 scoped_refptr<MockService> mock_service1(
3249 new NiceMock<MockService>(control_interface(),
3250 dispatcher(),
3251 metrics(),
3252 manager()));
3253
3254 EXPECT_CALL(*mock_service0.get(), IsConnected())
3255 .WillRepeatedly(Return(false));
3256 EXPECT_CALL(*mock_service1.get(), IsConnected())
3257 .WillRepeatedly(Return(false));
3258
3259 manager()->RegisterService(mock_service0);
3260 manager()->RegisterService(mock_service1);
3261
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003262 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003263 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3264
3265 manager()->DeregisterService(mock_service0);
3266 manager()->DeregisterService(mock_service1);
3267}
3268
3269TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003270 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003271 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003272 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3273 .Times(AnyNumber());
3274 scoped_refptr<MockService> mock_service0(
3275 new NiceMock<MockService>(control_interface(),
3276 dispatcher(),
3277 metrics(),
3278 manager()));
3279
3280 scoped_refptr<MockService> mock_service1(
3281 new NiceMock<MockService>(control_interface(),
3282 dispatcher(),
3283 metrics(),
3284 manager()));
3285
3286 EXPECT_CALL(*mock_service0.get(), IsConnected())
3287 .WillRepeatedly(Return(false));
3288 EXPECT_CALL(*mock_service1.get(), IsConnected())
3289 .WillRepeatedly(Return(true));
3290 EXPECT_CALL(*mock_service0.get(), state())
3291 .WillRepeatedly(Return(Service::kStateIdle));
3292 EXPECT_CALL(*mock_service1.get(), state())
3293 .WillRepeatedly(Return(Service::kStateConnected));
3294
3295 manager()->RegisterService(mock_service0);
3296 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003297 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003298
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003299 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003300 EXPECT_EQ("online", manager()->CalculateState(NULL));
3301
3302 manager()->DeregisterService(mock_service0);
3303 manager()->DeregisterService(mock_service1);
3304}
3305
Paul Stewart03e29f72013-09-26 00:49:48 -07003306TEST_F(ManagerTest, RefreshConnectionState) {
3307 EXPECT_CALL(*manager_adaptor_,
3308 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3309 RefreshConnectionState();
3310 Mock::VerifyAndClearExpectations(manager_adaptor_);
3311
3312 scoped_refptr<MockService> mock_service(
3313 new NiceMock<MockService>(control_interface(),
3314 dispatcher(),
3315 metrics(),
3316 manager()));
3317 EXPECT_CALL(*manager_adaptor_,
3318 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3319 manager()->RegisterService(mock_service);
3320 RefreshConnectionState();
3321
3322 scoped_refptr<MockConnection> mock_connection(
3323 new NiceMock<MockConnection>(device_info_.get()));
3324 mock_service->set_mock_connection(mock_connection);
3325 EXPECT_CALL(*mock_service, state())
3326 .WillOnce(Return(Service::kStateIdle));
3327 RefreshConnectionState();
3328
3329 Mock::VerifyAndClearExpectations(manager_adaptor_);
3330 EXPECT_CALL(*mock_service, state())
3331 .WillOnce(Return(Service::kStatePortal));
3332 EXPECT_CALL(*manager_adaptor_,
3333 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3334 RefreshConnectionState();
3335 Mock::VerifyAndClearExpectations(manager_adaptor_);
3336
3337 mock_service->set_mock_connection(NULL);
3338 manager()->DeregisterService(mock_service);
3339}
3340
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003341TEST_F(ManagerTest, StartupPortalList) {
3342 // Simulate loading value from the default profile.
3343 const string kProfileValue("wifi,vpn");
3344 manager()->props_.check_portal_list = kProfileValue;
3345
3346 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3347 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3348 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3349
3350 const string kStartupValue("cellular,ethernet");
3351 manager()->SetStartupPortalList(kStartupValue);
3352 // Ensure profile value is not overwritten, so when we save the default
3353 // profile, the correct value will still be written.
3354 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3355
3356 // However we should read back a different list.
3357 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3358 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3359 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3360
3361 const string kRuntimeValue("ppp");
3362 // Setting a runtime value over the control API should overwrite both
3363 // the profile value and what we read back.
3364 Error error;
3365 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003366 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003367 kRuntimeValue,
3368 &error);
3369 ASSERT_TRUE(error.IsSuccess());
3370 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3371 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3372 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3373 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3374}
3375
Paul Stewart036dba02012-08-07 12:34:41 -07003376TEST_F(ManagerTest, LinkMonitorEnabled) {
3377 const string kEnabledTechnologies("wifi,vpn");
3378 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3379 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3380 EXPECT_FALSE(
3381 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3382}
3383
Paul Stewart85aea152013-01-22 09:31:56 -08003384TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003385 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003386 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003387 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003388 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003389 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003390 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3391 AdoptProfile(manager(), profile);
3392 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3393 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3394 scoped_ptr<MockStore> store1(new MockStore);
3395 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3396}
3397
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003398TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003399 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003400 DisableTechnologyReplyHandler disable_technology_reply_handler;
3401 ResultCallback disable_technology_callback(
3402 Bind(&DisableTechnologyReplyHandler::ReportResult,
3403 disable_technology_reply_handler.AsWeakPtr()));
3404 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3405
Ben Chan923a5022013-09-20 11:23:23 -07003406 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003407 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003408 EXPECT_TRUE(error.IsSuccess());
3409
Joshua Krollda798622012-06-05 12:30:48 -07003410 ON_CALL(*mock_devices_[0], technology())
3411 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003412 ON_CALL(*mock_devices_[1], technology())
3413 .WillByDefault(Return(Technology::kCellular));
3414 ON_CALL(*mock_devices_[2], technology())
3415 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003416
3417 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003418 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003419
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003420 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003421 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3422 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003423 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003424 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003425 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003426 EXPECT_TRUE(error.IsSuccess());
3427
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003428 // Ethernet Device is enabled, and mock doesn't change error from
3429 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003430 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3431 mock_devices_[0]->enabled_ = true;
3432 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003433 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003434 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003435 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003436
3437 // Ethernet Device is disabled, and mock doesn't change error from
3438 // kOperationInitiated, so expect enable to say operation in progress.
3439 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3440 mock_devices_[0]->enabled_ = false;
3441 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003442 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003443 &error, disable_technology_callback);
3444 EXPECT_TRUE(error.IsOngoing());
3445
3446 // Cellular Device is enabled, but disable failed.
3447 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3448 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3449 mock_devices_[1]->enabled_ = true;
3450 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003451 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003452 &error, disable_technology_callback);
3453 EXPECT_EQ(Error::kPermissionDenied, error.type());
3454
3455 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3456 // if one is in progress (even if the other completed immediately).
3457 manager()->RegisterDevice(mock_devices_[2]);
3458 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3459 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3460 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3461 mock_devices_[1]->enabled_ = true;
3462 mock_devices_[2]->enabled_ = true;
3463 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003464 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003465 &error, disable_technology_callback);
3466 EXPECT_TRUE(error.IsOngoing());
3467
3468 // ...and order doesn't matter.
3469 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3470 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3471 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3472 mock_devices_[1]->enabled_ = true;
3473 mock_devices_[2]->enabled_ = true;
3474 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003475 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003476 &error, disable_technology_callback);
3477 EXPECT_TRUE(error.IsOngoing());
3478 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3479
3480 // Multiple Cellular Devices in enabled state. Even if all disable
3481 // operations complete asynchronously, we only get one call to the
3482 // DisableTechnologyReplyHandler::ReportResult.
3483 ResultCallback device1_result_callback;
3484 ResultCallback device2_result_callback;
3485 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3486 .WillOnce(SaveArg<2>(&device1_result_callback));
3487 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3488 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3489 SaveArg<2>(&device2_result_callback)));
3490 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3491 mock_devices_[1]->enabled_ = true;
3492 mock_devices_[2]->enabled_ = true;
3493 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003494 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003495 &error, disable_technology_callback);
3496 EXPECT_TRUE(error.IsOngoing());
3497 device1_result_callback.Run(Error(Error::kSuccess));
3498 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003499}
3500
Paul Stewart4d5efb72012-09-17 12:24:34 -07003501TEST_F(ManagerTest, IgnoredSearchList) {
3502 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003503 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003504 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003505
3506 const string kIgnored0 = "chromium.org";
3507 ignored_paths.push_back(kIgnored0);
3508 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003509 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003510 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3511
3512 const string kIgnored1 = "google.com";
3513 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3514 ignored_paths.push_back(kIgnored1);
3515 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003516 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003517 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3518
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003519 ignored_paths.clear();
3520 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3521 SetIgnoredDNSSearchPaths("", NULL);
3522 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3523
Paul Stewart4d5efb72012-09-17 12:24:34 -07003524 SetResolver(Resolver::GetInstance());
3525}
3526
Paul Stewartbfb82552012-10-24 16:48:48 -07003527TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3528 // Test to make sure that every service state-change causes the
3529 // Manager to emit a new service list.
3530 scoped_refptr<MockService> mock_service(
3531 new NiceMock<MockService>(control_interface(),
3532 dispatcher(),
3533 metrics(),
3534 manager()));
3535 EXPECT_CALL(*mock_service, state())
3536 .WillRepeatedly(Return(Service::kStateIdle));
3537
3538 manager()->RegisterService(mock_service);
3539 EXPECT_CALL(
3540 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003541 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003542 EXPECT_CALL(
3543 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003544 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003545 CompleteServiceSort();
3546
3547 Mock::VerifyAndClearExpectations(manager_adaptor_);
3548 EXPECT_CALL(
3549 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003550 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003551 EXPECT_CALL(
3552 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003553 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003554 manager()->UpdateService(mock_service.get());
3555 CompleteServiceSort();
3556
3557 manager()->DeregisterService(mock_service);
3558}
3559
3560TEST_F(ManagerTest, EnumerateServices) {
3561 scoped_refptr<MockService> mock_service(
3562 new NiceMock<MockService>(control_interface(),
3563 dispatcher(),
3564 metrics(),
3565 manager()));
3566 manager()->RegisterService(mock_service);
3567
3568 EXPECT_CALL(*mock_service, state())
3569 .WillRepeatedly(Return(Service::kStateConnected));
3570 EXPECT_CALL(*mock_service, IsVisible())
3571 .WillRepeatedly(Return(false));
3572 EXPECT_TRUE(EnumerateAvailableServices().empty());
3573 EXPECT_TRUE(EnumerateWatchedServices().empty());
3574
3575 EXPECT_CALL(*mock_service, state())
3576 .WillRepeatedly(Return(Service::kStateIdle));
3577 EXPECT_TRUE(EnumerateAvailableServices().empty());
3578 EXPECT_TRUE(EnumerateWatchedServices().empty());
3579
3580 EXPECT_CALL(*mock_service, IsVisible())
3581 .WillRepeatedly(Return(true));
3582 Service::ConnectState unwatched_states[] = {
3583 Service::kStateUnknown,
3584 Service::kStateIdle,
3585 Service::kStateFailure
3586 };
3587 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3588 EXPECT_CALL(*mock_service, state())
3589 .WillRepeatedly(Return(unwatched_states[i]));
3590 EXPECT_FALSE(EnumerateAvailableServices().empty());
3591 EXPECT_TRUE(EnumerateWatchedServices().empty());
3592 }
3593
3594 Service::ConnectState watched_states[] = {
3595 Service::kStateAssociating,
3596 Service::kStateConfiguring,
3597 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003598 Service::kStatePortal,
3599 Service::kStateOnline
3600 };
3601 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3602 EXPECT_CALL(*mock_service, state())
3603 .WillRepeatedly(Return(watched_states[i]));
3604 EXPECT_FALSE(EnumerateAvailableServices().empty());
3605 EXPECT_FALSE(EnumerateWatchedServices().empty());
3606 }
3607
3608 manager()->DeregisterService(mock_service);
3609}
3610
Paul Stewart39db5ca2013-03-18 14:15:17 -07003611TEST_F(ManagerTest, ConnectToBestServices) {
3612 scoped_refptr<MockService> wifi_service0(
3613 new NiceMock<MockService>(control_interface(),
3614 dispatcher(),
3615 metrics(),
3616 manager()));
3617 EXPECT_CALL(*wifi_service0.get(), state())
3618 .WillRepeatedly(Return(Service::kStateIdle));
3619 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3620 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003621 wifi_service0->SetConnectable(true);
3622 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003623 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3624 EXPECT_CALL(*wifi_service0.get(), technology())
3625 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003626 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3627 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003628 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3629 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003630
3631 scoped_refptr<MockService> wifi_service1(
3632 new NiceMock<MockService>(control_interface(),
3633 dispatcher(),
3634 metrics(),
3635 manager()));
3636 EXPECT_CALL(*wifi_service1.get(), state())
3637 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003638 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3639 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003640 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3641 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003642 wifi_service1->SetAutoConnect(true);
3643 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003644 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3645 EXPECT_CALL(*wifi_service1.get(), technology())
3646 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003647 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3648 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003649
3650 scoped_refptr<MockService> wifi_service2(
3651 new NiceMock<MockService>(control_interface(),
3652 dispatcher(),
3653 metrics(),
3654 manager()));
3655 EXPECT_CALL(*wifi_service2.get(), state())
3656 .WillRepeatedly(Return(Service::kStateConnected));
3657 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3658 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003659 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3660 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003661 wifi_service2->SetAutoConnect(true);
3662 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003663 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3664 EXPECT_CALL(*wifi_service2.get(), technology())
3665 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003666 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3667 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003668
3669 manager()->RegisterService(wifi_service0);
3670 manager()->RegisterService(wifi_service1);
3671 manager()->RegisterService(wifi_service2);
3672
3673 CompleteServiceSort();
3674 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3675
3676 scoped_refptr<MockService> cell_service(
3677 new NiceMock<MockService>(control_interface(),
3678 dispatcher(),
3679 metrics(),
3680 manager()));
3681
3682 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003683 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003684 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003685 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003686 EXPECT_CALL(*cell_service.get(), IsVisible())
3687 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003688 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003689 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003690 EXPECT_CALL(*cell_service.get(), technology())
3691 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003692 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3693 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003694 manager()->RegisterService(cell_service);
3695
Arman Uguray6fe4f262013-08-02 20:21:55 -07003696 scoped_refptr<MockService> wimax_service(
3697 new NiceMock<MockService>(control_interface(),
3698 dispatcher(),
3699 metrics(),
3700 manager()));
3701
3702 EXPECT_CALL(*wimax_service.get(), state())
3703 .WillRepeatedly(Return(Service::kStateConnected));
3704 EXPECT_CALL(*wimax_service.get(), IsConnected())
3705 .WillRepeatedly(Return(true));
3706 EXPECT_CALL(*wimax_service.get(), IsVisible())
3707 .WillRepeatedly(Return(true));
3708 wimax_service->SetAutoConnect(true);
3709 wimax_service->SetConnectable(true);
3710 EXPECT_CALL(*wimax_service.get(), technology())
3711 .WillRepeatedly(Return(Technology::kWiMax));
3712 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3713 .WillRepeatedly(Return(false));
3714 manager()->RegisterService(wimax_service);
3715
Paul Stewart39db5ca2013-03-18 14:15:17 -07003716 scoped_refptr<MockService> vpn_service(
3717 new NiceMock<MockService>(control_interface(),
3718 dispatcher(),
3719 metrics(),
3720 manager()));
3721
3722 EXPECT_CALL(*vpn_service.get(), state())
3723 .WillRepeatedly(Return(Service::kStateIdle));
3724 EXPECT_CALL(*vpn_service.get(), IsConnected())
3725 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003726 EXPECT_CALL(*vpn_service.get(), IsVisible())
3727 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003728 wifi_service2->SetAutoConnect(false);
3729 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003730 EXPECT_CALL(*vpn_service.get(), technology())
3731 .WillRepeatedly(Return(Technology::kVPN));
3732 manager()->RegisterService(vpn_service);
3733
3734 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003735 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003736
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003737 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3738 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003739 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003740 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3741 .Times(0); // Explicitly disconnected.
3742 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003743 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003744
3745 manager()->ConnectToBestServices(NULL);
3746 dispatcher()->DispatchPendingEvents();
3747
3748 // After this operation, since the Connect calls above are mocked and
3749 // no actual state changes have occurred, we should expect that the
3750 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003751 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003752}
3753
Christopher Wiley83889862013-05-02 15:55:09 -07003754TEST_F(ManagerTest, VerifyWhenNotConnected) {
3755 const string kFakeCertificate("fake cert");
3756 const string kFakePublicKey("fake public key");
3757 const string kFakeNonce("fake public key");
3758 const string kFakeSignedData("fake signed data");
3759 const string kFakeUdn("fake udn");
3760 const vector<uint8_t> kSSID(10, 87);
3761 const string kConfiguredSSID("AConfiguredDestination");
3762 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3763 kConfiguredSSID.end());
3764 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3765 scoped_refptr<MockWiFiService> mock_destination(
3766 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3767 metrics(), manager(), wifi_provider_,
3768 kSSID, "", "none", false));
3769 // Register this service, but don't mark it as connected.
3770 manager()->RegisterService(mock_destination);
3771 // Verify that if we're not connected to anything, verification fails.
3772 {
3773 LOG(INFO) << "Can't verify if not connected.";
3774 EXPECT_CALL(*crypto_util_proxy_,
3775 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3776 Error error(Error::kOperationInitiated);
3777 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3778 kFakeSignedData, kFakeUdn, "", "",
3779 ResultBoolCallback(), &error);
3780 EXPECT_TRUE(error.IsFailure());
3781 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3782 }
3783 {
3784 // However, if the destination is already configured, we might be
3785 // connected to it via something other than WiFi, and we shouldn't
3786 // enforce the WiFi check.
3787 EXPECT_CALL(*crypto_util_proxy_,
3788 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3789 kFakeSignedData, kFakeUdn,
3790 kConfiguredSSIDVector, kConfiguredBSSID,
3791 _, _)).Times(1).WillOnce(Return(true));
3792 Error error(Error::kOperationInitiated);
3793 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3794 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3795 kConfiguredBSSID, ResultBoolCallback(),
3796 &error);
3797 EXPECT_FALSE(error.IsFailure());
3798 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3799 }
3800}
3801
Christopher Wiley1057cd72013-02-28 15:21:29 -08003802TEST_F(ManagerTest, VerifyDestination) {
3803 const string kFakeCertificate("fake cert");
3804 const string kFakePublicKey("fake public key");
3805 const string kFakeNonce("fake public key");
3806 const string kFakeSignedData("fake signed data");
3807 const string kFakeUdn("fake udn");
3808 const char kSSIDStr[] = "fake ssid";
3809 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003810 const string kConfiguredSSID("AConfiguredDestination");
3811 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3812 kConfiguredSSID.end());
3813 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003814 const string kFakeData("muffin man");
3815 scoped_refptr<MockWiFiService> mock_destination(
3816 new NiceMock<MockWiFiService>(control_interface(),
3817 dispatcher(),
3818 metrics(),
3819 manager(),
3820 wifi_provider_,
3821 kSSID,
3822 "",
3823 "none",
3824 false));
3825 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003826 // Making the service look online will let service lookup in
3827 // VerifyDestinatoin succeed.
3828 EXPECT_CALL(*mock_destination.get(), IsConnected())
3829 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003830 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003831
3832 // Lead off by verifying that the basic VerifyDestination flow works.
3833 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003834 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003835 ResultBoolCallback passed_down_callback;
3836 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3837 kFakePublicKey,
3838 kFakeNonce,
3839 kFakeSignedData,
3840 kFakeUdn,
3841 kSSID,
3842 _,
3843 _,
3844 _))
3845 .Times(1)
3846 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3847 // Ask the manager to verify the current destination. This should look
3848 // up our previously registered service, and pass some metadata about
3849 // that service down to the CryptoUtilProxy to verify.
3850 Error error(Error::kOperationInitiated);
3851 ResultBoolCallback cb = Bind(
3852 &DestinationVerificationTest::ResultBoolCallbackStub,
3853 dv_test.AsWeakPtr());
3854 manager()->VerifyDestination(kFakeCertificate,
3855 kFakePublicKey,
3856 kFakeNonce,
3857 kFakeSignedData,
3858 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003859 // Ask to be verified against that service.
3860 "", "",
3861 cb,
3862 &error);
3863 // We assert here, because if the operation is not ongoing, it is
3864 // inconsistent with shim behavior to call the callback anyway.
3865 ASSERT_TRUE(error.IsOngoing());
3866 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3867 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3868 // Call the callback passed into the CryptoUtilProxy, which
3869 // should find its way into the callback passed into the manager.
3870 // In real code, that callback passed into the manager is from the
3871 // DBus adaptor.
3872 Error e;
3873 passed_down_callback.Run(e, true);
3874 Mock::VerifyAndClearExpectations(&dv_test);
3875 }
3876
Christopher Wiley1057cd72013-02-28 15:21:29 -08003877 // Now for a slightly more complex variant. When we encrypt data,
3878 // we do the same verification step but monkey with the callback to
3879 // link ourselves to an encrypt step afterward.
3880 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003881 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003882 ResultBoolCallback passed_down_callback;
3883 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3884 kFakePublicKey,
3885 kFakeNonce,
3886 kFakeSignedData,
3887 kFakeUdn,
3888 kSSID,
3889 _,
3890 _,
3891 _))
3892 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3893
3894 Error error(Error::kOperationInitiated);
3895 ResultStringCallback cb = Bind(
3896 &DestinationVerificationTest::ResultStringCallbackStub,
3897 dv_test.AsWeakPtr());
3898 manager()->VerifyAndEncryptData(kFakeCertificate,
3899 kFakePublicKey,
3900 kFakeNonce,
3901 kFakeSignedData,
3902 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003903 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003904 kFakeData,
3905 cb,
3906 &error);
3907 ASSERT_TRUE(error.IsOngoing());
3908 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3909 // Now, if we call that passed down callback, we should see encrypt being
3910 // called.
3911 ResultStringCallback second_passed_down_callback;
3912 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3913 kFakeData,
3914 _,
3915 _))
3916 .Times(1)
3917 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3918 Return(true)));
3919 Error e;
3920 passed_down_callback.Run(e, true);
3921 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3922 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3923 // And if we call the second passed down callback, we should see the
3924 // original function we passed down to VerifyDestination getting called.
3925 e.Reset();
3926 second_passed_down_callback.Run(e, "");
3927 Mock::VerifyAndClearExpectations(&dv_test);
3928 }
3929
3930 // If verification fails on the way to trying to encrypt, we should ditch
3931 // without calling encrypt at all.
3932 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003933 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003934 ResultBoolCallback passed_down_callback;
3935 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3936 kFakePublicKey,
3937 kFakeNonce,
3938 kFakeSignedData,
3939 kFakeUdn,
3940 kSSID,
3941 _,
3942 _,
3943 _))
3944 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3945
3946 Error error(Error::kOperationInitiated);
3947 ResultStringCallback cb = Bind(
3948 &DestinationVerificationTest::ResultStringCallbackStub,
3949 dv_test.AsWeakPtr());
3950 manager()->VerifyAndEncryptData(kFakeCertificate,
3951 kFakePublicKey,
3952 kFakeNonce,
3953 kFakeSignedData,
3954 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003955 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003956 kFakeData,
3957 cb,
3958 &error);
3959 ASSERT_TRUE(error.IsOngoing());
3960 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3961 Error e(Error::kOperationFailed);
3962 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3963 // Although we're ditching, this callback is what cleans up the pending
3964 // DBus call.
3965 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3966 passed_down_callback.Run(e, false);
3967 Mock::VerifyAndClearExpectations(&dv_test);
3968 }
3969}
3970
Paul Stewartd2e1c362013-03-03 19:06:07 -08003971TEST_F(ManagerTest, IsProfileBefore) {
3972 scoped_refptr<MockProfile> profile0(
3973 new NiceMock<MockProfile>(
3974 control_interface(), metrics(), manager(), ""));
3975 scoped_refptr<MockProfile> profile1(
3976 new NiceMock<MockProfile>(
3977 control_interface(), metrics(), manager(), ""));
3978
3979 AdoptProfile(manager(), profile0);
3980 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3981 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3982 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3983
3984 // A few abnormal cases, but it's good to track their behavior.
3985 scoped_refptr<MockProfile> profile2(
3986 new NiceMock<MockProfile>(
3987 control_interface(), metrics(), manager(), ""));
3988 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3989 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3990 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3991 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3992}
3993
Paul Stewart967eaeb2013-04-25 19:53:07 -07003994TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
3995 MockStore storage0;
3996 MockStore storage1;
3997 MockStore storage2;
3998
3999 scoped_refptr<MockProfile> profile0(
4000 new NiceMock<MockProfile>(
4001 control_interface(), metrics(), manager(), ""));
4002 scoped_refptr<MockProfile> profile1(
4003 new NiceMock<MockProfile>(
4004 control_interface(), metrics(), manager(), ""));
4005 scoped_refptr<MockProfile> profile2(
4006 new NiceMock<MockProfile>(
4007 control_interface(), metrics(), manager(), ""));
4008
4009 AdoptProfile(manager(), profile0);
4010 AdoptProfile(manager(), profile1);
4011 AdoptProfile(manager(), profile2);
4012
4013 scoped_refptr<MockService> service(
4014 new NiceMock<MockService>(control_interface(),
4015 dispatcher(),
4016 metrics(),
4017 manager()));
4018
4019 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4020 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4021 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4022
4023 const string kEntry0("aluminum_crutch");
4024 const string kEntry2("rehashed_faces");
4025
4026 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4027 .WillOnce(Return(kEntry0));
4028 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4029 .WillOnce(Return(""));
4030 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4031 .WillOnce(Return(kEntry2));
4032
4033 const string kProfileRpc0("service_station");
4034 const string kProfileRpc2("crystal_tiaras");
4035
4036 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4037 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4038 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4039
4040 map<string, string> entries =
4041 manager()->GetLoadableProfileEntriesForService(service);
4042 EXPECT_EQ(2, entries.size());
4043 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4044 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4045 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4046 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4047}
4048
mukesh agrawal00752532013-05-03 15:46:55 -07004049TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004050 // We need a real glib here, so that profiles are persisted.
4051 GLib glib;
4052 ScopedTempDir temp_dir;
4053 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4054 Manager manager(control_interface(),
4055 dispatcher(),
4056 metrics(),
4057 &glib,
4058 run_path(),
4059 storage_path(),
4060 temp_dir.path().value());
4061 // Can't use |wifi_provider_|, because it's owned by the Manager
4062 // object in the fixture.
4063 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4064 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004065 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004066 // Give manager a valid place to write the user profile list.
4067 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4068
4069 // With no user profiles, the WiFiProvider should be called once
4070 // (for the default profile).
4071 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4072 manager.InitializeProfiles();
4073 Mock::VerifyAndClearExpectations(wifi_provider);
4074
4075 // With |n| user profiles, the WiFiProvider should be called |n+1|
4076 // times. First, create 2 user profiles...
4077 const char kProfile0[] = "~user/profile0";
4078 const char kProfile1[] = "~user/profile1";
4079 string profile_rpc_path;
4080 Error error;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004081 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawald142fd62013-05-01 16:50:57 -07004082 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4083 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4084 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4085 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4086
4087 // ... then reset manager state ...
4088 manager.profiles_.clear();
4089
4090 // ...then check that the WiFiProvider is notified about all three
4091 // profiles (one default, two user).
4092 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4093 manager.InitializeProfiles();
4094 Mock::VerifyAndClearExpectations(wifi_provider);
4095}
4096
mukesh agrawal00752532013-05-03 15:46:55 -07004097TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4098 // We need a real glib here, so that profiles are persisted.
4099 GLib glib;
4100 ScopedTempDir temp_dir;
4101 scoped_ptr<Manager> manager;
4102 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4103
4104 // Instantiate a Manager with empty persistent storage. Check that
4105 // defaults are set.
4106 //
4107 // Note that we use the same directory for default and user profiles.
4108 // This doesn't affect the test results, because we don't push a
4109 // user profile.
4110 manager.reset(new Manager(control_interface(),
4111 dispatcher(),
4112 metrics(),
4113 &glib,
4114 run_path(),
4115 temp_dir.path().value(),
4116 temp_dir.path().value()));
4117 manager->InitializeProfiles();
4118 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4119 manager->props_.check_portal_list);
4120 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4121 manager->props_.ignored_dns_search_paths);
4122 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4123 manager->props_.link_monitor_technologies);
4124 EXPECT_EQ(PortalDetector::kDefaultURL,
4125 manager->props_.portal_url);
4126 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4127 manager->props_.portal_check_interval_seconds);
4128
4129 // Change one of the settings.
4130 static const string kCustomCheckPortalList = "fiber0";
4131 Error error;
4132 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4133 manager->profiles_[0]->Save();
4134
4135 // Instantiate a new manager. It should have our settings for
4136 // check_portal_list, rather than the default.
4137 manager.reset(new Manager(control_interface(),
4138 dispatcher(),
4139 metrics(),
4140 &glib,
4141 run_path(),
4142 temp_dir.path().value(),
4143 temp_dir.path().value()));
4144 manager->InitializeProfiles();
4145 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4146
4147 // If we clear the persistent storage, we again get the default value.
4148 ASSERT_TRUE(temp_dir.Delete());
4149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4150 manager.reset(new Manager(control_interface(),
4151 dispatcher(),
4152 metrics(),
4153 &glib,
4154 run_path(),
4155 temp_dir.path().value(),
4156 temp_dir.path().value()));
4157 manager->InitializeProfiles();
4158 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4159 manager->props_.check_portal_list);
4160}
4161
mukesh agrawalb94adde2013-08-22 18:17:26 -07004162TEST_F(ManagerTest, ProfileStackChangeLogging) {
4163 // We use a real glib here, since Manager and Profile don't provide an
4164 // easy way to mock out KeyFileStore.
4165 GLib glib;
4166 ScopedTempDir temp_dir;
4167 scoped_ptr<Manager> manager;
4168 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4169 manager.reset(new Manager(control_interface(),
4170 dispatcher(),
4171 metrics(),
4172 &glib,
4173 run_path(),
4174 temp_dir.path().value(),
4175 temp_dir.path().value()));
4176
4177 ScopedMockLog log;
4178 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4179 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4180 manager->InitializeProfiles();
4181
4182 const char kProfile0[] = "~user/profile0";
4183 const char kProfile1[] = "~user/profile1";
4184 const char kProfile2[] = "~user/profile2";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004185 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawalb94adde2013-08-22 18:17:26 -07004186 TestCreateProfile(manager.get(), kProfile0);
4187 TestCreateProfile(manager.get(), kProfile1);
4188 TestCreateProfile(manager.get(), kProfile2);
4189
4190 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4191 TestPushProfile(manager.get(), kProfile0);
4192
4193 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4194 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4195
4196 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4197 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4198
4199 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4200 TestPopProfile(manager.get(), kProfile2);
4201
4202 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4203 TestPopAnyProfile(manager.get());
4204
4205 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4206 TestPopAllUserProfiles(manager.get());
4207}
4208
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004209// Custom property setters should return false, and make no changes, if
4210// the new value is the same as the old value.
4211TEST_F(ManagerTest, CustomSetterNoopChange) {
4212 // SetCheckPortalList
4213 {
4214 static const string kCheckPortalList = "weird-device,weirder-device";
4215 Error error;
4216 // Set to known value.
4217 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4218 EXPECT_TRUE(error.IsSuccess());
4219 // Set to same value.
4220 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4221 EXPECT_TRUE(error.IsSuccess());
4222 }
4223
4224 // SetIgnoredDNSSearchPaths
4225 {
4226 NiceMock<MockResolver> resolver;
4227 static const string kIgnoredPaths = "example.com,example.org";
4228 Error error;
4229 SetResolver(&resolver);
4230 // Set to known value.
4231 EXPECT_CALL(resolver, set_ignored_search_list(_));
4232 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4233 EXPECT_TRUE(error.IsSuccess());
4234 Mock::VerifyAndClearExpectations(&resolver);
4235 // Set to same value.
4236 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4237 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4238 EXPECT_TRUE(error.IsSuccess());
4239 Mock::VerifyAndClearExpectations(&resolver);
4240 }
4241}
4242
Paul Stewart7de7e022013-08-28 09:42:50 -07004243TEST_F(ManagerTest, GeoLocation) {
4244 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4245
4246 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4247 dispatcher(),
4248 metrics(),
4249 manager(),
4250 "null",
4251 "addr",
4252 0));
4253
4254 // Manager should ignore gelocation info from technologies it does not know.
4255 EXPECT_CALL(*device, technology())
4256 .Times(AtLeast(1))
4257 .WillRepeatedly(Return(Technology::kEthernet));
4258 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4259 manager()->OnDeviceGeolocationInfoUpdated(device);
4260 Mock::VerifyAndClearExpectations(device);
4261 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4262
4263 // Manager should add WiFi geolocation info.
4264 EXPECT_CALL(*device, technology())
4265 .Times(AtLeast(1))
4266 .WillRepeatedly(Return(Technology::kWifi));
4267 EXPECT_CALL(*device, GetGeolocationObjects())
4268 .WillOnce(Return(vector<GeolocationInfo>()));
4269 manager()->OnDeviceGeolocationInfoUpdated(device);
4270 Mock::VerifyAndClearExpectations(device);
4271 auto location_infos = manager()->GetNetworksForGeolocation();
4272 EXPECT_EQ(1, location_infos.size());
4273 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4274
4275 // Manager should inclusively add cellular info.
4276 EXPECT_CALL(*device, technology())
4277 .Times(AtLeast(1))
4278 .WillRepeatedly(Return(Technology::kCellular));
4279 EXPECT_CALL(*device, GetGeolocationObjects())
4280 .WillOnce(Return(vector<GeolocationInfo>()));
4281 manager()->OnDeviceGeolocationInfoUpdated(device);
4282 location_infos = manager()->GetNetworksForGeolocation();
4283 EXPECT_EQ(2, location_infos.size());
4284 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4285 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4286}
4287
Peter Qiu574996a2014-04-04 10:55:47 -07004288TEST_F(ManagerTest, IsWifiIdle) {
4289 // No registered service.
4290 EXPECT_FALSE(manager()->IsWifiIdle());
4291
4292 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4293 dispatcher(),
4294 metrics(),
4295 manager()));
4296
4297 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4298 dispatcher(),
4299 metrics(),
4300 manager()));
4301
4302 manager()->RegisterService(wifi_service);
4303 manager()->RegisterService(cell_service);
4304
4305 EXPECT_CALL(*wifi_service.get(), technology())
4306 .WillRepeatedly(Return(Technology::kWifi));
4307 EXPECT_CALL(*cell_service.get(), technology())
4308 .WillRepeatedly(Return(Technology::kCellular));
4309
4310 // Cellular is connected.
4311 EXPECT_CALL(*cell_service.get(), IsConnected())
4312 .WillRepeatedly(Return(true));
4313 manager()->UpdateService(cell_service);
4314
4315 // No wifi connection attempt.
4316 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4317 .WillRepeatedly(Return(false));
4318 EXPECT_CALL(*wifi_service.get(), IsConnected())
4319 .WillRepeatedly(Return(false));
4320 manager()->UpdateService(wifi_service);
4321 EXPECT_TRUE(manager()->IsWifiIdle());
4322
4323 // Attempt wifi connection.
4324 Mock::VerifyAndClearExpectations(wifi_service);
4325 EXPECT_CALL(*wifi_service.get(), technology())
4326 .WillRepeatedly(Return(Technology::kWifi));
4327 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4328 .WillRepeatedly(Return(true));
4329 EXPECT_CALL(*wifi_service.get(), IsConnected())
4330 .WillRepeatedly(Return(false));
4331 manager()->UpdateService(wifi_service);
4332 EXPECT_FALSE(manager()->IsWifiIdle());
4333
4334 // wifi connected.
4335 Mock::VerifyAndClearExpectations(wifi_service);
4336 EXPECT_CALL(*wifi_service.get(), technology())
4337 .WillRepeatedly(Return(Technology::kWifi));
4338 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4339 .WillRepeatedly(Return(false));
4340 EXPECT_CALL(*wifi_service.get(), IsConnected())
4341 .WillRepeatedly(Return(true));
4342 manager()->UpdateService(wifi_service);
4343 EXPECT_FALSE(manager()->IsWifiIdle());
4344}
4345
Chris Masone9be4a9d2011-05-16 15:44:09 -07004346} // namespace shill