blob: 4c1f394d022b1c63c50aab62f3d826f196311d4b [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 Stewartc1dec4d2011-12-08 15:25:28 -0800114
115 // Replace the manager's adaptor with a quieter one, and one
116 // we can do EXPECT*() against. Passes ownership.
117 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700118 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700119 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700120
Paul Stewartfdd16072011-09-16 12:41:35 -0700121 bool IsDeviceRegistered(const DeviceRefPtr &device,
122 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700123 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700124 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700125 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700126 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700127 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700128
Paul Stewarta849a3d2011-11-03 05:54:09 -0700129 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
130 manager->profiles_.push_back(profile);
131 }
132
Paul Stewart75225512012-01-26 22:51:33 -0800133 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
134 return manager->ephemeral_profile_;
135 }
136
Chris Masone6515aab2011-10-12 16:19:09 -0700137 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
138 Profile::Identifier id("rather", "irrelevant");
139 scoped_ptr<Profile> profile(new Profile(control_interface(),
140 manager,
141 id,
142 "",
143 false));
144 FilePath final_path(storage_path());
145 final_path = final_path.Append("test.profile");
146 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
147 storage->set_path(final_path);
148 if (!storage->Open())
149 return NULL;
150 profile->set_storage(storage.release()); // Passes ownership.
151 return profile.release();
152 }
153
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700154 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
155 const string &profile_identifier,
156 const string &service_name) {
157 GLib glib;
158 KeyFileStore store(&glib);
159 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
160 return store.Open() &&
161 store.SetString(service_name, "rather", "irrelevant") &&
162 store.Close();
163 }
164
165 Error::Type TestCreateProfile(Manager *manager, const string &name) {
166 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800167 string path;
168 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700169 return error.type();
170 }
171
172 Error::Type TestPopAnyProfile(Manager *manager) {
173 Error error;
174 manager->PopAnyProfile(&error);
175 return error.type();
176 }
177
178 Error::Type TestPopProfile(Manager *manager, const string &name) {
179 Error error;
180 manager->PopProfile(name, &error);
181 return error.type();
182 }
183
184 Error::Type TestPushProfile(Manager *manager, const string &name) {
185 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800186 string path;
187 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700188 return error.type();
189 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000190
Paul Stewartcb3eb892012-06-07 14:24:46 -0700191 void AddMockProfileToManager(Manager *manager) {
192 scoped_refptr<MockProfile> profile(
193 new MockProfile(control_interface(), manager, ""));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200194 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/"));
195 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700196 AdoptProfile(manager, profile);
197 }
198
Paul Stewartdfa46052012-06-26 09:44:14 -0700199 void CompleteServiceSort() {
200 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
201 dispatcher()->DispatchPendingEvents();
202 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
203 }
204
Paul Stewart49739c02012-08-08 17:24:03 -0700205 RpcIdentifier GetDefaultServiceRpcIdentifier() {
206 return manager()->GetDefaultServiceRpcIdentifier(NULL);
207 }
208
Paul Stewart4d5efb72012-09-17 12:24:34 -0700209 void SetResolver(Resolver *resolver) {
210 manager()->resolver_ = resolver;
211 }
212
213 void SetIgnoredDNSSearchPaths(const string &search_paths) {
214 manager()->SetIgnoredDNSSearchPaths(search_paths, NULL);
215 }
216
217 const string &GetIgnoredDNSSearchPaths() {
218 return manager()->props_.ignored_dns_search_paths;
219 }
220
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700221 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000222 typedef scoped_refptr<MockService> MockServiceRefPtr;
223
Darin Petkova5e07ef2012-07-09 14:27:57 +0200224 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
225 public:
226 ServiceWatcher() {}
227 virtual ~ServiceWatcher() {}
228
229 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
230
231 private:
232 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
233 };
234
Darin Petkovca621542012-07-25 14:25:56 +0200235 class TestProxyFactory : public ProxyFactory {
236 public:
237 TestProxyFactory() {}
238
239 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
240 PowerManagerProxyDelegate */*delegate*/) {
241 return NULL;
242 }
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
246 };
247
Darin Petkov3ec55342012-09-28 14:04:44 +0200248 class TerminationActionTest :
249 public base::SupportsWeakPtr<TerminationActionTest> {
250 public:
251 static const char kActionName[];
252
253 TerminationActionTest() : manager_(NULL) {}
254 virtual ~TerminationActionTest() {}
255
256 MOCK_METHOD1(Done, void(const Error &error));
257
258 void Action() {
259 manager_->TerminationActionComplete("action");
260 }
261
262 void set_manager(Manager *manager) { manager_ = manager; }
263
264 private:
265 Manager *manager_;
266 DISALLOW_COPY_AND_ASSIGN(TerminationActionTest);
267 };
268
Darin Petkovca621542012-07-25 14:25:56 +0200269 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
270 power_manager_->power_state_ = state;
271 }
272
273 void SetPowerManager() {
274 manager()->set_power_manager(power_manager_.release());
275 }
276
Darin Petkov3ec55342012-09-28 14:04:44 +0200277 HookTable *GetTerminationActions() {
278 return &manager()->termination_actions_;
279 }
280
Darin Petkovca621542012-07-25 14:25:56 +0200281 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
282 manager()->OnPowerStateChanged(state);
283 }
284
Darin Petkov3ec55342012-09-28 14:04:44 +0200285 void OnSuspendDelay(uint32 sequence_number) {
286 manager()->OnSuspendDelay(sequence_number);
287 }
288
289 void OnSuspendActionsComplete(uint32 sequence_number, const Error &error) {
290 manager()->OnSuspendActionsComplete(sequence_number, error);
291 }
292
Paul Stewartbfb82552012-10-24 16:48:48 -0700293 vector<string> EnumerateAvailableServices() {
294 return manager()->EnumerateAvailableServices(NULL);
295 }
296
297 vector<string> EnumerateWatchedServices() {
298 return manager()->EnumerateWatchedServices(NULL);
299 }
300
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000301 MockServiceRefPtr MakeAutoConnectableService() {
302 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
303 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800304 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000305 manager());
306 service->MakeFavorite();
307 service->set_connectable(true);
308 return service;
309 }
310
Darin Petkovca621542012-07-25 14:25:56 +0200311 TestProxyFactory proxy_factory_;
312 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700313 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700314 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800315 scoped_ptr<MockDeviceInfo> device_info_;
316
317 // This pointer is owned by the manager, and only tracked here for EXPECT*()
318 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700319};
320
Darin Petkov3ec55342012-09-28 14:04:44 +0200321const char ManagerTest::TerminationActionTest::kActionName[] = "action";
322
Paul Stewart22aa71b2011-09-16 12:15:11 -0700323bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700324 if (!manager()->sort_services_task_.IsCancelled()) {
325 manager()->SortServicesTask();
326 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700327 return (svc0.get() == manager()->services_[0].get() &&
328 svc1.get() == manager()->services_[1].get());
329}
330
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700331TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700332 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
333 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700334}
335
Chris Masone9be4a9d2011-05-16 15:44:09 -0700336TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700337 ON_CALL(*mock_devices_[0].get(), technology())
338 .WillByDefault(Return(Technology::kEthernet));
339 ON_CALL(*mock_devices_[1].get(), technology())
340 .WillByDefault(Return(Technology::kWifi));
341 ON_CALL(*mock_devices_[2].get(), technology())
342 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700343
Paul Stewart22aa71b2011-09-16 12:15:11 -0700344 manager()->RegisterDevice(mock_devices_[0]);
345 manager()->RegisterDevice(mock_devices_[1]);
346 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700347
Paul Stewart22aa71b2011-09-16 12:15:11 -0700348 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
349 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
350 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700351}
352
Paul Stewarta41e38d2011-11-11 07:47:29 -0800353TEST_F(ManagerTest, DeviceRegistrationAndStart) {
354 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500355 mock_devices_[0]->enabled_persistent_ = true;
356 mock_devices_[1]->enabled_persistent_ = false;
357 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800358 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500359 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800360 .Times(0);
361 manager()->RegisterDevice(mock_devices_[0]);
362 manager()->RegisterDevice(mock_devices_[1]);
363}
364
365TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
366 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
367 DeviceRefPtr device_ref(mock_devices_[0].get());
368 AdoptProfile(manager(), profile); // Passes ownership.
369 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200370 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800371 manager()->RegisterDevice(mock_devices_[0]);
372}
373
Chris Masone9be4a9d2011-05-16 15:44:09 -0700374TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700375 ON_CALL(*mock_devices_[0].get(), technology())
376 .WillByDefault(Return(Technology::kEthernet));
377 ON_CALL(*mock_devices_[1].get(), technology())
378 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700379
Gaurav Shah435de2c2011-11-17 19:01:07 -0800380 manager()->RegisterDevice(mock_devices_[0]);
381 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700382
Paul Stewart22aa71b2011-09-16 12:15:11 -0700383 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
384 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700385
Paul Stewart212d60f2012-07-12 10:59:13 -0700386 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
387 AdoptProfile(manager(), profile); // Passes ownership.
388
Eric Shienbrood9a245532012-03-07 14:20:39 -0500389 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700390 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800391 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700392 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700393
Eric Shienbrood9a245532012-03-07 14:20:39 -0500394 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700395 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800396 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700397 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700398}
399
400TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700401 // It's much easier and safer to use a real GLib for this test.
402 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700403 Manager manager(control_interface(),
404 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800405 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700406 &glib,
407 run_path(),
408 storage_path(),
409 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700410 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
411 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700412 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700413
Chris Masone9be4a9d2011-05-16 15:44:09 -0700414 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700415 new NiceMock<MockService>(control_interface(),
416 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800417 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700418 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700419 scoped_refptr<MockService> mock_service2(
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));
Paul Stewartce4ec192012-03-14 12:53:46 -0700424
mukesh agrawal51a7e932011-07-27 16:18:26 -0700425 string service1_name(mock_service->UniqueName());
426 string service2_name(mock_service2->UniqueName());
427
428 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
429 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700430 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700431 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700432 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700433 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700434 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700435
Chris Masone9d779932011-08-25 16:33:41 -0700436 manager.RegisterService(mock_service);
437 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700438
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800439 Error error;
440 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700441 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700442 EXPECT_EQ(2, ids.size());
443 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
444 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700445
Chris Masone9d779932011-08-25 16:33:41 -0700446 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
447 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
448
449 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700450}
451
Chris Masone6515aab2011-10-12 16:19:09 -0700452TEST_F(ManagerTest, RegisterKnownService) {
453 // It's much easier and safer to use a real GLib for this test.
454 GLib glib;
455 Manager manager(control_interface(),
456 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800457 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700458 &glib,
459 run_path(),
460 storage_path(),
461 string());
462 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
463 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700464 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700465 {
466 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
467 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800468 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700469 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700470 ASSERT_TRUE(profile->AdoptService(service1));
471 ASSERT_TRUE(profile->ContainsService(service1));
472 } // Force destruction of service1.
473
474 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
475 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800476 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700477 &manager));
478 manager.RegisterService(service2);
479 EXPECT_EQ(service2->profile().get(), profile.get());
480 manager.Stop();
481}
482
483TEST_F(ManagerTest, RegisterUnknownService) {
484 // It's much easier and safer to use a real GLib for this test.
485 GLib glib;
486 Manager manager(control_interface(),
487 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800488 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700489 &glib,
490 run_path(),
491 storage_path(),
492 string());
493 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
494 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700495 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700496 {
497 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
498 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800499 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700500 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700501 ASSERT_TRUE(profile->AdoptService(service1));
502 ASSERT_TRUE(profile->ContainsService(service1));
503 } // Force destruction of service1.
504 scoped_refptr<MockService> mock_service2(
505 new NiceMock<MockService>(control_interface(),
506 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800507 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700508 &manager));
509 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
510 .WillRepeatedly(Return(mock_service2->UniqueName()));
511 manager.RegisterService(mock_service2);
512 EXPECT_NE(mock_service2->profile().get(), profile.get());
513 manager.Stop();
514}
515
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000516TEST_F(ManagerTest, DeregisterUnregisteredService) {
517 // WiFi assumes that it can deregister a service that is not
518 // registered. (E.g. a hidden service can be deregistered when it
519 // loses its last endpoint, and again when WiFi is Stop()-ed.)
520 //
521 // So test that doing so doesn't cause a crash.
522 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
523 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800524 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000525 manager());
526 manager()->DeregisterService(service);
527}
528
Chris Masonea8a2c252011-06-27 22:16:30 -0700529TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700530 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700531 map<string, ::DBus::Variant> props;
532 Error error(Error::kInvalidProperty, "");
533 {
534 ::DBus::Error dbus_error;
535 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700536 manager()->mutable_store()->SetStringProperty(
537 flimflam::kCheckPortalListProperty,
538 expected,
539 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700540 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700541 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
542 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
543 expected);
544 }
545 {
546 ::DBus::Error dbus_error;
547 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700548 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
549 expected,
550 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700551 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700552 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
553 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
554 expected);
555 }
556}
557
Chris Masone3c3f6a12011-07-01 10:01:41 -0700558TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700559 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800560 manager()->RegisterDevice(mock_devices_[0]);
561 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700562 {
563 map<string, ::DBus::Variant> props;
564 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700565 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700566 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700567 vector < ::DBus::Path> devices =
568 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700569 EXPECT_EQ(2, devices.size());
570 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700571}
572
mukesh agrawal2366eed2012-03-20 18:21:50 -0700573TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700574 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700575 map<string, ::DBus::Variant> props;
576 ::DBus::Error dbus_error;
577 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
578 map<string, ::DBus::Variant>::const_iterator prop =
579 props.find(flimflam::kServicesProperty);
580 ASSERT_FALSE(prop == props.end());
581 const ::DBus::Variant &variant = prop->second;
582 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
583}
584
Chris Masone6791a432011-07-12 13:23:19 -0700585TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700586 Manager manager(control_interface(),
587 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800588 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700589 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700590 run_path(),
591 storage_path(),
592 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700593 scoped_refptr<MockService> s2(new MockService(control_interface(),
594 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800595 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700596 &manager));
597 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700598 {
Chris Masone6515aab2011-10-12 16:19:09 -0700599 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700600 ProfileRefPtr profile(
601 new Profile(control_interface(), &manager, id, "", false));
602 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700603 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700604 .WillRepeatedly(Return(true));
605 EXPECT_CALL(*storage, Flush())
606 .Times(AnyNumber())
607 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700608 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700609 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700610 }
Chris Masone6515aab2011-10-12 16:19:09 -0700611 // Create a profile that already has |s2| in it.
612 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700613 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700614
Chris Masone6515aab2011-10-12 16:19:09 -0700615 // Now, move the Service |s2| to another profile.
616 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
617 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700618
619 // Force destruction of the original Profile, to ensure that the Service
620 // is kept alive and populated with data.
621 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700622 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700623 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700624}
625
Paul Stewart7f61e522012-03-22 11:13:45 -0700626TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
627 scoped_refptr<MockProfile> mock_profile(
628 new MockProfile(control_interface(), manager(), ""));
629 const string kProfileName("profile0");
630 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
631 .WillRepeatedly(Return(kProfileName));
632 AdoptProfile(manager(), mock_profile);
633
634 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
635 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
636 EXPECT_EQ(mock_profile.get(), profile.get());
637}
638
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800639TEST_F(ManagerTest, SetProfileForService) {
640 scoped_refptr<MockProfile> profile0(
641 new MockProfile(control_interface(), manager(), ""));
642 string profile_name0("profile0");
643 EXPECT_CALL(*profile0, GetRpcIdentifier())
644 .WillRepeatedly(Return(profile_name0));
645 AdoptProfile(manager(), profile0);
646 scoped_refptr<MockService> service(new MockService(control_interface(),
647 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800648 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800649 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700650 EXPECT_FALSE(manager()->HasService(service));
651 {
652 Error error;
653 EXPECT_CALL(*profile0, AdoptService(_))
654 .WillOnce(Return(true));
655 // Expect that setting the profile of a service that does not already
656 // have one assigned does not cause a crash.
657 manager()->SetProfileForService(service, "profile0", &error);
658 EXPECT_TRUE(error.IsSuccess());
659 }
660
661 // The service should be registered as a side-effect of the profile being
662 // set for this service.
663 EXPECT_TRUE(manager()->HasService(service));
664
665 // Since we have mocked Profile::AdoptServie() above, the service's
666 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800667 service->set_profile(profile0);
668
669 {
670 Error error;
671 manager()->SetProfileForService(service, "foo", &error);
672 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700673 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800674 }
675
676 {
677 Error error;
678 manager()->SetProfileForService(service, profile_name0, &error);
679 EXPECT_EQ(Error::kInvalidArguments, error.type());
680 EXPECT_EQ("Service is already connected to this profile", error.message());
681 }
682
683 scoped_refptr<MockProfile> profile1(
684 new MockProfile(control_interface(), manager(), ""));
685 string profile_name1("profile1");
686 EXPECT_CALL(*profile1, GetRpcIdentifier())
687 .WillRepeatedly(Return(profile_name1));
688 AdoptProfile(manager(), profile1);
689
690 {
691 Error error;
692 EXPECT_CALL(*profile1, AdoptService(_))
693 .WillOnce(Return(true));
694 EXPECT_CALL(*profile0, AbandonService(_))
695 .WillOnce(Return(true));
696 manager()->SetProfileForService(service, profile_name1, &error);
697 EXPECT_TRUE(error.IsSuccess());
698 }
699}
700
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700701TEST_F(ManagerTest, CreateProfile) {
702 // It's much easier to use real Glib here since we want the storage
703 // side-effects.
704 GLib glib;
705 ScopedTempDir temp_dir;
706 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
707
708 Manager manager(control_interface(),
709 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800710 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700711 &glib,
712 run_path(),
713 storage_path(),
714 temp_dir.path().value());
715
716 // Invalid name should be rejected.
717 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
718
Paul Stewartd0a3b812012-03-28 22:48:22 -0700719 // A profile with invalid characters in it should similarly be rejected.
720 EXPECT_EQ(Error::kInvalidArguments,
721 TestCreateProfile(&manager, "valid_profile"));
722
723 // We should be able to create a machine profile.
724 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700725
Gary Morainb672d352012-04-25 09:19:06 -0700726 // We should succeed in creating a valid user profile. Verify the returned
727 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700728 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700729 {
730 Error error;
731 string path;
732 manager.CreateProfile(kProfile, &path, &error);
733 EXPECT_EQ(Error::kSuccess, error.type());
734 EXPECT_EQ("/profile_rpc", path);
735 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700736
737 // We should fail in creating it a second time (already exists).
738 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
739}
740
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700741// We receive PopProfile when a user logs out, and it should always trigger a
742// MemoryLog Clear() call.
743TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
744 GLib glib;
745 ScopedTempDir temp_dir;
746 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
747 Manager manager(control_interface(),
748 dispatcher(),
749 metrics(),
750 &glib,
751 run_path(),
752 storage_path(),
753 temp_dir.path().value());
754 const char kProfile0[] = "~user/profile0";
755 const char kPurgedMessage[] = "This message should be purged";
756 // Create a profile and push it on the stack, leave one uncreated
757 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
758 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
759
760 // Popping a profile which isn't on top should still clear the log.
761 LOG(INFO) << kPurgedMessage;
762 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
763 kPurgedMessage));
764 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
765 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
766 kPurgedMessage));
767
768 // Popping an invalid profile name should do the same thing.
769 LOG(INFO) << kPurgedMessage;
770 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
771 kPurgedMessage));
772 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
773 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
774 kPurgedMessage));
775
776 // Successful pops also purge the message log.
777 LOG(INFO) << kPurgedMessage;
778 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
779 kPurgedMessage));
780 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
781 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
782 kPurgedMessage));
783}
784
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700785TEST_F(ManagerTest, PushPopProfile) {
786 // It's much easier to use real Glib in creating a Manager for this
787 // test here since we want the storage side-effects.
788 GLib glib;
789 ScopedTempDir temp_dir;
790 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
791 Manager manager(control_interface(),
792 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800793 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700794 &glib,
795 run_path(),
796 storage_path(),
797 temp_dir.path().value());
798
799 // Pushing an invalid profile should fail.
800 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
801
Paul Stewartd0a3b812012-03-28 22:48:22 -0700802 // Pushing a default profile that does not exist should fail.
803 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700804
805 const char kProfile0[] = "~user/profile0";
806 const char kProfile1[] = "~user/profile1";
807
808 // Create a couple of profiles.
809 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
810 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
811
812 // Push these profiles on the stack.
813 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
814 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
815
816 // Pushing a profile a second time should fail.
817 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
818 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
819
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800820 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700821 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800822 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700823
824 // Make sure a profile name that doesn't exist fails.
825 const char kProfile2Id[] = "profile2";
826 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
827 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
828
829 // Create a new service, with a specific storage name.
830 scoped_refptr<MockService> service(
831 new NiceMock<MockService>(control_interface(),
832 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800833 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700834 &manager));
835 const char kServiceName[] = "service_storage_name";
836 EXPECT_CALL(*service.get(), GetStorageIdentifier())
837 .WillRepeatedly(Return(kServiceName));
838 EXPECT_CALL(*service.get(), Load(_))
839 .WillRepeatedly(Return(true));
840
841 // Add this service to the manager -- it should end up in the ephemeral
842 // profile.
843 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800844 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700845
846 // Create storage for a profile that contains the service storage name.
847 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
848 kServiceName));
849
850 // When we push the profile, the service should move away from the
851 // ephemeral profile to this new profile since it has an entry for
852 // this service.
853 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800854 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700855 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
856
857 // Insert another profile that should supersede ownership of the service.
858 const char kProfile3Id[] = "profile3";
859 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
860 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
861 kServiceName));
862 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
863 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
864
865 // Popping an invalid profile name should fail.
866 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
867
868 // Popping an profile that is not at the top of the stack should fail.
869 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
870
871 // Popping the top profile should succeed.
872 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
873
874 // Moreover the service should have switched profiles to profile 2.
875 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
876
877 // Popping the top profile should succeed.
878 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
879
880 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800881 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700882
883 // Pop the remaining two services off the stack.
884 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
885 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
886
887 // Next pop should fail with "stack is empty".
888 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700889
890 const char kMachineProfile0[] = "machineprofile0";
891 const char kMachineProfile1[] = "machineprofile1";
892 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
893 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
894
895 // Should be able to push a machine profile.
896 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
897
898 // Should be able to push a user profile atop a machine profile.
899 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
900
901 // Pushing a system-wide profile on top of a user profile should fail.
902 EXPECT_EQ(Error::kInvalidArguments,
903 TestPushProfile(&manager, kMachineProfile1));
904
905 // However if we pop the user profile, we should be able stack another
906 // machine profile on.
907 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
908 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700909}
910
Paul Stewarte73d05c2012-03-29 16:26:05 -0700911TEST_F(ManagerTest, RemoveProfile) {
912 // It's much easier to use real Glib in creating a Manager for this
913 // test here since we want the storage side-effects.
914 GLib glib;
915 ScopedTempDir temp_dir;
916 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
917 Manager manager(control_interface(),
918 dispatcher(),
919 metrics(),
920 &glib,
921 run_path(),
922 storage_path(),
923 temp_dir.path().value());
924
925 const char kProfile0[] = "profile0";
926 FilePath profile_path(
927 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
928
929 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
930 ASSERT_TRUE(file_util::PathExists(profile_path));
931
932 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
933
934 // Remove should fail since the profile is still on the stack.
935 {
936 Error error;
937 manager.RemoveProfile(kProfile0, &error);
938 EXPECT_EQ(Error::kInvalidArguments, error.type());
939 }
940
941 // Profile path should still exist.
942 EXPECT_TRUE(file_util::PathExists(profile_path));
943
944 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
945
946 // This should succeed now that the profile is off the stack.
947 {
948 Error error;
949 manager.RemoveProfile(kProfile0, &error);
950 EXPECT_EQ(Error::kSuccess, error.type());
951 }
952
953 // Profile path should no longer exist.
954 EXPECT_FALSE(file_util::PathExists(profile_path));
955
956 // Another remove succeeds, due to a foible in file_util::Delete --
957 // it is not an error to delete a file that does not exist.
958 {
959 Error error;
960 manager.RemoveProfile(kProfile0, &error);
961 EXPECT_EQ(Error::kSuccess, error.type());
962 }
963
964 // Let's create an error case that will "work". Create a non-empty
965 // directory in the place of the profile pathname.
966 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
967 {
968 Error error;
969 manager.RemoveProfile(kProfile0, &error);
970 EXPECT_EQ(Error::kOperationFailed, error.type());
971 }
972}
973
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700974TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
975 // It's much easier to use real Glib in creating a Manager for this
976 // test here since we want the storage side-effects.
977 GLib glib;
978 ScopedTempDir temp_dir;
979 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
980 Manager manager(control_interface(),
981 dispatcher(),
982 metrics(),
983 &glib,
984 run_path(),
985 storage_path(),
986 temp_dir.path().value());
987
988 const char kProfile0[] = "profile0";
989 FilePath profile_path(
990 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
991
992 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
993 ASSERT_TRUE(file_util::PathExists(profile_path));
994 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
995
996 // Ensure that even if the backing filestore is removed, we still can't
997 // create a profile twice.
998 ASSERT_TRUE(file_util::Delete(profile_path, false));
999 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
1000}
1001
Paul Stewart75225512012-01-26 22:51:33 -08001002// Use this matcher instead of passing RefPtrs directly into the arguments
1003// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
1004// system teardown.
1005MATCHER_P(IsRefPtrTo, ref_address, "") {
1006 return arg.get() == ref_address;
1007}
1008
1009TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1010 MockServiceRefPtr s_not_in_profile(
1011 new NiceMock<MockService>(control_interface(),
1012 dispatcher(),
1013 metrics(),
1014 manager()));
1015 MockServiceRefPtr s_not_in_group(
1016 new NiceMock<MockService>(control_interface(),
1017 dispatcher(),
1018 metrics(),
1019 manager()));
1020 MockServiceRefPtr s_configure_fail(
1021 new NiceMock<MockService>(control_interface(),
1022 dispatcher(),
1023 metrics(),
1024 manager()));
1025 MockServiceRefPtr s_configure_succeed(
1026 new NiceMock<MockService>(control_interface(),
1027 dispatcher(),
1028 metrics(),
1029 manager()));
1030
1031 string entry_name("entry_name");
1032 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1033 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1034 .WillRepeatedly(Return("not_entry_name"));
1035 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1036 .WillRepeatedly(Return(entry_name));
1037 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1038 .WillRepeatedly(Return(entry_name));
1039
1040 manager()->RegisterService(s_not_in_profile);
1041 manager()->RegisterService(s_not_in_group);
1042 manager()->RegisterService(s_configure_fail);
1043 manager()->RegisterService(s_configure_succeed);
1044
1045 scoped_refptr<MockProfile> profile0(
1046 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1047 scoped_refptr<MockProfile> profile1(
1048 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1049
1050 s_not_in_group->set_profile(profile1);
1051 s_configure_fail->set_profile(profile1);
1052 s_configure_succeed->set_profile(profile1);
1053
1054 AdoptProfile(manager(), profile0);
1055 AdoptProfile(manager(), profile1);
1056
1057 // No services are a member of this profile.
1058 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1059
1060 // No services that are members of this profile have this entry name.
1061 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1062
1063 // Only services that are members of the profile and group will be abandoned.
1064 EXPECT_CALL(*profile1.get(),
1065 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1066 EXPECT_CALL(*profile1.get(),
1067 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1068 EXPECT_CALL(*profile1.get(),
1069 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1070 .WillOnce(Return(true));
1071 EXPECT_CALL(*profile1.get(),
1072 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1073 .WillOnce(Return(true));
1074
1075 // Never allow services to re-join profile1.
1076 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1077 .WillRepeatedly(Return(false));
1078
1079 // Only allow one of the members of the profile and group to successfully
1080 // join profile0.
1081 EXPECT_CALL(*profile0.get(),
1082 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1083 EXPECT_CALL(*profile0.get(),
1084 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1085 EXPECT_CALL(*profile0.get(),
1086 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1087 .WillOnce(Return(false));
1088 EXPECT_CALL(*profile0.get(),
1089 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1090 .WillOnce(Return(true));
1091
1092 // Expect the failed-to-configure service to have Unload() called on it.
1093 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1094 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1095 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1096 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1097
1098 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1099
1100 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1101 EXPECT_EQ(profile1, s_not_in_group->profile());
1102 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1103
1104 // Since we are using a MockProfile, the profile does not actually change,
1105 // since ConfigureService was not actually called on the service.
1106 EXPECT_EQ(profile1, s_configure_succeed->profile());
1107}
1108
Paul Stewart65512e12012-03-26 18:01:08 -07001109TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1110 MockServiceRefPtr s_will_remove0(
1111 new NiceMock<MockService>(control_interface(),
1112 dispatcher(),
1113 metrics(),
1114 manager()));
1115 MockServiceRefPtr s_will_remove1(
1116 new NiceMock<MockService>(control_interface(),
1117 dispatcher(),
1118 metrics(),
1119 manager()));
1120 MockServiceRefPtr s_will_not_remove0(
1121 new NiceMock<MockService>(control_interface(),
1122 dispatcher(),
1123 metrics(),
1124 manager()));
1125 MockServiceRefPtr s_will_not_remove1(
1126 new NiceMock<MockService>(control_interface(),
1127 dispatcher(),
1128 metrics(),
1129 manager()));
1130
1131 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1132 .Times(4); // Once for each registration.
1133
1134 string entry_name("entry_name");
1135 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1136 .WillRepeatedly(Return(entry_name));
1137 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1138 .WillRepeatedly(Return(entry_name));
1139 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1140 .WillRepeatedly(Return(entry_name));
1141 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1142 .WillRepeatedly(Return(entry_name));
1143
1144 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001145 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001146 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001147 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001148 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001149 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001150 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001151 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001152
1153 // One for each service added above.
1154 ASSERT_EQ(4, manager()->services_.size());
1155
1156 scoped_refptr<MockProfile> profile(
1157 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1158
1159 s_will_remove0->set_profile(profile);
1160 s_will_remove1->set_profile(profile);
1161 s_will_not_remove0->set_profile(profile);
1162 s_will_not_remove1->set_profile(profile);
1163
1164 AdoptProfile(manager(), profile);
1165
1166 // Deny any of the services re-entry to the profile.
1167 EXPECT_CALL(*profile, ConfigureService(_))
1168 .WillRepeatedly(Return(false));
1169
1170 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1171 .WillOnce(Return(true));
1172 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1173 .WillOnce(Return(true));
1174 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1175 .WillOnce(Return(true));
1176 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1177 .WillOnce(Return(true));
1178
1179 EXPECT_CALL(*s_will_remove0, Unload())
1180 .WillOnce(Return(true));
1181 EXPECT_CALL(*s_will_remove1, Unload())
1182 .WillOnce(Return(true));
1183 EXPECT_CALL(*s_will_not_remove0, Unload())
1184 .WillOnce(Return(false));
1185 EXPECT_CALL(*s_will_not_remove1, Unload())
1186 .WillOnce(Return(false));
1187
1188
1189 // This will cause all the profiles to be unloaded.
1190 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1191
1192 // 2 of the 4 services added above should have been unregistered and
1193 // removed, leaving 2.
1194 EXPECT_EQ(2, manager()->services_.size());
1195 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1196 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1197}
1198
1199TEST_F(ManagerTest, PopProfileWithUnload) {
1200 MockServiceRefPtr s_will_remove0(
1201 new NiceMock<MockService>(control_interface(),
1202 dispatcher(),
1203 metrics(),
1204 manager()));
1205 MockServiceRefPtr s_will_remove1(
1206 new NiceMock<MockService>(control_interface(),
1207 dispatcher(),
1208 metrics(),
1209 manager()));
1210 MockServiceRefPtr s_will_not_remove0(
1211 new NiceMock<MockService>(control_interface(),
1212 dispatcher(),
1213 metrics(),
1214 manager()));
1215 MockServiceRefPtr s_will_not_remove1(
1216 new NiceMock<MockService>(control_interface(),
1217 dispatcher(),
1218 metrics(),
1219 manager()));
1220
1221 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1222 .Times(5); // Once for each registration, and one after profile pop.
1223
1224 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001225 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001226 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001227 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001228 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001229 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001230 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001231 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001232
1233 // One for each service added above.
1234 ASSERT_EQ(4, manager()->services_.size());
1235
1236 scoped_refptr<MockProfile> profile0(
1237 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1238 scoped_refptr<MockProfile> profile1(
1239 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1240
1241 s_will_remove0->set_profile(profile1);
1242 s_will_remove1->set_profile(profile1);
1243 s_will_not_remove0->set_profile(profile1);
1244 s_will_not_remove1->set_profile(profile1);
1245
1246 AdoptProfile(manager(), profile0);
1247 AdoptProfile(manager(), profile1);
1248
1249 // Deny any of the services entry to profile0, so they will all be unloaded.
1250 EXPECT_CALL(*profile0, ConfigureService(_))
1251 .WillRepeatedly(Return(false));
1252
1253 EXPECT_CALL(*s_will_remove0, Unload())
1254 .WillOnce(Return(true));
1255 EXPECT_CALL(*s_will_remove1, Unload())
1256 .WillOnce(Return(true));
1257 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001258 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001259 EXPECT_CALL(*s_will_not_remove1, Unload())
1260 .WillOnce(Return(false));
1261
1262 // This will pop profile1, which should cause all our profiles to unload.
1263 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001264 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001265
1266 // 2 of the 4 services added above should have been unregistered and
1267 // removed, leaving 2.
1268 EXPECT_EQ(2, manager()->services_.size());
1269 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1270 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001271
1272 // Expect the unloaded services to lose their profile reference.
1273 EXPECT_FALSE(s_will_remove0->profile());
1274 EXPECT_FALSE(s_will_remove1->profile());
1275
1276 // If we explicitly deregister a service, the effect should be the same
1277 // with respect to the profile reference.
1278 ASSERT_TRUE(s_will_not_remove0->profile());
1279 manager()->DeregisterService(s_will_not_remove0);
1280 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001281}
1282
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001283TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001284 {
1285 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001286 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1287 flimflam::kOfflineModeProperty,
1288 PropertyStoreTest::kBoolV,
1289 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001290 }
1291 {
1292 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001293 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1294 flimflam::kCountryProperty,
1295 PropertyStoreTest::kStringV,
1296 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001297 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001298 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001299 {
1300 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001301 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1302 flimflam::kCountryProperty,
1303 PropertyStoreTest::kBoolV,
1304 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001305 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001306 }
1307 {
1308 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001309 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1310 flimflam::kOfflineModeProperty,
1311 PropertyStoreTest::kStringV,
1312 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001313 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001314 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001315 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001316 {
1317 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001318 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001319 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001320 flimflam::kEnabledTechnologiesProperty,
1321 PropertyStoreTest::kStringsV,
1322 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001323 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001324 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001325}
1326
mukesh agrawal32399322011-09-01 10:53:43 -07001327TEST_F(ManagerTest, RequestScan) {
1328 {
1329 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001330 manager()->RegisterDevice(mock_devices_[0].get());
1331 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001332 EXPECT_CALL(*mock_devices_[0], technology())
1333 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001334 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001335 EXPECT_CALL(*mock_devices_[1], technology())
1336 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001337 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001338 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001339 }
1340
1341 {
1342 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001343 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001344 EXPECT_EQ(Error::kInvalidArguments, error.type());
1345 }
1346}
1347
Darin Petkovb65c2452012-02-23 15:17:06 +01001348TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001349 KeyValueStore args;
1350 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001351 manager()->GetService(args, &e);
1352 EXPECT_EQ(Error::kInvalidArguments, e.type());
1353 EXPECT_EQ("must specify service type", e.message());
1354}
1355
1356TEST_F(ManagerTest, GetServiceUnknownType) {
1357 KeyValueStore args;
1358 Error e;
1359 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1360 manager()->GetService(args, &e);
1361 EXPECT_EQ(Error::kNotSupported, e.type());
1362 EXPECT_EQ("service type is unsupported", e.message());
1363}
1364
1365TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1366 KeyValueStore args;
1367 Error e;
1368 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1369 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001370 EXPECT_EQ(Error::kInvalidArguments, e.type());
1371 EXPECT_EQ("no wifi devices available", e.message());
1372}
1373
Darin Petkovb65c2452012-02-23 15:17:06 +01001374TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001375 KeyValueStore args;
1376 Error e;
1377 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001378 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001379 manager()->RegisterDevice(mock_wifi_);
1380 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1381 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001382 manager()->GetService(args, &e);
1383 EXPECT_TRUE(e.IsSuccess());
1384}
1385
Darin Petkov33af05c2012-02-28 10:10:30 +01001386TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1387 KeyValueStore args;
1388 Error e;
1389 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001390 scoped_refptr<MockProfile> profile(
1391 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1392 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001393 ServiceRefPtr service = manager()->GetService(args, &e);
1394 EXPECT_EQ(Error::kNotSupported, e.type());
1395 EXPECT_FALSE(service);
1396}
1397
Darin Petkovb65c2452012-02-23 15:17:06 +01001398TEST_F(ManagerTest, GetServiceVPN) {
1399 KeyValueStore args;
1400 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001401 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001402 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001403 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1404 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001405 scoped_refptr<MockProfile> profile(
1406 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1407 AdoptProfile(manager(), profile);
1408 ServiceRefPtr updated_service;
1409 EXPECT_CALL(*profile, UpdateService(_))
1410 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1411 ServiceRefPtr configured_service;
1412 EXPECT_CALL(*profile, ConfigureService(_))
1413 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001414 ServiceRefPtr service = manager()->GetService(args, &e);
1415 EXPECT_TRUE(e.IsSuccess());
1416 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001417 EXPECT_EQ(service, updated_service);
1418 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001419}
1420
Darin Petkovc63dcf02012-05-24 11:51:43 +02001421TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1422 KeyValueStore args;
1423 Error e;
1424 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1425 ServiceRefPtr service = manager()->GetService(args, &e);
1426 EXPECT_EQ(Error::kInvalidArguments, e.type());
1427 EXPECT_EQ("Missing WiMAX network id.", e.message());
1428 EXPECT_FALSE(service);
1429}
1430
Darin Petkovd1cd7972012-05-22 15:26:15 +02001431TEST_F(ManagerTest, GetServiceWiMax) {
1432 KeyValueStore args;
1433 Error e;
1434 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001435 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1436 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1437 ServiceRefPtr service = manager()->GetService(args, &e);
1438 EXPECT_TRUE(e.IsSuccess());
1439 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001440}
1441
Paul Stewart7f61e522012-03-22 11:13:45 -07001442TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1443 // Manager calls ActiveProfile() so we need at least one profile installed.
1444 scoped_refptr<MockProfile> profile(
1445 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1446 AdoptProfile(manager(), profile);
1447
1448 KeyValueStore args;
1449 args.SetString(flimflam::kProfileProperty, "xxx");
1450 Error error;
1451 manager()->ConfigureService(args, &error);
1452 EXPECT_EQ(Error::kInvalidArguments, error.type());
1453 EXPECT_EQ("Invalid profile name xxx", error.message());
1454}
1455
1456TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1457 // Manager calls ActiveProfile() so we need at least one profile installed.
1458 scoped_refptr<MockProfile> profile(
1459 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1460 AdoptProfile(manager(), profile);
1461
1462 KeyValueStore args;
1463 Error error;
1464 manager()->ConfigureService(args, &error);
1465 EXPECT_EQ(Error::kInvalidArguments, error.type());
1466 EXPECT_EQ("must specify service type", error.message());
1467}
1468
1469// A registered service in the ephemeral profile should be moved to the
1470// active profile as a part of configuration if no profile was explicitly
1471// specified.
1472TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1473 scoped_refptr<MockProfile> profile(
1474 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1475
1476 AdoptProfile(manager(), profile); // This is now the active profile.
1477
1478 const std::vector<uint8_t> ssid;
1479 scoped_refptr<MockWiFiService> service(
1480 new NiceMock<MockWiFiService>(control_interface(),
1481 dispatcher(),
1482 metrics(),
1483 manager(),
1484 mock_wifi_,
1485 ssid,
1486 "",
1487 "",
1488 false));
1489
1490 manager()->RegisterService(service);
1491 service->set_profile(GetEphemeralProfile(manager()));
1492
1493 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1494 // the same device as that used above causes a refcounting loop.
1495 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1496 dispatcher(),
1497 metrics(),
1498 manager(),
1499 "wifi1",
1500 "addr5",
1501 5));
1502 manager()->RegisterDevice(wifi);
1503 EXPECT_CALL(*wifi, GetService(_, _))
1504 .WillOnce(Return(service));
1505 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1506 .WillOnce(Return(true));
1507 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1508 .WillOnce(Return(true));
1509
1510 KeyValueStore args;
1511 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1512 Error error;
1513 manager()->ConfigureService(args, &error);
1514 EXPECT_TRUE(error.IsSuccess());
1515}
1516
1517// If were configure a service that was already registered and explicitly
1518// specify a profile, it should be moved from the profile it was previously
1519// in to the specified profile if one was requested.
1520TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1521 scoped_refptr<MockProfile> profile0(
1522 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1523 scoped_refptr<MockProfile> profile1(
1524 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1525
1526 const string kProfileName0 = "profile0";
1527 const string kProfileName1 = "profile1";
1528
1529 EXPECT_CALL(*profile0, GetRpcIdentifier())
1530 .WillRepeatedly(Return(kProfileName0));
1531 EXPECT_CALL(*profile1, GetRpcIdentifier())
1532 .WillRepeatedly(Return(kProfileName1));
1533
1534 AdoptProfile(manager(), profile0);
1535 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1536
1537 const std::vector<uint8_t> ssid;
1538 scoped_refptr<MockWiFiService> service(
1539 new NiceMock<MockWiFiService>(control_interface(),
1540 dispatcher(),
1541 metrics(),
1542 manager(),
1543 mock_wifi_,
1544 ssid,
1545 "",
1546 "",
1547 false));
1548
1549 manager()->RegisterService(service);
1550 service->set_profile(profile1);
1551
1552 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1553 // the same device as that used above causes a refcounting loop.
1554 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1555 dispatcher(),
1556 metrics(),
1557 manager(),
1558 "wifi1",
1559 "addr5",
1560 5));
1561 manager()->RegisterDevice(wifi);
1562 EXPECT_CALL(*wifi, GetService(_, _))
1563 .WillOnce(Return(service));
1564 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1565 .WillOnce(Return(true));
1566 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1567 .WillOnce(Return(true));
1568 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1569 .WillOnce(Return(true));
1570
1571 KeyValueStore args;
1572 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1573 args.SetString(flimflam::kProfileProperty, kProfileName0);
1574 Error error;
1575 manager()->ConfigureService(args, &error);
1576 EXPECT_TRUE(error.IsSuccess());
1577 service->set_profile(NULL); // Breaks refcounting loop.
1578}
1579
1580// An unregistered service should remain unregistered, but its contents should
1581// be saved to the specified profile nonetheless.
1582TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1583 scoped_refptr<MockProfile> profile0(
1584 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1585 scoped_refptr<MockProfile> profile1(
1586 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1587
1588 const string kProfileName0 = "profile0";
1589 const string kProfileName1 = "profile1";
1590
1591 EXPECT_CALL(*profile0, GetRpcIdentifier())
1592 .WillRepeatedly(Return(kProfileName0));
1593 EXPECT_CALL(*profile1, GetRpcIdentifier())
1594 .WillRepeatedly(Return(kProfileName1));
1595
1596 AdoptProfile(manager(), profile0);
1597 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1598
1599 const std::vector<uint8_t> ssid;
1600 scoped_refptr<MockWiFiService> service(
1601 new NiceMock<MockWiFiService>(control_interface(),
1602 dispatcher(),
1603 metrics(),
1604 manager(),
1605 mock_wifi_,
1606 ssid,
1607 "",
1608 "",
1609 false));
1610
1611 service->set_profile(profile1);
1612
1613 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1614 // the same device as that used above causes a refcounting loop.
1615 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1616 dispatcher(),
1617 metrics(),
1618 manager(),
1619 "wifi1",
1620 "addr5",
1621 5));
1622 manager()->RegisterDevice(wifi);
1623 EXPECT_CALL(*wifi, GetService(_, _))
1624 .WillOnce(Return(service));
1625 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1626 .WillOnce(Return(true));
1627 EXPECT_CALL(*profile0, AdoptService(_))
1628 .Times(0);
1629 EXPECT_CALL(*profile1, AdoptService(_))
1630 .Times(0);
1631
1632 KeyValueStore args;
1633 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1634 args.SetString(flimflam::kProfileProperty, kProfileName0);
1635 Error error;
1636 manager()->ConfigureService(args, &error);
1637 EXPECT_TRUE(error.IsSuccess());
1638}
1639
Paul Stewart22aa71b2011-09-16 12:15:11 -07001640TEST_F(ManagerTest, TechnologyOrder) {
1641 Error error;
1642 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1643 string(flimflam::kTypeWifi), &error);
1644 ASSERT_TRUE(error.IsSuccess());
1645 EXPECT_EQ(manager()->GetTechnologyOrder(),
1646 string(flimflam::kTypeEthernet) + "," +
1647 string(flimflam::kTypeWifi));
1648
1649 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1650 string(flimflam::kTypeWifi), &error);
1651 ASSERT_FALSE(error.IsSuccess());
1652 EXPECT_EQ(Error::kInvalidArguments, error.type());
1653 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1654 string(flimflam::kTypeWifi),
1655 manager()->GetTechnologyOrder());
1656}
1657
1658TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001659 // TODO(quiche): Some of these tests would probably fit better in
1660 // service_unittest, since the actual comparison of Services is
1661 // implemented in Service. (crosbug.com/23370)
1662
Paul Stewart22aa71b2011-09-16 12:15:11 -07001663 scoped_refptr<MockService> mock_service0(
1664 new NiceMock<MockService>(control_interface(),
1665 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001666 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001667 manager()));
1668 scoped_refptr<MockService> mock_service1(
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()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001673
1674 manager()->RegisterService(mock_service0);
1675 manager()->RegisterService(mock_service1);
1676
1677 // Services should already be sorted by UniqueName
1678 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1679
1680 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001681 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001682 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1683
1684 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001685 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001686 manager()->UpdateService(mock_service1);
1687 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1688
1689 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001690 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001691 manager()->UpdateService(mock_service0);
1692 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1693
1694 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001695 EXPECT_CALL(*mock_service0.get(), technology())
1696 .WillRepeatedly(Return((Technology::kWifi)));
1697 EXPECT_CALL(*mock_service1.get(), technology())
1698 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001699
1700 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001701 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001702 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001703 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1704
1705 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1706 string(flimflam::kTypeEthernet), &error);
1707 EXPECT_TRUE(error.IsSuccess());
1708 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1709
Gaurav Shah435de2c2011-11-17 19:01:07 -08001710 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001711 mock_service0->set_priority(1);
1712 manager()->UpdateService(mock_service0);
1713 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1714
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001715 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001716 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001717 manager()->UpdateService(mock_service1);
1718 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1719
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001720 // Auto-connect.
1721 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001722 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001723 mock_service1->set_auto_connect(false);
1724 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001725 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1726
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001727 // Connectable.
1728 mock_service1->set_connectable(true);
1729 manager()->UpdateService(mock_service1);
1730 mock_service0->set_connectable(false);
1731 manager()->UpdateService(mock_service0);
1732 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1733
1734 // IsFailed.
1735 EXPECT_CALL(*mock_service0.get(), state())
1736 .WillRepeatedly(Return(Service::kStateIdle));
1737 EXPECT_CALL(*mock_service0.get(), IsFailed())
1738 .WillRepeatedly(Return(false));
1739 manager()->UpdateService(mock_service0);
1740 EXPECT_CALL(*mock_service0.get(), state())
1741 .WillRepeatedly(Return(Service::kStateFailure));
1742 EXPECT_CALL(*mock_service1.get(), IsFailed())
1743 .WillRepeatedly(Return(true));
1744 manager()->UpdateService(mock_service1);
1745 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1746
1747 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001748 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001749 .WillRepeatedly(Return(Service::kStateAssociating));
1750 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001751 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001752 manager()->UpdateService(mock_service1);
1753 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1754
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001755 // Connected.
1756 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001757 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001758 EXPECT_CALL(*mock_service0.get(), IsConnected())
1759 .WillRepeatedly(Return(true));
1760 manager()->UpdateService(mock_service0);
1761 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1762
Paul Stewarta121c442012-06-09 14:12:58 -07001763 // Portal.
1764 EXPECT_CALL(*mock_service1.get(), state())
1765 .WillRepeatedly(Return(Service::kStateConnected));
1766 EXPECT_CALL(*mock_service1.get(), IsConnected())
1767 .WillRepeatedly(Return(true));
1768 manager()->UpdateService(mock_service1);
1769 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1770
Paul Stewart22aa71b2011-09-16 12:15:11 -07001771 manager()->DeregisterService(mock_service0);
1772 manager()->DeregisterService(mock_service1);
1773}
1774
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001775TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001776 MockMetrics mock_metrics;
1777 manager()->set_metrics(&mock_metrics);
1778
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001779 scoped_refptr<MockService> mock_service0(
1780 new NiceMock<MockService>(control_interface(),
1781 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001782 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001783 manager()));
1784 scoped_refptr<MockService> mock_service1(
1785 new NiceMock<MockService>(control_interface(),
1786 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001787 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001788 manager()));
1789
1790 scoped_refptr<MockConnection> mock_connection0(
1791 new NiceMock<MockConnection>(device_info_.get()));
1792 scoped_refptr<MockConnection> mock_connection1(
1793 new NiceMock<MockConnection>(device_info_.get()));
1794
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001795 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001796 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001797 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001798 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001799 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001800 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001801
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001802 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001803 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001804
1805 mock_service1->set_priority(1);
1806 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001807 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001808
1809 mock_service1->set_priority(0);
1810 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001811 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001812
Paul Stewartce4ec192012-03-14 12:53:46 -07001813 mock_service0->set_mock_connection(mock_connection0);
1814 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001815
1816 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001817 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001818 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001819
Darin Petkova5e07ef2012-07-09 14:27:57 +02001820 ServiceWatcher service_watcher;
1821 int tag =
1822 manager()->RegisterDefaultServiceCallback(
1823 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1824 service_watcher.AsWeakPtr()));
1825 EXPECT_EQ(1, tag);
1826
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001827 mock_service1->set_priority(1);
1828 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1829 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001830 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001831 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001832 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001833
Darin Petkova5e07ef2012-07-09 14:27:57 +02001834 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001835 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001836 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001837 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001838 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001839 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001840 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001841
Paul Stewartce4ec192012-03-14 12:53:46 -07001842 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001843 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001844 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001845 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001846
1847 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001848 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001849}
1850
Darin Petkova5e07ef2012-07-09 14:27:57 +02001851TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1852 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1853 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1854
1855 MockMetrics mock_metrics;
1856 manager()->set_metrics(&mock_metrics);
1857
1858 scoped_refptr<MockService> mock_service(
1859 new NiceMock<MockService>(
1860 control_interface(), dispatcher(), metrics(), manager()));
1861 ServiceRefPtr service = mock_service;
1862 ServiceRefPtr null_service;
1863
1864 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1865 manager()->NotifyDefaultServiceChanged(null_service);
1866
1867 ServiceWatcher service_watcher1;
1868 ServiceWatcher service_watcher2;
1869 int tag1 =
1870 manager()->RegisterDefaultServiceCallback(
1871 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1872 service_watcher1.AsWeakPtr()));
1873 EXPECT_EQ(1, tag1);
1874 int tag2 =
1875 manager()->RegisterDefaultServiceCallback(
1876 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1877 service_watcher2.AsWeakPtr()));
1878 EXPECT_EQ(2, tag2);
1879
1880 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1881 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1882 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1883 manager()->NotifyDefaultServiceChanged(null_service);
1884
1885 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1886 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1887 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1888 manager()->NotifyDefaultServiceChanged(mock_service);
1889
1890 manager()->DeregisterDefaultServiceCallback(tag1);
1891 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1892 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1893 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1894 manager()->NotifyDefaultServiceChanged(mock_service);
1895 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1896
1897 manager()->DeregisterDefaultServiceCallback(tag2);
1898 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1899 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1900 manager()->NotifyDefaultServiceChanged(mock_service);
1901
1902 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1903 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1904}
1905
Gaurav Shah435de2c2011-11-17 19:01:07 -08001906TEST_F(ManagerTest, AvailableTechnologies) {
1907 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1908 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001909 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001910 manager(),
1911 "null4",
1912 "addr4",
1913 0));
1914 manager()->RegisterDevice(mock_devices_[0]);
1915 manager()->RegisterDevice(mock_devices_[1]);
1916 manager()->RegisterDevice(mock_devices_[2]);
1917 manager()->RegisterDevice(mock_devices_[3]);
1918
1919 ON_CALL(*mock_devices_[0].get(), technology())
1920 .WillByDefault(Return(Technology::kEthernet));
1921 ON_CALL(*mock_devices_[1].get(), technology())
1922 .WillByDefault(Return(Technology::kWifi));
1923 ON_CALL(*mock_devices_[2].get(), technology())
1924 .WillByDefault(Return(Technology::kCellular));
1925 ON_CALL(*mock_devices_[3].get(), technology())
1926 .WillByDefault(Return(Technology::kWifi));
1927
1928 set<string> expected_technologies;
1929 expected_technologies.insert(Technology::NameFromIdentifier(
1930 Technology::kEthernet));
1931 expected_technologies.insert(Technology::NameFromIdentifier(
1932 Technology::kWifi));
1933 expected_technologies.insert(Technology::NameFromIdentifier(
1934 Technology::kCellular));
1935 Error error;
1936 vector<string> technologies = manager()->AvailableTechnologies(&error);
1937
1938 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1939 ContainerEq(expected_technologies));
1940}
1941
1942TEST_F(ManagerTest, ConnectedTechnologies) {
1943 scoped_refptr<MockService> connected_service1(
1944 new NiceMock<MockService>(control_interface(),
1945 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001946 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001947 manager()));
1948 scoped_refptr<MockService> connected_service2(
1949 new NiceMock<MockService>(control_interface(),
1950 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001951 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001952 manager()));
1953 scoped_refptr<MockService> disconnected_service1(
1954 new NiceMock<MockService>(control_interface(),
1955 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001956 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001957 manager()));
1958 scoped_refptr<MockService> disconnected_service2(
1959 new NiceMock<MockService>(control_interface(),
1960 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001961 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001962 manager()));
1963
1964 ON_CALL(*connected_service1.get(), IsConnected())
1965 .WillByDefault(Return(true));
1966 ON_CALL(*connected_service2.get(), IsConnected())
1967 .WillByDefault(Return(true));
1968
1969 manager()->RegisterService(connected_service1);
1970 manager()->RegisterService(connected_service2);
1971 manager()->RegisterService(disconnected_service1);
1972 manager()->RegisterService(disconnected_service2);
1973
1974 manager()->RegisterDevice(mock_devices_[0]);
1975 manager()->RegisterDevice(mock_devices_[1]);
1976 manager()->RegisterDevice(mock_devices_[2]);
1977 manager()->RegisterDevice(mock_devices_[3]);
1978
1979 ON_CALL(*mock_devices_[0].get(), technology())
1980 .WillByDefault(Return(Technology::kEthernet));
1981 ON_CALL(*mock_devices_[1].get(), technology())
1982 .WillByDefault(Return(Technology::kWifi));
1983 ON_CALL(*mock_devices_[2].get(), technology())
1984 .WillByDefault(Return(Technology::kCellular));
1985 ON_CALL(*mock_devices_[3].get(), technology())
1986 .WillByDefault(Return(Technology::kWifi));
1987
1988 mock_devices_[0]->SelectService(connected_service1);
1989 mock_devices_[1]->SelectService(disconnected_service1);
1990 mock_devices_[2]->SelectService(disconnected_service2);
1991 mock_devices_[3]->SelectService(connected_service2);
1992
1993 set<string> expected_technologies;
1994 expected_technologies.insert(Technology::NameFromIdentifier(
1995 Technology::kEthernet));
1996 expected_technologies.insert(Technology::NameFromIdentifier(
1997 Technology::kWifi));
1998 Error error;
1999
2000 vector<string> technologies = manager()->ConnectedTechnologies(&error);
2001 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
2002 ContainerEq(expected_technologies));
2003}
2004
2005TEST_F(ManagerTest, DefaultTechnology) {
2006 scoped_refptr<MockService> connected_service(
2007 new NiceMock<MockService>(control_interface(),
2008 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002009 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002010 manager()));
2011 scoped_refptr<MockService> disconnected_service(
2012 new NiceMock<MockService>(control_interface(),
2013 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002014 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002015 manager()));
2016
2017 // Connected. WiFi.
2018 ON_CALL(*connected_service.get(), IsConnected())
2019 .WillByDefault(Return(true));
2020 ON_CALL(*connected_service.get(), state())
2021 .WillByDefault(Return(Service::kStateConnected));
2022 ON_CALL(*connected_service.get(), technology())
2023 .WillByDefault(Return(Technology::kWifi));
2024
2025 // Disconnected. Ethernet.
2026 ON_CALL(*disconnected_service.get(), technology())
2027 .WillByDefault(Return(Technology::kEthernet));
2028
2029 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002030 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002031 Error error;
2032 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2033
2034
2035 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002036 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002037 // Connected service should be brought to the front now.
2038 string expected_technology =
2039 Technology::NameFromIdentifier(Technology::kWifi);
2040 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2041}
2042
Paul Stewart212d60f2012-07-12 10:59:13 -07002043TEST_F(ManagerTest, Stop) {
2044 scoped_refptr<MockProfile> profile(
2045 new NiceMock<MockProfile>(control_interface(), manager(), ""));
2046 AdoptProfile(manager(), profile);
2047 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002048 new NiceMock<MockService>(control_interface(),
2049 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002050 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002051 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002052 manager()->RegisterService(service);
2053 manager()->RegisterDevice(mock_devices_[0]);
2054 EXPECT_CALL(*profile.get(),
2055 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2056 .WillOnce(Return(true));
2057 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2058 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002059 manager()->Stop();
2060}
2061
mukesh agrawal00917ce2011-11-22 23:56:55 +00002062TEST_F(ManagerTest, UpdateServiceConnected) {
2063 scoped_refptr<MockService> mock_service(
2064 new NiceMock<MockService>(control_interface(),
2065 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002066 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002067 manager()));
2068 manager()->RegisterService(mock_service);
2069 EXPECT_FALSE(mock_service->favorite());
2070 EXPECT_FALSE(mock_service->auto_connect());
2071
Gaurav Shah435de2c2011-11-17 19:01:07 -08002072 EXPECT_CALL(*mock_service.get(), IsConnected())
2073 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002074 manager()->UpdateService(mock_service);
2075 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2076 // to mock out MakeFavorite. And mocking that out would break the
2077 // SortServices test. (crosbug.com/23370)
2078 EXPECT_TRUE(mock_service->favorite());
2079 EXPECT_TRUE(mock_service->auto_connect());
2080}
2081
Thieu Led4e9e552012-02-16 16:26:07 -08002082TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2083 // This tests the case where the user connects to a service that is
2084 // currently associated with a profile. We want to make sure that the
2085 // favorite flag is set and that the flag is saved to the current
2086 // profile.
2087 scoped_refptr<MockService> mock_service(
2088 new NiceMock<MockService>(control_interface(),
2089 dispatcher(),
2090 metrics(),
2091 manager()));
2092 manager()->RegisterService(mock_service);
2093 EXPECT_FALSE(mock_service->favorite());
2094 EXPECT_FALSE(mock_service->auto_connect());
2095
Gary Moraind93615e2012-04-27 11:50:03 -07002096 scoped_refptr<MockProfile> profile(
2097 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002098
Gary Moraind93615e2012-04-27 11:50:03 -07002099 mock_service->set_profile(profile);
2100 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002101 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002102 EXPECT_CALL(*profile,
2103 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002104 manager()->UpdateService(mock_service);
2105 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2106 // to mock out MakeFavorite. And mocking that out would break the
2107 // SortServices test. (crosbug.com/23370)
2108 EXPECT_TRUE(mock_service->favorite());
2109 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002110 // This releases the ref on the mock profile.
2111 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002112}
2113
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002114TEST_F(ManagerTest, SaveSuccessfulService) {
2115 scoped_refptr<MockProfile> profile(
2116 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2117 AdoptProfile(manager(), profile);
2118 scoped_refptr<MockService> service(
2119 new NiceMock<MockService>(control_interface(),
2120 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002121 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002122 manager()));
2123
2124 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2125 ServiceRefPtr expect_service(service.get());
2126
2127 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2128 .WillOnce(Return(false));
2129 manager()->RegisterService(service);
2130
2131 EXPECT_CALL(*service.get(), state())
2132 .WillRepeatedly(Return(Service::kStateConnected));
2133 EXPECT_CALL(*service.get(), IsConnected())
2134 .WillRepeatedly(Return(true));
2135 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2136 .WillOnce(Return(true));
2137 manager()->UpdateService(service);
2138}
2139
Darin Petkove7c6ad32012-06-29 10:22:09 +02002140TEST_F(ManagerTest, UpdateDevice) {
2141 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2142 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2143 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2144 AdoptProfile(manager(), profile0); // Passes ownership.
2145 AdoptProfile(manager(), profile1); // Passes ownership.
2146 AdoptProfile(manager(), profile2); // Passes ownership.
2147 DeviceRefPtr device_ref(mock_devices_[0].get());
2148 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2149 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2150 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2151 manager()->UpdateDevice(mock_devices_[0]);
2152}
2153
Paul Stewart1b253142012-01-26 14:05:52 -08002154TEST_F(ManagerTest, EnumerateProfiles) {
2155 vector<string> profile_paths;
2156 for (size_t i = 0; i < 10; i++) {
2157 scoped_refptr<MockProfile> profile(
2158 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002159 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002160 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2161 .WillOnce(Return(profile_paths.back()));
2162 AdoptProfile(manager(), profile);
2163 }
2164
2165 Error error;
2166 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2167 EXPECT_TRUE(error.IsSuccess());
2168 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2169 for (size_t i = 0; i < profile_paths.size(); i++) {
2170 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2171 }
2172}
2173
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002174TEST_F(ManagerTest, AutoConnectOnRegister) {
2175 MockServiceRefPtr service = MakeAutoConnectableService();
2176 EXPECT_CALL(*service.get(), AutoConnect());
2177 manager()->RegisterService(service);
2178 dispatcher()->DispatchPendingEvents();
2179}
2180
2181TEST_F(ManagerTest, AutoConnectOnUpdate) {
2182 MockServiceRefPtr service1 = MakeAutoConnectableService();
2183 service1->set_priority(1);
2184 MockServiceRefPtr service2 = MakeAutoConnectableService();
2185 service2->set_priority(2);
2186 manager()->RegisterService(service1);
2187 manager()->RegisterService(service2);
2188 dispatcher()->DispatchPendingEvents();
2189
2190 EXPECT_CALL(*service1.get(), AutoConnect());
2191 EXPECT_CALL(*service2.get(), state())
2192 .WillRepeatedly(Return(Service::kStateFailure));
2193 EXPECT_CALL(*service2.get(), IsFailed())
2194 .WillRepeatedly(Return(true));
2195 EXPECT_CALL(*service2.get(), IsConnected())
2196 .WillRepeatedly(Return(false));
2197 manager()->UpdateService(service2);
2198 dispatcher()->DispatchPendingEvents();
2199}
2200
2201TEST_F(ManagerTest, AutoConnectOnDeregister) {
2202 MockServiceRefPtr service1 = MakeAutoConnectableService();
2203 service1->set_priority(1);
2204 MockServiceRefPtr service2 = MakeAutoConnectableService();
2205 service2->set_priority(2);
2206 manager()->RegisterService(service1);
2207 manager()->RegisterService(service2);
2208 dispatcher()->DispatchPendingEvents();
2209
2210 EXPECT_CALL(*service1.get(), AutoConnect());
2211 manager()->DeregisterService(service2);
2212 dispatcher()->DispatchPendingEvents();
2213}
2214
Darin Petkov3ec55342012-09-28 14:04:44 +02002215TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2216 MockServiceRefPtr service = MakeAutoConnectableService();
2217 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2218 SetPowerManager();
2219 EXPECT_CALL(*service, AutoConnect()).Times(0);
2220 manager()->RegisterService(service);
2221 dispatcher()->DispatchPendingEvents();
2222}
2223
Darin Petkovca621542012-07-25 14:25:56 +02002224TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2225 MockServiceRefPtr service = MakeAutoConnectableService();
2226 SetPowerState(PowerManagerProxyDelegate::kMem);
2227 SetPowerManager();
2228 EXPECT_CALL(*service, AutoConnect()).Times(0);
2229 manager()->RegisterService(service);
2230 dispatcher()->DispatchPendingEvents();
2231}
2232
2233TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2234 MockServiceRefPtr service = MakeAutoConnectableService();
2235 SetPowerState(PowerManagerProxyDelegate::kOn);
2236 SetPowerManager();
2237 EXPECT_CALL(*service, AutoConnect());
2238 manager()->RegisterService(service);
2239 dispatcher()->DispatchPendingEvents();
2240}
2241
2242TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2243 MockServiceRefPtr service = MakeAutoConnectableService();
2244 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2245 SetPowerManager();
2246 EXPECT_CALL(*service, AutoConnect());
2247 manager()->RegisterService(service);
2248 dispatcher()->DispatchPendingEvents();
2249}
2250
2251TEST_F(ManagerTest, OnPowerStateChanged) {
2252 MockServiceRefPtr service = MakeAutoConnectableService();
2253 SetPowerState(PowerManagerProxyDelegate::kOn);
2254 SetPowerManager();
2255 EXPECT_CALL(*service, AutoConnect());
2256 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002257 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002258 dispatcher()->DispatchPendingEvents();
2259
mukesh agrawal784566d2012-08-08 18:32:58 -07002260 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002261 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2262 EXPECT_CALL(*service, AutoConnect());
2263 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002264 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002265
mukesh agrawal784566d2012-08-08 18:32:58 -07002266 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002267 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2268 EXPECT_CALL(*service, AutoConnect()).Times(0);
2269 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002270 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002271}
2272
Darin Petkov3ec55342012-09-28 14:04:44 +02002273TEST_F(ManagerTest, AddTerminationAction) {
2274 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
2275 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_));
2276 SetPowerManager();
2277 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2278 manager()->AddTerminationAction("action1", base::Closure());
2279 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2280 manager()->AddTerminationAction("action2", base::Closure());
2281}
2282
2283TEST_F(ManagerTest, RemoveTerminationAction) {
2284 static const char kKey1[] = "action1";
2285 static const char kKey2[] = "action2";
2286
2287 MockPowerManager &power_manager = *power_manager_;
2288 SetPowerManager();
2289
2290 // Removing an action when the hook table is empty should not result in any
2291 // calls to the power manager.
2292 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2293 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2294 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2295 manager()->RemoveTerminationAction("unknown");
2296 Mock::VerifyAndClearExpectations(&power_manager);
2297
2298 manager()->AddTerminationAction(kKey1, base::Closure());
2299 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2300 manager()->AddTerminationAction(kKey2, base::Closure());
2301
2302 // Removing an action that ends up with a non-empty hook table should not
2303 // result in any calls to the power manager.
2304 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2305 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2306 manager()->RemoveTerminationAction(kKey1);
2307 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2308 Mock::VerifyAndClearExpectations(&power_manager);
2309
2310 // Removing the last action should trigger unregistering from the power
2311 // manager.
2312 EXPECT_CALL(power_manager, UnregisterSuspendDelay());
2313 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2314 manager()->RemoveTerminationAction(kKey2);
2315 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2316}
2317
2318TEST_F(ManagerTest, RunTerminationActions) {
2319 TerminationActionTest test_action;
2320 const string kActionName = "action";
2321
2322 EXPECT_CALL(test_action, Done(_));
2323 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2324 test_action.AsWeakPtr()));
2325
2326 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2327 Bind(&TerminationActionTest::Action,
2328 test_action.AsWeakPtr()));
2329 test_action.set_manager(manager());
2330 EXPECT_CALL(test_action, Done(_));
2331 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2332 test_action.AsWeakPtr()));
2333}
2334
2335TEST_F(ManagerTest, OnSuspendDelay) {
2336 const uint32 kSeqNumber = 123;
2337 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2338 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2339 SetPowerManager();
2340 OnSuspendDelay(kSeqNumber);
2341}
2342
2343TEST_F(ManagerTest, OnSuspendActionsComplete) {
2344 const uint32 kSeqNumber = 54321;
2345 Error error;
2346 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2347 SetPowerManager();
2348 OnSuspendActionsComplete(kSeqNumber, error);
2349}
2350
Paul Stewartc681fa02012-03-02 19:40:04 -08002351TEST_F(ManagerTest, RecheckPortal) {
2352 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2353 .WillOnce(Return(false));
2354 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2355 .WillOnce(Return(true));
2356 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2357 .Times(0);
2358
2359 manager()->RegisterDevice(mock_devices_[0]);
2360 manager()->RegisterDevice(mock_devices_[1]);
2361 manager()->RegisterDevice(mock_devices_[2]);
2362
2363 manager()->RecheckPortal(NULL);
2364}
2365
Paul Stewartd215af62012-04-24 23:25:50 -07002366TEST_F(ManagerTest, RecheckPortalOnService) {
2367 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2368 dispatcher(),
2369 metrics(),
2370 manager());
2371 EXPECT_CALL(*mock_devices_[0].get(),
2372 IsConnectedToService(IsRefPtrTo(service)))
2373 .WillOnce(Return(false));
2374 EXPECT_CALL(*mock_devices_[1].get(),
2375 IsConnectedToService(IsRefPtrTo(service)))
2376 .WillOnce(Return(true));
2377 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2378 .WillOnce(Return(true));
2379 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2380 .Times(0);
2381
2382 manager()->RegisterDevice(mock_devices_[0]);
2383 manager()->RegisterDevice(mock_devices_[1]);
2384 manager()->RegisterDevice(mock_devices_[2]);
2385
2386 manager()->RecheckPortalOnService(service);
2387}
2388
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002389TEST_F(ManagerTest, GetDefaultService) {
2390 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002391 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002392
2393 scoped_refptr<MockService> mock_service(
2394 new NiceMock<MockService>(control_interface(),
2395 dispatcher(),
2396 metrics(),
2397 manager()));
2398
2399 manager()->RegisterService(mock_service);
2400 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002401 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002402
2403 scoped_refptr<MockConnection> mock_connection(
2404 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002405 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002406 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002407 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002408
Paul Stewartce4ec192012-03-14 12:53:46 -07002409 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002410 manager()->DeregisterService(mock_service);
2411}
2412
Paul Stewart13ed2252012-03-21 12:52:46 -07002413TEST_F(ManagerTest, GetServiceWithGUID) {
2414 scoped_refptr<MockService> mock_service0(
2415 new NiceMock<MockService>(control_interface(),
2416 dispatcher(),
2417 metrics(),
2418 manager()));
2419
2420 scoped_refptr<MockService> mock_service1(
2421 new NiceMock<MockService>(control_interface(),
2422 dispatcher(),
2423 metrics(),
2424 manager()));
2425
Paul Stewartcb59fed2012-03-21 21:14:46 -07002426 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2427 .Times(0);
2428 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2429 .Times(0);
2430
Paul Stewart13ed2252012-03-21 12:52:46 -07002431 manager()->RegisterService(mock_service0);
2432 manager()->RegisterService(mock_service1);
2433
2434 const string kGUID0 = "GUID0";
2435 const string kGUID1 = "GUID1";
2436
2437 {
2438 Error error;
2439 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2440 EXPECT_FALSE(error.IsSuccess());
2441 EXPECT_FALSE(service);
2442 }
2443
2444 KeyValueStore args;
2445 args.SetString(flimflam::kGuidProperty, kGUID1);
2446
2447 {
2448 Error error;
2449 ServiceRefPtr service = manager()->GetService(args, &error);
2450 EXPECT_EQ(Error::kInvalidArguments, error.type());
2451 EXPECT_FALSE(service);
2452 }
2453
2454 mock_service0->set_guid(kGUID0);
2455 mock_service1->set_guid(kGUID1);
2456
2457 {
2458 Error error;
2459 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2460 EXPECT_TRUE(error.IsSuccess());
2461 EXPECT_EQ(mock_service0.get(), service.get());
2462 }
2463
2464 {
2465 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002466 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2467 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002468 ServiceRefPtr service = manager()->GetService(args, &error);
2469 EXPECT_TRUE(error.IsSuccess());
2470 EXPECT_EQ(mock_service1.get(), service.get());
2471 }
2472
2473 manager()->DeregisterService(mock_service0);
2474 manager()->DeregisterService(mock_service1);
2475}
2476
Gary Morain028545d2012-04-07 14:55:52 -07002477
2478TEST_F(ManagerTest, CalculateStateOffline) {
2479 MockMetrics mock_metrics;
2480 manager()->set_metrics(&mock_metrics);
2481 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2482 .Times(AnyNumber());
2483 scoped_refptr<MockService> mock_service0(
2484 new NiceMock<MockService>(control_interface(),
2485 dispatcher(),
2486 metrics(),
2487 manager()));
2488
2489 scoped_refptr<MockService> mock_service1(
2490 new NiceMock<MockService>(control_interface(),
2491 dispatcher(),
2492 metrics(),
2493 manager()));
2494
2495 EXPECT_CALL(*mock_service0.get(), IsConnected())
2496 .WillRepeatedly(Return(false));
2497 EXPECT_CALL(*mock_service1.get(), IsConnected())
2498 .WillRepeatedly(Return(false));
2499
2500 manager()->RegisterService(mock_service0);
2501 manager()->RegisterService(mock_service1);
2502
2503 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2504
2505 manager()->DeregisterService(mock_service0);
2506 manager()->DeregisterService(mock_service1);
2507}
2508
2509TEST_F(ManagerTest, CalculateStateOnline) {
2510 MockMetrics mock_metrics;
2511 manager()->set_metrics(&mock_metrics);
2512 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2513 .Times(AnyNumber());
2514 scoped_refptr<MockService> mock_service0(
2515 new NiceMock<MockService>(control_interface(),
2516 dispatcher(),
2517 metrics(),
2518 manager()));
2519
2520 scoped_refptr<MockService> mock_service1(
2521 new NiceMock<MockService>(control_interface(),
2522 dispatcher(),
2523 metrics(),
2524 manager()));
2525
2526 EXPECT_CALL(*mock_service0.get(), IsConnected())
2527 .WillRepeatedly(Return(false));
2528 EXPECT_CALL(*mock_service1.get(), IsConnected())
2529 .WillRepeatedly(Return(true));
2530 EXPECT_CALL(*mock_service0.get(), state())
2531 .WillRepeatedly(Return(Service::kStateIdle));
2532 EXPECT_CALL(*mock_service1.get(), state())
2533 .WillRepeatedly(Return(Service::kStateConnected));
2534
2535 manager()->RegisterService(mock_service0);
2536 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002537 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002538
2539 EXPECT_EQ("online", manager()->CalculateState(NULL));
2540
2541 manager()->DeregisterService(mock_service0);
2542 manager()->DeregisterService(mock_service1);
2543}
2544
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002545TEST_F(ManagerTest, StartupPortalList) {
2546 // Simulate loading value from the default profile.
2547 const string kProfileValue("wifi,vpn");
2548 manager()->props_.check_portal_list = kProfileValue;
2549
2550 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2551 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2552 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2553
2554 const string kStartupValue("cellular,ethernet");
2555 manager()->SetStartupPortalList(kStartupValue);
2556 // Ensure profile value is not overwritten, so when we save the default
2557 // profile, the correct value will still be written.
2558 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2559
2560 // However we should read back a different list.
2561 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2562 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2563 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2564
2565 const string kRuntimeValue("ppp");
2566 // Setting a runtime value over the control API should overwrite both
2567 // the profile value and what we read back.
2568 Error error;
2569 manager()->mutable_store()->SetStringProperty(
2570 flimflam::kCheckPortalListProperty,
2571 kRuntimeValue,
2572 &error);
2573 ASSERT_TRUE(error.IsSuccess());
2574 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2575 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2576 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2577 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2578}
2579
Paul Stewart036dba02012-08-07 12:34:41 -07002580TEST_F(ManagerTest, LinkMonitorEnabled) {
2581 const string kEnabledTechnologies("wifi,vpn");
2582 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2583 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2584 EXPECT_FALSE(
2585 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2586}
2587
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002588TEST_F(ManagerTest, EnableTechnology) {
2589 Error error(Error::kOperationInitiated);
2590 ResultCallback callback;
2591 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2592 EXPECT_TRUE(error.IsSuccess());
2593
Joshua Krollda798622012-06-05 12:30:48 -07002594 ON_CALL(*mock_devices_[0], technology())
2595 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002596
2597 manager()->RegisterDevice(mock_devices_[0]);
2598
2599 // Device is enabled, so expect operation is successful.
2600 mock_devices_[0]->enabled_ = true;
2601 error.Populate(Error::kOperationInitiated);
2602 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2603 EXPECT_TRUE(error.IsSuccess());
2604
2605 // Device is disabled, so expect operation in progress.
2606 mock_devices_[0]->enabled_ = false;
2607 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2608 error.Populate(Error::kOperationInitiated);
2609 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2610 EXPECT_TRUE(error.IsOngoing());
2611}
2612
2613TEST_F(ManagerTest, DisableTechnology) {
2614 Error error(Error::kOperationInitiated);
2615 ResultCallback callback;
2616 manager()->DisableTechnology(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 disabled, so expect operation is successful.
2625 error.Populate(Error::kOperationInitiated);
2626 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2627 EXPECT_TRUE(error.IsSuccess());
2628
2629 // Device is enabled, so expect operation in progress.
2630 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2631 mock_devices_[0]->enabled_ = true;
2632 error.Populate(Error::kOperationInitiated);
2633 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2634 EXPECT_TRUE(error.IsOngoing());
2635}
2636
Paul Stewart4d5efb72012-09-17 12:24:34 -07002637TEST_F(ManagerTest, IgnoredSearchList) {
2638 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
2639 SetResolver(resolver.get());
2640 vector<string> ignored_paths;
2641 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2642 SetIgnoredDNSSearchPaths("");
2643 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
2644
2645 const string kIgnored0 = "chromium.org";
2646 ignored_paths.push_back(kIgnored0);
2647 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2648 SetIgnoredDNSSearchPaths(kIgnored0);
2649 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
2650
2651 const string kIgnored1 = "google.com";
2652 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
2653 ignored_paths.push_back(kIgnored1);
2654 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2655 SetIgnoredDNSSearchPaths(kIgnoredSum);
2656 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
2657
2658 SetResolver(Resolver::GetInstance());
2659}
2660
Paul Stewartbfb82552012-10-24 16:48:48 -07002661TEST_F(ManagerTest, ServiceStateChangeEmitsServices) {
2662 // Test to make sure that every service state-change causes the
2663 // Manager to emit a new service list.
2664 scoped_refptr<MockService> mock_service(
2665 new NiceMock<MockService>(control_interface(),
2666 dispatcher(),
2667 metrics(),
2668 manager()));
2669 EXPECT_CALL(*mock_service, state())
2670 .WillRepeatedly(Return(Service::kStateIdle));
2671
2672 manager()->RegisterService(mock_service);
2673 EXPECT_CALL(
2674 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2675 flimflam::kServicesProperty, _)).Times(1);
2676 EXPECT_CALL(
2677 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2678 flimflam::kServiceWatchListProperty, _)).Times(1);
2679 CompleteServiceSort();
2680
2681 Mock::VerifyAndClearExpectations(manager_adaptor_);
2682 EXPECT_CALL(
2683 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2684 flimflam::kServicesProperty, _)).Times(1);
2685 EXPECT_CALL(
2686 *manager_adaptor_, EmitRpcIdentifierArrayChanged(
2687 flimflam::kServiceWatchListProperty, _)).Times(1);
2688 manager()->UpdateService(mock_service.get());
2689 CompleteServiceSort();
2690
2691 manager()->DeregisterService(mock_service);
2692}
2693
2694TEST_F(ManagerTest, EnumerateServices) {
2695 scoped_refptr<MockService> mock_service(
2696 new NiceMock<MockService>(control_interface(),
2697 dispatcher(),
2698 metrics(),
2699 manager()));
2700 manager()->RegisterService(mock_service);
2701
2702 EXPECT_CALL(*mock_service, state())
2703 .WillRepeatedly(Return(Service::kStateConnected));
2704 EXPECT_CALL(*mock_service, IsVisible())
2705 .WillRepeatedly(Return(false));
2706 EXPECT_TRUE(EnumerateAvailableServices().empty());
2707 EXPECT_TRUE(EnumerateWatchedServices().empty());
2708
2709 EXPECT_CALL(*mock_service, state())
2710 .WillRepeatedly(Return(Service::kStateIdle));
2711 EXPECT_TRUE(EnumerateAvailableServices().empty());
2712 EXPECT_TRUE(EnumerateWatchedServices().empty());
2713
2714 EXPECT_CALL(*mock_service, IsVisible())
2715 .WillRepeatedly(Return(true));
2716 Service::ConnectState unwatched_states[] = {
2717 Service::kStateUnknown,
2718 Service::kStateIdle,
2719 Service::kStateFailure
2720 };
2721 for (size_t i = 0; i < arraysize(unwatched_states); ++i) {
2722 EXPECT_CALL(*mock_service, state())
2723 .WillRepeatedly(Return(unwatched_states[i]));
2724 EXPECT_FALSE(EnumerateAvailableServices().empty());
2725 EXPECT_TRUE(EnumerateWatchedServices().empty());
2726 }
2727
2728 Service::ConnectState watched_states[] = {
2729 Service::kStateAssociating,
2730 Service::kStateConfiguring,
2731 Service::kStateConnected,
2732 Service::kStateDisconnected,
2733 Service::kStatePortal,
2734 Service::kStateOnline
2735 };
2736 for (size_t i = 0; i < arraysize(watched_states); ++i) {
2737 EXPECT_CALL(*mock_service, state())
2738 .WillRepeatedly(Return(watched_states[i]));
2739 EXPECT_FALSE(EnumerateAvailableServices().empty());
2740 EXPECT_FALSE(EnumerateWatchedServices().empty());
2741 }
2742
2743 manager()->DeregisterService(mock_service);
2744}
2745
Chris Masone9be4a9d2011-05-16 15:44:09 -07002746} // namespace shill