blob: e19fff7bb144ba0d107210b77dfe3f1198535147 [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);
Peter Qiue783f1c2014-05-02 11:42:33 -07001569 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1570 Metrics::kUserInitiatedEventWifiScan)).Times(1);
Ben Chan923a5022013-09-20 11:23:23 -07001571 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
Peter Qiue783f1c2014-05-02 11:42:33 -07001572 manager()->DeregisterDevice(mock_devices_[0].get());
1573 manager()->DeregisterDevice(mock_devices_[1].get());
1574 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1575 Mock::VerifyAndClearExpectations(mock_devices_[1]);
1576
1577 manager()->RegisterDevice(mock_devices_[0].get());
1578 EXPECT_CALL(*mock_devices_[0], technology())
1579 .WillRepeatedly(Return(Technology::kWifi));
1580 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1581 Metrics::kUserInitiatedEventWifiScan)).Times(1);
1582 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
1583 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1584 manager()->DeregisterDevice(mock_devices_[0].get());
1585 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1586
1587 manager()->RegisterDevice(mock_devices_[0].get());
1588 EXPECT_CALL(*mock_devices_[0], technology())
1589 .WillRepeatedly(Return(Technology::kUnknown));
1590 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1591 Metrics::kUserInitiatedEventWifiScan)).Times(0);
1592 EXPECT_CALL(*mock_devices_[0], Scan(_, _, _)).Times(0);
1593 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1594 manager()->DeregisterDevice(mock_devices_[0].get());
1595 Mock::VerifyAndClearExpectations(mock_devices_[0]);
mukesh agrawal32399322011-09-01 10:53:43 -07001596 }
1597
1598 {
1599 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001600 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001601 EXPECT_EQ(Error::kInvalidArguments, error.type());
1602 }
1603}
1604
Darin Petkovb65c2452012-02-23 15:17:06 +01001605TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001606 KeyValueStore args;
1607 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001608 manager()->GetService(args, &e);
1609 EXPECT_EQ(Error::kInvalidArguments, e.type());
1610 EXPECT_EQ("must specify service type", e.message());
1611}
1612
1613TEST_F(ManagerTest, GetServiceUnknownType) {
1614 KeyValueStore args;
1615 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001616 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001617 manager()->GetService(args, &e);
1618 EXPECT_EQ(Error::kNotSupported, e.type());
1619 EXPECT_EQ("service type is unsupported", e.message());
1620}
1621
Paul Stewart35eff132013-04-12 12:08:40 -07001622TEST_F(ManagerTest, GetServiceEthernetEap) {
1623 KeyValueStore args;
1624 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001625 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1626 dispatcher(),
1627 metrics(),
1628 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001629 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001630 SetEapProviderService(service);
1631 EXPECT_EQ(service, manager()->GetService(args, &e));
1632 EXPECT_TRUE(e.IsSuccess());
1633}
1634
Darin Petkovb65c2452012-02-23 15:17:06 +01001635TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001636 KeyValueStore args;
1637 Error e;
1638 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001639 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001640 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001641 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001642 manager()->GetService(args, &e);
1643 EXPECT_TRUE(e.IsSuccess());
1644}
1645
Darin Petkov33af05c2012-02-28 10:10:30 +01001646TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1647 KeyValueStore args;
1648 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001649 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001650 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001651 new StrictMock<MockProfile>(
1652 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001653 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001654 ServiceRefPtr service = manager()->GetService(args, &e);
1655 EXPECT_EQ(Error::kNotSupported, e.type());
1656 EXPECT_FALSE(service);
1657}
1658
Darin Petkovb65c2452012-02-23 15:17:06 +01001659TEST_F(ManagerTest, GetServiceVPN) {
1660 KeyValueStore args;
1661 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001662 args.SetString(kTypeProperty, kTypeVPN);
1663 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1664 args.SetString(kProviderHostProperty, "10.8.0.1");
1665 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001666 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001667 new StrictMock<MockProfile>(
1668 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001669 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001670
1671#if defined(DISABLE_VPN)
1672
1673 ServiceRefPtr service = manager()->GetService(args, &e);
1674 EXPECT_EQ(Error::kNotSupported, e.type());
1675 EXPECT_FALSE(service);
1676
1677#else
1678
Paul Stewart7f5ad572012-06-04 15:18:54 -07001679 ServiceRefPtr updated_service;
1680 EXPECT_CALL(*profile, UpdateService(_))
1681 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1682 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001683 EXPECT_CALL(*profile, LoadService(_))
1684 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001685 EXPECT_CALL(*profile, ConfigureService(_))
1686 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001687 ServiceRefPtr service = manager()->GetService(args, &e);
1688 EXPECT_TRUE(e.IsSuccess());
1689 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001690 EXPECT_EQ(service, updated_service);
1691 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001692
1693#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001694}
1695
Ben Chan520eb172013-10-30 20:51:04 -07001696#if !defined(DISABLE_WIMAX)
1697
Darin Petkovc63dcf02012-05-24 11:51:43 +02001698TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1699 KeyValueStore args;
1700 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001701 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001702 ServiceRefPtr service = manager()->GetService(args, &e);
1703 EXPECT_EQ(Error::kInvalidArguments, e.type());
1704 EXPECT_EQ("Missing WiMAX network id.", e.message());
1705 EXPECT_FALSE(service);
1706}
1707
Darin Petkovd1cd7972012-05-22 15:26:15 +02001708TEST_F(ManagerTest, GetServiceWiMax) {
1709 KeyValueStore args;
1710 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001711 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001712 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001713 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001714 ServiceRefPtr service = manager()->GetService(args, &e);
1715 EXPECT_TRUE(e.IsSuccess());
1716 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001717}
1718
Ben Chan520eb172013-10-30 20:51:04 -07001719#endif // DISABLE_WIMAX
1720
Paul Stewart7f61e522012-03-22 11:13:45 -07001721TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1722 // Manager calls ActiveProfile() so we need at least one profile installed.
1723 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001724 new NiceMock<MockProfile>(
1725 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001726 AdoptProfile(manager(), profile);
1727
1728 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001729 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001730 Error error;
1731 manager()->ConfigureService(args, &error);
1732 EXPECT_EQ(Error::kInvalidArguments, error.type());
1733 EXPECT_EQ("Invalid profile name xxx", error.message());
1734}
1735
1736TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1737 // Manager calls ActiveProfile() so we need at least one profile installed.
1738 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001739 new NiceMock<MockProfile>(
1740 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001741 AdoptProfile(manager(), profile);
1742
1743 KeyValueStore args;
1744 Error error;
1745 manager()->ConfigureService(args, &error);
1746 EXPECT_EQ(Error::kInvalidArguments, error.type());
1747 EXPECT_EQ("must specify service type", error.message());
1748}
1749
1750// A registered service in the ephemeral profile should be moved to the
1751// active profile as a part of configuration if no profile was explicitly
1752// specified.
1753TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1754 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001755 new NiceMock<MockProfile>(
1756 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001757
1758 AdoptProfile(manager(), profile); // This is now the active profile.
1759
Paul Stewartd2e1c362013-03-03 19:06:07 -08001760 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001761 scoped_refptr<MockWiFiService> service(
1762 new NiceMock<MockWiFiService>(control_interface(),
1763 dispatcher(),
1764 metrics(),
1765 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001766 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001767 ssid,
1768 "",
1769 "",
1770 false));
1771
1772 manager()->RegisterService(service);
1773 service->set_profile(GetEphemeralProfile(manager()));
1774
Paul Stewart3c504012013-01-17 17:49:58 -08001775 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001776 .WillOnce(Return(service));
1777 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1778 .WillOnce(Return(true));
1779 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1780 .WillOnce(Return(true));
1781
1782 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001783 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001784 Error error;
1785 manager()->ConfigureService(args, &error);
1786 EXPECT_TRUE(error.IsSuccess());
1787}
1788
Paul Stewart2c575d22012-12-07 12:28:57 -08001789// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001790// specify a profile, it should be moved from the profile it was previously
1791// in to the specified profile if one was requested.
1792TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1793 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001794 new NiceMock<MockProfile>(
1795 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001796 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001797 new NiceMock<MockProfile>(
1798 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001799
1800 const string kProfileName0 = "profile0";
1801 const string kProfileName1 = "profile1";
1802
1803 EXPECT_CALL(*profile0, GetRpcIdentifier())
1804 .WillRepeatedly(Return(kProfileName0));
1805 EXPECT_CALL(*profile1, GetRpcIdentifier())
1806 .WillRepeatedly(Return(kProfileName1));
1807
1808 AdoptProfile(manager(), profile0);
1809 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1810
Paul Stewartd2e1c362013-03-03 19:06:07 -08001811 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001812 scoped_refptr<MockWiFiService> service(
1813 new NiceMock<MockWiFiService>(control_interface(),
1814 dispatcher(),
1815 metrics(),
1816 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001817 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001818 ssid,
1819 "",
1820 "",
1821 false));
1822
1823 manager()->RegisterService(service);
1824 service->set_profile(profile1);
1825
Paul Stewart3c504012013-01-17 17:49:58 -08001826 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001827 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001828 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1829 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001830 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1831 .WillOnce(Return(true));
1832 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1833 .WillOnce(Return(true));
1834 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1835 .WillOnce(Return(true));
1836
1837 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001838 args.SetString(kTypeProperty, kTypeWifi);
1839 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001840 Error error;
1841 manager()->ConfigureService(args, &error);
1842 EXPECT_TRUE(error.IsSuccess());
1843 service->set_profile(NULL); // Breaks refcounting loop.
1844}
1845
Paul Stewart2c575d22012-12-07 12:28:57 -08001846// If we configure a service that is already a member of the specified
1847// profile, the Manager should not call LoadService or AdoptService again
1848// on this service.
1849TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1850 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001851 new NiceMock<MockProfile>(
1852 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001853
1854 const string kProfileName0 = "profile0";
1855
1856 EXPECT_CALL(*profile0, GetRpcIdentifier())
1857 .WillRepeatedly(Return(kProfileName0));
1858
1859 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1860
Paul Stewartd2e1c362013-03-03 19:06:07 -08001861 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001862 scoped_refptr<MockWiFiService> service(
1863 new NiceMock<MockWiFiService>(control_interface(),
1864 dispatcher(),
1865 metrics(),
1866 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001867 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001868 ssid,
1869 "",
1870 "",
1871 false));
1872
1873 manager()->RegisterService(service);
1874 service->set_profile(profile0);
1875
Paul Stewart3c504012013-01-17 17:49:58 -08001876 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001877 .WillOnce(Return(service));
1878 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1879 .Times(0);
1880 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1881 .WillOnce(Return(true));
1882 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1883 .Times(0);
1884
1885 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001886 args.SetString(kTypeProperty, kTypeWifi);
1887 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001888 Error error;
1889 manager()->ConfigureService(args, &error);
1890 EXPECT_TRUE(error.IsSuccess());
1891 service->set_profile(NULL); // Breaks refcounting loop.
1892}
1893
Paul Stewart7f61e522012-03-22 11:13:45 -07001894// An unregistered service should remain unregistered, but its contents should
1895// be saved to the specified profile nonetheless.
1896TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1897 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001898 new NiceMock<MockProfile>(
1899 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001900 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001901 new NiceMock<MockProfile>(
1902 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001903
1904 const string kProfileName0 = "profile0";
1905 const string kProfileName1 = "profile1";
1906
1907 EXPECT_CALL(*profile0, GetRpcIdentifier())
1908 .WillRepeatedly(Return(kProfileName0));
1909 EXPECT_CALL(*profile1, GetRpcIdentifier())
1910 .WillRepeatedly(Return(kProfileName1));
1911
1912 AdoptProfile(manager(), profile0);
1913 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1914
Paul Stewartd2e1c362013-03-03 19:06:07 -08001915 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001916 scoped_refptr<MockWiFiService> service(
1917 new NiceMock<MockWiFiService>(control_interface(),
1918 dispatcher(),
1919 metrics(),
1920 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001921 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001922 ssid,
1923 "",
1924 "",
1925 false));
1926
1927 service->set_profile(profile1);
1928
Paul Stewart3c504012013-01-17 17:49:58 -08001929 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001930 .WillOnce(Return(service));
1931 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1932 .WillOnce(Return(true));
1933 EXPECT_CALL(*profile0, AdoptService(_))
1934 .Times(0);
1935 EXPECT_CALL(*profile1, AdoptService(_))
1936 .Times(0);
1937
1938 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001939 args.SetString(kTypeProperty, kTypeWifi);
1940 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001941 Error error;
1942 manager()->ConfigureService(args, &error);
1943 EXPECT_TRUE(error.IsSuccess());
1944}
1945
Paul Stewartd2e1c362013-03-03 19:06:07 -08001946TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1947 KeyValueStore args;
1948 Error error;
1949 ServiceRefPtr service =
1950 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001951 EXPECT_EQ(Error::kInvalidArguments, error.type());
1952 EXPECT_EQ("must specify service type", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001953 EXPECT_EQ(NULL, service.get());
1954}
1955
1956TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1957 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001958 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001959 Error error;
1960 ServiceRefPtr service =
1961 manager()->ConfigureServiceForProfile("", args, &error);
1962 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001963 EXPECT_EQ("service type is unsupported", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001964 EXPECT_EQ(NULL, service.get());
1965}
1966
1967TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1968 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001969 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001970 Error error;
1971 ServiceRefPtr service =
1972 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1973 EXPECT_EQ(Error::kNotFound, error.type());
1974 EXPECT_EQ("Profile specified was not found", error.message());
1975 EXPECT_EQ(NULL, service.get());
1976}
1977
1978TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1979 const string kProfileName0 = "profile0";
1980 const string kProfileName1 = "profile1";
1981 scoped_refptr<MockProfile> profile0(
1982 AddNamedMockProfileToManager(manager(), kProfileName0));
1983
1984 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001985 args.SetString(kTypeProperty, kTypeWifi);
1986 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001987 Error error;
1988 ServiceRefPtr service =
1989 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1990 EXPECT_EQ(Error::kInvalidArguments, error.type());
1991 EXPECT_EQ("Profile argument does not match that in "
1992 "the configuration arguments", error.message());
1993 EXPECT_EQ(NULL, service.get());
1994}
1995
1996TEST_F(ManagerTest,
1997 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1998 const string kProfileName0 = "profile0";
1999 scoped_refptr<MockProfile> profile0(
2000 AddNamedMockProfileToManager(manager(), kProfileName0));
2001 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002002 args.SetString(kTypeProperty, kTypeWifi);
2003 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002004
2005 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2006 .WillOnce(Return(WiFiServiceRefPtr()));
2007 EXPECT_CALL(*wifi_provider_, GetService(_, _))
2008 .WillOnce(Return(WiFiServiceRefPtr()));
2009 Error error;
2010 ServiceRefPtr service =
2011 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2012 // Since we didn't set the error in the GetService expectation above...
2013 EXPECT_TRUE(error.IsSuccess());
2014 EXPECT_EQ(NULL, service.get());
2015}
2016
2017TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
2018 const string kProfileName0 = "profile0";
2019 scoped_refptr<MockProfile> profile0(
2020 AddNamedMockProfileToManager(manager(), kProfileName0));
2021
2022 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002023 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002024
2025 scoped_refptr<MockWiFiService> mock_service(
2026 new NiceMock<MockWiFiService>(control_interface(),
2027 dispatcher(),
2028 metrics(),
2029 manager(),
2030 wifi_provider_,
2031 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002032 kModeManaged,
2033 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002034 false));
2035 ServiceRefPtr mock_service_generic(mock_service.get());
2036 mock_service->set_profile(profile0);
2037 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2038 .WillOnce(Return(WiFiServiceRefPtr()));
2039 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2040 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2041 .WillOnce(Return(true));
2042 Error error;
2043 ServiceRefPtr service =
2044 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2045 EXPECT_TRUE(error.IsSuccess());
2046 EXPECT_EQ(mock_service.get(), service.get());
2047 mock_service->set_profile(NULL); // Breaks reference cycle.
2048}
2049
2050TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2051 scoped_refptr<MockService> mock_service(
2052 new NiceMock<MockService>(control_interface(),
2053 dispatcher(),
2054 metrics(),
2055 manager()));
2056 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002057 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002058 manager()->RegisterService(mock_service);
2059 ServiceRefPtr mock_service_generic(mock_service.get());
2060
2061 const string kProfileName = "profile";
2062 scoped_refptr<MockProfile> profile(
2063 AddNamedMockProfileToManager(manager(), kProfileName));
2064 mock_service->set_profile(profile);
2065
2066 EXPECT_CALL(*mock_service, technology())
2067 .WillOnce(Return(Technology::kCellular))
2068 .WillOnce(Return(Technology::kWifi));
2069
2070 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2071 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2072 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2073
2074 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002075 args.SetString(kTypeProperty, kTypeWifi);
2076 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002077
2078 // The first attempt should fail because the service reports a technology
2079 // other than "WiFi".
2080 {
2081 Error error;
2082 ServiceRefPtr service =
2083 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2084 EXPECT_EQ(NULL, service.get());
2085 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002086 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002087 }
2088
2089 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2090 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2091
2092 {
2093 Error error;
2094 ServiceRefPtr service =
2095 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2096 EXPECT_TRUE(error.IsSuccess());
2097 EXPECT_EQ(mock_service.get(), service.get());
2098 EXPECT_EQ(profile.get(), service->profile().get());
2099 }
2100 mock_service->set_profile(NULL); // Breaks reference cycle.
2101}
2102
2103TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2104 const string kProfileName = "profile";
2105 scoped_refptr<MockProfile> profile(
2106 AddNamedMockProfileToManager(manager(), kProfileName));
2107
2108 scoped_refptr<MockWiFiService> mock_service(
2109 new NiceMock<MockWiFiService>(control_interface(),
2110 dispatcher(),
2111 metrics(),
2112 manager(),
2113 wifi_provider_,
2114 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002115 kModeManaged,
2116 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002117 false));
2118 mock_service->set_profile(profile);
2119 ServiceRefPtr mock_service_generic(mock_service.get());
2120
2121 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002122 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002123 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2124 .WillOnce(Return(mock_service));
2125 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2126 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2127 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2128 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2129
2130 Error error;
2131 ServiceRefPtr service =
2132 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2133 EXPECT_TRUE(error.IsSuccess());
2134 EXPECT_EQ(mock_service.get(), service.get());
2135 EXPECT_EQ(profile.get(), service->profile().get());
2136 mock_service->set_profile(NULL); // Breaks reference cycle.
2137}
2138
2139TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2140 const string kProfileName = "profile";
2141 scoped_refptr<MockProfile> profile(
2142 AddNamedMockProfileToManager(manager(), kProfileName));
2143
2144 scoped_refptr<MockWiFiService> mock_service(
2145 new NiceMock<MockWiFiService>(control_interface(),
2146 dispatcher(),
2147 metrics(),
2148 manager(),
2149 wifi_provider_,
2150 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002151 kModeManaged,
2152 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002153 false));
2154 mock_service->set_profile(GetEphemeralProfile(manager()));
2155 ServiceRefPtr mock_service_generic(mock_service.get());
2156
2157 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002158 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002159 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2160 .WillOnce(Return(mock_service));
2161 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2162 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2163 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2164
2165 Error error;
2166 ServiceRefPtr service =
2167 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2168 EXPECT_TRUE(error.IsSuccess());
2169 EXPECT_EQ(mock_service.get(), service.get());
2170 EXPECT_EQ(profile.get(), service->profile().get());
2171 mock_service->set_profile(NULL); // Breaks reference cycle.
2172}
2173
2174TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2175 const string kProfileName0 = "profile0";
2176 scoped_refptr<MockProfile> profile0(
2177 AddNamedMockProfileToManager(manager(), kProfileName0));
2178 const string kProfileName1 = "profile1";
2179 scoped_refptr<MockProfile> profile1(
2180 AddNamedMockProfileToManager(manager(), kProfileName1));
2181
2182 scoped_refptr<MockWiFiService> mock_service(
2183 new NiceMock<MockWiFiService>(control_interface(),
2184 dispatcher(),
2185 metrics(),
2186 manager(),
2187 wifi_provider_,
2188 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002189 kModeManaged,
2190 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002191 false));
2192 manager()->RegisterService(mock_service);
2193 mock_service->set_profile(profile0);
2194 ServiceRefPtr mock_service_generic(mock_service.get());
2195
2196 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002197 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002198 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2199 .WillOnce(Return(mock_service));
2200 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2201 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2202 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2203 // This happens once to make the service loadable for the ConfigureService
2204 // below, and a second time after the service is modified.
2205 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2206 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2207 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2208 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2209
2210 Error error;
2211 ServiceRefPtr service =
2212 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2213 EXPECT_TRUE(error.IsSuccess());
2214 EXPECT_EQ(mock_service.get(), service.get());
2215 mock_service->set_profile(NULL); // Breaks reference cycle.
2216}
2217
2218TEST_F(ManagerTest,
2219 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2220 const string kProfileName0 = "profile0";
2221 scoped_refptr<MockProfile> profile0(
2222 AddNamedMockProfileToManager(manager(), kProfileName0));
2223 const string kProfileName1 = "profile1";
2224 scoped_refptr<MockProfile> profile1(
2225 AddNamedMockProfileToManager(manager(), kProfileName1));
2226
2227 scoped_refptr<MockWiFiService> matching_service(
2228 new StrictMock<MockWiFiService>(control_interface(),
2229 dispatcher(),
2230 metrics(),
2231 manager(),
2232 wifi_provider_,
2233 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002234 kModeManaged,
2235 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002236 false));
2237 matching_service->set_profile(profile1);
2238
2239 // We need to get rid of our reference to this mock service as soon
2240 // as Manager::ConfigureServiceForProfile() takes a reference in its
2241 // call to WiFiProvider::CreateTemporaryService(). This way the
2242 // latter function can keep a DCHECK(service->HasOneRef() even in
2243 // unit tests.
2244 temp_mock_service_ =
2245 new NiceMock<MockWiFiService>(control_interface(),
2246 dispatcher(),
2247 metrics(),
2248 manager(),
2249 wifi_provider_,
2250 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002251 kModeManaged,
2252 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002253 false);
2254
2255 // Only hold a pointer here so we don't affect the refcount.
2256 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2257
2258 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002259 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002260 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2261 .WillOnce(Return(matching_service));
2262 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2263 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2264 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2265 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2266 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2267 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2268 .Times(1);
2269 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2270 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2271
2272 Error error;
2273 ServiceRefPtr service =
2274 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2275 EXPECT_TRUE(error.IsSuccess());
2276 EXPECT_EQ(NULL, service.get());
2277 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2278}
2279
Paul Stewart7a20aa42013-01-17 12:21:41 -08002280TEST_F(ManagerTest, FindMatchingService) {
2281 KeyValueStore args;
2282 {
2283 Error error;
2284 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2285 EXPECT_EQ(Error::kNotFound, error.type());
2286 }
2287
2288 scoped_refptr<MockService> mock_service0(
2289 new NiceMock<MockService>(control_interface(),
2290 dispatcher(),
2291 metrics(),
2292 manager()));
2293 scoped_refptr<MockService> mock_service1(
2294 new NiceMock<MockService>(control_interface(),
2295 dispatcher(),
2296 metrics(),
2297 manager()));
2298 manager()->RegisterService(mock_service0);
2299 manager()->RegisterService(mock_service1);
2300 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2301 .WillOnce(Return(true))
2302 .WillRepeatedly(Return(false));
2303 {
2304 Error error;
2305 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2306 EXPECT_TRUE(error.IsSuccess());
2307 }
2308 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2309 .WillOnce(Return(true))
2310 .WillRepeatedly(Return(false));
2311 {
2312 Error error;
2313 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2314 EXPECT_TRUE(error.IsSuccess());
2315 }
2316 {
2317 Error error;
2318 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2319 EXPECT_EQ(Error::kNotFound, error.type());
2320 }
2321}
2322
Paul Stewart22aa71b2011-09-16 12:15:11 -07002323TEST_F(ManagerTest, TechnologyOrder) {
2324 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002325 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2326 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002327 ASSERT_TRUE(error.IsSuccess());
2328 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002329 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002330
Ben Chan923a5022013-09-20 11:23:23 -07002331 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2332 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002333 ASSERT_FALSE(error.IsSuccess());
2334 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002335 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002336 manager()->GetTechnologyOrder());
2337}
2338
2339TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002340 // TODO(quiche): Some of these tests would probably fit better in
2341 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002342 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002343
mukesh agrawale37ad322013-10-08 16:33:56 -07002344 // Construct our Services so that the string comparison of
2345 // unique_name_ differs from the numerical comparison of
2346 // serial_number_.
2347 vector<scoped_refptr<MockService>> mock_services;
2348 for (size_t i = 0; i < 11; ++i) {
2349 mock_services.push_back(
2350 new NiceMock<MockService>(control_interface(),
2351 dispatcher(),
2352 metrics(),
2353 manager()));
2354 }
2355 scoped_refptr<MockService> mock_service2 = mock_services[2];
2356 scoped_refptr<MockService> mock_service10 = mock_services[10];
2357 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002358
mukesh agrawale37ad322013-10-08 16:33:56 -07002359 manager()->RegisterService(mock_service2);
2360 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002361
mukesh agrawale37ad322013-10-08 16:33:56 -07002362 // Services should already be sorted by |serial_number_|.
2363 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002364
2365 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002366 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002367 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002368
2369 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002370 mock_service10->SetStrength(1);
2371 manager()->UpdateService(mock_service10);
2372 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002373
2374 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002375 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2376 manager()->UpdateService(mock_service2);
2377 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002378
2379 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002380 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002381 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002382 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002383 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002384
2385 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002386 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002387 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002388 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002389
Ben Chan923a5022013-09-20 11:23:23 -07002390 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2391 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002392 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002393 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002394
Gaurav Shah435de2c2011-11-17 19:01:07 -08002395 // Priority.
mukesh agrawale37ad322013-10-08 16:33:56 -07002396 mock_service2->SetPriority(1, NULL);
2397 manager()->UpdateService(mock_service2);
2398 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002399
Paul Stewart2da34c02013-10-17 15:28:56 -07002400 // HasEverConnected.
2401 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002402 manager()->UpdateService(mock_service10);
2403 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002404
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002405 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002406 mock_service2->SetAutoConnect(true);
2407 manager()->UpdateService(mock_service2);
2408 mock_service10->SetAutoConnect(false);
2409 manager()->UpdateService(mock_service10);
2410 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002411
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002412 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2413 // to any of the others below, so we reset to the default state after
2414 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002415 EXPECT_CALL(*mock_service10.get(),
2416 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002417 .WillOnce(Return(true))
2418 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002419 manager()->UpdateService(mock_service10);
2420 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2421 manager()->UpdateService(mock_service2);
2422 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002423
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002424 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002425 mock_service10->SetConnectable(true);
2426 manager()->UpdateService(mock_service10);
2427 mock_service2->SetConnectable(false);
2428 manager()->UpdateService(mock_service2);
2429 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002430
2431 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002432 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002433 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002434 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002435 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002436 manager()->UpdateService(mock_service2);
2437 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002438 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002439 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002440 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002441 manager()->UpdateService(mock_service10);
2442 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002443
2444 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002445 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002446 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002447 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002448 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002449 manager()->UpdateService(mock_service10);
2450 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002451
mukesh agrawale37ad322013-10-08 16:33:56 -07002452 // Connected-but-portalled preferred over unconnected.
2453 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002454 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002455 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002456 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002457 manager()->UpdateService(mock_service2);
2458 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002459
mukesh agrawale37ad322013-10-08 16:33:56 -07002460 // Connected preferred over connected-but-portalled.
2461 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002462 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002463 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002464 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002465 manager()->UpdateService(mock_service10);
2466 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002467
mukesh agrawale37ad322013-10-08 16:33:56 -07002468 manager()->DeregisterService(mock_service2);
2469 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002470}
2471
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002472TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002473 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002474 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002475
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002476 scoped_refptr<MockService> mock_service0(
2477 new NiceMock<MockService>(control_interface(),
2478 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002479 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002480 manager()));
2481 scoped_refptr<MockService> mock_service1(
2482 new NiceMock<MockService>(control_interface(),
2483 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002484 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002485 manager()));
2486
2487 scoped_refptr<MockConnection> mock_connection0(
2488 new NiceMock<MockConnection>(device_info_.get()));
2489 scoped_refptr<MockConnection> mock_connection1(
2490 new NiceMock<MockConnection>(device_info_.get()));
2491
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002492 // A single registered Service, without a connection. The
2493 // DefaultService should be NULL. If a change notification is
2494 // generated, it should reference kNullPath.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002495 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002496 EXPECT_CALL(*manager_adaptor_,
2497 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2498 DBusAdaptor::kNullPath))
2499 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002500 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002501 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002502
2503 // Adding another Service, also without a connection, does not
2504 // change DefaultService. Furthermore, we do not send a change
2505 // notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002506 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002507 EXPECT_CALL(*manager_adaptor_,
2508 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2509 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002510 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002511 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002512
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002513 // An explicit sort doesn't change anything, and does not emit a
2514 // change notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002515 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002516 EXPECT_CALL(*manager_adaptor_,
2517 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2518 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002519 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002520 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002521
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002522 // Re-ordering the unconnected Services doesn't change
2523 // DefaultService, and (hence) does not emit a change notification
2524 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002525 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002526 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002527 EXPECT_CALL(*manager_adaptor_,
2528 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2529 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002530 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002531 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002532
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002533 // Re-ordering the unconnected Services doesn't change
2534 // DefaultService, and (hence) does not emit a change notification
2535 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002536 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002537 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002538 EXPECT_CALL(*manager_adaptor_,
2539 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2540 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002541 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002542 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002543
Paul Stewartce4ec192012-03-14 12:53:46 -07002544 mock_service0->set_mock_connection(mock_connection0);
2545 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002546
mukesh agrawale37ad322013-10-08 16:33:56 -07002547 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002548 // from ServiceOrderIs. We notify others of the change in
2549 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002550 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002551 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002552 EXPECT_CALL(*manager_adaptor_,
2553 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002554 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002555 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002556
Darin Petkova5e07ef2012-07-09 14:27:57 +02002557 ServiceWatcher service_watcher;
2558 int tag =
2559 manager()->RegisterDefaultServiceCallback(
2560 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2561 service_watcher.AsWeakPtr()));
2562 EXPECT_EQ(1, tag);
2563
mukesh agrawale37ad322013-10-08 16:33:56 -07002564 // Changing the ordering causes the DefaultService to change, and
2565 // appropriate notifications are sent.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002566 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002567 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2568 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002569 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002570 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002571 EXPECT_CALL(*manager_adaptor_,
2572 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002573 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002574 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002575
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002576 // Deregistering a DefaultServiceCallback works as expected. (Later
2577 // code causes DefaultService changes, but we see no further calls
2578 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002579 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002580 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002581
2582 // Deregistering the current DefaultService causes the other Service
2583 // to become default. Appropriate notifications are sent.
2584 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002585 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002586 EXPECT_CALL(*manager_adaptor_,
2587 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002588 mock_service1->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002589 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002590 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002591
mukesh agrawale37ad322013-10-08 16:33:56 -07002592 // Deregistering the only Service causes the DefaultService to become
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002593 // NULL. Appropriate notifications are sent.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002594 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002595 EXPECT_CALL(*manager_adaptor_,
2596 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002597 mock_service0->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002598 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002599 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002600
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002601 // An explicit sort doesn't change anything, and does not generate
2602 // an external notification.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002603 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002604 EXPECT_CALL(*manager_adaptor_,
2605 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002606 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002607}
2608
Darin Petkova5e07ef2012-07-09 14:27:57 +02002609TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2610 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2611 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2612
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002613 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002614 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002615
2616 scoped_refptr<MockService> mock_service(
2617 new NiceMock<MockService>(
2618 control_interface(), dispatcher(), metrics(), manager()));
2619 ServiceRefPtr service = mock_service;
2620 ServiceRefPtr null_service;
2621
2622 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2623 manager()->NotifyDefaultServiceChanged(null_service);
2624
2625 ServiceWatcher service_watcher1;
2626 ServiceWatcher service_watcher2;
2627 int tag1 =
2628 manager()->RegisterDefaultServiceCallback(
2629 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2630 service_watcher1.AsWeakPtr()));
2631 EXPECT_EQ(1, tag1);
2632 int tag2 =
2633 manager()->RegisterDefaultServiceCallback(
2634 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2635 service_watcher2.AsWeakPtr()));
2636 EXPECT_EQ(2, tag2);
2637
2638 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2639 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2640 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2641 manager()->NotifyDefaultServiceChanged(null_service);
2642
2643 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2644 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2645 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2646 manager()->NotifyDefaultServiceChanged(mock_service);
2647
2648 manager()->DeregisterDefaultServiceCallback(tag1);
2649 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2650 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2651 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2652 manager()->NotifyDefaultServiceChanged(mock_service);
2653 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2654
2655 manager()->DeregisterDefaultServiceCallback(tag2);
2656 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2657 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2658 manager()->NotifyDefaultServiceChanged(mock_service);
2659
2660 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2661 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2662}
2663
Peter Qiu9d581932014-04-14 16:37:37 -07002664TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2665 int connection_id1 = 100;
2666 int connection_id2 = 200;
2667 scoped_refptr<MockService> mock_service1 =
2668 new NiceMock<MockService>(control_interface(), dispatcher(),
2669 metrics(), manager());
2670 mock_service1->set_connection_id(connection_id1);
2671 scoped_refptr<MockService> mock_service2 =
2672 new NiceMock<MockService>(control_interface(), dispatcher(),
2673 metrics(), manager());
2674 mock_service2->set_connection_id(connection_id1);
2675 scoped_refptr<MockService> mock_service3 =
2676 new NiceMock<MockService>(control_interface(), dispatcher(),
2677 metrics(), manager());
2678 mock_service3->set_connection_id(connection_id2);
2679
2680 manager()->RegisterService(mock_service1);
2681 manager()->RegisterService(mock_service2);
2682 manager()->RegisterService(mock_service3);
2683
2684 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2685 manager()->ReportServicesOnSameNetwork(connection_id1);
2686
2687 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2688 manager()->ReportServicesOnSameNetwork(connection_id2);
2689}
2690
Gaurav Shah435de2c2011-11-17 19:01:07 -08002691TEST_F(ManagerTest, AvailableTechnologies) {
2692 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2693 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002694 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002695 manager(),
2696 "null4",
2697 "addr4",
2698 0));
2699 manager()->RegisterDevice(mock_devices_[0]);
2700 manager()->RegisterDevice(mock_devices_[1]);
2701 manager()->RegisterDevice(mock_devices_[2]);
2702 manager()->RegisterDevice(mock_devices_[3]);
2703
2704 ON_CALL(*mock_devices_[0].get(), technology())
2705 .WillByDefault(Return(Technology::kEthernet));
2706 ON_CALL(*mock_devices_[1].get(), technology())
2707 .WillByDefault(Return(Technology::kWifi));
2708 ON_CALL(*mock_devices_[2].get(), technology())
2709 .WillByDefault(Return(Technology::kCellular));
2710 ON_CALL(*mock_devices_[3].get(), technology())
2711 .WillByDefault(Return(Technology::kWifi));
2712
2713 set<string> expected_technologies;
2714 expected_technologies.insert(Technology::NameFromIdentifier(
2715 Technology::kEthernet));
2716 expected_technologies.insert(Technology::NameFromIdentifier(
2717 Technology::kWifi));
2718 expected_technologies.insert(Technology::NameFromIdentifier(
2719 Technology::kCellular));
2720 Error error;
2721 vector<string> technologies = manager()->AvailableTechnologies(&error);
2722
2723 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2724 ContainerEq(expected_technologies));
2725}
2726
2727TEST_F(ManagerTest, ConnectedTechnologies) {
2728 scoped_refptr<MockService> connected_service1(
2729 new NiceMock<MockService>(control_interface(),
2730 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002731 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002732 manager()));
2733 scoped_refptr<MockService> connected_service2(
2734 new NiceMock<MockService>(control_interface(),
2735 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002736 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002737 manager()));
2738 scoped_refptr<MockService> disconnected_service1(
2739 new NiceMock<MockService>(control_interface(),
2740 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002741 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002742 manager()));
2743 scoped_refptr<MockService> disconnected_service2(
2744 new NiceMock<MockService>(control_interface(),
2745 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002746 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002747 manager()));
2748
2749 ON_CALL(*connected_service1.get(), IsConnected())
2750 .WillByDefault(Return(true));
2751 ON_CALL(*connected_service2.get(), IsConnected())
2752 .WillByDefault(Return(true));
2753
2754 manager()->RegisterService(connected_service1);
2755 manager()->RegisterService(connected_service2);
2756 manager()->RegisterService(disconnected_service1);
2757 manager()->RegisterService(disconnected_service2);
2758
2759 manager()->RegisterDevice(mock_devices_[0]);
2760 manager()->RegisterDevice(mock_devices_[1]);
2761 manager()->RegisterDevice(mock_devices_[2]);
2762 manager()->RegisterDevice(mock_devices_[3]);
2763
2764 ON_CALL(*mock_devices_[0].get(), technology())
2765 .WillByDefault(Return(Technology::kEthernet));
2766 ON_CALL(*mock_devices_[1].get(), technology())
2767 .WillByDefault(Return(Technology::kWifi));
2768 ON_CALL(*mock_devices_[2].get(), technology())
2769 .WillByDefault(Return(Technology::kCellular));
2770 ON_CALL(*mock_devices_[3].get(), technology())
2771 .WillByDefault(Return(Technology::kWifi));
2772
2773 mock_devices_[0]->SelectService(connected_service1);
2774 mock_devices_[1]->SelectService(disconnected_service1);
2775 mock_devices_[2]->SelectService(disconnected_service2);
2776 mock_devices_[3]->SelectService(connected_service2);
2777
2778 set<string> expected_technologies;
2779 expected_technologies.insert(Technology::NameFromIdentifier(
2780 Technology::kEthernet));
2781 expected_technologies.insert(Technology::NameFromIdentifier(
2782 Technology::kWifi));
2783 Error error;
2784
2785 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2786 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2787 ContainerEq(expected_technologies));
2788}
2789
2790TEST_F(ManagerTest, DefaultTechnology) {
2791 scoped_refptr<MockService> connected_service(
2792 new NiceMock<MockService>(control_interface(),
2793 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002794 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002795 manager()));
2796 scoped_refptr<MockService> disconnected_service(
2797 new NiceMock<MockService>(control_interface(),
2798 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002799 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002800 manager()));
2801
2802 // Connected. WiFi.
2803 ON_CALL(*connected_service.get(), IsConnected())
2804 .WillByDefault(Return(true));
2805 ON_CALL(*connected_service.get(), state())
2806 .WillByDefault(Return(Service::kStateConnected));
2807 ON_CALL(*connected_service.get(), technology())
2808 .WillByDefault(Return(Technology::kWifi));
2809
2810 // Disconnected. Ethernet.
2811 ON_CALL(*disconnected_service.get(), technology())
2812 .WillByDefault(Return(Technology::kEthernet));
2813
2814 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002815 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002816 Error error;
2817 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2818
2819
2820 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002821 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002822 // Connected service should be brought to the front now.
2823 string expected_technology =
2824 Technology::NameFromIdentifier(Technology::kWifi);
2825 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2826}
2827
Paul Stewart212d60f2012-07-12 10:59:13 -07002828TEST_F(ManagerTest, Stop) {
2829 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002830 new NiceMock<MockProfile>(
2831 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002832 AdoptProfile(manager(), profile);
2833 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002834 new NiceMock<MockService>(control_interface(),
2835 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002836 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002837 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002838 manager()->RegisterService(service);
2839 manager()->RegisterDevice(mock_devices_[0]);
2840 EXPECT_CALL(*profile.get(),
2841 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2842 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002843 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002844 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2845 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002846 manager()->Stop();
2847}
2848
mukesh agrawal00917ce2011-11-22 23:56:55 +00002849TEST_F(ManagerTest, UpdateServiceConnected) {
2850 scoped_refptr<MockService> mock_service(
2851 new NiceMock<MockService>(control_interface(),
2852 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002853 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002854 manager()));
2855 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002856 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002857 EXPECT_FALSE(mock_service->auto_connect());
2858
Gaurav Shah435de2c2011-11-17 19:01:07 -08002859 EXPECT_CALL(*mock_service.get(), IsConnected())
2860 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002861 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002862 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2863 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2864 // would break the SortServices test. (crbug.com/206367)
2865 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002866 EXPECT_TRUE(mock_service->auto_connect());
2867}
2868
Paul Stewart2da34c02013-10-17 15:28:56 -07002869TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002870 // This tests the case where the user connects to a service that is
2871 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002872 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002873 scoped_refptr<MockService> mock_service(
2874 new NiceMock<MockService>(control_interface(),
2875 dispatcher(),
2876 metrics(),
2877 manager()));
2878 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002879 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002880 EXPECT_FALSE(mock_service->auto_connect());
2881
Gary Moraind93615e2012-04-27 11:50:03 -07002882 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002883 new MockProfile(
2884 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002885
Gary Moraind93615e2012-04-27 11:50:03 -07002886 mock_service->set_profile(profile);
2887 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002888 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002889 EXPECT_CALL(*profile,
2890 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002891 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002892 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2893 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2894 // would break the SortServices test. (crbug.com/206367)
2895 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002896 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002897 // This releases the ref on the mock profile.
2898 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002899}
2900
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002901TEST_F(ManagerTest, SaveSuccessfulService) {
2902 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002903 new StrictMock<MockProfile>(
2904 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002905 AdoptProfile(manager(), profile);
2906 scoped_refptr<MockService> service(
2907 new NiceMock<MockService>(control_interface(),
2908 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002909 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002910 manager()));
2911
2912 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2913 ServiceRefPtr expect_service(service.get());
2914
2915 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2916 .WillOnce(Return(false));
2917 manager()->RegisterService(service);
2918
2919 EXPECT_CALL(*service.get(), state())
2920 .WillRepeatedly(Return(Service::kStateConnected));
2921 EXPECT_CALL(*service.get(), IsConnected())
2922 .WillRepeatedly(Return(true));
2923 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2924 .WillOnce(Return(true));
2925 manager()->UpdateService(service);
2926}
2927
Darin Petkove7c6ad32012-06-29 10:22:09 +02002928TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002929 MockProfile *profile0 =
2930 new MockProfile(control_interface(), metrics(), manager(), "");
2931 MockProfile *profile1 =
2932 new MockProfile(control_interface(), metrics(), manager(), "");
2933 MockProfile *profile2 =
2934 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002935 AdoptProfile(manager(), profile0); // Passes ownership.
2936 AdoptProfile(manager(), profile1); // Passes ownership.
2937 AdoptProfile(manager(), profile2); // Passes ownership.
2938 DeviceRefPtr device_ref(mock_devices_[0].get());
2939 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2940 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2941 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2942 manager()->UpdateDevice(mock_devices_[0]);
2943}
2944
Paul Stewart1b253142012-01-26 14:05:52 -08002945TEST_F(ManagerTest, EnumerateProfiles) {
2946 vector<string> profile_paths;
2947 for (size_t i = 0; i < 10; i++) {
2948 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002949 new StrictMock<MockProfile>(
2950 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002951 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002952 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2953 .WillOnce(Return(profile_paths.back()));
2954 AdoptProfile(manager(), profile);
2955 }
2956
2957 Error error;
2958 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2959 EXPECT_TRUE(error.IsSuccess());
2960 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2961 for (size_t i = 0; i < profile_paths.size(); i++) {
2962 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2963 }
2964}
2965
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002966TEST_F(ManagerTest, AutoConnectOnRegister) {
2967 MockServiceRefPtr service = MakeAutoConnectableService();
2968 EXPECT_CALL(*service.get(), AutoConnect());
2969 manager()->RegisterService(service);
2970 dispatcher()->DispatchPendingEvents();
2971}
2972
2973TEST_F(ManagerTest, AutoConnectOnUpdate) {
2974 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002975 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002976 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002977 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002978 manager()->RegisterService(service1);
2979 manager()->RegisterService(service2);
2980 dispatcher()->DispatchPendingEvents();
2981
2982 EXPECT_CALL(*service1.get(), AutoConnect());
2983 EXPECT_CALL(*service2.get(), state())
2984 .WillRepeatedly(Return(Service::kStateFailure));
2985 EXPECT_CALL(*service2.get(), IsFailed())
2986 .WillRepeatedly(Return(true));
2987 EXPECT_CALL(*service2.get(), IsConnected())
2988 .WillRepeatedly(Return(false));
2989 manager()->UpdateService(service2);
2990 dispatcher()->DispatchPendingEvents();
2991}
2992
2993TEST_F(ManagerTest, AutoConnectOnDeregister) {
2994 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002995 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002996 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002997 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002998 manager()->RegisterService(service1);
2999 manager()->RegisterService(service2);
3000 dispatcher()->DispatchPendingEvents();
3001
3002 EXPECT_CALL(*service1.get(), AutoConnect());
3003 manager()->DeregisterService(service2);
3004 dispatcher()->DispatchPendingEvents();
3005}
3006
Daniel Eratfac09532014-04-17 20:25:59 -07003007TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003008 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003009 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02003010 SetPowerManager();
3011 EXPECT_CALL(*service, AutoConnect()).Times(0);
3012 manager()->RegisterService(service);
3013 dispatcher()->DispatchPendingEvents();
3014}
3015
Daniel Eratfac09532014-04-17 20:25:59 -07003016TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02003017 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07003018 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02003019 SetPowerManager();
3020 EXPECT_CALL(*service, AutoConnect());
3021 manager()->RegisterService(service);
3022 dispatcher()->DispatchPendingEvents();
3023}
3024
Paul Stewart63864b62012-11-07 15:10:55 -08003025TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
3026 SetRunning(false);
3027 MockServiceRefPtr service = MakeAutoConnectableService();
3028 EXPECT_CALL(*service, AutoConnect()).Times(0);
3029 manager()->RegisterService(service);
3030 dispatcher()->DispatchPendingEvents();
3031}
3032
Daniel Eratfac09532014-04-17 20:25:59 -07003033TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02003034 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02003035 SetPowerManager();
3036 EXPECT_CALL(*service, AutoConnect());
3037 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07003038 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003039 dispatcher()->DispatchPendingEvents();
3040
Daniel Eratfac09532014-04-17 20:25:59 -07003041 const int kSuspendId = 1;
3042 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
3043 OnSuspendImminent(kSuspendId);
3044 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02003045 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003046 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003047
Daniel Eratfac09532014-04-17 20:25:59 -07003048 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
3049 OnSuspendDone(kSuspendId);
3050 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02003051 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003052 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003053}
3054
Darin Petkov3ec55342012-09-28 14:04:44 +02003055TEST_F(ManagerTest, AddTerminationAction) {
Daniel Eratfac09532014-04-17 20:25:59 -07003056 EXPECT_CALL(*power_manager_, AddSuspendDelay(_, _, _, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02003057 SetPowerManager();
3058 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3059 manager()->AddTerminationAction("action1", base::Closure());
3060 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3061 manager()->AddTerminationAction("action2", base::Closure());
3062}
3063
3064TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003065 const char kKey1[] = "action1";
3066 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02003067
3068 MockPowerManager &power_manager = *power_manager_;
3069 SetPowerManager();
3070
3071 // Removing an action when the hook table is empty should not result in any
3072 // calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003073 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003074 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3075 manager()->RemoveTerminationAction("unknown");
3076 Mock::VerifyAndClearExpectations(&power_manager);
3077
Daniel Eratfac09532014-04-17 20:25:59 -07003078 EXPECT_CALL(power_manager, AddSuspendDelay(_, _, _, _, _))
3079 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003080 manager()->AddTerminationAction(kKey1, base::Closure());
3081 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3082 manager()->AddTerminationAction(kKey2, base::Closure());
Daniel Eratfac09532014-04-17 20:25:59 -07003083 Mock::VerifyAndClearExpectations(&power_manager);
Darin Petkov3ec55342012-09-28 14:04:44 +02003084
3085 // Removing an action that ends up with a non-empty hook table should not
3086 // result in any calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003087 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003088 manager()->RemoveTerminationAction(kKey1);
3089 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3090 Mock::VerifyAndClearExpectations(&power_manager);
3091
3092 // Removing the last action should trigger unregistering from the power
3093 // manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003094 EXPECT_CALL(power_manager, RemoveSuspendDelay(_))
Daniel Erat0818cca2012-12-14 10:16:21 -08003095 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003096 manager()->RemoveTerminationAction(kKey2);
3097 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3098}
3099
3100TEST_F(ManagerTest, RunTerminationActions) {
3101 TerminationActionTest test_action;
3102 const string kActionName = "action";
3103
3104 EXPECT_CALL(test_action, Done(_));
3105 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3106 test_action.AsWeakPtr()));
3107
3108 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3109 Bind(&TerminationActionTest::Action,
3110 test_action.AsWeakPtr()));
3111 test_action.set_manager(manager());
3112 EXPECT_CALL(test_action, Done(_));
3113 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3114 test_action.AsWeakPtr()));
3115}
3116
Daniel Erat0818cca2012-12-14 10:16:21 -08003117TEST_F(ManagerTest, OnSuspendImminent) {
3118 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02003119 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Eratfac09532014-04-17 20:25:59 -07003120 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003121 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003122 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02003123}
3124
3125TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003126 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02003127 Error error;
Daniel Eratfac09532014-04-17 20:25:59 -07003128 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003129 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003130 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003131}
3132
Paul Stewartc681fa02012-03-02 19:40:04 -08003133TEST_F(ManagerTest, RecheckPortal) {
3134 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3135 .WillOnce(Return(false));
3136 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3137 .WillOnce(Return(true));
3138 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3139 .Times(0);
3140
3141 manager()->RegisterDevice(mock_devices_[0]);
3142 manager()->RegisterDevice(mock_devices_[1]);
3143 manager()->RegisterDevice(mock_devices_[2]);
3144
3145 manager()->RecheckPortal(NULL);
3146}
3147
Paul Stewartd215af62012-04-24 23:25:50 -07003148TEST_F(ManagerTest, RecheckPortalOnService) {
3149 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3150 dispatcher(),
3151 metrics(),
3152 manager());
3153 EXPECT_CALL(*mock_devices_[0].get(),
3154 IsConnectedToService(IsRefPtrTo(service)))
3155 .WillOnce(Return(false));
3156 EXPECT_CALL(*mock_devices_[1].get(),
3157 IsConnectedToService(IsRefPtrTo(service)))
3158 .WillOnce(Return(true));
3159 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3160 .WillOnce(Return(true));
3161 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3162 .Times(0);
3163
3164 manager()->RegisterDevice(mock_devices_[0]);
3165 manager()->RegisterDevice(mock_devices_[1]);
3166 manager()->RegisterDevice(mock_devices_[2]);
3167
3168 manager()->RecheckPortalOnService(service);
3169}
3170
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003171TEST_F(ManagerTest, GetDefaultService) {
3172 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003173 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003174
3175 scoped_refptr<MockService> mock_service(
3176 new NiceMock<MockService>(control_interface(),
3177 dispatcher(),
3178 metrics(),
3179 manager()));
3180
3181 manager()->RegisterService(mock_service);
3182 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003183 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003184
3185 scoped_refptr<MockConnection> mock_connection(
3186 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003187 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003188 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003189 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003190
Paul Stewartce4ec192012-03-14 12:53:46 -07003191 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003192 manager()->DeregisterService(mock_service);
3193}
3194
Paul Stewart13ed2252012-03-21 12:52:46 -07003195TEST_F(ManagerTest, GetServiceWithGUID) {
3196 scoped_refptr<MockService> mock_service0(
3197 new NiceMock<MockService>(control_interface(),
3198 dispatcher(),
3199 metrics(),
3200 manager()));
3201
3202 scoped_refptr<MockService> mock_service1(
3203 new NiceMock<MockService>(control_interface(),
3204 dispatcher(),
3205 metrics(),
3206 manager()));
3207
Paul Stewartcb59fed2012-03-21 21:14:46 -07003208 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3209 .Times(0);
3210 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3211 .Times(0);
3212
Paul Stewart13ed2252012-03-21 12:52:46 -07003213 manager()->RegisterService(mock_service0);
3214 manager()->RegisterService(mock_service1);
3215
3216 const string kGUID0 = "GUID0";
3217 const string kGUID1 = "GUID1";
3218
3219 {
3220 Error error;
3221 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3222 EXPECT_FALSE(error.IsSuccess());
3223 EXPECT_FALSE(service);
3224 }
3225
3226 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003227 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003228
3229 {
3230 Error error;
3231 ServiceRefPtr service = manager()->GetService(args, &error);
3232 EXPECT_EQ(Error::kInvalidArguments, error.type());
3233 EXPECT_FALSE(service);
3234 }
3235
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003236 mock_service0->SetGuid(kGUID0, NULL);
3237 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003238
3239 {
3240 Error error;
3241 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3242 EXPECT_TRUE(error.IsSuccess());
3243 EXPECT_EQ(mock_service0.get(), service.get());
3244 }
3245
3246 {
3247 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003248 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3249 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003250 ServiceRefPtr service = manager()->GetService(args, &error);
3251 EXPECT_TRUE(error.IsSuccess());
3252 EXPECT_EQ(mock_service1.get(), service.get());
3253 }
3254
3255 manager()->DeregisterService(mock_service0);
3256 manager()->DeregisterService(mock_service1);
3257}
3258
Gary Morain028545d2012-04-07 14:55:52 -07003259
3260TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003261 EXPECT_FALSE(manager()->IsOnline());
3262 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3263
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003264 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003265 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003266 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3267 .Times(AnyNumber());
3268 scoped_refptr<MockService> mock_service0(
3269 new NiceMock<MockService>(control_interface(),
3270 dispatcher(),
3271 metrics(),
3272 manager()));
3273
3274 scoped_refptr<MockService> mock_service1(
3275 new NiceMock<MockService>(control_interface(),
3276 dispatcher(),
3277 metrics(),
3278 manager()));
3279
3280 EXPECT_CALL(*mock_service0.get(), IsConnected())
3281 .WillRepeatedly(Return(false));
3282 EXPECT_CALL(*mock_service1.get(), IsConnected())
3283 .WillRepeatedly(Return(false));
3284
3285 manager()->RegisterService(mock_service0);
3286 manager()->RegisterService(mock_service1);
3287
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003288 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003289 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3290
3291 manager()->DeregisterService(mock_service0);
3292 manager()->DeregisterService(mock_service1);
3293}
3294
3295TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003296 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003297 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003298 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3299 .Times(AnyNumber());
3300 scoped_refptr<MockService> mock_service0(
3301 new NiceMock<MockService>(control_interface(),
3302 dispatcher(),
3303 metrics(),
3304 manager()));
3305
3306 scoped_refptr<MockService> mock_service1(
3307 new NiceMock<MockService>(control_interface(),
3308 dispatcher(),
3309 metrics(),
3310 manager()));
3311
3312 EXPECT_CALL(*mock_service0.get(), IsConnected())
3313 .WillRepeatedly(Return(false));
3314 EXPECT_CALL(*mock_service1.get(), IsConnected())
3315 .WillRepeatedly(Return(true));
3316 EXPECT_CALL(*mock_service0.get(), state())
3317 .WillRepeatedly(Return(Service::kStateIdle));
3318 EXPECT_CALL(*mock_service1.get(), state())
3319 .WillRepeatedly(Return(Service::kStateConnected));
3320
3321 manager()->RegisterService(mock_service0);
3322 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003323 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003324
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003325 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003326 EXPECT_EQ("online", manager()->CalculateState(NULL));
3327
3328 manager()->DeregisterService(mock_service0);
3329 manager()->DeregisterService(mock_service1);
3330}
3331
Paul Stewart03e29f72013-09-26 00:49:48 -07003332TEST_F(ManagerTest, RefreshConnectionState) {
3333 EXPECT_CALL(*manager_adaptor_,
3334 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3335 RefreshConnectionState();
3336 Mock::VerifyAndClearExpectations(manager_adaptor_);
3337
3338 scoped_refptr<MockService> mock_service(
3339 new NiceMock<MockService>(control_interface(),
3340 dispatcher(),
3341 metrics(),
3342 manager()));
3343 EXPECT_CALL(*manager_adaptor_,
3344 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3345 manager()->RegisterService(mock_service);
3346 RefreshConnectionState();
3347
3348 scoped_refptr<MockConnection> mock_connection(
3349 new NiceMock<MockConnection>(device_info_.get()));
3350 mock_service->set_mock_connection(mock_connection);
3351 EXPECT_CALL(*mock_service, state())
3352 .WillOnce(Return(Service::kStateIdle));
3353 RefreshConnectionState();
3354
3355 Mock::VerifyAndClearExpectations(manager_adaptor_);
3356 EXPECT_CALL(*mock_service, state())
3357 .WillOnce(Return(Service::kStatePortal));
3358 EXPECT_CALL(*manager_adaptor_,
3359 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3360 RefreshConnectionState();
3361 Mock::VerifyAndClearExpectations(manager_adaptor_);
3362
3363 mock_service->set_mock_connection(NULL);
3364 manager()->DeregisterService(mock_service);
3365}
3366
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003367TEST_F(ManagerTest, StartupPortalList) {
3368 // Simulate loading value from the default profile.
3369 const string kProfileValue("wifi,vpn");
3370 manager()->props_.check_portal_list = kProfileValue;
3371
3372 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3373 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3374 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3375
3376 const string kStartupValue("cellular,ethernet");
3377 manager()->SetStartupPortalList(kStartupValue);
3378 // Ensure profile value is not overwritten, so when we save the default
3379 // profile, the correct value will still be written.
3380 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3381
3382 // However we should read back a different list.
3383 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3384 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3385 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3386
3387 const string kRuntimeValue("ppp");
3388 // Setting a runtime value over the control API should overwrite both
3389 // the profile value and what we read back.
3390 Error error;
3391 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003392 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003393 kRuntimeValue,
3394 &error);
3395 ASSERT_TRUE(error.IsSuccess());
3396 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3397 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3398 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3399 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3400}
3401
Paul Stewart036dba02012-08-07 12:34:41 -07003402TEST_F(ManagerTest, LinkMonitorEnabled) {
3403 const string kEnabledTechnologies("wifi,vpn");
3404 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3405 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3406 EXPECT_FALSE(
3407 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3408}
3409
Paul Stewart85aea152013-01-22 09:31:56 -08003410TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003411 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003412 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003413 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003414 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003415 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003416 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3417 AdoptProfile(manager(), profile);
3418 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3419 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3420 scoped_ptr<MockStore> store1(new MockStore);
3421 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3422}
3423
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003424TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003425 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003426 DisableTechnologyReplyHandler disable_technology_reply_handler;
3427 ResultCallback disable_technology_callback(
3428 Bind(&DisableTechnologyReplyHandler::ReportResult,
3429 disable_technology_reply_handler.AsWeakPtr()));
3430 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3431
Ben Chan923a5022013-09-20 11:23:23 -07003432 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003433 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003434 EXPECT_TRUE(error.IsSuccess());
3435
Joshua Krollda798622012-06-05 12:30:48 -07003436 ON_CALL(*mock_devices_[0], technology())
3437 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003438 ON_CALL(*mock_devices_[1], technology())
3439 .WillByDefault(Return(Technology::kCellular));
3440 ON_CALL(*mock_devices_[2], technology())
3441 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003442
3443 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003444 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003445
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003446 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003447 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3448 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003449 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003450 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003451 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003452 EXPECT_TRUE(error.IsSuccess());
3453
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003454 // Ethernet Device is enabled, and mock doesn't change error from
3455 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003456 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3457 mock_devices_[0]->enabled_ = true;
3458 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003459 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003460 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003461 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003462
3463 // Ethernet Device is disabled, and mock doesn't change error from
3464 // kOperationInitiated, so expect enable to say operation in progress.
3465 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3466 mock_devices_[0]->enabled_ = false;
3467 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003468 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003469 &error, disable_technology_callback);
3470 EXPECT_TRUE(error.IsOngoing());
3471
3472 // Cellular Device is enabled, but disable failed.
3473 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3474 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3475 mock_devices_[1]->enabled_ = true;
3476 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003477 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003478 &error, disable_technology_callback);
3479 EXPECT_EQ(Error::kPermissionDenied, error.type());
3480
3481 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3482 // if one is in progress (even if the other completed immediately).
3483 manager()->RegisterDevice(mock_devices_[2]);
3484 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3485 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3486 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3487 mock_devices_[1]->enabled_ = true;
3488 mock_devices_[2]->enabled_ = true;
3489 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003490 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003491 &error, disable_technology_callback);
3492 EXPECT_TRUE(error.IsOngoing());
3493
3494 // ...and order doesn't matter.
3495 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3496 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3497 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3498 mock_devices_[1]->enabled_ = true;
3499 mock_devices_[2]->enabled_ = true;
3500 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003501 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003502 &error, disable_technology_callback);
3503 EXPECT_TRUE(error.IsOngoing());
3504 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3505
3506 // Multiple Cellular Devices in enabled state. Even if all disable
3507 // operations complete asynchronously, we only get one call to the
3508 // DisableTechnologyReplyHandler::ReportResult.
3509 ResultCallback device1_result_callback;
3510 ResultCallback device2_result_callback;
3511 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3512 .WillOnce(SaveArg<2>(&device1_result_callback));
3513 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3514 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3515 SaveArg<2>(&device2_result_callback)));
3516 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3517 mock_devices_[1]->enabled_ = true;
3518 mock_devices_[2]->enabled_ = true;
3519 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003520 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003521 &error, disable_technology_callback);
3522 EXPECT_TRUE(error.IsOngoing());
3523 device1_result_callback.Run(Error(Error::kSuccess));
3524 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003525}
3526
Paul Stewart4d5efb72012-09-17 12:24:34 -07003527TEST_F(ManagerTest, IgnoredSearchList) {
3528 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003529 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003530 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003531
3532 const string kIgnored0 = "chromium.org";
3533 ignored_paths.push_back(kIgnored0);
3534 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003535 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003536 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3537
3538 const string kIgnored1 = "google.com";
3539 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3540 ignored_paths.push_back(kIgnored1);
3541 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003542 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003543 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3544
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003545 ignored_paths.clear();
3546 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3547 SetIgnoredDNSSearchPaths("", NULL);
3548 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3549
Paul Stewart4d5efb72012-09-17 12:24:34 -07003550 SetResolver(Resolver::GetInstance());
3551}
3552
Paul Stewartbfb82552012-10-24 16:48:48 -07003553TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3554 // Test to make sure that every service state-change causes the
3555 // Manager to emit a new service list.
3556 scoped_refptr<MockService> mock_service(
3557 new NiceMock<MockService>(control_interface(),
3558 dispatcher(),
3559 metrics(),
3560 manager()));
3561 EXPECT_CALL(*mock_service, state())
3562 .WillRepeatedly(Return(Service::kStateIdle));
3563
3564 manager()->RegisterService(mock_service);
3565 EXPECT_CALL(
3566 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003567 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003568 EXPECT_CALL(
3569 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003570 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003571 CompleteServiceSort();
3572
3573 Mock::VerifyAndClearExpectations(manager_adaptor_);
3574 EXPECT_CALL(
3575 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003576 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003577 EXPECT_CALL(
3578 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003579 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003580 manager()->UpdateService(mock_service.get());
3581 CompleteServiceSort();
3582
3583 manager()->DeregisterService(mock_service);
3584}
3585
3586TEST_F(ManagerTest, EnumerateServices) {
3587 scoped_refptr<MockService> mock_service(
3588 new NiceMock<MockService>(control_interface(),
3589 dispatcher(),
3590 metrics(),
3591 manager()));
3592 manager()->RegisterService(mock_service);
3593
3594 EXPECT_CALL(*mock_service, state())
3595 .WillRepeatedly(Return(Service::kStateConnected));
3596 EXPECT_CALL(*mock_service, IsVisible())
3597 .WillRepeatedly(Return(false));
3598 EXPECT_TRUE(EnumerateAvailableServices().empty());
3599 EXPECT_TRUE(EnumerateWatchedServices().empty());
3600
3601 EXPECT_CALL(*mock_service, state())
3602 .WillRepeatedly(Return(Service::kStateIdle));
3603 EXPECT_TRUE(EnumerateAvailableServices().empty());
3604 EXPECT_TRUE(EnumerateWatchedServices().empty());
3605
3606 EXPECT_CALL(*mock_service, IsVisible())
3607 .WillRepeatedly(Return(true));
3608 Service::ConnectState unwatched_states[] = {
3609 Service::kStateUnknown,
3610 Service::kStateIdle,
3611 Service::kStateFailure
3612 };
3613 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3614 EXPECT_CALL(*mock_service, state())
3615 .WillRepeatedly(Return(unwatched_states[i]));
3616 EXPECT_FALSE(EnumerateAvailableServices().empty());
3617 EXPECT_TRUE(EnumerateWatchedServices().empty());
3618 }
3619
3620 Service::ConnectState watched_states[] = {
3621 Service::kStateAssociating,
3622 Service::kStateConfiguring,
3623 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003624 Service::kStatePortal,
3625 Service::kStateOnline
3626 };
3627 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3628 EXPECT_CALL(*mock_service, state())
3629 .WillRepeatedly(Return(watched_states[i]));
3630 EXPECT_FALSE(EnumerateAvailableServices().empty());
3631 EXPECT_FALSE(EnumerateWatchedServices().empty());
3632 }
3633
3634 manager()->DeregisterService(mock_service);
3635}
3636
Paul Stewart39db5ca2013-03-18 14:15:17 -07003637TEST_F(ManagerTest, ConnectToBestServices) {
3638 scoped_refptr<MockService> wifi_service0(
3639 new NiceMock<MockService>(control_interface(),
3640 dispatcher(),
3641 metrics(),
3642 manager()));
3643 EXPECT_CALL(*wifi_service0.get(), state())
3644 .WillRepeatedly(Return(Service::kStateIdle));
3645 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3646 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003647 wifi_service0->SetConnectable(true);
3648 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003649 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3650 EXPECT_CALL(*wifi_service0.get(), technology())
3651 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003652 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3653 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003654 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3655 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003656
3657 scoped_refptr<MockService> wifi_service1(
3658 new NiceMock<MockService>(control_interface(),
3659 dispatcher(),
3660 metrics(),
3661 manager()));
3662 EXPECT_CALL(*wifi_service1.get(), state())
3663 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003664 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3665 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003666 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3667 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003668 wifi_service1->SetAutoConnect(true);
3669 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003670 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3671 EXPECT_CALL(*wifi_service1.get(), technology())
3672 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003673 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3674 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003675
3676 scoped_refptr<MockService> wifi_service2(
3677 new NiceMock<MockService>(control_interface(),
3678 dispatcher(),
3679 metrics(),
3680 manager()));
3681 EXPECT_CALL(*wifi_service2.get(), state())
3682 .WillRepeatedly(Return(Service::kStateConnected));
3683 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3684 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003685 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3686 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003687 wifi_service2->SetAutoConnect(true);
3688 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003689 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3690 EXPECT_CALL(*wifi_service2.get(), technology())
3691 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003692 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3693 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003694
3695 manager()->RegisterService(wifi_service0);
3696 manager()->RegisterService(wifi_service1);
3697 manager()->RegisterService(wifi_service2);
3698
3699 CompleteServiceSort();
3700 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3701
3702 scoped_refptr<MockService> cell_service(
3703 new NiceMock<MockService>(control_interface(),
3704 dispatcher(),
3705 metrics(),
3706 manager()));
3707
3708 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003709 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003710 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003711 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003712 EXPECT_CALL(*cell_service.get(), IsVisible())
3713 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003714 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003715 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003716 EXPECT_CALL(*cell_service.get(), technology())
3717 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003718 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3719 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003720 manager()->RegisterService(cell_service);
3721
Arman Uguray6fe4f262013-08-02 20:21:55 -07003722 scoped_refptr<MockService> wimax_service(
3723 new NiceMock<MockService>(control_interface(),
3724 dispatcher(),
3725 metrics(),
3726 manager()));
3727
3728 EXPECT_CALL(*wimax_service.get(), state())
3729 .WillRepeatedly(Return(Service::kStateConnected));
3730 EXPECT_CALL(*wimax_service.get(), IsConnected())
3731 .WillRepeatedly(Return(true));
3732 EXPECT_CALL(*wimax_service.get(), IsVisible())
3733 .WillRepeatedly(Return(true));
3734 wimax_service->SetAutoConnect(true);
3735 wimax_service->SetConnectable(true);
3736 EXPECT_CALL(*wimax_service.get(), technology())
3737 .WillRepeatedly(Return(Technology::kWiMax));
3738 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3739 .WillRepeatedly(Return(false));
3740 manager()->RegisterService(wimax_service);
3741
Paul Stewart39db5ca2013-03-18 14:15:17 -07003742 scoped_refptr<MockService> vpn_service(
3743 new NiceMock<MockService>(control_interface(),
3744 dispatcher(),
3745 metrics(),
3746 manager()));
3747
3748 EXPECT_CALL(*vpn_service.get(), state())
3749 .WillRepeatedly(Return(Service::kStateIdle));
3750 EXPECT_CALL(*vpn_service.get(), IsConnected())
3751 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003752 EXPECT_CALL(*vpn_service.get(), IsVisible())
3753 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003754 wifi_service2->SetAutoConnect(false);
3755 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003756 EXPECT_CALL(*vpn_service.get(), technology())
3757 .WillRepeatedly(Return(Technology::kVPN));
3758 manager()->RegisterService(vpn_service);
3759
3760 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003761 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003762
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003763 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3764 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003765 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003766 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3767 .Times(0); // Explicitly disconnected.
3768 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003769 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003770
3771 manager()->ConnectToBestServices(NULL);
3772 dispatcher()->DispatchPendingEvents();
3773
3774 // After this operation, since the Connect calls above are mocked and
3775 // no actual state changes have occurred, we should expect that the
3776 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003777 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003778}
3779
Christopher Wiley83889862013-05-02 15:55:09 -07003780TEST_F(ManagerTest, VerifyWhenNotConnected) {
3781 const string kFakeCertificate("fake cert");
3782 const string kFakePublicKey("fake public key");
3783 const string kFakeNonce("fake public key");
3784 const string kFakeSignedData("fake signed data");
3785 const string kFakeUdn("fake udn");
3786 const vector<uint8_t> kSSID(10, 87);
3787 const string kConfiguredSSID("AConfiguredDestination");
3788 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3789 kConfiguredSSID.end());
3790 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3791 scoped_refptr<MockWiFiService> mock_destination(
3792 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3793 metrics(), manager(), wifi_provider_,
3794 kSSID, "", "none", false));
3795 // Register this service, but don't mark it as connected.
3796 manager()->RegisterService(mock_destination);
3797 // Verify that if we're not connected to anything, verification fails.
3798 {
3799 LOG(INFO) << "Can't verify if not connected.";
3800 EXPECT_CALL(*crypto_util_proxy_,
3801 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3802 Error error(Error::kOperationInitiated);
3803 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3804 kFakeSignedData, kFakeUdn, "", "",
3805 ResultBoolCallback(), &error);
3806 EXPECT_TRUE(error.IsFailure());
3807 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3808 }
3809 {
3810 // However, if the destination is already configured, we might be
3811 // connected to it via something other than WiFi, and we shouldn't
3812 // enforce the WiFi check.
3813 EXPECT_CALL(*crypto_util_proxy_,
3814 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3815 kFakeSignedData, kFakeUdn,
3816 kConfiguredSSIDVector, kConfiguredBSSID,
3817 _, _)).Times(1).WillOnce(Return(true));
3818 Error error(Error::kOperationInitiated);
3819 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3820 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3821 kConfiguredBSSID, ResultBoolCallback(),
3822 &error);
3823 EXPECT_FALSE(error.IsFailure());
3824 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3825 }
3826}
3827
Christopher Wiley1057cd72013-02-28 15:21:29 -08003828TEST_F(ManagerTest, VerifyDestination) {
3829 const string kFakeCertificate("fake cert");
3830 const string kFakePublicKey("fake public key");
3831 const string kFakeNonce("fake public key");
3832 const string kFakeSignedData("fake signed data");
3833 const string kFakeUdn("fake udn");
3834 const char kSSIDStr[] = "fake ssid";
3835 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003836 const string kConfiguredSSID("AConfiguredDestination");
3837 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3838 kConfiguredSSID.end());
3839 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003840 const string kFakeData("muffin man");
3841 scoped_refptr<MockWiFiService> mock_destination(
3842 new NiceMock<MockWiFiService>(control_interface(),
3843 dispatcher(),
3844 metrics(),
3845 manager(),
3846 wifi_provider_,
3847 kSSID,
3848 "",
3849 "none",
3850 false));
3851 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003852 // Making the service look online will let service lookup in
3853 // VerifyDestinatoin succeed.
3854 EXPECT_CALL(*mock_destination.get(), IsConnected())
3855 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003856 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003857
3858 // Lead off by verifying that the basic VerifyDestination flow works.
3859 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003860 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003861 ResultBoolCallback passed_down_callback;
3862 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3863 kFakePublicKey,
3864 kFakeNonce,
3865 kFakeSignedData,
3866 kFakeUdn,
3867 kSSID,
3868 _,
3869 _,
3870 _))
3871 .Times(1)
3872 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3873 // Ask the manager to verify the current destination. This should look
3874 // up our previously registered service, and pass some metadata about
3875 // that service down to the CryptoUtilProxy to verify.
3876 Error error(Error::kOperationInitiated);
3877 ResultBoolCallback cb = Bind(
3878 &DestinationVerificationTest::ResultBoolCallbackStub,
3879 dv_test.AsWeakPtr());
3880 manager()->VerifyDestination(kFakeCertificate,
3881 kFakePublicKey,
3882 kFakeNonce,
3883 kFakeSignedData,
3884 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003885 // Ask to be verified against that service.
3886 "", "",
3887 cb,
3888 &error);
3889 // We assert here, because if the operation is not ongoing, it is
3890 // inconsistent with shim behavior to call the callback anyway.
3891 ASSERT_TRUE(error.IsOngoing());
3892 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3893 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3894 // Call the callback passed into the CryptoUtilProxy, which
3895 // should find its way into the callback passed into the manager.
3896 // In real code, that callback passed into the manager is from the
3897 // DBus adaptor.
3898 Error e;
3899 passed_down_callback.Run(e, true);
3900 Mock::VerifyAndClearExpectations(&dv_test);
3901 }
3902
Christopher Wiley1057cd72013-02-28 15:21:29 -08003903 // Now for a slightly more complex variant. When we encrypt data,
3904 // we do the same verification step but monkey with the callback to
3905 // link ourselves to an encrypt step afterward.
3906 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003907 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003908 ResultBoolCallback passed_down_callback;
3909 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3910 kFakePublicKey,
3911 kFakeNonce,
3912 kFakeSignedData,
3913 kFakeUdn,
3914 kSSID,
3915 _,
3916 _,
3917 _))
3918 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3919
3920 Error error(Error::kOperationInitiated);
3921 ResultStringCallback cb = Bind(
3922 &DestinationVerificationTest::ResultStringCallbackStub,
3923 dv_test.AsWeakPtr());
3924 manager()->VerifyAndEncryptData(kFakeCertificate,
3925 kFakePublicKey,
3926 kFakeNonce,
3927 kFakeSignedData,
3928 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003929 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003930 kFakeData,
3931 cb,
3932 &error);
3933 ASSERT_TRUE(error.IsOngoing());
3934 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3935 // Now, if we call that passed down callback, we should see encrypt being
3936 // called.
3937 ResultStringCallback second_passed_down_callback;
3938 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3939 kFakeData,
3940 _,
3941 _))
3942 .Times(1)
3943 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3944 Return(true)));
3945 Error e;
3946 passed_down_callback.Run(e, true);
3947 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3948 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3949 // And if we call the second passed down callback, we should see the
3950 // original function we passed down to VerifyDestination getting called.
3951 e.Reset();
3952 second_passed_down_callback.Run(e, "");
3953 Mock::VerifyAndClearExpectations(&dv_test);
3954 }
3955
3956 // If verification fails on the way to trying to encrypt, we should ditch
3957 // without calling encrypt at all.
3958 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003959 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003960 ResultBoolCallback passed_down_callback;
3961 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3962 kFakePublicKey,
3963 kFakeNonce,
3964 kFakeSignedData,
3965 kFakeUdn,
3966 kSSID,
3967 _,
3968 _,
3969 _))
3970 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3971
3972 Error error(Error::kOperationInitiated);
3973 ResultStringCallback cb = Bind(
3974 &DestinationVerificationTest::ResultStringCallbackStub,
3975 dv_test.AsWeakPtr());
3976 manager()->VerifyAndEncryptData(kFakeCertificate,
3977 kFakePublicKey,
3978 kFakeNonce,
3979 kFakeSignedData,
3980 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003981 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003982 kFakeData,
3983 cb,
3984 &error);
3985 ASSERT_TRUE(error.IsOngoing());
3986 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3987 Error e(Error::kOperationFailed);
3988 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3989 // Although we're ditching, this callback is what cleans up the pending
3990 // DBus call.
3991 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3992 passed_down_callback.Run(e, false);
3993 Mock::VerifyAndClearExpectations(&dv_test);
3994 }
3995}
3996
Paul Stewartd2e1c362013-03-03 19:06:07 -08003997TEST_F(ManagerTest, IsProfileBefore) {
3998 scoped_refptr<MockProfile> profile0(
3999 new NiceMock<MockProfile>(
4000 control_interface(), metrics(), manager(), ""));
4001 scoped_refptr<MockProfile> profile1(
4002 new NiceMock<MockProfile>(
4003 control_interface(), metrics(), manager(), ""));
4004
4005 AdoptProfile(manager(), profile0);
4006 AdoptProfile(manager(), profile1); // profile1 is after profile0.
4007 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
4008 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
4009
4010 // A few abnormal cases, but it's good to track their behavior.
4011 scoped_refptr<MockProfile> profile2(
4012 new NiceMock<MockProfile>(
4013 control_interface(), metrics(), manager(), ""));
4014 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
4015 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
4016 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
4017 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
4018}
4019
Paul Stewart967eaeb2013-04-25 19:53:07 -07004020TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
4021 MockStore storage0;
4022 MockStore storage1;
4023 MockStore storage2;
4024
4025 scoped_refptr<MockProfile> profile0(
4026 new NiceMock<MockProfile>(
4027 control_interface(), metrics(), manager(), ""));
4028 scoped_refptr<MockProfile> profile1(
4029 new NiceMock<MockProfile>(
4030 control_interface(), metrics(), manager(), ""));
4031 scoped_refptr<MockProfile> profile2(
4032 new NiceMock<MockProfile>(
4033 control_interface(), metrics(), manager(), ""));
4034
4035 AdoptProfile(manager(), profile0);
4036 AdoptProfile(manager(), profile1);
4037 AdoptProfile(manager(), profile2);
4038
4039 scoped_refptr<MockService> service(
4040 new NiceMock<MockService>(control_interface(),
4041 dispatcher(),
4042 metrics(),
4043 manager()));
4044
4045 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4046 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4047 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4048
4049 const string kEntry0("aluminum_crutch");
4050 const string kEntry2("rehashed_faces");
4051
4052 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4053 .WillOnce(Return(kEntry0));
4054 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4055 .WillOnce(Return(""));
4056 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4057 .WillOnce(Return(kEntry2));
4058
4059 const string kProfileRpc0("service_station");
4060 const string kProfileRpc2("crystal_tiaras");
4061
4062 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4063 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4064 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4065
4066 map<string, string> entries =
4067 manager()->GetLoadableProfileEntriesForService(service);
4068 EXPECT_EQ(2, entries.size());
4069 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4070 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4071 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4072 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4073}
4074
mukesh agrawal00752532013-05-03 15:46:55 -07004075TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004076 // We need a real glib here, so that profiles are persisted.
4077 GLib glib;
4078 ScopedTempDir temp_dir;
4079 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4080 Manager manager(control_interface(),
4081 dispatcher(),
4082 metrics(),
4083 &glib,
4084 run_path(),
4085 storage_path(),
4086 temp_dir.path().value());
4087 // Can't use |wifi_provider_|, because it's owned by the Manager
4088 // object in the fixture.
4089 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4090 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004091 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004092 // Give manager a valid place to write the user profile list.
4093 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4094
4095 // With no user profiles, the WiFiProvider should be called once
4096 // (for the default profile).
4097 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4098 manager.InitializeProfiles();
4099 Mock::VerifyAndClearExpectations(wifi_provider);
4100
4101 // With |n| user profiles, the WiFiProvider should be called |n+1|
4102 // times. First, create 2 user profiles...
4103 const char kProfile0[] = "~user/profile0";
4104 const char kProfile1[] = "~user/profile1";
4105 string profile_rpc_path;
4106 Error error;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004107 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawald142fd62013-05-01 16:50:57 -07004108 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4109 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4110 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4111 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4112
4113 // ... then reset manager state ...
4114 manager.profiles_.clear();
4115
4116 // ...then check that the WiFiProvider is notified about all three
4117 // profiles (one default, two user).
4118 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4119 manager.InitializeProfiles();
4120 Mock::VerifyAndClearExpectations(wifi_provider);
4121}
4122
mukesh agrawal00752532013-05-03 15:46:55 -07004123TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4124 // We need a real glib here, so that profiles are persisted.
4125 GLib glib;
4126 ScopedTempDir temp_dir;
4127 scoped_ptr<Manager> manager;
4128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4129
4130 // Instantiate a Manager with empty persistent storage. Check that
4131 // defaults are set.
4132 //
4133 // Note that we use the same directory for default and user profiles.
4134 // This doesn't affect the test results, because we don't push a
4135 // user profile.
4136 manager.reset(new Manager(control_interface(),
4137 dispatcher(),
4138 metrics(),
4139 &glib,
4140 run_path(),
4141 temp_dir.path().value(),
4142 temp_dir.path().value()));
4143 manager->InitializeProfiles();
4144 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4145 manager->props_.check_portal_list);
4146 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4147 manager->props_.ignored_dns_search_paths);
4148 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4149 manager->props_.link_monitor_technologies);
4150 EXPECT_EQ(PortalDetector::kDefaultURL,
4151 manager->props_.portal_url);
4152 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4153 manager->props_.portal_check_interval_seconds);
4154
4155 // Change one of the settings.
4156 static const string kCustomCheckPortalList = "fiber0";
4157 Error error;
4158 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4159 manager->profiles_[0]->Save();
4160
4161 // Instantiate a new manager. It should have our settings for
4162 // check_portal_list, rather than the default.
4163 manager.reset(new Manager(control_interface(),
4164 dispatcher(),
4165 metrics(),
4166 &glib,
4167 run_path(),
4168 temp_dir.path().value(),
4169 temp_dir.path().value()));
4170 manager->InitializeProfiles();
4171 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4172
4173 // If we clear the persistent storage, we again get the default value.
4174 ASSERT_TRUE(temp_dir.Delete());
4175 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4176 manager.reset(new Manager(control_interface(),
4177 dispatcher(),
4178 metrics(),
4179 &glib,
4180 run_path(),
4181 temp_dir.path().value(),
4182 temp_dir.path().value()));
4183 manager->InitializeProfiles();
4184 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4185 manager->props_.check_portal_list);
4186}
4187
mukesh agrawalb94adde2013-08-22 18:17:26 -07004188TEST_F(ManagerTest, ProfileStackChangeLogging) {
4189 // We use a real glib here, since Manager and Profile don't provide an
4190 // easy way to mock out KeyFileStore.
4191 GLib glib;
4192 ScopedTempDir temp_dir;
4193 scoped_ptr<Manager> manager;
4194 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4195 manager.reset(new Manager(control_interface(),
4196 dispatcher(),
4197 metrics(),
4198 &glib,
4199 run_path(),
4200 temp_dir.path().value(),
4201 temp_dir.path().value()));
4202
4203 ScopedMockLog log;
4204 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4205 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4206 manager->InitializeProfiles();
4207
4208 const char kProfile0[] = "~user/profile0";
4209 const char kProfile1[] = "~user/profile1";
4210 const char kProfile2[] = "~user/profile2";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004211 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawalb94adde2013-08-22 18:17:26 -07004212 TestCreateProfile(manager.get(), kProfile0);
4213 TestCreateProfile(manager.get(), kProfile1);
4214 TestCreateProfile(manager.get(), kProfile2);
4215
4216 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4217 TestPushProfile(manager.get(), kProfile0);
4218
4219 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4220 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4221
4222 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4223 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4224
4225 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4226 TestPopProfile(manager.get(), kProfile2);
4227
4228 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4229 TestPopAnyProfile(manager.get());
4230
4231 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4232 TestPopAllUserProfiles(manager.get());
4233}
4234
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004235// Custom property setters should return false, and make no changes, if
4236// the new value is the same as the old value.
4237TEST_F(ManagerTest, CustomSetterNoopChange) {
4238 // SetCheckPortalList
4239 {
4240 static const string kCheckPortalList = "weird-device,weirder-device";
4241 Error error;
4242 // Set to known value.
4243 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4244 EXPECT_TRUE(error.IsSuccess());
4245 // Set to same value.
4246 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4247 EXPECT_TRUE(error.IsSuccess());
4248 }
4249
4250 // SetIgnoredDNSSearchPaths
4251 {
4252 NiceMock<MockResolver> resolver;
4253 static const string kIgnoredPaths = "example.com,example.org";
4254 Error error;
4255 SetResolver(&resolver);
4256 // Set to known value.
4257 EXPECT_CALL(resolver, set_ignored_search_list(_));
4258 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4259 EXPECT_TRUE(error.IsSuccess());
4260 Mock::VerifyAndClearExpectations(&resolver);
4261 // Set to same value.
4262 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4263 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4264 EXPECT_TRUE(error.IsSuccess());
4265 Mock::VerifyAndClearExpectations(&resolver);
4266 }
4267}
4268
Paul Stewart7de7e022013-08-28 09:42:50 -07004269TEST_F(ManagerTest, GeoLocation) {
4270 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4271
4272 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4273 dispatcher(),
4274 metrics(),
4275 manager(),
4276 "null",
4277 "addr",
4278 0));
4279
4280 // Manager should ignore gelocation info from technologies it does not know.
4281 EXPECT_CALL(*device, technology())
4282 .Times(AtLeast(1))
4283 .WillRepeatedly(Return(Technology::kEthernet));
4284 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4285 manager()->OnDeviceGeolocationInfoUpdated(device);
4286 Mock::VerifyAndClearExpectations(device);
4287 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4288
4289 // Manager should add WiFi geolocation info.
4290 EXPECT_CALL(*device, technology())
4291 .Times(AtLeast(1))
4292 .WillRepeatedly(Return(Technology::kWifi));
4293 EXPECT_CALL(*device, GetGeolocationObjects())
4294 .WillOnce(Return(vector<GeolocationInfo>()));
4295 manager()->OnDeviceGeolocationInfoUpdated(device);
4296 Mock::VerifyAndClearExpectations(device);
4297 auto location_infos = manager()->GetNetworksForGeolocation();
4298 EXPECT_EQ(1, location_infos.size());
4299 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4300
4301 // Manager should inclusively add cellular info.
4302 EXPECT_CALL(*device, technology())
4303 .Times(AtLeast(1))
4304 .WillRepeatedly(Return(Technology::kCellular));
4305 EXPECT_CALL(*device, GetGeolocationObjects())
4306 .WillOnce(Return(vector<GeolocationInfo>()));
4307 manager()->OnDeviceGeolocationInfoUpdated(device);
4308 location_infos = manager()->GetNetworksForGeolocation();
4309 EXPECT_EQ(2, location_infos.size());
4310 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4311 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4312}
4313
Peter Qiu574996a2014-04-04 10:55:47 -07004314TEST_F(ManagerTest, IsWifiIdle) {
4315 // No registered service.
4316 EXPECT_FALSE(manager()->IsWifiIdle());
4317
4318 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4319 dispatcher(),
4320 metrics(),
4321 manager()));
4322
4323 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4324 dispatcher(),
4325 metrics(),
4326 manager()));
4327
4328 manager()->RegisterService(wifi_service);
4329 manager()->RegisterService(cell_service);
4330
4331 EXPECT_CALL(*wifi_service.get(), technology())
4332 .WillRepeatedly(Return(Technology::kWifi));
4333 EXPECT_CALL(*cell_service.get(), technology())
4334 .WillRepeatedly(Return(Technology::kCellular));
4335
4336 // Cellular is connected.
4337 EXPECT_CALL(*cell_service.get(), IsConnected())
4338 .WillRepeatedly(Return(true));
4339 manager()->UpdateService(cell_service);
4340
4341 // No wifi connection attempt.
4342 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4343 .WillRepeatedly(Return(false));
4344 EXPECT_CALL(*wifi_service.get(), IsConnected())
4345 .WillRepeatedly(Return(false));
4346 manager()->UpdateService(wifi_service);
4347 EXPECT_TRUE(manager()->IsWifiIdle());
4348
4349 // Attempt wifi connection.
4350 Mock::VerifyAndClearExpectations(wifi_service);
4351 EXPECT_CALL(*wifi_service.get(), technology())
4352 .WillRepeatedly(Return(Technology::kWifi));
4353 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4354 .WillRepeatedly(Return(true));
4355 EXPECT_CALL(*wifi_service.get(), IsConnected())
4356 .WillRepeatedly(Return(false));
4357 manager()->UpdateService(wifi_service);
4358 EXPECT_FALSE(manager()->IsWifiIdle());
4359
4360 // wifi connected.
4361 Mock::VerifyAndClearExpectations(wifi_service);
4362 EXPECT_CALL(*wifi_service.get(), technology())
4363 .WillRepeatedly(Return(Technology::kWifi));
4364 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4365 .WillRepeatedly(Return(false));
4366 EXPECT_CALL(*wifi_service.get(), IsConnected())
4367 .WillRepeatedly(Return(true));
4368 manager()->UpdateService(wifi_service);
4369 EXPECT_FALSE(manager()->IsWifiIdle());
4370}
4371
Chris Masone9be4a9d2011-05-16 15:44:09 -07004372} // namespace shill