blob: edf03f0d4e4d587fe65ef63b9eb1832b1c5bb491 [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
1267 // This will pop profile1, which should cause all our profiles to unload.
1268 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001269 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001270
1271 // 2 of the 4 services added above should have been unregistered and
1272 // removed, leaving 2.
1273 EXPECT_EQ(2, manager()->services_.size());
1274 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1275 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001276
1277 // Expect the unloaded services to lose their profile reference.
1278 EXPECT_FALSE(s_will_remove0->profile());
1279 EXPECT_FALSE(s_will_remove1->profile());
1280
1281 // If we explicitly deregister a service, the effect should be the same
1282 // with respect to the profile reference.
1283 ASSERT_TRUE(s_will_not_remove0->profile());
1284 manager()->DeregisterService(s_will_not_remove0);
1285 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001286}
1287
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001288TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001289 {
1290 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001291 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1292 flimflam::kOfflineModeProperty,
1293 PropertyStoreTest::kBoolV,
1294 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001295 }
1296 {
1297 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001298 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1299 flimflam::kCountryProperty,
1300 PropertyStoreTest::kStringV,
1301 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001302 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001303 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001304 {
1305 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001306 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1307 flimflam::kCountryProperty,
1308 PropertyStoreTest::kBoolV,
1309 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001310 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001311 }
1312 {
1313 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001314 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1315 flimflam::kOfflineModeProperty,
1316 PropertyStoreTest::kStringV,
1317 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001318 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001319 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001320 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001321 {
1322 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001323 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001324 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001325 flimflam::kEnabledTechnologiesProperty,
1326 PropertyStoreTest::kStringsV,
1327 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001328 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001329 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001330}
1331
mukesh agrawal32399322011-09-01 10:53:43 -07001332TEST_F(ManagerTest, RequestScan) {
1333 {
1334 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001335 manager()->RegisterDevice(mock_devices_[0].get());
1336 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001337 EXPECT_CALL(*mock_devices_[0], technology())
1338 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001339 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001340 EXPECT_CALL(*mock_devices_[1], technology())
1341 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001342 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001343 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001344 }
1345
1346 {
1347 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001348 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001349 EXPECT_EQ(Error::kInvalidArguments, error.type());
1350 }
1351}
1352
Darin Petkovb65c2452012-02-23 15:17:06 +01001353TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001354 KeyValueStore args;
1355 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001356 manager()->GetService(args, &e);
1357 EXPECT_EQ(Error::kInvalidArguments, e.type());
1358 EXPECT_EQ("must specify service type", e.message());
1359}
1360
1361TEST_F(ManagerTest, GetServiceUnknownType) {
1362 KeyValueStore args;
1363 Error e;
1364 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1365 manager()->GetService(args, &e);
1366 EXPECT_EQ(Error::kNotSupported, e.type());
1367 EXPECT_EQ("service type is unsupported", e.message());
1368}
1369
1370TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1371 KeyValueStore args;
1372 Error e;
1373 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1374 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001375 EXPECT_EQ(Error::kInvalidArguments, e.type());
1376 EXPECT_EQ("no wifi devices available", e.message());
1377}
1378
Darin Petkovb65c2452012-02-23 15:17:06 +01001379TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001380 KeyValueStore args;
1381 Error e;
1382 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001383 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001384 manager()->RegisterDevice(mock_wifi_);
1385 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1386 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001387 manager()->GetService(args, &e);
1388 EXPECT_TRUE(e.IsSuccess());
1389}
1390
Darin Petkov33af05c2012-02-28 10:10:30 +01001391TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1392 KeyValueStore args;
1393 Error e;
1394 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001395 scoped_refptr<MockProfile> profile(
1396 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1397 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001398 ServiceRefPtr service = manager()->GetService(args, &e);
1399 EXPECT_EQ(Error::kNotSupported, e.type());
1400 EXPECT_FALSE(service);
1401}
1402
Darin Petkovb65c2452012-02-23 15:17:06 +01001403TEST_F(ManagerTest, GetServiceVPN) {
1404 KeyValueStore args;
1405 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001406 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001407 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001408 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1409 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001410 scoped_refptr<MockProfile> profile(
1411 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1412 AdoptProfile(manager(), profile);
1413 ServiceRefPtr updated_service;
1414 EXPECT_CALL(*profile, UpdateService(_))
1415 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1416 ServiceRefPtr configured_service;
1417 EXPECT_CALL(*profile, ConfigureService(_))
1418 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001419 ServiceRefPtr service = manager()->GetService(args, &e);
1420 EXPECT_TRUE(e.IsSuccess());
1421 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001422 EXPECT_EQ(service, updated_service);
1423 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001424}
1425
Darin Petkovc63dcf02012-05-24 11:51:43 +02001426TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1427 KeyValueStore args;
1428 Error e;
1429 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1430 ServiceRefPtr service = manager()->GetService(args, &e);
1431 EXPECT_EQ(Error::kInvalidArguments, e.type());
1432 EXPECT_EQ("Missing WiMAX network id.", e.message());
1433 EXPECT_FALSE(service);
1434}
1435
Darin Petkovd1cd7972012-05-22 15:26:15 +02001436TEST_F(ManagerTest, GetServiceWiMax) {
1437 KeyValueStore args;
1438 Error e;
1439 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001440 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1441 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1442 ServiceRefPtr service = manager()->GetService(args, &e);
1443 EXPECT_TRUE(e.IsSuccess());
1444 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001445}
1446
Paul Stewart7f61e522012-03-22 11:13:45 -07001447TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1448 // Manager calls ActiveProfile() so we need at least one profile installed.
1449 scoped_refptr<MockProfile> profile(
1450 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1451 AdoptProfile(manager(), profile);
1452
1453 KeyValueStore args;
1454 args.SetString(flimflam::kProfileProperty, "xxx");
1455 Error error;
1456 manager()->ConfigureService(args, &error);
1457 EXPECT_EQ(Error::kInvalidArguments, error.type());
1458 EXPECT_EQ("Invalid profile name xxx", error.message());
1459}
1460
1461TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1462 // Manager calls ActiveProfile() so we need at least one profile installed.
1463 scoped_refptr<MockProfile> profile(
1464 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1465 AdoptProfile(manager(), profile);
1466
1467 KeyValueStore args;
1468 Error error;
1469 manager()->ConfigureService(args, &error);
1470 EXPECT_EQ(Error::kInvalidArguments, error.type());
1471 EXPECT_EQ("must specify service type", error.message());
1472}
1473
1474// A registered service in the ephemeral profile should be moved to the
1475// active profile as a part of configuration if no profile was explicitly
1476// specified.
1477TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1478 scoped_refptr<MockProfile> profile(
1479 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1480
1481 AdoptProfile(manager(), profile); // This is now the active profile.
1482
1483 const std::vector<uint8_t> ssid;
1484 scoped_refptr<MockWiFiService> service(
1485 new NiceMock<MockWiFiService>(control_interface(),
1486 dispatcher(),
1487 metrics(),
1488 manager(),
1489 mock_wifi_,
1490 ssid,
1491 "",
1492 "",
1493 false));
1494
1495 manager()->RegisterService(service);
1496 service->set_profile(GetEphemeralProfile(manager()));
1497
1498 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1499 // the same device as that used above causes a refcounting loop.
1500 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1501 dispatcher(),
1502 metrics(),
1503 manager(),
1504 "wifi1",
1505 "addr5",
1506 5));
1507 manager()->RegisterDevice(wifi);
1508 EXPECT_CALL(*wifi, GetService(_, _))
1509 .WillOnce(Return(service));
1510 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1511 .WillOnce(Return(true));
1512 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1513 .WillOnce(Return(true));
1514
1515 KeyValueStore args;
1516 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1517 Error error;
1518 manager()->ConfigureService(args, &error);
1519 EXPECT_TRUE(error.IsSuccess());
1520}
1521
1522// If were configure a service that was already registered and explicitly
1523// specify a profile, it should be moved from the profile it was previously
1524// in to the specified profile if one was requested.
1525TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1526 scoped_refptr<MockProfile> profile0(
1527 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1528 scoped_refptr<MockProfile> profile1(
1529 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1530
1531 const string kProfileName0 = "profile0";
1532 const string kProfileName1 = "profile1";
1533
1534 EXPECT_CALL(*profile0, GetRpcIdentifier())
1535 .WillRepeatedly(Return(kProfileName0));
1536 EXPECT_CALL(*profile1, GetRpcIdentifier())
1537 .WillRepeatedly(Return(kProfileName1));
1538
1539 AdoptProfile(manager(), profile0);
1540 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1541
1542 const std::vector<uint8_t> ssid;
1543 scoped_refptr<MockWiFiService> service(
1544 new NiceMock<MockWiFiService>(control_interface(),
1545 dispatcher(),
1546 metrics(),
1547 manager(),
1548 mock_wifi_,
1549 ssid,
1550 "",
1551 "",
1552 false));
1553
1554 manager()->RegisterService(service);
1555 service->set_profile(profile1);
1556
1557 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1558 // the same device as that used above causes a refcounting loop.
1559 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1560 dispatcher(),
1561 metrics(),
1562 manager(),
1563 "wifi1",
1564 "addr5",
1565 5));
1566 manager()->RegisterDevice(wifi);
1567 EXPECT_CALL(*wifi, GetService(_, _))
1568 .WillOnce(Return(service));
1569 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1570 .WillOnce(Return(true));
1571 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1572 .WillOnce(Return(true));
1573 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1574 .WillOnce(Return(true));
1575
1576 KeyValueStore args;
1577 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1578 args.SetString(flimflam::kProfileProperty, kProfileName0);
1579 Error error;
1580 manager()->ConfigureService(args, &error);
1581 EXPECT_TRUE(error.IsSuccess());
1582 service->set_profile(NULL); // Breaks refcounting loop.
1583}
1584
1585// An unregistered service should remain unregistered, but its contents should
1586// be saved to the specified profile nonetheless.
1587TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1588 scoped_refptr<MockProfile> profile0(
1589 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1590 scoped_refptr<MockProfile> profile1(
1591 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1592
1593 const string kProfileName0 = "profile0";
1594 const string kProfileName1 = "profile1";
1595
1596 EXPECT_CALL(*profile0, GetRpcIdentifier())
1597 .WillRepeatedly(Return(kProfileName0));
1598 EXPECT_CALL(*profile1, GetRpcIdentifier())
1599 .WillRepeatedly(Return(kProfileName1));
1600
1601 AdoptProfile(manager(), profile0);
1602 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1603
1604 const std::vector<uint8_t> ssid;
1605 scoped_refptr<MockWiFiService> service(
1606 new NiceMock<MockWiFiService>(control_interface(),
1607 dispatcher(),
1608 metrics(),
1609 manager(),
1610 mock_wifi_,
1611 ssid,
1612 "",
1613 "",
1614 false));
1615
1616 service->set_profile(profile1);
1617
1618 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1619 // the same device as that used above causes a refcounting loop.
1620 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1621 dispatcher(),
1622 metrics(),
1623 manager(),
1624 "wifi1",
1625 "addr5",
1626 5));
1627 manager()->RegisterDevice(wifi);
1628 EXPECT_CALL(*wifi, GetService(_, _))
1629 .WillOnce(Return(service));
1630 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1631 .WillOnce(Return(true));
1632 EXPECT_CALL(*profile0, AdoptService(_))
1633 .Times(0);
1634 EXPECT_CALL(*profile1, AdoptService(_))
1635 .Times(0);
1636
1637 KeyValueStore args;
1638 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1639 args.SetString(flimflam::kProfileProperty, kProfileName0);
1640 Error error;
1641 manager()->ConfigureService(args, &error);
1642 EXPECT_TRUE(error.IsSuccess());
1643}
1644
Paul Stewart22aa71b2011-09-16 12:15:11 -07001645TEST_F(ManagerTest, TechnologyOrder) {
1646 Error error;
1647 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1648 string(flimflam::kTypeWifi), &error);
1649 ASSERT_TRUE(error.IsSuccess());
1650 EXPECT_EQ(manager()->GetTechnologyOrder(),
1651 string(flimflam::kTypeEthernet) + "," +
1652 string(flimflam::kTypeWifi));
1653
1654 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1655 string(flimflam::kTypeWifi), &error);
1656 ASSERT_FALSE(error.IsSuccess());
1657 EXPECT_EQ(Error::kInvalidArguments, error.type());
1658 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1659 string(flimflam::kTypeWifi),
1660 manager()->GetTechnologyOrder());
1661}
1662
1663TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001664 // TODO(quiche): Some of these tests would probably fit better in
1665 // service_unittest, since the actual comparison of Services is
1666 // implemented in Service. (crosbug.com/23370)
1667
Paul Stewart22aa71b2011-09-16 12:15:11 -07001668 scoped_refptr<MockService> mock_service0(
1669 new NiceMock<MockService>(control_interface(),
1670 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001671 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001672 manager()));
1673 scoped_refptr<MockService> mock_service1(
1674 new NiceMock<MockService>(control_interface(),
1675 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001676 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001677 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001678
1679 manager()->RegisterService(mock_service0);
1680 manager()->RegisterService(mock_service1);
1681
1682 // Services should already be sorted by UniqueName
1683 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1684
1685 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001686 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001687 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1688
1689 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001690 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001691 manager()->UpdateService(mock_service1);
1692 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1693
1694 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001695 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001696 manager()->UpdateService(mock_service0);
1697 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1698
1699 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001700 EXPECT_CALL(*mock_service0.get(), technology())
1701 .WillRepeatedly(Return((Technology::kWifi)));
1702 EXPECT_CALL(*mock_service1.get(), technology())
1703 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001704
1705 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001706 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001707 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001708 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1709
1710 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1711 string(flimflam::kTypeEthernet), &error);
1712 EXPECT_TRUE(error.IsSuccess());
1713 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1714
Gaurav Shah435de2c2011-11-17 19:01:07 -08001715 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001716 mock_service0->set_priority(1);
1717 manager()->UpdateService(mock_service0);
1718 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1719
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001720 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001721 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001722 manager()->UpdateService(mock_service1);
1723 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1724
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001725 // Auto-connect.
1726 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001727 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001728 mock_service1->set_auto_connect(false);
1729 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001730 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1731
Paul Stewartdf3c0a82012-11-09 15:54:33 -08001732 // Test is-dependent-on. It doesn't make sense to have this ranking compare
1733 // to any of the others below, so we reset to the default state after
1734 // testing.
1735 EXPECT_CALL(*mock_service1.get(),
1736 IsDependentOn(ServiceRefPtr(mock_service0.get())))
1737 .WillOnce(Return(true))
1738 .WillRepeatedly(Return(false));
1739 manager()->UpdateService(mock_service1);
1740 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1741 manager()->UpdateService(mock_service0);
1742 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1743
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001744 // Connectable.
1745 mock_service1->set_connectable(true);
1746 manager()->UpdateService(mock_service1);
1747 mock_service0->set_connectable(false);
1748 manager()->UpdateService(mock_service0);
1749 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1750
1751 // IsFailed.
1752 EXPECT_CALL(*mock_service0.get(), state())
1753 .WillRepeatedly(Return(Service::kStateIdle));
1754 EXPECT_CALL(*mock_service0.get(), IsFailed())
1755 .WillRepeatedly(Return(false));
1756 manager()->UpdateService(mock_service0);
1757 EXPECT_CALL(*mock_service0.get(), state())
1758 .WillRepeatedly(Return(Service::kStateFailure));
1759 EXPECT_CALL(*mock_service1.get(), IsFailed())
1760 .WillRepeatedly(Return(true));
1761 manager()->UpdateService(mock_service1);
1762 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1763
1764 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001765 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001766 .WillRepeatedly(Return(Service::kStateAssociating));
1767 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001768 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001769 manager()->UpdateService(mock_service1);
1770 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1771
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001772 // Connected.
1773 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001774 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001775 EXPECT_CALL(*mock_service0.get(), IsConnected())
1776 .WillRepeatedly(Return(true));
1777 manager()->UpdateService(mock_service0);
1778 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1779
Paul Stewarta121c442012-06-09 14:12:58 -07001780 // Portal.
1781 EXPECT_CALL(*mock_service1.get(), state())
1782 .WillRepeatedly(Return(Service::kStateConnected));
1783 EXPECT_CALL(*mock_service1.get(), IsConnected())
1784 .WillRepeatedly(Return(true));
1785 manager()->UpdateService(mock_service1);
1786 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1787
Paul Stewart22aa71b2011-09-16 12:15:11 -07001788 manager()->DeregisterService(mock_service0);
1789 manager()->DeregisterService(mock_service1);
1790}
1791
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001792TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001793 MockMetrics mock_metrics;
1794 manager()->set_metrics(&mock_metrics);
1795
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001796 scoped_refptr<MockService> mock_service0(
1797 new NiceMock<MockService>(control_interface(),
1798 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001799 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001800 manager()));
1801 scoped_refptr<MockService> mock_service1(
1802 new NiceMock<MockService>(control_interface(),
1803 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001804 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001805 manager()));
1806
1807 scoped_refptr<MockConnection> mock_connection0(
1808 new NiceMock<MockConnection>(device_info_.get()));
1809 scoped_refptr<MockConnection> mock_connection1(
1810 new NiceMock<MockConnection>(device_info_.get()));
1811
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001812 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001813 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001814 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001815 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001816 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001817 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001818
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001819 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001820 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001821
1822 mock_service1->set_priority(1);
1823 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001824 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001825
1826 mock_service1->set_priority(0);
1827 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001828 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001829
Paul Stewartce4ec192012-03-14 12:53:46 -07001830 mock_service0->set_mock_connection(mock_connection0);
1831 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001832
1833 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001834 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001835 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001836
Darin Petkova5e07ef2012-07-09 14:27:57 +02001837 ServiceWatcher service_watcher;
1838 int tag =
1839 manager()->RegisterDefaultServiceCallback(
1840 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1841 service_watcher.AsWeakPtr()));
1842 EXPECT_EQ(1, tag);
1843
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001844 mock_service1->set_priority(1);
1845 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1846 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001847 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001848 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001849 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001850
Darin Petkova5e07ef2012-07-09 14:27:57 +02001851 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001852 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001853 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001854 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001855 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001856 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001857 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001858
Paul Stewartce4ec192012-03-14 12:53:46 -07001859 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001860 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001861 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001862 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001863
1864 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001865 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001866}
1867
Darin Petkova5e07ef2012-07-09 14:27:57 +02001868TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1869 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1870 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1871
1872 MockMetrics mock_metrics;
1873 manager()->set_metrics(&mock_metrics);
1874
1875 scoped_refptr<MockService> mock_service(
1876 new NiceMock<MockService>(
1877 control_interface(), dispatcher(), metrics(), manager()));
1878 ServiceRefPtr service = mock_service;
1879 ServiceRefPtr null_service;
1880
1881 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1882 manager()->NotifyDefaultServiceChanged(null_service);
1883
1884 ServiceWatcher service_watcher1;
1885 ServiceWatcher service_watcher2;
1886 int tag1 =
1887 manager()->RegisterDefaultServiceCallback(
1888 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1889 service_watcher1.AsWeakPtr()));
1890 EXPECT_EQ(1, tag1);
1891 int tag2 =
1892 manager()->RegisterDefaultServiceCallback(
1893 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1894 service_watcher2.AsWeakPtr()));
1895 EXPECT_EQ(2, tag2);
1896
1897 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1898 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1899 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1900 manager()->NotifyDefaultServiceChanged(null_service);
1901
1902 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1903 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1904 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1905 manager()->NotifyDefaultServiceChanged(mock_service);
1906
1907 manager()->DeregisterDefaultServiceCallback(tag1);
1908 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1909 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1910 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1911 manager()->NotifyDefaultServiceChanged(mock_service);
1912 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1913
1914 manager()->DeregisterDefaultServiceCallback(tag2);
1915 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1916 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1917 manager()->NotifyDefaultServiceChanged(mock_service);
1918
1919 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1920 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1921}
1922
Gaurav Shah435de2c2011-11-17 19:01:07 -08001923TEST_F(ManagerTest, AvailableTechnologies) {
1924 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1925 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001926 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001927 manager(),
1928 "null4",
1929 "addr4",
1930 0));
1931 manager()->RegisterDevice(mock_devices_[0]);
1932 manager()->RegisterDevice(mock_devices_[1]);
1933 manager()->RegisterDevice(mock_devices_[2]);
1934 manager()->RegisterDevice(mock_devices_[3]);
1935
1936 ON_CALL(*mock_devices_[0].get(), technology())
1937 .WillByDefault(Return(Technology::kEthernet));
1938 ON_CALL(*mock_devices_[1].get(), technology())
1939 .WillByDefault(Return(Technology::kWifi));
1940 ON_CALL(*mock_devices_[2].get(), technology())
1941 .WillByDefault(Return(Technology::kCellular));
1942 ON_CALL(*mock_devices_[3].get(), technology())
1943 .WillByDefault(Return(Technology::kWifi));
1944
1945 set<string> expected_technologies;
1946 expected_technologies.insert(Technology::NameFromIdentifier(
1947 Technology::kEthernet));
1948 expected_technologies.insert(Technology::NameFromIdentifier(
1949 Technology::kWifi));
1950 expected_technologies.insert(Technology::NameFromIdentifier(
1951 Technology::kCellular));
1952 Error error;
1953 vector<string> technologies = manager()->AvailableTechnologies(&error);
1954
1955 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1956 ContainerEq(expected_technologies));
1957}
1958
1959TEST_F(ManagerTest, ConnectedTechnologies) {
1960 scoped_refptr<MockService> connected_service1(
1961 new NiceMock<MockService>(control_interface(),
1962 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001963 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001964 manager()));
1965 scoped_refptr<MockService> connected_service2(
1966 new NiceMock<MockService>(control_interface(),
1967 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001968 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001969 manager()));
1970 scoped_refptr<MockService> disconnected_service1(
1971 new NiceMock<MockService>(control_interface(),
1972 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001973 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001974 manager()));
1975 scoped_refptr<MockService> disconnected_service2(
1976 new NiceMock<MockService>(control_interface(),
1977 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001978 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001979 manager()));
1980
1981 ON_CALL(*connected_service1.get(), IsConnected())
1982 .WillByDefault(Return(true));
1983 ON_CALL(*connected_service2.get(), IsConnected())
1984 .WillByDefault(Return(true));
1985
1986 manager()->RegisterService(connected_service1);
1987 manager()->RegisterService(connected_service2);
1988 manager()->RegisterService(disconnected_service1);
1989 manager()->RegisterService(disconnected_service2);
1990
1991 manager()->RegisterDevice(mock_devices_[0]);
1992 manager()->RegisterDevice(mock_devices_[1]);
1993 manager()->RegisterDevice(mock_devices_[2]);
1994 manager()->RegisterDevice(mock_devices_[3]);
1995
1996 ON_CALL(*mock_devices_[0].get(), technology())
1997 .WillByDefault(Return(Technology::kEthernet));
1998 ON_CALL(*mock_devices_[1].get(), technology())
1999 .WillByDefault(Return(Technology::kWifi));
2000 ON_CALL(*mock_devices_[2].get(), technology())
2001 .WillByDefault(Return(Technology::kCellular));
2002 ON_CALL(*mock_devices_[3].get(), technology())
2003 .WillByDefault(Return(Technology::kWifi));
2004
2005 mock_devices_[0]->SelectService(connected_service1);
2006 mock_devices_[1]->SelectService(disconnected_service1);
2007 mock_devices_[2]->SelectService(disconnected_service2);
2008 mock_devices_[3]->SelectService(connected_service2);
2009
2010 set<string> expected_technologies;
2011 expected_technologies.insert(Technology::NameFromIdentifier(
2012 Technology::kEthernet));
2013 expected_technologies.insert(Technology::NameFromIdentifier(
2014 Technology::kWifi));
2015 Error error;
2016
2017 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2018 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2019 ContainerEq(expected_technologies));
2020}
2021
2022TEST_F(ManagerTest, DefaultTechnology) {
2023 scoped_refptr<MockService> connected_service(
2024 new NiceMock<MockService>(control_interface(),
2025 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002026 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002027 manager()));
2028 scoped_refptr<MockService> disconnected_service(
2029 new NiceMock<MockService>(control_interface(),
2030 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002031 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002032 manager()));
2033
2034 // Connected. WiFi.
2035 ON_CALL(*connected_service.get(), IsConnected())
2036 .WillByDefault(Return(true));
2037 ON_CALL(*connected_service.get(), state())
2038 .WillByDefault(Return(Service::kStateConnected));
2039 ON_CALL(*connected_service.get(), technology())
2040 .WillByDefault(Return(Technology::kWifi));
2041
2042 // Disconnected. Ethernet.
2043 ON_CALL(*disconnected_service.get(), technology())
2044 .WillByDefault(Return(Technology::kEthernet));
2045
2046 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002047 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002048 Error error;
2049 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2050
2051
2052 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002053 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002054 // Connected service should be brought to the front now.
2055 string expected_technology =
2056 Technology::NameFromIdentifier(Technology::kWifi);
2057 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2058}
2059
Paul Stewart212d60f2012-07-12 10:59:13 -07002060TEST_F(ManagerTest, Stop) {
2061 scoped_refptr<MockProfile> profile(
2062 new NiceMock<MockProfile>(control_interface(), manager(), ""));
2063 AdoptProfile(manager(), profile);
2064 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002065 new NiceMock<MockService>(control_interface(),
2066 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002067 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002068 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002069 manager()->RegisterService(service);
2070 manager()->RegisterDevice(mock_devices_[0]);
2071 EXPECT_CALL(*profile.get(),
2072 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2073 .WillOnce(Return(true));
2074 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2075 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002076 manager()->Stop();
2077}
2078
mukesh agrawal00917ce2011-11-22 23:56:55 +00002079TEST_F(ManagerTest, UpdateServiceConnected) {
2080 scoped_refptr<MockService> mock_service(
2081 new NiceMock<MockService>(control_interface(),
2082 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002083 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002084 manager()));
2085 manager()->RegisterService(mock_service);
2086 EXPECT_FALSE(mock_service->favorite());
2087 EXPECT_FALSE(mock_service->auto_connect());
2088
Gaurav Shah435de2c2011-11-17 19:01:07 -08002089 EXPECT_CALL(*mock_service.get(), IsConnected())
2090 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002091 manager()->UpdateService(mock_service);
2092 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2093 // to mock out MakeFavorite. And mocking that out would break the
2094 // SortServices test. (crosbug.com/23370)
2095 EXPECT_TRUE(mock_service->favorite());
2096 EXPECT_TRUE(mock_service->auto_connect());
2097}
2098
Thieu Led4e9e552012-02-16 16:26:07 -08002099TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2100 // This tests the case where the user connects to a service that is
2101 // currently associated with a profile. We want to make sure that the
2102 // favorite flag is set and that the flag is saved to the current
2103 // profile.
2104 scoped_refptr<MockService> mock_service(
2105 new NiceMock<MockService>(control_interface(),
2106 dispatcher(),
2107 metrics(),
2108 manager()));
2109 manager()->RegisterService(mock_service);
2110 EXPECT_FALSE(mock_service->favorite());
2111 EXPECT_FALSE(mock_service->auto_connect());
2112
Gary Moraind93615e2012-04-27 11:50:03 -07002113 scoped_refptr<MockProfile> profile(
2114 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002115
Gary Moraind93615e2012-04-27 11:50:03 -07002116 mock_service->set_profile(profile);
2117 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002118 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002119 EXPECT_CALL(*profile,
2120 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002121 manager()->UpdateService(mock_service);
2122 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2123 // to mock out MakeFavorite. And mocking that out would break the
2124 // SortServices test. (crosbug.com/23370)
2125 EXPECT_TRUE(mock_service->favorite());
2126 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002127 // This releases the ref on the mock profile.
2128 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002129}
2130
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002131TEST_F(ManagerTest, SaveSuccessfulService) {
2132 scoped_refptr<MockProfile> profile(
2133 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2134 AdoptProfile(manager(), profile);
2135 scoped_refptr<MockService> service(
2136 new NiceMock<MockService>(control_interface(),
2137 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002138 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002139 manager()));
2140
2141 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2142 ServiceRefPtr expect_service(service.get());
2143
2144 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2145 .WillOnce(Return(false));
2146 manager()->RegisterService(service);
2147
2148 EXPECT_CALL(*service.get(), state())
2149 .WillRepeatedly(Return(Service::kStateConnected));
2150 EXPECT_CALL(*service.get(), IsConnected())
2151 .WillRepeatedly(Return(true));
2152 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2153 .WillOnce(Return(true));
2154 manager()->UpdateService(service);
2155}
2156
Darin Petkove7c6ad32012-06-29 10:22:09 +02002157TEST_F(ManagerTest, UpdateDevice) {
2158 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2159 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2160 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2161 AdoptProfile(manager(), profile0); // Passes ownership.
2162 AdoptProfile(manager(), profile1); // Passes ownership.
2163 AdoptProfile(manager(), profile2); // Passes ownership.
2164 DeviceRefPtr device_ref(mock_devices_[0].get());
2165 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2166 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2167 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2168 manager()->UpdateDevice(mock_devices_[0]);
2169}
2170
Paul Stewart1b253142012-01-26 14:05:52 -08002171TEST_F(ManagerTest, EnumerateProfiles) {
2172 vector<string> profile_paths;
2173 for (size_t i = 0; i < 10; i++) {
2174 scoped_refptr<MockProfile> profile(
2175 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002176 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002177 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2178 .WillOnce(Return(profile_paths.back()));
2179 AdoptProfile(manager(), profile);
2180 }
2181
2182 Error error;
2183 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2184 EXPECT_TRUE(error.IsSuccess());
2185 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2186 for (size_t i = 0; i < profile_paths.size(); i++) {
2187 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2188 }
2189}
2190
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002191TEST_F(ManagerTest, AutoConnectOnRegister) {
2192 MockServiceRefPtr service = MakeAutoConnectableService();
2193 EXPECT_CALL(*service.get(), AutoConnect());
2194 manager()->RegisterService(service);
2195 dispatcher()->DispatchPendingEvents();
2196}
2197
2198TEST_F(ManagerTest, AutoConnectOnUpdate) {
2199 MockServiceRefPtr service1 = MakeAutoConnectableService();
2200 service1->set_priority(1);
2201 MockServiceRefPtr service2 = MakeAutoConnectableService();
2202 service2->set_priority(2);
2203 manager()->RegisterService(service1);
2204 manager()->RegisterService(service2);
2205 dispatcher()->DispatchPendingEvents();
2206
2207 EXPECT_CALL(*service1.get(), AutoConnect());
2208 EXPECT_CALL(*service2.get(), state())
2209 .WillRepeatedly(Return(Service::kStateFailure));
2210 EXPECT_CALL(*service2.get(), IsFailed())
2211 .WillRepeatedly(Return(true));
2212 EXPECT_CALL(*service2.get(), IsConnected())
2213 .WillRepeatedly(Return(false));
2214 manager()->UpdateService(service2);
2215 dispatcher()->DispatchPendingEvents();
2216}
2217
2218TEST_F(ManagerTest, AutoConnectOnDeregister) {
2219 MockServiceRefPtr service1 = MakeAutoConnectableService();
2220 service1->set_priority(1);
2221 MockServiceRefPtr service2 = MakeAutoConnectableService();
2222 service2->set_priority(2);
2223 manager()->RegisterService(service1);
2224 manager()->RegisterService(service2);
2225 dispatcher()->DispatchPendingEvents();
2226
2227 EXPECT_CALL(*service1.get(), AutoConnect());
2228 manager()->DeregisterService(service2);
2229 dispatcher()->DispatchPendingEvents();
2230}
2231
Darin Petkov3ec55342012-09-28 14:04:44 +02002232TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2233 MockServiceRefPtr service = MakeAutoConnectableService();
2234 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2235 SetPowerManager();
2236 EXPECT_CALL(*service, AutoConnect()).Times(0);
2237 manager()->RegisterService(service);
2238 dispatcher()->DispatchPendingEvents();
2239}
2240
Darin Petkovca621542012-07-25 14:25:56 +02002241TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2242 MockServiceRefPtr service = MakeAutoConnectableService();
2243 SetPowerState(PowerManagerProxyDelegate::kMem);
2244 SetPowerManager();
2245 EXPECT_CALL(*service, AutoConnect()).Times(0);
2246 manager()->RegisterService(service);
2247 dispatcher()->DispatchPendingEvents();
2248}
2249
2250TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2251 MockServiceRefPtr service = MakeAutoConnectableService();
2252 SetPowerState(PowerManagerProxyDelegate::kOn);
2253 SetPowerManager();
2254 EXPECT_CALL(*service, AutoConnect());
2255 manager()->RegisterService(service);
2256 dispatcher()->DispatchPendingEvents();
2257}
2258
2259TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2260 MockServiceRefPtr service = MakeAutoConnectableService();
2261 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2262 SetPowerManager();
2263 EXPECT_CALL(*service, AutoConnect());
2264 manager()->RegisterService(service);
2265 dispatcher()->DispatchPendingEvents();
2266}
2267
Paul Stewart63864b62012-11-07 15:10:55 -08002268TEST_F(ManagerTest, AutoConnectWhileNotRunning) {
2269 SetRunning(false);
2270 MockServiceRefPtr service = MakeAutoConnectableService();
2271 EXPECT_CALL(*service, AutoConnect()).Times(0);
2272 manager()->RegisterService(service);
2273 dispatcher()->DispatchPendingEvents();
2274}
2275
Darin Petkovca621542012-07-25 14:25:56 +02002276TEST_F(ManagerTest, OnPowerStateChanged) {
2277 MockServiceRefPtr service = MakeAutoConnectableService();
2278 SetPowerState(PowerManagerProxyDelegate::kOn);
2279 SetPowerManager();
2280 EXPECT_CALL(*service, AutoConnect());
2281 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002282 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002283 dispatcher()->DispatchPendingEvents();
2284
mukesh agrawal784566d2012-08-08 18:32:58 -07002285 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002286 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2287 EXPECT_CALL(*service, AutoConnect());
2288 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002289 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002290
mukesh agrawal784566d2012-08-08 18:32:58 -07002291 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002292 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2293 EXPECT_CALL(*service, AutoConnect()).Times(0);
2294 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002295 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002296}
2297
Darin Petkov3ec55342012-09-28 14:04:44 +02002298TEST_F(ManagerTest, AddTerminationAction) {
2299 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
2300 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_));
2301 SetPowerManager();
2302 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2303 manager()->AddTerminationAction("action1", base::Closure());
2304 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2305 manager()->AddTerminationAction("action2", base::Closure());
2306}
2307
2308TEST_F(ManagerTest, RemoveTerminationAction) {
2309 static const char kKey1[] = "action1";
2310 static const char kKey2[] = "action2";
2311
2312 MockPowerManager &power_manager = *power_manager_;
2313 SetPowerManager();
2314
2315 // Removing an action when the hook table is empty should not result in any
2316 // calls to the power manager.
2317 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2318 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2319 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2320 manager()->RemoveTerminationAction("unknown");
2321 Mock::VerifyAndClearExpectations(&power_manager);
2322
2323 manager()->AddTerminationAction(kKey1, base::Closure());
2324 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2325 manager()->AddTerminationAction(kKey2, base::Closure());
2326
2327 // Removing an action that ends up with a non-empty hook table should not
2328 // result in any calls to the power manager.
2329 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2330 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2331 manager()->RemoveTerminationAction(kKey1);
2332 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2333 Mock::VerifyAndClearExpectations(&power_manager);
2334
2335 // Removing the last action should trigger unregistering from the power
2336 // manager.
2337 EXPECT_CALL(power_manager, UnregisterSuspendDelay());
2338 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2339 manager()->RemoveTerminationAction(kKey2);
2340 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2341}
2342
2343TEST_F(ManagerTest, RunTerminationActions) {
2344 TerminationActionTest test_action;
2345 const string kActionName = "action";
2346
2347 EXPECT_CALL(test_action, Done(_));
2348 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2349 test_action.AsWeakPtr()));
2350
2351 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2352 Bind(&TerminationActionTest::Action,
2353 test_action.AsWeakPtr()));
2354 test_action.set_manager(manager());
2355 EXPECT_CALL(test_action, Done(_));
2356 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2357 test_action.AsWeakPtr()));
2358}
2359
2360TEST_F(ManagerTest, OnSuspendDelay) {
2361 const uint32 kSeqNumber = 123;
2362 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2363 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2364 SetPowerManager();
2365 OnSuspendDelay(kSeqNumber);
2366}
2367
2368TEST_F(ManagerTest, OnSuspendActionsComplete) {
2369 const uint32 kSeqNumber = 54321;
2370 Error error;
2371 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2372 SetPowerManager();
2373 OnSuspendActionsComplete(kSeqNumber, error);
2374}
2375
Paul Stewartc681fa02012-03-02 19:40:04 -08002376TEST_F(ManagerTest, RecheckPortal) {
2377 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2378 .WillOnce(Return(false));
2379 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2380 .WillOnce(Return(true));
2381 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2382 .Times(0);
2383
2384 manager()->RegisterDevice(mock_devices_[0]);
2385 manager()->RegisterDevice(mock_devices_[1]);
2386 manager()->RegisterDevice(mock_devices_[2]);
2387
2388 manager()->RecheckPortal(NULL);
2389}
2390
Paul Stewartd215af62012-04-24 23:25:50 -07002391TEST_F(ManagerTest, RecheckPortalOnService) {
2392 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2393 dispatcher(),
2394 metrics(),
2395 manager());
2396 EXPECT_CALL(*mock_devices_[0].get(),
2397 IsConnectedToService(IsRefPtrTo(service)))
2398 .WillOnce(Return(false));
2399 EXPECT_CALL(*mock_devices_[1].get(),
2400 IsConnectedToService(IsRefPtrTo(service)))
2401 .WillOnce(Return(true));
2402 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2403 .WillOnce(Return(true));
2404 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2405 .Times(0);
2406
2407 manager()->RegisterDevice(mock_devices_[0]);
2408 manager()->RegisterDevice(mock_devices_[1]);
2409 manager()->RegisterDevice(mock_devices_[2]);
2410
2411 manager()->RecheckPortalOnService(service);
2412}
2413
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002414TEST_F(ManagerTest, GetDefaultService) {
2415 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002416 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002417
2418 scoped_refptr<MockService> mock_service(
2419 new NiceMock<MockService>(control_interface(),
2420 dispatcher(),
2421 metrics(),
2422 manager()));
2423
2424 manager()->RegisterService(mock_service);
2425 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002426 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002427
2428 scoped_refptr<MockConnection> mock_connection(
2429 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002430 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002431 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002432 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002433
Paul Stewartce4ec192012-03-14 12:53:46 -07002434 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002435 manager()->DeregisterService(mock_service);
2436}
2437
Paul Stewart13ed2252012-03-21 12:52:46 -07002438TEST_F(ManagerTest, GetServiceWithGUID) {
2439 scoped_refptr<MockService> mock_service0(
2440 new NiceMock<MockService>(control_interface(),
2441 dispatcher(),
2442 metrics(),
2443 manager()));
2444
2445 scoped_refptr<MockService> mock_service1(
2446 new NiceMock<MockService>(control_interface(),
2447 dispatcher(),
2448 metrics(),
2449 manager()));
2450
Paul Stewartcb59fed2012-03-21 21:14:46 -07002451 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2452 .Times(0);
2453 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2454 .Times(0);
2455
Paul Stewart13ed2252012-03-21 12:52:46 -07002456 manager()->RegisterService(mock_service0);
2457 manager()->RegisterService(mock_service1);
2458
2459 const string kGUID0 = "GUID0";
2460 const string kGUID1 = "GUID1";
2461
2462 {
2463 Error error;
2464 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2465 EXPECT_FALSE(error.IsSuccess());
2466 EXPECT_FALSE(service);
2467 }
2468
2469 KeyValueStore args;
2470 args.SetString(flimflam::kGuidProperty, kGUID1);
2471
2472 {
2473 Error error;
2474 ServiceRefPtr service = manager()->GetService(args, &error);
2475 EXPECT_EQ(Error::kInvalidArguments, error.type());
2476 EXPECT_FALSE(service);
2477 }
2478
2479 mock_service0->set_guid(kGUID0);
2480 mock_service1->set_guid(kGUID1);
2481
2482 {
2483 Error error;
2484 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2485 EXPECT_TRUE(error.IsSuccess());
2486 EXPECT_EQ(mock_service0.get(), service.get());
2487 }
2488
2489 {
2490 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002491 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2492 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002493 ServiceRefPtr service = manager()->GetService(args, &error);
2494 EXPECT_TRUE(error.IsSuccess());
2495 EXPECT_EQ(mock_service1.get(), service.get());
2496 }
2497
2498 manager()->DeregisterService(mock_service0);
2499 manager()->DeregisterService(mock_service1);
2500}
2501
Gary Morain028545d2012-04-07 14:55:52 -07002502
2503TEST_F(ManagerTest, CalculateStateOffline) {
2504 MockMetrics mock_metrics;
2505 manager()->set_metrics(&mock_metrics);
2506 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2507 .Times(AnyNumber());
2508 scoped_refptr<MockService> mock_service0(
2509 new NiceMock<MockService>(control_interface(),
2510 dispatcher(),
2511 metrics(),
2512 manager()));
2513
2514 scoped_refptr<MockService> mock_service1(
2515 new NiceMock<MockService>(control_interface(),
2516 dispatcher(),
2517 metrics(),
2518 manager()));
2519
2520 EXPECT_CALL(*mock_service0.get(), IsConnected())
2521 .WillRepeatedly(Return(false));
2522 EXPECT_CALL(*mock_service1.get(), IsConnected())
2523 .WillRepeatedly(Return(false));
2524
2525 manager()->RegisterService(mock_service0);
2526 manager()->RegisterService(mock_service1);
2527
2528 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2529
2530 manager()->DeregisterService(mock_service0);
2531 manager()->DeregisterService(mock_service1);
2532}
2533
2534TEST_F(ManagerTest, CalculateStateOnline) {
2535 MockMetrics mock_metrics;
2536 manager()->set_metrics(&mock_metrics);
2537 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2538 .Times(AnyNumber());
2539 scoped_refptr<MockService> mock_service0(
2540 new NiceMock<MockService>(control_interface(),
2541 dispatcher(),
2542 metrics(),
2543 manager()));
2544
2545 scoped_refptr<MockService> mock_service1(
2546 new NiceMock<MockService>(control_interface(),
2547 dispatcher(),
2548 metrics(),
2549 manager()));
2550
2551 EXPECT_CALL(*mock_service0.get(), IsConnected())
2552 .WillRepeatedly(Return(false));
2553 EXPECT_CALL(*mock_service1.get(), IsConnected())
2554 .WillRepeatedly(Return(true));
2555 EXPECT_CALL(*mock_service0.get(), state())
2556 .WillRepeatedly(Return(Service::kStateIdle));
2557 EXPECT_CALL(*mock_service1.get(), state())
2558 .WillRepeatedly(Return(Service::kStateConnected));
2559
2560 manager()->RegisterService(mock_service0);
2561 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002562 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002563
2564 EXPECT_EQ("online", manager()->CalculateState(NULL));
2565
2566 manager()->DeregisterService(mock_service0);
2567 manager()->DeregisterService(mock_service1);
2568}
2569
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002570TEST_F(ManagerTest, StartupPortalList) {
2571 // Simulate loading value from the default profile.
2572 const string kProfileValue("wifi,vpn");
2573 manager()->props_.check_portal_list = kProfileValue;
2574
2575 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2576 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2577 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2578
2579 const string kStartupValue("cellular,ethernet");
2580 manager()->SetStartupPortalList(kStartupValue);
2581 // Ensure profile value is not overwritten, so when we save the default
2582 // profile, the correct value will still be written.
2583 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2584
2585 // However we should read back a different list.
2586 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2587 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2588 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2589
2590 const string kRuntimeValue("ppp");
2591 // Setting a runtime value over the control API should overwrite both
2592 // the profile value and what we read back.
2593 Error error;
2594 manager()->mutable_store()->SetStringProperty(
2595 flimflam::kCheckPortalListProperty,
2596 kRuntimeValue,
2597 &error);
2598 ASSERT_TRUE(error.IsSuccess());
2599 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2600 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2601 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2602 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2603}
2604
Paul Stewart036dba02012-08-07 12:34:41 -07002605TEST_F(ManagerTest, LinkMonitorEnabled) {
2606 const string kEnabledTechnologies("wifi,vpn");
2607 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2608 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2609 EXPECT_FALSE(
2610 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2611}
2612
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002613TEST_F(ManagerTest, EnableTechnology) {
2614 Error error(Error::kOperationInitiated);
2615 ResultCallback callback;
2616 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2617 EXPECT_TRUE(error.IsSuccess());
2618
Joshua Krollda798622012-06-05 12:30:48 -07002619 ON_CALL(*mock_devices_[0], technology())
2620 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002621
2622 manager()->RegisterDevice(mock_devices_[0]);
2623
2624 // Device is enabled, so expect operation is successful.
2625 mock_devices_[0]->enabled_ = true;
2626 error.Populate(Error::kOperationInitiated);
2627 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2628 EXPECT_TRUE(error.IsSuccess());
2629
2630 // Device is disabled, so expect operation in progress.
2631 mock_devices_[0]->enabled_ = false;
2632 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2633 error.Populate(Error::kOperationInitiated);
2634 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2635 EXPECT_TRUE(error.IsOngoing());
2636}
2637
2638TEST_F(ManagerTest, DisableTechnology) {
2639 Error error(Error::kOperationInitiated);
2640 ResultCallback callback;
2641 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2642 EXPECT_TRUE(error.IsSuccess());
2643
Joshua Krollda798622012-06-05 12:30:48 -07002644 ON_CALL(*mock_devices_[0], technology())
2645 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002646
2647 manager()->RegisterDevice(mock_devices_[0]);
2648
2649 // Device is disabled, so expect operation is successful.
2650 error.Populate(Error::kOperationInitiated);
2651 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2652 EXPECT_TRUE(error.IsSuccess());
2653
2654 // Device is enabled, so expect operation in progress.
2655 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2656 mock_devices_[0]->enabled_ = true;
2657 error.Populate(Error::kOperationInitiated);
2658 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2659 EXPECT_TRUE(error.IsOngoing());
2660}
2661
Paul Stewart4d5efb72012-09-17 12:24:34 -07002662TEST_F(ManagerTest, IgnoredSearchList) {
2663 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
2664 SetResolver(resolver.get());
2665 vector<string> ignored_paths;
2666 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2667 SetIgnoredDNSSearchPaths("");
2668 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
2669
2670 const string kIgnored0 = "chromium.org";
2671 ignored_paths.push_back(kIgnored0);
2672 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2673 SetIgnoredDNSSearchPaths(kIgnored0);
2674 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
2675
2676 const string kIgnored1 = "google.com";
2677 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
2678 ignored_paths.push_back(kIgnored1);
2679 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2680 SetIgnoredDNSSearchPaths(kIgnoredSum);
2681 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
2682
2683 SetResolver(Resolver::GetInstance());
2684}
2685
Paul Stewartbfb82552012-10-24 16:48:48 -07002686TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
2687 // Test to make sure that every service state-change causes the
2688 // Manager to emit a new service list.
2689 scoped_refptr<MockService> mock_service(
2690 new NiceMock<MockService>(control_interface(),
2691 dispatcher(),
2692 metrics(),
2693 manager()));
2694 EXPECT_CALL(*mock_service, state())
2695 .WillRepeatedly(Return(Service::kStateIdle));
2696
2697 manager()->RegisterService(mock_service);
2698 EXPECT_CALL(
2699 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2700 flimflam::kServicesProperty, _)).Times(1);
2701 EXPECT_CALL(
2702 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2703 flimflam::kServiceWatchListProperty, _)).Times(1);
2704 CompleteServiceSort();
2705
2706 Mock::VerifyAndClearExpectations(manager_adaptor_);
2707 EXPECT_CALL(
2708 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2709 flimflam::kServicesProperty, _)).Times(1);
2710 EXPECT_CALL(
2711 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2712 flimflam::kServiceWatchListProperty, _)).Times(1);
2713 manager()->UpdateService(mock_service.get());
2714 CompleteServiceSort();
2715
2716 manager()->DeregisterService(mock_service);
2717}
2718
2719TEST_F(ManagerTest, EnumerateServices) {
2720 scoped_refptr<MockService> mock_service(
2721 new NiceMock<MockService>(control_interface(),
2722 dispatcher(),
2723 metrics(),
2724 manager()));
2725 manager()->RegisterService(mock_service);
2726
2727 EXPECT_CALL(*mock_service, state())
2728 .WillRepeatedly(Return(Service::kStateConnected));
2729 EXPECT_CALL(*mock_service, IsVisible())
2730 .WillRepeatedly(Return(false));
2731 EXPECT_TRUE(EnumerateAvailableServices().empty());
2732 EXPECT_TRUE(EnumerateWatchedServices().empty());
2733
2734 EXPECT_CALL(*mock_service, state())
2735 .WillRepeatedly(Return(Service::kStateIdle));
2736 EXPECT_TRUE(EnumerateAvailableServices().empty());
2737 EXPECT_TRUE(EnumerateWatchedServices().empty());
2738
2739 EXPECT_CALL(*mock_service, IsVisible())
2740 .WillRepeatedly(Return(true));
2741 Service::ConnectState unwatched_states[] = {
2742 Service::kStateUnknown,
2743 Service::kStateIdle,
2744 Service::kStateFailure
2745 };
2746 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
2747 EXPECT_CALL(*mock_service, state())
2748 .WillRepeatedly(Return(unwatched_states[i]));
2749 EXPECT_FALSE(EnumerateAvailableServices().empty());
2750 EXPECT_TRUE(EnumerateWatchedServices().empty());
2751 }
2752
2753 Service::ConnectState watched_states[] = {
2754 Service::kStateAssociating,
2755 Service::kStateConfiguring,
2756 Service::kStateConnected,
2757 Service::kStateDisconnected,
2758 Service::kStatePortal,
2759 Service::kStateOnline
2760 };
2761 for (size_t i = 0; i < arraysize(watched_states); ++i) {
2762 EXPECT_CALL(*mock_service, state())
2763 .WillRepeatedly(Return(watched_states[i]));
2764 EXPECT_FALSE(EnumerateAvailableServices().empty());
2765 EXPECT_FALSE(EnumerateWatchedServices().empty());
2766 }
2767
2768 manager()->DeregisterService(mock_service);
2769}
2770
Chris Masone9be4a9d2011-05-16 15:44:09 -07002771} // namespace shill