blob: e840308bb27ac2e9e019908e1757fcaf287552a3 [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>
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Chris Masone6791a432011-07-12 13:23:19 -07009#include <set>
10
Chris Masone9be4a9d2011-05-16 15:44:09 -070011#include <glib.h>
12
Ben Chan11c213f2014-09-05 08:21:06 -070013#include <base/files/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080014#include <base/files/scoped_temp_dir.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/stl_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080016#include <base/strings/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070018#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070019#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070020
mukesh agrawal32399322011-09-01 10:53:43 -070021#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070022#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070023#include "shill/error.h"
Ben Chan87602512014-11-07 20:50:05 -080024#include "shill/ethernet/mock_ethernet_eap_provider.h"
Paul Stewart7de7e022013-08-28 09:42:50 -070025#include "shill/geolocation_info.h"
Chris Masone6515aab2011-10-12 16:19:09 -070026#include "shill/glib.h"
27#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070028#include "shill/key_value_store.h"
mukesh agrawal00752532013-05-03 15:46:55 -070029#include "shill/link_monitor.h"
Christopher Wiley3e7635e2012-08-15 09:46:17 -070030#include "shill/logging.h"
mukesh agrawal32399322011-09-01 10:53:43 -070031#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080032#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070033#include "shill/mock_control.h"
Christopher Wiley1057cd72013-02-28 15:21:29 -080034#include "shill/mock_crypto_util_proxy.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070035#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080036#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070037#include "shill/mock_glib.h"
mukesh agrawalb94adde2013-08-22 18:17:26 -070038#include "shill/mock_log.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000039#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020040#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070041#include "shill/mock_profile.h"
Ben Chana55469d2014-01-27 16:35:29 -080042#include "shill/mock_proxy_factory.h"
Paul Stewart4d5efb72012-09-17 12:24:34 -070043#include "shill/mock_resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070044#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070045#include "shill/mock_store.h"
mukesh agrawal00752532013-05-03 15:46:55 -070046#include "shill/portal_detector.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047#include "shill/property_store_unittest.h"
mukesh agrawal00752532013-05-03 15:46:55 -070048#include "shill/resolver.h"
Chris Masone6515aab2011-10-12 16:19:09 -070049#include "shill/service_under_test.h"
Paul Stewartf2860342014-05-09 14:29:16 -070050#include "shill/testing.h"
Ben Chand6a8b512014-11-18 10:45:15 -080051#include "shill/wifi/mock_wifi_provider.h"
52#include "shill/wifi/mock_wifi_service.h"
53#include "shill/wifi/wifi_service.h"
Ben Chanc3d707d2014-10-31 08:56:05 -070054#include "shill/wimax/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070055
Christopher Wiley1057cd72013-02-28 15:21:29 -080056using base::Bind;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080057using base::FilePath;
Paul Stewart5ad16062013-02-21 18:10:48 -080058using base::ScopedTempDir;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070059using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070060using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061using std::string;
62using std::vector;
63
Chris Masone9be4a9d2011-05-16 15:44:09 -070064namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070065using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070066using ::testing::AnyNumber;
Paul Stewart7de7e022013-08-28 09:42:50 -070067using ::testing::AtLeast;
Gaurav Shah435de2c2011-11-17 19:01:07 -080068using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070069using ::testing::DoAll;
Paul Stewart22ce7652014-10-15 21:26:44 -070070using ::testing::ElementsAre;
mukesh agrawalb94adde2013-08-22 18:17:26 -070071using ::testing::HasSubstr;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070072using ::testing::InSequence;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070073using ::testing::Invoke;
mukesh agrawal784566d2012-08-08 18:32:58 -070074using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070075using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070076using ::testing::NiceMock;
Paul Stewart967eaeb2013-04-25 19:53:07 -070077using ::testing::Ref;
Chris Masone9be4a9d2011-05-16 15:44:09 -070078using ::testing::Return;
Ben Chana55469d2014-01-27 16:35:29 -080079using ::testing::ReturnNull;
Paul Stewartce4ec192012-03-14 12:53:46 -070080using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070081using ::testing::SaveArg;
Daniel Erat0818cca2012-12-14 10:16:21 -080082using ::testing::SetArgumentPointee;
Gaurav Shah435de2c2011-11-17 19:01:07 -080083using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080084using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070085using ::testing::Test;
mukesh agrawal46c27cc2013-07-10 16:39:10 -070086using ::testing::WithArg;
Chris Masone9be4a9d2011-05-16 15:44:09 -070087
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070089 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070090 ManagerTest()
Ben Chancc225ef2014-09-30 13:26:51 -070091 : power_manager_(new MockPowerManager(nullptr, &proxy_factory_)),
92 device_info_(new NiceMock<MockDeviceInfo>(control_interface(),
93 nullptr,
94 nullptr,
95 nullptr)),
Paul Stewart3c504012013-01-17 17:49:58 -080096 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()),
Paul Stewart35eff132013-04-12 12:08:40 -070097 ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()),
Christopher Wiley1057cd72013-02-28 15:21:29 -080098 wifi_provider_(new NiceMock<MockWiFiProvider>()),
Ben Chancc225ef2014-09-30 13:26:51 -070099 crypto_util_proxy_(
100 new NiceMock<MockCryptoUtilProxy>(dispatcher(), glib())) {
Ben Chana55469d2014-01-27 16:35:29 -0800101 ON_CALL(proxy_factory_, CreatePowerManagerProxy(_))
102 .WillByDefault(ReturnNull());
103
Paul Stewart22aa71b2011-09-16 12:15:11 -0700104 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
105 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800106 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700107 manager(),
108 "null0",
109 "addr0",
110 0));
111 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
112 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800113 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700114 manager(),
115 "null1",
116 "addr1",
117 1));
118 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
119 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800120 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700121 manager(),
122 "null2",
123 "addr2",
124 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800125 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
126 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800127 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800128 manager(),
129 "null3",
130 "addr3",
131 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700132 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800133 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800134
135 // Replace the manager's adaptor with a quieter one, and one
136 // we can do EXPECT*() against. Passes ownership.
137 manager()->adaptor_.reset(manager_adaptor_);
Paul Stewart3c504012013-01-17 17:49:58 -0800138
Paul Stewart35eff132013-04-12 12:08:40 -0700139 // Replace the manager's Ethernet EAP provider with our mock.
140 // Passes ownership.
141 manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_);
142
Paul Stewart3c504012013-01-17 17:49:58 -0800143 // Replace the manager's WiFi provider with our mock. Passes
144 // ownership.
145 manager()->wifi_provider_.reset(wifi_provider_);
Christopher Wiley1057cd72013-02-28 15:21:29 -0800146
Paul Stewartb87d22b2013-07-29 11:11:37 -0700147 // Update the manager's map from technology to provider.
148 manager()->UpdateProviderMapping();
149
Christopher Wiley1057cd72013-02-28 15:21:29 -0800150 // Replace the manager's crypto util proxy with our mock. Passes
151 // ownership.
152 manager()->crypto_util_proxy_.reset(crypto_util_proxy_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700153 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700154 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700155
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100156 void SetMetrics(Metrics *metrics) {
157 manager()->set_metrics(metrics);
158 }
159
Paul Stewartfdd16072011-09-16 12:41:35 -0700160 bool IsDeviceRegistered(const DeviceRefPtr &device,
161 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700162 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700163 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700164 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700165 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700166 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700167
Paul Stewarta849a3d2011-11-03 05:54:09 -0700168 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
169 manager->profiles_.push_back(profile);
170 }
171
Paul Stewart63864b62012-11-07 15:10:55 -0800172 void SetRunning(bool running) {
173 manager()->running_ = running;
174 }
175
Paul Stewart75225512012-01-26 22:51:33 -0800176 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
177 return manager->ephemeral_profile_;
178 }
179
Paul Stewart307c2502013-03-23 12:32:10 -0700180 vector<ProfileRefPtr> &GetProfiles(Manager *manager) {
181 return manager->profiles_;
182 }
183
Chris Masone6515aab2011-10-12 16:19:09 -0700184 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
185 Profile::Identifier id("rather", "irrelevant");
Chris Masone6515aab2011-10-12 16:19:09 -0700186 FilePath final_path(storage_path());
187 final_path = final_path.Append("test.profile");
Ben Chancd477322014-10-17 14:19:30 -0700188 std::unique_ptr<KeyFileStore> storage(new KeyFileStore(glib));
Chris Masone6515aab2011-10-12 16:19:09 -0700189 storage->set_path(final_path);
190 if (!storage->Open())
Ben Chancc225ef2014-09-30 13:26:51 -0700191 return nullptr;
Paul Stewart5ad16062013-02-21 18:10:48 -0800192 Profile *profile(new Profile(control_interface(),
Thieu Le5133b712013-02-19 14:47:21 -0800193 metrics(),
Paul Stewart5ad16062013-02-21 18:10:48 -0800194 manager,
195 id,
196 "",
197 false));
198 profile->set_storage(storage.release()); // Passes ownership of "storage".
199 return profile; // Passes onwership of "profile".
Chris Masone6515aab2011-10-12 16:19:09 -0700200 }
201
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700202 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700203 const string &user_identifier,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700204 const string &profile_identifier,
205 const string &service_name) {
206 GLib glib;
207 KeyFileStore store(&glib);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700208 store.set_path(temp_dir->path().Append(
209 base::StringPrintf("%s/%s.profile", user_identifier.c_str(),
210 profile_identifier.c_str())));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700211 return store.Open() &&
212 store.SetString(service_name, "rather", "irrelevant") &&
213 store.Close();
214 }
215
216 Error::Type TestCreateProfile(Manager *manager, const string &name) {
217 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800218 string path;
219 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700220 return error.type();
221 }
222
223 Error::Type TestPopAnyProfile(Manager *manager) {
224 Error error;
225 manager->PopAnyProfile(&error);
226 return error.type();
227 }
228
Paul Stewart307c2502013-03-23 12:32:10 -0700229 Error::Type TestPopAllUserProfiles(Manager *manager) {
230 Error error;
231 manager->PopAllUserProfiles(&error);
232 return error.type();
233 }
234
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700235 Error::Type TestPopProfile(Manager *manager, const string &name) {
236 Error error;
237 manager->PopProfile(name, &error);
238 return error.type();
239 }
240
241 Error::Type TestPushProfile(Manager *manager, const string &name) {
242 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800243 string path;
244 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700245 return error.type();
246 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000247
Paul Stewartf3eced92013-04-17 12:18:22 -0700248 Error::Type TestInsertUserProfile(Manager *manager,
249 const string &name,
250 const string &user_hash) {
251 Error error;
252 string path;
253 manager->InsertUserProfile(name, user_hash, &path, &error);
254 return error.type();
255 }
256
Paul Stewartd2e1c362013-03-03 19:06:07 -0800257 scoped_refptr<MockProfile> AddNamedMockProfileToManager(
258 Manager *manager, const string &name) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700259 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -0800260 new MockProfile(control_interface(), metrics(), manager, ""));
Paul Stewartd2e1c362013-03-03 19:06:07 -0800261 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200262 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700263 AdoptProfile(manager, profile);
Paul Stewartd2e1c362013-03-03 19:06:07 -0800264 return profile;
265 }
266
267 void AddMockProfileToManager(Manager *manager) {
268 AddNamedMockProfileToManager(manager, "/");
Paul Stewartcb3eb892012-06-07 14:24:46 -0700269 }
270
Paul Stewartdfa46052012-06-26 09:44:14 -0700271 void CompleteServiceSort() {
Paul Stewartfbca6bc2014-10-07 08:26:03 -0700272 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewartdfa46052012-06-26 09:44:14 -0700273 dispatcher()->DispatchPendingEvents();
Paul Stewartfbca6bc2014-10-07 08:26:03 -0700274 EXPECT_FALSE(IsSortServicesTaskPending());
275 }
276
277 bool IsSortServicesTaskPending() {
278 return !manager()->sort_services_task_.IsCancelled();
Paul Stewartdfa46052012-06-26 09:44:14 -0700279 }
280
Paul Stewart03e29f72013-09-26 00:49:48 -0700281 void RefreshConnectionState() {
282 manager()->RefreshConnectionState();
283 }
284
Paul Stewart49739c02012-08-08 17:24:03 -0700285 RpcIdentifier GetDefaultServiceRpcIdentifier() {
Ben Chancc225ef2014-09-30 13:26:51 -0700286 return manager()->GetDefaultServiceRpcIdentifier(nullptr);
Paul Stewart49739c02012-08-08 17:24:03 -0700287 }
288
Paul Stewart4d5efb72012-09-17 12:24:34 -0700289 void SetResolver(Resolver *resolver) {
290 manager()->resolver_ = resolver;
291 }
292
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700293 bool SetIgnoredDNSSearchPaths(const string &search_paths, Error *error) {
294 return manager()->SetIgnoredDNSSearchPaths(search_paths, error);
295 }
296
297 bool SetCheckPortalList(const string &check_portal_list, Error *error) {
298 return manager()->SetCheckPortalList(check_portal_list, error);
Paul Stewart4d5efb72012-09-17 12:24:34 -0700299 }
300
301 const string &GetIgnoredDNSSearchPaths() {
302 return manager()->props_.ignored_dns_search_paths;
303 }
304
Paul Stewartd2e1c362013-03-03 19:06:07 -0800305 WiFiServiceRefPtr ReleaseTempMockService() {
306 // Take a reference to hold during this function.
307 WiFiServiceRefPtr temp_service = temp_mock_service_;
Ben Chancc225ef2014-09-30 13:26:51 -0700308 temp_mock_service_ = nullptr;
Paul Stewartd2e1c362013-03-03 19:06:07 -0800309 return temp_service;
310 }
311
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700312 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000313 typedef scoped_refptr<MockService> MockServiceRefPtr;
314
Darin Petkova5e07ef2012-07-09 14:27:57 +0200315 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
316 public:
317 ServiceWatcher() {}
318 virtual ~ServiceWatcher() {}
319
320 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
321
322 private:
323 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
324 };
325
Darin Petkov3ec55342012-09-28 14:04:44 +0200326 class TerminationActionTest :
327 public base::SupportsWeakPtr<TerminationActionTest> {
328 public:
329 static const char kActionName[];
330
Ben Chancc225ef2014-09-30 13:26:51 -0700331 TerminationActionTest() : manager_(nullptr) {}
Darin Petkov3ec55342012-09-28 14:04:44 +0200332 virtual ~TerminationActionTest() {}
333
334 MOCK_METHOD1(Done, void(const Error &error));
335
336 void Action() {
337 manager_->TerminationActionComplete("action");
338 }
339
340 void set_manager(Manager *manager) { manager_ = manager; }
341
342 private:
343 Manager *manager_;
344 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
345 };
346
Christopher Wiley1057cd72013-02-28 15:21:29 -0800347 class DestinationVerificationTest :
348 public base::SupportsWeakPtr<DestinationVerificationTest> {
349 public:
350 DestinationVerificationTest() {}
351 virtual ~DestinationVerificationTest() {}
352
353 MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag));
354 MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result,
355 const string &value));
356 private:
357 DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest);
358 };
359
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700360 class DisableTechnologyReplyHandler :
361 public base::SupportsWeakPtr<DisableTechnologyReplyHandler> {
362 public:
363 DisableTechnologyReplyHandler() {}
364 virtual ~DisableTechnologyReplyHandler() {}
365
366 MOCK_METHOD1(ReportResult, void(const Error &));
367
368 private:
369 DISALLOW_COPY_AND_ASSIGN(DisableTechnologyReplyHandler);
370 };
371
Daniel Eratfac09532014-04-17 20:25:59 -0700372 void SetSuspending(bool suspending) {
373 power_manager_->suspending_ = suspending;
Darin Petkovca621542012-07-25 14:25:56 +0200374 }
375
376 void SetPowerManager() {
377 manager()->set_power_manager(power_manager_.release());
378 }
379
Darin Petkov3ec55342012-09-28 14:04:44 +0200380 HookTable *GetTerminationActions() {
381 return &manager()->termination_actions_;
382 }
383
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700384 void OnSuspendImminent() {
385 manager()->OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +0200386 }
387
Samuel Tan68b73d22014-10-28 17:00:56 -0700388 void OnDarkSuspendImminent() {
389 manager()->OnDarkSuspendImminent();
390 }
391
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700392 void OnSuspendDone() {
393 manager()->OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -0700394 }
395
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700396 void OnSuspendActionsComplete(const Error &error) {
397 manager()->OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +0200398 }
399
Paul Stewartbfb82552012-10-24 16:48:48 -0700400 vector<string> EnumerateAvailableServices() {
Ben Chancc225ef2014-09-30 13:26:51 -0700401 return manager()->EnumerateAvailableServices(nullptr);
Paul Stewartbfb82552012-10-24 16:48:48 -0700402 }
403
404 vector<string> EnumerateWatchedServices() {
Ben Chancc225ef2014-09-30 13:26:51 -0700405 return manager()->EnumerateWatchedServices(nullptr);
Paul Stewartbfb82552012-10-24 16:48:48 -0700406 }
407
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000408 MockServiceRefPtr MakeAutoConnectableService() {
409 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
410 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800411 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000412 manager());
Paul Stewart22ce7652014-10-15 21:26:44 -0700413 service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700414 service->SetConnectable(true);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000415 return service;
416 }
417
Paul Stewart35eff132013-04-12 12:08:40 -0700418 void SetEapProviderService(const ServiceRefPtr &service) {
419 ethernet_eap_provider_->set_service(service);
420 }
421
Paul Stewart22ce7652014-10-15 21:26:44 -0700422 const std::vector<Technology::Identifier> &GetTechnologyOrder() {
423 return manager()->technology_order_;
424 }
425
Ben Chana55469d2014-01-27 16:35:29 -0800426 NiceMock<MockProxyFactory> proxy_factory_;
Ben Chancd477322014-10-17 14:19:30 -0700427 std::unique_ptr<MockPowerManager> power_manager_;
Ben Chane2ee5e02014-09-19 19:29:42 -0700428 vector<scoped_refptr<MockDevice>> mock_devices_;
Ben Chancd477322014-10-17 14:19:30 -0700429 std::unique_ptr<MockDeviceInfo> device_info_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800430
Paul Stewartd2e1c362013-03-03 19:06:07 -0800431 // This service is held for the manager, and given ownership in a mock
432 // function. This ensures that when the Manager takes ownership, there
433 // is only one reference left.
434 scoped_refptr<MockWiFiService> temp_mock_service_;
435
Paul Stewart3c504012013-01-17 17:49:58 -0800436 // These pointers are owned by the manager, and only tracked here for
437 // EXPECT*()
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800438 ManagerMockAdaptor *manager_adaptor_;
Paul Stewart35eff132013-04-12 12:08:40 -0700439 MockEthernetEapProvider *ethernet_eap_provider_;
Paul Stewart3c504012013-01-17 17:49:58 -0800440 MockWiFiProvider *wifi_provider_;
Christopher Wiley1057cd72013-02-28 15:21:29 -0800441 MockCryptoUtilProxy *crypto_util_proxy_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700442};
443
Darin Petkov3ec55342012-09-28 14:04:44 +0200444const char ManagerTest::TerminationActionTest::kActionName[] = "action";
445
Paul Stewart22aa71b2011-09-16 12:15:11 -0700446bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700447 if (!manager()->sort_services_task_.IsCancelled()) {
448 manager()->SortServicesTask();
449 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700450 return (svc0.get() == manager()->services_[0].get() &&
451 svc1.get() == manager()->services_[1].get());
452}
453
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700454void SetErrorPermissionDenied(Error *error) {
455 error->Populate(Error::kPermissionDenied);
456}
457
Arman Uguray2f352e62013-08-28 19:12:53 -0700458void SetErrorSuccess(Error *error) {
459 error->Reset();
460}
461
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700462TEST_F(ManagerTest, Contains) {
Ben Chan923a5022013-09-20 11:23:23 -0700463 EXPECT_TRUE(manager()->store().Contains(kStateProperty));
mukesh agrawalde29fa82011-09-16 16:16:36 -0700464 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700465}
466
Chris Masone9be4a9d2011-05-16 15:44:09 -0700467TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700468 ON_CALL(*mock_devices_[0].get(), technology())
469 .WillByDefault(Return(Technology::kEthernet));
470 ON_CALL(*mock_devices_[1].get(), technology())
471 .WillByDefault(Return(Technology::kWifi));
472 ON_CALL(*mock_devices_[2].get(), technology())
473 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700474
Paul Stewart22aa71b2011-09-16 12:15:11 -0700475 manager()->RegisterDevice(mock_devices_[0]);
476 manager()->RegisterDevice(mock_devices_[1]);
477 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700478
Paul Stewart22aa71b2011-09-16 12:15:11 -0700479 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
480 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
481 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700482}
483
Paul Stewarta41e38d2011-11-11 07:47:29 -0800484TEST_F(ManagerTest, DeviceRegistrationAndStart) {
485 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500486 mock_devices_[0]->enabled_persistent_ = true;
487 mock_devices_[1]->enabled_persistent_ = false;
488 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800489 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500490 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800491 .Times(0);
492 manager()->RegisterDevice(mock_devices_[0]);
493 manager()->RegisterDevice(mock_devices_[1]);
494}
495
496TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
Thieu Le5133b712013-02-19 14:47:21 -0800497 MockProfile *profile =
498 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewarta41e38d2011-11-11 07:47:29 -0800499 DeviceRefPtr device_ref(mock_devices_[0].get());
500 AdoptProfile(manager(), profile); // Passes ownership.
501 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200502 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800503 manager()->RegisterDevice(mock_devices_[0]);
504}
505
Chris Masone9be4a9d2011-05-16 15:44:09 -0700506TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700507 ON_CALL(*mock_devices_[0].get(), technology())
508 .WillByDefault(Return(Technology::kEthernet));
509 ON_CALL(*mock_devices_[1].get(), technology())
510 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700511
Gaurav Shah435de2c2011-11-17 19:01:07 -0800512 manager()->RegisterDevice(mock_devices_[0]);
513 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700514
Paul Stewart22aa71b2011-09-16 12:15:11 -0700515 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
516 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700517
Thieu Le5133b712013-02-19 14:47:21 -0800518 MockProfile *profile =
519 new MockProfile(control_interface(), metrics(), manager(), "");
Paul Stewart212d60f2012-07-12 10:59:13 -0700520 AdoptProfile(manager(), profile); // Passes ownership.
521
Eric Shienbrood9a245532012-03-07 14:20:39 -0500522 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700523 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800524 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700525 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700526
Eric Shienbrood9a245532012-03-07 14:20:39 -0500527 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700528 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800529 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700530 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700531}
532
533TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700534 // It's much easier and safer to use a real GLib for this test.
535 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700536 Manager manager(control_interface(),
537 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800538 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700539 &glib,
540 run_path(),
541 storage_path(),
542 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700543 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
544 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700545 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700546
Chris Masone9be4a9d2011-05-16 15:44:09 -0700547 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700548 new NiceMock<MockService>(control_interface(),
549 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800550 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700551 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700552 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700553 new NiceMock<MockService>(control_interface(),
554 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800555 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700556 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700557
Darin Petkov457728b2013-01-09 09:49:08 +0100558 string service1_name(mock_service->unique_name());
559 string service2_name(mock_service2->unique_name());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700560
561 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
562 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700563 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700564 .WillRepeatedly(Return(service2_name));
Paul Stewartee6b3d72013-07-12 16:07:51 -0700565 // TODO(quiche): make this EXPECT_CALL work (crbug.com/203247)
Chris Masone9d779932011-08-25 16:33:41 -0700566 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
Ben Chan923a5022013-09-20 11:23:23 -0700567 // EmitRpcIdentifierArrayChanged(kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700568
Chris Masone9d779932011-08-25 16:33:41 -0700569 manager.RegisterService(mock_service);
570 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700571
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800572 Error error;
573 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700574 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700575 EXPECT_EQ(2, ids.size());
576 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
577 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700578
Ben Chancc225ef2014-09-30 13:26:51 -0700579 EXPECT_NE(nullptr, manager.FindService(service1_name).get());
580 EXPECT_NE(nullptr, manager.FindService(service2_name).get());
Chris Masone9d779932011-08-25 16:33:41 -0700581
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700582 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700583 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700584}
585
Chris Masone6515aab2011-10-12 16:19:09 -0700586TEST_F(ManagerTest, RegisterKnownService) {
587 // It's much easier and safer to use a real GLib for this test.
588 GLib glib;
589 Manager manager(control_interface(),
590 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800591 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700592 &glib,
593 run_path(),
594 storage_path(),
595 string());
596 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
597 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700598 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700599 {
600 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
601 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800602 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700603 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700604 ASSERT_TRUE(profile->AdoptService(service1));
605 ASSERT_TRUE(profile->ContainsService(service1));
606 } // Force destruction of service1.
607
608 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
609 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800610 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700611 &manager));
612 manager.RegisterService(service2);
613 EXPECT_EQ(service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700614
615 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700616 manager.Stop();
617}
618
619TEST_F(ManagerTest, RegisterUnknownService) {
620 // It's much easier and safer to use a real GLib for this test.
621 GLib glib;
622 Manager manager(control_interface(),
623 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800624 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700625 &glib,
626 run_path(),
627 storage_path(),
628 string());
629 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
630 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700631 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700632 {
633 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
634 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800635 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700636 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700637 ASSERT_TRUE(profile->AdoptService(service1));
638 ASSERT_TRUE(profile->ContainsService(service1));
639 } // Force destruction of service1.
640 scoped_refptr<MockService> mock_service2(
641 new NiceMock<MockService>(control_interface(),
642 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800643 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700644 &manager));
645 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
Darin Petkov457728b2013-01-09 09:49:08 +0100646 .WillRepeatedly(Return(mock_service2->unique_name()));
Chris Masone6515aab2011-10-12 16:19:09 -0700647 manager.RegisterService(mock_service2);
648 EXPECT_NE(mock_service2->profile().get(), profile.get());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700649
650 manager.set_power_manager(power_manager_.release());
Chris Masone6515aab2011-10-12 16:19:09 -0700651 manager.Stop();
652}
653
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000654TEST_F(ManagerTest, DeregisterUnregisteredService) {
655 // WiFi assumes that it can deregister a service that is not
656 // registered. (E.g. a hidden service can be deregistered when it
657 // loses its last endpoint, and again when WiFi is Stop()-ed.)
658 //
659 // So test that doing so doesn't cause a crash.
660 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
661 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800662 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000663 manager());
664 manager()->DeregisterService(service);
665}
666
Chris Masonea8a2c252011-06-27 22:16:30 -0700667TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700668 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700669 map<string, ::DBus::Variant> props;
670 Error error(Error::kInvalidProperty, "");
671 {
672 ::DBus::Error dbus_error;
673 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700674 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -0700675 kCheckPortalListProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700676 expected,
677 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700678 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700679 ASSERT_FALSE(props.find(kCheckPortalListProperty) == props.end());
680 EXPECT_EQ(props[kCheckPortalListProperty].reader().get_string(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700681 expected);
682 }
683 {
684 ::DBus::Error dbus_error;
685 bool expected = true;
Ben Chan923a5022013-09-20 11:23:23 -0700686 manager()->mutable_store()->SetBoolProperty(kOfflineModeProperty,
mukesh agrawalde29fa82011-09-16 16:16:36 -0700687 expected,
688 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700689 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700690 ASSERT_FALSE(props.find(kOfflineModeProperty) == props.end());
691 EXPECT_EQ(props[kOfflineModeProperty].reader().get_bool(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700692 expected);
693 }
694}
695
Chris Masone3c3f6a12011-07-01 10:01:41 -0700696TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700697 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800698 manager()->RegisterDevice(mock_devices_[0]);
699 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700700 {
701 map<string, ::DBus::Variant> props;
702 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700703 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Ben Chan923a5022013-09-20 11:23:23 -0700704 ASSERT_FALSE(props.find(kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700705 vector < ::DBus::Path> devices =
Ben Chane2ee5e02014-09-19 19:29:42 -0700706 props[kDevicesProperty].operator vector<::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700707 EXPECT_EQ(2, devices.size());
708 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700709}
710
mukesh agrawal2366eed2012-03-20 18:21:50 -0700711TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700712 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700713 map<string, ::DBus::Variant> props;
714 ::DBus::Error dbus_error;
715 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
716 map<string, ::DBus::Variant>::const_iterator prop =
Ben Chan923a5022013-09-20 11:23:23 -0700717 props.find(kServicesProperty);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700718 ASSERT_FALSE(prop == props.end());
719 const ::DBus::Variant &variant = prop->second;
720 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
721}
722
Chris Masone6791a432011-07-12 13:23:19 -0700723TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700724 Manager manager(control_interface(),
725 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800726 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700727 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700728 run_path(),
729 storage_path(),
730 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700731 scoped_refptr<MockService> s2(new MockService(control_interface(),
732 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800733 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700734 &manager));
735 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700736 {
Chris Masone6515aab2011-10-12 16:19:09 -0700737 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700738 ProfileRefPtr profile(
Thieu Le5133b712013-02-19 14:47:21 -0800739 new Profile(control_interface(), metrics(), &manager, id, "", false));
Chris Masoneb9c00592011-10-06 13:10:39 -0700740 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700741 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700742 .WillRepeatedly(Return(true));
743 EXPECT_CALL(*storage, Flush())
744 .Times(AnyNumber())
745 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700746 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700747 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700748 }
Chris Masone6515aab2011-10-12 16:19:09 -0700749 // Create a profile that already has |s2| in it.
Thieu Le5133b712013-02-19 14:47:21 -0800750 ProfileRefPtr profile(
751 new EphemeralProfile(control_interface(), metrics(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700752 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700753
Chris Masone6515aab2011-10-12 16:19:09 -0700754 // Now, move the Service |s2| to another profile.
755 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
756 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700757
758 // Force destruction of the original Profile, to ensure that the Service
759 // is kept alive and populated with data.
Ben Chancc225ef2014-09-30 13:26:51 -0700760 profile = nullptr;
Chris Masone6515aab2011-10-12 16:19:09 -0700761 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700762 manager.set_power_manager(power_manager_.release());
Chris Masone9d779932011-08-25 16:33:41 -0700763 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700764}
765
Paul Stewart7f61e522012-03-22 11:13:45 -0700766TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
767 scoped_refptr<MockProfile> mock_profile(
Thieu Le5133b712013-02-19 14:47:21 -0800768 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -0700769 const string kProfileName("profile0");
770 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
771 .WillRepeatedly(Return(kProfileName));
772 AdoptProfile(manager(), mock_profile);
773
774 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
775 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
776 EXPECT_EQ(mock_profile.get(), profile.get());
777}
778
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800779TEST_F(ManagerTest, SetProfileForService) {
780 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -0800781 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800782 string profile_name0("profile0");
783 EXPECT_CALL(*profile0, GetRpcIdentifier())
784 .WillRepeatedly(Return(profile_name0));
785 AdoptProfile(manager(), profile0);
786 scoped_refptr<MockService> service(new MockService(control_interface(),
787 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800788 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800789 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700790 EXPECT_FALSE(manager()->HasService(service));
791 {
792 Error error;
793 EXPECT_CALL(*profile0, AdoptService(_))
794 .WillOnce(Return(true));
795 // Expect that setting the profile of a service that does not already
796 // have one assigned does not cause a crash.
797 manager()->SetProfileForService(service, "profile0", &error);
798 EXPECT_TRUE(error.IsSuccess());
799 }
800
801 // The service should be registered as a side-effect of the profile being
802 // set for this service.
803 EXPECT_TRUE(manager()->HasService(service));
804
805 // Since we have mocked Profile::AdoptServie() above, the service's
806 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800807 service->set_profile(profile0);
808
809 {
810 Error error;
811 manager()->SetProfileForService(service, "foo", &error);
812 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700813 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800814 }
815
816 {
817 Error error;
818 manager()->SetProfileForService(service, profile_name0, &error);
819 EXPECT_EQ(Error::kInvalidArguments, error.type());
820 EXPECT_EQ("Service is already connected to this profile", error.message());
821 }
822
823 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -0800824 new MockProfile(control_interface(), metrics(), manager(), ""));
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800825 string profile_name1("profile1");
826 EXPECT_CALL(*profile1, GetRpcIdentifier())
827 .WillRepeatedly(Return(profile_name1));
828 AdoptProfile(manager(), profile1);
829
830 {
831 Error error;
832 EXPECT_CALL(*profile1, AdoptService(_))
833 .WillOnce(Return(true));
834 EXPECT_CALL(*profile0, AbandonService(_))
835 .WillOnce(Return(true));
836 manager()->SetProfileForService(service, profile_name1, &error);
837 EXPECT_TRUE(error.IsSuccess());
838 }
839}
840
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700841TEST_F(ManagerTest, CreateProfile) {
842 // It's much easier to use real Glib here since we want the storage
843 // side-effects.
844 GLib glib;
845 ScopedTempDir temp_dir;
846 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
847
848 Manager manager(control_interface(),
849 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800850 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700851 &glib,
852 run_path(),
853 storage_path(),
854 temp_dir.path().value());
855
856 // Invalid name should be rejected.
857 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
858
Paul Stewartd0a3b812012-03-28 22:48:22 -0700859 // A profile with invalid characters in it should similarly be rejected.
860 EXPECT_EQ(Error::kInvalidArguments,
861 TestCreateProfile(&manager, "valid_profile"));
862
863 // We should be able to create a machine profile.
864 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700865
Gary Morainb672d352012-04-25 09:19:06 -0700866 // We should succeed in creating a valid user profile. Verify the returned
867 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700868 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700869 {
870 Error error;
871 string path;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700872 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Gary Morainb672d352012-04-25 09:19:06 -0700873 manager.CreateProfile(kProfile, &path, &error);
874 EXPECT_EQ(Error::kSuccess, error.type());
875 EXPECT_EQ("/profile_rpc", path);
876 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700877
878 // We should fail in creating it a second time (already exists).
879 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
880}
881
882TEST_F(ManagerTest, PushPopProfile) {
883 // It's much easier to use real Glib in creating a Manager for this
884 // test here since we want the storage side-effects.
885 GLib glib;
886 ScopedTempDir temp_dir;
887 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
888 Manager manager(control_interface(),
889 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800890 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700891 &glib,
892 run_path(),
893 storage_path(),
894 temp_dir.path().value());
mukesh agrawal92496a42014-04-08 16:04:43 -0700895 vector<ProfileRefPtr> &profiles = GetProfiles(&manager);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700896
897 // Pushing an invalid profile should fail.
898 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
899
mukesh agrawal92496a42014-04-08 16:04:43 -0700900 // Create and push a default profile. Should succeed.
901 const char kDefaultProfile0[] = "default";
902 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kDefaultProfile0));
903 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kDefaultProfile0));
904 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kDefaultProfile0));
905
906 // Pushing a default profile that does not exist on disk will _not_
907 // fail, because we'll use temporary storage for it.
908 const char kMissingDefaultProfile[] = "missingdefault";
909 EXPECT_EQ(Error::kSuccess,
910 TestPushProfile(&manager, kMissingDefaultProfile));
911 EXPECT_EQ(1, profiles.size());
912 EXPECT_EQ(Error::kSuccess,
913 TestPopProfile(&manager, kMissingDefaultProfile));
914 EXPECT_EQ(0, profiles.size());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700915
916 const char kProfile0[] = "~user/profile0";
917 const char kProfile1[] = "~user/profile1";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700918 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700919
920 // Create a couple of profiles.
921 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
922 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
923
924 // Push these profiles on the stack.
925 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
926 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
927
928 // Pushing a profile a second time should fail.
929 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
930 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
931
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800932 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700933 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800934 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700935
936 // Make sure a profile name that doesn't exist fails.
937 const char kProfile2Id[] = "profile2";
938 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
939 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
940
941 // Create a new service, with a specific storage name.
942 scoped_refptr<MockService> service(
943 new NiceMock<MockService>(control_interface(),
944 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800945 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700946 &manager));
947 const char kServiceName[] = "service_storage_name";
948 EXPECT_CALL(*service.get(), GetStorageIdentifier())
949 .WillRepeatedly(Return(kServiceName));
950 EXPECT_CALL(*service.get(), Load(_))
951 .WillRepeatedly(Return(true));
952
953 // Add this service to the manager -- it should end up in the ephemeral
954 // profile.
955 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800956 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700957
958 // Create storage for a profile that contains the service storage name.
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700959 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile2Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700960 kServiceName));
961
962 // When we push the profile, the service should move away from the
963 // ephemeral profile to this new profile since it has an entry for
964 // this service.
Paul Stewart2eee6132014-05-09 13:33:26 -0700965 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700966 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800967 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700968 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
969
970 // Insert another profile that should supersede ownership of the service.
971 const char kProfile3Id[] = "profile3";
972 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
mukesh agrawal0a59a5a2014-04-24 19:10:46 -0700973 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, "user", kProfile3Id,
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700974 kServiceName));
Paul Stewart2eee6132014-05-09 13:33:26 -0700975 // We don't verify this expectation inline, since this would clear other
976 // recurring expectations on the service.
977 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700978 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
979 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
980
981 // Popping an invalid profile name should fail.
982 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
983
984 // Popping an profile that is not at the top of the stack should fail.
985 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
986
987 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700988 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700989 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
990
991 // Moreover the service should have switched profiles to profile 2.
992 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
993
994 // Popping the top profile should succeed.
Paul Stewart2eee6132014-05-09 13:33:26 -0700995 EXPECT_CALL(*service, ClearExplicitlyDisconnected());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700996 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
997
998 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800999 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001000
mukesh agrawal92496a42014-04-08 16:04:43 -07001001 // Pop the remaining two profiles off the stack.
Paul Stewart2eee6132014-05-09 13:33:26 -07001002 EXPECT_CALL(*service, ClearExplicitlyDisconnected()).Times(2);
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001003 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1004 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
Paul Stewart2eee6132014-05-09 13:33:26 -07001005 Mock::VerifyAndClearExpectations(service);
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001006
1007 // Next pop should fail with "stack is empty".
1008 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -07001009
1010 const char kMachineProfile0[] = "machineprofile0";
1011 const char kMachineProfile1[] = "machineprofile1";
1012 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
1013 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
1014
1015 // Should be able to push a machine profile.
1016 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
1017
1018 // Should be able to push a user profile atop a machine profile.
1019 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1020
1021 // Pushing a system-wide profile on top of a user profile should fail.
1022 EXPECT_EQ(Error::kInvalidArguments,
1023 TestPushProfile(&manager, kMachineProfile1));
1024
1025 // However if we pop the user profile, we should be able stack another
1026 // machine profile on.
1027 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1028 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001029
1030 // Add two user profiles to the top of the stack.
1031 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1032 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
Paul Stewart307c2502013-03-23 12:32:10 -07001033 EXPECT_EQ(4, profiles.size());
1034
1035 // PopAllUserProfiles should remove both user profiles, leaving the two
1036 // machine profiles.
1037 EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager));
1038 EXPECT_EQ(2, profiles.size());
1039 EXPECT_TRUE(profiles[0]->GetUser().empty());
1040 EXPECT_TRUE(profiles[1]->GetUser().empty());
Paul Stewartf3eced92013-04-17 12:18:22 -07001041
1042 // Use InsertUserProfile() instead. Although a machine profile is valid
1043 // in this state, it cannot be added via InsertUserProfile.
1044 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1));
1045 EXPECT_EQ(Error::kInvalidArguments,
1046 TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1"));
1047 const char kUserHash0[] = "userhash0";
1048 const char kUserHash1[] = "userhash1";
1049 EXPECT_EQ(Error::kSuccess,
1050 TestInsertUserProfile(&manager, kProfile0, kUserHash0));
1051 EXPECT_EQ(Error::kSuccess,
1052 TestInsertUserProfile(&manager, kProfile1, kUserHash1));
1053 EXPECT_EQ(3, profiles.size());
1054 EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash());
1055 EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash());
Paul Stewart5dc40aa2011-10-28 19:43:43 -07001056}
1057
Paul Stewarte73d05c2012-03-29 16:26:05 -07001058TEST_F(ManagerTest, RemoveProfile) {
1059 // It's much easier to use real Glib in creating a Manager for this
1060 // test here since we want the storage side-effects.
1061 GLib glib;
1062 ScopedTempDir temp_dir;
1063 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1064 Manager manager(control_interface(),
1065 dispatcher(),
1066 metrics(),
1067 &glib,
1068 run_path(),
1069 storage_path(),
1070 temp_dir.path().value());
1071
1072 const char kProfile0[] = "profile0";
1073 FilePath profile_path(
1074 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1075
1076 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001077 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001078
1079 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1080
1081 // Remove should fail since the profile is still on the stack.
1082 {
1083 Error error;
1084 manager.RemoveProfile(kProfile0, &error);
1085 EXPECT_EQ(Error::kInvalidArguments, error.type());
1086 }
1087
1088 // Profile path should still exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001089 EXPECT_TRUE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001090
1091 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
1092
1093 // This should succeed now that the profile is off the stack.
1094 {
1095 Error error;
1096 manager.RemoveProfile(kProfile0, &error);
1097 EXPECT_EQ(Error::kSuccess, error.type());
1098 }
1099
1100 // Profile path should no longer exist.
Ben Chana0ddf462014-02-06 11:32:42 -08001101 EXPECT_FALSE(base::PathExists(profile_path));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001102
Ben Chana0ddf462014-02-06 11:32:42 -08001103 // Another remove succeeds, due to a foible in base::DeleteFile --
Paul Stewarte73d05c2012-03-29 16:26:05 -07001104 // it is not an error to delete a file that does not exist.
1105 {
1106 Error error;
1107 manager.RemoveProfile(kProfile0, &error);
1108 EXPECT_EQ(Error::kSuccess, error.type());
1109 }
1110
1111 // Let's create an error case that will "work". Create a non-empty
1112 // directory in the place of the profile pathname.
Ben Chana0ddf462014-02-06 11:32:42 -08001113 ASSERT_TRUE(base::CreateDirectory(profile_path.Append("foo")));
Paul Stewarte73d05c2012-03-29 16:26:05 -07001114 {
1115 Error error;
1116 manager.RemoveProfile(kProfile0, &error);
1117 EXPECT_EQ(Error::kOperationFailed, error.type());
1118 }
1119}
1120
Paul Stewartd3d03882013-08-29 15:43:42 -07001121TEST_F(ManagerTest, RemoveService) {
1122 MockServiceRefPtr mock_service(
1123 new NiceMock<MockService>(control_interface(),
1124 dispatcher(),
1125 metrics(),
1126 manager()));
1127
1128 // Used in expectations which cannot accept a mock refptr.
1129 const ServiceRefPtr &service = mock_service;
1130
1131 manager()->RegisterService(service);
1132 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1133
1134 scoped_refptr<MockProfile> profile(
1135 new StrictMock<MockProfile>(
1136 control_interface(), metrics(), manager(), ""));
1137 AdoptProfile(manager(), profile);
1138
1139 // If service is ephemeral, it should be unloaded and left ephemeral.
1140 EXPECT_CALL(*profile, AbandonService(service)).Times(0);
1141 EXPECT_CALL(*profile, ConfigureService(service)).Times(0);
1142 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(false));
1143 manager()->RemoveService(service);
1144 Mock::VerifyAndClearExpectations(mock_service);
1145 Mock::VerifyAndClearExpectations(profile);
1146 EXPECT_EQ(GetEphemeralProfile(manager()), service->profile().get());
1147 EXPECT_TRUE(manager()->HasService(service)); // Since Unload() was false.
1148
1149 // If service is not ephemeral and the Manager finds a profile to assign
1150 // the service to, the service should be re-parented. Note that since we
1151 // are using a MockProfile, ConfigureService() never actually changes the
1152 // Service's profile.
1153 service->set_profile(profile);
1154 EXPECT_CALL(*profile, AbandonService(service));
1155 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(true));
1156 EXPECT_CALL(*mock_service, Unload()).Times(0);
1157 manager()->RemoveService(service);
1158 Mock::VerifyAndClearExpectations(mock_service);
1159 Mock::VerifyAndClearExpectations(profile);
1160 EXPECT_TRUE(manager()->HasService(service));
1161 EXPECT_EQ(profile.get(), service->profile().get());
1162
1163 // If service becomes ephemeral since there is no profile to support it,
1164 // it should be unloaded.
1165 EXPECT_CALL(*profile, AbandonService(service));
1166 EXPECT_CALL(*profile, ConfigureService(service)).WillOnce(Return(false));
1167 EXPECT_CALL(*mock_service, Unload()).WillOnce(Return(true));
1168 manager()->RemoveService(service);
1169 EXPECT_FALSE(manager()->HasService(service));
1170}
1171
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001172TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
1173 // It's much easier to use real Glib in creating a Manager for this
1174 // test here since we want the storage side-effects.
1175 GLib glib;
1176 ScopedTempDir temp_dir;
1177 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1178 Manager manager(control_interface(),
1179 dispatcher(),
1180 metrics(),
1181 &glib,
1182 run_path(),
1183 storage_path(),
1184 temp_dir.path().value());
1185
1186 const char kProfile0[] = "profile0";
1187 FilePath profile_path(
1188 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
1189
1190 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
Ben Chana0ddf462014-02-06 11:32:42 -08001191 ASSERT_TRUE(base::PathExists(profile_path));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001192 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1193
1194 // Ensure that even if the backing filestore is removed, we still can't
1195 // create a profile twice.
Ben Chana0ddf462014-02-06 11:32:42 -08001196 ASSERT_TRUE(base::DeleteFile(profile_path, false));
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001197 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1198}
1199
Paul Stewart75225512012-01-26 22:51:33 -08001200TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1201 MockServiceRefPtr s_not_in_profile(
1202 new NiceMock<MockService>(control_interface(),
1203 dispatcher(),
1204 metrics(),
1205 manager()));
1206 MockServiceRefPtr s_not_in_group(
1207 new NiceMock<MockService>(control_interface(),
1208 dispatcher(),
1209 metrics(),
1210 manager()));
1211 MockServiceRefPtr s_configure_fail(
1212 new NiceMock<MockService>(control_interface(),
1213 dispatcher(),
1214 metrics(),
1215 manager()));
1216 MockServiceRefPtr s_configure_succeed(
1217 new NiceMock<MockService>(control_interface(),
1218 dispatcher(),
1219 metrics(),
1220 manager()));
1221
1222 string entry_name("entry_name");
1223 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1224 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1225 .WillRepeatedly(Return("not_entry_name"));
1226 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1227 .WillRepeatedly(Return(entry_name));
1228 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1229 .WillRepeatedly(Return(entry_name));
1230
1231 manager()->RegisterService(s_not_in_profile);
1232 manager()->RegisterService(s_not_in_group);
1233 manager()->RegisterService(s_configure_fail);
1234 manager()->RegisterService(s_configure_succeed);
1235
1236 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001237 new StrictMock<MockProfile>(
1238 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001239 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001240 new StrictMock<MockProfile>(
1241 control_interface(), metrics(), manager(), ""));
Paul Stewart75225512012-01-26 22:51:33 -08001242
1243 s_not_in_group->set_profile(profile1);
1244 s_configure_fail->set_profile(profile1);
1245 s_configure_succeed->set_profile(profile1);
1246
1247 AdoptProfile(manager(), profile0);
1248 AdoptProfile(manager(), profile1);
1249
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001250 CompleteServiceSort();
1251
Paul Stewart75225512012-01-26 22:51:33 -08001252 // No services are a member of this profile.
1253 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001254 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001255
1256 // No services that are members of this profile have this entry name.
1257 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001258 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001259
1260 // Only services that are members of the profile and group will be abandoned.
1261 EXPECT_CALL(*profile1.get(),
1262 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1263 EXPECT_CALL(*profile1.get(),
1264 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1265 EXPECT_CALL(*profile1.get(),
1266 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1267 .WillOnce(Return(true));
1268 EXPECT_CALL(*profile1.get(),
1269 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1270 .WillOnce(Return(true));
1271
1272 // Never allow services to re-join profile1.
1273 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1274 .WillRepeatedly(Return(false));
1275
1276 // Only allow one of the members of the profile and group to successfully
1277 // join profile0.
1278 EXPECT_CALL(*profile0.get(),
1279 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1280 EXPECT_CALL(*profile0.get(),
1281 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1282 EXPECT_CALL(*profile0.get(),
1283 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1284 .WillOnce(Return(false));
1285 EXPECT_CALL(*profile0.get(),
1286 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1287 .WillOnce(Return(true));
1288
1289 // Expect the failed-to-configure service to have Unload() called on it.
1290 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1291 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1292 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1293 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1294
1295 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001296 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewart75225512012-01-26 22:51:33 -08001297
1298 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1299 EXPECT_EQ(profile1, s_not_in_group->profile());
1300 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1301
1302 // Since we are using a MockProfile, the profile does not actually change,
1303 // since ConfigureService was not actually called on the service.
1304 EXPECT_EQ(profile1, s_configure_succeed->profile());
1305}
1306
Paul Stewart65512e12012-03-26 18:01:08 -07001307TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1308 MockServiceRefPtr s_will_remove0(
1309 new NiceMock<MockService>(control_interface(),
1310 dispatcher(),
1311 metrics(),
1312 manager()));
1313 MockServiceRefPtr s_will_remove1(
1314 new NiceMock<MockService>(control_interface(),
1315 dispatcher(),
1316 metrics(),
1317 manager()));
1318 MockServiceRefPtr s_will_not_remove0(
1319 new NiceMock<MockService>(control_interface(),
1320 dispatcher(),
1321 metrics(),
1322 manager()));
1323 MockServiceRefPtr s_will_not_remove1(
1324 new NiceMock<MockService>(control_interface(),
1325 dispatcher(),
1326 metrics(),
1327 manager()));
1328
Ben Chancc225ef2014-09-30 13:26:51 -07001329 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(nullptr))
Paul Stewart65512e12012-03-26 18:01:08 -07001330 .Times(4); // Once for each registration.
1331
1332 string entry_name("entry_name");
1333 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1334 .WillRepeatedly(Return(entry_name));
1335 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1336 .WillRepeatedly(Return(entry_name));
1337 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1338 .WillRepeatedly(Return(entry_name));
1339 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1340 .WillRepeatedly(Return(entry_name));
1341
1342 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001343 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001344 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001345 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001346 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001347 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001348 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001349 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001350
1351 // One for each service added above.
1352 ASSERT_EQ(4, manager()->services_.size());
1353
1354 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001355 new StrictMock<MockProfile>(
1356 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001357
1358 s_will_remove0->set_profile(profile);
1359 s_will_remove1->set_profile(profile);
1360 s_will_not_remove0->set_profile(profile);
1361 s_will_not_remove1->set_profile(profile);
1362
1363 AdoptProfile(manager(), profile);
1364
1365 // Deny any of the services re-entry to the profile.
1366 EXPECT_CALL(*profile, ConfigureService(_))
1367 .WillRepeatedly(Return(false));
1368
1369 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1370 .WillOnce(Return(true));
1371 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1372 .WillOnce(Return(true));
1373 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1374 .WillOnce(Return(true));
1375 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1376 .WillOnce(Return(true));
1377
1378 EXPECT_CALL(*s_will_remove0, Unload())
1379 .WillOnce(Return(true));
1380 EXPECT_CALL(*s_will_remove1, Unload())
1381 .WillOnce(Return(true));
1382 EXPECT_CALL(*s_will_not_remove0, Unload())
1383 .WillOnce(Return(false));
1384 EXPECT_CALL(*s_will_not_remove1, Unload())
1385 .WillOnce(Return(false));
1386
1387
1388 // This will cause all the profiles to be unloaded.
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001389 EXPECT_FALSE(IsSortServicesTaskPending());
Paul Stewart65512e12012-03-26 18:01:08 -07001390 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
Paul Stewartfbca6bc2014-10-07 08:26:03 -07001391 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewart65512e12012-03-26 18:01:08 -07001392
1393 // 2 of the 4 services added above should have been unregistered and
1394 // removed, leaving 2.
1395 EXPECT_EQ(2, manager()->services_.size());
1396 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1397 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1398}
1399
1400TEST_F(ManagerTest, PopProfileWithUnload) {
1401 MockServiceRefPtr s_will_remove0(
1402 new NiceMock<MockService>(control_interface(),
1403 dispatcher(),
1404 metrics(),
1405 manager()));
1406 MockServiceRefPtr s_will_remove1(
1407 new NiceMock<MockService>(control_interface(),
1408 dispatcher(),
1409 metrics(),
1410 manager()));
1411 MockServiceRefPtr s_will_not_remove0(
1412 new NiceMock<MockService>(control_interface(),
1413 dispatcher(),
1414 metrics(),
1415 manager()));
1416 MockServiceRefPtr s_will_not_remove1(
1417 new NiceMock<MockService>(control_interface(),
1418 dispatcher(),
1419 metrics(),
1420 manager()));
1421
Ben Chancc225ef2014-09-30 13:26:51 -07001422 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(nullptr))
Paul Stewart65512e12012-03-26 18:01:08 -07001423 .Times(5); // Once for each registration, and one after profile pop.
1424
1425 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001426 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001427 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001428 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001429 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001430 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001431 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001432 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001433
1434 // One for each service added above.
1435 ASSERT_EQ(4, manager()->services_.size());
1436
1437 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001438 new StrictMock<MockProfile>(
1439 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001440 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001441 new StrictMock<MockProfile>(
1442 control_interface(), metrics(), manager(), ""));
Paul Stewart65512e12012-03-26 18:01:08 -07001443
1444 s_will_remove0->set_profile(profile1);
1445 s_will_remove1->set_profile(profile1);
1446 s_will_not_remove0->set_profile(profile1);
1447 s_will_not_remove1->set_profile(profile1);
1448
1449 AdoptProfile(manager(), profile0);
1450 AdoptProfile(manager(), profile1);
1451
1452 // Deny any of the services entry to profile0, so they will all be unloaded.
1453 EXPECT_CALL(*profile0, ConfigureService(_))
1454 .WillRepeatedly(Return(false));
1455
1456 EXPECT_CALL(*s_will_remove0, Unload())
1457 .WillOnce(Return(true));
1458 EXPECT_CALL(*s_will_remove1, Unload())
1459 .WillOnce(Return(true));
1460 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001461 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001462 EXPECT_CALL(*s_will_not_remove1, Unload())
1463 .WillOnce(Return(false));
1464
Philipp Neubeck79173602012-11-13 21:10:09 +01001465 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1466 // profile list.
1467 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1468 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1469
Paul Stewart65512e12012-03-26 18:01:08 -07001470 // This will pop profile1, which should cause all our profiles to unload.
1471 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001472 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001473
1474 // 2 of the 4 services added above should have been unregistered and
1475 // removed, leaving 2.
1476 EXPECT_EQ(2, manager()->services_.size());
1477 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1478 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001479
1480 // Expect the unloaded services to lose their profile reference.
1481 EXPECT_FALSE(s_will_remove0->profile());
1482 EXPECT_FALSE(s_will_remove1->profile());
1483
1484 // If we explicitly deregister a service, the effect should be the same
1485 // with respect to the profile reference.
1486 ASSERT_TRUE(s_will_not_remove0->profile());
1487 manager()->DeregisterService(s_will_not_remove0);
1488 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001489}
1490
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001491TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001492 {
1493 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001494 ::DBus::Variant offline_mode;
1495 offline_mode.writer().append_bool(true);
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001496 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001497 kOfflineModeProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001498 offline_mode,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001499 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001500 }
1501 {
1502 ::DBus::Error error;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001503 ::DBus::Variant country;
1504 country.writer().append_string("a_country");
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001505 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001506 kCountryProperty,
mukesh agrawalbebf1b82013-04-23 15:06:33 -07001507 country,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001508 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001509 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001510 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001511 {
1512 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001513 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001514 kCountryProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001515 PropertyStoreTest::kBoolV,
1516 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001517 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001518 }
1519 {
1520 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001521 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001522 kOfflineModeProperty,
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001523 PropertyStoreTest::kStringV,
1524 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001525 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001526 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001527 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001528 {
1529 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001530 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001531 manager()->mutable_store(),
Ben Chan923a5022013-09-20 11:23:23 -07001532 kEnabledTechnologiesProperty,
Chris Masonea8a2c252011-06-27 22:16:30 -07001533 PropertyStoreTest::kStringsV,
1534 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001535 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001536 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001537}
1538
mukesh agrawal32399322011-09-01 10:53:43 -07001539TEST_F(ManagerTest, RequestScan) {
1540 {
1541 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001542 manager()->RegisterDevice(mock_devices_[0].get());
1543 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001544 EXPECT_CALL(*mock_devices_[0], technology())
1545 .WillRepeatedly(Return(Technology::kWifi));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001546 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
Joshua Krollda798622012-06-05 12:30:48 -07001547 EXPECT_CALL(*mock_devices_[1], technology())
1548 .WillRepeatedly(Return(Technology::kUnknown));
Wade Guthrie4823f4f2013-07-25 10:03:03 -07001549 EXPECT_CALL(*mock_devices_[1], Scan(_, _, _)).Times(0);
Peter Qiue783f1c2014-05-02 11:42:33 -07001550 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1551 Metrics::kUserInitiatedEventWifiScan)).Times(1);
Ben Chan923a5022013-09-20 11:23:23 -07001552 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
Peter Qiue783f1c2014-05-02 11:42:33 -07001553 manager()->DeregisterDevice(mock_devices_[0].get());
1554 manager()->DeregisterDevice(mock_devices_[1].get());
1555 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1556 Mock::VerifyAndClearExpectations(mock_devices_[1]);
1557
1558 manager()->RegisterDevice(mock_devices_[0].get());
1559 EXPECT_CALL(*mock_devices_[0], technology())
1560 .WillRepeatedly(Return(Technology::kWifi));
1561 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1562 Metrics::kUserInitiatedEventWifiScan)).Times(1);
1563 EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _, _));
1564 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1565 manager()->DeregisterDevice(mock_devices_[0].get());
1566 Mock::VerifyAndClearExpectations(mock_devices_[0]);
1567
1568 manager()->RegisterDevice(mock_devices_[0].get());
1569 EXPECT_CALL(*mock_devices_[0], technology())
1570 .WillRepeatedly(Return(Technology::kUnknown));
1571 EXPECT_CALL(*metrics(), NotifyUserInitiatedEvent(
1572 Metrics::kUserInitiatedEventWifiScan)).Times(0);
1573 EXPECT_CALL(*mock_devices_[0], Scan(_, _, _)).Times(0);
1574 manager()->RequestScan(Device::kFullScan, kTypeWifi, &error);
1575 manager()->DeregisterDevice(mock_devices_[0].get());
1576 Mock::VerifyAndClearExpectations(mock_devices_[0]);
mukesh agrawal32399322011-09-01 10:53:43 -07001577 }
1578
1579 {
1580 Error error;
Wade Guthrie68d41092013-04-02 12:56:02 -07001581 manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001582 EXPECT_EQ(Error::kInvalidArguments, error.type());
1583 }
1584}
1585
Darin Petkovb65c2452012-02-23 15:17:06 +01001586TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001587 KeyValueStore args;
1588 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001589 manager()->GetService(args, &e);
1590 EXPECT_EQ(Error::kInvalidArguments, e.type());
1591 EXPECT_EQ("must specify service type", e.message());
1592}
1593
1594TEST_F(ManagerTest, GetServiceUnknownType) {
1595 KeyValueStore args;
1596 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001597 args.SetString(kTypeProperty, kTypeEthernet);
Darin Petkovb65c2452012-02-23 15:17:06 +01001598 manager()->GetService(args, &e);
1599 EXPECT_EQ(Error::kNotSupported, e.type());
1600 EXPECT_EQ("service type is unsupported", e.message());
1601}
1602
Paul Stewart35eff132013-04-12 12:08:40 -07001603TEST_F(ManagerTest, GetServiceEthernetEap) {
1604 KeyValueStore args;
1605 Error e;
Paul Stewart55fc64c2013-07-18 09:51:35 -07001606 ServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1607 dispatcher(),
1608 metrics(),
1609 manager());
Ben Chan923a5022013-09-20 11:23:23 -07001610 args.SetString(kTypeProperty, kTypeEthernetEap);
Paul Stewart35eff132013-04-12 12:08:40 -07001611 SetEapProviderService(service);
1612 EXPECT_EQ(service, manager()->GetService(args, &e));
1613 EXPECT_TRUE(e.IsSuccess());
1614}
1615
Darin Petkovb65c2452012-02-23 15:17:06 +01001616TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001617 KeyValueStore args;
1618 Error e;
1619 WiFiServiceRefPtr wifi_service;
Ben Chan923a5022013-09-20 11:23:23 -07001620 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart3c504012013-01-17 17:49:58 -08001621 EXPECT_CALL(*wifi_provider_, GetService(_, _))
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001622 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001623 manager()->GetService(args, &e);
1624 EXPECT_TRUE(e.IsSuccess());
1625}
1626
Darin Petkov33af05c2012-02-28 10:10:30 +01001627TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1628 KeyValueStore args;
1629 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001630 args.SetString(kTypeProperty, kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001631 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001632 new StrictMock<MockProfile>(
1633 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001634 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001635 ServiceRefPtr service = manager()->GetService(args, &e);
1636 EXPECT_EQ(Error::kNotSupported, e.type());
1637 EXPECT_FALSE(service);
1638}
1639
Darin Petkovb65c2452012-02-23 15:17:06 +01001640TEST_F(ManagerTest, GetServiceVPN) {
1641 KeyValueStore args;
1642 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001643 args.SetString(kTypeProperty, kTypeVPN);
1644 args.SetString(kProviderTypeProperty, kProviderOpenVpn);
1645 args.SetString(kProviderHostProperty, "10.8.0.1");
1646 args.SetString(kNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001647 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001648 new StrictMock<MockProfile>(
1649 control_interface(), metrics(), manager(), ""));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001650 AdoptProfile(manager(), profile);
Darin Petkovc3505a52013-03-18 15:13:29 +01001651
1652#if defined(DISABLE_VPN)
1653
1654 ServiceRefPtr service = manager()->GetService(args, &e);
1655 EXPECT_EQ(Error::kNotSupported, e.type());
1656 EXPECT_FALSE(service);
1657
1658#else
1659
Paul Stewart7f5ad572012-06-04 15:18:54 -07001660 ServiceRefPtr updated_service;
1661 EXPECT_CALL(*profile, UpdateService(_))
1662 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1663 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001664 EXPECT_CALL(*profile, LoadService(_))
1665 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001666 EXPECT_CALL(*profile, ConfigureService(_))
1667 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001668 ServiceRefPtr service = manager()->GetService(args, &e);
1669 EXPECT_TRUE(e.IsSuccess());
1670 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001671 EXPECT_EQ(service, updated_service);
1672 EXPECT_EQ(service, configured_service);
Darin Petkovc3505a52013-03-18 15:13:29 +01001673
1674#endif // DISABLE_VPN
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001675}
1676
Ben Chan520eb172013-10-30 20:51:04 -07001677#if !defined(DISABLE_WIMAX)
1678
Darin Petkovc63dcf02012-05-24 11:51:43 +02001679TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1680 KeyValueStore args;
1681 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001682 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001683 ServiceRefPtr service = manager()->GetService(args, &e);
1684 EXPECT_EQ(Error::kInvalidArguments, e.type());
1685 EXPECT_EQ("Missing WiMAX network id.", e.message());
1686 EXPECT_FALSE(service);
1687}
1688
Darin Petkovd1cd7972012-05-22 15:26:15 +02001689TEST_F(ManagerTest, GetServiceWiMax) {
1690 KeyValueStore args;
1691 Error e;
Ben Chan923a5022013-09-20 11:23:23 -07001692 args.SetString(kTypeProperty, kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001693 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
Ben Chan923a5022013-09-20 11:23:23 -07001694 args.SetString(kNameProperty, "WiMAX Network");
Darin Petkovc63dcf02012-05-24 11:51:43 +02001695 ServiceRefPtr service = manager()->GetService(args, &e);
1696 EXPECT_TRUE(e.IsSuccess());
1697 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001698}
1699
Ben Chan520eb172013-10-30 20:51:04 -07001700#endif // DISABLE_WIMAX
1701
Paul Stewart7f61e522012-03-22 11:13:45 -07001702TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1703 // Manager calls ActiveProfile() so we need at least one profile installed.
1704 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001705 new NiceMock<MockProfile>(
1706 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001707 AdoptProfile(manager(), profile);
1708
1709 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001710 args.SetString(kProfileProperty, "xxx");
Paul Stewart7f61e522012-03-22 11:13:45 -07001711 Error error;
1712 manager()->ConfigureService(args, &error);
1713 EXPECT_EQ(Error::kInvalidArguments, error.type());
1714 EXPECT_EQ("Invalid profile name xxx", error.message());
1715}
1716
1717TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1718 // Manager calls ActiveProfile() so we need at least one profile installed.
1719 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001720 new NiceMock<MockProfile>(
1721 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001722 AdoptProfile(manager(), profile);
1723
1724 KeyValueStore args;
1725 Error error;
1726 manager()->ConfigureService(args, &error);
1727 EXPECT_EQ(Error::kInvalidArguments, error.type());
1728 EXPECT_EQ("must specify service type", error.message());
1729}
1730
1731// A registered service in the ephemeral profile should be moved to the
1732// active profile as a part of configuration if no profile was explicitly
1733// specified.
1734TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1735 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08001736 new NiceMock<MockProfile>(
1737 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001738
1739 AdoptProfile(manager(), profile); // This is now the active profile.
1740
Paul Stewartd2e1c362013-03-03 19:06:07 -08001741 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001742 scoped_refptr<MockWiFiService> service(
1743 new NiceMock<MockWiFiService>(control_interface(),
1744 dispatcher(),
1745 metrics(),
1746 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001747 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001748 ssid,
1749 "",
1750 "",
1751 false));
1752
1753 manager()->RegisterService(service);
1754 service->set_profile(GetEphemeralProfile(manager()));
1755
Paul Stewart3c504012013-01-17 17:49:58 -08001756 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001757 .WillOnce(Return(service));
1758 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1759 .WillOnce(Return(true));
1760 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1761 .WillOnce(Return(true));
1762
1763 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001764 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewart7f61e522012-03-22 11:13:45 -07001765 Error error;
1766 manager()->ConfigureService(args, &error);
1767 EXPECT_TRUE(error.IsSuccess());
1768}
1769
Paul Stewart2c575d22012-12-07 12:28:57 -08001770// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001771// specify a profile, it should be moved from the profile it was previously
1772// in to the specified profile if one was requested.
1773TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1774 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001775 new NiceMock<MockProfile>(
1776 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001777 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001778 new NiceMock<MockProfile>(
1779 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001780
1781 const string kProfileName0 = "profile0";
1782 const string kProfileName1 = "profile1";
1783
1784 EXPECT_CALL(*profile0, GetRpcIdentifier())
1785 .WillRepeatedly(Return(kProfileName0));
1786 EXPECT_CALL(*profile1, GetRpcIdentifier())
1787 .WillRepeatedly(Return(kProfileName1));
1788
1789 AdoptProfile(manager(), profile0);
1790 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1791
Paul Stewartd2e1c362013-03-03 19:06:07 -08001792 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001793 scoped_refptr<MockWiFiService> service(
1794 new NiceMock<MockWiFiService>(control_interface(),
1795 dispatcher(),
1796 metrics(),
1797 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001798 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001799 ssid,
1800 "",
1801 "",
1802 false));
1803
1804 manager()->RegisterService(service);
1805 service->set_profile(profile1);
1806
Paul Stewart3c504012013-01-17 17:49:58 -08001807 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001808 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001809 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1810 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001811 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1812 .WillOnce(Return(true));
1813 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1814 .WillOnce(Return(true));
1815 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1816 .WillOnce(Return(true));
1817
1818 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001819 args.SetString(kTypeProperty, kTypeWifi);
1820 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001821 Error error;
1822 manager()->ConfigureService(args, &error);
1823 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001824 service->set_profile(nullptr); // Breaks refcounting loop.
Paul Stewart7f61e522012-03-22 11:13:45 -07001825}
1826
Paul Stewart2c575d22012-12-07 12:28:57 -08001827// If we configure a service that is already a member of the specified
1828// profile, the Manager should not call LoadService or AdoptService again
1829// on this service.
1830TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1831 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001832 new NiceMock<MockProfile>(
1833 control_interface(), metrics(), manager(), ""));
Paul Stewart2c575d22012-12-07 12:28:57 -08001834
1835 const string kProfileName0 = "profile0";
1836
1837 EXPECT_CALL(*profile0, GetRpcIdentifier())
1838 .WillRepeatedly(Return(kProfileName0));
1839
1840 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1841
Paul Stewartd2e1c362013-03-03 19:06:07 -08001842 const vector<uint8_t> ssid;
Paul Stewart2c575d22012-12-07 12:28:57 -08001843 scoped_refptr<MockWiFiService> service(
1844 new NiceMock<MockWiFiService>(control_interface(),
1845 dispatcher(),
1846 metrics(),
1847 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001848 wifi_provider_,
Paul Stewart2c575d22012-12-07 12:28:57 -08001849 ssid,
1850 "",
1851 "",
1852 false));
1853
1854 manager()->RegisterService(service);
1855 service->set_profile(profile0);
1856
Paul Stewart3c504012013-01-17 17:49:58 -08001857 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart2c575d22012-12-07 12:28:57 -08001858 .WillOnce(Return(service));
1859 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1860 .Times(0);
1861 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1862 .WillOnce(Return(true));
1863 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1864 .Times(0);
1865
1866 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001867 args.SetString(kTypeProperty, kTypeWifi);
1868 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart2c575d22012-12-07 12:28:57 -08001869 Error error;
1870 manager()->ConfigureService(args, &error);
1871 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001872 service->set_profile(nullptr); // Breaks refcounting loop.
Paul Stewart2c575d22012-12-07 12:28:57 -08001873}
1874
Paul Stewart7f61e522012-03-22 11:13:45 -07001875// An unregistered service should remain unregistered, but its contents should
1876// be saved to the specified profile nonetheless.
1877TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1878 scoped_refptr<MockProfile> profile0(
Thieu Le5133b712013-02-19 14:47:21 -08001879 new NiceMock<MockProfile>(
1880 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001881 scoped_refptr<MockProfile> profile1(
Thieu Le5133b712013-02-19 14:47:21 -08001882 new NiceMock<MockProfile>(
1883 control_interface(), metrics(), manager(), ""));
Paul Stewart7f61e522012-03-22 11:13:45 -07001884
1885 const string kProfileName0 = "profile0";
1886 const string kProfileName1 = "profile1";
1887
1888 EXPECT_CALL(*profile0, GetRpcIdentifier())
1889 .WillRepeatedly(Return(kProfileName0));
1890 EXPECT_CALL(*profile1, GetRpcIdentifier())
1891 .WillRepeatedly(Return(kProfileName1));
1892
1893 AdoptProfile(manager(), profile0);
1894 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1895
Paul Stewartd2e1c362013-03-03 19:06:07 -08001896 const vector<uint8_t> ssid;
Paul Stewart7f61e522012-03-22 11:13:45 -07001897 scoped_refptr<MockWiFiService> service(
1898 new NiceMock<MockWiFiService>(control_interface(),
1899 dispatcher(),
1900 metrics(),
1901 manager(),
Paul Stewart3c504012013-01-17 17:49:58 -08001902 wifi_provider_,
Paul Stewart7f61e522012-03-22 11:13:45 -07001903 ssid,
1904 "",
1905 "",
1906 false));
1907
1908 service->set_profile(profile1);
1909
Paul Stewart3c504012013-01-17 17:49:58 -08001910 EXPECT_CALL(*wifi_provider_, GetService(_, _))
Paul Stewart7f61e522012-03-22 11:13:45 -07001911 .WillOnce(Return(service));
1912 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1913 .WillOnce(Return(true));
1914 EXPECT_CALL(*profile0, AdoptService(_))
1915 .Times(0);
1916 EXPECT_CALL(*profile1, AdoptService(_))
1917 .Times(0);
1918
1919 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001920 args.SetString(kTypeProperty, kTypeWifi);
1921 args.SetString(kProfileProperty, kProfileName0);
Paul Stewart7f61e522012-03-22 11:13:45 -07001922 Error error;
1923 manager()->ConfigureService(args, &error);
1924 EXPECT_TRUE(error.IsSuccess());
1925}
1926
Paul Stewartd2e1c362013-03-03 19:06:07 -08001927TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) {
1928 KeyValueStore args;
1929 Error error;
1930 ServiceRefPtr service =
1931 manager()->ConfigureServiceForProfile("", args, &error);
Paul Stewart6ae05892013-07-29 12:21:12 -07001932 EXPECT_EQ(Error::kInvalidArguments, error.type());
1933 EXPECT_EQ("must specify service type", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001934 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001935}
1936
1937TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) {
1938 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001939 args.SetString(kTypeProperty, kTypeCellular);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001940 Error error;
1941 ServiceRefPtr service =
1942 manager()->ConfigureServiceForProfile("", args, &error);
1943 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07001944 EXPECT_EQ("service type is unsupported", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001945 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001946}
1947
1948TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) {
1949 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001950 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001951 Error error;
1952 ServiceRefPtr service =
1953 manager()->ConfigureServiceForProfile("/profile/foo", args, &error);
1954 EXPECT_EQ(Error::kNotFound, error.type());
1955 EXPECT_EQ("Profile specified was not found", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001956 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001957}
1958
1959TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) {
1960 const string kProfileName0 = "profile0";
1961 const string kProfileName1 = "profile1";
1962 scoped_refptr<MockProfile> profile0(
1963 AddNamedMockProfileToManager(manager(), kProfileName0));
1964
1965 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001966 args.SetString(kTypeProperty, kTypeWifi);
1967 args.SetString(kProfileProperty, kProfileName1);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001968 Error error;
1969 ServiceRefPtr service =
1970 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1971 EXPECT_EQ(Error::kInvalidArguments, error.type());
1972 EXPECT_EQ("Profile argument does not match that in "
1973 "the configuration arguments", error.message());
Ben Chancc225ef2014-09-30 13:26:51 -07001974 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001975}
1976
1977TEST_F(ManagerTest,
1978 ConfigureServiceForProfileWithNoMatchingServiceFailGetService) {
1979 const string kProfileName0 = "profile0";
1980 scoped_refptr<MockProfile> profile0(
1981 AddNamedMockProfileToManager(manager(), kProfileName0));
1982 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07001983 args.SetString(kTypeProperty, kTypeWifi);
1984 args.SetString(kProfileProperty, kProfileName0);
Paul Stewartd2e1c362013-03-03 19:06:07 -08001985
1986 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
1987 .WillOnce(Return(WiFiServiceRefPtr()));
1988 EXPECT_CALL(*wifi_provider_, GetService(_, _))
1989 .WillOnce(Return(WiFiServiceRefPtr()));
1990 Error error;
1991 ServiceRefPtr service =
1992 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
1993 // Since we didn't set the error in the GetService expectation above...
1994 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07001995 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08001996}
1997
1998TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) {
1999 const string kProfileName0 = "profile0";
2000 scoped_refptr<MockProfile> profile0(
2001 AddNamedMockProfileToManager(manager(), kProfileName0));
2002
2003 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002004 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002005
2006 scoped_refptr<MockWiFiService> mock_service(
2007 new NiceMock<MockWiFiService>(control_interface(),
2008 dispatcher(),
2009 metrics(),
2010 manager(),
2011 wifi_provider_,
2012 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002013 kModeManaged,
2014 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002015 false));
2016 ServiceRefPtr mock_service_generic(mock_service.get());
2017 mock_service->set_profile(profile0);
2018 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2019 .WillOnce(Return(WiFiServiceRefPtr()));
2020 EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service));
2021 EXPECT_CALL(*profile0, UpdateService(mock_service_generic))
2022 .WillOnce(Return(true));
2023 Error error;
2024 ServiceRefPtr service =
2025 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2026 EXPECT_TRUE(error.IsSuccess());
2027 EXPECT_EQ(mock_service.get(), service.get());
Ben Chancc225ef2014-09-30 13:26:51 -07002028 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002029}
2030
2031TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) {
2032 scoped_refptr<MockService> mock_service(
2033 new NiceMock<MockService>(control_interface(),
2034 dispatcher(),
2035 metrics(),
2036 manager()));
2037 const string kGUID = "a guid";
Ben Chancc225ef2014-09-30 13:26:51 -07002038 mock_service->SetGuid(kGUID, nullptr);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002039 manager()->RegisterService(mock_service);
2040 ServiceRefPtr mock_service_generic(mock_service.get());
2041
2042 const string kProfileName = "profile";
2043 scoped_refptr<MockProfile> profile(
2044 AddNamedMockProfileToManager(manager(), kProfileName));
2045 mock_service->set_profile(profile);
2046
2047 EXPECT_CALL(*mock_service, technology())
2048 .WillOnce(Return(Technology::kCellular))
2049 .WillOnce(Return(Technology::kWifi));
2050
2051 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0);
2052 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2053 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2054
2055 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002056 args.SetString(kTypeProperty, kTypeWifi);
2057 args.SetString(kGuidProperty, kGUID);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002058
2059 // The first attempt should fail because the service reports a technology
2060 // other than "WiFi".
2061 {
2062 Error error;
2063 ServiceRefPtr service =
2064 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
Ben Chancc225ef2014-09-30 13:26:51 -07002065 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002066 EXPECT_EQ(Error::kNotSupported, error.type());
Paul Stewart6ae05892013-07-29 12:21:12 -07002067 EXPECT_EQ("This GUID matches a non-wifi service", error.message());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002068 }
2069
2070 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2071 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2072
2073 {
2074 Error error;
2075 ServiceRefPtr service =
2076 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2077 EXPECT_TRUE(error.IsSuccess());
2078 EXPECT_EQ(mock_service.get(), service.get());
2079 EXPECT_EQ(profile.get(), service->profile().get());
2080 }
Ben Chancc225ef2014-09-30 13:26:51 -07002081 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002082}
2083
2084TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) {
2085 const string kProfileName = "profile";
2086 scoped_refptr<MockProfile> profile(
2087 AddNamedMockProfileToManager(manager(), kProfileName));
2088
2089 scoped_refptr<MockWiFiService> mock_service(
2090 new NiceMock<MockWiFiService>(control_interface(),
2091 dispatcher(),
2092 metrics(),
2093 manager(),
2094 wifi_provider_,
2095 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002096 kModeManaged,
2097 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002098 false));
2099 mock_service->set_profile(profile);
2100 ServiceRefPtr mock_service_generic(mock_service.get());
2101
2102 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002103 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002104 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2105 .WillOnce(Return(mock_service));
2106 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2107 EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0);
2108 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2109 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2110
2111 Error error;
2112 ServiceRefPtr service =
2113 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2114 EXPECT_TRUE(error.IsSuccess());
2115 EXPECT_EQ(mock_service.get(), service.get());
2116 EXPECT_EQ(profile.get(), service->profile().get());
Ben Chancc225ef2014-09-30 13:26:51 -07002117 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002118}
2119
2120TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) {
2121 const string kProfileName = "profile";
2122 scoped_refptr<MockProfile> profile(
2123 AddNamedMockProfileToManager(manager(), kProfileName));
2124
2125 scoped_refptr<MockWiFiService> mock_service(
2126 new NiceMock<MockWiFiService>(control_interface(),
2127 dispatcher(),
2128 metrics(),
2129 manager(),
2130 wifi_provider_,
2131 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002132 kModeManaged,
2133 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002134 false));
2135 mock_service->set_profile(GetEphemeralProfile(manager()));
2136 ServiceRefPtr mock_service_generic(mock_service.get());
2137
2138 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002139 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002140 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2141 .WillOnce(Return(mock_service));
2142 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2143 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2144 EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1);
2145
2146 Error error;
2147 ServiceRefPtr service =
2148 manager()->ConfigureServiceForProfile(kProfileName, args, &error);
2149 EXPECT_TRUE(error.IsSuccess());
2150 EXPECT_EQ(mock_service.get(), service.get());
2151 EXPECT_EQ(profile.get(), service->profile().get());
Ben Chancc225ef2014-09-30 13:26:51 -07002152 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002153}
2154
2155TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) {
2156 const string kProfileName0 = "profile0";
2157 scoped_refptr<MockProfile> profile0(
2158 AddNamedMockProfileToManager(manager(), kProfileName0));
2159 const string kProfileName1 = "profile1";
2160 scoped_refptr<MockProfile> profile1(
2161 AddNamedMockProfileToManager(manager(), kProfileName1));
2162
2163 scoped_refptr<MockWiFiService> mock_service(
2164 new NiceMock<MockWiFiService>(control_interface(),
2165 dispatcher(),
2166 metrics(),
2167 manager(),
2168 wifi_provider_,
2169 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002170 kModeManaged,
2171 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002172 false));
2173 manager()->RegisterService(mock_service);
2174 mock_service->set_profile(profile0);
2175 ServiceRefPtr mock_service_generic(mock_service.get());
2176
2177 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002178 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002179 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2180 .WillOnce(Return(mock_service));
2181 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2182 EXPECT_CALL(*profile0, AbandonService(_)).Times(0);
2183 EXPECT_CALL(*profile1, AdoptService(_)).Times(0);
2184 // This happens once to make the service loadable for the ConfigureService
2185 // below, and a second time after the service is modified.
2186 EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0);
2187 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0);
2188 EXPECT_CALL(*mock_service, Configure(_, _)).Times(1);
2189 EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1);
2190
2191 Error error;
2192 ServiceRefPtr service =
2193 manager()->ConfigureServiceForProfile(kProfileName1, args, &error);
2194 EXPECT_TRUE(error.IsSuccess());
2195 EXPECT_EQ(mock_service.get(), service.get());
Ben Chancc225ef2014-09-30 13:26:51 -07002196 mock_service->set_profile(nullptr); // Breaks reference cycle.
Paul Stewartd2e1c362013-03-03 19:06:07 -08002197}
2198
2199TEST_F(ManagerTest,
2200 ConfigureServiceForProfileMatchingServiceProceedingProfile) {
2201 const string kProfileName0 = "profile0";
2202 scoped_refptr<MockProfile> profile0(
2203 AddNamedMockProfileToManager(manager(), kProfileName0));
2204 const string kProfileName1 = "profile1";
2205 scoped_refptr<MockProfile> profile1(
2206 AddNamedMockProfileToManager(manager(), kProfileName1));
2207
2208 scoped_refptr<MockWiFiService> matching_service(
2209 new StrictMock<MockWiFiService>(control_interface(),
2210 dispatcher(),
2211 metrics(),
2212 manager(),
2213 wifi_provider_,
2214 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002215 kModeManaged,
2216 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002217 false));
2218 matching_service->set_profile(profile1);
2219
2220 // We need to get rid of our reference to this mock service as soon
2221 // as Manager::ConfigureServiceForProfile() takes a reference in its
2222 // call to WiFiProvider::CreateTemporaryService(). This way the
2223 // latter function can keep a DCHECK(service->HasOneRef() even in
2224 // unit tests.
2225 temp_mock_service_ =
2226 new NiceMock<MockWiFiService>(control_interface(),
2227 dispatcher(),
2228 metrics(),
2229 manager(),
2230 wifi_provider_,
2231 vector<uint8_t>(),
Ben Chan923a5022013-09-20 11:23:23 -07002232 kModeManaged,
2233 kSecurityNone,
Paul Stewartd2e1c362013-03-03 19:06:07 -08002234 false);
2235
2236 // Only hold a pointer here so we don't affect the refcount.
2237 MockWiFiService *mock_service_ptr = temp_mock_service_.get();
2238
2239 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07002240 args.SetString(kTypeProperty, kTypeWifi);
Paul Stewartd2e1c362013-03-03 19:06:07 -08002241 EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _))
2242 .WillOnce(Return(matching_service));
2243 EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0);
2244 EXPECT_CALL(*profile1, AbandonService(_)).Times(0);
2245 EXPECT_CALL(*profile0, AdoptService(_)).Times(0);
2246 EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _))
2247 .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService));
2248 EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr)))
2249 .Times(1);
2250 EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1);
2251 EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1);
2252
2253 Error error;
2254 ServiceRefPtr service =
2255 manager()->ConfigureServiceForProfile(kProfileName0, args, &error);
2256 EXPECT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07002257 EXPECT_EQ(nullptr, service.get());
Paul Stewartd2e1c362013-03-03 19:06:07 -08002258 EXPECT_EQ(profile1.get(), matching_service->profile().get());
2259}
2260
Paul Stewart7a20aa42013-01-17 12:21:41 -08002261TEST_F(ManagerTest, FindMatchingService) {
2262 KeyValueStore args;
2263 {
2264 Error error;
2265 ServiceRefPtr service = manager()->FindMatchingService(args, &error);
2266 EXPECT_EQ(Error::kNotFound, error.type());
2267 }
2268
2269 scoped_refptr<MockService> mock_service0(
2270 new NiceMock<MockService>(control_interface(),
2271 dispatcher(),
2272 metrics(),
2273 manager()));
2274 scoped_refptr<MockService> mock_service1(
2275 new NiceMock<MockService>(control_interface(),
2276 dispatcher(),
2277 metrics(),
2278 manager()));
2279 manager()->RegisterService(mock_service0);
2280 manager()->RegisterService(mock_service1);
2281 EXPECT_CALL(*mock_service0, DoPropertiesMatch(_))
2282 .WillOnce(Return(true))
2283 .WillRepeatedly(Return(false));
2284 {
2285 Error error;
2286 EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error));
2287 EXPECT_TRUE(error.IsSuccess());
2288 }
2289 EXPECT_CALL(*mock_service1, DoPropertiesMatch(_))
2290 .WillOnce(Return(true))
2291 .WillRepeatedly(Return(false));
2292 {
2293 Error error;
2294 EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error));
2295 EXPECT_TRUE(error.IsSuccess());
2296 }
2297 {
2298 Error error;
2299 EXPECT_FALSE(manager()->FindMatchingService(args, &error));
2300 EXPECT_EQ(Error::kNotFound, error.type());
2301 }
2302}
2303
Paul Stewart22aa71b2011-09-16 12:15:11 -07002304TEST_F(ManagerTest, TechnologyOrder) {
2305 Error error;
Paul Stewart22ce7652014-10-15 21:26:44 -07002306 EXPECT_THAT(GetTechnologyOrder(), ElementsAre(Technology::kVPN,
2307 Technology::kEthernet,
2308 Technology::kWifi,
2309 Technology::kWiMax,
2310 Technology::kCellular));
2311
2312 EXPECT_FALSE(IsSortServicesTaskPending());
Ben Chan923a5022013-09-20 11:23:23 -07002313 manager()->SetTechnologyOrder(string(kTypeEthernet) + "," + string(kTypeWifi),
2314 &error);
Paul Stewart22ce7652014-10-15 21:26:44 -07002315 EXPECT_TRUE(IsSortServicesTaskPending());
Paul Stewart22aa71b2011-09-16 12:15:11 -07002316 ASSERT_TRUE(error.IsSuccess());
2317 EXPECT_EQ(manager()->GetTechnologyOrder(),
Ben Chan923a5022013-09-20 11:23:23 -07002318 string(kTypeEthernet) + "," + string(kTypeWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07002319
Ben Chan923a5022013-09-20 11:23:23 -07002320 manager()->SetTechnologyOrder(string(kTypeEthernet) + "x," +
2321 string(kTypeWifi), &error);
Paul Stewart22aa71b2011-09-16 12:15:11 -07002322 ASSERT_FALSE(error.IsSuccess());
2323 EXPECT_EQ(Error::kInvalidArguments, error.type());
Ben Chan923a5022013-09-20 11:23:23 -07002324 EXPECT_EQ(string(kTypeEthernet) + "," + string(kTypeWifi),
Paul Stewart22aa71b2011-09-16 12:15:11 -07002325 manager()->GetTechnologyOrder());
2326}
2327
Peter Qiu700de642014-07-14 16:31:30 -07002328TEST_F(ManagerTest, ConnectionStatusCheck) {
2329 // Setup mock metrics and service.
2330 MockMetrics mock_metrics(dispatcher());
2331 SetMetrics(&mock_metrics);
2332 scoped_refptr<MockService> mock_service = new NiceMock<MockService>(
2333 control_interface(), dispatcher(), metrics(), manager());
2334 manager()->RegisterService(mock_service);
2335
2336 // Device not connected.
2337 EXPECT_CALL(*mock_service.get(), IsConnected())
2338 .WillOnce(Return(false));
2339 EXPECT_CALL(mock_metrics,
2340 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOffline));
Peter Qiu94d18af2014-09-11 15:54:15 -07002341 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002342
2343 // Device connected, but not online.
2344 EXPECT_CALL(*mock_service.get(), IsConnected())
2345 .WillOnce(Return(true));
2346 EXPECT_CALL(*mock_service.get(), IsOnline())
2347 .WillOnce(Return(false));
2348 EXPECT_CALL(mock_metrics,
2349 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline)).Times(0);
2350 EXPECT_CALL(mock_metrics,
2351 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002352 manager()->ConnectionStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002353
2354 // Device connected and online.
2355 EXPECT_CALL(*mock_service.get(), IsConnected())
2356 .WillOnce(Return(true));
2357 EXPECT_CALL(*mock_service.get(), IsOnline())
2358 .WillOnce(Return(true));
2359 EXPECT_CALL(mock_metrics,
2360 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusOnline));
2361 EXPECT_CALL(mock_metrics,
2362 NotifyDeviceConnectionStatus(Metrics::kConnectionStatusConnected));
Peter Qiu94d18af2014-09-11 15:54:15 -07002363 manager()->ConnectionStatusCheck();
2364}
2365
2366TEST_F(ManagerTest, DevicePresenceStatusCheck) {
2367 // Setup mock metrics and service.
2368 MockMetrics mock_metrics(dispatcher());
2369 SetMetrics(&mock_metrics);
2370
2371 manager()->RegisterDevice(mock_devices_[0]);
2372 manager()->RegisterDevice(mock_devices_[1]);
2373 manager()->RegisterDevice(mock_devices_[2]);
2374 manager()->RegisterDevice(mock_devices_[3]);
2375
2376 ON_CALL(*mock_devices_[0].get(), technology())
2377 .WillByDefault(Return(Technology::kEthernet));
2378 ON_CALL(*mock_devices_[1].get(), technology())
2379 .WillByDefault(Return(Technology::kWifi));
2380 ON_CALL(*mock_devices_[2].get(), technology())
2381 .WillByDefault(Return(Technology::kCellular));
2382 ON_CALL(*mock_devices_[3].get(), technology())
2383 .WillByDefault(Return(Technology::kWifi));
2384
2385 EXPECT_CALL(mock_metrics,
2386 NotifyDevicePresenceStatus(Technology::kEthernet, true));
2387 EXPECT_CALL(mock_metrics,
2388 NotifyDevicePresenceStatus(Technology::kWifi, true));
2389 EXPECT_CALL(mock_metrics,
2390 NotifyDevicePresenceStatus(Technology::kWiMax, false));
2391 EXPECT_CALL(mock_metrics,
2392 NotifyDevicePresenceStatus(Technology::kCellular, true));
2393 manager()->DevicePresenceStatusCheck();
Peter Qiu700de642014-07-14 16:31:30 -07002394}
2395
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002396TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002397 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002398 SetMetrics(&mock_metrics);
Thieu Lea20cbc22012-01-09 22:01:43 +00002399
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002400 scoped_refptr<MockService> mock_service0(
2401 new NiceMock<MockService>(control_interface(),
2402 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002403 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002404 manager()));
2405 scoped_refptr<MockService> mock_service1(
2406 new NiceMock<MockService>(control_interface(),
2407 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002408 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002409 manager()));
2410
2411 scoped_refptr<MockConnection> mock_connection0(
2412 new NiceMock<MockConnection>(device_info_.get()));
2413 scoped_refptr<MockConnection> mock_connection1(
2414 new NiceMock<MockConnection>(device_info_.get()));
2415
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002416 // A single registered Service, without a connection. The
Ben Chancc225ef2014-09-30 13:26:51 -07002417 // DefaultService should be nullptr. If a change notification is
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002418 // generated, it should reference kNullPath.
Ben Chancc225ef2014-09-30 13:26:51 -07002419 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002420 EXPECT_CALL(*manager_adaptor_,
2421 EmitRpcIdentifierChanged(kDefaultServiceProperty,
2422 DBusAdaptor::kNullPath))
2423 .Times(AnyNumber());
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002424 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002425 CompleteServiceSort();
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002426
2427 // Adding another Service, also without a connection, does not
2428 // change DefaultService. Furthermore, we do not send a change
2429 // notification for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002430 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002431 EXPECT_CALL(*manager_adaptor_,
2432 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2433 .Times(0);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002434 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002435 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002436
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002437 // An explicit sort doesn't change anything, and does not emit a
2438 // change notification for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002439 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002440 EXPECT_CALL(*manager_adaptor_,
2441 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2442 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002443 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002444 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002445
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002446 // Re-ordering the unconnected Services doesn't change
2447 // DefaultService, and (hence) does not emit a change notification
2448 // for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002449 mock_service1->SetPriority(1, nullptr);
2450 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002451 EXPECT_CALL(*manager_adaptor_,
2452 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2453 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002454 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002455 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002456
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002457 // Re-ordering the unconnected Services doesn't change
2458 // DefaultService, and (hence) does not emit a change notification
2459 // for DefaultService.
Ben Chancc225ef2014-09-30 13:26:51 -07002460 mock_service1->SetPriority(0, nullptr);
2461 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002462 EXPECT_CALL(*manager_adaptor_,
2463 EmitRpcIdentifierChanged(kDefaultServiceProperty, _))
2464 .Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002465 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002466 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002467
Paul Stewartce4ec192012-03-14 12:53:46 -07002468 mock_service0->set_mock_connection(mock_connection0);
2469 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002470
mukesh agrawale37ad322013-10-08 16:33:56 -07002471 // If both Services have Connections, the DefaultService follows
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002472 // from ServiceOrderIs. We notify others of the change in
2473 // DefaultService.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002474 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002475 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002476 EXPECT_CALL(*manager_adaptor_,
2477 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002478 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002479 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002480
Darin Petkova5e07ef2012-07-09 14:27:57 +02002481 ServiceWatcher service_watcher;
2482 int tag =
2483 manager()->RegisterDefaultServiceCallback(
2484 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2485 service_watcher.AsWeakPtr()));
2486 EXPECT_EQ(1, tag);
2487
mukesh agrawale37ad322013-10-08 16:33:56 -07002488 // Changing the ordering causes the DefaultService to change, and
2489 // appropriate notifications are sent.
Ben Chancc225ef2014-09-30 13:26:51 -07002490 mock_service1->SetPriority(1, nullptr);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002491 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
2492 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002493 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00002494 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002495 EXPECT_CALL(*manager_adaptor_,
2496 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Paul Stewartdfa46052012-06-26 09:44:14 -07002497 manager()->SortServicesTask();
mukesh agrawale37ad322013-10-08 16:33:56 -07002498 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002499
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002500 // Deregistering a DefaultServiceCallback works as expected. (Later
2501 // code causes DefaultService changes, but we see no further calls
2502 // to |service_watcher|.)
Darin Petkova5e07ef2012-07-09 14:27:57 +02002503 manager()->DeregisterDefaultServiceCallback(tag);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002504 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002505
2506 // Deregistering the current DefaultService causes the other Service
2507 // to become default. Appropriate notifications are sent.
2508 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00002509 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002510 EXPECT_CALL(*manager_adaptor_,
2511 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Ben Chancc225ef2014-09-30 13:26:51 -07002512 mock_service1->set_mock_connection(nullptr); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002513 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002514 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002515
mukesh agrawale37ad322013-10-08 16:33:56 -07002516 // Deregistering the only Service causes the DefaultService to become
Ben Chancc225ef2014-09-30 13:26:51 -07002517 // nullptr. Appropriate notifications are sent.
2518 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002519 EXPECT_CALL(*manager_adaptor_,
2520 EmitRpcIdentifierChanged(kDefaultServiceProperty, _));
Ben Chancc225ef2014-09-30 13:26:51 -07002521 mock_service0->set_mock_connection(nullptr); // So DeregisterService works.
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002522 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002523 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002524
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002525 // An explicit sort doesn't change anything, and does not generate
2526 // an external notification.
Ben Chancc225ef2014-09-30 13:26:51 -07002527 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
mukesh agrawalf2bbad22014-02-25 14:33:32 -08002528 EXPECT_CALL(*manager_adaptor_,
2529 EmitRpcIdentifierChanged(kDefaultServiceProperty, _)).Times(0);
Paul Stewartdfa46052012-06-26 09:44:14 -07002530 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08002531}
2532
Darin Petkova5e07ef2012-07-09 14:27:57 +02002533TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
2534 EXPECT_EQ(0, manager()->default_service_callback_tag_);
2535 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2536
Thieu Le6c1e3bb2013-02-06 15:20:35 -08002537 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01002538 SetMetrics(&mock_metrics);
Darin Petkova5e07ef2012-07-09 14:27:57 +02002539
2540 scoped_refptr<MockService> mock_service(
2541 new NiceMock<MockService>(
2542 control_interface(), dispatcher(), metrics(), manager()));
2543 ServiceRefPtr service = mock_service;
2544 ServiceRefPtr null_service;
2545
Ben Chancc225ef2014-09-30 13:26:51 -07002546 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002547 manager()->NotifyDefaultServiceChanged(null_service);
2548
2549 ServiceWatcher service_watcher1;
2550 ServiceWatcher service_watcher2;
2551 int tag1 =
2552 manager()->RegisterDefaultServiceCallback(
2553 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2554 service_watcher1.AsWeakPtr()));
2555 EXPECT_EQ(1, tag1);
2556 int tag2 =
2557 manager()->RegisterDefaultServiceCallback(
2558 Bind(&ServiceWatcher::OnDefaultServiceChanged,
2559 service_watcher2.AsWeakPtr()));
2560 EXPECT_EQ(2, tag2);
2561
2562 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
2563 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
Ben Chancc225ef2014-09-30 13:26:51 -07002564 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(nullptr));
Darin Petkova5e07ef2012-07-09 14:27:57 +02002565 manager()->NotifyDefaultServiceChanged(null_service);
2566
2567 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
2568 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2569 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2570 manager()->NotifyDefaultServiceChanged(mock_service);
2571
2572 manager()->DeregisterDefaultServiceCallback(tag1);
2573 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
2574 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
2575 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2576 manager()->NotifyDefaultServiceChanged(mock_service);
2577 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
2578
2579 manager()->DeregisterDefaultServiceCallback(tag2);
2580 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
2581 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
2582 manager()->NotifyDefaultServiceChanged(mock_service);
2583
2584 EXPECT_EQ(2, manager()->default_service_callback_tag_);
2585 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
2586}
2587
Peter Qiu9d581932014-04-14 16:37:37 -07002588TEST_F(ManagerTest, ReportServicesOnSameNetwork) {
2589 int connection_id1 = 100;
2590 int connection_id2 = 200;
2591 scoped_refptr<MockService> mock_service1 =
2592 new NiceMock<MockService>(control_interface(), dispatcher(),
2593 metrics(), manager());
2594 mock_service1->set_connection_id(connection_id1);
2595 scoped_refptr<MockService> mock_service2 =
2596 new NiceMock<MockService>(control_interface(), dispatcher(),
2597 metrics(), manager());
2598 mock_service2->set_connection_id(connection_id1);
2599 scoped_refptr<MockService> mock_service3 =
2600 new NiceMock<MockService>(control_interface(), dispatcher(),
2601 metrics(), manager());
2602 mock_service3->set_connection_id(connection_id2);
2603
2604 manager()->RegisterService(mock_service1);
2605 manager()->RegisterService(mock_service2);
2606 manager()->RegisterService(mock_service3);
2607
2608 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(2));
2609 manager()->ReportServicesOnSameNetwork(connection_id1);
2610
2611 EXPECT_CALL(*metrics(), NotifyServicesOnSameNetwork(1));
2612 manager()->ReportServicesOnSameNetwork(connection_id2);
2613}
2614
Gaurav Shah435de2c2011-11-17 19:01:07 -08002615TEST_F(ManagerTest, AvailableTechnologies) {
2616 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
2617 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002618 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002619 manager(),
2620 "null4",
2621 "addr4",
2622 0));
2623 manager()->RegisterDevice(mock_devices_[0]);
2624 manager()->RegisterDevice(mock_devices_[1]);
2625 manager()->RegisterDevice(mock_devices_[2]);
2626 manager()->RegisterDevice(mock_devices_[3]);
2627
2628 ON_CALL(*mock_devices_[0].get(), technology())
2629 .WillByDefault(Return(Technology::kEthernet));
2630 ON_CALL(*mock_devices_[1].get(), technology())
2631 .WillByDefault(Return(Technology::kWifi));
2632 ON_CALL(*mock_devices_[2].get(), technology())
2633 .WillByDefault(Return(Technology::kCellular));
2634 ON_CALL(*mock_devices_[3].get(), technology())
2635 .WillByDefault(Return(Technology::kWifi));
2636
2637 set<string> expected_technologies;
2638 expected_technologies.insert(Technology::NameFromIdentifier(
2639 Technology::kEthernet));
2640 expected_technologies.insert(Technology::NameFromIdentifier(
2641 Technology::kWifi));
2642 expected_technologies.insert(Technology::NameFromIdentifier(
2643 Technology::kCellular));
2644 Error error;
2645 vector<string> technologies = manager()->AvailableTechnologies(&error);
2646
2647 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2648 ContainerEq(expected_technologies));
2649}
2650
2651TEST_F(ManagerTest, ConnectedTechnologies) {
2652 scoped_refptr<MockService> connected_service1(
2653 new NiceMock<MockService>(control_interface(),
2654 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002655 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002656 manager()));
2657 scoped_refptr<MockService> connected_service2(
2658 new NiceMock<MockService>(control_interface(),
2659 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002660 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002661 manager()));
2662 scoped_refptr<MockService> disconnected_service1(
2663 new NiceMock<MockService>(control_interface(),
2664 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002665 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002666 manager()));
2667 scoped_refptr<MockService> disconnected_service2(
2668 new NiceMock<MockService>(control_interface(),
2669 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002670 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002671 manager()));
2672
2673 ON_CALL(*connected_service1.get(), IsConnected())
2674 .WillByDefault(Return(true));
2675 ON_CALL(*connected_service2.get(), IsConnected())
2676 .WillByDefault(Return(true));
2677
2678 manager()->RegisterService(connected_service1);
2679 manager()->RegisterService(connected_service2);
2680 manager()->RegisterService(disconnected_service1);
2681 manager()->RegisterService(disconnected_service2);
2682
2683 manager()->RegisterDevice(mock_devices_[0]);
2684 manager()->RegisterDevice(mock_devices_[1]);
2685 manager()->RegisterDevice(mock_devices_[2]);
2686 manager()->RegisterDevice(mock_devices_[3]);
2687
2688 ON_CALL(*mock_devices_[0].get(), technology())
2689 .WillByDefault(Return(Technology::kEthernet));
2690 ON_CALL(*mock_devices_[1].get(), technology())
2691 .WillByDefault(Return(Technology::kWifi));
2692 ON_CALL(*mock_devices_[2].get(), technology())
2693 .WillByDefault(Return(Technology::kCellular));
2694 ON_CALL(*mock_devices_[3].get(), technology())
2695 .WillByDefault(Return(Technology::kWifi));
2696
2697 mock_devices_[0]->SelectService(connected_service1);
2698 mock_devices_[1]->SelectService(disconnected_service1);
2699 mock_devices_[2]->SelectService(disconnected_service2);
2700 mock_devices_[3]->SelectService(connected_service2);
2701
2702 set<string> expected_technologies;
2703 expected_technologies.insert(Technology::NameFromIdentifier(
2704 Technology::kEthernet));
2705 expected_technologies.insert(Technology::NameFromIdentifier(
2706 Technology::kWifi));
2707 Error error;
2708
2709 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2710 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2711 ContainerEq(expected_technologies));
2712}
2713
2714TEST_F(ManagerTest, DefaultTechnology) {
2715 scoped_refptr<MockService> connected_service(
2716 new NiceMock<MockService>(control_interface(),
2717 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002718 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002719 manager()));
2720 scoped_refptr<MockService> disconnected_service(
2721 new NiceMock<MockService>(control_interface(),
2722 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002723 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002724 manager()));
2725
2726 // Connected. WiFi.
2727 ON_CALL(*connected_service.get(), IsConnected())
2728 .WillByDefault(Return(true));
2729 ON_CALL(*connected_service.get(), state())
2730 .WillByDefault(Return(Service::kStateConnected));
2731 ON_CALL(*connected_service.get(), technology())
2732 .WillByDefault(Return(Technology::kWifi));
2733
2734 // Disconnected. Ethernet.
2735 ON_CALL(*disconnected_service.get(), technology())
2736 .WillByDefault(Return(Technology::kEthernet));
2737
2738 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002739 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002740 Error error;
2741 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2742
2743
2744 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002745 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002746 // Connected service should be brought to the front now.
2747 string expected_technology =
2748 Technology::NameFromIdentifier(Technology::kWifi);
2749 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2750}
2751
Paul Stewart212d60f2012-07-12 10:59:13 -07002752TEST_F(ManagerTest, Stop) {
2753 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002754 new NiceMock<MockProfile>(
2755 control_interface(), metrics(), manager(), ""));
Paul Stewart212d60f2012-07-12 10:59:13 -07002756 AdoptProfile(manager(), profile);
2757 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002758 new NiceMock<MockService>(control_interface(),
2759 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002760 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002761 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002762 manager()->RegisterService(service);
2763 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002764 SetPowerManager();
2765 EXPECT_TRUE(manager()->power_manager());
Paul Stewart212d60f2012-07-12 10:59:13 -07002766 EXPECT_CALL(*profile.get(),
2767 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2768 .WillOnce(Return(true));
Wade Guthrie60a37062013-04-02 11:39:09 -07002769 EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true));
Paul Stewart212d60f2012-07-12 10:59:13 -07002770 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
Samuel Tan0d061192014-07-07 15:45:15 -07002771 EXPECT_CALL(*service.get(), Disconnect(_, StrEq("Stop"))).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002772 manager()->Stop();
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002773 EXPECT_FALSE(manager()->power_manager());
Thieu Le1271d682011-11-02 22:48:19 +00002774}
2775
mukesh agrawal00917ce2011-11-22 23:56:55 +00002776TEST_F(ManagerTest, UpdateServiceConnected) {
2777 scoped_refptr<MockService> mock_service(
2778 new NiceMock<MockService>(control_interface(),
2779 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002780 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002781 manager()));
2782 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002783 EXPECT_FALSE(mock_service->retain_auto_connect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002784 EXPECT_FALSE(mock_service->auto_connect());
2785
Gaurav Shah435de2c2011-11-17 19:01:07 -08002786 EXPECT_CALL(*mock_service.get(), IsConnected())
2787 .WillRepeatedly(Return(true));
Paul Stewart22ce7652014-10-15 21:26:44 -07002788 EXPECT_CALL(*mock_service.get(), EnableAndRetainAutoConnect());
mukesh agrawal00917ce2011-11-22 23:56:55 +00002789 manager()->UpdateService(mock_service);
mukesh agrawal00917ce2011-11-22 23:56:55 +00002790}
2791
Paul Stewart2da34c02013-10-17 15:28:56 -07002792TEST_F(ManagerTest, UpdateServiceConnectedPersistAutoConnect) {
Thieu Led4e9e552012-02-16 16:26:07 -08002793 // This tests the case where the user connects to a service that is
2794 // currently associated with a profile. We want to make sure that the
Paul Stewart2da34c02013-10-17 15:28:56 -07002795 // auto_connect flag is set and that the is saved to the current profile.
Thieu Led4e9e552012-02-16 16:26:07 -08002796 scoped_refptr<MockService> mock_service(
2797 new NiceMock<MockService>(control_interface(),
2798 dispatcher(),
2799 metrics(),
2800 manager()));
2801 manager()->RegisterService(mock_service);
Paul Stewart2da34c02013-10-17 15:28:56 -07002802 EXPECT_FALSE(mock_service->retain_auto_connect());
Thieu Led4e9e552012-02-16 16:26:07 -08002803 EXPECT_FALSE(mock_service->auto_connect());
2804
Gary Moraind93615e2012-04-27 11:50:03 -07002805 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002806 new MockProfile(
2807 control_interface(), metrics(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002808
Gary Moraind93615e2012-04-27 11:50:03 -07002809 mock_service->set_profile(profile);
2810 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002811 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002812 EXPECT_CALL(*profile,
2813 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Paul Stewart22ce7652014-10-15 21:26:44 -07002814 EXPECT_CALL(*mock_service.get(), EnableAndRetainAutoConnect());
Thieu Led4e9e552012-02-16 16:26:07 -08002815 manager()->UpdateService(mock_service);
Gary Moraind93615e2012-04-27 11:50:03 -07002816 // This releases the ref on the mock profile.
Ben Chancc225ef2014-09-30 13:26:51 -07002817 mock_service->set_profile(nullptr);
Thieu Led4e9e552012-02-16 16:26:07 -08002818}
2819
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002820TEST_F(ManagerTest, SaveSuccessfulService) {
2821 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002822 new StrictMock<MockProfile>(
2823 control_interface(), metrics(), manager(), ""));
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002824 AdoptProfile(manager(), profile);
2825 scoped_refptr<MockService> service(
2826 new NiceMock<MockService>(control_interface(),
2827 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002828 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002829 manager()));
2830
2831 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2832 ServiceRefPtr expect_service(service.get());
2833
2834 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2835 .WillOnce(Return(false));
2836 manager()->RegisterService(service);
2837
2838 EXPECT_CALL(*service.get(), state())
2839 .WillRepeatedly(Return(Service::kStateConnected));
2840 EXPECT_CALL(*service.get(), IsConnected())
2841 .WillRepeatedly(Return(true));
2842 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2843 .WillOnce(Return(true));
2844 manager()->UpdateService(service);
2845}
2846
Darin Petkove7c6ad32012-06-29 10:22:09 +02002847TEST_F(ManagerTest, UpdateDevice) {
Thieu Le5133b712013-02-19 14:47:21 -08002848 MockProfile *profile0 =
2849 new MockProfile(control_interface(), metrics(), manager(), "");
2850 MockProfile *profile1 =
2851 new MockProfile(control_interface(), metrics(), manager(), "");
2852 MockProfile *profile2 =
2853 new MockProfile(control_interface(), metrics(), manager(), "");
Darin Petkove7c6ad32012-06-29 10:22:09 +02002854 AdoptProfile(manager(), profile0); // Passes ownership.
2855 AdoptProfile(manager(), profile1); // Passes ownership.
2856 AdoptProfile(manager(), profile2); // Passes ownership.
2857 DeviceRefPtr device_ref(mock_devices_[0].get());
2858 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2859 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2860 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2861 manager()->UpdateDevice(mock_devices_[0]);
2862}
2863
Paul Stewart1b253142012-01-26 14:05:52 -08002864TEST_F(ManagerTest, EnumerateProfiles) {
2865 vector<string> profile_paths;
2866 for (size_t i = 0; i < 10; i++) {
2867 scoped_refptr<MockProfile> profile(
Thieu Le5133b712013-02-19 14:47:21 -08002868 new StrictMock<MockProfile>(
2869 control_interface(), metrics(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002870 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002871 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2872 .WillOnce(Return(profile_paths.back()));
2873 AdoptProfile(manager(), profile);
2874 }
2875
2876 Error error;
2877 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2878 EXPECT_TRUE(error.IsSuccess());
2879 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2880 for (size_t i = 0; i < profile_paths.size(); i++) {
2881 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2882 }
2883}
2884
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002885TEST_F(ManagerTest, AutoConnectOnRegister) {
2886 MockServiceRefPtr service = MakeAutoConnectableService();
2887 EXPECT_CALL(*service.get(), AutoConnect());
2888 manager()->RegisterService(service);
2889 dispatcher()->DispatchPendingEvents();
2890}
2891
2892TEST_F(ManagerTest, AutoConnectOnUpdate) {
2893 MockServiceRefPtr service1 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07002894 service1->SetPriority(1, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002895 MockServiceRefPtr service2 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07002896 service2->SetPriority(2, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002897 manager()->RegisterService(service1);
2898 manager()->RegisterService(service2);
2899 dispatcher()->DispatchPendingEvents();
2900
2901 EXPECT_CALL(*service1.get(), AutoConnect());
2902 EXPECT_CALL(*service2.get(), state())
2903 .WillRepeatedly(Return(Service::kStateFailure));
2904 EXPECT_CALL(*service2.get(), IsFailed())
2905 .WillRepeatedly(Return(true));
2906 EXPECT_CALL(*service2.get(), IsConnected())
2907 .WillRepeatedly(Return(false));
2908 manager()->UpdateService(service2);
2909 dispatcher()->DispatchPendingEvents();
2910}
2911
2912TEST_F(ManagerTest, AutoConnectOnDeregister) {
2913 MockServiceRefPtr service1 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07002914 service1->SetPriority(1, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002915 MockServiceRefPtr service2 = MakeAutoConnectableService();
Ben Chancc225ef2014-09-30 13:26:51 -07002916 service2->SetPriority(2, nullptr);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002917 manager()->RegisterService(service1);
2918 manager()->RegisterService(service2);
2919 dispatcher()->DispatchPendingEvents();
2920
2921 EXPECT_CALL(*service1.get(), AutoConnect());
2922 manager()->DeregisterService(service2);
2923 dispatcher()->DispatchPendingEvents();
2924}
2925
Daniel Eratfac09532014-04-17 20:25:59 -07002926TEST_F(ManagerTest, AutoConnectOnSuspending) {
Darin Petkov3ec55342012-09-28 14:04:44 +02002927 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002928 SetSuspending(true);
Darin Petkov3ec55342012-09-28 14:04:44 +02002929 SetPowerManager();
2930 EXPECT_CALL(*service, AutoConnect()).Times(0);
2931 manager()->RegisterService(service);
2932 dispatcher()->DispatchPendingEvents();
2933}
2934
Daniel Eratfac09532014-04-17 20:25:59 -07002935TEST_F(ManagerTest, AutoConnectOnNotSuspending) {
Darin Petkovca621542012-07-25 14:25:56 +02002936 MockServiceRefPtr service = MakeAutoConnectableService();
Daniel Eratfac09532014-04-17 20:25:59 -07002937 SetSuspending(false);
Darin Petkovca621542012-07-25 14:25:56 +02002938 SetPowerManager();
2939 EXPECT_CALL(*service, AutoConnect());
2940 manager()->RegisterService(service);
2941 dispatcher()->DispatchPendingEvents();
2942}
2943
Paul Stewart63864b62012-11-07 15:10:55 -08002944TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2945 SetRunning(false);
2946 MockServiceRefPtr service = MakeAutoConnectableService();
2947 EXPECT_CALL(*service, AutoConnect()).Times(0);
2948 manager()->RegisterService(service);
2949 dispatcher()->DispatchPendingEvents();
2950}
2951
Daniel Eratfac09532014-04-17 20:25:59 -07002952TEST_F(ManagerTest, Suspend) {
Darin Petkovca621542012-07-25 14:25:56 +02002953 MockServiceRefPtr service = MakeAutoConnectableService();
Darin Petkovca621542012-07-25 14:25:56 +02002954 SetPowerManager();
2955 EXPECT_CALL(*service, AutoConnect());
2956 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002957 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002958 dispatcher()->DispatchPendingEvents();
2959
Samuel Tanfbe8d2b2014-09-15 20:23:59 -07002960 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend(_));
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07002961 OnSuspendImminent();
Daniel Eratfac09532014-04-17 20:25:59 -07002962 EXPECT_CALL(*service, AutoConnect()).Times(0);
Darin Petkovca621542012-07-25 14:25:56 +02002963 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002964 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002965
Daniel Eratfac09532014-04-17 20:25:59 -07002966 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07002967 OnSuspendDone();
Daniel Eratfac09532014-04-17 20:25:59 -07002968 EXPECT_CALL(*service, AutoConnect());
Darin Petkovca621542012-07-25 14:25:56 +02002969 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002970 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002971}
2972
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002973TEST_F(ManagerTest, AddTerminationAction) {
Darin Petkov3ec55342012-09-28 14:04:44 +02002974 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2975 manager()->AddTerminationAction("action1", base::Closure());
2976 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2977 manager()->AddTerminationAction("action2", base::Closure());
2978}
2979
2980TEST_F(ManagerTest, RemoveTerminationAction) {
Daniel Erat0818cca2012-12-14 10:16:21 -08002981 const char kKey1[] = "action1";
2982 const char kKey2[] = "action2";
Darin Petkov3ec55342012-09-28 14:04:44 +02002983
Ben Chan16d3acb2014-05-13 22:08:14 -07002984 // Removing an action when the hook table is empty.
Darin Petkov3ec55342012-09-28 14:04:44 +02002985 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2986 manager()->RemoveTerminationAction("unknown");
Darin Petkov3ec55342012-09-28 14:04:44 +02002987
mukesh agrawal6eb4f2c2014-07-01 14:31:10 -07002988 // Fill hook table with two items.
Darin Petkov3ec55342012-09-28 14:04:44 +02002989 manager()->AddTerminationAction(kKey1, base::Closure());
2990 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2991 manager()->AddTerminationAction(kKey2, base::Closure());
2992
Ben Chan16d3acb2014-05-13 22:08:14 -07002993 // Removing an action that ends up with a non-empty hook table.
Darin Petkov3ec55342012-09-28 14:04:44 +02002994 manager()->RemoveTerminationAction(kKey1);
2995 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
Darin Petkov3ec55342012-09-28 14:04:44 +02002996
Ben Chan16d3acb2014-05-13 22:08:14 -07002997 // Removing the last action.
Darin Petkov3ec55342012-09-28 14:04:44 +02002998 manager()->RemoveTerminationAction(kKey2);
2999 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
3000}
3001
3002TEST_F(ManagerTest, RunTerminationActions) {
3003 TerminationActionTest test_action;
3004 const string kActionName = "action";
3005
3006 EXPECT_CALL(test_action, Done(_));
3007 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3008 test_action.AsWeakPtr()));
3009
3010 manager()->AddTerminationAction(TerminationActionTest::kActionName,
3011 Bind(&TerminationActionTest::Action,
3012 test_action.AsWeakPtr()));
3013 test_action.set_manager(manager());
3014 EXPECT_CALL(test_action, Done(_));
3015 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
3016 test_action.AsWeakPtr()));
3017}
3018
Samuel Tanfbe8d2b2014-09-15 20:23:59 -07003019TEST_F(ManagerTest, OnSuspendImminentDevicesPresent) {
3020 EXPECT_CALL(*mock_devices_[0].get(), OnBeforeSuspend(_));
3021 EXPECT_CALL(*mock_devices_[1].get(), OnBeforeSuspend(_));
3022 EXPECT_CALL(*mock_devices_[2].get(), OnBeforeSuspend(_));
3023 manager()->RegisterDevice(mock_devices_[0]);
3024 manager()->RegisterDevice(mock_devices_[1]);
3025 manager()->RegisterDevice(mock_devices_[2]);
3026 SetPowerManager();
3027 OnSuspendImminent();
3028}
3029
3030TEST_F(ManagerTest, OnSuspendImminentNoDevicesPresent) {
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003031 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003032 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003033 OnSuspendImminent();
Darin Petkov3ec55342012-09-28 14:04:44 +02003034}
3035
Samuel Tan68b73d22014-10-28 17:00:56 -07003036TEST_F(ManagerTest, OnDarkSuspendImminentDevicesPresent) {
3037 EXPECT_CALL(*mock_devices_[0].get(), OnDarkResume(_));
3038 EXPECT_CALL(*mock_devices_[1].get(), OnDarkResume(_));
3039 EXPECT_CALL(*mock_devices_[2].get(), OnDarkResume(_));
3040 manager()->RegisterDevice(mock_devices_[0]);
3041 manager()->RegisterDevice(mock_devices_[1]);
3042 manager()->RegisterDevice(mock_devices_[2]);
3043 SetPowerManager();
3044 OnDarkSuspendImminent();
3045}
3046
3047TEST_F(ManagerTest, OnDarkSuspendImminentNoDevicesPresent) {
3048 EXPECT_CALL(*power_manager_, ReportDarkSuspendReadiness());
3049 SetPowerManager();
3050 OnDarkSuspendImminent();
3051}
3052
Darin Petkov3ec55342012-09-28 14:04:44 +02003053TEST_F(ManagerTest, OnSuspendActionsComplete) {
Darin Petkov3ec55342012-09-28 14:04:44 +02003054 Error error;
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003055 EXPECT_CALL(*power_manager_, ReportSuspendReadiness());
Darin Petkov3ec55342012-09-28 14:04:44 +02003056 SetPowerManager();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -07003057 OnSuspendActionsComplete(error);
Darin Petkov3ec55342012-09-28 14:04:44 +02003058}
3059
Paul Stewartc681fa02012-03-02 19:40:04 -08003060TEST_F(ManagerTest, RecheckPortal) {
3061 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
3062 .WillOnce(Return(false));
3063 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
3064 .WillOnce(Return(true));
3065 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
3066 .Times(0);
3067
3068 manager()->RegisterDevice(mock_devices_[0]);
3069 manager()->RegisterDevice(mock_devices_[1]);
3070 manager()->RegisterDevice(mock_devices_[2]);
3071
Ben Chancc225ef2014-09-30 13:26:51 -07003072 manager()->RecheckPortal(nullptr);
Paul Stewartc681fa02012-03-02 19:40:04 -08003073}
3074
Paul Stewartd215af62012-04-24 23:25:50 -07003075TEST_F(ManagerTest, RecheckPortalOnService) {
3076 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
3077 dispatcher(),
3078 metrics(),
3079 manager());
3080 EXPECT_CALL(*mock_devices_[0].get(),
3081 IsConnectedToService(IsRefPtrTo(service)))
3082 .WillOnce(Return(false));
3083 EXPECT_CALL(*mock_devices_[1].get(),
3084 IsConnectedToService(IsRefPtrTo(service)))
3085 .WillOnce(Return(true));
3086 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
3087 .WillOnce(Return(true));
3088 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
3089 .Times(0);
3090
3091 manager()->RegisterDevice(mock_devices_[0]);
3092 manager()->RegisterDevice(mock_devices_[1]);
3093 manager()->RegisterDevice(mock_devices_[2]);
3094
3095 manager()->RecheckPortalOnService(service);
3096}
3097
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003098TEST_F(ManagerTest, GetDefaultService) {
3099 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003100 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003101
3102 scoped_refptr<MockService> mock_service(
3103 new NiceMock<MockService>(control_interface(),
3104 dispatcher(),
3105 metrics(),
3106 manager()));
3107
3108 manager()->RegisterService(mock_service);
3109 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003110 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003111
3112 scoped_refptr<MockConnection> mock_connection(
3113 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07003114 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003115 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07003116 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003117
Ben Chancc225ef2014-09-30 13:26:51 -07003118 mock_service->set_mock_connection(nullptr);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07003119 manager()->DeregisterService(mock_service);
3120}
3121
Paul Stewart13ed2252012-03-21 12:52:46 -07003122TEST_F(ManagerTest, GetServiceWithGUID) {
3123 scoped_refptr<MockService> mock_service0(
3124 new NiceMock<MockService>(control_interface(),
3125 dispatcher(),
3126 metrics(),
3127 manager()));
3128
3129 scoped_refptr<MockService> mock_service1(
3130 new NiceMock<MockService>(control_interface(),
3131 dispatcher(),
3132 metrics(),
3133 manager()));
3134
Paul Stewartcb59fed2012-03-21 21:14:46 -07003135 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
3136 .Times(0);
3137 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
3138 .Times(0);
3139
Paul Stewart13ed2252012-03-21 12:52:46 -07003140 manager()->RegisterService(mock_service0);
3141 manager()->RegisterService(mock_service1);
3142
3143 const string kGUID0 = "GUID0";
3144 const string kGUID1 = "GUID1";
3145
3146 {
3147 Error error;
3148 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3149 EXPECT_FALSE(error.IsSuccess());
3150 EXPECT_FALSE(service);
3151 }
3152
3153 KeyValueStore args;
Ben Chan923a5022013-09-20 11:23:23 -07003154 args.SetString(kGuidProperty, kGUID1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003155
3156 {
3157 Error error;
3158 ServiceRefPtr service = manager()->GetService(args, &error);
3159 EXPECT_EQ(Error::kInvalidArguments, error.type());
3160 EXPECT_FALSE(service);
3161 }
3162
Ben Chancc225ef2014-09-30 13:26:51 -07003163 mock_service0->SetGuid(kGUID0, nullptr);
3164 mock_service1->SetGuid(kGUID1, nullptr);
Paul Stewart13ed2252012-03-21 12:52:46 -07003165
3166 {
3167 Error error;
3168 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
3169 EXPECT_TRUE(error.IsSuccess());
3170 EXPECT_EQ(mock_service0.get(), service.get());
3171 }
3172
3173 {
3174 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07003175 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
3176 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07003177 ServiceRefPtr service = manager()->GetService(args, &error);
3178 EXPECT_TRUE(error.IsSuccess());
3179 EXPECT_EQ(mock_service1.get(), service.get());
3180 }
3181
3182 manager()->DeregisterService(mock_service0);
3183 manager()->DeregisterService(mock_service1);
3184}
3185
Gary Morain028545d2012-04-07 14:55:52 -07003186
3187TEST_F(ManagerTest, CalculateStateOffline) {
Peter Qiu700de642014-07-14 16:31:30 -07003188 EXPECT_FALSE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003189 EXPECT_EQ("offline", manager()->CalculateState(nullptr));
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003190
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003191 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003192 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003193 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3194 .Times(AnyNumber());
3195 scoped_refptr<MockService> mock_service0(
3196 new NiceMock<MockService>(control_interface(),
3197 dispatcher(),
3198 metrics(),
3199 manager()));
3200
3201 scoped_refptr<MockService> mock_service1(
3202 new NiceMock<MockService>(control_interface(),
3203 dispatcher(),
3204 metrics(),
3205 manager()));
3206
3207 EXPECT_CALL(*mock_service0.get(), IsConnected())
3208 .WillRepeatedly(Return(false));
3209 EXPECT_CALL(*mock_service1.get(), IsConnected())
3210 .WillRepeatedly(Return(false));
3211
3212 manager()->RegisterService(mock_service0);
3213 manager()->RegisterService(mock_service1);
3214
Peter Qiu700de642014-07-14 16:31:30 -07003215 EXPECT_FALSE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003216 EXPECT_EQ("offline", manager()->CalculateState(nullptr));
Gary Morain028545d2012-04-07 14:55:52 -07003217
3218 manager()->DeregisterService(mock_service0);
3219 manager()->DeregisterService(mock_service1);
3220}
3221
3222TEST_F(ManagerTest, CalculateStateOnline) {
Thieu Le6c1e3bb2013-02-06 15:20:35 -08003223 MockMetrics mock_metrics(dispatcher());
Darin Petkov4cbff5b2013-01-29 16:29:05 +01003224 SetMetrics(&mock_metrics);
Gary Morain028545d2012-04-07 14:55:52 -07003225 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
3226 .Times(AnyNumber());
3227 scoped_refptr<MockService> mock_service0(
3228 new NiceMock<MockService>(control_interface(),
3229 dispatcher(),
3230 metrics(),
3231 manager()));
3232
3233 scoped_refptr<MockService> mock_service1(
3234 new NiceMock<MockService>(control_interface(),
3235 dispatcher(),
3236 metrics(),
3237 manager()));
3238
3239 EXPECT_CALL(*mock_service0.get(), IsConnected())
3240 .WillRepeatedly(Return(false));
3241 EXPECT_CALL(*mock_service1.get(), IsConnected())
3242 .WillRepeatedly(Return(true));
3243 EXPECT_CALL(*mock_service0.get(), state())
3244 .WillRepeatedly(Return(Service::kStateIdle));
3245 EXPECT_CALL(*mock_service1.get(), state())
3246 .WillRepeatedly(Return(Service::kStateConnected));
3247
3248 manager()->RegisterService(mock_service0);
3249 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07003250 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07003251
Peter Qiu700de642014-07-14 16:31:30 -07003252 EXPECT_TRUE(manager()->IsConnected());
Ben Chancc225ef2014-09-30 13:26:51 -07003253 EXPECT_EQ("online", manager()->CalculateState(nullptr));
Gary Morain028545d2012-04-07 14:55:52 -07003254
3255 manager()->DeregisterService(mock_service0);
3256 manager()->DeregisterService(mock_service1);
3257}
3258
Paul Stewart03e29f72013-09-26 00:49:48 -07003259TEST_F(ManagerTest, RefreshConnectionState) {
3260 EXPECT_CALL(*manager_adaptor_,
3261 EmitStringChanged(kConnectionStateProperty, kStateIdle));
3262 RefreshConnectionState();
3263 Mock::VerifyAndClearExpectations(manager_adaptor_);
3264
3265 scoped_refptr<MockService> mock_service(
3266 new NiceMock<MockService>(control_interface(),
3267 dispatcher(),
3268 metrics(),
3269 manager()));
3270 EXPECT_CALL(*manager_adaptor_,
3271 EmitStringChanged(kConnectionStateProperty, _)).Times(0);
3272 manager()->RegisterService(mock_service);
3273 RefreshConnectionState();
3274
3275 scoped_refptr<MockConnection> mock_connection(
3276 new NiceMock<MockConnection>(device_info_.get()));
3277 mock_service->set_mock_connection(mock_connection);
3278 EXPECT_CALL(*mock_service, state())
3279 .WillOnce(Return(Service::kStateIdle));
3280 RefreshConnectionState();
3281
3282 Mock::VerifyAndClearExpectations(manager_adaptor_);
3283 EXPECT_CALL(*mock_service, state())
3284 .WillOnce(Return(Service::kStatePortal));
3285 EXPECT_CALL(*manager_adaptor_,
3286 EmitStringChanged(kConnectionStateProperty, kStatePortal));
3287 RefreshConnectionState();
3288 Mock::VerifyAndClearExpectations(manager_adaptor_);
3289
Ben Chancc225ef2014-09-30 13:26:51 -07003290 mock_service->set_mock_connection(nullptr);
Paul Stewart03e29f72013-09-26 00:49:48 -07003291 manager()->DeregisterService(mock_service);
3292}
3293
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003294TEST_F(ManagerTest, StartupPortalList) {
3295 // Simulate loading value from the default profile.
3296 const string kProfileValue("wifi,vpn");
3297 manager()->props_.check_portal_list = kProfileValue;
3298
Ben Chancc225ef2014-09-30 13:26:51 -07003299 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003300 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3301 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3302
3303 const string kStartupValue("cellular,ethernet");
3304 manager()->SetStartupPortalList(kStartupValue);
3305 // Ensure profile value is not overwritten, so when we save the default
3306 // profile, the correct value will still be written.
3307 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
3308
3309 // However we should read back a different list.
Ben Chancc225ef2014-09-30 13:26:51 -07003310 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003311 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
3312 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3313
3314 const string kRuntimeValue("ppp");
3315 // Setting a runtime value over the control API should overwrite both
3316 // the profile value and what we read back.
3317 Error error;
3318 manager()->mutable_store()->SetStringProperty(
Ben Chan923a5022013-09-20 11:23:23 -07003319 kCheckPortalListProperty,
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003320 kRuntimeValue,
3321 &error);
3322 ASSERT_TRUE(error.IsSuccess());
Ben Chancc225ef2014-09-30 13:26:51 -07003323 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(nullptr));
Paul Stewart10e9e4e2012-04-26 19:46:28 -07003324 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
3325 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
3326 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
3327}
3328
Paul Stewart036dba02012-08-07 12:34:41 -07003329TEST_F(ManagerTest, LinkMonitorEnabled) {
3330 const string kEnabledTechnologies("wifi,vpn");
3331 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
3332 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
3333 EXPECT_FALSE(
3334 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
3335}
3336
Ben Chan8e6b8ef2014-07-14 21:50:18 -07003337TEST_F(ManagerTest, IsTechnologyAutoConnectDisabled) {
3338 const string kNoAutoConnectTechnologies("wifi,cellular");
3339 manager()->props_.no_auto_connect_technologies = kNoAutoConnectTechnologies;
3340 EXPECT_TRUE(manager()->IsTechnologyAutoConnectDisabled(Technology::kWifi));
3341 EXPECT_TRUE(
3342 manager()->IsTechnologyAutoConnectDisabled(Technology::kCellular));
3343 EXPECT_FALSE(
3344 manager()->IsTechnologyAutoConnectDisabled(Technology::kEthernet));
3345}
3346
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003347TEST_F(ManagerTest, SetEnabledStateForTechnology) {
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003348 Error error(Error::kOperationInitiated);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003349 DisableTechnologyReplyHandler disable_technology_reply_handler;
3350 ResultCallback disable_technology_callback(
3351 Bind(&DisableTechnologyReplyHandler::ReportResult,
3352 disable_technology_reply_handler.AsWeakPtr()));
3353 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_)).Times(0);
3354
Ben Chan923a5022013-09-20 11:23:23 -07003355 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003356 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003357 EXPECT_TRUE(error.IsSuccess());
3358
Joshua Krollda798622012-06-05 12:30:48 -07003359 ON_CALL(*mock_devices_[0], technology())
3360 .WillByDefault(Return(Technology::kEthernet));
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003361 ON_CALL(*mock_devices_[1], technology())
3362 .WillByDefault(Return(Technology::kCellular));
3363 ON_CALL(*mock_devices_[2], technology())
3364 .WillByDefault(Return(Technology::kCellular));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003365
3366 manager()->RegisterDevice(mock_devices_[0]);
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003367 manager()->RegisterDevice(mock_devices_[1]);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003368
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003369 // Ethernet Device is disabled, so disable succeeds immediately.
Arman Uguray2f352e62013-08-28 19:12:53 -07003370 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _))
3371 .WillOnce(WithArg<1>(Invoke(SetErrorSuccess)));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003372 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003373 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003374 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003375 EXPECT_TRUE(error.IsSuccess());
3376
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003377 // Ethernet Device is enabled, and mock doesn't change error from
3378 // kOperationInitiated, so expect disable to say operation in progress.
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003379 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
3380 mock_devices_[0]->enabled_ = true;
3381 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003382 manager()->SetEnabledStateForTechnology(kTypeEthernet, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003383 &error, disable_technology_callback);
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003384 EXPECT_TRUE(error.IsOngoing());
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003385
3386 // Ethernet Device is disabled, and mock doesn't change error from
3387 // kOperationInitiated, so expect enable to say operation in progress.
3388 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
3389 mock_devices_[0]->enabled_ = false;
3390 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003391 manager()->SetEnabledStateForTechnology(kTypeEthernet, true,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003392 &error, disable_technology_callback);
3393 EXPECT_TRUE(error.IsOngoing());
3394
3395 // Cellular Device is enabled, but disable failed.
3396 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3397 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3398 mock_devices_[1]->enabled_ = true;
3399 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003400 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003401 &error, disable_technology_callback);
3402 EXPECT_EQ(Error::kPermissionDenied, error.type());
3403
3404 // Multiple Cellular Devices in enabled state. Should indicate IsOngoing
3405 // if one is in progress (even if the other completed immediately).
3406 manager()->RegisterDevice(mock_devices_[2]);
3407 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3408 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3409 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _));
3410 mock_devices_[1]->enabled_ = true;
3411 mock_devices_[2]->enabled_ = true;
3412 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003413 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003414 &error, disable_technology_callback);
3415 EXPECT_TRUE(error.IsOngoing());
3416
3417 // ...and order doesn't matter.
3418 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
3419 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3420 .WillOnce(WithArg<1>(Invoke(SetErrorPermissionDenied)));
3421 mock_devices_[1]->enabled_ = true;
3422 mock_devices_[2]->enabled_ = true;
3423 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003424 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003425 &error, disable_technology_callback);
3426 EXPECT_TRUE(error.IsOngoing());
3427 Mock::VerifyAndClearExpectations(&disable_technology_reply_handler);
3428
3429 // Multiple Cellular Devices in enabled state. Even if all disable
3430 // operations complete asynchronously, we only get one call to the
3431 // DisableTechnologyReplyHandler::ReportResult.
3432 ResultCallback device1_result_callback;
3433 ResultCallback device2_result_callback;
3434 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _))
3435 .WillOnce(SaveArg<2>(&device1_result_callback));
3436 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _))
3437 .WillOnce(DoAll(WithArg<1>(Invoke(SetErrorPermissionDenied)),
3438 SaveArg<2>(&device2_result_callback)));
3439 EXPECT_CALL(disable_technology_reply_handler, ReportResult(_));
3440 mock_devices_[1]->enabled_ = true;
3441 mock_devices_[2]->enabled_ = true;
3442 error.Populate(Error::kOperationInitiated);
Ben Chan923a5022013-09-20 11:23:23 -07003443 manager()->SetEnabledStateForTechnology(kTypeCellular, false,
mukesh agrawal46c27cc2013-07-10 16:39:10 -07003444 &error, disable_technology_callback);
3445 EXPECT_TRUE(error.IsOngoing());
3446 device1_result_callback.Run(Error(Error::kSuccess));
3447 device2_result_callback.Run(Error(Error::kSuccess));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04003448}
3449
Paul Stewart4d5efb72012-09-17 12:24:34 -07003450TEST_F(ManagerTest, IgnoredSearchList) {
Ben Chancd477322014-10-17 14:19:30 -07003451 std::unique_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003452 vector<string> ignored_paths;
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003453 SetResolver(resolver.get());
Paul Stewart4d5efb72012-09-17 12:24:34 -07003454
3455 const string kIgnored0 = "chromium.org";
3456 ignored_paths.push_back(kIgnored0);
3457 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003458 SetIgnoredDNSSearchPaths(kIgnored0, nullptr);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003459 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
3460
3461 const string kIgnored1 = "google.com";
3462 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
3463 ignored_paths.push_back(kIgnored1);
3464 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003465 SetIgnoredDNSSearchPaths(kIgnoredSum, nullptr);
Paul Stewart4d5efb72012-09-17 12:24:34 -07003466 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
3467
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003468 ignored_paths.clear();
3469 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
Ben Chancc225ef2014-09-30 13:26:51 -07003470 SetIgnoredDNSSearchPaths("", nullptr);
mukesh agrawalbebf1b82013-04-23 15:06:33 -07003471 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
3472
Paul Stewart4d5efb72012-09-17 12:24:34 -07003473 SetResolver(Resolver::GetInstance());
3474}
3475
Paul Stewartbfb82552012-10-24 16:48:48 -07003476TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
3477 // Test to make sure that every service state-change causes the
3478 // Manager to emit a new service list.
3479 scoped_refptr<MockService> mock_service(
3480 new NiceMock<MockService>(control_interface(),
3481 dispatcher(),
3482 metrics(),
3483 manager()));
3484 EXPECT_CALL(*mock_service, state())
3485 .WillRepeatedly(Return(Service::kStateIdle));
3486
3487 manager()->RegisterService(mock_service);
3488 EXPECT_CALL(
3489 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003490 kServiceCompleteListProperty, _)).Times(1);
3491 EXPECT_CALL(
3492 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003493 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003494 EXPECT_CALL(
3495 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003496 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003497 CompleteServiceSort();
3498
3499 Mock::VerifyAndClearExpectations(manager_adaptor_);
3500 EXPECT_CALL(
3501 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Paul Stewart19a14db2014-05-30 11:31:26 -07003502 kServiceCompleteListProperty, _)).Times(1);
3503 EXPECT_CALL(
3504 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003505 kServicesProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003506 EXPECT_CALL(
3507 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
Ben Chan923a5022013-09-20 11:23:23 -07003508 kServiceWatchListProperty, _)).Times(1);
Paul Stewartbfb82552012-10-24 16:48:48 -07003509 manager()->UpdateService(mock_service.get());
3510 CompleteServiceSort();
3511
3512 manager()->DeregisterService(mock_service);
3513}
3514
3515TEST_F(ManagerTest, EnumerateServices) {
3516 scoped_refptr<MockService> mock_service(
3517 new NiceMock<MockService>(control_interface(),
3518 dispatcher(),
3519 metrics(),
3520 manager()));
3521 manager()->RegisterService(mock_service);
3522
3523 EXPECT_CALL(*mock_service, state())
3524 .WillRepeatedly(Return(Service::kStateConnected));
3525 EXPECT_CALL(*mock_service, IsVisible())
3526 .WillRepeatedly(Return(false));
3527 EXPECT_TRUE(EnumerateAvailableServices().empty());
3528 EXPECT_TRUE(EnumerateWatchedServices().empty());
3529
3530 EXPECT_CALL(*mock_service, state())
3531 .WillRepeatedly(Return(Service::kStateIdle));
3532 EXPECT_TRUE(EnumerateAvailableServices().empty());
3533 EXPECT_TRUE(EnumerateWatchedServices().empty());
3534
3535 EXPECT_CALL(*mock_service, IsVisible())
3536 .WillRepeatedly(Return(true));
3537 Service::ConnectState unwatched_states[] = {
3538 Service::kStateUnknown,
3539 Service::kStateIdle,
3540 Service::kStateFailure
3541 };
3542 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
3543 EXPECT_CALL(*mock_service, state())
3544 .WillRepeatedly(Return(unwatched_states[i]));
3545 EXPECT_FALSE(EnumerateAvailableServices().empty());
3546 EXPECT_TRUE(EnumerateWatchedServices().empty());
3547 }
3548
3549 Service::ConnectState watched_states[] = {
3550 Service::kStateAssociating,
3551 Service::kStateConfiguring,
3552 Service::kStateConnected,
Paul Stewartbfb82552012-10-24 16:48:48 -07003553 Service::kStatePortal,
3554 Service::kStateOnline
3555 };
3556 for (size_t i = 0; i < arraysize(watched_states); ++i) {
3557 EXPECT_CALL(*mock_service, state())
3558 .WillRepeatedly(Return(watched_states[i]));
3559 EXPECT_FALSE(EnumerateAvailableServices().empty());
3560 EXPECT_FALSE(EnumerateWatchedServices().empty());
3561 }
3562
3563 manager()->DeregisterService(mock_service);
3564}
3565
Paul Stewart39db5ca2013-03-18 14:15:17 -07003566TEST_F(ManagerTest, ConnectToBestServices) {
3567 scoped_refptr<MockService> wifi_service0(
3568 new NiceMock<MockService>(control_interface(),
3569 dispatcher(),
3570 metrics(),
3571 manager()));
3572 EXPECT_CALL(*wifi_service0.get(), state())
3573 .WillRepeatedly(Return(Service::kStateIdle));
3574 EXPECT_CALL(*wifi_service0.get(), IsConnected())
3575 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003576 wifi_service0->SetConnectable(true);
3577 wifi_service0->SetAutoConnect(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003578 wifi_service0->SetSecurity(Service::kCryptoAes, true, true);
3579 EXPECT_CALL(*wifi_service0.get(), technology())
3580 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003581 EXPECT_CALL(*wifi_service0.get(), IsVisible())
3582 .WillRepeatedly(Return(false));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003583 EXPECT_CALL(*wifi_service0.get(), explicitly_disconnected())
3584 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003585
3586 scoped_refptr<MockService> wifi_service1(
3587 new NiceMock<MockService>(control_interface(),
3588 dispatcher(),
3589 metrics(),
3590 manager()));
3591 EXPECT_CALL(*wifi_service1.get(), state())
3592 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003593 EXPECT_CALL(*wifi_service1.get(), IsVisible())
3594 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003595 EXPECT_CALL(*wifi_service1.get(), IsConnected())
3596 .WillRepeatedly(Return(false));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003597 wifi_service1->SetAutoConnect(true);
3598 wifi_service1->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003599 wifi_service1->SetSecurity(Service::kCryptoRc4, true, true);
3600 EXPECT_CALL(*wifi_service1.get(), technology())
3601 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003602 EXPECT_CALL(*wifi_service1.get(), explicitly_disconnected())
3603 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003604
3605 scoped_refptr<MockService> wifi_service2(
3606 new NiceMock<MockService>(control_interface(),
3607 dispatcher(),
3608 metrics(),
3609 manager()));
3610 EXPECT_CALL(*wifi_service2.get(), state())
3611 .WillRepeatedly(Return(Service::kStateConnected));
3612 EXPECT_CALL(*wifi_service2.get(), IsConnected())
3613 .WillRepeatedly(Return(true));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003614 EXPECT_CALL(*wifi_service2.get(), IsVisible())
3615 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003616 wifi_service2->SetAutoConnect(true);
3617 wifi_service2->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003618 wifi_service2->SetSecurity(Service::kCryptoNone, false, false);
3619 EXPECT_CALL(*wifi_service2.get(), technology())
3620 .WillRepeatedly(Return(Technology::kWifi));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003621 EXPECT_CALL(*wifi_service2.get(), explicitly_disconnected())
3622 .WillRepeatedly(Return(false));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003623
3624 manager()->RegisterService(wifi_service0);
3625 manager()->RegisterService(wifi_service1);
3626 manager()->RegisterService(wifi_service2);
3627
3628 CompleteServiceSort();
3629 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0));
3630
3631 scoped_refptr<MockService> cell_service(
3632 new NiceMock<MockService>(control_interface(),
3633 dispatcher(),
3634 metrics(),
3635 manager()));
3636
3637 EXPECT_CALL(*cell_service.get(), state())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003638 .WillRepeatedly(Return(Service::kStateIdle));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003639 EXPECT_CALL(*cell_service.get(), IsConnected())
Arman Uguray6fe4f262013-08-02 20:21:55 -07003640 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003641 EXPECT_CALL(*cell_service.get(), IsVisible())
3642 .WillRepeatedly(Return(true));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003643 cell_service->SetAutoConnect(true);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003644 cell_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003645 EXPECT_CALL(*cell_service.get(), technology())
3646 .WillRepeatedly(Return(Technology::kCellular));
Arman Uguray6fe4f262013-08-02 20:21:55 -07003647 EXPECT_CALL(*cell_service.get(), explicitly_disconnected())
3648 .WillRepeatedly(Return(true));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003649 manager()->RegisterService(cell_service);
3650
Arman Uguray6fe4f262013-08-02 20:21:55 -07003651 scoped_refptr<MockService> wimax_service(
3652 new NiceMock<MockService>(control_interface(),
3653 dispatcher(),
3654 metrics(),
3655 manager()));
3656
3657 EXPECT_CALL(*wimax_service.get(), state())
3658 .WillRepeatedly(Return(Service::kStateConnected));
3659 EXPECT_CALL(*wimax_service.get(), IsConnected())
3660 .WillRepeatedly(Return(true));
3661 EXPECT_CALL(*wimax_service.get(), IsVisible())
3662 .WillRepeatedly(Return(true));
3663 wimax_service->SetAutoConnect(true);
3664 wimax_service->SetConnectable(true);
3665 EXPECT_CALL(*wimax_service.get(), technology())
3666 .WillRepeatedly(Return(Technology::kWiMax));
3667 EXPECT_CALL(*wimax_service.get(), explicitly_disconnected())
3668 .WillRepeatedly(Return(false));
3669 manager()->RegisterService(wimax_service);
3670
Paul Stewart39db5ca2013-03-18 14:15:17 -07003671 scoped_refptr<MockService> vpn_service(
3672 new NiceMock<MockService>(control_interface(),
3673 dispatcher(),
3674 metrics(),
3675 manager()));
3676
3677 EXPECT_CALL(*vpn_service.get(), state())
3678 .WillRepeatedly(Return(Service::kStateIdle));
3679 EXPECT_CALL(*vpn_service.get(), IsConnected())
3680 .WillRepeatedly(Return(false));
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003681 EXPECT_CALL(*vpn_service.get(), IsVisible())
3682 .WillRepeatedly(Return(true));
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07003683 wifi_service2->SetAutoConnect(false);
3684 vpn_service->SetConnectable(true);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003685 EXPECT_CALL(*vpn_service.get(), technology())
3686 .WillRepeatedly(Return(Technology::kVPN));
3687 manager()->RegisterService(vpn_service);
3688
3689 // The connected services should be at the top.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003690 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003691
Paul Stewartf7d7d3f2013-04-24 14:47:44 -07003692 EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible.
3693 EXPECT_CALL(*wifi_service1.get(), Connect(_, _));
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003694 EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003695 EXPECT_CALL(*cell_service.get(), Connect(_, _))
3696 .Times(0); // Explicitly disconnected.
3697 EXPECT_CALL(*wimax_service.get(), Connect(_, _)).Times(0); // Is connected.
mukesh agrawaldc7b8442012-09-27 13:48:14 -07003698 EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect.
Paul Stewart39db5ca2013-03-18 14:15:17 -07003699
Ben Chancc225ef2014-09-30 13:26:51 -07003700 manager()->ConnectToBestServices(nullptr);
Paul Stewart39db5ca2013-03-18 14:15:17 -07003701 dispatcher()->DispatchPendingEvents();
3702
3703 // After this operation, since the Connect calls above are mocked and
3704 // no actual state changes have occurred, we should expect that the
3705 // service sorting order will not have changed.
Arman Uguray6fe4f262013-08-02 20:21:55 -07003706 EXPECT_TRUE(ServiceOrderIs(wifi_service2, wimax_service));
Paul Stewart39db5ca2013-03-18 14:15:17 -07003707}
3708
Rebecca Silberstein6862b382014-09-11 08:24:51 -07003709TEST_F(ManagerTest, CreateConnectivityReport) {
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -07003710 // Add devices
3711 // WiFi
3712 auto wifi_device = make_scoped_refptr(
3713 new NiceMock<MockDevice>(control_interface(),
3714 dispatcher(),
3715 metrics(),
3716 manager(),
3717 "null",
3718 "addr",
3719 0));
3720 manager()->RegisterDevice(wifi_device);
3721 // Cell
3722 auto cell_device = make_scoped_refptr(
3723 new NiceMock<MockDevice>(control_interface(),
3724 dispatcher(),
3725 metrics(),
3726 manager(),
3727 "null",
3728 "addr",
3729 1));
3730 manager()->RegisterDevice(cell_device);
3731 // WiMax
3732 auto wimax_device = make_scoped_refptr(
3733 new NiceMock<MockDevice>(control_interface(),
3734 dispatcher(),
3735 metrics(),
3736 manager(),
3737 "null",
3738 "addr",
3739 2));
3740 manager()->RegisterDevice(wimax_device);
3741 // Ethernet
3742 auto eth_device = make_scoped_refptr(
3743 new NiceMock<MockDevice>(control_interface(),
3744 dispatcher(),
3745 metrics(),
3746 manager(),
3747 "null",
3748 "addr",
3749 3));
3750 manager()->RegisterDevice(eth_device);
3751 // VPN Device -- base device for a service that will not be connected
3752 auto vpn_device = make_scoped_refptr(
3753 new NiceMock<MockDevice>(control_interface(),
3754 dispatcher(),
3755 metrics(),
3756 manager(),
3757 "null",
3758 "addr",
3759 4));
3760 manager()->RegisterDevice(vpn_device);
3761
3762 // Add service for multiple devices
3763 // WiFi
3764 MockServiceRefPtr wifi_service =
3765 new NiceMock<MockService>(control_interface(),
3766 dispatcher(),
3767 metrics(),
3768 manager());
3769 manager()->RegisterService(wifi_service);
3770 EXPECT_CALL(*wifi_service.get(), state())
3771 .WillRepeatedly(Return(Service::kStateConnected));
3772 EXPECT_CALL(*wifi_service.get(), IsConnected())
3773 .WillRepeatedly(Return(true));
3774 EXPECT_CALL(*wifi_device.get(),
3775 IsConnectedToService(_)).WillRepeatedly(Return(false));
3776 EXPECT_CALL(*wifi_device.get(),
3777 IsConnectedToService(IsRefPtrTo(wifi_service)))
3778 .WillRepeatedly(Return(true));
3779
3780 // Cell
3781 MockServiceRefPtr cell_service =
3782 new NiceMock<MockService>(control_interface(),
3783 dispatcher(),
3784 metrics(),
3785 manager());
3786 manager()->RegisterService(cell_service);
3787 EXPECT_CALL(*cell_service.get(), state())
3788 .WillRepeatedly(Return(Service::kStateConnected));
3789 EXPECT_CALL(*cell_service.get(), IsConnected())
3790 .WillRepeatedly(Return(true));
3791 EXPECT_CALL(*cell_device.get(),
3792 IsConnectedToService(_)).WillRepeatedly(Return(false));
3793 EXPECT_CALL(*cell_device.get(),
3794 IsConnectedToService(IsRefPtrTo(cell_service)))
3795 .WillRepeatedly(Return(true));
3796
3797 // WiMax
3798 MockServiceRefPtr wimax_service =
3799 new NiceMock<MockService>(control_interface(),
3800 dispatcher(),
3801 metrics(),
3802 manager());
3803 manager()->RegisterService(wimax_service);
3804 EXPECT_CALL(*wimax_service.get(), state())
3805 .WillRepeatedly(Return(Service::kStateConnected));
3806 EXPECT_CALL(*wimax_service.get(), IsConnected())
3807 .WillRepeatedly(Return(true));
3808
3809 EXPECT_CALL(*wimax_device.get(),
3810 IsConnectedToService(_)).WillRepeatedly(Return(false));
3811 EXPECT_CALL(*wimax_device.get(),
3812 IsConnectedToService(IsRefPtrTo(wimax_service)))
3813 .WillRepeatedly(Return(true));
3814
3815 // Ethernet
3816 MockServiceRefPtr eth_service =
3817 new NiceMock<MockService>(control_interface(),
3818 dispatcher(),
3819 metrics(),
3820 manager());
3821 manager()->RegisterService(eth_service);
3822 EXPECT_CALL(*eth_service.get(), state())
3823 .WillRepeatedly(Return(Service::kStateConnected));
3824 EXPECT_CALL(*eth_service.get(), IsConnected())
3825 .WillRepeatedly(Return(true));
3826 EXPECT_CALL(*eth_device.get(),
3827 IsConnectedToService(_)).WillRepeatedly(Return(false));
3828 EXPECT_CALL(*eth_device.get(),
3829 IsConnectedToService(IsRefPtrTo(eth_service)))
3830 .WillRepeatedly(Return(true));
3831
3832 // VPN: Service exists but is not connected and will not trigger a
3833 // connectivity report.
3834 MockServiceRefPtr vpn_service =
3835 new NiceMock<MockService>(control_interface(),
3836 dispatcher(),
3837 metrics(),
3838 manager());
3839 manager()->RegisterService(vpn_service);
3840 EXPECT_CALL(*vpn_service.get(), state())
3841 .WillRepeatedly(Return(Service::kStateIdle));
3842 EXPECT_CALL(*vpn_service.get(), IsConnected())
3843 .WillRepeatedly(Return(false));
3844
3845 EXPECT_CALL(*wifi_device.get(), StartConnectivityTest())
3846 .WillOnce(Return(true));
3847 EXPECT_CALL(*cell_device.get(), StartConnectivityTest())
3848 .WillOnce(Return(true));
3849 EXPECT_CALL(*wimax_device.get(), StartConnectivityTest())
3850 .WillOnce(Return(true));
3851 EXPECT_CALL(*eth_device.get(), StartConnectivityTest())
3852 .WillOnce(Return(true));
3853 EXPECT_CALL(*vpn_device.get(), StartConnectivityTest()).Times(0);
Ben Chancc225ef2014-09-30 13:26:51 -07003854 manager()->CreateConnectivityReport(nullptr);
Rebecca Silberstein6862b382014-09-11 08:24:51 -07003855 dispatcher()->DispatchPendingEvents();
3856}
3857
Christopher Wiley83889862013-05-02 15:55:09 -07003858TEST_F(ManagerTest, VerifyWhenNotConnected) {
3859 const string kFakeCertificate("fake cert");
3860 const string kFakePublicKey("fake public key");
3861 const string kFakeNonce("fake public key");
3862 const string kFakeSignedData("fake signed data");
3863 const string kFakeUdn("fake udn");
3864 const vector<uint8_t> kSSID(10, 87);
3865 const string kConfiguredSSID("AConfiguredDestination");
3866 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3867 kConfiguredSSID.end());
3868 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
3869 scoped_refptr<MockWiFiService> mock_destination(
3870 new NiceMock<MockWiFiService>(control_interface(), dispatcher(),
3871 metrics(), manager(), wifi_provider_,
3872 kSSID, "", "none", false));
3873 // Register this service, but don't mark it as connected.
3874 manager()->RegisterService(mock_destination);
3875 // Verify that if we're not connected to anything, verification fails.
3876 {
3877 LOG(INFO) << "Can't verify if not connected.";
3878 EXPECT_CALL(*crypto_util_proxy_,
3879 VerifyDestination(_, _, _, _, _, _, _, _, _)).Times(0);
3880 Error error(Error::kOperationInitiated);
3881 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3882 kFakeSignedData, kFakeUdn, "", "",
3883 ResultBoolCallback(), &error);
3884 EXPECT_TRUE(error.IsFailure());
3885 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3886 }
3887 {
3888 // However, if the destination is already configured, we might be
3889 // connected to it via something other than WiFi, and we shouldn't
3890 // enforce the WiFi check.
3891 EXPECT_CALL(*crypto_util_proxy_,
3892 VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3893 kFakeSignedData, kFakeUdn,
3894 kConfiguredSSIDVector, kConfiguredBSSID,
3895 _, _)).Times(1).WillOnce(Return(true));
3896 Error error(Error::kOperationInitiated);
3897 manager()->VerifyDestination(kFakeCertificate, kFakePublicKey, kFakeNonce,
3898 kFakeSignedData, kFakeUdn, kConfiguredSSID,
3899 kConfiguredBSSID, ResultBoolCallback(),
3900 &error);
3901 EXPECT_FALSE(error.IsFailure());
3902 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3903 }
3904}
3905
Christopher Wiley1057cd72013-02-28 15:21:29 -08003906TEST_F(ManagerTest, VerifyDestination) {
3907 const string kFakeCertificate("fake cert");
3908 const string kFakePublicKey("fake public key");
3909 const string kFakeNonce("fake public key");
3910 const string kFakeSignedData("fake signed data");
3911 const string kFakeUdn("fake udn");
3912 const char kSSIDStr[] = "fake ssid";
3913 const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr));
Christopher Wileycdde79f2013-05-01 14:26:56 -07003914 const string kConfiguredSSID("AConfiguredDestination");
3915 const vector<uint8_t> kConfiguredSSIDVector(kConfiguredSSID.begin(),
3916 kConfiguredSSID.end());
3917 const string kConfiguredBSSID("aa:bb:aa:bb:aa:bb");
Christopher Wiley1057cd72013-02-28 15:21:29 -08003918 const string kFakeData("muffin man");
3919 scoped_refptr<MockWiFiService> mock_destination(
3920 new NiceMock<MockWiFiService>(control_interface(),
3921 dispatcher(),
3922 metrics(),
3923 manager(),
3924 wifi_provider_,
3925 kSSID,
3926 "",
3927 "none",
3928 false));
3929 manager()->RegisterService(mock_destination);
Christopher Wiley1057cd72013-02-28 15:21:29 -08003930 // Making the service look online will let service lookup in
3931 // VerifyDestinatoin succeed.
3932 EXPECT_CALL(*mock_destination.get(), IsConnected())
3933 .WillRepeatedly(Return(true));
Christopher Wiley83889862013-05-02 15:55:09 -07003934 StrictMock<DestinationVerificationTest> dv_test;
Christopher Wiley1057cd72013-02-28 15:21:29 -08003935
3936 // Lead off by verifying that the basic VerifyDestination flow works.
3937 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003938 LOG(INFO) << "Basic VerifyDestination flow.";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003939 ResultBoolCallback passed_down_callback;
3940 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3941 kFakePublicKey,
3942 kFakeNonce,
3943 kFakeSignedData,
3944 kFakeUdn,
3945 kSSID,
3946 _,
3947 _,
3948 _))
3949 .Times(1)
3950 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3951 // Ask the manager to verify the current destination. This should look
3952 // up our previously registered service, and pass some metadata about
3953 // that service down to the CryptoUtilProxy to verify.
3954 Error error(Error::kOperationInitiated);
3955 ResultBoolCallback cb = Bind(
3956 &DestinationVerificationTest::ResultBoolCallbackStub,
3957 dv_test.AsWeakPtr());
3958 manager()->VerifyDestination(kFakeCertificate,
3959 kFakePublicKey,
3960 kFakeNonce,
3961 kFakeSignedData,
3962 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07003963 // Ask to be verified against that service.
3964 "", "",
3965 cb,
3966 &error);
3967 // We assert here, because if the operation is not ongoing, it is
3968 // inconsistent with shim behavior to call the callback anyway.
3969 ASSERT_TRUE(error.IsOngoing());
3970 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
3971 EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1);
3972 // Call the callback passed into the CryptoUtilProxy, which
3973 // should find its way into the callback passed into the manager.
3974 // In real code, that callback passed into the manager is from the
3975 // DBus adaptor.
3976 Error e;
3977 passed_down_callback.Run(e, true);
3978 Mock::VerifyAndClearExpectations(&dv_test);
3979 }
3980
Christopher Wiley1057cd72013-02-28 15:21:29 -08003981 // Now for a slightly more complex variant. When we encrypt data,
3982 // we do the same verification step but monkey with the callback to
3983 // link ourselves to an encrypt step afterward.
3984 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07003985 LOG(INFO) << "Basic VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08003986 ResultBoolCallback passed_down_callback;
3987 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
3988 kFakePublicKey,
3989 kFakeNonce,
3990 kFakeSignedData,
3991 kFakeUdn,
3992 kSSID,
3993 _,
3994 _,
3995 _))
3996 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
3997
3998 Error error(Error::kOperationInitiated);
3999 ResultStringCallback cb = Bind(
4000 &DestinationVerificationTest::ResultStringCallbackStub,
4001 dv_test.AsWeakPtr());
4002 manager()->VerifyAndEncryptData(kFakeCertificate,
4003 kFakePublicKey,
4004 kFakeNonce,
4005 kFakeSignedData,
4006 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004007 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08004008 kFakeData,
4009 cb,
4010 &error);
4011 ASSERT_TRUE(error.IsOngoing());
4012 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4013 // Now, if we call that passed down callback, we should see encrypt being
4014 // called.
4015 ResultStringCallback second_passed_down_callback;
4016 EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey,
4017 kFakeData,
4018 _,
4019 _))
4020 .Times(1)
4021 .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback),
4022 Return(true)));
4023 Error e;
4024 passed_down_callback.Run(e, true);
4025 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4026 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1);
4027 // And if we call the second passed down callback, we should see the
4028 // original function we passed down to VerifyDestination getting called.
4029 e.Reset();
4030 second_passed_down_callback.Run(e, "");
4031 Mock::VerifyAndClearExpectations(&dv_test);
4032 }
4033
4034 // If verification fails on the way to trying to encrypt, we should ditch
4035 // without calling encrypt at all.
4036 {
Christopher Wileycdde79f2013-05-01 14:26:56 -07004037 LOG(INFO) << "Failed VerifyAndEncryptData";
Christopher Wiley1057cd72013-02-28 15:21:29 -08004038 ResultBoolCallback passed_down_callback;
4039 EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate,
4040 kFakePublicKey,
4041 kFakeNonce,
4042 kFakeSignedData,
4043 kFakeUdn,
4044 kSSID,
4045 _,
4046 _,
4047 _))
4048 .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true)));
4049
4050 Error error(Error::kOperationInitiated);
4051 ResultStringCallback cb = Bind(
4052 &DestinationVerificationTest::ResultStringCallbackStub,
4053 dv_test.AsWeakPtr());
4054 manager()->VerifyAndEncryptData(kFakeCertificate,
4055 kFakePublicKey,
4056 kFakeNonce,
4057 kFakeSignedData,
4058 kFakeUdn,
Christopher Wileycdde79f2013-05-01 14:26:56 -07004059 "", "",
Christopher Wiley1057cd72013-02-28 15:21:29 -08004060 kFakeData,
4061 cb,
4062 &error);
4063 ASSERT_TRUE(error.IsOngoing());
4064 Mock::VerifyAndClearExpectations(crypto_util_proxy_);
4065 Error e(Error::kOperationFailed);
4066 EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0);
4067 // Although we're ditching, this callback is what cleans up the pending
4068 // DBus call.
4069 EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1);
4070 passed_down_callback.Run(e, false);
4071 Mock::VerifyAndClearExpectations(&dv_test);
4072 }
4073}
4074
Paul Stewartd2e1c362013-03-03 19:06:07 -08004075TEST_F(ManagerTest, IsProfileBefore) {
4076 scoped_refptr<MockProfile> profile0(
4077 new NiceMock<MockProfile>(
4078 control_interface(), metrics(), manager(), ""));
4079 scoped_refptr<MockProfile> profile1(
4080 new NiceMock<MockProfile>(
4081 control_interface(), metrics(), manager(), ""));
4082
4083 AdoptProfile(manager(), profile0);
4084 AdoptProfile(manager(), profile1); // profile1 is after profile0.
4085 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1));
4086 EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0));
4087
4088 // A few abnormal cases, but it's good to track their behavior.
4089 scoped_refptr<MockProfile> profile2(
4090 new NiceMock<MockProfile>(
4091 control_interface(), metrics(), manager(), ""));
4092 EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2));
4093 EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2));
4094 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0));
4095 EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1));
4096}
4097
Paul Stewart967eaeb2013-04-25 19:53:07 -07004098TEST_F(ManagerTest, GetLoadableProfileEntriesForService) {
4099 MockStore storage0;
4100 MockStore storage1;
4101 MockStore storage2;
4102
4103 scoped_refptr<MockProfile> profile0(
4104 new NiceMock<MockProfile>(
4105 control_interface(), metrics(), manager(), ""));
4106 scoped_refptr<MockProfile> profile1(
4107 new NiceMock<MockProfile>(
4108 control_interface(), metrics(), manager(), ""));
4109 scoped_refptr<MockProfile> profile2(
4110 new NiceMock<MockProfile>(
4111 control_interface(), metrics(), manager(), ""));
4112
4113 AdoptProfile(manager(), profile0);
4114 AdoptProfile(manager(), profile1);
4115 AdoptProfile(manager(), profile2);
4116
4117 scoped_refptr<MockService> service(
4118 new NiceMock<MockService>(control_interface(),
4119 dispatcher(),
4120 metrics(),
4121 manager()));
4122
4123 EXPECT_CALL(*profile0, GetConstStorage()).WillOnce(Return(&storage0));
4124 EXPECT_CALL(*profile1, GetConstStorage()).WillOnce(Return(&storage1));
4125 EXPECT_CALL(*profile2, GetConstStorage()).WillOnce(Return(&storage2));
4126
4127 const string kEntry0("aluminum_crutch");
4128 const string kEntry2("rehashed_faces");
4129
4130 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage0)))
4131 .WillOnce(Return(kEntry0));
4132 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage1)))
4133 .WillOnce(Return(""));
4134 EXPECT_CALL(*service, GetLoadableStorageIdentifier(Ref(storage2)))
4135 .WillOnce(Return(kEntry2));
4136
4137 const string kProfileRpc0("service_station");
4138 const string kProfileRpc2("crystal_tiaras");
4139
4140 EXPECT_CALL(*profile0, GetRpcIdentifier()).WillOnce(Return(kProfileRpc0));
4141 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(0);
4142 EXPECT_CALL(*profile2, GetRpcIdentifier()).WillOnce(Return(kProfileRpc2));
4143
4144 map<string, string> entries =
4145 manager()->GetLoadableProfileEntriesForService(service);
4146 EXPECT_EQ(2, entries.size());
4147 EXPECT_TRUE(ContainsKey(entries, kProfileRpc0));
4148 EXPECT_TRUE(ContainsKey(entries, kProfileRpc2));
4149 EXPECT_EQ(kEntry0, entries[kProfileRpc0]);
4150 EXPECT_EQ(kEntry2, entries[kProfileRpc2]);
4151}
4152
mukesh agrawal00752532013-05-03 15:46:55 -07004153TEST_F(ManagerTest, InitializeProfilesInformsProviders) {
mukesh agrawald142fd62013-05-01 16:50:57 -07004154 // We need a real glib here, so that profiles are persisted.
4155 GLib glib;
4156 ScopedTempDir temp_dir;
4157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4158 Manager manager(control_interface(),
4159 dispatcher(),
4160 metrics(),
4161 &glib,
4162 run_path(),
4163 storage_path(),
4164 temp_dir.path().value());
4165 // Can't use |wifi_provider_|, because it's owned by the Manager
4166 // object in the fixture.
4167 MockWiFiProvider *wifi_provider = new NiceMock<MockWiFiProvider>();
4168 manager.wifi_provider_.reset(wifi_provider); // pass ownership
Paul Stewartb87d22b2013-07-29 11:11:37 -07004169 manager.UpdateProviderMapping();
mukesh agrawald142fd62013-05-01 16:50:57 -07004170 // Give manager a valid place to write the user profile list.
4171 manager.user_profile_list_path_ = temp_dir.path().Append("user_profile_list");
4172
4173 // With no user profiles, the WiFiProvider should be called once
4174 // (for the default profile).
4175 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_));
4176 manager.InitializeProfiles();
4177 Mock::VerifyAndClearExpectations(wifi_provider);
4178
4179 // With |n| user profiles, the WiFiProvider should be called |n+1|
4180 // times. First, create 2 user profiles...
4181 const char kProfile0[] = "~user/profile0";
4182 const char kProfile1[] = "~user/profile1";
4183 string profile_rpc_path;
4184 Error error;
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004185 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawald142fd62013-05-01 16:50:57 -07004186 manager.CreateProfile(kProfile0, &profile_rpc_path, &error);
4187 manager.PushProfile(kProfile0, &profile_rpc_path, &error);
4188 manager.CreateProfile(kProfile1, &profile_rpc_path, &error);
4189 manager.PushProfile(kProfile1, &profile_rpc_path, &error);
4190
4191 // ... then reset manager state ...
4192 manager.profiles_.clear();
4193
4194 // ...then check that the WiFiProvider is notified about all three
4195 // profiles (one default, two user).
4196 EXPECT_CALL(*wifi_provider, CreateServicesFromProfile(_)).Times(3);
4197 manager.InitializeProfiles();
4198 Mock::VerifyAndClearExpectations(wifi_provider);
4199}
4200
mukesh agrawal00752532013-05-03 15:46:55 -07004201TEST_F(ManagerTest, InitializeProfilesHandlesDefaults) {
4202 // We need a real glib here, so that profiles are persisted.
4203 GLib glib;
4204 ScopedTempDir temp_dir;
Ben Chancd477322014-10-17 14:19:30 -07004205 std::unique_ptr<Manager> manager;
mukesh agrawal00752532013-05-03 15:46:55 -07004206 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4207
4208 // Instantiate a Manager with empty persistent storage. Check that
4209 // defaults are set.
4210 //
4211 // Note that we use the same directory for default and user profiles.
4212 // This doesn't affect the test results, because we don't push a
4213 // user profile.
4214 manager.reset(new Manager(control_interface(),
4215 dispatcher(),
4216 metrics(),
4217 &glib,
4218 run_path(),
4219 temp_dir.path().value(),
4220 temp_dir.path().value()));
4221 manager->InitializeProfiles();
4222 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4223 manager->props_.check_portal_list);
4224 EXPECT_EQ(Resolver::kDefaultIgnoredSearchList,
4225 manager->props_.ignored_dns_search_paths);
4226 EXPECT_EQ(LinkMonitor::kDefaultLinkMonitorTechnologies,
4227 manager->props_.link_monitor_technologies);
Rebecca Silberstein3d49ea42014-08-21 11:20:50 -07004228 EXPECT_EQ(ConnectivityTrial::kDefaultURL,
mukesh agrawal00752532013-05-03 15:46:55 -07004229 manager->props_.portal_url);
4230 EXPECT_EQ(PortalDetector::kDefaultCheckIntervalSeconds,
4231 manager->props_.portal_check_interval_seconds);
4232
4233 // Change one of the settings.
4234 static const string kCustomCheckPortalList = "fiber0";
4235 Error error;
4236 manager->SetCheckPortalList(kCustomCheckPortalList, &error);
4237 manager->profiles_[0]->Save();
4238
4239 // Instantiate a new manager. It should have our settings for
4240 // check_portal_list, rather than the default.
4241 manager.reset(new Manager(control_interface(),
4242 dispatcher(),
4243 metrics(),
4244 &glib,
4245 run_path(),
4246 temp_dir.path().value(),
4247 temp_dir.path().value()));
4248 manager->InitializeProfiles();
4249 EXPECT_EQ(kCustomCheckPortalList, manager->props_.check_portal_list);
4250
4251 // If we clear the persistent storage, we again get the default value.
4252 ASSERT_TRUE(temp_dir.Delete());
4253 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4254 manager.reset(new Manager(control_interface(),
4255 dispatcher(),
4256 metrics(),
4257 &glib,
4258 run_path(),
4259 temp_dir.path().value(),
4260 temp_dir.path().value()));
4261 manager->InitializeProfiles();
4262 EXPECT_EQ(PortalDetector::kDefaultCheckPortalList,
4263 manager->props_.check_portal_list);
4264}
4265
mukesh agrawalb94adde2013-08-22 18:17:26 -07004266TEST_F(ManagerTest, ProfileStackChangeLogging) {
4267 // We use a real glib here, since Manager and Profile don't provide an
4268 // easy way to mock out KeyFileStore.
4269 GLib glib;
4270 ScopedTempDir temp_dir;
Ben Chancd477322014-10-17 14:19:30 -07004271 std::unique_ptr<Manager> manager;
mukesh agrawalb94adde2013-08-22 18:17:26 -07004272 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
4273 manager.reset(new Manager(control_interface(),
4274 dispatcher(),
4275 metrics(),
4276 &glib,
4277 run_path(),
4278 temp_dir.path().value(),
4279 temp_dir.path().value()));
4280
4281 ScopedMockLog log;
4282 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
4283 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4284 manager->InitializeProfiles();
4285
4286 const char kProfile0[] = "~user/profile0";
4287 const char kProfile1[] = "~user/profile1";
4288 const char kProfile2[] = "~user/profile2";
mukesh agrawal0a59a5a2014-04-24 19:10:46 -07004289 ASSERT_TRUE(base::CreateDirectory(temp_dir.path().Append("user")));
mukesh agrawalb94adde2013-08-22 18:17:26 -07004290 TestCreateProfile(manager.get(), kProfile0);
4291 TestCreateProfile(manager.get(), kProfile1);
4292 TestCreateProfile(manager.get(), kProfile2);
4293
4294 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4295 TestPushProfile(manager.get(), kProfile0);
4296
4297 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4298 TestInsertUserProfile(manager.get(), kProfile1, "not-so-random-string");
4299
4300 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("4 profile(s)")));
4301 TestInsertUserProfile(manager.get(), kProfile2, "very-random-string");
4302
4303 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("3 profile(s)")));
4304 TestPopProfile(manager.get(), kProfile2);
4305
4306 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("2 profile(s)")));
4307 TestPopAnyProfile(manager.get());
4308
4309 EXPECT_CALL(log, Log(logging::LOG_INFO, _, HasSubstr("1 profile(s)")));
4310 TestPopAllUserProfiles(manager.get());
4311}
4312
mukesh agrawalbebf1b82013-04-23 15:06:33 -07004313// Custom property setters should return false, and make no changes, if
4314// the new value is the same as the old value.
4315TEST_F(ManagerTest, CustomSetterNoopChange) {
4316 // SetCheckPortalList
4317 {
4318 static const string kCheckPortalList = "weird-device,weirder-device";
4319 Error error;
4320 // Set to known value.
4321 EXPECT_TRUE(SetCheckPortalList(kCheckPortalList, &error));
4322 EXPECT_TRUE(error.IsSuccess());
4323 // Set to same value.
4324 EXPECT_FALSE(SetCheckPortalList(kCheckPortalList, &error));
4325 EXPECT_TRUE(error.IsSuccess());
4326 }
4327
4328 // SetIgnoredDNSSearchPaths
4329 {
4330 NiceMock<MockResolver> resolver;
4331 static const string kIgnoredPaths = "example.com,example.org";
4332 Error error;
4333 SetResolver(&resolver);
4334 // Set to known value.
4335 EXPECT_CALL(resolver, set_ignored_search_list(_));
4336 EXPECT_TRUE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4337 EXPECT_TRUE(error.IsSuccess());
4338 Mock::VerifyAndClearExpectations(&resolver);
4339 // Set to same value.
4340 EXPECT_CALL(resolver, set_ignored_search_list(_)).Times(0);
4341 EXPECT_FALSE(SetIgnoredDNSSearchPaths(kIgnoredPaths, &error));
4342 EXPECT_TRUE(error.IsSuccess());
4343 Mock::VerifyAndClearExpectations(&resolver);
4344 }
4345}
4346
Paul Stewart7de7e022013-08-28 09:42:50 -07004347TEST_F(ManagerTest, GeoLocation) {
4348 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4349
4350 auto device = make_scoped_refptr(new NiceMock<MockDevice>(control_interface(),
4351 dispatcher(),
4352 metrics(),
4353 manager(),
4354 "null",
4355 "addr",
4356 0));
4357
4358 // Manager should ignore gelocation info from technologies it does not know.
4359 EXPECT_CALL(*device, technology())
4360 .Times(AtLeast(1))
4361 .WillRepeatedly(Return(Technology::kEthernet));
4362 EXPECT_CALL(*device, GetGeolocationObjects()).Times(0);
4363 manager()->OnDeviceGeolocationInfoUpdated(device);
4364 Mock::VerifyAndClearExpectations(device);
4365 EXPECT_TRUE(manager()->GetNetworksForGeolocation().empty());
4366
4367 // Manager should add WiFi geolocation info.
4368 EXPECT_CALL(*device, technology())
4369 .Times(AtLeast(1))
4370 .WillRepeatedly(Return(Technology::kWifi));
4371 EXPECT_CALL(*device, GetGeolocationObjects())
4372 .WillOnce(Return(vector<GeolocationInfo>()));
4373 manager()->OnDeviceGeolocationInfoUpdated(device);
4374 Mock::VerifyAndClearExpectations(device);
4375 auto location_infos = manager()->GetNetworksForGeolocation();
4376 EXPECT_EQ(1, location_infos.size());
4377 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4378
4379 // Manager should inclusively add cellular info.
4380 EXPECT_CALL(*device, technology())
4381 .Times(AtLeast(1))
4382 .WillRepeatedly(Return(Technology::kCellular));
4383 EXPECT_CALL(*device, GetGeolocationObjects())
4384 .WillOnce(Return(vector<GeolocationInfo>()));
4385 manager()->OnDeviceGeolocationInfoUpdated(device);
4386 location_infos = manager()->GetNetworksForGeolocation();
4387 EXPECT_EQ(2, location_infos.size());
4388 EXPECT_TRUE(ContainsKey(location_infos, kGeoWifiAccessPointsProperty));
4389 EXPECT_TRUE(ContainsKey(location_infos, kGeoCellTowersProperty));
4390}
4391
Peter Qiu574996a2014-04-04 10:55:47 -07004392TEST_F(ManagerTest, IsWifiIdle) {
4393 // No registered service.
4394 EXPECT_FALSE(manager()->IsWifiIdle());
4395
4396 scoped_refptr<MockService> wifi_service(new MockService(control_interface(),
4397 dispatcher(),
4398 metrics(),
4399 manager()));
4400
4401 scoped_refptr<MockService> cell_service(new MockService(control_interface(),
4402 dispatcher(),
4403 metrics(),
4404 manager()));
4405
4406 manager()->RegisterService(wifi_service);
4407 manager()->RegisterService(cell_service);
4408
4409 EXPECT_CALL(*wifi_service.get(), technology())
4410 .WillRepeatedly(Return(Technology::kWifi));
4411 EXPECT_CALL(*cell_service.get(), technology())
4412 .WillRepeatedly(Return(Technology::kCellular));
4413
4414 // Cellular is connected.
4415 EXPECT_CALL(*cell_service.get(), IsConnected())
4416 .WillRepeatedly(Return(true));
4417 manager()->UpdateService(cell_service);
4418
4419 // No wifi connection attempt.
4420 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4421 .WillRepeatedly(Return(false));
4422 EXPECT_CALL(*wifi_service.get(), IsConnected())
4423 .WillRepeatedly(Return(false));
4424 manager()->UpdateService(wifi_service);
4425 EXPECT_TRUE(manager()->IsWifiIdle());
4426
4427 // Attempt wifi connection.
4428 Mock::VerifyAndClearExpectations(wifi_service);
4429 EXPECT_CALL(*wifi_service.get(), technology())
4430 .WillRepeatedly(Return(Technology::kWifi));
4431 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4432 .WillRepeatedly(Return(true));
4433 EXPECT_CALL(*wifi_service.get(), IsConnected())
4434 .WillRepeatedly(Return(false));
4435 manager()->UpdateService(wifi_service);
4436 EXPECT_FALSE(manager()->IsWifiIdle());
4437
4438 // wifi connected.
4439 Mock::VerifyAndClearExpectations(wifi_service);
4440 EXPECT_CALL(*wifi_service.get(), technology())
4441 .WillRepeatedly(Return(Technology::kWifi));
4442 EXPECT_CALL(*wifi_service.get(), IsConnecting())
4443 .WillRepeatedly(Return(false));
4444 EXPECT_CALL(*wifi_service.get(), IsConnected())
4445 .WillRepeatedly(Return(true));
4446 manager()->UpdateService(wifi_service);
4447 EXPECT_FALSE(manager()->IsWifiIdle());
4448}
4449
Paul Stewart2cb3fa72014-11-13 01:43:12 -08004450TEST_F(ManagerTest, DetectMultiHomedDevices) {
4451 vector<scoped_refptr<MockConnection>> mock_connections;
4452 vector<ConnectionRefPtr> device_connections;
Paul Stewart182888e2014-11-13 01:43:12 -08004453 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
4454 dispatcher(),
4455 metrics(),
4456 manager(),
4457 "null4",
4458 "addr4",
4459 0));
4460 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
4461 dispatcher(),
4462 metrics(),
4463 manager(),
4464 "null5",
4465 "addr5",
4466 0));
Paul Stewart2cb3fa72014-11-13 01:43:12 -08004467 for (const auto &device : mock_devices_) {
4468 manager()->RegisterDevice(device);
4469 mock_connections.emplace_back(
4470 new NiceMock<MockConnection>(device_info_.get()));
4471 device_connections.emplace_back(mock_connections.back());
4472 }
4473 EXPECT_CALL(*mock_connections[1], GetSubnetName()).WillOnce(Return("1"));
4474 EXPECT_CALL(*mock_connections[2], GetSubnetName()).WillOnce(Return("2"));
4475 EXPECT_CALL(*mock_connections[3], GetSubnetName()).WillOnce(Return("1"));
Paul Stewart182888e2014-11-13 01:43:12 -08004476 EXPECT_CALL(*mock_connections[4], GetSubnetName()).WillOnce(Return(""));
4477 EXPECT_CALL(*mock_connections[5], GetSubnetName()).WillOnce(Return(""));
Paul Stewart2cb3fa72014-11-13 01:43:12 -08004478
4479 // Do not assign a connection to mock_devices_[0].
4480 EXPECT_CALL(*mock_devices_[1], connection())
4481 .WillRepeatedly(ReturnRef(device_connections[1]));
4482 EXPECT_CALL(*mock_devices_[2], connection())
4483 .WillRepeatedly(ReturnRef(device_connections[2]));
4484 EXPECT_CALL(*mock_devices_[3], connection())
4485 .WillRepeatedly(ReturnRef(device_connections[3]));
Paul Stewart182888e2014-11-13 01:43:12 -08004486 EXPECT_CALL(*mock_devices_[4], connection())
4487 .WillRepeatedly(ReturnRef(device_connections[4]));
4488 EXPECT_CALL(*mock_devices_[5], connection())
4489 .WillRepeatedly(ReturnRef(device_connections[5]));
Paul Stewart2cb3fa72014-11-13 01:43:12 -08004490
4491 EXPECT_CALL(*mock_devices_[0], SetIsMultiHomed(false));
4492 EXPECT_CALL(*mock_devices_[1], SetIsMultiHomed(true));
4493 EXPECT_CALL(*mock_devices_[2], SetIsMultiHomed(false));
4494 EXPECT_CALL(*mock_devices_[3], SetIsMultiHomed(true));
Paul Stewart182888e2014-11-13 01:43:12 -08004495 EXPECT_CALL(*mock_devices_[4], SetIsMultiHomed(false));
4496 EXPECT_CALL(*mock_devices_[5], SetIsMultiHomed(false));
Paul Stewart2cb3fa72014-11-13 01:43:12 -08004497 manager()->DetectMultiHomedDevices();
4498}
4499
Paul Stewartff6be292014-11-24 17:05:03 -08004500TEST_F(ManagerTest, IsTechnologyProhibited) {
4501 // Test initial state.
4502 EXPECT_EQ("", manager()->props_.prohibited_technologies);
4503 EXPECT_FALSE(manager()->IsTechnologyProhibited(Technology::kWiMax));
4504 EXPECT_FALSE(manager()->IsTechnologyProhibited(Technology::kVPN));
4505
4506 Error smoke_error;
4507 EXPECT_FALSE(manager()->SetProhibitedTechnologies("smoke_signal",
4508 &smoke_error));
4509 EXPECT_EQ(Error::kInvalidArguments, smoke_error.type());
4510
4511 ON_CALL(*mock_devices_[0], technology())
4512 .WillByDefault(Return(Technology::kVPN));
4513 ON_CALL(*mock_devices_[1], technology())
4514 .WillByDefault(Return(Technology::kWiMax));
4515 ON_CALL(*mock_devices_[2], technology())
4516 .WillByDefault(Return(Technology::kWifi));
4517
4518 manager()->RegisterDevice(mock_devices_[0]);
4519 manager()->RegisterDevice(mock_devices_[1]);
4520 manager()->RegisterDevice(mock_devices_[2]);
4521
4522 // Registered devices of prohibited technology types should be disabled.
4523 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
4524 EXPECT_CALL(*mock_devices_[1], SetEnabledPersistent(false, _, _));
4525 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(false, _, _)).Times(0);
4526 Error error;
4527 manager()->SetProhibitedTechnologies("wimax,vpn", &error);
4528 EXPECT_TRUE(manager()->IsTechnologyProhibited(Technology::kVPN));
4529 EXPECT_TRUE(manager()->IsTechnologyProhibited(Technology::kWiMax));
4530 EXPECT_FALSE(manager()->IsTechnologyProhibited(Technology::kWifi));
4531 Mock::VerifyAndClearExpectations(mock_devices_[0]);
4532 Mock::VerifyAndClearExpectations(mock_devices_[1]);
4533 Mock::VerifyAndClearExpectations(mock_devices_[2]);
4534
4535 // Newly registered devices should be disabled.
4536 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
4537 dispatcher(),
4538 metrics(),
4539 manager(),
4540 "null4",
4541 "addr4",
4542 0));
4543 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
4544 dispatcher(),
4545 metrics(),
4546 manager(),
4547 "null5",
4548 "addr5",
4549 0));
4550 ON_CALL(*mock_devices_[3], technology())
4551 .WillByDefault(Return(Technology::kVPN));
4552 ON_CALL(*mock_devices_[4], technology())
4553 .WillByDefault(Return(Technology::kWiMax));
4554 ON_CALL(*mock_devices_[5], technology())
4555 .WillByDefault(Return(Technology::kWifi));
4556
4557 EXPECT_CALL(*mock_devices_[3], SetEnabledPersistent(false, _, _));
4558 EXPECT_CALL(*mock_devices_[4], SetEnabledPersistent(false, _, _));
4559 EXPECT_CALL(*mock_devices_[5], SetEnabledPersistent(false, _, _)).Times(0);
4560
4561 manager()->RegisterDevice(mock_devices_[3]);
4562 manager()->RegisterDevice(mock_devices_[4]);
4563 manager()->RegisterDevice(mock_devices_[5]);
4564 Mock::VerifyAndClearExpectations(mock_devices_[3]);
4565 Mock::VerifyAndClearExpectations(mock_devices_[4]);
4566 Mock::VerifyAndClearExpectations(mock_devices_[5]);
4567
4568 // Calls to enable a non-prohibited technology should succeed.
4569 Error enable_error(Error::kOperationInitiated);
4570 DisableTechnologyReplyHandler technology_reply_handler;
4571 ResultCallback enable_technology_callback(
4572 Bind(&DisableTechnologyReplyHandler::ReportResult,
4573 technology_reply_handler.AsWeakPtr()));
4574 EXPECT_CALL(*mock_devices_[2], SetEnabledPersistent(true, _, _));
4575 EXPECT_CALL(*mock_devices_[5], SetEnabledPersistent(true, _, _));
4576 manager()->SetEnabledStateForTechnology(
4577 "wifi", true, &enable_error, enable_technology_callback);
4578 EXPECT_EQ(Error::kOperationInitiated, enable_error.type());
4579
4580 // Calls to enable a prohibited technology should fail.
4581 Error enable_prohibited_error(Error::kOperationInitiated);
4582 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _)).Times(0);
4583 EXPECT_CALL(*mock_devices_[3], SetEnabledPersistent(true, _, _)).Times(0);
4584 manager()->SetEnabledStateForTechnology(
4585 "vpn", true, &enable_prohibited_error, enable_technology_callback);
4586 EXPECT_EQ(Error::kPermissionDenied, enable_prohibited_error.type());
4587}
4588
Chris Masone9be4a9d2011-05-16 15:44:09 -07004589} // namespace shill