blob: 1ba75f7c0a81b08da53819d04cefad32db086a42 [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_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700147 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700148 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700149
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100150 void SetMetrics(Metrics *metrics) {
151 manager()->set_metrics(metrics);
152 }
153
Paul Stewartfdd16072011-09-16 12:41:35 -0700154 bool IsDeviceRegistered(const DeviceRefPtr &device,
155 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700156 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700157 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700158 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700159 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700160 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700161
Paul Stewarta849a3d2011-11-03 05:54:09 -0700162 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
163 manager->profiles_.push_back(profile);
164 }
165
Paul Stewart63864b62012-11-07 15:10:55 -0800166 void SetRunning(bool running) {
167 manager()->running_ = running;
168 }
169
Paul Stewart75225512012-01-26 22:51:33 -0800170 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
171 return manager->ephemeral_profile_;
172 }
173
Paul Stewart307c2502013-03-23 12:32:10 -0700174 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
175 return manager->profiles_;
176 }
177
Chris Masone6515aab2011-10-12 16:19:09 -0700178 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
179 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700180 FilePath final_path(storage_path());
181 final_path = final_path.Append("test.profile");
182 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
183 storage->set_path(final_path);
184 if (!storage->Open())
185 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800186 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800187 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800188 manager,
189 id,
190 "",
191 false));
192 profile->set_storage(storage.release()); // Passes ownership of "storage".
193 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700194 }
195
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700196 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
197 const string &profile_identifier,
198 const string &service_name) {
199 GLib glib;
200 KeyFileStore store(&glib);
201 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
202 return store.Open() &&
203 store.SetString(service_name, "rather", "irrelevant") &&
204 store.Close();
205 }
206
207 Error::Type TestCreateProfile(Manager *manager, const string &name) {
208 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800209 string path;
210 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700211 return error.type();
212 }
213
214 Error::Type TestPopAnyProfile(Manager *manager) {
215 Error error;
216 manager->PopAnyProfile(&error);
217 return error.type();
218 }
219
Paul Stewart307c2502013-03-23 12:32:10 -0700220 Error::Type TestPopAllUserProfiles(Manager *manager) {
221 Error error;
222 manager->PopAllUserProfiles(&error);
223 return error.type();
224 }
225
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700226 Error::Type TestPopProfile(Manager *manager, const string &name) {
227 Error error;
228 manager->PopProfile(name, &error);
229 return error.type();
230 }
231
232 Error::Type TestPushProfile(Manager *manager, const string &name) {
233 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800234 string path;
235 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700236 return error.type();
237 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000238
Paul Stewartf3eced92013-04-17 12:18:22 -0700239 Error::Type TestInsertUserProfile(Manager *manager,
240 const string &name,
241 const string &user_hash) {
242 Error error;
243 string path;
244 manager->InsertUserProfile(name, user_hash, &path, &error);
245 return error.type();
246 }
247
Paul Stewartd2e1c362013-03-03 19:06:07 -0800248 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
249 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700250 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800251 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800252 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200253 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700254 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800255 return profile;
256 }
257
258 void AddMockProfileToManager(Manager *manager) {
259 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700260 }
261
Paul Stewartdfa46052012-06-26 09:44:14 -0700262 void CompleteServiceSort() {
263 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
264 dispatcher()->DispatchPendingEvents();
265 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
266 }
267
Paul Stewart03e29f72013-09-26 00:49:48 -0700268 void RefreshConnectionState() {
269 manager()->RefreshConnectionState();
270 }
271
Paul Stewart49739c02012-08-08 17:24:03 -0700272 RpcIdentifier GetDefaultServiceRpcIdentifier() {
273 return manager()->GetDefaultServiceRpcIdentifier(NULL);
274 }
275
Paul Stewart4d5efb72012-09-17 12:24:34 -0700276 void SetResolver(Resolver *resolver) {
277 manager()->resolver_ = resolver;
278 }
279
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700280 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
281 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
282 }
283
284 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
285 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700286 }
287
288 const string &GetIgnoredDNSSearchPaths() {
289 return manager()->props_.ignored_dns_search_paths;
290 }
291
Paul Stewartd2e1c362013-03-03 19:06:07 -0800292 WiFiServiceRefPtr ReleaseTempMockService() {
293 // Take a reference to hold during this function.
294 WiFiServiceRefPtr temp_service = temp_mock_service_;
295 temp_mock_service_ = NULL;
296 return temp_service;
297 }
298
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700299 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000300 typedef scoped_refptr<MockService> MockServiceRefPtr;
301
Darin Petkova5e07ef2012-07-09 14:27:57 +0200302 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
303 public:
304 ServiceWatcher() {}
305 virtual ~ServiceWatcher() {}
306
307 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
308
309 private:
310 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
311 };
312
Darin Petkovca621542012-07-25 14:25:56 +0200313 class TestProxyFactory : public ProxyFactory {
314 public:
315 TestProxyFactory() {}
316
317 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
318 PowerManagerProxyDelegate */*delegate*/) {
319 return NULL;
320 }
321
322 private:
323 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
324 };
325
Darin Petkov3ec55342012-09-28 14:04:44 +0200326 class TerminationActionTest :
327 public base::SupportsWeakPtr<TerminationActionTest> {
328 public:
329 static const char kActionName[];
330
331 TerminationActionTest() : manager_(NULL) {}
332 virtual ~TerminationActionTest() {}
333
334 MOCK_METHOD1(Done, void(const Error &error));
335
336 void Action() {
337 manager_->TerminationActionComplete("action");
338 }
339
340 void set_manager(Manager *manager) { manager_ = manager; }
341
342 private:
343 Manager *manager_;
344 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
345 };
346
Christopher Wiley1057cd72013-02-28 15:21:29 -0800347 class DestinationVerificationTest :
348 public base::SupportsWeakPtr<DestinationVerificationTest> {
349 public:
350 DestinationVerificationTest() {}
351 virtual ~DestinationVerificationTest() {}
352
353 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
354 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
355 const string &value));
356 private:
357 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
358 };
359
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700360 class DisableTechnologyReplyHandler :
361 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
362 public:
363 DisableTechnologyReplyHandler() {}
364 virtual ~DisableTechnologyReplyHandler() {}
365
366 MOCK_METHOD1(ReportResult, void(const Error &));
367
368 private:
369 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
370 };
371
Darin Petkovca621542012-07-25 14:25:56 +0200372 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
373 power_manager_->power_state_ = state;
374 }
375
376 void SetPowerManager() {
377 manager()->set_power_manager(power_manager_.release());
378 }
379
Darin Petkov3ec55342012-09-28 14:04:44 +0200380 HookTable *GetTerminationActions() {
381 return &manager()->termination_actions_;
382 }
383
Darin Petkovca621542012-07-25 14:25:56 +0200384 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
385 manager()->OnPowerStateChanged(state);
386 }
387
Daniel Erat0818cca2012-12-14 10:16:21 -0800388 void OnSuspendImminent(int suspend_id) {
389 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200390 }
391
Daniel Erat0818cca2012-12-14 10:16:21 -0800392 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
393 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200394 }
395
Paul Stewartbfb82552012-10-24 16:48:48 -0700396 vector<string> EnumerateAvailableServices() {
397 return manager()->EnumerateAvailableServices(NULL);
398 }
399
400 vector<string> EnumerateWatchedServices() {
401 return manager()->EnumerateWatchedServices(NULL);
402 }
403
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000404 MockServiceRefPtr MakeAutoConnectableService() {
405 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
406 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800407 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000408 manager());
Paul Stewart2da34c02013-10-17 15:28:56 -0700409 service->EnableAndRetainAutoConnect();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700410 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000411 return service;
412 }
413
Paul Stewart35eff132013-04-12 12:08:40 -0700414 void SetEapProviderService(const ServiceRefPtr &service) {
415 ethernet_eap_provider_->set_service(service);
416 }
417
Darin Petkovca621542012-07-25 14:25:56 +0200418 TestProxyFactory proxy_factory_;
419 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700420 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800421 scoped_ptr<MockDeviceInfo> device_info_;
422
Paul Stewartd2e1c362013-03-03 19:06:07 -0800423 // This service is held for the manager, and given ownership in a mock
424 // function. This ensures that when the Manager takes ownership, there
425 // is only one reference left.
426 scoped_refptr<MockWiFiService> temp_mock_service_;
427
Paul Stewart3c504012013-01-17 17:49:58 -0800428 // These pointers are owned by the manager, and only tracked here for
429 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800430 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700431 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800432 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800433 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700434};
435
Darin Petkov3ec55342012-09-28 14:04:44 +0200436const char ManagerTest::TerminationActionTest::kActionName[] = "action";
437
Paul Stewart22aa71b2011-09-16 12:15:11 -0700438bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700439 if (!manager()->sort_services_task_.IsCancelled()) {
440 manager()->SortServicesTask();
441 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700442 return (svc0.get() == manager()->services_[0].get() &&
443 svc1.get() == manager()->services_[1].get());
444}
445
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700446void SetErrorPermissionDenied(Error *error) {
447 error->Populate(Error::kPermissionDenied);
448}
449
Arman Uguray2f352e62013-08-28 19:12:53 -0700450void SetErrorSuccess(Error *error) {
451 error->Reset();
452}
453
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700454TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700455 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700456 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700457}
458
Chris Masone9be4a9d2011-05-16 15:44:09 -0700459TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700460 ON_CALL(*mock_devices_[0].get(), technology())
461 .WillByDefault(Return(Technology::kEthernet));
462 ON_CALL(*mock_devices_[1].get(), technology())
463 .WillByDefault(Return(Technology::kWifi));
464 ON_CALL(*mock_devices_[2].get(), technology())
465 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700466
Paul Stewart22aa71b2011-09-16 12:15:11 -0700467 manager()->RegisterDevice(mock_devices_[0]);
468 manager()->RegisterDevice(mock_devices_[1]);
469 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700470
Paul Stewart22aa71b2011-09-16 12:15:11 -0700471 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
472 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
473 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700474}
475
Paul Stewarta41e38d2011-11-11 07:47:29 -0800476TEST_F(ManagerTest, DeviceRegistrationAndStart) {
477 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500478 mock_devices_[0]->enabled_persistent_ = true;
479 mock_devices_[1]->enabled_persistent_ = false;
480 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800481 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500482 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800483 .Times(0);
484 manager()->RegisterDevice(mock_devices_[0]);
485 manager()->RegisterDevice(mock_devices_[1]);
486}
487
488TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800489 MockProfile *profile =
490 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800491 DeviceRefPtr device_ref(mock_devices_[0].get());
492 AdoptProfile(manager(), profile); // Passes ownership.
493 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200494 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800495 manager()->RegisterDevice(mock_devices_[0]);
496}
497
Chris Masone9be4a9d2011-05-16 15:44:09 -0700498TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700499 ON_CALL(*mock_devices_[0].get(), technology())
500 .WillByDefault(Return(Technology::kEthernet));
501 ON_CALL(*mock_devices_[1].get(), technology())
502 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700503
Gaurav Shah435de2c2011-11-17 19:01:07 -0800504 manager()->RegisterDevice(mock_devices_[0]);
505 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700506
Paul Stewart22aa71b2011-09-16 12:15:11 -0700507 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
508 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700509
Thieu Le5133b712013-02-19 14:47:21 -0800510 MockProfile *profile =
511 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700512 AdoptProfile(manager(), profile); // Passes ownership.
513
Eric Shienbrood9a245532012-03-07 14:20:39 -0500514 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700515 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800516 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700517 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700518
Eric Shienbrood9a245532012-03-07 14:20:39 -0500519 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700520 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800521 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700522 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700523}
524
525TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700526 // It's much easier and safer to use a real GLib for this test.
527 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700528 Manager manager(control_interface(),
529 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800530 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700531 &glib,
532 run_path(),
533 storage_path(),
534 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700535 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
536 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700537 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700538
Chris Masone9be4a9d2011-05-16 15:44:09 -0700539 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700540 new NiceMock<MockService>(control_interface(),
541 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800542 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700543 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700544 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700545 new NiceMock<MockService>(control_interface(),
546 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800547 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700548 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700549
Darin Petkov457728b2013-01-09 09:49:08 +0100550 string service1_name(mock_service->unique_name());
551 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700552
553 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
554 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700555 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700556 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700557 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700558 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700559 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700560
Chris Masone9d779932011-08-25 16:33:41 -0700561 manager.RegisterService(mock_service);
562 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700563
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800564 Error error;
565 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700566 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700567 EXPECT_EQ(2, ids.size());
568 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
569 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700570
Chris Masone9d779932011-08-25 16:33:41 -0700571 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
572 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
573
574 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700575}
576
Chris Masone6515aab2011-10-12 16:19:09 -0700577TEST_F(ManagerTest, RegisterKnownService) {
578 // It's much easier and safer to use a real GLib for this test.
579 GLib glib;
580 Manager manager(control_interface(),
581 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800582 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700583 &glib,
584 run_path(),
585 storage_path(),
586 string());
587 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
588 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700589 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700590 {
591 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
592 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800593 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700594 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700595 ASSERT_TRUE(profile->AdoptService(service1));
596 ASSERT_TRUE(profile->ContainsService(service1));
597 } // Force destruction of service1.
598
599 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
600 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800601 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700602 &manager));
603 manager.RegisterService(service2);
604 EXPECT_EQ(service2->profile().get(), profile.get());
605 manager.Stop();
606}
607
608TEST_F(ManagerTest, RegisterUnknownService) {
609 // It's much easier and safer to use a real GLib for this test.
610 GLib glib;
611 Manager manager(control_interface(),
612 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800613 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700614 &glib,
615 run_path(),
616 storage_path(),
617 string());
618 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
619 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700620 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700621 {
622 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
623 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800624 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700625 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700626 ASSERT_TRUE(profile->AdoptService(service1));
627 ASSERT_TRUE(profile->ContainsService(service1));
628 } // Force destruction of service1.
629 scoped_refptr<MockService> mock_service2(
630 new NiceMock<MockService>(control_interface(),
631 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800632 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700633 &manager));
634 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100635 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700636 manager.RegisterService(mock_service2);
637 EXPECT_NE(mock_service2->profile().get(), profile.get());
638 manager.Stop();
639}
640
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000641TEST_F(ManagerTest, DeregisterUnregisteredService) {
642 // WiFi assumes that it can deregister a service that is not
643 // registered. (E.g. a hidden service can be deregistered when it
644 // loses its last endpoint, and again when WiFi is Stop()-ed.)
645 //
646 // So test that doing so doesn't cause a crash.
647 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
648 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800649 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000650 manager());
651 manager()->DeregisterService(service);
652}
653
Chris Masonea8a2c252011-06-27 22:16:30 -0700654TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700655 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700656 map<string, ::DBus::Variant> props;
657 Error error(Error::kInvalidProperty, "");
658 {
659 ::DBus::Error dbus_error;
660 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700661 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700662 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700663 expected,
664 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700665 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700666 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
667 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700668 expected);
669 }
670 {
671 ::DBus::Error dbus_error;
672 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700673 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700674 expected,
675 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700676 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700677 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
678 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700679 expected);
680 }
681}
682
Chris Masone3c3f6a12011-07-01 10:01:41 -0700683TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700684 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800685 manager()->RegisterDevice(mock_devices_[0]);
686 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700687 {
688 map<string, ::DBus::Variant> props;
689 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700690 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700691 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700692 vector < ::DBus::Path> devices =
Ben Chan923a5022013-09-20 11:23:23 -0700693 props[kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700694 EXPECT_EQ(2, devices.size());
695 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700696}
697
mukesh agrawal2366eed2012-03-20 18:21:50 -0700698TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700699 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700700 map<string, ::DBus::Variant> props;
701 ::DBus::Error dbus_error;
702 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
703 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700704 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700705 ASSERT_FALSE(prop == props.end());
706 const ::DBus::Variant &variant = prop->second;
707 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
708}
709
Chris Masone6791a432011-07-12 13:23:19 -0700710TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700711 Manager manager(control_interface(),
712 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800713 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700714 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700715 run_path(),
716 storage_path(),
717 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700718 scoped_refptr<MockService> s2(new MockService(control_interface(),
719 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800720 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700721 &manager));
722 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700723 {
Chris Masone6515aab2011-10-12 16:19:09 -0700724 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700725 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800726 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700727 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700728 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700729 .WillRepeatedly(Return(true));
730 EXPECT_CALL(*storage, Flush())
731 .Times(AnyNumber())
732 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700733 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700734 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700735 }
Chris Masone6515aab2011-10-12 16:19:09 -0700736 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800737 ProfileRefPtr profile(
738 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700739 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700740
Chris Masone6515aab2011-10-12 16:19:09 -0700741 // Now, move the Service |s2| to another profile.
742 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
743 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700744
745 // Force destruction of the original Profile, to ensure that the Service
746 // is kept alive and populated with data.
747 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700748 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700749 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700750}
751
Paul Stewart7f61e522012-03-22 11:13:45 -0700752TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
753 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800754 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700755 const string kProfileName("profile0");
756 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
757 .WillRepeatedly(Return(kProfileName));
758 AdoptProfile(manager(), mock_profile);
759
760 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
761 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
762 EXPECT_EQ(mock_profile.get(), profile.get());
763}
764
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800765TEST_F(ManagerTest, SetProfileForService) {
766 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800767 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800768 string profile_name0("profile0");
769 EXPECT_CALL(*profile0, GetRpcIdentifier())
770 .WillRepeatedly(Return(profile_name0));
771 AdoptProfile(manager(), profile0);
772 scoped_refptr<MockService> service(new MockService(control_interface(),
773 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800774 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800775 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700776 EXPECT_FALSE(manager()->HasService(service));
777 {
778 Error error;
779 EXPECT_CALL(*profile0, AdoptService(_))
780 .WillOnce(Return(true));
781 // Expect that setting the profile of a service that does not already
782 // have one assigned does not cause a crash.
783 manager()->SetProfileForService(service, "profile0", &error);
784 EXPECT_TRUE(error.IsSuccess());
785 }
786
787 // The service should be registered as a side-effect of the profile being
788 // set for this service.
789 EXPECT_TRUE(manager()->HasService(service));
790
791 // Since we have mocked Profile::AdoptServie() above, the service's
792 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800793 service->set_profile(profile0);
794
795 {
796 Error error;
797 manager()->SetProfileForService(service, "foo", &error);
798 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700799 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800800 }
801
802 {
803 Error error;
804 manager()->SetProfileForService(service, profile_name0, &error);
805 EXPECT_EQ(Error::kInvalidArguments, error.type());
806 EXPECT_EQ("Service is already connected to this profile", error.message());
807 }
808
809 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800810 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800811 string profile_name1("profile1");
812 EXPECT_CALL(*profile1, GetRpcIdentifier())
813 .WillRepeatedly(Return(profile_name1));
814 AdoptProfile(manager(), profile1);
815
816 {
817 Error error;
818 EXPECT_CALL(*profile1, AdoptService(_))
819 .WillOnce(Return(true));
820 EXPECT_CALL(*profile0, AbandonService(_))
821 .WillOnce(Return(true));
822 manager()->SetProfileForService(service, profile_name1, &error);
823 EXPECT_TRUE(error.IsSuccess());
824 }
825}
826
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700827TEST_F(ManagerTest, CreateProfile) {
828 // It's much easier to use real Glib here since we want the storage
829 // side-effects.
830 GLib glib;
831 ScopedTempDir temp_dir;
832 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
833
834 Manager manager(control_interface(),
835 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800836 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700837 &glib,
838 run_path(),
839 storage_path(),
840 temp_dir.path().value());
841
842 // Invalid name should be rejected.
843 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
844
Paul Stewartd0a3b812012-03-28 22:48:22 -0700845 // A profile with invalid characters in it should similarly be rejected.
846 EXPECT_EQ(Error::kInvalidArguments,
847 TestCreateProfile(&manager, "valid_profile"));
848
849 // We should be able to create a machine profile.
850 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700851
Gary Morainb672d352012-04-25 09:19:06 -0700852 // We should succeed in creating a valid user profile. Verify the returned
853 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700854 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700855 {
856 Error error;
857 string path;
858 manager.CreateProfile(kProfile, &path, &error);
859 EXPECT_EQ(Error::kSuccess, error.type());
860 EXPECT_EQ("/profile_rpc", path);
861 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700862
863 // We should fail in creating it a second time (already exists).
864 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
865}
866
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700867// We receive PopProfile when a user logs out, and it should always trigger a
868// MemoryLog Clear() call.
869TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
870 GLib glib;
871 ScopedTempDir temp_dir;
872 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
873 Manager manager(control_interface(),
874 dispatcher(),
875 metrics(),
876 &glib,
877 run_path(),
878 storage_path(),
879 temp_dir.path().value());
880 const char kProfile0[] = "~user/profile0";
881 const char kPurgedMessage[] = "This message should be purged";
882 // Create a profile and push it on the stack, leave one uncreated
883 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
884 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
885
886 // Popping a profile which isn't on top should still clear the log.
887 LOG(INFO) << kPurgedMessage;
888 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
889 kPurgedMessage));
890 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
891 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
892 kPurgedMessage));
893
894 // Popping an invalid profile name should do the same thing.
895 LOG(INFO) << kPurgedMessage;
896 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
897 kPurgedMessage));
898 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
899 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
900 kPurgedMessage));
901
902 // Successful pops also purge the message log.
903 LOG(INFO) << kPurgedMessage;
904 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
905 kPurgedMessage));
906 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
907 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
908 kPurgedMessage));
909}
910
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700911TEST_F(ManagerTest, PushPopProfile) {
912 // It's much easier to use real Glib in creating a Manager for this
913 // test here since we want the storage side-effects.
914 GLib glib;
915 ScopedTempDir temp_dir;
916 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
917 Manager manager(control_interface(),
918 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800919 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700920 &glib,
921 run_path(),
922 storage_path(),
923 temp_dir.path().value());
924
925 // Pushing an invalid profile should fail.
926 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
927
Paul Stewartd0a3b812012-03-28 22:48:22 -0700928 // Pushing a default profile that does not exist should fail.
929 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700930
931 const char kProfile0[] = "~user/profile0";
932 const char kProfile1[] = "~user/profile1";
933
934 // Create a couple of profiles.
935 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
936 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
937
938 // Push these profiles on the stack.
939 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
940 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
941
942 // Pushing a profile a second time should fail.
943 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
944 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
945
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800946 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700947 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800948 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700949
950 // Make sure a profile name that doesn't exist fails.
951 const char kProfile2Id[] = "profile2";
952 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
953 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
954
955 // Create a new service, with a specific storage name.
956 scoped_refptr<MockService> service(
957 new NiceMock<MockService>(control_interface(),
958 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800959 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700960 &manager));
961 const char kServiceName[] = "service_storage_name";
962 EXPECT_CALL(*service.get(), GetStorageIdentifier())
963 .WillRepeatedly(Return(kServiceName));
964 EXPECT_CALL(*service.get(), Load(_))
965 .WillRepeatedly(Return(true));
966
967 // Add this service to the manager -- it should end up in the ephemeral
968 // profile.
969 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800970 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700971
972 // Create storage for a profile that contains the service storage name.
973 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
974 kServiceName));
975
976 // When we push the profile, the service should move away from the
977 // ephemeral profile to this new profile since it has an entry for
978 // this service.
979 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800980 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700981 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
982
983 // Insert another profile that should supersede ownership of the service.
984 const char kProfile3Id[] = "profile3";
985 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
986 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
987 kServiceName));
988 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
989 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
990
991 // Popping an invalid profile name should fail.
992 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
993
994 // Popping an profile that is not at the top of the stack should fail.
995 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
996
997 // Popping the top profile should succeed.
998 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
999
1000 // Moreover the service should have switched profiles to profile 2.
1001 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
1002
1003 // Popping the top profile should succeed.
1004 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1005
1006 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -08001007 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001008
1009 // Pop the remaining two services off the stack.
1010 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1011 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1012
1013 // Next pop should fail with "stack is empty".
1014 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -07001015
1016 const char kMachineProfile0[] = "machineprofile0";
1017 const char kMachineProfile1[] = "machineprofile1";
1018 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1019 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1020
1021 // Should be able to push a machine profile.
1022 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1023
1024 // Should be able to push a user profile atop a machine profile.
1025 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1026
1027 // Pushing a system-wide profile on top of a user profile should fail.
1028 EXPECT_EQ(Error::kInvalidArguments,
1029 TestPushProfile(&manager, kMachineProfile1));
1030
1031 // However if we pop the user profile, we should be able stack another
1032 // machine profile on.
1033 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1034 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001035
1036 // Add two user profiles to the top of the stack.
1037 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1038 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
1039 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
1040 EXPECT_EQ(4, profiles.size());
1041
1042 // PopAllUserProfiles should remove both user profiles, leaving the two
1043 // machine profiles.
1044 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1045 EXPECT_EQ(2, profiles.size());
1046 EXPECT_TRUE(profiles[0]->GetUser().empty());
1047 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001048
1049 // Use InsertUserProfile() instead. Although a machine profile is valid
1050 // in this state, it cannot be added via InsertUserProfile.
1051 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1052 EXPECT_EQ(Error::kInvalidArguments,
1053 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1054 const char kUserHash0[] = "userhash0";
1055 const char kUserHash1[] = "userhash1";
1056 EXPECT_EQ(Error::kSuccess,
1057 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1058 EXPECT_EQ(Error::kSuccess,
1059 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1060 EXPECT_EQ(3, profiles.size());
1061 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1062 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001063}
1064
Paul Stewarte73d05c2012-03-29 16:26:05 -07001065TEST_F(ManagerTest, RemoveProfile) {
1066 // It's much easier to use real Glib in creating a Manager for this
1067 // test here since we want the storage side-effects.
1068 GLib glib;
1069 ScopedTempDir temp_dir;
1070 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1071 Manager manager(control_interface(),
1072 dispatcher(),
1073 metrics(),
1074 &glib,
1075 run_path(),
1076 storage_path(),
1077 temp_dir.path().value());
1078
1079 const char kProfile0[] = "profile0";
1080 FilePath profile_path(
1081 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1082
1083 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1084 ASSERT_TRUE(file_util::PathExists(profile_path));
1085
1086 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1087
1088 // Remove should fail since the profile is still on the stack.
1089 {
1090 Error error;
1091 manager.RemoveProfile(kProfile0, &error);
1092 EXPECT_EQ(Error::kInvalidArguments, error.type());
1093 }
1094
1095 // Profile path should still exist.
1096 EXPECT_TRUE(file_util::PathExists(profile_path));
1097
1098 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1099
1100 // This should succeed now that the profile is off the stack.
1101 {
1102 Error error;
1103 manager.RemoveProfile(kProfile0, &error);
1104 EXPECT_EQ(Error::kSuccess, error.type());
1105 }
1106
1107 // Profile path should no longer exist.
1108 EXPECT_FALSE(file_util::PathExists(profile_path));
1109
1110 // Another remove succeeds, due to a foible in file_util::Delete --
1111 // it is not an error to delete a file that does not exist.
1112 {
1113 Error error;
1114 manager.RemoveProfile(kProfile0, &error);
1115 EXPECT_EQ(Error::kSuccess, error.type());
1116 }
1117
1118 // Let's create an error case that will "work". Create a non-empty
1119 // directory in the place of the profile pathname.
1120 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
1121 {
1122 Error error;
1123 manager.RemoveProfile(kProfile0, &error);
1124 EXPECT_EQ(Error::kOperationFailed, error.type());
1125 }
1126}
1127
Paul Stewartd3d03882013-08-29 15:43:42 -07001128TEST_F(ManagerTest, RemoveService) {
1129 MockServiceRefPtr mock_service(
1130 new NiceMock<MockService>(control_interface(),
1131 dispatcher(),
1132 metrics(),
1133 manager()));
1134
1135 // Used in expectations which cannot accept a mock refptr.
1136 const ServiceRefPtr &service = mock_service;
1137
1138 manager()->RegisterService(service);
1139 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1140
1141 scoped_refptr<MockProfile> profile(
1142 new StrictMock<MockProfile>(
1143 control_interface(), metrics(), manager(), ""));
1144 AdoptProfile(manager(), profile);
1145
1146 // If service is ephemeral, it should be unloaded and left ephemeral.
1147 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1148 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1149 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1150 manager()->RemoveService(service);
1151 Mock::VerifyAndClearExpectations(mock_service);
1152 Mock::VerifyAndClearExpectations(profile);
1153 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1154 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1155
1156 // If service is not ephemeral and the Manager finds a profile to assign
1157 // the service to, the service should be re-parented. Note that since we
1158 // are using a MockProfile, ConfigureService() never actually changes the
1159 // Service's profile.
1160 service->set_profile(profile);
1161 EXPECT_CALL(*profile, AbandonService(service));
1162 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1163 EXPECT_CALL(*mock_service, Unload()).Times(0);
1164 manager()->RemoveService(service);
1165 Mock::VerifyAndClearExpectations(mock_service);
1166 Mock::VerifyAndClearExpectations(profile);
1167 EXPECT_TRUE(manager()->HasService(service));
1168 EXPECT_EQ(profile.get(), service->profile().get());
1169
1170 // If service becomes ephemeral since there is no profile to support it,
1171 // it should be unloaded.
1172 EXPECT_CALL(*profile, AbandonService(service));
1173 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1174 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1175 manager()->RemoveService(service);
1176 EXPECT_FALSE(manager()->HasService(service));
1177}
1178
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001179TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1180 // It's much easier to use real Glib in creating a Manager for this
1181 // test here since we want the storage side-effects.
1182 GLib glib;
1183 ScopedTempDir temp_dir;
1184 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1185 Manager manager(control_interface(),
1186 dispatcher(),
1187 metrics(),
1188 &glib,
1189 run_path(),
1190 storage_path(),
1191 temp_dir.path().value());
1192
1193 const char kProfile0[] = "profile0";
1194 FilePath profile_path(
1195 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1196
1197 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1198 ASSERT_TRUE(file_util::PathExists(profile_path));
1199 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1200
1201 // Ensure that even if the backing filestore is removed, we still can't
1202 // create a profile twice.
1203 ASSERT_TRUE(file_util::Delete(profile_path, false));
1204 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1205}
1206
Paul Stewart75225512012-01-26 22:51:33 -08001207// Use this matcher instead of passing RefPtrs directly into the arguments
1208// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1209// system teardown.
1210MATCHER_P(IsRefPtrTo, ref_address, "") {
1211 return arg.get() == ref_address;
1212}
1213
1214TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1215 MockServiceRefPtr s_not_in_profile(
1216 new NiceMock<MockService>(control_interface(),
1217 dispatcher(),
1218 metrics(),
1219 manager()));
1220 MockServiceRefPtr s_not_in_group(
1221 new NiceMock<MockService>(control_interface(),
1222 dispatcher(),
1223 metrics(),
1224 manager()));
1225 MockServiceRefPtr s_configure_fail(
1226 new NiceMock<MockService>(control_interface(),
1227 dispatcher(),
1228 metrics(),
1229 manager()));
1230 MockServiceRefPtr s_configure_succeed(
1231 new NiceMock<MockService>(control_interface(),
1232 dispatcher(),
1233 metrics(),
1234 manager()));
1235
1236 string entry_name("entry_name");
1237 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1238 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1239 .WillRepeatedly(Return("not_entry_name"));
1240 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1241 .WillRepeatedly(Return(entry_name));
1242 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1243 .WillRepeatedly(Return(entry_name));
1244
1245 manager()->RegisterService(s_not_in_profile);
1246 manager()->RegisterService(s_not_in_group);
1247 manager()->RegisterService(s_configure_fail);
1248 manager()->RegisterService(s_configure_succeed);
1249
1250 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001251 new StrictMock<MockProfile>(
1252 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001253 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001254 new StrictMock<MockProfile>(
1255 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001256
1257 s_not_in_group->set_profile(profile1);
1258 s_configure_fail->set_profile(profile1);
1259 s_configure_succeed->set_profile(profile1);
1260
1261 AdoptProfile(manager(), profile0);
1262 AdoptProfile(manager(), profile1);
1263
1264 // No services are a member of this profile.
1265 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1266
1267 // No services that are members of this profile have this entry name.
1268 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1269
1270 // Only services that are members of the profile and group will be abandoned.
1271 EXPECT_CALL(*profile1.get(),
1272 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1273 EXPECT_CALL(*profile1.get(),
1274 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1275 EXPECT_CALL(*profile1.get(),
1276 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1277 .WillOnce(Return(true));
1278 EXPECT_CALL(*profile1.get(),
1279 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1280 .WillOnce(Return(true));
1281
1282 // Never allow services to re-join profile1.
1283 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1284 .WillRepeatedly(Return(false));
1285
1286 // Only allow one of the members of the profile and group to successfully
1287 // join profile0.
1288 EXPECT_CALL(*profile0.get(),
1289 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1290 EXPECT_CALL(*profile0.get(),
1291 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1292 EXPECT_CALL(*profile0.get(),
1293 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1294 .WillOnce(Return(false));
1295 EXPECT_CALL(*profile0.get(),
1296 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1297 .WillOnce(Return(true));
1298
1299 // Expect the failed-to-configure service to have Unload() called on it.
1300 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1301 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1302 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1303 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1304
1305 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1306
1307 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1308 EXPECT_EQ(profile1, s_not_in_group->profile());
1309 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1310
1311 // Since we are using a MockProfile, the profile does not actually change,
1312 // since ConfigureService was not actually called on the service.
1313 EXPECT_EQ(profile1, s_configure_succeed->profile());
1314}
1315
Paul Stewart65512e12012-03-26 18:01:08 -07001316TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1317 MockServiceRefPtr s_will_remove0(
1318 new NiceMock<MockService>(control_interface(),
1319 dispatcher(),
1320 metrics(),
1321 manager()));
1322 MockServiceRefPtr s_will_remove1(
1323 new NiceMock<MockService>(control_interface(),
1324 dispatcher(),
1325 metrics(),
1326 manager()));
1327 MockServiceRefPtr s_will_not_remove0(
1328 new NiceMock<MockService>(control_interface(),
1329 dispatcher(),
1330 metrics(),
1331 manager()));
1332 MockServiceRefPtr s_will_not_remove1(
1333 new NiceMock<MockService>(control_interface(),
1334 dispatcher(),
1335 metrics(),
1336 manager()));
1337
1338 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1339 .Times(4); // Once for each registration.
1340
1341 string entry_name("entry_name");
1342 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1343 .WillRepeatedly(Return(entry_name));
1344 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1345 .WillRepeatedly(Return(entry_name));
1346 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1347 .WillRepeatedly(Return(entry_name));
1348 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1349 .WillRepeatedly(Return(entry_name));
1350
1351 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001352 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001353 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001354 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001355 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001356 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001357 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001358 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001359
1360 // One for each service added above.
1361 ASSERT_EQ(4, manager()->services_.size());
1362
1363 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001364 new StrictMock<MockProfile>(
1365 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001366
1367 s_will_remove0->set_profile(profile);
1368 s_will_remove1->set_profile(profile);
1369 s_will_not_remove0->set_profile(profile);
1370 s_will_not_remove1->set_profile(profile);
1371
1372 AdoptProfile(manager(), profile);
1373
1374 // Deny any of the services re-entry to the profile.
1375 EXPECT_CALL(*profile, ConfigureService(_))
1376 .WillRepeatedly(Return(false));
1377
1378 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1379 .WillOnce(Return(true));
1380 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1381 .WillOnce(Return(true));
1382 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1383 .WillOnce(Return(true));
1384 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1385 .WillOnce(Return(true));
1386
1387 EXPECT_CALL(*s_will_remove0, Unload())
1388 .WillOnce(Return(true));
1389 EXPECT_CALL(*s_will_remove1, Unload())
1390 .WillOnce(Return(true));
1391 EXPECT_CALL(*s_will_not_remove0, Unload())
1392 .WillOnce(Return(false));
1393 EXPECT_CALL(*s_will_not_remove1, Unload())
1394 .WillOnce(Return(false));
1395
1396
1397 // This will cause all the profiles to be unloaded.
1398 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1399
1400 // 2 of the 4 services added above should have been unregistered and
1401 // removed, leaving 2.
1402 EXPECT_EQ(2, manager()->services_.size());
1403 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1404 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1405}
1406
1407TEST_F(ManagerTest, PopProfileWithUnload) {
1408 MockServiceRefPtr s_will_remove0(
1409 new NiceMock<MockService>(control_interface(),
1410 dispatcher(),
1411 metrics(),
1412 manager()));
1413 MockServiceRefPtr s_will_remove1(
1414 new NiceMock<MockService>(control_interface(),
1415 dispatcher(),
1416 metrics(),
1417 manager()));
1418 MockServiceRefPtr s_will_not_remove0(
1419 new NiceMock<MockService>(control_interface(),
1420 dispatcher(),
1421 metrics(),
1422 manager()));
1423 MockServiceRefPtr s_will_not_remove1(
1424 new NiceMock<MockService>(control_interface(),
1425 dispatcher(),
1426 metrics(),
1427 manager()));
1428
1429 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1430 .Times(5); // Once for each registration, and one after profile pop.
1431
1432 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001433 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001434 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001435 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001436 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001437 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001438 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001439 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001440
1441 // One for each service added above.
1442 ASSERT_EQ(4, manager()->services_.size());
1443
1444 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001445 new StrictMock<MockProfile>(
1446 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001447 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001448 new StrictMock<MockProfile>(
1449 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001450
1451 s_will_remove0->set_profile(profile1);
1452 s_will_remove1->set_profile(profile1);
1453 s_will_not_remove0->set_profile(profile1);
1454 s_will_not_remove1->set_profile(profile1);
1455
1456 AdoptProfile(manager(), profile0);
1457 AdoptProfile(manager(), profile1);
1458
1459 // Deny any of the services entry to profile0, so they will all be unloaded.
1460 EXPECT_CALL(*profile0, ConfigureService(_))
1461 .WillRepeatedly(Return(false));
1462
1463 EXPECT_CALL(*s_will_remove0, Unload())
1464 .WillOnce(Return(true));
1465 EXPECT_CALL(*s_will_remove1, Unload())
1466 .WillOnce(Return(true));
1467 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001468 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001469 EXPECT_CALL(*s_will_not_remove1, Unload())
1470 .WillOnce(Return(false));
1471
Philipp Neubeck79173602012-11-13 21:10:09 +01001472 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1473 // profile list.
1474 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1475 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1476
Paul Stewart65512e12012-03-26 18:01:08 -07001477 // This will pop profile1, which should cause all our profiles to unload.
1478 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001479 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001480
1481 // 2 of the 4 services added above should have been unregistered and
1482 // removed, leaving 2.
1483 EXPECT_EQ(2, manager()->services_.size());
1484 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1485 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001486
1487 // Expect the unloaded services to lose their profile reference.
1488 EXPECT_FALSE(s_will_remove0->profile());
1489 EXPECT_FALSE(s_will_remove1->profile());
1490
1491 // If we explicitly deregister a service, the effect should be the same
1492 // with respect to the profile reference.
1493 ASSERT_TRUE(s_will_not_remove0->profile());
1494 manager()->DeregisterService(s_will_not_remove0);
1495 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001496}
1497
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001498TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001499 {
1500 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001501 ::DBus::Variant offline_mode;
1502 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001503 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001504 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001505 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001506 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001507 }
1508 {
1509 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001510 ::DBus::Variant country;
1511 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001512 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001513 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001514 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001515 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001516 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001517 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001518 {
1519 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001520 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001521 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001522 PropertyStoreTest::kBoolV,
1523 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001524 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001525 }
1526 {
1527 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001528 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001529 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001530 PropertyStoreTest::kStringV,
1531 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001532 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001533 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001534 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001535 {
1536 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001537 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001538 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001539 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001540 PropertyStoreTest::kStringsV,
1541 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001542 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001543 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001544}
1545
mukesh agrawal32399322011-09-01 10:53:43 -07001546TEST_F(ManagerTest, RequestScan) {
1547 {
1548 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001549 manager()->RegisterDevice(mock_devices_[0].get());
1550 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001551 EXPECT_CALL(*mock_devices_[0], technology())
1552 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001553 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001554 EXPECT_CALL(*mock_devices_[1], technology())
1555 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001556 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Ben Chan923a5022013-09-20 11:23:23 -07001557 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001558 }
1559
1560 {
1561 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001562 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001563 EXPECT_EQ(Error::kInvalidArguments, error.type());
1564 }
1565}
1566
Darin Petkovb65c2452012-02-23 15:17:06 +01001567TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001568 KeyValueStore args;
1569 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001570 manager()->GetService(args, &e);
1571 EXPECT_EQ(Error::kInvalidArguments, e.type());
1572 EXPECT_EQ("must specify service type", e.message());
1573}
1574
1575TEST_F(ManagerTest, GetServiceUnknownType) {
1576 KeyValueStore args;
1577 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001578 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001579 manager()->GetService(args, &e);
1580 EXPECT_EQ(Error::kNotSupported, e.type());
1581 EXPECT_EQ("service type is unsupported", e.message());
1582}
1583
Paul Stewart35eff132013-04-12 12:08:40 -07001584TEST_F(ManagerTest, GetServiceEthernetEap) {
1585 KeyValueStore args;
1586 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001587 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1588 dispatcher(),
1589 metrics(),
1590 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001591 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001592 SetEapProviderService(service);
1593 EXPECT_EQ(service, manager()->GetService(args, &e));
1594 EXPECT_TRUE(e.IsSuccess());
1595}
1596
Darin Petkovb65c2452012-02-23 15:17:06 +01001597TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001598 KeyValueStore args;
1599 Error e;
1600 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001601 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001602 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001603 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001604 manager()->GetService(args, &e);
1605 EXPECT_TRUE(e.IsSuccess());
1606}
1607
Darin Petkov33af05c2012-02-28 10:10:30 +01001608TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1609 KeyValueStore args;
1610 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001611 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001612 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001613 new StrictMock<MockProfile>(
1614 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001615 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001616 ServiceRefPtr service = manager()->GetService(args, &e);
1617 EXPECT_EQ(Error::kNotSupported, e.type());
1618 EXPECT_FALSE(service);
1619}
1620
Darin Petkovb65c2452012-02-23 15:17:06 +01001621TEST_F(ManagerTest, GetServiceVPN) {
1622 KeyValueStore args;
1623 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001624 args.SetString(kTypeProperty, kTypeVPN);
1625 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1626 args.SetString(kProviderHostProperty, "10.8.0.1");
1627 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001628 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001629 new StrictMock<MockProfile>(
1630 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001631 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001632
1633#if defined(DISABLE_VPN)
1634
1635 ServiceRefPtr service = manager()->GetService(args, &e);
1636 EXPECT_EQ(Error::kNotSupported, e.type());
1637 EXPECT_FALSE(service);
1638
1639#else
1640
Paul Stewart7f5ad572012-06-04 15:18:54 -07001641 ServiceRefPtr updated_service;
1642 EXPECT_CALL(*profile, UpdateService(_))
1643 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1644 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001645 EXPECT_CALL(*profile, LoadService(_))
1646 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001647 EXPECT_CALL(*profile, ConfigureService(_))
1648 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001649 ServiceRefPtr service = manager()->GetService(args, &e);
1650 EXPECT_TRUE(e.IsSuccess());
1651 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001652 EXPECT_EQ(service, updated_service);
1653 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001654
1655#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001656}
1657
Ben Chan520eb172013-10-30 20:51:04 -07001658#if !defined(DISABLE_WIMAX)
1659
Darin Petkovc63dcf02012-05-24 11:51:43 +02001660TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1661 KeyValueStore args;
1662 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001663 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001664 ServiceRefPtr service = manager()->GetService(args, &e);
1665 EXPECT_EQ(Error::kInvalidArguments, e.type());
1666 EXPECT_EQ("Missing WiMAX network id.", e.message());
1667 EXPECT_FALSE(service);
1668}
1669
Darin Petkovd1cd7972012-05-22 15:26:15 +02001670TEST_F(ManagerTest, GetServiceWiMax) {
1671 KeyValueStore args;
1672 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001673 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001674 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001675 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001676 ServiceRefPtr service = manager()->GetService(args, &e);
1677 EXPECT_TRUE(e.IsSuccess());
1678 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001679}
1680
Ben Chan520eb172013-10-30 20:51:04 -07001681#endif // DISABLE_WIMAX
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
mukesh agrawale37ad322013-10-08 16:33:56 -07002306 // Construct our Services so that the string comparison of
2307 // unique_name_ differs from the numerical comparison of
2308 // serial_number_.
2309 vector<scoped_refptr<MockService>> mock_services;
2310 for (size_t i = 0; i < 11; ++i) {
2311 mock_services.push_back(
2312 new NiceMock<MockService>(control_interface(),
2313 dispatcher(),
2314 metrics(),
2315 manager()));
2316 }
2317 scoped_refptr<MockService> mock_service2 = mock_services[2];
2318 scoped_refptr<MockService> mock_service10 = mock_services[10];
2319 mock_services.clear();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002320
mukesh agrawale37ad322013-10-08 16:33:56 -07002321 manager()->RegisterService(mock_service2);
2322 manager()->RegisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002323
mukesh agrawale37ad322013-10-08 16:33:56 -07002324 // Services should already be sorted by |serial_number_|.
2325 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002326
2327 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002328 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002329 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002330
2331 // Two otherwise equal services should be reordered by strength
mukesh agrawale37ad322013-10-08 16:33:56 -07002332 mock_service10->SetStrength(1);
2333 manager()->UpdateService(mock_service10);
2334 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002335
2336 // Security
mukesh agrawale37ad322013-10-08 16:33:56 -07002337 mock_service2->SetSecurity(Service::kCryptoAes, true, true);
2338 manager()->UpdateService(mock_service2);
2339 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002340
2341 // Technology
mukesh agrawale37ad322013-10-08 16:33:56 -07002342 EXPECT_CALL(*mock_service2.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002343 .WillRepeatedly(Return((Technology::kWifi)));
mukesh agrawale37ad322013-10-08 16:33:56 -07002344 EXPECT_CALL(*mock_service10.get(), technology())
Joshua Kroll053fa822012-06-05 09:50:43 -07002345 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002346
2347 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002348 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002349 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002350 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002351
Ben Chan923a5022013-09-20 11:23:23 -07002352 manager()->SetTechnologyOrder(string(kTypeWifi) + "," + string(kTypeEthernet),
2353 &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002354 EXPECT_TRUE(error.IsSuccess());
mukesh agrawale37ad322013-10-08 16:33:56 -07002355 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002356
Gaurav Shah435de2c2011-11-17 19:01:07 -08002357 // Priority.
mukesh agrawale37ad322013-10-08 16:33:56 -07002358 mock_service2->SetPriority(1, NULL);
2359 manager()->UpdateService(mock_service2);
2360 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002361
Paul Stewart2da34c02013-10-17 15:28:56 -07002362 // HasEverConnected.
2363 mock_service10->has_ever_connected_ = true;
mukesh agrawale37ad322013-10-08 16:33:56 -07002364 manager()->UpdateService(mock_service10);
2365 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002366
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002367 // Auto-connect.
mukesh agrawale37ad322013-10-08 16:33:56 -07002368 mock_service2->SetAutoConnect(true);
2369 manager()->UpdateService(mock_service2);
2370 mock_service10->SetAutoConnect(false);
2371 manager()->UpdateService(mock_service10);
2372 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002373
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002374 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2375 // to any of the others below, so we reset to the default state after
2376 // testing.
mukesh agrawale37ad322013-10-08 16:33:56 -07002377 EXPECT_CALL(*mock_service10.get(),
2378 IsDependentOn(ServiceRefPtr(mock_service2.get())))
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002379 .WillOnce(Return(true))
2380 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002381 manager()->UpdateService(mock_service10);
2382 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
2383 manager()->UpdateService(mock_service2);
2384 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002385
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002386 // Connectable.
mukesh agrawale37ad322013-10-08 16:33:56 -07002387 mock_service10->SetConnectable(true);
2388 manager()->UpdateService(mock_service10);
2389 mock_service2->SetConnectable(false);
2390 manager()->UpdateService(mock_service2);
2391 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002392
2393 // IsFailed.
mukesh agrawale37ad322013-10-08 16:33:56 -07002394 EXPECT_CALL(*mock_service2.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002395 .WillRepeatedly(Return(Service::kStateIdle));
mukesh agrawale37ad322013-10-08 16:33:56 -07002396 EXPECT_CALL(*mock_service2.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002397 .WillRepeatedly(Return(false));
mukesh agrawale37ad322013-10-08 16:33:56 -07002398 manager()->UpdateService(mock_service2);
2399 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002400 .WillRepeatedly(Return(Service::kStateFailure));
mukesh agrawale37ad322013-10-08 16:33:56 -07002401 EXPECT_CALL(*mock_service10.get(), IsFailed())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002402 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002403 manager()->UpdateService(mock_service10);
2404 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002405
2406 // Connecting.
mukesh agrawale37ad322013-10-08 16:33:56 -07002407 EXPECT_CALL(*mock_service10.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002408 .WillRepeatedly(Return(Service::kStateAssociating));
mukesh agrawale37ad322013-10-08 16:33:56 -07002409 EXPECT_CALL(*mock_service10.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002410 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002411 manager()->UpdateService(mock_service10);
2412 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002413
mukesh agrawale37ad322013-10-08 16:33:56 -07002414 // Connected-but-portalled preferred over unconnected.
2415 EXPECT_CALL(*mock_service2.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002416 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawale37ad322013-10-08 16:33:56 -07002417 EXPECT_CALL(*mock_service2.get(), IsConnected())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002418 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002419 manager()->UpdateService(mock_service2);
2420 EXPECT_TRUE(ServiceOrderIs(mock_service2, mock_service10));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002421
mukesh agrawale37ad322013-10-08 16:33:56 -07002422 // Connected preferred over connected-but-portalled.
2423 EXPECT_CALL(*mock_service10.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002424 .WillRepeatedly(Return(Service::kStateConnected));
mukesh agrawale37ad322013-10-08 16:33:56 -07002425 EXPECT_CALL(*mock_service10.get(), IsConnected())
Paul Stewarta121c442012-06-09 14:12:58 -07002426 .WillRepeatedly(Return(true));
mukesh agrawale37ad322013-10-08 16:33:56 -07002427 manager()->UpdateService(mock_service10);
2428 EXPECT_TRUE(ServiceOrderIs(mock_service10, mock_service2));
Paul Stewarta121c442012-06-09 14:12:58 -07002429
mukesh agrawale37ad322013-10-08 16:33:56 -07002430 manager()->DeregisterService(mock_service2);
2431 manager()->DeregisterService(mock_service10);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002432}
2433
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002434TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002435 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002436 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002437
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002438 scoped_refptr<MockService> mock_service0(
2439 new NiceMock<MockService>(control_interface(),
2440 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002441 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002442 manager()));
2443 scoped_refptr<MockService> mock_service1(
2444 new NiceMock<MockService>(control_interface(),
2445 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002446 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002447 manager()));
2448
2449 scoped_refptr<MockConnection> mock_connection0(
2450 new NiceMock<MockConnection>(device_info_.get()));
2451 scoped_refptr<MockConnection> mock_connection1(
2452 new NiceMock<MockConnection>(device_info_.get()));
2453
mukesh agrawale37ad322013-10-08 16:33:56 -07002454 // If neither service has a Connection, the DefaultService should be
2455 // NULL.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002456 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002457 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002458 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002459 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002460 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002461 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002462
mukesh agrawale37ad322013-10-08 16:33:56 -07002463 // An explicit sort doesn't change anything.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002464 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002465 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002466 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002467
mukesh agrawale37ad322013-10-08 16:33:56 -07002468 // Re-ordering the services doesn't change DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002469 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002470 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002471 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002472 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002473
mukesh agrawale37ad322013-10-08 16:33:56 -07002474 // Re-ordering the services doesn't change DefaultService.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002475 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002476 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002477 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002478 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002479
Paul Stewartce4ec192012-03-14 12:53:46 -07002480 mock_service0->set_mock_connection(mock_connection0);
2481 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002482
mukesh agrawale37ad322013-10-08 16:33:56 -07002483 // If both Services have Connections, the DefaultService follows
2484 // from ServiceOrderIs.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002485 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002486 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002487 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002488 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002489
Darin Petkova5e07ef2012-07-09 14:27:57 +02002490 ServiceWatcher service_watcher;
2491 int tag =
2492 manager()->RegisterDefaultServiceCallback(
2493 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2494 service_watcher.AsWeakPtr()));
2495 EXPECT_EQ(1, tag);
2496
mukesh agrawale37ad322013-10-08 16:33:56 -07002497 // Changing the ordering causes the DefaultService to change, and
2498 // appropriate notifications are sent.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002499 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002500 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2501 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002502 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002503 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002504 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002505 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002506
mukesh agrawale37ad322013-10-08 16:33:56 -07002507 // Deregistering the current DefaultService causes the other Service
2508 // to become default.
Darin Petkova5e07ef2012-07-09 14:27:57 +02002509 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002510 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002511 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00002512 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawale37ad322013-10-08 16:33:56 -07002513 mock_service1->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002514 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002515 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002516
mukesh agrawale37ad322013-10-08 16:33:56 -07002517 // Deregistering the only Service causes the DefaultService to become
2518 // NULL.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002519 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
mukesh agrawale37ad322013-10-08 16:33:56 -07002520 mock_service0->set_mock_connection(NULL); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002521 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002522 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002523
mukesh agrawale37ad322013-10-08 16:33:56 -07002524 // An explicit sort doesn't change anything.
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002525 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002526 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002527}
2528
Darin Petkova5e07ef2012-07-09 14:27:57 +02002529TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2530 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2531 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2532
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002533 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002534 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002535
2536 scoped_refptr<MockService> mock_service(
2537 new NiceMock<MockService>(
2538 control_interface(), dispatcher(), metrics(), manager()));
2539 ServiceRefPtr service = mock_service;
2540 ServiceRefPtr null_service;
2541
2542 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2543 manager()->NotifyDefaultServiceChanged(null_service);
2544
2545 ServiceWatcher service_watcher1;
2546 ServiceWatcher service_watcher2;
2547 int tag1 =
2548 manager()->RegisterDefaultServiceCallback(
2549 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2550 service_watcher1.AsWeakPtr()));
2551 EXPECT_EQ(1, tag1);
2552 int tag2 =
2553 manager()->RegisterDefaultServiceCallback(
2554 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2555 service_watcher2.AsWeakPtr()));
2556 EXPECT_EQ(2, tag2);
2557
2558 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2559 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2560 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2561 manager()->NotifyDefaultServiceChanged(null_service);
2562
2563 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2564 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2565 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2566 manager()->NotifyDefaultServiceChanged(mock_service);
2567
2568 manager()->DeregisterDefaultServiceCallback(tag1);
2569 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2570 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2571 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2572 manager()->NotifyDefaultServiceChanged(mock_service);
2573 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2574
2575 manager()->DeregisterDefaultServiceCallback(tag2);
2576 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2577 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2578 manager()->NotifyDefaultServiceChanged(mock_service);
2579
2580 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2581 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2582}
2583
Gaurav Shah435de2c2011-11-17 19:01:07 -08002584TEST_F(ManagerTest, AvailableTechnologies) {
2585 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2586 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002587 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002588 manager(),
2589 "null4",
2590 "addr4",
2591 0));
2592 manager()->RegisterDevice(mock_devices_[0]);
2593 manager()->RegisterDevice(mock_devices_[1]);
2594 manager()->RegisterDevice(mock_devices_[2]);
2595 manager()->RegisterDevice(mock_devices_[3]);
2596
2597 ON_CALL(*mock_devices_[0].get(), technology())
2598 .WillByDefault(Return(Technology::kEthernet));
2599 ON_CALL(*mock_devices_[1].get(), technology())
2600 .WillByDefault(Return(Technology::kWifi));
2601 ON_CALL(*mock_devices_[2].get(), technology())
2602 .WillByDefault(Return(Technology::kCellular));
2603 ON_CALL(*mock_devices_[3].get(), technology())
2604 .WillByDefault(Return(Technology::kWifi));
2605
2606 set<string> expected_technologies;
2607 expected_technologies.insert(Technology::NameFromIdentifier(
2608 Technology::kEthernet));
2609 expected_technologies.insert(Technology::NameFromIdentifier(
2610 Technology::kWifi));
2611 expected_technologies.insert(Technology::NameFromIdentifier(
2612 Technology::kCellular));
2613 Error error;
2614 vector<string> technologies = manager()->AvailableTechnologies(&error);
2615
2616 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2617 ContainerEq(expected_technologies));
2618}
2619
2620TEST_F(ManagerTest, ConnectedTechnologies) {
2621 scoped_refptr<MockService> connected_service1(
2622 new NiceMock<MockService>(control_interface(),
2623 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002624 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002625 manager()));
2626 scoped_refptr<MockService> connected_service2(
2627 new NiceMock<MockService>(control_interface(),
2628 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002629 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002630 manager()));
2631 scoped_refptr<MockService> disconnected_service1(
2632 new NiceMock<MockService>(control_interface(),
2633 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002634 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002635 manager()));
2636 scoped_refptr<MockService> disconnected_service2(
2637 new NiceMock<MockService>(control_interface(),
2638 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002639 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002640 manager()));
2641
2642 ON_CALL(*connected_service1.get(), IsConnected())
2643 .WillByDefault(Return(true));
2644 ON_CALL(*connected_service2.get(), IsConnected())
2645 .WillByDefault(Return(true));
2646
2647 manager()->RegisterService(connected_service1);
2648 manager()->RegisterService(connected_service2);
2649 manager()->RegisterService(disconnected_service1);
2650 manager()->RegisterService(disconnected_service2);
2651
2652 manager()->RegisterDevice(mock_devices_[0]);
2653 manager()->RegisterDevice(mock_devices_[1]);
2654 manager()->RegisterDevice(mock_devices_[2]);
2655 manager()->RegisterDevice(mock_devices_[3]);
2656
2657 ON_CALL(*mock_devices_[0].get(), technology())
2658 .WillByDefault(Return(Technology::kEthernet));
2659 ON_CALL(*mock_devices_[1].get(), technology())
2660 .WillByDefault(Return(Technology::kWifi));
2661 ON_CALL(*mock_devices_[2].get(), technology())
2662 .WillByDefault(Return(Technology::kCellular));
2663 ON_CALL(*mock_devices_[3].get(), technology())
2664 .WillByDefault(Return(Technology::kWifi));
2665
2666 mock_devices_[0]->SelectService(connected_service1);
2667 mock_devices_[1]->SelectService(disconnected_service1);
2668 mock_devices_[2]->SelectService(disconnected_service2);
2669 mock_devices_[3]->SelectService(connected_service2);
2670
2671 set<string> expected_technologies;
2672 expected_technologies.insert(Technology::NameFromIdentifier(
2673 Technology::kEthernet));
2674 expected_technologies.insert(Technology::NameFromIdentifier(
2675 Technology::kWifi));
2676 Error error;
2677
2678 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2679 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2680 ContainerEq(expected_technologies));
2681}
2682
2683TEST_F(ManagerTest, DefaultTechnology) {
2684 scoped_refptr<MockService> connected_service(
2685 new NiceMock<MockService>(control_interface(),
2686 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002687 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002688 manager()));
2689 scoped_refptr<MockService> disconnected_service(
2690 new NiceMock<MockService>(control_interface(),
2691 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002692 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002693 manager()));
2694
2695 // Connected. WiFi.
2696 ON_CALL(*connected_service.get(), IsConnected())
2697 .WillByDefault(Return(true));
2698 ON_CALL(*connected_service.get(), state())
2699 .WillByDefault(Return(Service::kStateConnected));
2700 ON_CALL(*connected_service.get(), technology())
2701 .WillByDefault(Return(Technology::kWifi));
2702
2703 // Disconnected. Ethernet.
2704 ON_CALL(*disconnected_service.get(), technology())
2705 .WillByDefault(Return(Technology::kEthernet));
2706
2707 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002708 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002709 Error error;
2710 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2711
2712
2713 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002714 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002715 // Connected service should be brought to the front now.
2716 string expected_technology =
2717 Technology::NameFromIdentifier(Technology::kWifi);
2718 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2719}
2720
Paul Stewart212d60f2012-07-12 10:59:13 -07002721TEST_F(ManagerTest, Stop) {
2722 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002723 new NiceMock<MockProfile>(
2724 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002725 AdoptProfile(manager(), profile);
2726 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002727 new NiceMock<MockService>(control_interface(),
2728 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002729 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002730 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002731 manager()->RegisterService(service);
2732 manager()->RegisterDevice(mock_devices_[0]);
2733 EXPECT_CALL(*profile.get(),
2734 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2735 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002736 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002737 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2738 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002739 manager()->Stop();
2740}
2741
mukesh agrawal00917ce2011-11-22 23:56:55 +00002742TEST_F(ManagerTest, UpdateServiceConnected) {
2743 scoped_refptr<MockService> mock_service(
2744 new NiceMock<MockService>(control_interface(),
2745 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002746 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002747 manager()));
2748 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002749 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002750 EXPECT_FALSE(mock_service->auto_connect());
2751
Gaurav Shah435de2c2011-11-17 19:01:07 -08002752 EXPECT_CALL(*mock_service.get(), IsConnected())
2753 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002754 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002755 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2756 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2757 // would break the SortServices test. (crbug.com/206367)
2758 EXPECT_TRUE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002759 EXPECT_TRUE(mock_service->auto_connect());
2760}
2761
Paul Stewart2da34c02013-10-17 15:28:56 -07002762TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002763 // This tests the case where the user connects to a service that is
2764 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002765 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002766 scoped_refptr<MockService> mock_service(
2767 new NiceMock<MockService>(control_interface(),
2768 dispatcher(),
2769 metrics(),
2770 manager()));
2771 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002772 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002773 EXPECT_FALSE(mock_service->auto_connect());
2774
Gary Moraind93615e2012-04-27 11:50:03 -07002775 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002776 new MockProfile(
2777 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002778
Gary Moraind93615e2012-04-27 11:50:03 -07002779 mock_service->set_profile(profile);
2780 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002781 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002782 EXPECT_CALL(*profile,
2783 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002784 manager()->UpdateService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002785 // We can't EXPECT_CALL(..., EnableAndRetainAutoConnect), because that
2786 // requires us to mock out EnableAndRetainAutoConnect. And mocking that out
2787 // would break the SortServices test. (crbug.com/206367)
2788 EXPECT_TRUE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002789 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002790 // This releases the ref on the mock profile.
2791 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002792}
2793
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002794TEST_F(ManagerTest, SaveSuccessfulService) {
2795 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002796 new StrictMock<MockProfile>(
2797 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002798 AdoptProfile(manager(), profile);
2799 scoped_refptr<MockService> service(
2800 new NiceMock<MockService>(control_interface(),
2801 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002802 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002803 manager()));
2804
2805 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2806 ServiceRefPtr expect_service(service.get());
2807
2808 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2809 .WillOnce(Return(false));
2810 manager()->RegisterService(service);
2811
2812 EXPECT_CALL(*service.get(), state())
2813 .WillRepeatedly(Return(Service::kStateConnected));
2814 EXPECT_CALL(*service.get(), IsConnected())
2815 .WillRepeatedly(Return(true));
2816 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2817 .WillOnce(Return(true));
2818 manager()->UpdateService(service);
2819}
2820
Darin Petkove7c6ad32012-06-29 10:22:09 +02002821TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002822 MockProfile *profile0 =
2823 new MockProfile(control_interface(), metrics(), manager(), "");
2824 MockProfile *profile1 =
2825 new MockProfile(control_interface(), metrics(), manager(), "");
2826 MockProfile *profile2 =
2827 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002828 AdoptProfile(manager(), profile0); // Passes ownership.
2829 AdoptProfile(manager(), profile1); // Passes ownership.
2830 AdoptProfile(manager(), profile2); // Passes ownership.
2831 DeviceRefPtr device_ref(mock_devices_[0].get());
2832 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2833 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2834 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2835 manager()->UpdateDevice(mock_devices_[0]);
2836}
2837
Paul Stewart1b253142012-01-26 14:05:52 -08002838TEST_F(ManagerTest, EnumerateProfiles) {
2839 vector<string> profile_paths;
2840 for (size_t i = 0; i < 10; i++) {
2841 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002842 new StrictMock<MockProfile>(
2843 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002844 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002845 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2846 .WillOnce(Return(profile_paths.back()));
2847 AdoptProfile(manager(), profile);
2848 }
2849
2850 Error error;
2851 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2852 EXPECT_TRUE(error.IsSuccess());
2853 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2854 for (size_t i = 0; i < profile_paths.size(); i++) {
2855 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2856 }
2857}
2858
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002859TEST_F(ManagerTest, AutoConnectOnRegister) {
2860 MockServiceRefPtr service = MakeAutoConnectableService();
2861 EXPECT_CALL(*service.get(), AutoConnect());
2862 manager()->RegisterService(service);
2863 dispatcher()->DispatchPendingEvents();
2864}
2865
2866TEST_F(ManagerTest, AutoConnectOnUpdate) {
2867 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002868 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002869 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002870 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002871 manager()->RegisterService(service1);
2872 manager()->RegisterService(service2);
2873 dispatcher()->DispatchPendingEvents();
2874
2875 EXPECT_CALL(*service1.get(), AutoConnect());
2876 EXPECT_CALL(*service2.get(), state())
2877 .WillRepeatedly(Return(Service::kStateFailure));
2878 EXPECT_CALL(*service2.get(), IsFailed())
2879 .WillRepeatedly(Return(true));
2880 EXPECT_CALL(*service2.get(), IsConnected())
2881 .WillRepeatedly(Return(false));
2882 manager()->UpdateService(service2);
2883 dispatcher()->DispatchPendingEvents();
2884}
2885
2886TEST_F(ManagerTest, AutoConnectOnDeregister) {
2887 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002888 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002889 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002890 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002891 manager()->RegisterService(service1);
2892 manager()->RegisterService(service2);
2893 dispatcher()->DispatchPendingEvents();
2894
2895 EXPECT_CALL(*service1.get(), AutoConnect());
2896 manager()->DeregisterService(service2);
2897 dispatcher()->DispatchPendingEvents();
2898}
2899
Darin Petkov3ec55342012-09-28 14:04:44 +02002900TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2901 MockServiceRefPtr service = MakeAutoConnectableService();
2902 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2903 SetPowerManager();
2904 EXPECT_CALL(*service, AutoConnect()).Times(0);
2905 manager()->RegisterService(service);
2906 dispatcher()->DispatchPendingEvents();
2907}
2908
Darin Petkovca621542012-07-25 14:25:56 +02002909TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2910 MockServiceRefPtr service = MakeAutoConnectableService();
2911 SetPowerState(PowerManagerProxyDelegate::kMem);
2912 SetPowerManager();
2913 EXPECT_CALL(*service, AutoConnect()).Times(0);
2914 manager()->RegisterService(service);
2915 dispatcher()->DispatchPendingEvents();
2916}
2917
2918TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2919 MockServiceRefPtr service = MakeAutoConnectableService();
2920 SetPowerState(PowerManagerProxyDelegate::kOn);
2921 SetPowerManager();
2922 EXPECT_CALL(*service, AutoConnect());
2923 manager()->RegisterService(service);
2924 dispatcher()->DispatchPendingEvents();
2925}
2926
2927TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2928 MockServiceRefPtr service = MakeAutoConnectableService();
2929 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2930 SetPowerManager();
2931 EXPECT_CALL(*service, AutoConnect());
2932 manager()->RegisterService(service);
2933 dispatcher()->DispatchPendingEvents();
2934}
2935
Paul Stewart63864b62012-11-07 15:10:55 -08002936TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2937 SetRunning(false);
2938 MockServiceRefPtr service = MakeAutoConnectableService();
2939 EXPECT_CALL(*service, AutoConnect()).Times(0);
2940 manager()->RegisterService(service);
2941 dispatcher()->DispatchPendingEvents();
2942}
2943
Darin Petkovca621542012-07-25 14:25:56 +02002944TEST_F(ManagerTest, OnPowerStateChanged) {
2945 MockServiceRefPtr service = MakeAutoConnectableService();
2946 SetPowerState(PowerManagerProxyDelegate::kOn);
2947 SetPowerManager();
2948 EXPECT_CALL(*service, AutoConnect());
2949 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002950 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002951 dispatcher()->DispatchPendingEvents();
2952
mukesh agrawal784566d2012-08-08 18:32:58 -07002953 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002954 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2955 EXPECT_CALL(*service, AutoConnect());
2956 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002957 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002958
mukesh agrawal784566d2012-08-08 18:32:58 -07002959 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002960 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2961 EXPECT_CALL(*service, AutoConnect()).Times(0);
2962 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002963 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002964}
2965
Darin Petkov3ec55342012-09-28 14:04:44 +02002966TEST_F(ManagerTest, AddTerminationAction) {
2967 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
Daniel Eratf9753672013-01-24 10:17:02 -08002968 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02002969 SetPowerManager();
2970 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2971 manager()->AddTerminationAction("action1", base::Closure());
2972 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2973 manager()->AddTerminationAction("action2", base::Closure());
2974}
2975
2976TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002977 const char kKey1[] = "action1";
2978 const char kKey2[] = "action2";
2979 const int kSuspendDelayId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002980
2981 MockPowerManager &power_manager = *power_manager_;
2982 SetPowerManager();
2983
2984 // Removing an action when the hook table is empty should not result in any
2985 // calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002986 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002987 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2988 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2989 manager()->RemoveTerminationAction("unknown");
2990 Mock::VerifyAndClearExpectations(&power_manager);
2991
Daniel Eratf9753672013-01-24 10:17:02 -08002992 EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _))
2993 .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true)));
Daniel Erat0818cca2012-12-14 10:16:21 -08002994 EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1);
Darin Petkov3ec55342012-09-28 14:04:44 +02002995 manager()->AddTerminationAction(kKey1, base::Closure());
2996 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2997 manager()->AddTerminationAction(kKey2, base::Closure());
2998
2999 // Removing an action that ends up with a non-empty hook table should not
3000 // result in any calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08003001 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02003002 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
3003 manager()->RemoveTerminationAction(kKey1);
3004 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
3005 Mock::VerifyAndClearExpectations(&power_manager);
3006
3007 // Removing the last action should trigger unregistering from the power
3008 // manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08003009 EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId))
3010 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02003011 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
3012 manager()->RemoveTerminationAction(kKey2);
3013 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3014}
3015
3016TEST_F(ManagerTest, RunTerminationActions) {
3017 TerminationActionTest test_action;
3018 const string kActionName = "action";
3019
3020 EXPECT_CALL(test_action, Done(_));
3021 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3022 test_action.AsWeakPtr()));
3023
3024 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3025 Bind(&TerminationActionTest::Action,
3026 test_action.AsWeakPtr()));
3027 test_action.set_manager(manager());
3028 EXPECT_CALL(test_action, Done(_));
3029 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3030 test_action.AsWeakPtr()));
3031}
3032
Daniel Erat0818cca2012-12-14 10:16:21 -08003033TEST_F(ManagerTest, OnSuspendImminent) {
3034 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02003035 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Erat0818cca2012-12-14 10:16:21 -08003036 EXPECT_CALL(*power_manager_,
3037 ReportSuspendReadiness(
3038 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003039 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003040 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02003041}
3042
3043TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08003044 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02003045 Error error;
Daniel Erat0818cca2012-12-14 10:16:21 -08003046 EXPECT_CALL(*power_manager_,
3047 ReportSuspendReadiness(
3048 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02003049 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08003050 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003051}
3052
Paul Stewartc681fa02012-03-02 19:40:04 -08003053TEST_F(ManagerTest, RecheckPortal) {
3054 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3055 .WillOnce(Return(false));
3056 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3057 .WillOnce(Return(true));
3058 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3059 .Times(0);
3060
3061 manager()->RegisterDevice(mock_devices_[0]);
3062 manager()->RegisterDevice(mock_devices_[1]);
3063 manager()->RegisterDevice(mock_devices_[2]);
3064
3065 manager()->RecheckPortal(NULL);
3066}
3067
Paul Stewartd215af62012-04-24 23:25:50 -07003068TEST_F(ManagerTest, RecheckPortalOnService) {
3069 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3070 dispatcher(),
3071 metrics(),
3072 manager());
3073 EXPECT_CALL(*mock_devices_[0].get(),
3074 IsConnectedToService(IsRefPtrTo(service)))
3075 .WillOnce(Return(false));
3076 EXPECT_CALL(*mock_devices_[1].get(),
3077 IsConnectedToService(IsRefPtrTo(service)))
3078 .WillOnce(Return(true));
3079 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3080 .WillOnce(Return(true));
3081 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3082 .Times(0);
3083
3084 manager()->RegisterDevice(mock_devices_[0]);
3085 manager()->RegisterDevice(mock_devices_[1]);
3086 manager()->RegisterDevice(mock_devices_[2]);
3087
3088 manager()->RecheckPortalOnService(service);
3089}
3090
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003091TEST_F(ManagerTest, GetDefaultService) {
3092 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003093 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003094
3095 scoped_refptr<MockService> mock_service(
3096 new NiceMock<MockService>(control_interface(),
3097 dispatcher(),
3098 metrics(),
3099 manager()));
3100
3101 manager()->RegisterService(mock_service);
3102 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003103 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003104
3105 scoped_refptr<MockConnection> mock_connection(
3106 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003107 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003108 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003109 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003110
Paul Stewartce4ec192012-03-14 12:53:46 -07003111 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003112 manager()->DeregisterService(mock_service);
3113}
3114
Paul Stewart13ed2252012-03-21 12:52:46 -07003115TEST_F(ManagerTest, GetServiceWithGUID) {
3116 scoped_refptr<MockService> mock_service0(
3117 new NiceMock<MockService>(control_interface(),
3118 dispatcher(),
3119 metrics(),
3120 manager()));
3121
3122 scoped_refptr<MockService> mock_service1(
3123 new NiceMock<MockService>(control_interface(),
3124 dispatcher(),
3125 metrics(),
3126 manager()));
3127
Paul Stewartcb59fed2012-03-21 21:14:46 -07003128 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3129 .Times(0);
3130 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3131 .Times(0);
3132
Paul Stewart13ed2252012-03-21 12:52:46 -07003133 manager()->RegisterService(mock_service0);
3134 manager()->RegisterService(mock_service1);
3135
3136 const string kGUID0 = "GUID0";
3137 const string kGUID1 = "GUID1";
3138
3139 {
3140 Error error;
3141 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3142 EXPECT_FALSE(error.IsSuccess());
3143 EXPECT_FALSE(service);
3144 }
3145
3146 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003147 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003148
3149 {
3150 Error error;
3151 ServiceRefPtr service = manager()->GetService(args, &error);
3152 EXPECT_EQ(Error::kInvalidArguments, error.type());
3153 EXPECT_FALSE(service);
3154 }
3155
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003156 mock_service0->SetGuid(kGUID0, NULL);
3157 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003158
3159 {
3160 Error error;
3161 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3162 EXPECT_TRUE(error.IsSuccess());
3163 EXPECT_EQ(mock_service0.get(), service.get());
3164 }
3165
3166 {
3167 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003168 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3169 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003170 ServiceRefPtr service = manager()->GetService(args, &error);
3171 EXPECT_TRUE(error.IsSuccess());
3172 EXPECT_EQ(mock_service1.get(), service.get());
3173 }
3174
3175 manager()->DeregisterService(mock_service0);
3176 manager()->DeregisterService(mock_service1);
3177}
3178
Gary Morain028545d2012-04-07 14:55:52 -07003179
3180TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003181 EXPECT_FALSE(manager()->IsOnline());
3182 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3183
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003184 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003185 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003186 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3187 .Times(AnyNumber());
3188 scoped_refptr<MockService> mock_service0(
3189 new NiceMock<MockService>(control_interface(),
3190 dispatcher(),
3191 metrics(),
3192 manager()));
3193
3194 scoped_refptr<MockService> mock_service1(
3195 new NiceMock<MockService>(control_interface(),
3196 dispatcher(),
3197 metrics(),
3198 manager()));
3199
3200 EXPECT_CALL(*mock_service0.get(), IsConnected())
3201 .WillRepeatedly(Return(false));
3202 EXPECT_CALL(*mock_service1.get(), IsConnected())
3203 .WillRepeatedly(Return(false));
3204
3205 manager()->RegisterService(mock_service0);
3206 manager()->RegisterService(mock_service1);
3207
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003208 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003209 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3210
3211 manager()->DeregisterService(mock_service0);
3212 manager()->DeregisterService(mock_service1);
3213}
3214
3215TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003216 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003217 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003218 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3219 .Times(AnyNumber());
3220 scoped_refptr<MockService> mock_service0(
3221 new NiceMock<MockService>(control_interface(),
3222 dispatcher(),
3223 metrics(),
3224 manager()));
3225
3226 scoped_refptr<MockService> mock_service1(
3227 new NiceMock<MockService>(control_interface(),
3228 dispatcher(),
3229 metrics(),
3230 manager()));
3231
3232 EXPECT_CALL(*mock_service0.get(), IsConnected())
3233 .WillRepeatedly(Return(false));
3234 EXPECT_CALL(*mock_service1.get(), IsConnected())
3235 .WillRepeatedly(Return(true));
3236 EXPECT_CALL(*mock_service0.get(), state())
3237 .WillRepeatedly(Return(Service::kStateIdle));
3238 EXPECT_CALL(*mock_service1.get(), state())
3239 .WillRepeatedly(Return(Service::kStateConnected));
3240
3241 manager()->RegisterService(mock_service0);
3242 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003243 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003244
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003245 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003246 EXPECT_EQ("online", manager()->CalculateState(NULL));
3247
3248 manager()->DeregisterService(mock_service0);
3249 manager()->DeregisterService(mock_service1);
3250}
3251
Paul Stewart03e29f72013-09-26 00:49:48 -07003252TEST_F(ManagerTest, RefreshConnectionState) {
3253 EXPECT_CALL(*manager_adaptor_,
3254 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3255 RefreshConnectionState();
3256 Mock::VerifyAndClearExpectations(manager_adaptor_);
3257
3258 scoped_refptr<MockService> mock_service(
3259 new NiceMock<MockService>(control_interface(),
3260 dispatcher(),
3261 metrics(),
3262 manager()));
3263 EXPECT_CALL(*manager_adaptor_,
3264 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3265 manager()->RegisterService(mock_service);
3266 RefreshConnectionState();
3267
3268 scoped_refptr<MockConnection> mock_connection(
3269 new NiceMock<MockConnection>(device_info_.get()));
3270 mock_service->set_mock_connection(mock_connection);
3271 EXPECT_CALL(*mock_service, state())
3272 .WillOnce(Return(Service::kStateIdle));
3273 RefreshConnectionState();
3274
3275 Mock::VerifyAndClearExpectations(manager_adaptor_);
3276 EXPECT_CALL(*mock_service, state())
3277 .WillOnce(Return(Service::kStatePortal));
3278 EXPECT_CALL(*manager_adaptor_,
3279 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3280 RefreshConnectionState();
3281 Mock::VerifyAndClearExpectations(manager_adaptor_);
3282
3283 mock_service->set_mock_connection(NULL);
3284 manager()->DeregisterService(mock_service);
3285}
3286
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003287TEST_F(ManagerTest, StartupPortalList) {
3288 // Simulate loading value from the default profile.
3289 const string kProfileValue("wifi,vpn");
3290 manager()->props_.check_portal_list = kProfileValue;
3291
3292 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3293 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3294 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3295
3296 const string kStartupValue("cellular,ethernet");
3297 manager()->SetStartupPortalList(kStartupValue);
3298 // Ensure profile value is not overwritten, so when we save the default
3299 // profile, the correct value will still be written.
3300 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3301
3302 // However we should read back a different list.
3303 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3304 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3305 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3306
3307 const string kRuntimeValue("ppp");
3308 // Setting a runtime value over the control API should overwrite both
3309 // the profile value and what we read back.
3310 Error error;
3311 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003312 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003313 kRuntimeValue,
3314 &error);
3315 ASSERT_TRUE(error.IsSuccess());
3316 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3317 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3318 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3319 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3320}
3321
Paul Stewart036dba02012-08-07 12:34:41 -07003322TEST_F(ManagerTest, LinkMonitorEnabled) {
3323 const string kEnabledTechnologies("wifi,vpn");
3324 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3325 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3326 EXPECT_FALSE(
3327 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3328}
3329
Paul Stewart85aea152013-01-22 09:31:56 -08003330TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003331 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003332 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003333 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003334 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003335 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003336 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3337 AdoptProfile(manager(), profile);
3338 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3339 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3340 scoped_ptr<MockStore> store1(new MockStore);
3341 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3342}
3343
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003344TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003345 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003346 DisableTechnologyReplyHandler disable_technology_reply_handler;
3347 ResultCallback disable_technology_callback(
3348 Bind(&DisableTechnologyReplyHandler::ReportResult,
3349 disable_technology_reply_handler.AsWeakPtr()));
3350 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3351
Ben Chan923a5022013-09-20 11:23:23 -07003352 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003353 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003354 EXPECT_TRUE(error.IsSuccess());
3355
Joshua Krollda798622012-06-05 12:30:48 -07003356 ON_CALL(*mock_devices_[0], technology())
3357 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003358 ON_CALL(*mock_devices_[1], technology())
3359 .WillByDefault(Return(Technology::kCellular));
3360 ON_CALL(*mock_devices_[2], technology())
3361 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003362
3363 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003364 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003365
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003366 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003367 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3368 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003369 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003370 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003371 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003372 EXPECT_TRUE(error.IsSuccess());
3373
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003374 // Ethernet Device is enabled, and mock doesn't change error from
3375 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003376 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3377 mock_devices_[0]->enabled_ = true;
3378 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003379 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003380 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003381 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003382
3383 // Ethernet Device is disabled, and mock doesn't change error from
3384 // kOperationInitiated, so expect enable to say operation in progress.
3385 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3386 mock_devices_[0]->enabled_ = false;
3387 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003388 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003389 &error, disable_technology_callback);
3390 EXPECT_TRUE(error.IsOngoing());
3391
3392 // Cellular Device is enabled, but disable failed.
3393 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3394 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3395 mock_devices_[1]->enabled_ = true;
3396 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003397 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003398 &error, disable_technology_callback);
3399 EXPECT_EQ(Error::kPermissionDenied, error.type());
3400
3401 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3402 // if one is in progress (even if the other completed immediately).
3403 manager()->RegisterDevice(mock_devices_[2]);
3404 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3405 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3406 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3407 mock_devices_[1]->enabled_ = true;
3408 mock_devices_[2]->enabled_ = true;
3409 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003410 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003411 &error, disable_technology_callback);
3412 EXPECT_TRUE(error.IsOngoing());
3413
3414 // ...and order doesn't matter.
3415 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3416 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3417 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3418 mock_devices_[1]->enabled_ = true;
3419 mock_devices_[2]->enabled_ = true;
3420 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003421 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003422 &error, disable_technology_callback);
3423 EXPECT_TRUE(error.IsOngoing());
3424 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3425
3426 // Multiple Cellular Devices in enabled state. Even if all disable
3427 // operations complete asynchronously, we only get one call to the
3428 // DisableTechnologyReplyHandler::ReportResult.
3429 ResultCallback device1_result_callback;
3430 ResultCallback device2_result_callback;
3431 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3432 .WillOnce(SaveArg<2>(&device1_result_callback));
3433 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3434 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3435 SaveArg<2>(&device2_result_callback)));
3436 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3437 mock_devices_[1]->enabled_ = true;
3438 mock_devices_[2]->enabled_ = true;
3439 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003440 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003441 &error, disable_technology_callback);
3442 EXPECT_TRUE(error.IsOngoing());
3443 device1_result_callback.Run(Error(Error::kSuccess));
3444 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003445}
3446
Paul Stewart4d5efb72012-09-17 12:24:34 -07003447TEST_F(ManagerTest, IgnoredSearchList) {
3448 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003449 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003450 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003451
3452 const string kIgnored0 = "chromium.org";
3453 ignored_paths.push_back(kIgnored0);
3454 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003455 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003456 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3457
3458 const string kIgnored1 = "google.com";
3459 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3460 ignored_paths.push_back(kIgnored1);
3461 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003462 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003463 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3464
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003465 ignored_paths.clear();
3466 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3467 SetIgnoredDNSSearchPaths("", NULL);
3468 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3469
Paul Stewart4d5efb72012-09-17 12:24:34 -07003470 SetResolver(Resolver::GetInstance());
3471}
3472
Paul Stewartbfb82552012-10-24 16:48:48 -07003473TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3474 // Test to make sure that every service state-change causes the
3475 // Manager to emit a new service list.
3476 scoped_refptr<MockService> mock_service(
3477 new NiceMock<MockService>(control_interface(),
3478 dispatcher(),
3479 metrics(),
3480 manager()));
3481 EXPECT_CALL(*mock_service, state())
3482 .WillRepeatedly(Return(Service::kStateIdle));
3483
3484 manager()->RegisterService(mock_service);
3485 EXPECT_CALL(
3486 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003487 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003488 EXPECT_CALL(
3489 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003490 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003491 CompleteServiceSort();
3492
3493 Mock::VerifyAndClearExpectations(manager_adaptor_);
3494 EXPECT_CALL(
3495 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003496 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003497 EXPECT_CALL(
3498 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003499 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003500 manager()->UpdateService(mock_service.get());
3501 CompleteServiceSort();
3502
3503 manager()->DeregisterService(mock_service);
3504}
3505
3506TEST_F(ManagerTest, EnumerateServices) {
3507 scoped_refptr<MockService> mock_service(
3508 new NiceMock<MockService>(control_interface(),
3509 dispatcher(),
3510 metrics(),
3511 manager()));
3512 manager()->RegisterService(mock_service);
3513
3514 EXPECT_CALL(*mock_service, state())
3515 .WillRepeatedly(Return(Service::kStateConnected));
3516 EXPECT_CALL(*mock_service, IsVisible())
3517 .WillRepeatedly(Return(false));
3518 EXPECT_TRUE(EnumerateAvailableServices().empty());
3519 EXPECT_TRUE(EnumerateWatchedServices().empty());
3520
3521 EXPECT_CALL(*mock_service, state())
3522 .WillRepeatedly(Return(Service::kStateIdle));
3523 EXPECT_TRUE(EnumerateAvailableServices().empty());
3524 EXPECT_TRUE(EnumerateWatchedServices().empty());
3525
3526 EXPECT_CALL(*mock_service, IsVisible())
3527 .WillRepeatedly(Return(true));
3528 Service::ConnectState unwatched_states[] = {
3529 Service::kStateUnknown,
3530 Service::kStateIdle,
3531 Service::kStateFailure
3532 };
3533 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3534 EXPECT_CALL(*mock_service, state())
3535 .WillRepeatedly(Return(unwatched_states[i]));
3536 EXPECT_FALSE(EnumerateAvailableServices().empty());
3537 EXPECT_TRUE(EnumerateWatchedServices().empty());
3538 }
3539
3540 Service::ConnectState watched_states[] = {
3541 Service::kStateAssociating,
3542 Service::kStateConfiguring,
3543 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003544 Service::kStatePortal,
3545 Service::kStateOnline
3546 };
3547 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3548 EXPECT_CALL(*mock_service, state())
3549 .WillRepeatedly(Return(watched_states[i]));
3550 EXPECT_FALSE(EnumerateAvailableServices().empty());
3551 EXPECT_FALSE(EnumerateWatchedServices().empty());
3552 }
3553
3554 manager()->DeregisterService(mock_service);
3555}
3556
Paul Stewart39db5ca2013-03-18 14:15:17 -07003557TEST_F(ManagerTest, ConnectToBestServices) {
3558 scoped_refptr<MockService> wifi_service0(
3559 new NiceMock<MockService>(control_interface(),
3560 dispatcher(),
3561 metrics(),
3562 manager()));
3563 EXPECT_CALL(*wifi_service0.get(), state())
3564 .WillRepeatedly(Return(Service::kStateIdle));
3565 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3566 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003567 wifi_service0->SetConnectable(true);
3568 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003569 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3570 EXPECT_CALL(*wifi_service0.get(), technology())
3571 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003572 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3573 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003574 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3575 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003576
3577 scoped_refptr<MockService> wifi_service1(
3578 new NiceMock<MockService>(control_interface(),
3579 dispatcher(),
3580 metrics(),
3581 manager()));
3582 EXPECT_CALL(*wifi_service1.get(), state())
3583 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003584 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3585 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003586 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3587 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003588 wifi_service1->SetAutoConnect(true);
3589 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003590 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3591 EXPECT_CALL(*wifi_service1.get(), technology())
3592 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003593 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3594 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003595
3596 scoped_refptr<MockService> wifi_service2(
3597 new NiceMock<MockService>(control_interface(),
3598 dispatcher(),
3599 metrics(),
3600 manager()));
3601 EXPECT_CALL(*wifi_service2.get(), state())
3602 .WillRepeatedly(Return(Service::kStateConnected));
3603 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3604 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003605 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3606 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003607 wifi_service2->SetAutoConnect(true);
3608 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003609 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3610 EXPECT_CALL(*wifi_service2.get(), technology())
3611 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003612 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3613 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003614
3615 manager()->RegisterService(wifi_service0);
3616 manager()->RegisterService(wifi_service1);
3617 manager()->RegisterService(wifi_service2);
3618
3619 CompleteServiceSort();
3620 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3621
3622 scoped_refptr<MockService> cell_service(
3623 new NiceMock<MockService>(control_interface(),
3624 dispatcher(),
3625 metrics(),
3626 manager()));
3627
3628 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003629 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003630 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003631 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003632 EXPECT_CALL(*cell_service.get(), IsVisible())
3633 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003634 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003635 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003636 EXPECT_CALL(*cell_service.get(), technology())
3637 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003638 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3639 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003640 manager()->RegisterService(cell_service);
3641
Arman Uguray6fe4f262013-08-02 20:21:55 -07003642 scoped_refptr<MockService> wimax_service(
3643 new NiceMock<MockService>(control_interface(),
3644 dispatcher(),
3645 metrics(),
3646 manager()));
3647
3648 EXPECT_CALL(*wimax_service.get(), state())
3649 .WillRepeatedly(Return(Service::kStateConnected));
3650 EXPECT_CALL(*wimax_service.get(), IsConnected())
3651 .WillRepeatedly(Return(true));
3652 EXPECT_CALL(*wimax_service.get(), IsVisible())
3653 .WillRepeatedly(Return(true));
3654 wimax_service->SetAutoConnect(true);
3655 wimax_service->SetConnectable(true);
3656 EXPECT_CALL(*wimax_service.get(), technology())
3657 .WillRepeatedly(Return(Technology::kWiMax));
3658 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3659 .WillRepeatedly(Return(false));
3660 manager()->RegisterService(wimax_service);
3661
Paul Stewart39db5ca2013-03-18 14:15:17 -07003662 scoped_refptr<MockService> vpn_service(
3663 new NiceMock<MockService>(control_interface(),
3664 dispatcher(),
3665 metrics(),
3666 manager()));
3667
3668 EXPECT_CALL(*vpn_service.get(), state())
3669 .WillRepeatedly(Return(Service::kStateIdle));
3670 EXPECT_CALL(*vpn_service.get(), IsConnected())
3671 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003672 EXPECT_CALL(*vpn_service.get(), IsVisible())
3673 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003674 wifi_service2->SetAutoConnect(false);
3675 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003676 EXPECT_CALL(*vpn_service.get(), technology())
3677 .WillRepeatedly(Return(Technology::kVPN));
3678 manager()->RegisterService(vpn_service);
3679
3680 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003681 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003682
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003683 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3684 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003685 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003686 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3687 .Times(0); // Explicitly disconnected.
3688 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003689 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003690
3691 manager()->ConnectToBestServices(NULL);
3692 dispatcher()->DispatchPendingEvents();
3693
3694 // After this operation, since the Connect calls above are mocked and
3695 // no actual state changes have occurred, we should expect that the
3696 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003697 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003698}
3699
Christopher Wiley83889862013-05-02 15:55:09 -07003700TEST_F(ManagerTest, VerifyWhenNotConnected) {
3701 const string kFakeCertificate("fake cert");
3702 const string kFakePublicKey("fake public key");
3703 const string kFakeNonce("fake public key");
3704 const string kFakeSignedData("fake signed data");
3705 const string kFakeUdn("fake udn");
3706 const vector<uint8_t> kSSID(10, 87);
3707 const string kConfiguredSSID("AConfiguredDestination");
3708 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3709 kConfiguredSSID.end());
3710 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3711 scoped_refptr<MockWiFiService> mock_destination(
3712 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3713 metrics(), manager(), wifi_provider_,
3714 kSSID, "", "none", false));
3715 // Register this service, but don't mark it as connected.
3716 manager()->RegisterService(mock_destination);
3717 // Verify that if we're not connected to anything, verification fails.
3718 {
3719 LOG(INFO) << "Can't verify if not connected.";
3720 EXPECT_CALL(*crypto_util_proxy_,
3721 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3722 Error error(Error::kOperationInitiated);
3723 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3724 kFakeSignedData, kFakeUdn, "", "",
3725 ResultBoolCallback(), &error);
3726 EXPECT_TRUE(error.IsFailure());
3727 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3728 }
3729 {
3730 // However, if the destination is already configured, we might be
3731 // connected to it via something other than WiFi, and we shouldn't
3732 // enforce the WiFi check.
3733 EXPECT_CALL(*crypto_util_proxy_,
3734 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3735 kFakeSignedData, kFakeUdn,
3736 kConfiguredSSIDVector, kConfiguredBSSID,
3737 _, _)).Times(1).WillOnce(Return(true));
3738 Error error(Error::kOperationInitiated);
3739 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3740 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3741 kConfiguredBSSID, ResultBoolCallback(),
3742 &error);
3743 EXPECT_FALSE(error.IsFailure());
3744 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3745 }
3746}
3747
Christopher Wiley1057cd72013-02-28 15:21:29 -08003748TEST_F(ManagerTest, VerifyDestination) {
3749 const string kFakeCertificate("fake cert");
3750 const string kFakePublicKey("fake public key");
3751 const string kFakeNonce("fake public key");
3752 const string kFakeSignedData("fake signed data");
3753 const string kFakeUdn("fake udn");
3754 const char kSSIDStr[] = "fake ssid";
3755 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003756 const string kConfiguredSSID("AConfiguredDestination");
3757 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3758 kConfiguredSSID.end());
3759 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003760 const string kFakeData("muffin man");
3761 scoped_refptr<MockWiFiService> mock_destination(
3762 new NiceMock<MockWiFiService>(control_interface(),
3763 dispatcher(),
3764 metrics(),
3765 manager(),
3766 wifi_provider_,
3767 kSSID,
3768 "",
3769 "none",
3770 false));
3771 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003772 // Making the service look online will let service lookup in
3773 // VerifyDestinatoin succeed.
3774 EXPECT_CALL(*mock_destination.get(), IsConnected())
3775 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003776 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003777
3778 // Lead off by verifying that the basic VerifyDestination flow works.
3779 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003780 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003781 ResultBoolCallback passed_down_callback;
3782 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3783 kFakePublicKey,
3784 kFakeNonce,
3785 kFakeSignedData,
3786 kFakeUdn,
3787 kSSID,
3788 _,
3789 _,
3790 _))
3791 .Times(1)
3792 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3793 // Ask the manager to verify the current destination. This should look
3794 // up our previously registered service, and pass some metadata about
3795 // that service down to the CryptoUtilProxy to verify.
3796 Error error(Error::kOperationInitiated);
3797 ResultBoolCallback cb = Bind(
3798 &DestinationVerificationTest::ResultBoolCallbackStub,
3799 dv_test.AsWeakPtr());
3800 manager()->VerifyDestination(kFakeCertificate,
3801 kFakePublicKey,
3802 kFakeNonce,
3803 kFakeSignedData,
3804 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003805 // Ask to be verified against that service.
3806 "", "",
3807 cb,
3808 &error);
3809 // We assert here, because if the operation is not ongoing, it is
3810 // inconsistent with shim behavior to call the callback anyway.
3811 ASSERT_TRUE(error.IsOngoing());
3812 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3813 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3814 // Call the callback passed into the CryptoUtilProxy, which
3815 // should find its way into the callback passed into the manager.
3816 // In real code, that callback passed into the manager is from the
3817 // DBus adaptor.
3818 Error e;
3819 passed_down_callback.Run(e, true);
3820 Mock::VerifyAndClearExpectations(&dv_test);
3821 }
3822
Christopher Wiley1057cd72013-02-28 15:21:29 -08003823 // Now for a slightly more complex variant. When we encrypt data,
3824 // we do the same verification step but monkey with the callback to
3825 // link ourselves to an encrypt step afterward.
3826 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003827 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003828 ResultBoolCallback passed_down_callback;
3829 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3830 kFakePublicKey,
3831 kFakeNonce,
3832 kFakeSignedData,
3833 kFakeUdn,
3834 kSSID,
3835 _,
3836 _,
3837 _))
3838 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3839
3840 Error error(Error::kOperationInitiated);
3841 ResultStringCallback cb = Bind(
3842 &DestinationVerificationTest::ResultStringCallbackStub,
3843 dv_test.AsWeakPtr());
3844 manager()->VerifyAndEncryptData(kFakeCertificate,
3845 kFakePublicKey,
3846 kFakeNonce,
3847 kFakeSignedData,
3848 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003849 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003850 kFakeData,
3851 cb,
3852 &error);
3853 ASSERT_TRUE(error.IsOngoing());
3854 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3855 // Now, if we call that passed down callback, we should see encrypt being
3856 // called.
3857 ResultStringCallback second_passed_down_callback;
3858 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3859 kFakeData,
3860 _,
3861 _))
3862 .Times(1)
3863 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3864 Return(true)));
3865 Error e;
3866 passed_down_callback.Run(e, true);
3867 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3868 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3869 // And if we call the second passed down callback, we should see the
3870 // original function we passed down to VerifyDestination getting called.
3871 e.Reset();
3872 second_passed_down_callback.Run(e, "");
3873 Mock::VerifyAndClearExpectations(&dv_test);
3874 }
3875
3876 // If verification fails on the way to trying to encrypt, we should ditch
3877 // without calling encrypt at all.
3878 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003879 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003880 ResultBoolCallback passed_down_callback;
3881 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3882 kFakePublicKey,
3883 kFakeNonce,
3884 kFakeSignedData,
3885 kFakeUdn,
3886 kSSID,
3887 _,
3888 _,
3889 _))
3890 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3891
3892 Error error(Error::kOperationInitiated);
3893 ResultStringCallback cb = Bind(
3894 &DestinationVerificationTest::ResultStringCallbackStub,
3895 dv_test.AsWeakPtr());
3896 manager()->VerifyAndEncryptData(kFakeCertificate,
3897 kFakePublicKey,
3898 kFakeNonce,
3899 kFakeSignedData,
3900 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003901 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003902 kFakeData,
3903 cb,
3904 &error);
3905 ASSERT_TRUE(error.IsOngoing());
3906 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3907 Error e(Error::kOperationFailed);
3908 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3909 // Although we're ditching, this callback is what cleans up the pending
3910 // DBus call.
3911 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3912 passed_down_callback.Run(e, false);
3913 Mock::VerifyAndClearExpectations(&dv_test);
3914 }
3915}
3916
Paul Stewartd2e1c362013-03-03 19:06:07 -08003917TEST_F(ManagerTest, IsProfileBefore) {
3918 scoped_refptr<MockProfile> profile0(
3919 new NiceMock<MockProfile>(
3920 control_interface(), metrics(), manager(), ""));
3921 scoped_refptr<MockProfile> profile1(
3922 new NiceMock<MockProfile>(
3923 control_interface(), metrics(), manager(), ""));
3924
3925 AdoptProfile(manager(), profile0);
3926 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3927 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3928 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3929
3930 // A few abnormal cases, but it's good to track their behavior.
3931 scoped_refptr<MockProfile> profile2(
3932 new NiceMock<MockProfile>(
3933 control_interface(), metrics(), manager(), ""));
3934 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3935 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3936 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3937 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3938}
3939
Paul Stewart967eaeb2013-04-25 19:53:07 -07003940TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
3941 MockStore storage0;
3942 MockStore storage1;
3943 MockStore storage2;
3944
3945 scoped_refptr<MockProfile> profile0(
3946 new NiceMock<MockProfile>(
3947 control_interface(), metrics(), manager(), ""));
3948 scoped_refptr<MockProfile> profile1(
3949 new NiceMock<MockProfile>(
3950 control_interface(), metrics(), manager(), ""));
3951 scoped_refptr<MockProfile> profile2(
3952 new NiceMock<MockProfile>(
3953 control_interface(), metrics(), manager(), ""));
3954
3955 AdoptProfile(manager(), profile0);
3956 AdoptProfile(manager(), profile1);
3957 AdoptProfile(manager(), profile2);
3958
3959 scoped_refptr<MockService> service(
3960 new NiceMock<MockService>(control_interface(),
3961 dispatcher(),
3962 metrics(),
3963 manager()));
3964
3965 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
3966 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
3967 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
3968
3969 const string kEntry0("aluminum_crutch");
3970 const string kEntry2("rehashed_faces");
3971
3972 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
3973 .WillOnce(Return(kEntry0));
3974 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
3975 .WillOnce(Return(""));
3976 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
3977 .WillOnce(Return(kEntry2));
3978
3979 const string kProfileRpc0("service_station");
3980 const string kProfileRpc2("crystal_tiaras");
3981
3982 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
3983 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
3984 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
3985
3986 map<string, string> entries =
3987 manager()->GetLoadableProfileEntriesForService(service);
3988 EXPECT_EQ(2, entries.size());
3989 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
3990 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
3991 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
3992 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
3993}
3994
mukesh agrawal00752532013-05-03 15:46:55 -07003995TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07003996 // We need a real glib here, so that profiles are persisted.
3997 GLib glib;
3998 ScopedTempDir temp_dir;
3999 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4000 Manager manager(control_interface(),
4001 dispatcher(),
4002 metrics(),
4003 &glib,
4004 run_path(),
4005 storage_path(),
4006 temp_dir.path().value());
4007 // Can't use |wifi_provider_|, because it's owned by the Manager
4008 // object in the fixture.
4009 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4010 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004011 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004012 // Give manager a valid place to write the user profile list.
4013 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4014
4015 // With no user profiles, the WiFiProvider should be called once
4016 // (for the default profile).
4017 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4018 manager.InitializeProfiles();
4019 Mock::VerifyAndClearExpectations(wifi_provider);
4020
4021 // With |n| user profiles, the WiFiProvider should be called |n+1|
4022 // times. First, create 2 user profiles...
4023 const char kProfile0[] = "~user/profile0";
4024 const char kProfile1[] = "~user/profile1";
4025 string profile_rpc_path;
4026 Error error;
4027 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4028 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4029 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4030 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4031
4032 // ... then reset manager state ...
4033 manager.profiles_.clear();
4034
4035 // ...then check that the WiFiProvider is notified about all three
4036 // profiles (one default, two user).
4037 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4038 manager.InitializeProfiles();
4039 Mock::VerifyAndClearExpectations(wifi_provider);
4040}
4041
mukesh agrawal00752532013-05-03 15:46:55 -07004042TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4043 // We need a real glib here, so that profiles are persisted.
4044 GLib glib;
4045 ScopedTempDir temp_dir;
4046 scoped_ptr<Manager> manager;
4047 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4048
4049 // Instantiate a Manager with empty persistent storage. Check that
4050 // defaults are set.
4051 //
4052 // Note that we use the same directory for default and user profiles.
4053 // This doesn't affect the test results, because we don't push a
4054 // user profile.
4055 manager.reset(new Manager(control_interface(),
4056 dispatcher(),
4057 metrics(),
4058 &glib,
4059 run_path(),
4060 temp_dir.path().value(),
4061 temp_dir.path().value()));
4062 manager->InitializeProfiles();
4063 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4064 manager->props_.check_portal_list);
4065 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4066 manager->props_.ignored_dns_search_paths);
4067 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4068 manager->props_.link_monitor_technologies);
4069 EXPECT_EQ(PortalDetector::kDefaultURL,
4070 manager->props_.portal_url);
4071 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4072 manager->props_.portal_check_interval_seconds);
4073
4074 // Change one of the settings.
4075 static const string kCustomCheckPortalList = "fiber0";
4076 Error error;
4077 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4078 manager->profiles_[0]->Save();
4079
4080 // Instantiate a new manager. It should have our settings for
4081 // check_portal_list, rather than the default.
4082 manager.reset(new Manager(control_interface(),
4083 dispatcher(),
4084 metrics(),
4085 &glib,
4086 run_path(),
4087 temp_dir.path().value(),
4088 temp_dir.path().value()));
4089 manager->InitializeProfiles();
4090 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4091
4092 // If we clear the persistent storage, we again get the default value.
4093 ASSERT_TRUE(temp_dir.Delete());
4094 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4095 manager.reset(new Manager(control_interface(),
4096 dispatcher(),
4097 metrics(),
4098 &glib,
4099 run_path(),
4100 temp_dir.path().value(),
4101 temp_dir.path().value()));
4102 manager->InitializeProfiles();
4103 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4104 manager->props_.check_portal_list);
4105}
4106
mukesh agrawalb94adde2013-08-22 18:17:26 -07004107TEST_F(ManagerTest, ProfileStackChangeLogging) {
4108 // We use a real glib here, since Manager and Profile don't provide an
4109 // easy way to mock out KeyFileStore.
4110 GLib glib;
4111 ScopedTempDir temp_dir;
4112 scoped_ptr<Manager> manager;
4113 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4114 manager.reset(new Manager(control_interface(),
4115 dispatcher(),
4116 metrics(),
4117 &glib,
4118 run_path(),
4119 temp_dir.path().value(),
4120 temp_dir.path().value()));
4121
4122 ScopedMockLog log;
4123 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4124 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4125 manager->InitializeProfiles();
4126
4127 const char kProfile0[] = "~user/profile0";
4128 const char kProfile1[] = "~user/profile1";
4129 const char kProfile2[] = "~user/profile2";
4130 TestCreateProfile(manager.get(), kProfile0);
4131 TestCreateProfile(manager.get(), kProfile1);
4132 TestCreateProfile(manager.get(), kProfile2);
4133
4134 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4135 TestPushProfile(manager.get(), kProfile0);
4136
4137 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4138 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4139
4140 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4141 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4142
4143 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4144 TestPopProfile(manager.get(), kProfile2);
4145
4146 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4147 TestPopAnyProfile(manager.get());
4148
4149 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4150 TestPopAllUserProfiles(manager.get());
4151}
4152
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004153// Custom property setters should return false, and make no changes, if
4154// the new value is the same as the old value.
4155TEST_F(ManagerTest, CustomSetterNoopChange) {
4156 // SetCheckPortalList
4157 {
4158 static const string kCheckPortalList = "weird-device,weirder-device";
4159 Error error;
4160 // Set to known value.
4161 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4162 EXPECT_TRUE(error.IsSuccess());
4163 // Set to same value.
4164 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4165 EXPECT_TRUE(error.IsSuccess());
4166 }
4167
4168 // SetIgnoredDNSSearchPaths
4169 {
4170 NiceMock<MockResolver> resolver;
4171 static const string kIgnoredPaths = "example.com,example.org";
4172 Error error;
4173 SetResolver(&resolver);
4174 // Set to known value.
4175 EXPECT_CALL(resolver, set_ignored_search_list(_));
4176 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4177 EXPECT_TRUE(error.IsSuccess());
4178 Mock::VerifyAndClearExpectations(&resolver);
4179 // Set to same value.
4180 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4181 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4182 EXPECT_TRUE(error.IsSuccess());
4183 Mock::VerifyAndClearExpectations(&resolver);
4184 }
4185}
4186
Paul Stewart7de7e022013-08-28 09:42:50 -07004187TEST_F(ManagerTest, GeoLocation) {
4188 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4189
4190 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4191 dispatcher(),
4192 metrics(),
4193 manager(),
4194 "null",
4195 "addr",
4196 0));
4197
4198 // Manager should ignore gelocation info from technologies it does not know.
4199 EXPECT_CALL(*device, technology())
4200 .Times(AtLeast(1))
4201 .WillRepeatedly(Return(Technology::kEthernet));
4202 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4203 manager()->OnDeviceGeolocationInfoUpdated(device);
4204 Mock::VerifyAndClearExpectations(device);
4205 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4206
4207 // Manager should add WiFi geolocation info.
4208 EXPECT_CALL(*device, technology())
4209 .Times(AtLeast(1))
4210 .WillRepeatedly(Return(Technology::kWifi));
4211 EXPECT_CALL(*device, GetGeolocationObjects())
4212 .WillOnce(Return(vector<GeolocationInfo>()));
4213 manager()->OnDeviceGeolocationInfoUpdated(device);
4214 Mock::VerifyAndClearExpectations(device);
4215 auto location_infos = manager()->GetNetworksForGeolocation();
4216 EXPECT_EQ(1, location_infos.size());
4217 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4218
4219 // Manager should inclusively add cellular info.
4220 EXPECT_CALL(*device, technology())
4221 .Times(AtLeast(1))
4222 .WillRepeatedly(Return(Technology::kCellular));
4223 EXPECT_CALL(*device, GetGeolocationObjects())
4224 .WillOnce(Return(vector<GeolocationInfo>()));
4225 manager()->OnDeviceGeolocationInfoUpdated(device);
4226 location_infos = manager()->GetNetworksForGeolocation();
4227 EXPECT_EQ(2, location_infos.size());
4228 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4229 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4230}
4231
Chris Masone9be4a9d2011-05-16 15:44:09 -07004232} // namespace shill