blob: 0a6a9c38bf37d2138ea3be81e2fa33e18d5bfc84 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone9be4a9d2011-05-16 15:44:09 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07004
5#include "shill/manager.h"
6
Jason Glasgowdf7c5532012-05-14 14:41:45 -04007#include <map>
Chris Masone6791a432011-07-12 13:23:19 -07008#include <set>
9
Chris Masone9be4a9d2011-05-16 15:44:09 -070010#include <glib.h>
11
Paul Stewarte73d05c2012-03-29 16:26:05 -070012#include <base/file_util.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/scoped_temp_dir.h>
14#include <base/stl_util.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070015#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070017#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070018#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070019
mukesh agrawal32399322011-09-01 10:53:43 -070020#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070021#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070022#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070023#include "shill/glib.h"
24#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070025#include "shill/key_value_store.h"
Christopher Wiley3e7635e2012-08-15 09:46:17 -070026#include "shill/logging.h"
mukesh agrawal32399322011-09-01 10:53:43 -070027#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080028#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070030#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080031#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070032#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000033#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020034#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070035#include "shill/mock_profile.h"
Paul Stewart4d5efb72012-09-17 12:24:34 -070036#include "shill/mock_resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070037#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070038#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070039#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070040#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070041#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020042#include "shill/proxy_factory.h"
Chris Masone6515aab2011-10-12 16:19:09 -070043#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070044#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020045#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070046
47using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070048using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070049using std::string;
50using std::vector;
51
Chris Masone9be4a9d2011-05-16 15:44:09 -070052namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070053using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070054using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080055using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070056using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070057using ::testing::InSequence;
mukesh agrawal784566d2012-08-08 18:32:58 -070058using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070059using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070060using ::testing::NiceMock;
61using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070062using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070063using ::testing::SaveArg;
Gaurav Shah435de2c2011-11-17 19:01:07 -080064using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080065using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070066using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070067
Chris Masone3bd3c8c2011-06-13 08:20:26 -070068class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070069 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070070 ManagerTest()
Darin Petkov3ec55342012-09-28 14:04:44 +020071 : power_manager_(new MockPowerManager(NULL, &proxy_factory_)),
Darin Petkovca621542012-07-25 14:25:56 +020072 mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080074 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070075 manager(),
76 "wifi0",
77 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080078 4)),
79 device_info_(new NiceMock<MockDeviceInfo>(
80 control_interface(),
81 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080082 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080083 reinterpret_cast<Manager*>(NULL))),
84 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070085 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
86 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080087 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070088 manager(),
89 "null0",
90 "addr0",
91 0));
92 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
93 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080094 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070095 manager(),
96 "null1",
97 "addr1",
98 1));
99 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
100 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800101 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700102 manager(),
103 "null2",
104 "addr2",
105 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800106 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
107 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800108 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800109 manager(),
110 "null3",
111 "addr3",
112 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700113 manager()->connect_profiles_to_rpc_ = false;
Paul Stewart63864b62012-11-07 15:10:55 -0800114 SetRunning(true);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800115
116 // Replace the manager's adaptor with a quieter one, and one
117 // we can do EXPECT*() against. Passes ownership.
118 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700119 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700120 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700121
Paul Stewartfdd16072011-09-16 12:41:35 -0700122 bool IsDeviceRegistered(const DeviceRefPtr &device,
123 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700124 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700125 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700126 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700127 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700128 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700129
Paul Stewarta849a3d2011-11-03 05:54:09 -0700130 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
131 manager->profiles_.push_back(profile);
132 }
133
Paul Stewart63864b62012-11-07 15:10:55 -0800134 void SetRunning(bool running) {
135 manager()->running_ = running;
136 }
137
Paul Stewart75225512012-01-26 22:51:33 -0800138 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
139 return manager->ephemeral_profile_;
140 }
141
Chris Masone6515aab2011-10-12 16:19:09 -0700142 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
143 Profile::Identifier id("rather", "irrelevant");
144 scoped_ptr<Profile> profile(new Profile(control_interface(),
145 manager,
146 id,
147 "",
148 false));
149 FilePath final_path(storage_path());
150 final_path = final_path.Append("test.profile");
151 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
152 storage->set_path(final_path);
153 if (!storage->Open())
154 return NULL;
155 profile->set_storage(storage.release()); // Passes ownership.
156 return profile.release();
157 }
158
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700159 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
160 const string &profile_identifier,
161 const string &service_name) {
162 GLib glib;
163 KeyFileStore store(&glib);
164 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
165 return store.Open() &&
166 store.SetString(service_name, "rather", "irrelevant") &&
167 store.Close();
168 }
169
170 Error::Type TestCreateProfile(Manager *manager, const string &name) {
171 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800172 string path;
173 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700174 return error.type();
175 }
176
177 Error::Type TestPopAnyProfile(Manager *manager) {
178 Error error;
179 manager->PopAnyProfile(&error);
180 return error.type();
181 }
182
183 Error::Type TestPopProfile(Manager *manager, const string &name) {
184 Error error;
185 manager->PopProfile(name, &error);
186 return error.type();
187 }
188
189 Error::Type TestPushProfile(Manager *manager, const string &name) {
190 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800191 string path;
192 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700193 return error.type();
194 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000195
Paul Stewartcb3eb892012-06-07 14:24:46 -0700196 void AddMockProfileToManager(Manager *manager) {
197 scoped_refptr<MockProfile> profile(
198 new MockProfile(control_interface(), manager, ""));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200199 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/"));
200 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700201 AdoptProfile(manager, profile);
202 }
203
Paul Stewartdfa46052012-06-26 09:44:14 -0700204 void CompleteServiceSort() {
205 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
206 dispatcher()->DispatchPendingEvents();
207 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
208 }
209
Paul Stewart49739c02012-08-08 17:24:03 -0700210 RpcIdentifier GetDefaultServiceRpcIdentifier() {
211 return manager()->GetDefaultServiceRpcIdentifier(NULL);
212 }
213
Paul Stewart4d5efb72012-09-17 12:24:34 -0700214 void SetResolver(Resolver *resolver) {
215 manager()->resolver_ = resolver;
216 }
217
218 void SetIgnoredDNSSearchPaths(const string &search_paths) {
219 manager()->SetIgnoredDNSSearchPaths(search_paths, NULL);
220 }
221
222 const string &GetIgnoredDNSSearchPaths() {
223 return manager()->props_.ignored_dns_search_paths;
224 }
225
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700226 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000227 typedef scoped_refptr<MockService> MockServiceRefPtr;
228
Darin Petkova5e07ef2012-07-09 14:27:57 +0200229 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
230 public:
231 ServiceWatcher() {}
232 virtual ~ServiceWatcher() {}
233
234 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
235
236 private:
237 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
238 };
239
Darin Petkovca621542012-07-25 14:25:56 +0200240 class TestProxyFactory : public ProxyFactory {
241 public:
242 TestProxyFactory() {}
243
244 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
245 PowerManagerProxyDelegate */*delegate*/) {
246 return NULL;
247 }
248
249 private:
250 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
251 };
252
Darin Petkov3ec55342012-09-28 14:04:44 +0200253 class TerminationActionTest :
254 public base::SupportsWeakPtr<TerminationActionTest> {
255 public:
256 static const char kActionName[];
257
258 TerminationActionTest() : manager_(NULL) {}
259 virtual ~TerminationActionTest() {}
260
261 MOCK_METHOD1(Done, void(const Error &error));
262
263 void Action() {
264 manager_->TerminationActionComplete("action");
265 }
266
267 void set_manager(Manager *manager) { manager_ = manager; }
268
269 private:
270 Manager *manager_;
271 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
272 };
273
Darin Petkovca621542012-07-25 14:25:56 +0200274 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
275 power_manager_->power_state_ = state;
276 }
277
278 void SetPowerManager() {
279 manager()->set_power_manager(power_manager_.release());
280 }
281
Darin Petkov3ec55342012-09-28 14:04:44 +0200282 HookTable *GetTerminationActions() {
283 return &manager()->termination_actions_;
284 }
285
Darin Petkovca621542012-07-25 14:25:56 +0200286 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
287 manager()->OnPowerStateChanged(state);
288 }
289
Darin Petkov3ec55342012-09-28 14:04:44 +0200290 void OnSuspendDelay(uint32 sequence_number) {
291 manager()->OnSuspendDelay(sequence_number);
292 }
293
294 void OnSuspendActionsComplete(uint32 sequence_number, const Error &error) {
295 manager()->OnSuspendActionsComplete(sequence_number, error);
296 }
297
Paul Stewartbfb82552012-10-24 16:48:48 -0700298 vector<string> EnumerateAvailableServices() {
299 return manager()->EnumerateAvailableServices(NULL);
300 }
301
302 vector<string> EnumerateWatchedServices() {
303 return manager()->EnumerateWatchedServices(NULL);
304 }
305
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000306 MockServiceRefPtr MakeAutoConnectableService() {
307 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
308 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800309 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000310 manager());
311 service->MakeFavorite();
312 service->set_connectable(true);
313 return service;
314 }
315
Darin Petkovca621542012-07-25 14:25:56 +0200316 TestProxyFactory proxy_factory_;
317 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700318 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700319 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800320 scoped_ptr<MockDeviceInfo> device_info_;
321
322 // This pointer is owned by the manager, and only tracked here for EXPECT*()
323 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700324};
325
Darin Petkov3ec55342012-09-28 14:04:44 +0200326const char ManagerTest::TerminationActionTest::kActionName[] = "action";
327
Paul Stewart22aa71b2011-09-16 12:15:11 -0700328bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700329 if (!manager()->sort_services_task_.IsCancelled()) {
330 manager()->SortServicesTask();
331 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700332 return (svc0.get() == manager()->services_[0].get() &&
333 svc1.get() == manager()->services_[1].get());
334}
335
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700336TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700337 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
338 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700339}
340
Chris Masone9be4a9d2011-05-16 15:44:09 -0700341TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700342 ON_CALL(*mock_devices_[0].get(), technology())
343 .WillByDefault(Return(Technology::kEthernet));
344 ON_CALL(*mock_devices_[1].get(), technology())
345 .WillByDefault(Return(Technology::kWifi));
346 ON_CALL(*mock_devices_[2].get(), technology())
347 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700348
Paul Stewart22aa71b2011-09-16 12:15:11 -0700349 manager()->RegisterDevice(mock_devices_[0]);
350 manager()->RegisterDevice(mock_devices_[1]);
351 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700352
Paul Stewart22aa71b2011-09-16 12:15:11 -0700353 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
354 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
355 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700356}
357
Paul Stewarta41e38d2011-11-11 07:47:29 -0800358TEST_F(ManagerTest, DeviceRegistrationAndStart) {
359 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500360 mock_devices_[0]->enabled_persistent_ = true;
361 mock_devices_[1]->enabled_persistent_ = false;
362 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800363 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500364 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800365 .Times(0);
366 manager()->RegisterDevice(mock_devices_[0]);
367 manager()->RegisterDevice(mock_devices_[1]);
368}
369
370TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
371 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
372 DeviceRefPtr device_ref(mock_devices_[0].get());
373 AdoptProfile(manager(), profile); // Passes ownership.
374 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200375 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800376 manager()->RegisterDevice(mock_devices_[0]);
377}
378
Chris Masone9be4a9d2011-05-16 15:44:09 -0700379TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700380 ON_CALL(*mock_devices_[0].get(), technology())
381 .WillByDefault(Return(Technology::kEthernet));
382 ON_CALL(*mock_devices_[1].get(), technology())
383 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700384
Gaurav Shah435de2c2011-11-17 19:01:07 -0800385 manager()->RegisterDevice(mock_devices_[0]);
386 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700387
Paul Stewart22aa71b2011-09-16 12:15:11 -0700388 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
389 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700390
Paul Stewart212d60f2012-07-12 10:59:13 -0700391 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
392 AdoptProfile(manager(), profile); // Passes ownership.
393
Eric Shienbrood9a245532012-03-07 14:20:39 -0500394 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700395 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800396 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700397 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700398
Eric Shienbrood9a245532012-03-07 14:20:39 -0500399 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700400 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800401 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700402 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700403}
404
405TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700406 // It's much easier and safer to use a real GLib for this test.
407 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700408 Manager manager(control_interface(),
409 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800410 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700411 &glib,
412 run_path(),
413 storage_path(),
414 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700415 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
416 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700417 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700418
Chris Masone9be4a9d2011-05-16 15:44:09 -0700419 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700420 new NiceMock<MockService>(control_interface(),
421 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800422 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700423 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700424 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700425 new NiceMock<MockService>(control_interface(),
426 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800427 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700428 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700429
mukesh agrawal51a7e932011-07-27 16:18:26 -0700430 string service1_name(mock_service->UniqueName());
431 string service2_name(mock_service2->UniqueName());
432
433 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
434 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700435 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700436 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700437 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700438 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700439 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700440
Chris Masone9d779932011-08-25 16:33:41 -0700441 manager.RegisterService(mock_service);
442 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700443
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800444 Error error;
445 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700446 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700447 EXPECT_EQ(2, ids.size());
448 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
449 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700450
Chris Masone9d779932011-08-25 16:33:41 -0700451 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
452 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
453
454 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700455}
456
Chris Masone6515aab2011-10-12 16:19:09 -0700457TEST_F(ManagerTest, RegisterKnownService) {
458 // It's much easier and safer to use a real GLib for this test.
459 GLib glib;
460 Manager manager(control_interface(),
461 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800462 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700463 &glib,
464 run_path(),
465 storage_path(),
466 string());
467 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
468 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700469 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700470 {
471 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
472 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800473 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700474 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700475 ASSERT_TRUE(profile->AdoptService(service1));
476 ASSERT_TRUE(profile->ContainsService(service1));
477 } // Force destruction of service1.
478
479 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
480 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800481 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700482 &manager));
483 manager.RegisterService(service2);
484 EXPECT_EQ(service2->profile().get(), profile.get());
485 manager.Stop();
486}
487
488TEST_F(ManagerTest, RegisterUnknownService) {
489 // It's much easier and safer to use a real GLib for this test.
490 GLib glib;
491 Manager manager(control_interface(),
492 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800493 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700494 &glib,
495 run_path(),
496 storage_path(),
497 string());
498 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
499 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700500 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700501 {
502 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
503 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800504 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700505 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700506 ASSERT_TRUE(profile->AdoptService(service1));
507 ASSERT_TRUE(profile->ContainsService(service1));
508 } // Force destruction of service1.
509 scoped_refptr<MockService> mock_service2(
510 new NiceMock<MockService>(control_interface(),
511 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800512 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700513 &manager));
514 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
515 .WillRepeatedly(Return(mock_service2->UniqueName()));
516 manager.RegisterService(mock_service2);
517 EXPECT_NE(mock_service2->profile().get(), profile.get());
518 manager.Stop();
519}
520
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000521TEST_F(ManagerTest, DeregisterUnregisteredService) {
522 // WiFi assumes that it can deregister a service that is not
523 // registered. (E.g. a hidden service can be deregistered when it
524 // loses its last endpoint, and again when WiFi is Stop()-ed.)
525 //
526 // So test that doing so doesn't cause a crash.
527 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
528 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800529 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000530 manager());
531 manager()->DeregisterService(service);
532}
533
Chris Masonea8a2c252011-06-27 22:16:30 -0700534TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700535 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700536 map<string, ::DBus::Variant> props;
537 Error error(Error::kInvalidProperty, "");
538 {
539 ::DBus::Error dbus_error;
540 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700541 manager()->mutable_store()->SetStringProperty(
542 flimflam::kCheckPortalListProperty,
543 expected,
544 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700545 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700546 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
547 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
548 expected);
549 }
550 {
551 ::DBus::Error dbus_error;
552 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700553 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
554 expected,
555 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700556 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700557 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
558 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
559 expected);
560 }
561}
562
Chris Masone3c3f6a12011-07-01 10:01:41 -0700563TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700564 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800565 manager()->RegisterDevice(mock_devices_[0]);
566 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700567 {
568 map<string, ::DBus::Variant> props;
569 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700570 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700571 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700572 vector < ::DBus::Path> devices =
573 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700574 EXPECT_EQ(2, devices.size());
575 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700576}
577
mukesh agrawal2366eed2012-03-20 18:21:50 -0700578TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700579 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700580 map<string, ::DBus::Variant> props;
581 ::DBus::Error dbus_error;
582 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
583 map<string, ::DBus::Variant>::const_iterator prop =
584 props.find(flimflam::kServicesProperty);
585 ASSERT_FALSE(prop == props.end());
586 const ::DBus::Variant &variant = prop->second;
587 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
588}
589
Chris Masone6791a432011-07-12 13:23:19 -0700590TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700591 Manager manager(control_interface(),
592 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800593 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700594 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700595 run_path(),
596 storage_path(),
597 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700598 scoped_refptr<MockService> s2(new MockService(control_interface(),
599 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800600 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700601 &manager));
602 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700603 {
Chris Masone6515aab2011-10-12 16:19:09 -0700604 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700605 ProfileRefPtr profile(
606 new Profile(control_interface(), &manager, id, "", false));
607 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700608 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700609 .WillRepeatedly(Return(true));
610 EXPECT_CALL(*storage, Flush())
611 .Times(AnyNumber())
612 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700613 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700614 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700615 }
Chris Masone6515aab2011-10-12 16:19:09 -0700616 // Create a profile that already has |s2| in it.
617 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700618 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700619
Chris Masone6515aab2011-10-12 16:19:09 -0700620 // Now, move the Service |s2| to another profile.
621 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
622 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700623
624 // Force destruction of the original Profile, to ensure that the Service
625 // is kept alive and populated with data.
626 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700627 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700628 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700629}
630
Paul Stewart7f61e522012-03-22 11:13:45 -0700631TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
632 scoped_refptr<MockProfile> mock_profile(
633 new MockProfile(control_interface(), manager(), ""));
634 const string kProfileName("profile0");
635 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
636 .WillRepeatedly(Return(kProfileName));
637 AdoptProfile(manager(), mock_profile);
638
639 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
640 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
641 EXPECT_EQ(mock_profile.get(), profile.get());
642}
643
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800644TEST_F(ManagerTest, SetProfileForService) {
645 scoped_refptr<MockProfile> profile0(
646 new MockProfile(control_interface(), manager(), ""));
647 string profile_name0("profile0");
648 EXPECT_CALL(*profile0, GetRpcIdentifier())
649 .WillRepeatedly(Return(profile_name0));
650 AdoptProfile(manager(), profile0);
651 scoped_refptr<MockService> service(new MockService(control_interface(),
652 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800653 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800654 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700655 EXPECT_FALSE(manager()->HasService(service));
656 {
657 Error error;
658 EXPECT_CALL(*profile0, AdoptService(_))
659 .WillOnce(Return(true));
660 // Expect that setting the profile of a service that does not already
661 // have one assigned does not cause a crash.
662 manager()->SetProfileForService(service, "profile0", &error);
663 EXPECT_TRUE(error.IsSuccess());
664 }
665
666 // The service should be registered as a side-effect of the profile being
667 // set for this service.
668 EXPECT_TRUE(manager()->HasService(service));
669
670 // Since we have mocked Profile::AdoptServie() above, the service's
671 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800672 service->set_profile(profile0);
673
674 {
675 Error error;
676 manager()->SetProfileForService(service, "foo", &error);
677 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700678 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800679 }
680
681 {
682 Error error;
683 manager()->SetProfileForService(service, profile_name0, &error);
684 EXPECT_EQ(Error::kInvalidArguments, error.type());
685 EXPECT_EQ("Service is already connected to this profile", error.message());
686 }
687
688 scoped_refptr<MockProfile> profile1(
689 new MockProfile(control_interface(), manager(), ""));
690 string profile_name1("profile1");
691 EXPECT_CALL(*profile1, GetRpcIdentifier())
692 .WillRepeatedly(Return(profile_name1));
693 AdoptProfile(manager(), profile1);
694
695 {
696 Error error;
697 EXPECT_CALL(*profile1, AdoptService(_))
698 .WillOnce(Return(true));
699 EXPECT_CALL(*profile0, AbandonService(_))
700 .WillOnce(Return(true));
701 manager()->SetProfileForService(service, profile_name1, &error);
702 EXPECT_TRUE(error.IsSuccess());
703 }
704}
705
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700706TEST_F(ManagerTest, CreateProfile) {
707 // It's much easier to use real Glib here since we want the storage
708 // side-effects.
709 GLib glib;
710 ScopedTempDir temp_dir;
711 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
712
713 Manager manager(control_interface(),
714 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800715 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700716 &glib,
717 run_path(),
718 storage_path(),
719 temp_dir.path().value());
720
721 // Invalid name should be rejected.
722 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
723
Paul Stewartd0a3b812012-03-28 22:48:22 -0700724 // A profile with invalid characters in it should similarly be rejected.
725 EXPECT_EQ(Error::kInvalidArguments,
726 TestCreateProfile(&manager, "valid_profile"));
727
728 // We should be able to create a machine profile.
729 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700730
Gary Morainb672d352012-04-25 09:19:06 -0700731 // We should succeed in creating a valid user profile. Verify the returned
732 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700733 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700734 {
735 Error error;
736 string path;
737 manager.CreateProfile(kProfile, &path, &error);
738 EXPECT_EQ(Error::kSuccess, error.type());
739 EXPECT_EQ("/profile_rpc", path);
740 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700741
742 // We should fail in creating it a second time (already exists).
743 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
744}
745
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700746// We receive PopProfile when a user logs out, and it should always trigger a
747// MemoryLog Clear() call.
748TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
749 GLib glib;
750 ScopedTempDir temp_dir;
751 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
752 Manager manager(control_interface(),
753 dispatcher(),
754 metrics(),
755 &glib,
756 run_path(),
757 storage_path(),
758 temp_dir.path().value());
759 const char kProfile0[] = "~user/profile0";
760 const char kPurgedMessage[] = "This message should be purged";
761 // Create a profile and push it on the stack, leave one uncreated
762 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
763 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
764
765 // Popping a profile which isn't on top should still clear the log.
766 LOG(INFO) << kPurgedMessage;
767 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
768 kPurgedMessage));
769 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
770 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
771 kPurgedMessage));
772
773 // Popping an invalid profile name should do the same thing.
774 LOG(INFO) << kPurgedMessage;
775 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
776 kPurgedMessage));
777 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
778 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
779 kPurgedMessage));
780
781 // Successful pops also purge the message log.
782 LOG(INFO) << kPurgedMessage;
783 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
784 kPurgedMessage));
785 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
786 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
787 kPurgedMessage));
788}
789
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700790TEST_F(ManagerTest, PushPopProfile) {
791 // It's much easier to use real Glib in creating a Manager for this
792 // test here since we want the storage side-effects.
793 GLib glib;
794 ScopedTempDir temp_dir;
795 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
796 Manager manager(control_interface(),
797 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800798 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700799 &glib,
800 run_path(),
801 storage_path(),
802 temp_dir.path().value());
803
804 // Pushing an invalid profile should fail.
805 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
806
Paul Stewartd0a3b812012-03-28 22:48:22 -0700807 // Pushing a default profile that does not exist should fail.
808 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700809
810 const char kProfile0[] = "~user/profile0";
811 const char kProfile1[] = "~user/profile1";
812
813 // Create a couple of profiles.
814 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
815 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
816
817 // Push these profiles on the stack.
818 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
819 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
820
821 // Pushing a profile a second time should fail.
822 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
823 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
824
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800825 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700826 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800827 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700828
829 // Make sure a profile name that doesn't exist fails.
830 const char kProfile2Id[] = "profile2";
831 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
832 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
833
834 // Create a new service, with a specific storage name.
835 scoped_refptr<MockService> service(
836 new NiceMock<MockService>(control_interface(),
837 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800838 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700839 &manager));
840 const char kServiceName[] = "service_storage_name";
841 EXPECT_CALL(*service.get(), GetStorageIdentifier())
842 .WillRepeatedly(Return(kServiceName));
843 EXPECT_CALL(*service.get(), Load(_))
844 .WillRepeatedly(Return(true));
845
846 // Add this service to the manager -- it should end up in the ephemeral
847 // profile.
848 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800849 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700850
851 // Create storage for a profile that contains the service storage name.
852 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
853 kServiceName));
854
855 // When we push the profile, the service should move away from the
856 // ephemeral profile to this new profile since it has an entry for
857 // this service.
858 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800859 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700860 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
861
862 // Insert another profile that should supersede ownership of the service.
863 const char kProfile3Id[] = "profile3";
864 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
865 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
866 kServiceName));
867 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
868 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
869
870 // Popping an invalid profile name should fail.
871 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
872
873 // Popping an profile that is not at the top of the stack should fail.
874 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
875
876 // Popping the top profile should succeed.
877 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
878
879 // Moreover the service should have switched profiles to profile 2.
880 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
881
882 // Popping the top profile should succeed.
883 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
884
885 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800886 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700887
888 // Pop the remaining two services off the stack.
889 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
890 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
891
892 // Next pop should fail with "stack is empty".
893 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700894
895 const char kMachineProfile0[] = "machineprofile0";
896 const char kMachineProfile1[] = "machineprofile1";
897 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
898 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
899
900 // Should be able to push a machine profile.
901 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
902
903 // Should be able to push a user profile atop a machine profile.
904 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
905
906 // Pushing a system-wide profile on top of a user profile should fail.
907 EXPECT_EQ(Error::kInvalidArguments,
908 TestPushProfile(&manager, kMachineProfile1));
909
910 // However if we pop the user profile, we should be able stack another
911 // machine profile on.
912 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
913 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700914}
915
Paul Stewarte73d05c2012-03-29 16:26:05 -0700916TEST_F(ManagerTest, RemoveProfile) {
917 // It's much easier to use real Glib in creating a Manager for this
918 // test here since we want the storage side-effects.
919 GLib glib;
920 ScopedTempDir temp_dir;
921 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
922 Manager manager(control_interface(),
923 dispatcher(),
924 metrics(),
925 &glib,
926 run_path(),
927 storage_path(),
928 temp_dir.path().value());
929
930 const char kProfile0[] = "profile0";
931 FilePath profile_path(
932 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
933
934 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
935 ASSERT_TRUE(file_util::PathExists(profile_path));
936
937 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
938
939 // Remove should fail since the profile is still on the stack.
940 {
941 Error error;
942 manager.RemoveProfile(kProfile0, &error);
943 EXPECT_EQ(Error::kInvalidArguments, error.type());
944 }
945
946 // Profile path should still exist.
947 EXPECT_TRUE(file_util::PathExists(profile_path));
948
949 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
950
951 // This should succeed now that the profile is off the stack.
952 {
953 Error error;
954 manager.RemoveProfile(kProfile0, &error);
955 EXPECT_EQ(Error::kSuccess, error.type());
956 }
957
958 // Profile path should no longer exist.
959 EXPECT_FALSE(file_util::PathExists(profile_path));
960
961 // Another remove succeeds, due to a foible in file_util::Delete --
962 // it is not an error to delete a file that does not exist.
963 {
964 Error error;
965 manager.RemoveProfile(kProfile0, &error);
966 EXPECT_EQ(Error::kSuccess, error.type());
967 }
968
969 // Let's create an error case that will "work". Create a non-empty
970 // directory in the place of the profile pathname.
971 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
972 {
973 Error error;
974 manager.RemoveProfile(kProfile0, &error);
975 EXPECT_EQ(Error::kOperationFailed, error.type());
976 }
977}
978
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700979TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
980 // It's much easier to use real Glib in creating a Manager for this
981 // test here since we want the storage side-effects.
982 GLib glib;
983 ScopedTempDir temp_dir;
984 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
985 Manager manager(control_interface(),
986 dispatcher(),
987 metrics(),
988 &glib,
989 run_path(),
990 storage_path(),
991 temp_dir.path().value());
992
993 const char kProfile0[] = "profile0";
994 FilePath profile_path(
995 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
996
997 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
998 ASSERT_TRUE(file_util::PathExists(profile_path));
999 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
1000
1001 // Ensure that even if the backing filestore is removed, we still can't
1002 // create a profile twice.
1003 ASSERT_TRUE(file_util::Delete(profile_path, false));
1004 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1005}
1006
Paul Stewart75225512012-01-26 22:51:33 -08001007// Use this matcher instead of passing RefPtrs directly into the arguments
1008// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1009// system teardown.
1010MATCHER_P(IsRefPtrTo, ref_address, "") {
1011 return arg.get() == ref_address;
1012}
1013
1014TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1015 MockServiceRefPtr s_not_in_profile(
1016 new NiceMock<MockService>(control_interface(),
1017 dispatcher(),
1018 metrics(),
1019 manager()));
1020 MockServiceRefPtr s_not_in_group(
1021 new NiceMock<MockService>(control_interface(),
1022 dispatcher(),
1023 metrics(),
1024 manager()));
1025 MockServiceRefPtr s_configure_fail(
1026 new NiceMock<MockService>(control_interface(),
1027 dispatcher(),
1028 metrics(),
1029 manager()));
1030 MockServiceRefPtr s_configure_succeed(
1031 new NiceMock<MockService>(control_interface(),
1032 dispatcher(),
1033 metrics(),
1034 manager()));
1035
1036 string entry_name("entry_name");
1037 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1038 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1039 .WillRepeatedly(Return("not_entry_name"));
1040 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1041 .WillRepeatedly(Return(entry_name));
1042 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1043 .WillRepeatedly(Return(entry_name));
1044
1045 manager()->RegisterService(s_not_in_profile);
1046 manager()->RegisterService(s_not_in_group);
1047 manager()->RegisterService(s_configure_fail);
1048 manager()->RegisterService(s_configure_succeed);
1049
1050 scoped_refptr<MockProfile> profile0(
1051 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1052 scoped_refptr<MockProfile> profile1(
1053 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1054
1055 s_not_in_group->set_profile(profile1);
1056 s_configure_fail->set_profile(profile1);
1057 s_configure_succeed->set_profile(profile1);
1058
1059 AdoptProfile(manager(), profile0);
1060 AdoptProfile(manager(), profile1);
1061
1062 // No services are a member of this profile.
1063 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1064
1065 // No services that are members of this profile have this entry name.
1066 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1067
1068 // Only services that are members of the profile and group will be abandoned.
1069 EXPECT_CALL(*profile1.get(),
1070 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1071 EXPECT_CALL(*profile1.get(),
1072 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1073 EXPECT_CALL(*profile1.get(),
1074 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1075 .WillOnce(Return(true));
1076 EXPECT_CALL(*profile1.get(),
1077 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1078 .WillOnce(Return(true));
1079
1080 // Never allow services to re-join profile1.
1081 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1082 .WillRepeatedly(Return(false));
1083
1084 // Only allow one of the members of the profile and group to successfully
1085 // join profile0.
1086 EXPECT_CALL(*profile0.get(),
1087 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1088 EXPECT_CALL(*profile0.get(),
1089 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1090 EXPECT_CALL(*profile0.get(),
1091 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1092 .WillOnce(Return(false));
1093 EXPECT_CALL(*profile0.get(),
1094 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1095 .WillOnce(Return(true));
1096
1097 // Expect the failed-to-configure service to have Unload() called on it.
1098 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1099 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1100 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1101 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1102
1103 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1104
1105 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1106 EXPECT_EQ(profile1, s_not_in_group->profile());
1107 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1108
1109 // Since we are using a MockProfile, the profile does not actually change,
1110 // since ConfigureService was not actually called on the service.
1111 EXPECT_EQ(profile1, s_configure_succeed->profile());
1112}
1113
Paul Stewart65512e12012-03-26 18:01:08 -07001114TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1115 MockServiceRefPtr s_will_remove0(
1116 new NiceMock<MockService>(control_interface(),
1117 dispatcher(),
1118 metrics(),
1119 manager()));
1120 MockServiceRefPtr s_will_remove1(
1121 new NiceMock<MockService>(control_interface(),
1122 dispatcher(),
1123 metrics(),
1124 manager()));
1125 MockServiceRefPtr s_will_not_remove0(
1126 new NiceMock<MockService>(control_interface(),
1127 dispatcher(),
1128 metrics(),
1129 manager()));
1130 MockServiceRefPtr s_will_not_remove1(
1131 new NiceMock<MockService>(control_interface(),
1132 dispatcher(),
1133 metrics(),
1134 manager()));
1135
1136 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1137 .Times(4); // Once for each registration.
1138
1139 string entry_name("entry_name");
1140 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1141 .WillRepeatedly(Return(entry_name));
1142 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1143 .WillRepeatedly(Return(entry_name));
1144 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1145 .WillRepeatedly(Return(entry_name));
1146 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1147 .WillRepeatedly(Return(entry_name));
1148
1149 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001150 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001151 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001152 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001153 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001154 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001155 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001156 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001157
1158 // One for each service added above.
1159 ASSERT_EQ(4, manager()->services_.size());
1160
1161 scoped_refptr<MockProfile> profile(
1162 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1163
1164 s_will_remove0->set_profile(profile);
1165 s_will_remove1->set_profile(profile);
1166 s_will_not_remove0->set_profile(profile);
1167 s_will_not_remove1->set_profile(profile);
1168
1169 AdoptProfile(manager(), profile);
1170
1171 // Deny any of the services re-entry to the profile.
1172 EXPECT_CALL(*profile, ConfigureService(_))
1173 .WillRepeatedly(Return(false));
1174
1175 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1176 .WillOnce(Return(true));
1177 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1178 .WillOnce(Return(true));
1179 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1180 .WillOnce(Return(true));
1181 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1182 .WillOnce(Return(true));
1183
1184 EXPECT_CALL(*s_will_remove0, Unload())
1185 .WillOnce(Return(true));
1186 EXPECT_CALL(*s_will_remove1, Unload())
1187 .WillOnce(Return(true));
1188 EXPECT_CALL(*s_will_not_remove0, Unload())
1189 .WillOnce(Return(false));
1190 EXPECT_CALL(*s_will_not_remove1, Unload())
1191 .WillOnce(Return(false));
1192
1193
1194 // This will cause all the profiles to be unloaded.
1195 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1196
1197 // 2 of the 4 services added above should have been unregistered and
1198 // removed, leaving 2.
1199 EXPECT_EQ(2, manager()->services_.size());
1200 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1201 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1202}
1203
1204TEST_F(ManagerTest, PopProfileWithUnload) {
1205 MockServiceRefPtr s_will_remove0(
1206 new NiceMock<MockService>(control_interface(),
1207 dispatcher(),
1208 metrics(),
1209 manager()));
1210 MockServiceRefPtr s_will_remove1(
1211 new NiceMock<MockService>(control_interface(),
1212 dispatcher(),
1213 metrics(),
1214 manager()));
1215 MockServiceRefPtr s_will_not_remove0(
1216 new NiceMock<MockService>(control_interface(),
1217 dispatcher(),
1218 metrics(),
1219 manager()));
1220 MockServiceRefPtr s_will_not_remove1(
1221 new NiceMock<MockService>(control_interface(),
1222 dispatcher(),
1223 metrics(),
1224 manager()));
1225
1226 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1227 .Times(5); // Once for each registration, and one after profile pop.
1228
1229 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001230 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001231 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001232 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001233 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001234 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001235 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001236 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001237
1238 // One for each service added above.
1239 ASSERT_EQ(4, manager()->services_.size());
1240
1241 scoped_refptr<MockProfile> profile0(
1242 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1243 scoped_refptr<MockProfile> profile1(
1244 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1245
1246 s_will_remove0->set_profile(profile1);
1247 s_will_remove1->set_profile(profile1);
1248 s_will_not_remove0->set_profile(profile1);
1249 s_will_not_remove1->set_profile(profile1);
1250
1251 AdoptProfile(manager(), profile0);
1252 AdoptProfile(manager(), profile1);
1253
1254 // Deny any of the services entry to profile0, so they will all be unloaded.
1255 EXPECT_CALL(*profile0, ConfigureService(_))
1256 .WillRepeatedly(Return(false));
1257
1258 EXPECT_CALL(*s_will_remove0, Unload())
1259 .WillOnce(Return(true));
1260 EXPECT_CALL(*s_will_remove1, Unload())
1261 .WillOnce(Return(true));
1262 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001263 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001264 EXPECT_CALL(*s_will_not_remove1, Unload())
1265 .WillOnce(Return(false));
1266
Philipp Neubeck79173602012-11-13 21:10:09 +01001267 // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the
1268 // profile list.
1269 EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber());
1270 EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber());
1271
Paul Stewart65512e12012-03-26 18:01:08 -07001272 // This will pop profile1, which should cause all our profiles to unload.
1273 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001274 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001275
1276 // 2 of the 4 services added above should have been unregistered and
1277 // removed, leaving 2.
1278 EXPECT_EQ(2, manager()->services_.size());
1279 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1280 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001281
1282 // Expect the unloaded services to lose their profile reference.
1283 EXPECT_FALSE(s_will_remove0->profile());
1284 EXPECT_FALSE(s_will_remove1->profile());
1285
1286 // If we explicitly deregister a service, the effect should be the same
1287 // with respect to the profile reference.
1288 ASSERT_TRUE(s_will_not_remove0->profile());
1289 manager()->DeregisterService(s_will_not_remove0);
1290 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001291}
1292
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001293TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001294 {
1295 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001296 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1297 flimflam::kOfflineModeProperty,
1298 PropertyStoreTest::kBoolV,
1299 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001300 }
1301 {
1302 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001303 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1304 flimflam::kCountryProperty,
1305 PropertyStoreTest::kStringV,
1306 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001307 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001308 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001309 {
1310 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001311 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1312 flimflam::kCountryProperty,
1313 PropertyStoreTest::kBoolV,
1314 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001315 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001316 }
1317 {
1318 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001319 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1320 flimflam::kOfflineModeProperty,
1321 PropertyStoreTest::kStringV,
1322 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001323 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001324 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001325 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001326 {
1327 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001328 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001329 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001330 flimflam::kEnabledTechnologiesProperty,
1331 PropertyStoreTest::kStringsV,
1332 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001333 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001334 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001335}
1336
mukesh agrawal32399322011-09-01 10:53:43 -07001337TEST_F(ManagerTest, RequestScan) {
1338 {
1339 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001340 manager()->RegisterDevice(mock_devices_[0].get());
1341 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001342 EXPECT_CALL(*mock_devices_[0], technology())
1343 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001344 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001345 EXPECT_CALL(*mock_devices_[1], technology())
1346 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001347 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001348 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001349 }
1350
1351 {
1352 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001353 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001354 EXPECT_EQ(Error::kInvalidArguments, error.type());
1355 }
1356}
1357
Darin Petkovb65c2452012-02-23 15:17:06 +01001358TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001359 KeyValueStore args;
1360 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001361 manager()->GetService(args, &e);
1362 EXPECT_EQ(Error::kInvalidArguments, e.type());
1363 EXPECT_EQ("must specify service type", e.message());
1364}
1365
1366TEST_F(ManagerTest, GetServiceUnknownType) {
1367 KeyValueStore args;
1368 Error e;
1369 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1370 manager()->GetService(args, &e);
1371 EXPECT_EQ(Error::kNotSupported, e.type());
1372 EXPECT_EQ("service type is unsupported", e.message());
1373}
1374
1375TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1376 KeyValueStore args;
1377 Error e;
1378 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1379 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001380 EXPECT_EQ(Error::kInvalidArguments, e.type());
1381 EXPECT_EQ("no wifi devices available", e.message());
1382}
1383
Darin Petkovb65c2452012-02-23 15:17:06 +01001384TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001385 KeyValueStore args;
1386 Error e;
1387 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001388 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001389 manager()->RegisterDevice(mock_wifi_);
1390 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1391 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001392 manager()->GetService(args, &e);
1393 EXPECT_TRUE(e.IsSuccess());
1394}
1395
Darin Petkov33af05c2012-02-28 10:10:30 +01001396TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1397 KeyValueStore args;
1398 Error e;
1399 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001400 scoped_refptr<MockProfile> profile(
1401 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1402 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001403 ServiceRefPtr service = manager()->GetService(args, &e);
1404 EXPECT_EQ(Error::kNotSupported, e.type());
1405 EXPECT_FALSE(service);
1406}
1407
Darin Petkovb65c2452012-02-23 15:17:06 +01001408TEST_F(ManagerTest, GetServiceVPN) {
1409 KeyValueStore args;
1410 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001411 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001412 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001413 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1414 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001415 scoped_refptr<MockProfile> profile(
1416 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1417 AdoptProfile(manager(), profile);
1418 ServiceRefPtr updated_service;
1419 EXPECT_CALL(*profile, UpdateService(_))
1420 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1421 ServiceRefPtr configured_service;
Paul Stewart2c575d22012-12-07 12:28:57 -08001422 EXPECT_CALL(*profile, LoadService(_))
1423 .WillOnce(Return(false));
Paul Stewart7f5ad572012-06-04 15:18:54 -07001424 EXPECT_CALL(*profile, ConfigureService(_))
1425 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001426 ServiceRefPtr service = manager()->GetService(args, &e);
1427 EXPECT_TRUE(e.IsSuccess());
1428 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001429 EXPECT_EQ(service, updated_service);
1430 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001431}
1432
Darin Petkovc63dcf02012-05-24 11:51:43 +02001433TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1434 KeyValueStore args;
1435 Error e;
1436 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1437 ServiceRefPtr service = manager()->GetService(args, &e);
1438 EXPECT_EQ(Error::kInvalidArguments, e.type());
1439 EXPECT_EQ("Missing WiMAX network id.", e.message());
1440 EXPECT_FALSE(service);
1441}
1442
Darin Petkovd1cd7972012-05-22 15:26:15 +02001443TEST_F(ManagerTest, GetServiceWiMax) {
1444 KeyValueStore args;
1445 Error e;
1446 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001447 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1448 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1449 ServiceRefPtr service = manager()->GetService(args, &e);
1450 EXPECT_TRUE(e.IsSuccess());
1451 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001452}
1453
Paul Stewart7f61e522012-03-22 11:13:45 -07001454TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1455 // Manager calls ActiveProfile() so we need at least one profile installed.
1456 scoped_refptr<MockProfile> profile(
1457 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1458 AdoptProfile(manager(), profile);
1459
1460 KeyValueStore args;
1461 args.SetString(flimflam::kProfileProperty, "xxx");
1462 Error error;
1463 manager()->ConfigureService(args, &error);
1464 EXPECT_EQ(Error::kInvalidArguments, error.type());
1465 EXPECT_EQ("Invalid profile name xxx", error.message());
1466}
1467
1468TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1469 // Manager calls ActiveProfile() so we need at least one profile installed.
1470 scoped_refptr<MockProfile> profile(
1471 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1472 AdoptProfile(manager(), profile);
1473
1474 KeyValueStore args;
1475 Error error;
1476 manager()->ConfigureService(args, &error);
1477 EXPECT_EQ(Error::kInvalidArguments, error.type());
1478 EXPECT_EQ("must specify service type", error.message());
1479}
1480
1481// A registered service in the ephemeral profile should be moved to the
1482// active profile as a part of configuration if no profile was explicitly
1483// specified.
1484TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1485 scoped_refptr<MockProfile> profile(
1486 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1487
1488 AdoptProfile(manager(), profile); // This is now the active profile.
1489
1490 const std::vector<uint8_t> ssid;
1491 scoped_refptr<MockWiFiService> service(
1492 new NiceMock<MockWiFiService>(control_interface(),
1493 dispatcher(),
1494 metrics(),
1495 manager(),
1496 mock_wifi_,
1497 ssid,
1498 "",
1499 "",
1500 false));
1501
1502 manager()->RegisterService(service);
1503 service->set_profile(GetEphemeralProfile(manager()));
1504
1505 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1506 // the same device as that used above causes a refcounting loop.
1507 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1508 dispatcher(),
1509 metrics(),
1510 manager(),
1511 "wifi1",
1512 "addr5",
1513 5));
1514 manager()->RegisterDevice(wifi);
1515 EXPECT_CALL(*wifi, GetService(_, _))
1516 .WillOnce(Return(service));
1517 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1518 .WillOnce(Return(true));
1519 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1520 .WillOnce(Return(true));
1521
1522 KeyValueStore args;
1523 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1524 Error error;
1525 manager()->ConfigureService(args, &error);
1526 EXPECT_TRUE(error.IsSuccess());
1527}
1528
Paul Stewart2c575d22012-12-07 12:28:57 -08001529// If we configure a service that was already registered and explicitly
Paul Stewart7f61e522012-03-22 11:13:45 -07001530// specify a profile, it should be moved from the profile it was previously
1531// in to the specified profile if one was requested.
1532TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1533 scoped_refptr<MockProfile> profile0(
1534 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1535 scoped_refptr<MockProfile> profile1(
1536 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1537
1538 const string kProfileName0 = "profile0";
1539 const string kProfileName1 = "profile1";
1540
1541 EXPECT_CALL(*profile0, GetRpcIdentifier())
1542 .WillRepeatedly(Return(kProfileName0));
1543 EXPECT_CALL(*profile1, GetRpcIdentifier())
1544 .WillRepeatedly(Return(kProfileName1));
1545
1546 AdoptProfile(manager(), profile0);
1547 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1548
1549 const std::vector<uint8_t> ssid;
1550 scoped_refptr<MockWiFiService> service(
1551 new NiceMock<MockWiFiService>(control_interface(),
1552 dispatcher(),
1553 metrics(),
1554 manager(),
1555 mock_wifi_,
1556 ssid,
1557 "",
1558 "",
1559 false));
1560
1561 manager()->RegisterService(service);
1562 service->set_profile(profile1);
1563
1564 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1565 // the same device as that used above causes a refcounting loop.
1566 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1567 dispatcher(),
1568 metrics(),
1569 manager(),
1570 "wifi1",
1571 "addr5",
1572 5));
1573 manager()->RegisterDevice(wifi);
1574 EXPECT_CALL(*wifi, GetService(_, _))
1575 .WillOnce(Return(service));
Paul Stewart2c575d22012-12-07 12:28:57 -08001576 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1577 .WillOnce(Return(true));
Paul Stewart7f61e522012-03-22 11:13:45 -07001578 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1579 .WillOnce(Return(true));
1580 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1581 .WillOnce(Return(true));
1582 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1583 .WillOnce(Return(true));
1584
1585 KeyValueStore args;
1586 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1587 args.SetString(flimflam::kProfileProperty, kProfileName0);
1588 Error error;
1589 manager()->ConfigureService(args, &error);
1590 EXPECT_TRUE(error.IsSuccess());
1591 service->set_profile(NULL); // Breaks refcounting loop.
1592}
1593
Paul Stewart2c575d22012-12-07 12:28:57 -08001594// If we configure a service that is already a member of the specified
1595// profile, the Manager should not call LoadService or AdoptService again
1596// on this service.
1597TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) {
1598 scoped_refptr<MockProfile> profile0(
1599 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1600
1601 const string kProfileName0 = "profile0";
1602
1603 EXPECT_CALL(*profile0, GetRpcIdentifier())
1604 .WillRepeatedly(Return(kProfileName0));
1605
1606 AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile.
1607
1608 const std::vector<uint8_t> ssid;
1609 scoped_refptr<MockWiFiService> service(
1610 new NiceMock<MockWiFiService>(control_interface(),
1611 dispatcher(),
1612 metrics(),
1613 manager(),
1614 mock_wifi_,
1615 ssid,
1616 "",
1617 "",
1618 false));
1619
1620 manager()->RegisterService(service);
1621 service->set_profile(profile0);
1622
1623 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1624 // the same device as that used above causes a refcounting loop.
1625 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1626 dispatcher(),
1627 metrics(),
1628 manager(),
1629 "wifi1",
1630 "addr5",
1631 5));
1632 manager()->RegisterDevice(wifi);
1633 EXPECT_CALL(*wifi, GetService(_, _))
1634 .WillOnce(Return(service));
1635 EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get())))
1636 .Times(0);
1637 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1638 .WillOnce(Return(true));
1639 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1640 .Times(0);
1641
1642 KeyValueStore args;
1643 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1644 args.SetString(flimflam::kProfileProperty, kProfileName0);
1645 Error error;
1646 manager()->ConfigureService(args, &error);
1647 EXPECT_TRUE(error.IsSuccess());
1648 service->set_profile(NULL); // Breaks refcounting loop.
1649}
1650
Paul Stewart7f61e522012-03-22 11:13:45 -07001651// An unregistered service should remain unregistered, but its contents should
1652// be saved to the specified profile nonetheless.
1653TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1654 scoped_refptr<MockProfile> profile0(
1655 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1656 scoped_refptr<MockProfile> profile1(
1657 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1658
1659 const string kProfileName0 = "profile0";
1660 const string kProfileName1 = "profile1";
1661
1662 EXPECT_CALL(*profile0, GetRpcIdentifier())
1663 .WillRepeatedly(Return(kProfileName0));
1664 EXPECT_CALL(*profile1, GetRpcIdentifier())
1665 .WillRepeatedly(Return(kProfileName1));
1666
1667 AdoptProfile(manager(), profile0);
1668 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1669
1670 const std::vector<uint8_t> ssid;
1671 scoped_refptr<MockWiFiService> service(
1672 new NiceMock<MockWiFiService>(control_interface(),
1673 dispatcher(),
1674 metrics(),
1675 manager(),
1676 mock_wifi_,
1677 ssid,
1678 "",
1679 "",
1680 false));
1681
1682 service->set_profile(profile1);
1683
1684 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1685 // the same device as that used above causes a refcounting loop.
1686 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1687 dispatcher(),
1688 metrics(),
1689 manager(),
1690 "wifi1",
1691 "addr5",
1692 5));
1693 manager()->RegisterDevice(wifi);
1694 EXPECT_CALL(*wifi, GetService(_, _))
1695 .WillOnce(Return(service));
1696 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1697 .WillOnce(Return(true));
1698 EXPECT_CALL(*profile0, AdoptService(_))
1699 .Times(0);
1700 EXPECT_CALL(*profile1, AdoptService(_))
1701 .Times(0);
1702
1703 KeyValueStore args;
1704 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1705 args.SetString(flimflam::kProfileProperty, kProfileName0);
1706 Error error;
1707 manager()->ConfigureService(args, &error);
1708 EXPECT_TRUE(error.IsSuccess());
1709}
1710
Paul Stewart22aa71b2011-09-16 12:15:11 -07001711TEST_F(ManagerTest, TechnologyOrder) {
1712 Error error;
1713 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1714 string(flimflam::kTypeWifi), &error);
1715 ASSERT_TRUE(error.IsSuccess());
1716 EXPECT_EQ(manager()->GetTechnologyOrder(),
1717 string(flimflam::kTypeEthernet) + "," +
1718 string(flimflam::kTypeWifi));
1719
1720 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1721 string(flimflam::kTypeWifi), &error);
1722 ASSERT_FALSE(error.IsSuccess());
1723 EXPECT_EQ(Error::kInvalidArguments, error.type());
1724 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1725 string(flimflam::kTypeWifi),
1726 manager()->GetTechnologyOrder());
1727}
1728
1729TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001730 // TODO(quiche): Some of these tests would probably fit better in
1731 // service_unittest, since the actual comparison of Services is
1732 // implemented in Service. (crosbug.com/23370)
1733
Paul Stewart22aa71b2011-09-16 12:15:11 -07001734 scoped_refptr<MockService> mock_service0(
1735 new NiceMock<MockService>(control_interface(),
1736 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001737 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001738 manager()));
1739 scoped_refptr<MockService> mock_service1(
1740 new NiceMock<MockService>(control_interface(),
1741 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001742 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001743 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001744
1745 manager()->RegisterService(mock_service0);
1746 manager()->RegisterService(mock_service1);
1747
1748 // Services should already be sorted by UniqueName
1749 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1750
1751 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001752 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001753 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1754
1755 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001756 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001757 manager()->UpdateService(mock_service1);
1758 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1759
1760 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001761 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001762 manager()->UpdateService(mock_service0);
1763 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1764
1765 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001766 EXPECT_CALL(*mock_service0.get(), technology())
1767 .WillRepeatedly(Return((Technology::kWifi)));
1768 EXPECT_CALL(*mock_service1.get(), technology())
1769 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001770
1771 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001772 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001773 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001774 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1775
1776 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1777 string(flimflam::kTypeEthernet), &error);
1778 EXPECT_TRUE(error.IsSuccess());
1779 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1780
Gaurav Shah435de2c2011-11-17 19:01:07 -08001781 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001782 mock_service0->set_priority(1);
1783 manager()->UpdateService(mock_service0);
1784 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1785
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001786 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001787 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001788 manager()->UpdateService(mock_service1);
1789 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1790
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001791 // Auto-connect.
1792 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001793 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001794 mock_service1->set_auto_connect(false);
1795 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001796 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1797
Paul Stewartdf3c0a82012-11-09 15:54:33 -08001798 // Test is-dependent-on. It doesn't make sense to have this ranking compare
1799 // to any of the others below, so we reset to the default state after
1800 // testing.
1801 EXPECT_CALL(*mock_service1.get(),
1802 IsDependentOn(ServiceRefPtr(mock_service0.get())))
1803 .WillOnce(Return(true))
1804 .WillRepeatedly(Return(false));
1805 manager()->UpdateService(mock_service1);
1806 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1807 manager()->UpdateService(mock_service0);
1808 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1809
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001810 // Connectable.
1811 mock_service1->set_connectable(true);
1812 manager()->UpdateService(mock_service1);
1813 mock_service0->set_connectable(false);
1814 manager()->UpdateService(mock_service0);
1815 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1816
1817 // IsFailed.
1818 EXPECT_CALL(*mock_service0.get(), state())
1819 .WillRepeatedly(Return(Service::kStateIdle));
1820 EXPECT_CALL(*mock_service0.get(), IsFailed())
1821 .WillRepeatedly(Return(false));
1822 manager()->UpdateService(mock_service0);
1823 EXPECT_CALL(*mock_service0.get(), state())
1824 .WillRepeatedly(Return(Service::kStateFailure));
1825 EXPECT_CALL(*mock_service1.get(), IsFailed())
1826 .WillRepeatedly(Return(true));
1827 manager()->UpdateService(mock_service1);
1828 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1829
1830 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001831 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001832 .WillRepeatedly(Return(Service::kStateAssociating));
1833 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001834 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001835 manager()->UpdateService(mock_service1);
1836 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1837
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001838 // Connected.
1839 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001840 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001841 EXPECT_CALL(*mock_service0.get(), IsConnected())
1842 .WillRepeatedly(Return(true));
1843 manager()->UpdateService(mock_service0);
1844 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1845
Paul Stewarta121c442012-06-09 14:12:58 -07001846 // Portal.
1847 EXPECT_CALL(*mock_service1.get(), state())
1848 .WillRepeatedly(Return(Service::kStateConnected));
1849 EXPECT_CALL(*mock_service1.get(), IsConnected())
1850 .WillRepeatedly(Return(true));
1851 manager()->UpdateService(mock_service1);
1852 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1853
Paul Stewart22aa71b2011-09-16 12:15:11 -07001854 manager()->DeregisterService(mock_service0);
1855 manager()->DeregisterService(mock_service1);
1856}
1857
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001858TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001859 MockMetrics mock_metrics;
1860 manager()->set_metrics(&mock_metrics);
1861
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001862 scoped_refptr<MockService> mock_service0(
1863 new NiceMock<MockService>(control_interface(),
1864 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001865 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001866 manager()));
1867 scoped_refptr<MockService> mock_service1(
1868 new NiceMock<MockService>(control_interface(),
1869 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001870 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001871 manager()));
1872
1873 scoped_refptr<MockConnection> mock_connection0(
1874 new NiceMock<MockConnection>(device_info_.get()));
1875 scoped_refptr<MockConnection> mock_connection1(
1876 new NiceMock<MockConnection>(device_info_.get()));
1877
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001878 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001879 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001880 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001881 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001882 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001883 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001884
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001885 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001886 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001887
1888 mock_service1->set_priority(1);
1889 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001890 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001891
1892 mock_service1->set_priority(0);
1893 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001894 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001895
Paul Stewartce4ec192012-03-14 12:53:46 -07001896 mock_service0->set_mock_connection(mock_connection0);
1897 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001898
1899 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001900 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001901 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001902
Darin Petkova5e07ef2012-07-09 14:27:57 +02001903 ServiceWatcher service_watcher;
1904 int tag =
1905 manager()->RegisterDefaultServiceCallback(
1906 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1907 service_watcher.AsWeakPtr()));
1908 EXPECT_EQ(1, tag);
1909
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001910 mock_service1->set_priority(1);
1911 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1912 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001913 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001914 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001915 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001916
Darin Petkova5e07ef2012-07-09 14:27:57 +02001917 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001918 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001919 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001920 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001921 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001922 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001923 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001924
Paul Stewartce4ec192012-03-14 12:53:46 -07001925 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001926 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001927 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001928 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001929
1930 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001931 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001932}
1933
Darin Petkova5e07ef2012-07-09 14:27:57 +02001934TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1935 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1936 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1937
1938 MockMetrics mock_metrics;
1939 manager()->set_metrics(&mock_metrics);
1940
1941 scoped_refptr<MockService> mock_service(
1942 new NiceMock<MockService>(
1943 control_interface(), dispatcher(), metrics(), manager()));
1944 ServiceRefPtr service = mock_service;
1945 ServiceRefPtr null_service;
1946
1947 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1948 manager()->NotifyDefaultServiceChanged(null_service);
1949
1950 ServiceWatcher service_watcher1;
1951 ServiceWatcher service_watcher2;
1952 int tag1 =
1953 manager()->RegisterDefaultServiceCallback(
1954 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1955 service_watcher1.AsWeakPtr()));
1956 EXPECT_EQ(1, tag1);
1957 int tag2 =
1958 manager()->RegisterDefaultServiceCallback(
1959 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1960 service_watcher2.AsWeakPtr()));
1961 EXPECT_EQ(2, tag2);
1962
1963 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1964 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1965 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1966 manager()->NotifyDefaultServiceChanged(null_service);
1967
1968 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1969 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1970 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1971 manager()->NotifyDefaultServiceChanged(mock_service);
1972
1973 manager()->DeregisterDefaultServiceCallback(tag1);
1974 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1975 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1976 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1977 manager()->NotifyDefaultServiceChanged(mock_service);
1978 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1979
1980 manager()->DeregisterDefaultServiceCallback(tag2);
1981 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1982 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1983 manager()->NotifyDefaultServiceChanged(mock_service);
1984
1985 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1986 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1987}
1988
Gaurav Shah435de2c2011-11-17 19:01:07 -08001989TEST_F(ManagerTest, AvailableTechnologies) {
1990 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1991 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001992 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001993 manager(),
1994 "null4",
1995 "addr4",
1996 0));
1997 manager()->RegisterDevice(mock_devices_[0]);
1998 manager()->RegisterDevice(mock_devices_[1]);
1999 manager()->RegisterDevice(mock_devices_[2]);
2000 manager()->RegisterDevice(mock_devices_[3]);
2001
2002 ON_CALL(*mock_devices_[0].get(), technology())
2003 .WillByDefault(Return(Technology::kEthernet));
2004 ON_CALL(*mock_devices_[1].get(), technology())
2005 .WillByDefault(Return(Technology::kWifi));
2006 ON_CALL(*mock_devices_[2].get(), technology())
2007 .WillByDefault(Return(Technology::kCellular));
2008 ON_CALL(*mock_devices_[3].get(), technology())
2009 .WillByDefault(Return(Technology::kWifi));
2010
2011 set<string> expected_technologies;
2012 expected_technologies.insert(Technology::NameFromIdentifier(
2013 Technology::kEthernet));
2014 expected_technologies.insert(Technology::NameFromIdentifier(
2015 Technology::kWifi));
2016 expected_technologies.insert(Technology::NameFromIdentifier(
2017 Technology::kCellular));
2018 Error error;
2019 vector<string> technologies = manager()->AvailableTechnologies(&error);
2020
2021 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2022 ContainerEq(expected_technologies));
2023}
2024
2025TEST_F(ManagerTest, ConnectedTechnologies) {
2026 scoped_refptr<MockService> connected_service1(
2027 new NiceMock<MockService>(control_interface(),
2028 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002029 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002030 manager()));
2031 scoped_refptr<MockService> connected_service2(
2032 new NiceMock<MockService>(control_interface(),
2033 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002034 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002035 manager()));
2036 scoped_refptr<MockService> disconnected_service1(
2037 new NiceMock<MockService>(control_interface(),
2038 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002039 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002040 manager()));
2041 scoped_refptr<MockService> disconnected_service2(
2042 new NiceMock<MockService>(control_interface(),
2043 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002044 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002045 manager()));
2046
2047 ON_CALL(*connected_service1.get(), IsConnected())
2048 .WillByDefault(Return(true));
2049 ON_CALL(*connected_service2.get(), IsConnected())
2050 .WillByDefault(Return(true));
2051
2052 manager()->RegisterService(connected_service1);
2053 manager()->RegisterService(connected_service2);
2054 manager()->RegisterService(disconnected_service1);
2055 manager()->RegisterService(disconnected_service2);
2056
2057 manager()->RegisterDevice(mock_devices_[0]);
2058 manager()->RegisterDevice(mock_devices_[1]);
2059 manager()->RegisterDevice(mock_devices_[2]);
2060 manager()->RegisterDevice(mock_devices_[3]);
2061
2062 ON_CALL(*mock_devices_[0].get(), technology())
2063 .WillByDefault(Return(Technology::kEthernet));
2064 ON_CALL(*mock_devices_[1].get(), technology())
2065 .WillByDefault(Return(Technology::kWifi));
2066 ON_CALL(*mock_devices_[2].get(), technology())
2067 .WillByDefault(Return(Technology::kCellular));
2068 ON_CALL(*mock_devices_[3].get(), technology())
2069 .WillByDefault(Return(Technology::kWifi));
2070
2071 mock_devices_[0]->SelectService(connected_service1);
2072 mock_devices_[1]->SelectService(disconnected_service1);
2073 mock_devices_[2]->SelectService(disconnected_service2);
2074 mock_devices_[3]->SelectService(connected_service2);
2075
2076 set<string> expected_technologies;
2077 expected_technologies.insert(Technology::NameFromIdentifier(
2078 Technology::kEthernet));
2079 expected_technologies.insert(Technology::NameFromIdentifier(
2080 Technology::kWifi));
2081 Error error;
2082
2083 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2084 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2085 ContainerEq(expected_technologies));
2086}
2087
2088TEST_F(ManagerTest, DefaultTechnology) {
2089 scoped_refptr<MockService> connected_service(
2090 new NiceMock<MockService>(control_interface(),
2091 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002092 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002093 manager()));
2094 scoped_refptr<MockService> disconnected_service(
2095 new NiceMock<MockService>(control_interface(),
2096 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002097 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002098 manager()));
2099
2100 // Connected. WiFi.
2101 ON_CALL(*connected_service.get(), IsConnected())
2102 .WillByDefault(Return(true));
2103 ON_CALL(*connected_service.get(), state())
2104 .WillByDefault(Return(Service::kStateConnected));
2105 ON_CALL(*connected_service.get(), technology())
2106 .WillByDefault(Return(Technology::kWifi));
2107
2108 // Disconnected. Ethernet.
2109 ON_CALL(*disconnected_service.get(), technology())
2110 .WillByDefault(Return(Technology::kEthernet));
2111
2112 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002113 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002114 Error error;
2115 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2116
2117
2118 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002119 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002120 // Connected service should be brought to the front now.
2121 string expected_technology =
2122 Technology::NameFromIdentifier(Technology::kWifi);
2123 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2124}
2125
Paul Stewart212d60f2012-07-12 10:59:13 -07002126TEST_F(ManagerTest, Stop) {
2127 scoped_refptr<MockProfile> profile(
2128 new NiceMock<MockProfile>(control_interface(), manager(), ""));
2129 AdoptProfile(manager(), profile);
2130 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002131 new NiceMock<MockService>(control_interface(),
2132 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002133 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002134 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002135 manager()->RegisterService(service);
2136 manager()->RegisterDevice(mock_devices_[0]);
2137 EXPECT_CALL(*profile.get(),
2138 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2139 .WillOnce(Return(true));
2140 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2141 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002142 manager()->Stop();
2143}
2144
mukesh agrawal00917ce2011-11-22 23:56:55 +00002145TEST_F(ManagerTest, UpdateServiceConnected) {
2146 scoped_refptr<MockService> mock_service(
2147 new NiceMock<MockService>(control_interface(),
2148 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002149 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002150 manager()));
2151 manager()->RegisterService(mock_service);
2152 EXPECT_FALSE(mock_service->favorite());
2153 EXPECT_FALSE(mock_service->auto_connect());
2154
Gaurav Shah435de2c2011-11-17 19:01:07 -08002155 EXPECT_CALL(*mock_service.get(), IsConnected())
2156 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002157 manager()->UpdateService(mock_service);
2158 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2159 // to mock out MakeFavorite. And mocking that out would break the
2160 // SortServices test. (crosbug.com/23370)
2161 EXPECT_TRUE(mock_service->favorite());
2162 EXPECT_TRUE(mock_service->auto_connect());
2163}
2164
Thieu Led4e9e552012-02-16 16:26:07 -08002165TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2166 // This tests the case where the user connects to a service that is
2167 // currently associated with a profile. We want to make sure that the
2168 // favorite flag is set and that the flag is saved to the current
2169 // profile.
2170 scoped_refptr<MockService> mock_service(
2171 new NiceMock<MockService>(control_interface(),
2172 dispatcher(),
2173 metrics(),
2174 manager()));
2175 manager()->RegisterService(mock_service);
2176 EXPECT_FALSE(mock_service->favorite());
2177 EXPECT_FALSE(mock_service->auto_connect());
2178
Gary Moraind93615e2012-04-27 11:50:03 -07002179 scoped_refptr<MockProfile> profile(
2180 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002181
Gary Moraind93615e2012-04-27 11:50:03 -07002182 mock_service->set_profile(profile);
2183 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002184 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002185 EXPECT_CALL(*profile,
2186 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002187 manager()->UpdateService(mock_service);
2188 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2189 // to mock out MakeFavorite. And mocking that out would break the
2190 // SortServices test. (crosbug.com/23370)
2191 EXPECT_TRUE(mock_service->favorite());
2192 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002193 // This releases the ref on the mock profile.
2194 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002195}
2196
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002197TEST_F(ManagerTest, SaveSuccessfulService) {
2198 scoped_refptr<MockProfile> profile(
2199 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2200 AdoptProfile(manager(), profile);
2201 scoped_refptr<MockService> service(
2202 new NiceMock<MockService>(control_interface(),
2203 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002204 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002205 manager()));
2206
2207 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2208 ServiceRefPtr expect_service(service.get());
2209
2210 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2211 .WillOnce(Return(false));
2212 manager()->RegisterService(service);
2213
2214 EXPECT_CALL(*service.get(), state())
2215 .WillRepeatedly(Return(Service::kStateConnected));
2216 EXPECT_CALL(*service.get(), IsConnected())
2217 .WillRepeatedly(Return(true));
2218 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2219 .WillOnce(Return(true));
2220 manager()->UpdateService(service);
2221}
2222
Darin Petkove7c6ad32012-06-29 10:22:09 +02002223TEST_F(ManagerTest, UpdateDevice) {
2224 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2225 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2226 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2227 AdoptProfile(manager(), profile0); // Passes ownership.
2228 AdoptProfile(manager(), profile1); // Passes ownership.
2229 AdoptProfile(manager(), profile2); // Passes ownership.
2230 DeviceRefPtr device_ref(mock_devices_[0].get());
2231 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2232 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2233 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2234 manager()->UpdateDevice(mock_devices_[0]);
2235}
2236
Paul Stewart1b253142012-01-26 14:05:52 -08002237TEST_F(ManagerTest, EnumerateProfiles) {
2238 vector<string> profile_paths;
2239 for (size_t i = 0; i < 10; i++) {
2240 scoped_refptr<MockProfile> profile(
2241 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002242 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002243 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2244 .WillOnce(Return(profile_paths.back()));
2245 AdoptProfile(manager(), profile);
2246 }
2247
2248 Error error;
2249 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2250 EXPECT_TRUE(error.IsSuccess());
2251 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2252 for (size_t i = 0; i < profile_paths.size(); i++) {
2253 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2254 }
2255}
2256
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002257TEST_F(ManagerTest, AutoConnectOnRegister) {
2258 MockServiceRefPtr service = MakeAutoConnectableService();
2259 EXPECT_CALL(*service.get(), AutoConnect());
2260 manager()->RegisterService(service);
2261 dispatcher()->DispatchPendingEvents();
2262}
2263
2264TEST_F(ManagerTest, AutoConnectOnUpdate) {
2265 MockServiceRefPtr service1 = MakeAutoConnectableService();
2266 service1->set_priority(1);
2267 MockServiceRefPtr service2 = MakeAutoConnectableService();
2268 service2->set_priority(2);
2269 manager()->RegisterService(service1);
2270 manager()->RegisterService(service2);
2271 dispatcher()->DispatchPendingEvents();
2272
2273 EXPECT_CALL(*service1.get(), AutoConnect());
2274 EXPECT_CALL(*service2.get(), state())
2275 .WillRepeatedly(Return(Service::kStateFailure));
2276 EXPECT_CALL(*service2.get(), IsFailed())
2277 .WillRepeatedly(Return(true));
2278 EXPECT_CALL(*service2.get(), IsConnected())
2279 .WillRepeatedly(Return(false));
2280 manager()->UpdateService(service2);
2281 dispatcher()->DispatchPendingEvents();
2282}
2283
2284TEST_F(ManagerTest, AutoConnectOnDeregister) {
2285 MockServiceRefPtr service1 = MakeAutoConnectableService();
2286 service1->set_priority(1);
2287 MockServiceRefPtr service2 = MakeAutoConnectableService();
2288 service2->set_priority(2);
2289 manager()->RegisterService(service1);
2290 manager()->RegisterService(service2);
2291 dispatcher()->DispatchPendingEvents();
2292
2293 EXPECT_CALL(*service1.get(), AutoConnect());
2294 manager()->DeregisterService(service2);
2295 dispatcher()->DispatchPendingEvents();
2296}
2297
Darin Petkov3ec55342012-09-28 14:04:44 +02002298TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2299 MockServiceRefPtr service = MakeAutoConnectableService();
2300 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2301 SetPowerManager();
2302 EXPECT_CALL(*service, AutoConnect()).Times(0);
2303 manager()->RegisterService(service);
2304 dispatcher()->DispatchPendingEvents();
2305}
2306
Darin Petkovca621542012-07-25 14:25:56 +02002307TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2308 MockServiceRefPtr service = MakeAutoConnectableService();
2309 SetPowerState(PowerManagerProxyDelegate::kMem);
2310 SetPowerManager();
2311 EXPECT_CALL(*service, AutoConnect()).Times(0);
2312 manager()->RegisterService(service);
2313 dispatcher()->DispatchPendingEvents();
2314}
2315
2316TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2317 MockServiceRefPtr service = MakeAutoConnectableService();
2318 SetPowerState(PowerManagerProxyDelegate::kOn);
2319 SetPowerManager();
2320 EXPECT_CALL(*service, AutoConnect());
2321 manager()->RegisterService(service);
2322 dispatcher()->DispatchPendingEvents();
2323}
2324
2325TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2326 MockServiceRefPtr service = MakeAutoConnectableService();
2327 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2328 SetPowerManager();
2329 EXPECT_CALL(*service, AutoConnect());
2330 manager()->RegisterService(service);
2331 dispatcher()->DispatchPendingEvents();
2332}
2333
Paul Stewart63864b62012-11-07 15:10:55 -08002334TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2335 SetRunning(false);
2336 MockServiceRefPtr service = MakeAutoConnectableService();
2337 EXPECT_CALL(*service, AutoConnect()).Times(0);
2338 manager()->RegisterService(service);
2339 dispatcher()->DispatchPendingEvents();
2340}
2341
Darin Petkovca621542012-07-25 14:25:56 +02002342TEST_F(ManagerTest, OnPowerStateChanged) {
2343 MockServiceRefPtr service = MakeAutoConnectableService();
2344 SetPowerState(PowerManagerProxyDelegate::kOn);
2345 SetPowerManager();
2346 EXPECT_CALL(*service, AutoConnect());
2347 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002348 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002349 dispatcher()->DispatchPendingEvents();
2350
mukesh agrawal784566d2012-08-08 18:32:58 -07002351 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002352 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2353 EXPECT_CALL(*service, AutoConnect());
2354 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002355 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002356
mukesh agrawal784566d2012-08-08 18:32:58 -07002357 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002358 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2359 EXPECT_CALL(*service, AutoConnect()).Times(0);
2360 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002361 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002362}
2363
Darin Petkov3ec55342012-09-28 14:04:44 +02002364TEST_F(ManagerTest, AddTerminationAction) {
2365 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
2366 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_));
2367 SetPowerManager();
2368 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2369 manager()->AddTerminationAction("action1", base::Closure());
2370 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2371 manager()->AddTerminationAction("action2", base::Closure());
2372}
2373
2374TEST_F(ManagerTest, RemoveTerminationAction) {
2375 static const char kKey1[] = "action1";
2376 static const char kKey2[] = "action2";
2377
2378 MockPowerManager &power_manager = *power_manager_;
2379 SetPowerManager();
2380
2381 // Removing an action when the hook table is empty should not result in any
2382 // calls to the power manager.
2383 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2384 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2385 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2386 manager()->RemoveTerminationAction("unknown");
2387 Mock::VerifyAndClearExpectations(&power_manager);
2388
2389 manager()->AddTerminationAction(kKey1, base::Closure());
2390 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2391 manager()->AddTerminationAction(kKey2, base::Closure());
2392
2393 // Removing an action that ends up with a non-empty hook table should not
2394 // result in any calls to the power manager.
2395 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2396 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2397 manager()->RemoveTerminationAction(kKey1);
2398 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2399 Mock::VerifyAndClearExpectations(&power_manager);
2400
2401 // Removing the last action should trigger unregistering from the power
2402 // manager.
2403 EXPECT_CALL(power_manager, UnregisterSuspendDelay());
2404 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2405 manager()->RemoveTerminationAction(kKey2);
2406 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2407}
2408
2409TEST_F(ManagerTest, RunTerminationActions) {
2410 TerminationActionTest test_action;
2411 const string kActionName = "action";
2412
2413 EXPECT_CALL(test_action, Done(_));
2414 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2415 test_action.AsWeakPtr()));
2416
2417 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2418 Bind(&TerminationActionTest::Action,
2419 test_action.AsWeakPtr()));
2420 test_action.set_manager(manager());
2421 EXPECT_CALL(test_action, Done(_));
2422 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2423 test_action.AsWeakPtr()));
2424}
2425
2426TEST_F(ManagerTest, OnSuspendDelay) {
2427 const uint32 kSeqNumber = 123;
2428 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2429 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2430 SetPowerManager();
2431 OnSuspendDelay(kSeqNumber);
2432}
2433
2434TEST_F(ManagerTest, OnSuspendActionsComplete) {
2435 const uint32 kSeqNumber = 54321;
2436 Error error;
2437 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2438 SetPowerManager();
2439 OnSuspendActionsComplete(kSeqNumber, error);
2440}
2441
Paul Stewartc681fa02012-03-02 19:40:04 -08002442TEST_F(ManagerTest, RecheckPortal) {
2443 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2444 .WillOnce(Return(false));
2445 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2446 .WillOnce(Return(true));
2447 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2448 .Times(0);
2449
2450 manager()->RegisterDevice(mock_devices_[0]);
2451 manager()->RegisterDevice(mock_devices_[1]);
2452 manager()->RegisterDevice(mock_devices_[2]);
2453
2454 manager()->RecheckPortal(NULL);
2455}
2456
Paul Stewartd215af62012-04-24 23:25:50 -07002457TEST_F(ManagerTest, RecheckPortalOnService) {
2458 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2459 dispatcher(),
2460 metrics(),
2461 manager());
2462 EXPECT_CALL(*mock_devices_[0].get(),
2463 IsConnectedToService(IsRefPtrTo(service)))
2464 .WillOnce(Return(false));
2465 EXPECT_CALL(*mock_devices_[1].get(),
2466 IsConnectedToService(IsRefPtrTo(service)))
2467 .WillOnce(Return(true));
2468 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2469 .WillOnce(Return(true));
2470 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2471 .Times(0);
2472
2473 manager()->RegisterDevice(mock_devices_[0]);
2474 manager()->RegisterDevice(mock_devices_[1]);
2475 manager()->RegisterDevice(mock_devices_[2]);
2476
2477 manager()->RecheckPortalOnService(service);
2478}
2479
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002480TEST_F(ManagerTest, GetDefaultService) {
2481 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002482 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002483
2484 scoped_refptr<MockService> mock_service(
2485 new NiceMock<MockService>(control_interface(),
2486 dispatcher(),
2487 metrics(),
2488 manager()));
2489
2490 manager()->RegisterService(mock_service);
2491 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002492 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002493
2494 scoped_refptr<MockConnection> mock_connection(
2495 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002496 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002497 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002498 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002499
Paul Stewartce4ec192012-03-14 12:53:46 -07002500 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002501 manager()->DeregisterService(mock_service);
2502}
2503
Paul Stewart13ed2252012-03-21 12:52:46 -07002504TEST_F(ManagerTest, GetServiceWithGUID) {
2505 scoped_refptr<MockService> mock_service0(
2506 new NiceMock<MockService>(control_interface(),
2507 dispatcher(),
2508 metrics(),
2509 manager()));
2510
2511 scoped_refptr<MockService> mock_service1(
2512 new NiceMock<MockService>(control_interface(),
2513 dispatcher(),
2514 metrics(),
2515 manager()));
2516
Paul Stewartcb59fed2012-03-21 21:14:46 -07002517 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2518 .Times(0);
2519 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2520 .Times(0);
2521
Paul Stewart13ed2252012-03-21 12:52:46 -07002522 manager()->RegisterService(mock_service0);
2523 manager()->RegisterService(mock_service1);
2524
2525 const string kGUID0 = "GUID0";
2526 const string kGUID1 = "GUID1";
2527
2528 {
2529 Error error;
2530 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2531 EXPECT_FALSE(error.IsSuccess());
2532 EXPECT_FALSE(service);
2533 }
2534
2535 KeyValueStore args;
2536 args.SetString(flimflam::kGuidProperty, kGUID1);
2537
2538 {
2539 Error error;
2540 ServiceRefPtr service = manager()->GetService(args, &error);
2541 EXPECT_EQ(Error::kInvalidArguments, error.type());
2542 EXPECT_FALSE(service);
2543 }
2544
2545 mock_service0->set_guid(kGUID0);
2546 mock_service1->set_guid(kGUID1);
2547
2548 {
2549 Error error;
2550 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2551 EXPECT_TRUE(error.IsSuccess());
2552 EXPECT_EQ(mock_service0.get(), service.get());
2553 }
2554
2555 {
2556 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002557 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2558 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002559 ServiceRefPtr service = manager()->GetService(args, &error);
2560 EXPECT_TRUE(error.IsSuccess());
2561 EXPECT_EQ(mock_service1.get(), service.get());
2562 }
2563
2564 manager()->DeregisterService(mock_service0);
2565 manager()->DeregisterService(mock_service1);
2566}
2567
Gary Morain028545d2012-04-07 14:55:52 -07002568
2569TEST_F(ManagerTest, CalculateStateOffline) {
2570 MockMetrics mock_metrics;
2571 manager()->set_metrics(&mock_metrics);
2572 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2573 .Times(AnyNumber());
2574 scoped_refptr<MockService> mock_service0(
2575 new NiceMock<MockService>(control_interface(),
2576 dispatcher(),
2577 metrics(),
2578 manager()));
2579
2580 scoped_refptr<MockService> mock_service1(
2581 new NiceMock<MockService>(control_interface(),
2582 dispatcher(),
2583 metrics(),
2584 manager()));
2585
2586 EXPECT_CALL(*mock_service0.get(), IsConnected())
2587 .WillRepeatedly(Return(false));
2588 EXPECT_CALL(*mock_service1.get(), IsConnected())
2589 .WillRepeatedly(Return(false));
2590
2591 manager()->RegisterService(mock_service0);
2592 manager()->RegisterService(mock_service1);
2593
2594 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2595
2596 manager()->DeregisterService(mock_service0);
2597 manager()->DeregisterService(mock_service1);
2598}
2599
2600TEST_F(ManagerTest, CalculateStateOnline) {
2601 MockMetrics mock_metrics;
2602 manager()->set_metrics(&mock_metrics);
2603 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2604 .Times(AnyNumber());
2605 scoped_refptr<MockService> mock_service0(
2606 new NiceMock<MockService>(control_interface(),
2607 dispatcher(),
2608 metrics(),
2609 manager()));
2610
2611 scoped_refptr<MockService> mock_service1(
2612 new NiceMock<MockService>(control_interface(),
2613 dispatcher(),
2614 metrics(),
2615 manager()));
2616
2617 EXPECT_CALL(*mock_service0.get(), IsConnected())
2618 .WillRepeatedly(Return(false));
2619 EXPECT_CALL(*mock_service1.get(), IsConnected())
2620 .WillRepeatedly(Return(true));
2621 EXPECT_CALL(*mock_service0.get(), state())
2622 .WillRepeatedly(Return(Service::kStateIdle));
2623 EXPECT_CALL(*mock_service1.get(), state())
2624 .WillRepeatedly(Return(Service::kStateConnected));
2625
2626 manager()->RegisterService(mock_service0);
2627 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002628 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002629
2630 EXPECT_EQ("online", manager()->CalculateState(NULL));
2631
2632 manager()->DeregisterService(mock_service0);
2633 manager()->DeregisterService(mock_service1);
2634}
2635
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002636TEST_F(ManagerTest, StartupPortalList) {
2637 // Simulate loading value from the default profile.
2638 const string kProfileValue("wifi,vpn");
2639 manager()->props_.check_portal_list = kProfileValue;
2640
2641 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2642 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2643 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2644
2645 const string kStartupValue("cellular,ethernet");
2646 manager()->SetStartupPortalList(kStartupValue);
2647 // Ensure profile value is not overwritten, so when we save the default
2648 // profile, the correct value will still be written.
2649 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2650
2651 // However we should read back a different list.
2652 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2653 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2654 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2655
2656 const string kRuntimeValue("ppp");
2657 // Setting a runtime value over the control API should overwrite both
2658 // the profile value and what we read back.
2659 Error error;
2660 manager()->mutable_store()->SetStringProperty(
2661 flimflam::kCheckPortalListProperty,
2662 kRuntimeValue,
2663 &error);
2664 ASSERT_TRUE(error.IsSuccess());
2665 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2666 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2667 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2668 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2669}
2670
Paul Stewart036dba02012-08-07 12:34:41 -07002671TEST_F(ManagerTest, LinkMonitorEnabled) {
2672 const string kEnabledTechnologies("wifi,vpn");
2673 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2674 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2675 EXPECT_FALSE(
2676 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2677}
2678
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002679TEST_F(ManagerTest, EnableTechnology) {
2680 Error error(Error::kOperationInitiated);
2681 ResultCallback callback;
2682 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2683 EXPECT_TRUE(error.IsSuccess());
2684
Joshua Krollda798622012-06-05 12:30:48 -07002685 ON_CALL(*mock_devices_[0], technology())
2686 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002687
2688 manager()->RegisterDevice(mock_devices_[0]);
2689
2690 // Device is enabled, so expect operation is successful.
2691 mock_devices_[0]->enabled_ = true;
2692 error.Populate(Error::kOperationInitiated);
2693 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2694 EXPECT_TRUE(error.IsSuccess());
2695
2696 // Device is disabled, so expect operation in progress.
2697 mock_devices_[0]->enabled_ = false;
2698 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2699 error.Populate(Error::kOperationInitiated);
2700 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2701 EXPECT_TRUE(error.IsOngoing());
2702}
2703
2704TEST_F(ManagerTest, DisableTechnology) {
2705 Error error(Error::kOperationInitiated);
2706 ResultCallback callback;
2707 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2708 EXPECT_TRUE(error.IsSuccess());
2709
Joshua Krollda798622012-06-05 12:30:48 -07002710 ON_CALL(*mock_devices_[0], technology())
2711 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002712
2713 manager()->RegisterDevice(mock_devices_[0]);
2714
2715 // Device is disabled, so expect operation is successful.
2716 error.Populate(Error::kOperationInitiated);
2717 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2718 EXPECT_TRUE(error.IsSuccess());
2719
2720 // Device is enabled, so expect operation in progress.
2721 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2722 mock_devices_[0]->enabled_ = true;
2723 error.Populate(Error::kOperationInitiated);
2724 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2725 EXPECT_TRUE(error.IsOngoing());
2726}
2727
Paul Stewart4d5efb72012-09-17 12:24:34 -07002728TEST_F(ManagerTest, IgnoredSearchList) {
2729 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
2730 SetResolver(resolver.get());
2731 vector<string> ignored_paths;
2732 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2733 SetIgnoredDNSSearchPaths("");
2734 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
2735
2736 const string kIgnored0 = "chromium.org";
2737 ignored_paths.push_back(kIgnored0);
2738 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2739 SetIgnoredDNSSearchPaths(kIgnored0);
2740 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
2741
2742 const string kIgnored1 = "google.com";
2743 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
2744 ignored_paths.push_back(kIgnored1);
2745 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2746 SetIgnoredDNSSearchPaths(kIgnoredSum);
2747 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
2748
2749 SetResolver(Resolver::GetInstance());
2750}
2751
Paul Stewartbfb82552012-10-24 16:48:48 -07002752TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
2753 // Test to make sure that every service state-change causes the
2754 // Manager to emit a new service list.
2755 scoped_refptr<MockService> mock_service(
2756 new NiceMock<MockService>(control_interface(),
2757 dispatcher(),
2758 metrics(),
2759 manager()));
2760 EXPECT_CALL(*mock_service, state())
2761 .WillRepeatedly(Return(Service::kStateIdle));
2762
2763 manager()->RegisterService(mock_service);
2764 EXPECT_CALL(
2765 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2766 flimflam::kServicesProperty, _)).Times(1);
2767 EXPECT_CALL(
2768 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2769 flimflam::kServiceWatchListProperty, _)).Times(1);
2770 CompleteServiceSort();
2771
2772 Mock::VerifyAndClearExpectations(manager_adaptor_);
2773 EXPECT_CALL(
2774 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2775 flimflam::kServicesProperty, _)).Times(1);
2776 EXPECT_CALL(
2777 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2778 flimflam::kServiceWatchListProperty, _)).Times(1);
2779 manager()->UpdateService(mock_service.get());
2780 CompleteServiceSort();
2781
2782 manager()->DeregisterService(mock_service);
2783}
2784
2785TEST_F(ManagerTest, EnumerateServices) {
2786 scoped_refptr<MockService> mock_service(
2787 new NiceMock<MockService>(control_interface(),
2788 dispatcher(),
2789 metrics(),
2790 manager()));
2791 manager()->RegisterService(mock_service);
2792
2793 EXPECT_CALL(*mock_service, state())
2794 .WillRepeatedly(Return(Service::kStateConnected));
2795 EXPECT_CALL(*mock_service, IsVisible())
2796 .WillRepeatedly(Return(false));
2797 EXPECT_TRUE(EnumerateAvailableServices().empty());
2798 EXPECT_TRUE(EnumerateWatchedServices().empty());
2799
2800 EXPECT_CALL(*mock_service, state())
2801 .WillRepeatedly(Return(Service::kStateIdle));
2802 EXPECT_TRUE(EnumerateAvailableServices().empty());
2803 EXPECT_TRUE(EnumerateWatchedServices().empty());
2804
2805 EXPECT_CALL(*mock_service, IsVisible())
2806 .WillRepeatedly(Return(true));
2807 Service::ConnectState unwatched_states[] = {
2808 Service::kStateUnknown,
2809 Service::kStateIdle,
2810 Service::kStateFailure
2811 };
2812 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
2813 EXPECT_CALL(*mock_service, state())
2814 .WillRepeatedly(Return(unwatched_states[i]));
2815 EXPECT_FALSE(EnumerateAvailableServices().empty());
2816 EXPECT_TRUE(EnumerateWatchedServices().empty());
2817 }
2818
2819 Service::ConnectState watched_states[] = {
2820 Service::kStateAssociating,
2821 Service::kStateConfiguring,
2822 Service::kStateConnected,
2823 Service::kStateDisconnected,
2824 Service::kStatePortal,
2825 Service::kStateOnline
2826 };
2827 for (size_t i = 0; i < arraysize(watched_states); ++i) {
2828 EXPECT_CALL(*mock_service, state())
2829 .WillRepeatedly(Return(watched_states[i]));
2830 EXPECT_FALSE(EnumerateAvailableServices().empty());
2831 EXPECT_FALSE(EnumerateWatchedServices().empty());
2832 }
2833
2834 manager()->DeregisterService(mock_service);
2835}
2836
Chris Masone9be4a9d2011-05-16 15:44:09 -07002837} // namespace shill