blob: 441cfe372e2b02700e941dc90bfded740b47d234 [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"
Chris Masone9be4a9d2011-05-16 15:44:09 -070036#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070037#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070039#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070040#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020041#include "shill/proxy_factory.h"
Chris Masone6515aab2011-10-12 16:19:09 -070042#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070043#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020044#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070045
46using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070047using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070048using std::string;
49using std::vector;
50
Chris Masone9be4a9d2011-05-16 15:44:09 -070051namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070052using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070053using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080054using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070055using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070056using ::testing::InSequence;
mukesh agrawal784566d2012-08-08 18:32:58 -070057using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070058using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070059using ::testing::NiceMock;
60using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070061using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070062using ::testing::SaveArg;
Gaurav Shah435de2c2011-11-17 19:01:07 -080063using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080064using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070065using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070066
Chris Masone3bd3c8c2011-06-13 08:20:26 -070067class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070068 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070069 ManagerTest()
Darin Petkovca621542012-07-25 14:25:56 +020070 : power_manager_(new MockPowerManager(&proxy_factory_)),
71 mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070072 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080073 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070074 manager(),
75 "wifi0",
76 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080077 4)),
78 device_info_(new NiceMock<MockDeviceInfo>(
79 control_interface(),
80 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080082 reinterpret_cast<Manager*>(NULL))),
83 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070084 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
85 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080086 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070087 manager(),
88 "null0",
89 "addr0",
90 0));
91 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
92 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080093 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070094 manager(),
95 "null1",
96 "addr1",
97 1));
98 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
99 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800100 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700101 manager(),
102 "null2",
103 "addr2",
104 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800105 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
106 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800108 manager(),
109 "null3",
110 "addr3",
111 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700112 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800113
114 // Replace the manager's adaptor with a quieter one, and one
115 // we can do EXPECT*() against. Passes ownership.
116 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700117 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700118 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700119
Paul Stewartfdd16072011-09-16 12:41:35 -0700120 bool IsDeviceRegistered(const DeviceRefPtr &device,
121 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700122 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700123 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700124 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700125 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700126 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700127
Paul Stewarta849a3d2011-11-03 05:54:09 -0700128 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
129 manager->profiles_.push_back(profile);
130 }
131
Paul Stewart75225512012-01-26 22:51:33 -0800132 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
133 return manager->ephemeral_profile_;
134 }
135
Chris Masone6515aab2011-10-12 16:19:09 -0700136 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
137 Profile::Identifier id("rather", "irrelevant");
138 scoped_ptr<Profile> profile(new Profile(control_interface(),
139 manager,
140 id,
141 "",
142 false));
143 FilePath final_path(storage_path());
144 final_path = final_path.Append("test.profile");
145 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
146 storage->set_path(final_path);
147 if (!storage->Open())
148 return NULL;
149 profile->set_storage(storage.release()); // Passes ownership.
150 return profile.release();
151 }
152
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700153 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
154 const string &profile_identifier,
155 const string &service_name) {
156 GLib glib;
157 KeyFileStore store(&glib);
158 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
159 return store.Open() &&
160 store.SetString(service_name, "rather", "irrelevant") &&
161 store.Close();
162 }
163
164 Error::Type TestCreateProfile(Manager *manager, const string &name) {
165 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800166 string path;
167 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700168 return error.type();
169 }
170
171 Error::Type TestPopAnyProfile(Manager *manager) {
172 Error error;
173 manager->PopAnyProfile(&error);
174 return error.type();
175 }
176
177 Error::Type TestPopProfile(Manager *manager, const string &name) {
178 Error error;
179 manager->PopProfile(name, &error);
180 return error.type();
181 }
182
183 Error::Type TestPushProfile(Manager *manager, const string &name) {
184 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800185 string path;
186 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700187 return error.type();
188 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000189
Paul Stewartcb3eb892012-06-07 14:24:46 -0700190 void AddMockProfileToManager(Manager *manager) {
191 scoped_refptr<MockProfile> profile(
192 new MockProfile(control_interface(), manager, ""));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200193 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/"));
194 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700195 AdoptProfile(manager, profile);
196 }
197
Paul Stewartdfa46052012-06-26 09:44:14 -0700198 void CompleteServiceSort() {
199 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
200 dispatcher()->DispatchPendingEvents();
201 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
202 }
203
Paul Stewart49739c02012-08-08 17:24:03 -0700204 RpcIdentifier GetDefaultServiceRpcIdentifier() {
205 return manager()->GetDefaultServiceRpcIdentifier(NULL);
206 }
207
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700208 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000209 typedef scoped_refptr<MockService> MockServiceRefPtr;
210
Darin Petkova5e07ef2012-07-09 14:27:57 +0200211 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
212 public:
213 ServiceWatcher() {}
214 virtual ~ServiceWatcher() {}
215
216 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
217
218 private:
219 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
220 };
221
Darin Petkovca621542012-07-25 14:25:56 +0200222 class TestProxyFactory : public ProxyFactory {
223 public:
224 TestProxyFactory() {}
225
226 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
227 PowerManagerProxyDelegate */*delegate*/) {
228 return NULL;
229 }
230
231 private:
232 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
233 };
234
235 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
236 power_manager_->power_state_ = state;
237 }
238
239 void SetPowerManager() {
240 manager()->set_power_manager(power_manager_.release());
241 }
242
243 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
244 manager()->OnPowerStateChanged(state);
245 }
246
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000247 MockServiceRefPtr MakeAutoConnectableService() {
248 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
249 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800250 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000251 manager());
252 service->MakeFavorite();
253 service->set_connectable(true);
254 return service;
255 }
256
Darin Petkovca621542012-07-25 14:25:56 +0200257 TestProxyFactory proxy_factory_;
258 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700259 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700260 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800261 scoped_ptr<MockDeviceInfo> device_info_;
262
263 // This pointer is owned by the manager, and only tracked here for EXPECT*()
264 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700265};
266
Paul Stewart22aa71b2011-09-16 12:15:11 -0700267bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700268 if (!manager()->sort_services_task_.IsCancelled()) {
269 manager()->SortServicesTask();
270 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700271 return (svc0.get() == manager()->services_[0].get() &&
272 svc1.get() == manager()->services_[1].get());
273}
274
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700275TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700276 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
277 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700278}
279
Chris Masone9be4a9d2011-05-16 15:44:09 -0700280TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700281 ON_CALL(*mock_devices_[0].get(), technology())
282 .WillByDefault(Return(Technology::kEthernet));
283 ON_CALL(*mock_devices_[1].get(), technology())
284 .WillByDefault(Return(Technology::kWifi));
285 ON_CALL(*mock_devices_[2].get(), technology())
286 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700287
Paul Stewart22aa71b2011-09-16 12:15:11 -0700288 manager()->RegisterDevice(mock_devices_[0]);
289 manager()->RegisterDevice(mock_devices_[1]);
290 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700291
Paul Stewart22aa71b2011-09-16 12:15:11 -0700292 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
293 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
294 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700295}
296
Paul Stewarta41e38d2011-11-11 07:47:29 -0800297TEST_F(ManagerTest, DeviceRegistrationAndStart) {
298 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500299 mock_devices_[0]->enabled_persistent_ = true;
300 mock_devices_[1]->enabled_persistent_ = false;
301 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800302 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500303 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800304 .Times(0);
305 manager()->RegisterDevice(mock_devices_[0]);
306 manager()->RegisterDevice(mock_devices_[1]);
307}
308
309TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
310 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
311 DeviceRefPtr device_ref(mock_devices_[0].get());
312 AdoptProfile(manager(), profile); // Passes ownership.
313 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200314 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800315 manager()->RegisterDevice(mock_devices_[0]);
316}
317
Chris Masone9be4a9d2011-05-16 15:44:09 -0700318TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700319 ON_CALL(*mock_devices_[0].get(), technology())
320 .WillByDefault(Return(Technology::kEthernet));
321 ON_CALL(*mock_devices_[1].get(), technology())
322 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700323
Gaurav Shah435de2c2011-11-17 19:01:07 -0800324 manager()->RegisterDevice(mock_devices_[0]);
325 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700326
Paul Stewart22aa71b2011-09-16 12:15:11 -0700327 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
328 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700329
Paul Stewart212d60f2012-07-12 10:59:13 -0700330 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
331 AdoptProfile(manager(), profile); // Passes ownership.
332
Eric Shienbrood9a245532012-03-07 14:20:39 -0500333 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700334 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800335 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700336 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700337
Eric Shienbrood9a245532012-03-07 14:20:39 -0500338 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700339 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800340 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700341 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700342}
343
344TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700345 // It's much easier and safer to use a real GLib for this test.
346 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700347 Manager manager(control_interface(),
348 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800349 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700350 &glib,
351 run_path(),
352 storage_path(),
353 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700354 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
355 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700356 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700357
Chris Masone9be4a9d2011-05-16 15:44:09 -0700358 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700359 new NiceMock<MockService>(control_interface(),
360 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800361 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700362 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700363 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700364 new NiceMock<MockService>(control_interface(),
365 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800366 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700367 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700368
mukesh agrawal51a7e932011-07-27 16:18:26 -0700369 string service1_name(mock_service->UniqueName());
370 string service2_name(mock_service2->UniqueName());
371
372 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
373 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700374 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700375 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700376 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700377 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700378 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700379
Chris Masone9d779932011-08-25 16:33:41 -0700380 manager.RegisterService(mock_service);
381 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700382
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800383 Error error;
384 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700385 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700386 EXPECT_EQ(2, ids.size());
387 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
388 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700389
Chris Masone9d779932011-08-25 16:33:41 -0700390 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
391 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
392
393 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700394}
395
Chris Masone6515aab2011-10-12 16:19:09 -0700396TEST_F(ManagerTest, RegisterKnownService) {
397 // It's much easier and safer to use a real GLib for this test.
398 GLib glib;
399 Manager manager(control_interface(),
400 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800401 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700402 &glib,
403 run_path(),
404 storage_path(),
405 string());
406 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
407 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700408 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700409 {
410 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
411 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800412 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700413 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700414 ASSERT_TRUE(profile->AdoptService(service1));
415 ASSERT_TRUE(profile->ContainsService(service1));
416 } // Force destruction of service1.
417
418 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
419 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800420 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700421 &manager));
422 manager.RegisterService(service2);
423 EXPECT_EQ(service2->profile().get(), profile.get());
424 manager.Stop();
425}
426
427TEST_F(ManagerTest, RegisterUnknownService) {
428 // It's much easier and safer to use a real GLib for this test.
429 GLib glib;
430 Manager manager(control_interface(),
431 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800432 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700433 &glib,
434 run_path(),
435 storage_path(),
436 string());
437 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
438 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700439 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700440 {
441 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
442 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800443 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700444 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700445 ASSERT_TRUE(profile->AdoptService(service1));
446 ASSERT_TRUE(profile->ContainsService(service1));
447 } // Force destruction of service1.
448 scoped_refptr<MockService> mock_service2(
449 new NiceMock<MockService>(control_interface(),
450 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800451 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700452 &manager));
453 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
454 .WillRepeatedly(Return(mock_service2->UniqueName()));
455 manager.RegisterService(mock_service2);
456 EXPECT_NE(mock_service2->profile().get(), profile.get());
457 manager.Stop();
458}
459
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000460TEST_F(ManagerTest, DeregisterUnregisteredService) {
461 // WiFi assumes that it can deregister a service that is not
462 // registered. (E.g. a hidden service can be deregistered when it
463 // loses its last endpoint, and again when WiFi is Stop()-ed.)
464 //
465 // So test that doing so doesn't cause a crash.
466 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
467 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800468 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000469 manager());
470 manager()->DeregisterService(service);
471}
472
Chris Masonea8a2c252011-06-27 22:16:30 -0700473TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700474 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700475 map<string, ::DBus::Variant> props;
476 Error error(Error::kInvalidProperty, "");
477 {
478 ::DBus::Error dbus_error;
479 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700480 manager()->mutable_store()->SetStringProperty(
481 flimflam::kCheckPortalListProperty,
482 expected,
483 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700484 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700485 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
486 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
487 expected);
488 }
489 {
490 ::DBus::Error dbus_error;
491 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700492 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
493 expected,
494 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700495 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700496 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
497 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
498 expected);
499 }
500}
501
Chris Masone3c3f6a12011-07-01 10:01:41 -0700502TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700503 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800504 manager()->RegisterDevice(mock_devices_[0]);
505 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700506 {
507 map<string, ::DBus::Variant> props;
508 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700509 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700510 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700511 vector < ::DBus::Path> devices =
512 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700513 EXPECT_EQ(2, devices.size());
514 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700515}
516
mukesh agrawal2366eed2012-03-20 18:21:50 -0700517TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700518 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700519 map<string, ::DBus::Variant> props;
520 ::DBus::Error dbus_error;
521 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
522 map<string, ::DBus::Variant>::const_iterator prop =
523 props.find(flimflam::kServicesProperty);
524 ASSERT_FALSE(prop == props.end());
525 const ::DBus::Variant &variant = prop->second;
526 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
527}
528
Chris Masone6791a432011-07-12 13:23:19 -0700529TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700530 Manager manager(control_interface(),
531 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800532 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700533 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700534 run_path(),
535 storage_path(),
536 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700537 scoped_refptr<MockService> s2(new MockService(control_interface(),
538 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800539 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700540 &manager));
541 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700542 {
Chris Masone6515aab2011-10-12 16:19:09 -0700543 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700544 ProfileRefPtr profile(
545 new Profile(control_interface(), &manager, id, "", false));
546 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700547 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700548 .WillRepeatedly(Return(true));
549 EXPECT_CALL(*storage, Flush())
550 .Times(AnyNumber())
551 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700552 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700553 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700554 }
Chris Masone6515aab2011-10-12 16:19:09 -0700555 // Create a profile that already has |s2| in it.
556 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700557 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700558
Chris Masone6515aab2011-10-12 16:19:09 -0700559 // Now, move the Service |s2| to another profile.
560 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
561 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700562
563 // Force destruction of the original Profile, to ensure that the Service
564 // is kept alive and populated with data.
565 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700566 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700567 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700568}
569
Paul Stewart7f61e522012-03-22 11:13:45 -0700570TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
571 scoped_refptr<MockProfile> mock_profile(
572 new MockProfile(control_interface(), manager(), ""));
573 const string kProfileName("profile0");
574 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
575 .WillRepeatedly(Return(kProfileName));
576 AdoptProfile(manager(), mock_profile);
577
578 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
579 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
580 EXPECT_EQ(mock_profile.get(), profile.get());
581}
582
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800583TEST_F(ManagerTest, SetProfileForService) {
584 scoped_refptr<MockProfile> profile0(
585 new MockProfile(control_interface(), manager(), ""));
586 string profile_name0("profile0");
587 EXPECT_CALL(*profile0, GetRpcIdentifier())
588 .WillRepeatedly(Return(profile_name0));
589 AdoptProfile(manager(), profile0);
590 scoped_refptr<MockService> service(new MockService(control_interface(),
591 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800592 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800593 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700594 EXPECT_FALSE(manager()->HasService(service));
595 {
596 Error error;
597 EXPECT_CALL(*profile0, AdoptService(_))
598 .WillOnce(Return(true));
599 // Expect that setting the profile of a service that does not already
600 // have one assigned does not cause a crash.
601 manager()->SetProfileForService(service, "profile0", &error);
602 EXPECT_TRUE(error.IsSuccess());
603 }
604
605 // The service should be registered as a side-effect of the profile being
606 // set for this service.
607 EXPECT_TRUE(manager()->HasService(service));
608
609 // Since we have mocked Profile::AdoptServie() above, the service's
610 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800611 service->set_profile(profile0);
612
613 {
614 Error error;
615 manager()->SetProfileForService(service, "foo", &error);
616 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700617 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800618 }
619
620 {
621 Error error;
622 manager()->SetProfileForService(service, profile_name0, &error);
623 EXPECT_EQ(Error::kInvalidArguments, error.type());
624 EXPECT_EQ("Service is already connected to this profile", error.message());
625 }
626
627 scoped_refptr<MockProfile> profile1(
628 new MockProfile(control_interface(), manager(), ""));
629 string profile_name1("profile1");
630 EXPECT_CALL(*profile1, GetRpcIdentifier())
631 .WillRepeatedly(Return(profile_name1));
632 AdoptProfile(manager(), profile1);
633
634 {
635 Error error;
636 EXPECT_CALL(*profile1, AdoptService(_))
637 .WillOnce(Return(true));
638 EXPECT_CALL(*profile0, AbandonService(_))
639 .WillOnce(Return(true));
640 manager()->SetProfileForService(service, profile_name1, &error);
641 EXPECT_TRUE(error.IsSuccess());
642 }
643}
644
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700645TEST_F(ManagerTest, CreateProfile) {
646 // It's much easier to use real Glib here since we want the storage
647 // side-effects.
648 GLib glib;
649 ScopedTempDir temp_dir;
650 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
651
652 Manager manager(control_interface(),
653 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800654 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700655 &glib,
656 run_path(),
657 storage_path(),
658 temp_dir.path().value());
659
660 // Invalid name should be rejected.
661 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
662
Paul Stewartd0a3b812012-03-28 22:48:22 -0700663 // A profile with invalid characters in it should similarly be rejected.
664 EXPECT_EQ(Error::kInvalidArguments,
665 TestCreateProfile(&manager, "valid_profile"));
666
667 // We should be able to create a machine profile.
668 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700669
Gary Morainb672d352012-04-25 09:19:06 -0700670 // We should succeed in creating a valid user profile. Verify the returned
671 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700672 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700673 {
674 Error error;
675 string path;
676 manager.CreateProfile(kProfile, &path, &error);
677 EXPECT_EQ(Error::kSuccess, error.type());
678 EXPECT_EQ("/profile_rpc", path);
679 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700680
681 // We should fail in creating it a second time (already exists).
682 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
683}
684
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700685// We receive PopProfile when a user logs out, and it should always trigger a
686// MemoryLog Clear() call.
687TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) {
688 GLib glib;
689 ScopedTempDir temp_dir;
690 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
691 Manager manager(control_interface(),
692 dispatcher(),
693 metrics(),
694 &glib,
695 run_path(),
696 storage_path(),
697 temp_dir.path().value());
698 const char kProfile0[] = "~user/profile0";
699 const char kPurgedMessage[] = "This message should be purged";
700 // Create a profile and push it on the stack, leave one uncreated
701 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
702 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
703
704 // Popping a profile which isn't on top should still clear the log.
705 LOG(INFO) << kPurgedMessage;
706 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
707 kPurgedMessage));
708 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1"));
709 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
710 kPurgedMessage));
711
712 // Popping an invalid profile name should do the same thing.
713 LOG(INFO) << kPurgedMessage;
714 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
715 kPurgedMessage));
716 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
717 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
718 kPurgedMessage));
719
720 // Successful pops also purge the message log.
721 LOG(INFO) << kPurgedMessage;
722 EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText(
723 kPurgedMessage));
724 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0));
725 EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText(
726 kPurgedMessage));
727}
728
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700729TEST_F(ManagerTest, PushPopProfile) {
730 // It's much easier to use real Glib in creating a Manager for this
731 // test here since we want the storage side-effects.
732 GLib glib;
733 ScopedTempDir temp_dir;
734 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
735 Manager manager(control_interface(),
736 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800737 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700738 &glib,
739 run_path(),
740 storage_path(),
741 temp_dir.path().value());
742
743 // Pushing an invalid profile should fail.
744 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
745
Paul Stewartd0a3b812012-03-28 22:48:22 -0700746 // Pushing a default profile that does not exist should fail.
747 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700748
749 const char kProfile0[] = "~user/profile0";
750 const char kProfile1[] = "~user/profile1";
751
752 // Create a couple of profiles.
753 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
754 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
755
756 // Push these profiles on the stack.
757 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
758 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
759
760 // Pushing a profile a second time should fail.
761 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
762 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
763
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800764 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700765 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800766 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700767
768 // Make sure a profile name that doesn't exist fails.
769 const char kProfile2Id[] = "profile2";
770 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
771 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
772
773 // Create a new service, with a specific storage name.
774 scoped_refptr<MockService> service(
775 new NiceMock<MockService>(control_interface(),
776 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800777 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700778 &manager));
779 const char kServiceName[] = "service_storage_name";
780 EXPECT_CALL(*service.get(), GetStorageIdentifier())
781 .WillRepeatedly(Return(kServiceName));
782 EXPECT_CALL(*service.get(), Load(_))
783 .WillRepeatedly(Return(true));
784
785 // Add this service to the manager -- it should end up in the ephemeral
786 // profile.
787 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800788 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700789
790 // Create storage for a profile that contains the service storage name.
791 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
792 kServiceName));
793
794 // When we push the profile, the service should move away from the
795 // ephemeral profile to this new profile since it has an entry for
796 // this service.
797 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800798 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700799 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
800
801 // Insert another profile that should supersede ownership of the service.
802 const char kProfile3Id[] = "profile3";
803 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
804 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
805 kServiceName));
806 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
807 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
808
809 // Popping an invalid profile name should fail.
810 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
811
812 // Popping an profile that is not at the top of the stack should fail.
813 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
814
815 // Popping the top profile should succeed.
816 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
817
818 // Moreover the service should have switched profiles to profile 2.
819 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
820
821 // Popping the top profile should succeed.
822 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
823
824 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800825 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700826
827 // Pop the remaining two services off the stack.
828 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
829 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
830
831 // Next pop should fail with "stack is empty".
832 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700833
834 const char kMachineProfile0[] = "machineprofile0";
835 const char kMachineProfile1[] = "machineprofile1";
836 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
837 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
838
839 // Should be able to push a machine profile.
840 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
841
842 // Should be able to push a user profile atop a machine profile.
843 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
844
845 // Pushing a system-wide profile on top of a user profile should fail.
846 EXPECT_EQ(Error::kInvalidArguments,
847 TestPushProfile(&manager, kMachineProfile1));
848
849 // However if we pop the user profile, we should be able stack another
850 // machine profile on.
851 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
852 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700853}
854
Paul Stewarte73d05c2012-03-29 16:26:05 -0700855TEST_F(ManagerTest, RemoveProfile) {
856 // It's much easier to use real Glib in creating a Manager for this
857 // test here since we want the storage side-effects.
858 GLib glib;
859 ScopedTempDir temp_dir;
860 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
861 Manager manager(control_interface(),
862 dispatcher(),
863 metrics(),
864 &glib,
865 run_path(),
866 storage_path(),
867 temp_dir.path().value());
868
869 const char kProfile0[] = "profile0";
870 FilePath profile_path(
871 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
872
873 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
874 ASSERT_TRUE(file_util::PathExists(profile_path));
875
876 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
877
878 // Remove should fail since the profile is still on the stack.
879 {
880 Error error;
881 manager.RemoveProfile(kProfile0, &error);
882 EXPECT_EQ(Error::kInvalidArguments, error.type());
883 }
884
885 // Profile path should still exist.
886 EXPECT_TRUE(file_util::PathExists(profile_path));
887
888 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
889
890 // This should succeed now that the profile is off the stack.
891 {
892 Error error;
893 manager.RemoveProfile(kProfile0, &error);
894 EXPECT_EQ(Error::kSuccess, error.type());
895 }
896
897 // Profile path should no longer exist.
898 EXPECT_FALSE(file_util::PathExists(profile_path));
899
900 // Another remove succeeds, due to a foible in file_util::Delete --
901 // it is not an error to delete a file that does not exist.
902 {
903 Error error;
904 manager.RemoveProfile(kProfile0, &error);
905 EXPECT_EQ(Error::kSuccess, error.type());
906 }
907
908 // Let's create an error case that will "work". Create a non-empty
909 // directory in the place of the profile pathname.
910 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
911 {
912 Error error;
913 manager.RemoveProfile(kProfile0, &error);
914 EXPECT_EQ(Error::kOperationFailed, error.type());
915 }
916}
917
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700918TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
919 // It's much easier to use real Glib in creating a Manager for this
920 // test here since we want the storage side-effects.
921 GLib glib;
922 ScopedTempDir temp_dir;
923 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
924 Manager manager(control_interface(),
925 dispatcher(),
926 metrics(),
927 &glib,
928 run_path(),
929 storage_path(),
930 temp_dir.path().value());
931
932 const char kProfile0[] = "profile0";
933 FilePath profile_path(
934 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
935
936 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
937 ASSERT_TRUE(file_util::PathExists(profile_path));
938 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
939
940 // Ensure that even if the backing filestore is removed, we still can't
941 // create a profile twice.
942 ASSERT_TRUE(file_util::Delete(profile_path, false));
943 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
944}
945
Paul Stewart75225512012-01-26 22:51:33 -0800946// Use this matcher instead of passing RefPtrs directly into the arguments
947// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
948// system teardown.
949MATCHER_P(IsRefPtrTo, ref_address, "") {
950 return arg.get() == ref_address;
951}
952
953TEST_F(ManagerTest, HandleProfileEntryDeletion) {
954 MockServiceRefPtr s_not_in_profile(
955 new NiceMock<MockService>(control_interface(),
956 dispatcher(),
957 metrics(),
958 manager()));
959 MockServiceRefPtr s_not_in_group(
960 new NiceMock<MockService>(control_interface(),
961 dispatcher(),
962 metrics(),
963 manager()));
964 MockServiceRefPtr s_configure_fail(
965 new NiceMock<MockService>(control_interface(),
966 dispatcher(),
967 metrics(),
968 manager()));
969 MockServiceRefPtr s_configure_succeed(
970 new NiceMock<MockService>(control_interface(),
971 dispatcher(),
972 metrics(),
973 manager()));
974
975 string entry_name("entry_name");
976 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
977 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
978 .WillRepeatedly(Return("not_entry_name"));
979 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
980 .WillRepeatedly(Return(entry_name));
981 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
982 .WillRepeatedly(Return(entry_name));
983
984 manager()->RegisterService(s_not_in_profile);
985 manager()->RegisterService(s_not_in_group);
986 manager()->RegisterService(s_configure_fail);
987 manager()->RegisterService(s_configure_succeed);
988
989 scoped_refptr<MockProfile> profile0(
990 new StrictMock<MockProfile>(control_interface(), manager(), ""));
991 scoped_refptr<MockProfile> profile1(
992 new StrictMock<MockProfile>(control_interface(), manager(), ""));
993
994 s_not_in_group->set_profile(profile1);
995 s_configure_fail->set_profile(profile1);
996 s_configure_succeed->set_profile(profile1);
997
998 AdoptProfile(manager(), profile0);
999 AdoptProfile(manager(), profile1);
1000
1001 // No services are a member of this profile.
1002 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
1003
1004 // No services that are members of this profile have this entry name.
1005 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
1006
1007 // Only services that are members of the profile and group will be abandoned.
1008 EXPECT_CALL(*profile1.get(),
1009 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1010 EXPECT_CALL(*profile1.get(),
1011 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1012 EXPECT_CALL(*profile1.get(),
1013 AbandonService(IsRefPtrTo(s_configure_fail.get())))
1014 .WillOnce(Return(true));
1015 EXPECT_CALL(*profile1.get(),
1016 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
1017 .WillOnce(Return(true));
1018
1019 // Never allow services to re-join profile1.
1020 EXPECT_CALL(*profile1.get(), ConfigureService(_))
1021 .WillRepeatedly(Return(false));
1022
1023 // Only allow one of the members of the profile and group to successfully
1024 // join profile0.
1025 EXPECT_CALL(*profile0.get(),
1026 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
1027 EXPECT_CALL(*profile0.get(),
1028 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
1029 EXPECT_CALL(*profile0.get(),
1030 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
1031 .WillOnce(Return(false));
1032 EXPECT_CALL(*profile0.get(),
1033 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
1034 .WillOnce(Return(true));
1035
1036 // Expect the failed-to-configure service to have Unload() called on it.
1037 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
1038 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
1039 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
1040 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
1041
1042 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
1043
1044 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1045 EXPECT_EQ(profile1, s_not_in_group->profile());
1046 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1047
1048 // Since we are using a MockProfile, the profile does not actually change,
1049 // since ConfigureService was not actually called on the service.
1050 EXPECT_EQ(profile1, s_configure_succeed->profile());
1051}
1052
Paul Stewart65512e12012-03-26 18:01:08 -07001053TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1054 MockServiceRefPtr s_will_remove0(
1055 new NiceMock<MockService>(control_interface(),
1056 dispatcher(),
1057 metrics(),
1058 manager()));
1059 MockServiceRefPtr s_will_remove1(
1060 new NiceMock<MockService>(control_interface(),
1061 dispatcher(),
1062 metrics(),
1063 manager()));
1064 MockServiceRefPtr s_will_not_remove0(
1065 new NiceMock<MockService>(control_interface(),
1066 dispatcher(),
1067 metrics(),
1068 manager()));
1069 MockServiceRefPtr s_will_not_remove1(
1070 new NiceMock<MockService>(control_interface(),
1071 dispatcher(),
1072 metrics(),
1073 manager()));
1074
1075 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1076 .Times(4); // Once for each registration.
1077
1078 string entry_name("entry_name");
1079 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1080 .WillRepeatedly(Return(entry_name));
1081 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1082 .WillRepeatedly(Return(entry_name));
1083 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1084 .WillRepeatedly(Return(entry_name));
1085 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1086 .WillRepeatedly(Return(entry_name));
1087
1088 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001089 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001090 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001091 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001092 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001093 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001094 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001095 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001096
1097 // One for each service added above.
1098 ASSERT_EQ(4, manager()->services_.size());
1099
1100 scoped_refptr<MockProfile> profile(
1101 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1102
1103 s_will_remove0->set_profile(profile);
1104 s_will_remove1->set_profile(profile);
1105 s_will_not_remove0->set_profile(profile);
1106 s_will_not_remove1->set_profile(profile);
1107
1108 AdoptProfile(manager(), profile);
1109
1110 // Deny any of the services re-entry to the profile.
1111 EXPECT_CALL(*profile, ConfigureService(_))
1112 .WillRepeatedly(Return(false));
1113
1114 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1115 .WillOnce(Return(true));
1116 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1117 .WillOnce(Return(true));
1118 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1119 .WillOnce(Return(true));
1120 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1121 .WillOnce(Return(true));
1122
1123 EXPECT_CALL(*s_will_remove0, Unload())
1124 .WillOnce(Return(true));
1125 EXPECT_CALL(*s_will_remove1, Unload())
1126 .WillOnce(Return(true));
1127 EXPECT_CALL(*s_will_not_remove0, Unload())
1128 .WillOnce(Return(false));
1129 EXPECT_CALL(*s_will_not_remove1, Unload())
1130 .WillOnce(Return(false));
1131
1132
1133 // This will cause all the profiles to be unloaded.
1134 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1135
1136 // 2 of the 4 services added above should have been unregistered and
1137 // removed, leaving 2.
1138 EXPECT_EQ(2, manager()->services_.size());
1139 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1140 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1141}
1142
1143TEST_F(ManagerTest, PopProfileWithUnload) {
1144 MockServiceRefPtr s_will_remove0(
1145 new NiceMock<MockService>(control_interface(),
1146 dispatcher(),
1147 metrics(),
1148 manager()));
1149 MockServiceRefPtr s_will_remove1(
1150 new NiceMock<MockService>(control_interface(),
1151 dispatcher(),
1152 metrics(),
1153 manager()));
1154 MockServiceRefPtr s_will_not_remove0(
1155 new NiceMock<MockService>(control_interface(),
1156 dispatcher(),
1157 metrics(),
1158 manager()));
1159 MockServiceRefPtr s_will_not_remove1(
1160 new NiceMock<MockService>(control_interface(),
1161 dispatcher(),
1162 metrics(),
1163 manager()));
1164
1165 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1166 .Times(5); // Once for each registration, and one after profile pop.
1167
1168 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001169 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001170 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001171 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001172 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001173 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001174 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001175 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001176
1177 // One for each service added above.
1178 ASSERT_EQ(4, manager()->services_.size());
1179
1180 scoped_refptr<MockProfile> profile0(
1181 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1182 scoped_refptr<MockProfile> profile1(
1183 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1184
1185 s_will_remove0->set_profile(profile1);
1186 s_will_remove1->set_profile(profile1);
1187 s_will_not_remove0->set_profile(profile1);
1188 s_will_not_remove1->set_profile(profile1);
1189
1190 AdoptProfile(manager(), profile0);
1191 AdoptProfile(manager(), profile1);
1192
1193 // Deny any of the services entry to profile0, so they will all be unloaded.
1194 EXPECT_CALL(*profile0, ConfigureService(_))
1195 .WillRepeatedly(Return(false));
1196
1197 EXPECT_CALL(*s_will_remove0, Unload())
1198 .WillOnce(Return(true));
1199 EXPECT_CALL(*s_will_remove1, Unload())
1200 .WillOnce(Return(true));
1201 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001202 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001203 EXPECT_CALL(*s_will_not_remove1, Unload())
1204 .WillOnce(Return(false));
1205
1206 // This will pop profile1, which should cause all our profiles to unload.
1207 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001208 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001209
1210 // 2 of the 4 services added above should have been unregistered and
1211 // removed, leaving 2.
1212 EXPECT_EQ(2, manager()->services_.size());
1213 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1214 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001215
1216 // Expect the unloaded services to lose their profile reference.
1217 EXPECT_FALSE(s_will_remove0->profile());
1218 EXPECT_FALSE(s_will_remove1->profile());
1219
1220 // If we explicitly deregister a service, the effect should be the same
1221 // with respect to the profile reference.
1222 ASSERT_TRUE(s_will_not_remove0->profile());
1223 manager()->DeregisterService(s_will_not_remove0);
1224 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001225}
1226
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001227TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001228 {
1229 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001230 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1231 flimflam::kOfflineModeProperty,
1232 PropertyStoreTest::kBoolV,
1233 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001234 }
1235 {
1236 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001237 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1238 flimflam::kCountryProperty,
1239 PropertyStoreTest::kStringV,
1240 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001241 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001242 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001243 {
1244 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001245 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1246 flimflam::kCountryProperty,
1247 PropertyStoreTest::kBoolV,
1248 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001249 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001250 }
1251 {
1252 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001253 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1254 flimflam::kOfflineModeProperty,
1255 PropertyStoreTest::kStringV,
1256 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001257 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001258 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001259 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001260 {
1261 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001262 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001263 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001264 flimflam::kEnabledTechnologiesProperty,
1265 PropertyStoreTest::kStringsV,
1266 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001267 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001268 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001269}
1270
mukesh agrawal32399322011-09-01 10:53:43 -07001271TEST_F(ManagerTest, RequestScan) {
1272 {
1273 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001274 manager()->RegisterDevice(mock_devices_[0].get());
1275 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001276 EXPECT_CALL(*mock_devices_[0], technology())
1277 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001278 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001279 EXPECT_CALL(*mock_devices_[1], technology())
1280 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001281 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001282 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001283 }
1284
1285 {
1286 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001287 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001288 EXPECT_EQ(Error::kInvalidArguments, error.type());
1289 }
1290}
1291
Darin Petkovb65c2452012-02-23 15:17:06 +01001292TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001293 KeyValueStore args;
1294 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001295 manager()->GetService(args, &e);
1296 EXPECT_EQ(Error::kInvalidArguments, e.type());
1297 EXPECT_EQ("must specify service type", e.message());
1298}
1299
1300TEST_F(ManagerTest, GetServiceUnknownType) {
1301 KeyValueStore args;
1302 Error e;
1303 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1304 manager()->GetService(args, &e);
1305 EXPECT_EQ(Error::kNotSupported, e.type());
1306 EXPECT_EQ("service type is unsupported", e.message());
1307}
1308
1309TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1310 KeyValueStore args;
1311 Error e;
1312 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1313 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001314 EXPECT_EQ(Error::kInvalidArguments, e.type());
1315 EXPECT_EQ("no wifi devices available", e.message());
1316}
1317
Darin Petkovb65c2452012-02-23 15:17:06 +01001318TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001319 KeyValueStore args;
1320 Error e;
1321 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001322 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001323 manager()->RegisterDevice(mock_wifi_);
1324 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1325 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001326 manager()->GetService(args, &e);
1327 EXPECT_TRUE(e.IsSuccess());
1328}
1329
Darin Petkov33af05c2012-02-28 10:10:30 +01001330TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1331 KeyValueStore args;
1332 Error e;
1333 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001334 scoped_refptr<MockProfile> profile(
1335 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1336 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001337 ServiceRefPtr service = manager()->GetService(args, &e);
1338 EXPECT_EQ(Error::kNotSupported, e.type());
1339 EXPECT_FALSE(service);
1340}
1341
Darin Petkovb65c2452012-02-23 15:17:06 +01001342TEST_F(ManagerTest, GetServiceVPN) {
1343 KeyValueStore args;
1344 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001345 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001346 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001347 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1348 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001349 scoped_refptr<MockProfile> profile(
1350 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1351 AdoptProfile(manager(), profile);
1352 ServiceRefPtr updated_service;
1353 EXPECT_CALL(*profile, UpdateService(_))
1354 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1355 ServiceRefPtr configured_service;
1356 EXPECT_CALL(*profile, ConfigureService(_))
1357 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001358 ServiceRefPtr service = manager()->GetService(args, &e);
1359 EXPECT_TRUE(e.IsSuccess());
1360 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001361 EXPECT_EQ(service, updated_service);
1362 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001363}
1364
Darin Petkovc63dcf02012-05-24 11:51:43 +02001365TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1366 KeyValueStore args;
1367 Error e;
1368 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1369 ServiceRefPtr service = manager()->GetService(args, &e);
1370 EXPECT_EQ(Error::kInvalidArguments, e.type());
1371 EXPECT_EQ("Missing WiMAX network id.", e.message());
1372 EXPECT_FALSE(service);
1373}
1374
Darin Petkovd1cd7972012-05-22 15:26:15 +02001375TEST_F(ManagerTest, GetServiceWiMax) {
1376 KeyValueStore args;
1377 Error e;
1378 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001379 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1380 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1381 ServiceRefPtr service = manager()->GetService(args, &e);
1382 EXPECT_TRUE(e.IsSuccess());
1383 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001384}
1385
Paul Stewart7f61e522012-03-22 11:13:45 -07001386TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1387 // Manager calls ActiveProfile() so we need at least one profile installed.
1388 scoped_refptr<MockProfile> profile(
1389 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1390 AdoptProfile(manager(), profile);
1391
1392 KeyValueStore args;
1393 args.SetString(flimflam::kProfileProperty, "xxx");
1394 Error error;
1395 manager()->ConfigureService(args, &error);
1396 EXPECT_EQ(Error::kInvalidArguments, error.type());
1397 EXPECT_EQ("Invalid profile name xxx", error.message());
1398}
1399
1400TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1401 // Manager calls ActiveProfile() so we need at least one profile installed.
1402 scoped_refptr<MockProfile> profile(
1403 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1404 AdoptProfile(manager(), profile);
1405
1406 KeyValueStore args;
1407 Error error;
1408 manager()->ConfigureService(args, &error);
1409 EXPECT_EQ(Error::kInvalidArguments, error.type());
1410 EXPECT_EQ("must specify service type", error.message());
1411}
1412
1413// A registered service in the ephemeral profile should be moved to the
1414// active profile as a part of configuration if no profile was explicitly
1415// specified.
1416TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1417 scoped_refptr<MockProfile> profile(
1418 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1419
1420 AdoptProfile(manager(), profile); // This is now the active profile.
1421
1422 const std::vector<uint8_t> ssid;
1423 scoped_refptr<MockWiFiService> service(
1424 new NiceMock<MockWiFiService>(control_interface(),
1425 dispatcher(),
1426 metrics(),
1427 manager(),
1428 mock_wifi_,
1429 ssid,
1430 "",
1431 "",
1432 false));
1433
1434 manager()->RegisterService(service);
1435 service->set_profile(GetEphemeralProfile(manager()));
1436
1437 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1438 // the same device as that used above causes a refcounting loop.
1439 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1440 dispatcher(),
1441 metrics(),
1442 manager(),
1443 "wifi1",
1444 "addr5",
1445 5));
1446 manager()->RegisterDevice(wifi);
1447 EXPECT_CALL(*wifi, GetService(_, _))
1448 .WillOnce(Return(service));
1449 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1450 .WillOnce(Return(true));
1451 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1452 .WillOnce(Return(true));
1453
1454 KeyValueStore args;
1455 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1456 Error error;
1457 manager()->ConfigureService(args, &error);
1458 EXPECT_TRUE(error.IsSuccess());
1459}
1460
1461// If were configure a service that was already registered and explicitly
1462// specify a profile, it should be moved from the profile it was previously
1463// in to the specified profile if one was requested.
1464TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1465 scoped_refptr<MockProfile> profile0(
1466 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1467 scoped_refptr<MockProfile> profile1(
1468 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1469
1470 const string kProfileName0 = "profile0";
1471 const string kProfileName1 = "profile1";
1472
1473 EXPECT_CALL(*profile0, GetRpcIdentifier())
1474 .WillRepeatedly(Return(kProfileName0));
1475 EXPECT_CALL(*profile1, GetRpcIdentifier())
1476 .WillRepeatedly(Return(kProfileName1));
1477
1478 AdoptProfile(manager(), profile0);
1479 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1480
1481 const std::vector<uint8_t> ssid;
1482 scoped_refptr<MockWiFiService> service(
1483 new NiceMock<MockWiFiService>(control_interface(),
1484 dispatcher(),
1485 metrics(),
1486 manager(),
1487 mock_wifi_,
1488 ssid,
1489 "",
1490 "",
1491 false));
1492
1493 manager()->RegisterService(service);
1494 service->set_profile(profile1);
1495
1496 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1497 // the same device as that used above causes a refcounting loop.
1498 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1499 dispatcher(),
1500 metrics(),
1501 manager(),
1502 "wifi1",
1503 "addr5",
1504 5));
1505 manager()->RegisterDevice(wifi);
1506 EXPECT_CALL(*wifi, GetService(_, _))
1507 .WillOnce(Return(service));
1508 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1509 .WillOnce(Return(true));
1510 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1511 .WillOnce(Return(true));
1512 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1513 .WillOnce(Return(true));
1514
1515 KeyValueStore args;
1516 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1517 args.SetString(flimflam::kProfileProperty, kProfileName0);
1518 Error error;
1519 manager()->ConfigureService(args, &error);
1520 EXPECT_TRUE(error.IsSuccess());
1521 service->set_profile(NULL); // Breaks refcounting loop.
1522}
1523
1524// An unregistered service should remain unregistered, but its contents should
1525// be saved to the specified profile nonetheless.
1526TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1527 scoped_refptr<MockProfile> profile0(
1528 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1529 scoped_refptr<MockProfile> profile1(
1530 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1531
1532 const string kProfileName0 = "profile0";
1533 const string kProfileName1 = "profile1";
1534
1535 EXPECT_CALL(*profile0, GetRpcIdentifier())
1536 .WillRepeatedly(Return(kProfileName0));
1537 EXPECT_CALL(*profile1, GetRpcIdentifier())
1538 .WillRepeatedly(Return(kProfileName1));
1539
1540 AdoptProfile(manager(), profile0);
1541 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1542
1543 const std::vector<uint8_t> ssid;
1544 scoped_refptr<MockWiFiService> service(
1545 new NiceMock<MockWiFiService>(control_interface(),
1546 dispatcher(),
1547 metrics(),
1548 manager(),
1549 mock_wifi_,
1550 ssid,
1551 "",
1552 "",
1553 false));
1554
1555 service->set_profile(profile1);
1556
1557 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1558 // the same device as that used above causes a refcounting loop.
1559 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1560 dispatcher(),
1561 metrics(),
1562 manager(),
1563 "wifi1",
1564 "addr5",
1565 5));
1566 manager()->RegisterDevice(wifi);
1567 EXPECT_CALL(*wifi, GetService(_, _))
1568 .WillOnce(Return(service));
1569 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1570 .WillOnce(Return(true));
1571 EXPECT_CALL(*profile0, AdoptService(_))
1572 .Times(0);
1573 EXPECT_CALL(*profile1, AdoptService(_))
1574 .Times(0);
1575
1576 KeyValueStore args;
1577 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1578 args.SetString(flimflam::kProfileProperty, kProfileName0);
1579 Error error;
1580 manager()->ConfigureService(args, &error);
1581 EXPECT_TRUE(error.IsSuccess());
1582}
1583
Paul Stewart22aa71b2011-09-16 12:15:11 -07001584TEST_F(ManagerTest, TechnologyOrder) {
1585 Error error;
1586 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1587 string(flimflam::kTypeWifi), &error);
1588 ASSERT_TRUE(error.IsSuccess());
1589 EXPECT_EQ(manager()->GetTechnologyOrder(),
1590 string(flimflam::kTypeEthernet) + "," +
1591 string(flimflam::kTypeWifi));
1592
1593 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1594 string(flimflam::kTypeWifi), &error);
1595 ASSERT_FALSE(error.IsSuccess());
1596 EXPECT_EQ(Error::kInvalidArguments, error.type());
1597 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1598 string(flimflam::kTypeWifi),
1599 manager()->GetTechnologyOrder());
1600}
1601
1602TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001603 // TODO(quiche): Some of these tests would probably fit better in
1604 // service_unittest, since the actual comparison of Services is
1605 // implemented in Service. (crosbug.com/23370)
1606
Paul Stewart22aa71b2011-09-16 12:15:11 -07001607 scoped_refptr<MockService> mock_service0(
1608 new NiceMock<MockService>(control_interface(),
1609 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001610 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001611 manager()));
1612 scoped_refptr<MockService> mock_service1(
1613 new NiceMock<MockService>(control_interface(),
1614 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001615 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001616 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001617
1618 manager()->RegisterService(mock_service0);
1619 manager()->RegisterService(mock_service1);
1620
1621 // Services should already be sorted by UniqueName
1622 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1623
1624 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001625 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001626 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1627
1628 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001629 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001630 manager()->UpdateService(mock_service1);
1631 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1632
1633 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001634 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001635 manager()->UpdateService(mock_service0);
1636 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1637
1638 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001639 EXPECT_CALL(*mock_service0.get(), technology())
1640 .WillRepeatedly(Return((Technology::kWifi)));
1641 EXPECT_CALL(*mock_service1.get(), technology())
1642 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001643
1644 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001645 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001646 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001647 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1648
1649 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1650 string(flimflam::kTypeEthernet), &error);
1651 EXPECT_TRUE(error.IsSuccess());
1652 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1653
Gaurav Shah435de2c2011-11-17 19:01:07 -08001654 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001655 mock_service0->set_priority(1);
1656 manager()->UpdateService(mock_service0);
1657 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1658
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001659 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001660 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001661 manager()->UpdateService(mock_service1);
1662 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1663
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001664 // Auto-connect.
1665 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001666 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001667 mock_service1->set_auto_connect(false);
1668 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001669 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1670
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001671 // Connectable.
1672 mock_service1->set_connectable(true);
1673 manager()->UpdateService(mock_service1);
1674 mock_service0->set_connectable(false);
1675 manager()->UpdateService(mock_service0);
1676 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1677
1678 // IsFailed.
1679 EXPECT_CALL(*mock_service0.get(), state())
1680 .WillRepeatedly(Return(Service::kStateIdle));
1681 EXPECT_CALL(*mock_service0.get(), IsFailed())
1682 .WillRepeatedly(Return(false));
1683 manager()->UpdateService(mock_service0);
1684 EXPECT_CALL(*mock_service0.get(), state())
1685 .WillRepeatedly(Return(Service::kStateFailure));
1686 EXPECT_CALL(*mock_service1.get(), IsFailed())
1687 .WillRepeatedly(Return(true));
1688 manager()->UpdateService(mock_service1);
1689 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1690
1691 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001692 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001693 .WillRepeatedly(Return(Service::kStateAssociating));
1694 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001695 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001696 manager()->UpdateService(mock_service1);
1697 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1698
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001699 // Connected.
1700 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001701 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001702 EXPECT_CALL(*mock_service0.get(), IsConnected())
1703 .WillRepeatedly(Return(true));
1704 manager()->UpdateService(mock_service0);
1705 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1706
Paul Stewarta121c442012-06-09 14:12:58 -07001707 // Portal.
1708 EXPECT_CALL(*mock_service1.get(), state())
1709 .WillRepeatedly(Return(Service::kStateConnected));
1710 EXPECT_CALL(*mock_service1.get(), IsConnected())
1711 .WillRepeatedly(Return(true));
1712 manager()->UpdateService(mock_service1);
1713 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1714
Paul Stewart22aa71b2011-09-16 12:15:11 -07001715 manager()->DeregisterService(mock_service0);
1716 manager()->DeregisterService(mock_service1);
1717}
1718
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001719TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001720 MockMetrics mock_metrics;
1721 manager()->set_metrics(&mock_metrics);
1722
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001723 scoped_refptr<MockService> mock_service0(
1724 new NiceMock<MockService>(control_interface(),
1725 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001726 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001727 manager()));
1728 scoped_refptr<MockService> mock_service1(
1729 new NiceMock<MockService>(control_interface(),
1730 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001731 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001732 manager()));
1733
1734 scoped_refptr<MockConnection> mock_connection0(
1735 new NiceMock<MockConnection>(device_info_.get()));
1736 scoped_refptr<MockConnection> mock_connection1(
1737 new NiceMock<MockConnection>(device_info_.get()));
1738
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001739 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001740 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001741 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001742 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001743 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001744 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001745
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001746 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001747 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001748
1749 mock_service1->set_priority(1);
1750 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001751 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001752
1753 mock_service1->set_priority(0);
1754 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001755 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001756
Paul Stewartce4ec192012-03-14 12:53:46 -07001757 mock_service0->set_mock_connection(mock_connection0);
1758 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001759
1760 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001761 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001762 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001763
Darin Petkova5e07ef2012-07-09 14:27:57 +02001764 ServiceWatcher service_watcher;
1765 int tag =
1766 manager()->RegisterDefaultServiceCallback(
1767 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1768 service_watcher.AsWeakPtr()));
1769 EXPECT_EQ(1, tag);
1770
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001771 mock_service1->set_priority(1);
1772 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1773 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001774 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001775 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001776 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001777
Darin Petkova5e07ef2012-07-09 14:27:57 +02001778 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001779 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001780 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001781 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001782 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001783 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001784 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001785
Paul Stewartce4ec192012-03-14 12:53:46 -07001786 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001787 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001788 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001789 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001790
1791 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001792 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001793}
1794
Darin Petkova5e07ef2012-07-09 14:27:57 +02001795TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1796 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1797 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1798
1799 MockMetrics mock_metrics;
1800 manager()->set_metrics(&mock_metrics);
1801
1802 scoped_refptr<MockService> mock_service(
1803 new NiceMock<MockService>(
1804 control_interface(), dispatcher(), metrics(), manager()));
1805 ServiceRefPtr service = mock_service;
1806 ServiceRefPtr null_service;
1807
1808 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1809 manager()->NotifyDefaultServiceChanged(null_service);
1810
1811 ServiceWatcher service_watcher1;
1812 ServiceWatcher service_watcher2;
1813 int tag1 =
1814 manager()->RegisterDefaultServiceCallback(
1815 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1816 service_watcher1.AsWeakPtr()));
1817 EXPECT_EQ(1, tag1);
1818 int tag2 =
1819 manager()->RegisterDefaultServiceCallback(
1820 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1821 service_watcher2.AsWeakPtr()));
1822 EXPECT_EQ(2, tag2);
1823
1824 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1825 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1826 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1827 manager()->NotifyDefaultServiceChanged(null_service);
1828
1829 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1830 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1831 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1832 manager()->NotifyDefaultServiceChanged(mock_service);
1833
1834 manager()->DeregisterDefaultServiceCallback(tag1);
1835 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1836 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1837 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1838 manager()->NotifyDefaultServiceChanged(mock_service);
1839 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1840
1841 manager()->DeregisterDefaultServiceCallback(tag2);
1842 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1843 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1844 manager()->NotifyDefaultServiceChanged(mock_service);
1845
1846 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1847 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1848}
1849
Gaurav Shah435de2c2011-11-17 19:01:07 -08001850TEST_F(ManagerTest, AvailableTechnologies) {
1851 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1852 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001853 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001854 manager(),
1855 "null4",
1856 "addr4",
1857 0));
1858 manager()->RegisterDevice(mock_devices_[0]);
1859 manager()->RegisterDevice(mock_devices_[1]);
1860 manager()->RegisterDevice(mock_devices_[2]);
1861 manager()->RegisterDevice(mock_devices_[3]);
1862
1863 ON_CALL(*mock_devices_[0].get(), technology())
1864 .WillByDefault(Return(Technology::kEthernet));
1865 ON_CALL(*mock_devices_[1].get(), technology())
1866 .WillByDefault(Return(Technology::kWifi));
1867 ON_CALL(*mock_devices_[2].get(), technology())
1868 .WillByDefault(Return(Technology::kCellular));
1869 ON_CALL(*mock_devices_[3].get(), technology())
1870 .WillByDefault(Return(Technology::kWifi));
1871
1872 set<string> expected_technologies;
1873 expected_technologies.insert(Technology::NameFromIdentifier(
1874 Technology::kEthernet));
1875 expected_technologies.insert(Technology::NameFromIdentifier(
1876 Technology::kWifi));
1877 expected_technologies.insert(Technology::NameFromIdentifier(
1878 Technology::kCellular));
1879 Error error;
1880 vector<string> technologies = manager()->AvailableTechnologies(&error);
1881
1882 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1883 ContainerEq(expected_technologies));
1884}
1885
1886TEST_F(ManagerTest, ConnectedTechnologies) {
1887 scoped_refptr<MockService> connected_service1(
1888 new NiceMock<MockService>(control_interface(),
1889 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001890 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001891 manager()));
1892 scoped_refptr<MockService> connected_service2(
1893 new NiceMock<MockService>(control_interface(),
1894 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001895 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001896 manager()));
1897 scoped_refptr<MockService> disconnected_service1(
1898 new NiceMock<MockService>(control_interface(),
1899 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001900 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001901 manager()));
1902 scoped_refptr<MockService> disconnected_service2(
1903 new NiceMock<MockService>(control_interface(),
1904 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001905 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001906 manager()));
1907
1908 ON_CALL(*connected_service1.get(), IsConnected())
1909 .WillByDefault(Return(true));
1910 ON_CALL(*connected_service2.get(), IsConnected())
1911 .WillByDefault(Return(true));
1912
1913 manager()->RegisterService(connected_service1);
1914 manager()->RegisterService(connected_service2);
1915 manager()->RegisterService(disconnected_service1);
1916 manager()->RegisterService(disconnected_service2);
1917
1918 manager()->RegisterDevice(mock_devices_[0]);
1919 manager()->RegisterDevice(mock_devices_[1]);
1920 manager()->RegisterDevice(mock_devices_[2]);
1921 manager()->RegisterDevice(mock_devices_[3]);
1922
1923 ON_CALL(*mock_devices_[0].get(), technology())
1924 .WillByDefault(Return(Technology::kEthernet));
1925 ON_CALL(*mock_devices_[1].get(), technology())
1926 .WillByDefault(Return(Technology::kWifi));
1927 ON_CALL(*mock_devices_[2].get(), technology())
1928 .WillByDefault(Return(Technology::kCellular));
1929 ON_CALL(*mock_devices_[3].get(), technology())
1930 .WillByDefault(Return(Technology::kWifi));
1931
1932 mock_devices_[0]->SelectService(connected_service1);
1933 mock_devices_[1]->SelectService(disconnected_service1);
1934 mock_devices_[2]->SelectService(disconnected_service2);
1935 mock_devices_[3]->SelectService(connected_service2);
1936
1937 set<string> expected_technologies;
1938 expected_technologies.insert(Technology::NameFromIdentifier(
1939 Technology::kEthernet));
1940 expected_technologies.insert(Technology::NameFromIdentifier(
1941 Technology::kWifi));
1942 Error error;
1943
1944 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1945 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1946 ContainerEq(expected_technologies));
1947}
1948
1949TEST_F(ManagerTest, DefaultTechnology) {
1950 scoped_refptr<MockService> connected_service(
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 scoped_refptr<MockService> disconnected_service(
1956 new NiceMock<MockService>(control_interface(),
1957 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001958 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001959 manager()));
1960
1961 // Connected. WiFi.
1962 ON_CALL(*connected_service.get(), IsConnected())
1963 .WillByDefault(Return(true));
1964 ON_CALL(*connected_service.get(), state())
1965 .WillByDefault(Return(Service::kStateConnected));
1966 ON_CALL(*connected_service.get(), technology())
1967 .WillByDefault(Return(Technology::kWifi));
1968
1969 // Disconnected. Ethernet.
1970 ON_CALL(*disconnected_service.get(), technology())
1971 .WillByDefault(Return(Technology::kEthernet));
1972
1973 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001974 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001975 Error error;
1976 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1977
1978
1979 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001980 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001981 // Connected service should be brought to the front now.
1982 string expected_technology =
1983 Technology::NameFromIdentifier(Technology::kWifi);
1984 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1985}
1986
Paul Stewart212d60f2012-07-12 10:59:13 -07001987TEST_F(ManagerTest, Stop) {
1988 scoped_refptr<MockProfile> profile(
1989 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1990 AdoptProfile(manager(), profile);
1991 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00001992 new NiceMock<MockService>(control_interface(),
1993 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001994 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001995 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07001996 manager()->RegisterService(service);
1997 manager()->RegisterDevice(mock_devices_[0]);
1998 EXPECT_CALL(*profile.get(),
1999 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
2000 .WillOnce(Return(true));
2001 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
2002 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00002003 manager()->Stop();
2004}
2005
mukesh agrawal00917ce2011-11-22 23:56:55 +00002006TEST_F(ManagerTest, UpdateServiceConnected) {
2007 scoped_refptr<MockService> mock_service(
2008 new NiceMock<MockService>(control_interface(),
2009 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002010 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00002011 manager()));
2012 manager()->RegisterService(mock_service);
2013 EXPECT_FALSE(mock_service->favorite());
2014 EXPECT_FALSE(mock_service->auto_connect());
2015
Gaurav Shah435de2c2011-11-17 19:01:07 -08002016 EXPECT_CALL(*mock_service.get(), IsConnected())
2017 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00002018 manager()->UpdateService(mock_service);
2019 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2020 // to mock out MakeFavorite. And mocking that out would break the
2021 // SortServices test. (crosbug.com/23370)
2022 EXPECT_TRUE(mock_service->favorite());
2023 EXPECT_TRUE(mock_service->auto_connect());
2024}
2025
Thieu Led4e9e552012-02-16 16:26:07 -08002026TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
2027 // This tests the case where the user connects to a service that is
2028 // currently associated with a profile. We want to make sure that the
2029 // favorite flag is set and that the flag is saved to the current
2030 // profile.
2031 scoped_refptr<MockService> mock_service(
2032 new NiceMock<MockService>(control_interface(),
2033 dispatcher(),
2034 metrics(),
2035 manager()));
2036 manager()->RegisterService(mock_service);
2037 EXPECT_FALSE(mock_service->favorite());
2038 EXPECT_FALSE(mock_service->auto_connect());
2039
Gary Moraind93615e2012-04-27 11:50:03 -07002040 scoped_refptr<MockProfile> profile(
2041 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08002042
Gary Moraind93615e2012-04-27 11:50:03 -07002043 mock_service->set_profile(profile);
2044 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002045 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002046 EXPECT_CALL(*profile,
2047 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002048 manager()->UpdateService(mock_service);
2049 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2050 // to mock out MakeFavorite. And mocking that out would break the
2051 // SortServices test. (crosbug.com/23370)
2052 EXPECT_TRUE(mock_service->favorite());
2053 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002054 // This releases the ref on the mock profile.
2055 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002056}
2057
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002058TEST_F(ManagerTest, SaveSuccessfulService) {
2059 scoped_refptr<MockProfile> profile(
2060 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2061 AdoptProfile(manager(), profile);
2062 scoped_refptr<MockService> service(
2063 new NiceMock<MockService>(control_interface(),
2064 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002065 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002066 manager()));
2067
2068 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2069 ServiceRefPtr expect_service(service.get());
2070
2071 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2072 .WillOnce(Return(false));
2073 manager()->RegisterService(service);
2074
2075 EXPECT_CALL(*service.get(), state())
2076 .WillRepeatedly(Return(Service::kStateConnected));
2077 EXPECT_CALL(*service.get(), IsConnected())
2078 .WillRepeatedly(Return(true));
2079 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2080 .WillOnce(Return(true));
2081 manager()->UpdateService(service);
2082}
2083
Darin Petkove7c6ad32012-06-29 10:22:09 +02002084TEST_F(ManagerTest, UpdateDevice) {
2085 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2086 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2087 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2088 AdoptProfile(manager(), profile0); // Passes ownership.
2089 AdoptProfile(manager(), profile1); // Passes ownership.
2090 AdoptProfile(manager(), profile2); // Passes ownership.
2091 DeviceRefPtr device_ref(mock_devices_[0].get());
2092 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2093 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2094 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2095 manager()->UpdateDevice(mock_devices_[0]);
2096}
2097
Paul Stewart1b253142012-01-26 14:05:52 -08002098TEST_F(ManagerTest, EnumerateProfiles) {
2099 vector<string> profile_paths;
2100 for (size_t i = 0; i < 10; i++) {
2101 scoped_refptr<MockProfile> profile(
2102 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002103 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002104 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2105 .WillOnce(Return(profile_paths.back()));
2106 AdoptProfile(manager(), profile);
2107 }
2108
2109 Error error;
2110 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2111 EXPECT_TRUE(error.IsSuccess());
2112 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2113 for (size_t i = 0; i < profile_paths.size(); i++) {
2114 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2115 }
2116}
2117
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002118TEST_F(ManagerTest, AutoConnectOnRegister) {
2119 MockServiceRefPtr service = MakeAutoConnectableService();
2120 EXPECT_CALL(*service.get(), AutoConnect());
2121 manager()->RegisterService(service);
2122 dispatcher()->DispatchPendingEvents();
2123}
2124
2125TEST_F(ManagerTest, AutoConnectOnUpdate) {
2126 MockServiceRefPtr service1 = MakeAutoConnectableService();
2127 service1->set_priority(1);
2128 MockServiceRefPtr service2 = MakeAutoConnectableService();
2129 service2->set_priority(2);
2130 manager()->RegisterService(service1);
2131 manager()->RegisterService(service2);
2132 dispatcher()->DispatchPendingEvents();
2133
2134 EXPECT_CALL(*service1.get(), AutoConnect());
2135 EXPECT_CALL(*service2.get(), state())
2136 .WillRepeatedly(Return(Service::kStateFailure));
2137 EXPECT_CALL(*service2.get(), IsFailed())
2138 .WillRepeatedly(Return(true));
2139 EXPECT_CALL(*service2.get(), IsConnected())
2140 .WillRepeatedly(Return(false));
2141 manager()->UpdateService(service2);
2142 dispatcher()->DispatchPendingEvents();
2143}
2144
2145TEST_F(ManagerTest, AutoConnectOnDeregister) {
2146 MockServiceRefPtr service1 = MakeAutoConnectableService();
2147 service1->set_priority(1);
2148 MockServiceRefPtr service2 = MakeAutoConnectableService();
2149 service2->set_priority(2);
2150 manager()->RegisterService(service1);
2151 manager()->RegisterService(service2);
2152 dispatcher()->DispatchPendingEvents();
2153
2154 EXPECT_CALL(*service1.get(), AutoConnect());
2155 manager()->DeregisterService(service2);
2156 dispatcher()->DispatchPendingEvents();
2157}
2158
Darin Petkovca621542012-07-25 14:25:56 +02002159TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2160 MockServiceRefPtr service = MakeAutoConnectableService();
2161 SetPowerState(PowerManagerProxyDelegate::kMem);
2162 SetPowerManager();
2163 EXPECT_CALL(*service, AutoConnect()).Times(0);
2164 manager()->RegisterService(service);
2165 dispatcher()->DispatchPendingEvents();
2166}
2167
2168TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2169 MockServiceRefPtr service = MakeAutoConnectableService();
2170 SetPowerState(PowerManagerProxyDelegate::kOn);
2171 SetPowerManager();
2172 EXPECT_CALL(*service, AutoConnect());
2173 manager()->RegisterService(service);
2174 dispatcher()->DispatchPendingEvents();
2175}
2176
2177TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2178 MockServiceRefPtr service = MakeAutoConnectableService();
2179 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2180 SetPowerManager();
2181 EXPECT_CALL(*service, AutoConnect());
2182 manager()->RegisterService(service);
2183 dispatcher()->DispatchPendingEvents();
2184}
2185
2186TEST_F(ManagerTest, OnPowerStateChanged) {
2187 MockServiceRefPtr service = MakeAutoConnectableService();
2188 SetPowerState(PowerManagerProxyDelegate::kOn);
2189 SetPowerManager();
2190 EXPECT_CALL(*service, AutoConnect());
2191 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002192 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002193 dispatcher()->DispatchPendingEvents();
2194
mukesh agrawal784566d2012-08-08 18:32:58 -07002195 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002196 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2197 EXPECT_CALL(*service, AutoConnect());
2198 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002199 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002200
mukesh agrawal784566d2012-08-08 18:32:58 -07002201 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002202 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2203 EXPECT_CALL(*service, AutoConnect()).Times(0);
2204 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002205 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002206}
2207
Paul Stewartc681fa02012-03-02 19:40:04 -08002208TEST_F(ManagerTest, RecheckPortal) {
2209 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2210 .WillOnce(Return(false));
2211 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2212 .WillOnce(Return(true));
2213 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2214 .Times(0);
2215
2216 manager()->RegisterDevice(mock_devices_[0]);
2217 manager()->RegisterDevice(mock_devices_[1]);
2218 manager()->RegisterDevice(mock_devices_[2]);
2219
2220 manager()->RecheckPortal(NULL);
2221}
2222
Paul Stewartd215af62012-04-24 23:25:50 -07002223TEST_F(ManagerTest, RecheckPortalOnService) {
2224 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2225 dispatcher(),
2226 metrics(),
2227 manager());
2228 EXPECT_CALL(*mock_devices_[0].get(),
2229 IsConnectedToService(IsRefPtrTo(service)))
2230 .WillOnce(Return(false));
2231 EXPECT_CALL(*mock_devices_[1].get(),
2232 IsConnectedToService(IsRefPtrTo(service)))
2233 .WillOnce(Return(true));
2234 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2235 .WillOnce(Return(true));
2236 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2237 .Times(0);
2238
2239 manager()->RegisterDevice(mock_devices_[0]);
2240 manager()->RegisterDevice(mock_devices_[1]);
2241 manager()->RegisterDevice(mock_devices_[2]);
2242
2243 manager()->RecheckPortalOnService(service);
2244}
2245
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002246TEST_F(ManagerTest, GetDefaultService) {
2247 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002248 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002249
2250 scoped_refptr<MockService> mock_service(
2251 new NiceMock<MockService>(control_interface(),
2252 dispatcher(),
2253 metrics(),
2254 manager()));
2255
2256 manager()->RegisterService(mock_service);
2257 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002258 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002259
2260 scoped_refptr<MockConnection> mock_connection(
2261 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002262 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002263 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002264 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002265
Paul Stewartce4ec192012-03-14 12:53:46 -07002266 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002267 manager()->DeregisterService(mock_service);
2268}
2269
Paul Stewart13ed2252012-03-21 12:52:46 -07002270TEST_F(ManagerTest, GetServiceWithGUID) {
2271 scoped_refptr<MockService> mock_service0(
2272 new NiceMock<MockService>(control_interface(),
2273 dispatcher(),
2274 metrics(),
2275 manager()));
2276
2277 scoped_refptr<MockService> mock_service1(
2278 new NiceMock<MockService>(control_interface(),
2279 dispatcher(),
2280 metrics(),
2281 manager()));
2282
Paul Stewartcb59fed2012-03-21 21:14:46 -07002283 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2284 .Times(0);
2285 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2286 .Times(0);
2287
Paul Stewart13ed2252012-03-21 12:52:46 -07002288 manager()->RegisterService(mock_service0);
2289 manager()->RegisterService(mock_service1);
2290
2291 const string kGUID0 = "GUID0";
2292 const string kGUID1 = "GUID1";
2293
2294 {
2295 Error error;
2296 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2297 EXPECT_FALSE(error.IsSuccess());
2298 EXPECT_FALSE(service);
2299 }
2300
2301 KeyValueStore args;
2302 args.SetString(flimflam::kGuidProperty, kGUID1);
2303
2304 {
2305 Error error;
2306 ServiceRefPtr service = manager()->GetService(args, &error);
2307 EXPECT_EQ(Error::kInvalidArguments, error.type());
2308 EXPECT_FALSE(service);
2309 }
2310
2311 mock_service0->set_guid(kGUID0);
2312 mock_service1->set_guid(kGUID1);
2313
2314 {
2315 Error error;
2316 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2317 EXPECT_TRUE(error.IsSuccess());
2318 EXPECT_EQ(mock_service0.get(), service.get());
2319 }
2320
2321 {
2322 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002323 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2324 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002325 ServiceRefPtr service = manager()->GetService(args, &error);
2326 EXPECT_TRUE(error.IsSuccess());
2327 EXPECT_EQ(mock_service1.get(), service.get());
2328 }
2329
2330 manager()->DeregisterService(mock_service0);
2331 manager()->DeregisterService(mock_service1);
2332}
2333
Gary Morain028545d2012-04-07 14:55:52 -07002334
2335TEST_F(ManagerTest, CalculateStateOffline) {
2336 MockMetrics mock_metrics;
2337 manager()->set_metrics(&mock_metrics);
2338 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2339 .Times(AnyNumber());
2340 scoped_refptr<MockService> mock_service0(
2341 new NiceMock<MockService>(control_interface(),
2342 dispatcher(),
2343 metrics(),
2344 manager()));
2345
2346 scoped_refptr<MockService> mock_service1(
2347 new NiceMock<MockService>(control_interface(),
2348 dispatcher(),
2349 metrics(),
2350 manager()));
2351
2352 EXPECT_CALL(*mock_service0.get(), IsConnected())
2353 .WillRepeatedly(Return(false));
2354 EXPECT_CALL(*mock_service1.get(), IsConnected())
2355 .WillRepeatedly(Return(false));
2356
2357 manager()->RegisterService(mock_service0);
2358 manager()->RegisterService(mock_service1);
2359
2360 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2361
2362 manager()->DeregisterService(mock_service0);
2363 manager()->DeregisterService(mock_service1);
2364}
2365
2366TEST_F(ManagerTest, CalculateStateOnline) {
2367 MockMetrics mock_metrics;
2368 manager()->set_metrics(&mock_metrics);
2369 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2370 .Times(AnyNumber());
2371 scoped_refptr<MockService> mock_service0(
2372 new NiceMock<MockService>(control_interface(),
2373 dispatcher(),
2374 metrics(),
2375 manager()));
2376
2377 scoped_refptr<MockService> mock_service1(
2378 new NiceMock<MockService>(control_interface(),
2379 dispatcher(),
2380 metrics(),
2381 manager()));
2382
2383 EXPECT_CALL(*mock_service0.get(), IsConnected())
2384 .WillRepeatedly(Return(false));
2385 EXPECT_CALL(*mock_service1.get(), IsConnected())
2386 .WillRepeatedly(Return(true));
2387 EXPECT_CALL(*mock_service0.get(), state())
2388 .WillRepeatedly(Return(Service::kStateIdle));
2389 EXPECT_CALL(*mock_service1.get(), state())
2390 .WillRepeatedly(Return(Service::kStateConnected));
2391
2392 manager()->RegisterService(mock_service0);
2393 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002394 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002395
2396 EXPECT_EQ("online", manager()->CalculateState(NULL));
2397
2398 manager()->DeregisterService(mock_service0);
2399 manager()->DeregisterService(mock_service1);
2400}
2401
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002402TEST_F(ManagerTest, StartupPortalList) {
2403 // Simulate loading value from the default profile.
2404 const string kProfileValue("wifi,vpn");
2405 manager()->props_.check_portal_list = kProfileValue;
2406
2407 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2408 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2409 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2410
2411 const string kStartupValue("cellular,ethernet");
2412 manager()->SetStartupPortalList(kStartupValue);
2413 // Ensure profile value is not overwritten, so when we save the default
2414 // profile, the correct value will still be written.
2415 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2416
2417 // However we should read back a different list.
2418 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2419 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2420 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2421
2422 const string kRuntimeValue("ppp");
2423 // Setting a runtime value over the control API should overwrite both
2424 // the profile value and what we read back.
2425 Error error;
2426 manager()->mutable_store()->SetStringProperty(
2427 flimflam::kCheckPortalListProperty,
2428 kRuntimeValue,
2429 &error);
2430 ASSERT_TRUE(error.IsSuccess());
2431 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2432 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2433 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2434 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2435}
2436
Paul Stewart036dba02012-08-07 12:34:41 -07002437TEST_F(ManagerTest, LinkMonitorEnabled) {
2438 const string kEnabledTechnologies("wifi,vpn");
2439 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2440 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2441 EXPECT_FALSE(
2442 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2443}
2444
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002445TEST_F(ManagerTest, EnableTechnology) {
2446 Error error(Error::kOperationInitiated);
2447 ResultCallback callback;
2448 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2449 EXPECT_TRUE(error.IsSuccess());
2450
Joshua Krollda798622012-06-05 12:30:48 -07002451 ON_CALL(*mock_devices_[0], technology())
2452 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002453
2454 manager()->RegisterDevice(mock_devices_[0]);
2455
2456 // Device is enabled, so expect operation is successful.
2457 mock_devices_[0]->enabled_ = true;
2458 error.Populate(Error::kOperationInitiated);
2459 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2460 EXPECT_TRUE(error.IsSuccess());
2461
2462 // Device is disabled, so expect operation in progress.
2463 mock_devices_[0]->enabled_ = false;
2464 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2465 error.Populate(Error::kOperationInitiated);
2466 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2467 EXPECT_TRUE(error.IsOngoing());
2468}
2469
2470TEST_F(ManagerTest, DisableTechnology) {
2471 Error error(Error::kOperationInitiated);
2472 ResultCallback callback;
2473 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2474 EXPECT_TRUE(error.IsSuccess());
2475
Joshua Krollda798622012-06-05 12:30:48 -07002476 ON_CALL(*mock_devices_[0], technology())
2477 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002478
2479 manager()->RegisterDevice(mock_devices_[0]);
2480
2481 // Device is disabled, so expect operation is successful.
2482 error.Populate(Error::kOperationInitiated);
2483 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2484 EXPECT_TRUE(error.IsSuccess());
2485
2486 // Device is enabled, so expect operation in progress.
2487 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2488 mock_devices_[0]->enabled_ = true;
2489 error.Populate(Error::kOperationInitiated);
2490 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2491 EXPECT_TRUE(error.IsOngoing());
2492}
2493
Chris Masone9be4a9d2011-05-16 15:44:09 -07002494} // namespace shill