blob: 2acf28d45b98c4baa8ad30071456ba742475a54b [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"
Chris Masone6515aab2011-10-12 16:19:09 -070023#include "shill/glib.h"
24#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070025#include "shill/key_value_store.h"
Christopher Wiley3e7635e2012-08-15 09:46:17 -070026#include "shill/logging.h"
mukesh agrawal32399322011-09-01 10:53:43 -070027#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080028#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/mock_control.h"
Christopher Wiley1057cd72013-02-28 15:21:29 -080030#include "shill/mock_crypto_util_proxy.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070031#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080032#include "shill/mock_device_info.h"
Paul Stewart35eff132013-04-12 12:08:40 -070033#include "shill/mock_ethernet_eap_provider.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070034#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000035#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020036#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070037#include "shill/mock_profile.h"
Paul Stewart4d5efb72012-09-17 12:24:34 -070038#include "shill/mock_resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070039#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070040#include "shill/mock_store.h"
Paul Stewart3c504012013-01-17 17:49:58 -080041#include "shill/mock_wifi_provider.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070042#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020044#include "shill/proxy_factory.h"
Chris Masone6515aab2011-10-12 16:19:09 -070045#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070046#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020047#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070048
Christopher Wiley1057cd72013-02-28 15:21:29 -080049using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080050using base::FilePath;
Paul Stewart5ad16062013-02-21 18:10:48 -080051using base::ScopedTempDir;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070052using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070053using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070054using std::string;
55using std::vector;
56
Chris Masone9be4a9d2011-05-16 15:44:09 -070057namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070058using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070059using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080060using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070061using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070062using ::testing::InSequence;
mukesh agrawal784566d2012-08-08 18:32:58 -070063using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070064using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070065using ::testing::NiceMock;
66using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070067using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070068using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080069using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080070using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080071using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070072using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070073
Chris Masone3bd3c8c2011-06-13 08:20:26 -070074class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070075 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070076 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020077 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080078 device_info_(new NiceMock<MockDeviceInfo>(
79 control_interface(),
80 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080082 reinterpret_cast<Manager*>(NULL))),
Paul Stewart3c504012013-01-17 17:49:58 -080083 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070084 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080085 wifi_provider_(new NiceMock<MockWiFiProvider>()),
86 crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(),
87 glib())) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070088 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
89 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080090 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070091 manager(),
92 "null0",
93 "addr0",
94 0));
95 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
96 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080097 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070098 manager(),
99 "null1",
100 "addr1",
101 1));
102 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
103 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800104 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700105 manager(),
106 "null2",
107 "addr2",
108 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800109 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
110 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800111 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800112 manager(),
113 "null3",
114 "addr3",
115 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700116 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800117 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800118
119 // Replace the manager's adaptor with a quieter one, and one
120 // we can do EXPECT*() against. Passes ownership.
121 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800122
Paul Stewart35eff132013-04-12 12:08:40 -0700123 // Replace the manager's Ethernet EAP provider with our mock.
124 // Passes ownership.
125 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
126
Paul Stewart3c504012013-01-17 17:49:58 -0800127 // Replace the manager's WiFi provider with our mock. Passes
128 // ownership.
129 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800130
131 // Replace the manager's crypto util proxy with our mock. Passes
132 // ownership.
133 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700134 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700135 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700136
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100137 void SetMetrics(Metrics *metrics) {
138 manager()->set_metrics(metrics);
139 }
140
Paul Stewartfdd16072011-09-16 12:41:35 -0700141 bool IsDeviceRegistered(const DeviceRefPtr &device,
142 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700143 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700144 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700145 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700146 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700147 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700148
Paul Stewarta849a3d2011-11-03 05:54:09 -0700149 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
150 manager->profiles_.push_back(profile);
151 }
152
Paul Stewart63864b62012-11-07 15:10:55 -0800153 void SetRunning(bool running) {
154 manager()->running_ = running;
155 }
156
Paul Stewart75225512012-01-26 22:51:33 -0800157 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
158 return manager->ephemeral_profile_;
159 }
160
Paul Stewart307c2502013-03-23 12:32:10 -0700161 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
162 return manager->profiles_;
163 }
164
Chris Masone6515aab2011-10-12 16:19:09 -0700165 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
166 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700167 FilePath final_path(storage_path());
168 final_path = final_path.Append("test.profile");
169 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
170 storage->set_path(final_path);
171 if (!storage->Open())
172 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800173 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800174 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800175 manager,
176 id,
177 "",
178 false));
179 profile->set_storage(storage.release()); // Passes ownership of "storage".
180 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700181 }
182
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700183 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
184 const string &profile_identifier,
185 const string &service_name) {
186 GLib glib;
187 KeyFileStore store(&glib);
188 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
189 return store.Open() &&
190 store.SetString(service_name, "rather", "irrelevant") &&
191 store.Close();
192 }
193
194 Error::Type TestCreateProfile(Manager *manager, const string &name) {
195 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800196 string path;
197 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700198 return error.type();
199 }
200
201 Error::Type TestPopAnyProfile(Manager *manager) {
202 Error error;
203 manager->PopAnyProfile(&error);
204 return error.type();
205 }
206
Paul Stewart307c2502013-03-23 12:32:10 -0700207 Error::Type TestPopAllUserProfiles(Manager *manager) {
208 Error error;
209 manager->PopAllUserProfiles(&error);
210 return error.type();
211 }
212
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700213 Error::Type TestPopProfile(Manager *manager, const string &name) {
214 Error error;
215 manager->PopProfile(name, &error);
216 return error.type();
217 }
218
219 Error::Type TestPushProfile(Manager *manager, const string &name) {
220 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800221 string path;
222 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700223 return error.type();
224 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000225
Paul Stewartf3eced92013-04-17 12:18:22 -0700226 Error::Type TestInsertUserProfile(Manager *manager,
227 const string &name,
228 const string &user_hash) {
229 Error error;
230 string path;
231 manager->InsertUserProfile(name, user_hash, &path, &error);
232 return error.type();
233 }
234
Paul Stewartd2e1c362013-03-03 19:06:07 -0800235 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
236 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700237 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800238 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800239 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200240 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700241 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800242 return profile;
243 }
244
245 void AddMockProfileToManager(Manager *manager) {
246 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700247 }
248
Paul Stewartdfa46052012-06-26 09:44:14 -0700249 void CompleteServiceSort() {
250 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
251 dispatcher()->DispatchPendingEvents();
252 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
253 }
254
Paul Stewart49739c02012-08-08 17:24:03 -0700255 RpcIdentifier GetDefaultServiceRpcIdentifier() {
256 return manager()->GetDefaultServiceRpcIdentifier(NULL);
257 }
258
Paul Stewart4d5efb72012-09-17 12:24:34 -0700259 void SetResolver(Resolver *resolver) {
260 manager()->resolver_ = resolver;
261 }
262
263 void SetIgnoredDNSSearchPaths(const string &search_paths) {
264 manager()->SetIgnoredDNSSearchPaths(search_paths, NULL);
265 }
266
267 const string &GetIgnoredDNSSearchPaths() {
268 return manager()->props_.ignored_dns_search_paths;
269 }
270
Paul Stewartd2e1c362013-03-03 19:06:07 -0800271 WiFiServiceRefPtr ReleaseTempMockService() {
272 // Take a reference to hold during this function.
273 WiFiServiceRefPtr temp_service = temp_mock_service_;
274 temp_mock_service_ = NULL;
275 return temp_service;
276 }
277
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700278 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000279 typedef scoped_refptr<MockService> MockServiceRefPtr;
280
Darin Petkova5e07ef2012-07-09 14:27:57 +0200281 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
282 public:
283 ServiceWatcher() {}
284 virtual ~ServiceWatcher() {}
285
286 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
287
288 private:
289 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
290 };
291
Darin Petkovca621542012-07-25 14:25:56 +0200292 class TestProxyFactory : public ProxyFactory {
293 public:
294 TestProxyFactory() {}
295
296 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
297 PowerManagerProxyDelegate */*delegate*/) {
298 return NULL;
299 }
300
301 private:
302 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
303 };
304
Darin Petkov3ec55342012-09-28 14:04:44 +0200305 class TerminationActionTest :
306 public base::SupportsWeakPtr<TerminationActionTest> {
307 public:
308 static const char kActionName[];
309
310 TerminationActionTest() : manager_(NULL) {}
311 virtual ~TerminationActionTest() {}
312
313 MOCK_METHOD1(Done, void(const Error &error));
314
315 void Action() {
316 manager_->TerminationActionComplete("action");
317 }
318
319 void set_manager(Manager *manager) { manager_ = manager; }
320
321 private:
322 Manager *manager_;
323 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
324 };
325
Christopher Wiley1057cd72013-02-28 15:21:29 -0800326 class DestinationVerificationTest :
327 public base::SupportsWeakPtr<DestinationVerificationTest> {
328 public:
329 DestinationVerificationTest() {}
330 virtual ~DestinationVerificationTest() {}
331
332 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
333 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
334 const string &value));
335 private:
336 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
337 };
338
Darin Petkovca621542012-07-25 14:25:56 +0200339 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
340 power_manager_->power_state_ = state;
341 }
342
343 void SetPowerManager() {
344 manager()->set_power_manager(power_manager_.release());
345 }
346
Darin Petkov3ec55342012-09-28 14:04:44 +0200347 HookTable *GetTerminationActions() {
348 return &manager()->termination_actions_;
349 }
350
Darin Petkovca621542012-07-25 14:25:56 +0200351 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
352 manager()->OnPowerStateChanged(state);
353 }
354
Daniel Erat0818cca2012-12-14 10:16:21 -0800355 void OnSuspendImminent(int suspend_id) {
356 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200357 }
358
Daniel Erat0818cca2012-12-14 10:16:21 -0800359 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
360 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200361 }
362
Paul Stewartbfb82552012-10-24 16:48:48 -0700363 vector<string> EnumerateAvailableServices() {
364 return manager()->EnumerateAvailableServices(NULL);
365 }
366
367 vector<string> EnumerateWatchedServices() {
368 return manager()->EnumerateWatchedServices(NULL);
369 }
370
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000371 MockServiceRefPtr MakeAutoConnectableService() {
372 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
373 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800374 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000375 manager());
376 service->MakeFavorite();
377 service->set_connectable(true);
378 return service;
379 }
380
Paul Stewart35eff132013-04-12 12:08:40 -0700381 void SetEapProviderService(const ServiceRefPtr &service) {
382 ethernet_eap_provider_->set_service(service);
383 }
384
Darin Petkovca621542012-07-25 14:25:56 +0200385 TestProxyFactory proxy_factory_;
386 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700387 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800388 scoped_ptr<MockDeviceInfo> device_info_;
389
Paul Stewartd2e1c362013-03-03 19:06:07 -0800390 // This service is held for the manager, and given ownership in a mock
391 // function. This ensures that when the Manager takes ownership, there
392 // is only one reference left.
393 scoped_refptr<MockWiFiService> temp_mock_service_;
394
Paul Stewart3c504012013-01-17 17:49:58 -0800395 // These pointers are owned by the manager, and only tracked here for
396 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800397 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700398 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800399 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800400 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700401};
402
Darin Petkov3ec55342012-09-28 14:04:44 +0200403const char ManagerTest::TerminationActionTest::kActionName[] = "action";
404
Paul Stewart22aa71b2011-09-16 12:15:11 -0700405bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700406 if (!manager()->sort_services_task_.IsCancelled()) {
407 manager()->SortServicesTask();
408 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700409 return (svc0.get() == manager()->services_[0].get() &&
410 svc1.get() == manager()->services_[1].get());
411}
412
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700413TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700414 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
415 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700416}
417
Chris Masone9be4a9d2011-05-16 15:44:09 -0700418TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700419 ON_CALL(*mock_devices_[0].get(), technology())
420 .WillByDefault(Return(Technology::kEthernet));
421 ON_CALL(*mock_devices_[1].get(), technology())
422 .WillByDefault(Return(Technology::kWifi));
423 ON_CALL(*mock_devices_[2].get(), technology())
424 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700425
Paul Stewart22aa71b2011-09-16 12:15:11 -0700426 manager()->RegisterDevice(mock_devices_[0]);
427 manager()->RegisterDevice(mock_devices_[1]);
428 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700429
Paul Stewart22aa71b2011-09-16 12:15:11 -0700430 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
431 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
432 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700433}
434
Paul Stewarta41e38d2011-11-11 07:47:29 -0800435TEST_F(ManagerTest, DeviceRegistrationAndStart) {
436 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500437 mock_devices_[0]->enabled_persistent_ = true;
438 mock_devices_[1]->enabled_persistent_ = false;
439 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800440 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500441 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800442 .Times(0);
443 manager()->RegisterDevice(mock_devices_[0]);
444 manager()->RegisterDevice(mock_devices_[1]);
445}
446
447TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800448 MockProfile *profile =
449 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800450 DeviceRefPtr device_ref(mock_devices_[0].get());
451 AdoptProfile(manager(), profile); // Passes ownership.
452 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200453 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800454 manager()->RegisterDevice(mock_devices_[0]);
455}
456
Chris Masone9be4a9d2011-05-16 15:44:09 -0700457TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700458 ON_CALL(*mock_devices_[0].get(), technology())
459 .WillByDefault(Return(Technology::kEthernet));
460 ON_CALL(*mock_devices_[1].get(), technology())
461 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700462
Gaurav Shah435de2c2011-11-17 19:01:07 -0800463 manager()->RegisterDevice(mock_devices_[0]);
464 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700465
Paul Stewart22aa71b2011-09-16 12:15:11 -0700466 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
467 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700468
Thieu Le5133b712013-02-19 14:47:21 -0800469 MockProfile *profile =
470 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700471 AdoptProfile(manager(), profile); // Passes ownership.
472
Eric Shienbrood9a245532012-03-07 14:20:39 -0500473 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700474 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800475 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700476 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700477
Eric Shienbrood9a245532012-03-07 14:20:39 -0500478 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700479 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800480 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700481 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700482}
483
484TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700485 // It's much easier and safer to use a real GLib for this test.
486 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700487 Manager manager(control_interface(),
488 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800489 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700490 &glib,
491 run_path(),
492 storage_path(),
493 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700494 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
495 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700496 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700497
Chris Masone9be4a9d2011-05-16 15:44:09 -0700498 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700499 new NiceMock<MockService>(control_interface(),
500 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800501 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700502 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700503 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700504 new NiceMock<MockService>(control_interface(),
505 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800506 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700507 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700508
Darin Petkov457728b2013-01-09 09:49:08 +0100509 string service1_name(mock_service->unique_name());
510 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700511
512 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
513 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700514 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700515 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700516 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700517 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700518 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700519
Chris Masone9d779932011-08-25 16:33:41 -0700520 manager.RegisterService(mock_service);
521 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700522
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800523 Error error;
524 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700525 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700526 EXPECT_EQ(2, ids.size());
527 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
528 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700529
Chris Masone9d779932011-08-25 16:33:41 -0700530 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
531 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
532
533 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700534}
535
Chris Masone6515aab2011-10-12 16:19:09 -0700536TEST_F(ManagerTest, RegisterKnownService) {
537 // It's much easier and safer to use a real GLib for this test.
538 GLib glib;
539 Manager manager(control_interface(),
540 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800541 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700542 &glib,
543 run_path(),
544 storage_path(),
545 string());
546 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
547 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700548 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700549 {
550 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
551 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800552 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700553 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700554 ASSERT_TRUE(profile->AdoptService(service1));
555 ASSERT_TRUE(profile->ContainsService(service1));
556 } // Force destruction of service1.
557
558 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
559 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800560 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700561 &manager));
562 manager.RegisterService(service2);
563 EXPECT_EQ(service2->profile().get(), profile.get());
564 manager.Stop();
565}
566
567TEST_F(ManagerTest, RegisterUnknownService) {
568 // It's much easier and safer to use a real GLib for this test.
569 GLib glib;
570 Manager manager(control_interface(),
571 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800572 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700573 &glib,
574 run_path(),
575 storage_path(),
576 string());
577 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
578 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700579 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700580 {
581 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
582 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800583 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700584 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700585 ASSERT_TRUE(profile->AdoptService(service1));
586 ASSERT_TRUE(profile->ContainsService(service1));
587 } // Force destruction of service1.
588 scoped_refptr<MockService> mock_service2(
589 new NiceMock<MockService>(control_interface(),
590 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800591 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700592 &manager));
593 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100594 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700595 manager.RegisterService(mock_service2);
596 EXPECT_NE(mock_service2->profile().get(), profile.get());
597 manager.Stop();
598}
599
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000600TEST_F(ManagerTest, DeregisterUnregisteredService) {
601 // WiFi assumes that it can deregister a service that is not
602 // registered. (E.g. a hidden service can be deregistered when it
603 // loses its last endpoint, and again when WiFi is Stop()-ed.)
604 //
605 // So test that doing so doesn't cause a crash.
606 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
607 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800608 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000609 manager());
610 manager()->DeregisterService(service);
611}
612
Chris Masonea8a2c252011-06-27 22:16:30 -0700613TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700614 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700615 map<string, ::DBus::Variant> props;
616 Error error(Error::kInvalidProperty, "");
617 {
618 ::DBus::Error dbus_error;
619 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700620 manager()->mutable_store()->SetStringProperty(
621 flimflam::kCheckPortalListProperty,
622 expected,
623 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700624 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700625 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
626 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
627 expected);
628 }
629 {
630 ::DBus::Error dbus_error;
631 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700632 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
633 expected,
634 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700635 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700636 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
637 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
638 expected);
639 }
640}
641
Chris Masone3c3f6a12011-07-01 10:01:41 -0700642TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700643 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800644 manager()->RegisterDevice(mock_devices_[0]);
645 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700646 {
647 map<string, ::DBus::Variant> props;
648 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700649 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700650 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700651 vector < ::DBus::Path> devices =
652 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700653 EXPECT_EQ(2, devices.size());
654 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700655}
656
mukesh agrawal2366eed2012-03-20 18:21:50 -0700657TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700658 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700659 map<string, ::DBus::Variant> props;
660 ::DBus::Error dbus_error;
661 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
662 map<string, ::DBus::Variant>::const_iterator prop =
663 props.find(flimflam::kServicesProperty);
664 ASSERT_FALSE(prop == props.end());
665 const ::DBus::Variant &variant = prop->second;
666 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
667}
668
Chris Masone6791a432011-07-12 13:23:19 -0700669TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700670 Manager manager(control_interface(),
671 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800672 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700673 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700674 run_path(),
675 storage_path(),
676 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700677 scoped_refptr<MockService> s2(new MockService(control_interface(),
678 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800679 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700680 &manager));
681 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700682 {
Chris Masone6515aab2011-10-12 16:19:09 -0700683 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700684 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800685 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700686 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700687 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700688 .WillRepeatedly(Return(true));
689 EXPECT_CALL(*storage, Flush())
690 .Times(AnyNumber())
691 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700692 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700693 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700694 }
Chris Masone6515aab2011-10-12 16:19:09 -0700695 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800696 ProfileRefPtr profile(
697 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700698 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700699
Chris Masone6515aab2011-10-12 16:19:09 -0700700 // Now, move the Service |s2| to another profile.
701 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
702 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700703
704 // Force destruction of the original Profile, to ensure that the Service
705 // is kept alive and populated with data.
706 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700707 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700708 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700709}
710
Paul Stewart7f61e522012-03-22 11:13:45 -0700711TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
712 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800713 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700714 const string kProfileName("profile0");
715 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
716 .WillRepeatedly(Return(kProfileName));
717 AdoptProfile(manager(), mock_profile);
718
719 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
720 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
721 EXPECT_EQ(mock_profile.get(), profile.get());
722}
723
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800724TEST_F(ManagerTest, SetProfileForService) {
725 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800726 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800727 string profile_name0("profile0");
728 EXPECT_CALL(*profile0, GetRpcIdentifier())
729 .WillRepeatedly(Return(profile_name0));
730 AdoptProfile(manager(), profile0);
731 scoped_refptr<MockService> service(new MockService(control_interface(),
732 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800733 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800734 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700735 EXPECT_FALSE(manager()->HasService(service));
736 {
737 Error error;
738 EXPECT_CALL(*profile0, AdoptService(_))
739 .WillOnce(Return(true));
740 // Expect that setting the profile of a service that does not already
741 // have one assigned does not cause a crash.
742 manager()->SetProfileForService(service, "profile0", &error);
743 EXPECT_TRUE(error.IsSuccess());
744 }
745
746 // The service should be registered as a side-effect of the profile being
747 // set for this service.
748 EXPECT_TRUE(manager()->HasService(service));
749
750 // Since we have mocked Profile::AdoptServie() above, the service's
751 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800752 service->set_profile(profile0);
753
754 {
755 Error error;
756 manager()->SetProfileForService(service, "foo", &error);
757 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700758 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800759 }
760
761 {
762 Error error;
763 manager()->SetProfileForService(service, profile_name0, &error);
764 EXPECT_EQ(Error::kInvalidArguments, error.type());
765 EXPECT_EQ("Service is already connected to this profile", error.message());
766 }
767
768 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800769 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800770 string profile_name1("profile1");
771 EXPECT_CALL(*profile1, GetRpcIdentifier())
772 .WillRepeatedly(Return(profile_name1));
773 AdoptProfile(manager(), profile1);
774
775 {
776 Error error;
777 EXPECT_CALL(*profile1, AdoptService(_))
778 .WillOnce(Return(true));
779 EXPECT_CALL(*profile0, AbandonService(_))
780 .WillOnce(Return(true));
781 manager()->SetProfileForService(service, profile_name1, &error);
782 EXPECT_TRUE(error.IsSuccess());
783 }
784}
785
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700786TEST_F(ManagerTest, CreateProfile) {
787 // It's much easier to use real Glib here since we want the storage
788 // side-effects.
789 GLib glib;
790 ScopedTempDir temp_dir;
791 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
792
793 Manager manager(control_interface(),
794 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800795 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700796 &glib,
797 run_path(),
798 storage_path(),
799 temp_dir.path().value());
800
801 // Invalid name should be rejected.
802 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
803
Paul Stewartd0a3b812012-03-28 22:48:22 -0700804 // A profile with invalid characters in it should similarly be rejected.
805 EXPECT_EQ(Error::kInvalidArguments,
806 TestCreateProfile(&manager, "valid_profile"));
807
808 // We should be able to create a machine profile.
809 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700810
Gary Morainb672d352012-04-25 09:19:06 -0700811 // We should succeed in creating a valid user profile. Verify the returned
812 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700813 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700814 {
815 Error error;
816 string path;
817 manager.CreateProfile(kProfile, &path, &error);
818 EXPECT_EQ(Error::kSuccess, error.type());
819 EXPECT_EQ("/profile_rpc", path);
820 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700821
822 // We should fail in creating it a second time (already exists).
823 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
824}
825
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700826// We receive PopProfile when a user logs out, and it should always trigger a
827// MemoryLog Clear() call.
828TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
829 GLib glib;
830 ScopedTempDir temp_dir;
831 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
832 Manager manager(control_interface(),
833 dispatcher(),
834 metrics(),
835 &glib,
836 run_path(),
837 storage_path(),
838 temp_dir.path().value());
839 const char kProfile0[] = "~user/profile0";
840 const char kPurgedMessage[] = "This message should be purged";
841 // Create a profile and push it on the stack, leave one uncreated
842 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
843 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
844
845 // Popping a profile which isn't on top should still clear the log.
846 LOG(INFO) << kPurgedMessage;
847 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
848 kPurgedMessage));
849 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
850 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
851 kPurgedMessage));
852
853 // Popping an invalid profile name should do the same thing.
854 LOG(INFO) << kPurgedMessage;
855 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
856 kPurgedMessage));
857 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
858 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
859 kPurgedMessage));
860
861 // Successful pops also purge the message log.
862 LOG(INFO) << kPurgedMessage;
863 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
864 kPurgedMessage));
865 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
866 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
867 kPurgedMessage));
868}
869
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700870TEST_F(ManagerTest, PushPopProfile) {
871 // It's much easier to use real Glib in creating a Manager for this
872 // test here since we want the storage side-effects.
873 GLib glib;
874 ScopedTempDir temp_dir;
875 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
876 Manager manager(control_interface(),
877 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800878 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700879 &glib,
880 run_path(),
881 storage_path(),
882 temp_dir.path().value());
883
884 // Pushing an invalid profile should fail.
885 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
886
Paul Stewartd0a3b812012-03-28 22:48:22 -0700887 // Pushing a default profile that does not exist should fail.
888 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700889
890 const char kProfile0[] = "~user/profile0";
891 const char kProfile1[] = "~user/profile1";
892
893 // Create a couple of profiles.
894 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
895 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
896
897 // Push these profiles on the stack.
898 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
899 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
900
901 // Pushing a profile a second time should fail.
902 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
903 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
904
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800905 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700906 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800907 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700908
909 // Make sure a profile name that doesn't exist fails.
910 const char kProfile2Id[] = "profile2";
911 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
912 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
913
914 // Create a new service, with a specific storage name.
915 scoped_refptr<MockService> service(
916 new NiceMock<MockService>(control_interface(),
917 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800918 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700919 &manager));
920 const char kServiceName[] = "service_storage_name";
921 EXPECT_CALL(*service.get(), GetStorageIdentifier())
922 .WillRepeatedly(Return(kServiceName));
923 EXPECT_CALL(*service.get(), Load(_))
924 .WillRepeatedly(Return(true));
925
926 // Add this service to the manager -- it should end up in the ephemeral
927 // profile.
928 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800929 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700930
931 // Create storage for a profile that contains the service storage name.
932 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
933 kServiceName));
934
935 // When we push the profile, the service should move away from the
936 // ephemeral profile to this new profile since it has an entry for
937 // this service.
938 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800939 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700940 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
941
942 // Insert another profile that should supersede ownership of the service.
943 const char kProfile3Id[] = "profile3";
944 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
945 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
946 kServiceName));
947 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
948 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
949
950 // Popping an invalid profile name should fail.
951 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
952
953 // Popping an profile that is not at the top of the stack should fail.
954 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
955
956 // Popping the top profile should succeed.
957 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
958
959 // Moreover the service should have switched profiles to profile 2.
960 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
961
962 // Popping the top profile should succeed.
963 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
964
965 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800966 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700967
968 // Pop the remaining two services off the stack.
969 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
970 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
971
972 // Next pop should fail with "stack is empty".
973 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700974
975 const char kMachineProfile0[] = "machineprofile0";
976 const char kMachineProfile1[] = "machineprofile1";
977 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
978 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
979
980 // Should be able to push a machine profile.
981 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
982
983 // Should be able to push a user profile atop a machine profile.
984 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
985
986 // Pushing a system-wide profile on top of a user profile should fail.
987 EXPECT_EQ(Error::kInvalidArguments,
988 TestPushProfile(&manager, kMachineProfile1));
989
990 // However if we pop the user profile, we should be able stack another
991 // machine profile on.
992 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
993 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -0700994
995 // Add two user profiles to the top of the stack.
996 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
997 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
998 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
999 EXPECT_EQ(4, profiles.size());
1000
1001 // PopAllUserProfiles should remove both user profiles, leaving the two
1002 // machine profiles.
1003 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1004 EXPECT_EQ(2, profiles.size());
1005 EXPECT_TRUE(profiles[0]->GetUser().empty());
1006 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001007
1008 // Use InsertUserProfile() instead. Although a machine profile is valid
1009 // in this state, it cannot be added via InsertUserProfile.
1010 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1011 EXPECT_EQ(Error::kInvalidArguments,
1012 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1013 const char kUserHash0[] = "userhash0";
1014 const char kUserHash1[] = "userhash1";
1015 EXPECT_EQ(Error::kSuccess,
1016 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1017 EXPECT_EQ(Error::kSuccess,
1018 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1019 EXPECT_EQ(3, profiles.size());
1020 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1021 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001022}
1023
Paul Stewarte73d05c2012-03-29 16:26:05 -07001024TEST_F(ManagerTest, RemoveProfile) {
1025 // It's much easier to use real Glib in creating a Manager for this
1026 // test here since we want the storage side-effects.
1027 GLib glib;
1028 ScopedTempDir temp_dir;
1029 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1030 Manager manager(control_interface(),
1031 dispatcher(),
1032 metrics(),
1033 &glib,
1034 run_path(),
1035 storage_path(),
1036 temp_dir.path().value());
1037
1038 const char kProfile0[] = "profile0";
1039 FilePath profile_path(
1040 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1041
1042 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1043 ASSERT_TRUE(file_util::PathExists(profile_path));
1044
1045 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1046
1047 // Remove should fail since the profile is still on the stack.
1048 {
1049 Error error;
1050 manager.RemoveProfile(kProfile0, &error);
1051 EXPECT_EQ(Error::kInvalidArguments, error.type());
1052 }
1053
1054 // Profile path should still exist.
1055 EXPECT_TRUE(file_util::PathExists(profile_path));
1056
1057 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1058
1059 // This should succeed now that the profile is off the stack.
1060 {
1061 Error error;
1062 manager.RemoveProfile(kProfile0, &error);
1063 EXPECT_EQ(Error::kSuccess, error.type());
1064 }
1065
1066 // Profile path should no longer exist.
1067 EXPECT_FALSE(file_util::PathExists(profile_path));
1068
1069 // Another remove succeeds, due to a foible in file_util::Delete --
1070 // it is not an error to delete a file that does not exist.
1071 {
1072 Error error;
1073 manager.RemoveProfile(kProfile0, &error);
1074 EXPECT_EQ(Error::kSuccess, error.type());
1075 }
1076
1077 // Let's create an error case that will "work". Create a non-empty
1078 // directory in the place of the profile pathname.
1079 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
1080 {
1081 Error error;
1082 manager.RemoveProfile(kProfile0, &error);
1083 EXPECT_EQ(Error::kOperationFailed, error.type());
1084 }
1085}
1086
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001087TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1088 // It's much easier to use real Glib in creating a Manager for this
1089 // test here since we want the storage side-effects.
1090 GLib glib;
1091 ScopedTempDir temp_dir;
1092 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1093 Manager manager(control_interface(),
1094 dispatcher(),
1095 metrics(),
1096 &glib,
1097 run_path(),
1098 storage_path(),
1099 temp_dir.path().value());
1100
1101 const char kProfile0[] = "profile0";
1102 FilePath profile_path(
1103 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1104
1105 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1106 ASSERT_TRUE(file_util::PathExists(profile_path));
1107 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1108
1109 // Ensure that even if the backing filestore is removed, we still can't
1110 // create a profile twice.
1111 ASSERT_TRUE(file_util::Delete(profile_path, false));
1112 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1113}
1114
Paul Stewart75225512012-01-26 22:51:33 -08001115// Use this matcher instead of passing RefPtrs directly into the arguments
1116// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1117// system teardown.
1118MATCHER_P(IsRefPtrTo, ref_address, "") {
1119 return arg.get() == ref_address;
1120}
1121
1122TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1123 MockServiceRefPtr s_not_in_profile(
1124 new NiceMock<MockService>(control_interface(),
1125 dispatcher(),
1126 metrics(),
1127 manager()));
1128 MockServiceRefPtr s_not_in_group(
1129 new NiceMock<MockService>(control_interface(),
1130 dispatcher(),
1131 metrics(),
1132 manager()));
1133 MockServiceRefPtr s_configure_fail(
1134 new NiceMock<MockService>(control_interface(),
1135 dispatcher(),
1136 metrics(),
1137 manager()));
1138 MockServiceRefPtr s_configure_succeed(
1139 new NiceMock<MockService>(control_interface(),
1140 dispatcher(),
1141 metrics(),
1142 manager()));
1143
1144 string entry_name("entry_name");
1145 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1146 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1147 .WillRepeatedly(Return("not_entry_name"));
1148 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1149 .WillRepeatedly(Return(entry_name));
1150 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1151 .WillRepeatedly(Return(entry_name));
1152
1153 manager()->RegisterService(s_not_in_profile);
1154 manager()->RegisterService(s_not_in_group);
1155 manager()->RegisterService(s_configure_fail);
1156 manager()->RegisterService(s_configure_succeed);
1157
1158 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001159 new StrictMock<MockProfile>(
1160 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001161 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001162 new StrictMock<MockProfile>(
1163 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001164
1165 s_not_in_group->set_profile(profile1);
1166 s_configure_fail->set_profile(profile1);
1167 s_configure_succeed->set_profile(profile1);
1168
1169 AdoptProfile(manager(), profile0);
1170 AdoptProfile(manager(), profile1);
1171
1172 // No services are a member of this profile.
1173 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1174
1175 // No services that are members of this profile have this entry name.
1176 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1177
1178 // Only services that are members of the profile and group will be abandoned.
1179 EXPECT_CALL(*profile1.get(),
1180 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1181 EXPECT_CALL(*profile1.get(),
1182 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1183 EXPECT_CALL(*profile1.get(),
1184 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1185 .WillOnce(Return(true));
1186 EXPECT_CALL(*profile1.get(),
1187 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1188 .WillOnce(Return(true));
1189
1190 // Never allow services to re-join profile1.
1191 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1192 .WillRepeatedly(Return(false));
1193
1194 // Only allow one of the members of the profile and group to successfully
1195 // join profile0.
1196 EXPECT_CALL(*profile0.get(),
1197 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1198 EXPECT_CALL(*profile0.get(),
1199 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1200 EXPECT_CALL(*profile0.get(),
1201 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1202 .WillOnce(Return(false));
1203 EXPECT_CALL(*profile0.get(),
1204 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1205 .WillOnce(Return(true));
1206
1207 // Expect the failed-to-configure service to have Unload() called on it.
1208 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1209 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1210 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1211 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1212
1213 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1214
1215 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1216 EXPECT_EQ(profile1, s_not_in_group->profile());
1217 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1218
1219 // Since we are using a MockProfile, the profile does not actually change,
1220 // since ConfigureService was not actually called on the service.
1221 EXPECT_EQ(profile1, s_configure_succeed->profile());
1222}
1223
Paul Stewart65512e12012-03-26 18:01:08 -07001224TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1225 MockServiceRefPtr s_will_remove0(
1226 new NiceMock<MockService>(control_interface(),
1227 dispatcher(),
1228 metrics(),
1229 manager()));
1230 MockServiceRefPtr s_will_remove1(
1231 new NiceMock<MockService>(control_interface(),
1232 dispatcher(),
1233 metrics(),
1234 manager()));
1235 MockServiceRefPtr s_will_not_remove0(
1236 new NiceMock<MockService>(control_interface(),
1237 dispatcher(),
1238 metrics(),
1239 manager()));
1240 MockServiceRefPtr s_will_not_remove1(
1241 new NiceMock<MockService>(control_interface(),
1242 dispatcher(),
1243 metrics(),
1244 manager()));
1245
1246 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1247 .Times(4); // Once for each registration.
1248
1249 string entry_name("entry_name");
1250 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1251 .WillRepeatedly(Return(entry_name));
1252 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1253 .WillRepeatedly(Return(entry_name));
1254 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1255 .WillRepeatedly(Return(entry_name));
1256 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1257 .WillRepeatedly(Return(entry_name));
1258
1259 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001260 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001261 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001262 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001263 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001264 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001265 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001266 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001267
1268 // One for each service added above.
1269 ASSERT_EQ(4, manager()->services_.size());
1270
1271 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001272 new StrictMock<MockProfile>(
1273 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001274
1275 s_will_remove0->set_profile(profile);
1276 s_will_remove1->set_profile(profile);
1277 s_will_not_remove0->set_profile(profile);
1278 s_will_not_remove1->set_profile(profile);
1279
1280 AdoptProfile(manager(), profile);
1281
1282 // Deny any of the services re-entry to the profile.
1283 EXPECT_CALL(*profile, ConfigureService(_))
1284 .WillRepeatedly(Return(false));
1285
1286 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1287 .WillOnce(Return(true));
1288 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1289 .WillOnce(Return(true));
1290 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1291 .WillOnce(Return(true));
1292 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1293 .WillOnce(Return(true));
1294
1295 EXPECT_CALL(*s_will_remove0, Unload())
1296 .WillOnce(Return(true));
1297 EXPECT_CALL(*s_will_remove1, Unload())
1298 .WillOnce(Return(true));
1299 EXPECT_CALL(*s_will_not_remove0, Unload())
1300 .WillOnce(Return(false));
1301 EXPECT_CALL(*s_will_not_remove1, Unload())
1302 .WillOnce(Return(false));
1303
1304
1305 // This will cause all the profiles to be unloaded.
1306 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1307
1308 // 2 of the 4 services added above should have been unregistered and
1309 // removed, leaving 2.
1310 EXPECT_EQ(2, manager()->services_.size());
1311 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1312 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1313}
1314
1315TEST_F(ManagerTest, PopProfileWithUnload) {
1316 MockServiceRefPtr s_will_remove0(
1317 new NiceMock<MockService>(control_interface(),
1318 dispatcher(),
1319 metrics(),
1320 manager()));
1321 MockServiceRefPtr s_will_remove1(
1322 new NiceMock<MockService>(control_interface(),
1323 dispatcher(),
1324 metrics(),
1325 manager()));
1326 MockServiceRefPtr s_will_not_remove0(
1327 new NiceMock<MockService>(control_interface(),
1328 dispatcher(),
1329 metrics(),
1330 manager()));
1331 MockServiceRefPtr s_will_not_remove1(
1332 new NiceMock<MockService>(control_interface(),
1333 dispatcher(),
1334 metrics(),
1335 manager()));
1336
1337 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1338 .Times(5); // Once for each registration, and one after profile pop.
1339
1340 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001341 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001342 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001343 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001344 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001345 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001346 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001347 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001348
1349 // One for each service added above.
1350 ASSERT_EQ(4, manager()->services_.size());
1351
1352 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001353 new StrictMock<MockProfile>(
1354 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001355 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001356 new StrictMock<MockProfile>(
1357 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001358
1359 s_will_remove0->set_profile(profile1);
1360 s_will_remove1->set_profile(profile1);
1361 s_will_not_remove0->set_profile(profile1);
1362 s_will_not_remove1->set_profile(profile1);
1363
1364 AdoptProfile(manager(), profile0);
1365 AdoptProfile(manager(), profile1);
1366
1367 // Deny any of the services entry to profile0, so they will all be unloaded.
1368 EXPECT_CALL(*profile0, ConfigureService(_))
1369 .WillRepeatedly(Return(false));
1370
1371 EXPECT_CALL(*s_will_remove0, Unload())
1372 .WillOnce(Return(true));
1373 EXPECT_CALL(*s_will_remove1, Unload())
1374 .WillOnce(Return(true));
1375 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001376 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001377 EXPECT_CALL(*s_will_not_remove1, Unload())
1378 .WillOnce(Return(false));
1379
Philipp Neubeck79173602012-11-13 21:10:09 +01001380 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1381 // profile list.
1382 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1383 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1384
Paul Stewart65512e12012-03-26 18:01:08 -07001385 // This will pop profile1, which should cause all our profiles to unload.
1386 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001387 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001388
1389 // 2 of the 4 services added above should have been unregistered and
1390 // removed, leaving 2.
1391 EXPECT_EQ(2, manager()->services_.size());
1392 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1393 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001394
1395 // Expect the unloaded services to lose their profile reference.
1396 EXPECT_FALSE(s_will_remove0->profile());
1397 EXPECT_FALSE(s_will_remove1->profile());
1398
1399 // If we explicitly deregister a service, the effect should be the same
1400 // with respect to the profile reference.
1401 ASSERT_TRUE(s_will_not_remove0->profile());
1402 manager()->DeregisterService(s_will_not_remove0);
1403 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001404}
1405
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001406TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001407 {
1408 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001409 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1410 flimflam::kOfflineModeProperty,
1411 PropertyStoreTest::kBoolV,
1412 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001413 }
1414 {
1415 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001416 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1417 flimflam::kCountryProperty,
1418 PropertyStoreTest::kStringV,
1419 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001420 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001421 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001422 {
1423 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001424 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1425 flimflam::kCountryProperty,
1426 PropertyStoreTest::kBoolV,
1427 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001428 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001429 }
1430 {
1431 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001432 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1433 flimflam::kOfflineModeProperty,
1434 PropertyStoreTest::kStringV,
1435 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001436 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001437 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001438 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001439 {
1440 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001441 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001442 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001443 flimflam::kEnabledTechnologiesProperty,
1444 PropertyStoreTest::kStringsV,
1445 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001446 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001447 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001448}
1449
mukesh agrawal32399322011-09-01 10:53:43 -07001450TEST_F(ManagerTest, RequestScan) {
1451 {
1452 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001453 manager()->RegisterDevice(mock_devices_[0].get());
1454 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001455 EXPECT_CALL(*mock_devices_[0], technology())
1456 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie68d41092013-04-02 12:56:02 -07001457 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _));
Joshua Krollda798622012-06-05 12:30:48 -07001458 EXPECT_CALL(*mock_devices_[1], technology())
1459 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie68d41092013-04-02 12:56:02 -07001460 EXPECT_CALL(*mock_devices_[1], Scan(_, _)).Times(0);
1461 manager()->RequestScan(Device::kFullScan, flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001462 }
1463
1464 {
1465 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001466 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001467 EXPECT_EQ(Error::kInvalidArguments, error.type());
1468 }
1469}
1470
Darin Petkovb65c2452012-02-23 15:17:06 +01001471TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001472 KeyValueStore args;
1473 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001474 manager()->GetService(args, &e);
1475 EXPECT_EQ(Error::kInvalidArguments, e.type());
1476 EXPECT_EQ("must specify service type", e.message());
1477}
1478
1479TEST_F(ManagerTest, GetServiceUnknownType) {
1480 KeyValueStore args;
1481 Error e;
1482 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1483 manager()->GetService(args, &e);
1484 EXPECT_EQ(Error::kNotSupported, e.type());
1485 EXPECT_EQ("service type is unsupported", e.message());
1486}
1487
Paul Stewart35eff132013-04-12 12:08:40 -07001488TEST_F(ManagerTest, GetServiceEthernetEap) {
1489 KeyValueStore args;
1490 Error e;
1491 ServiceRefPtr service;
1492 args.SetString(flimflam::kTypeProperty, kTypeEthernetEap);
1493 SetEapProviderService(service);
1494 EXPECT_EQ(service, manager()->GetService(args, &e));
1495 EXPECT_TRUE(e.IsSuccess());
1496}
1497
Darin Petkovb65c2452012-02-23 15:17:06 +01001498TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001499 KeyValueStore args;
1500 Error e;
1501 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001502 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001503 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001504 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001505 manager()->GetService(args, &e);
1506 EXPECT_TRUE(e.IsSuccess());
1507}
1508
Darin Petkov33af05c2012-02-28 10:10:30 +01001509TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1510 KeyValueStore args;
1511 Error e;
1512 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001513 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001514 new StrictMock<MockProfile>(
1515 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001516 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001517 ServiceRefPtr service = manager()->GetService(args, &e);
1518 EXPECT_EQ(Error::kNotSupported, e.type());
1519 EXPECT_FALSE(service);
1520}
1521
Darin Petkovb65c2452012-02-23 15:17:06 +01001522TEST_F(ManagerTest, GetServiceVPN) {
1523 KeyValueStore args;
1524 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001525 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001526 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001527 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
Darin Petkov4e02ba22013-04-02 13:44:08 +02001528 args.SetString(flimflam::kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001529 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001530 new StrictMock<MockProfile>(
1531 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001532 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001533
1534#if defined(DISABLE_VPN)
1535
1536 ServiceRefPtr service = manager()->GetService(args, &e);
1537 EXPECT_EQ(Error::kNotSupported, e.type());
1538 EXPECT_FALSE(service);
1539
1540#else
1541
Paul Stewart7f5ad572012-06-04 15:18:54 -07001542 ServiceRefPtr updated_service;
1543 EXPECT_CALL(*profile, UpdateService(_))
1544 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1545 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001546 EXPECT_CALL(*profile, LoadService(_))
1547 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001548 EXPECT_CALL(*profile, ConfigureService(_))
1549 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001550 ServiceRefPtr service = manager()->GetService(args, &e);
1551 EXPECT_TRUE(e.IsSuccess());
1552 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001553 EXPECT_EQ(service, updated_service);
1554 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001555
1556#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001557}
1558
Darin Petkovc63dcf02012-05-24 11:51:43 +02001559TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1560 KeyValueStore args;
1561 Error e;
1562 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1563 ServiceRefPtr service = manager()->GetService(args, &e);
1564 EXPECT_EQ(Error::kInvalidArguments, e.type());
1565 EXPECT_EQ("Missing WiMAX network id.", e.message());
1566 EXPECT_FALSE(service);
1567}
1568
Darin Petkovd1cd7972012-05-22 15:26:15 +02001569TEST_F(ManagerTest, GetServiceWiMax) {
1570 KeyValueStore args;
1571 Error e;
1572 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001573 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1574 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1575 ServiceRefPtr service = manager()->GetService(args, &e);
1576 EXPECT_TRUE(e.IsSuccess());
1577 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001578}
1579
Paul Stewart7f61e522012-03-22 11:13:45 -07001580TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1581 // Manager calls ActiveProfile() so we need at least one profile installed.
1582 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001583 new NiceMock<MockProfile>(
1584 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001585 AdoptProfile(manager(), profile);
1586
1587 KeyValueStore args;
1588 args.SetString(flimflam::kProfileProperty, "xxx");
1589 Error error;
1590 manager()->ConfigureService(args, &error);
1591 EXPECT_EQ(Error::kInvalidArguments, error.type());
1592 EXPECT_EQ("Invalid profile name xxx", error.message());
1593}
1594
1595TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1596 // Manager calls ActiveProfile() so we need at least one profile installed.
1597 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001598 new NiceMock<MockProfile>(
1599 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001600 AdoptProfile(manager(), profile);
1601
1602 KeyValueStore args;
1603 Error error;
1604 manager()->ConfigureService(args, &error);
1605 EXPECT_EQ(Error::kInvalidArguments, error.type());
1606 EXPECT_EQ("must specify service type", error.message());
1607}
1608
1609// A registered service in the ephemeral profile should be moved to the
1610// active profile as a part of configuration if no profile was explicitly
1611// specified.
1612TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1613 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001614 new NiceMock<MockProfile>(
1615 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001616
1617 AdoptProfile(manager(), profile); // This is now the active profile.
1618
Paul Stewartd2e1c362013-03-03 19:06:07 -08001619 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001620 scoped_refptr<MockWiFiService> service(
1621 new NiceMock<MockWiFiService>(control_interface(),
1622 dispatcher(),
1623 metrics(),
1624 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001625 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001626 ssid,
1627 "",
1628 "",
1629 false));
1630
1631 manager()->RegisterService(service);
1632 service->set_profile(GetEphemeralProfile(manager()));
1633
Paul Stewart3c504012013-01-17 17:49:58 -08001634 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001635 .WillOnce(Return(service));
1636 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1637 .WillOnce(Return(true));
1638 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1639 .WillOnce(Return(true));
1640
1641 KeyValueStore args;
1642 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1643 Error error;
1644 manager()->ConfigureService(args, &error);
1645 EXPECT_TRUE(error.IsSuccess());
1646}
1647
Paul Stewart2c575d22012-12-07 12:28:57 -08001648// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001649// specify a profile, it should be moved from the profile it was previously
1650// in to the specified profile if one was requested.
1651TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1652 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001653 new NiceMock<MockProfile>(
1654 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001655 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001656 new NiceMock<MockProfile>(
1657 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001658
1659 const string kProfileName0 = "profile0";
1660 const string kProfileName1 = "profile1";
1661
1662 EXPECT_CALL(*profile0, GetRpcIdentifier())
1663 .WillRepeatedly(Return(kProfileName0));
1664 EXPECT_CALL(*profile1, GetRpcIdentifier())
1665 .WillRepeatedly(Return(kProfileName1));
1666
1667 AdoptProfile(manager(), profile0);
1668 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1669
Paul Stewartd2e1c362013-03-03 19:06:07 -08001670 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001671 scoped_refptr<MockWiFiService> service(
1672 new NiceMock<MockWiFiService>(control_interface(),
1673 dispatcher(),
1674 metrics(),
1675 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001676 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001677 ssid,
1678 "",
1679 "",
1680 false));
1681
1682 manager()->RegisterService(service);
1683 service->set_profile(profile1);
1684
Paul Stewart3c504012013-01-17 17:49:58 -08001685 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001686 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001687 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1688 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001689 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1690 .WillOnce(Return(true));
1691 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1692 .WillOnce(Return(true));
1693 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1694 .WillOnce(Return(true));
1695
1696 KeyValueStore args;
1697 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1698 args.SetString(flimflam::kProfileProperty, kProfileName0);
1699 Error error;
1700 manager()->ConfigureService(args, &error);
1701 EXPECT_TRUE(error.IsSuccess());
1702 service->set_profile(NULL); // Breaks refcounting loop.
1703}
1704
Paul Stewart2c575d22012-12-07 12:28:57 -08001705// If we configure a service that is already a member of the specified
1706// profile, the Manager should not call LoadService or AdoptService again
1707// on this service.
1708TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1709 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001710 new NiceMock<MockProfile>(
1711 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001712
1713 const string kProfileName0 = "profile0";
1714
1715 EXPECT_CALL(*profile0, GetRpcIdentifier())
1716 .WillRepeatedly(Return(kProfileName0));
1717
1718 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1719
Paul Stewartd2e1c362013-03-03 19:06:07 -08001720 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001721 scoped_refptr<MockWiFiService> service(
1722 new NiceMock<MockWiFiService>(control_interface(),
1723 dispatcher(),
1724 metrics(),
1725 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001726 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001727 ssid,
1728 "",
1729 "",
1730 false));
1731
1732 manager()->RegisterService(service);
1733 service->set_profile(profile0);
1734
Paul Stewart3c504012013-01-17 17:49:58 -08001735 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001736 .WillOnce(Return(service));
1737 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1738 .Times(0);
1739 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1740 .WillOnce(Return(true));
1741 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1742 .Times(0);
1743
1744 KeyValueStore args;
1745 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1746 args.SetString(flimflam::kProfileProperty, kProfileName0);
1747 Error error;
1748 manager()->ConfigureService(args, &error);
1749 EXPECT_TRUE(error.IsSuccess());
1750 service->set_profile(NULL); // Breaks refcounting loop.
1751}
1752
Paul Stewart7f61e522012-03-22 11:13:45 -07001753// An unregistered service should remain unregistered, but its contents should
1754// be saved to the specified profile nonetheless.
1755TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1756 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001757 new NiceMock<MockProfile>(
1758 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001759 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001760 new NiceMock<MockProfile>(
1761 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001762
1763 const string kProfileName0 = "profile0";
1764 const string kProfileName1 = "profile1";
1765
1766 EXPECT_CALL(*profile0, GetRpcIdentifier())
1767 .WillRepeatedly(Return(kProfileName0));
1768 EXPECT_CALL(*profile1, GetRpcIdentifier())
1769 .WillRepeatedly(Return(kProfileName1));
1770
1771 AdoptProfile(manager(), profile0);
1772 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1773
Paul Stewartd2e1c362013-03-03 19:06:07 -08001774 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001775 scoped_refptr<MockWiFiService> service(
1776 new NiceMock<MockWiFiService>(control_interface(),
1777 dispatcher(),
1778 metrics(),
1779 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001780 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001781 ssid,
1782 "",
1783 "",
1784 false));
1785
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));
1790 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1791 .WillOnce(Return(true));
1792 EXPECT_CALL(*profile0, AdoptService(_))
1793 .Times(0);
1794 EXPECT_CALL(*profile1, AdoptService(_))
1795 .Times(0);
1796
1797 KeyValueStore args;
1798 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1799 args.SetString(flimflam::kProfileProperty, kProfileName0);
1800 Error error;
1801 manager()->ConfigureService(args, &error);
1802 EXPECT_TRUE(error.IsSuccess());
1803}
1804
Paul Stewartd2e1c362013-03-03 19:06:07 -08001805TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1806 KeyValueStore args;
1807 Error error;
1808 ServiceRefPtr service =
1809 manager()->ConfigureServiceForProfile("", args, &error);
1810 EXPECT_EQ(Error::kNotSupported, error.type());
1811 EXPECT_EQ("This method only supports WiFi services", error.message());
1812 EXPECT_EQ(NULL, service.get());
1813}
1814
1815TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1816 KeyValueStore args;
1817 args.SetString(flimflam::kTypeProperty, flimflam::kTypeCellular);
1818 Error error;
1819 ServiceRefPtr service =
1820 manager()->ConfigureServiceForProfile("", args, &error);
1821 EXPECT_EQ(Error::kNotSupported, error.type());
1822 EXPECT_EQ("This method only supports WiFi services", error.message());
1823 EXPECT_EQ(NULL, service.get());
1824}
1825
1826TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1827 KeyValueStore args;
1828 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1829 Error error;
1830 ServiceRefPtr service =
1831 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1832 EXPECT_EQ(Error::kNotFound, error.type());
1833 EXPECT_EQ("Profile specified was not found", error.message());
1834 EXPECT_EQ(NULL, service.get());
1835}
1836
1837TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1838 const string kProfileName0 = "profile0";
1839 const string kProfileName1 = "profile1";
1840 scoped_refptr<MockProfile> profile0(
1841 AddNamedMockProfileToManager(manager(), kProfileName0));
1842
1843 KeyValueStore args;
1844 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1845 args.SetString(flimflam::kProfileProperty, kProfileName1);
1846 Error error;
1847 ServiceRefPtr service =
1848 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1849 EXPECT_EQ(Error::kInvalidArguments, error.type());
1850 EXPECT_EQ("Profile argument does not match that in "
1851 "the configuration arguments", error.message());
1852 EXPECT_EQ(NULL, service.get());
1853}
1854
1855TEST_F(ManagerTest,
1856 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1857 const string kProfileName0 = "profile0";
1858 scoped_refptr<MockProfile> profile0(
1859 AddNamedMockProfileToManager(manager(), kProfileName0));
1860 KeyValueStore args;
1861 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1862 args.SetString(flimflam::kProfileProperty, kProfileName0);
1863
1864 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1865 .WillOnce(Return(WiFiServiceRefPtr()));
1866 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1867 .WillOnce(Return(WiFiServiceRefPtr()));
1868 Error error;
1869 ServiceRefPtr service =
1870 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1871 // Since we didn't set the error in the GetService expectation above...
1872 EXPECT_TRUE(error.IsSuccess());
1873 EXPECT_EQ(NULL, service.get());
1874}
1875
1876TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1877 const string kProfileName0 = "profile0";
1878 scoped_refptr<MockProfile> profile0(
1879 AddNamedMockProfileToManager(manager(), kProfileName0));
1880
1881 KeyValueStore args;
1882 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1883
1884 scoped_refptr<MockWiFiService> mock_service(
1885 new NiceMock<MockWiFiService>(control_interface(),
1886 dispatcher(),
1887 metrics(),
1888 manager(),
1889 wifi_provider_,
1890 vector<uint8_t>(),
1891 flimflam::kModeManaged,
1892 flimflam::kSecurityNone,
1893 false));
1894 ServiceRefPtr mock_service_generic(mock_service.get());
1895 mock_service->set_profile(profile0);
1896 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1897 .WillOnce(Return(WiFiServiceRefPtr()));
1898 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
1899 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
1900 .WillOnce(Return(true));
1901 Error error;
1902 ServiceRefPtr service =
1903 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1904 EXPECT_TRUE(error.IsSuccess());
1905 EXPECT_EQ(mock_service.get(), service.get());
1906 mock_service->set_profile(NULL); // Breaks reference cycle.
1907}
1908
1909TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
1910 scoped_refptr<MockService> mock_service(
1911 new NiceMock<MockService>(control_interface(),
1912 dispatcher(),
1913 metrics(),
1914 manager()));
1915 const string kGUID = "a guid";
1916 mock_service->set_guid(kGUID);
1917 manager()->RegisterService(mock_service);
1918 ServiceRefPtr mock_service_generic(mock_service.get());
1919
1920 const string kProfileName = "profile";
1921 scoped_refptr<MockProfile> profile(
1922 AddNamedMockProfileToManager(manager(), kProfileName));
1923 mock_service->set_profile(profile);
1924
1925 EXPECT_CALL(*mock_service, technology())
1926 .WillOnce(Return(Technology::kCellular))
1927 .WillOnce(Return(Technology::kWifi));
1928
1929 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
1930 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1931 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1932
1933 KeyValueStore args;
1934 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1935 args.SetString(flimflam::kGuidProperty, kGUID);
1936
1937 // The first attempt should fail because the service reports a technology
1938 // other than "WiFi".
1939 {
1940 Error error;
1941 ServiceRefPtr service =
1942 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1943 EXPECT_EQ(NULL, service.get());
1944 EXPECT_EQ(Error::kNotSupported, error.type());
1945 EXPECT_EQ("This GUID matches a non-WiFi service", error.message());
1946 }
1947
1948 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
1949 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
1950
1951 {
1952 Error error;
1953 ServiceRefPtr service =
1954 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1955 EXPECT_TRUE(error.IsSuccess());
1956 EXPECT_EQ(mock_service.get(), service.get());
1957 EXPECT_EQ(profile.get(), service->profile().get());
1958 }
1959 mock_service->set_profile(NULL); // Breaks reference cycle.
1960}
1961
1962TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
1963 const string kProfileName = "profile";
1964 scoped_refptr<MockProfile> profile(
1965 AddNamedMockProfileToManager(manager(), kProfileName));
1966
1967 scoped_refptr<MockWiFiService> mock_service(
1968 new NiceMock<MockWiFiService>(control_interface(),
1969 dispatcher(),
1970 metrics(),
1971 manager(),
1972 wifi_provider_,
1973 vector<uint8_t>(),
1974 flimflam::kModeManaged,
1975 flimflam::kSecurityNone,
1976 false));
1977 mock_service->set_profile(profile);
1978 ServiceRefPtr mock_service_generic(mock_service.get());
1979
1980 KeyValueStore args;
1981 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1982 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1983 .WillOnce(Return(mock_service));
1984 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1985 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1986 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
1987 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
1988
1989 Error error;
1990 ServiceRefPtr service =
1991 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1992 EXPECT_TRUE(error.IsSuccess());
1993 EXPECT_EQ(mock_service.get(), service.get());
1994 EXPECT_EQ(profile.get(), service->profile().get());
1995 mock_service->set_profile(NULL); // Breaks reference cycle.
1996}
1997
1998TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
1999 const string kProfileName = "profile";
2000 scoped_refptr<MockProfile> profile(
2001 AddNamedMockProfileToManager(manager(), kProfileName));
2002
2003 scoped_refptr<MockWiFiService> mock_service(
2004 new NiceMock<MockWiFiService>(control_interface(),
2005 dispatcher(),
2006 metrics(),
2007 manager(),
2008 wifi_provider_,
2009 vector<uint8_t>(),
2010 flimflam::kModeManaged,
2011 flimflam::kSecurityNone,
2012 false));
2013 mock_service->set_profile(GetEphemeralProfile(manager()));
2014 ServiceRefPtr mock_service_generic(mock_service.get());
2015
2016 KeyValueStore args;
2017 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2018 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2019 .WillOnce(Return(mock_service));
2020 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2021 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2022 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2023
2024 Error error;
2025 ServiceRefPtr service =
2026 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2027 EXPECT_TRUE(error.IsSuccess());
2028 EXPECT_EQ(mock_service.get(), service.get());
2029 EXPECT_EQ(profile.get(), service->profile().get());
2030 mock_service->set_profile(NULL); // Breaks reference cycle.
2031}
2032
2033TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2034 const string kProfileName0 = "profile0";
2035 scoped_refptr<MockProfile> profile0(
2036 AddNamedMockProfileToManager(manager(), kProfileName0));
2037 const string kProfileName1 = "profile1";
2038 scoped_refptr<MockProfile> profile1(
2039 AddNamedMockProfileToManager(manager(), kProfileName1));
2040
2041 scoped_refptr<MockWiFiService> mock_service(
2042 new NiceMock<MockWiFiService>(control_interface(),
2043 dispatcher(),
2044 metrics(),
2045 manager(),
2046 wifi_provider_,
2047 vector<uint8_t>(),
2048 flimflam::kModeManaged,
2049 flimflam::kSecurityNone,
2050 false));
2051 manager()->RegisterService(mock_service);
2052 mock_service->set_profile(profile0);
2053 ServiceRefPtr mock_service_generic(mock_service.get());
2054
2055 KeyValueStore args;
2056 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2057 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2058 .WillOnce(Return(mock_service));
2059 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2060 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2061 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2062 // This happens once to make the service loadable for the ConfigureService
2063 // below, and a second time after the service is modified.
2064 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2065 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2066 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2067 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2068
2069 Error error;
2070 ServiceRefPtr service =
2071 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2072 EXPECT_TRUE(error.IsSuccess());
2073 EXPECT_EQ(mock_service.get(), service.get());
2074 mock_service->set_profile(NULL); // Breaks reference cycle.
2075}
2076
2077TEST_F(ManagerTest,
2078 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2079 const string kProfileName0 = "profile0";
2080 scoped_refptr<MockProfile> profile0(
2081 AddNamedMockProfileToManager(manager(), kProfileName0));
2082 const string kProfileName1 = "profile1";
2083 scoped_refptr<MockProfile> profile1(
2084 AddNamedMockProfileToManager(manager(), kProfileName1));
2085
2086 scoped_refptr<MockWiFiService> matching_service(
2087 new StrictMock<MockWiFiService>(control_interface(),
2088 dispatcher(),
2089 metrics(),
2090 manager(),
2091 wifi_provider_,
2092 vector<uint8_t>(),
2093 flimflam::kModeManaged,
2094 flimflam::kSecurityNone,
2095 false));
2096 matching_service->set_profile(profile1);
2097
2098 // We need to get rid of our reference to this mock service as soon
2099 // as Manager::ConfigureServiceForProfile() takes a reference in its
2100 // call to WiFiProvider::CreateTemporaryService(). This way the
2101 // latter function can keep a DCHECK(service->HasOneRef() even in
2102 // unit tests.
2103 temp_mock_service_ =
2104 new NiceMock<MockWiFiService>(control_interface(),
2105 dispatcher(),
2106 metrics(),
2107 manager(),
2108 wifi_provider_,
2109 vector<uint8_t>(),
2110 flimflam::kModeManaged,
2111 flimflam::kSecurityNone,
2112 false);
2113
2114 // Only hold a pointer here so we don't affect the refcount.
2115 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2116
2117 KeyValueStore args;
2118 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2119 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2120 .WillOnce(Return(matching_service));
2121 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2122 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2123 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2124 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2125 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2126 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2127 .Times(1);
2128 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2129 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2130
2131 Error error;
2132 ServiceRefPtr service =
2133 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2134 EXPECT_TRUE(error.IsSuccess());
2135 EXPECT_EQ(NULL, service.get());
2136 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2137}
2138
Paul Stewart7a20aa42013-01-17 12:21:41 -08002139TEST_F(ManagerTest, FindMatchingService) {
2140 KeyValueStore args;
2141 {
2142 Error error;
2143 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2144 EXPECT_EQ(Error::kNotFound, error.type());
2145 }
2146
2147 scoped_refptr<MockService> mock_service0(
2148 new NiceMock<MockService>(control_interface(),
2149 dispatcher(),
2150 metrics(),
2151 manager()));
2152 scoped_refptr<MockService> mock_service1(
2153 new NiceMock<MockService>(control_interface(),
2154 dispatcher(),
2155 metrics(),
2156 manager()));
2157 manager()->RegisterService(mock_service0);
2158 manager()->RegisterService(mock_service1);
2159 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2160 .WillOnce(Return(true))
2161 .WillRepeatedly(Return(false));
2162 {
2163 Error error;
2164 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2165 EXPECT_TRUE(error.IsSuccess());
2166 }
2167 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2168 .WillOnce(Return(true))
2169 .WillRepeatedly(Return(false));
2170 {
2171 Error error;
2172 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2173 EXPECT_TRUE(error.IsSuccess());
2174 }
2175 {
2176 Error error;
2177 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2178 EXPECT_EQ(Error::kNotFound, error.type());
2179 }
2180}
2181
Paul Stewart22aa71b2011-09-16 12:15:11 -07002182TEST_F(ManagerTest, TechnologyOrder) {
2183 Error error;
2184 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
2185 string(flimflam::kTypeWifi), &error);
2186 ASSERT_TRUE(error.IsSuccess());
2187 EXPECT_EQ(manager()->GetTechnologyOrder(),
2188 string(flimflam::kTypeEthernet) + "," +
2189 string(flimflam::kTypeWifi));
2190
2191 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
2192 string(flimflam::kTypeWifi), &error);
2193 ASSERT_FALSE(error.IsSuccess());
2194 EXPECT_EQ(Error::kInvalidArguments, error.type());
2195 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
2196 string(flimflam::kTypeWifi),
2197 manager()->GetTechnologyOrder());
2198}
2199
2200TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002201 // TODO(quiche): Some of these tests would probably fit better in
2202 // service_unittest, since the actual comparison of Services is
2203 // implemented in Service. (crosbug.com/23370)
2204
Paul Stewart22aa71b2011-09-16 12:15:11 -07002205 scoped_refptr<MockService> mock_service0(
2206 new NiceMock<MockService>(control_interface(),
2207 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002208 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002209 manager()));
2210 scoped_refptr<MockService> mock_service1(
2211 new NiceMock<MockService>(control_interface(),
2212 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002213 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002214 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002215
2216 manager()->RegisterService(mock_service0);
2217 manager()->RegisterService(mock_service1);
2218
Darin Petkov457728b2013-01-09 09:49:08 +01002219 // Services should already be sorted by |unique_name_|
Paul Stewart22aa71b2011-09-16 12:15:11 -07002220 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2221
2222 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002223 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002224 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2225
2226 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01002227 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002228 manager()->UpdateService(mock_service1);
2229 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2230
2231 // Security
mukesh agrawal43970a22013-02-15 16:00:07 -08002232 mock_service0->SetSecurity(Service::kCryptoAes, true, true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002233 manager()->UpdateService(mock_service0);
2234 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2235
2236 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07002237 EXPECT_CALL(*mock_service0.get(), technology())
2238 .WillRepeatedly(Return((Technology::kWifi)));
2239 EXPECT_CALL(*mock_service1.get(), technology())
2240 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002241
2242 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002243 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002244 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002245 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2246
2247 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
2248 string(flimflam::kTypeEthernet), &error);
2249 EXPECT_TRUE(error.IsSuccess());
2250 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2251
Gaurav Shah435de2c2011-11-17 19:01:07 -08002252 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07002253 mock_service0->set_priority(1);
2254 manager()->UpdateService(mock_service0);
2255 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2256
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002257 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00002258 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002259 manager()->UpdateService(mock_service1);
2260 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2261
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002262 // Auto-connect.
2263 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002264 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002265 mock_service1->set_auto_connect(false);
2266 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002267 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2268
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002269 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2270 // to any of the others below, so we reset to the default state after
2271 // testing.
2272 EXPECT_CALL(*mock_service1.get(),
2273 IsDependentOn(ServiceRefPtr(mock_service0.get())))
2274 .WillOnce(Return(true))
2275 .WillRepeatedly(Return(false));
2276 manager()->UpdateService(mock_service1);
2277 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2278 manager()->UpdateService(mock_service0);
2279 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2280
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002281 // Connectable.
2282 mock_service1->set_connectable(true);
2283 manager()->UpdateService(mock_service1);
2284 mock_service0->set_connectable(false);
2285 manager()->UpdateService(mock_service0);
2286 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2287
2288 // IsFailed.
2289 EXPECT_CALL(*mock_service0.get(), state())
2290 .WillRepeatedly(Return(Service::kStateIdle));
2291 EXPECT_CALL(*mock_service0.get(), IsFailed())
2292 .WillRepeatedly(Return(false));
2293 manager()->UpdateService(mock_service0);
2294 EXPECT_CALL(*mock_service0.get(), state())
2295 .WillRepeatedly(Return(Service::kStateFailure));
2296 EXPECT_CALL(*mock_service1.get(), IsFailed())
2297 .WillRepeatedly(Return(true));
2298 manager()->UpdateService(mock_service1);
2299 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2300
2301 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07002302 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002303 .WillRepeatedly(Return(Service::kStateAssociating));
2304 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002305 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002306 manager()->UpdateService(mock_service1);
2307 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2308
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002309 // Connected.
2310 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002311 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002312 EXPECT_CALL(*mock_service0.get(), IsConnected())
2313 .WillRepeatedly(Return(true));
2314 manager()->UpdateService(mock_service0);
2315 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2316
Paul Stewarta121c442012-06-09 14:12:58 -07002317 // Portal.
2318 EXPECT_CALL(*mock_service1.get(), state())
2319 .WillRepeatedly(Return(Service::kStateConnected));
2320 EXPECT_CALL(*mock_service1.get(), IsConnected())
2321 .WillRepeatedly(Return(true));
2322 manager()->UpdateService(mock_service1);
2323 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2324
Paul Stewart22aa71b2011-09-16 12:15:11 -07002325 manager()->DeregisterService(mock_service0);
2326 manager()->DeregisterService(mock_service1);
2327}
2328
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002329TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002330 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002331 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002332
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002333 scoped_refptr<MockService> mock_service0(
2334 new NiceMock<MockService>(control_interface(),
2335 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002336 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002337 manager()));
2338 scoped_refptr<MockService> mock_service1(
2339 new NiceMock<MockService>(control_interface(),
2340 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002341 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002342 manager()));
2343
2344 scoped_refptr<MockConnection> mock_connection0(
2345 new NiceMock<MockConnection>(device_info_.get()));
2346 scoped_refptr<MockConnection> mock_connection1(
2347 new NiceMock<MockConnection>(device_info_.get()));
2348
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002349 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002350 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002351 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002352 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002353 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002354 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002355
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002356 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002357 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002358
2359 mock_service1->set_priority(1);
2360 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002361 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002362
2363 mock_service1->set_priority(0);
2364 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002365 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002366
Paul Stewartce4ec192012-03-14 12:53:46 -07002367 mock_service0->set_mock_connection(mock_connection0);
2368 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002369
2370 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002371 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002372 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002373
Darin Petkova5e07ef2012-07-09 14:27:57 +02002374 ServiceWatcher service_watcher;
2375 int tag =
2376 manager()->RegisterDefaultServiceCallback(
2377 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2378 service_watcher.AsWeakPtr()));
2379 EXPECT_EQ(1, tag);
2380
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002381 mock_service1->set_priority(1);
2382 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2383 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002384 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002385 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002386 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002387
Darin Petkova5e07ef2012-07-09 14:27:57 +02002388 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002389 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002390 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00002391 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002392 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002393 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002394 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002395
Paul Stewartce4ec192012-03-14 12:53:46 -07002396 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002397 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002398 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002399 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002400
2401 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002402 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002403}
2404
Darin Petkova5e07ef2012-07-09 14:27:57 +02002405TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2406 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2407 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2408
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002409 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002410 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002411
2412 scoped_refptr<MockService> mock_service(
2413 new NiceMock<MockService>(
2414 control_interface(), dispatcher(), metrics(), manager()));
2415 ServiceRefPtr service = mock_service;
2416 ServiceRefPtr null_service;
2417
2418 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2419 manager()->NotifyDefaultServiceChanged(null_service);
2420
2421 ServiceWatcher service_watcher1;
2422 ServiceWatcher service_watcher2;
2423 int tag1 =
2424 manager()->RegisterDefaultServiceCallback(
2425 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2426 service_watcher1.AsWeakPtr()));
2427 EXPECT_EQ(1, tag1);
2428 int tag2 =
2429 manager()->RegisterDefaultServiceCallback(
2430 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2431 service_watcher2.AsWeakPtr()));
2432 EXPECT_EQ(2, tag2);
2433
2434 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2435 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2436 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2437 manager()->NotifyDefaultServiceChanged(null_service);
2438
2439 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2440 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2441 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2442 manager()->NotifyDefaultServiceChanged(mock_service);
2443
2444 manager()->DeregisterDefaultServiceCallback(tag1);
2445 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2446 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2447 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2448 manager()->NotifyDefaultServiceChanged(mock_service);
2449 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2450
2451 manager()->DeregisterDefaultServiceCallback(tag2);
2452 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2453 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2454 manager()->NotifyDefaultServiceChanged(mock_service);
2455
2456 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2457 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2458}
2459
Gaurav Shah435de2c2011-11-17 19:01:07 -08002460TEST_F(ManagerTest, AvailableTechnologies) {
2461 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2462 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002463 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002464 manager(),
2465 "null4",
2466 "addr4",
2467 0));
2468 manager()->RegisterDevice(mock_devices_[0]);
2469 manager()->RegisterDevice(mock_devices_[1]);
2470 manager()->RegisterDevice(mock_devices_[2]);
2471 manager()->RegisterDevice(mock_devices_[3]);
2472
2473 ON_CALL(*mock_devices_[0].get(), technology())
2474 .WillByDefault(Return(Technology::kEthernet));
2475 ON_CALL(*mock_devices_[1].get(), technology())
2476 .WillByDefault(Return(Technology::kWifi));
2477 ON_CALL(*mock_devices_[2].get(), technology())
2478 .WillByDefault(Return(Technology::kCellular));
2479 ON_CALL(*mock_devices_[3].get(), technology())
2480 .WillByDefault(Return(Technology::kWifi));
2481
2482 set<string> expected_technologies;
2483 expected_technologies.insert(Technology::NameFromIdentifier(
2484 Technology::kEthernet));
2485 expected_technologies.insert(Technology::NameFromIdentifier(
2486 Technology::kWifi));
2487 expected_technologies.insert(Technology::NameFromIdentifier(
2488 Technology::kCellular));
2489 Error error;
2490 vector<string> technologies = manager()->AvailableTechnologies(&error);
2491
2492 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2493 ContainerEq(expected_technologies));
2494}
2495
2496TEST_F(ManagerTest, ConnectedTechnologies) {
2497 scoped_refptr<MockService> connected_service1(
2498 new NiceMock<MockService>(control_interface(),
2499 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002500 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002501 manager()));
2502 scoped_refptr<MockService> connected_service2(
2503 new NiceMock<MockService>(control_interface(),
2504 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002505 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002506 manager()));
2507 scoped_refptr<MockService> disconnected_service1(
2508 new NiceMock<MockService>(control_interface(),
2509 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002510 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002511 manager()));
2512 scoped_refptr<MockService> disconnected_service2(
2513 new NiceMock<MockService>(control_interface(),
2514 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002515 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002516 manager()));
2517
2518 ON_CALL(*connected_service1.get(), IsConnected())
2519 .WillByDefault(Return(true));
2520 ON_CALL(*connected_service2.get(), IsConnected())
2521 .WillByDefault(Return(true));
2522
2523 manager()->RegisterService(connected_service1);
2524 manager()->RegisterService(connected_service2);
2525 manager()->RegisterService(disconnected_service1);
2526 manager()->RegisterService(disconnected_service2);
2527
2528 manager()->RegisterDevice(mock_devices_[0]);
2529 manager()->RegisterDevice(mock_devices_[1]);
2530 manager()->RegisterDevice(mock_devices_[2]);
2531 manager()->RegisterDevice(mock_devices_[3]);
2532
2533 ON_CALL(*mock_devices_[0].get(), technology())
2534 .WillByDefault(Return(Technology::kEthernet));
2535 ON_CALL(*mock_devices_[1].get(), technology())
2536 .WillByDefault(Return(Technology::kWifi));
2537 ON_CALL(*mock_devices_[2].get(), technology())
2538 .WillByDefault(Return(Technology::kCellular));
2539 ON_CALL(*mock_devices_[3].get(), technology())
2540 .WillByDefault(Return(Technology::kWifi));
2541
2542 mock_devices_[0]->SelectService(connected_service1);
2543 mock_devices_[1]->SelectService(disconnected_service1);
2544 mock_devices_[2]->SelectService(disconnected_service2);
2545 mock_devices_[3]->SelectService(connected_service2);
2546
2547 set<string> expected_technologies;
2548 expected_technologies.insert(Technology::NameFromIdentifier(
2549 Technology::kEthernet));
2550 expected_technologies.insert(Technology::NameFromIdentifier(
2551 Technology::kWifi));
2552 Error error;
2553
2554 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2555 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2556 ContainerEq(expected_technologies));
2557}
2558
2559TEST_F(ManagerTest, DefaultTechnology) {
2560 scoped_refptr<MockService> connected_service(
2561 new NiceMock<MockService>(control_interface(),
2562 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002563 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002564 manager()));
2565 scoped_refptr<MockService> disconnected_service(
2566 new NiceMock<MockService>(control_interface(),
2567 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002568 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002569 manager()));
2570
2571 // Connected. WiFi.
2572 ON_CALL(*connected_service.get(), IsConnected())
2573 .WillByDefault(Return(true));
2574 ON_CALL(*connected_service.get(), state())
2575 .WillByDefault(Return(Service::kStateConnected));
2576 ON_CALL(*connected_service.get(), technology())
2577 .WillByDefault(Return(Technology::kWifi));
2578
2579 // Disconnected. Ethernet.
2580 ON_CALL(*disconnected_service.get(), technology())
2581 .WillByDefault(Return(Technology::kEthernet));
2582
2583 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002584 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002585 Error error;
2586 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2587
2588
2589 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002590 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002591 // Connected service should be brought to the front now.
2592 string expected_technology =
2593 Technology::NameFromIdentifier(Technology::kWifi);
2594 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2595}
2596
Paul Stewart212d60f2012-07-12 10:59:13 -07002597TEST_F(ManagerTest, Stop) {
2598 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002599 new NiceMock<MockProfile>(
2600 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002601 AdoptProfile(manager(), profile);
2602 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002603 new NiceMock<MockService>(control_interface(),
2604 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002605 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002606 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002607 manager()->RegisterService(service);
2608 manager()->RegisterDevice(mock_devices_[0]);
2609 EXPECT_CALL(*profile.get(),
2610 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2611 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002612 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002613 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2614 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002615 manager()->Stop();
2616}
2617
mukesh agrawal00917ce2011-11-22 23:56:55 +00002618TEST_F(ManagerTest, UpdateServiceConnected) {
2619 scoped_refptr<MockService> mock_service(
2620 new NiceMock<MockService>(control_interface(),
2621 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002622 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002623 manager()));
2624 manager()->RegisterService(mock_service);
2625 EXPECT_FALSE(mock_service->favorite());
2626 EXPECT_FALSE(mock_service->auto_connect());
2627
Gaurav Shah435de2c2011-11-17 19:01:07 -08002628 EXPECT_CALL(*mock_service.get(), IsConnected())
2629 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002630 manager()->UpdateService(mock_service);
2631 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2632 // to mock out MakeFavorite. And mocking that out would break the
2633 // SortServices test. (crosbug.com/23370)
2634 EXPECT_TRUE(mock_service->favorite());
2635 EXPECT_TRUE(mock_service->auto_connect());
2636}
2637
Thieu Led4e9e552012-02-16 16:26:07 -08002638TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2639 // This tests the case where the user connects to a service that is
2640 // currently associated with a profile. We want to make sure that the
2641 // favorite flag is set and that the flag is saved to the current
2642 // profile.
2643 scoped_refptr<MockService> mock_service(
2644 new NiceMock<MockService>(control_interface(),
2645 dispatcher(),
2646 metrics(),
2647 manager()));
2648 manager()->RegisterService(mock_service);
2649 EXPECT_FALSE(mock_service->favorite());
2650 EXPECT_FALSE(mock_service->auto_connect());
2651
Gary Moraind93615e2012-04-27 11:50:03 -07002652 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002653 new MockProfile(
2654 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002655
Gary Moraind93615e2012-04-27 11:50:03 -07002656 mock_service->set_profile(profile);
2657 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002658 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002659 EXPECT_CALL(*profile,
2660 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002661 manager()->UpdateService(mock_service);
2662 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2663 // to mock out MakeFavorite. And mocking that out would break the
2664 // SortServices test. (crosbug.com/23370)
2665 EXPECT_TRUE(mock_service->favorite());
2666 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002667 // This releases the ref on the mock profile.
2668 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002669}
2670
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002671TEST_F(ManagerTest, SaveSuccessfulService) {
2672 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002673 new StrictMock<MockProfile>(
2674 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002675 AdoptProfile(manager(), profile);
2676 scoped_refptr<MockService> service(
2677 new NiceMock<MockService>(control_interface(),
2678 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002679 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002680 manager()));
2681
2682 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2683 ServiceRefPtr expect_service(service.get());
2684
2685 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2686 .WillOnce(Return(false));
2687 manager()->RegisterService(service);
2688
2689 EXPECT_CALL(*service.get(), state())
2690 .WillRepeatedly(Return(Service::kStateConnected));
2691 EXPECT_CALL(*service.get(), IsConnected())
2692 .WillRepeatedly(Return(true));
2693 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2694 .WillOnce(Return(true));
2695 manager()->UpdateService(service);
2696}
2697
Darin Petkove7c6ad32012-06-29 10:22:09 +02002698TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002699 MockProfile *profile0 =
2700 new MockProfile(control_interface(), metrics(), manager(), "");
2701 MockProfile *profile1 =
2702 new MockProfile(control_interface(), metrics(), manager(), "");
2703 MockProfile *profile2 =
2704 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002705 AdoptProfile(manager(), profile0); // Passes ownership.
2706 AdoptProfile(manager(), profile1); // Passes ownership.
2707 AdoptProfile(manager(), profile2); // Passes ownership.
2708 DeviceRefPtr device_ref(mock_devices_[0].get());
2709 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2710 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2711 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2712 manager()->UpdateDevice(mock_devices_[0]);
2713}
2714
Paul Stewart1b253142012-01-26 14:05:52 -08002715TEST_F(ManagerTest, EnumerateProfiles) {
2716 vector<string> profile_paths;
2717 for (size_t i = 0; i < 10; i++) {
2718 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002719 new StrictMock<MockProfile>(
2720 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002721 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002722 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2723 .WillOnce(Return(profile_paths.back()));
2724 AdoptProfile(manager(), profile);
2725 }
2726
2727 Error error;
2728 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2729 EXPECT_TRUE(error.IsSuccess());
2730 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2731 for (size_t i = 0; i < profile_paths.size(); i++) {
2732 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2733 }
2734}
2735
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002736TEST_F(ManagerTest, AutoConnectOnRegister) {
2737 MockServiceRefPtr service = MakeAutoConnectableService();
2738 EXPECT_CALL(*service.get(), AutoConnect());
2739 manager()->RegisterService(service);
2740 dispatcher()->DispatchPendingEvents();
2741}
2742
2743TEST_F(ManagerTest, AutoConnectOnUpdate) {
2744 MockServiceRefPtr service1 = MakeAutoConnectableService();
2745 service1->set_priority(1);
2746 MockServiceRefPtr service2 = MakeAutoConnectableService();
2747 service2->set_priority(2);
2748 manager()->RegisterService(service1);
2749 manager()->RegisterService(service2);
2750 dispatcher()->DispatchPendingEvents();
2751
2752 EXPECT_CALL(*service1.get(), AutoConnect());
2753 EXPECT_CALL(*service2.get(), state())
2754 .WillRepeatedly(Return(Service::kStateFailure));
2755 EXPECT_CALL(*service2.get(), IsFailed())
2756 .WillRepeatedly(Return(true));
2757 EXPECT_CALL(*service2.get(), IsConnected())
2758 .WillRepeatedly(Return(false));
2759 manager()->UpdateService(service2);
2760 dispatcher()->DispatchPendingEvents();
2761}
2762
2763TEST_F(ManagerTest, AutoConnectOnDeregister) {
2764 MockServiceRefPtr service1 = MakeAutoConnectableService();
2765 service1->set_priority(1);
2766 MockServiceRefPtr service2 = MakeAutoConnectableService();
2767 service2->set_priority(2);
2768 manager()->RegisterService(service1);
2769 manager()->RegisterService(service2);
2770 dispatcher()->DispatchPendingEvents();
2771
2772 EXPECT_CALL(*service1.get(), AutoConnect());
2773 manager()->DeregisterService(service2);
2774 dispatcher()->DispatchPendingEvents();
2775}
2776
Darin Petkov3ec55342012-09-28 14:04:44 +02002777TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2778 MockServiceRefPtr service = MakeAutoConnectableService();
2779 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2780 SetPowerManager();
2781 EXPECT_CALL(*service, AutoConnect()).Times(0);
2782 manager()->RegisterService(service);
2783 dispatcher()->DispatchPendingEvents();
2784}
2785
Darin Petkovca621542012-07-25 14:25:56 +02002786TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2787 MockServiceRefPtr service = MakeAutoConnectableService();
2788 SetPowerState(PowerManagerProxyDelegate::kMem);
2789 SetPowerManager();
2790 EXPECT_CALL(*service, AutoConnect()).Times(0);
2791 manager()->RegisterService(service);
2792 dispatcher()->DispatchPendingEvents();
2793}
2794
2795TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2796 MockServiceRefPtr service = MakeAutoConnectableService();
2797 SetPowerState(PowerManagerProxyDelegate::kOn);
2798 SetPowerManager();
2799 EXPECT_CALL(*service, AutoConnect());
2800 manager()->RegisterService(service);
2801 dispatcher()->DispatchPendingEvents();
2802}
2803
2804TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2805 MockServiceRefPtr service = MakeAutoConnectableService();
2806 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2807 SetPowerManager();
2808 EXPECT_CALL(*service, AutoConnect());
2809 manager()->RegisterService(service);
2810 dispatcher()->DispatchPendingEvents();
2811}
2812
Paul Stewart63864b62012-11-07 15:10:55 -08002813TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2814 SetRunning(false);
2815 MockServiceRefPtr service = MakeAutoConnectableService();
2816 EXPECT_CALL(*service, AutoConnect()).Times(0);
2817 manager()->RegisterService(service);
2818 dispatcher()->DispatchPendingEvents();
2819}
2820
Darin Petkovca621542012-07-25 14:25:56 +02002821TEST_F(ManagerTest, OnPowerStateChanged) {
2822 MockServiceRefPtr service = MakeAutoConnectableService();
2823 SetPowerState(PowerManagerProxyDelegate::kOn);
2824 SetPowerManager();
2825 EXPECT_CALL(*service, AutoConnect());
2826 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002827 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002828 dispatcher()->DispatchPendingEvents();
2829
mukesh agrawal784566d2012-08-08 18:32:58 -07002830 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002831 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2832 EXPECT_CALL(*service, AutoConnect());
2833 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002834 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002835
mukesh agrawal784566d2012-08-08 18:32:58 -07002836 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002837 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2838 EXPECT_CALL(*service, AutoConnect()).Times(0);
2839 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002840 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002841}
2842
Darin Petkov3ec55342012-09-28 14:04:44 +02002843TEST_F(ManagerTest, AddTerminationAction) {
2844 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
Daniel Eratf9753672013-01-24 10:17:02 -08002845 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02002846 SetPowerManager();
2847 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2848 manager()->AddTerminationAction("action1", base::Closure());
2849 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2850 manager()->AddTerminationAction("action2", base::Closure());
2851}
2852
2853TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002854 const char kKey1[] = "action1";
2855 const char kKey2[] = "action2";
2856 const int kSuspendDelayId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002857
2858 MockPowerManager &power_manager = *power_manager_;
2859 SetPowerManager();
2860
2861 // Removing an action when the hook table is empty should not result in any
2862 // calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002863 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002864 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2865 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2866 manager()->RemoveTerminationAction("unknown");
2867 Mock::VerifyAndClearExpectations(&power_manager);
2868
Daniel Eratf9753672013-01-24 10:17:02 -08002869 EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _))
2870 .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true)));
Daniel Erat0818cca2012-12-14 10:16:21 -08002871 EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1);
Darin Petkov3ec55342012-09-28 14:04:44 +02002872 manager()->AddTerminationAction(kKey1, base::Closure());
2873 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2874 manager()->AddTerminationAction(kKey2, base::Closure());
2875
2876 // Removing an action that ends up with a non-empty hook table should not
2877 // result in any calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002878 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002879 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2880 manager()->RemoveTerminationAction(kKey1);
2881 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2882 Mock::VerifyAndClearExpectations(&power_manager);
2883
2884 // Removing the last action should trigger unregistering from the power
2885 // manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002886 EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId))
2887 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02002888 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2889 manager()->RemoveTerminationAction(kKey2);
2890 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2891}
2892
2893TEST_F(ManagerTest, RunTerminationActions) {
2894 TerminationActionTest test_action;
2895 const string kActionName = "action";
2896
2897 EXPECT_CALL(test_action, Done(_));
2898 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2899 test_action.AsWeakPtr()));
2900
2901 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2902 Bind(&TerminationActionTest::Action,
2903 test_action.AsWeakPtr()));
2904 test_action.set_manager(manager());
2905 EXPECT_CALL(test_action, Done(_));
2906 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2907 test_action.AsWeakPtr()));
2908}
2909
Daniel Erat0818cca2012-12-14 10:16:21 -08002910TEST_F(ManagerTest, OnSuspendImminent) {
2911 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002912 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Erat0818cca2012-12-14 10:16:21 -08002913 EXPECT_CALL(*power_manager_,
2914 ReportSuspendReadiness(
2915 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002916 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002917 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02002918}
2919
2920TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002921 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02002922 Error error;
Daniel Erat0818cca2012-12-14 10:16:21 -08002923 EXPECT_CALL(*power_manager_,
2924 ReportSuspendReadiness(
2925 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002926 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002927 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02002928}
2929
Paul Stewartc681fa02012-03-02 19:40:04 -08002930TEST_F(ManagerTest, RecheckPortal) {
2931 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2932 .WillOnce(Return(false));
2933 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2934 .WillOnce(Return(true));
2935 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2936 .Times(0);
2937
2938 manager()->RegisterDevice(mock_devices_[0]);
2939 manager()->RegisterDevice(mock_devices_[1]);
2940 manager()->RegisterDevice(mock_devices_[2]);
2941
2942 manager()->RecheckPortal(NULL);
2943}
2944
Paul Stewartd215af62012-04-24 23:25:50 -07002945TEST_F(ManagerTest, RecheckPortalOnService) {
2946 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2947 dispatcher(),
2948 metrics(),
2949 manager());
2950 EXPECT_CALL(*mock_devices_[0].get(),
2951 IsConnectedToService(IsRefPtrTo(service)))
2952 .WillOnce(Return(false));
2953 EXPECT_CALL(*mock_devices_[1].get(),
2954 IsConnectedToService(IsRefPtrTo(service)))
2955 .WillOnce(Return(true));
2956 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2957 .WillOnce(Return(true));
2958 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2959 .Times(0);
2960
2961 manager()->RegisterDevice(mock_devices_[0]);
2962 manager()->RegisterDevice(mock_devices_[1]);
2963 manager()->RegisterDevice(mock_devices_[2]);
2964
2965 manager()->RecheckPortalOnService(service);
2966}
2967
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002968TEST_F(ManagerTest, GetDefaultService) {
2969 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002970 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002971
2972 scoped_refptr<MockService> mock_service(
2973 new NiceMock<MockService>(control_interface(),
2974 dispatcher(),
2975 metrics(),
2976 manager()));
2977
2978 manager()->RegisterService(mock_service);
2979 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002980 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002981
2982 scoped_refptr<MockConnection> mock_connection(
2983 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002984 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002985 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002986 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002987
Paul Stewartce4ec192012-03-14 12:53:46 -07002988 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002989 manager()->DeregisterService(mock_service);
2990}
2991
Paul Stewart13ed2252012-03-21 12:52:46 -07002992TEST_F(ManagerTest, GetServiceWithGUID) {
2993 scoped_refptr<MockService> mock_service0(
2994 new NiceMock<MockService>(control_interface(),
2995 dispatcher(),
2996 metrics(),
2997 manager()));
2998
2999 scoped_refptr<MockService> mock_service1(
3000 new NiceMock<MockService>(control_interface(),
3001 dispatcher(),
3002 metrics(),
3003 manager()));
3004
Paul Stewartcb59fed2012-03-21 21:14:46 -07003005 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3006 .Times(0);
3007 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3008 .Times(0);
3009
Paul Stewart13ed2252012-03-21 12:52:46 -07003010 manager()->RegisterService(mock_service0);
3011 manager()->RegisterService(mock_service1);
3012
3013 const string kGUID0 = "GUID0";
3014 const string kGUID1 = "GUID1";
3015
3016 {
3017 Error error;
3018 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3019 EXPECT_FALSE(error.IsSuccess());
3020 EXPECT_FALSE(service);
3021 }
3022
3023 KeyValueStore args;
3024 args.SetString(flimflam::kGuidProperty, kGUID1);
3025
3026 {
3027 Error error;
3028 ServiceRefPtr service = manager()->GetService(args, &error);
3029 EXPECT_EQ(Error::kInvalidArguments, error.type());
3030 EXPECT_FALSE(service);
3031 }
3032
3033 mock_service0->set_guid(kGUID0);
3034 mock_service1->set_guid(kGUID1);
3035
3036 {
3037 Error error;
3038 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3039 EXPECT_TRUE(error.IsSuccess());
3040 EXPECT_EQ(mock_service0.get(), service.get());
3041 }
3042
3043 {
3044 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003045 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3046 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003047 ServiceRefPtr service = manager()->GetService(args, &error);
3048 EXPECT_TRUE(error.IsSuccess());
3049 EXPECT_EQ(mock_service1.get(), service.get());
3050 }
3051
3052 manager()->DeregisterService(mock_service0);
3053 manager()->DeregisterService(mock_service1);
3054}
3055
Gary Morain028545d2012-04-07 14:55:52 -07003056
3057TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003058 EXPECT_FALSE(manager()->IsOnline());
3059 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3060
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003061 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003062 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003063 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3064 .Times(AnyNumber());
3065 scoped_refptr<MockService> mock_service0(
3066 new NiceMock<MockService>(control_interface(),
3067 dispatcher(),
3068 metrics(),
3069 manager()));
3070
3071 scoped_refptr<MockService> mock_service1(
3072 new NiceMock<MockService>(control_interface(),
3073 dispatcher(),
3074 metrics(),
3075 manager()));
3076
3077 EXPECT_CALL(*mock_service0.get(), IsConnected())
3078 .WillRepeatedly(Return(false));
3079 EXPECT_CALL(*mock_service1.get(), IsConnected())
3080 .WillRepeatedly(Return(false));
3081
3082 manager()->RegisterService(mock_service0);
3083 manager()->RegisterService(mock_service1);
3084
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003085 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003086 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3087
3088 manager()->DeregisterService(mock_service0);
3089 manager()->DeregisterService(mock_service1);
3090}
3091
3092TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003093 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003094 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003095 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3096 .Times(AnyNumber());
3097 scoped_refptr<MockService> mock_service0(
3098 new NiceMock<MockService>(control_interface(),
3099 dispatcher(),
3100 metrics(),
3101 manager()));
3102
3103 scoped_refptr<MockService> mock_service1(
3104 new NiceMock<MockService>(control_interface(),
3105 dispatcher(),
3106 metrics(),
3107 manager()));
3108
3109 EXPECT_CALL(*mock_service0.get(), IsConnected())
3110 .WillRepeatedly(Return(false));
3111 EXPECT_CALL(*mock_service1.get(), IsConnected())
3112 .WillRepeatedly(Return(true));
3113 EXPECT_CALL(*mock_service0.get(), state())
3114 .WillRepeatedly(Return(Service::kStateIdle));
3115 EXPECT_CALL(*mock_service1.get(), state())
3116 .WillRepeatedly(Return(Service::kStateConnected));
3117
3118 manager()->RegisterService(mock_service0);
3119 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003120 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003121
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003122 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003123 EXPECT_EQ("online", manager()->CalculateState(NULL));
3124
3125 manager()->DeregisterService(mock_service0);
3126 manager()->DeregisterService(mock_service1);
3127}
3128
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003129TEST_F(ManagerTest, StartupPortalList) {
3130 // Simulate loading value from the default profile.
3131 const string kProfileValue("wifi,vpn");
3132 manager()->props_.check_portal_list = kProfileValue;
3133
3134 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3135 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3136 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3137
3138 const string kStartupValue("cellular,ethernet");
3139 manager()->SetStartupPortalList(kStartupValue);
3140 // Ensure profile value is not overwritten, so when we save the default
3141 // profile, the correct value will still be written.
3142 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3143
3144 // However we should read back a different list.
3145 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3146 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3147 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3148
3149 const string kRuntimeValue("ppp");
3150 // Setting a runtime value over the control API should overwrite both
3151 // the profile value and what we read back.
3152 Error error;
3153 manager()->mutable_store()->SetStringProperty(
3154 flimflam::kCheckPortalListProperty,
3155 kRuntimeValue,
3156 &error);
3157 ASSERT_TRUE(error.IsSuccess());
3158 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3159 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3160 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3161 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3162}
3163
Paul Stewart036dba02012-08-07 12:34:41 -07003164TEST_F(ManagerTest, LinkMonitorEnabled) {
3165 const string kEnabledTechnologies("wifi,vpn");
3166 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3167 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3168 EXPECT_FALSE(
3169 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3170}
3171
Paul Stewart85aea152013-01-22 09:31:56 -08003172TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003173 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003174 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003175 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003176 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003177 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003178 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3179 AdoptProfile(manager(), profile);
3180 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3181 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3182 scoped_ptr<MockStore> store1(new MockStore);
3183 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3184}
3185
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003186TEST_F(ManagerTest, EnableTechnology) {
3187 Error error(Error::kOperationInitiated);
3188 ResultCallback callback;
3189 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3190 EXPECT_TRUE(error.IsSuccess());
3191
Joshua Krollda798622012-06-05 12:30:48 -07003192 ON_CALL(*mock_devices_[0], technology())
3193 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003194
3195 manager()->RegisterDevice(mock_devices_[0]);
3196
3197 // Device is enabled, so expect operation is successful.
3198 mock_devices_[0]->enabled_ = true;
3199 error.Populate(Error::kOperationInitiated);
3200 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3201 EXPECT_TRUE(error.IsSuccess());
3202
3203 // Device is disabled, so expect operation in progress.
3204 mock_devices_[0]->enabled_ = false;
3205 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3206 error.Populate(Error::kOperationInitiated);
3207 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3208 EXPECT_TRUE(error.IsOngoing());
3209}
3210
3211TEST_F(ManagerTest, DisableTechnology) {
3212 Error error(Error::kOperationInitiated);
3213 ResultCallback callback;
3214 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3215 EXPECT_TRUE(error.IsSuccess());
3216
Joshua Krollda798622012-06-05 12:30:48 -07003217 ON_CALL(*mock_devices_[0], technology())
3218 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003219
3220 manager()->RegisterDevice(mock_devices_[0]);
3221
3222 // Device is disabled, so expect operation is successful.
3223 error.Populate(Error::kOperationInitiated);
3224 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3225 EXPECT_TRUE(error.IsSuccess());
3226
3227 // Device is enabled, so expect operation in progress.
3228 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3229 mock_devices_[0]->enabled_ = true;
3230 error.Populate(Error::kOperationInitiated);
3231 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3232 EXPECT_TRUE(error.IsOngoing());
3233}
3234
Paul Stewart4d5efb72012-09-17 12:24:34 -07003235TEST_F(ManagerTest, IgnoredSearchList) {
3236 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
3237 SetResolver(resolver.get());
3238 vector<string> ignored_paths;
3239 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3240 SetIgnoredDNSSearchPaths("");
3241 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3242
3243 const string kIgnored0 = "chromium.org";
3244 ignored_paths.push_back(kIgnored0);
3245 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3246 SetIgnoredDNSSearchPaths(kIgnored0);
3247 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3248
3249 const string kIgnored1 = "google.com";
3250 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3251 ignored_paths.push_back(kIgnored1);
3252 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3253 SetIgnoredDNSSearchPaths(kIgnoredSum);
3254 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3255
3256 SetResolver(Resolver::GetInstance());
3257}
3258
Paul Stewartbfb82552012-10-24 16:48:48 -07003259TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3260 // Test to make sure that every service state-change causes the
3261 // Manager to emit a new service list.
3262 scoped_refptr<MockService> mock_service(
3263 new NiceMock<MockService>(control_interface(),
3264 dispatcher(),
3265 metrics(),
3266 manager()));
3267 EXPECT_CALL(*mock_service, state())
3268 .WillRepeatedly(Return(Service::kStateIdle));
3269
3270 manager()->RegisterService(mock_service);
3271 EXPECT_CALL(
3272 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3273 flimflam::kServicesProperty, _)).Times(1);
3274 EXPECT_CALL(
3275 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3276 flimflam::kServiceWatchListProperty, _)).Times(1);
3277 CompleteServiceSort();
3278
3279 Mock::VerifyAndClearExpectations(manager_adaptor_);
3280 EXPECT_CALL(
3281 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3282 flimflam::kServicesProperty, _)).Times(1);
3283 EXPECT_CALL(
3284 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3285 flimflam::kServiceWatchListProperty, _)).Times(1);
3286 manager()->UpdateService(mock_service.get());
3287 CompleteServiceSort();
3288
3289 manager()->DeregisterService(mock_service);
3290}
3291
3292TEST_F(ManagerTest, EnumerateServices) {
3293 scoped_refptr<MockService> mock_service(
3294 new NiceMock<MockService>(control_interface(),
3295 dispatcher(),
3296 metrics(),
3297 manager()));
3298 manager()->RegisterService(mock_service);
3299
3300 EXPECT_CALL(*mock_service, state())
3301 .WillRepeatedly(Return(Service::kStateConnected));
3302 EXPECT_CALL(*mock_service, IsVisible())
3303 .WillRepeatedly(Return(false));
3304 EXPECT_TRUE(EnumerateAvailableServices().empty());
3305 EXPECT_TRUE(EnumerateWatchedServices().empty());
3306
3307 EXPECT_CALL(*mock_service, state())
3308 .WillRepeatedly(Return(Service::kStateIdle));
3309 EXPECT_TRUE(EnumerateAvailableServices().empty());
3310 EXPECT_TRUE(EnumerateWatchedServices().empty());
3311
3312 EXPECT_CALL(*mock_service, IsVisible())
3313 .WillRepeatedly(Return(true));
3314 Service::ConnectState unwatched_states[] = {
3315 Service::kStateUnknown,
3316 Service::kStateIdle,
3317 Service::kStateFailure
3318 };
3319 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3320 EXPECT_CALL(*mock_service, state())
3321 .WillRepeatedly(Return(unwatched_states[i]));
3322 EXPECT_FALSE(EnumerateAvailableServices().empty());
3323 EXPECT_TRUE(EnumerateWatchedServices().empty());
3324 }
3325
3326 Service::ConnectState watched_states[] = {
3327 Service::kStateAssociating,
3328 Service::kStateConfiguring,
3329 Service::kStateConnected,
3330 Service::kStateDisconnected,
3331 Service::kStatePortal,
3332 Service::kStateOnline
3333 };
3334 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3335 EXPECT_CALL(*mock_service, state())
3336 .WillRepeatedly(Return(watched_states[i]));
3337 EXPECT_FALSE(EnumerateAvailableServices().empty());
3338 EXPECT_FALSE(EnumerateWatchedServices().empty());
3339 }
3340
3341 manager()->DeregisterService(mock_service);
3342}
3343
Paul Stewart39db5ca2013-03-18 14:15:17 -07003344TEST_F(ManagerTest, ConnectToBestServices) {
3345 scoped_refptr<MockService> wifi_service0(
3346 new NiceMock<MockService>(control_interface(),
3347 dispatcher(),
3348 metrics(),
3349 manager()));
3350 EXPECT_CALL(*wifi_service0.get(), state())
3351 .WillRepeatedly(Return(Service::kStateIdle));
3352 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3353 .WillRepeatedly(Return(false));
3354 wifi_service0->set_connectable(true);
3355 wifi_service0->set_auto_connect(true);
3356 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3357 EXPECT_CALL(*wifi_service0.get(), technology())
3358 .WillRepeatedly(Return(Technology::kWifi));
3359
3360 scoped_refptr<MockService> wifi_service1(
3361 new NiceMock<MockService>(control_interface(),
3362 dispatcher(),
3363 metrics(),
3364 manager()));
3365 EXPECT_CALL(*wifi_service1.get(), state())
3366 .WillRepeatedly(Return(Service::kStateIdle));
3367 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3368 .WillRepeatedly(Return(false));
3369 wifi_service1->set_auto_connect(true);
3370 wifi_service1->set_connectable(true);
3371 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3372 EXPECT_CALL(*wifi_service1.get(), technology())
3373 .WillRepeatedly(Return(Technology::kWifi));
3374
3375 scoped_refptr<MockService> wifi_service2(
3376 new NiceMock<MockService>(control_interface(),
3377 dispatcher(),
3378 metrics(),
3379 manager()));
3380 EXPECT_CALL(*wifi_service2.get(), state())
3381 .WillRepeatedly(Return(Service::kStateConnected));
3382 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3383 .WillRepeatedly(Return(true));
3384 wifi_service2->set_auto_connect(true);
3385 wifi_service2->set_connectable(true);
3386 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3387 EXPECT_CALL(*wifi_service2.get(), technology())
3388 .WillRepeatedly(Return(Technology::kWifi));
3389
3390 manager()->RegisterService(wifi_service0);
3391 manager()->RegisterService(wifi_service1);
3392 manager()->RegisterService(wifi_service2);
3393
3394 CompleteServiceSort();
3395 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3396
3397 scoped_refptr<MockService> cell_service(
3398 new NiceMock<MockService>(control_interface(),
3399 dispatcher(),
3400 metrics(),
3401 manager()));
3402
3403 EXPECT_CALL(*cell_service.get(), state())
3404 .WillRepeatedly(Return(Service::kStateConnected));
3405 EXPECT_CALL(*cell_service.get(), IsConnected())
3406 .WillRepeatedly(Return(true));
3407 wifi_service2->set_auto_connect(true);
3408 cell_service->set_connectable(true);
3409 EXPECT_CALL(*cell_service.get(), technology())
3410 .WillRepeatedly(Return(Technology::kCellular));
3411 manager()->RegisterService(cell_service);
3412
3413 scoped_refptr<MockService> vpn_service(
3414 new NiceMock<MockService>(control_interface(),
3415 dispatcher(),
3416 metrics(),
3417 manager()));
3418
3419 EXPECT_CALL(*vpn_service.get(), state())
3420 .WillRepeatedly(Return(Service::kStateIdle));
3421 EXPECT_CALL(*vpn_service.get(), IsConnected())
3422 .WillRepeatedly(Return(false));
3423 wifi_service2->set_auto_connect(false);
3424 vpn_service->set_connectable(true);
3425 EXPECT_CALL(*vpn_service.get(), technology())
3426 .WillRepeatedly(Return(Technology::kVPN));
3427 manager()->RegisterService(vpn_service);
3428
3429 // The connected services should be at the top.
3430 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3431
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003432 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(1);
3433 EXPECT_CALL(*wifi_service1.get(), Connect(_, _)).Times(0); // Lower prio.
3434 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
3435 EXPECT_CALL(*cell_service.get(), Connect(_, _)).Times(0); // Is connected.
3436 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003437
3438 manager()->ConnectToBestServices(NULL);
3439 dispatcher()->DispatchPendingEvents();
3440
3441 // After this operation, since the Connect calls above are mocked and
3442 // no actual state changes have occurred, we should expect that the
3443 // service sorting order will not have changed.
3444 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3445}
3446
Christopher Wiley1057cd72013-02-28 15:21:29 -08003447TEST_F(ManagerTest, VerifyDestination) {
3448 const string kFakeCertificate("fake cert");
3449 const string kFakePublicKey("fake public key");
3450 const string kFakeNonce("fake public key");
3451 const string kFakeSignedData("fake signed data");
3452 const string kFakeUdn("fake udn");
3453 const char kSSIDStr[] = "fake ssid";
3454 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
3455 const string kFakeData("muffin man");
3456 scoped_refptr<MockWiFiService> mock_destination(
3457 new NiceMock<MockWiFiService>(control_interface(),
3458 dispatcher(),
3459 metrics(),
3460 manager(),
3461 wifi_provider_,
3462 kSSID,
3463 "",
3464 "none",
3465 false));
3466 manager()->RegisterService(mock_destination);
3467 StrictMock<DestinationVerificationTest> dv_test;
3468
3469 // Verify that if we're not connected to anything, verification fails.
3470 {
3471 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3472 kFakePublicKey,
3473 kFakeNonce,
3474 kFakeSignedData,
3475 kFakeUdn,
3476 kSSID,
3477 _,
3478 _,
3479 _))
3480 .Times(0);
3481 Error error(Error::kOperationInitiated);
3482 ResultBoolCallback cb = Bind(
3483 &DestinationVerificationTest::ResultBoolCallbackStub,
3484 dv_test.AsWeakPtr());
3485 manager()->VerifyDestination(kFakeCertificate,
3486 kFakePublicKey,
3487 kFakeNonce,
3488 kFakeSignedData,
3489 kFakeUdn,
3490 cb,
3491 &error);
3492 EXPECT_TRUE(error.IsFailure());
3493 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3494 }
3495
3496 // Making the service look online will let service lookup in
3497 // VerifyDestinatoin succeed.
3498 EXPECT_CALL(*mock_destination.get(), IsConnected())
3499 .WillRepeatedly(Return(true));
3500
3501 // Lead off by verifying that the basic VerifyDestination flow works.
3502 {
3503 ResultBoolCallback passed_down_callback;
3504 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3505 kFakePublicKey,
3506 kFakeNonce,
3507 kFakeSignedData,
3508 kFakeUdn,
3509 kSSID,
3510 _,
3511 _,
3512 _))
3513 .Times(1)
3514 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3515 // Ask the manager to verify the current destination. This should look
3516 // up our previously registered service, and pass some metadata about
3517 // that service down to the CryptoUtilProxy to verify.
3518 Error error(Error::kOperationInitiated);
3519 ResultBoolCallback cb = Bind(
3520 &DestinationVerificationTest::ResultBoolCallbackStub,
3521 dv_test.AsWeakPtr());
3522 manager()->VerifyDestination(kFakeCertificate,
3523 kFakePublicKey,
3524 kFakeNonce,
3525 kFakeSignedData,
3526 kFakeUdn,
3527 cb,
3528 &error);
3529 // We assert here, because if the operation is not ongoing, it is
3530 // inconsistent with shim behavior to call the callback anyway.
3531 ASSERT_TRUE(error.IsOngoing());
3532 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3533 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3534 // Call the callback passed into the CryptoUtilProxy, which
3535 // should find its way into the callback passed into the manager.
3536 // In real code, that callback passed into the manager is from the
3537 // DBus adaptor.
3538 Error e;
3539 passed_down_callback.Run(e, true);
3540 Mock::VerifyAndClearExpectations(&dv_test);
3541 }
3542
3543 // Now for a slightly more complex variant. When we encrypt data,
3544 // we do the same verification step but monkey with the callback to
3545 // link ourselves to an encrypt step afterward.
3546 {
3547 ResultBoolCallback passed_down_callback;
3548 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3549 kFakePublicKey,
3550 kFakeNonce,
3551 kFakeSignedData,
3552 kFakeUdn,
3553 kSSID,
3554 _,
3555 _,
3556 _))
3557 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3558
3559 Error error(Error::kOperationInitiated);
3560 ResultStringCallback cb = Bind(
3561 &DestinationVerificationTest::ResultStringCallbackStub,
3562 dv_test.AsWeakPtr());
3563 manager()->VerifyAndEncryptData(kFakeCertificate,
3564 kFakePublicKey,
3565 kFakeNonce,
3566 kFakeSignedData,
3567 kFakeUdn,
3568 kFakeData,
3569 cb,
3570 &error);
3571 ASSERT_TRUE(error.IsOngoing());
3572 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3573 // Now, if we call that passed down callback, we should see encrypt being
3574 // called.
3575 ResultStringCallback second_passed_down_callback;
3576 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3577 kFakeData,
3578 _,
3579 _))
3580 .Times(1)
3581 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3582 Return(true)));
3583 Error e;
3584 passed_down_callback.Run(e, true);
3585 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3586 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3587 // And if we call the second passed down callback, we should see the
3588 // original function we passed down to VerifyDestination getting called.
3589 e.Reset();
3590 second_passed_down_callback.Run(e, "");
3591 Mock::VerifyAndClearExpectations(&dv_test);
3592 }
3593
3594 // If verification fails on the way to trying to encrypt, we should ditch
3595 // without calling encrypt at all.
3596 {
3597 ResultBoolCallback passed_down_callback;
3598 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3599 kFakePublicKey,
3600 kFakeNonce,
3601 kFakeSignedData,
3602 kFakeUdn,
3603 kSSID,
3604 _,
3605 _,
3606 _))
3607 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3608
3609 Error error(Error::kOperationInitiated);
3610 ResultStringCallback cb = Bind(
3611 &DestinationVerificationTest::ResultStringCallbackStub,
3612 dv_test.AsWeakPtr());
3613 manager()->VerifyAndEncryptData(kFakeCertificate,
3614 kFakePublicKey,
3615 kFakeNonce,
3616 kFakeSignedData,
3617 kFakeUdn,
3618 kFakeData,
3619 cb,
3620 &error);
3621 ASSERT_TRUE(error.IsOngoing());
3622 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3623 Error e(Error::kOperationFailed);
3624 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3625 // Although we're ditching, this callback is what cleans up the pending
3626 // DBus call.
3627 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3628 passed_down_callback.Run(e, false);
3629 Mock::VerifyAndClearExpectations(&dv_test);
3630 }
3631}
3632
Paul Stewartd2e1c362013-03-03 19:06:07 -08003633TEST_F(ManagerTest, IsProfileBefore) {
3634 scoped_refptr<MockProfile> profile0(
3635 new NiceMock<MockProfile>(
3636 control_interface(), metrics(), manager(), ""));
3637 scoped_refptr<MockProfile> profile1(
3638 new NiceMock<MockProfile>(
3639 control_interface(), metrics(), manager(), ""));
3640
3641 AdoptProfile(manager(), profile0);
3642 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3643 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3644 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3645
3646 // A few abnormal cases, but it's good to track their behavior.
3647 scoped_refptr<MockProfile> profile2(
3648 new NiceMock<MockProfile>(
3649 control_interface(), metrics(), manager(), ""));
3650 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3651 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3652 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3653 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3654}
3655
Chris Masone9be4a9d2011-05-16 15:44:09 -07003656} // namespace shill