blob: 26ed83f9d88f5692f74bd294f7fec5cb4ec28085 [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
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000293 MockServiceRefPtr MakeAutoConnectableService() {
294 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
295 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800296 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000297 manager());
298 service->MakeFavorite();
299 service->set_connectable(true);
300 return service;
301 }
302
Darin Petkovca621542012-07-25 14:25:56 +0200303 TestProxyFactory proxy_factory_;
304 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700305 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700306 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800307 scoped_ptr<MockDeviceInfo> device_info_;
308
309 // This pointer is owned by the manager, and only tracked here for EXPECT*()
310 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700311};
312
Darin Petkov3ec55342012-09-28 14:04:44 +0200313const char ManagerTest::TerminationActionTest::kActionName[] = "action";
314
Paul Stewart22aa71b2011-09-16 12:15:11 -0700315bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700316 if (!manager()->sort_services_task_.IsCancelled()) {
317 manager()->SortServicesTask();
318 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700319 return (svc0.get() == manager()->services_[0].get() &&
320 svc1.get() == manager()->services_[1].get());
321}
322
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700323TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700324 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
325 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700326}
327
Chris Masone9be4a9d2011-05-16 15:44:09 -0700328TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700329 ON_CALL(*mock_devices_[0].get(), technology())
330 .WillByDefault(Return(Technology::kEthernet));
331 ON_CALL(*mock_devices_[1].get(), technology())
332 .WillByDefault(Return(Technology::kWifi));
333 ON_CALL(*mock_devices_[2].get(), technology())
334 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700335
Paul Stewart22aa71b2011-09-16 12:15:11 -0700336 manager()->RegisterDevice(mock_devices_[0]);
337 manager()->RegisterDevice(mock_devices_[1]);
338 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700339
Paul Stewart22aa71b2011-09-16 12:15:11 -0700340 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
341 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
342 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700343}
344
Paul Stewarta41e38d2011-11-11 07:47:29 -0800345TEST_F(ManagerTest, DeviceRegistrationAndStart) {
346 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500347 mock_devices_[0]->enabled_persistent_ = true;
348 mock_devices_[1]->enabled_persistent_ = false;
349 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800350 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500351 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800352 .Times(0);
353 manager()->RegisterDevice(mock_devices_[0]);
354 manager()->RegisterDevice(mock_devices_[1]);
355}
356
357TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
358 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
359 DeviceRefPtr device_ref(mock_devices_[0].get());
360 AdoptProfile(manager(), profile); // Passes ownership.
361 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200362 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800363 manager()->RegisterDevice(mock_devices_[0]);
364}
365
Chris Masone9be4a9d2011-05-16 15:44:09 -0700366TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700367 ON_CALL(*mock_devices_[0].get(), technology())
368 .WillByDefault(Return(Technology::kEthernet));
369 ON_CALL(*mock_devices_[1].get(), technology())
370 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700371
Gaurav Shah435de2c2011-11-17 19:01:07 -0800372 manager()->RegisterDevice(mock_devices_[0]);
373 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700374
Paul Stewart22aa71b2011-09-16 12:15:11 -0700375 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
376 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700377
Paul Stewart212d60f2012-07-12 10:59:13 -0700378 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
379 AdoptProfile(manager(), profile); // Passes ownership.
380
Eric Shienbrood9a245532012-03-07 14:20:39 -0500381 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700382 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800383 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700384 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700385
Eric Shienbrood9a245532012-03-07 14:20:39 -0500386 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700387 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800388 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700389 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700390}
391
392TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700393 // It's much easier and safer to use a real GLib for this test.
394 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700395 Manager manager(control_interface(),
396 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800397 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700398 &glib,
399 run_path(),
400 storage_path(),
401 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700402 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
403 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700404 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700405
Chris Masone9be4a9d2011-05-16 15:44:09 -0700406 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700407 new NiceMock<MockService>(control_interface(),
408 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800409 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700410 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700411 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700412 new NiceMock<MockService>(control_interface(),
413 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800414 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700415 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700416
mukesh agrawal51a7e932011-07-27 16:18:26 -0700417 string service1_name(mock_service->UniqueName());
418 string service2_name(mock_service2->UniqueName());
419
420 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
421 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700422 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700423 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700424 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700425 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700426 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700427
Chris Masone9d779932011-08-25 16:33:41 -0700428 manager.RegisterService(mock_service);
429 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700430
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800431 Error error;
432 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700433 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700434 EXPECT_EQ(2, ids.size());
435 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
436 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700437
Chris Masone9d779932011-08-25 16:33:41 -0700438 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
439 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
440
441 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700442}
443
Chris Masone6515aab2011-10-12 16:19:09 -0700444TEST_F(ManagerTest, RegisterKnownService) {
445 // It's much easier and safer to use a real GLib for this test.
446 GLib glib;
447 Manager manager(control_interface(),
448 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800449 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700450 &glib,
451 run_path(),
452 storage_path(),
453 string());
454 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
455 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700456 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700457 {
458 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
459 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800460 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700461 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700462 ASSERT_TRUE(profile->AdoptService(service1));
463 ASSERT_TRUE(profile->ContainsService(service1));
464 } // Force destruction of service1.
465
466 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
467 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800468 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700469 &manager));
470 manager.RegisterService(service2);
471 EXPECT_EQ(service2->profile().get(), profile.get());
472 manager.Stop();
473}
474
475TEST_F(ManagerTest, RegisterUnknownService) {
476 // It's much easier and safer to use a real GLib for this test.
477 GLib glib;
478 Manager manager(control_interface(),
479 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800480 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700481 &glib,
482 run_path(),
483 storage_path(),
484 string());
485 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
486 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700487 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700488 {
489 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
490 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800491 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700492 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700493 ASSERT_TRUE(profile->AdoptService(service1));
494 ASSERT_TRUE(profile->ContainsService(service1));
495 } // Force destruction of service1.
496 scoped_refptr<MockService> mock_service2(
497 new NiceMock<MockService>(control_interface(),
498 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800499 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700500 &manager));
501 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
502 .WillRepeatedly(Return(mock_service2->UniqueName()));
503 manager.RegisterService(mock_service2);
504 EXPECT_NE(mock_service2->profile().get(), profile.get());
505 manager.Stop();
506}
507
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000508TEST_F(ManagerTest, DeregisterUnregisteredService) {
509 // WiFi assumes that it can deregister a service that is not
510 // registered. (E.g. a hidden service can be deregistered when it
511 // loses its last endpoint, and again when WiFi is Stop()-ed.)
512 //
513 // So test that doing so doesn't cause a crash.
514 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
515 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800516 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000517 manager());
518 manager()->DeregisterService(service);
519}
520
Chris Masonea8a2c252011-06-27 22:16:30 -0700521TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700522 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700523 map<string, ::DBus::Variant> props;
524 Error error(Error::kInvalidProperty, "");
525 {
526 ::DBus::Error dbus_error;
527 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700528 manager()->mutable_store()->SetStringProperty(
529 flimflam::kCheckPortalListProperty,
530 expected,
531 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700532 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700533 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
534 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
535 expected);
536 }
537 {
538 ::DBus::Error dbus_error;
539 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700540 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
541 expected,
542 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700543 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700544 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
545 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
546 expected);
547 }
548}
549
Chris Masone3c3f6a12011-07-01 10:01:41 -0700550TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700551 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800552 manager()->RegisterDevice(mock_devices_[0]);
553 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700554 {
555 map<string, ::DBus::Variant> props;
556 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700557 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700558 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700559 vector < ::DBus::Path> devices =
560 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700561 EXPECT_EQ(2, devices.size());
562 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700563}
564
mukesh agrawal2366eed2012-03-20 18:21:50 -0700565TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700566 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700567 map<string, ::DBus::Variant> props;
568 ::DBus::Error dbus_error;
569 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
570 map<string, ::DBus::Variant>::const_iterator prop =
571 props.find(flimflam::kServicesProperty);
572 ASSERT_FALSE(prop == props.end());
573 const ::DBus::Variant &variant = prop->second;
574 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
575}
576
Chris Masone6791a432011-07-12 13:23:19 -0700577TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700578 Manager manager(control_interface(),
579 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800580 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700581 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700582 run_path(),
583 storage_path(),
584 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700585 scoped_refptr<MockService> s2(new MockService(control_interface(),
586 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800587 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700588 &manager));
589 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700590 {
Chris Masone6515aab2011-10-12 16:19:09 -0700591 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700592 ProfileRefPtr profile(
593 new Profile(control_interface(), &manager, id, "", false));
594 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700595 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700596 .WillRepeatedly(Return(true));
597 EXPECT_CALL(*storage, Flush())
598 .Times(AnyNumber())
599 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700600 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700601 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700602 }
Chris Masone6515aab2011-10-12 16:19:09 -0700603 // Create a profile that already has |s2| in it.
604 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700605 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700606
Chris Masone6515aab2011-10-12 16:19:09 -0700607 // Now, move the Service |s2| to another profile.
608 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
609 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700610
611 // Force destruction of the original Profile, to ensure that the Service
612 // is kept alive and populated with data.
613 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700614 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700615 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700616}
617
Paul Stewart7f61e522012-03-22 11:13:45 -0700618TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
619 scoped_refptr<MockProfile> mock_profile(
620 new MockProfile(control_interface(), manager(), ""));
621 const string kProfileName("profile0");
622 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
623 .WillRepeatedly(Return(kProfileName));
624 AdoptProfile(manager(), mock_profile);
625
626 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
627 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
628 EXPECT_EQ(mock_profile.get(), profile.get());
629}
630
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800631TEST_F(ManagerTest, SetProfileForService) {
632 scoped_refptr<MockProfile> profile0(
633 new MockProfile(control_interface(), manager(), ""));
634 string profile_name0("profile0");
635 EXPECT_CALL(*profile0, GetRpcIdentifier())
636 .WillRepeatedly(Return(profile_name0));
637 AdoptProfile(manager(), profile0);
638 scoped_refptr<MockService> service(new MockService(control_interface(),
639 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800640 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800641 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700642 EXPECT_FALSE(manager()->HasService(service));
643 {
644 Error error;
645 EXPECT_CALL(*profile0, AdoptService(_))
646 .WillOnce(Return(true));
647 // Expect that setting the profile of a service that does not already
648 // have one assigned does not cause a crash.
649 manager()->SetProfileForService(service, "profile0", &error);
650 EXPECT_TRUE(error.IsSuccess());
651 }
652
653 // The service should be registered as a side-effect of the profile being
654 // set for this service.
655 EXPECT_TRUE(manager()->HasService(service));
656
657 // Since we have mocked Profile::AdoptServie() above, the service's
658 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800659 service->set_profile(profile0);
660
661 {
662 Error error;
663 manager()->SetProfileForService(service, "foo", &error);
664 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700665 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800666 }
667
668 {
669 Error error;
670 manager()->SetProfileForService(service, profile_name0, &error);
671 EXPECT_EQ(Error::kInvalidArguments, error.type());
672 EXPECT_EQ("Service is already connected to this profile", error.message());
673 }
674
675 scoped_refptr<MockProfile> profile1(
676 new MockProfile(control_interface(), manager(), ""));
677 string profile_name1("profile1");
678 EXPECT_CALL(*profile1, GetRpcIdentifier())
679 .WillRepeatedly(Return(profile_name1));
680 AdoptProfile(manager(), profile1);
681
682 {
683 Error error;
684 EXPECT_CALL(*profile1, AdoptService(_))
685 .WillOnce(Return(true));
686 EXPECT_CALL(*profile0, AbandonService(_))
687 .WillOnce(Return(true));
688 manager()->SetProfileForService(service, profile_name1, &error);
689 EXPECT_TRUE(error.IsSuccess());
690 }
691}
692
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700693TEST_F(ManagerTest, CreateProfile) {
694 // It's much easier to use real Glib here since we want the storage
695 // side-effects.
696 GLib glib;
697 ScopedTempDir temp_dir;
698 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
699
700 Manager manager(control_interface(),
701 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800702 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700703 &glib,
704 run_path(),
705 storage_path(),
706 temp_dir.path().value());
707
708 // Invalid name should be rejected.
709 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
710
Paul Stewartd0a3b812012-03-28 22:48:22 -0700711 // A profile with invalid characters in it should similarly be rejected.
712 EXPECT_EQ(Error::kInvalidArguments,
713 TestCreateProfile(&manager, "valid_profile"));
714
715 // We should be able to create a machine profile.
716 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700717
Gary Morainb672d352012-04-25 09:19:06 -0700718 // We should succeed in creating a valid user profile. Verify the returned
719 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700720 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700721 {
722 Error error;
723 string path;
724 manager.CreateProfile(kProfile, &path, &error);
725 EXPECT_EQ(Error::kSuccess, error.type());
726 EXPECT_EQ("/profile_rpc", path);
727 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700728
729 // We should fail in creating it a second time (already exists).
730 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
731}
732
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700733// We receive PopProfile when a user logs out, and it should always trigger a
734// MemoryLog Clear() call.
735TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
736 GLib glib;
737 ScopedTempDir temp_dir;
738 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
739 Manager manager(control_interface(),
740 dispatcher(),
741 metrics(),
742 &glib,
743 run_path(),
744 storage_path(),
745 temp_dir.path().value());
746 const char kProfile0[] = "~user/profile0";
747 const char kPurgedMessage[] = "This message should be purged";
748 // Create a profile and push it on the stack, leave one uncreated
749 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
750 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
751
752 // Popping a profile which isn't on top should still clear the log.
753 LOG(INFO) << kPurgedMessage;
754 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
755 kPurgedMessage));
756 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
757 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
758 kPurgedMessage));
759
760 // Popping an invalid profile name should do the same thing.
761 LOG(INFO) << kPurgedMessage;
762 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
763 kPurgedMessage));
764 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
765 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
766 kPurgedMessage));
767
768 // Successful pops also purge the message log.
769 LOG(INFO) << kPurgedMessage;
770 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
771 kPurgedMessage));
772 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
773 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
774 kPurgedMessage));
775}
776
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700777TEST_F(ManagerTest, PushPopProfile) {
778 // It's much easier to use real Glib in creating a Manager for this
779 // test here since we want the storage side-effects.
780 GLib glib;
781 ScopedTempDir temp_dir;
782 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
783 Manager manager(control_interface(),
784 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800785 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700786 &glib,
787 run_path(),
788 storage_path(),
789 temp_dir.path().value());
790
791 // Pushing an invalid profile should fail.
792 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
793
Paul Stewartd0a3b812012-03-28 22:48:22 -0700794 // Pushing a default profile that does not exist should fail.
795 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700796
797 const char kProfile0[] = "~user/profile0";
798 const char kProfile1[] = "~user/profile1";
799
800 // Create a couple of profiles.
801 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
802 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
803
804 // Push these profiles on the stack.
805 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
806 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
807
808 // Pushing a profile a second time should fail.
809 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
810 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
811
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800812 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700813 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800814 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700815
816 // Make sure a profile name that doesn't exist fails.
817 const char kProfile2Id[] = "profile2";
818 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
819 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
820
821 // Create a new service, with a specific storage name.
822 scoped_refptr<MockService> service(
823 new NiceMock<MockService>(control_interface(),
824 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800825 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700826 &manager));
827 const char kServiceName[] = "service_storage_name";
828 EXPECT_CALL(*service.get(), GetStorageIdentifier())
829 .WillRepeatedly(Return(kServiceName));
830 EXPECT_CALL(*service.get(), Load(_))
831 .WillRepeatedly(Return(true));
832
833 // Add this service to the manager -- it should end up in the ephemeral
834 // profile.
835 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800836 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700837
838 // Create storage for a profile that contains the service storage name.
839 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
840 kServiceName));
841
842 // When we push the profile, the service should move away from the
843 // ephemeral profile to this new profile since it has an entry for
844 // this service.
845 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800846 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700847 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
848
849 // Insert another profile that should supersede ownership of the service.
850 const char kProfile3Id[] = "profile3";
851 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
852 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
853 kServiceName));
854 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
855 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
856
857 // Popping an invalid profile name should fail.
858 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
859
860 // Popping an profile that is not at the top of the stack should fail.
861 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
862
863 // Popping the top profile should succeed.
864 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
865
866 // Moreover the service should have switched profiles to profile 2.
867 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
868
869 // Popping the top profile should succeed.
870 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
871
872 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800873 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700874
875 // Pop the remaining two services off the stack.
876 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
877 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
878
879 // Next pop should fail with "stack is empty".
880 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700881
882 const char kMachineProfile0[] = "machineprofile0";
883 const char kMachineProfile1[] = "machineprofile1";
884 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
885 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
886
887 // Should be able to push a machine profile.
888 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
889
890 // Should be able to push a user profile atop a machine profile.
891 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
892
893 // Pushing a system-wide profile on top of a user profile should fail.
894 EXPECT_EQ(Error::kInvalidArguments,
895 TestPushProfile(&manager, kMachineProfile1));
896
897 // However if we pop the user profile, we should be able stack another
898 // machine profile on.
899 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
900 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700901}
902
Paul Stewarte73d05c2012-03-29 16:26:05 -0700903TEST_F(ManagerTest, RemoveProfile) {
904 // It's much easier to use real Glib in creating a Manager for this
905 // test here since we want the storage side-effects.
906 GLib glib;
907 ScopedTempDir temp_dir;
908 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
909 Manager manager(control_interface(),
910 dispatcher(),
911 metrics(),
912 &glib,
913 run_path(),
914 storage_path(),
915 temp_dir.path().value());
916
917 const char kProfile0[] = "profile0";
918 FilePath profile_path(
919 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
920
921 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
922 ASSERT_TRUE(file_util::PathExists(profile_path));
923
924 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
925
926 // Remove should fail since the profile is still on the stack.
927 {
928 Error error;
929 manager.RemoveProfile(kProfile0, &error);
930 EXPECT_EQ(Error::kInvalidArguments, error.type());
931 }
932
933 // Profile path should still exist.
934 EXPECT_TRUE(file_util::PathExists(profile_path));
935
936 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
937
938 // This should succeed now that the profile is off the stack.
939 {
940 Error error;
941 manager.RemoveProfile(kProfile0, &error);
942 EXPECT_EQ(Error::kSuccess, error.type());
943 }
944
945 // Profile path should no longer exist.
946 EXPECT_FALSE(file_util::PathExists(profile_path));
947
948 // Another remove succeeds, due to a foible in file_util::Delete --
949 // it is not an error to delete a file that does not exist.
950 {
951 Error error;
952 manager.RemoveProfile(kProfile0, &error);
953 EXPECT_EQ(Error::kSuccess, error.type());
954 }
955
956 // Let's create an error case that will "work". Create a non-empty
957 // directory in the place of the profile pathname.
958 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
959 {
960 Error error;
961 manager.RemoveProfile(kProfile0, &error);
962 EXPECT_EQ(Error::kOperationFailed, error.type());
963 }
964}
965
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700966TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
967 // It's much easier to use real Glib in creating a Manager for this
968 // test here since we want the storage side-effects.
969 GLib glib;
970 ScopedTempDir temp_dir;
971 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
972 Manager manager(control_interface(),
973 dispatcher(),
974 metrics(),
975 &glib,
976 run_path(),
977 storage_path(),
978 temp_dir.path().value());
979
980 const char kProfile0[] = "profile0";
981 FilePath profile_path(
982 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
983
984 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
985 ASSERT_TRUE(file_util::PathExists(profile_path));
986 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
987
988 // Ensure that even if the backing filestore is removed, we still can't
989 // create a profile twice.
990 ASSERT_TRUE(file_util::Delete(profile_path, false));
991 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
992}
993
Paul Stewart75225512012-01-26 22:51:33 -0800994// Use this matcher instead of passing RefPtrs directly into the arguments
995// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
996// system teardown.
997MATCHER_P(IsRefPtrTo, ref_address, "") {
998 return arg.get() == ref_address;
999}
1000
1001TEST_F(ManagerTest, HandleProfileEntryDeletion) {
1002 MockServiceRefPtr s_not_in_profile(
1003 new NiceMock<MockService>(control_interface(),
1004 dispatcher(),
1005 metrics(),
1006 manager()));
1007 MockServiceRefPtr s_not_in_group(
1008 new NiceMock<MockService>(control_interface(),
1009 dispatcher(),
1010 metrics(),
1011 manager()));
1012 MockServiceRefPtr s_configure_fail(
1013 new NiceMock<MockService>(control_interface(),
1014 dispatcher(),
1015 metrics(),
1016 manager()));
1017 MockServiceRefPtr s_configure_succeed(
1018 new NiceMock<MockService>(control_interface(),
1019 dispatcher(),
1020 metrics(),
1021 manager()));
1022
1023 string entry_name("entry_name");
1024 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
1025 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
1026 .WillRepeatedly(Return("not_entry_name"));
1027 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
1028 .WillRepeatedly(Return(entry_name));
1029 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
1030 .WillRepeatedly(Return(entry_name));
1031
1032 manager()->RegisterService(s_not_in_profile);
1033 manager()->RegisterService(s_not_in_group);
1034 manager()->RegisterService(s_configure_fail);
1035 manager()->RegisterService(s_configure_succeed);
1036
1037 scoped_refptr<MockProfile> profile0(
1038 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1039 scoped_refptr<MockProfile> profile1(
1040 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1041
1042 s_not_in_group->set_profile(profile1);
1043 s_configure_fail->set_profile(profile1);
1044 s_configure_succeed->set_profile(profile1);
1045
1046 AdoptProfile(manager(), profile0);
1047 AdoptProfile(manager(), profile1);
1048
1049 // No services are a member of this profile.
1050 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1051
1052 // No services that are members of this profile have this entry name.
1053 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1054
1055 // Only services that are members of the profile and group will be abandoned.
1056 EXPECT_CALL(*profile1.get(),
1057 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1058 EXPECT_CALL(*profile1.get(),
1059 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1060 EXPECT_CALL(*profile1.get(),
1061 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1062 .WillOnce(Return(true));
1063 EXPECT_CALL(*profile1.get(),
1064 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1065 .WillOnce(Return(true));
1066
1067 // Never allow services to re-join profile1.
1068 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1069 .WillRepeatedly(Return(false));
1070
1071 // Only allow one of the members of the profile and group to successfully
1072 // join profile0.
1073 EXPECT_CALL(*profile0.get(),
1074 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1075 EXPECT_CALL(*profile0.get(),
1076 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1077 EXPECT_CALL(*profile0.get(),
1078 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1079 .WillOnce(Return(false));
1080 EXPECT_CALL(*profile0.get(),
1081 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1082 .WillOnce(Return(true));
1083
1084 // Expect the failed-to-configure service to have Unload() called on it.
1085 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1086 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1087 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1088 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1089
1090 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1091
1092 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1093 EXPECT_EQ(profile1, s_not_in_group->profile());
1094 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1095
1096 // Since we are using a MockProfile, the profile does not actually change,
1097 // since ConfigureService was not actually called on the service.
1098 EXPECT_EQ(profile1, s_configure_succeed->profile());
1099}
1100
Paul Stewart65512e12012-03-26 18:01:08 -07001101TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1102 MockServiceRefPtr s_will_remove0(
1103 new NiceMock<MockService>(control_interface(),
1104 dispatcher(),
1105 metrics(),
1106 manager()));
1107 MockServiceRefPtr s_will_remove1(
1108 new NiceMock<MockService>(control_interface(),
1109 dispatcher(),
1110 metrics(),
1111 manager()));
1112 MockServiceRefPtr s_will_not_remove0(
1113 new NiceMock<MockService>(control_interface(),
1114 dispatcher(),
1115 metrics(),
1116 manager()));
1117 MockServiceRefPtr s_will_not_remove1(
1118 new NiceMock<MockService>(control_interface(),
1119 dispatcher(),
1120 metrics(),
1121 manager()));
1122
1123 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1124 .Times(4); // Once for each registration.
1125
1126 string entry_name("entry_name");
1127 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1128 .WillRepeatedly(Return(entry_name));
1129 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1130 .WillRepeatedly(Return(entry_name));
1131 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1132 .WillRepeatedly(Return(entry_name));
1133 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1134 .WillRepeatedly(Return(entry_name));
1135
1136 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001137 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001138 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001139 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001140 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001141 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001142 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001143 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001144
1145 // One for each service added above.
1146 ASSERT_EQ(4, manager()->services_.size());
1147
1148 scoped_refptr<MockProfile> profile(
1149 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1150
1151 s_will_remove0->set_profile(profile);
1152 s_will_remove1->set_profile(profile);
1153 s_will_not_remove0->set_profile(profile);
1154 s_will_not_remove1->set_profile(profile);
1155
1156 AdoptProfile(manager(), profile);
1157
1158 // Deny any of the services re-entry to the profile.
1159 EXPECT_CALL(*profile, ConfigureService(_))
1160 .WillRepeatedly(Return(false));
1161
1162 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1163 .WillOnce(Return(true));
1164 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1165 .WillOnce(Return(true));
1166 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1167 .WillOnce(Return(true));
1168 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1169 .WillOnce(Return(true));
1170
1171 EXPECT_CALL(*s_will_remove0, Unload())
1172 .WillOnce(Return(true));
1173 EXPECT_CALL(*s_will_remove1, Unload())
1174 .WillOnce(Return(true));
1175 EXPECT_CALL(*s_will_not_remove0, Unload())
1176 .WillOnce(Return(false));
1177 EXPECT_CALL(*s_will_not_remove1, Unload())
1178 .WillOnce(Return(false));
1179
1180
1181 // This will cause all the profiles to be unloaded.
1182 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1183
1184 // 2 of the 4 services added above should have been unregistered and
1185 // removed, leaving 2.
1186 EXPECT_EQ(2, manager()->services_.size());
1187 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1188 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1189}
1190
1191TEST_F(ManagerTest, PopProfileWithUnload) {
1192 MockServiceRefPtr s_will_remove0(
1193 new NiceMock<MockService>(control_interface(),
1194 dispatcher(),
1195 metrics(),
1196 manager()));
1197 MockServiceRefPtr s_will_remove1(
1198 new NiceMock<MockService>(control_interface(),
1199 dispatcher(),
1200 metrics(),
1201 manager()));
1202 MockServiceRefPtr s_will_not_remove0(
1203 new NiceMock<MockService>(control_interface(),
1204 dispatcher(),
1205 metrics(),
1206 manager()));
1207 MockServiceRefPtr s_will_not_remove1(
1208 new NiceMock<MockService>(control_interface(),
1209 dispatcher(),
1210 metrics(),
1211 manager()));
1212
1213 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1214 .Times(5); // Once for each registration, and one after profile pop.
1215
1216 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001217 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001218 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001219 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001220 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001221 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001222 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001223 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001224
1225 // One for each service added above.
1226 ASSERT_EQ(4, manager()->services_.size());
1227
1228 scoped_refptr<MockProfile> profile0(
1229 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1230 scoped_refptr<MockProfile> profile1(
1231 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1232
1233 s_will_remove0->set_profile(profile1);
1234 s_will_remove1->set_profile(profile1);
1235 s_will_not_remove0->set_profile(profile1);
1236 s_will_not_remove1->set_profile(profile1);
1237
1238 AdoptProfile(manager(), profile0);
1239 AdoptProfile(manager(), profile1);
1240
1241 // Deny any of the services entry to profile0, so they will all be unloaded.
1242 EXPECT_CALL(*profile0, ConfigureService(_))
1243 .WillRepeatedly(Return(false));
1244
1245 EXPECT_CALL(*s_will_remove0, Unload())
1246 .WillOnce(Return(true));
1247 EXPECT_CALL(*s_will_remove1, Unload())
1248 .WillOnce(Return(true));
1249 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001250 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001251 EXPECT_CALL(*s_will_not_remove1, Unload())
1252 .WillOnce(Return(false));
1253
1254 // This will pop profile1, which should cause all our profiles to unload.
1255 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001256 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001257
1258 // 2 of the 4 services added above should have been unregistered and
1259 // removed, leaving 2.
1260 EXPECT_EQ(2, manager()->services_.size());
1261 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1262 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001263
1264 // Expect the unloaded services to lose their profile reference.
1265 EXPECT_FALSE(s_will_remove0->profile());
1266 EXPECT_FALSE(s_will_remove1->profile());
1267
1268 // If we explicitly deregister a service, the effect should be the same
1269 // with respect to the profile reference.
1270 ASSERT_TRUE(s_will_not_remove0->profile());
1271 manager()->DeregisterService(s_will_not_remove0);
1272 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001273}
1274
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001275TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001276 {
1277 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001278 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1279 flimflam::kOfflineModeProperty,
1280 PropertyStoreTest::kBoolV,
1281 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001282 }
1283 {
1284 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001285 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1286 flimflam::kCountryProperty,
1287 PropertyStoreTest::kStringV,
1288 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001289 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001290 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001291 {
1292 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001293 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1294 flimflam::kCountryProperty,
1295 PropertyStoreTest::kBoolV,
1296 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001297 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001298 }
1299 {
1300 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001301 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1302 flimflam::kOfflineModeProperty,
1303 PropertyStoreTest::kStringV,
1304 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001305 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001306 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001307 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001308 {
1309 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001310 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001311 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001312 flimflam::kEnabledTechnologiesProperty,
1313 PropertyStoreTest::kStringsV,
1314 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001315 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001316 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001317}
1318
mukesh agrawal32399322011-09-01 10:53:43 -07001319TEST_F(ManagerTest, RequestScan) {
1320 {
1321 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001322 manager()->RegisterDevice(mock_devices_[0].get());
1323 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001324 EXPECT_CALL(*mock_devices_[0], technology())
1325 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001326 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001327 EXPECT_CALL(*mock_devices_[1], technology())
1328 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001329 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001330 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001331 }
1332
1333 {
1334 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001335 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001336 EXPECT_EQ(Error::kInvalidArguments, error.type());
1337 }
1338}
1339
Darin Petkovb65c2452012-02-23 15:17:06 +01001340TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001341 KeyValueStore args;
1342 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001343 manager()->GetService(args, &e);
1344 EXPECT_EQ(Error::kInvalidArguments, e.type());
1345 EXPECT_EQ("must specify service type", e.message());
1346}
1347
1348TEST_F(ManagerTest, GetServiceUnknownType) {
1349 KeyValueStore args;
1350 Error e;
1351 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1352 manager()->GetService(args, &e);
1353 EXPECT_EQ(Error::kNotSupported, e.type());
1354 EXPECT_EQ("service type is unsupported", e.message());
1355}
1356
1357TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1358 KeyValueStore args;
1359 Error e;
1360 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1361 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001362 EXPECT_EQ(Error::kInvalidArguments, e.type());
1363 EXPECT_EQ("no wifi devices available", e.message());
1364}
1365
Darin Petkovb65c2452012-02-23 15:17:06 +01001366TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001367 KeyValueStore args;
1368 Error e;
1369 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001370 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001371 manager()->RegisterDevice(mock_wifi_);
1372 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1373 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001374 manager()->GetService(args, &e);
1375 EXPECT_TRUE(e.IsSuccess());
1376}
1377
Darin Petkov33af05c2012-02-28 10:10:30 +01001378TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1379 KeyValueStore args;
1380 Error e;
1381 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001382 scoped_refptr<MockProfile> profile(
1383 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1384 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001385 ServiceRefPtr service = manager()->GetService(args, &e);
1386 EXPECT_EQ(Error::kNotSupported, e.type());
1387 EXPECT_FALSE(service);
1388}
1389
Darin Petkovb65c2452012-02-23 15:17:06 +01001390TEST_F(ManagerTest, GetServiceVPN) {
1391 KeyValueStore args;
1392 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001393 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001394 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001395 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1396 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001397 scoped_refptr<MockProfile> profile(
1398 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1399 AdoptProfile(manager(), profile);
1400 ServiceRefPtr updated_service;
1401 EXPECT_CALL(*profile, UpdateService(_))
1402 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1403 ServiceRefPtr configured_service;
1404 EXPECT_CALL(*profile, ConfigureService(_))
1405 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001406 ServiceRefPtr service = manager()->GetService(args, &e);
1407 EXPECT_TRUE(e.IsSuccess());
1408 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001409 EXPECT_EQ(service, updated_service);
1410 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001411}
1412
Darin Petkovc63dcf02012-05-24 11:51:43 +02001413TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1414 KeyValueStore args;
1415 Error e;
1416 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1417 ServiceRefPtr service = manager()->GetService(args, &e);
1418 EXPECT_EQ(Error::kInvalidArguments, e.type());
1419 EXPECT_EQ("Missing WiMAX network id.", e.message());
1420 EXPECT_FALSE(service);
1421}
1422
Darin Petkovd1cd7972012-05-22 15:26:15 +02001423TEST_F(ManagerTest, GetServiceWiMax) {
1424 KeyValueStore args;
1425 Error e;
1426 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001427 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1428 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1429 ServiceRefPtr service = manager()->GetService(args, &e);
1430 EXPECT_TRUE(e.IsSuccess());
1431 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001432}
1433
Paul Stewart7f61e522012-03-22 11:13:45 -07001434TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1435 // Manager calls ActiveProfile() so we need at least one profile installed.
1436 scoped_refptr<MockProfile> profile(
1437 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1438 AdoptProfile(manager(), profile);
1439
1440 KeyValueStore args;
1441 args.SetString(flimflam::kProfileProperty, "xxx");
1442 Error error;
1443 manager()->ConfigureService(args, &error);
1444 EXPECT_EQ(Error::kInvalidArguments, error.type());
1445 EXPECT_EQ("Invalid profile name xxx", error.message());
1446}
1447
1448TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1449 // Manager calls ActiveProfile() so we need at least one profile installed.
1450 scoped_refptr<MockProfile> profile(
1451 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1452 AdoptProfile(manager(), profile);
1453
1454 KeyValueStore args;
1455 Error error;
1456 manager()->ConfigureService(args, &error);
1457 EXPECT_EQ(Error::kInvalidArguments, error.type());
1458 EXPECT_EQ("must specify service type", error.message());
1459}
1460
1461// A registered service in the ephemeral profile should be moved to the
1462// active profile as a part of configuration if no profile was explicitly
1463// specified.
1464TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1465 scoped_refptr<MockProfile> profile(
1466 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1467
1468 AdoptProfile(manager(), profile); // This is now the active profile.
1469
1470 const std::vector<uint8_t> ssid;
1471 scoped_refptr<MockWiFiService> service(
1472 new NiceMock<MockWiFiService>(control_interface(),
1473 dispatcher(),
1474 metrics(),
1475 manager(),
1476 mock_wifi_,
1477 ssid,
1478 "",
1479 "",
1480 false));
1481
1482 manager()->RegisterService(service);
1483 service->set_profile(GetEphemeralProfile(manager()));
1484
1485 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1486 // the same device as that used above causes a refcounting loop.
1487 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1488 dispatcher(),
1489 metrics(),
1490 manager(),
1491 "wifi1",
1492 "addr5",
1493 5));
1494 manager()->RegisterDevice(wifi);
1495 EXPECT_CALL(*wifi, GetService(_, _))
1496 .WillOnce(Return(service));
1497 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1498 .WillOnce(Return(true));
1499 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1500 .WillOnce(Return(true));
1501
1502 KeyValueStore args;
1503 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1504 Error error;
1505 manager()->ConfigureService(args, &error);
1506 EXPECT_TRUE(error.IsSuccess());
1507}
1508
1509// If were configure a service that was already registered and explicitly
1510// specify a profile, it should be moved from the profile it was previously
1511// in to the specified profile if one was requested.
1512TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1513 scoped_refptr<MockProfile> profile0(
1514 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1515 scoped_refptr<MockProfile> profile1(
1516 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1517
1518 const string kProfileName0 = "profile0";
1519 const string kProfileName1 = "profile1";
1520
1521 EXPECT_CALL(*profile0, GetRpcIdentifier())
1522 .WillRepeatedly(Return(kProfileName0));
1523 EXPECT_CALL(*profile1, GetRpcIdentifier())
1524 .WillRepeatedly(Return(kProfileName1));
1525
1526 AdoptProfile(manager(), profile0);
1527 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1528
1529 const std::vector<uint8_t> ssid;
1530 scoped_refptr<MockWiFiService> service(
1531 new NiceMock<MockWiFiService>(control_interface(),
1532 dispatcher(),
1533 metrics(),
1534 manager(),
1535 mock_wifi_,
1536 ssid,
1537 "",
1538 "",
1539 false));
1540
1541 manager()->RegisterService(service);
1542 service->set_profile(profile1);
1543
1544 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1545 // the same device as that used above causes a refcounting loop.
1546 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1547 dispatcher(),
1548 metrics(),
1549 manager(),
1550 "wifi1",
1551 "addr5",
1552 5));
1553 manager()->RegisterDevice(wifi);
1554 EXPECT_CALL(*wifi, GetService(_, _))
1555 .WillOnce(Return(service));
1556 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1557 .WillOnce(Return(true));
1558 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1559 .WillOnce(Return(true));
1560 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1561 .WillOnce(Return(true));
1562
1563 KeyValueStore args;
1564 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1565 args.SetString(flimflam::kProfileProperty, kProfileName0);
1566 Error error;
1567 manager()->ConfigureService(args, &error);
1568 EXPECT_TRUE(error.IsSuccess());
1569 service->set_profile(NULL); // Breaks refcounting loop.
1570}
1571
1572// An unregistered service should remain unregistered, but its contents should
1573// be saved to the specified profile nonetheless.
1574TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1575 scoped_refptr<MockProfile> profile0(
1576 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1577 scoped_refptr<MockProfile> profile1(
1578 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1579
1580 const string kProfileName0 = "profile0";
1581 const string kProfileName1 = "profile1";
1582
1583 EXPECT_CALL(*profile0, GetRpcIdentifier())
1584 .WillRepeatedly(Return(kProfileName0));
1585 EXPECT_CALL(*profile1, GetRpcIdentifier())
1586 .WillRepeatedly(Return(kProfileName1));
1587
1588 AdoptProfile(manager(), profile0);
1589 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1590
1591 const std::vector<uint8_t> ssid;
1592 scoped_refptr<MockWiFiService> service(
1593 new NiceMock<MockWiFiService>(control_interface(),
1594 dispatcher(),
1595 metrics(),
1596 manager(),
1597 mock_wifi_,
1598 ssid,
1599 "",
1600 "",
1601 false));
1602
1603 service->set_profile(profile1);
1604
1605 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1606 // the same device as that used above causes a refcounting loop.
1607 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1608 dispatcher(),
1609 metrics(),
1610 manager(),
1611 "wifi1",
1612 "addr5",
1613 5));
1614 manager()->RegisterDevice(wifi);
1615 EXPECT_CALL(*wifi, GetService(_, _))
1616 .WillOnce(Return(service));
1617 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1618 .WillOnce(Return(true));
1619 EXPECT_CALL(*profile0, AdoptService(_))
1620 .Times(0);
1621 EXPECT_CALL(*profile1, AdoptService(_))
1622 .Times(0);
1623
1624 KeyValueStore args;
1625 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1626 args.SetString(flimflam::kProfileProperty, kProfileName0);
1627 Error error;
1628 manager()->ConfigureService(args, &error);
1629 EXPECT_TRUE(error.IsSuccess());
1630}
1631
Paul Stewart22aa71b2011-09-16 12:15:11 -07001632TEST_F(ManagerTest, TechnologyOrder) {
1633 Error error;
1634 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1635 string(flimflam::kTypeWifi), &error);
1636 ASSERT_TRUE(error.IsSuccess());
1637 EXPECT_EQ(manager()->GetTechnologyOrder(),
1638 string(flimflam::kTypeEthernet) + "," +
1639 string(flimflam::kTypeWifi));
1640
1641 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1642 string(flimflam::kTypeWifi), &error);
1643 ASSERT_FALSE(error.IsSuccess());
1644 EXPECT_EQ(Error::kInvalidArguments, error.type());
1645 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1646 string(flimflam::kTypeWifi),
1647 manager()->GetTechnologyOrder());
1648}
1649
1650TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001651 // TODO(quiche): Some of these tests would probably fit better in
1652 // service_unittest, since the actual comparison of Services is
1653 // implemented in Service. (crosbug.com/23370)
1654
Paul Stewart22aa71b2011-09-16 12:15:11 -07001655 scoped_refptr<MockService> mock_service0(
1656 new NiceMock<MockService>(control_interface(),
1657 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001658 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001659 manager()));
1660 scoped_refptr<MockService> mock_service1(
1661 new NiceMock<MockService>(control_interface(),
1662 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001663 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001664 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001665
1666 manager()->RegisterService(mock_service0);
1667 manager()->RegisterService(mock_service1);
1668
1669 // Services should already be sorted by UniqueName
1670 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1671
1672 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001673 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001674 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1675
1676 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001677 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001678 manager()->UpdateService(mock_service1);
1679 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1680
1681 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001682 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001683 manager()->UpdateService(mock_service0);
1684 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1685
1686 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001687 EXPECT_CALL(*mock_service0.get(), technology())
1688 .WillRepeatedly(Return((Technology::kWifi)));
1689 EXPECT_CALL(*mock_service1.get(), technology())
1690 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001691
1692 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001693 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001694 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001695 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1696
1697 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1698 string(flimflam::kTypeEthernet), &error);
1699 EXPECT_TRUE(error.IsSuccess());
1700 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1701
Gaurav Shah435de2c2011-11-17 19:01:07 -08001702 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001703 mock_service0->set_priority(1);
1704 manager()->UpdateService(mock_service0);
1705 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1706
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001707 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001708 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001709 manager()->UpdateService(mock_service1);
1710 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1711
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001712 // Auto-connect.
1713 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001714 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001715 mock_service1->set_auto_connect(false);
1716 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001717 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1718
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001719 // Connectable.
1720 mock_service1->set_connectable(true);
1721 manager()->UpdateService(mock_service1);
1722 mock_service0->set_connectable(false);
1723 manager()->UpdateService(mock_service0);
1724 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1725
1726 // IsFailed.
1727 EXPECT_CALL(*mock_service0.get(), state())
1728 .WillRepeatedly(Return(Service::kStateIdle));
1729 EXPECT_CALL(*mock_service0.get(), IsFailed())
1730 .WillRepeatedly(Return(false));
1731 manager()->UpdateService(mock_service0);
1732 EXPECT_CALL(*mock_service0.get(), state())
1733 .WillRepeatedly(Return(Service::kStateFailure));
1734 EXPECT_CALL(*mock_service1.get(), IsFailed())
1735 .WillRepeatedly(Return(true));
1736 manager()->UpdateService(mock_service1);
1737 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1738
1739 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001740 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001741 .WillRepeatedly(Return(Service::kStateAssociating));
1742 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001743 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001744 manager()->UpdateService(mock_service1);
1745 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1746
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001747 // Connected.
1748 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001749 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001750 EXPECT_CALL(*mock_service0.get(), IsConnected())
1751 .WillRepeatedly(Return(true));
1752 manager()->UpdateService(mock_service0);
1753 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1754
Paul Stewarta121c442012-06-09 14:12:58 -07001755 // Portal.
1756 EXPECT_CALL(*mock_service1.get(), state())
1757 .WillRepeatedly(Return(Service::kStateConnected));
1758 EXPECT_CALL(*mock_service1.get(), IsConnected())
1759 .WillRepeatedly(Return(true));
1760 manager()->UpdateService(mock_service1);
1761 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1762
Paul Stewart22aa71b2011-09-16 12:15:11 -07001763 manager()->DeregisterService(mock_service0);
1764 manager()->DeregisterService(mock_service1);
1765}
1766
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001767TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001768 MockMetrics mock_metrics;
1769 manager()->set_metrics(&mock_metrics);
1770
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001771 scoped_refptr<MockService> mock_service0(
1772 new NiceMock<MockService>(control_interface(),
1773 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001774 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001775 manager()));
1776 scoped_refptr<MockService> mock_service1(
1777 new NiceMock<MockService>(control_interface(),
1778 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001779 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001780 manager()));
1781
1782 scoped_refptr<MockConnection> mock_connection0(
1783 new NiceMock<MockConnection>(device_info_.get()));
1784 scoped_refptr<MockConnection> mock_connection1(
1785 new NiceMock<MockConnection>(device_info_.get()));
1786
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001787 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001788 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001789 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001790 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001791 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001792 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001793
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001794 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001795 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001796
1797 mock_service1->set_priority(1);
1798 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001799 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001800
1801 mock_service1->set_priority(0);
1802 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001803 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001804
Paul Stewartce4ec192012-03-14 12:53:46 -07001805 mock_service0->set_mock_connection(mock_connection0);
1806 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001807
1808 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001809 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001810 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001811
Darin Petkova5e07ef2012-07-09 14:27:57 +02001812 ServiceWatcher service_watcher;
1813 int tag =
1814 manager()->RegisterDefaultServiceCallback(
1815 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1816 service_watcher.AsWeakPtr()));
1817 EXPECT_EQ(1, tag);
1818
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001819 mock_service1->set_priority(1);
1820 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1821 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001822 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001823 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001824 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001825
Darin Petkova5e07ef2012-07-09 14:27:57 +02001826 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001827 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001828 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001829 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001830 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001831 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001832 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001833
Paul Stewartce4ec192012-03-14 12:53:46 -07001834 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001835 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001836 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001837 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001838
1839 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001840 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001841}
1842
Darin Petkova5e07ef2012-07-09 14:27:57 +02001843TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1844 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1845 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1846
1847 MockMetrics mock_metrics;
1848 manager()->set_metrics(&mock_metrics);
1849
1850 scoped_refptr<MockService> mock_service(
1851 new NiceMock<MockService>(
1852 control_interface(), dispatcher(), metrics(), manager()));
1853 ServiceRefPtr service = mock_service;
1854 ServiceRefPtr null_service;
1855
1856 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1857 manager()->NotifyDefaultServiceChanged(null_service);
1858
1859 ServiceWatcher service_watcher1;
1860 ServiceWatcher service_watcher2;
1861 int tag1 =
1862 manager()->RegisterDefaultServiceCallback(
1863 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1864 service_watcher1.AsWeakPtr()));
1865 EXPECT_EQ(1, tag1);
1866 int tag2 =
1867 manager()->RegisterDefaultServiceCallback(
1868 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1869 service_watcher2.AsWeakPtr()));
1870 EXPECT_EQ(2, tag2);
1871
1872 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1873 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1874 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1875 manager()->NotifyDefaultServiceChanged(null_service);
1876
1877 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1878 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1879 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1880 manager()->NotifyDefaultServiceChanged(mock_service);
1881
1882 manager()->DeregisterDefaultServiceCallback(tag1);
1883 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1884 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1885 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1886 manager()->NotifyDefaultServiceChanged(mock_service);
1887 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1888
1889 manager()->DeregisterDefaultServiceCallback(tag2);
1890 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1891 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1892 manager()->NotifyDefaultServiceChanged(mock_service);
1893
1894 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1895 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1896}
1897
Gaurav Shah435de2c2011-11-17 19:01:07 -08001898TEST_F(ManagerTest, AvailableTechnologies) {
1899 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1900 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001901 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001902 manager(),
1903 "null4",
1904 "addr4",
1905 0));
1906 manager()->RegisterDevice(mock_devices_[0]);
1907 manager()->RegisterDevice(mock_devices_[1]);
1908 manager()->RegisterDevice(mock_devices_[2]);
1909 manager()->RegisterDevice(mock_devices_[3]);
1910
1911 ON_CALL(*mock_devices_[0].get(), technology())
1912 .WillByDefault(Return(Technology::kEthernet));
1913 ON_CALL(*mock_devices_[1].get(), technology())
1914 .WillByDefault(Return(Technology::kWifi));
1915 ON_CALL(*mock_devices_[2].get(), technology())
1916 .WillByDefault(Return(Technology::kCellular));
1917 ON_CALL(*mock_devices_[3].get(), technology())
1918 .WillByDefault(Return(Technology::kWifi));
1919
1920 set<string> expected_technologies;
1921 expected_technologies.insert(Technology::NameFromIdentifier(
1922 Technology::kEthernet));
1923 expected_technologies.insert(Technology::NameFromIdentifier(
1924 Technology::kWifi));
1925 expected_technologies.insert(Technology::NameFromIdentifier(
1926 Technology::kCellular));
1927 Error error;
1928 vector<string> technologies = manager()->AvailableTechnologies(&error);
1929
1930 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1931 ContainerEq(expected_technologies));
1932}
1933
1934TEST_F(ManagerTest, ConnectedTechnologies) {
1935 scoped_refptr<MockService> connected_service1(
1936 new NiceMock<MockService>(control_interface(),
1937 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001938 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001939 manager()));
1940 scoped_refptr<MockService> connected_service2(
1941 new NiceMock<MockService>(control_interface(),
1942 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001943 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001944 manager()));
1945 scoped_refptr<MockService> disconnected_service1(
1946 new NiceMock<MockService>(control_interface(),
1947 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001948 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001949 manager()));
1950 scoped_refptr<MockService> disconnected_service2(
1951 new NiceMock<MockService>(control_interface(),
1952 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001953 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001954 manager()));
1955
1956 ON_CALL(*connected_service1.get(), IsConnected())
1957 .WillByDefault(Return(true));
1958 ON_CALL(*connected_service2.get(), IsConnected())
1959 .WillByDefault(Return(true));
1960
1961 manager()->RegisterService(connected_service1);
1962 manager()->RegisterService(connected_service2);
1963 manager()->RegisterService(disconnected_service1);
1964 manager()->RegisterService(disconnected_service2);
1965
1966 manager()->RegisterDevice(mock_devices_[0]);
1967 manager()->RegisterDevice(mock_devices_[1]);
1968 manager()->RegisterDevice(mock_devices_[2]);
1969 manager()->RegisterDevice(mock_devices_[3]);
1970
1971 ON_CALL(*mock_devices_[0].get(), technology())
1972 .WillByDefault(Return(Technology::kEthernet));
1973 ON_CALL(*mock_devices_[1].get(), technology())
1974 .WillByDefault(Return(Technology::kWifi));
1975 ON_CALL(*mock_devices_[2].get(), technology())
1976 .WillByDefault(Return(Technology::kCellular));
1977 ON_CALL(*mock_devices_[3].get(), technology())
1978 .WillByDefault(Return(Technology::kWifi));
1979
1980 mock_devices_[0]->SelectService(connected_service1);
1981 mock_devices_[1]->SelectService(disconnected_service1);
1982 mock_devices_[2]->SelectService(disconnected_service2);
1983 mock_devices_[3]->SelectService(connected_service2);
1984
1985 set<string> expected_technologies;
1986 expected_technologies.insert(Technology::NameFromIdentifier(
1987 Technology::kEthernet));
1988 expected_technologies.insert(Technology::NameFromIdentifier(
1989 Technology::kWifi));
1990 Error error;
1991
1992 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1993 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1994 ContainerEq(expected_technologies));
1995}
1996
1997TEST_F(ManagerTest, DefaultTechnology) {
1998 scoped_refptr<MockService> connected_service(
1999 new NiceMock<MockService>(control_interface(),
2000 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002001 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002002 manager()));
2003 scoped_refptr<MockService> disconnected_service(
2004 new NiceMock<MockService>(control_interface(),
2005 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002006 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08002007 manager()));
2008
2009 // Connected. WiFi.
2010 ON_CALL(*connected_service.get(), IsConnected())
2011 .WillByDefault(Return(true));
2012 ON_CALL(*connected_service.get(), state())
2013 .WillByDefault(Return(Service::kStateConnected));
2014 ON_CALL(*connected_service.get(), technology())
2015 .WillByDefault(Return(Technology::kWifi));
2016
2017 // Disconnected. Ethernet.
2018 ON_CALL(*disconnected_service.get(), technology())
2019 .WillByDefault(Return(Technology::kEthernet));
2020
2021 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002022 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002023 Error error;
2024 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
2025
2026
2027 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07002028 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08002029 // Connected service should be brought to the front now.
2030 string expected_technology =
2031 Technology::NameFromIdentifier(Technology::kWifi);
2032 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
2033}
2034
Paul Stewart212d60f2012-07-12 10:59:13 -07002035TEST_F(ManagerTest, Stop) {
2036 scoped_refptr<MockProfile> profile(
2037 new NiceMock<MockProfile>(control_interface(), manager(), ""));
2038 AdoptProfile(manager(), profile);
2039 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00002040 new NiceMock<MockService>(control_interface(),
2041 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002042 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00002043 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07002044 manager()->RegisterService(service);
2045 manager()->RegisterDevice(mock_devices_[0]);
2046 EXPECT_CALL(*profile.get(),
2047 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2048 .WillOnce(Return(true));
2049 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2050 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002051 manager()->Stop();
2052}
2053
mukesh agrawal00917ce2011-11-22 23:56:55 +00002054TEST_F(ManagerTest, UpdateServiceConnected) {
2055 scoped_refptr<MockService> mock_service(
2056 new NiceMock<MockService>(control_interface(),
2057 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002058 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002059 manager()));
2060 manager()->RegisterService(mock_service);
2061 EXPECT_FALSE(mock_service->favorite());
2062 EXPECT_FALSE(mock_service->auto_connect());
2063
Gaurav Shah435de2c2011-11-17 19:01:07 -08002064 EXPECT_CALL(*mock_service.get(), IsConnected())
2065 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002066 manager()->UpdateService(mock_service);
2067 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2068 // to mock out MakeFavorite. And mocking that out would break the
2069 // SortServices test. (crosbug.com/23370)
2070 EXPECT_TRUE(mock_service->favorite());
2071 EXPECT_TRUE(mock_service->auto_connect());
2072}
2073
Thieu Led4e9e552012-02-16 16:26:07 -08002074TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2075 // This tests the case where the user connects to a service that is
2076 // currently associated with a profile. We want to make sure that the
2077 // favorite flag is set and that the flag is saved to the current
2078 // profile.
2079 scoped_refptr<MockService> mock_service(
2080 new NiceMock<MockService>(control_interface(),
2081 dispatcher(),
2082 metrics(),
2083 manager()));
2084 manager()->RegisterService(mock_service);
2085 EXPECT_FALSE(mock_service->favorite());
2086 EXPECT_FALSE(mock_service->auto_connect());
2087
Gary Moraind93615e2012-04-27 11:50:03 -07002088 scoped_refptr<MockProfile> profile(
2089 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002090
Gary Moraind93615e2012-04-27 11:50:03 -07002091 mock_service->set_profile(profile);
2092 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002093 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002094 EXPECT_CALL(*profile,
2095 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002096 manager()->UpdateService(mock_service);
2097 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2098 // to mock out MakeFavorite. And mocking that out would break the
2099 // SortServices test. (crosbug.com/23370)
2100 EXPECT_TRUE(mock_service->favorite());
2101 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002102 // This releases the ref on the mock profile.
2103 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002104}
2105
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002106TEST_F(ManagerTest, SaveSuccessfulService) {
2107 scoped_refptr<MockProfile> profile(
2108 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2109 AdoptProfile(manager(), profile);
2110 scoped_refptr<MockService> service(
2111 new NiceMock<MockService>(control_interface(),
2112 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002113 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002114 manager()));
2115
2116 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2117 ServiceRefPtr expect_service(service.get());
2118
2119 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2120 .WillOnce(Return(false));
2121 manager()->RegisterService(service);
2122
2123 EXPECT_CALL(*service.get(), state())
2124 .WillRepeatedly(Return(Service::kStateConnected));
2125 EXPECT_CALL(*service.get(), IsConnected())
2126 .WillRepeatedly(Return(true));
2127 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2128 .WillOnce(Return(true));
2129 manager()->UpdateService(service);
2130}
2131
Darin Petkove7c6ad32012-06-29 10:22:09 +02002132TEST_F(ManagerTest, UpdateDevice) {
2133 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2134 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2135 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2136 AdoptProfile(manager(), profile0); // Passes ownership.
2137 AdoptProfile(manager(), profile1); // Passes ownership.
2138 AdoptProfile(manager(), profile2); // Passes ownership.
2139 DeviceRefPtr device_ref(mock_devices_[0].get());
2140 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2141 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2142 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2143 manager()->UpdateDevice(mock_devices_[0]);
2144}
2145
Paul Stewart1b253142012-01-26 14:05:52 -08002146TEST_F(ManagerTest, EnumerateProfiles) {
2147 vector<string> profile_paths;
2148 for (size_t i = 0; i < 10; i++) {
2149 scoped_refptr<MockProfile> profile(
2150 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002151 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002152 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2153 .WillOnce(Return(profile_paths.back()));
2154 AdoptProfile(manager(), profile);
2155 }
2156
2157 Error error;
2158 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2159 EXPECT_TRUE(error.IsSuccess());
2160 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2161 for (size_t i = 0; i < profile_paths.size(); i++) {
2162 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2163 }
2164}
2165
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002166TEST_F(ManagerTest, AutoConnectOnRegister) {
2167 MockServiceRefPtr service = MakeAutoConnectableService();
2168 EXPECT_CALL(*service.get(), AutoConnect());
2169 manager()->RegisterService(service);
2170 dispatcher()->DispatchPendingEvents();
2171}
2172
2173TEST_F(ManagerTest, AutoConnectOnUpdate) {
2174 MockServiceRefPtr service1 = MakeAutoConnectableService();
2175 service1->set_priority(1);
2176 MockServiceRefPtr service2 = MakeAutoConnectableService();
2177 service2->set_priority(2);
2178 manager()->RegisterService(service1);
2179 manager()->RegisterService(service2);
2180 dispatcher()->DispatchPendingEvents();
2181
2182 EXPECT_CALL(*service1.get(), AutoConnect());
2183 EXPECT_CALL(*service2.get(), state())
2184 .WillRepeatedly(Return(Service::kStateFailure));
2185 EXPECT_CALL(*service2.get(), IsFailed())
2186 .WillRepeatedly(Return(true));
2187 EXPECT_CALL(*service2.get(), IsConnected())
2188 .WillRepeatedly(Return(false));
2189 manager()->UpdateService(service2);
2190 dispatcher()->DispatchPendingEvents();
2191}
2192
2193TEST_F(ManagerTest, AutoConnectOnDeregister) {
2194 MockServiceRefPtr service1 = MakeAutoConnectableService();
2195 service1->set_priority(1);
2196 MockServiceRefPtr service2 = MakeAutoConnectableService();
2197 service2->set_priority(2);
2198 manager()->RegisterService(service1);
2199 manager()->RegisterService(service2);
2200 dispatcher()->DispatchPendingEvents();
2201
2202 EXPECT_CALL(*service1.get(), AutoConnect());
2203 manager()->DeregisterService(service2);
2204 dispatcher()->DispatchPendingEvents();
2205}
2206
Darin Petkov3ec55342012-09-28 14:04:44 +02002207TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) {
2208 MockServiceRefPtr service = MakeAutoConnectableService();
2209 SetPowerState(PowerManagerProxyDelegate::kSuspending);
2210 SetPowerManager();
2211 EXPECT_CALL(*service, AutoConnect()).Times(0);
2212 manager()->RegisterService(service);
2213 dispatcher()->DispatchPendingEvents();
2214}
2215
Darin Petkovca621542012-07-25 14:25:56 +02002216TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2217 MockServiceRefPtr service = MakeAutoConnectableService();
2218 SetPowerState(PowerManagerProxyDelegate::kMem);
2219 SetPowerManager();
2220 EXPECT_CALL(*service, AutoConnect()).Times(0);
2221 manager()->RegisterService(service);
2222 dispatcher()->DispatchPendingEvents();
2223}
2224
2225TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2226 MockServiceRefPtr service = MakeAutoConnectableService();
2227 SetPowerState(PowerManagerProxyDelegate::kOn);
2228 SetPowerManager();
2229 EXPECT_CALL(*service, AutoConnect());
2230 manager()->RegisterService(service);
2231 dispatcher()->DispatchPendingEvents();
2232}
2233
2234TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2235 MockServiceRefPtr service = MakeAutoConnectableService();
2236 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2237 SetPowerManager();
2238 EXPECT_CALL(*service, AutoConnect());
2239 manager()->RegisterService(service);
2240 dispatcher()->DispatchPendingEvents();
2241}
2242
2243TEST_F(ManagerTest, OnPowerStateChanged) {
2244 MockServiceRefPtr service = MakeAutoConnectableService();
2245 SetPowerState(PowerManagerProxyDelegate::kOn);
2246 SetPowerManager();
2247 EXPECT_CALL(*service, AutoConnect());
2248 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002249 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002250 dispatcher()->DispatchPendingEvents();
2251
mukesh agrawal784566d2012-08-08 18:32:58 -07002252 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002253 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2254 EXPECT_CALL(*service, AutoConnect());
2255 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002256 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002257
mukesh agrawal784566d2012-08-08 18:32:58 -07002258 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002259 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2260 EXPECT_CALL(*service, AutoConnect()).Times(0);
2261 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002262 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002263}
2264
Darin Petkov3ec55342012-09-28 14:04:44 +02002265TEST_F(ManagerTest, AddTerminationAction) {
2266 EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _));
2267 EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_));
2268 SetPowerManager();
2269 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2270 manager()->AddTerminationAction("action1", base::Closure());
2271 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2272 manager()->AddTerminationAction("action2", base::Closure());
2273}
2274
2275TEST_F(ManagerTest, RemoveTerminationAction) {
2276 static const char kKey1[] = "action1";
2277 static const char kKey2[] = "action2";
2278
2279 MockPowerManager &power_manager = *power_manager_;
2280 SetPowerManager();
2281
2282 // Removing an action when the hook table is empty should not result in any
2283 // calls to the power manager.
2284 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2285 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2286 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2287 manager()->RemoveTerminationAction("unknown");
2288 Mock::VerifyAndClearExpectations(&power_manager);
2289
2290 manager()->AddTerminationAction(kKey1, base::Closure());
2291 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2292 manager()->AddTerminationAction(kKey2, base::Closure());
2293
2294 // Removing an action that ends up with a non-empty hook table should not
2295 // result in any calls to the power manager.
2296 EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0);
2297 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0);
2298 manager()->RemoveTerminationAction(kKey1);
2299 EXPECT_FALSE(GetTerminationActions()->IsEmpty());
2300 Mock::VerifyAndClearExpectations(&power_manager);
2301
2302 // Removing the last action should trigger unregistering from the power
2303 // manager.
2304 EXPECT_CALL(power_manager, UnregisterSuspendDelay());
2305 EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_));
2306 manager()->RemoveTerminationAction(kKey2);
2307 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2308}
2309
2310TEST_F(ManagerTest, RunTerminationActions) {
2311 TerminationActionTest test_action;
2312 const string kActionName = "action";
2313
2314 EXPECT_CALL(test_action, Done(_));
2315 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2316 test_action.AsWeakPtr()));
2317
2318 manager()->AddTerminationAction(TerminationActionTest::kActionName,
2319 Bind(&TerminationActionTest::Action,
2320 test_action.AsWeakPtr()));
2321 test_action.set_manager(manager());
2322 EXPECT_CALL(test_action, Done(_));
2323 manager()->RunTerminationActions(Bind(&TerminationActionTest::Done,
2324 test_action.AsWeakPtr()));
2325}
2326
2327TEST_F(ManagerTest, OnSuspendDelay) {
2328 const uint32 kSeqNumber = 123;
2329 EXPECT_TRUE(GetTerminationActions()->IsEmpty());
2330 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2331 SetPowerManager();
2332 OnSuspendDelay(kSeqNumber);
2333}
2334
2335TEST_F(ManagerTest, OnSuspendActionsComplete) {
2336 const uint32 kSeqNumber = 54321;
2337 Error error;
2338 EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber));
2339 SetPowerManager();
2340 OnSuspendActionsComplete(kSeqNumber, error);
2341}
2342
Paul Stewartc681fa02012-03-02 19:40:04 -08002343TEST_F(ManagerTest, RecheckPortal) {
2344 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2345 .WillOnce(Return(false));
2346 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2347 .WillOnce(Return(true));
2348 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2349 .Times(0);
2350
2351 manager()->RegisterDevice(mock_devices_[0]);
2352 manager()->RegisterDevice(mock_devices_[1]);
2353 manager()->RegisterDevice(mock_devices_[2]);
2354
2355 manager()->RecheckPortal(NULL);
2356}
2357
Paul Stewartd215af62012-04-24 23:25:50 -07002358TEST_F(ManagerTest, RecheckPortalOnService) {
2359 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2360 dispatcher(),
2361 metrics(),
2362 manager());
2363 EXPECT_CALL(*mock_devices_[0].get(),
2364 IsConnectedToService(IsRefPtrTo(service)))
2365 .WillOnce(Return(false));
2366 EXPECT_CALL(*mock_devices_[1].get(),
2367 IsConnectedToService(IsRefPtrTo(service)))
2368 .WillOnce(Return(true));
2369 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2370 .WillOnce(Return(true));
2371 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2372 .Times(0);
2373
2374 manager()->RegisterDevice(mock_devices_[0]);
2375 manager()->RegisterDevice(mock_devices_[1]);
2376 manager()->RegisterDevice(mock_devices_[2]);
2377
2378 manager()->RecheckPortalOnService(service);
2379}
2380
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002381TEST_F(ManagerTest, GetDefaultService) {
2382 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002383 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002384
2385 scoped_refptr<MockService> mock_service(
2386 new NiceMock<MockService>(control_interface(),
2387 dispatcher(),
2388 metrics(),
2389 manager()));
2390
2391 manager()->RegisterService(mock_service);
2392 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002393 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002394
2395 scoped_refptr<MockConnection> mock_connection(
2396 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002397 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002398 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002399 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002400
Paul Stewartce4ec192012-03-14 12:53:46 -07002401 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002402 manager()->DeregisterService(mock_service);
2403}
2404
Paul Stewart13ed2252012-03-21 12:52:46 -07002405TEST_F(ManagerTest, GetServiceWithGUID) {
2406 scoped_refptr<MockService> mock_service0(
2407 new NiceMock<MockService>(control_interface(),
2408 dispatcher(),
2409 metrics(),
2410 manager()));
2411
2412 scoped_refptr<MockService> mock_service1(
2413 new NiceMock<MockService>(control_interface(),
2414 dispatcher(),
2415 metrics(),
2416 manager()));
2417
Paul Stewartcb59fed2012-03-21 21:14:46 -07002418 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2419 .Times(0);
2420 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2421 .Times(0);
2422
Paul Stewart13ed2252012-03-21 12:52:46 -07002423 manager()->RegisterService(mock_service0);
2424 manager()->RegisterService(mock_service1);
2425
2426 const string kGUID0 = "GUID0";
2427 const string kGUID1 = "GUID1";
2428
2429 {
2430 Error error;
2431 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2432 EXPECT_FALSE(error.IsSuccess());
2433 EXPECT_FALSE(service);
2434 }
2435
2436 KeyValueStore args;
2437 args.SetString(flimflam::kGuidProperty, kGUID1);
2438
2439 {
2440 Error error;
2441 ServiceRefPtr service = manager()->GetService(args, &error);
2442 EXPECT_EQ(Error::kInvalidArguments, error.type());
2443 EXPECT_FALSE(service);
2444 }
2445
2446 mock_service0->set_guid(kGUID0);
2447 mock_service1->set_guid(kGUID1);
2448
2449 {
2450 Error error;
2451 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2452 EXPECT_TRUE(error.IsSuccess());
2453 EXPECT_EQ(mock_service0.get(), service.get());
2454 }
2455
2456 {
2457 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002458 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2459 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002460 ServiceRefPtr service = manager()->GetService(args, &error);
2461 EXPECT_TRUE(error.IsSuccess());
2462 EXPECT_EQ(mock_service1.get(), service.get());
2463 }
2464
2465 manager()->DeregisterService(mock_service0);
2466 manager()->DeregisterService(mock_service1);
2467}
2468
Gary Morain028545d2012-04-07 14:55:52 -07002469
2470TEST_F(ManagerTest, CalculateStateOffline) {
2471 MockMetrics mock_metrics;
2472 manager()->set_metrics(&mock_metrics);
2473 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2474 .Times(AnyNumber());
2475 scoped_refptr<MockService> mock_service0(
2476 new NiceMock<MockService>(control_interface(),
2477 dispatcher(),
2478 metrics(),
2479 manager()));
2480
2481 scoped_refptr<MockService> mock_service1(
2482 new NiceMock<MockService>(control_interface(),
2483 dispatcher(),
2484 metrics(),
2485 manager()));
2486
2487 EXPECT_CALL(*mock_service0.get(), IsConnected())
2488 .WillRepeatedly(Return(false));
2489 EXPECT_CALL(*mock_service1.get(), IsConnected())
2490 .WillRepeatedly(Return(false));
2491
2492 manager()->RegisterService(mock_service0);
2493 manager()->RegisterService(mock_service1);
2494
2495 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2496
2497 manager()->DeregisterService(mock_service0);
2498 manager()->DeregisterService(mock_service1);
2499}
2500
2501TEST_F(ManagerTest, CalculateStateOnline) {
2502 MockMetrics mock_metrics;
2503 manager()->set_metrics(&mock_metrics);
2504 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2505 .Times(AnyNumber());
2506 scoped_refptr<MockService> mock_service0(
2507 new NiceMock<MockService>(control_interface(),
2508 dispatcher(),
2509 metrics(),
2510 manager()));
2511
2512 scoped_refptr<MockService> mock_service1(
2513 new NiceMock<MockService>(control_interface(),
2514 dispatcher(),
2515 metrics(),
2516 manager()));
2517
2518 EXPECT_CALL(*mock_service0.get(), IsConnected())
2519 .WillRepeatedly(Return(false));
2520 EXPECT_CALL(*mock_service1.get(), IsConnected())
2521 .WillRepeatedly(Return(true));
2522 EXPECT_CALL(*mock_service0.get(), state())
2523 .WillRepeatedly(Return(Service::kStateIdle));
2524 EXPECT_CALL(*mock_service1.get(), state())
2525 .WillRepeatedly(Return(Service::kStateConnected));
2526
2527 manager()->RegisterService(mock_service0);
2528 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002529 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002530
2531 EXPECT_EQ("online", manager()->CalculateState(NULL));
2532
2533 manager()->DeregisterService(mock_service0);
2534 manager()->DeregisterService(mock_service1);
2535}
2536
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002537TEST_F(ManagerTest, StartupPortalList) {
2538 // Simulate loading value from the default profile.
2539 const string kProfileValue("wifi,vpn");
2540 manager()->props_.check_portal_list = kProfileValue;
2541
2542 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2543 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2544 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2545
2546 const string kStartupValue("cellular,ethernet");
2547 manager()->SetStartupPortalList(kStartupValue);
2548 // Ensure profile value is not overwritten, so when we save the default
2549 // profile, the correct value will still be written.
2550 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2551
2552 // However we should read back a different list.
2553 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2554 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2555 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2556
2557 const string kRuntimeValue("ppp");
2558 // Setting a runtime value over the control API should overwrite both
2559 // the profile value and what we read back.
2560 Error error;
2561 manager()->mutable_store()->SetStringProperty(
2562 flimflam::kCheckPortalListProperty,
2563 kRuntimeValue,
2564 &error);
2565 ASSERT_TRUE(error.IsSuccess());
2566 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2567 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2568 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2569 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2570}
2571
Paul Stewart036dba02012-08-07 12:34:41 -07002572TEST_F(ManagerTest, LinkMonitorEnabled) {
2573 const string kEnabledTechnologies("wifi,vpn");
2574 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2575 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2576 EXPECT_FALSE(
2577 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2578}
2579
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002580TEST_F(ManagerTest, EnableTechnology) {
2581 Error error(Error::kOperationInitiated);
2582 ResultCallback callback;
2583 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2584 EXPECT_TRUE(error.IsSuccess());
2585
Joshua Krollda798622012-06-05 12:30:48 -07002586 ON_CALL(*mock_devices_[0], technology())
2587 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002588
2589 manager()->RegisterDevice(mock_devices_[0]);
2590
2591 // Device is enabled, so expect operation is successful.
2592 mock_devices_[0]->enabled_ = true;
2593 error.Populate(Error::kOperationInitiated);
2594 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2595 EXPECT_TRUE(error.IsSuccess());
2596
2597 // Device is disabled, so expect operation in progress.
2598 mock_devices_[0]->enabled_ = false;
2599 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2600 error.Populate(Error::kOperationInitiated);
2601 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2602 EXPECT_TRUE(error.IsOngoing());
2603}
2604
2605TEST_F(ManagerTest, DisableTechnology) {
2606 Error error(Error::kOperationInitiated);
2607 ResultCallback callback;
2608 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2609 EXPECT_TRUE(error.IsSuccess());
2610
Joshua Krollda798622012-06-05 12:30:48 -07002611 ON_CALL(*mock_devices_[0], technology())
2612 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002613
2614 manager()->RegisterDevice(mock_devices_[0]);
2615
2616 // Device is disabled, so expect operation is successful.
2617 error.Populate(Error::kOperationInitiated);
2618 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2619 EXPECT_TRUE(error.IsSuccess());
2620
2621 // Device is enabled, so expect operation in progress.
2622 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2623 mock_devices_[0]->enabled_ = true;
2624 error.Populate(Error::kOperationInitiated);
2625 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2626 EXPECT_TRUE(error.IsOngoing());
2627}
2628
Paul Stewart4d5efb72012-09-17 12:24:34 -07002629TEST_F(ManagerTest, IgnoredSearchList) {
2630 scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>());
2631 SetResolver(resolver.get());
2632 vector<string> ignored_paths;
2633 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2634 SetIgnoredDNSSearchPaths("");
2635 EXPECT_EQ("", GetIgnoredDNSSearchPaths());
2636
2637 const string kIgnored0 = "chromium.org";
2638 ignored_paths.push_back(kIgnored0);
2639 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2640 SetIgnoredDNSSearchPaths(kIgnored0);
2641 EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths());
2642
2643 const string kIgnored1 = "google.com";
2644 const string kIgnoredSum = kIgnored0 + "," + kIgnored1;
2645 ignored_paths.push_back(kIgnored1);
2646 EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths));
2647 SetIgnoredDNSSearchPaths(kIgnoredSum);
2648 EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths());
2649
2650 SetResolver(Resolver::GetInstance());
2651}
2652
Chris Masone9be4a9d2011-05-16 15:44:09 -07002653} // namespace shill