blob: 335be36d19e22f85a72e8afc515953ff73a6be34 [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,
201 const string &profile_identifier,
202 const string &service_name) {
203 GLib glib;
204 KeyFileStore store(&glib);
205 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
206 return store.Open() &&
207 store.SetString(service_name, "rather", "irrelevant") &&
208 store.Close();
209 }
210
211 Error::Type TestCreateProfile(Manager *manager, const string &name) {
212 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800213 string path;
214 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700215 return error.type();
216 }
217
218 Error::Type TestPopAnyProfile(Manager *manager) {
219 Error error;
220 manager->PopAnyProfile(&error);
221 return error.type();
222 }
223
Paul Stewart307c2502013-03-23 12:32:10 -0700224 Error::Type TestPopAllUserProfiles(Manager *manager) {
225 Error error;
226 manager->PopAllUserProfiles(&error);
227 return error.type();
228 }
229
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700230 Error::Type TestPopProfile(Manager *manager, const string &name) {
231 Error error;
232 manager->PopProfile(name, &error);
233 return error.type();
234 }
235
236 Error::Type TestPushProfile(Manager *manager, const string &name) {
237 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800238 string path;
239 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700240 return error.type();
241 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000242
Paul Stewartf3eced92013-04-17 12:18:22 -0700243 Error::Type TestInsertUserProfile(Manager *manager,
244 const string &name,
245 const string &user_hash) {
246 Error error;
247 string path;
248 manager->InsertUserProfile(name, user_hash, &path, &error);
249 return error.type();
250 }
251
Paul Stewartd2e1c362013-03-03 19:06:07 -0800252 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
253 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700254 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800255 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800256 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200257 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700258 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800259 return profile;
260 }
261
262 void AddMockProfileToManager(Manager *manager) {
263 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700264 }
265
Paul Stewartdfa46052012-06-26 09:44:14 -0700266 void CompleteServiceSort() {
267 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
268 dispatcher()->DispatchPendingEvents();
269 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
270 }
271
Paul Stewart03e29f72013-09-26 00:49:48 -0700272 void RefreshConnectionState() {
273 manager()->RefreshConnectionState();
274 }
275
Paul Stewart49739c02012-08-08 17:24:03 -0700276 RpcIdentifier GetDefaultServiceRpcIdentifier() {
277 return manager()->GetDefaultServiceRpcIdentifier(NULL);
278 }
279
Paul Stewart4d5efb72012-09-17 12:24:34 -0700280 void SetResolver(Resolver *resolver) {
281 manager()->resolver_ = resolver;
282 }
283
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700284 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
285 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
286 }
287
288 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
289 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700290 }
291
292 const string &GetIgnoredDNSSearchPaths() {
293 return manager()->props_.ignored_dns_search_paths;
294 }
295
Paul Stewartd2e1c362013-03-03 19:06:07 -0800296 WiFiServiceRefPtr ReleaseTempMockService() {
297 // Take a reference to hold during this function.
298 WiFiServiceRefPtr temp_service = temp_mock_service_;
299 temp_mock_service_ = NULL;
300 return temp_service;
301 }
302
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700303 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000304 typedef scoped_refptr<MockService> MockServiceRefPtr;
305
Darin Petkova5e07ef2012-07-09 14:27:57 +0200306 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
307 public:
308 ServiceWatcher() {}
309 virtual ~ServiceWatcher() {}
310
311 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
312
313 private:
314 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
315 };
316
Darin Petkov3ec55342012-09-28 14:04:44 +0200317 class TerminationActionTest :
318 public base::SupportsWeakPtr<TerminationActionTest> {
319 public:
320 static const char kActionName[];
321
322 TerminationActionTest() : manager_(NULL) {}
323 virtual ~TerminationActionTest() {}
324
325 MOCK_METHOD1(Done, void(const Error &error));
326
327 void Action() {
328 manager_->TerminationActionComplete("action");
329 }
330
331 void set_manager(Manager *manager) { manager_ = manager; }
332
333 private:
334 Manager *manager_;
335 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
336 };
337
Christopher Wiley1057cd72013-02-28 15:21:29 -0800338 class DestinationVerificationTest :
339 public base::SupportsWeakPtr<DestinationVerificationTest> {
340 public:
341 DestinationVerificationTest() {}
342 virtual ~DestinationVerificationTest() {}
343
344 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
345 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
346 const string &value));
347 private:
348 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
349 };
350
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700351 class DisableTechnologyReplyHandler :
352 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
353 public:
354 DisableTechnologyReplyHandler() {}
355 virtual ~DisableTechnologyReplyHandler() {}
356
357 MOCK_METHOD1(ReportResult, void(const Error &));
358
359 private:
360 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
361 };
362
Daniel Eratfac09532014-04-17 20:25:59 -0700363 void SetSuspending(bool suspending) {
364 power_manager_->suspending_ = suspending;
Darin Petkovca621542012-07-25 14:25:56 +0200365 }
366
367 void SetPowerManager() {
368 manager()->set_power_manager(power_manager_.release());
369 }
370
Darin Petkov3ec55342012-09-28 14:04:44 +0200371 HookTable *GetTerminationActions() {
372 return &manager()->termination_actions_;
373 }
374
Daniel Erat0818cca2012-12-14 10:16:21 -0800375 void OnSuspendImminent(int suspend_id) {
376 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200377 }
378
Daniel Eratfac09532014-04-17 20:25:59 -0700379 void OnSuspendDone(int suspend_id) {
380 manager()->OnSuspendDone(suspend_id);
381 }
382
Daniel Erat0818cca2012-12-14 10:16:21 -0800383 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
384 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200385 }
386
Paul Stewartbfb82552012-10-24 16:48:48 -0700387 vector<string> EnumerateAvailableServices() {
388 return manager()->EnumerateAvailableServices(NULL);
389 }
390
391 vector<string> EnumerateWatchedServices() {
392 return manager()->EnumerateWatchedServices(NULL);
393 }
394
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000395 MockServiceRefPtr MakeAutoConnectableService() {
396 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
397 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800398 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000399 manager());
Paul Stewart2da34c02013-10-17 15:28:56 -0700400 service->EnableAndRetainAutoConnect();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700401 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000402 return service;
403 }
404
Paul Stewart35eff132013-04-12 12:08:40 -0700405 void SetEapProviderService(const ServiceRefPtr &service) {
406 ethernet_eap_provider_->set_service(service);
407 }
408
Ben Chana55469d2014-01-27 16:35:29 -0800409 NiceMock<MockProxyFactory> proxy_factory_;
Darin Petkovca621542012-07-25 14:25:56 +0200410 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700411 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800412 scoped_ptr<MockDeviceInfo> device_info_;
413
Paul Stewartd2e1c362013-03-03 19:06:07 -0800414 // This service is held for the manager, and given ownership in a mock
415 // function. This ensures that when the Manager takes ownership, there
416 // is only one reference left.
417 scoped_refptr<MockWiFiService> temp_mock_service_;
418
Paul Stewart3c504012013-01-17 17:49:58 -0800419 // These pointers are owned by the manager, and only tracked here for
420 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800421 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700422 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800423 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800424 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700425};
426
Darin Petkov3ec55342012-09-28 14:04:44 +0200427const char ManagerTest::TerminationActionTest::kActionName[] = "action";
428
Paul Stewart22aa71b2011-09-16 12:15:11 -0700429bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700430 if (!manager()->sort_services_task_.IsCancelled()) {
431 manager()->SortServicesTask();
432 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700433 return (svc0.get() == manager()->services_[0].get() &&
434 svc1.get() == manager()->services_[1].get());
435}
436
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700437void SetErrorPermissionDenied(Error *error) {
438 error->Populate(Error::kPermissionDenied);
439}
440
Arman Uguray2f352e62013-08-28 19:12:53 -0700441void SetErrorSuccess(Error *error) {
442 error->Reset();
443}
444
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700445TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700446 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700447 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700448}
449
Chris Masone9be4a9d2011-05-16 15:44:09 -0700450TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700451 ON_CALL(*mock_devices_[0].get(), technology())
452 .WillByDefault(Return(Technology::kEthernet));
453 ON_CALL(*mock_devices_[1].get(), technology())
454 .WillByDefault(Return(Technology::kWifi));
455 ON_CALL(*mock_devices_[2].get(), technology())
456 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700457
Paul Stewart22aa71b2011-09-16 12:15:11 -0700458 manager()->RegisterDevice(mock_devices_[0]);
459 manager()->RegisterDevice(mock_devices_[1]);
460 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700461
Paul Stewart22aa71b2011-09-16 12:15:11 -0700462 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
463 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
464 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700465}
466
Paul Stewarta41e38d2011-11-11 07:47:29 -0800467TEST_F(ManagerTest, DeviceRegistrationAndStart) {
468 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500469 mock_devices_[0]->enabled_persistent_ = true;
470 mock_devices_[1]->enabled_persistent_ = false;
471 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800472 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500473 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800474 .Times(0);
475 manager()->RegisterDevice(mock_devices_[0]);
476 manager()->RegisterDevice(mock_devices_[1]);
477}
478
479TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800480 MockProfile *profile =
481 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800482 DeviceRefPtr device_ref(mock_devices_[0].get());
483 AdoptProfile(manager(), profile); // Passes ownership.
484 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200485 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800486 manager()->RegisterDevice(mock_devices_[0]);
487}
488
Chris Masone9be4a9d2011-05-16 15:44:09 -0700489TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700490 ON_CALL(*mock_devices_[0].get(), technology())
491 .WillByDefault(Return(Technology::kEthernet));
492 ON_CALL(*mock_devices_[1].get(), technology())
493 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700494
Gaurav Shah435de2c2011-11-17 19:01:07 -0800495 manager()->RegisterDevice(mock_devices_[0]);
496 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700497
Paul Stewart22aa71b2011-09-16 12:15:11 -0700498 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
499 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700500
Thieu Le5133b712013-02-19 14:47:21 -0800501 MockProfile *profile =
502 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700503 AdoptProfile(manager(), profile); // Passes ownership.
504
Eric Shienbrood9a245532012-03-07 14:20:39 -0500505 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700506 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800507 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700508 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700509
Eric Shienbrood9a245532012-03-07 14:20:39 -0500510 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700511 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800512 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700513 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700514}
515
516TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700517 // It's much easier and safer to use a real GLib for this test.
518 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700519 Manager manager(control_interface(),
520 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800521 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700522 &glib,
523 run_path(),
524 storage_path(),
525 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700526 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
527 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700528 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700529
Chris Masone9be4a9d2011-05-16 15:44:09 -0700530 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700531 new NiceMock<MockService>(control_interface(),
532 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800533 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700534 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700535 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700536 new NiceMock<MockService>(control_interface(),
537 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800538 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700539 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700540
Darin Petkov457728b2013-01-09 09:49:08 +0100541 string service1_name(mock_service->unique_name());
542 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700543
544 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
545 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700546 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700547 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700548 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700549 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700550 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700551
Chris Masone9d779932011-08-25 16:33:41 -0700552 manager.RegisterService(mock_service);
553 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700554
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800555 Error error;
556 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700557 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700558 EXPECT_EQ(2, ids.size());
559 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
560 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700561
Chris Masone9d779932011-08-25 16:33:41 -0700562 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
563 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
564
565 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700566}
567
Chris Masone6515aab2011-10-12 16:19:09 -0700568TEST_F(ManagerTest, RegisterKnownService) {
569 // It's much easier and safer to use a real GLib for this test.
570 GLib glib;
571 Manager manager(control_interface(),
572 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800573 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700574 &glib,
575 run_path(),
576 storage_path(),
577 string());
578 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
579 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700580 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700581 {
582 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
583 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800584 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700585 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700586 ASSERT_TRUE(profile->AdoptService(service1));
587 ASSERT_TRUE(profile->ContainsService(service1));
588 } // Force destruction of service1.
589
590 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
591 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800592 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700593 &manager));
594 manager.RegisterService(service2);
595 EXPECT_EQ(service2->profile().get(), profile.get());
596 manager.Stop();
597}
598
599TEST_F(ManagerTest, RegisterUnknownService) {
600 // It's much easier and safer to use a real GLib for this test.
601 GLib glib;
602 Manager manager(control_interface(),
603 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800604 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700605 &glib,
606 run_path(),
607 storage_path(),
608 string());
609 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
610 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700611 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700612 {
613 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
614 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800615 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700616 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700617 ASSERT_TRUE(profile->AdoptService(service1));
618 ASSERT_TRUE(profile->ContainsService(service1));
619 } // Force destruction of service1.
620 scoped_refptr<MockService> mock_service2(
621 new NiceMock<MockService>(control_interface(),
622 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800623 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700624 &manager));
625 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100626 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700627 manager.RegisterService(mock_service2);
628 EXPECT_NE(mock_service2->profile().get(), profile.get());
629 manager.Stop();
630}
631
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000632TEST_F(ManagerTest, DeregisterUnregisteredService) {
633 // WiFi assumes that it can deregister a service that is not
634 // registered. (E.g. a hidden service can be deregistered when it
635 // loses its last endpoint, and again when WiFi is Stop()-ed.)
636 //
637 // So test that doing so doesn't cause a crash.
638 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
639 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800640 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000641 manager());
642 manager()->DeregisterService(service);
643}
644
Chris Masonea8a2c252011-06-27 22:16:30 -0700645TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700646 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700647 map<string, ::DBus::Variant> props;
648 Error error(Error::kInvalidProperty, "");
649 {
650 ::DBus::Error dbus_error;
651 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700652 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700653 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700654 expected,
655 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700656 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700657 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
658 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700659 expected);
660 }
661 {
662 ::DBus::Error dbus_error;
663 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700664 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700665 expected,
666 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700667 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700668 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
669 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700670 expected);
671 }
672}
673
Chris Masone3c3f6a12011-07-01 10:01:41 -0700674TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700675 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800676 manager()->RegisterDevice(mock_devices_[0]);
677 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700678 {
679 map<string, ::DBus::Variant> props;
680 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700681 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700682 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700683 vector < ::DBus::Path> devices =
Ben Chan923a5022013-09-20 11:23:23 -0700684 props[kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700685 EXPECT_EQ(2, devices.size());
686 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700687}
688
mukesh agrawal2366eed2012-03-20 18:21:50 -0700689TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700690 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700691 map<string, ::DBus::Variant> props;
692 ::DBus::Error dbus_error;
693 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
694 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700695 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700696 ASSERT_FALSE(prop == props.end());
697 const ::DBus::Variant &variant = prop->second;
698 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
699}
700
Chris Masone6791a432011-07-12 13:23:19 -0700701TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700702 Manager manager(control_interface(),
703 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800704 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700705 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700706 run_path(),
707 storage_path(),
708 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700709 scoped_refptr<MockService> s2(new MockService(control_interface(),
710 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800711 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700712 &manager));
713 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700714 {
Chris Masone6515aab2011-10-12 16:19:09 -0700715 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700716 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800717 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700718 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700719 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700720 .WillRepeatedly(Return(true));
721 EXPECT_CALL(*storage, Flush())
722 .Times(AnyNumber())
723 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700724 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700725 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700726 }
Chris Masone6515aab2011-10-12 16:19:09 -0700727 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800728 ProfileRefPtr profile(
729 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700730 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700731
Chris Masone6515aab2011-10-12 16:19:09 -0700732 // Now, move the Service |s2| to another profile.
733 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
734 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700735
736 // Force destruction of the original Profile, to ensure that the Service
737 // is kept alive and populated with data.
738 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700739 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700740 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700741}
742
Paul Stewart7f61e522012-03-22 11:13:45 -0700743TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
744 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800745 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700746 const string kProfileName("profile0");
747 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
748 .WillRepeatedly(Return(kProfileName));
749 AdoptProfile(manager(), mock_profile);
750
751 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
752 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
753 EXPECT_EQ(mock_profile.get(), profile.get());
754}
755
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800756TEST_F(ManagerTest, SetProfileForService) {
757 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800758 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800759 string profile_name0("profile0");
760 EXPECT_CALL(*profile0, GetRpcIdentifier())
761 .WillRepeatedly(Return(profile_name0));
762 AdoptProfile(manager(), profile0);
763 scoped_refptr<MockService> service(new MockService(control_interface(),
764 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800765 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800766 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700767 EXPECT_FALSE(manager()->HasService(service));
768 {
769 Error error;
770 EXPECT_CALL(*profile0, AdoptService(_))
771 .WillOnce(Return(true));
772 // Expect that setting the profile of a service that does not already
773 // have one assigned does not cause a crash.
774 manager()->SetProfileForService(service, "profile0", &error);
775 EXPECT_TRUE(error.IsSuccess());
776 }
777
778 // The service should be registered as a side-effect of the profile being
779 // set for this service.
780 EXPECT_TRUE(manager()->HasService(service));
781
782 // Since we have mocked Profile::AdoptServie() above, the service's
783 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800784 service->set_profile(profile0);
785
786 {
787 Error error;
788 manager()->SetProfileForService(service, "foo", &error);
789 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700790 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800791 }
792
793 {
794 Error error;
795 manager()->SetProfileForService(service, profile_name0, &error);
796 EXPECT_EQ(Error::kInvalidArguments, error.type());
797 EXPECT_EQ("Service is already connected to this profile", error.message());
798 }
799
800 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800801 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800802 string profile_name1("profile1");
803 EXPECT_CALL(*profile1, GetRpcIdentifier())
804 .WillRepeatedly(Return(profile_name1));
805 AdoptProfile(manager(), profile1);
806
807 {
808 Error error;
809 EXPECT_CALL(*profile1, AdoptService(_))
810 .WillOnce(Return(true));
811 EXPECT_CALL(*profile0, AbandonService(_))
812 .WillOnce(Return(true));
813 manager()->SetProfileForService(service, profile_name1, &error);
814 EXPECT_TRUE(error.IsSuccess());
815 }
816}
817
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700818TEST_F(ManagerTest, CreateProfile) {
819 // It's much easier to use real Glib here since we want the storage
820 // side-effects.
821 GLib glib;
822 ScopedTempDir temp_dir;
823 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
824
825 Manager manager(control_interface(),
826 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800827 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700828 &glib,
829 run_path(),
830 storage_path(),
831 temp_dir.path().value());
832
833 // Invalid name should be rejected.
834 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
835
Paul Stewartd0a3b812012-03-28 22:48:22 -0700836 // A profile with invalid characters in it should similarly be rejected.
837 EXPECT_EQ(Error::kInvalidArguments,
838 TestCreateProfile(&manager, "valid_profile"));
839
840 // We should be able to create a machine profile.
841 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700842
Gary Morainb672d352012-04-25 09:19:06 -0700843 // We should succeed in creating a valid user profile. Verify the returned
844 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700845 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700846 {
847 Error error;
848 string path;
849 manager.CreateProfile(kProfile, &path, &error);
850 EXPECT_EQ(Error::kSuccess, error.type());
851 EXPECT_EQ("/profile_rpc", path);
852 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700853
854 // We should fail in creating it a second time (already exists).
855 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
856}
857
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700858// We receive PopProfile when a user logs out, and it should always trigger a
859// MemoryLog Clear() call.
860TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
861 GLib glib;
862 ScopedTempDir temp_dir;
863 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
864 Manager manager(control_interface(),
865 dispatcher(),
866 metrics(),
867 &glib,
868 run_path(),
869 storage_path(),
870 temp_dir.path().value());
871 const char kProfile0[] = "~user/profile0";
872 const char kPurgedMessage[] = "This message should be purged";
873 // Create a profile and push it on the stack, leave one uncreated
874 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
875 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
876
877 // Popping a profile which isn't on top should still clear the log.
878 LOG(INFO) << kPurgedMessage;
879 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
880 kPurgedMessage));
881 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
882 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
883 kPurgedMessage));
884
885 // Popping an invalid profile name should do the same thing.
886 LOG(INFO) << kPurgedMessage;
887 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
888 kPurgedMessage));
889 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
890 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
891 kPurgedMessage));
892
893 // Successful pops also purge the message log.
894 LOG(INFO) << kPurgedMessage;
895 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
896 kPurgedMessage));
897 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
898 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
899 kPurgedMessage));
900}
901
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700902TEST_F(ManagerTest, PushPopProfile) {
903 // It's much easier to use real Glib in creating a Manager for this
904 // test here since we want the storage side-effects.
905 GLib glib;
906 ScopedTempDir temp_dir;
907 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
908 Manager manager(control_interface(),
909 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800910 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700911 &glib,
912 run_path(),
913 storage_path(),
914 temp_dir.path().value());
mukesh agrawal92496a42014-04-08 16:04:43 -0700915 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700916
917 // Pushing an invalid profile should fail.
918 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
919
mukesh agrawal92496a42014-04-08 16:04:43 -0700920 // Create and push a default profile. Should succeed.
921 const char kDefaultProfile0[] = "default";
922 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kDefaultProfile0));
923 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kDefaultProfile0));
924 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kDefaultProfile0));
925
926 // Pushing a default profile that does not exist on disk will _not_
927 // fail, because we'll use temporary storage for it.
928 const char kMissingDefaultProfile[] = "missingdefault";
929 EXPECT_EQ(Error::kSuccess,
930 TestPushProfile(&manager, kMissingDefaultProfile));
931 EXPECT_EQ(1, profiles.size());
932 EXPECT_EQ(Error::kSuccess,
933 TestPopProfile(&manager, kMissingDefaultProfile));
934 EXPECT_EQ(0, profiles.size());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700935
936 const char kProfile0[] = "~user/profile0";
937 const char kProfile1[] = "~user/profile1";
938
939 // Create a couple of profiles.
940 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
941 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
942
943 // Push these profiles on the stack.
944 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
945 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
946
947 // Pushing a profile a second time should fail.
948 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
949 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
950
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800951 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700952 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800953 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700954
955 // Make sure a profile name that doesn't exist fails.
956 const char kProfile2Id[] = "profile2";
957 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
958 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
959
960 // Create a new service, with a specific storage name.
961 scoped_refptr<MockService> service(
962 new NiceMock<MockService>(control_interface(),
963 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800964 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700965 &manager));
966 const char kServiceName[] = "service_storage_name";
967 EXPECT_CALL(*service.get(), GetStorageIdentifier())
968 .WillRepeatedly(Return(kServiceName));
969 EXPECT_CALL(*service.get(), Load(_))
970 .WillRepeatedly(Return(true));
971
972 // Add this service to the manager -- it should end up in the ephemeral
973 // profile.
974 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800975 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700976
977 // Create storage for a profile that contains the service storage name.
978 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
979 kServiceName));
980
981 // When we push the profile, the service should move away from the
982 // ephemeral profile to this new profile since it has an entry for
983 // this service.
984 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800985 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700986 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
987
988 // Insert another profile that should supersede ownership of the service.
989 const char kProfile3Id[] = "profile3";
990 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
991 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
992 kServiceName));
993 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
994 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
995
996 // Popping an invalid profile name should fail.
997 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
998
999 // Popping an profile that is not at the top of the stack should fail.
1000 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
1001
1002 // Popping the top profile should succeed.
1003 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
1004
1005 // Moreover the service should have switched profiles to profile 2.
1006 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
1007
1008 // Popping the top profile should succeed.
1009 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1010
1011 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -08001012 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001013
mukesh agrawal92496a42014-04-08 16:04:43 -07001014 // Pop the remaining two profiles off the stack.
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001015 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1016 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1017
1018 // Next pop should fail with "stack is empty".
1019 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -07001020
1021 const char kMachineProfile0[] = "machineprofile0";
1022 const char kMachineProfile1[] = "machineprofile1";
1023 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1024 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1025
1026 // Should be able to push a machine profile.
1027 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1028
1029 // Should be able to push a user profile atop a machine profile.
1030 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1031
1032 // Pushing a system-wide profile on top of a user profile should fail.
1033 EXPECT_EQ(Error::kInvalidArguments,
1034 TestPushProfile(&manager, kMachineProfile1));
1035
1036 // However if we pop the user profile, we should be able stack another
1037 // machine profile on.
1038 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1039 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001040
1041 // Add two user profiles to the top of the stack.
1042 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1043 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001044 EXPECT_EQ(4, profiles.size());
1045
1046 // PopAllUserProfiles should remove both user profiles, leaving the two
1047 // machine profiles.
1048 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1049 EXPECT_EQ(2, profiles.size());
1050 EXPECT_TRUE(profiles[0]->GetUser().empty());
1051 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001052
1053 // Use InsertUserProfile() instead. Although a machine profile is valid
1054 // in this state, it cannot be added via InsertUserProfile.
1055 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1056 EXPECT_EQ(Error::kInvalidArguments,
1057 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1058 const char kUserHash0[] = "userhash0";
1059 const char kUserHash1[] = "userhash1";
1060 EXPECT_EQ(Error::kSuccess,
1061 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1062 EXPECT_EQ(Error::kSuccess,
1063 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1064 EXPECT_EQ(3, profiles.size());
1065 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1066 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001067}
1068
Paul Stewarte73d05c2012-03-29 16:26:05 -07001069TEST_F(ManagerTest, RemoveProfile) {
1070 // It's much easier to use real Glib in creating a Manager for this
1071 // test here since we want the storage side-effects.
1072 GLib glib;
1073 ScopedTempDir temp_dir;
1074 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1075 Manager manager(control_interface(),
1076 dispatcher(),
1077 metrics(),
1078 &glib,
1079 run_path(),
1080 storage_path(),
1081 temp_dir.path().value());
1082
1083 const char kProfile0[] = "profile0";
1084 FilePath profile_path(
1085 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1086
1087 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001088 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001089
1090 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1091
1092 // Remove should fail since the profile is still on the stack.
1093 {
1094 Error error;
1095 manager.RemoveProfile(kProfile0, &error);
1096 EXPECT_EQ(Error::kInvalidArguments, error.type());
1097 }
1098
1099 // Profile path should still exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001100 EXPECT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001101
1102 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1103
1104 // This should succeed now that the profile is off the stack.
1105 {
1106 Error error;
1107 manager.RemoveProfile(kProfile0, &error);
1108 EXPECT_EQ(Error::kSuccess, error.type());
1109 }
1110
1111 // Profile path should no longer exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001112 EXPECT_FALSE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001113
Ben Chana0ddf462014-02-06 11:32:42 -08001114 // Another remove succeeds, due to a foible in base::DeleteFile --
Paul Stewarte73d05c2012-03-29 16:26:05 -07001115 // it is not an error to delete a file that does not exist.
1116 {
1117 Error error;
1118 manager.RemoveProfile(kProfile0, &error);
1119 EXPECT_EQ(Error::kSuccess, error.type());
1120 }
1121
1122 // Let's create an error case that will "work". Create a non-empty
1123 // directory in the place of the profile pathname.
Ben Chana0ddf462014-02-06 11:32:42 -08001124 ASSERT_TRUE(base::CreateDirectory(profile_path.Append("foo")));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001125 {
1126 Error error;
1127 manager.RemoveProfile(kProfile0, &error);
1128 EXPECT_EQ(Error::kOperationFailed, error.type());
1129 }
1130}
1131
Paul Stewartd3d03882013-08-29 15:43:42 -07001132TEST_F(ManagerTest, RemoveService) {
1133 MockServiceRefPtr mock_service(
1134 new NiceMock<MockService>(control_interface(),
1135 dispatcher(),
1136 metrics(),
1137 manager()));
1138
1139 // Used in expectations which cannot accept a mock refptr.
1140 const ServiceRefPtr &service = mock_service;
1141
1142 manager()->RegisterService(service);
1143 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1144
1145 scoped_refptr<MockProfile> profile(
1146 new StrictMock<MockProfile>(
1147 control_interface(), metrics(), manager(), ""));
1148 AdoptProfile(manager(), profile);
1149
1150 // If service is ephemeral, it should be unloaded and left ephemeral.
1151 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1152 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1153 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1154 manager()->RemoveService(service);
1155 Mock::VerifyAndClearExpectations(mock_service);
1156 Mock::VerifyAndClearExpectations(profile);
1157 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1158 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1159
1160 // If service is not ephemeral and the Manager finds a profile to assign
1161 // the service to, the service should be re-parented. Note that since we
1162 // are using a MockProfile, ConfigureService() never actually changes the
1163 // Service's profile.
1164 service->set_profile(profile);
1165 EXPECT_CALL(*profile, AbandonService(service));
1166 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1167 EXPECT_CALL(*mock_service, Unload()).Times(0);
1168 manager()->RemoveService(service);
1169 Mock::VerifyAndClearExpectations(mock_service);
1170 Mock::VerifyAndClearExpectations(profile);
1171 EXPECT_TRUE(manager()->HasService(service));
1172 EXPECT_EQ(profile.get(), service->profile().get());
1173
1174 // If service becomes ephemeral since there is no profile to support it,
1175 // it should be unloaded.
1176 EXPECT_CALL(*profile, AbandonService(service));
1177 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1178 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1179 manager()->RemoveService(service);
1180 EXPECT_FALSE(manager()->HasService(service));
1181}
1182
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001183TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1184 // It's much easier to use real Glib in creating a Manager for this
1185 // test here since we want the storage side-effects.
1186 GLib glib;
1187 ScopedTempDir temp_dir;
1188 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1189 Manager manager(control_interface(),
1190 dispatcher(),
1191 metrics(),
1192 &glib,
1193 run_path(),
1194 storage_path(),
1195 temp_dir.path().value());
1196
1197 const char kProfile0[] = "profile0";
1198 FilePath profile_path(
1199 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1200
1201 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001202 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001203 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1204
1205 // Ensure that even if the backing filestore is removed, we still can't
1206 // create a profile twice.
Ben Chana0ddf462014-02-06 11:32:42 -08001207 ASSERT_TRUE(base::DeleteFile(profile_path, false));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001208 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1209}
1210
Paul Stewart75225512012-01-26 22:51:33 -08001211// Use this matcher instead of passing RefPtrs directly into the arguments
1212// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1213// system teardown.
1214MATCHER_P(IsRefPtrTo, ref_address, "") {
1215 return arg.get() == ref_address;
1216}
1217
1218TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1219 MockServiceRefPtr s_not_in_profile(
1220 new NiceMock<MockService>(control_interface(),
1221 dispatcher(),
1222 metrics(),
1223 manager()));
1224 MockServiceRefPtr s_not_in_group(
1225 new NiceMock<MockService>(control_interface(),
1226 dispatcher(),
1227 metrics(),
1228 manager()));
1229 MockServiceRefPtr s_configure_fail(
1230 new NiceMock<MockService>(control_interface(),
1231 dispatcher(),
1232 metrics(),
1233 manager()));
1234 MockServiceRefPtr s_configure_succeed(
1235 new NiceMock<MockService>(control_interface(),
1236 dispatcher(),
1237 metrics(),
1238 manager()));
1239
1240 string entry_name("entry_name");
1241 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1242 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1243 .WillRepeatedly(Return("not_entry_name"));
1244 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1245 .WillRepeatedly(Return(entry_name));
1246 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1247 .WillRepeatedly(Return(entry_name));
1248
1249 manager()->RegisterService(s_not_in_profile);
1250 manager()->RegisterService(s_not_in_group);
1251 manager()->RegisterService(s_configure_fail);
1252 manager()->RegisterService(s_configure_succeed);
1253
1254 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001255 new StrictMock<MockProfile>(
1256 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001257 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001258 new StrictMock<MockProfile>(
1259 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001260
1261 s_not_in_group->set_profile(profile1);
1262 s_configure_fail->set_profile(profile1);
1263 s_configure_succeed->set_profile(profile1);
1264
1265 AdoptProfile(manager(), profile0);
1266 AdoptProfile(manager(), profile1);
1267
1268 // No services are a member of this profile.
1269 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1270
1271 // No services that are members of this profile have this entry name.
1272 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1273
1274 // Only services that are members of the profile and group will be abandoned.
1275 EXPECT_CALL(*profile1.get(),
1276 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1277 EXPECT_CALL(*profile1.get(),
1278 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1279 EXPECT_CALL(*profile1.get(),
1280 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1281 .WillOnce(Return(true));
1282 EXPECT_CALL(*profile1.get(),
1283 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1284 .WillOnce(Return(true));
1285
1286 // Never allow services to re-join profile1.
1287 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1288 .WillRepeatedly(Return(false));
1289
1290 // Only allow one of the members of the profile and group to successfully
1291 // join profile0.
1292 EXPECT_CALL(*profile0.get(),
1293 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1294 EXPECT_CALL(*profile0.get(),
1295 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1296 EXPECT_CALL(*profile0.get(),
1297 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1298 .WillOnce(Return(false));
1299 EXPECT_CALL(*profile0.get(),
1300 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1301 .WillOnce(Return(true));
1302
1303 // Expect the failed-to-configure service to have Unload() called on it.
1304 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1305 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1306 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1307 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1308
1309 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1310
1311 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1312 EXPECT_EQ(profile1, s_not_in_group->profile());
1313 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1314
1315 // Since we are using a MockProfile, the profile does not actually change,
1316 // since ConfigureService was not actually called on the service.
1317 EXPECT_EQ(profile1, s_configure_succeed->profile());
1318}
1319
Paul Stewart65512e12012-03-26 18:01:08 -07001320TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1321 MockServiceRefPtr s_will_remove0(
1322 new NiceMock<MockService>(control_interface(),
1323 dispatcher(),
1324 metrics(),
1325 manager()));
1326 MockServiceRefPtr s_will_remove1(
1327 new NiceMock<MockService>(control_interface(),
1328 dispatcher(),
1329 metrics(),
1330 manager()));
1331 MockServiceRefPtr s_will_not_remove0(
1332 new NiceMock<MockService>(control_interface(),
1333 dispatcher(),
1334 metrics(),
1335 manager()));
1336 MockServiceRefPtr s_will_not_remove1(
1337 new NiceMock<MockService>(control_interface(),
1338 dispatcher(),
1339 metrics(),
1340 manager()));
1341
1342 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1343 .Times(4); // Once for each registration.
1344
1345 string entry_name("entry_name");
1346 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1347 .WillRepeatedly(Return(entry_name));
1348 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1349 .WillRepeatedly(Return(entry_name));
1350 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1351 .WillRepeatedly(Return(entry_name));
1352 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1353 .WillRepeatedly(Return(entry_name));
1354
1355 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001356 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001357 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001358 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001359 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001360 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001361 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001362 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001363
1364 // One for each service added above.
1365 ASSERT_EQ(4, manager()->services_.size());
1366
1367 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001368 new StrictMock<MockProfile>(
1369 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001370
1371 s_will_remove0->set_profile(profile);
1372 s_will_remove1->set_profile(profile);
1373 s_will_not_remove0->set_profile(profile);
1374 s_will_not_remove1->set_profile(profile);
1375
1376 AdoptProfile(manager(), profile);
1377
1378 // Deny any of the services re-entry to the profile.
1379 EXPECT_CALL(*profile, ConfigureService(_))
1380 .WillRepeatedly(Return(false));
1381
1382 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1383 .WillOnce(Return(true));
1384 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1385 .WillOnce(Return(true));
1386 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1387 .WillOnce(Return(true));
1388 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1389 .WillOnce(Return(true));
1390
1391 EXPECT_CALL(*s_will_remove0, Unload())
1392 .WillOnce(Return(true));
1393 EXPECT_CALL(*s_will_remove1, Unload())
1394 .WillOnce(Return(true));
1395 EXPECT_CALL(*s_will_not_remove0, Unload())
1396 .WillOnce(Return(false));
1397 EXPECT_CALL(*s_will_not_remove1, Unload())
1398 .WillOnce(Return(false));
1399
1400
1401 // This will cause all the profiles to be unloaded.
1402 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1403
1404 // 2 of the 4 services added above should have been unregistered and
1405 // removed, leaving 2.
1406 EXPECT_EQ(2, manager()->services_.size());
1407 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1408 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1409}
1410
1411TEST_F(ManagerTest, PopProfileWithUnload) {
1412 MockServiceRefPtr s_will_remove0(
1413 new NiceMock<MockService>(control_interface(),
1414 dispatcher(),
1415 metrics(),
1416 manager()));
1417 MockServiceRefPtr s_will_remove1(
1418 new NiceMock<MockService>(control_interface(),
1419 dispatcher(),
1420 metrics(),
1421 manager()));
1422 MockServiceRefPtr s_will_not_remove0(
1423 new NiceMock<MockService>(control_interface(),
1424 dispatcher(),
1425 metrics(),
1426 manager()));
1427 MockServiceRefPtr s_will_not_remove1(
1428 new NiceMock<MockService>(control_interface(),
1429 dispatcher(),
1430 metrics(),
1431 manager()));
1432
1433 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1434 .Times(5); // Once for each registration, and one after profile pop.
1435
1436 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001437 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001438 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001439 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001440 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001441 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001442 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001443 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001444
1445 // One for each service added above.
1446 ASSERT_EQ(4, manager()->services_.size());
1447
1448 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001449 new StrictMock<MockProfile>(
1450 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001451 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001452 new StrictMock<MockProfile>(
1453 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001454
1455 s_will_remove0->set_profile(profile1);
1456 s_will_remove1->set_profile(profile1);
1457 s_will_not_remove0->set_profile(profile1);
1458 s_will_not_remove1->set_profile(profile1);
1459
1460 AdoptProfile(manager(), profile0);
1461 AdoptProfile(manager(), profile1);
1462
1463 // Deny any of the services entry to profile0, so they will all be unloaded.
1464 EXPECT_CALL(*profile0, ConfigureService(_))
1465 .WillRepeatedly(Return(false));
1466
1467 EXPECT_CALL(*s_will_remove0, Unload())
1468 .WillOnce(Return(true));
1469 EXPECT_CALL(*s_will_remove1, Unload())
1470 .WillOnce(Return(true));
1471 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001472 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001473 EXPECT_CALL(*s_will_not_remove1, Unload())
1474 .WillOnce(Return(false));
1475
Philipp Neubeck79173602012-11-13 21:10:09 +01001476 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1477 // profile list.
1478 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1479 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1480
Paul Stewart65512e12012-03-26 18:01:08 -07001481 // This will pop profile1, which should cause all our profiles to unload.
1482 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001483 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001484
1485 // 2 of the 4 services added above should have been unregistered and
1486 // removed, leaving 2.
1487 EXPECT_EQ(2, manager()->services_.size());
1488 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1489 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001490
1491 // Expect the unloaded services to lose their profile reference.
1492 EXPECT_FALSE(s_will_remove0->profile());
1493 EXPECT_FALSE(s_will_remove1->profile());
1494
1495 // If we explicitly deregister a service, the effect should be the same
1496 // with respect to the profile reference.
1497 ASSERT_TRUE(s_will_not_remove0->profile());
1498 manager()->DeregisterService(s_will_not_remove0);
1499 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001500}
1501
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001502TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001503 {
1504 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001505 ::DBus::Variant offline_mode;
1506 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001507 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001508 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001509 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001510 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001511 }
1512 {
1513 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001514 ::DBus::Variant country;
1515 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001516 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001517 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001518 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001519 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001520 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001521 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001522 {
1523 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001524 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001525 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001526 PropertyStoreTest::kBoolV,
1527 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001528 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001529 }
1530 {
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 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001534 PropertyStoreTest::kStringV,
1535 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001536 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001537 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001538 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001539 {
1540 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001541 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001542 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001543 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001544 PropertyStoreTest::kStringsV,
1545 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001546 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001547 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001548}
1549
mukesh agrawal32399322011-09-01 10:53:43 -07001550TEST_F(ManagerTest, RequestScan) {
1551 {
1552 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001553 manager()->RegisterDevice(mock_devices_[0].get());
1554 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001555 EXPECT_CALL(*mock_devices_[0], technology())
1556 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001557 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001558 EXPECT_CALL(*mock_devices_[1], technology())
1559 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001560 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Ben Chan923a5022013-09-20 11:23:23 -07001561 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001562 }
1563
1564 {
1565 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001566 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001567 EXPECT_EQ(Error::kInvalidArguments, error.type());
1568 }
1569}
1570
Darin Petkovb65c2452012-02-23 15:17:06 +01001571TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001572 KeyValueStore args;
1573 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001574 manager()->GetService(args, &e);
1575 EXPECT_EQ(Error::kInvalidArguments, e.type());
1576 EXPECT_EQ("must specify service type", e.message());
1577}
1578
1579TEST_F(ManagerTest, GetServiceUnknownType) {
1580 KeyValueStore args;
1581 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001582 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001583 manager()->GetService(args, &e);
1584 EXPECT_EQ(Error::kNotSupported, e.type());
1585 EXPECT_EQ("service type is unsupported", e.message());
1586}
1587
Paul Stewart35eff132013-04-12 12:08:40 -07001588TEST_F(ManagerTest, GetServiceEthernetEap) {
1589 KeyValueStore args;
1590 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001591 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1592 dispatcher(),
1593 metrics(),
1594 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001595 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001596 SetEapProviderService(service);
1597 EXPECT_EQ(service, manager()->GetService(args, &e));
1598 EXPECT_TRUE(e.IsSuccess());
1599}
1600
Darin Petkovb65c2452012-02-23 15:17:06 +01001601TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001602 KeyValueStore args;
1603 Error e;
1604 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001605 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001606 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001607 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001608 manager()->GetService(args, &e);
1609 EXPECT_TRUE(e.IsSuccess());
1610}
1611
Darin Petkov33af05c2012-02-28 10:10:30 +01001612TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1613 KeyValueStore args;
1614 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001615 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001616 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001617 new StrictMock<MockProfile>(
1618 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001619 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001620 ServiceRefPtr service = manager()->GetService(args, &e);
1621 EXPECT_EQ(Error::kNotSupported, e.type());
1622 EXPECT_FALSE(service);
1623}
1624
Darin Petkovb65c2452012-02-23 15:17:06 +01001625TEST_F(ManagerTest, GetServiceVPN) {
1626 KeyValueStore args;
1627 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001628 args.SetString(kTypeProperty, kTypeVPN);
1629 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1630 args.SetString(kProviderHostProperty, "10.8.0.1");
1631 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001632 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001633 new StrictMock<MockProfile>(
1634 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001635 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001636
1637#if defined(DISABLE_VPN)
1638
1639 ServiceRefPtr service = manager()->GetService(args, &e);
1640 EXPECT_EQ(Error::kNotSupported, e.type());
1641 EXPECT_FALSE(service);
1642
1643#else
1644
Paul Stewart7f5ad572012-06-04 15:18:54 -07001645 ServiceRefPtr updated_service;
1646 EXPECT_CALL(*profile, UpdateService(_))
1647 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1648 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001649 EXPECT_CALL(*profile, LoadService(_))
1650 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001651 EXPECT_CALL(*profile, ConfigureService(_))
1652 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001653 ServiceRefPtr service = manager()->GetService(args, &e);
1654 EXPECT_TRUE(e.IsSuccess());
1655 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001656 EXPECT_EQ(service, updated_service);
1657 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001658
1659#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001660}
1661
Ben Chan520eb172013-10-30 20:51:04 -07001662#if !defined(DISABLE_WIMAX)
1663
Darin Petkovc63dcf02012-05-24 11:51:43 +02001664TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1665 KeyValueStore args;
1666 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001667 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001668 ServiceRefPtr service = manager()->GetService(args, &e);
1669 EXPECT_EQ(Error::kInvalidArguments, e.type());
1670 EXPECT_EQ("Missing WiMAX network id.", e.message());
1671 EXPECT_FALSE(service);
1672}
1673
Darin Petkovd1cd7972012-05-22 15:26:15 +02001674TEST_F(ManagerTest, GetServiceWiMax) {
1675 KeyValueStore args;
1676 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001677 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001678 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001679 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001680 ServiceRefPtr service = manager()->GetService(args, &e);
1681 EXPECT_TRUE(e.IsSuccess());
1682 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001683}
1684
Ben Chan520eb172013-10-30 20:51:04 -07001685#endif // DISABLE_WIMAX
1686
Paul Stewart7f61e522012-03-22 11:13:45 -07001687TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1688 // Manager calls ActiveProfile() so we need at least one profile installed.
1689 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001690 new NiceMock<MockProfile>(
1691 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001692 AdoptProfile(manager(), profile);
1693
1694 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001695 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001696 Error error;
1697 manager()->ConfigureService(args, &error);
1698 EXPECT_EQ(Error::kInvalidArguments, error.type());
1699 EXPECT_EQ("Invalid profile name xxx", error.message());
1700}
1701
1702TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1703 // Manager calls ActiveProfile() so we need at least one profile installed.
1704 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001705 new NiceMock<MockProfile>(
1706 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001707 AdoptProfile(manager(), profile);
1708
1709 KeyValueStore args;
1710 Error error;
1711 manager()->ConfigureService(args, &error);
1712 EXPECT_EQ(Error::kInvalidArguments, error.type());
1713 EXPECT_EQ("must specify service type", error.message());
1714}
1715
1716// A registered service in the ephemeral profile should be moved to the
1717// active profile as a part of configuration if no profile was explicitly
1718// specified.
1719TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1720 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001721 new NiceMock<MockProfile>(
1722 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001723
1724 AdoptProfile(manager(), profile); // This is now the active profile.
1725
Paul Stewartd2e1c362013-03-03 19:06:07 -08001726 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001727 scoped_refptr<MockWiFiService> service(
1728 new NiceMock<MockWiFiService>(control_interface(),
1729 dispatcher(),
1730 metrics(),
1731 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001732 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001733 ssid,
1734 "",
1735 "",
1736 false));
1737
1738 manager()->RegisterService(service);
1739 service->set_profile(GetEphemeralProfile(manager()));
1740
Paul Stewart3c504012013-01-17 17:49:58 -08001741 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001742 .WillOnce(Return(service));
1743 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1744 .WillOnce(Return(true));
1745 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1746 .WillOnce(Return(true));
1747
1748 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001749 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001750 Error error;
1751 manager()->ConfigureService(args, &error);
1752 EXPECT_TRUE(error.IsSuccess());
1753}
1754
Paul Stewart2c575d22012-12-07 12:28:57 -08001755// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001756// specify a profile, it should be moved from the profile it was previously
1757// in to the specified profile if one was requested.
1758TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1759 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001760 new NiceMock<MockProfile>(
1761 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001762 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001763 new NiceMock<MockProfile>(
1764 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001765
1766 const string kProfileName0 = "profile0";
1767 const string kProfileName1 = "profile1";
1768
1769 EXPECT_CALL(*profile0, GetRpcIdentifier())
1770 .WillRepeatedly(Return(kProfileName0));
1771 EXPECT_CALL(*profile1, GetRpcIdentifier())
1772 .WillRepeatedly(Return(kProfileName1));
1773
1774 AdoptProfile(manager(), profile0);
1775 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1776
Paul Stewartd2e1c362013-03-03 19:06:07 -08001777 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001778 scoped_refptr<MockWiFiService> service(
1779 new NiceMock<MockWiFiService>(control_interface(),
1780 dispatcher(),
1781 metrics(),
1782 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001783 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001784 ssid,
1785 "",
1786 "",
1787 false));
1788
1789 manager()->RegisterService(service);
1790 service->set_profile(profile1);
1791
Paul Stewart3c504012013-01-17 17:49:58 -08001792 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001793 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001794 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1795 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001796 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1797 .WillOnce(Return(true));
1798 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1799 .WillOnce(Return(true));
1800 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1801 .WillOnce(Return(true));
1802
1803 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001804 args.SetString(kTypeProperty, kTypeWifi);
1805 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001806 Error error;
1807 manager()->ConfigureService(args, &error);
1808 EXPECT_TRUE(error.IsSuccess());
1809 service->set_profile(NULL); // Breaks refcounting loop.
1810}
1811
Paul Stewart2c575d22012-12-07 12:28:57 -08001812// If we configure a service that is already a member of the specified
1813// profile, the Manager should not call LoadService or AdoptService again
1814// on this service.
1815TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1816 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001817 new NiceMock<MockProfile>(
1818 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001819
1820 const string kProfileName0 = "profile0";
1821
1822 EXPECT_CALL(*profile0, GetRpcIdentifier())
1823 .WillRepeatedly(Return(kProfileName0));
1824
1825 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1826
Paul Stewartd2e1c362013-03-03 19:06:07 -08001827 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001828 scoped_refptr<MockWiFiService> service(
1829 new NiceMock<MockWiFiService>(control_interface(),
1830 dispatcher(),
1831 metrics(),
1832 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001833 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001834 ssid,
1835 "",
1836 "",
1837 false));
1838
1839 manager()->RegisterService(service);
1840 service->set_profile(profile0);
1841
Paul Stewart3c504012013-01-17 17:49:58 -08001842 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001843 .WillOnce(Return(service));
1844 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1845 .Times(0);
1846 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1847 .WillOnce(Return(true));
1848 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1849 .Times(0);
1850
1851 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001852 args.SetString(kTypeProperty, kTypeWifi);
1853 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001854 Error error;
1855 manager()->ConfigureService(args, &error);
1856 EXPECT_TRUE(error.IsSuccess());
1857 service->set_profile(NULL); // Breaks refcounting loop.
1858}
1859
Paul Stewart7f61e522012-03-22 11:13:45 -07001860// An unregistered service should remain unregistered, but its contents should
1861// be saved to the specified profile nonetheless.
1862TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1863 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001864 new NiceMock<MockProfile>(
1865 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001866 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001867 new NiceMock<MockProfile>(
1868 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001869
1870 const string kProfileName0 = "profile0";
1871 const string kProfileName1 = "profile1";
1872
1873 EXPECT_CALL(*profile0, GetRpcIdentifier())
1874 .WillRepeatedly(Return(kProfileName0));
1875 EXPECT_CALL(*profile1, GetRpcIdentifier())
1876 .WillRepeatedly(Return(kProfileName1));
1877
1878 AdoptProfile(manager(), profile0);
1879 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1880
Paul Stewartd2e1c362013-03-03 19:06:07 -08001881 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001882 scoped_refptr<MockWiFiService> service(
1883 new NiceMock<MockWiFiService>(control_interface(),
1884 dispatcher(),
1885 metrics(),
1886 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001887 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001888 ssid,
1889 "",
1890 "",
1891 false));
1892
1893 service->set_profile(profile1);
1894
Paul Stewart3c504012013-01-17 17:49:58 -08001895 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001896 .WillOnce(Return(service));
1897 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1898 .WillOnce(Return(true));
1899 EXPECT_CALL(*profile0, AdoptService(_))
1900 .Times(0);
1901 EXPECT_CALL(*profile1, AdoptService(_))
1902 .Times(0);
1903
1904 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001905 args.SetString(kTypeProperty, kTypeWifi);
1906 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001907 Error error;
1908 manager()->ConfigureService(args, &error);
1909 EXPECT_TRUE(error.IsSuccess());
1910}
1911
Paul Stewartd2e1c362013-03-03 19:06:07 -08001912TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1913 KeyValueStore args;
1914 Error error;
1915 ServiceRefPtr service =
1916 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001917 EXPECT_EQ(Error::kInvalidArguments, error.type());
1918 EXPECT_EQ("must specify service type", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001919 EXPECT_EQ(NULL, service.get());
1920}
1921
1922TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1923 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001924 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001925 Error error;
1926 ServiceRefPtr service =
1927 manager()->ConfigureServiceForProfile("", args, &error);
1928 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001929 EXPECT_EQ("service type is unsupported", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001930 EXPECT_EQ(NULL, service.get());
1931}
1932
1933TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1934 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001935 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001936 Error error;
1937 ServiceRefPtr service =
1938 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1939 EXPECT_EQ(Error::kNotFound, error.type());
1940 EXPECT_EQ("Profile specified was not found", error.message());
1941 EXPECT_EQ(NULL, service.get());
1942}
1943
1944TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1945 const string kProfileName0 = "profile0";
1946 const string kProfileName1 = "profile1";
1947 scoped_refptr<MockProfile> profile0(
1948 AddNamedMockProfileToManager(manager(), kProfileName0));
1949
1950 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001951 args.SetString(kTypeProperty, kTypeWifi);
1952 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001953 Error error;
1954 ServiceRefPtr service =
1955 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1956 EXPECT_EQ(Error::kInvalidArguments, error.type());
1957 EXPECT_EQ("Profile argument does not match that in "
1958 "the configuration arguments", error.message());
1959 EXPECT_EQ(NULL, service.get());
1960}
1961
1962TEST_F(ManagerTest,
1963 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1964 const string kProfileName0 = "profile0";
1965 scoped_refptr<MockProfile> profile0(
1966 AddNamedMockProfileToManager(manager(), kProfileName0));
1967 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001968 args.SetString(kTypeProperty, kTypeWifi);
1969 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001970
1971 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1972 .WillOnce(Return(WiFiServiceRefPtr()));
1973 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1974 .WillOnce(Return(WiFiServiceRefPtr()));
1975 Error error;
1976 ServiceRefPtr service =
1977 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1978 // Since we didn't set the error in the GetService expectation above...
1979 EXPECT_TRUE(error.IsSuccess());
1980 EXPECT_EQ(NULL, service.get());
1981}
1982
1983TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1984 const string kProfileName0 = "profile0";
1985 scoped_refptr<MockProfile> profile0(
1986 AddNamedMockProfileToManager(manager(), kProfileName0));
1987
1988 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001989 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001990
1991 scoped_refptr<MockWiFiService> mock_service(
1992 new NiceMock<MockWiFiService>(control_interface(),
1993 dispatcher(),
1994 metrics(),
1995 manager(),
1996 wifi_provider_,
1997 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07001998 kModeManaged,
1999 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002000 false));
2001 ServiceRefPtr mock_service_generic(mock_service.get());
2002 mock_service->set_profile(profile0);
2003 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2004 .WillOnce(Return(WiFiServiceRefPtr()));
2005 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2006 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2007 .WillOnce(Return(true));
2008 Error error;
2009 ServiceRefPtr service =
2010 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2011 EXPECT_TRUE(error.IsSuccess());
2012 EXPECT_EQ(mock_service.get(), service.get());
2013 mock_service->set_profile(NULL); // Breaks reference cycle.
2014}
2015
2016TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2017 scoped_refptr<MockService> mock_service(
2018 new NiceMock<MockService>(control_interface(),
2019 dispatcher(),
2020 metrics(),
2021 manager()));
2022 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002023 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002024 manager()->RegisterService(mock_service);
2025 ServiceRefPtr mock_service_generic(mock_service.get());
2026
2027 const string kProfileName = "profile";
2028 scoped_refptr<MockProfile> profile(
2029 AddNamedMockProfileToManager(manager(), kProfileName));
2030 mock_service->set_profile(profile);
2031
2032 EXPECT_CALL(*mock_service, technology())
2033 .WillOnce(Return(Technology::kCellular))
2034 .WillOnce(Return(Technology::kWifi));
2035
2036 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2037 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2038 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2039
2040 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002041 args.SetString(kTypeProperty, kTypeWifi);
2042 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002043
2044 // The first attempt should fail because the service reports a technology
2045 // other than "WiFi".
2046 {
2047 Error error;
2048 ServiceRefPtr service =
2049 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2050 EXPECT_EQ(NULL, service.get());
2051 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002052 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002053 }
2054
2055 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2056 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2057
2058 {
2059 Error error;
2060 ServiceRefPtr service =
2061 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2062 EXPECT_TRUE(error.IsSuccess());
2063 EXPECT_EQ(mock_service.get(), service.get());
2064 EXPECT_EQ(profile.get(), service->profile().get());
2065 }
2066 mock_service->set_profile(NULL); // Breaks reference cycle.
2067}
2068
2069TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2070 const string kProfileName = "profile";
2071 scoped_refptr<MockProfile> profile(
2072 AddNamedMockProfileToManager(manager(), kProfileName));
2073
2074 scoped_refptr<MockWiFiService> mock_service(
2075 new NiceMock<MockWiFiService>(control_interface(),
2076 dispatcher(),
2077 metrics(),
2078 manager(),
2079 wifi_provider_,
2080 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002081 kModeManaged,
2082 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002083 false));
2084 mock_service->set_profile(profile);
2085 ServiceRefPtr mock_service_generic(mock_service.get());
2086
2087 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002088 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002089 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2090 .WillOnce(Return(mock_service));
2091 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2092 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2093 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2094 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2095
2096 Error error;
2097 ServiceRefPtr service =
2098 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2099 EXPECT_TRUE(error.IsSuccess());
2100 EXPECT_EQ(mock_service.get(), service.get());
2101 EXPECT_EQ(profile.get(), service->profile().get());
2102 mock_service->set_profile(NULL); // Breaks reference cycle.
2103}
2104
2105TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2106 const string kProfileName = "profile";
2107 scoped_refptr<MockProfile> profile(
2108 AddNamedMockProfileToManager(manager(), kProfileName));
2109
2110 scoped_refptr<MockWiFiService> mock_service(
2111 new NiceMock<MockWiFiService>(control_interface(),
2112 dispatcher(),
2113 metrics(),
2114 manager(),
2115 wifi_provider_,
2116 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002117 kModeManaged,
2118 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002119 false));
2120 mock_service->set_profile(GetEphemeralProfile(manager()));
2121 ServiceRefPtr mock_service_generic(mock_service.get());
2122
2123 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002124 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002125 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2126 .WillOnce(Return(mock_service));
2127 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2128 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2129 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2130
2131 Error error;
2132 ServiceRefPtr service =
2133 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2134 EXPECT_TRUE(error.IsSuccess());
2135 EXPECT_EQ(mock_service.get(), service.get());
2136 EXPECT_EQ(profile.get(), service->profile().get());
2137 mock_service->set_profile(NULL); // Breaks reference cycle.
2138}
2139
2140TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2141 const string kProfileName0 = "profile0";
2142 scoped_refptr<MockProfile> profile0(
2143 AddNamedMockProfileToManager(manager(), kProfileName0));
2144 const string kProfileName1 = "profile1";
2145 scoped_refptr<MockProfile> profile1(
2146 AddNamedMockProfileToManager(manager(), kProfileName1));
2147
2148 scoped_refptr<MockWiFiService> mock_service(
2149 new NiceMock<MockWiFiService>(control_interface(),
2150 dispatcher(),
2151 metrics(),
2152 manager(),
2153 wifi_provider_,
2154 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002155 kModeManaged,
2156 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002157 false));
2158 manager()->RegisterService(mock_service);
2159 mock_service->set_profile(profile0);
2160 ServiceRefPtr mock_service_generic(mock_service.get());
2161
2162 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002163 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002164 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2165 .WillOnce(Return(mock_service));
2166 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2167 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2168 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2169 // This happens once to make the service loadable for the ConfigureService
2170 // below, and a second time after the service is modified.
2171 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2172 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2173 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2174 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2175
2176 Error error;
2177 ServiceRefPtr service =
2178 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2179 EXPECT_TRUE(error.IsSuccess());
2180 EXPECT_EQ(mock_service.get(), service.get());
2181 mock_service->set_profile(NULL); // Breaks reference cycle.
2182}
2183
2184TEST_F(ManagerTest,
2185 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2186 const string kProfileName0 = "profile0";
2187 scoped_refptr<MockProfile> profile0(
2188 AddNamedMockProfileToManager(manager(), kProfileName0));
2189 const string kProfileName1 = "profile1";
2190 scoped_refptr<MockProfile> profile1(
2191 AddNamedMockProfileToManager(manager(), kProfileName1));
2192
2193 scoped_refptr<MockWiFiService> matching_service(
2194 new StrictMock<MockWiFiService>(control_interface(),
2195 dispatcher(),
2196 metrics(),
2197 manager(),
2198 wifi_provider_,
2199 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002200 kModeManaged,
2201 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002202 false));
2203 matching_service->set_profile(profile1);
2204
2205 // We need to get rid of our reference to this mock service as soon
2206 // as Manager::ConfigureServiceForProfile() takes a reference in its
2207 // call to WiFiProvider::CreateTemporaryService(). This way the
2208 // latter function can keep a DCHECK(service->HasOneRef() even in
2209 // unit tests.
2210 temp_mock_service_ =
2211 new NiceMock<MockWiFiService>(control_interface(),
2212 dispatcher(),
2213 metrics(),
2214 manager(),
2215 wifi_provider_,
2216 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002217 kModeManaged,
2218 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002219 false);
2220
2221 // Only hold a pointer here so we don't affect the refcount.
2222 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2223
2224 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002225 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002226 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2227 .WillOnce(Return(matching_service));
2228 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2229 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2230 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2231 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2232 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2233 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2234 .Times(1);
2235 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2236 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2237
2238 Error error;
2239 ServiceRefPtr service =
2240 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2241 EXPECT_TRUE(error.IsSuccess());
2242 EXPECT_EQ(NULL, service.get());
2243 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2244}
2245
Paul Stewart7a20aa42013-01-17 12:21:41 -08002246TEST_F(ManagerTest, FindMatchingService) {
2247 KeyValueStore args;
2248 {
2249 Error error;
2250 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2251 EXPECT_EQ(Error::kNotFound, error.type());
2252 }
2253
2254 scoped_refptr<MockService> mock_service0(
2255 new NiceMock<MockService>(control_interface(),
2256 dispatcher(),
2257 metrics(),
2258 manager()));
2259 scoped_refptr<MockService> mock_service1(
2260 new NiceMock<MockService>(control_interface(),
2261 dispatcher(),
2262 metrics(),
2263 manager()));
2264 manager()->RegisterService(mock_service0);
2265 manager()->RegisterService(mock_service1);
2266 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2267 .WillOnce(Return(true))
2268 .WillRepeatedly(Return(false));
2269 {
2270 Error error;
2271 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2272 EXPECT_TRUE(error.IsSuccess());
2273 }
2274 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2275 .WillOnce(Return(true))
2276 .WillRepeatedly(Return(false));
2277 {
2278 Error error;
2279 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2280 EXPECT_TRUE(error.IsSuccess());
2281 }
2282 {
2283 Error error;
2284 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2285 EXPECT_EQ(Error::kNotFound, error.type());
2286 }
2287}
2288
Paul Stewart22aa71b2011-09-16 12:15:11 -07002289TEST_F(ManagerTest, TechnologyOrder) {
2290 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002291 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2292 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002293 ASSERT_TRUE(error.IsSuccess());
2294 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002295 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002296
Ben Chan923a5022013-09-20 11:23:23 -07002297 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2298 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002299 ASSERT_FALSE(error.IsSuccess());
2300 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002301 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002302 manager()->GetTechnologyOrder());
2303}
2304
2305TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002306 // TODO(quiche): Some of these tests would probably fit better in
2307 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002308 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002309
mukesh agrawale37ad322013-10-08 16:33:56 -07002310 // Construct our Services so that the string comparison of
2311 // unique_name_ differs from the numerical comparison of
2312 // serial_number_.
2313 vector<scoped_refptr<MockService>> mock_services;
2314 for (size_t i = 0; i < 11; ++i) {
2315 mock_services.push_back(
2316 new NiceMock<MockService>(control_interface(),
2317 dispatcher(),
2318 metrics(),
2319 manager()));
2320 }
2321 scoped_refptr<MockService> mock_service2 = mock_services[2];
2322 scoped_refptr<MockService> mock_service10 = mock_services[10];
2323 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002324
mukesh agrawale37ad322013-10-08 16:33:56 -07002325 manager()->RegisterService(mock_service2);
2326 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002327
mukesh agrawale37ad322013-10-08 16:33:56 -07002328 // Services should already be sorted by |serial_number_|.
2329 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002330
2331 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002332 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002333 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002334
2335 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002336 mock_service10->SetStrength(1);
2337 manager()->UpdateService(mock_service10);
2338 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002339
2340 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002341 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2342 manager()->UpdateService(mock_service2);
2343 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002344
2345 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002346 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002347 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002348 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002349 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002350
2351 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002352 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002353 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002354 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002355
Ben Chan923a5022013-09-20 11:23:23 -07002356 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2357 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002358 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002359 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002360
Gaurav Shah435de2c2011-11-17 19:01:07 -08002361 // Priority.
mukesh agrawale37ad322013-10-08 16:33:56 -07002362 mock_service2->SetPriority(1, NULL);
2363 manager()->UpdateService(mock_service2);
2364 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002365
Paul Stewart2da34c02013-10-17 15:28:56 -07002366 // HasEverConnected.
2367 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002368 manager()->UpdateService(mock_service10);
2369 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002370
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002371 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002372 mock_service2->SetAutoConnect(true);
2373 manager()->UpdateService(mock_service2);
2374 mock_service10->SetAutoConnect(false);
2375 manager()->UpdateService(mock_service10);
2376 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002377
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002378 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2379 // to any of the others below, so we reset to the default state after
2380 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002381 EXPECT_CALL(*mock_service10.get(),
2382 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002383 .WillOnce(Return(true))
2384 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002385 manager()->UpdateService(mock_service10);
2386 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2387 manager()->UpdateService(mock_service2);
2388 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002389
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002390 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002391 mock_service10->SetConnectable(true);
2392 manager()->UpdateService(mock_service10);
2393 mock_service2->SetConnectable(false);
2394 manager()->UpdateService(mock_service2);
2395 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002396
2397 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002398 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002399 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002400 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002401 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002402 manager()->UpdateService(mock_service2);
2403 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002404 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002405 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002406 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002407 manager()->UpdateService(mock_service10);
2408 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002409
2410 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002411 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002412 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002413 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002414 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002415 manager()->UpdateService(mock_service10);
2416 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002417
mukesh agrawale37ad322013-10-08 16:33:56 -07002418 // Connected-but-portalled preferred over unconnected.
2419 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002420 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002421 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002422 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002423 manager()->UpdateService(mock_service2);
2424 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002425
mukesh agrawale37ad322013-10-08 16:33:56 -07002426 // Connected preferred over connected-but-portalled.
2427 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002428 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002429 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002430 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002431 manager()->UpdateService(mock_service10);
2432 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002433
mukesh agrawale37ad322013-10-08 16:33:56 -07002434 manager()->DeregisterService(mock_service2);
2435 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002436}
2437
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002438TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002439 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002440 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002441
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002442 scoped_refptr<MockService> mock_service0(
2443 new NiceMock<MockService>(control_interface(),
2444 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002445 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002446 manager()));
2447 scoped_refptr<MockService> mock_service1(
2448 new NiceMock<MockService>(control_interface(),
2449 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002450 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002451 manager()));
2452
2453 scoped_refptr<MockConnection> mock_connection0(
2454 new NiceMock<MockConnection>(device_info_.get()));
2455 scoped_refptr<MockConnection> mock_connection1(
2456 new NiceMock<MockConnection>(device_info_.get()));
2457
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002458 // A single registered Service, without a connection. The
2459 // DefaultService should be NULL. If a change notification is
2460 // generated, it should reference kNullPath.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002461 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002462 EXPECT_CALL(*manager_adaptor_,
2463 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2464 DBusAdaptor::kNullPath))
2465 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002466 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002467 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002468
2469 // Adding another Service, also without a connection, does not
2470 // change DefaultService. Furthermore, we do not send a change
2471 // notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002472 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002473 EXPECT_CALL(*manager_adaptor_,
2474 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2475 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002476 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002477 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002478
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002479 // An explicit sort doesn't change anything, and does not emit a
2480 // change notification for DefaultService.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002481 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002482 EXPECT_CALL(*manager_adaptor_,
2483 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2484 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002485 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002486 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002487
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002488 // Re-ordering the unconnected Services doesn't change
2489 // DefaultService, and (hence) does not emit a change notification
2490 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002491 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002492 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002493 EXPECT_CALL(*manager_adaptor_,
2494 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2495 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002496 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002497 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002498
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002499 // Re-ordering the unconnected Services doesn't change
2500 // DefaultService, and (hence) does not emit a change notification
2501 // for DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002502 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002503 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002504 EXPECT_CALL(*manager_adaptor_,
2505 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2506 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002507 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002508 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002509
Paul Stewartce4ec192012-03-14 12:53:46 -07002510 mock_service0->set_mock_connection(mock_connection0);
2511 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002512
mukesh agrawale37ad322013-10-08 16:33:56 -07002513 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002514 // from ServiceOrderIs. We notify others of the change in
2515 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002516 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002517 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002518 EXPECT_CALL(*manager_adaptor_,
2519 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002520 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002521 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002522
Darin Petkova5e07ef2012-07-09 14:27:57 +02002523 ServiceWatcher service_watcher;
2524 int tag =
2525 manager()->RegisterDefaultServiceCallback(
2526 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2527 service_watcher.AsWeakPtr()));
2528 EXPECT_EQ(1, tag);
2529
mukesh agrawale37ad322013-10-08 16:33:56 -07002530 // Changing the ordering causes the DefaultService to change, and
2531 // appropriate notifications are sent.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002532 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002533 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2534 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002535 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002536 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002537 EXPECT_CALL(*manager_adaptor_,
2538 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002539 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002540 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002541
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002542 // Deregistering a DefaultServiceCallback works as expected. (Later
2543 // code causes DefaultService changes, but we see no further calls
2544 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002545 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002546 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002547
2548 // Deregistering the current DefaultService causes the other Service
2549 // to become default. Appropriate notifications are sent.
2550 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, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002554 mock_service1->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002555 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002556 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002557
mukesh agrawale37ad322013-10-08 16:33:56 -07002558 // Deregistering the only Service causes the DefaultService to become
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002559 // NULL. Appropriate notifications are sent.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002560 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002561 EXPECT_CALL(*manager_adaptor_,
2562 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
mukesh agrawale37ad322013-10-08 16:33:56 -07002563 mock_service0->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002564 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002565 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002566
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002567 // An explicit sort doesn't change anything, and does not generate
2568 // an external notification.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002569 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002570 EXPECT_CALL(*manager_adaptor_,
2571 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002572 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002573}
2574
Darin Petkova5e07ef2012-07-09 14:27:57 +02002575TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2576 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2577 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2578
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002579 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002580 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002581
2582 scoped_refptr<MockService> mock_service(
2583 new NiceMock<MockService>(
2584 control_interface(), dispatcher(), metrics(), manager()));
2585 ServiceRefPtr service = mock_service;
2586 ServiceRefPtr null_service;
2587
2588 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2589 manager()->NotifyDefaultServiceChanged(null_service);
2590
2591 ServiceWatcher service_watcher1;
2592 ServiceWatcher service_watcher2;
2593 int tag1 =
2594 manager()->RegisterDefaultServiceCallback(
2595 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2596 service_watcher1.AsWeakPtr()));
2597 EXPECT_EQ(1, tag1);
2598 int tag2 =
2599 manager()->RegisterDefaultServiceCallback(
2600 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2601 service_watcher2.AsWeakPtr()));
2602 EXPECT_EQ(2, tag2);
2603
2604 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2605 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2606 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2607 manager()->NotifyDefaultServiceChanged(null_service);
2608
2609 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2610 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2611 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2612 manager()->NotifyDefaultServiceChanged(mock_service);
2613
2614 manager()->DeregisterDefaultServiceCallback(tag1);
2615 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2616 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2617 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2618 manager()->NotifyDefaultServiceChanged(mock_service);
2619 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2620
2621 manager()->DeregisterDefaultServiceCallback(tag2);
2622 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2623 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2624 manager()->NotifyDefaultServiceChanged(mock_service);
2625
2626 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2627 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2628}
2629
Peter Qiu9d581932014-04-14 16:37:37 -07002630TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2631 int connection_id1 = 100;
2632 int connection_id2 = 200;
2633 scoped_refptr<MockService> mock_service1 =
2634 new NiceMock<MockService>(control_interface(), dispatcher(),
2635 metrics(), manager());
2636 mock_service1->set_connection_id(connection_id1);
2637 scoped_refptr<MockService> mock_service2 =
2638 new NiceMock<MockService>(control_interface(), dispatcher(),
2639 metrics(), manager());
2640 mock_service2->set_connection_id(connection_id1);
2641 scoped_refptr<MockService> mock_service3 =
2642 new NiceMock<MockService>(control_interface(), dispatcher(),
2643 metrics(), manager());
2644 mock_service3->set_connection_id(connection_id2);
2645
2646 manager()->RegisterService(mock_service1);
2647 manager()->RegisterService(mock_service2);
2648 manager()->RegisterService(mock_service3);
2649
2650 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2651 manager()->ReportServicesOnSameNetwork(connection_id1);
2652
2653 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2654 manager()->ReportServicesOnSameNetwork(connection_id2);
2655}
2656
Gaurav Shah435de2c2011-11-17 19:01:07 -08002657TEST_F(ManagerTest, AvailableTechnologies) {
2658 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2659 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002660 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002661 manager(),
2662 "null4",
2663 "addr4",
2664 0));
2665 manager()->RegisterDevice(mock_devices_[0]);
2666 manager()->RegisterDevice(mock_devices_[1]);
2667 manager()->RegisterDevice(mock_devices_[2]);
2668 manager()->RegisterDevice(mock_devices_[3]);
2669
2670 ON_CALL(*mock_devices_[0].get(), technology())
2671 .WillByDefault(Return(Technology::kEthernet));
2672 ON_CALL(*mock_devices_[1].get(), technology())
2673 .WillByDefault(Return(Technology::kWifi));
2674 ON_CALL(*mock_devices_[2].get(), technology())
2675 .WillByDefault(Return(Technology::kCellular));
2676 ON_CALL(*mock_devices_[3].get(), technology())
2677 .WillByDefault(Return(Technology::kWifi));
2678
2679 set<string> expected_technologies;
2680 expected_technologies.insert(Technology::NameFromIdentifier(
2681 Technology::kEthernet));
2682 expected_technologies.insert(Technology::NameFromIdentifier(
2683 Technology::kWifi));
2684 expected_technologies.insert(Technology::NameFromIdentifier(
2685 Technology::kCellular));
2686 Error error;
2687 vector<string> technologies = manager()->AvailableTechnologies(&error);
2688
2689 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2690 ContainerEq(expected_technologies));
2691}
2692
2693TEST_F(ManagerTest, ConnectedTechnologies) {
2694 scoped_refptr<MockService> connected_service1(
2695 new NiceMock<MockService>(control_interface(),
2696 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002697 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002698 manager()));
2699 scoped_refptr<MockService> connected_service2(
2700 new NiceMock<MockService>(control_interface(),
2701 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002702 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002703 manager()));
2704 scoped_refptr<MockService> disconnected_service1(
2705 new NiceMock<MockService>(control_interface(),
2706 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002707 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002708 manager()));
2709 scoped_refptr<MockService> disconnected_service2(
2710 new NiceMock<MockService>(control_interface(),
2711 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002712 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002713 manager()));
2714
2715 ON_CALL(*connected_service1.get(), IsConnected())
2716 .WillByDefault(Return(true));
2717 ON_CALL(*connected_service2.get(), IsConnected())
2718 .WillByDefault(Return(true));
2719
2720 manager()->RegisterService(connected_service1);
2721 manager()->RegisterService(connected_service2);
2722 manager()->RegisterService(disconnected_service1);
2723 manager()->RegisterService(disconnected_service2);
2724
2725 manager()->RegisterDevice(mock_devices_[0]);
2726 manager()->RegisterDevice(mock_devices_[1]);
2727 manager()->RegisterDevice(mock_devices_[2]);
2728 manager()->RegisterDevice(mock_devices_[3]);
2729
2730 ON_CALL(*mock_devices_[0].get(), technology())
2731 .WillByDefault(Return(Technology::kEthernet));
2732 ON_CALL(*mock_devices_[1].get(), technology())
2733 .WillByDefault(Return(Technology::kWifi));
2734 ON_CALL(*mock_devices_[2].get(), technology())
2735 .WillByDefault(Return(Technology::kCellular));
2736 ON_CALL(*mock_devices_[3].get(), technology())
2737 .WillByDefault(Return(Technology::kWifi));
2738
2739 mock_devices_[0]->SelectService(connected_service1);
2740 mock_devices_[1]->SelectService(disconnected_service1);
2741 mock_devices_[2]->SelectService(disconnected_service2);
2742 mock_devices_[3]->SelectService(connected_service2);
2743
2744 set<string> expected_technologies;
2745 expected_technologies.insert(Technology::NameFromIdentifier(
2746 Technology::kEthernet));
2747 expected_technologies.insert(Technology::NameFromIdentifier(
2748 Technology::kWifi));
2749 Error error;
2750
2751 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2752 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2753 ContainerEq(expected_technologies));
2754}
2755
2756TEST_F(ManagerTest, DefaultTechnology) {
2757 scoped_refptr<MockService> connected_service(
2758 new NiceMock<MockService>(control_interface(),
2759 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002760 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002761 manager()));
2762 scoped_refptr<MockService> disconnected_service(
2763 new NiceMock<MockService>(control_interface(),
2764 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002765 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002766 manager()));
2767
2768 // Connected. WiFi.
2769 ON_CALL(*connected_service.get(), IsConnected())
2770 .WillByDefault(Return(true));
2771 ON_CALL(*connected_service.get(), state())
2772 .WillByDefault(Return(Service::kStateConnected));
2773 ON_CALL(*connected_service.get(), technology())
2774 .WillByDefault(Return(Technology::kWifi));
2775
2776 // Disconnected. Ethernet.
2777 ON_CALL(*disconnected_service.get(), technology())
2778 .WillByDefault(Return(Technology::kEthernet));
2779
2780 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002781 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002782 Error error;
2783 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2784
2785
2786 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002787 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002788 // Connected service should be brought to the front now.
2789 string expected_technology =
2790 Technology::NameFromIdentifier(Technology::kWifi);
2791 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2792}
2793
Paul Stewart212d60f2012-07-12 10:59:13 -07002794TEST_F(ManagerTest, Stop) {
2795 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002796 new NiceMock<MockProfile>(
2797 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002798 AdoptProfile(manager(), profile);
2799 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002800 new NiceMock<MockService>(control_interface(),
2801 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002802 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002803 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002804 manager()->RegisterService(service);
2805 manager()->RegisterDevice(mock_devices_[0]);
2806 EXPECT_CALL(*profile.get(),
2807 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2808 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002809 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002810 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2811 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002812 manager()->Stop();
2813}
2814
mukesh agrawal00917ce2011-11-22 23:56:55 +00002815TEST_F(ManagerTest, UpdateServiceConnected) {
2816 scoped_refptr<MockService> mock_service(
2817 new NiceMock<MockService>(control_interface(),
2818 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002819 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002820 manager()));
2821 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002822 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002823 EXPECT_FALSE(mock_service->auto_connect());
2824
Gaurav Shah435de2c2011-11-17 19:01:07 -08002825 EXPECT_CALL(*mock_service.get(), IsConnected())
2826 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002827 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002828 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2829 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2830 // would break the SortServices test. (crbug.com/206367)
2831 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002832 EXPECT_TRUE(mock_service->auto_connect());
2833}
2834
Paul Stewart2da34c02013-10-17 15:28:56 -07002835TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002836 // This tests the case where the user connects to a service that is
2837 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002838 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002839 scoped_refptr<MockService> mock_service(
2840 new NiceMock<MockService>(control_interface(),
2841 dispatcher(),
2842 metrics(),
2843 manager()));
2844 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002845 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002846 EXPECT_FALSE(mock_service->auto_connect());
2847
Gary Moraind93615e2012-04-27 11:50:03 -07002848 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002849 new MockProfile(
2850 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002851
Gary Moraind93615e2012-04-27 11:50:03 -07002852 mock_service->set_profile(profile);
2853 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002854 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002855 EXPECT_CALL(*profile,
2856 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002857 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002858 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2859 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2860 // would break the SortServices test. (crbug.com/206367)
2861 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002862 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002863 // This releases the ref on the mock profile.
2864 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002865}
2866
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002867TEST_F(ManagerTest, SaveSuccessfulService) {
2868 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002869 new StrictMock<MockProfile>(
2870 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002871 AdoptProfile(manager(), profile);
2872 scoped_refptr<MockService> service(
2873 new NiceMock<MockService>(control_interface(),
2874 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002875 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002876 manager()));
2877
2878 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2879 ServiceRefPtr expect_service(service.get());
2880
2881 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2882 .WillOnce(Return(false));
2883 manager()->RegisterService(service);
2884
2885 EXPECT_CALL(*service.get(), state())
2886 .WillRepeatedly(Return(Service::kStateConnected));
2887 EXPECT_CALL(*service.get(), IsConnected())
2888 .WillRepeatedly(Return(true));
2889 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2890 .WillOnce(Return(true));
2891 manager()->UpdateService(service);
2892}
2893
Darin Petkove7c6ad32012-06-29 10:22:09 +02002894TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002895 MockProfile *profile0 =
2896 new MockProfile(control_interface(), metrics(), manager(), "");
2897 MockProfile *profile1 =
2898 new MockProfile(control_interface(), metrics(), manager(), "");
2899 MockProfile *profile2 =
2900 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002901 AdoptProfile(manager(), profile0); // Passes ownership.
2902 AdoptProfile(manager(), profile1); // Passes ownership.
2903 AdoptProfile(manager(), profile2); // Passes ownership.
2904 DeviceRefPtr device_ref(mock_devices_[0].get());
2905 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2906 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2907 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2908 manager()->UpdateDevice(mock_devices_[0]);
2909}
2910
Paul Stewart1b253142012-01-26 14:05:52 -08002911TEST_F(ManagerTest, EnumerateProfiles) {
2912 vector<string> profile_paths;
2913 for (size_t i = 0; i < 10; i++) {
2914 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002915 new StrictMock<MockProfile>(
2916 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002917 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002918 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2919 .WillOnce(Return(profile_paths.back()));
2920 AdoptProfile(manager(), profile);
2921 }
2922
2923 Error error;
2924 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2925 EXPECT_TRUE(error.IsSuccess());
2926 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2927 for (size_t i = 0; i < profile_paths.size(); i++) {
2928 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2929 }
2930}
2931
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002932TEST_F(ManagerTest, AutoConnectOnRegister) {
2933 MockServiceRefPtr service = MakeAutoConnectableService();
2934 EXPECT_CALL(*service.get(), AutoConnect());
2935 manager()->RegisterService(service);
2936 dispatcher()->DispatchPendingEvents();
2937}
2938
2939TEST_F(ManagerTest, AutoConnectOnUpdate) {
2940 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002941 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002942 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002943 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002944 manager()->RegisterService(service1);
2945 manager()->RegisterService(service2);
2946 dispatcher()->DispatchPendingEvents();
2947
2948 EXPECT_CALL(*service1.get(), AutoConnect());
2949 EXPECT_CALL(*service2.get(), state())
2950 .WillRepeatedly(Return(Service::kStateFailure));
2951 EXPECT_CALL(*service2.get(), IsFailed())
2952 .WillRepeatedly(Return(true));
2953 EXPECT_CALL(*service2.get(), IsConnected())
2954 .WillRepeatedly(Return(false));
2955 manager()->UpdateService(service2);
2956 dispatcher()->DispatchPendingEvents();
2957}
2958
2959TEST_F(ManagerTest, AutoConnectOnDeregister) {
2960 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002961 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002962 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002963 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002964 manager()->RegisterService(service1);
2965 manager()->RegisterService(service2);
2966 dispatcher()->DispatchPendingEvents();
2967
2968 EXPECT_CALL(*service1.get(), AutoConnect());
2969 manager()->DeregisterService(service2);
2970 dispatcher()->DispatchPendingEvents();
2971}
2972
Daniel Eratfac09532014-04-17 20:25:59 -07002973TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02002974 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002975 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02002976 SetPowerManager();
2977 EXPECT_CALL(*service, AutoConnect()).Times(0);
2978 manager()->RegisterService(service);
2979 dispatcher()->DispatchPendingEvents();
2980}
2981
Daniel Eratfac09532014-04-17 20:25:59 -07002982TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02002983 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002984 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02002985 SetPowerManager();
2986 EXPECT_CALL(*service, AutoConnect());
2987 manager()->RegisterService(service);
2988 dispatcher()->DispatchPendingEvents();
2989}
2990
Paul Stewart63864b62012-11-07 15:10:55 -08002991TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2992 SetRunning(false);
2993 MockServiceRefPtr service = MakeAutoConnectableService();
2994 EXPECT_CALL(*service, AutoConnect()).Times(0);
2995 manager()->RegisterService(service);
2996 dispatcher()->DispatchPendingEvents();
2997}
2998
Daniel Eratfac09532014-04-17 20:25:59 -07002999TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02003000 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02003001 SetPowerManager();
3002 EXPECT_CALL(*service, AutoConnect());
3003 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07003004 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003005 dispatcher()->DispatchPendingEvents();
3006
Daniel Eratfac09532014-04-17 20:25:59 -07003007 const int kSuspendId = 1;
3008 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
3009 OnSuspendImminent(kSuspendId);
3010 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02003011 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003012 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003013
Daniel Eratfac09532014-04-17 20:25:59 -07003014 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
3015 OnSuspendDone(kSuspendId);
3016 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02003017 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07003018 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02003019}
3020
Darin Petkov3ec55342012-09-28 14:04:44 +02003021TEST_F(ManagerTest, AddTerminationAction) {
Daniel Eratfac09532014-04-17 20:25:59 -07003022 EXPECT_CALL(*power_manager_, AddSuspendDelay(_, _, _, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02003023 SetPowerManager();
3024 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3025 manager()->AddTerminationAction("action1", base::Closure());
3026 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3027 manager()->AddTerminationAction("action2", base::Closure());
3028}
3029
3030TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003031 const char kKey1[] = "action1";
3032 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02003033
3034 MockPowerManager &power_manager = *power_manager_;
3035 SetPowerManager();
3036
3037 // Removing an action when the hook table is empty should not result in any
3038 // calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003039 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003040 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3041 manager()->RemoveTerminationAction("unknown");
3042 Mock::VerifyAndClearExpectations(&power_manager);
3043
Daniel Eratfac09532014-04-17 20:25:59 -07003044 EXPECT_CALL(power_manager, AddSuspendDelay(_, _, _, _, _))
3045 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003046 manager()->AddTerminationAction(kKey1, base::Closure());
3047 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3048 manager()->AddTerminationAction(kKey2, base::Closure());
Daniel Eratfac09532014-04-17 20:25:59 -07003049 Mock::VerifyAndClearExpectations(&power_manager);
Darin Petkov3ec55342012-09-28 14:04:44 +02003050
3051 // Removing an action that ends up with a non-empty hook table should not
3052 // result in any calls to the power manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003053 EXPECT_CALL(power_manager, RemoveSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003054 manager()->RemoveTerminationAction(kKey1);
3055 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3056 Mock::VerifyAndClearExpectations(&power_manager);
3057
3058 // Removing the last action should trigger unregistering from the power
3059 // manager.
Daniel Eratfac09532014-04-17 20:25:59 -07003060 EXPECT_CALL(power_manager, RemoveSuspendDelay(_))
Daniel Erat0818cca2012-12-14 10:16:21 -08003061 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003062 manager()->RemoveTerminationAction(kKey2);
3063 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3064}
3065
3066TEST_F(ManagerTest, RunTerminationActions) {
3067 TerminationActionTest test_action;
3068 const string kActionName = "action";
3069
3070 EXPECT_CALL(test_action, Done(_));
3071 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3072 test_action.AsWeakPtr()));
3073
3074 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3075 Bind(&TerminationActionTest::Action,
3076 test_action.AsWeakPtr()));
3077 test_action.set_manager(manager());
3078 EXPECT_CALL(test_action, Done(_));
3079 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3080 test_action.AsWeakPtr()));
3081}
3082
Daniel Erat0818cca2012-12-14 10:16:21 -08003083TEST_F(ManagerTest, OnSuspendImminent) {
3084 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02003085 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Eratfac09532014-04-17 20:25:59 -07003086 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003087 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003088 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02003089}
3090
3091TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003092 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02003093 Error error;
Daniel Eratfac09532014-04-17 20:25:59 -07003094 EXPECT_CALL(*power_manager_, ReportSuspendReadiness(_, kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003095 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003096 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003097}
3098
Paul Stewartc681fa02012-03-02 19:40:04 -08003099TEST_F(ManagerTest, RecheckPortal) {
3100 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3101 .WillOnce(Return(false));
3102 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3103 .WillOnce(Return(true));
3104 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3105 .Times(0);
3106
3107 manager()->RegisterDevice(mock_devices_[0]);
3108 manager()->RegisterDevice(mock_devices_[1]);
3109 manager()->RegisterDevice(mock_devices_[2]);
3110
3111 manager()->RecheckPortal(NULL);
3112}
3113
Paul Stewartd215af62012-04-24 23:25:50 -07003114TEST_F(ManagerTest, RecheckPortalOnService) {
3115 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3116 dispatcher(),
3117 metrics(),
3118 manager());
3119 EXPECT_CALL(*mock_devices_[0].get(),
3120 IsConnectedToService(IsRefPtrTo(service)))
3121 .WillOnce(Return(false));
3122 EXPECT_CALL(*mock_devices_[1].get(),
3123 IsConnectedToService(IsRefPtrTo(service)))
3124 .WillOnce(Return(true));
3125 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3126 .WillOnce(Return(true));
3127 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3128 .Times(0);
3129
3130 manager()->RegisterDevice(mock_devices_[0]);
3131 manager()->RegisterDevice(mock_devices_[1]);
3132 manager()->RegisterDevice(mock_devices_[2]);
3133
3134 manager()->RecheckPortalOnService(service);
3135}
3136
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003137TEST_F(ManagerTest, GetDefaultService) {
3138 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003139 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003140
3141 scoped_refptr<MockService> mock_service(
3142 new NiceMock<MockService>(control_interface(),
3143 dispatcher(),
3144 metrics(),
3145 manager()));
3146
3147 manager()->RegisterService(mock_service);
3148 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003149 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003150
3151 scoped_refptr<MockConnection> mock_connection(
3152 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003153 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003154 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003155 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003156
Paul Stewartce4ec192012-03-14 12:53:46 -07003157 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003158 manager()->DeregisterService(mock_service);
3159}
3160
Paul Stewart13ed2252012-03-21 12:52:46 -07003161TEST_F(ManagerTest, GetServiceWithGUID) {
3162 scoped_refptr<MockService> mock_service0(
3163 new NiceMock<MockService>(control_interface(),
3164 dispatcher(),
3165 metrics(),
3166 manager()));
3167
3168 scoped_refptr<MockService> mock_service1(
3169 new NiceMock<MockService>(control_interface(),
3170 dispatcher(),
3171 metrics(),
3172 manager()));
3173
Paul Stewartcb59fed2012-03-21 21:14:46 -07003174 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3175 .Times(0);
3176 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3177 .Times(0);
3178
Paul Stewart13ed2252012-03-21 12:52:46 -07003179 manager()->RegisterService(mock_service0);
3180 manager()->RegisterService(mock_service1);
3181
3182 const string kGUID0 = "GUID0";
3183 const string kGUID1 = "GUID1";
3184
3185 {
3186 Error error;
3187 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3188 EXPECT_FALSE(error.IsSuccess());
3189 EXPECT_FALSE(service);
3190 }
3191
3192 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003193 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003194
3195 {
3196 Error error;
3197 ServiceRefPtr service = manager()->GetService(args, &error);
3198 EXPECT_EQ(Error::kInvalidArguments, error.type());
3199 EXPECT_FALSE(service);
3200 }
3201
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003202 mock_service0->SetGuid(kGUID0, NULL);
3203 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003204
3205 {
3206 Error error;
3207 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3208 EXPECT_TRUE(error.IsSuccess());
3209 EXPECT_EQ(mock_service0.get(), service.get());
3210 }
3211
3212 {
3213 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003214 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3215 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003216 ServiceRefPtr service = manager()->GetService(args, &error);
3217 EXPECT_TRUE(error.IsSuccess());
3218 EXPECT_EQ(mock_service1.get(), service.get());
3219 }
3220
3221 manager()->DeregisterService(mock_service0);
3222 manager()->DeregisterService(mock_service1);
3223}
3224
Gary Morain028545d2012-04-07 14:55:52 -07003225
3226TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003227 EXPECT_FALSE(manager()->IsOnline());
3228 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3229
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003230 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003231 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003232 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3233 .Times(AnyNumber());
3234 scoped_refptr<MockService> mock_service0(
3235 new NiceMock<MockService>(control_interface(),
3236 dispatcher(),
3237 metrics(),
3238 manager()));
3239
3240 scoped_refptr<MockService> mock_service1(
3241 new NiceMock<MockService>(control_interface(),
3242 dispatcher(),
3243 metrics(),
3244 manager()));
3245
3246 EXPECT_CALL(*mock_service0.get(), IsConnected())
3247 .WillRepeatedly(Return(false));
3248 EXPECT_CALL(*mock_service1.get(), IsConnected())
3249 .WillRepeatedly(Return(false));
3250
3251 manager()->RegisterService(mock_service0);
3252 manager()->RegisterService(mock_service1);
3253
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003254 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003255 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3256
3257 manager()->DeregisterService(mock_service0);
3258 manager()->DeregisterService(mock_service1);
3259}
3260
3261TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003262 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003263 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003264 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3265 .Times(AnyNumber());
3266 scoped_refptr<MockService> mock_service0(
3267 new NiceMock<MockService>(control_interface(),
3268 dispatcher(),
3269 metrics(),
3270 manager()));
3271
3272 scoped_refptr<MockService> mock_service1(
3273 new NiceMock<MockService>(control_interface(),
3274 dispatcher(),
3275 metrics(),
3276 manager()));
3277
3278 EXPECT_CALL(*mock_service0.get(), IsConnected())
3279 .WillRepeatedly(Return(false));
3280 EXPECT_CALL(*mock_service1.get(), IsConnected())
3281 .WillRepeatedly(Return(true));
3282 EXPECT_CALL(*mock_service0.get(), state())
3283 .WillRepeatedly(Return(Service::kStateIdle));
3284 EXPECT_CALL(*mock_service1.get(), state())
3285 .WillRepeatedly(Return(Service::kStateConnected));
3286
3287 manager()->RegisterService(mock_service0);
3288 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003289 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003290
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003291 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003292 EXPECT_EQ("online", manager()->CalculateState(NULL));
3293
3294 manager()->DeregisterService(mock_service0);
3295 manager()->DeregisterService(mock_service1);
3296}
3297
Paul Stewart03e29f72013-09-26 00:49:48 -07003298TEST_F(ManagerTest, RefreshConnectionState) {
3299 EXPECT_CALL(*manager_adaptor_,
3300 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3301 RefreshConnectionState();
3302 Mock::VerifyAndClearExpectations(manager_adaptor_);
3303
3304 scoped_refptr<MockService> mock_service(
3305 new NiceMock<MockService>(control_interface(),
3306 dispatcher(),
3307 metrics(),
3308 manager()));
3309 EXPECT_CALL(*manager_adaptor_,
3310 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3311 manager()->RegisterService(mock_service);
3312 RefreshConnectionState();
3313
3314 scoped_refptr<MockConnection> mock_connection(
3315 new NiceMock<MockConnection>(device_info_.get()));
3316 mock_service->set_mock_connection(mock_connection);
3317 EXPECT_CALL(*mock_service, state())
3318 .WillOnce(Return(Service::kStateIdle));
3319 RefreshConnectionState();
3320
3321 Mock::VerifyAndClearExpectations(manager_adaptor_);
3322 EXPECT_CALL(*mock_service, state())
3323 .WillOnce(Return(Service::kStatePortal));
3324 EXPECT_CALL(*manager_adaptor_,
3325 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3326 RefreshConnectionState();
3327 Mock::VerifyAndClearExpectations(manager_adaptor_);
3328
3329 mock_service->set_mock_connection(NULL);
3330 manager()->DeregisterService(mock_service);
3331}
3332
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003333TEST_F(ManagerTest, StartupPortalList) {
3334 // Simulate loading value from the default profile.
3335 const string kProfileValue("wifi,vpn");
3336 manager()->props_.check_portal_list = kProfileValue;
3337
3338 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3339 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3340 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3341
3342 const string kStartupValue("cellular,ethernet");
3343 manager()->SetStartupPortalList(kStartupValue);
3344 // Ensure profile value is not overwritten, so when we save the default
3345 // profile, the correct value will still be written.
3346 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3347
3348 // However we should read back a different list.
3349 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3350 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3351 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3352
3353 const string kRuntimeValue("ppp");
3354 // Setting a runtime value over the control API should overwrite both
3355 // the profile value and what we read back.
3356 Error error;
3357 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003358 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003359 kRuntimeValue,
3360 &error);
3361 ASSERT_TRUE(error.IsSuccess());
3362 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3363 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3364 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3365 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3366}
3367
Paul Stewart036dba02012-08-07 12:34:41 -07003368TEST_F(ManagerTest, LinkMonitorEnabled) {
3369 const string kEnabledTechnologies("wifi,vpn");
3370 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3371 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3372 EXPECT_FALSE(
3373 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3374}
3375
Paul Stewart85aea152013-01-22 09:31:56 -08003376TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003377 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003378 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003379 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003380 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003381 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003382 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3383 AdoptProfile(manager(), profile);
3384 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3385 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3386 scoped_ptr<MockStore> store1(new MockStore);
3387 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3388}
3389
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003390TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003391 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003392 DisableTechnologyReplyHandler disable_technology_reply_handler;
3393 ResultCallback disable_technology_callback(
3394 Bind(&DisableTechnologyReplyHandler::ReportResult,
3395 disable_technology_reply_handler.AsWeakPtr()));
3396 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3397
Ben Chan923a5022013-09-20 11:23:23 -07003398 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003399 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003400 EXPECT_TRUE(error.IsSuccess());
3401
Joshua Krollda798622012-06-05 12:30:48 -07003402 ON_CALL(*mock_devices_[0], technology())
3403 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003404 ON_CALL(*mock_devices_[1], technology())
3405 .WillByDefault(Return(Technology::kCellular));
3406 ON_CALL(*mock_devices_[2], technology())
3407 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003408
3409 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003410 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003411
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003412 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003413 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3414 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003415 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003416 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003417 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003418 EXPECT_TRUE(error.IsSuccess());
3419
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003420 // Ethernet Device is enabled, and mock doesn't change error from
3421 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003422 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3423 mock_devices_[0]->enabled_ = true;
3424 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003425 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003426 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003427 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003428
3429 // Ethernet Device is disabled, and mock doesn't change error from
3430 // kOperationInitiated, so expect enable to say operation in progress.
3431 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3432 mock_devices_[0]->enabled_ = false;
3433 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003434 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003435 &error, disable_technology_callback);
3436 EXPECT_TRUE(error.IsOngoing());
3437
3438 // Cellular Device is enabled, but disable failed.
3439 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3440 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3441 mock_devices_[1]->enabled_ = true;
3442 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003443 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003444 &error, disable_technology_callback);
3445 EXPECT_EQ(Error::kPermissionDenied, error.type());
3446
3447 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3448 // if one is in progress (even if the other completed immediately).
3449 manager()->RegisterDevice(mock_devices_[2]);
3450 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3451 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3452 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3453 mock_devices_[1]->enabled_ = true;
3454 mock_devices_[2]->enabled_ = true;
3455 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003456 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003457 &error, disable_technology_callback);
3458 EXPECT_TRUE(error.IsOngoing());
3459
3460 // ...and order doesn't matter.
3461 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3462 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3463 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3464 mock_devices_[1]->enabled_ = true;
3465 mock_devices_[2]->enabled_ = true;
3466 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003467 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003468 &error, disable_technology_callback);
3469 EXPECT_TRUE(error.IsOngoing());
3470 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3471
3472 // Multiple Cellular Devices in enabled state. Even if all disable
3473 // operations complete asynchronously, we only get one call to the
3474 // DisableTechnologyReplyHandler::ReportResult.
3475 ResultCallback device1_result_callback;
3476 ResultCallback device2_result_callback;
3477 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3478 .WillOnce(SaveArg<2>(&device1_result_callback));
3479 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3480 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3481 SaveArg<2>(&device2_result_callback)));
3482 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3483 mock_devices_[1]->enabled_ = true;
3484 mock_devices_[2]->enabled_ = true;
3485 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003486 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003487 &error, disable_technology_callback);
3488 EXPECT_TRUE(error.IsOngoing());
3489 device1_result_callback.Run(Error(Error::kSuccess));
3490 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003491}
3492
Paul Stewart4d5efb72012-09-17 12:24:34 -07003493TEST_F(ManagerTest, IgnoredSearchList) {
3494 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003495 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003496 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003497
3498 const string kIgnored0 = "chromium.org";
3499 ignored_paths.push_back(kIgnored0);
3500 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003501 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003502 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3503
3504 const string kIgnored1 = "google.com";
3505 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3506 ignored_paths.push_back(kIgnored1);
3507 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003508 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003509 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3510
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003511 ignored_paths.clear();
3512 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3513 SetIgnoredDNSSearchPaths("", NULL);
3514 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3515
Paul Stewart4d5efb72012-09-17 12:24:34 -07003516 SetResolver(Resolver::GetInstance());
3517}
3518
Paul Stewartbfb82552012-10-24 16:48:48 -07003519TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3520 // Test to make sure that every service state-change causes the
3521 // Manager to emit a new service list.
3522 scoped_refptr<MockService> mock_service(
3523 new NiceMock<MockService>(control_interface(),
3524 dispatcher(),
3525 metrics(),
3526 manager()));
3527 EXPECT_CALL(*mock_service, state())
3528 .WillRepeatedly(Return(Service::kStateIdle));
3529
3530 manager()->RegisterService(mock_service);
3531 EXPECT_CALL(
3532 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003533 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003534 EXPECT_CALL(
3535 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003536 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003537 CompleteServiceSort();
3538
3539 Mock::VerifyAndClearExpectations(manager_adaptor_);
3540 EXPECT_CALL(
3541 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003542 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003543 EXPECT_CALL(
3544 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003545 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003546 manager()->UpdateService(mock_service.get());
3547 CompleteServiceSort();
3548
3549 manager()->DeregisterService(mock_service);
3550}
3551
3552TEST_F(ManagerTest, EnumerateServices) {
3553 scoped_refptr<MockService> mock_service(
3554 new NiceMock<MockService>(control_interface(),
3555 dispatcher(),
3556 metrics(),
3557 manager()));
3558 manager()->RegisterService(mock_service);
3559
3560 EXPECT_CALL(*mock_service, state())
3561 .WillRepeatedly(Return(Service::kStateConnected));
3562 EXPECT_CALL(*mock_service, IsVisible())
3563 .WillRepeatedly(Return(false));
3564 EXPECT_TRUE(EnumerateAvailableServices().empty());
3565 EXPECT_TRUE(EnumerateWatchedServices().empty());
3566
3567 EXPECT_CALL(*mock_service, state())
3568 .WillRepeatedly(Return(Service::kStateIdle));
3569 EXPECT_TRUE(EnumerateAvailableServices().empty());
3570 EXPECT_TRUE(EnumerateWatchedServices().empty());
3571
3572 EXPECT_CALL(*mock_service, IsVisible())
3573 .WillRepeatedly(Return(true));
3574 Service::ConnectState unwatched_states[] = {
3575 Service::kStateUnknown,
3576 Service::kStateIdle,
3577 Service::kStateFailure
3578 };
3579 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3580 EXPECT_CALL(*mock_service, state())
3581 .WillRepeatedly(Return(unwatched_states[i]));
3582 EXPECT_FALSE(EnumerateAvailableServices().empty());
3583 EXPECT_TRUE(EnumerateWatchedServices().empty());
3584 }
3585
3586 Service::ConnectState watched_states[] = {
3587 Service::kStateAssociating,
3588 Service::kStateConfiguring,
3589 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003590 Service::kStatePortal,
3591 Service::kStateOnline
3592 };
3593 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3594 EXPECT_CALL(*mock_service, state())
3595 .WillRepeatedly(Return(watched_states[i]));
3596 EXPECT_FALSE(EnumerateAvailableServices().empty());
3597 EXPECT_FALSE(EnumerateWatchedServices().empty());
3598 }
3599
3600 manager()->DeregisterService(mock_service);
3601}
3602
Paul Stewart39db5ca2013-03-18 14:15:17 -07003603TEST_F(ManagerTest, ConnectToBestServices) {
3604 scoped_refptr<MockService> wifi_service0(
3605 new NiceMock<MockService>(control_interface(),
3606 dispatcher(),
3607 metrics(),
3608 manager()));
3609 EXPECT_CALL(*wifi_service0.get(), state())
3610 .WillRepeatedly(Return(Service::kStateIdle));
3611 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3612 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003613 wifi_service0->SetConnectable(true);
3614 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003615 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3616 EXPECT_CALL(*wifi_service0.get(), technology())
3617 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003618 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3619 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003620 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3621 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003622
3623 scoped_refptr<MockService> wifi_service1(
3624 new NiceMock<MockService>(control_interface(),
3625 dispatcher(),
3626 metrics(),
3627 manager()));
3628 EXPECT_CALL(*wifi_service1.get(), state())
3629 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003630 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3631 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003632 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3633 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003634 wifi_service1->SetAutoConnect(true);
3635 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003636 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3637 EXPECT_CALL(*wifi_service1.get(), technology())
3638 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003639 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3640 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003641
3642 scoped_refptr<MockService> wifi_service2(
3643 new NiceMock<MockService>(control_interface(),
3644 dispatcher(),
3645 metrics(),
3646 manager()));
3647 EXPECT_CALL(*wifi_service2.get(), state())
3648 .WillRepeatedly(Return(Service::kStateConnected));
3649 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3650 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003651 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3652 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003653 wifi_service2->SetAutoConnect(true);
3654 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003655 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3656 EXPECT_CALL(*wifi_service2.get(), technology())
3657 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003658 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3659 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003660
3661 manager()->RegisterService(wifi_service0);
3662 manager()->RegisterService(wifi_service1);
3663 manager()->RegisterService(wifi_service2);
3664
3665 CompleteServiceSort();
3666 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3667
3668 scoped_refptr<MockService> cell_service(
3669 new NiceMock<MockService>(control_interface(),
3670 dispatcher(),
3671 metrics(),
3672 manager()));
3673
3674 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003675 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003676 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003677 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003678 EXPECT_CALL(*cell_service.get(), IsVisible())
3679 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003680 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003681 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003682 EXPECT_CALL(*cell_service.get(), technology())
3683 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003684 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3685 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003686 manager()->RegisterService(cell_service);
3687
Arman Uguray6fe4f262013-08-02 20:21:55 -07003688 scoped_refptr<MockService> wimax_service(
3689 new NiceMock<MockService>(control_interface(),
3690 dispatcher(),
3691 metrics(),
3692 manager()));
3693
3694 EXPECT_CALL(*wimax_service.get(), state())
3695 .WillRepeatedly(Return(Service::kStateConnected));
3696 EXPECT_CALL(*wimax_service.get(), IsConnected())
3697 .WillRepeatedly(Return(true));
3698 EXPECT_CALL(*wimax_service.get(), IsVisible())
3699 .WillRepeatedly(Return(true));
3700 wimax_service->SetAutoConnect(true);
3701 wimax_service->SetConnectable(true);
3702 EXPECT_CALL(*wimax_service.get(), technology())
3703 .WillRepeatedly(Return(Technology::kWiMax));
3704 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3705 .WillRepeatedly(Return(false));
3706 manager()->RegisterService(wimax_service);
3707
Paul Stewart39db5ca2013-03-18 14:15:17 -07003708 scoped_refptr<MockService> vpn_service(
3709 new NiceMock<MockService>(control_interface(),
3710 dispatcher(),
3711 metrics(),
3712 manager()));
3713
3714 EXPECT_CALL(*vpn_service.get(), state())
3715 .WillRepeatedly(Return(Service::kStateIdle));
3716 EXPECT_CALL(*vpn_service.get(), IsConnected())
3717 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003718 EXPECT_CALL(*vpn_service.get(), IsVisible())
3719 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003720 wifi_service2->SetAutoConnect(false);
3721 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003722 EXPECT_CALL(*vpn_service.get(), technology())
3723 .WillRepeatedly(Return(Technology::kVPN));
3724 manager()->RegisterService(vpn_service);
3725
3726 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003727 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003728
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003729 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3730 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003731 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003732 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3733 .Times(0); // Explicitly disconnected.
3734 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003735 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003736
3737 manager()->ConnectToBestServices(NULL);
3738 dispatcher()->DispatchPendingEvents();
3739
3740 // After this operation, since the Connect calls above are mocked and
3741 // no actual state changes have occurred, we should expect that the
3742 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003743 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003744}
3745
Christopher Wiley83889862013-05-02 15:55:09 -07003746TEST_F(ManagerTest, VerifyWhenNotConnected) {
3747 const string kFakeCertificate("fake cert");
3748 const string kFakePublicKey("fake public key");
3749 const string kFakeNonce("fake public key");
3750 const string kFakeSignedData("fake signed data");
3751 const string kFakeUdn("fake udn");
3752 const vector<uint8_t> kSSID(10, 87);
3753 const string kConfiguredSSID("AConfiguredDestination");
3754 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3755 kConfiguredSSID.end());
3756 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3757 scoped_refptr<MockWiFiService> mock_destination(
3758 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3759 metrics(), manager(), wifi_provider_,
3760 kSSID, "", "none", false));
3761 // Register this service, but don't mark it as connected.
3762 manager()->RegisterService(mock_destination);
3763 // Verify that if we're not connected to anything, verification fails.
3764 {
3765 LOG(INFO) << "Can't verify if not connected.";
3766 EXPECT_CALL(*crypto_util_proxy_,
3767 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3768 Error error(Error::kOperationInitiated);
3769 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3770 kFakeSignedData, kFakeUdn, "", "",
3771 ResultBoolCallback(), &error);
3772 EXPECT_TRUE(error.IsFailure());
3773 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3774 }
3775 {
3776 // However, if the destination is already configured, we might be
3777 // connected to it via something other than WiFi, and we shouldn't
3778 // enforce the WiFi check.
3779 EXPECT_CALL(*crypto_util_proxy_,
3780 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3781 kFakeSignedData, kFakeUdn,
3782 kConfiguredSSIDVector, kConfiguredBSSID,
3783 _, _)).Times(1).WillOnce(Return(true));
3784 Error error(Error::kOperationInitiated);
3785 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3786 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3787 kConfiguredBSSID, ResultBoolCallback(),
3788 &error);
3789 EXPECT_FALSE(error.IsFailure());
3790 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3791 }
3792}
3793
Christopher Wiley1057cd72013-02-28 15:21:29 -08003794TEST_F(ManagerTest, VerifyDestination) {
3795 const string kFakeCertificate("fake cert");
3796 const string kFakePublicKey("fake public key");
3797 const string kFakeNonce("fake public key");
3798 const string kFakeSignedData("fake signed data");
3799 const string kFakeUdn("fake udn");
3800 const char kSSIDStr[] = "fake ssid";
3801 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003802 const string kConfiguredSSID("AConfiguredDestination");
3803 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3804 kConfiguredSSID.end());
3805 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003806 const string kFakeData("muffin man");
3807 scoped_refptr<MockWiFiService> mock_destination(
3808 new NiceMock<MockWiFiService>(control_interface(),
3809 dispatcher(),
3810 metrics(),
3811 manager(),
3812 wifi_provider_,
3813 kSSID,
3814 "",
3815 "none",
3816 false));
3817 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003818 // Making the service look online will let service lookup in
3819 // VerifyDestinatoin succeed.
3820 EXPECT_CALL(*mock_destination.get(), IsConnected())
3821 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003822 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003823
3824 // Lead off by verifying that the basic VerifyDestination flow works.
3825 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003826 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003827 ResultBoolCallback passed_down_callback;
3828 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3829 kFakePublicKey,
3830 kFakeNonce,
3831 kFakeSignedData,
3832 kFakeUdn,
3833 kSSID,
3834 _,
3835 _,
3836 _))
3837 .Times(1)
3838 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3839 // Ask the manager to verify the current destination. This should look
3840 // up our previously registered service, and pass some metadata about
3841 // that service down to the CryptoUtilProxy to verify.
3842 Error error(Error::kOperationInitiated);
3843 ResultBoolCallback cb = Bind(
3844 &DestinationVerificationTest::ResultBoolCallbackStub,
3845 dv_test.AsWeakPtr());
3846 manager()->VerifyDestination(kFakeCertificate,
3847 kFakePublicKey,
3848 kFakeNonce,
3849 kFakeSignedData,
3850 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003851 // Ask to be verified against that service.
3852 "", "",
3853 cb,
3854 &error);
3855 // We assert here, because if the operation is not ongoing, it is
3856 // inconsistent with shim behavior to call the callback anyway.
3857 ASSERT_TRUE(error.IsOngoing());
3858 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3859 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3860 // Call the callback passed into the CryptoUtilProxy, which
3861 // should find its way into the callback passed into the manager.
3862 // In real code, that callback passed into the manager is from the
3863 // DBus adaptor.
3864 Error e;
3865 passed_down_callback.Run(e, true);
3866 Mock::VerifyAndClearExpectations(&dv_test);
3867 }
3868
Christopher Wiley1057cd72013-02-28 15:21:29 -08003869 // Now for a slightly more complex variant. When we encrypt data,
3870 // we do the same verification step but monkey with the callback to
3871 // link ourselves to an encrypt step afterward.
3872 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003873 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003874 ResultBoolCallback passed_down_callback;
3875 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3876 kFakePublicKey,
3877 kFakeNonce,
3878 kFakeSignedData,
3879 kFakeUdn,
3880 kSSID,
3881 _,
3882 _,
3883 _))
3884 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3885
3886 Error error(Error::kOperationInitiated);
3887 ResultStringCallback cb = Bind(
3888 &DestinationVerificationTest::ResultStringCallbackStub,
3889 dv_test.AsWeakPtr());
3890 manager()->VerifyAndEncryptData(kFakeCertificate,
3891 kFakePublicKey,
3892 kFakeNonce,
3893 kFakeSignedData,
3894 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003895 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003896 kFakeData,
3897 cb,
3898 &error);
3899 ASSERT_TRUE(error.IsOngoing());
3900 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3901 // Now, if we call that passed down callback, we should see encrypt being
3902 // called.
3903 ResultStringCallback second_passed_down_callback;
3904 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3905 kFakeData,
3906 _,
3907 _))
3908 .Times(1)
3909 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3910 Return(true)));
3911 Error e;
3912 passed_down_callback.Run(e, true);
3913 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3914 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3915 // And if we call the second passed down callback, we should see the
3916 // original function we passed down to VerifyDestination getting called.
3917 e.Reset();
3918 second_passed_down_callback.Run(e, "");
3919 Mock::VerifyAndClearExpectations(&dv_test);
3920 }
3921
3922 // If verification fails on the way to trying to encrypt, we should ditch
3923 // without calling encrypt at all.
3924 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003925 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003926 ResultBoolCallback passed_down_callback;
3927 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3928 kFakePublicKey,
3929 kFakeNonce,
3930 kFakeSignedData,
3931 kFakeUdn,
3932 kSSID,
3933 _,
3934 _,
3935 _))
3936 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3937
3938 Error error(Error::kOperationInitiated);
3939 ResultStringCallback cb = Bind(
3940 &DestinationVerificationTest::ResultStringCallbackStub,
3941 dv_test.AsWeakPtr());
3942 manager()->VerifyAndEncryptData(kFakeCertificate,
3943 kFakePublicKey,
3944 kFakeNonce,
3945 kFakeSignedData,
3946 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003947 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003948 kFakeData,
3949 cb,
3950 &error);
3951 ASSERT_TRUE(error.IsOngoing());
3952 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3953 Error e(Error::kOperationFailed);
3954 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3955 // Although we're ditching, this callback is what cleans up the pending
3956 // DBus call.
3957 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3958 passed_down_callback.Run(e, false);
3959 Mock::VerifyAndClearExpectations(&dv_test);
3960 }
3961}
3962
Paul Stewartd2e1c362013-03-03 19:06:07 -08003963TEST_F(ManagerTest, IsProfileBefore) {
3964 scoped_refptr<MockProfile> profile0(
3965 new NiceMock<MockProfile>(
3966 control_interface(), metrics(), manager(), ""));
3967 scoped_refptr<MockProfile> profile1(
3968 new NiceMock<MockProfile>(
3969 control_interface(), metrics(), manager(), ""));
3970
3971 AdoptProfile(manager(), profile0);
3972 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3973 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3974 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3975
3976 // A few abnormal cases, but it's good to track their behavior.
3977 scoped_refptr<MockProfile> profile2(
3978 new NiceMock<MockProfile>(
3979 control_interface(), metrics(), manager(), ""));
3980 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3981 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3982 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3983 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3984}
3985
Paul Stewart967eaeb2013-04-25 19:53:07 -07003986TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
3987 MockStore storage0;
3988 MockStore storage1;
3989 MockStore storage2;
3990
3991 scoped_refptr<MockProfile> profile0(
3992 new NiceMock<MockProfile>(
3993 control_interface(), metrics(), manager(), ""));
3994 scoped_refptr<MockProfile> profile1(
3995 new NiceMock<MockProfile>(
3996 control_interface(), metrics(), manager(), ""));
3997 scoped_refptr<MockProfile> profile2(
3998 new NiceMock<MockProfile>(
3999 control_interface(), metrics(), manager(), ""));
4000
4001 AdoptProfile(manager(), profile0);
4002 AdoptProfile(manager(), profile1);
4003 AdoptProfile(manager(), profile2);
4004
4005 scoped_refptr<MockService> service(
4006 new NiceMock<MockService>(control_interface(),
4007 dispatcher(),
4008 metrics(),
4009 manager()));
4010
4011 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4012 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4013 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4014
4015 const string kEntry0("aluminum_crutch");
4016 const string kEntry2("rehashed_faces");
4017
4018 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4019 .WillOnce(Return(kEntry0));
4020 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4021 .WillOnce(Return(""));
4022 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4023 .WillOnce(Return(kEntry2));
4024
4025 const string kProfileRpc0("service_station");
4026 const string kProfileRpc2("crystal_tiaras");
4027
4028 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4029 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4030 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4031
4032 map<string, string> entries =
4033 manager()->GetLoadableProfileEntriesForService(service);
4034 EXPECT_EQ(2, entries.size());
4035 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4036 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4037 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4038 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4039}
4040
mukesh agrawal00752532013-05-03 15:46:55 -07004041TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004042 // We need a real glib here, so that profiles are persisted.
4043 GLib glib;
4044 ScopedTempDir temp_dir;
4045 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4046 Manager manager(control_interface(),
4047 dispatcher(),
4048 metrics(),
4049 &glib,
4050 run_path(),
4051 storage_path(),
4052 temp_dir.path().value());
4053 // Can't use |wifi_provider_|, because it's owned by the Manager
4054 // object in the fixture.
4055 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4056 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004057 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004058 // Give manager a valid place to write the user profile list.
4059 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4060
4061 // With no user profiles, the WiFiProvider should be called once
4062 // (for the default profile).
4063 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4064 manager.InitializeProfiles();
4065 Mock::VerifyAndClearExpectations(wifi_provider);
4066
4067 // With |n| user profiles, the WiFiProvider should be called |n+1|
4068 // times. First, create 2 user profiles...
4069 const char kProfile0[] = "~user/profile0";
4070 const char kProfile1[] = "~user/profile1";
4071 string profile_rpc_path;
4072 Error error;
4073 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4074 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4075 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4076 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4077
4078 // ... then reset manager state ...
4079 manager.profiles_.clear();
4080
4081 // ...then check that the WiFiProvider is notified about all three
4082 // profiles (one default, two user).
4083 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4084 manager.InitializeProfiles();
4085 Mock::VerifyAndClearExpectations(wifi_provider);
4086}
4087
mukesh agrawal00752532013-05-03 15:46:55 -07004088TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4089 // We need a real glib here, so that profiles are persisted.
4090 GLib glib;
4091 ScopedTempDir temp_dir;
4092 scoped_ptr<Manager> manager;
4093 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4094
4095 // Instantiate a Manager with empty persistent storage. Check that
4096 // defaults are set.
4097 //
4098 // Note that we use the same directory for default and user profiles.
4099 // This doesn't affect the test results, because we don't push a
4100 // user profile.
4101 manager.reset(new Manager(control_interface(),
4102 dispatcher(),
4103 metrics(),
4104 &glib,
4105 run_path(),
4106 temp_dir.path().value(),
4107 temp_dir.path().value()));
4108 manager->InitializeProfiles();
4109 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4110 manager->props_.check_portal_list);
4111 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4112 manager->props_.ignored_dns_search_paths);
4113 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4114 manager->props_.link_monitor_technologies);
4115 EXPECT_EQ(PortalDetector::kDefaultURL,
4116 manager->props_.portal_url);
4117 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4118 manager->props_.portal_check_interval_seconds);
4119
4120 // Change one of the settings.
4121 static const string kCustomCheckPortalList = "fiber0";
4122 Error error;
4123 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4124 manager->profiles_[0]->Save();
4125
4126 // Instantiate a new manager. It should have our settings for
4127 // check_portal_list, rather than the default.
4128 manager.reset(new Manager(control_interface(),
4129 dispatcher(),
4130 metrics(),
4131 &glib,
4132 run_path(),
4133 temp_dir.path().value(),
4134 temp_dir.path().value()));
4135 manager->InitializeProfiles();
4136 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4137
4138 // If we clear the persistent storage, we again get the default value.
4139 ASSERT_TRUE(temp_dir.Delete());
4140 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4141 manager.reset(new Manager(control_interface(),
4142 dispatcher(),
4143 metrics(),
4144 &glib,
4145 run_path(),
4146 temp_dir.path().value(),
4147 temp_dir.path().value()));
4148 manager->InitializeProfiles();
4149 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4150 manager->props_.check_portal_list);
4151}
4152
mukesh agrawalb94adde2013-08-22 18:17:26 -07004153TEST_F(ManagerTest, ProfileStackChangeLogging) {
4154 // We use a real glib here, since Manager and Profile don't provide an
4155 // easy way to mock out KeyFileStore.
4156 GLib glib;
4157 ScopedTempDir temp_dir;
4158 scoped_ptr<Manager> manager;
4159 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4160 manager.reset(new Manager(control_interface(),
4161 dispatcher(),
4162 metrics(),
4163 &glib,
4164 run_path(),
4165 temp_dir.path().value(),
4166 temp_dir.path().value()));
4167
4168 ScopedMockLog log;
4169 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4170 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4171 manager->InitializeProfiles();
4172
4173 const char kProfile0[] = "~user/profile0";
4174 const char kProfile1[] = "~user/profile1";
4175 const char kProfile2[] = "~user/profile2";
4176 TestCreateProfile(manager.get(), kProfile0);
4177 TestCreateProfile(manager.get(), kProfile1);
4178 TestCreateProfile(manager.get(), kProfile2);
4179
4180 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4181 TestPushProfile(manager.get(), kProfile0);
4182
4183 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4184 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4185
4186 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4187 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4188
4189 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4190 TestPopProfile(manager.get(), kProfile2);
4191
4192 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4193 TestPopAnyProfile(manager.get());
4194
4195 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4196 TestPopAllUserProfiles(manager.get());
4197}
4198
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004199// Custom property setters should return false, and make no changes, if
4200// the new value is the same as the old value.
4201TEST_F(ManagerTest, CustomSetterNoopChange) {
4202 // SetCheckPortalList
4203 {
4204 static const string kCheckPortalList = "weird-device,weirder-device";
4205 Error error;
4206 // Set to known value.
4207 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4208 EXPECT_TRUE(error.IsSuccess());
4209 // Set to same value.
4210 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4211 EXPECT_TRUE(error.IsSuccess());
4212 }
4213
4214 // SetIgnoredDNSSearchPaths
4215 {
4216 NiceMock<MockResolver> resolver;
4217 static const string kIgnoredPaths = "example.com,example.org";
4218 Error error;
4219 SetResolver(&resolver);
4220 // Set to known value.
4221 EXPECT_CALL(resolver, set_ignored_search_list(_));
4222 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4223 EXPECT_TRUE(error.IsSuccess());
4224 Mock::VerifyAndClearExpectations(&resolver);
4225 // Set to same value.
4226 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4227 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4228 EXPECT_TRUE(error.IsSuccess());
4229 Mock::VerifyAndClearExpectations(&resolver);
4230 }
4231}
4232
Paul Stewart7de7e022013-08-28 09:42:50 -07004233TEST_F(ManagerTest, GeoLocation) {
4234 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4235
4236 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4237 dispatcher(),
4238 metrics(),
4239 manager(),
4240 "null",
4241 "addr",
4242 0));
4243
4244 // Manager should ignore gelocation info from technologies it does not know.
4245 EXPECT_CALL(*device, technology())
4246 .Times(AtLeast(1))
4247 .WillRepeatedly(Return(Technology::kEthernet));
4248 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4249 manager()->OnDeviceGeolocationInfoUpdated(device);
4250 Mock::VerifyAndClearExpectations(device);
4251 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4252
4253 // Manager should add WiFi geolocation info.
4254 EXPECT_CALL(*device, technology())
4255 .Times(AtLeast(1))
4256 .WillRepeatedly(Return(Technology::kWifi));
4257 EXPECT_CALL(*device, GetGeolocationObjects())
4258 .WillOnce(Return(vector<GeolocationInfo>()));
4259 manager()->OnDeviceGeolocationInfoUpdated(device);
4260 Mock::VerifyAndClearExpectations(device);
4261 auto location_infos = manager()->GetNetworksForGeolocation();
4262 EXPECT_EQ(1, location_infos.size());
4263 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4264
4265 // Manager should inclusively add cellular info.
4266 EXPECT_CALL(*device, technology())
4267 .Times(AtLeast(1))
4268 .WillRepeatedly(Return(Technology::kCellular));
4269 EXPECT_CALL(*device, GetGeolocationObjects())
4270 .WillOnce(Return(vector<GeolocationInfo>()));
4271 manager()->OnDeviceGeolocationInfoUpdated(device);
4272 location_infos = manager()->GetNetworksForGeolocation();
4273 EXPECT_EQ(2, location_infos.size());
4274 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4275 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4276}
4277
Peter Qiu574996a2014-04-04 10:55:47 -07004278TEST_F(ManagerTest, IsWifiIdle) {
4279 // No registered service.
4280 EXPECT_FALSE(manager()->IsWifiIdle());
4281
4282 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4283 dispatcher(),
4284 metrics(),
4285 manager()));
4286
4287 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4288 dispatcher(),
4289 metrics(),
4290 manager()));
4291
4292 manager()->RegisterService(wifi_service);
4293 manager()->RegisterService(cell_service);
4294
4295 EXPECT_CALL(*wifi_service.get(), technology())
4296 .WillRepeatedly(Return(Technology::kWifi));
4297 EXPECT_CALL(*cell_service.get(), technology())
4298 .WillRepeatedly(Return(Technology::kCellular));
4299
4300 // Cellular is connected.
4301 EXPECT_CALL(*cell_service.get(), IsConnected())
4302 .WillRepeatedly(Return(true));
4303 manager()->UpdateService(cell_service);
4304
4305 // No wifi connection attempt.
4306 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4307 .WillRepeatedly(Return(false));
4308 EXPECT_CALL(*wifi_service.get(), IsConnected())
4309 .WillRepeatedly(Return(false));
4310 manager()->UpdateService(wifi_service);
4311 EXPECT_TRUE(manager()->IsWifiIdle());
4312
4313 // Attempt wifi connection.
4314 Mock::VerifyAndClearExpectations(wifi_service);
4315 EXPECT_CALL(*wifi_service.get(), technology())
4316 .WillRepeatedly(Return(Technology::kWifi));
4317 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4318 .WillRepeatedly(Return(true));
4319 EXPECT_CALL(*wifi_service.get(), IsConnected())
4320 .WillRepeatedly(Return(false));
4321 manager()->UpdateService(wifi_service);
4322 EXPECT_FALSE(manager()->IsWifiIdle());
4323
4324 // wifi connected.
4325 Mock::VerifyAndClearExpectations(wifi_service);
4326 EXPECT_CALL(*wifi_service.get(), technology())
4327 .WillRepeatedly(Return(Technology::kWifi));
4328 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4329 .WillRepeatedly(Return(false));
4330 EXPECT_CALL(*wifi_service.get(), IsConnected())
4331 .WillRepeatedly(Return(true));
4332 manager()->UpdateService(wifi_service);
4333 EXPECT_FALSE(manager()->IsWifiIdle());
4334}
4335
Chris Masone9be4a9d2011-05-16 15:44:09 -07004336} // namespace shill