blob: 1d36318b1a4b98313482aa4187b3b3558af6bb36 [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>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070015#include <base/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"
Paul Stewart4d5efb72012-09-17 12:24:34 -070041#include "shill/mock_resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070042#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070043#include "shill/mock_store.h"
Paul Stewart3c504012013-01-17 17:49:58 -080044#include "shill/mock_wifi_provider.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070045#include "shill/mock_wifi_service.h"
mukesh agrawal00752532013-05-03 15:46:55 -070046#include "shill/portal_detector.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020048#include "shill/proxy_factory.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;
Paul Stewartce4ec192012-03-14 12:53:46 -070076using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070077using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080078using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080079using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080080using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070081using ::testing::Test;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070082using ::testing::WithArg;
Chris Masone9be4a9d2011-05-16 15:44:09 -070083
Chris Masone3bd3c8c2011-06-13 08:20:26 -070084class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070085 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070086 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020087 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080088 device_info_(new NiceMock<MockDeviceInfo>(
89 control_interface(),
90 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080091 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080092 reinterpret_cast<Manager*>(NULL))),
Paul Stewart3c504012013-01-17 17:49:58 -080093 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070094 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080095 wifi_provider_(new NiceMock<MockWiFiProvider>()),
96 crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(),
97 glib())) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070098 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
99 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800100 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700101 manager(),
102 "null0",
103 "addr0",
104 0));
105 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
106 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700108 manager(),
109 "null1",
110 "addr1",
111 1));
112 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
113 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800114 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700115 manager(),
116 "null2",
117 "addr2",
118 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800119 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
120 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800121 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800122 manager(),
123 "null3",
124 "addr3",
125 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700126 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800127 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800128
129 // Replace the manager's adaptor with a quieter one, and one
130 // we can do EXPECT*() against. Passes ownership.
131 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800132
Paul Stewart35eff132013-04-12 12:08:40 -0700133 // Replace the manager's Ethernet EAP provider with our mock.
134 // Passes ownership.
135 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
136
Paul Stewart3c504012013-01-17 17:49:58 -0800137 // Replace the manager's WiFi provider with our mock. Passes
138 // ownership.
139 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800140
Paul Stewartb87d22b2013-07-29 11:11:37 -0700141 // Update the manager's map from technology to provider.
142 manager()->UpdateProviderMapping();
143
Christopher Wiley1057cd72013-02-28 15:21:29 -0800144 // Replace the manager's crypto util proxy with our mock. Passes
145 // ownership.
146 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Paul Stewart9dd253e2013-04-22 08:32:59 -0700147
148 // Reset service serial number so service sorting by unique_name()
149 // (and by extension, sorting by order of creation) is predictable.
150 Service::serial_number_ = 10000;
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 Petkovca621542012-07-25 14:25:56 +0200317 class TestProxyFactory : public ProxyFactory {
318 public:
319 TestProxyFactory() {}
320
321 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
322 PowerManagerProxyDelegate */*delegate*/) {
323 return NULL;
324 }
325
326 private:
327 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
328 };
329
Darin Petkov3ec55342012-09-28 14:04:44 +0200330 class TerminationActionTest :
331 public base::SupportsWeakPtr<TerminationActionTest> {
332 public:
333 static const char kActionName[];
334
335 TerminationActionTest() : manager_(NULL) {}
336 virtual ~TerminationActionTest() {}
337
338 MOCK_METHOD1(Done, void(const Error &error));
339
340 void Action() {
341 manager_->TerminationActionComplete("action");
342 }
343
344 void set_manager(Manager *manager) { manager_ = manager; }
345
346 private:
347 Manager *manager_;
348 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
349 };
350
Christopher Wiley1057cd72013-02-28 15:21:29 -0800351 class DestinationVerificationTest :
352 public base::SupportsWeakPtr<DestinationVerificationTest> {
353 public:
354 DestinationVerificationTest() {}
355 virtual ~DestinationVerificationTest() {}
356
357 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
358 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
359 const string &value));
360 private:
361 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
362 };
363
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700364 class DisableTechnologyReplyHandler :
365 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
366 public:
367 DisableTechnologyReplyHandler() {}
368 virtual ~DisableTechnologyReplyHandler() {}
369
370 MOCK_METHOD1(ReportResult, void(const Error &));
371
372 private:
373 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
374 };
375
Darin Petkovca621542012-07-25 14:25:56 +0200376 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
377 power_manager_->power_state_ = state;
378 }
379
380 void SetPowerManager() {
381 manager()->set_power_manager(power_manager_.release());
382 }
383
Darin Petkov3ec55342012-09-28 14:04:44 +0200384 HookTable *GetTerminationActions() {
385 return &manager()->termination_actions_;
386 }
387
Darin Petkovca621542012-07-25 14:25:56 +0200388 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
389 manager()->OnPowerStateChanged(state);
390 }
391
Daniel Erat0818cca2012-12-14 10:16:21 -0800392 void OnSuspendImminent(int suspend_id) {
393 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200394 }
395
Daniel Erat0818cca2012-12-14 10:16:21 -0800396 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
397 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200398 }
399
Paul Stewartbfb82552012-10-24 16:48:48 -0700400 vector<string> EnumerateAvailableServices() {
401 return manager()->EnumerateAvailableServices(NULL);
402 }
403
404 vector<string> EnumerateWatchedServices() {
405 return manager()->EnumerateWatchedServices(NULL);
406 }
407
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000408 MockServiceRefPtr MakeAutoConnectableService() {
409 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
410 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800411 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000412 manager());
413 service->MakeFavorite();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700414 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000415 return service;
416 }
417
Paul Stewart35eff132013-04-12 12:08:40 -0700418 void SetEapProviderService(const ServiceRefPtr &service) {
419 ethernet_eap_provider_->set_service(service);
420 }
421
Darin Petkovca621542012-07-25 14:25:56 +0200422 TestProxyFactory proxy_factory_;
423 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700424 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800425 scoped_ptr<MockDeviceInfo> device_info_;
426
Paul Stewartd2e1c362013-03-03 19:06:07 -0800427 // This service is held for the manager, and given ownership in a mock
428 // function. This ensures that when the Manager takes ownership, there
429 // is only one reference left.
430 scoped_refptr<MockWiFiService> temp_mock_service_;
431
Paul Stewart3c504012013-01-17 17:49:58 -0800432 // These pointers are owned by the manager, and only tracked here for
433 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800434 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700435 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800436 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800437 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700438};
439
Darin Petkov3ec55342012-09-28 14:04:44 +0200440const char ManagerTest::TerminationActionTest::kActionName[] = "action";
441
Paul Stewart22aa71b2011-09-16 12:15:11 -0700442bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700443 if (!manager()->sort_services_task_.IsCancelled()) {
444 manager()->SortServicesTask();
445 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700446 return (svc0.get() == manager()->services_[0].get() &&
447 svc1.get() == manager()->services_[1].get());
448}
449
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700450void SetErrorPermissionDenied(Error *error) {
451 error->Populate(Error::kPermissionDenied);
452}
453
Arman Uguray2f352e62013-08-28 19:12:53 -0700454void SetErrorSuccess(Error *error) {
455 error->Reset();
456}
457
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700458TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700459 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700460 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700461}
462
Chris Masone9be4a9d2011-05-16 15:44:09 -0700463TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700464 ON_CALL(*mock_devices_[0].get(), technology())
465 .WillByDefault(Return(Technology::kEthernet));
466 ON_CALL(*mock_devices_[1].get(), technology())
467 .WillByDefault(Return(Technology::kWifi));
468 ON_CALL(*mock_devices_[2].get(), technology())
469 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700470
Paul Stewart22aa71b2011-09-16 12:15:11 -0700471 manager()->RegisterDevice(mock_devices_[0]);
472 manager()->RegisterDevice(mock_devices_[1]);
473 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700474
Paul Stewart22aa71b2011-09-16 12:15:11 -0700475 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
476 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
477 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700478}
479
Paul Stewarta41e38d2011-11-11 07:47:29 -0800480TEST_F(ManagerTest, DeviceRegistrationAndStart) {
481 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500482 mock_devices_[0]->enabled_persistent_ = true;
483 mock_devices_[1]->enabled_persistent_ = false;
484 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800485 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500486 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800487 .Times(0);
488 manager()->RegisterDevice(mock_devices_[0]);
489 manager()->RegisterDevice(mock_devices_[1]);
490}
491
492TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800493 MockProfile *profile =
494 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800495 DeviceRefPtr device_ref(mock_devices_[0].get());
496 AdoptProfile(manager(), profile); // Passes ownership.
497 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200498 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800499 manager()->RegisterDevice(mock_devices_[0]);
500}
501
Chris Masone9be4a9d2011-05-16 15:44:09 -0700502TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700503 ON_CALL(*mock_devices_[0].get(), technology())
504 .WillByDefault(Return(Technology::kEthernet));
505 ON_CALL(*mock_devices_[1].get(), technology())
506 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700507
Gaurav Shah435de2c2011-11-17 19:01:07 -0800508 manager()->RegisterDevice(mock_devices_[0]);
509 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700510
Paul Stewart22aa71b2011-09-16 12:15:11 -0700511 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
512 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700513
Thieu Le5133b712013-02-19 14:47:21 -0800514 MockProfile *profile =
515 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700516 AdoptProfile(manager(), profile); // Passes ownership.
517
Eric Shienbrood9a245532012-03-07 14:20:39 -0500518 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700519 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800520 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700521 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700522
Eric Shienbrood9a245532012-03-07 14:20:39 -0500523 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700524 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800525 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700526 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700527}
528
529TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700530 // It's much easier and safer to use a real GLib for this test.
531 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700532 Manager manager(control_interface(),
533 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800534 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700535 &glib,
536 run_path(),
537 storage_path(),
538 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700539 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
540 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700541 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700542
Chris Masone9be4a9d2011-05-16 15:44:09 -0700543 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700544 new NiceMock<MockService>(control_interface(),
545 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800546 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700547 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700548 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700549 new NiceMock<MockService>(control_interface(),
550 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800551 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700552 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700553
Darin Petkov457728b2013-01-09 09:49:08 +0100554 string service1_name(mock_service->unique_name());
555 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700556
557 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
558 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700559 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700560 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700561 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700562 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700563 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700564
Chris Masone9d779932011-08-25 16:33:41 -0700565 manager.RegisterService(mock_service);
566 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700567
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800568 Error error;
569 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700570 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700571 EXPECT_EQ(2, ids.size());
572 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
573 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700574
Chris Masone9d779932011-08-25 16:33:41 -0700575 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
576 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
577
578 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700579}
580
Chris Masone6515aab2011-10-12 16:19:09 -0700581TEST_F(ManagerTest, RegisterKnownService) {
582 // It's much easier and safer to use a real GLib for this test.
583 GLib glib;
584 Manager manager(control_interface(),
585 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800586 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700587 &glib,
588 run_path(),
589 storage_path(),
590 string());
591 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
592 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700593 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700594 {
595 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
596 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800597 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700598 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700599 ASSERT_TRUE(profile->AdoptService(service1));
600 ASSERT_TRUE(profile->ContainsService(service1));
601 } // Force destruction of service1.
602
603 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
604 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800605 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700606 &manager));
607 manager.RegisterService(service2);
608 EXPECT_EQ(service2->profile().get(), profile.get());
609 manager.Stop();
610}
611
612TEST_F(ManagerTest, RegisterUnknownService) {
613 // It's much easier and safer to use a real GLib for this test.
614 GLib glib;
615 Manager manager(control_interface(),
616 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800617 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700618 &glib,
619 run_path(),
620 storage_path(),
621 string());
622 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
623 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700624 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700625 {
626 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
627 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800628 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700629 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700630 ASSERT_TRUE(profile->AdoptService(service1));
631 ASSERT_TRUE(profile->ContainsService(service1));
632 } // Force destruction of service1.
633 scoped_refptr<MockService> mock_service2(
634 new NiceMock<MockService>(control_interface(),
635 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800636 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700637 &manager));
638 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100639 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700640 manager.RegisterService(mock_service2);
641 EXPECT_NE(mock_service2->profile().get(), profile.get());
642 manager.Stop();
643}
644
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000645TEST_F(ManagerTest, DeregisterUnregisteredService) {
646 // WiFi assumes that it can deregister a service that is not
647 // registered. (E.g. a hidden service can be deregistered when it
648 // loses its last endpoint, and again when WiFi is Stop()-ed.)
649 //
650 // So test that doing so doesn't cause a crash.
651 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
652 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800653 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000654 manager());
655 manager()->DeregisterService(service);
656}
657
Chris Masonea8a2c252011-06-27 22:16:30 -0700658TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700659 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700660 map<string, ::DBus::Variant> props;
661 Error error(Error::kInvalidProperty, "");
662 {
663 ::DBus::Error dbus_error;
664 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700665 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700666 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700667 expected,
668 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700669 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700670 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
671 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700672 expected);
673 }
674 {
675 ::DBus::Error dbus_error;
676 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700677 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700678 expected,
679 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700680 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700681 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
682 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700683 expected);
684 }
685}
686
Chris Masone3c3f6a12011-07-01 10:01:41 -0700687TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700688 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800689 manager()->RegisterDevice(mock_devices_[0]);
690 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700691 {
692 map<string, ::DBus::Variant> props;
693 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700694 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700695 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700696 vector < ::DBus::Path> devices =
Ben Chan923a5022013-09-20 11:23:23 -0700697 props[kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700698 EXPECT_EQ(2, devices.size());
699 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700700}
701
mukesh agrawal2366eed2012-03-20 18:21:50 -0700702TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700703 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700704 map<string, ::DBus::Variant> props;
705 ::DBus::Error dbus_error;
706 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
707 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700708 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700709 ASSERT_FALSE(prop == props.end());
710 const ::DBus::Variant &variant = prop->second;
711 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
712}
713
Chris Masone6791a432011-07-12 13:23:19 -0700714TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700715 Manager manager(control_interface(),
716 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800717 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700718 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700719 run_path(),
720 storage_path(),
721 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700722 scoped_refptr<MockService> s2(new MockService(control_interface(),
723 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800724 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700725 &manager));
726 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700727 {
Chris Masone6515aab2011-10-12 16:19:09 -0700728 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700729 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800730 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700731 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700732 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700733 .WillRepeatedly(Return(true));
734 EXPECT_CALL(*storage, Flush())
735 .Times(AnyNumber())
736 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700737 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700738 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700739 }
Chris Masone6515aab2011-10-12 16:19:09 -0700740 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800741 ProfileRefPtr profile(
742 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700743 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700744
Chris Masone6515aab2011-10-12 16:19:09 -0700745 // Now, move the Service |s2| to another profile.
746 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
747 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700748
749 // Force destruction of the original Profile, to ensure that the Service
750 // is kept alive and populated with data.
751 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700752 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700753 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700754}
755
Paul Stewart7f61e522012-03-22 11:13:45 -0700756TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
757 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800758 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700759 const string kProfileName("profile0");
760 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
761 .WillRepeatedly(Return(kProfileName));
762 AdoptProfile(manager(), mock_profile);
763
764 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
765 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
766 EXPECT_EQ(mock_profile.get(), profile.get());
767}
768
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800769TEST_F(ManagerTest, SetProfileForService) {
770 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800771 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800772 string profile_name0("profile0");
773 EXPECT_CALL(*profile0, GetRpcIdentifier())
774 .WillRepeatedly(Return(profile_name0));
775 AdoptProfile(manager(), profile0);
776 scoped_refptr<MockService> service(new MockService(control_interface(),
777 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800778 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800779 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700780 EXPECT_FALSE(manager()->HasService(service));
781 {
782 Error error;
783 EXPECT_CALL(*profile0, AdoptService(_))
784 .WillOnce(Return(true));
785 // Expect that setting the profile of a service that does not already
786 // have one assigned does not cause a crash.
787 manager()->SetProfileForService(service, "profile0", &error);
788 EXPECT_TRUE(error.IsSuccess());
789 }
790
791 // The service should be registered as a side-effect of the profile being
792 // set for this service.
793 EXPECT_TRUE(manager()->HasService(service));
794
795 // Since we have mocked Profile::AdoptServie() above, the service's
796 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800797 service->set_profile(profile0);
798
799 {
800 Error error;
801 manager()->SetProfileForService(service, "foo", &error);
802 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700803 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800804 }
805
806 {
807 Error error;
808 manager()->SetProfileForService(service, profile_name0, &error);
809 EXPECT_EQ(Error::kInvalidArguments, error.type());
810 EXPECT_EQ("Service is already connected to this profile", error.message());
811 }
812
813 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800814 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800815 string profile_name1("profile1");
816 EXPECT_CALL(*profile1, GetRpcIdentifier())
817 .WillRepeatedly(Return(profile_name1));
818 AdoptProfile(manager(), profile1);
819
820 {
821 Error error;
822 EXPECT_CALL(*profile1, AdoptService(_))
823 .WillOnce(Return(true));
824 EXPECT_CALL(*profile0, AbandonService(_))
825 .WillOnce(Return(true));
826 manager()->SetProfileForService(service, profile_name1, &error);
827 EXPECT_TRUE(error.IsSuccess());
828 }
829}
830
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700831TEST_F(ManagerTest, CreateProfile) {
832 // It's much easier to use real Glib here since we want the storage
833 // side-effects.
834 GLib glib;
835 ScopedTempDir temp_dir;
836 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
837
838 Manager manager(control_interface(),
839 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800840 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700841 &glib,
842 run_path(),
843 storage_path(),
844 temp_dir.path().value());
845
846 // Invalid name should be rejected.
847 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
848
Paul Stewartd0a3b812012-03-28 22:48:22 -0700849 // A profile with invalid characters in it should similarly be rejected.
850 EXPECT_EQ(Error::kInvalidArguments,
851 TestCreateProfile(&manager, "valid_profile"));
852
853 // We should be able to create a machine profile.
854 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700855
Gary Morainb672d352012-04-25 09:19:06 -0700856 // We should succeed in creating a valid user profile. Verify the returned
857 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700858 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700859 {
860 Error error;
861 string path;
862 manager.CreateProfile(kProfile, &path, &error);
863 EXPECT_EQ(Error::kSuccess, error.type());
864 EXPECT_EQ("/profile_rpc", path);
865 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700866
867 // We should fail in creating it a second time (already exists).
868 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
869}
870
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700871// We receive PopProfile when a user logs out, and it should always trigger a
872// MemoryLog Clear() call.
873TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
874 GLib glib;
875 ScopedTempDir temp_dir;
876 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
877 Manager manager(control_interface(),
878 dispatcher(),
879 metrics(),
880 &glib,
881 run_path(),
882 storage_path(),
883 temp_dir.path().value());
884 const char kProfile0[] = "~user/profile0";
885 const char kPurgedMessage[] = "This message should be purged";
886 // Create a profile and push it on the stack, leave one uncreated
887 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
888 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
889
890 // Popping a profile which isn't on top should still clear the log.
891 LOG(INFO) << kPurgedMessage;
892 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
893 kPurgedMessage));
894 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
895 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
896 kPurgedMessage));
897
898 // Popping an invalid profile name should do the same thing.
899 LOG(INFO) << kPurgedMessage;
900 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
901 kPurgedMessage));
902 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
903 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
904 kPurgedMessage));
905
906 // Successful pops also purge the message log.
907 LOG(INFO) << kPurgedMessage;
908 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
909 kPurgedMessage));
910 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
911 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
912 kPurgedMessage));
913}
914
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700915TEST_F(ManagerTest, PushPopProfile) {
916 // It's much easier to use real Glib in creating a Manager for this
917 // test here since we want the storage side-effects.
918 GLib glib;
919 ScopedTempDir temp_dir;
920 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
921 Manager manager(control_interface(),
922 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800923 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700924 &glib,
925 run_path(),
926 storage_path(),
927 temp_dir.path().value());
928
929 // Pushing an invalid profile should fail.
930 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
931
Paul Stewartd0a3b812012-03-28 22:48:22 -0700932 // Pushing a default profile that does not exist should fail.
933 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700934
935 const char kProfile0[] = "~user/profile0";
936 const char kProfile1[] = "~user/profile1";
937
938 // Create a couple of profiles.
939 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
940 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
941
942 // Push these profiles on the stack.
943 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
944 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
945
946 // Pushing a profile a second time should fail.
947 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
948 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
949
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800950 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700951 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800952 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700953
954 // Make sure a profile name that doesn't exist fails.
955 const char kProfile2Id[] = "profile2";
956 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
957 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
958
959 // Create a new service, with a specific storage name.
960 scoped_refptr<MockService> service(
961 new NiceMock<MockService>(control_interface(),
962 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800963 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700964 &manager));
965 const char kServiceName[] = "service_storage_name";
966 EXPECT_CALL(*service.get(), GetStorageIdentifier())
967 .WillRepeatedly(Return(kServiceName));
968 EXPECT_CALL(*service.get(), Load(_))
969 .WillRepeatedly(Return(true));
970
971 // Add this service to the manager -- it should end up in the ephemeral
972 // profile.
973 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800974 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700975
976 // Create storage for a profile that contains the service storage name.
977 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
978 kServiceName));
979
980 // When we push the profile, the service should move away from the
981 // ephemeral profile to this new profile since it has an entry for
982 // this service.
983 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800984 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700985 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
986
987 // Insert another profile that should supersede ownership of the service.
988 const char kProfile3Id[] = "profile3";
989 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
990 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
991 kServiceName));
992 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
993 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
994
995 // Popping an invalid profile name should fail.
996 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
997
998 // Popping an profile that is not at the top of the stack should fail.
999 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
1000
1001 // Popping the top profile should succeed.
1002 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
1003
1004 // Moreover the service should have switched profiles to profile 2.
1005 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
1006
1007 // Popping the top profile should succeed.
1008 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1009
1010 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -08001011 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001012
1013 // Pop the remaining two services off the stack.
1014 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1015 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1016
1017 // Next pop should fail with "stack is empty".
1018 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -07001019
1020 const char kMachineProfile0[] = "machineprofile0";
1021 const char kMachineProfile1[] = "machineprofile1";
1022 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1023 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1024
1025 // Should be able to push a machine profile.
1026 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1027
1028 // Should be able to push a user profile atop a machine profile.
1029 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1030
1031 // Pushing a system-wide profile on top of a user profile should fail.
1032 EXPECT_EQ(Error::kInvalidArguments,
1033 TestPushProfile(&manager, kMachineProfile1));
1034
1035 // However if we pop the user profile, we should be able stack another
1036 // machine profile on.
1037 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1038 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001039
1040 // Add two user profiles to the top of the stack.
1041 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1042 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
1043 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
1044 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));
1088 ASSERT_TRUE(file_util::PathExists(profile_path));
1089
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.
1100 EXPECT_TRUE(file_util::PathExists(profile_path));
1101
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.
1112 EXPECT_FALSE(file_util::PathExists(profile_path));
1113
1114 // Another remove succeeds, due to a foible in file_util::Delete --
1115 // 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.
1124 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
1125 {
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));
1202 ASSERT_TRUE(file_util::PathExists(profile_path));
1203 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.
1207 ASSERT_TRUE(file_util::Delete(profile_path, false));
1208 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
Darin Petkovc63dcf02012-05-24 11:51:43 +02001662TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1663 KeyValueStore args;
1664 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001665 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001666 ServiceRefPtr service = manager()->GetService(args, &e);
1667 EXPECT_EQ(Error::kInvalidArguments, e.type());
1668 EXPECT_EQ("Missing WiMAX network id.", e.message());
1669 EXPECT_FALSE(service);
1670}
1671
Darin Petkovd1cd7972012-05-22 15:26:15 +02001672TEST_F(ManagerTest, GetServiceWiMax) {
1673 KeyValueStore args;
1674 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001675 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001676 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001677 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001678 ServiceRefPtr service = manager()->GetService(args, &e);
1679 EXPECT_TRUE(e.IsSuccess());
1680 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001681}
1682
Paul Stewart7f61e522012-03-22 11:13:45 -07001683TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1684 // Manager calls ActiveProfile() so we need at least one profile installed.
1685 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001686 new NiceMock<MockProfile>(
1687 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001688 AdoptProfile(manager(), profile);
1689
1690 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001691 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001692 Error error;
1693 manager()->ConfigureService(args, &error);
1694 EXPECT_EQ(Error::kInvalidArguments, error.type());
1695 EXPECT_EQ("Invalid profile name xxx", error.message());
1696}
1697
1698TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1699 // Manager calls ActiveProfile() so we need at least one profile installed.
1700 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001701 new NiceMock<MockProfile>(
1702 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001703 AdoptProfile(manager(), profile);
1704
1705 KeyValueStore args;
1706 Error error;
1707 manager()->ConfigureService(args, &error);
1708 EXPECT_EQ(Error::kInvalidArguments, error.type());
1709 EXPECT_EQ("must specify service type", error.message());
1710}
1711
1712// A registered service in the ephemeral profile should be moved to the
1713// active profile as a part of configuration if no profile was explicitly
1714// specified.
1715TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1716 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001717 new NiceMock<MockProfile>(
1718 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001719
1720 AdoptProfile(manager(), profile); // This is now the active profile.
1721
Paul Stewartd2e1c362013-03-03 19:06:07 -08001722 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001723 scoped_refptr<MockWiFiService> service(
1724 new NiceMock<MockWiFiService>(control_interface(),
1725 dispatcher(),
1726 metrics(),
1727 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001728 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001729 ssid,
1730 "",
1731 "",
1732 false));
1733
1734 manager()->RegisterService(service);
1735 service->set_profile(GetEphemeralProfile(manager()));
1736
Paul Stewart3c504012013-01-17 17:49:58 -08001737 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001738 .WillOnce(Return(service));
1739 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1740 .WillOnce(Return(true));
1741 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1742 .WillOnce(Return(true));
1743
1744 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001745 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001746 Error error;
1747 manager()->ConfigureService(args, &error);
1748 EXPECT_TRUE(error.IsSuccess());
1749}
1750
Paul Stewart2c575d22012-12-07 12:28:57 -08001751// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001752// specify a profile, it should be moved from the profile it was previously
1753// in to the specified profile if one was requested.
1754TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1755 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001756 new NiceMock<MockProfile>(
1757 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001758 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001759 new NiceMock<MockProfile>(
1760 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001761
1762 const string kProfileName0 = "profile0";
1763 const string kProfileName1 = "profile1";
1764
1765 EXPECT_CALL(*profile0, GetRpcIdentifier())
1766 .WillRepeatedly(Return(kProfileName0));
1767 EXPECT_CALL(*profile1, GetRpcIdentifier())
1768 .WillRepeatedly(Return(kProfileName1));
1769
1770 AdoptProfile(manager(), profile0);
1771 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1772
Paul Stewartd2e1c362013-03-03 19:06:07 -08001773 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001774 scoped_refptr<MockWiFiService> service(
1775 new NiceMock<MockWiFiService>(control_interface(),
1776 dispatcher(),
1777 metrics(),
1778 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001779 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001780 ssid,
1781 "",
1782 "",
1783 false));
1784
1785 manager()->RegisterService(service);
1786 service->set_profile(profile1);
1787
Paul Stewart3c504012013-01-17 17:49:58 -08001788 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001789 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001790 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1791 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001792 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1793 .WillOnce(Return(true));
1794 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1795 .WillOnce(Return(true));
1796 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1797 .WillOnce(Return(true));
1798
1799 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001800 args.SetString(kTypeProperty, kTypeWifi);
1801 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001802 Error error;
1803 manager()->ConfigureService(args, &error);
1804 EXPECT_TRUE(error.IsSuccess());
1805 service->set_profile(NULL); // Breaks refcounting loop.
1806}
1807
Paul Stewart2c575d22012-12-07 12:28:57 -08001808// If we configure a service that is already a member of the specified
1809// profile, the Manager should not call LoadService or AdoptService again
1810// on this service.
1811TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1812 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001813 new NiceMock<MockProfile>(
1814 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001815
1816 const string kProfileName0 = "profile0";
1817
1818 EXPECT_CALL(*profile0, GetRpcIdentifier())
1819 .WillRepeatedly(Return(kProfileName0));
1820
1821 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1822
Paul Stewartd2e1c362013-03-03 19:06:07 -08001823 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001824 scoped_refptr<MockWiFiService> service(
1825 new NiceMock<MockWiFiService>(control_interface(),
1826 dispatcher(),
1827 metrics(),
1828 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001829 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001830 ssid,
1831 "",
1832 "",
1833 false));
1834
1835 manager()->RegisterService(service);
1836 service->set_profile(profile0);
1837
Paul Stewart3c504012013-01-17 17:49:58 -08001838 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001839 .WillOnce(Return(service));
1840 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1841 .Times(0);
1842 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1843 .WillOnce(Return(true));
1844 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1845 .Times(0);
1846
1847 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001848 args.SetString(kTypeProperty, kTypeWifi);
1849 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001850 Error error;
1851 manager()->ConfigureService(args, &error);
1852 EXPECT_TRUE(error.IsSuccess());
1853 service->set_profile(NULL); // Breaks refcounting loop.
1854}
1855
Paul Stewart7f61e522012-03-22 11:13:45 -07001856// An unregistered service should remain unregistered, but its contents should
1857// be saved to the specified profile nonetheless.
1858TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1859 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001860 new NiceMock<MockProfile>(
1861 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001862 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001863 new NiceMock<MockProfile>(
1864 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001865
1866 const string kProfileName0 = "profile0";
1867 const string kProfileName1 = "profile1";
1868
1869 EXPECT_CALL(*profile0, GetRpcIdentifier())
1870 .WillRepeatedly(Return(kProfileName0));
1871 EXPECT_CALL(*profile1, GetRpcIdentifier())
1872 .WillRepeatedly(Return(kProfileName1));
1873
1874 AdoptProfile(manager(), profile0);
1875 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1876
Paul Stewartd2e1c362013-03-03 19:06:07 -08001877 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001878 scoped_refptr<MockWiFiService> service(
1879 new NiceMock<MockWiFiService>(control_interface(),
1880 dispatcher(),
1881 metrics(),
1882 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001883 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001884 ssid,
1885 "",
1886 "",
1887 false));
1888
1889 service->set_profile(profile1);
1890
Paul Stewart3c504012013-01-17 17:49:58 -08001891 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001892 .WillOnce(Return(service));
1893 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1894 .WillOnce(Return(true));
1895 EXPECT_CALL(*profile0, AdoptService(_))
1896 .Times(0);
1897 EXPECT_CALL(*profile1, AdoptService(_))
1898 .Times(0);
1899
1900 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001901 args.SetString(kTypeProperty, kTypeWifi);
1902 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001903 Error error;
1904 manager()->ConfigureService(args, &error);
1905 EXPECT_TRUE(error.IsSuccess());
1906}
1907
Paul Stewartd2e1c362013-03-03 19:06:07 -08001908TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1909 KeyValueStore args;
1910 Error error;
1911 ServiceRefPtr service =
1912 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001913 EXPECT_EQ(Error::kInvalidArguments, error.type());
1914 EXPECT_EQ("must specify service type", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001915 EXPECT_EQ(NULL, service.get());
1916}
1917
1918TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1919 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001920 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001921 Error error;
1922 ServiceRefPtr service =
1923 manager()->ConfigureServiceForProfile("", args, &error);
1924 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001925 EXPECT_EQ("service type is unsupported", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001926 EXPECT_EQ(NULL, service.get());
1927}
1928
1929TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1930 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001931 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001932 Error error;
1933 ServiceRefPtr service =
1934 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1935 EXPECT_EQ(Error::kNotFound, error.type());
1936 EXPECT_EQ("Profile specified was not found", error.message());
1937 EXPECT_EQ(NULL, service.get());
1938}
1939
1940TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1941 const string kProfileName0 = "profile0";
1942 const string kProfileName1 = "profile1";
1943 scoped_refptr<MockProfile> profile0(
1944 AddNamedMockProfileToManager(manager(), kProfileName0));
1945
1946 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001947 args.SetString(kTypeProperty, kTypeWifi);
1948 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001949 Error error;
1950 ServiceRefPtr service =
1951 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1952 EXPECT_EQ(Error::kInvalidArguments, error.type());
1953 EXPECT_EQ("Profile argument does not match that in "
1954 "the configuration arguments", error.message());
1955 EXPECT_EQ(NULL, service.get());
1956}
1957
1958TEST_F(ManagerTest,
1959 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1960 const string kProfileName0 = "profile0";
1961 scoped_refptr<MockProfile> profile0(
1962 AddNamedMockProfileToManager(manager(), kProfileName0));
1963 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001964 args.SetString(kTypeProperty, kTypeWifi);
1965 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001966
1967 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1968 .WillOnce(Return(WiFiServiceRefPtr()));
1969 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1970 .WillOnce(Return(WiFiServiceRefPtr()));
1971 Error error;
1972 ServiceRefPtr service =
1973 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1974 // Since we didn't set the error in the GetService expectation above...
1975 EXPECT_TRUE(error.IsSuccess());
1976 EXPECT_EQ(NULL, service.get());
1977}
1978
1979TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1980 const string kProfileName0 = "profile0";
1981 scoped_refptr<MockProfile> profile0(
1982 AddNamedMockProfileToManager(manager(), kProfileName0));
1983
1984 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001985 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001986
1987 scoped_refptr<MockWiFiService> mock_service(
1988 new NiceMock<MockWiFiService>(control_interface(),
1989 dispatcher(),
1990 metrics(),
1991 manager(),
1992 wifi_provider_,
1993 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07001994 kModeManaged,
1995 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08001996 false));
1997 ServiceRefPtr mock_service_generic(mock_service.get());
1998 mock_service->set_profile(profile0);
1999 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2000 .WillOnce(Return(WiFiServiceRefPtr()));
2001 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2002 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2003 .WillOnce(Return(true));
2004 Error error;
2005 ServiceRefPtr service =
2006 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2007 EXPECT_TRUE(error.IsSuccess());
2008 EXPECT_EQ(mock_service.get(), service.get());
2009 mock_service->set_profile(NULL); // Breaks reference cycle.
2010}
2011
2012TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2013 scoped_refptr<MockService> mock_service(
2014 new NiceMock<MockService>(control_interface(),
2015 dispatcher(),
2016 metrics(),
2017 manager()));
2018 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002019 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002020 manager()->RegisterService(mock_service);
2021 ServiceRefPtr mock_service_generic(mock_service.get());
2022
2023 const string kProfileName = "profile";
2024 scoped_refptr<MockProfile> profile(
2025 AddNamedMockProfileToManager(manager(), kProfileName));
2026 mock_service->set_profile(profile);
2027
2028 EXPECT_CALL(*mock_service, technology())
2029 .WillOnce(Return(Technology::kCellular))
2030 .WillOnce(Return(Technology::kWifi));
2031
2032 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2033 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2034 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2035
2036 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002037 args.SetString(kTypeProperty, kTypeWifi);
2038 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002039
2040 // The first attempt should fail because the service reports a technology
2041 // other than "WiFi".
2042 {
2043 Error error;
2044 ServiceRefPtr service =
2045 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2046 EXPECT_EQ(NULL, service.get());
2047 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002048 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002049 }
2050
2051 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2052 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2053
2054 {
2055 Error error;
2056 ServiceRefPtr service =
2057 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2058 EXPECT_TRUE(error.IsSuccess());
2059 EXPECT_EQ(mock_service.get(), service.get());
2060 EXPECT_EQ(profile.get(), service->profile().get());
2061 }
2062 mock_service->set_profile(NULL); // Breaks reference cycle.
2063}
2064
2065TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2066 const string kProfileName = "profile";
2067 scoped_refptr<MockProfile> profile(
2068 AddNamedMockProfileToManager(manager(), kProfileName));
2069
2070 scoped_refptr<MockWiFiService> mock_service(
2071 new NiceMock<MockWiFiService>(control_interface(),
2072 dispatcher(),
2073 metrics(),
2074 manager(),
2075 wifi_provider_,
2076 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002077 kModeManaged,
2078 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002079 false));
2080 mock_service->set_profile(profile);
2081 ServiceRefPtr mock_service_generic(mock_service.get());
2082
2083 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002084 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002085 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2086 .WillOnce(Return(mock_service));
2087 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2088 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2089 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2090 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2091
2092 Error error;
2093 ServiceRefPtr service =
2094 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2095 EXPECT_TRUE(error.IsSuccess());
2096 EXPECT_EQ(mock_service.get(), service.get());
2097 EXPECT_EQ(profile.get(), service->profile().get());
2098 mock_service->set_profile(NULL); // Breaks reference cycle.
2099}
2100
2101TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2102 const string kProfileName = "profile";
2103 scoped_refptr<MockProfile> profile(
2104 AddNamedMockProfileToManager(manager(), kProfileName));
2105
2106 scoped_refptr<MockWiFiService> mock_service(
2107 new NiceMock<MockWiFiService>(control_interface(),
2108 dispatcher(),
2109 metrics(),
2110 manager(),
2111 wifi_provider_,
2112 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002113 kModeManaged,
2114 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002115 false));
2116 mock_service->set_profile(GetEphemeralProfile(manager()));
2117 ServiceRefPtr mock_service_generic(mock_service.get());
2118
2119 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002120 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002121 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2122 .WillOnce(Return(mock_service));
2123 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2124 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2125 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2126
2127 Error error;
2128 ServiceRefPtr service =
2129 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2130 EXPECT_TRUE(error.IsSuccess());
2131 EXPECT_EQ(mock_service.get(), service.get());
2132 EXPECT_EQ(profile.get(), service->profile().get());
2133 mock_service->set_profile(NULL); // Breaks reference cycle.
2134}
2135
2136TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2137 const string kProfileName0 = "profile0";
2138 scoped_refptr<MockProfile> profile0(
2139 AddNamedMockProfileToManager(manager(), kProfileName0));
2140 const string kProfileName1 = "profile1";
2141 scoped_refptr<MockProfile> profile1(
2142 AddNamedMockProfileToManager(manager(), kProfileName1));
2143
2144 scoped_refptr<MockWiFiService> mock_service(
2145 new NiceMock<MockWiFiService>(control_interface(),
2146 dispatcher(),
2147 metrics(),
2148 manager(),
2149 wifi_provider_,
2150 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002151 kModeManaged,
2152 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002153 false));
2154 manager()->RegisterService(mock_service);
2155 mock_service->set_profile(profile0);
2156 ServiceRefPtr mock_service_generic(mock_service.get());
2157
2158 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002159 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002160 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2161 .WillOnce(Return(mock_service));
2162 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2163 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2164 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2165 // This happens once to make the service loadable for the ConfigureService
2166 // below, and a second time after the service is modified.
2167 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2168 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2169 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2170 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2171
2172 Error error;
2173 ServiceRefPtr service =
2174 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2175 EXPECT_TRUE(error.IsSuccess());
2176 EXPECT_EQ(mock_service.get(), service.get());
2177 mock_service->set_profile(NULL); // Breaks reference cycle.
2178}
2179
2180TEST_F(ManagerTest,
2181 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2182 const string kProfileName0 = "profile0";
2183 scoped_refptr<MockProfile> profile0(
2184 AddNamedMockProfileToManager(manager(), kProfileName0));
2185 const string kProfileName1 = "profile1";
2186 scoped_refptr<MockProfile> profile1(
2187 AddNamedMockProfileToManager(manager(), kProfileName1));
2188
2189 scoped_refptr<MockWiFiService> matching_service(
2190 new StrictMock<MockWiFiService>(control_interface(),
2191 dispatcher(),
2192 metrics(),
2193 manager(),
2194 wifi_provider_,
2195 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002196 kModeManaged,
2197 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002198 false));
2199 matching_service->set_profile(profile1);
2200
2201 // We need to get rid of our reference to this mock service as soon
2202 // as Manager::ConfigureServiceForProfile() takes a reference in its
2203 // call to WiFiProvider::CreateTemporaryService(). This way the
2204 // latter function can keep a DCHECK(service->HasOneRef() even in
2205 // unit tests.
2206 temp_mock_service_ =
2207 new NiceMock<MockWiFiService>(control_interface(),
2208 dispatcher(),
2209 metrics(),
2210 manager(),
2211 wifi_provider_,
2212 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002213 kModeManaged,
2214 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002215 false);
2216
2217 // Only hold a pointer here so we don't affect the refcount.
2218 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2219
2220 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002221 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002222 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2223 .WillOnce(Return(matching_service));
2224 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2225 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2226 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2227 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2228 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2229 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2230 .Times(1);
2231 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2232 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2233
2234 Error error;
2235 ServiceRefPtr service =
2236 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2237 EXPECT_TRUE(error.IsSuccess());
2238 EXPECT_EQ(NULL, service.get());
2239 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2240}
2241
Paul Stewart7a20aa42013-01-17 12:21:41 -08002242TEST_F(ManagerTest, FindMatchingService) {
2243 KeyValueStore args;
2244 {
2245 Error error;
2246 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2247 EXPECT_EQ(Error::kNotFound, error.type());
2248 }
2249
2250 scoped_refptr<MockService> mock_service0(
2251 new NiceMock<MockService>(control_interface(),
2252 dispatcher(),
2253 metrics(),
2254 manager()));
2255 scoped_refptr<MockService> mock_service1(
2256 new NiceMock<MockService>(control_interface(),
2257 dispatcher(),
2258 metrics(),
2259 manager()));
2260 manager()->RegisterService(mock_service0);
2261 manager()->RegisterService(mock_service1);
2262 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2263 .WillOnce(Return(true))
2264 .WillRepeatedly(Return(false));
2265 {
2266 Error error;
2267 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2268 EXPECT_TRUE(error.IsSuccess());
2269 }
2270 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2271 .WillOnce(Return(true))
2272 .WillRepeatedly(Return(false));
2273 {
2274 Error error;
2275 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2276 EXPECT_TRUE(error.IsSuccess());
2277 }
2278 {
2279 Error error;
2280 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2281 EXPECT_EQ(Error::kNotFound, error.type());
2282 }
2283}
2284
Paul Stewart22aa71b2011-09-16 12:15:11 -07002285TEST_F(ManagerTest, TechnologyOrder) {
2286 Error error;
Ben Chan923a5022013-09-20 11:23:23 -07002287 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2288 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002289 ASSERT_TRUE(error.IsSuccess());
2290 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002291 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002292
Ben Chan923a5022013-09-20 11:23:23 -07002293 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2294 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002295 ASSERT_FALSE(error.IsSuccess());
2296 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002297 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002298 manager()->GetTechnologyOrder());
2299}
2300
2301TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002302 // TODO(quiche): Some of these tests would probably fit better in
2303 // service_unittest, since the actual comparison of Services is
Paul Stewartee6b3d72013-07-12 16:07:51 -07002304 // implemented in Service. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002305
Paul Stewart22aa71b2011-09-16 12:15:11 -07002306 scoped_refptr<MockService> mock_service0(
2307 new NiceMock<MockService>(control_interface(),
2308 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002309 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002310 manager()));
2311 scoped_refptr<MockService> mock_service1(
2312 new NiceMock<MockService>(control_interface(),
2313 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002314 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002315 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002316
2317 manager()->RegisterService(mock_service0);
2318 manager()->RegisterService(mock_service1);
2319
Darin Petkov457728b2013-01-09 09:49:08 +01002320 // Services should already be sorted by |unique_name_|
Paul Stewart22aa71b2011-09-16 12:15:11 -07002321 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2322
2323 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002324 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002325 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2326
2327 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01002328 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002329 manager()->UpdateService(mock_service1);
2330 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2331
2332 // Security
mukesh agrawal43970a22013-02-15 16:00:07 -08002333 mock_service0->SetSecurity(Service::kCryptoAes, true, true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002334 manager()->UpdateService(mock_service0);
2335 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2336
2337 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07002338 EXPECT_CALL(*mock_service0.get(), technology())
2339 .WillRepeatedly(Return((Technology::kWifi)));
2340 EXPECT_CALL(*mock_service1.get(), technology())
2341 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002342
2343 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002344 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002345 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002346 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2347
Ben Chan923a5022013-09-20 11:23:23 -07002348 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2349 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002350 EXPECT_TRUE(error.IsSuccess());
2351 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2352
Gaurav Shah435de2c2011-11-17 19:01:07 -08002353 // Priority.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002354 mock_service0->SetPriority(1, NULL);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002355 manager()->UpdateService(mock_service0);
2356 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2357
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002358 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00002359 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002360 manager()->UpdateService(mock_service1);
2361 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2362
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002363 // Auto-connect.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002364 mock_service0->SetAutoConnect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002365 manager()->UpdateService(mock_service0);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002366 mock_service1->SetAutoConnect(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002367 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002368 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2369
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002370 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2371 // to any of the others below, so we reset to the default state after
2372 // testing.
2373 EXPECT_CALL(*mock_service1.get(),
2374 IsDependentOn(ServiceRefPtr(mock_service0.get())))
2375 .WillOnce(Return(true))
2376 .WillRepeatedly(Return(false));
2377 manager()->UpdateService(mock_service1);
2378 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2379 manager()->UpdateService(mock_service0);
2380 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2381
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002382 // Connectable.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002383 mock_service1->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002384 manager()->UpdateService(mock_service1);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002385 mock_service0->SetConnectable(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002386 manager()->UpdateService(mock_service0);
2387 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2388
2389 // IsFailed.
2390 EXPECT_CALL(*mock_service0.get(), state())
2391 .WillRepeatedly(Return(Service::kStateIdle));
2392 EXPECT_CALL(*mock_service0.get(), IsFailed())
2393 .WillRepeatedly(Return(false));
2394 manager()->UpdateService(mock_service0);
2395 EXPECT_CALL(*mock_service0.get(), state())
2396 .WillRepeatedly(Return(Service::kStateFailure));
2397 EXPECT_CALL(*mock_service1.get(), IsFailed())
2398 .WillRepeatedly(Return(true));
2399 manager()->UpdateService(mock_service1);
2400 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2401
2402 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07002403 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002404 .WillRepeatedly(Return(Service::kStateAssociating));
2405 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002406 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002407 manager()->UpdateService(mock_service1);
2408 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2409
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002410 // Connected.
2411 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002412 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002413 EXPECT_CALL(*mock_service0.get(), IsConnected())
2414 .WillRepeatedly(Return(true));
2415 manager()->UpdateService(mock_service0);
2416 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2417
Paul Stewarta121c442012-06-09 14:12:58 -07002418 // Portal.
2419 EXPECT_CALL(*mock_service1.get(), state())
2420 .WillRepeatedly(Return(Service::kStateConnected));
2421 EXPECT_CALL(*mock_service1.get(), IsConnected())
2422 .WillRepeatedly(Return(true));
2423 manager()->UpdateService(mock_service1);
2424 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2425
Paul Stewart22aa71b2011-09-16 12:15:11 -07002426 manager()->DeregisterService(mock_service0);
2427 manager()->DeregisterService(mock_service1);
2428}
2429
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002430TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002431 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002432 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002433
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002434 scoped_refptr<MockService> mock_service0(
2435 new NiceMock<MockService>(control_interface(),
2436 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002437 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002438 manager()));
2439 scoped_refptr<MockService> mock_service1(
2440 new NiceMock<MockService>(control_interface(),
2441 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002442 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002443 manager()));
2444
2445 scoped_refptr<MockConnection> mock_connection0(
2446 new NiceMock<MockConnection>(device_info_.get()));
2447 scoped_refptr<MockConnection> mock_connection1(
2448 new NiceMock<MockConnection>(device_info_.get()));
2449
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002450 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002451 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002452 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002453 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002454 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002455 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002456
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002457 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002458 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002459
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002460 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002461 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002462 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002463
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002464 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002465 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002466 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002467
Paul Stewartce4ec192012-03-14 12:53:46 -07002468 mock_service0->set_mock_connection(mock_connection0);
2469 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002470
2471 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002472 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002473 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002474
Darin Petkova5e07ef2012-07-09 14:27:57 +02002475 ServiceWatcher service_watcher;
2476 int tag =
2477 manager()->RegisterDefaultServiceCallback(
2478 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2479 service_watcher.AsWeakPtr()));
2480 EXPECT_EQ(1, tag);
2481
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002482 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002483 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2484 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002485 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002486 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002487 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002488
Darin Petkova5e07ef2012-07-09 14:27:57 +02002489 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002490 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002491 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00002492 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002493 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002494 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002495 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002496
Paul Stewartce4ec192012-03-14 12:53:46 -07002497 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002498 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002499 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002500 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002501
2502 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002503 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002504}
2505
Darin Petkova5e07ef2012-07-09 14:27:57 +02002506TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2507 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2508 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2509
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002510 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002511 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002512
2513 scoped_refptr<MockService> mock_service(
2514 new NiceMock<MockService>(
2515 control_interface(), dispatcher(), metrics(), manager()));
2516 ServiceRefPtr service = mock_service;
2517 ServiceRefPtr null_service;
2518
2519 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2520 manager()->NotifyDefaultServiceChanged(null_service);
2521
2522 ServiceWatcher service_watcher1;
2523 ServiceWatcher service_watcher2;
2524 int tag1 =
2525 manager()->RegisterDefaultServiceCallback(
2526 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2527 service_watcher1.AsWeakPtr()));
2528 EXPECT_EQ(1, tag1);
2529 int tag2 =
2530 manager()->RegisterDefaultServiceCallback(
2531 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2532 service_watcher2.AsWeakPtr()));
2533 EXPECT_EQ(2, tag2);
2534
2535 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2536 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2537 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2538 manager()->NotifyDefaultServiceChanged(null_service);
2539
2540 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2541 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2542 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2543 manager()->NotifyDefaultServiceChanged(mock_service);
2544
2545 manager()->DeregisterDefaultServiceCallback(tag1);
2546 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2547 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2548 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2549 manager()->NotifyDefaultServiceChanged(mock_service);
2550 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2551
2552 manager()->DeregisterDefaultServiceCallback(tag2);
2553 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2554 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2555 manager()->NotifyDefaultServiceChanged(mock_service);
2556
2557 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2558 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2559}
2560
Gaurav Shah435de2c2011-11-17 19:01:07 -08002561TEST_F(ManagerTest, AvailableTechnologies) {
2562 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2563 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002564 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002565 manager(),
2566 "null4",
2567 "addr4",
2568 0));
2569 manager()->RegisterDevice(mock_devices_[0]);
2570 manager()->RegisterDevice(mock_devices_[1]);
2571 manager()->RegisterDevice(mock_devices_[2]);
2572 manager()->RegisterDevice(mock_devices_[3]);
2573
2574 ON_CALL(*mock_devices_[0].get(), technology())
2575 .WillByDefault(Return(Technology::kEthernet));
2576 ON_CALL(*mock_devices_[1].get(), technology())
2577 .WillByDefault(Return(Technology::kWifi));
2578 ON_CALL(*mock_devices_[2].get(), technology())
2579 .WillByDefault(Return(Technology::kCellular));
2580 ON_CALL(*mock_devices_[3].get(), technology())
2581 .WillByDefault(Return(Technology::kWifi));
2582
2583 set<string> expected_technologies;
2584 expected_technologies.insert(Technology::NameFromIdentifier(
2585 Technology::kEthernet));
2586 expected_technologies.insert(Technology::NameFromIdentifier(
2587 Technology::kWifi));
2588 expected_technologies.insert(Technology::NameFromIdentifier(
2589 Technology::kCellular));
2590 Error error;
2591 vector<string> technologies = manager()->AvailableTechnologies(&error);
2592
2593 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2594 ContainerEq(expected_technologies));
2595}
2596
2597TEST_F(ManagerTest, ConnectedTechnologies) {
2598 scoped_refptr<MockService> connected_service1(
2599 new NiceMock<MockService>(control_interface(),
2600 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002601 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002602 manager()));
2603 scoped_refptr<MockService> connected_service2(
2604 new NiceMock<MockService>(control_interface(),
2605 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002606 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002607 manager()));
2608 scoped_refptr<MockService> disconnected_service1(
2609 new NiceMock<MockService>(control_interface(),
2610 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002611 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002612 manager()));
2613 scoped_refptr<MockService> disconnected_service2(
2614 new NiceMock<MockService>(control_interface(),
2615 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002616 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002617 manager()));
2618
2619 ON_CALL(*connected_service1.get(), IsConnected())
2620 .WillByDefault(Return(true));
2621 ON_CALL(*connected_service2.get(), IsConnected())
2622 .WillByDefault(Return(true));
2623
2624 manager()->RegisterService(connected_service1);
2625 manager()->RegisterService(connected_service2);
2626 manager()->RegisterService(disconnected_service1);
2627 manager()->RegisterService(disconnected_service2);
2628
2629 manager()->RegisterDevice(mock_devices_[0]);
2630 manager()->RegisterDevice(mock_devices_[1]);
2631 manager()->RegisterDevice(mock_devices_[2]);
2632 manager()->RegisterDevice(mock_devices_[3]);
2633
2634 ON_CALL(*mock_devices_[0].get(), technology())
2635 .WillByDefault(Return(Technology::kEthernet));
2636 ON_CALL(*mock_devices_[1].get(), technology())
2637 .WillByDefault(Return(Technology::kWifi));
2638 ON_CALL(*mock_devices_[2].get(), technology())
2639 .WillByDefault(Return(Technology::kCellular));
2640 ON_CALL(*mock_devices_[3].get(), technology())
2641 .WillByDefault(Return(Technology::kWifi));
2642
2643 mock_devices_[0]->SelectService(connected_service1);
2644 mock_devices_[1]->SelectService(disconnected_service1);
2645 mock_devices_[2]->SelectService(disconnected_service2);
2646 mock_devices_[3]->SelectService(connected_service2);
2647
2648 set<string> expected_technologies;
2649 expected_technologies.insert(Technology::NameFromIdentifier(
2650 Technology::kEthernet));
2651 expected_technologies.insert(Technology::NameFromIdentifier(
2652 Technology::kWifi));
2653 Error error;
2654
2655 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2656 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2657 ContainerEq(expected_technologies));
2658}
2659
2660TEST_F(ManagerTest, DefaultTechnology) {
2661 scoped_refptr<MockService> connected_service(
2662 new NiceMock<MockService>(control_interface(),
2663 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002664 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002665 manager()));
2666 scoped_refptr<MockService> disconnected_service(
2667 new NiceMock<MockService>(control_interface(),
2668 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002669 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002670 manager()));
2671
2672 // Connected. WiFi.
2673 ON_CALL(*connected_service.get(), IsConnected())
2674 .WillByDefault(Return(true));
2675 ON_CALL(*connected_service.get(), state())
2676 .WillByDefault(Return(Service::kStateConnected));
2677 ON_CALL(*connected_service.get(), technology())
2678 .WillByDefault(Return(Technology::kWifi));
2679
2680 // Disconnected. Ethernet.
2681 ON_CALL(*disconnected_service.get(), technology())
2682 .WillByDefault(Return(Technology::kEthernet));
2683
2684 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002685 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002686 Error error;
2687 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2688
2689
2690 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002691 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002692 // Connected service should be brought to the front now.
2693 string expected_technology =
2694 Technology::NameFromIdentifier(Technology::kWifi);
2695 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2696}
2697
Paul Stewart212d60f2012-07-12 10:59:13 -07002698TEST_F(ManagerTest, Stop) {
2699 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002700 new NiceMock<MockProfile>(
2701 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002702 AdoptProfile(manager(), profile);
2703 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002704 new NiceMock<MockService>(control_interface(),
2705 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002706 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002707 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002708 manager()->RegisterService(service);
2709 manager()->RegisterDevice(mock_devices_[0]);
2710 EXPECT_CALL(*profile.get(),
2711 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2712 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002713 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002714 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2715 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002716 manager()->Stop();
2717}
2718
mukesh agrawal00917ce2011-11-22 23:56:55 +00002719TEST_F(ManagerTest, UpdateServiceConnected) {
2720 scoped_refptr<MockService> mock_service(
2721 new NiceMock<MockService>(control_interface(),
2722 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002723 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002724 manager()));
2725 manager()->RegisterService(mock_service);
2726 EXPECT_FALSE(mock_service->favorite());
2727 EXPECT_FALSE(mock_service->auto_connect());
2728
Gaurav Shah435de2c2011-11-17 19:01:07 -08002729 EXPECT_CALL(*mock_service.get(), IsConnected())
2730 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002731 manager()->UpdateService(mock_service);
2732 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2733 // to mock out MakeFavorite. And mocking that out would break the
Paul Stewartee6b3d72013-07-12 16:07:51 -07002734 // SortServices test. (crbug.com/206367)
mukesh agrawal00917ce2011-11-22 23:56:55 +00002735 EXPECT_TRUE(mock_service->favorite());
2736 EXPECT_TRUE(mock_service->auto_connect());
2737}
2738
Thieu Led4e9e552012-02-16 16:26:07 -08002739TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2740 // This tests the case where the user connects to a service that is
2741 // currently associated with a profile. We want to make sure that the
2742 // favorite flag is set and that the flag is saved to the current
2743 // profile.
2744 scoped_refptr<MockService> mock_service(
2745 new NiceMock<MockService>(control_interface(),
2746 dispatcher(),
2747 metrics(),
2748 manager()));
2749 manager()->RegisterService(mock_service);
2750 EXPECT_FALSE(mock_service->favorite());
2751 EXPECT_FALSE(mock_service->auto_connect());
2752
Gary Moraind93615e2012-04-27 11:50:03 -07002753 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002754 new MockProfile(
2755 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002756
Gary Moraind93615e2012-04-27 11:50:03 -07002757 mock_service->set_profile(profile);
2758 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002759 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002760 EXPECT_CALL(*profile,
2761 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002762 manager()->UpdateService(mock_service);
2763 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2764 // to mock out MakeFavorite. And mocking that out would break the
Paul Stewartee6b3d72013-07-12 16:07:51 -07002765 // SortServices test. (crbug.com/206367)
Thieu Led4e9e552012-02-16 16:26:07 -08002766 EXPECT_TRUE(mock_service->favorite());
2767 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002768 // This releases the ref on the mock profile.
2769 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002770}
2771
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002772TEST_F(ManagerTest, SaveSuccessfulService) {
2773 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002774 new StrictMock<MockProfile>(
2775 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002776 AdoptProfile(manager(), profile);
2777 scoped_refptr<MockService> service(
2778 new NiceMock<MockService>(control_interface(),
2779 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002780 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002781 manager()));
2782
2783 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2784 ServiceRefPtr expect_service(service.get());
2785
2786 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2787 .WillOnce(Return(false));
2788 manager()->RegisterService(service);
2789
2790 EXPECT_CALL(*service.get(), state())
2791 .WillRepeatedly(Return(Service::kStateConnected));
2792 EXPECT_CALL(*service.get(), IsConnected())
2793 .WillRepeatedly(Return(true));
2794 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2795 .WillOnce(Return(true));
2796 manager()->UpdateService(service);
2797}
2798
Darin Petkove7c6ad32012-06-29 10:22:09 +02002799TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002800 MockProfile *profile0 =
2801 new MockProfile(control_interface(), metrics(), manager(), "");
2802 MockProfile *profile1 =
2803 new MockProfile(control_interface(), metrics(), manager(), "");
2804 MockProfile *profile2 =
2805 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002806 AdoptProfile(manager(), profile0); // Passes ownership.
2807 AdoptProfile(manager(), profile1); // Passes ownership.
2808 AdoptProfile(manager(), profile2); // Passes ownership.
2809 DeviceRefPtr device_ref(mock_devices_[0].get());
2810 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2811 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2812 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2813 manager()->UpdateDevice(mock_devices_[0]);
2814}
2815
Paul Stewart1b253142012-01-26 14:05:52 -08002816TEST_F(ManagerTest, EnumerateProfiles) {
2817 vector<string> profile_paths;
2818 for (size_t i = 0; i < 10; i++) {
2819 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002820 new StrictMock<MockProfile>(
2821 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002822 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002823 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2824 .WillOnce(Return(profile_paths.back()));
2825 AdoptProfile(manager(), profile);
2826 }
2827
2828 Error error;
2829 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2830 EXPECT_TRUE(error.IsSuccess());
2831 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2832 for (size_t i = 0; i < profile_paths.size(); i++) {
2833 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2834 }
2835}
2836
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002837TEST_F(ManagerTest, AutoConnectOnRegister) {
2838 MockServiceRefPtr service = MakeAutoConnectableService();
2839 EXPECT_CALL(*service.get(), AutoConnect());
2840 manager()->RegisterService(service);
2841 dispatcher()->DispatchPendingEvents();
2842}
2843
2844TEST_F(ManagerTest, AutoConnectOnUpdate) {
2845 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002846 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002847 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002848 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002849 manager()->RegisterService(service1);
2850 manager()->RegisterService(service2);
2851 dispatcher()->DispatchPendingEvents();
2852
2853 EXPECT_CALL(*service1.get(), AutoConnect());
2854 EXPECT_CALL(*service2.get(), state())
2855 .WillRepeatedly(Return(Service::kStateFailure));
2856 EXPECT_CALL(*service2.get(), IsFailed())
2857 .WillRepeatedly(Return(true));
2858 EXPECT_CALL(*service2.get(), IsConnected())
2859 .WillRepeatedly(Return(false));
2860 manager()->UpdateService(service2);
2861 dispatcher()->DispatchPendingEvents();
2862}
2863
2864TEST_F(ManagerTest, AutoConnectOnDeregister) {
2865 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002866 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002867 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002868 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002869 manager()->RegisterService(service1);
2870 manager()->RegisterService(service2);
2871 dispatcher()->DispatchPendingEvents();
2872
2873 EXPECT_CALL(*service1.get(), AutoConnect());
2874 manager()->DeregisterService(service2);
2875 dispatcher()->DispatchPendingEvents();
2876}
2877
Darin Petkov3ec55342012-09-28 14:04:44 +02002878TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2879 MockServiceRefPtr service = MakeAutoConnectableService();
2880 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2881 SetPowerManager();
2882 EXPECT_CALL(*service, AutoConnect()).Times(0);
2883 manager()->RegisterService(service);
2884 dispatcher()->DispatchPendingEvents();
2885}
2886
Darin Petkovca621542012-07-25 14:25:56 +02002887TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2888 MockServiceRefPtr service = MakeAutoConnectableService();
2889 SetPowerState(PowerManagerProxyDelegate::kMem);
2890 SetPowerManager();
2891 EXPECT_CALL(*service, AutoConnect()).Times(0);
2892 manager()->RegisterService(service);
2893 dispatcher()->DispatchPendingEvents();
2894}
2895
2896TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2897 MockServiceRefPtr service = MakeAutoConnectableService();
2898 SetPowerState(PowerManagerProxyDelegate::kOn);
2899 SetPowerManager();
2900 EXPECT_CALL(*service, AutoConnect());
2901 manager()->RegisterService(service);
2902 dispatcher()->DispatchPendingEvents();
2903}
2904
2905TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2906 MockServiceRefPtr service = MakeAutoConnectableService();
2907 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2908 SetPowerManager();
2909 EXPECT_CALL(*service, AutoConnect());
2910 manager()->RegisterService(service);
2911 dispatcher()->DispatchPendingEvents();
2912}
2913
Paul Stewart63864b62012-11-07 15:10:55 -08002914TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2915 SetRunning(false);
2916 MockServiceRefPtr service = MakeAutoConnectableService();
2917 EXPECT_CALL(*service, AutoConnect()).Times(0);
2918 manager()->RegisterService(service);
2919 dispatcher()->DispatchPendingEvents();
2920}
2921
Darin Petkovca621542012-07-25 14:25:56 +02002922TEST_F(ManagerTest, OnPowerStateChanged) {
2923 MockServiceRefPtr service = MakeAutoConnectableService();
2924 SetPowerState(PowerManagerProxyDelegate::kOn);
2925 SetPowerManager();
2926 EXPECT_CALL(*service, AutoConnect());
2927 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002928 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002929 dispatcher()->DispatchPendingEvents();
2930
mukesh agrawal784566d2012-08-08 18:32:58 -07002931 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002932 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2933 EXPECT_CALL(*service, AutoConnect());
2934 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002935 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002936
mukesh agrawal784566d2012-08-08 18:32:58 -07002937 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002938 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2939 EXPECT_CALL(*service, AutoConnect()).Times(0);
2940 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002941 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002942}
2943
Darin Petkov3ec55342012-09-28 14:04:44 +02002944TEST_F(ManagerTest, AddTerminationAction) {
2945 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
Daniel Eratf9753672013-01-24 10:17:02 -08002946 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02002947 SetPowerManager();
2948 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2949 manager()->AddTerminationAction("action1", base::Closure());
2950 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2951 manager()->AddTerminationAction("action2", base::Closure());
2952}
2953
2954TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002955 const char kKey1[] = "action1";
2956 const char kKey2[] = "action2";
2957 const int kSuspendDelayId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002958
2959 MockPowerManager &power_manager = *power_manager_;
2960 SetPowerManager();
2961
2962 // Removing an action when the hook table is empty should not result in any
2963 // calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002964 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002965 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2966 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2967 manager()->RemoveTerminationAction("unknown");
2968 Mock::VerifyAndClearExpectations(&power_manager);
2969
Daniel Eratf9753672013-01-24 10:17:02 -08002970 EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _))
2971 .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true)));
Daniel Erat0818cca2012-12-14 10:16:21 -08002972 EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1);
Darin Petkov3ec55342012-09-28 14:04:44 +02002973 manager()->AddTerminationAction(kKey1, base::Closure());
2974 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2975 manager()->AddTerminationAction(kKey2, base::Closure());
2976
2977 // Removing an action that ends up with a non-empty hook table should not
2978 // result in any calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002979 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002980 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2981 manager()->RemoveTerminationAction(kKey1);
2982 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2983 Mock::VerifyAndClearExpectations(&power_manager);
2984
2985 // Removing the last action should trigger unregistering from the power
2986 // manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002987 EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId))
2988 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02002989 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2990 manager()->RemoveTerminationAction(kKey2);
2991 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2992}
2993
2994TEST_F(ManagerTest, RunTerminationActions) {
2995 TerminationActionTest test_action;
2996 const string kActionName = "action";
2997
2998 EXPECT_CALL(test_action, Done(_));
2999 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3000 test_action.AsWeakPtr()));
3001
3002 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3003 Bind(&TerminationActionTest::Action,
3004 test_action.AsWeakPtr()));
3005 test_action.set_manager(manager());
3006 EXPECT_CALL(test_action, Done(_));
3007 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3008 test_action.AsWeakPtr()));
3009}
3010
Daniel Erat0818cca2012-12-14 10:16:21 -08003011TEST_F(ManagerTest, OnSuspendImminent) {
3012 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02003013 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Erat0818cca2012-12-14 10:16:21 -08003014 EXPECT_CALL(*power_manager_,
3015 ReportSuspendReadiness(
3016 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003017 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003018 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02003019}
3020
3021TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003022 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02003023 Error error;
Daniel Erat0818cca2012-12-14 10:16:21 -08003024 EXPECT_CALL(*power_manager_,
3025 ReportSuspendReadiness(
3026 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003027 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003028 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003029}
3030
Paul Stewartc681fa02012-03-02 19:40:04 -08003031TEST_F(ManagerTest, RecheckPortal) {
3032 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3033 .WillOnce(Return(false));
3034 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3035 .WillOnce(Return(true));
3036 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3037 .Times(0);
3038
3039 manager()->RegisterDevice(mock_devices_[0]);
3040 manager()->RegisterDevice(mock_devices_[1]);
3041 manager()->RegisterDevice(mock_devices_[2]);
3042
3043 manager()->RecheckPortal(NULL);
3044}
3045
Paul Stewartd215af62012-04-24 23:25:50 -07003046TEST_F(ManagerTest, RecheckPortalOnService) {
3047 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3048 dispatcher(),
3049 metrics(),
3050 manager());
3051 EXPECT_CALL(*mock_devices_[0].get(),
3052 IsConnectedToService(IsRefPtrTo(service)))
3053 .WillOnce(Return(false));
3054 EXPECT_CALL(*mock_devices_[1].get(),
3055 IsConnectedToService(IsRefPtrTo(service)))
3056 .WillOnce(Return(true));
3057 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3058 .WillOnce(Return(true));
3059 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3060 .Times(0);
3061
3062 manager()->RegisterDevice(mock_devices_[0]);
3063 manager()->RegisterDevice(mock_devices_[1]);
3064 manager()->RegisterDevice(mock_devices_[2]);
3065
3066 manager()->RecheckPortalOnService(service);
3067}
3068
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003069TEST_F(ManagerTest, GetDefaultService) {
3070 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003071 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003072
3073 scoped_refptr<MockService> mock_service(
3074 new NiceMock<MockService>(control_interface(),
3075 dispatcher(),
3076 metrics(),
3077 manager()));
3078
3079 manager()->RegisterService(mock_service);
3080 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003081 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003082
3083 scoped_refptr<MockConnection> mock_connection(
3084 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003085 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003086 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003087 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003088
Paul Stewartce4ec192012-03-14 12:53:46 -07003089 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003090 manager()->DeregisterService(mock_service);
3091}
3092
Paul Stewart13ed2252012-03-21 12:52:46 -07003093TEST_F(ManagerTest, GetServiceWithGUID) {
3094 scoped_refptr<MockService> mock_service0(
3095 new NiceMock<MockService>(control_interface(),
3096 dispatcher(),
3097 metrics(),
3098 manager()));
3099
3100 scoped_refptr<MockService> mock_service1(
3101 new NiceMock<MockService>(control_interface(),
3102 dispatcher(),
3103 metrics(),
3104 manager()));
3105
Paul Stewartcb59fed2012-03-21 21:14:46 -07003106 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3107 .Times(0);
3108 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3109 .Times(0);
3110
Paul Stewart13ed2252012-03-21 12:52:46 -07003111 manager()->RegisterService(mock_service0);
3112 manager()->RegisterService(mock_service1);
3113
3114 const string kGUID0 = "GUID0";
3115 const string kGUID1 = "GUID1";
3116
3117 {
3118 Error error;
3119 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3120 EXPECT_FALSE(error.IsSuccess());
3121 EXPECT_FALSE(service);
3122 }
3123
3124 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003125 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003126
3127 {
3128 Error error;
3129 ServiceRefPtr service = manager()->GetService(args, &error);
3130 EXPECT_EQ(Error::kInvalidArguments, error.type());
3131 EXPECT_FALSE(service);
3132 }
3133
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003134 mock_service0->SetGuid(kGUID0, NULL);
3135 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003136
3137 {
3138 Error error;
3139 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3140 EXPECT_TRUE(error.IsSuccess());
3141 EXPECT_EQ(mock_service0.get(), service.get());
3142 }
3143
3144 {
3145 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003146 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3147 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003148 ServiceRefPtr service = manager()->GetService(args, &error);
3149 EXPECT_TRUE(error.IsSuccess());
3150 EXPECT_EQ(mock_service1.get(), service.get());
3151 }
3152
3153 manager()->DeregisterService(mock_service0);
3154 manager()->DeregisterService(mock_service1);
3155}
3156
Gary Morain028545d2012-04-07 14:55:52 -07003157
3158TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003159 EXPECT_FALSE(manager()->IsOnline());
3160 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3161
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003162 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003163 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003164 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3165 .Times(AnyNumber());
3166 scoped_refptr<MockService> mock_service0(
3167 new NiceMock<MockService>(control_interface(),
3168 dispatcher(),
3169 metrics(),
3170 manager()));
3171
3172 scoped_refptr<MockService> mock_service1(
3173 new NiceMock<MockService>(control_interface(),
3174 dispatcher(),
3175 metrics(),
3176 manager()));
3177
3178 EXPECT_CALL(*mock_service0.get(), IsConnected())
3179 .WillRepeatedly(Return(false));
3180 EXPECT_CALL(*mock_service1.get(), IsConnected())
3181 .WillRepeatedly(Return(false));
3182
3183 manager()->RegisterService(mock_service0);
3184 manager()->RegisterService(mock_service1);
3185
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003186 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003187 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3188
3189 manager()->DeregisterService(mock_service0);
3190 manager()->DeregisterService(mock_service1);
3191}
3192
3193TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003194 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003195 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003196 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3197 .Times(AnyNumber());
3198 scoped_refptr<MockService> mock_service0(
3199 new NiceMock<MockService>(control_interface(),
3200 dispatcher(),
3201 metrics(),
3202 manager()));
3203
3204 scoped_refptr<MockService> mock_service1(
3205 new NiceMock<MockService>(control_interface(),
3206 dispatcher(),
3207 metrics(),
3208 manager()));
3209
3210 EXPECT_CALL(*mock_service0.get(), IsConnected())
3211 .WillRepeatedly(Return(false));
3212 EXPECT_CALL(*mock_service1.get(), IsConnected())
3213 .WillRepeatedly(Return(true));
3214 EXPECT_CALL(*mock_service0.get(), state())
3215 .WillRepeatedly(Return(Service::kStateIdle));
3216 EXPECT_CALL(*mock_service1.get(), state())
3217 .WillRepeatedly(Return(Service::kStateConnected));
3218
3219 manager()->RegisterService(mock_service0);
3220 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003221 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003222
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003223 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003224 EXPECT_EQ("online", manager()->CalculateState(NULL));
3225
3226 manager()->DeregisterService(mock_service0);
3227 manager()->DeregisterService(mock_service1);
3228}
3229
Paul Stewart03e29f72013-09-26 00:49:48 -07003230TEST_F(ManagerTest, RefreshConnectionState) {
3231 EXPECT_CALL(*manager_adaptor_,
3232 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3233 RefreshConnectionState();
3234 Mock::VerifyAndClearExpectations(manager_adaptor_);
3235
3236 scoped_refptr<MockService> mock_service(
3237 new NiceMock<MockService>(control_interface(),
3238 dispatcher(),
3239 metrics(),
3240 manager()));
3241 EXPECT_CALL(*manager_adaptor_,
3242 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3243 manager()->RegisterService(mock_service);
3244 RefreshConnectionState();
3245
3246 scoped_refptr<MockConnection> mock_connection(
3247 new NiceMock<MockConnection>(device_info_.get()));
3248 mock_service->set_mock_connection(mock_connection);
3249 EXPECT_CALL(*mock_service, state())
3250 .WillOnce(Return(Service::kStateIdle));
3251 RefreshConnectionState();
3252
3253 Mock::VerifyAndClearExpectations(manager_adaptor_);
3254 EXPECT_CALL(*mock_service, state())
3255 .WillOnce(Return(Service::kStatePortal));
3256 EXPECT_CALL(*manager_adaptor_,
3257 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3258 RefreshConnectionState();
3259 Mock::VerifyAndClearExpectations(manager_adaptor_);
3260
3261 mock_service->set_mock_connection(NULL);
3262 manager()->DeregisterService(mock_service);
3263}
3264
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003265TEST_F(ManagerTest, StartupPortalList) {
3266 // Simulate loading value from the default profile.
3267 const string kProfileValue("wifi,vpn");
3268 manager()->props_.check_portal_list = kProfileValue;
3269
3270 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3271 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3272 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3273
3274 const string kStartupValue("cellular,ethernet");
3275 manager()->SetStartupPortalList(kStartupValue);
3276 // Ensure profile value is not overwritten, so when we save the default
3277 // profile, the correct value will still be written.
3278 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3279
3280 // However we should read back a different list.
3281 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3282 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3283 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3284
3285 const string kRuntimeValue("ppp");
3286 // Setting a runtime value over the control API should overwrite both
3287 // the profile value and what we read back.
3288 Error error;
3289 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003290 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003291 kRuntimeValue,
3292 &error);
3293 ASSERT_TRUE(error.IsSuccess());
3294 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3295 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3296 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3297 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3298}
3299
Paul Stewart036dba02012-08-07 12:34:41 -07003300TEST_F(ManagerTest, LinkMonitorEnabled) {
3301 const string kEnabledTechnologies("wifi,vpn");
3302 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3303 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3304 EXPECT_FALSE(
3305 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3306}
3307
Paul Stewart85aea152013-01-22 09:31:56 -08003308TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003309 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003310 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003311 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003312 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003313 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003314 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3315 AdoptProfile(manager(), profile);
3316 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3317 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3318 scoped_ptr<MockStore> store1(new MockStore);
3319 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3320}
3321
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003322TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003323 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003324 DisableTechnologyReplyHandler disable_technology_reply_handler;
3325 ResultCallback disable_technology_callback(
3326 Bind(&DisableTechnologyReplyHandler::ReportResult,
3327 disable_technology_reply_handler.AsWeakPtr()));
3328 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3329
Ben Chan923a5022013-09-20 11:23:23 -07003330 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003331 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003332 EXPECT_TRUE(error.IsSuccess());
3333
Joshua Krollda798622012-06-05 12:30:48 -07003334 ON_CALL(*mock_devices_[0], technology())
3335 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003336 ON_CALL(*mock_devices_[1], technology())
3337 .WillByDefault(Return(Technology::kCellular));
3338 ON_CALL(*mock_devices_[2], technology())
3339 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003340
3341 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003342 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003343
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003344 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003345 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3346 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003347 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003348 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003349 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003350 EXPECT_TRUE(error.IsSuccess());
3351
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003352 // Ethernet Device is enabled, and mock doesn't change error from
3353 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003354 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3355 mock_devices_[0]->enabled_ = true;
3356 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003357 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003358 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003359 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003360
3361 // Ethernet Device is disabled, and mock doesn't change error from
3362 // kOperationInitiated, so expect enable to say operation in progress.
3363 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3364 mock_devices_[0]->enabled_ = false;
3365 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003366 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003367 &error, disable_technology_callback);
3368 EXPECT_TRUE(error.IsOngoing());
3369
3370 // Cellular Device is enabled, but disable failed.
3371 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3372 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3373 mock_devices_[1]->enabled_ = true;
3374 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003375 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003376 &error, disable_technology_callback);
3377 EXPECT_EQ(Error::kPermissionDenied, error.type());
3378
3379 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3380 // if one is in progress (even if the other completed immediately).
3381 manager()->RegisterDevice(mock_devices_[2]);
3382 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3383 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3384 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3385 mock_devices_[1]->enabled_ = true;
3386 mock_devices_[2]->enabled_ = true;
3387 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003388 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003389 &error, disable_technology_callback);
3390 EXPECT_TRUE(error.IsOngoing());
3391
3392 // ...and order doesn't matter.
3393 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3394 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3395 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3396 mock_devices_[1]->enabled_ = true;
3397 mock_devices_[2]->enabled_ = true;
3398 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003399 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003400 &error, disable_technology_callback);
3401 EXPECT_TRUE(error.IsOngoing());
3402 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3403
3404 // Multiple Cellular Devices in enabled state. Even if all disable
3405 // operations complete asynchronously, we only get one call to the
3406 // DisableTechnologyReplyHandler::ReportResult.
3407 ResultCallback device1_result_callback;
3408 ResultCallback device2_result_callback;
3409 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3410 .WillOnce(SaveArg<2>(&device1_result_callback));
3411 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3412 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3413 SaveArg<2>(&device2_result_callback)));
3414 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3415 mock_devices_[1]->enabled_ = true;
3416 mock_devices_[2]->enabled_ = true;
3417 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003418 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003419 &error, disable_technology_callback);
3420 EXPECT_TRUE(error.IsOngoing());
3421 device1_result_callback.Run(Error(Error::kSuccess));
3422 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003423}
3424
Paul Stewart4d5efb72012-09-17 12:24:34 -07003425TEST_F(ManagerTest, IgnoredSearchList) {
3426 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003427 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003428 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003429
3430 const string kIgnored0 = "chromium.org";
3431 ignored_paths.push_back(kIgnored0);
3432 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003433 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003434 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3435
3436 const string kIgnored1 = "google.com";
3437 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3438 ignored_paths.push_back(kIgnored1);
3439 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003440 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003441 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3442
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003443 ignored_paths.clear();
3444 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3445 SetIgnoredDNSSearchPaths("", NULL);
3446 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3447
Paul Stewart4d5efb72012-09-17 12:24:34 -07003448 SetResolver(Resolver::GetInstance());
3449}
3450
Paul Stewartbfb82552012-10-24 16:48:48 -07003451TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3452 // Test to make sure that every service state-change causes the
3453 // Manager to emit a new service list.
3454 scoped_refptr<MockService> mock_service(
3455 new NiceMock<MockService>(control_interface(),
3456 dispatcher(),
3457 metrics(),
3458 manager()));
3459 EXPECT_CALL(*mock_service, state())
3460 .WillRepeatedly(Return(Service::kStateIdle));
3461
3462 manager()->RegisterService(mock_service);
3463 EXPECT_CALL(
3464 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003465 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003466 EXPECT_CALL(
3467 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003468 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003469 CompleteServiceSort();
3470
3471 Mock::VerifyAndClearExpectations(manager_adaptor_);
3472 EXPECT_CALL(
3473 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003474 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003475 EXPECT_CALL(
3476 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003477 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003478 manager()->UpdateService(mock_service.get());
3479 CompleteServiceSort();
3480
3481 manager()->DeregisterService(mock_service);
3482}
3483
3484TEST_F(ManagerTest, EnumerateServices) {
3485 scoped_refptr<MockService> mock_service(
3486 new NiceMock<MockService>(control_interface(),
3487 dispatcher(),
3488 metrics(),
3489 manager()));
3490 manager()->RegisterService(mock_service);
3491
3492 EXPECT_CALL(*mock_service, state())
3493 .WillRepeatedly(Return(Service::kStateConnected));
3494 EXPECT_CALL(*mock_service, IsVisible())
3495 .WillRepeatedly(Return(false));
3496 EXPECT_TRUE(EnumerateAvailableServices().empty());
3497 EXPECT_TRUE(EnumerateWatchedServices().empty());
3498
3499 EXPECT_CALL(*mock_service, state())
3500 .WillRepeatedly(Return(Service::kStateIdle));
3501 EXPECT_TRUE(EnumerateAvailableServices().empty());
3502 EXPECT_TRUE(EnumerateWatchedServices().empty());
3503
3504 EXPECT_CALL(*mock_service, IsVisible())
3505 .WillRepeatedly(Return(true));
3506 Service::ConnectState unwatched_states[] = {
3507 Service::kStateUnknown,
3508 Service::kStateIdle,
3509 Service::kStateFailure
3510 };
3511 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3512 EXPECT_CALL(*mock_service, state())
3513 .WillRepeatedly(Return(unwatched_states[i]));
3514 EXPECT_FALSE(EnumerateAvailableServices().empty());
3515 EXPECT_TRUE(EnumerateWatchedServices().empty());
3516 }
3517
3518 Service::ConnectState watched_states[] = {
3519 Service::kStateAssociating,
3520 Service::kStateConfiguring,
3521 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003522 Service::kStatePortal,
3523 Service::kStateOnline
3524 };
3525 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3526 EXPECT_CALL(*mock_service, state())
3527 .WillRepeatedly(Return(watched_states[i]));
3528 EXPECT_FALSE(EnumerateAvailableServices().empty());
3529 EXPECT_FALSE(EnumerateWatchedServices().empty());
3530 }
3531
3532 manager()->DeregisterService(mock_service);
3533}
3534
Paul Stewart39db5ca2013-03-18 14:15:17 -07003535TEST_F(ManagerTest, ConnectToBestServices) {
3536 scoped_refptr<MockService> wifi_service0(
3537 new NiceMock<MockService>(control_interface(),
3538 dispatcher(),
3539 metrics(),
3540 manager()));
3541 EXPECT_CALL(*wifi_service0.get(), state())
3542 .WillRepeatedly(Return(Service::kStateIdle));
3543 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3544 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003545 wifi_service0->SetConnectable(true);
3546 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003547 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3548 EXPECT_CALL(*wifi_service0.get(), technology())
3549 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003550 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3551 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003552 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3553 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003554
3555 scoped_refptr<MockService> wifi_service1(
3556 new NiceMock<MockService>(control_interface(),
3557 dispatcher(),
3558 metrics(),
3559 manager()));
3560 EXPECT_CALL(*wifi_service1.get(), state())
3561 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003562 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3563 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003564 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3565 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003566 wifi_service1->SetAutoConnect(true);
3567 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003568 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3569 EXPECT_CALL(*wifi_service1.get(), technology())
3570 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003571 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3572 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003573
3574 scoped_refptr<MockService> wifi_service2(
3575 new NiceMock<MockService>(control_interface(),
3576 dispatcher(),
3577 metrics(),
3578 manager()));
3579 EXPECT_CALL(*wifi_service2.get(), state())
3580 .WillRepeatedly(Return(Service::kStateConnected));
3581 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3582 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003583 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3584 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003585 wifi_service2->SetAutoConnect(true);
3586 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003587 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3588 EXPECT_CALL(*wifi_service2.get(), technology())
3589 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003590 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3591 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003592
3593 manager()->RegisterService(wifi_service0);
3594 manager()->RegisterService(wifi_service1);
3595 manager()->RegisterService(wifi_service2);
3596
3597 CompleteServiceSort();
3598 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3599
3600 scoped_refptr<MockService> cell_service(
3601 new NiceMock<MockService>(control_interface(),
3602 dispatcher(),
3603 metrics(),
3604 manager()));
3605
3606 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003607 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003608 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003609 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003610 EXPECT_CALL(*cell_service.get(), IsVisible())
3611 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003612 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003613 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003614 EXPECT_CALL(*cell_service.get(), technology())
3615 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003616 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3617 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003618 manager()->RegisterService(cell_service);
3619
Arman Uguray6fe4f262013-08-02 20:21:55 -07003620 scoped_refptr<MockService> wimax_service(
3621 new NiceMock<MockService>(control_interface(),
3622 dispatcher(),
3623 metrics(),
3624 manager()));
3625
3626 EXPECT_CALL(*wimax_service.get(), state())
3627 .WillRepeatedly(Return(Service::kStateConnected));
3628 EXPECT_CALL(*wimax_service.get(), IsConnected())
3629 .WillRepeatedly(Return(true));
3630 EXPECT_CALL(*wimax_service.get(), IsVisible())
3631 .WillRepeatedly(Return(true));
3632 wimax_service->SetAutoConnect(true);
3633 wimax_service->SetConnectable(true);
3634 EXPECT_CALL(*wimax_service.get(), technology())
3635 .WillRepeatedly(Return(Technology::kWiMax));
3636 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3637 .WillRepeatedly(Return(false));
3638 manager()->RegisterService(wimax_service);
3639
Paul Stewart39db5ca2013-03-18 14:15:17 -07003640 scoped_refptr<MockService> vpn_service(
3641 new NiceMock<MockService>(control_interface(),
3642 dispatcher(),
3643 metrics(),
3644 manager()));
3645
3646 EXPECT_CALL(*vpn_service.get(), state())
3647 .WillRepeatedly(Return(Service::kStateIdle));
3648 EXPECT_CALL(*vpn_service.get(), IsConnected())
3649 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003650 EXPECT_CALL(*vpn_service.get(), IsVisible())
3651 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003652 wifi_service2->SetAutoConnect(false);
3653 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003654 EXPECT_CALL(*vpn_service.get(), technology())
3655 .WillRepeatedly(Return(Technology::kVPN));
3656 manager()->RegisterService(vpn_service);
3657
3658 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003659 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003660
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003661 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3662 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003663 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003664 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3665 .Times(0); // Explicitly disconnected.
3666 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003667 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003668
3669 manager()->ConnectToBestServices(NULL);
3670 dispatcher()->DispatchPendingEvents();
3671
3672 // After this operation, since the Connect calls above are mocked and
3673 // no actual state changes have occurred, we should expect that the
3674 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003675 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003676}
3677
Christopher Wiley83889862013-05-02 15:55:09 -07003678TEST_F(ManagerTest, VerifyWhenNotConnected) {
3679 const string kFakeCertificate("fake cert");
3680 const string kFakePublicKey("fake public key");
3681 const string kFakeNonce("fake public key");
3682 const string kFakeSignedData("fake signed data");
3683 const string kFakeUdn("fake udn");
3684 const vector<uint8_t> kSSID(10, 87);
3685 const string kConfiguredSSID("AConfiguredDestination");
3686 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3687 kConfiguredSSID.end());
3688 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3689 scoped_refptr<MockWiFiService> mock_destination(
3690 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3691 metrics(), manager(), wifi_provider_,
3692 kSSID, "", "none", false));
3693 // Register this service, but don't mark it as connected.
3694 manager()->RegisterService(mock_destination);
3695 // Verify that if we're not connected to anything, verification fails.
3696 {
3697 LOG(INFO) << "Can't verify if not connected.";
3698 EXPECT_CALL(*crypto_util_proxy_,
3699 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3700 Error error(Error::kOperationInitiated);
3701 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3702 kFakeSignedData, kFakeUdn, "", "",
3703 ResultBoolCallback(), &error);
3704 EXPECT_TRUE(error.IsFailure());
3705 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3706 }
3707 {
3708 // However, if the destination is already configured, we might be
3709 // connected to it via something other than WiFi, and we shouldn't
3710 // enforce the WiFi check.
3711 EXPECT_CALL(*crypto_util_proxy_,
3712 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3713 kFakeSignedData, kFakeUdn,
3714 kConfiguredSSIDVector, kConfiguredBSSID,
3715 _, _)).Times(1).WillOnce(Return(true));
3716 Error error(Error::kOperationInitiated);
3717 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3718 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3719 kConfiguredBSSID, ResultBoolCallback(),
3720 &error);
3721 EXPECT_FALSE(error.IsFailure());
3722 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3723 }
3724}
3725
Christopher Wiley1057cd72013-02-28 15:21:29 -08003726TEST_F(ManagerTest, VerifyDestination) {
3727 const string kFakeCertificate("fake cert");
3728 const string kFakePublicKey("fake public key");
3729 const string kFakeNonce("fake public key");
3730 const string kFakeSignedData("fake signed data");
3731 const string kFakeUdn("fake udn");
3732 const char kSSIDStr[] = "fake ssid";
3733 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003734 const string kConfiguredSSID("AConfiguredDestination");
3735 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3736 kConfiguredSSID.end());
3737 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003738 const string kFakeData("muffin man");
3739 scoped_refptr<MockWiFiService> mock_destination(
3740 new NiceMock<MockWiFiService>(control_interface(),
3741 dispatcher(),
3742 metrics(),
3743 manager(),
3744 wifi_provider_,
3745 kSSID,
3746 "",
3747 "none",
3748 false));
3749 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003750 // Making the service look online will let service lookup in
3751 // VerifyDestinatoin succeed.
3752 EXPECT_CALL(*mock_destination.get(), IsConnected())
3753 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003754 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003755
3756 // Lead off by verifying that the basic VerifyDestination flow works.
3757 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003758 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003759 ResultBoolCallback passed_down_callback;
3760 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3761 kFakePublicKey,
3762 kFakeNonce,
3763 kFakeSignedData,
3764 kFakeUdn,
3765 kSSID,
3766 _,
3767 _,
3768 _))
3769 .Times(1)
3770 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3771 // Ask the manager to verify the current destination. This should look
3772 // up our previously registered service, and pass some metadata about
3773 // that service down to the CryptoUtilProxy to verify.
3774 Error error(Error::kOperationInitiated);
3775 ResultBoolCallback cb = Bind(
3776 &DestinationVerificationTest::ResultBoolCallbackStub,
3777 dv_test.AsWeakPtr());
3778 manager()->VerifyDestination(kFakeCertificate,
3779 kFakePublicKey,
3780 kFakeNonce,
3781 kFakeSignedData,
3782 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003783 // Ask to be verified against that service.
3784 "", "",
3785 cb,
3786 &error);
3787 // We assert here, because if the operation is not ongoing, it is
3788 // inconsistent with shim behavior to call the callback anyway.
3789 ASSERT_TRUE(error.IsOngoing());
3790 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3791 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3792 // Call the callback passed into the CryptoUtilProxy, which
3793 // should find its way into the callback passed into the manager.
3794 // In real code, that callback passed into the manager is from the
3795 // DBus adaptor.
3796 Error e;
3797 passed_down_callback.Run(e, true);
3798 Mock::VerifyAndClearExpectations(&dv_test);
3799 }
3800
Christopher Wiley1057cd72013-02-28 15:21:29 -08003801 // Now for a slightly more complex variant. When we encrypt data,
3802 // we do the same verification step but monkey with the callback to
3803 // link ourselves to an encrypt step afterward.
3804 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003805 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003806 ResultBoolCallback passed_down_callback;
3807 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3808 kFakePublicKey,
3809 kFakeNonce,
3810 kFakeSignedData,
3811 kFakeUdn,
3812 kSSID,
3813 _,
3814 _,
3815 _))
3816 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3817
3818 Error error(Error::kOperationInitiated);
3819 ResultStringCallback cb = Bind(
3820 &DestinationVerificationTest::ResultStringCallbackStub,
3821 dv_test.AsWeakPtr());
3822 manager()->VerifyAndEncryptData(kFakeCertificate,
3823 kFakePublicKey,
3824 kFakeNonce,
3825 kFakeSignedData,
3826 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003827 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003828 kFakeData,
3829 cb,
3830 &error);
3831 ASSERT_TRUE(error.IsOngoing());
3832 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3833 // Now, if we call that passed down callback, we should see encrypt being
3834 // called.
3835 ResultStringCallback second_passed_down_callback;
3836 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3837 kFakeData,
3838 _,
3839 _))
3840 .Times(1)
3841 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3842 Return(true)));
3843 Error e;
3844 passed_down_callback.Run(e, true);
3845 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3846 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3847 // And if we call the second passed down callback, we should see the
3848 // original function we passed down to VerifyDestination getting called.
3849 e.Reset();
3850 second_passed_down_callback.Run(e, "");
3851 Mock::VerifyAndClearExpectations(&dv_test);
3852 }
3853
3854 // If verification fails on the way to trying to encrypt, we should ditch
3855 // without calling encrypt at all.
3856 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003857 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003858 ResultBoolCallback passed_down_callback;
3859 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3860 kFakePublicKey,
3861 kFakeNonce,
3862 kFakeSignedData,
3863 kFakeUdn,
3864 kSSID,
3865 _,
3866 _,
3867 _))
3868 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3869
3870 Error error(Error::kOperationInitiated);
3871 ResultStringCallback cb = Bind(
3872 &DestinationVerificationTest::ResultStringCallbackStub,
3873 dv_test.AsWeakPtr());
3874 manager()->VerifyAndEncryptData(kFakeCertificate,
3875 kFakePublicKey,
3876 kFakeNonce,
3877 kFakeSignedData,
3878 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003879 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003880 kFakeData,
3881 cb,
3882 &error);
3883 ASSERT_TRUE(error.IsOngoing());
3884 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3885 Error e(Error::kOperationFailed);
3886 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3887 // Although we're ditching, this callback is what cleans up the pending
3888 // DBus call.
3889 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3890 passed_down_callback.Run(e, false);
3891 Mock::VerifyAndClearExpectations(&dv_test);
3892 }
3893}
3894
Paul Stewartd2e1c362013-03-03 19:06:07 -08003895TEST_F(ManagerTest, IsProfileBefore) {
3896 scoped_refptr<MockProfile> profile0(
3897 new NiceMock<MockProfile>(
3898 control_interface(), metrics(), manager(), ""));
3899 scoped_refptr<MockProfile> profile1(
3900 new NiceMock<MockProfile>(
3901 control_interface(), metrics(), manager(), ""));
3902
3903 AdoptProfile(manager(), profile0);
3904 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3905 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3906 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3907
3908 // A few abnormal cases, but it's good to track their behavior.
3909 scoped_refptr<MockProfile> profile2(
3910 new NiceMock<MockProfile>(
3911 control_interface(), metrics(), manager(), ""));
3912 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3913 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3914 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3915 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3916}
3917
Paul Stewart967eaeb2013-04-25 19:53:07 -07003918TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
3919 MockStore storage0;
3920 MockStore storage1;
3921 MockStore storage2;
3922
3923 scoped_refptr<MockProfile> profile0(
3924 new NiceMock<MockProfile>(
3925 control_interface(), metrics(), manager(), ""));
3926 scoped_refptr<MockProfile> profile1(
3927 new NiceMock<MockProfile>(
3928 control_interface(), metrics(), manager(), ""));
3929 scoped_refptr<MockProfile> profile2(
3930 new NiceMock<MockProfile>(
3931 control_interface(), metrics(), manager(), ""));
3932
3933 AdoptProfile(manager(), profile0);
3934 AdoptProfile(manager(), profile1);
3935 AdoptProfile(manager(), profile2);
3936
3937 scoped_refptr<MockService> service(
3938 new NiceMock<MockService>(control_interface(),
3939 dispatcher(),
3940 metrics(),
3941 manager()));
3942
3943 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
3944 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
3945 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
3946
3947 const string kEntry0("aluminum_crutch");
3948 const string kEntry2("rehashed_faces");
3949
3950 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
3951 .WillOnce(Return(kEntry0));
3952 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
3953 .WillOnce(Return(""));
3954 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
3955 .WillOnce(Return(kEntry2));
3956
3957 const string kProfileRpc0("service_station");
3958 const string kProfileRpc2("crystal_tiaras");
3959
3960 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
3961 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
3962 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
3963
3964 map<string, string> entries =
3965 manager()->GetLoadableProfileEntriesForService(service);
3966 EXPECT_EQ(2, entries.size());
3967 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
3968 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
3969 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
3970 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
3971}
3972
mukesh agrawal00752532013-05-03 15:46:55 -07003973TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07003974 // We need a real glib here, so that profiles are persisted.
3975 GLib glib;
3976 ScopedTempDir temp_dir;
3977 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
3978 Manager manager(control_interface(),
3979 dispatcher(),
3980 metrics(),
3981 &glib,
3982 run_path(),
3983 storage_path(),
3984 temp_dir.path().value());
3985 // Can't use |wifi_provider_|, because it's owned by the Manager
3986 // object in the fixture.
3987 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
3988 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07003989 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07003990 // Give manager a valid place to write the user profile list.
3991 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
3992
3993 // With no user profiles, the WiFiProvider should be called once
3994 // (for the default profile).
3995 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
3996 manager.InitializeProfiles();
3997 Mock::VerifyAndClearExpectations(wifi_provider);
3998
3999 // With |n| user profiles, the WiFiProvider should be called |n+1|
4000 // times. First, create 2 user profiles...
4001 const char kProfile0[] = "~user/profile0";
4002 const char kProfile1[] = "~user/profile1";
4003 string profile_rpc_path;
4004 Error error;
4005 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4006 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4007 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4008 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4009
4010 // ... then reset manager state ...
4011 manager.profiles_.clear();
4012
4013 // ...then check that the WiFiProvider is notified about all three
4014 // profiles (one default, two user).
4015 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4016 manager.InitializeProfiles();
4017 Mock::VerifyAndClearExpectations(wifi_provider);
4018}
4019
mukesh agrawal00752532013-05-03 15:46:55 -07004020TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4021 // We need a real glib here, so that profiles are persisted.
4022 GLib glib;
4023 ScopedTempDir temp_dir;
4024 scoped_ptr<Manager> manager;
4025 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4026
4027 // Instantiate a Manager with empty persistent storage. Check that
4028 // defaults are set.
4029 //
4030 // Note that we use the same directory for default and user profiles.
4031 // This doesn't affect the test results, because we don't push a
4032 // user profile.
4033 manager.reset(new Manager(control_interface(),
4034 dispatcher(),
4035 metrics(),
4036 &glib,
4037 run_path(),
4038 temp_dir.path().value(),
4039 temp_dir.path().value()));
4040 manager->InitializeProfiles();
4041 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4042 manager->props_.check_portal_list);
4043 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4044 manager->props_.ignored_dns_search_paths);
4045 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4046 manager->props_.link_monitor_technologies);
4047 EXPECT_EQ(PortalDetector::kDefaultURL,
4048 manager->props_.portal_url);
4049 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4050 manager->props_.portal_check_interval_seconds);
4051
4052 // Change one of the settings.
4053 static const string kCustomCheckPortalList = "fiber0";
4054 Error error;
4055 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4056 manager->profiles_[0]->Save();
4057
4058 // Instantiate a new manager. It should have our settings for
4059 // check_portal_list, rather than the default.
4060 manager.reset(new Manager(control_interface(),
4061 dispatcher(),
4062 metrics(),
4063 &glib,
4064 run_path(),
4065 temp_dir.path().value(),
4066 temp_dir.path().value()));
4067 manager->InitializeProfiles();
4068 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4069
4070 // If we clear the persistent storage, we again get the default value.
4071 ASSERT_TRUE(temp_dir.Delete());
4072 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4073 manager.reset(new Manager(control_interface(),
4074 dispatcher(),
4075 metrics(),
4076 &glib,
4077 run_path(),
4078 temp_dir.path().value(),
4079 temp_dir.path().value()));
4080 manager->InitializeProfiles();
4081 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4082 manager->props_.check_portal_list);
4083}
4084
mukesh agrawalb94adde2013-08-22 18:17:26 -07004085TEST_F(ManagerTest, ProfileStackChangeLogging) {
4086 // We use a real glib here, since Manager and Profile don't provide an
4087 // easy way to mock out KeyFileStore.
4088 GLib glib;
4089 ScopedTempDir temp_dir;
4090 scoped_ptr<Manager> manager;
4091 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4092 manager.reset(new Manager(control_interface(),
4093 dispatcher(),
4094 metrics(),
4095 &glib,
4096 run_path(),
4097 temp_dir.path().value(),
4098 temp_dir.path().value()));
4099
4100 ScopedMockLog log;
4101 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4102 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4103 manager->InitializeProfiles();
4104
4105 const char kProfile0[] = "~user/profile0";
4106 const char kProfile1[] = "~user/profile1";
4107 const char kProfile2[] = "~user/profile2";
4108 TestCreateProfile(manager.get(), kProfile0);
4109 TestCreateProfile(manager.get(), kProfile1);
4110 TestCreateProfile(manager.get(), kProfile2);
4111
4112 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4113 TestPushProfile(manager.get(), kProfile0);
4114
4115 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4116 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4117
4118 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4119 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4120
4121 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4122 TestPopProfile(manager.get(), kProfile2);
4123
4124 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4125 TestPopAnyProfile(manager.get());
4126
4127 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4128 TestPopAllUserProfiles(manager.get());
4129}
4130
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004131// Custom property setters should return false, and make no changes, if
4132// the new value is the same as the old value.
4133TEST_F(ManagerTest, CustomSetterNoopChange) {
4134 // SetCheckPortalList
4135 {
4136 static const string kCheckPortalList = "weird-device,weirder-device";
4137 Error error;
4138 // Set to known value.
4139 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4140 EXPECT_TRUE(error.IsSuccess());
4141 // Set to same value.
4142 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4143 EXPECT_TRUE(error.IsSuccess());
4144 }
4145
4146 // SetIgnoredDNSSearchPaths
4147 {
4148 NiceMock<MockResolver> resolver;
4149 static const string kIgnoredPaths = "example.com,example.org";
4150 Error error;
4151 SetResolver(&resolver);
4152 // Set to known value.
4153 EXPECT_CALL(resolver, set_ignored_search_list(_));
4154 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4155 EXPECT_TRUE(error.IsSuccess());
4156 Mock::VerifyAndClearExpectations(&resolver);
4157 // Set to same value.
4158 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4159 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4160 EXPECT_TRUE(error.IsSuccess());
4161 Mock::VerifyAndClearExpectations(&resolver);
4162 }
4163}
4164
Paul Stewart7de7e022013-08-28 09:42:50 -07004165TEST_F(ManagerTest, GeoLocation) {
4166 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4167
4168 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4169 dispatcher(),
4170 metrics(),
4171 manager(),
4172 "null",
4173 "addr",
4174 0));
4175
4176 // Manager should ignore gelocation info from technologies it does not know.
4177 EXPECT_CALL(*device, technology())
4178 .Times(AtLeast(1))
4179 .WillRepeatedly(Return(Technology::kEthernet));
4180 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4181 manager()->OnDeviceGeolocationInfoUpdated(device);
4182 Mock::VerifyAndClearExpectations(device);
4183 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4184
4185 // Manager should add WiFi geolocation info.
4186 EXPECT_CALL(*device, technology())
4187 .Times(AtLeast(1))
4188 .WillRepeatedly(Return(Technology::kWifi));
4189 EXPECT_CALL(*device, GetGeolocationObjects())
4190 .WillOnce(Return(vector<GeolocationInfo>()));
4191 manager()->OnDeviceGeolocationInfoUpdated(device);
4192 Mock::VerifyAndClearExpectations(device);
4193 auto location_infos = manager()->GetNetworksForGeolocation();
4194 EXPECT_EQ(1, location_infos.size());
4195 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4196
4197 // Manager should inclusively add cellular info.
4198 EXPECT_CALL(*device, technology())
4199 .Times(AtLeast(1))
4200 .WillRepeatedly(Return(Technology::kCellular));
4201 EXPECT_CALL(*device, GetGeolocationObjects())
4202 .WillOnce(Return(vector<GeolocationInfo>()));
4203 manager()->OnDeviceGeolocationInfoUpdated(device);
4204 location_infos = manager()->GetNetworksForGeolocation();
4205 EXPECT_EQ(2, location_infos.size());
4206 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4207 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4208}
4209
Chris Masone9be4a9d2011-05-16 15:44:09 -07004210} // namespace shill