blob: b6d1164641cebd0aad0eae264060f67de5a048c4 [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_);
Paul Stewart9dd253e2013-04-22 08:32:59 -0700134
135 // Reset service serial number so service sorting by unique_name()
136 // (and by extension, sorting by order of creation) is predictable.
137 Service::serial_number_ = 10000;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700138 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700139 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700140
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100141 void SetMetrics(Metrics *metrics) {
142 manager()->set_metrics(metrics);
143 }
144
Paul Stewartfdd16072011-09-16 12:41:35 -0700145 bool IsDeviceRegistered(const DeviceRefPtr &device,
146 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700147 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700148 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700149 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700150 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700151 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700152
Paul Stewarta849a3d2011-11-03 05:54:09 -0700153 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
154 manager->profiles_.push_back(profile);
155 }
156
Paul Stewart63864b62012-11-07 15:10:55 -0800157 void SetRunning(bool running) {
158 manager()->running_ = running;
159 }
160
Paul Stewart75225512012-01-26 22:51:33 -0800161 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
162 return manager->ephemeral_profile_;
163 }
164
Paul Stewart307c2502013-03-23 12:32:10 -0700165 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
166 return manager->profiles_;
167 }
168
Chris Masone6515aab2011-10-12 16:19:09 -0700169 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
170 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700171 FilePath final_path(storage_path());
172 final_path = final_path.Append("test.profile");
173 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
174 storage->set_path(final_path);
175 if (!storage->Open())
176 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800177 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800178 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800179 manager,
180 id,
181 "",
182 false));
183 profile->set_storage(storage.release()); // Passes ownership of "storage".
184 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700185 }
186
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700187 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
188 const string &profile_identifier,
189 const string &service_name) {
190 GLib glib;
191 KeyFileStore store(&glib);
192 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
193 return store.Open() &&
194 store.SetString(service_name, "rather", "irrelevant") &&
195 store.Close();
196 }
197
198 Error::Type TestCreateProfile(Manager *manager, const string &name) {
199 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800200 string path;
201 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700202 return error.type();
203 }
204
205 Error::Type TestPopAnyProfile(Manager *manager) {
206 Error error;
207 manager->PopAnyProfile(&error);
208 return error.type();
209 }
210
Paul Stewart307c2502013-03-23 12:32:10 -0700211 Error::Type TestPopAllUserProfiles(Manager *manager) {
212 Error error;
213 manager->PopAllUserProfiles(&error);
214 return error.type();
215 }
216
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700217 Error::Type TestPopProfile(Manager *manager, const string &name) {
218 Error error;
219 manager->PopProfile(name, &error);
220 return error.type();
221 }
222
223 Error::Type TestPushProfile(Manager *manager, const string &name) {
224 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800225 string path;
226 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700227 return error.type();
228 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000229
Paul Stewartf3eced92013-04-17 12:18:22 -0700230 Error::Type TestInsertUserProfile(Manager *manager,
231 const string &name,
232 const string &user_hash) {
233 Error error;
234 string path;
235 manager->InsertUserProfile(name, user_hash, &path, &error);
236 return error.type();
237 }
238
Paul Stewartd2e1c362013-03-03 19:06:07 -0800239 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
240 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700241 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800242 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800243 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200244 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700245 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800246 return profile;
247 }
248
249 void AddMockProfileToManager(Manager *manager) {
250 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700251 }
252
Paul Stewartdfa46052012-06-26 09:44:14 -0700253 void CompleteServiceSort() {
254 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
255 dispatcher()->DispatchPendingEvents();
256 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
257 }
258
Paul Stewart49739c02012-08-08 17:24:03 -0700259 RpcIdentifier GetDefaultServiceRpcIdentifier() {
260 return manager()->GetDefaultServiceRpcIdentifier(NULL);
261 }
262
Paul Stewart4d5efb72012-09-17 12:24:34 -0700263 void SetResolver(Resolver *resolver) {
264 manager()->resolver_ = resolver;
265 }
266
267 void SetIgnoredDNSSearchPaths(const string &search_paths) {
268 manager()->SetIgnoredDNSSearchPaths(search_paths, NULL);
269 }
270
271 const string &GetIgnoredDNSSearchPaths() {
272 return manager()->props_.ignored_dns_search_paths;
273 }
274
Paul Stewartd2e1c362013-03-03 19:06:07 -0800275 WiFiServiceRefPtr ReleaseTempMockService() {
276 // Take a reference to hold during this function.
277 WiFiServiceRefPtr temp_service = temp_mock_service_;
278 temp_mock_service_ = NULL;
279 return temp_service;
280 }
281
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700282 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000283 typedef scoped_refptr<MockService> MockServiceRefPtr;
284
Darin Petkova5e07ef2012-07-09 14:27:57 +0200285 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
286 public:
287 ServiceWatcher() {}
288 virtual ~ServiceWatcher() {}
289
290 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
291
292 private:
293 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
294 };
295
Darin Petkovca621542012-07-25 14:25:56 +0200296 class TestProxyFactory : public ProxyFactory {
297 public:
298 TestProxyFactory() {}
299
300 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
301 PowerManagerProxyDelegate */*delegate*/) {
302 return NULL;
303 }
304
305 private:
306 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
307 };
308
Darin Petkov3ec55342012-09-28 14:04:44 +0200309 class TerminationActionTest :
310 public base::SupportsWeakPtr<TerminationActionTest> {
311 public:
312 static const char kActionName[];
313
314 TerminationActionTest() : manager_(NULL) {}
315 virtual ~TerminationActionTest() {}
316
317 MOCK_METHOD1(Done, void(const Error &error));
318
319 void Action() {
320 manager_->TerminationActionComplete("action");
321 }
322
323 void set_manager(Manager *manager) { manager_ = manager; }
324
325 private:
326 Manager *manager_;
327 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
328 };
329
Christopher Wiley1057cd72013-02-28 15:21:29 -0800330 class DestinationVerificationTest :
331 public base::SupportsWeakPtr<DestinationVerificationTest> {
332 public:
333 DestinationVerificationTest() {}
334 virtual ~DestinationVerificationTest() {}
335
336 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
337 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
338 const string &value));
339 private:
340 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
341 };
342
Darin Petkovca621542012-07-25 14:25:56 +0200343 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
344 power_manager_->power_state_ = state;
345 }
346
347 void SetPowerManager() {
348 manager()->set_power_manager(power_manager_.release());
349 }
350
Darin Petkov3ec55342012-09-28 14:04:44 +0200351 HookTable *GetTerminationActions() {
352 return &manager()->termination_actions_;
353 }
354
Darin Petkovca621542012-07-25 14:25:56 +0200355 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
356 manager()->OnPowerStateChanged(state);
357 }
358
Daniel Erat0818cca2012-12-14 10:16:21 -0800359 void OnSuspendImminent(int suspend_id) {
360 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200361 }
362
Daniel Erat0818cca2012-12-14 10:16:21 -0800363 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
364 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200365 }
366
Paul Stewartbfb82552012-10-24 16:48:48 -0700367 vector<string> EnumerateAvailableServices() {
368 return manager()->EnumerateAvailableServices(NULL);
369 }
370
371 vector<string> EnumerateWatchedServices() {
372 return manager()->EnumerateWatchedServices(NULL);
373 }
374
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000375 MockServiceRefPtr MakeAutoConnectableService() {
376 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
377 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800378 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000379 manager());
380 service->MakeFavorite();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700381 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000382 return service;
383 }
384
Paul Stewart35eff132013-04-12 12:08:40 -0700385 void SetEapProviderService(const ServiceRefPtr &service) {
386 ethernet_eap_provider_->set_service(service);
387 }
388
Darin Petkovca621542012-07-25 14:25:56 +0200389 TestProxyFactory proxy_factory_;
390 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700391 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800392 scoped_ptr<MockDeviceInfo> device_info_;
393
Paul Stewartd2e1c362013-03-03 19:06:07 -0800394 // This service is held for the manager, and given ownership in a mock
395 // function. This ensures that when the Manager takes ownership, there
396 // is only one reference left.
397 scoped_refptr<MockWiFiService> temp_mock_service_;
398
Paul Stewart3c504012013-01-17 17:49:58 -0800399 // These pointers are owned by the manager, and only tracked here for
400 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800401 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700402 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800403 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800404 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700405};
406
Darin Petkov3ec55342012-09-28 14:04:44 +0200407const char ManagerTest::TerminationActionTest::kActionName[] = "action";
408
Paul Stewart22aa71b2011-09-16 12:15:11 -0700409bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700410 if (!manager()->sort_services_task_.IsCancelled()) {
411 manager()->SortServicesTask();
412 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700413 return (svc0.get() == manager()->services_[0].get() &&
414 svc1.get() == manager()->services_[1].get());
415}
416
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700417TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700418 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
419 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700420}
421
Chris Masone9be4a9d2011-05-16 15:44:09 -0700422TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700423 ON_CALL(*mock_devices_[0].get(), technology())
424 .WillByDefault(Return(Technology::kEthernet));
425 ON_CALL(*mock_devices_[1].get(), technology())
426 .WillByDefault(Return(Technology::kWifi));
427 ON_CALL(*mock_devices_[2].get(), technology())
428 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700429
Paul Stewart22aa71b2011-09-16 12:15:11 -0700430 manager()->RegisterDevice(mock_devices_[0]);
431 manager()->RegisterDevice(mock_devices_[1]);
432 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700433
Paul Stewart22aa71b2011-09-16 12:15:11 -0700434 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
435 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
436 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700437}
438
Paul Stewarta41e38d2011-11-11 07:47:29 -0800439TEST_F(ManagerTest, DeviceRegistrationAndStart) {
440 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500441 mock_devices_[0]->enabled_persistent_ = true;
442 mock_devices_[1]->enabled_persistent_ = false;
443 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800444 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500445 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800446 .Times(0);
447 manager()->RegisterDevice(mock_devices_[0]);
448 manager()->RegisterDevice(mock_devices_[1]);
449}
450
451TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800452 MockProfile *profile =
453 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800454 DeviceRefPtr device_ref(mock_devices_[0].get());
455 AdoptProfile(manager(), profile); // Passes ownership.
456 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200457 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800458 manager()->RegisterDevice(mock_devices_[0]);
459}
460
Chris Masone9be4a9d2011-05-16 15:44:09 -0700461TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700462 ON_CALL(*mock_devices_[0].get(), technology())
463 .WillByDefault(Return(Technology::kEthernet));
464 ON_CALL(*mock_devices_[1].get(), technology())
465 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700466
Gaurav Shah435de2c2011-11-17 19:01:07 -0800467 manager()->RegisterDevice(mock_devices_[0]);
468 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700469
Paul Stewart22aa71b2011-09-16 12:15:11 -0700470 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
471 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700472
Thieu Le5133b712013-02-19 14:47:21 -0800473 MockProfile *profile =
474 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700475 AdoptProfile(manager(), profile); // Passes ownership.
476
Eric Shienbrood9a245532012-03-07 14:20:39 -0500477 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700478 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800479 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700480 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700481
Eric Shienbrood9a245532012-03-07 14:20:39 -0500482 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700483 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800484 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700485 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700486}
487
488TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700489 // It's much easier and safer to use a real GLib for this test.
490 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700491 Manager manager(control_interface(),
492 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800493 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700494 &glib,
495 run_path(),
496 storage_path(),
497 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700498 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
499 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700500 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700501
Chris Masone9be4a9d2011-05-16 15:44:09 -0700502 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700503 new NiceMock<MockService>(control_interface(),
504 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800505 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700506 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700507 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700508 new NiceMock<MockService>(control_interface(),
509 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800510 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700511 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700512
Darin Petkov457728b2013-01-09 09:49:08 +0100513 string service1_name(mock_service->unique_name());
514 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700515
516 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
517 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700518 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700519 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700520 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700521 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700522 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700523
Chris Masone9d779932011-08-25 16:33:41 -0700524 manager.RegisterService(mock_service);
525 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700526
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800527 Error error;
528 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700529 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700530 EXPECT_EQ(2, ids.size());
531 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
532 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700533
Chris Masone9d779932011-08-25 16:33:41 -0700534 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
535 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
536
537 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700538}
539
Chris Masone6515aab2011-10-12 16:19:09 -0700540TEST_F(ManagerTest, RegisterKnownService) {
541 // It's much easier and safer to use a real GLib for this test.
542 GLib glib;
543 Manager manager(control_interface(),
544 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800545 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700546 &glib,
547 run_path(),
548 storage_path(),
549 string());
550 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
551 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700552 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700553 {
554 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
555 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800556 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700557 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700558 ASSERT_TRUE(profile->AdoptService(service1));
559 ASSERT_TRUE(profile->ContainsService(service1));
560 } // Force destruction of service1.
561
562 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
563 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800564 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700565 &manager));
566 manager.RegisterService(service2);
567 EXPECT_EQ(service2->profile().get(), profile.get());
568 manager.Stop();
569}
570
571TEST_F(ManagerTest, RegisterUnknownService) {
572 // It's much easier and safer to use a real GLib for this test.
573 GLib glib;
574 Manager manager(control_interface(),
575 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800576 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700577 &glib,
578 run_path(),
579 storage_path(),
580 string());
581 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
582 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700583 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700584 {
585 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
586 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800587 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700588 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700589 ASSERT_TRUE(profile->AdoptService(service1));
590 ASSERT_TRUE(profile->ContainsService(service1));
591 } // Force destruction of service1.
592 scoped_refptr<MockService> mock_service2(
593 new NiceMock<MockService>(control_interface(),
594 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800595 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700596 &manager));
597 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100598 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700599 manager.RegisterService(mock_service2);
600 EXPECT_NE(mock_service2->profile().get(), profile.get());
601 manager.Stop();
602}
603
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000604TEST_F(ManagerTest, DeregisterUnregisteredService) {
605 // WiFi assumes that it can deregister a service that is not
606 // registered. (E.g. a hidden service can be deregistered when it
607 // loses its last endpoint, and again when WiFi is Stop()-ed.)
608 //
609 // So test that doing so doesn't cause a crash.
610 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
611 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800612 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000613 manager());
614 manager()->DeregisterService(service);
615}
616
Chris Masonea8a2c252011-06-27 22:16:30 -0700617TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700618 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700619 map<string, ::DBus::Variant> props;
620 Error error(Error::kInvalidProperty, "");
621 {
622 ::DBus::Error dbus_error;
623 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700624 manager()->mutable_store()->SetStringProperty(
625 flimflam::kCheckPortalListProperty,
626 expected,
627 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700628 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700629 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
630 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
631 expected);
632 }
633 {
634 ::DBus::Error dbus_error;
635 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700636 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
637 expected,
638 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700639 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700640 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
641 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
642 expected);
643 }
644}
645
Chris Masone3c3f6a12011-07-01 10:01:41 -0700646TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700647 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800648 manager()->RegisterDevice(mock_devices_[0]);
649 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700650 {
651 map<string, ::DBus::Variant> props;
652 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700653 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700654 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700655 vector < ::DBus::Path> devices =
656 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700657 EXPECT_EQ(2, devices.size());
658 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700659}
660
mukesh agrawal2366eed2012-03-20 18:21:50 -0700661TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700662 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700663 map<string, ::DBus::Variant> props;
664 ::DBus::Error dbus_error;
665 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
666 map<string, ::DBus::Variant>::const_iterator prop =
667 props.find(flimflam::kServicesProperty);
668 ASSERT_FALSE(prop == props.end());
669 const ::DBus::Variant &variant = prop->second;
670 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
671}
672
Chris Masone6791a432011-07-12 13:23:19 -0700673TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700674 Manager manager(control_interface(),
675 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800676 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700677 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700678 run_path(),
679 storage_path(),
680 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700681 scoped_refptr<MockService> s2(new MockService(control_interface(),
682 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800683 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700684 &manager));
685 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700686 {
Chris Masone6515aab2011-10-12 16:19:09 -0700687 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700688 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800689 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700690 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700691 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700692 .WillRepeatedly(Return(true));
693 EXPECT_CALL(*storage, Flush())
694 .Times(AnyNumber())
695 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700696 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700697 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700698 }
Chris Masone6515aab2011-10-12 16:19:09 -0700699 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800700 ProfileRefPtr profile(
701 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700702 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700703
Chris Masone6515aab2011-10-12 16:19:09 -0700704 // Now, move the Service |s2| to another profile.
705 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
706 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700707
708 // Force destruction of the original Profile, to ensure that the Service
709 // is kept alive and populated with data.
710 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700711 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700712 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700713}
714
Paul Stewart7f61e522012-03-22 11:13:45 -0700715TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
716 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800717 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700718 const string kProfileName("profile0");
719 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
720 .WillRepeatedly(Return(kProfileName));
721 AdoptProfile(manager(), mock_profile);
722
723 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
724 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
725 EXPECT_EQ(mock_profile.get(), profile.get());
726}
727
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800728TEST_F(ManagerTest, SetProfileForService) {
729 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800730 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800731 string profile_name0("profile0");
732 EXPECT_CALL(*profile0, GetRpcIdentifier())
733 .WillRepeatedly(Return(profile_name0));
734 AdoptProfile(manager(), profile0);
735 scoped_refptr<MockService> service(new MockService(control_interface(),
736 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800737 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800738 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700739 EXPECT_FALSE(manager()->HasService(service));
740 {
741 Error error;
742 EXPECT_CALL(*profile0, AdoptService(_))
743 .WillOnce(Return(true));
744 // Expect that setting the profile of a service that does not already
745 // have one assigned does not cause a crash.
746 manager()->SetProfileForService(service, "profile0", &error);
747 EXPECT_TRUE(error.IsSuccess());
748 }
749
750 // The service should be registered as a side-effect of the profile being
751 // set for this service.
752 EXPECT_TRUE(manager()->HasService(service));
753
754 // Since we have mocked Profile::AdoptServie() above, the service's
755 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800756 service->set_profile(profile0);
757
758 {
759 Error error;
760 manager()->SetProfileForService(service, "foo", &error);
761 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700762 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800763 }
764
765 {
766 Error error;
767 manager()->SetProfileForService(service, profile_name0, &error);
768 EXPECT_EQ(Error::kInvalidArguments, error.type());
769 EXPECT_EQ("Service is already connected to this profile", error.message());
770 }
771
772 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800773 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800774 string profile_name1("profile1");
775 EXPECT_CALL(*profile1, GetRpcIdentifier())
776 .WillRepeatedly(Return(profile_name1));
777 AdoptProfile(manager(), profile1);
778
779 {
780 Error error;
781 EXPECT_CALL(*profile1, AdoptService(_))
782 .WillOnce(Return(true));
783 EXPECT_CALL(*profile0, AbandonService(_))
784 .WillOnce(Return(true));
785 manager()->SetProfileForService(service, profile_name1, &error);
786 EXPECT_TRUE(error.IsSuccess());
787 }
788}
789
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700790TEST_F(ManagerTest, CreateProfile) {
791 // It's much easier to use real Glib here since we want the storage
792 // side-effects.
793 GLib glib;
794 ScopedTempDir temp_dir;
795 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
796
797 Manager manager(control_interface(),
798 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800799 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700800 &glib,
801 run_path(),
802 storage_path(),
803 temp_dir.path().value());
804
805 // Invalid name should be rejected.
806 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
807
Paul Stewartd0a3b812012-03-28 22:48:22 -0700808 // A profile with invalid characters in it should similarly be rejected.
809 EXPECT_EQ(Error::kInvalidArguments,
810 TestCreateProfile(&manager, "valid_profile"));
811
812 // We should be able to create a machine profile.
813 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700814
Gary Morainb672d352012-04-25 09:19:06 -0700815 // We should succeed in creating a valid user profile. Verify the returned
816 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700817 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700818 {
819 Error error;
820 string path;
821 manager.CreateProfile(kProfile, &path, &error);
822 EXPECT_EQ(Error::kSuccess, error.type());
823 EXPECT_EQ("/profile_rpc", path);
824 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700825
826 // We should fail in creating it a second time (already exists).
827 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
828}
829
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700830// We receive PopProfile when a user logs out, and it should always trigger a
831// MemoryLog Clear() call.
832TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
833 GLib glib;
834 ScopedTempDir temp_dir;
835 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
836 Manager manager(control_interface(),
837 dispatcher(),
838 metrics(),
839 &glib,
840 run_path(),
841 storage_path(),
842 temp_dir.path().value());
843 const char kProfile0[] = "~user/profile0";
844 const char kPurgedMessage[] = "This message should be purged";
845 // Create a profile and push it on the stack, leave one uncreated
846 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
847 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
848
849 // Popping a profile which isn't on top should still clear the log.
850 LOG(INFO) << kPurgedMessage;
851 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
852 kPurgedMessage));
853 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
854 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
855 kPurgedMessage));
856
857 // Popping an invalid profile name should do the same thing.
858 LOG(INFO) << kPurgedMessage;
859 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
860 kPurgedMessage));
861 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
862 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
863 kPurgedMessage));
864
865 // Successful pops also purge the message log.
866 LOG(INFO) << kPurgedMessage;
867 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
868 kPurgedMessage));
869 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
870 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
871 kPurgedMessage));
872}
873
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700874TEST_F(ManagerTest, PushPopProfile) {
875 // It's much easier to use real Glib in creating a Manager for this
876 // test here since we want the storage side-effects.
877 GLib glib;
878 ScopedTempDir temp_dir;
879 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
880 Manager manager(control_interface(),
881 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800882 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700883 &glib,
884 run_path(),
885 storage_path(),
886 temp_dir.path().value());
887
888 // Pushing an invalid profile should fail.
889 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
890
Paul Stewartd0a3b812012-03-28 22:48:22 -0700891 // Pushing a default profile that does not exist should fail.
892 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700893
894 const char kProfile0[] = "~user/profile0";
895 const char kProfile1[] = "~user/profile1";
896
897 // Create a couple of profiles.
898 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
899 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
900
901 // Push these profiles on the stack.
902 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
903 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
904
905 // Pushing a profile a second time should fail.
906 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
907 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
908
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800909 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700910 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800911 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700912
913 // Make sure a profile name that doesn't exist fails.
914 const char kProfile2Id[] = "profile2";
915 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
916 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
917
918 // Create a new service, with a specific storage name.
919 scoped_refptr<MockService> service(
920 new NiceMock<MockService>(control_interface(),
921 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800922 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700923 &manager));
924 const char kServiceName[] = "service_storage_name";
925 EXPECT_CALL(*service.get(), GetStorageIdentifier())
926 .WillRepeatedly(Return(kServiceName));
927 EXPECT_CALL(*service.get(), Load(_))
928 .WillRepeatedly(Return(true));
929
930 // Add this service to the manager -- it should end up in the ephemeral
931 // profile.
932 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800933 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700934
935 // Create storage for a profile that contains the service storage name.
936 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
937 kServiceName));
938
939 // When we push the profile, the service should move away from the
940 // ephemeral profile to this new profile since it has an entry for
941 // this service.
942 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800943 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700944 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
945
946 // Insert another profile that should supersede ownership of the service.
947 const char kProfile3Id[] = "profile3";
948 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
949 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
950 kServiceName));
951 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
952 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
953
954 // Popping an invalid profile name should fail.
955 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
956
957 // Popping an profile that is not at the top of the stack should fail.
958 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
959
960 // Popping the top profile should succeed.
961 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
962
963 // Moreover the service should have switched profiles to profile 2.
964 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
965
966 // Popping the top profile should succeed.
967 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
968
969 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800970 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700971
972 // Pop the remaining two services off the stack.
973 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
974 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
975
976 // Next pop should fail with "stack is empty".
977 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700978
979 const char kMachineProfile0[] = "machineprofile0";
980 const char kMachineProfile1[] = "machineprofile1";
981 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
982 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
983
984 // Should be able to push a machine profile.
985 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
986
987 // Should be able to push a user profile atop a machine profile.
988 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
989
990 // Pushing a system-wide profile on top of a user profile should fail.
991 EXPECT_EQ(Error::kInvalidArguments,
992 TestPushProfile(&manager, kMachineProfile1));
993
994 // However if we pop the user profile, we should be able stack another
995 // machine profile on.
996 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
997 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -0700998
999 // Add two user profiles to the top of the stack.
1000 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1001 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
1002 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
1003 EXPECT_EQ(4, profiles.size());
1004
1005 // PopAllUserProfiles should remove both user profiles, leaving the two
1006 // machine profiles.
1007 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1008 EXPECT_EQ(2, profiles.size());
1009 EXPECT_TRUE(profiles[0]->GetUser().empty());
1010 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001011
1012 // Use InsertUserProfile() instead. Although a machine profile is valid
1013 // in this state, it cannot be added via InsertUserProfile.
1014 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1015 EXPECT_EQ(Error::kInvalidArguments,
1016 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1017 const char kUserHash0[] = "userhash0";
1018 const char kUserHash1[] = "userhash1";
1019 EXPECT_EQ(Error::kSuccess,
1020 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1021 EXPECT_EQ(Error::kSuccess,
1022 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1023 EXPECT_EQ(3, profiles.size());
1024 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1025 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001026}
1027
Paul Stewarte73d05c2012-03-29 16:26:05 -07001028TEST_F(ManagerTest, RemoveProfile) {
1029 // It's much easier to use real Glib in creating a Manager for this
1030 // test here since we want the storage side-effects.
1031 GLib glib;
1032 ScopedTempDir temp_dir;
1033 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1034 Manager manager(control_interface(),
1035 dispatcher(),
1036 metrics(),
1037 &glib,
1038 run_path(),
1039 storage_path(),
1040 temp_dir.path().value());
1041
1042 const char kProfile0[] = "profile0";
1043 FilePath profile_path(
1044 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1045
1046 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1047 ASSERT_TRUE(file_util::PathExists(profile_path));
1048
1049 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1050
1051 // Remove should fail since the profile is still on the stack.
1052 {
1053 Error error;
1054 manager.RemoveProfile(kProfile0, &error);
1055 EXPECT_EQ(Error::kInvalidArguments, error.type());
1056 }
1057
1058 // Profile path should still exist.
1059 EXPECT_TRUE(file_util::PathExists(profile_path));
1060
1061 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1062
1063 // This should succeed now that the profile is off the stack.
1064 {
1065 Error error;
1066 manager.RemoveProfile(kProfile0, &error);
1067 EXPECT_EQ(Error::kSuccess, error.type());
1068 }
1069
1070 // Profile path should no longer exist.
1071 EXPECT_FALSE(file_util::PathExists(profile_path));
1072
1073 // Another remove succeeds, due to a foible in file_util::Delete --
1074 // it is not an error to delete a file that does not exist.
1075 {
1076 Error error;
1077 manager.RemoveProfile(kProfile0, &error);
1078 EXPECT_EQ(Error::kSuccess, error.type());
1079 }
1080
1081 // Let's create an error case that will "work". Create a non-empty
1082 // directory in the place of the profile pathname.
1083 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
1084 {
1085 Error error;
1086 manager.RemoveProfile(kProfile0, &error);
1087 EXPECT_EQ(Error::kOperationFailed, error.type());
1088 }
1089}
1090
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001091TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1092 // It's much easier to use real Glib in creating a Manager for this
1093 // test here since we want the storage side-effects.
1094 GLib glib;
1095 ScopedTempDir temp_dir;
1096 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1097 Manager manager(control_interface(),
1098 dispatcher(),
1099 metrics(),
1100 &glib,
1101 run_path(),
1102 storage_path(),
1103 temp_dir.path().value());
1104
1105 const char kProfile0[] = "profile0";
1106 FilePath profile_path(
1107 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1108
1109 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1110 ASSERT_TRUE(file_util::PathExists(profile_path));
1111 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1112
1113 // Ensure that even if the backing filestore is removed, we still can't
1114 // create a profile twice.
1115 ASSERT_TRUE(file_util::Delete(profile_path, false));
1116 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1117}
1118
Paul Stewart75225512012-01-26 22:51:33 -08001119// Use this matcher instead of passing RefPtrs directly into the arguments
1120// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1121// system teardown.
1122MATCHER_P(IsRefPtrTo, ref_address, "") {
1123 return arg.get() == ref_address;
1124}
1125
1126TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1127 MockServiceRefPtr s_not_in_profile(
1128 new NiceMock<MockService>(control_interface(),
1129 dispatcher(),
1130 metrics(),
1131 manager()));
1132 MockServiceRefPtr s_not_in_group(
1133 new NiceMock<MockService>(control_interface(),
1134 dispatcher(),
1135 metrics(),
1136 manager()));
1137 MockServiceRefPtr s_configure_fail(
1138 new NiceMock<MockService>(control_interface(),
1139 dispatcher(),
1140 metrics(),
1141 manager()));
1142 MockServiceRefPtr s_configure_succeed(
1143 new NiceMock<MockService>(control_interface(),
1144 dispatcher(),
1145 metrics(),
1146 manager()));
1147
1148 string entry_name("entry_name");
1149 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1150 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1151 .WillRepeatedly(Return("not_entry_name"));
1152 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1153 .WillRepeatedly(Return(entry_name));
1154 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1155 .WillRepeatedly(Return(entry_name));
1156
1157 manager()->RegisterService(s_not_in_profile);
1158 manager()->RegisterService(s_not_in_group);
1159 manager()->RegisterService(s_configure_fail);
1160 manager()->RegisterService(s_configure_succeed);
1161
1162 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001163 new StrictMock<MockProfile>(
1164 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001165 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001166 new StrictMock<MockProfile>(
1167 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001168
1169 s_not_in_group->set_profile(profile1);
1170 s_configure_fail->set_profile(profile1);
1171 s_configure_succeed->set_profile(profile1);
1172
1173 AdoptProfile(manager(), profile0);
1174 AdoptProfile(manager(), profile1);
1175
1176 // No services are a member of this profile.
1177 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1178
1179 // No services that are members of this profile have this entry name.
1180 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1181
1182 // Only services that are members of the profile and group will be abandoned.
1183 EXPECT_CALL(*profile1.get(),
1184 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1185 EXPECT_CALL(*profile1.get(),
1186 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1187 EXPECT_CALL(*profile1.get(),
1188 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1189 .WillOnce(Return(true));
1190 EXPECT_CALL(*profile1.get(),
1191 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1192 .WillOnce(Return(true));
1193
1194 // Never allow services to re-join profile1.
1195 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1196 .WillRepeatedly(Return(false));
1197
1198 // Only allow one of the members of the profile and group to successfully
1199 // join profile0.
1200 EXPECT_CALL(*profile0.get(),
1201 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1202 EXPECT_CALL(*profile0.get(),
1203 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1204 EXPECT_CALL(*profile0.get(),
1205 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1206 .WillOnce(Return(false));
1207 EXPECT_CALL(*profile0.get(),
1208 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1209 .WillOnce(Return(true));
1210
1211 // Expect the failed-to-configure service to have Unload() called on it.
1212 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1213 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1214 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1215 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1216
1217 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1218
1219 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1220 EXPECT_EQ(profile1, s_not_in_group->profile());
1221 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1222
1223 // Since we are using a MockProfile, the profile does not actually change,
1224 // since ConfigureService was not actually called on the service.
1225 EXPECT_EQ(profile1, s_configure_succeed->profile());
1226}
1227
Paul Stewart65512e12012-03-26 18:01:08 -07001228TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1229 MockServiceRefPtr s_will_remove0(
1230 new NiceMock<MockService>(control_interface(),
1231 dispatcher(),
1232 metrics(),
1233 manager()));
1234 MockServiceRefPtr s_will_remove1(
1235 new NiceMock<MockService>(control_interface(),
1236 dispatcher(),
1237 metrics(),
1238 manager()));
1239 MockServiceRefPtr s_will_not_remove0(
1240 new NiceMock<MockService>(control_interface(),
1241 dispatcher(),
1242 metrics(),
1243 manager()));
1244 MockServiceRefPtr s_will_not_remove1(
1245 new NiceMock<MockService>(control_interface(),
1246 dispatcher(),
1247 metrics(),
1248 manager()));
1249
1250 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1251 .Times(4); // Once for each registration.
1252
1253 string entry_name("entry_name");
1254 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1255 .WillRepeatedly(Return(entry_name));
1256 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1257 .WillRepeatedly(Return(entry_name));
1258 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1259 .WillRepeatedly(Return(entry_name));
1260 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1261 .WillRepeatedly(Return(entry_name));
1262
1263 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001264 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001265 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001266 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001267 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001268 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001269 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001270 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001271
1272 // One for each service added above.
1273 ASSERT_EQ(4, manager()->services_.size());
1274
1275 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001276 new StrictMock<MockProfile>(
1277 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001278
1279 s_will_remove0->set_profile(profile);
1280 s_will_remove1->set_profile(profile);
1281 s_will_not_remove0->set_profile(profile);
1282 s_will_not_remove1->set_profile(profile);
1283
1284 AdoptProfile(manager(), profile);
1285
1286 // Deny any of the services re-entry to the profile.
1287 EXPECT_CALL(*profile, ConfigureService(_))
1288 .WillRepeatedly(Return(false));
1289
1290 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1291 .WillOnce(Return(true));
1292 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1293 .WillOnce(Return(true));
1294 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1295 .WillOnce(Return(true));
1296 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1297 .WillOnce(Return(true));
1298
1299 EXPECT_CALL(*s_will_remove0, Unload())
1300 .WillOnce(Return(true));
1301 EXPECT_CALL(*s_will_remove1, Unload())
1302 .WillOnce(Return(true));
1303 EXPECT_CALL(*s_will_not_remove0, Unload())
1304 .WillOnce(Return(false));
1305 EXPECT_CALL(*s_will_not_remove1, Unload())
1306 .WillOnce(Return(false));
1307
1308
1309 // This will cause all the profiles to be unloaded.
1310 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1311
1312 // 2 of the 4 services added above should have been unregistered and
1313 // removed, leaving 2.
1314 EXPECT_EQ(2, manager()->services_.size());
1315 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1316 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1317}
1318
1319TEST_F(ManagerTest, PopProfileWithUnload) {
1320 MockServiceRefPtr s_will_remove0(
1321 new NiceMock<MockService>(control_interface(),
1322 dispatcher(),
1323 metrics(),
1324 manager()));
1325 MockServiceRefPtr s_will_remove1(
1326 new NiceMock<MockService>(control_interface(),
1327 dispatcher(),
1328 metrics(),
1329 manager()));
1330 MockServiceRefPtr s_will_not_remove0(
1331 new NiceMock<MockService>(control_interface(),
1332 dispatcher(),
1333 metrics(),
1334 manager()));
1335 MockServiceRefPtr s_will_not_remove1(
1336 new NiceMock<MockService>(control_interface(),
1337 dispatcher(),
1338 metrics(),
1339 manager()));
1340
1341 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1342 .Times(5); // Once for each registration, and one after profile pop.
1343
1344 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001345 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001346 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001347 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001348 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001349 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001350 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001351 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001352
1353 // One for each service added above.
1354 ASSERT_EQ(4, manager()->services_.size());
1355
1356 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001357 new StrictMock<MockProfile>(
1358 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001359 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001360 new StrictMock<MockProfile>(
1361 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001362
1363 s_will_remove0->set_profile(profile1);
1364 s_will_remove1->set_profile(profile1);
1365 s_will_not_remove0->set_profile(profile1);
1366 s_will_not_remove1->set_profile(profile1);
1367
1368 AdoptProfile(manager(), profile0);
1369 AdoptProfile(manager(), profile1);
1370
1371 // Deny any of the services entry to profile0, so they will all be unloaded.
1372 EXPECT_CALL(*profile0, ConfigureService(_))
1373 .WillRepeatedly(Return(false));
1374
1375 EXPECT_CALL(*s_will_remove0, Unload())
1376 .WillOnce(Return(true));
1377 EXPECT_CALL(*s_will_remove1, Unload())
1378 .WillOnce(Return(true));
1379 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001380 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001381 EXPECT_CALL(*s_will_not_remove1, Unload())
1382 .WillOnce(Return(false));
1383
Philipp Neubeck79173602012-11-13 21:10:09 +01001384 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1385 // profile list.
1386 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1387 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1388
Paul Stewart65512e12012-03-26 18:01:08 -07001389 // This will pop profile1, which should cause all our profiles to unload.
1390 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001391 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001392
1393 // 2 of the 4 services added above should have been unregistered and
1394 // removed, leaving 2.
1395 EXPECT_EQ(2, manager()->services_.size());
1396 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1397 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001398
1399 // Expect the unloaded services to lose their profile reference.
1400 EXPECT_FALSE(s_will_remove0->profile());
1401 EXPECT_FALSE(s_will_remove1->profile());
1402
1403 // If we explicitly deregister a service, the effect should be the same
1404 // with respect to the profile reference.
1405 ASSERT_TRUE(s_will_not_remove0->profile());
1406 manager()->DeregisterService(s_will_not_remove0);
1407 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001408}
1409
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001410TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001411 {
1412 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001413 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1414 flimflam::kOfflineModeProperty,
1415 PropertyStoreTest::kBoolV,
1416 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001417 }
1418 {
1419 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001420 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1421 flimflam::kCountryProperty,
1422 PropertyStoreTest::kStringV,
1423 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001424 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001425 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001426 {
1427 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001428 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1429 flimflam::kCountryProperty,
1430 PropertyStoreTest::kBoolV,
1431 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001432 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001433 }
1434 {
1435 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001436 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1437 flimflam::kOfflineModeProperty,
1438 PropertyStoreTest::kStringV,
1439 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001440 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001441 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001442 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001443 {
1444 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001445 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001446 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001447 flimflam::kEnabledTechnologiesProperty,
1448 PropertyStoreTest::kStringsV,
1449 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001450 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001451 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001452}
1453
mukesh agrawal32399322011-09-01 10:53:43 -07001454TEST_F(ManagerTest, RequestScan) {
1455 {
1456 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001457 manager()->RegisterDevice(mock_devices_[0].get());
1458 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001459 EXPECT_CALL(*mock_devices_[0], technology())
1460 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie68d41092013-04-02 12:56:02 -07001461 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _));
Joshua Krollda798622012-06-05 12:30:48 -07001462 EXPECT_CALL(*mock_devices_[1], technology())
1463 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie68d41092013-04-02 12:56:02 -07001464 EXPECT_CALL(*mock_devices_[1], Scan(_, _)).Times(0);
1465 manager()->RequestScan(Device::kFullScan, flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001466 }
1467
1468 {
1469 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001470 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001471 EXPECT_EQ(Error::kInvalidArguments, error.type());
1472 }
1473}
1474
Darin Petkovb65c2452012-02-23 15:17:06 +01001475TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001476 KeyValueStore args;
1477 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001478 manager()->GetService(args, &e);
1479 EXPECT_EQ(Error::kInvalidArguments, e.type());
1480 EXPECT_EQ("must specify service type", e.message());
1481}
1482
1483TEST_F(ManagerTest, GetServiceUnknownType) {
1484 KeyValueStore args;
1485 Error e;
1486 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1487 manager()->GetService(args, &e);
1488 EXPECT_EQ(Error::kNotSupported, e.type());
1489 EXPECT_EQ("service type is unsupported", e.message());
1490}
1491
Paul Stewart35eff132013-04-12 12:08:40 -07001492TEST_F(ManagerTest, GetServiceEthernetEap) {
1493 KeyValueStore args;
1494 Error e;
1495 ServiceRefPtr service;
1496 args.SetString(flimflam::kTypeProperty, kTypeEthernetEap);
1497 SetEapProviderService(service);
1498 EXPECT_EQ(service, manager()->GetService(args, &e));
1499 EXPECT_TRUE(e.IsSuccess());
1500}
1501
Darin Petkovb65c2452012-02-23 15:17:06 +01001502TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001503 KeyValueStore args;
1504 Error e;
1505 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001506 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001507 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001508 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001509 manager()->GetService(args, &e);
1510 EXPECT_TRUE(e.IsSuccess());
1511}
1512
Darin Petkov33af05c2012-02-28 10:10:30 +01001513TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1514 KeyValueStore args;
1515 Error e;
1516 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001517 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001518 new StrictMock<MockProfile>(
1519 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001520 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001521 ServiceRefPtr service = manager()->GetService(args, &e);
1522 EXPECT_EQ(Error::kNotSupported, e.type());
1523 EXPECT_FALSE(service);
1524}
1525
Darin Petkovb65c2452012-02-23 15:17:06 +01001526TEST_F(ManagerTest, GetServiceVPN) {
1527 KeyValueStore args;
1528 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001529 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001530 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001531 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
Darin Petkov4e02ba22013-04-02 13:44:08 +02001532 args.SetString(flimflam::kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001533 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001534 new StrictMock<MockProfile>(
1535 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001536 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001537
1538#if defined(DISABLE_VPN)
1539
1540 ServiceRefPtr service = manager()->GetService(args, &e);
1541 EXPECT_EQ(Error::kNotSupported, e.type());
1542 EXPECT_FALSE(service);
1543
1544#else
1545
Paul Stewart7f5ad572012-06-04 15:18:54 -07001546 ServiceRefPtr updated_service;
1547 EXPECT_CALL(*profile, UpdateService(_))
1548 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1549 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001550 EXPECT_CALL(*profile, LoadService(_))
1551 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001552 EXPECT_CALL(*profile, ConfigureService(_))
1553 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001554 ServiceRefPtr service = manager()->GetService(args, &e);
1555 EXPECT_TRUE(e.IsSuccess());
1556 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001557 EXPECT_EQ(service, updated_service);
1558 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001559
1560#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001561}
1562
Darin Petkovc63dcf02012-05-24 11:51:43 +02001563TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1564 KeyValueStore args;
1565 Error e;
1566 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1567 ServiceRefPtr service = manager()->GetService(args, &e);
1568 EXPECT_EQ(Error::kInvalidArguments, e.type());
1569 EXPECT_EQ("Missing WiMAX network id.", e.message());
1570 EXPECT_FALSE(service);
1571}
1572
Darin Petkovd1cd7972012-05-22 15:26:15 +02001573TEST_F(ManagerTest, GetServiceWiMax) {
1574 KeyValueStore args;
1575 Error e;
1576 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001577 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1578 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1579 ServiceRefPtr service = manager()->GetService(args, &e);
1580 EXPECT_TRUE(e.IsSuccess());
1581 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001582}
1583
Paul Stewart7f61e522012-03-22 11:13:45 -07001584TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1585 // Manager calls ActiveProfile() so we need at least one profile installed.
1586 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001587 new NiceMock<MockProfile>(
1588 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001589 AdoptProfile(manager(), profile);
1590
1591 KeyValueStore args;
1592 args.SetString(flimflam::kProfileProperty, "xxx");
1593 Error error;
1594 manager()->ConfigureService(args, &error);
1595 EXPECT_EQ(Error::kInvalidArguments, error.type());
1596 EXPECT_EQ("Invalid profile name xxx", error.message());
1597}
1598
1599TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1600 // Manager calls ActiveProfile() so we need at least one profile installed.
1601 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001602 new NiceMock<MockProfile>(
1603 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001604 AdoptProfile(manager(), profile);
1605
1606 KeyValueStore args;
1607 Error error;
1608 manager()->ConfigureService(args, &error);
1609 EXPECT_EQ(Error::kInvalidArguments, error.type());
1610 EXPECT_EQ("must specify service type", error.message());
1611}
1612
1613// A registered service in the ephemeral profile should be moved to the
1614// active profile as a part of configuration if no profile was explicitly
1615// specified.
1616TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1617 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001618 new NiceMock<MockProfile>(
1619 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001620
1621 AdoptProfile(manager(), profile); // This is now the active profile.
1622
Paul Stewartd2e1c362013-03-03 19:06:07 -08001623 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001624 scoped_refptr<MockWiFiService> service(
1625 new NiceMock<MockWiFiService>(control_interface(),
1626 dispatcher(),
1627 metrics(),
1628 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001629 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001630 ssid,
1631 "",
1632 "",
1633 false));
1634
1635 manager()->RegisterService(service);
1636 service->set_profile(GetEphemeralProfile(manager()));
1637
Paul Stewart3c504012013-01-17 17:49:58 -08001638 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001639 .WillOnce(Return(service));
1640 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1641 .WillOnce(Return(true));
1642 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1643 .WillOnce(Return(true));
1644
1645 KeyValueStore args;
1646 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1647 Error error;
1648 manager()->ConfigureService(args, &error);
1649 EXPECT_TRUE(error.IsSuccess());
1650}
1651
Paul Stewart2c575d22012-12-07 12:28:57 -08001652// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001653// specify a profile, it should be moved from the profile it was previously
1654// in to the specified profile if one was requested.
1655TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1656 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001657 new NiceMock<MockProfile>(
1658 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001659 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001660 new NiceMock<MockProfile>(
1661 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001662
1663 const string kProfileName0 = "profile0";
1664 const string kProfileName1 = "profile1";
1665
1666 EXPECT_CALL(*profile0, GetRpcIdentifier())
1667 .WillRepeatedly(Return(kProfileName0));
1668 EXPECT_CALL(*profile1, GetRpcIdentifier())
1669 .WillRepeatedly(Return(kProfileName1));
1670
1671 AdoptProfile(manager(), profile0);
1672 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1673
Paul Stewartd2e1c362013-03-03 19:06:07 -08001674 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001675 scoped_refptr<MockWiFiService> service(
1676 new NiceMock<MockWiFiService>(control_interface(),
1677 dispatcher(),
1678 metrics(),
1679 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001680 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001681 ssid,
1682 "",
1683 "",
1684 false));
1685
1686 manager()->RegisterService(service);
1687 service->set_profile(profile1);
1688
Paul Stewart3c504012013-01-17 17:49:58 -08001689 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001690 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001691 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1692 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001693 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1694 .WillOnce(Return(true));
1695 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1696 .WillOnce(Return(true));
1697 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1698 .WillOnce(Return(true));
1699
1700 KeyValueStore args;
1701 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1702 args.SetString(flimflam::kProfileProperty, kProfileName0);
1703 Error error;
1704 manager()->ConfigureService(args, &error);
1705 EXPECT_TRUE(error.IsSuccess());
1706 service->set_profile(NULL); // Breaks refcounting loop.
1707}
1708
Paul Stewart2c575d22012-12-07 12:28:57 -08001709// If we configure a service that is already a member of the specified
1710// profile, the Manager should not call LoadService or AdoptService again
1711// on this service.
1712TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1713 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001714 new NiceMock<MockProfile>(
1715 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001716
1717 const string kProfileName0 = "profile0";
1718
1719 EXPECT_CALL(*profile0, GetRpcIdentifier())
1720 .WillRepeatedly(Return(kProfileName0));
1721
1722 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1723
Paul Stewartd2e1c362013-03-03 19:06:07 -08001724 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001725 scoped_refptr<MockWiFiService> service(
1726 new NiceMock<MockWiFiService>(control_interface(),
1727 dispatcher(),
1728 metrics(),
1729 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001730 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001731 ssid,
1732 "",
1733 "",
1734 false));
1735
1736 manager()->RegisterService(service);
1737 service->set_profile(profile0);
1738
Paul Stewart3c504012013-01-17 17:49:58 -08001739 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001740 .WillOnce(Return(service));
1741 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1742 .Times(0);
1743 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1744 .WillOnce(Return(true));
1745 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1746 .Times(0);
1747
1748 KeyValueStore args;
1749 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1750 args.SetString(flimflam::kProfileProperty, kProfileName0);
1751 Error error;
1752 manager()->ConfigureService(args, &error);
1753 EXPECT_TRUE(error.IsSuccess());
1754 service->set_profile(NULL); // Breaks refcounting loop.
1755}
1756
Paul Stewart7f61e522012-03-22 11:13:45 -07001757// An unregistered service should remain unregistered, but its contents should
1758// be saved to the specified profile nonetheless.
1759TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1760 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001761 new NiceMock<MockProfile>(
1762 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001763 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001764 new NiceMock<MockProfile>(
1765 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001766
1767 const string kProfileName0 = "profile0";
1768 const string kProfileName1 = "profile1";
1769
1770 EXPECT_CALL(*profile0, GetRpcIdentifier())
1771 .WillRepeatedly(Return(kProfileName0));
1772 EXPECT_CALL(*profile1, GetRpcIdentifier())
1773 .WillRepeatedly(Return(kProfileName1));
1774
1775 AdoptProfile(manager(), profile0);
1776 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1777
Paul Stewartd2e1c362013-03-03 19:06:07 -08001778 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001779 scoped_refptr<MockWiFiService> service(
1780 new NiceMock<MockWiFiService>(control_interface(),
1781 dispatcher(),
1782 metrics(),
1783 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001784 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001785 ssid,
1786 "",
1787 "",
1788 false));
1789
1790 service->set_profile(profile1);
1791
Paul Stewart3c504012013-01-17 17:49:58 -08001792 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001793 .WillOnce(Return(service));
1794 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1795 .WillOnce(Return(true));
1796 EXPECT_CALL(*profile0, AdoptService(_))
1797 .Times(0);
1798 EXPECT_CALL(*profile1, AdoptService(_))
1799 .Times(0);
1800
1801 KeyValueStore args;
1802 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1803 args.SetString(flimflam::kProfileProperty, kProfileName0);
1804 Error error;
1805 manager()->ConfigureService(args, &error);
1806 EXPECT_TRUE(error.IsSuccess());
1807}
1808
Paul Stewartd2e1c362013-03-03 19:06:07 -08001809TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1810 KeyValueStore args;
1811 Error error;
1812 ServiceRefPtr service =
1813 manager()->ConfigureServiceForProfile("", args, &error);
1814 EXPECT_EQ(Error::kNotSupported, error.type());
1815 EXPECT_EQ("This method only supports WiFi services", error.message());
1816 EXPECT_EQ(NULL, service.get());
1817}
1818
1819TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1820 KeyValueStore args;
1821 args.SetString(flimflam::kTypeProperty, flimflam::kTypeCellular);
1822 Error error;
1823 ServiceRefPtr service =
1824 manager()->ConfigureServiceForProfile("", args, &error);
1825 EXPECT_EQ(Error::kNotSupported, error.type());
1826 EXPECT_EQ("This method only supports WiFi services", error.message());
1827 EXPECT_EQ(NULL, service.get());
1828}
1829
1830TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1831 KeyValueStore args;
1832 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1833 Error error;
1834 ServiceRefPtr service =
1835 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1836 EXPECT_EQ(Error::kNotFound, error.type());
1837 EXPECT_EQ("Profile specified was not found", error.message());
1838 EXPECT_EQ(NULL, service.get());
1839}
1840
1841TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1842 const string kProfileName0 = "profile0";
1843 const string kProfileName1 = "profile1";
1844 scoped_refptr<MockProfile> profile0(
1845 AddNamedMockProfileToManager(manager(), kProfileName0));
1846
1847 KeyValueStore args;
1848 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1849 args.SetString(flimflam::kProfileProperty, kProfileName1);
1850 Error error;
1851 ServiceRefPtr service =
1852 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1853 EXPECT_EQ(Error::kInvalidArguments, error.type());
1854 EXPECT_EQ("Profile argument does not match that in "
1855 "the configuration arguments", error.message());
1856 EXPECT_EQ(NULL, service.get());
1857}
1858
1859TEST_F(ManagerTest,
1860 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1861 const string kProfileName0 = "profile0";
1862 scoped_refptr<MockProfile> profile0(
1863 AddNamedMockProfileToManager(manager(), kProfileName0));
1864 KeyValueStore args;
1865 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1866 args.SetString(flimflam::kProfileProperty, kProfileName0);
1867
1868 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1869 .WillOnce(Return(WiFiServiceRefPtr()));
1870 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1871 .WillOnce(Return(WiFiServiceRefPtr()));
1872 Error error;
1873 ServiceRefPtr service =
1874 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1875 // Since we didn't set the error in the GetService expectation above...
1876 EXPECT_TRUE(error.IsSuccess());
1877 EXPECT_EQ(NULL, service.get());
1878}
1879
1880TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1881 const string kProfileName0 = "profile0";
1882 scoped_refptr<MockProfile> profile0(
1883 AddNamedMockProfileToManager(manager(), kProfileName0));
1884
1885 KeyValueStore args;
1886 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1887
1888 scoped_refptr<MockWiFiService> mock_service(
1889 new NiceMock<MockWiFiService>(control_interface(),
1890 dispatcher(),
1891 metrics(),
1892 manager(),
1893 wifi_provider_,
1894 vector<uint8_t>(),
1895 flimflam::kModeManaged,
1896 flimflam::kSecurityNone,
1897 false));
1898 ServiceRefPtr mock_service_generic(mock_service.get());
1899 mock_service->set_profile(profile0);
1900 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1901 .WillOnce(Return(WiFiServiceRefPtr()));
1902 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
1903 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
1904 .WillOnce(Return(true));
1905 Error error;
1906 ServiceRefPtr service =
1907 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1908 EXPECT_TRUE(error.IsSuccess());
1909 EXPECT_EQ(mock_service.get(), service.get());
1910 mock_service->set_profile(NULL); // Breaks reference cycle.
1911}
1912
1913TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
1914 scoped_refptr<MockService> mock_service(
1915 new NiceMock<MockService>(control_interface(),
1916 dispatcher(),
1917 metrics(),
1918 manager()));
1919 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001920 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001921 manager()->RegisterService(mock_service);
1922 ServiceRefPtr mock_service_generic(mock_service.get());
1923
1924 const string kProfileName = "profile";
1925 scoped_refptr<MockProfile> profile(
1926 AddNamedMockProfileToManager(manager(), kProfileName));
1927 mock_service->set_profile(profile);
1928
1929 EXPECT_CALL(*mock_service, technology())
1930 .WillOnce(Return(Technology::kCellular))
1931 .WillOnce(Return(Technology::kWifi));
1932
1933 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
1934 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1935 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1936
1937 KeyValueStore args;
1938 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1939 args.SetString(flimflam::kGuidProperty, kGUID);
1940
1941 // The first attempt should fail because the service reports a technology
1942 // other than "WiFi".
1943 {
1944 Error error;
1945 ServiceRefPtr service =
1946 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1947 EXPECT_EQ(NULL, service.get());
1948 EXPECT_EQ(Error::kNotSupported, error.type());
1949 EXPECT_EQ("This GUID matches a non-WiFi service", error.message());
1950 }
1951
1952 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
1953 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
1954
1955 {
1956 Error error;
1957 ServiceRefPtr service =
1958 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1959 EXPECT_TRUE(error.IsSuccess());
1960 EXPECT_EQ(mock_service.get(), service.get());
1961 EXPECT_EQ(profile.get(), service->profile().get());
1962 }
1963 mock_service->set_profile(NULL); // Breaks reference cycle.
1964}
1965
1966TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
1967 const string kProfileName = "profile";
1968 scoped_refptr<MockProfile> profile(
1969 AddNamedMockProfileToManager(manager(), kProfileName));
1970
1971 scoped_refptr<MockWiFiService> mock_service(
1972 new NiceMock<MockWiFiService>(control_interface(),
1973 dispatcher(),
1974 metrics(),
1975 manager(),
1976 wifi_provider_,
1977 vector<uint8_t>(),
1978 flimflam::kModeManaged,
1979 flimflam::kSecurityNone,
1980 false));
1981 mock_service->set_profile(profile);
1982 ServiceRefPtr mock_service_generic(mock_service.get());
1983
1984 KeyValueStore args;
1985 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1986 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1987 .WillOnce(Return(mock_service));
1988 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1989 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1990 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
1991 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
1992
1993 Error error;
1994 ServiceRefPtr service =
1995 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1996 EXPECT_TRUE(error.IsSuccess());
1997 EXPECT_EQ(mock_service.get(), service.get());
1998 EXPECT_EQ(profile.get(), service->profile().get());
1999 mock_service->set_profile(NULL); // Breaks reference cycle.
2000}
2001
2002TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2003 const string kProfileName = "profile";
2004 scoped_refptr<MockProfile> profile(
2005 AddNamedMockProfileToManager(manager(), kProfileName));
2006
2007 scoped_refptr<MockWiFiService> mock_service(
2008 new NiceMock<MockWiFiService>(control_interface(),
2009 dispatcher(),
2010 metrics(),
2011 manager(),
2012 wifi_provider_,
2013 vector<uint8_t>(),
2014 flimflam::kModeManaged,
2015 flimflam::kSecurityNone,
2016 false));
2017 mock_service->set_profile(GetEphemeralProfile(manager()));
2018 ServiceRefPtr mock_service_generic(mock_service.get());
2019
2020 KeyValueStore args;
2021 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2022 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2023 .WillOnce(Return(mock_service));
2024 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2025 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2026 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2027
2028 Error error;
2029 ServiceRefPtr service =
2030 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2031 EXPECT_TRUE(error.IsSuccess());
2032 EXPECT_EQ(mock_service.get(), service.get());
2033 EXPECT_EQ(profile.get(), service->profile().get());
2034 mock_service->set_profile(NULL); // Breaks reference cycle.
2035}
2036
2037TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2038 const string kProfileName0 = "profile0";
2039 scoped_refptr<MockProfile> profile0(
2040 AddNamedMockProfileToManager(manager(), kProfileName0));
2041 const string kProfileName1 = "profile1";
2042 scoped_refptr<MockProfile> profile1(
2043 AddNamedMockProfileToManager(manager(), kProfileName1));
2044
2045 scoped_refptr<MockWiFiService> mock_service(
2046 new NiceMock<MockWiFiService>(control_interface(),
2047 dispatcher(),
2048 metrics(),
2049 manager(),
2050 wifi_provider_,
2051 vector<uint8_t>(),
2052 flimflam::kModeManaged,
2053 flimflam::kSecurityNone,
2054 false));
2055 manager()->RegisterService(mock_service);
2056 mock_service->set_profile(profile0);
2057 ServiceRefPtr mock_service_generic(mock_service.get());
2058
2059 KeyValueStore args;
2060 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2061 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2062 .WillOnce(Return(mock_service));
2063 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2064 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2065 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2066 // This happens once to make the service loadable for the ConfigureService
2067 // below, and a second time after the service is modified.
2068 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2069 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2070 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2071 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2072
2073 Error error;
2074 ServiceRefPtr service =
2075 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2076 EXPECT_TRUE(error.IsSuccess());
2077 EXPECT_EQ(mock_service.get(), service.get());
2078 mock_service->set_profile(NULL); // Breaks reference cycle.
2079}
2080
2081TEST_F(ManagerTest,
2082 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2083 const string kProfileName0 = "profile0";
2084 scoped_refptr<MockProfile> profile0(
2085 AddNamedMockProfileToManager(manager(), kProfileName0));
2086 const string kProfileName1 = "profile1";
2087 scoped_refptr<MockProfile> profile1(
2088 AddNamedMockProfileToManager(manager(), kProfileName1));
2089
2090 scoped_refptr<MockWiFiService> matching_service(
2091 new StrictMock<MockWiFiService>(control_interface(),
2092 dispatcher(),
2093 metrics(),
2094 manager(),
2095 wifi_provider_,
2096 vector<uint8_t>(),
2097 flimflam::kModeManaged,
2098 flimflam::kSecurityNone,
2099 false));
2100 matching_service->set_profile(profile1);
2101
2102 // We need to get rid of our reference to this mock service as soon
2103 // as Manager::ConfigureServiceForProfile() takes a reference in its
2104 // call to WiFiProvider::CreateTemporaryService(). This way the
2105 // latter function can keep a DCHECK(service->HasOneRef() even in
2106 // unit tests.
2107 temp_mock_service_ =
2108 new NiceMock<MockWiFiService>(control_interface(),
2109 dispatcher(),
2110 metrics(),
2111 manager(),
2112 wifi_provider_,
2113 vector<uint8_t>(),
2114 flimflam::kModeManaged,
2115 flimflam::kSecurityNone,
2116 false);
2117
2118 // Only hold a pointer here so we don't affect the refcount.
2119 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2120
2121 KeyValueStore args;
2122 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2123 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2124 .WillOnce(Return(matching_service));
2125 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2126 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2127 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2128 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2129 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2130 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2131 .Times(1);
2132 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2133 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2134
2135 Error error;
2136 ServiceRefPtr service =
2137 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2138 EXPECT_TRUE(error.IsSuccess());
2139 EXPECT_EQ(NULL, service.get());
2140 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2141}
2142
Paul Stewart7a20aa42013-01-17 12:21:41 -08002143TEST_F(ManagerTest, FindMatchingService) {
2144 KeyValueStore args;
2145 {
2146 Error error;
2147 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2148 EXPECT_EQ(Error::kNotFound, error.type());
2149 }
2150
2151 scoped_refptr<MockService> mock_service0(
2152 new NiceMock<MockService>(control_interface(),
2153 dispatcher(),
2154 metrics(),
2155 manager()));
2156 scoped_refptr<MockService> mock_service1(
2157 new NiceMock<MockService>(control_interface(),
2158 dispatcher(),
2159 metrics(),
2160 manager()));
2161 manager()->RegisterService(mock_service0);
2162 manager()->RegisterService(mock_service1);
2163 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2164 .WillOnce(Return(true))
2165 .WillRepeatedly(Return(false));
2166 {
2167 Error error;
2168 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2169 EXPECT_TRUE(error.IsSuccess());
2170 }
2171 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2172 .WillOnce(Return(true))
2173 .WillRepeatedly(Return(false));
2174 {
2175 Error error;
2176 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2177 EXPECT_TRUE(error.IsSuccess());
2178 }
2179 {
2180 Error error;
2181 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2182 EXPECT_EQ(Error::kNotFound, error.type());
2183 }
2184}
2185
Paul Stewart22aa71b2011-09-16 12:15:11 -07002186TEST_F(ManagerTest, TechnologyOrder) {
2187 Error error;
2188 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
2189 string(flimflam::kTypeWifi), &error);
2190 ASSERT_TRUE(error.IsSuccess());
2191 EXPECT_EQ(manager()->GetTechnologyOrder(),
2192 string(flimflam::kTypeEthernet) + "," +
2193 string(flimflam::kTypeWifi));
2194
2195 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
2196 string(flimflam::kTypeWifi), &error);
2197 ASSERT_FALSE(error.IsSuccess());
2198 EXPECT_EQ(Error::kInvalidArguments, error.type());
2199 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
2200 string(flimflam::kTypeWifi),
2201 manager()->GetTechnologyOrder());
2202}
2203
2204TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002205 // TODO(quiche): Some of these tests would probably fit better in
2206 // service_unittest, since the actual comparison of Services is
2207 // implemented in Service. (crosbug.com/23370)
2208
Paul Stewart22aa71b2011-09-16 12:15:11 -07002209 scoped_refptr<MockService> mock_service0(
2210 new NiceMock<MockService>(control_interface(),
2211 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002212 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002213 manager()));
2214 scoped_refptr<MockService> mock_service1(
2215 new NiceMock<MockService>(control_interface(),
2216 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002217 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002218 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002219
2220 manager()->RegisterService(mock_service0);
2221 manager()->RegisterService(mock_service1);
2222
Darin Petkov457728b2013-01-09 09:49:08 +01002223 // Services should already be sorted by |unique_name_|
Paul Stewart22aa71b2011-09-16 12:15:11 -07002224 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2225
2226 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002227 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002228 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2229
2230 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01002231 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002232 manager()->UpdateService(mock_service1);
2233 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2234
2235 // Security
mukesh agrawal43970a22013-02-15 16:00:07 -08002236 mock_service0->SetSecurity(Service::kCryptoAes, true, true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002237 manager()->UpdateService(mock_service0);
2238 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2239
2240 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07002241 EXPECT_CALL(*mock_service0.get(), technology())
2242 .WillRepeatedly(Return((Technology::kWifi)));
2243 EXPECT_CALL(*mock_service1.get(), technology())
2244 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002245
2246 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002247 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002248 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002249 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2250
2251 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
2252 string(flimflam::kTypeEthernet), &error);
2253 EXPECT_TRUE(error.IsSuccess());
2254 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2255
Gaurav Shah435de2c2011-11-17 19:01:07 -08002256 // Priority.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002257 mock_service0->SetPriority(1, NULL);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002258 manager()->UpdateService(mock_service0);
2259 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2260
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002261 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00002262 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002263 manager()->UpdateService(mock_service1);
2264 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2265
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002266 // Auto-connect.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002267 mock_service0->SetAutoConnect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002268 manager()->UpdateService(mock_service0);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002269 mock_service1->SetAutoConnect(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002270 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002271 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2272
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002273 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2274 // to any of the others below, so we reset to the default state after
2275 // testing.
2276 EXPECT_CALL(*mock_service1.get(),
2277 IsDependentOn(ServiceRefPtr(mock_service0.get())))
2278 .WillOnce(Return(true))
2279 .WillRepeatedly(Return(false));
2280 manager()->UpdateService(mock_service1);
2281 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2282 manager()->UpdateService(mock_service0);
2283 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2284
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002285 // Connectable.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002286 mock_service1->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002287 manager()->UpdateService(mock_service1);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002288 mock_service0->SetConnectable(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002289 manager()->UpdateService(mock_service0);
2290 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2291
2292 // IsFailed.
2293 EXPECT_CALL(*mock_service0.get(), state())
2294 .WillRepeatedly(Return(Service::kStateIdle));
2295 EXPECT_CALL(*mock_service0.get(), IsFailed())
2296 .WillRepeatedly(Return(false));
2297 manager()->UpdateService(mock_service0);
2298 EXPECT_CALL(*mock_service0.get(), state())
2299 .WillRepeatedly(Return(Service::kStateFailure));
2300 EXPECT_CALL(*mock_service1.get(), IsFailed())
2301 .WillRepeatedly(Return(true));
2302 manager()->UpdateService(mock_service1);
2303 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2304
2305 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07002306 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002307 .WillRepeatedly(Return(Service::kStateAssociating));
2308 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002309 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002310 manager()->UpdateService(mock_service1);
2311 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2312
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002313 // Connected.
2314 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002315 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002316 EXPECT_CALL(*mock_service0.get(), IsConnected())
2317 .WillRepeatedly(Return(true));
2318 manager()->UpdateService(mock_service0);
2319 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2320
Paul Stewarta121c442012-06-09 14:12:58 -07002321 // Portal.
2322 EXPECT_CALL(*mock_service1.get(), state())
2323 .WillRepeatedly(Return(Service::kStateConnected));
2324 EXPECT_CALL(*mock_service1.get(), IsConnected())
2325 .WillRepeatedly(Return(true));
2326 manager()->UpdateService(mock_service1);
2327 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2328
Paul Stewart22aa71b2011-09-16 12:15:11 -07002329 manager()->DeregisterService(mock_service0);
2330 manager()->DeregisterService(mock_service1);
2331}
2332
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002333TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002334 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002335 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002336
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002337 scoped_refptr<MockService> mock_service0(
2338 new NiceMock<MockService>(control_interface(),
2339 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002340 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002341 manager()));
2342 scoped_refptr<MockService> mock_service1(
2343 new NiceMock<MockService>(control_interface(),
2344 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002345 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002346 manager()));
2347
2348 scoped_refptr<MockConnection> mock_connection0(
2349 new NiceMock<MockConnection>(device_info_.get()));
2350 scoped_refptr<MockConnection> mock_connection1(
2351 new NiceMock<MockConnection>(device_info_.get()));
2352
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002353 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002354 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002355 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002356 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002357 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002358 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002359
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002360 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002361 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002362
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002363 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002364 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002365 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002366
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002367 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002368 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002369 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002370
Paul Stewartce4ec192012-03-14 12:53:46 -07002371 mock_service0->set_mock_connection(mock_connection0);
2372 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002373
2374 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002375 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002376 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002377
Darin Petkova5e07ef2012-07-09 14:27:57 +02002378 ServiceWatcher service_watcher;
2379 int tag =
2380 manager()->RegisterDefaultServiceCallback(
2381 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2382 service_watcher.AsWeakPtr()));
2383 EXPECT_EQ(1, tag);
2384
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002385 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002386 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2387 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002388 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002389 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002390 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002391
Darin Petkova5e07ef2012-07-09 14:27:57 +02002392 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002393 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002394 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00002395 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002396 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002397 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002398 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002399
Paul Stewartce4ec192012-03-14 12:53:46 -07002400 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002401 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002402 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002403 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002404
2405 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002406 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002407}
2408
Darin Petkova5e07ef2012-07-09 14:27:57 +02002409TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2410 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2411 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2412
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002413 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002414 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002415
2416 scoped_refptr<MockService> mock_service(
2417 new NiceMock<MockService>(
2418 control_interface(), dispatcher(), metrics(), manager()));
2419 ServiceRefPtr service = mock_service;
2420 ServiceRefPtr null_service;
2421
2422 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2423 manager()->NotifyDefaultServiceChanged(null_service);
2424
2425 ServiceWatcher service_watcher1;
2426 ServiceWatcher service_watcher2;
2427 int tag1 =
2428 manager()->RegisterDefaultServiceCallback(
2429 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2430 service_watcher1.AsWeakPtr()));
2431 EXPECT_EQ(1, tag1);
2432 int tag2 =
2433 manager()->RegisterDefaultServiceCallback(
2434 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2435 service_watcher2.AsWeakPtr()));
2436 EXPECT_EQ(2, tag2);
2437
2438 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2439 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2440 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2441 manager()->NotifyDefaultServiceChanged(null_service);
2442
2443 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2444 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2445 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2446 manager()->NotifyDefaultServiceChanged(mock_service);
2447
2448 manager()->DeregisterDefaultServiceCallback(tag1);
2449 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2450 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2451 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2452 manager()->NotifyDefaultServiceChanged(mock_service);
2453 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2454
2455 manager()->DeregisterDefaultServiceCallback(tag2);
2456 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2457 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2458 manager()->NotifyDefaultServiceChanged(mock_service);
2459
2460 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2461 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2462}
2463
Gaurav Shah435de2c2011-11-17 19:01:07 -08002464TEST_F(ManagerTest, AvailableTechnologies) {
2465 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2466 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002467 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002468 manager(),
2469 "null4",
2470 "addr4",
2471 0));
2472 manager()->RegisterDevice(mock_devices_[0]);
2473 manager()->RegisterDevice(mock_devices_[1]);
2474 manager()->RegisterDevice(mock_devices_[2]);
2475 manager()->RegisterDevice(mock_devices_[3]);
2476
2477 ON_CALL(*mock_devices_[0].get(), technology())
2478 .WillByDefault(Return(Technology::kEthernet));
2479 ON_CALL(*mock_devices_[1].get(), technology())
2480 .WillByDefault(Return(Technology::kWifi));
2481 ON_CALL(*mock_devices_[2].get(), technology())
2482 .WillByDefault(Return(Technology::kCellular));
2483 ON_CALL(*mock_devices_[3].get(), technology())
2484 .WillByDefault(Return(Technology::kWifi));
2485
2486 set<string> expected_technologies;
2487 expected_technologies.insert(Technology::NameFromIdentifier(
2488 Technology::kEthernet));
2489 expected_technologies.insert(Technology::NameFromIdentifier(
2490 Technology::kWifi));
2491 expected_technologies.insert(Technology::NameFromIdentifier(
2492 Technology::kCellular));
2493 Error error;
2494 vector<string> technologies = manager()->AvailableTechnologies(&error);
2495
2496 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2497 ContainerEq(expected_technologies));
2498}
2499
2500TEST_F(ManagerTest, ConnectedTechnologies) {
2501 scoped_refptr<MockService> connected_service1(
2502 new NiceMock<MockService>(control_interface(),
2503 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002504 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002505 manager()));
2506 scoped_refptr<MockService> connected_service2(
2507 new NiceMock<MockService>(control_interface(),
2508 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002509 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002510 manager()));
2511 scoped_refptr<MockService> disconnected_service1(
2512 new NiceMock<MockService>(control_interface(),
2513 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002514 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002515 manager()));
2516 scoped_refptr<MockService> disconnected_service2(
2517 new NiceMock<MockService>(control_interface(),
2518 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002519 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002520 manager()));
2521
2522 ON_CALL(*connected_service1.get(), IsConnected())
2523 .WillByDefault(Return(true));
2524 ON_CALL(*connected_service2.get(), IsConnected())
2525 .WillByDefault(Return(true));
2526
2527 manager()->RegisterService(connected_service1);
2528 manager()->RegisterService(connected_service2);
2529 manager()->RegisterService(disconnected_service1);
2530 manager()->RegisterService(disconnected_service2);
2531
2532 manager()->RegisterDevice(mock_devices_[0]);
2533 manager()->RegisterDevice(mock_devices_[1]);
2534 manager()->RegisterDevice(mock_devices_[2]);
2535 manager()->RegisterDevice(mock_devices_[3]);
2536
2537 ON_CALL(*mock_devices_[0].get(), technology())
2538 .WillByDefault(Return(Technology::kEthernet));
2539 ON_CALL(*mock_devices_[1].get(), technology())
2540 .WillByDefault(Return(Technology::kWifi));
2541 ON_CALL(*mock_devices_[2].get(), technology())
2542 .WillByDefault(Return(Technology::kCellular));
2543 ON_CALL(*mock_devices_[3].get(), technology())
2544 .WillByDefault(Return(Technology::kWifi));
2545
2546 mock_devices_[0]->SelectService(connected_service1);
2547 mock_devices_[1]->SelectService(disconnected_service1);
2548 mock_devices_[2]->SelectService(disconnected_service2);
2549 mock_devices_[3]->SelectService(connected_service2);
2550
2551 set<string> expected_technologies;
2552 expected_technologies.insert(Technology::NameFromIdentifier(
2553 Technology::kEthernet));
2554 expected_technologies.insert(Technology::NameFromIdentifier(
2555 Technology::kWifi));
2556 Error error;
2557
2558 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2559 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2560 ContainerEq(expected_technologies));
2561}
2562
2563TEST_F(ManagerTest, DefaultTechnology) {
2564 scoped_refptr<MockService> connected_service(
2565 new NiceMock<MockService>(control_interface(),
2566 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002567 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002568 manager()));
2569 scoped_refptr<MockService> disconnected_service(
2570 new NiceMock<MockService>(control_interface(),
2571 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002572 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002573 manager()));
2574
2575 // Connected. WiFi.
2576 ON_CALL(*connected_service.get(), IsConnected())
2577 .WillByDefault(Return(true));
2578 ON_CALL(*connected_service.get(), state())
2579 .WillByDefault(Return(Service::kStateConnected));
2580 ON_CALL(*connected_service.get(), technology())
2581 .WillByDefault(Return(Technology::kWifi));
2582
2583 // Disconnected. Ethernet.
2584 ON_CALL(*disconnected_service.get(), technology())
2585 .WillByDefault(Return(Technology::kEthernet));
2586
2587 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002588 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002589 Error error;
2590 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2591
2592
2593 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002594 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002595 // Connected service should be brought to the front now.
2596 string expected_technology =
2597 Technology::NameFromIdentifier(Technology::kWifi);
2598 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2599}
2600
Paul Stewart212d60f2012-07-12 10:59:13 -07002601TEST_F(ManagerTest, Stop) {
2602 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002603 new NiceMock<MockProfile>(
2604 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002605 AdoptProfile(manager(), profile);
2606 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002607 new NiceMock<MockService>(control_interface(),
2608 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002609 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002610 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002611 manager()->RegisterService(service);
2612 manager()->RegisterDevice(mock_devices_[0]);
2613 EXPECT_CALL(*profile.get(),
2614 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2615 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002616 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002617 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2618 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002619 manager()->Stop();
2620}
2621
mukesh agrawal00917ce2011-11-22 23:56:55 +00002622TEST_F(ManagerTest, UpdateServiceConnected) {
2623 scoped_refptr<MockService> mock_service(
2624 new NiceMock<MockService>(control_interface(),
2625 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002626 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002627 manager()));
2628 manager()->RegisterService(mock_service);
2629 EXPECT_FALSE(mock_service->favorite());
2630 EXPECT_FALSE(mock_service->auto_connect());
2631
Gaurav Shah435de2c2011-11-17 19:01:07 -08002632 EXPECT_CALL(*mock_service.get(), IsConnected())
2633 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002634 manager()->UpdateService(mock_service);
2635 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2636 // to mock out MakeFavorite. And mocking that out would break the
2637 // SortServices test. (crosbug.com/23370)
2638 EXPECT_TRUE(mock_service->favorite());
2639 EXPECT_TRUE(mock_service->auto_connect());
2640}
2641
Thieu Led4e9e552012-02-16 16:26:07 -08002642TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2643 // This tests the case where the user connects to a service that is
2644 // currently associated with a profile. We want to make sure that the
2645 // favorite flag is set and that the flag is saved to the current
2646 // profile.
2647 scoped_refptr<MockService> mock_service(
2648 new NiceMock<MockService>(control_interface(),
2649 dispatcher(),
2650 metrics(),
2651 manager()));
2652 manager()->RegisterService(mock_service);
2653 EXPECT_FALSE(mock_service->favorite());
2654 EXPECT_FALSE(mock_service->auto_connect());
2655
Gary Moraind93615e2012-04-27 11:50:03 -07002656 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002657 new MockProfile(
2658 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002659
Gary Moraind93615e2012-04-27 11:50:03 -07002660 mock_service->set_profile(profile);
2661 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002662 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002663 EXPECT_CALL(*profile,
2664 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002665 manager()->UpdateService(mock_service);
2666 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2667 // to mock out MakeFavorite. And mocking that out would break the
2668 // SortServices test. (crosbug.com/23370)
2669 EXPECT_TRUE(mock_service->favorite());
2670 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002671 // This releases the ref on the mock profile.
2672 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002673}
2674
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002675TEST_F(ManagerTest, SaveSuccessfulService) {
2676 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002677 new StrictMock<MockProfile>(
2678 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002679 AdoptProfile(manager(), profile);
2680 scoped_refptr<MockService> service(
2681 new NiceMock<MockService>(control_interface(),
2682 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002683 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002684 manager()));
2685
2686 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2687 ServiceRefPtr expect_service(service.get());
2688
2689 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2690 .WillOnce(Return(false));
2691 manager()->RegisterService(service);
2692
2693 EXPECT_CALL(*service.get(), state())
2694 .WillRepeatedly(Return(Service::kStateConnected));
2695 EXPECT_CALL(*service.get(), IsConnected())
2696 .WillRepeatedly(Return(true));
2697 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2698 .WillOnce(Return(true));
2699 manager()->UpdateService(service);
2700}
2701
Darin Petkove7c6ad32012-06-29 10:22:09 +02002702TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002703 MockProfile *profile0 =
2704 new MockProfile(control_interface(), metrics(), manager(), "");
2705 MockProfile *profile1 =
2706 new MockProfile(control_interface(), metrics(), manager(), "");
2707 MockProfile *profile2 =
2708 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002709 AdoptProfile(manager(), profile0); // Passes ownership.
2710 AdoptProfile(manager(), profile1); // Passes ownership.
2711 AdoptProfile(manager(), profile2); // Passes ownership.
2712 DeviceRefPtr device_ref(mock_devices_[0].get());
2713 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2714 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2715 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2716 manager()->UpdateDevice(mock_devices_[0]);
2717}
2718
Paul Stewart1b253142012-01-26 14:05:52 -08002719TEST_F(ManagerTest, EnumerateProfiles) {
2720 vector<string> profile_paths;
2721 for (size_t i = 0; i < 10; i++) {
2722 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002723 new StrictMock<MockProfile>(
2724 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002725 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002726 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2727 .WillOnce(Return(profile_paths.back()));
2728 AdoptProfile(manager(), profile);
2729 }
2730
2731 Error error;
2732 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2733 EXPECT_TRUE(error.IsSuccess());
2734 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2735 for (size_t i = 0; i < profile_paths.size(); i++) {
2736 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2737 }
2738}
2739
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002740TEST_F(ManagerTest, AutoConnectOnRegister) {
2741 MockServiceRefPtr service = MakeAutoConnectableService();
2742 EXPECT_CALL(*service.get(), AutoConnect());
2743 manager()->RegisterService(service);
2744 dispatcher()->DispatchPendingEvents();
2745}
2746
2747TEST_F(ManagerTest, AutoConnectOnUpdate) {
2748 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002749 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002750 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002751 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002752 manager()->RegisterService(service1);
2753 manager()->RegisterService(service2);
2754 dispatcher()->DispatchPendingEvents();
2755
2756 EXPECT_CALL(*service1.get(), AutoConnect());
2757 EXPECT_CALL(*service2.get(), state())
2758 .WillRepeatedly(Return(Service::kStateFailure));
2759 EXPECT_CALL(*service2.get(), IsFailed())
2760 .WillRepeatedly(Return(true));
2761 EXPECT_CALL(*service2.get(), IsConnected())
2762 .WillRepeatedly(Return(false));
2763 manager()->UpdateService(service2);
2764 dispatcher()->DispatchPendingEvents();
2765}
2766
2767TEST_F(ManagerTest, AutoConnectOnDeregister) {
2768 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002769 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002770 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002771 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002772 manager()->RegisterService(service1);
2773 manager()->RegisterService(service2);
2774 dispatcher()->DispatchPendingEvents();
2775
2776 EXPECT_CALL(*service1.get(), AutoConnect());
2777 manager()->DeregisterService(service2);
2778 dispatcher()->DispatchPendingEvents();
2779}
2780
Darin Petkov3ec55342012-09-28 14:04:44 +02002781TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2782 MockServiceRefPtr service = MakeAutoConnectableService();
2783 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2784 SetPowerManager();
2785 EXPECT_CALL(*service, AutoConnect()).Times(0);
2786 manager()->RegisterService(service);
2787 dispatcher()->DispatchPendingEvents();
2788}
2789
Darin Petkovca621542012-07-25 14:25:56 +02002790TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2791 MockServiceRefPtr service = MakeAutoConnectableService();
2792 SetPowerState(PowerManagerProxyDelegate::kMem);
2793 SetPowerManager();
2794 EXPECT_CALL(*service, AutoConnect()).Times(0);
2795 manager()->RegisterService(service);
2796 dispatcher()->DispatchPendingEvents();
2797}
2798
2799TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2800 MockServiceRefPtr service = MakeAutoConnectableService();
2801 SetPowerState(PowerManagerProxyDelegate::kOn);
2802 SetPowerManager();
2803 EXPECT_CALL(*service, AutoConnect());
2804 manager()->RegisterService(service);
2805 dispatcher()->DispatchPendingEvents();
2806}
2807
2808TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2809 MockServiceRefPtr service = MakeAutoConnectableService();
2810 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2811 SetPowerManager();
2812 EXPECT_CALL(*service, AutoConnect());
2813 manager()->RegisterService(service);
2814 dispatcher()->DispatchPendingEvents();
2815}
2816
Paul Stewart63864b62012-11-07 15:10:55 -08002817TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2818 SetRunning(false);
2819 MockServiceRefPtr service = MakeAutoConnectableService();
2820 EXPECT_CALL(*service, AutoConnect()).Times(0);
2821 manager()->RegisterService(service);
2822 dispatcher()->DispatchPendingEvents();
2823}
2824
Darin Petkovca621542012-07-25 14:25:56 +02002825TEST_F(ManagerTest, OnPowerStateChanged) {
2826 MockServiceRefPtr service = MakeAutoConnectableService();
2827 SetPowerState(PowerManagerProxyDelegate::kOn);
2828 SetPowerManager();
2829 EXPECT_CALL(*service, AutoConnect());
2830 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002831 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002832 dispatcher()->DispatchPendingEvents();
2833
mukesh agrawal784566d2012-08-08 18:32:58 -07002834 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002835 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2836 EXPECT_CALL(*service, AutoConnect());
2837 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002838 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002839
mukesh agrawal784566d2012-08-08 18:32:58 -07002840 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002841 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2842 EXPECT_CALL(*service, AutoConnect()).Times(0);
2843 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002844 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002845}
2846
Darin Petkov3ec55342012-09-28 14:04:44 +02002847TEST_F(ManagerTest, AddTerminationAction) {
2848 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
Daniel Eratf9753672013-01-24 10:17:02 -08002849 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02002850 SetPowerManager();
2851 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2852 manager()->AddTerminationAction("action1", base::Closure());
2853 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2854 manager()->AddTerminationAction("action2", base::Closure());
2855}
2856
2857TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002858 const char kKey1[] = "action1";
2859 const char kKey2[] = "action2";
2860 const int kSuspendDelayId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002861
2862 MockPowerManager &power_manager = *power_manager_;
2863 SetPowerManager();
2864
2865 // Removing an action when the hook table is empty should not result in any
2866 // calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002867 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002868 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2869 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2870 manager()->RemoveTerminationAction("unknown");
2871 Mock::VerifyAndClearExpectations(&power_manager);
2872
Daniel Eratf9753672013-01-24 10:17:02 -08002873 EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _))
2874 .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true)));
Daniel Erat0818cca2012-12-14 10:16:21 -08002875 EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1);
Darin Petkov3ec55342012-09-28 14:04:44 +02002876 manager()->AddTerminationAction(kKey1, base::Closure());
2877 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2878 manager()->AddTerminationAction(kKey2, base::Closure());
2879
2880 // Removing an action that ends up with a non-empty hook table should not
2881 // result in any calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002882 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002883 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2884 manager()->RemoveTerminationAction(kKey1);
2885 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2886 Mock::VerifyAndClearExpectations(&power_manager);
2887
2888 // Removing the last action should trigger unregistering from the power
2889 // manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002890 EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId))
2891 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02002892 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2893 manager()->RemoveTerminationAction(kKey2);
2894 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2895}
2896
2897TEST_F(ManagerTest, RunTerminationActions) {
2898 TerminationActionTest test_action;
2899 const string kActionName = "action";
2900
2901 EXPECT_CALL(test_action, Done(_));
2902 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2903 test_action.AsWeakPtr()));
2904
2905 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2906 Bind(&TerminationActionTest::Action,
2907 test_action.AsWeakPtr()));
2908 test_action.set_manager(manager());
2909 EXPECT_CALL(test_action, Done(_));
2910 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2911 test_action.AsWeakPtr()));
2912}
2913
Daniel Erat0818cca2012-12-14 10:16:21 -08002914TEST_F(ManagerTest, OnSuspendImminent) {
2915 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002916 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Erat0818cca2012-12-14 10:16:21 -08002917 EXPECT_CALL(*power_manager_,
2918 ReportSuspendReadiness(
2919 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002920 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002921 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02002922}
2923
2924TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002925 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02002926 Error error;
Daniel Erat0818cca2012-12-14 10:16:21 -08002927 EXPECT_CALL(*power_manager_,
2928 ReportSuspendReadiness(
2929 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002930 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002931 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02002932}
2933
Paul Stewartc681fa02012-03-02 19:40:04 -08002934TEST_F(ManagerTest, RecheckPortal) {
2935 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2936 .WillOnce(Return(false));
2937 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2938 .WillOnce(Return(true));
2939 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2940 .Times(0);
2941
2942 manager()->RegisterDevice(mock_devices_[0]);
2943 manager()->RegisterDevice(mock_devices_[1]);
2944 manager()->RegisterDevice(mock_devices_[2]);
2945
2946 manager()->RecheckPortal(NULL);
2947}
2948
Paul Stewartd215af62012-04-24 23:25:50 -07002949TEST_F(ManagerTest, RecheckPortalOnService) {
2950 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2951 dispatcher(),
2952 metrics(),
2953 manager());
2954 EXPECT_CALL(*mock_devices_[0].get(),
2955 IsConnectedToService(IsRefPtrTo(service)))
2956 .WillOnce(Return(false));
2957 EXPECT_CALL(*mock_devices_[1].get(),
2958 IsConnectedToService(IsRefPtrTo(service)))
2959 .WillOnce(Return(true));
2960 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2961 .WillOnce(Return(true));
2962 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2963 .Times(0);
2964
2965 manager()->RegisterDevice(mock_devices_[0]);
2966 manager()->RegisterDevice(mock_devices_[1]);
2967 manager()->RegisterDevice(mock_devices_[2]);
2968
2969 manager()->RecheckPortalOnService(service);
2970}
2971
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002972TEST_F(ManagerTest, GetDefaultService) {
2973 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002974 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002975
2976 scoped_refptr<MockService> mock_service(
2977 new NiceMock<MockService>(control_interface(),
2978 dispatcher(),
2979 metrics(),
2980 manager()));
2981
2982 manager()->RegisterService(mock_service);
2983 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002984 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002985
2986 scoped_refptr<MockConnection> mock_connection(
2987 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002988 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002989 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002990 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002991
Paul Stewartce4ec192012-03-14 12:53:46 -07002992 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002993 manager()->DeregisterService(mock_service);
2994}
2995
Paul Stewart13ed2252012-03-21 12:52:46 -07002996TEST_F(ManagerTest, GetServiceWithGUID) {
2997 scoped_refptr<MockService> mock_service0(
2998 new NiceMock<MockService>(control_interface(),
2999 dispatcher(),
3000 metrics(),
3001 manager()));
3002
3003 scoped_refptr<MockService> mock_service1(
3004 new NiceMock<MockService>(control_interface(),
3005 dispatcher(),
3006 metrics(),
3007 manager()));
3008
Paul Stewartcb59fed2012-03-21 21:14:46 -07003009 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3010 .Times(0);
3011 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3012 .Times(0);
3013
Paul Stewart13ed2252012-03-21 12:52:46 -07003014 manager()->RegisterService(mock_service0);
3015 manager()->RegisterService(mock_service1);
3016
3017 const string kGUID0 = "GUID0";
3018 const string kGUID1 = "GUID1";
3019
3020 {
3021 Error error;
3022 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3023 EXPECT_FALSE(error.IsSuccess());
3024 EXPECT_FALSE(service);
3025 }
3026
3027 KeyValueStore args;
3028 args.SetString(flimflam::kGuidProperty, kGUID1);
3029
3030 {
3031 Error error;
3032 ServiceRefPtr service = manager()->GetService(args, &error);
3033 EXPECT_EQ(Error::kInvalidArguments, error.type());
3034 EXPECT_FALSE(service);
3035 }
3036
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003037 mock_service0->SetGuid(kGUID0, NULL);
3038 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003039
3040 {
3041 Error error;
3042 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3043 EXPECT_TRUE(error.IsSuccess());
3044 EXPECT_EQ(mock_service0.get(), service.get());
3045 }
3046
3047 {
3048 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003049 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3050 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003051 ServiceRefPtr service = manager()->GetService(args, &error);
3052 EXPECT_TRUE(error.IsSuccess());
3053 EXPECT_EQ(mock_service1.get(), service.get());
3054 }
3055
3056 manager()->DeregisterService(mock_service0);
3057 manager()->DeregisterService(mock_service1);
3058}
3059
Gary Morain028545d2012-04-07 14:55:52 -07003060
3061TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003062 EXPECT_FALSE(manager()->IsOnline());
3063 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3064
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003065 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003066 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003067 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3068 .Times(AnyNumber());
3069 scoped_refptr<MockService> mock_service0(
3070 new NiceMock<MockService>(control_interface(),
3071 dispatcher(),
3072 metrics(),
3073 manager()));
3074
3075 scoped_refptr<MockService> mock_service1(
3076 new NiceMock<MockService>(control_interface(),
3077 dispatcher(),
3078 metrics(),
3079 manager()));
3080
3081 EXPECT_CALL(*mock_service0.get(), IsConnected())
3082 .WillRepeatedly(Return(false));
3083 EXPECT_CALL(*mock_service1.get(), IsConnected())
3084 .WillRepeatedly(Return(false));
3085
3086 manager()->RegisterService(mock_service0);
3087 manager()->RegisterService(mock_service1);
3088
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003089 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003090 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3091
3092 manager()->DeregisterService(mock_service0);
3093 manager()->DeregisterService(mock_service1);
3094}
3095
3096TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003097 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003098 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003099 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3100 .Times(AnyNumber());
3101 scoped_refptr<MockService> mock_service0(
3102 new NiceMock<MockService>(control_interface(),
3103 dispatcher(),
3104 metrics(),
3105 manager()));
3106
3107 scoped_refptr<MockService> mock_service1(
3108 new NiceMock<MockService>(control_interface(),
3109 dispatcher(),
3110 metrics(),
3111 manager()));
3112
3113 EXPECT_CALL(*mock_service0.get(), IsConnected())
3114 .WillRepeatedly(Return(false));
3115 EXPECT_CALL(*mock_service1.get(), IsConnected())
3116 .WillRepeatedly(Return(true));
3117 EXPECT_CALL(*mock_service0.get(), state())
3118 .WillRepeatedly(Return(Service::kStateIdle));
3119 EXPECT_CALL(*mock_service1.get(), state())
3120 .WillRepeatedly(Return(Service::kStateConnected));
3121
3122 manager()->RegisterService(mock_service0);
3123 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003124 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003125
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003126 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003127 EXPECT_EQ("online", manager()->CalculateState(NULL));
3128
3129 manager()->DeregisterService(mock_service0);
3130 manager()->DeregisterService(mock_service1);
3131}
3132
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003133TEST_F(ManagerTest, StartupPortalList) {
3134 // Simulate loading value from the default profile.
3135 const string kProfileValue("wifi,vpn");
3136 manager()->props_.check_portal_list = kProfileValue;
3137
3138 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3139 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3140 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3141
3142 const string kStartupValue("cellular,ethernet");
3143 manager()->SetStartupPortalList(kStartupValue);
3144 // Ensure profile value is not overwritten, so when we save the default
3145 // profile, the correct value will still be written.
3146 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3147
3148 // However we should read back a different list.
3149 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3150 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3151 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3152
3153 const string kRuntimeValue("ppp");
3154 // Setting a runtime value over the control API should overwrite both
3155 // the profile value and what we read back.
3156 Error error;
3157 manager()->mutable_store()->SetStringProperty(
3158 flimflam::kCheckPortalListProperty,
3159 kRuntimeValue,
3160 &error);
3161 ASSERT_TRUE(error.IsSuccess());
3162 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3163 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3164 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3165 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3166}
3167
Paul Stewart036dba02012-08-07 12:34:41 -07003168TEST_F(ManagerTest, LinkMonitorEnabled) {
3169 const string kEnabledTechnologies("wifi,vpn");
3170 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3171 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3172 EXPECT_FALSE(
3173 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3174}
3175
Paul Stewart85aea152013-01-22 09:31:56 -08003176TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003177 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003178 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003179 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003180 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003181 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003182 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3183 AdoptProfile(manager(), profile);
3184 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3185 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3186 scoped_ptr<MockStore> store1(new MockStore);
3187 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3188}
3189
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003190TEST_F(ManagerTest, EnableTechnology) {
3191 Error error(Error::kOperationInitiated);
3192 ResultCallback callback;
3193 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3194 EXPECT_TRUE(error.IsSuccess());
3195
Joshua Krollda798622012-06-05 12:30:48 -07003196 ON_CALL(*mock_devices_[0], technology())
3197 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003198
3199 manager()->RegisterDevice(mock_devices_[0]);
3200
3201 // Device is enabled, so expect operation is successful.
3202 mock_devices_[0]->enabled_ = true;
3203 error.Populate(Error::kOperationInitiated);
3204 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3205 EXPECT_TRUE(error.IsSuccess());
3206
3207 // Device is disabled, so expect operation in progress.
3208 mock_devices_[0]->enabled_ = false;
3209 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3210 error.Populate(Error::kOperationInitiated);
3211 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3212 EXPECT_TRUE(error.IsOngoing());
3213}
3214
3215TEST_F(ManagerTest, DisableTechnology) {
3216 Error error(Error::kOperationInitiated);
3217 ResultCallback callback;
3218 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3219 EXPECT_TRUE(error.IsSuccess());
3220
Joshua Krollda798622012-06-05 12:30:48 -07003221 ON_CALL(*mock_devices_[0], technology())
3222 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003223
3224 manager()->RegisterDevice(mock_devices_[0]);
3225
3226 // Device is disabled, so expect operation is successful.
3227 error.Populate(Error::kOperationInitiated);
3228 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3229 EXPECT_TRUE(error.IsSuccess());
3230
3231 // Device is enabled, so expect operation in progress.
3232 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3233 mock_devices_[0]->enabled_ = true;
3234 error.Populate(Error::kOperationInitiated);
3235 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3236 EXPECT_TRUE(error.IsOngoing());
3237}
3238
Paul Stewart4d5efb72012-09-17 12:24:34 -07003239TEST_F(ManagerTest, IgnoredSearchList) {
3240 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
3241 SetResolver(resolver.get());
3242 vector<string> ignored_paths;
3243 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3244 SetIgnoredDNSSearchPaths("");
3245 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3246
3247 const string kIgnored0 = "chromium.org";
3248 ignored_paths.push_back(kIgnored0);
3249 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3250 SetIgnoredDNSSearchPaths(kIgnored0);
3251 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3252
3253 const string kIgnored1 = "google.com";
3254 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3255 ignored_paths.push_back(kIgnored1);
3256 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3257 SetIgnoredDNSSearchPaths(kIgnoredSum);
3258 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3259
3260 SetResolver(Resolver::GetInstance());
3261}
3262
Paul Stewartbfb82552012-10-24 16:48:48 -07003263TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3264 // Test to make sure that every service state-change causes the
3265 // Manager to emit a new service list.
3266 scoped_refptr<MockService> mock_service(
3267 new NiceMock<MockService>(control_interface(),
3268 dispatcher(),
3269 metrics(),
3270 manager()));
3271 EXPECT_CALL(*mock_service, state())
3272 .WillRepeatedly(Return(Service::kStateIdle));
3273
3274 manager()->RegisterService(mock_service);
3275 EXPECT_CALL(
3276 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3277 flimflam::kServicesProperty, _)).Times(1);
3278 EXPECT_CALL(
3279 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3280 flimflam::kServiceWatchListProperty, _)).Times(1);
3281 CompleteServiceSort();
3282
3283 Mock::VerifyAndClearExpectations(manager_adaptor_);
3284 EXPECT_CALL(
3285 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3286 flimflam::kServicesProperty, _)).Times(1);
3287 EXPECT_CALL(
3288 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3289 flimflam::kServiceWatchListProperty, _)).Times(1);
3290 manager()->UpdateService(mock_service.get());
3291 CompleteServiceSort();
3292
3293 manager()->DeregisterService(mock_service);
3294}
3295
3296TEST_F(ManagerTest, EnumerateServices) {
3297 scoped_refptr<MockService> mock_service(
3298 new NiceMock<MockService>(control_interface(),
3299 dispatcher(),
3300 metrics(),
3301 manager()));
3302 manager()->RegisterService(mock_service);
3303
3304 EXPECT_CALL(*mock_service, state())
3305 .WillRepeatedly(Return(Service::kStateConnected));
3306 EXPECT_CALL(*mock_service, IsVisible())
3307 .WillRepeatedly(Return(false));
3308 EXPECT_TRUE(EnumerateAvailableServices().empty());
3309 EXPECT_TRUE(EnumerateWatchedServices().empty());
3310
3311 EXPECT_CALL(*mock_service, state())
3312 .WillRepeatedly(Return(Service::kStateIdle));
3313 EXPECT_TRUE(EnumerateAvailableServices().empty());
3314 EXPECT_TRUE(EnumerateWatchedServices().empty());
3315
3316 EXPECT_CALL(*mock_service, IsVisible())
3317 .WillRepeatedly(Return(true));
3318 Service::ConnectState unwatched_states[] = {
3319 Service::kStateUnknown,
3320 Service::kStateIdle,
3321 Service::kStateFailure
3322 };
3323 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3324 EXPECT_CALL(*mock_service, state())
3325 .WillRepeatedly(Return(unwatched_states[i]));
3326 EXPECT_FALSE(EnumerateAvailableServices().empty());
3327 EXPECT_TRUE(EnumerateWatchedServices().empty());
3328 }
3329
3330 Service::ConnectState watched_states[] = {
3331 Service::kStateAssociating,
3332 Service::kStateConfiguring,
3333 Service::kStateConnected,
3334 Service::kStateDisconnected,
3335 Service::kStatePortal,
3336 Service::kStateOnline
3337 };
3338 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3339 EXPECT_CALL(*mock_service, state())
3340 .WillRepeatedly(Return(watched_states[i]));
3341 EXPECT_FALSE(EnumerateAvailableServices().empty());
3342 EXPECT_FALSE(EnumerateWatchedServices().empty());
3343 }
3344
3345 manager()->DeregisterService(mock_service);
3346}
3347
Paul Stewart39db5ca2013-03-18 14:15:17 -07003348TEST_F(ManagerTest, ConnectToBestServices) {
3349 scoped_refptr<MockService> wifi_service0(
3350 new NiceMock<MockService>(control_interface(),
3351 dispatcher(),
3352 metrics(),
3353 manager()));
3354 EXPECT_CALL(*wifi_service0.get(), state())
3355 .WillRepeatedly(Return(Service::kStateIdle));
3356 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3357 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003358 wifi_service0->SetConnectable(true);
3359 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003360 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3361 EXPECT_CALL(*wifi_service0.get(), technology())
3362 .WillRepeatedly(Return(Technology::kWifi));
3363
3364 scoped_refptr<MockService> wifi_service1(
3365 new NiceMock<MockService>(control_interface(),
3366 dispatcher(),
3367 metrics(),
3368 manager()));
3369 EXPECT_CALL(*wifi_service1.get(), state())
3370 .WillRepeatedly(Return(Service::kStateIdle));
3371 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3372 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003373 wifi_service1->SetAutoConnect(true);
3374 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003375 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3376 EXPECT_CALL(*wifi_service1.get(), technology())
3377 .WillRepeatedly(Return(Technology::kWifi));
3378
3379 scoped_refptr<MockService> wifi_service2(
3380 new NiceMock<MockService>(control_interface(),
3381 dispatcher(),
3382 metrics(),
3383 manager()));
3384 EXPECT_CALL(*wifi_service2.get(), state())
3385 .WillRepeatedly(Return(Service::kStateConnected));
3386 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3387 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003388 wifi_service2->SetAutoConnect(true);
3389 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003390 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3391 EXPECT_CALL(*wifi_service2.get(), technology())
3392 .WillRepeatedly(Return(Technology::kWifi));
3393
3394 manager()->RegisterService(wifi_service0);
3395 manager()->RegisterService(wifi_service1);
3396 manager()->RegisterService(wifi_service2);
3397
3398 CompleteServiceSort();
3399 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3400
3401 scoped_refptr<MockService> cell_service(
3402 new NiceMock<MockService>(control_interface(),
3403 dispatcher(),
3404 metrics(),
3405 manager()));
3406
3407 EXPECT_CALL(*cell_service.get(), state())
3408 .WillRepeatedly(Return(Service::kStateConnected));
3409 EXPECT_CALL(*cell_service.get(), IsConnected())
3410 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003411 wifi_service2->SetAutoConnect(true);
3412 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003413 EXPECT_CALL(*cell_service.get(), technology())
3414 .WillRepeatedly(Return(Technology::kCellular));
3415 manager()->RegisterService(cell_service);
3416
3417 scoped_refptr<MockService> vpn_service(
3418 new NiceMock<MockService>(control_interface(),
3419 dispatcher(),
3420 metrics(),
3421 manager()));
3422
3423 EXPECT_CALL(*vpn_service.get(), state())
3424 .WillRepeatedly(Return(Service::kStateIdle));
3425 EXPECT_CALL(*vpn_service.get(), IsConnected())
3426 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003427 wifi_service2->SetAutoConnect(false);
3428 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003429 EXPECT_CALL(*vpn_service.get(), technology())
3430 .WillRepeatedly(Return(Technology::kVPN));
3431 manager()->RegisterService(vpn_service);
3432
3433 // The connected services should be at the top.
3434 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3435
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003436 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(1);
3437 EXPECT_CALL(*wifi_service1.get(), Connect(_, _)).Times(0); // Lower prio.
3438 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
3439 EXPECT_CALL(*cell_service.get(), Connect(_, _)).Times(0); // Is connected.
3440 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003441
3442 manager()->ConnectToBestServices(NULL);
3443 dispatcher()->DispatchPendingEvents();
3444
3445 // After this operation, since the Connect calls above are mocked and
3446 // no actual state changes have occurred, we should expect that the
3447 // service sorting order will not have changed.
3448 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3449}
3450
Christopher Wiley1057cd72013-02-28 15:21:29 -08003451TEST_F(ManagerTest, VerifyDestination) {
3452 const string kFakeCertificate("fake cert");
3453 const string kFakePublicKey("fake public key");
3454 const string kFakeNonce("fake public key");
3455 const string kFakeSignedData("fake signed data");
3456 const string kFakeUdn("fake udn");
3457 const char kSSIDStr[] = "fake ssid";
3458 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
3459 const string kFakeData("muffin man");
3460 scoped_refptr<MockWiFiService> mock_destination(
3461 new NiceMock<MockWiFiService>(control_interface(),
3462 dispatcher(),
3463 metrics(),
3464 manager(),
3465 wifi_provider_,
3466 kSSID,
3467 "",
3468 "none",
3469 false));
3470 manager()->RegisterService(mock_destination);
3471 StrictMock<DestinationVerificationTest> dv_test;
3472
3473 // Verify that if we're not connected to anything, verification fails.
3474 {
3475 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3476 kFakePublicKey,
3477 kFakeNonce,
3478 kFakeSignedData,
3479 kFakeUdn,
3480 kSSID,
3481 _,
3482 _,
3483 _))
3484 .Times(0);
3485 Error error(Error::kOperationInitiated);
3486 ResultBoolCallback cb = Bind(
3487 &DestinationVerificationTest::ResultBoolCallbackStub,
3488 dv_test.AsWeakPtr());
3489 manager()->VerifyDestination(kFakeCertificate,
3490 kFakePublicKey,
3491 kFakeNonce,
3492 kFakeSignedData,
3493 kFakeUdn,
3494 cb,
3495 &error);
3496 EXPECT_TRUE(error.IsFailure());
3497 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3498 }
3499
3500 // Making the service look online will let service lookup in
3501 // VerifyDestinatoin succeed.
3502 EXPECT_CALL(*mock_destination.get(), IsConnected())
3503 .WillRepeatedly(Return(true));
3504
3505 // Lead off by verifying that the basic VerifyDestination flow works.
3506 {
3507 ResultBoolCallback passed_down_callback;
3508 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3509 kFakePublicKey,
3510 kFakeNonce,
3511 kFakeSignedData,
3512 kFakeUdn,
3513 kSSID,
3514 _,
3515 _,
3516 _))
3517 .Times(1)
3518 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3519 // Ask the manager to verify the current destination. This should look
3520 // up our previously registered service, and pass some metadata about
3521 // that service down to the CryptoUtilProxy to verify.
3522 Error error(Error::kOperationInitiated);
3523 ResultBoolCallback cb = Bind(
3524 &DestinationVerificationTest::ResultBoolCallbackStub,
3525 dv_test.AsWeakPtr());
3526 manager()->VerifyDestination(kFakeCertificate,
3527 kFakePublicKey,
3528 kFakeNonce,
3529 kFakeSignedData,
3530 kFakeUdn,
3531 cb,
3532 &error);
3533 // We assert here, because if the operation is not ongoing, it is
3534 // inconsistent with shim behavior to call the callback anyway.
3535 ASSERT_TRUE(error.IsOngoing());
3536 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3537 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3538 // Call the callback passed into the CryptoUtilProxy, which
3539 // should find its way into the callback passed into the manager.
3540 // In real code, that callback passed into the manager is from the
3541 // DBus adaptor.
3542 Error e;
3543 passed_down_callback.Run(e, true);
3544 Mock::VerifyAndClearExpectations(&dv_test);
3545 }
3546
3547 // Now for a slightly more complex variant. When we encrypt data,
3548 // we do the same verification step but monkey with the callback to
3549 // link ourselves to an encrypt step afterward.
3550 {
3551 ResultBoolCallback passed_down_callback;
3552 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3553 kFakePublicKey,
3554 kFakeNonce,
3555 kFakeSignedData,
3556 kFakeUdn,
3557 kSSID,
3558 _,
3559 _,
3560 _))
3561 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3562
3563 Error error(Error::kOperationInitiated);
3564 ResultStringCallback cb = Bind(
3565 &DestinationVerificationTest::ResultStringCallbackStub,
3566 dv_test.AsWeakPtr());
3567 manager()->VerifyAndEncryptData(kFakeCertificate,
3568 kFakePublicKey,
3569 kFakeNonce,
3570 kFakeSignedData,
3571 kFakeUdn,
3572 kFakeData,
3573 cb,
3574 &error);
3575 ASSERT_TRUE(error.IsOngoing());
3576 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3577 // Now, if we call that passed down callback, we should see encrypt being
3578 // called.
3579 ResultStringCallback second_passed_down_callback;
3580 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3581 kFakeData,
3582 _,
3583 _))
3584 .Times(1)
3585 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3586 Return(true)));
3587 Error e;
3588 passed_down_callback.Run(e, true);
3589 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3590 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3591 // And if we call the second passed down callback, we should see the
3592 // original function we passed down to VerifyDestination getting called.
3593 e.Reset();
3594 second_passed_down_callback.Run(e, "");
3595 Mock::VerifyAndClearExpectations(&dv_test);
3596 }
3597
3598 // If verification fails on the way to trying to encrypt, we should ditch
3599 // without calling encrypt at all.
3600 {
3601 ResultBoolCallback passed_down_callback;
3602 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3603 kFakePublicKey,
3604 kFakeNonce,
3605 kFakeSignedData,
3606 kFakeUdn,
3607 kSSID,
3608 _,
3609 _,
3610 _))
3611 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3612
3613 Error error(Error::kOperationInitiated);
3614 ResultStringCallback cb = Bind(
3615 &DestinationVerificationTest::ResultStringCallbackStub,
3616 dv_test.AsWeakPtr());
3617 manager()->VerifyAndEncryptData(kFakeCertificate,
3618 kFakePublicKey,
3619 kFakeNonce,
3620 kFakeSignedData,
3621 kFakeUdn,
3622 kFakeData,
3623 cb,
3624 &error);
3625 ASSERT_TRUE(error.IsOngoing());
3626 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3627 Error e(Error::kOperationFailed);
3628 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3629 // Although we're ditching, this callback is what cleans up the pending
3630 // DBus call.
3631 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3632 passed_down_callback.Run(e, false);
3633 Mock::VerifyAndClearExpectations(&dv_test);
3634 }
3635}
3636
Paul Stewartd2e1c362013-03-03 19:06:07 -08003637TEST_F(ManagerTest, IsProfileBefore) {
3638 scoped_refptr<MockProfile> profile0(
3639 new NiceMock<MockProfile>(
3640 control_interface(), metrics(), manager(), ""));
3641 scoped_refptr<MockProfile> profile1(
3642 new NiceMock<MockProfile>(
3643 control_interface(), metrics(), manager(), ""));
3644
3645 AdoptProfile(manager(), profile0);
3646 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3647 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3648 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3649
3650 // A few abnormal cases, but it's good to track their behavior.
3651 scoped_refptr<MockProfile> profile2(
3652 new NiceMock<MockProfile>(
3653 control_interface(), metrics(), manager(), ""));
3654 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3655 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3656 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3657 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3658}
3659
Chris Masone9be4a9d2011-05-16 15:44:09 -07003660} // namespace shill