blob: e95214796bbea8fcc4cf71f82c17f926643d98f3 [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;
Paul Stewart967eaeb2013-04-25 19:53:07 -070066using ::testing::Ref;
Chris Masone9be4a9d2011-05-16 15:44:09 -070067using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070068using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070069using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080070using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080071using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080072using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070073using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070074
Chris Masone3bd3c8c2011-06-13 08:20:26 -070075class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070076 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070077 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020078 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080079 device_info_(new NiceMock<MockDeviceInfo>(
80 control_interface(),
81 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080082 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080083 reinterpret_cast<Manager*>(NULL))),
Paul Stewart3c504012013-01-17 17:49:58 -080084 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070085 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080086 wifi_provider_(new NiceMock<MockWiFiProvider>()),
87 crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(),
88 glib())) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070089 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
90 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080091 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070092 manager(),
93 "null0",
94 "addr0",
95 0));
96 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
97 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080098 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070099 manager(),
100 "null1",
101 "addr1",
102 1));
103 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
104 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800105 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700106 manager(),
107 "null2",
108 "addr2",
109 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800110 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
111 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800112 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800113 manager(),
114 "null3",
115 "addr3",
116 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700117 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800118 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800119
120 // Replace the manager's adaptor with a quieter one, and one
121 // we can do EXPECT*() against. Passes ownership.
122 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800123
Paul Stewart35eff132013-04-12 12:08:40 -0700124 // Replace the manager's Ethernet EAP provider with our mock.
125 // Passes ownership.
126 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
127
Paul Stewart3c504012013-01-17 17:49:58 -0800128 // Replace the manager's WiFi provider with our mock. Passes
129 // ownership.
130 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800131
132 // Replace the manager's crypto util proxy with our mock. Passes
133 // ownership.
134 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Paul Stewart9dd253e2013-04-22 08:32:59 -0700135
136 // Reset service serial number so service sorting by unique_name()
137 // (and by extension, sorting by order of creation) is predictable.
138 Service::serial_number_ = 10000;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700139 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700140 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700141
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100142 void SetMetrics(Metrics *metrics) {
143 manager()->set_metrics(metrics);
144 }
145
Paul Stewartfdd16072011-09-16 12:41:35 -0700146 bool IsDeviceRegistered(const DeviceRefPtr &device,
147 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700148 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700149 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700150 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700151 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700152 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700153
Paul Stewarta849a3d2011-11-03 05:54:09 -0700154 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
155 manager->profiles_.push_back(profile);
156 }
157
Paul Stewart63864b62012-11-07 15:10:55 -0800158 void SetRunning(bool running) {
159 manager()->running_ = running;
160 }
161
Paul Stewart75225512012-01-26 22:51:33 -0800162 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
163 return manager->ephemeral_profile_;
164 }
165
Paul Stewart307c2502013-03-23 12:32:10 -0700166 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
167 return manager->profiles_;
168 }
169
Chris Masone6515aab2011-10-12 16:19:09 -0700170 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
171 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700172 FilePath final_path(storage_path());
173 final_path = final_path.Append("test.profile");
174 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
175 storage->set_path(final_path);
176 if (!storage->Open())
177 return NULL;
Paul Stewart5ad16062013-02-21 18:10:48 -0800178 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800179 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800180 manager,
181 id,
182 "",
183 false));
184 profile->set_storage(storage.release()); // Passes ownership of "storage".
185 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700186 }
187
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700188 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
189 const string &profile_identifier,
190 const string &service_name) {
191 GLib glib;
192 KeyFileStore store(&glib);
193 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
194 return store.Open() &&
195 store.SetString(service_name, "rather", "irrelevant") &&
196 store.Close();
197 }
198
199 Error::Type TestCreateProfile(Manager *manager, const string &name) {
200 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800201 string path;
202 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700203 return error.type();
204 }
205
206 Error::Type TestPopAnyProfile(Manager *manager) {
207 Error error;
208 manager->PopAnyProfile(&error);
209 return error.type();
210 }
211
Paul Stewart307c2502013-03-23 12:32:10 -0700212 Error::Type TestPopAllUserProfiles(Manager *manager) {
213 Error error;
214 manager->PopAllUserProfiles(&error);
215 return error.type();
216 }
217
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700218 Error::Type TestPopProfile(Manager *manager, const string &name) {
219 Error error;
220 manager->PopProfile(name, &error);
221 return error.type();
222 }
223
224 Error::Type TestPushProfile(Manager *manager, const string &name) {
225 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800226 string path;
227 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700228 return error.type();
229 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000230
Paul Stewartf3eced92013-04-17 12:18:22 -0700231 Error::Type TestInsertUserProfile(Manager *manager,
232 const string &name,
233 const string &user_hash) {
234 Error error;
235 string path;
236 manager->InsertUserProfile(name, user_hash, &path, &error);
237 return error.type();
238 }
239
Paul Stewartd2e1c362013-03-03 19:06:07 -0800240 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
241 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700242 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800243 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800244 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200245 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700246 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800247 return profile;
248 }
249
250 void AddMockProfileToManager(Manager *manager) {
251 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700252 }
253
Paul Stewartdfa46052012-06-26 09:44:14 -0700254 void CompleteServiceSort() {
255 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
256 dispatcher()->DispatchPendingEvents();
257 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
258 }
259
Paul Stewart49739c02012-08-08 17:24:03 -0700260 RpcIdentifier GetDefaultServiceRpcIdentifier() {
261 return manager()->GetDefaultServiceRpcIdentifier(NULL);
262 }
263
Paul Stewart4d5efb72012-09-17 12:24:34 -0700264 void SetResolver(Resolver *resolver) {
265 manager()->resolver_ = resolver;
266 }
267
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700268 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
269 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
270 }
271
272 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
273 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700274 }
275
276 const string &GetIgnoredDNSSearchPaths() {
277 return manager()->props_.ignored_dns_search_paths;
278 }
279
Paul Stewartd2e1c362013-03-03 19:06:07 -0800280 WiFiServiceRefPtr ReleaseTempMockService() {
281 // Take a reference to hold during this function.
282 WiFiServiceRefPtr temp_service = temp_mock_service_;
283 temp_mock_service_ = NULL;
284 return temp_service;
285 }
286
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700287 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000288 typedef scoped_refptr<MockService> MockServiceRefPtr;
289
Darin Petkova5e07ef2012-07-09 14:27:57 +0200290 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
291 public:
292 ServiceWatcher() {}
293 virtual ~ServiceWatcher() {}
294
295 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
296
297 private:
298 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
299 };
300
Darin Petkovca621542012-07-25 14:25:56 +0200301 class TestProxyFactory : public ProxyFactory {
302 public:
303 TestProxyFactory() {}
304
305 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
306 PowerManagerProxyDelegate */*delegate*/) {
307 return NULL;
308 }
309
310 private:
311 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
312 };
313
Darin Petkov3ec55342012-09-28 14:04:44 +0200314 class TerminationActionTest :
315 public base::SupportsWeakPtr<TerminationActionTest> {
316 public:
317 static const char kActionName[];
318
319 TerminationActionTest() : manager_(NULL) {}
320 virtual ~TerminationActionTest() {}
321
322 MOCK_METHOD1(Done, void(const Error &error));
323
324 void Action() {
325 manager_->TerminationActionComplete("action");
326 }
327
328 void set_manager(Manager *manager) { manager_ = manager; }
329
330 private:
331 Manager *manager_;
332 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
333 };
334
Christopher Wiley1057cd72013-02-28 15:21:29 -0800335 class DestinationVerificationTest :
336 public base::SupportsWeakPtr<DestinationVerificationTest> {
337 public:
338 DestinationVerificationTest() {}
339 virtual ~DestinationVerificationTest() {}
340
341 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
342 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
343 const string &value));
344 private:
345 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
346 };
347
Darin Petkovca621542012-07-25 14:25:56 +0200348 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
349 power_manager_->power_state_ = state;
350 }
351
352 void SetPowerManager() {
353 manager()->set_power_manager(power_manager_.release());
354 }
355
Darin Petkov3ec55342012-09-28 14:04:44 +0200356 HookTable *GetTerminationActions() {
357 return &manager()->termination_actions_;
358 }
359
Darin Petkovca621542012-07-25 14:25:56 +0200360 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
361 manager()->OnPowerStateChanged(state);
362 }
363
Daniel Erat0818cca2012-12-14 10:16:21 -0800364 void OnSuspendImminent(int suspend_id) {
365 manager()->OnSuspendImminent(suspend_id);
Darin Petkov3ec55342012-09-28 14:04:44 +0200366 }
367
Daniel Erat0818cca2012-12-14 10:16:21 -0800368 void OnSuspendActionsComplete(int suspend_id, const Error &error) {
369 manager()->OnSuspendActionsComplete(suspend_id, error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200370 }
371
Paul Stewartbfb82552012-10-24 16:48:48 -0700372 vector<string> EnumerateAvailableServices() {
373 return manager()->EnumerateAvailableServices(NULL);
374 }
375
376 vector<string> EnumerateWatchedServices() {
377 return manager()->EnumerateWatchedServices(NULL);
378 }
379
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000380 MockServiceRefPtr MakeAutoConnectableService() {
381 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
382 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800383 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000384 manager());
385 service->MakeFavorite();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700386 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000387 return service;
388 }
389
Paul Stewart35eff132013-04-12 12:08:40 -0700390 void SetEapProviderService(const ServiceRefPtr &service) {
391 ethernet_eap_provider_->set_service(service);
392 }
393
Darin Petkovca621542012-07-25 14:25:56 +0200394 TestProxyFactory proxy_factory_;
395 scoped_ptr<MockPowerManager> power_manager_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700396 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800397 scoped_ptr<MockDeviceInfo> device_info_;
398
Paul Stewartd2e1c362013-03-03 19:06:07 -0800399 // This service is held for the manager, and given ownership in a mock
400 // function. This ensures that when the Manager takes ownership, there
401 // is only one reference left.
402 scoped_refptr<MockWiFiService> temp_mock_service_;
403
Paul Stewart3c504012013-01-17 17:49:58 -0800404 // These pointers are owned by the manager, and only tracked here for
405 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800406 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700407 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800408 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800409 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700410};
411
Darin Petkov3ec55342012-09-28 14:04:44 +0200412const char ManagerTest::TerminationActionTest::kActionName[] = "action";
413
Paul Stewart22aa71b2011-09-16 12:15:11 -0700414bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700415 if (!manager()->sort_services_task_.IsCancelled()) {
416 manager()->SortServicesTask();
417 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700418 return (svc0.get() == manager()->services_[0].get() &&
419 svc1.get() == manager()->services_[1].get());
420}
421
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700422TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700423 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
424 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700425}
426
Chris Masone9be4a9d2011-05-16 15:44:09 -0700427TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700428 ON_CALL(*mock_devices_[0].get(), technology())
429 .WillByDefault(Return(Technology::kEthernet));
430 ON_CALL(*mock_devices_[1].get(), technology())
431 .WillByDefault(Return(Technology::kWifi));
432 ON_CALL(*mock_devices_[2].get(), technology())
433 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700434
Paul Stewart22aa71b2011-09-16 12:15:11 -0700435 manager()->RegisterDevice(mock_devices_[0]);
436 manager()->RegisterDevice(mock_devices_[1]);
437 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700438
Paul Stewart22aa71b2011-09-16 12:15:11 -0700439 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
440 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
441 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700442}
443
Paul Stewarta41e38d2011-11-11 07:47:29 -0800444TEST_F(ManagerTest, DeviceRegistrationAndStart) {
445 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500446 mock_devices_[0]->enabled_persistent_ = true;
447 mock_devices_[1]->enabled_persistent_ = false;
448 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800449 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500450 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800451 .Times(0);
452 manager()->RegisterDevice(mock_devices_[0]);
453 manager()->RegisterDevice(mock_devices_[1]);
454}
455
456TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800457 MockProfile *profile =
458 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800459 DeviceRefPtr device_ref(mock_devices_[0].get());
460 AdoptProfile(manager(), profile); // Passes ownership.
461 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200462 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800463 manager()->RegisterDevice(mock_devices_[0]);
464}
465
Chris Masone9be4a9d2011-05-16 15:44:09 -0700466TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700467 ON_CALL(*mock_devices_[0].get(), technology())
468 .WillByDefault(Return(Technology::kEthernet));
469 ON_CALL(*mock_devices_[1].get(), technology())
470 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700471
Gaurav Shah435de2c2011-11-17 19:01:07 -0800472 manager()->RegisterDevice(mock_devices_[0]);
473 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700474
Paul Stewart22aa71b2011-09-16 12:15:11 -0700475 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
476 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700477
Thieu Le5133b712013-02-19 14:47:21 -0800478 MockProfile *profile =
479 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700480 AdoptProfile(manager(), profile); // Passes ownership.
481
Eric Shienbrood9a245532012-03-07 14:20:39 -0500482 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700483 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800484 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700485 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700486
Eric Shienbrood9a245532012-03-07 14:20:39 -0500487 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700488 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800489 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700490 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700491}
492
493TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700494 // It's much easier and safer to use a real GLib for this test.
495 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700496 Manager manager(control_interface(),
497 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800498 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700499 &glib,
500 run_path(),
501 storage_path(),
502 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700503 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
504 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700505 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700506
Chris Masone9be4a9d2011-05-16 15:44:09 -0700507 scoped_refptr<MockService> mock_service(
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));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700512 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700513 new NiceMock<MockService>(control_interface(),
514 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800515 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700516 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700517
Darin Petkov457728b2013-01-09 09:49:08 +0100518 string service1_name(mock_service->unique_name());
519 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700520
521 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
522 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700523 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700524 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700525 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700526 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700527 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700528
Chris Masone9d779932011-08-25 16:33:41 -0700529 manager.RegisterService(mock_service);
530 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700531
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800532 Error error;
533 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700534 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700535 EXPECT_EQ(2, ids.size());
536 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
537 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700538
Chris Masone9d779932011-08-25 16:33:41 -0700539 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
540 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
541
542 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700543}
544
Chris Masone6515aab2011-10-12 16:19:09 -0700545TEST_F(ManagerTest, RegisterKnownService) {
546 // It's much easier and safer to use a real GLib for this test.
547 GLib glib;
548 Manager manager(control_interface(),
549 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800550 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700551 &glib,
552 run_path(),
553 storage_path(),
554 string());
555 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
556 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700557 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700558 {
559 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
560 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800561 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700562 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700563 ASSERT_TRUE(profile->AdoptService(service1));
564 ASSERT_TRUE(profile->ContainsService(service1));
565 } // Force destruction of service1.
566
567 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
568 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800569 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700570 &manager));
571 manager.RegisterService(service2);
572 EXPECT_EQ(service2->profile().get(), profile.get());
573 manager.Stop();
574}
575
576TEST_F(ManagerTest, RegisterUnknownService) {
577 // It's much easier and safer to use a real GLib for this test.
578 GLib glib;
579 Manager manager(control_interface(),
580 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800581 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700582 &glib,
583 run_path(),
584 storage_path(),
585 string());
586 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
587 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700588 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700589 {
590 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
591 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800592 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700593 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700594 ASSERT_TRUE(profile->AdoptService(service1));
595 ASSERT_TRUE(profile->ContainsService(service1));
596 } // Force destruction of service1.
597 scoped_refptr<MockService> mock_service2(
598 new NiceMock<MockService>(control_interface(),
599 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800600 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700601 &manager));
602 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100603 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700604 manager.RegisterService(mock_service2);
605 EXPECT_NE(mock_service2->profile().get(), profile.get());
606 manager.Stop();
607}
608
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000609TEST_F(ManagerTest, DeregisterUnregisteredService) {
610 // WiFi assumes that it can deregister a service that is not
611 // registered. (E.g. a hidden service can be deregistered when it
612 // loses its last endpoint, and again when WiFi is Stop()-ed.)
613 //
614 // So test that doing so doesn't cause a crash.
615 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
616 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800617 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000618 manager());
619 manager()->DeregisterService(service);
620}
621
Chris Masonea8a2c252011-06-27 22:16:30 -0700622TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700623 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700624 map<string, ::DBus::Variant> props;
625 Error error(Error::kInvalidProperty, "");
626 {
627 ::DBus::Error dbus_error;
628 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700629 manager()->mutable_store()->SetStringProperty(
630 flimflam::kCheckPortalListProperty,
631 expected,
632 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700633 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700634 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
635 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
636 expected);
637 }
638 {
639 ::DBus::Error dbus_error;
640 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700641 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
642 expected,
643 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700644 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700645 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
646 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
647 expected);
648 }
649}
650
Chris Masone3c3f6a12011-07-01 10:01:41 -0700651TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700652 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800653 manager()->RegisterDevice(mock_devices_[0]);
654 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700655 {
656 map<string, ::DBus::Variant> props;
657 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700658 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700659 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700660 vector < ::DBus::Path> devices =
661 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700662 EXPECT_EQ(2, devices.size());
663 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700664}
665
mukesh agrawal2366eed2012-03-20 18:21:50 -0700666TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700667 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700668 map<string, ::DBus::Variant> props;
669 ::DBus::Error dbus_error;
670 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
671 map<string, ::DBus::Variant>::const_iterator prop =
672 props.find(flimflam::kServicesProperty);
673 ASSERT_FALSE(prop == props.end());
674 const ::DBus::Variant &variant = prop->second;
675 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
676}
677
Chris Masone6791a432011-07-12 13:23:19 -0700678TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700679 Manager manager(control_interface(),
680 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800681 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700682 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700683 run_path(),
684 storage_path(),
685 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700686 scoped_refptr<MockService> s2(new MockService(control_interface(),
687 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800688 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700689 &manager));
690 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700691 {
Chris Masone6515aab2011-10-12 16:19:09 -0700692 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700693 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800694 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700695 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700696 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700697 .WillRepeatedly(Return(true));
698 EXPECT_CALL(*storage, Flush())
699 .Times(AnyNumber())
700 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700701 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700702 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700703 }
Chris Masone6515aab2011-10-12 16:19:09 -0700704 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800705 ProfileRefPtr profile(
706 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700707 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700708
Chris Masone6515aab2011-10-12 16:19:09 -0700709 // Now, move the Service |s2| to another profile.
710 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
711 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700712
713 // Force destruction of the original Profile, to ensure that the Service
714 // is kept alive and populated with data.
715 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700716 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700717 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700718}
719
Paul Stewart7f61e522012-03-22 11:13:45 -0700720TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
721 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800722 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700723 const string kProfileName("profile0");
724 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
725 .WillRepeatedly(Return(kProfileName));
726 AdoptProfile(manager(), mock_profile);
727
728 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
729 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
730 EXPECT_EQ(mock_profile.get(), profile.get());
731}
732
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800733TEST_F(ManagerTest, SetProfileForService) {
734 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800735 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800736 string profile_name0("profile0");
737 EXPECT_CALL(*profile0, GetRpcIdentifier())
738 .WillRepeatedly(Return(profile_name0));
739 AdoptProfile(manager(), profile0);
740 scoped_refptr<MockService> service(new MockService(control_interface(),
741 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800742 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800743 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700744 EXPECT_FALSE(manager()->HasService(service));
745 {
746 Error error;
747 EXPECT_CALL(*profile0, AdoptService(_))
748 .WillOnce(Return(true));
749 // Expect that setting the profile of a service that does not already
750 // have one assigned does not cause a crash.
751 manager()->SetProfileForService(service, "profile0", &error);
752 EXPECT_TRUE(error.IsSuccess());
753 }
754
755 // The service should be registered as a side-effect of the profile being
756 // set for this service.
757 EXPECT_TRUE(manager()->HasService(service));
758
759 // Since we have mocked Profile::AdoptServie() above, the service's
760 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800761 service->set_profile(profile0);
762
763 {
764 Error error;
765 manager()->SetProfileForService(service, "foo", &error);
766 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700767 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800768 }
769
770 {
771 Error error;
772 manager()->SetProfileForService(service, profile_name0, &error);
773 EXPECT_EQ(Error::kInvalidArguments, error.type());
774 EXPECT_EQ("Service is already connected to this profile", error.message());
775 }
776
777 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800778 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800779 string profile_name1("profile1");
780 EXPECT_CALL(*profile1, GetRpcIdentifier())
781 .WillRepeatedly(Return(profile_name1));
782 AdoptProfile(manager(), profile1);
783
784 {
785 Error error;
786 EXPECT_CALL(*profile1, AdoptService(_))
787 .WillOnce(Return(true));
788 EXPECT_CALL(*profile0, AbandonService(_))
789 .WillOnce(Return(true));
790 manager()->SetProfileForService(service, profile_name1, &error);
791 EXPECT_TRUE(error.IsSuccess());
792 }
793}
794
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700795TEST_F(ManagerTest, CreateProfile) {
796 // It's much easier to use real Glib here since we want the storage
797 // side-effects.
798 GLib glib;
799 ScopedTempDir temp_dir;
800 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
801
802 Manager manager(control_interface(),
803 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800804 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700805 &glib,
806 run_path(),
807 storage_path(),
808 temp_dir.path().value());
809
810 // Invalid name should be rejected.
811 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
812
Paul Stewartd0a3b812012-03-28 22:48:22 -0700813 // A profile with invalid characters in it should similarly be rejected.
814 EXPECT_EQ(Error::kInvalidArguments,
815 TestCreateProfile(&manager, "valid_profile"));
816
817 // We should be able to create a machine profile.
818 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700819
Gary Morainb672d352012-04-25 09:19:06 -0700820 // We should succeed in creating a valid user profile. Verify the returned
821 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700822 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700823 {
824 Error error;
825 string path;
826 manager.CreateProfile(kProfile, &path, &error);
827 EXPECT_EQ(Error::kSuccess, error.type());
828 EXPECT_EQ("/profile_rpc", path);
829 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700830
831 // We should fail in creating it a second time (already exists).
832 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
833}
834
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700835// We receive PopProfile when a user logs out, and it should always trigger a
836// MemoryLog Clear() call.
837TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
838 GLib glib;
839 ScopedTempDir temp_dir;
840 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
841 Manager manager(control_interface(),
842 dispatcher(),
843 metrics(),
844 &glib,
845 run_path(),
846 storage_path(),
847 temp_dir.path().value());
848 const char kProfile0[] = "~user/profile0";
849 const char kPurgedMessage[] = "This message should be purged";
850 // Create a profile and push it on the stack, leave one uncreated
851 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
852 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
853
854 // Popping a profile which isn't on top should still clear the log.
855 LOG(INFO) << kPurgedMessage;
856 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
857 kPurgedMessage));
858 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
859 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
860 kPurgedMessage));
861
862 // Popping an invalid profile name should do the same thing.
863 LOG(INFO) << kPurgedMessage;
864 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
865 kPurgedMessage));
866 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
867 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
868 kPurgedMessage));
869
870 // Successful pops also purge the message log.
871 LOG(INFO) << kPurgedMessage;
872 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
873 kPurgedMessage));
874 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
875 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
876 kPurgedMessage));
877}
878
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700879TEST_F(ManagerTest, PushPopProfile) {
880 // It's much easier to use real Glib in creating a Manager for this
881 // test here since we want the storage side-effects.
882 GLib glib;
883 ScopedTempDir temp_dir;
884 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
885 Manager manager(control_interface(),
886 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800887 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700888 &glib,
889 run_path(),
890 storage_path(),
891 temp_dir.path().value());
892
893 // Pushing an invalid profile should fail.
894 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
895
Paul Stewartd0a3b812012-03-28 22:48:22 -0700896 // Pushing a default profile that does not exist should fail.
897 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700898
899 const char kProfile0[] = "~user/profile0";
900 const char kProfile1[] = "~user/profile1";
901
902 // Create a couple of profiles.
903 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
904 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
905
906 // Push these profiles on the stack.
907 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
908 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
909
910 // Pushing a profile a second time should fail.
911 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
912 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
913
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800914 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700915 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800916 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700917
918 // Make sure a profile name that doesn't exist fails.
919 const char kProfile2Id[] = "profile2";
920 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
921 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
922
923 // Create a new service, with a specific storage name.
924 scoped_refptr<MockService> service(
925 new NiceMock<MockService>(control_interface(),
926 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800927 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700928 &manager));
929 const char kServiceName[] = "service_storage_name";
930 EXPECT_CALL(*service.get(), GetStorageIdentifier())
931 .WillRepeatedly(Return(kServiceName));
932 EXPECT_CALL(*service.get(), Load(_))
933 .WillRepeatedly(Return(true));
934
935 // Add this service to the manager -- it should end up in the ephemeral
936 // profile.
937 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800938 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700939
940 // Create storage for a profile that contains the service storage name.
941 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
942 kServiceName));
943
944 // When we push the profile, the service should move away from the
945 // ephemeral profile to this new profile since it has an entry for
946 // this service.
947 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800948 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700949 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
950
951 // Insert another profile that should supersede ownership of the service.
952 const char kProfile3Id[] = "profile3";
953 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
954 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
955 kServiceName));
956 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
957 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
958
959 // Popping an invalid profile name should fail.
960 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
961
962 // Popping an profile that is not at the top of the stack should fail.
963 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
964
965 // Popping the top profile should succeed.
966 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
967
968 // Moreover the service should have switched profiles to profile 2.
969 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
970
971 // Popping the top profile should succeed.
972 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
973
974 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800975 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700976
977 // Pop the remaining two services off the stack.
978 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
979 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
980
981 // Next pop should fail with "stack is empty".
982 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700983
984 const char kMachineProfile0[] = "machineprofile0";
985 const char kMachineProfile1[] = "machineprofile1";
986 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
987 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
988
989 // Should be able to push a machine profile.
990 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
991
992 // Should be able to push a user profile atop a machine profile.
993 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
994
995 // Pushing a system-wide profile on top of a user profile should fail.
996 EXPECT_EQ(Error::kInvalidArguments,
997 TestPushProfile(&manager, kMachineProfile1));
998
999 // However if we pop the user profile, we should be able stack another
1000 // machine profile on.
1001 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1002 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001003
1004 // Add two user profiles to the top of the stack.
1005 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1006 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
1007 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
1008 EXPECT_EQ(4, profiles.size());
1009
1010 // PopAllUserProfiles should remove both user profiles, leaving the two
1011 // machine profiles.
1012 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1013 EXPECT_EQ(2, profiles.size());
1014 EXPECT_TRUE(profiles[0]->GetUser().empty());
1015 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001016
1017 // Use InsertUserProfile() instead. Although a machine profile is valid
1018 // in this state, it cannot be added via InsertUserProfile.
1019 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1020 EXPECT_EQ(Error::kInvalidArguments,
1021 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1022 const char kUserHash0[] = "userhash0";
1023 const char kUserHash1[] = "userhash1";
1024 EXPECT_EQ(Error::kSuccess,
1025 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1026 EXPECT_EQ(Error::kSuccess,
1027 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1028 EXPECT_EQ(3, profiles.size());
1029 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1030 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001031}
1032
Paul Stewarte73d05c2012-03-29 16:26:05 -07001033TEST_F(ManagerTest, RemoveProfile) {
1034 // It's much easier to use real Glib in creating a Manager for this
1035 // test here since we want the storage side-effects.
1036 GLib glib;
1037 ScopedTempDir temp_dir;
1038 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1039 Manager manager(control_interface(),
1040 dispatcher(),
1041 metrics(),
1042 &glib,
1043 run_path(),
1044 storage_path(),
1045 temp_dir.path().value());
1046
1047 const char kProfile0[] = "profile0";
1048 FilePath profile_path(
1049 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1050
1051 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1052 ASSERT_TRUE(file_util::PathExists(profile_path));
1053
1054 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1055
1056 // Remove should fail since the profile is still on the stack.
1057 {
1058 Error error;
1059 manager.RemoveProfile(kProfile0, &error);
1060 EXPECT_EQ(Error::kInvalidArguments, error.type());
1061 }
1062
1063 // Profile path should still exist.
1064 EXPECT_TRUE(file_util::PathExists(profile_path));
1065
1066 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1067
1068 // This should succeed now that the profile is off the stack.
1069 {
1070 Error error;
1071 manager.RemoveProfile(kProfile0, &error);
1072 EXPECT_EQ(Error::kSuccess, error.type());
1073 }
1074
1075 // Profile path should no longer exist.
1076 EXPECT_FALSE(file_util::PathExists(profile_path));
1077
1078 // Another remove succeeds, due to a foible in file_util::Delete --
1079 // it is not an error to delete a file that does not exist.
1080 {
1081 Error error;
1082 manager.RemoveProfile(kProfile0, &error);
1083 EXPECT_EQ(Error::kSuccess, error.type());
1084 }
1085
1086 // Let's create an error case that will "work". Create a non-empty
1087 // directory in the place of the profile pathname.
1088 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
1089 {
1090 Error error;
1091 manager.RemoveProfile(kProfile0, &error);
1092 EXPECT_EQ(Error::kOperationFailed, error.type());
1093 }
1094}
1095
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001096TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1097 // It's much easier to use real Glib in creating a Manager for this
1098 // test here since we want the storage side-effects.
1099 GLib glib;
1100 ScopedTempDir temp_dir;
1101 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1102 Manager manager(control_interface(),
1103 dispatcher(),
1104 metrics(),
1105 &glib,
1106 run_path(),
1107 storage_path(),
1108 temp_dir.path().value());
1109
1110 const char kProfile0[] = "profile0";
1111 FilePath profile_path(
1112 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1113
1114 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
1115 ASSERT_TRUE(file_util::PathExists(profile_path));
1116 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1117
1118 // Ensure that even if the backing filestore is removed, we still can't
1119 // create a profile twice.
1120 ASSERT_TRUE(file_util::Delete(profile_path, false));
1121 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1122}
1123
Paul Stewart75225512012-01-26 22:51:33 -08001124// Use this matcher instead of passing RefPtrs directly into the arguments
1125// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1126// system teardown.
1127MATCHER_P(IsRefPtrTo, ref_address, "") {
1128 return arg.get() == ref_address;
1129}
1130
1131TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1132 MockServiceRefPtr s_not_in_profile(
1133 new NiceMock<MockService>(control_interface(),
1134 dispatcher(),
1135 metrics(),
1136 manager()));
1137 MockServiceRefPtr s_not_in_group(
1138 new NiceMock<MockService>(control_interface(),
1139 dispatcher(),
1140 metrics(),
1141 manager()));
1142 MockServiceRefPtr s_configure_fail(
1143 new NiceMock<MockService>(control_interface(),
1144 dispatcher(),
1145 metrics(),
1146 manager()));
1147 MockServiceRefPtr s_configure_succeed(
1148 new NiceMock<MockService>(control_interface(),
1149 dispatcher(),
1150 metrics(),
1151 manager()));
1152
1153 string entry_name("entry_name");
1154 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1155 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1156 .WillRepeatedly(Return("not_entry_name"));
1157 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1158 .WillRepeatedly(Return(entry_name));
1159 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1160 .WillRepeatedly(Return(entry_name));
1161
1162 manager()->RegisterService(s_not_in_profile);
1163 manager()->RegisterService(s_not_in_group);
1164 manager()->RegisterService(s_configure_fail);
1165 manager()->RegisterService(s_configure_succeed);
1166
1167 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001168 new StrictMock<MockProfile>(
1169 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001170 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001171 new StrictMock<MockProfile>(
1172 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001173
1174 s_not_in_group->set_profile(profile1);
1175 s_configure_fail->set_profile(profile1);
1176 s_configure_succeed->set_profile(profile1);
1177
1178 AdoptProfile(manager(), profile0);
1179 AdoptProfile(manager(), profile1);
1180
1181 // No services are a member of this profile.
1182 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1183
1184 // No services that are members of this profile have this entry name.
1185 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1186
1187 // Only services that are members of the profile and group will be abandoned.
1188 EXPECT_CALL(*profile1.get(),
1189 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1190 EXPECT_CALL(*profile1.get(),
1191 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1192 EXPECT_CALL(*profile1.get(),
1193 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1194 .WillOnce(Return(true));
1195 EXPECT_CALL(*profile1.get(),
1196 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1197 .WillOnce(Return(true));
1198
1199 // Never allow services to re-join profile1.
1200 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1201 .WillRepeatedly(Return(false));
1202
1203 // Only allow one of the members of the profile and group to successfully
1204 // join profile0.
1205 EXPECT_CALL(*profile0.get(),
1206 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1207 EXPECT_CALL(*profile0.get(),
1208 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1209 EXPECT_CALL(*profile0.get(),
1210 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1211 .WillOnce(Return(false));
1212 EXPECT_CALL(*profile0.get(),
1213 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1214 .WillOnce(Return(true));
1215
1216 // Expect the failed-to-configure service to have Unload() called on it.
1217 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1218 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1219 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1220 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1221
1222 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1223
1224 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1225 EXPECT_EQ(profile1, s_not_in_group->profile());
1226 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1227
1228 // Since we are using a MockProfile, the profile does not actually change,
1229 // since ConfigureService was not actually called on the service.
1230 EXPECT_EQ(profile1, s_configure_succeed->profile());
1231}
1232
Paul Stewart65512e12012-03-26 18:01:08 -07001233TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1234 MockServiceRefPtr s_will_remove0(
1235 new NiceMock<MockService>(control_interface(),
1236 dispatcher(),
1237 metrics(),
1238 manager()));
1239 MockServiceRefPtr s_will_remove1(
1240 new NiceMock<MockService>(control_interface(),
1241 dispatcher(),
1242 metrics(),
1243 manager()));
1244 MockServiceRefPtr s_will_not_remove0(
1245 new NiceMock<MockService>(control_interface(),
1246 dispatcher(),
1247 metrics(),
1248 manager()));
1249 MockServiceRefPtr s_will_not_remove1(
1250 new NiceMock<MockService>(control_interface(),
1251 dispatcher(),
1252 metrics(),
1253 manager()));
1254
1255 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1256 .Times(4); // Once for each registration.
1257
1258 string entry_name("entry_name");
1259 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1260 .WillRepeatedly(Return(entry_name));
1261 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1262 .WillRepeatedly(Return(entry_name));
1263 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1264 .WillRepeatedly(Return(entry_name));
1265 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1266 .WillRepeatedly(Return(entry_name));
1267
1268 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001269 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001270 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001271 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001272 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001273 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001274 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001275 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001276
1277 // One for each service added above.
1278 ASSERT_EQ(4, manager()->services_.size());
1279
1280 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001281 new StrictMock<MockProfile>(
1282 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001283
1284 s_will_remove0->set_profile(profile);
1285 s_will_remove1->set_profile(profile);
1286 s_will_not_remove0->set_profile(profile);
1287 s_will_not_remove1->set_profile(profile);
1288
1289 AdoptProfile(manager(), profile);
1290
1291 // Deny any of the services re-entry to the profile.
1292 EXPECT_CALL(*profile, ConfigureService(_))
1293 .WillRepeatedly(Return(false));
1294
1295 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1296 .WillOnce(Return(true));
1297 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1298 .WillOnce(Return(true));
1299 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1300 .WillOnce(Return(true));
1301 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1302 .WillOnce(Return(true));
1303
1304 EXPECT_CALL(*s_will_remove0, Unload())
1305 .WillOnce(Return(true));
1306 EXPECT_CALL(*s_will_remove1, Unload())
1307 .WillOnce(Return(true));
1308 EXPECT_CALL(*s_will_not_remove0, Unload())
1309 .WillOnce(Return(false));
1310 EXPECT_CALL(*s_will_not_remove1, Unload())
1311 .WillOnce(Return(false));
1312
1313
1314 // This will cause all the profiles to be unloaded.
1315 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1316
1317 // 2 of the 4 services added above should have been unregistered and
1318 // removed, leaving 2.
1319 EXPECT_EQ(2, manager()->services_.size());
1320 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1321 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1322}
1323
1324TEST_F(ManagerTest, PopProfileWithUnload) {
1325 MockServiceRefPtr s_will_remove0(
1326 new NiceMock<MockService>(control_interface(),
1327 dispatcher(),
1328 metrics(),
1329 manager()));
1330 MockServiceRefPtr s_will_remove1(
1331 new NiceMock<MockService>(control_interface(),
1332 dispatcher(),
1333 metrics(),
1334 manager()));
1335 MockServiceRefPtr s_will_not_remove0(
1336 new NiceMock<MockService>(control_interface(),
1337 dispatcher(),
1338 metrics(),
1339 manager()));
1340 MockServiceRefPtr s_will_not_remove1(
1341 new NiceMock<MockService>(control_interface(),
1342 dispatcher(),
1343 metrics(),
1344 manager()));
1345
1346 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1347 .Times(5); // Once for each registration, and one after profile pop.
1348
1349 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001350 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001351 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001352 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001353 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001354 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001355 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001356 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001357
1358 // One for each service added above.
1359 ASSERT_EQ(4, manager()->services_.size());
1360
1361 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001362 new StrictMock<MockProfile>(
1363 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001364 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001365 new StrictMock<MockProfile>(
1366 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001367
1368 s_will_remove0->set_profile(profile1);
1369 s_will_remove1->set_profile(profile1);
1370 s_will_not_remove0->set_profile(profile1);
1371 s_will_not_remove1->set_profile(profile1);
1372
1373 AdoptProfile(manager(), profile0);
1374 AdoptProfile(manager(), profile1);
1375
1376 // Deny any of the services entry to profile0, so they will all be unloaded.
1377 EXPECT_CALL(*profile0, ConfigureService(_))
1378 .WillRepeatedly(Return(false));
1379
1380 EXPECT_CALL(*s_will_remove0, Unload())
1381 .WillOnce(Return(true));
1382 EXPECT_CALL(*s_will_remove1, Unload())
1383 .WillOnce(Return(true));
1384 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001385 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001386 EXPECT_CALL(*s_will_not_remove1, Unload())
1387 .WillOnce(Return(false));
1388
Philipp Neubeck79173602012-11-13 21:10:09 +01001389 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1390 // profile list.
1391 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1392 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1393
Paul Stewart65512e12012-03-26 18:01:08 -07001394 // This will pop profile1, which should cause all our profiles to unload.
1395 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001396 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001397
1398 // 2 of the 4 services added above should have been unregistered and
1399 // removed, leaving 2.
1400 EXPECT_EQ(2, manager()->services_.size());
1401 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1402 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001403
1404 // Expect the unloaded services to lose their profile reference.
1405 EXPECT_FALSE(s_will_remove0->profile());
1406 EXPECT_FALSE(s_will_remove1->profile());
1407
1408 // If we explicitly deregister a service, the effect should be the same
1409 // with respect to the profile reference.
1410 ASSERT_TRUE(s_will_not_remove0->profile());
1411 manager()->DeregisterService(s_will_not_remove0);
1412 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001413}
1414
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001415TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001416 {
1417 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001418 ::DBus::Variant offline_mode;
1419 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001420 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1421 flimflam::kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001422 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001423 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001424 }
1425 {
1426 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001427 ::DBus::Variant country;
1428 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001429 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1430 flimflam::kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001431 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001432 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001433 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001434 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001435 {
1436 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001437 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1438 flimflam::kCountryProperty,
1439 PropertyStoreTest::kBoolV,
1440 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001441 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001442 }
1443 {
1444 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001445 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1446 flimflam::kOfflineModeProperty,
1447 PropertyStoreTest::kStringV,
1448 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001449 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001450 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001451 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001452 {
1453 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001454 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001455 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001456 flimflam::kEnabledTechnologiesProperty,
1457 PropertyStoreTest::kStringsV,
1458 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001459 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001460 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001461}
1462
mukesh agrawal32399322011-09-01 10:53:43 -07001463TEST_F(ManagerTest, RequestScan) {
1464 {
1465 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001466 manager()->RegisterDevice(mock_devices_[0].get());
1467 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001468 EXPECT_CALL(*mock_devices_[0], technology())
1469 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie68d41092013-04-02 12:56:02 -07001470 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _));
Joshua Krollda798622012-06-05 12:30:48 -07001471 EXPECT_CALL(*mock_devices_[1], technology())
1472 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie68d41092013-04-02 12:56:02 -07001473 EXPECT_CALL(*mock_devices_[1], Scan(_, _)).Times(0);
1474 manager()->RequestScan(Device::kFullScan, flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001475 }
1476
1477 {
1478 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001479 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001480 EXPECT_EQ(Error::kInvalidArguments, error.type());
1481 }
1482}
1483
Darin Petkovb65c2452012-02-23 15:17:06 +01001484TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001485 KeyValueStore args;
1486 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001487 manager()->GetService(args, &e);
1488 EXPECT_EQ(Error::kInvalidArguments, e.type());
1489 EXPECT_EQ("must specify service type", e.message());
1490}
1491
1492TEST_F(ManagerTest, GetServiceUnknownType) {
1493 KeyValueStore args;
1494 Error e;
1495 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1496 manager()->GetService(args, &e);
1497 EXPECT_EQ(Error::kNotSupported, e.type());
1498 EXPECT_EQ("service type is unsupported", e.message());
1499}
1500
Paul Stewart35eff132013-04-12 12:08:40 -07001501TEST_F(ManagerTest, GetServiceEthernetEap) {
1502 KeyValueStore args;
1503 Error e;
1504 ServiceRefPtr service;
1505 args.SetString(flimflam::kTypeProperty, kTypeEthernetEap);
1506 SetEapProviderService(service);
1507 EXPECT_EQ(service, manager()->GetService(args, &e));
1508 EXPECT_TRUE(e.IsSuccess());
1509}
1510
Darin Petkovb65c2452012-02-23 15:17:06 +01001511TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001512 KeyValueStore args;
1513 Error e;
1514 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001515 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001516 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001517 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001518 manager()->GetService(args, &e);
1519 EXPECT_TRUE(e.IsSuccess());
1520}
1521
Darin Petkov33af05c2012-02-28 10:10:30 +01001522TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1523 KeyValueStore args;
1524 Error e;
1525 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001526 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001527 new StrictMock<MockProfile>(
1528 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001529 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001530 ServiceRefPtr service = manager()->GetService(args, &e);
1531 EXPECT_EQ(Error::kNotSupported, e.type());
1532 EXPECT_FALSE(service);
1533}
1534
Darin Petkovb65c2452012-02-23 15:17:06 +01001535TEST_F(ManagerTest, GetServiceVPN) {
1536 KeyValueStore args;
1537 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001538 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001539 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001540 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
Darin Petkov4e02ba22013-04-02 13:44:08 +02001541 args.SetString(flimflam::kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001542 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001543 new StrictMock<MockProfile>(
1544 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001545 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001546
1547#if defined(DISABLE_VPN)
1548
1549 ServiceRefPtr service = manager()->GetService(args, &e);
1550 EXPECT_EQ(Error::kNotSupported, e.type());
1551 EXPECT_FALSE(service);
1552
1553#else
1554
Paul Stewart7f5ad572012-06-04 15:18:54 -07001555 ServiceRefPtr updated_service;
1556 EXPECT_CALL(*profile, UpdateService(_))
1557 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1558 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001559 EXPECT_CALL(*profile, LoadService(_))
1560 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001561 EXPECT_CALL(*profile, ConfigureService(_))
1562 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001563 ServiceRefPtr service = manager()->GetService(args, &e);
1564 EXPECT_TRUE(e.IsSuccess());
1565 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001566 EXPECT_EQ(service, updated_service);
1567 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001568
1569#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001570}
1571
Darin Petkovc63dcf02012-05-24 11:51:43 +02001572TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1573 KeyValueStore args;
1574 Error e;
1575 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1576 ServiceRefPtr service = manager()->GetService(args, &e);
1577 EXPECT_EQ(Error::kInvalidArguments, e.type());
1578 EXPECT_EQ("Missing WiMAX network id.", e.message());
1579 EXPECT_FALSE(service);
1580}
1581
Darin Petkovd1cd7972012-05-22 15:26:15 +02001582TEST_F(ManagerTest, GetServiceWiMax) {
1583 KeyValueStore args;
1584 Error e;
1585 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001586 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1587 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1588 ServiceRefPtr service = manager()->GetService(args, &e);
1589 EXPECT_TRUE(e.IsSuccess());
1590 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001591}
1592
Paul Stewart7f61e522012-03-22 11:13:45 -07001593TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1594 // Manager calls ActiveProfile() so we need at least one profile installed.
1595 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001596 new NiceMock<MockProfile>(
1597 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001598 AdoptProfile(manager(), profile);
1599
1600 KeyValueStore args;
1601 args.SetString(flimflam::kProfileProperty, "xxx");
1602 Error error;
1603 manager()->ConfigureService(args, &error);
1604 EXPECT_EQ(Error::kInvalidArguments, error.type());
1605 EXPECT_EQ("Invalid profile name xxx", error.message());
1606}
1607
1608TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1609 // Manager calls ActiveProfile() so we need at least one profile installed.
1610 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001611 new NiceMock<MockProfile>(
1612 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001613 AdoptProfile(manager(), profile);
1614
1615 KeyValueStore args;
1616 Error error;
1617 manager()->ConfigureService(args, &error);
1618 EXPECT_EQ(Error::kInvalidArguments, error.type());
1619 EXPECT_EQ("must specify service type", error.message());
1620}
1621
1622// A registered service in the ephemeral profile should be moved to the
1623// active profile as a part of configuration if no profile was explicitly
1624// specified.
1625TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1626 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001627 new NiceMock<MockProfile>(
1628 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001629
1630 AdoptProfile(manager(), profile); // This is now the active profile.
1631
Paul Stewartd2e1c362013-03-03 19:06:07 -08001632 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001633 scoped_refptr<MockWiFiService> service(
1634 new NiceMock<MockWiFiService>(control_interface(),
1635 dispatcher(),
1636 metrics(),
1637 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001638 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001639 ssid,
1640 "",
1641 "",
1642 false));
1643
1644 manager()->RegisterService(service);
1645 service->set_profile(GetEphemeralProfile(manager()));
1646
Paul Stewart3c504012013-01-17 17:49:58 -08001647 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001648 .WillOnce(Return(service));
1649 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1650 .WillOnce(Return(true));
1651 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1652 .WillOnce(Return(true));
1653
1654 KeyValueStore args;
1655 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1656 Error error;
1657 manager()->ConfigureService(args, &error);
1658 EXPECT_TRUE(error.IsSuccess());
1659}
1660
Paul Stewart2c575d22012-12-07 12:28:57 -08001661// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001662// specify a profile, it should be moved from the profile it was previously
1663// in to the specified profile if one was requested.
1664TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1665 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001666 new NiceMock<MockProfile>(
1667 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001668 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001669 new NiceMock<MockProfile>(
1670 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001671
1672 const string kProfileName0 = "profile0";
1673 const string kProfileName1 = "profile1";
1674
1675 EXPECT_CALL(*profile0, GetRpcIdentifier())
1676 .WillRepeatedly(Return(kProfileName0));
1677 EXPECT_CALL(*profile1, GetRpcIdentifier())
1678 .WillRepeatedly(Return(kProfileName1));
1679
1680 AdoptProfile(manager(), profile0);
1681 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1682
Paul Stewartd2e1c362013-03-03 19:06:07 -08001683 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001684 scoped_refptr<MockWiFiService> service(
1685 new NiceMock<MockWiFiService>(control_interface(),
1686 dispatcher(),
1687 metrics(),
1688 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001689 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001690 ssid,
1691 "",
1692 "",
1693 false));
1694
1695 manager()->RegisterService(service);
1696 service->set_profile(profile1);
1697
Paul Stewart3c504012013-01-17 17:49:58 -08001698 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001699 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001700 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1701 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001702 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1703 .WillOnce(Return(true));
1704 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1705 .WillOnce(Return(true));
1706 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1707 .WillOnce(Return(true));
1708
1709 KeyValueStore args;
1710 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1711 args.SetString(flimflam::kProfileProperty, kProfileName0);
1712 Error error;
1713 manager()->ConfigureService(args, &error);
1714 EXPECT_TRUE(error.IsSuccess());
1715 service->set_profile(NULL); // Breaks refcounting loop.
1716}
1717
Paul Stewart2c575d22012-12-07 12:28:57 -08001718// If we configure a service that is already a member of the specified
1719// profile, the Manager should not call LoadService or AdoptService again
1720// on this service.
1721TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1722 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001723 new NiceMock<MockProfile>(
1724 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001725
1726 const string kProfileName0 = "profile0";
1727
1728 EXPECT_CALL(*profile0, GetRpcIdentifier())
1729 .WillRepeatedly(Return(kProfileName0));
1730
1731 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1732
Paul Stewartd2e1c362013-03-03 19:06:07 -08001733 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001734 scoped_refptr<MockWiFiService> service(
1735 new NiceMock<MockWiFiService>(control_interface(),
1736 dispatcher(),
1737 metrics(),
1738 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001739 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001740 ssid,
1741 "",
1742 "",
1743 false));
1744
1745 manager()->RegisterService(service);
1746 service->set_profile(profile0);
1747
Paul Stewart3c504012013-01-17 17:49:58 -08001748 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001749 .WillOnce(Return(service));
1750 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1751 .Times(0);
1752 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1753 .WillOnce(Return(true));
1754 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1755 .Times(0);
1756
1757 KeyValueStore args;
1758 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1759 args.SetString(flimflam::kProfileProperty, kProfileName0);
1760 Error error;
1761 manager()->ConfigureService(args, &error);
1762 EXPECT_TRUE(error.IsSuccess());
1763 service->set_profile(NULL); // Breaks refcounting loop.
1764}
1765
Paul Stewart7f61e522012-03-22 11:13:45 -07001766// An unregistered service should remain unregistered, but its contents should
1767// be saved to the specified profile nonetheless.
1768TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1769 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001770 new NiceMock<MockProfile>(
1771 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001772 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001773 new NiceMock<MockProfile>(
1774 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001775
1776 const string kProfileName0 = "profile0";
1777 const string kProfileName1 = "profile1";
1778
1779 EXPECT_CALL(*profile0, GetRpcIdentifier())
1780 .WillRepeatedly(Return(kProfileName0));
1781 EXPECT_CALL(*profile1, GetRpcIdentifier())
1782 .WillRepeatedly(Return(kProfileName1));
1783
1784 AdoptProfile(manager(), profile0);
1785 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1786
Paul Stewartd2e1c362013-03-03 19:06:07 -08001787 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001788 scoped_refptr<MockWiFiService> service(
1789 new NiceMock<MockWiFiService>(control_interface(),
1790 dispatcher(),
1791 metrics(),
1792 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001793 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001794 ssid,
1795 "",
1796 "",
1797 false));
1798
1799 service->set_profile(profile1);
1800
Paul Stewart3c504012013-01-17 17:49:58 -08001801 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001802 .WillOnce(Return(service));
1803 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1804 .WillOnce(Return(true));
1805 EXPECT_CALL(*profile0, AdoptService(_))
1806 .Times(0);
1807 EXPECT_CALL(*profile1, AdoptService(_))
1808 .Times(0);
1809
1810 KeyValueStore args;
1811 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1812 args.SetString(flimflam::kProfileProperty, kProfileName0);
1813 Error error;
1814 manager()->ConfigureService(args, &error);
1815 EXPECT_TRUE(error.IsSuccess());
1816}
1817
Paul Stewartd2e1c362013-03-03 19:06:07 -08001818TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1819 KeyValueStore args;
1820 Error error;
1821 ServiceRefPtr service =
1822 manager()->ConfigureServiceForProfile("", args, &error);
1823 EXPECT_EQ(Error::kNotSupported, error.type());
1824 EXPECT_EQ("This method only supports WiFi services", error.message());
1825 EXPECT_EQ(NULL, service.get());
1826}
1827
1828TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1829 KeyValueStore args;
1830 args.SetString(flimflam::kTypeProperty, flimflam::kTypeCellular);
1831 Error error;
1832 ServiceRefPtr service =
1833 manager()->ConfigureServiceForProfile("", args, &error);
1834 EXPECT_EQ(Error::kNotSupported, error.type());
1835 EXPECT_EQ("This method only supports WiFi services", error.message());
1836 EXPECT_EQ(NULL, service.get());
1837}
1838
1839TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1840 KeyValueStore args;
1841 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1842 Error error;
1843 ServiceRefPtr service =
1844 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1845 EXPECT_EQ(Error::kNotFound, error.type());
1846 EXPECT_EQ("Profile specified was not found", error.message());
1847 EXPECT_EQ(NULL, service.get());
1848}
1849
1850TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1851 const string kProfileName0 = "profile0";
1852 const string kProfileName1 = "profile1";
1853 scoped_refptr<MockProfile> profile0(
1854 AddNamedMockProfileToManager(manager(), kProfileName0));
1855
1856 KeyValueStore args;
1857 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1858 args.SetString(flimflam::kProfileProperty, kProfileName1);
1859 Error error;
1860 ServiceRefPtr service =
1861 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1862 EXPECT_EQ(Error::kInvalidArguments, error.type());
1863 EXPECT_EQ("Profile argument does not match that in "
1864 "the configuration arguments", error.message());
1865 EXPECT_EQ(NULL, service.get());
1866}
1867
1868TEST_F(ManagerTest,
1869 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1870 const string kProfileName0 = "profile0";
1871 scoped_refptr<MockProfile> profile0(
1872 AddNamedMockProfileToManager(manager(), kProfileName0));
1873 KeyValueStore args;
1874 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1875 args.SetString(flimflam::kProfileProperty, kProfileName0);
1876
1877 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1878 .WillOnce(Return(WiFiServiceRefPtr()));
1879 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1880 .WillOnce(Return(WiFiServiceRefPtr()));
1881 Error error;
1882 ServiceRefPtr service =
1883 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1884 // Since we didn't set the error in the GetService expectation above...
1885 EXPECT_TRUE(error.IsSuccess());
1886 EXPECT_EQ(NULL, service.get());
1887}
1888
1889TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1890 const string kProfileName0 = "profile0";
1891 scoped_refptr<MockProfile> profile0(
1892 AddNamedMockProfileToManager(manager(), kProfileName0));
1893
1894 KeyValueStore args;
1895 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1896
1897 scoped_refptr<MockWiFiService> mock_service(
1898 new NiceMock<MockWiFiService>(control_interface(),
1899 dispatcher(),
1900 metrics(),
1901 manager(),
1902 wifi_provider_,
1903 vector<uint8_t>(),
1904 flimflam::kModeManaged,
1905 flimflam::kSecurityNone,
1906 false));
1907 ServiceRefPtr mock_service_generic(mock_service.get());
1908 mock_service->set_profile(profile0);
1909 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1910 .WillOnce(Return(WiFiServiceRefPtr()));
1911 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
1912 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
1913 .WillOnce(Return(true));
1914 Error error;
1915 ServiceRefPtr service =
1916 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1917 EXPECT_TRUE(error.IsSuccess());
1918 EXPECT_EQ(mock_service.get(), service.get());
1919 mock_service->set_profile(NULL); // Breaks reference cycle.
1920}
1921
1922TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
1923 scoped_refptr<MockService> mock_service(
1924 new NiceMock<MockService>(control_interface(),
1925 dispatcher(),
1926 metrics(),
1927 manager()));
1928 const string kGUID = "a guid";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001929 mock_service->SetGuid(kGUID, NULL);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001930 manager()->RegisterService(mock_service);
1931 ServiceRefPtr mock_service_generic(mock_service.get());
1932
1933 const string kProfileName = "profile";
1934 scoped_refptr<MockProfile> profile(
1935 AddNamedMockProfileToManager(manager(), kProfileName));
1936 mock_service->set_profile(profile);
1937
1938 EXPECT_CALL(*mock_service, technology())
1939 .WillOnce(Return(Technology::kCellular))
1940 .WillOnce(Return(Technology::kWifi));
1941
1942 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
1943 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1944 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1945
1946 KeyValueStore args;
1947 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1948 args.SetString(flimflam::kGuidProperty, kGUID);
1949
1950 // The first attempt should fail because the service reports a technology
1951 // other than "WiFi".
1952 {
1953 Error error;
1954 ServiceRefPtr service =
1955 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1956 EXPECT_EQ(NULL, service.get());
1957 EXPECT_EQ(Error::kNotSupported, error.type());
1958 EXPECT_EQ("This GUID matches a non-WiFi service", error.message());
1959 }
1960
1961 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
1962 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
1963
1964 {
1965 Error error;
1966 ServiceRefPtr service =
1967 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
1968 EXPECT_TRUE(error.IsSuccess());
1969 EXPECT_EQ(mock_service.get(), service.get());
1970 EXPECT_EQ(profile.get(), service->profile().get());
1971 }
1972 mock_service->set_profile(NULL); // Breaks reference cycle.
1973}
1974
1975TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
1976 const string kProfileName = "profile";
1977 scoped_refptr<MockProfile> profile(
1978 AddNamedMockProfileToManager(manager(), kProfileName));
1979
1980 scoped_refptr<MockWiFiService> mock_service(
1981 new NiceMock<MockWiFiService>(control_interface(),
1982 dispatcher(),
1983 metrics(),
1984 manager(),
1985 wifi_provider_,
1986 vector<uint8_t>(),
1987 flimflam::kModeManaged,
1988 flimflam::kSecurityNone,
1989 false));
1990 mock_service->set_profile(profile);
1991 ServiceRefPtr mock_service_generic(mock_service.get());
1992
1993 KeyValueStore args;
1994 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1995 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1996 .WillOnce(Return(mock_service));
1997 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
1998 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
1999 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2000 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2001
2002 Error error;
2003 ServiceRefPtr service =
2004 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2005 EXPECT_TRUE(error.IsSuccess());
2006 EXPECT_EQ(mock_service.get(), service.get());
2007 EXPECT_EQ(profile.get(), service->profile().get());
2008 mock_service->set_profile(NULL); // Breaks reference cycle.
2009}
2010
2011TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2012 const string kProfileName = "profile";
2013 scoped_refptr<MockProfile> profile(
2014 AddNamedMockProfileToManager(manager(), kProfileName));
2015
2016 scoped_refptr<MockWiFiService> mock_service(
2017 new NiceMock<MockWiFiService>(control_interface(),
2018 dispatcher(),
2019 metrics(),
2020 manager(),
2021 wifi_provider_,
2022 vector<uint8_t>(),
2023 flimflam::kModeManaged,
2024 flimflam::kSecurityNone,
2025 false));
2026 mock_service->set_profile(GetEphemeralProfile(manager()));
2027 ServiceRefPtr mock_service_generic(mock_service.get());
2028
2029 KeyValueStore args;
2030 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2031 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2032 .WillOnce(Return(mock_service));
2033 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2034 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2035 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2036
2037 Error error;
2038 ServiceRefPtr service =
2039 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2040 EXPECT_TRUE(error.IsSuccess());
2041 EXPECT_EQ(mock_service.get(), service.get());
2042 EXPECT_EQ(profile.get(), service->profile().get());
2043 mock_service->set_profile(NULL); // Breaks reference cycle.
2044}
2045
2046TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2047 const string kProfileName0 = "profile0";
2048 scoped_refptr<MockProfile> profile0(
2049 AddNamedMockProfileToManager(manager(), kProfileName0));
2050 const string kProfileName1 = "profile1";
2051 scoped_refptr<MockProfile> profile1(
2052 AddNamedMockProfileToManager(manager(), kProfileName1));
2053
2054 scoped_refptr<MockWiFiService> mock_service(
2055 new NiceMock<MockWiFiService>(control_interface(),
2056 dispatcher(),
2057 metrics(),
2058 manager(),
2059 wifi_provider_,
2060 vector<uint8_t>(),
2061 flimflam::kModeManaged,
2062 flimflam::kSecurityNone,
2063 false));
2064 manager()->RegisterService(mock_service);
2065 mock_service->set_profile(profile0);
2066 ServiceRefPtr mock_service_generic(mock_service.get());
2067
2068 KeyValueStore args;
2069 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2070 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2071 .WillOnce(Return(mock_service));
2072 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2073 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2074 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2075 // This happens once to make the service loadable for the ConfigureService
2076 // below, and a second time after the service is modified.
2077 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2078 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2079 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2080 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2081
2082 Error error;
2083 ServiceRefPtr service =
2084 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2085 EXPECT_TRUE(error.IsSuccess());
2086 EXPECT_EQ(mock_service.get(), service.get());
2087 mock_service->set_profile(NULL); // Breaks reference cycle.
2088}
2089
2090TEST_F(ManagerTest,
2091 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2092 const string kProfileName0 = "profile0";
2093 scoped_refptr<MockProfile> profile0(
2094 AddNamedMockProfileToManager(manager(), kProfileName0));
2095 const string kProfileName1 = "profile1";
2096 scoped_refptr<MockProfile> profile1(
2097 AddNamedMockProfileToManager(manager(), kProfileName1));
2098
2099 scoped_refptr<MockWiFiService> matching_service(
2100 new StrictMock<MockWiFiService>(control_interface(),
2101 dispatcher(),
2102 metrics(),
2103 manager(),
2104 wifi_provider_,
2105 vector<uint8_t>(),
2106 flimflam::kModeManaged,
2107 flimflam::kSecurityNone,
2108 false));
2109 matching_service->set_profile(profile1);
2110
2111 // We need to get rid of our reference to this mock service as soon
2112 // as Manager::ConfigureServiceForProfile() takes a reference in its
2113 // call to WiFiProvider::CreateTemporaryService(). This way the
2114 // latter function can keep a DCHECK(service->HasOneRef() even in
2115 // unit tests.
2116 temp_mock_service_ =
2117 new NiceMock<MockWiFiService>(control_interface(),
2118 dispatcher(),
2119 metrics(),
2120 manager(),
2121 wifi_provider_,
2122 vector<uint8_t>(),
2123 flimflam::kModeManaged,
2124 flimflam::kSecurityNone,
2125 false);
2126
2127 // Only hold a pointer here so we don't affect the refcount.
2128 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2129
2130 KeyValueStore args;
2131 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
2132 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2133 .WillOnce(Return(matching_service));
2134 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2135 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2136 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2137 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2138 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2139 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2140 .Times(1);
2141 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2142 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2143
2144 Error error;
2145 ServiceRefPtr service =
2146 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2147 EXPECT_TRUE(error.IsSuccess());
2148 EXPECT_EQ(NULL, service.get());
2149 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2150}
2151
Paul Stewart7a20aa42013-01-17 12:21:41 -08002152TEST_F(ManagerTest, FindMatchingService) {
2153 KeyValueStore args;
2154 {
2155 Error error;
2156 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2157 EXPECT_EQ(Error::kNotFound, error.type());
2158 }
2159
2160 scoped_refptr<MockService> mock_service0(
2161 new NiceMock<MockService>(control_interface(),
2162 dispatcher(),
2163 metrics(),
2164 manager()));
2165 scoped_refptr<MockService> mock_service1(
2166 new NiceMock<MockService>(control_interface(),
2167 dispatcher(),
2168 metrics(),
2169 manager()));
2170 manager()->RegisterService(mock_service0);
2171 manager()->RegisterService(mock_service1);
2172 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2173 .WillOnce(Return(true))
2174 .WillRepeatedly(Return(false));
2175 {
2176 Error error;
2177 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2178 EXPECT_TRUE(error.IsSuccess());
2179 }
2180 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2181 .WillOnce(Return(true))
2182 .WillRepeatedly(Return(false));
2183 {
2184 Error error;
2185 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2186 EXPECT_TRUE(error.IsSuccess());
2187 }
2188 {
2189 Error error;
2190 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2191 EXPECT_EQ(Error::kNotFound, error.type());
2192 }
2193}
2194
Paul Stewart22aa71b2011-09-16 12:15:11 -07002195TEST_F(ManagerTest, TechnologyOrder) {
2196 Error error;
2197 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
2198 string(flimflam::kTypeWifi), &error);
2199 ASSERT_TRUE(error.IsSuccess());
2200 EXPECT_EQ(manager()->GetTechnologyOrder(),
2201 string(flimflam::kTypeEthernet) + "," +
2202 string(flimflam::kTypeWifi));
2203
2204 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
2205 string(flimflam::kTypeWifi), &error);
2206 ASSERT_FALSE(error.IsSuccess());
2207 EXPECT_EQ(Error::kInvalidArguments, error.type());
2208 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
2209 string(flimflam::kTypeWifi),
2210 manager()->GetTechnologyOrder());
2211}
2212
2213TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00002214 // TODO(quiche): Some of these tests would probably fit better in
2215 // service_unittest, since the actual comparison of Services is
2216 // implemented in Service. (crosbug.com/23370)
2217
Paul Stewart22aa71b2011-09-16 12:15:11 -07002218 scoped_refptr<MockService> mock_service0(
2219 new NiceMock<MockService>(control_interface(),
2220 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002221 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002222 manager()));
2223 scoped_refptr<MockService> mock_service1(
2224 new NiceMock<MockService>(control_interface(),
2225 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002226 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002227 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002228
2229 manager()->RegisterService(mock_service0);
2230 manager()->RegisterService(mock_service1);
2231
Darin Petkov457728b2013-01-09 09:49:08 +01002232 // Services should already be sorted by |unique_name_|
Paul Stewart22aa71b2011-09-16 12:15:11 -07002233 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2234
2235 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07002236 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002237 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2238
2239 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01002240 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002241 manager()->UpdateService(mock_service1);
2242 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2243
2244 // Security
mukesh agrawal43970a22013-02-15 16:00:07 -08002245 mock_service0->SetSecurity(Service::kCryptoAes, true, true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002246 manager()->UpdateService(mock_service0);
2247 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2248
2249 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07002250 EXPECT_CALL(*mock_service0.get(), technology())
2251 .WillRepeatedly(Return((Technology::kWifi)));
2252 EXPECT_CALL(*mock_service1.get(), technology())
2253 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002254
2255 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08002256 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07002257 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002258 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2259
2260 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
2261 string(flimflam::kTypeEthernet), &error);
2262 EXPECT_TRUE(error.IsSuccess());
2263 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2264
Gaurav Shah435de2c2011-11-17 19:01:07 -08002265 // Priority.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002266 mock_service0->SetPriority(1, NULL);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002267 manager()->UpdateService(mock_service0);
2268 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2269
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002270 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00002271 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07002272 manager()->UpdateService(mock_service1);
2273 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2274
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002275 // Auto-connect.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002276 mock_service0->SetAutoConnect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002277 manager()->UpdateService(mock_service0);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002278 mock_service1->SetAutoConnect(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002279 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002280 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2281
Paul Stewartdf3c0a82012-11-09 15:54:33 -08002282 // Test is-dependent-on. It doesn't make sense to have this ranking compare
2283 // to any of the others below, so we reset to the default state after
2284 // testing.
2285 EXPECT_CALL(*mock_service1.get(),
2286 IsDependentOn(ServiceRefPtr(mock_service0.get())))
2287 .WillOnce(Return(true))
2288 .WillRepeatedly(Return(false));
2289 manager()->UpdateService(mock_service1);
2290 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2291 manager()->UpdateService(mock_service0);
2292 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2293
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002294 // Connectable.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002295 mock_service1->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002296 manager()->UpdateService(mock_service1);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002297 mock_service0->SetConnectable(false);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002298 manager()->UpdateService(mock_service0);
2299 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2300
2301 // IsFailed.
2302 EXPECT_CALL(*mock_service0.get(), state())
2303 .WillRepeatedly(Return(Service::kStateIdle));
2304 EXPECT_CALL(*mock_service0.get(), IsFailed())
2305 .WillRepeatedly(Return(false));
2306 manager()->UpdateService(mock_service0);
2307 EXPECT_CALL(*mock_service0.get(), state())
2308 .WillRepeatedly(Return(Service::kStateFailure));
2309 EXPECT_CALL(*mock_service1.get(), IsFailed())
2310 .WillRepeatedly(Return(true));
2311 manager()->UpdateService(mock_service1);
2312 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2313
2314 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07002315 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002316 .WillRepeatedly(Return(Service::kStateAssociating));
2317 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08002318 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002319 manager()->UpdateService(mock_service1);
2320 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2321
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002322 // Connected.
2323 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07002324 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002325 EXPECT_CALL(*mock_service0.get(), IsConnected())
2326 .WillRepeatedly(Return(true));
2327 manager()->UpdateService(mock_service0);
2328 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
2329
Paul Stewarta121c442012-06-09 14:12:58 -07002330 // Portal.
2331 EXPECT_CALL(*mock_service1.get(), state())
2332 .WillRepeatedly(Return(Service::kStateConnected));
2333 EXPECT_CALL(*mock_service1.get(), IsConnected())
2334 .WillRepeatedly(Return(true));
2335 manager()->UpdateService(mock_service1);
2336 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
2337
Paul Stewart22aa71b2011-09-16 12:15:11 -07002338 manager()->DeregisterService(mock_service0);
2339 manager()->DeregisterService(mock_service1);
2340}
2341
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002342TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002343 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002344 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002345
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002346 scoped_refptr<MockService> mock_service0(
2347 new NiceMock<MockService>(control_interface(),
2348 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002349 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002350 manager()));
2351 scoped_refptr<MockService> mock_service1(
2352 new NiceMock<MockService>(control_interface(),
2353 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002354 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002355 manager()));
2356
2357 scoped_refptr<MockConnection> mock_connection0(
2358 new NiceMock<MockConnection>(device_info_.get()));
2359 scoped_refptr<MockConnection> mock_connection1(
2360 new NiceMock<MockConnection>(device_info_.get()));
2361
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002362 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002363 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002364 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002365 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002366 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002367 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002368
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002369 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002370 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002371
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002372 mock_service1->SetPriority(1, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002373 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002374 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002375
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002376 mock_service1->SetPriority(0, NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002377 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002378 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002379
Paul Stewartce4ec192012-03-14 12:53:46 -07002380 mock_service0->set_mock_connection(mock_connection0);
2381 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002382
2383 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002384 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002385 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002386
Darin Petkova5e07ef2012-07-09 14:27:57 +02002387 ServiceWatcher service_watcher;
2388 int tag =
2389 manager()->RegisterDefaultServiceCallback(
2390 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2391 service_watcher.AsWeakPtr()));
2392 EXPECT_EQ(1, tag);
2393
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002394 mock_service1->SetPriority(1, NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002395 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2396 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002397 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002398 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07002399 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002400
Darin Petkova5e07ef2012-07-09 14:27:57 +02002401 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002402 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002403 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00002404 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002405 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002406 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002407 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002408
Paul Stewartce4ec192012-03-14 12:53:46 -07002409 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002410 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002411 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002412 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002413
2414 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07002415 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002416}
2417
Darin Petkova5e07ef2012-07-09 14:27:57 +02002418TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2419 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2420 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2421
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002422 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002423 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002424
2425 scoped_refptr<MockService> mock_service(
2426 new NiceMock<MockService>(
2427 control_interface(), dispatcher(), metrics(), manager()));
2428 ServiceRefPtr service = mock_service;
2429 ServiceRefPtr null_service;
2430
2431 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2432 manager()->NotifyDefaultServiceChanged(null_service);
2433
2434 ServiceWatcher service_watcher1;
2435 ServiceWatcher service_watcher2;
2436 int tag1 =
2437 manager()->RegisterDefaultServiceCallback(
2438 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2439 service_watcher1.AsWeakPtr()));
2440 EXPECT_EQ(1, tag1);
2441 int tag2 =
2442 manager()->RegisterDefaultServiceCallback(
2443 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2444 service_watcher2.AsWeakPtr()));
2445 EXPECT_EQ(2, tag2);
2446
2447 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2448 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
2449 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
2450 manager()->NotifyDefaultServiceChanged(null_service);
2451
2452 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2453 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2454 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2455 manager()->NotifyDefaultServiceChanged(mock_service);
2456
2457 manager()->DeregisterDefaultServiceCallback(tag1);
2458 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2459 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2460 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2461 manager()->NotifyDefaultServiceChanged(mock_service);
2462 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2463
2464 manager()->DeregisterDefaultServiceCallback(tag2);
2465 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2466 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2467 manager()->NotifyDefaultServiceChanged(mock_service);
2468
2469 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2470 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2471}
2472
Gaurav Shah435de2c2011-11-17 19:01:07 -08002473TEST_F(ManagerTest, AvailableTechnologies) {
2474 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2475 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002476 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002477 manager(),
2478 "null4",
2479 "addr4",
2480 0));
2481 manager()->RegisterDevice(mock_devices_[0]);
2482 manager()->RegisterDevice(mock_devices_[1]);
2483 manager()->RegisterDevice(mock_devices_[2]);
2484 manager()->RegisterDevice(mock_devices_[3]);
2485
2486 ON_CALL(*mock_devices_[0].get(), technology())
2487 .WillByDefault(Return(Technology::kEthernet));
2488 ON_CALL(*mock_devices_[1].get(), technology())
2489 .WillByDefault(Return(Technology::kWifi));
2490 ON_CALL(*mock_devices_[2].get(), technology())
2491 .WillByDefault(Return(Technology::kCellular));
2492 ON_CALL(*mock_devices_[3].get(), technology())
2493 .WillByDefault(Return(Technology::kWifi));
2494
2495 set<string> expected_technologies;
2496 expected_technologies.insert(Technology::NameFromIdentifier(
2497 Technology::kEthernet));
2498 expected_technologies.insert(Technology::NameFromIdentifier(
2499 Technology::kWifi));
2500 expected_technologies.insert(Technology::NameFromIdentifier(
2501 Technology::kCellular));
2502 Error error;
2503 vector<string> technologies = manager()->AvailableTechnologies(&error);
2504
2505 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2506 ContainerEq(expected_technologies));
2507}
2508
2509TEST_F(ManagerTest, ConnectedTechnologies) {
2510 scoped_refptr<MockService> connected_service1(
2511 new NiceMock<MockService>(control_interface(),
2512 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002513 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002514 manager()));
2515 scoped_refptr<MockService> connected_service2(
2516 new NiceMock<MockService>(control_interface(),
2517 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002518 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002519 manager()));
2520 scoped_refptr<MockService> disconnected_service1(
2521 new NiceMock<MockService>(control_interface(),
2522 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002523 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002524 manager()));
2525 scoped_refptr<MockService> disconnected_service2(
2526 new NiceMock<MockService>(control_interface(),
2527 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002528 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002529 manager()));
2530
2531 ON_CALL(*connected_service1.get(), IsConnected())
2532 .WillByDefault(Return(true));
2533 ON_CALL(*connected_service2.get(), IsConnected())
2534 .WillByDefault(Return(true));
2535
2536 manager()->RegisterService(connected_service1);
2537 manager()->RegisterService(connected_service2);
2538 manager()->RegisterService(disconnected_service1);
2539 manager()->RegisterService(disconnected_service2);
2540
2541 manager()->RegisterDevice(mock_devices_[0]);
2542 manager()->RegisterDevice(mock_devices_[1]);
2543 manager()->RegisterDevice(mock_devices_[2]);
2544 manager()->RegisterDevice(mock_devices_[3]);
2545
2546 ON_CALL(*mock_devices_[0].get(), technology())
2547 .WillByDefault(Return(Technology::kEthernet));
2548 ON_CALL(*mock_devices_[1].get(), technology())
2549 .WillByDefault(Return(Technology::kWifi));
2550 ON_CALL(*mock_devices_[2].get(), technology())
2551 .WillByDefault(Return(Technology::kCellular));
2552 ON_CALL(*mock_devices_[3].get(), technology())
2553 .WillByDefault(Return(Technology::kWifi));
2554
2555 mock_devices_[0]->SelectService(connected_service1);
2556 mock_devices_[1]->SelectService(disconnected_service1);
2557 mock_devices_[2]->SelectService(disconnected_service2);
2558 mock_devices_[3]->SelectService(connected_service2);
2559
2560 set<string> expected_technologies;
2561 expected_technologies.insert(Technology::NameFromIdentifier(
2562 Technology::kEthernet));
2563 expected_technologies.insert(Technology::NameFromIdentifier(
2564 Technology::kWifi));
2565 Error error;
2566
2567 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2568 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2569 ContainerEq(expected_technologies));
2570}
2571
2572TEST_F(ManagerTest, DefaultTechnology) {
2573 scoped_refptr<MockService> connected_service(
2574 new NiceMock<MockService>(control_interface(),
2575 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002576 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002577 manager()));
2578 scoped_refptr<MockService> disconnected_service(
2579 new NiceMock<MockService>(control_interface(),
2580 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002581 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002582 manager()));
2583
2584 // Connected. WiFi.
2585 ON_CALL(*connected_service.get(), IsConnected())
2586 .WillByDefault(Return(true));
2587 ON_CALL(*connected_service.get(), state())
2588 .WillByDefault(Return(Service::kStateConnected));
2589 ON_CALL(*connected_service.get(), technology())
2590 .WillByDefault(Return(Technology::kWifi));
2591
2592 // Disconnected. Ethernet.
2593 ON_CALL(*disconnected_service.get(), technology())
2594 .WillByDefault(Return(Technology::kEthernet));
2595
2596 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002597 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002598 Error error;
2599 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2600
2601
2602 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002603 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002604 // Connected service should be brought to the front now.
2605 string expected_technology =
2606 Technology::NameFromIdentifier(Technology::kWifi);
2607 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2608}
2609
Paul Stewart212d60f2012-07-12 10:59:13 -07002610TEST_F(ManagerTest, Stop) {
2611 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002612 new NiceMock<MockProfile>(
2613 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002614 AdoptProfile(manager(), profile);
2615 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002616 new NiceMock<MockService>(control_interface(),
2617 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002618 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002619 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002620 manager()->RegisterService(service);
2621 manager()->RegisterDevice(mock_devices_[0]);
2622 EXPECT_CALL(*profile.get(),
2623 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2624 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002625 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002626 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2627 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002628 manager()->Stop();
2629}
2630
mukesh agrawal00917ce2011-11-22 23:56:55 +00002631TEST_F(ManagerTest, UpdateServiceConnected) {
2632 scoped_refptr<MockService> mock_service(
2633 new NiceMock<MockService>(control_interface(),
2634 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002635 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002636 manager()));
2637 manager()->RegisterService(mock_service);
2638 EXPECT_FALSE(mock_service->favorite());
2639 EXPECT_FALSE(mock_service->auto_connect());
2640
Gaurav Shah435de2c2011-11-17 19:01:07 -08002641 EXPECT_CALL(*mock_service.get(), IsConnected())
2642 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002643 manager()->UpdateService(mock_service);
2644 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2645 // to mock out MakeFavorite. And mocking that out would break the
2646 // SortServices test. (crosbug.com/23370)
2647 EXPECT_TRUE(mock_service->favorite());
2648 EXPECT_TRUE(mock_service->auto_connect());
2649}
2650
Thieu Led4e9e552012-02-16 16:26:07 -08002651TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2652 // This tests the case where the user connects to a service that is
2653 // currently associated with a profile. We want to make sure that the
2654 // favorite flag is set and that the flag is saved to the current
2655 // profile.
2656 scoped_refptr<MockService> mock_service(
2657 new NiceMock<MockService>(control_interface(),
2658 dispatcher(),
2659 metrics(),
2660 manager()));
2661 manager()->RegisterService(mock_service);
2662 EXPECT_FALSE(mock_service->favorite());
2663 EXPECT_FALSE(mock_service->auto_connect());
2664
Gary Moraind93615e2012-04-27 11:50:03 -07002665 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002666 new MockProfile(
2667 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002668
Gary Moraind93615e2012-04-27 11:50:03 -07002669 mock_service->set_profile(profile);
2670 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002671 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002672 EXPECT_CALL(*profile,
2673 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002674 manager()->UpdateService(mock_service);
2675 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2676 // to mock out MakeFavorite. And mocking that out would break the
2677 // SortServices test. (crosbug.com/23370)
2678 EXPECT_TRUE(mock_service->favorite());
2679 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002680 // This releases the ref on the mock profile.
2681 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002682}
2683
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002684TEST_F(ManagerTest, SaveSuccessfulService) {
2685 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002686 new StrictMock<MockProfile>(
2687 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002688 AdoptProfile(manager(), profile);
2689 scoped_refptr<MockService> service(
2690 new NiceMock<MockService>(control_interface(),
2691 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002692 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002693 manager()));
2694
2695 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2696 ServiceRefPtr expect_service(service.get());
2697
2698 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2699 .WillOnce(Return(false));
2700 manager()->RegisterService(service);
2701
2702 EXPECT_CALL(*service.get(), state())
2703 .WillRepeatedly(Return(Service::kStateConnected));
2704 EXPECT_CALL(*service.get(), IsConnected())
2705 .WillRepeatedly(Return(true));
2706 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2707 .WillOnce(Return(true));
2708 manager()->UpdateService(service);
2709}
2710
Darin Petkove7c6ad32012-06-29 10:22:09 +02002711TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002712 MockProfile *profile0 =
2713 new MockProfile(control_interface(), metrics(), manager(), "");
2714 MockProfile *profile1 =
2715 new MockProfile(control_interface(), metrics(), manager(), "");
2716 MockProfile *profile2 =
2717 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002718 AdoptProfile(manager(), profile0); // Passes ownership.
2719 AdoptProfile(manager(), profile1); // Passes ownership.
2720 AdoptProfile(manager(), profile2); // Passes ownership.
2721 DeviceRefPtr device_ref(mock_devices_[0].get());
2722 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2723 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2724 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2725 manager()->UpdateDevice(mock_devices_[0]);
2726}
2727
Paul Stewart1b253142012-01-26 14:05:52 -08002728TEST_F(ManagerTest, EnumerateProfiles) {
2729 vector<string> profile_paths;
2730 for (size_t i = 0; i < 10; i++) {
2731 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002732 new StrictMock<MockProfile>(
2733 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002734 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002735 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2736 .WillOnce(Return(profile_paths.back()));
2737 AdoptProfile(manager(), profile);
2738 }
2739
2740 Error error;
2741 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2742 EXPECT_TRUE(error.IsSuccess());
2743 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2744 for (size_t i = 0; i < profile_paths.size(); i++) {
2745 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2746 }
2747}
2748
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002749TEST_F(ManagerTest, AutoConnectOnRegister) {
2750 MockServiceRefPtr service = MakeAutoConnectableService();
2751 EXPECT_CALL(*service.get(), AutoConnect());
2752 manager()->RegisterService(service);
2753 dispatcher()->DispatchPendingEvents();
2754}
2755
2756TEST_F(ManagerTest, AutoConnectOnUpdate) {
2757 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002758 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002759 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002760 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002761 manager()->RegisterService(service1);
2762 manager()->RegisterService(service2);
2763 dispatcher()->DispatchPendingEvents();
2764
2765 EXPECT_CALL(*service1.get(), AutoConnect());
2766 EXPECT_CALL(*service2.get(), state())
2767 .WillRepeatedly(Return(Service::kStateFailure));
2768 EXPECT_CALL(*service2.get(), IsFailed())
2769 .WillRepeatedly(Return(true));
2770 EXPECT_CALL(*service2.get(), IsConnected())
2771 .WillRepeatedly(Return(false));
2772 manager()->UpdateService(service2);
2773 dispatcher()->DispatchPendingEvents();
2774}
2775
2776TEST_F(ManagerTest, AutoConnectOnDeregister) {
2777 MockServiceRefPtr service1 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002778 service1->SetPriority(1, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002779 MockServiceRefPtr service2 = MakeAutoConnectableService();
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07002780 service2->SetPriority(2, NULL);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002781 manager()->RegisterService(service1);
2782 manager()->RegisterService(service2);
2783 dispatcher()->DispatchPendingEvents();
2784
2785 EXPECT_CALL(*service1.get(), AutoConnect());
2786 manager()->DeregisterService(service2);
2787 dispatcher()->DispatchPendingEvents();
2788}
2789
Darin Petkov3ec55342012-09-28 14:04:44 +02002790TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2791 MockServiceRefPtr service = MakeAutoConnectableService();
2792 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2793 SetPowerManager();
2794 EXPECT_CALL(*service, AutoConnect()).Times(0);
2795 manager()->RegisterService(service);
2796 dispatcher()->DispatchPendingEvents();
2797}
2798
Darin Petkovca621542012-07-25 14:25:56 +02002799TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2800 MockServiceRefPtr service = MakeAutoConnectableService();
2801 SetPowerState(PowerManagerProxyDelegate::kMem);
2802 SetPowerManager();
2803 EXPECT_CALL(*service, AutoConnect()).Times(0);
2804 manager()->RegisterService(service);
2805 dispatcher()->DispatchPendingEvents();
2806}
2807
2808TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2809 MockServiceRefPtr service = MakeAutoConnectableService();
2810 SetPowerState(PowerManagerProxyDelegate::kOn);
2811 SetPowerManager();
2812 EXPECT_CALL(*service, AutoConnect());
2813 manager()->RegisterService(service);
2814 dispatcher()->DispatchPendingEvents();
2815}
2816
2817TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2818 MockServiceRefPtr service = MakeAutoConnectableService();
2819 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2820 SetPowerManager();
2821 EXPECT_CALL(*service, AutoConnect());
2822 manager()->RegisterService(service);
2823 dispatcher()->DispatchPendingEvents();
2824}
2825
Paul Stewart63864b62012-11-07 15:10:55 -08002826TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2827 SetRunning(false);
2828 MockServiceRefPtr service = MakeAutoConnectableService();
2829 EXPECT_CALL(*service, AutoConnect()).Times(0);
2830 manager()->RegisterService(service);
2831 dispatcher()->DispatchPendingEvents();
2832}
2833
Darin Petkovca621542012-07-25 14:25:56 +02002834TEST_F(ManagerTest, OnPowerStateChanged) {
2835 MockServiceRefPtr service = MakeAutoConnectableService();
2836 SetPowerState(PowerManagerProxyDelegate::kOn);
2837 SetPowerManager();
2838 EXPECT_CALL(*service, AutoConnect());
2839 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002840 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002841 dispatcher()->DispatchPendingEvents();
2842
mukesh agrawal784566d2012-08-08 18:32:58 -07002843 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002844 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2845 EXPECT_CALL(*service, AutoConnect());
2846 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002847 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002848
mukesh agrawal784566d2012-08-08 18:32:58 -07002849 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002850 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2851 EXPECT_CALL(*service, AutoConnect()).Times(0);
2852 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002853 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002854}
2855
Darin Petkov3ec55342012-09-28 14:04:44 +02002856TEST_F(ManagerTest, AddTerminationAction) {
2857 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
Daniel Eratf9753672013-01-24 10:17:02 -08002858 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _));
Darin Petkov3ec55342012-09-28 14:04:44 +02002859 SetPowerManager();
2860 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2861 manager()->AddTerminationAction("action1", base::Closure());
2862 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2863 manager()->AddTerminationAction("action2", base::Closure());
2864}
2865
2866TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002867 const char kKey1[] = "action1";
2868 const char kKey2[] = "action2";
2869 const int kSuspendDelayId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002870
2871 MockPowerManager &power_manager = *power_manager_;
2872 SetPowerManager();
2873
2874 // Removing an action when the hook table is empty should not result in any
2875 // calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002876 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002877 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2878 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2879 manager()->RemoveTerminationAction("unknown");
2880 Mock::VerifyAndClearExpectations(&power_manager);
2881
Daniel Eratf9753672013-01-24 10:17:02 -08002882 EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _))
2883 .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true)));
Daniel Erat0818cca2012-12-14 10:16:21 -08002884 EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1);
Darin Petkov3ec55342012-09-28 14:04:44 +02002885 manager()->AddTerminationAction(kKey1, base::Closure());
2886 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2887 manager()->AddTerminationAction(kKey2, base::Closure());
2888
2889 // Removing an action that ends up with a non-empty hook table should not
2890 // result in any calls to the power manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002891 EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0);
Darin Petkov3ec55342012-09-28 14:04:44 +02002892 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2893 manager()->RemoveTerminationAction(kKey1);
2894 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2895 Mock::VerifyAndClearExpectations(&power_manager);
2896
2897 // Removing the last action should trigger unregistering from the power
2898 // manager.
Daniel Erat0818cca2012-12-14 10:16:21 -08002899 EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId))
2900 .WillOnce(Return(true));
Darin Petkov3ec55342012-09-28 14:04:44 +02002901 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2902 manager()->RemoveTerminationAction(kKey2);
2903 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2904}
2905
2906TEST_F(ManagerTest, RunTerminationActions) {
2907 TerminationActionTest test_action;
2908 const string kActionName = "action";
2909
2910 EXPECT_CALL(test_action, Done(_));
2911 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2912 test_action.AsWeakPtr()));
2913
2914 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2915 Bind(&TerminationActionTest::Action,
2916 test_action.AsWeakPtr()));
2917 test_action.set_manager(manager());
2918 EXPECT_CALL(test_action, Done(_));
2919 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2920 test_action.AsWeakPtr()));
2921}
2922
Daniel Erat0818cca2012-12-14 10:16:21 -08002923TEST_F(ManagerTest, OnSuspendImminent) {
2924 const int kSuspendId = 123;
Darin Petkov3ec55342012-09-28 14:04:44 +02002925 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
Daniel Erat0818cca2012-12-14 10:16:21 -08002926 EXPECT_CALL(*power_manager_,
2927 ReportSuspendReadiness(
2928 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002929 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002930 OnSuspendImminent(kSuspendId);
Darin Petkov3ec55342012-09-28 14:04:44 +02002931}
2932
2933TEST_F(ManagerTest, OnSuspendActionsComplete) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002934 const int kSuspendId = 54321;
Darin Petkov3ec55342012-09-28 14:04:44 +02002935 Error error;
Daniel Erat0818cca2012-12-14 10:16:21 -08002936 EXPECT_CALL(*power_manager_,
2937 ReportSuspendReadiness(
2938 manager()->suspend_delay_id_for_testing(), kSuspendId));
Darin Petkov3ec55342012-09-28 14:04:44 +02002939 SetPowerManager();
Daniel Erat0818cca2012-12-14 10:16:21 -08002940 OnSuspendActionsComplete(kSuspendId, error);
Darin Petkov3ec55342012-09-28 14:04:44 +02002941}
2942
Paul Stewartc681fa02012-03-02 19:40:04 -08002943TEST_F(ManagerTest, RecheckPortal) {
2944 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2945 .WillOnce(Return(false));
2946 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2947 .WillOnce(Return(true));
2948 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2949 .Times(0);
2950
2951 manager()->RegisterDevice(mock_devices_[0]);
2952 manager()->RegisterDevice(mock_devices_[1]);
2953 manager()->RegisterDevice(mock_devices_[2]);
2954
2955 manager()->RecheckPortal(NULL);
2956}
2957
Paul Stewartd215af62012-04-24 23:25:50 -07002958TEST_F(ManagerTest, RecheckPortalOnService) {
2959 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2960 dispatcher(),
2961 metrics(),
2962 manager());
2963 EXPECT_CALL(*mock_devices_[0].get(),
2964 IsConnectedToService(IsRefPtrTo(service)))
2965 .WillOnce(Return(false));
2966 EXPECT_CALL(*mock_devices_[1].get(),
2967 IsConnectedToService(IsRefPtrTo(service)))
2968 .WillOnce(Return(true));
2969 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2970 .WillOnce(Return(true));
2971 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2972 .Times(0);
2973
2974 manager()->RegisterDevice(mock_devices_[0]);
2975 manager()->RegisterDevice(mock_devices_[1]);
2976 manager()->RegisterDevice(mock_devices_[2]);
2977
2978 manager()->RecheckPortalOnService(service);
2979}
2980
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002981TEST_F(ManagerTest, GetDefaultService) {
2982 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002983 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002984
2985 scoped_refptr<MockService> mock_service(
2986 new NiceMock<MockService>(control_interface(),
2987 dispatcher(),
2988 metrics(),
2989 manager()));
2990
2991 manager()->RegisterService(mock_service);
2992 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002993 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002994
2995 scoped_refptr<MockConnection> mock_connection(
2996 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002997 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002998 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002999 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003000
Paul Stewartce4ec192012-03-14 12:53:46 -07003001 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003002 manager()->DeregisterService(mock_service);
3003}
3004
Paul Stewart13ed2252012-03-21 12:52:46 -07003005TEST_F(ManagerTest, GetServiceWithGUID) {
3006 scoped_refptr<MockService> mock_service0(
3007 new NiceMock<MockService>(control_interface(),
3008 dispatcher(),
3009 metrics(),
3010 manager()));
3011
3012 scoped_refptr<MockService> mock_service1(
3013 new NiceMock<MockService>(control_interface(),
3014 dispatcher(),
3015 metrics(),
3016 manager()));
3017
Paul Stewartcb59fed2012-03-21 21:14:46 -07003018 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3019 .Times(0);
3020 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3021 .Times(0);
3022
Paul Stewart13ed2252012-03-21 12:52:46 -07003023 manager()->RegisterService(mock_service0);
3024 manager()->RegisterService(mock_service1);
3025
3026 const string kGUID0 = "GUID0";
3027 const string kGUID1 = "GUID1";
3028
3029 {
3030 Error error;
3031 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3032 EXPECT_FALSE(error.IsSuccess());
3033 EXPECT_FALSE(service);
3034 }
3035
3036 KeyValueStore args;
3037 args.SetString(flimflam::kGuidProperty, kGUID1);
3038
3039 {
3040 Error error;
3041 ServiceRefPtr service = manager()->GetService(args, &error);
3042 EXPECT_EQ(Error::kInvalidArguments, error.type());
3043 EXPECT_FALSE(service);
3044 }
3045
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003046 mock_service0->SetGuid(kGUID0, NULL);
3047 mock_service1->SetGuid(kGUID1, NULL);
Paul Stewart13ed2252012-03-21 12:52:46 -07003048
3049 {
3050 Error error;
3051 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3052 EXPECT_TRUE(error.IsSuccess());
3053 EXPECT_EQ(mock_service0.get(), service.get());
3054 }
3055
3056 {
3057 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003058 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3059 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003060 ServiceRefPtr service = manager()->GetService(args, &error);
3061 EXPECT_TRUE(error.IsSuccess());
3062 EXPECT_EQ(mock_service1.get(), service.get());
3063 }
3064
3065 manager()->DeregisterService(mock_service0);
3066 manager()->DeregisterService(mock_service1);
3067}
3068
Gary Morain028545d2012-04-07 14:55:52 -07003069
3070TEST_F(ManagerTest, CalculateStateOffline) {
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003071 EXPECT_FALSE(manager()->IsOnline());
3072 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3073
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003074 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003075 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003076 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3077 .Times(AnyNumber());
3078 scoped_refptr<MockService> mock_service0(
3079 new NiceMock<MockService>(control_interface(),
3080 dispatcher(),
3081 metrics(),
3082 manager()));
3083
3084 scoped_refptr<MockService> mock_service1(
3085 new NiceMock<MockService>(control_interface(),
3086 dispatcher(),
3087 metrics(),
3088 manager()));
3089
3090 EXPECT_CALL(*mock_service0.get(), IsConnected())
3091 .WillRepeatedly(Return(false));
3092 EXPECT_CALL(*mock_service1.get(), IsConnected())
3093 .WillRepeatedly(Return(false));
3094
3095 manager()->RegisterService(mock_service0);
3096 manager()->RegisterService(mock_service1);
3097
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003098 EXPECT_FALSE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003099 EXPECT_EQ("offline", manager()->CalculateState(NULL));
3100
3101 manager()->DeregisterService(mock_service0);
3102 manager()->DeregisterService(mock_service1);
3103}
3104
3105TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003106 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003107 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003108 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3109 .Times(AnyNumber());
3110 scoped_refptr<MockService> mock_service0(
3111 new NiceMock<MockService>(control_interface(),
3112 dispatcher(),
3113 metrics(),
3114 manager()));
3115
3116 scoped_refptr<MockService> mock_service1(
3117 new NiceMock<MockService>(control_interface(),
3118 dispatcher(),
3119 metrics(),
3120 manager()));
3121
3122 EXPECT_CALL(*mock_service0.get(), IsConnected())
3123 .WillRepeatedly(Return(false));
3124 EXPECT_CALL(*mock_service1.get(), IsConnected())
3125 .WillRepeatedly(Return(true));
3126 EXPECT_CALL(*mock_service0.get(), state())
3127 .WillRepeatedly(Return(Service::kStateIdle));
3128 EXPECT_CALL(*mock_service1.get(), state())
3129 .WillRepeatedly(Return(Service::kStateConnected));
3130
3131 manager()->RegisterService(mock_service0);
3132 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003133 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003134
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003135 EXPECT_TRUE(manager()->IsOnline());
Gary Morain028545d2012-04-07 14:55:52 -07003136 EXPECT_EQ("online", manager()->CalculateState(NULL));
3137
3138 manager()->DeregisterService(mock_service0);
3139 manager()->DeregisterService(mock_service1);
3140}
3141
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003142TEST_F(ManagerTest, StartupPortalList) {
3143 // Simulate loading value from the default profile.
3144 const string kProfileValue("wifi,vpn");
3145 manager()->props_.check_portal_list = kProfileValue;
3146
3147 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
3148 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3149 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3150
3151 const string kStartupValue("cellular,ethernet");
3152 manager()->SetStartupPortalList(kStartupValue);
3153 // Ensure profile value is not overwritten, so when we save the default
3154 // profile, the correct value will still be written.
3155 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3156
3157 // However we should read back a different list.
3158 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
3159 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3160 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3161
3162 const string kRuntimeValue("ppp");
3163 // Setting a runtime value over the control API should overwrite both
3164 // the profile value and what we read back.
3165 Error error;
3166 manager()->mutable_store()->SetStringProperty(
3167 flimflam::kCheckPortalListProperty,
3168 kRuntimeValue,
3169 &error);
3170 ASSERT_TRUE(error.IsSuccess());
3171 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
3172 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3173 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3174 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3175}
3176
Paul Stewart036dba02012-08-07 12:34:41 -07003177TEST_F(ManagerTest, LinkMonitorEnabled) {
3178 const string kEnabledTechnologies("wifi,vpn");
3179 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3180 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3181 EXPECT_FALSE(
3182 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3183}
3184
Paul Stewart85aea152013-01-22 09:31:56 -08003185TEST_F(ManagerTest, IsDefaultProfile) {
Paul Stewart3c504012013-01-17 17:49:58 -08003186 EXPECT_TRUE(manager()->IsDefaultProfile(NULL));
Paul Stewart85aea152013-01-22 09:31:56 -08003187 scoped_ptr<MockStore> store0(new MockStore);
Paul Stewart3c504012013-01-17 17:49:58 -08003188 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
Paul Stewart85aea152013-01-22 09:31:56 -08003189 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08003190 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart85aea152013-01-22 09:31:56 -08003191 EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get()));
3192 AdoptProfile(manager(), profile);
3193 EXPECT_TRUE(manager()->IsDefaultProfile(store0.get()));
3194 EXPECT_FALSE(manager()->IsDefaultProfile(NULL));
3195 scoped_ptr<MockStore> store1(new MockStore);
3196 EXPECT_FALSE(manager()->IsDefaultProfile(store1.get()));
3197}
3198
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003199TEST_F(ManagerTest, EnableTechnology) {
3200 Error error(Error::kOperationInitiated);
3201 ResultCallback callback;
3202 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3203 EXPECT_TRUE(error.IsSuccess());
3204
Joshua Krollda798622012-06-05 12:30:48 -07003205 ON_CALL(*mock_devices_[0], technology())
3206 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003207
3208 manager()->RegisterDevice(mock_devices_[0]);
3209
3210 // Device is enabled, so expect operation is successful.
3211 mock_devices_[0]->enabled_ = true;
3212 error.Populate(Error::kOperationInitiated);
3213 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3214 EXPECT_TRUE(error.IsSuccess());
3215
3216 // Device is disabled, so expect operation in progress.
3217 mock_devices_[0]->enabled_ = false;
3218 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3219 error.Populate(Error::kOperationInitiated);
3220 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
3221 EXPECT_TRUE(error.IsOngoing());
3222}
3223
3224TEST_F(ManagerTest, DisableTechnology) {
3225 Error error(Error::kOperationInitiated);
3226 ResultCallback callback;
3227 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3228 EXPECT_TRUE(error.IsSuccess());
3229
Joshua Krollda798622012-06-05 12:30:48 -07003230 ON_CALL(*mock_devices_[0], technology())
3231 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003232
3233 manager()->RegisterDevice(mock_devices_[0]);
3234
3235 // Device is disabled, so expect operation is successful.
3236 error.Populate(Error::kOperationInitiated);
3237 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3238 EXPECT_TRUE(error.IsSuccess());
3239
3240 // Device is enabled, so expect operation in progress.
3241 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3242 mock_devices_[0]->enabled_ = true;
3243 error.Populate(Error::kOperationInitiated);
3244 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
3245 EXPECT_TRUE(error.IsOngoing());
3246}
3247
Paul Stewart4d5efb72012-09-17 12:24:34 -07003248TEST_F(ManagerTest, IgnoredSearchList) {
3249 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003250 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003251 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003252
3253 const string kIgnored0 = "chromium.org";
3254 ignored_paths.push_back(kIgnored0);
3255 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003256 SetIgnoredDNSSearchPaths(kIgnored0, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003257 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3258
3259 const string kIgnored1 = "google.com";
3260 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3261 ignored_paths.push_back(kIgnored1);
3262 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003263 SetIgnoredDNSSearchPaths(kIgnoredSum, NULL);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003264 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3265
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003266 ignored_paths.clear();
3267 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
3268 SetIgnoredDNSSearchPaths("", NULL);
3269 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3270
Paul Stewart4d5efb72012-09-17 12:24:34 -07003271 SetResolver(Resolver::GetInstance());
3272}
3273
Paul Stewartbfb82552012-10-24 16:48:48 -07003274TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3275 // Test to make sure that every service state-change causes the
3276 // Manager to emit a new service list.
3277 scoped_refptr<MockService> mock_service(
3278 new NiceMock<MockService>(control_interface(),
3279 dispatcher(),
3280 metrics(),
3281 manager()));
3282 EXPECT_CALL(*mock_service, state())
3283 .WillRepeatedly(Return(Service::kStateIdle));
3284
3285 manager()->RegisterService(mock_service);
3286 EXPECT_CALL(
3287 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3288 flimflam::kServicesProperty, _)).Times(1);
3289 EXPECT_CALL(
3290 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3291 flimflam::kServiceWatchListProperty, _)).Times(1);
3292 CompleteServiceSort();
3293
3294 Mock::VerifyAndClearExpectations(manager_adaptor_);
3295 EXPECT_CALL(
3296 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3297 flimflam::kServicesProperty, _)).Times(1);
3298 EXPECT_CALL(
3299 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
3300 flimflam::kServiceWatchListProperty, _)).Times(1);
3301 manager()->UpdateService(mock_service.get());
3302 CompleteServiceSort();
3303
3304 manager()->DeregisterService(mock_service);
3305}
3306
3307TEST_F(ManagerTest, EnumerateServices) {
3308 scoped_refptr<MockService> mock_service(
3309 new NiceMock<MockService>(control_interface(),
3310 dispatcher(),
3311 metrics(),
3312 manager()));
3313 manager()->RegisterService(mock_service);
3314
3315 EXPECT_CALL(*mock_service, state())
3316 .WillRepeatedly(Return(Service::kStateConnected));
3317 EXPECT_CALL(*mock_service, IsVisible())
3318 .WillRepeatedly(Return(false));
3319 EXPECT_TRUE(EnumerateAvailableServices().empty());
3320 EXPECT_TRUE(EnumerateWatchedServices().empty());
3321
3322 EXPECT_CALL(*mock_service, state())
3323 .WillRepeatedly(Return(Service::kStateIdle));
3324 EXPECT_TRUE(EnumerateAvailableServices().empty());
3325 EXPECT_TRUE(EnumerateWatchedServices().empty());
3326
3327 EXPECT_CALL(*mock_service, IsVisible())
3328 .WillRepeatedly(Return(true));
3329 Service::ConnectState unwatched_states[] = {
3330 Service::kStateUnknown,
3331 Service::kStateIdle,
3332 Service::kStateFailure
3333 };
3334 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3335 EXPECT_CALL(*mock_service, state())
3336 .WillRepeatedly(Return(unwatched_states[i]));
3337 EXPECT_FALSE(EnumerateAvailableServices().empty());
3338 EXPECT_TRUE(EnumerateWatchedServices().empty());
3339 }
3340
3341 Service::ConnectState watched_states[] = {
3342 Service::kStateAssociating,
3343 Service::kStateConfiguring,
3344 Service::kStateConnected,
3345 Service::kStateDisconnected,
3346 Service::kStatePortal,
3347 Service::kStateOnline
3348 };
3349 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3350 EXPECT_CALL(*mock_service, state())
3351 .WillRepeatedly(Return(watched_states[i]));
3352 EXPECT_FALSE(EnumerateAvailableServices().empty());
3353 EXPECT_FALSE(EnumerateWatchedServices().empty());
3354 }
3355
3356 manager()->DeregisterService(mock_service);
3357}
3358
Paul Stewart39db5ca2013-03-18 14:15:17 -07003359TEST_F(ManagerTest, ConnectToBestServices) {
3360 scoped_refptr<MockService> wifi_service0(
3361 new NiceMock<MockService>(control_interface(),
3362 dispatcher(),
3363 metrics(),
3364 manager()));
3365 EXPECT_CALL(*wifi_service0.get(), state())
3366 .WillRepeatedly(Return(Service::kStateIdle));
3367 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3368 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003369 wifi_service0->SetConnectable(true);
3370 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003371 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3372 EXPECT_CALL(*wifi_service0.get(), technology())
3373 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003374 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3375 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003376
3377 scoped_refptr<MockService> wifi_service1(
3378 new NiceMock<MockService>(control_interface(),
3379 dispatcher(),
3380 metrics(),
3381 manager()));
3382 EXPECT_CALL(*wifi_service1.get(), state())
3383 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003384 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3385 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003386 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3387 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003388 wifi_service1->SetAutoConnect(true);
3389 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003390 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3391 EXPECT_CALL(*wifi_service1.get(), technology())
3392 .WillRepeatedly(Return(Technology::kWifi));
3393
3394 scoped_refptr<MockService> wifi_service2(
3395 new NiceMock<MockService>(control_interface(),
3396 dispatcher(),
3397 metrics(),
3398 manager()));
3399 EXPECT_CALL(*wifi_service2.get(), state())
3400 .WillRepeatedly(Return(Service::kStateConnected));
3401 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3402 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003403 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3404 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003405 wifi_service2->SetAutoConnect(true);
3406 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003407 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3408 EXPECT_CALL(*wifi_service2.get(), technology())
3409 .WillRepeatedly(Return(Technology::kWifi));
3410
3411 manager()->RegisterService(wifi_service0);
3412 manager()->RegisterService(wifi_service1);
3413 manager()->RegisterService(wifi_service2);
3414
3415 CompleteServiceSort();
3416 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3417
3418 scoped_refptr<MockService> cell_service(
3419 new NiceMock<MockService>(control_interface(),
3420 dispatcher(),
3421 metrics(),
3422 manager()));
3423
3424 EXPECT_CALL(*cell_service.get(), state())
3425 .WillRepeatedly(Return(Service::kStateConnected));
3426 EXPECT_CALL(*cell_service.get(), IsConnected())
3427 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003428 EXPECT_CALL(*cell_service.get(), IsVisible())
3429 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003430 wifi_service2->SetAutoConnect(true);
3431 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003432 EXPECT_CALL(*cell_service.get(), technology())
3433 .WillRepeatedly(Return(Technology::kCellular));
3434 manager()->RegisterService(cell_service);
3435
3436 scoped_refptr<MockService> vpn_service(
3437 new NiceMock<MockService>(control_interface(),
3438 dispatcher(),
3439 metrics(),
3440 manager()));
3441
3442 EXPECT_CALL(*vpn_service.get(), state())
3443 .WillRepeatedly(Return(Service::kStateIdle));
3444 EXPECT_CALL(*vpn_service.get(), IsConnected())
3445 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003446 EXPECT_CALL(*vpn_service.get(), IsVisible())
3447 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003448 wifi_service2->SetAutoConnect(false);
3449 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003450 EXPECT_CALL(*vpn_service.get(), technology())
3451 .WillRepeatedly(Return(Technology::kVPN));
3452 manager()->RegisterService(vpn_service);
3453
3454 // The connected services should be at the top.
3455 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3456
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003457 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3458 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003459 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
3460 EXPECT_CALL(*cell_service.get(), Connect(_, _)).Times(0); // Is connected.
3461 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003462
3463 manager()->ConnectToBestServices(NULL);
3464 dispatcher()->DispatchPendingEvents();
3465
3466 // After this operation, since the Connect calls above are mocked and
3467 // no actual state changes have occurred, we should expect that the
3468 // service sorting order will not have changed.
3469 EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service));
3470}
3471
Christopher Wiley83889862013-05-02 15:55:09 -07003472TEST_F(ManagerTest, VerifyWhenNotConnected) {
3473 const string kFakeCertificate("fake cert");
3474 const string kFakePublicKey("fake public key");
3475 const string kFakeNonce("fake public key");
3476 const string kFakeSignedData("fake signed data");
3477 const string kFakeUdn("fake udn");
3478 const vector<uint8_t> kSSID(10, 87);
3479 const string kConfiguredSSID("AConfiguredDestination");
3480 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3481 kConfiguredSSID.end());
3482 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3483 scoped_refptr<MockWiFiService> mock_destination(
3484 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3485 metrics(), manager(), wifi_provider_,
3486 kSSID, "", "none", false));
3487 // Register this service, but don't mark it as connected.
3488 manager()->RegisterService(mock_destination);
3489 // Verify that if we're not connected to anything, verification fails.
3490 {
3491 LOG(INFO) << "Can't verify if not connected.";
3492 EXPECT_CALL(*crypto_util_proxy_,
3493 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3494 Error error(Error::kOperationInitiated);
3495 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3496 kFakeSignedData, kFakeUdn, "", "",
3497 ResultBoolCallback(), &error);
3498 EXPECT_TRUE(error.IsFailure());
3499 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3500 }
3501 {
3502 // However, if the destination is already configured, we might be
3503 // connected to it via something other than WiFi, and we shouldn't
3504 // enforce the WiFi check.
3505 EXPECT_CALL(*crypto_util_proxy_,
3506 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3507 kFakeSignedData, kFakeUdn,
3508 kConfiguredSSIDVector, kConfiguredBSSID,
3509 _, _)).Times(1).WillOnce(Return(true));
3510 Error error(Error::kOperationInitiated);
3511 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3512 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3513 kConfiguredBSSID, ResultBoolCallback(),
3514 &error);
3515 EXPECT_FALSE(error.IsFailure());
3516 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3517 }
3518}
3519
Christopher Wiley1057cd72013-02-28 15:21:29 -08003520TEST_F(ManagerTest, VerifyDestination) {
3521 const string kFakeCertificate("fake cert");
3522 const string kFakePublicKey("fake public key");
3523 const string kFakeNonce("fake public key");
3524 const string kFakeSignedData("fake signed data");
3525 const string kFakeUdn("fake udn");
3526 const char kSSIDStr[] = "fake ssid";
3527 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003528 const string kConfiguredSSID("AConfiguredDestination");
3529 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3530 kConfiguredSSID.end());
3531 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003532 const string kFakeData("muffin man");
3533 scoped_refptr<MockWiFiService> mock_destination(
3534 new NiceMock<MockWiFiService>(control_interface(),
3535 dispatcher(),
3536 metrics(),
3537 manager(),
3538 wifi_provider_,
3539 kSSID,
3540 "",
3541 "none",
3542 false));
3543 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003544 // Making the service look online will let service lookup in
3545 // VerifyDestinatoin succeed.
3546 EXPECT_CALL(*mock_destination.get(), IsConnected())
3547 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003548 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003549
3550 // Lead off by verifying that the basic VerifyDestination flow works.
3551 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003552 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003553 ResultBoolCallback passed_down_callback;
3554 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3555 kFakePublicKey,
3556 kFakeNonce,
3557 kFakeSignedData,
3558 kFakeUdn,
3559 kSSID,
3560 _,
3561 _,
3562 _))
3563 .Times(1)
3564 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3565 // Ask the manager to verify the current destination. This should look
3566 // up our previously registered service, and pass some metadata about
3567 // that service down to the CryptoUtilProxy to verify.
3568 Error error(Error::kOperationInitiated);
3569 ResultBoolCallback cb = Bind(
3570 &DestinationVerificationTest::ResultBoolCallbackStub,
3571 dv_test.AsWeakPtr());
3572 manager()->VerifyDestination(kFakeCertificate,
3573 kFakePublicKey,
3574 kFakeNonce,
3575 kFakeSignedData,
3576 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003577 // Ask to be verified against that service.
3578 "", "",
3579 cb,
3580 &error);
3581 // We assert here, because if the operation is not ongoing, it is
3582 // inconsistent with shim behavior to call the callback anyway.
3583 ASSERT_TRUE(error.IsOngoing());
3584 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3585 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3586 // Call the callback passed into the CryptoUtilProxy, which
3587 // should find its way into the callback passed into the manager.
3588 // In real code, that callback passed into the manager is from the
3589 // DBus adaptor.
3590 Error e;
3591 passed_down_callback.Run(e, true);
3592 Mock::VerifyAndClearExpectations(&dv_test);
3593 }
3594
Christopher Wiley1057cd72013-02-28 15:21:29 -08003595 // Now for a slightly more complex variant. When we encrypt data,
3596 // we do the same verification step but monkey with the callback to
3597 // link ourselves to an encrypt step afterward.
3598 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003599 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003600 ResultBoolCallback passed_down_callback;
3601 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3602 kFakePublicKey,
3603 kFakeNonce,
3604 kFakeSignedData,
3605 kFakeUdn,
3606 kSSID,
3607 _,
3608 _,
3609 _))
3610 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3611
3612 Error error(Error::kOperationInitiated);
3613 ResultStringCallback cb = Bind(
3614 &DestinationVerificationTest::ResultStringCallbackStub,
3615 dv_test.AsWeakPtr());
3616 manager()->VerifyAndEncryptData(kFakeCertificate,
3617 kFakePublicKey,
3618 kFakeNonce,
3619 kFakeSignedData,
3620 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003621 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003622 kFakeData,
3623 cb,
3624 &error);
3625 ASSERT_TRUE(error.IsOngoing());
3626 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3627 // Now, if we call that passed down callback, we should see encrypt being
3628 // called.
3629 ResultStringCallback second_passed_down_callback;
3630 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
3631 kFakeData,
3632 _,
3633 _))
3634 .Times(1)
3635 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
3636 Return(true)));
3637 Error e;
3638 passed_down_callback.Run(e, true);
3639 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3640 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
3641 // And if we call the second passed down callback, we should see the
3642 // original function we passed down to VerifyDestination getting called.
3643 e.Reset();
3644 second_passed_down_callback.Run(e, "");
3645 Mock::VerifyAndClearExpectations(&dv_test);
3646 }
3647
3648 // If verification fails on the way to trying to encrypt, we should ditch
3649 // without calling encrypt at all.
3650 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003651 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003652 ResultBoolCallback passed_down_callback;
3653 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3654 kFakePublicKey,
3655 kFakeNonce,
3656 kFakeSignedData,
3657 kFakeUdn,
3658 kSSID,
3659 _,
3660 _,
3661 _))
3662 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3663
3664 Error error(Error::kOperationInitiated);
3665 ResultStringCallback cb = Bind(
3666 &DestinationVerificationTest::ResultStringCallbackStub,
3667 dv_test.AsWeakPtr());
3668 manager()->VerifyAndEncryptData(kFakeCertificate,
3669 kFakePublicKey,
3670 kFakeNonce,
3671 kFakeSignedData,
3672 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003673 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08003674 kFakeData,
3675 cb,
3676 &error);
3677 ASSERT_TRUE(error.IsOngoing());
3678 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3679 Error e(Error::kOperationFailed);
3680 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
3681 // Although we're ditching, this callback is what cleans up the pending
3682 // DBus call.
3683 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
3684 passed_down_callback.Run(e, false);
3685 Mock::VerifyAndClearExpectations(&dv_test);
3686 }
3687}
3688
Paul Stewartd2e1c362013-03-03 19:06:07 -08003689TEST_F(ManagerTest, IsProfileBefore) {
3690 scoped_refptr<MockProfile> profile0(
3691 new NiceMock<MockProfile>(
3692 control_interface(), metrics(), manager(), ""));
3693 scoped_refptr<MockProfile> profile1(
3694 new NiceMock<MockProfile>(
3695 control_interface(), metrics(), manager(), ""));
3696
3697 AdoptProfile(manager(), profile0);
3698 AdoptProfile(manager(), profile1); // profile1 is after profile0.
3699 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
3700 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
3701
3702 // A few abnormal cases, but it's good to track their behavior.
3703 scoped_refptr<MockProfile> profile2(
3704 new NiceMock<MockProfile>(
3705 control_interface(), metrics(), manager(), ""));
3706 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
3707 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
3708 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
3709 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
3710}
3711
Paul Stewart967eaeb2013-04-25 19:53:07 -07003712TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
3713 MockStore storage0;
3714 MockStore storage1;
3715 MockStore storage2;
3716
3717 scoped_refptr<MockProfile> profile0(
3718 new NiceMock<MockProfile>(
3719 control_interface(), metrics(), manager(), ""));
3720 scoped_refptr<MockProfile> profile1(
3721 new NiceMock<MockProfile>(
3722 control_interface(), metrics(), manager(), ""));
3723 scoped_refptr<MockProfile> profile2(
3724 new NiceMock<MockProfile>(
3725 control_interface(), metrics(), manager(), ""));
3726
3727 AdoptProfile(manager(), profile0);
3728 AdoptProfile(manager(), profile1);
3729 AdoptProfile(manager(), profile2);
3730
3731 scoped_refptr<MockService> service(
3732 new NiceMock<MockService>(control_interface(),
3733 dispatcher(),
3734 metrics(),
3735 manager()));
3736
3737 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
3738 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
3739 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
3740
3741 const string kEntry0("aluminum_crutch");
3742 const string kEntry2("rehashed_faces");
3743
3744 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
3745 .WillOnce(Return(kEntry0));
3746 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
3747 .WillOnce(Return(""));
3748 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
3749 .WillOnce(Return(kEntry2));
3750
3751 const string kProfileRpc0("service_station");
3752 const string kProfileRpc2("crystal_tiaras");
3753
3754 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
3755 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
3756 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
3757
3758 map<string, string> entries =
3759 manager()->GetLoadableProfileEntriesForService(service);
3760 EXPECT_EQ(2, entries.size());
3761 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
3762 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
3763 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
3764 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
3765}
3766
mukesh agrawald142fd62013-05-01 16:50:57 -07003767TEST_F(ManagerTest, InitializeProfiles) {
3768 // We need a real glib here, so that profiles are persisted.
3769 GLib glib;
3770 ScopedTempDir temp_dir;
3771 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
3772 Manager manager(control_interface(),
3773 dispatcher(),
3774 metrics(),
3775 &glib,
3776 run_path(),
3777 storage_path(),
3778 temp_dir.path().value());
3779 // Can't use |wifi_provider_|, because it's owned by the Manager
3780 // object in the fixture.
3781 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
3782 manager.wifi_provider_.reset(wifi_provider); // pass ownership
3783 // Give manager a valid place to write the user profile list.
3784 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
3785
3786 // With no user profiles, the WiFiProvider should be called once
3787 // (for the default profile).
3788 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
3789 manager.InitializeProfiles();
3790 Mock::VerifyAndClearExpectations(wifi_provider);
3791
3792 // With |n| user profiles, the WiFiProvider should be called |n+1|
3793 // times. First, create 2 user profiles...
3794 const char kProfile0[] = "~user/profile0";
3795 const char kProfile1[] = "~user/profile1";
3796 string profile_rpc_path;
3797 Error error;
3798 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
3799 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
3800 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
3801 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
3802
3803 // ... then reset manager state ...
3804 manager.profiles_.clear();
3805
3806 // ...then check that the WiFiProvider is notified about all three
3807 // profiles (one default, two user).
3808 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
3809 manager.InitializeProfiles();
3810 Mock::VerifyAndClearExpectations(wifi_provider);
3811}
3812
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003813// Custom property setters should return false, and make no changes, if
3814// the new value is the same as the old value.
3815TEST_F(ManagerTest, CustomSetterNoopChange) {
3816 // SetCheckPortalList
3817 {
3818 static const string kCheckPortalList = "weird-device,weirder-device";
3819 Error error;
3820 // Set to known value.
3821 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
3822 EXPECT_TRUE(error.IsSuccess());
3823 // Set to same value.
3824 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
3825 EXPECT_TRUE(error.IsSuccess());
3826 }
3827
3828 // SetIgnoredDNSSearchPaths
3829 {
3830 NiceMock<MockResolver> resolver;
3831 static const string kIgnoredPaths = "example.com,example.org";
3832 Error error;
3833 SetResolver(&resolver);
3834 // Set to known value.
3835 EXPECT_CALL(resolver, set_ignored_search_list(_));
3836 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
3837 EXPECT_TRUE(error.IsSuccess());
3838 Mock::VerifyAndClearExpectations(&resolver);
3839 // Set to same value.
3840 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
3841 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
3842 EXPECT_TRUE(error.IsSuccess());
3843 Mock::VerifyAndClearExpectations(&resolver);
3844 }
3845}
3846
Chris Masone9be4a9d2011-05-16 15:44:09 -07003847} // namespace shill