blob: 1a36418d4ee9d50df50688fcc027617a8b6c7409 [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"
mukesh agrawal32399322011-09-01 10:53:43 -070026#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080027#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070028#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070029#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080030#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070031#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000032#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020033#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070034#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070035#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070036#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070037#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070038#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020040#include "shill/proxy_factory.h"
Chris Masone6515aab2011-10-12 16:19:09 -070041#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070042#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020043#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070044
45using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070046using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047using std::string;
48using std::vector;
49
Chris Masone9be4a9d2011-05-16 15:44:09 -070050namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070051using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070052using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080053using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070054using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070055using ::testing::InSequence;
mukesh agrawal784566d2012-08-08 18:32:58 -070056using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070057using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070058using ::testing::NiceMock;
59using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070060using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070061using ::testing::SaveArg;
Gaurav Shah435de2c2011-11-17 19:01:07 -080062using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080063using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070064using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070065
Chris Masone3bd3c8c2011-06-13 08:20:26 -070066class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070067 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070068 ManagerTest()
Darin Petkovca621542012-07-25 14:25:56 +020069 : power_manager_(new MockPowerManager(&proxy_factory_)),
70 mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070071 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080072 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073 manager(),
74 "wifi0",
75 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080076 4)),
77 device_info_(new NiceMock<MockDeviceInfo>(
78 control_interface(),
79 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080080 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080081 reinterpret_cast<Manager*>(NULL))),
82 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070083 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
84 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080085 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070086 manager(),
87 "null0",
88 "addr0",
89 0));
90 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
91 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070093 manager(),
94 "null1",
95 "addr1",
96 1));
97 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
98 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080099 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700100 manager(),
101 "null2",
102 "addr2",
103 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800104 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
105 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800106 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800107 manager(),
108 "null3",
109 "addr3",
110 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700111 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800112
113 // Replace the manager's adaptor with a quieter one, and one
114 // we can do EXPECT*() against. Passes ownership.
115 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700116 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700117 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700118
Paul Stewartfdd16072011-09-16 12:41:35 -0700119 bool IsDeviceRegistered(const DeviceRefPtr &device,
120 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700121 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700122 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700123 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700124 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700125 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700126
Paul Stewarta849a3d2011-11-03 05:54:09 -0700127 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
128 manager->profiles_.push_back(profile);
129 }
130
Paul Stewart75225512012-01-26 22:51:33 -0800131 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
132 return manager->ephemeral_profile_;
133 }
134
Chris Masone6515aab2011-10-12 16:19:09 -0700135 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
136 Profile::Identifier id("rather", "irrelevant");
137 scoped_ptr<Profile> profile(new Profile(control_interface(),
138 manager,
139 id,
140 "",
141 false));
142 FilePath final_path(storage_path());
143 final_path = final_path.Append("test.profile");
144 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
145 storage->set_path(final_path);
146 if (!storage->Open())
147 return NULL;
148 profile->set_storage(storage.release()); // Passes ownership.
149 return profile.release();
150 }
151
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700152 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
153 const string &profile_identifier,
154 const string &service_name) {
155 GLib glib;
156 KeyFileStore store(&glib);
157 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
158 return store.Open() &&
159 store.SetString(service_name, "rather", "irrelevant") &&
160 store.Close();
161 }
162
163 Error::Type TestCreateProfile(Manager *manager, const string &name) {
164 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800165 string path;
166 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700167 return error.type();
168 }
169
170 Error::Type TestPopAnyProfile(Manager *manager) {
171 Error error;
172 manager->PopAnyProfile(&error);
173 return error.type();
174 }
175
176 Error::Type TestPopProfile(Manager *manager, const string &name) {
177 Error error;
178 manager->PopProfile(name, &error);
179 return error.type();
180 }
181
182 Error::Type TestPushProfile(Manager *manager, const string &name) {
183 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800184 string path;
185 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700186 return error.type();
187 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000188
Paul Stewartcb3eb892012-06-07 14:24:46 -0700189 void AddMockProfileToManager(Manager *manager) {
190 scoped_refptr<MockProfile> profile(
191 new MockProfile(control_interface(), manager, ""));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200192 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/"));
193 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700194 AdoptProfile(manager, profile);
195 }
196
Paul Stewartdfa46052012-06-26 09:44:14 -0700197 void CompleteServiceSort() {
198 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
199 dispatcher()->DispatchPendingEvents();
200 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
201 }
202
Paul Stewart49739c02012-08-08 17:24:03 -0700203 RpcIdentifier GetDefaultServiceRpcIdentifier() {
204 return manager()->GetDefaultServiceRpcIdentifier(NULL);
205 }
206
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700207 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000208 typedef scoped_refptr<MockService> MockServiceRefPtr;
209
Darin Petkova5e07ef2012-07-09 14:27:57 +0200210 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
211 public:
212 ServiceWatcher() {}
213 virtual ~ServiceWatcher() {}
214
215 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
216
217 private:
218 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
219 };
220
Darin Petkovca621542012-07-25 14:25:56 +0200221 class TestProxyFactory : public ProxyFactory {
222 public:
223 TestProxyFactory() {}
224
225 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
226 PowerManagerProxyDelegate */*delegate*/) {
227 return NULL;
228 }
229
230 private:
231 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
232 };
233
234 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
235 power_manager_->power_state_ = state;
236 }
237
238 void SetPowerManager() {
239 manager()->set_power_manager(power_manager_.release());
240 }
241
242 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
243 manager()->OnPowerStateChanged(state);
244 }
245
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000246 MockServiceRefPtr MakeAutoConnectableService() {
247 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
248 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800249 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000250 manager());
251 service->MakeFavorite();
252 service->set_connectable(true);
253 return service;
254 }
255
Darin Petkovca621542012-07-25 14:25:56 +0200256 TestProxyFactory proxy_factory_;
257 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700258 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700259 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800260 scoped_ptr<MockDeviceInfo> device_info_;
261
262 // This pointer is owned by the manager, and only tracked here for EXPECT*()
263 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700264};
265
Paul Stewart22aa71b2011-09-16 12:15:11 -0700266bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700267 if (!manager()->sort_services_task_.IsCancelled()) {
268 manager()->SortServicesTask();
269 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700270 return (svc0.get() == manager()->services_[0].get() &&
271 svc1.get() == manager()->services_[1].get());
272}
273
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700274TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700275 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
276 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700277}
278
Chris Masone9be4a9d2011-05-16 15:44:09 -0700279TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700280 ON_CALL(*mock_devices_[0].get(), technology())
281 .WillByDefault(Return(Technology::kEthernet));
282 ON_CALL(*mock_devices_[1].get(), technology())
283 .WillByDefault(Return(Technology::kWifi));
284 ON_CALL(*mock_devices_[2].get(), technology())
285 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700286
Paul Stewart22aa71b2011-09-16 12:15:11 -0700287 manager()->RegisterDevice(mock_devices_[0]);
288 manager()->RegisterDevice(mock_devices_[1]);
289 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700290
Paul Stewart22aa71b2011-09-16 12:15:11 -0700291 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
292 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
293 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700294}
295
Paul Stewarta41e38d2011-11-11 07:47:29 -0800296TEST_F(ManagerTest, DeviceRegistrationAndStart) {
297 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500298 mock_devices_[0]->enabled_persistent_ = true;
299 mock_devices_[1]->enabled_persistent_ = false;
300 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800301 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500302 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800303 .Times(0);
304 manager()->RegisterDevice(mock_devices_[0]);
305 manager()->RegisterDevice(mock_devices_[1]);
306}
307
308TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
309 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
310 DeviceRefPtr device_ref(mock_devices_[0].get());
311 AdoptProfile(manager(), profile); // Passes ownership.
312 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200313 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800314 manager()->RegisterDevice(mock_devices_[0]);
315}
316
Chris Masone9be4a9d2011-05-16 15:44:09 -0700317TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700318 ON_CALL(*mock_devices_[0].get(), technology())
319 .WillByDefault(Return(Technology::kEthernet));
320 ON_CALL(*mock_devices_[1].get(), technology())
321 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700322
Gaurav Shah435de2c2011-11-17 19:01:07 -0800323 manager()->RegisterDevice(mock_devices_[0]);
324 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700325
Paul Stewart22aa71b2011-09-16 12:15:11 -0700326 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
327 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700328
Paul Stewart212d60f2012-07-12 10:59:13 -0700329 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
330 AdoptProfile(manager(), profile); // Passes ownership.
331
Eric Shienbrood9a245532012-03-07 14:20:39 -0500332 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700333 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800334 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700335 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700336
Eric Shienbrood9a245532012-03-07 14:20:39 -0500337 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700338 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800339 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700340 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700341}
342
343TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700344 // It's much easier and safer to use a real GLib for this test.
345 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700346 Manager manager(control_interface(),
347 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800348 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700349 &glib,
350 run_path(),
351 storage_path(),
352 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700353 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
354 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700355 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700356
Chris Masone9be4a9d2011-05-16 15:44:09 -0700357 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700358 new NiceMock<MockService>(control_interface(),
359 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800360 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700361 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700362 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700363 new NiceMock<MockService>(control_interface(),
364 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800365 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700366 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700367
mukesh agrawal51a7e932011-07-27 16:18:26 -0700368 string service1_name(mock_service->UniqueName());
369 string service2_name(mock_service2->UniqueName());
370
371 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
372 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700373 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700374 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700375 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700376 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700377 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700378
Chris Masone9d779932011-08-25 16:33:41 -0700379 manager.RegisterService(mock_service);
380 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700381
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800382 Error error;
383 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700384 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700385 EXPECT_EQ(2, ids.size());
386 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
387 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700388
Chris Masone9d779932011-08-25 16:33:41 -0700389 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
390 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
391
392 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700393}
394
Chris Masone6515aab2011-10-12 16:19:09 -0700395TEST_F(ManagerTest, RegisterKnownService) {
396 // It's much easier and safer to use a real GLib for this test.
397 GLib glib;
398 Manager manager(control_interface(),
399 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800400 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700401 &glib,
402 run_path(),
403 storage_path(),
404 string());
405 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
406 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700407 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700408 {
409 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
410 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800411 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700412 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700413 ASSERT_TRUE(profile->AdoptService(service1));
414 ASSERT_TRUE(profile->ContainsService(service1));
415 } // Force destruction of service1.
416
417 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
418 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800419 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700420 &manager));
421 manager.RegisterService(service2);
422 EXPECT_EQ(service2->profile().get(), profile.get());
423 manager.Stop();
424}
425
426TEST_F(ManagerTest, RegisterUnknownService) {
427 // It's much easier and safer to use a real GLib for this test.
428 GLib glib;
429 Manager manager(control_interface(),
430 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800431 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700432 &glib,
433 run_path(),
434 storage_path(),
435 string());
436 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
437 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700438 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700439 {
440 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
441 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800442 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700443 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700444 ASSERT_TRUE(profile->AdoptService(service1));
445 ASSERT_TRUE(profile->ContainsService(service1));
446 } // Force destruction of service1.
447 scoped_refptr<MockService> mock_service2(
448 new NiceMock<MockService>(control_interface(),
449 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800450 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700451 &manager));
452 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
453 .WillRepeatedly(Return(mock_service2->UniqueName()));
454 manager.RegisterService(mock_service2);
455 EXPECT_NE(mock_service2->profile().get(), profile.get());
456 manager.Stop();
457}
458
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000459TEST_F(ManagerTest, DeregisterUnregisteredService) {
460 // WiFi assumes that it can deregister a service that is not
461 // registered. (E.g. a hidden service can be deregistered when it
462 // loses its last endpoint, and again when WiFi is Stop()-ed.)
463 //
464 // So test that doing so doesn't cause a crash.
465 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
466 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800467 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000468 manager());
469 manager()->DeregisterService(service);
470}
471
Chris Masonea8a2c252011-06-27 22:16:30 -0700472TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700473 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700474 map<string, ::DBus::Variant> props;
475 Error error(Error::kInvalidProperty, "");
476 {
477 ::DBus::Error dbus_error;
478 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700479 manager()->mutable_store()->SetStringProperty(
480 flimflam::kCheckPortalListProperty,
481 expected,
482 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700483 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700484 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
485 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
486 expected);
487 }
488 {
489 ::DBus::Error dbus_error;
490 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700491 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
492 expected,
493 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700494 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700495 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
496 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
497 expected);
498 }
499}
500
Chris Masone3c3f6a12011-07-01 10:01:41 -0700501TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700502 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800503 manager()->RegisterDevice(mock_devices_[0]);
504 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700505 {
506 map<string, ::DBus::Variant> props;
507 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700508 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700509 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700510 vector < ::DBus::Path> devices =
511 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700512 EXPECT_EQ(2, devices.size());
513 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700514}
515
mukesh agrawal2366eed2012-03-20 18:21:50 -0700516TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700517 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700518 map<string, ::DBus::Variant> props;
519 ::DBus::Error dbus_error;
520 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
521 map<string, ::DBus::Variant>::const_iterator prop =
522 props.find(flimflam::kServicesProperty);
523 ASSERT_FALSE(prop == props.end());
524 const ::DBus::Variant &variant = prop->second;
525 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
526}
527
Chris Masone6791a432011-07-12 13:23:19 -0700528TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700529 Manager manager(control_interface(),
530 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800531 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700532 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700533 run_path(),
534 storage_path(),
535 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700536 scoped_refptr<MockService> s2(new MockService(control_interface(),
537 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800538 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700539 &manager));
540 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700541 {
Chris Masone6515aab2011-10-12 16:19:09 -0700542 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700543 ProfileRefPtr profile(
544 new Profile(control_interface(), &manager, id, "", false));
545 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700546 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700547 .WillRepeatedly(Return(true));
548 EXPECT_CALL(*storage, Flush())
549 .Times(AnyNumber())
550 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700551 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700552 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700553 }
Chris Masone6515aab2011-10-12 16:19:09 -0700554 // Create a profile that already has |s2| in it.
555 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700556 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700557
Chris Masone6515aab2011-10-12 16:19:09 -0700558 // Now, move the Service |s2| to another profile.
559 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
560 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700561
562 // Force destruction of the original Profile, to ensure that the Service
563 // is kept alive and populated with data.
564 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700565 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700566 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700567}
568
Paul Stewart7f61e522012-03-22 11:13:45 -0700569TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
570 scoped_refptr<MockProfile> mock_profile(
571 new MockProfile(control_interface(), manager(), ""));
572 const string kProfileName("profile0");
573 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
574 .WillRepeatedly(Return(kProfileName));
575 AdoptProfile(manager(), mock_profile);
576
577 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
578 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
579 EXPECT_EQ(mock_profile.get(), profile.get());
580}
581
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800582TEST_F(ManagerTest, SetProfileForService) {
583 scoped_refptr<MockProfile> profile0(
584 new MockProfile(control_interface(), manager(), ""));
585 string profile_name0("profile0");
586 EXPECT_CALL(*profile0, GetRpcIdentifier())
587 .WillRepeatedly(Return(profile_name0));
588 AdoptProfile(manager(), profile0);
589 scoped_refptr<MockService> service(new MockService(control_interface(),
590 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800591 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800592 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700593 EXPECT_FALSE(manager()->HasService(service));
594 {
595 Error error;
596 EXPECT_CALL(*profile0, AdoptService(_))
597 .WillOnce(Return(true));
598 // Expect that setting the profile of a service that does not already
599 // have one assigned does not cause a crash.
600 manager()->SetProfileForService(service, "profile0", &error);
601 EXPECT_TRUE(error.IsSuccess());
602 }
603
604 // The service should be registered as a side-effect of the profile being
605 // set for this service.
606 EXPECT_TRUE(manager()->HasService(service));
607
608 // Since we have mocked Profile::AdoptServie() above, the service's
609 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800610 service->set_profile(profile0);
611
612 {
613 Error error;
614 manager()->SetProfileForService(service, "foo", &error);
615 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700616 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800617 }
618
619 {
620 Error error;
621 manager()->SetProfileForService(service, profile_name0, &error);
622 EXPECT_EQ(Error::kInvalidArguments, error.type());
623 EXPECT_EQ("Service is already connected to this profile", error.message());
624 }
625
626 scoped_refptr<MockProfile> profile1(
627 new MockProfile(control_interface(), manager(), ""));
628 string profile_name1("profile1");
629 EXPECT_CALL(*profile1, GetRpcIdentifier())
630 .WillRepeatedly(Return(profile_name1));
631 AdoptProfile(manager(), profile1);
632
633 {
634 Error error;
635 EXPECT_CALL(*profile1, AdoptService(_))
636 .WillOnce(Return(true));
637 EXPECT_CALL(*profile0, AbandonService(_))
638 .WillOnce(Return(true));
639 manager()->SetProfileForService(service, profile_name1, &error);
640 EXPECT_TRUE(error.IsSuccess());
641 }
642}
643
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700644TEST_F(ManagerTest, CreateProfile) {
645 // It's much easier to use real Glib here since we want the storage
646 // side-effects.
647 GLib glib;
648 ScopedTempDir temp_dir;
649 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
650
651 Manager manager(control_interface(),
652 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800653 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700654 &glib,
655 run_path(),
656 storage_path(),
657 temp_dir.path().value());
658
659 // Invalid name should be rejected.
660 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
661
Paul Stewartd0a3b812012-03-28 22:48:22 -0700662 // A profile with invalid characters in it should similarly be rejected.
663 EXPECT_EQ(Error::kInvalidArguments,
664 TestCreateProfile(&manager, "valid_profile"));
665
666 // We should be able to create a machine profile.
667 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700668
Gary Morainb672d352012-04-25 09:19:06 -0700669 // We should succeed in creating a valid user profile. Verify the returned
670 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700671 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700672 {
673 Error error;
674 string path;
675 manager.CreateProfile(kProfile, &path, &error);
676 EXPECT_EQ(Error::kSuccess, error.type());
677 EXPECT_EQ("/profile_rpc", path);
678 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700679
680 // We should fail in creating it a second time (already exists).
681 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
682}
683
684TEST_F(ManagerTest, PushPopProfile) {
685 // It's much easier to use real Glib in creating a Manager for this
686 // test here since we want the storage side-effects.
687 GLib glib;
688 ScopedTempDir temp_dir;
689 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
690 Manager manager(control_interface(),
691 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800692 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700693 &glib,
694 run_path(),
695 storage_path(),
696 temp_dir.path().value());
697
698 // Pushing an invalid profile should fail.
699 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
700
Paul Stewartd0a3b812012-03-28 22:48:22 -0700701 // Pushing a default profile that does not exist should fail.
702 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700703
704 const char kProfile0[] = "~user/profile0";
705 const char kProfile1[] = "~user/profile1";
706
707 // Create a couple of profiles.
708 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
709 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
710
711 // Push these profiles on the stack.
712 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
713 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
714
715 // Pushing a profile a second time should fail.
716 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
717 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
718
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800719 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700720 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800721 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700722
723 // Make sure a profile name that doesn't exist fails.
724 const char kProfile2Id[] = "profile2";
725 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
726 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
727
728 // Create a new service, with a specific storage name.
729 scoped_refptr<MockService> service(
730 new NiceMock<MockService>(control_interface(),
731 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800732 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700733 &manager));
734 const char kServiceName[] = "service_storage_name";
735 EXPECT_CALL(*service.get(), GetStorageIdentifier())
736 .WillRepeatedly(Return(kServiceName));
737 EXPECT_CALL(*service.get(), Load(_))
738 .WillRepeatedly(Return(true));
739
740 // Add this service to the manager -- it should end up in the ephemeral
741 // profile.
742 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800743 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700744
745 // Create storage for a profile that contains the service storage name.
746 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
747 kServiceName));
748
749 // When we push the profile, the service should move away from the
750 // ephemeral profile to this new profile since it has an entry for
751 // this service.
752 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800753 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700754 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
755
756 // Insert another profile that should supersede ownership of the service.
757 const char kProfile3Id[] = "profile3";
758 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
759 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
760 kServiceName));
761 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
762 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
763
764 // Popping an invalid profile name should fail.
765 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
766
767 // Popping an profile that is not at the top of the stack should fail.
768 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
769
770 // Popping the top profile should succeed.
771 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
772
773 // Moreover the service should have switched profiles to profile 2.
774 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
775
776 // Popping the top profile should succeed.
777 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
778
779 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800780 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700781
782 // Pop the remaining two services off the stack.
783 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
784 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
785
786 // Next pop should fail with "stack is empty".
787 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700788
789 const char kMachineProfile0[] = "machineprofile0";
790 const char kMachineProfile1[] = "machineprofile1";
791 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
792 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
793
794 // Should be able to push a machine profile.
795 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
796
797 // Should be able to push a user profile atop a machine profile.
798 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
799
800 // Pushing a system-wide profile on top of a user profile should fail.
801 EXPECT_EQ(Error::kInvalidArguments,
802 TestPushProfile(&manager, kMachineProfile1));
803
804 // However if we pop the user profile, we should be able stack another
805 // machine profile on.
806 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
807 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700808}
809
Paul Stewarte73d05c2012-03-29 16:26:05 -0700810TEST_F(ManagerTest, RemoveProfile) {
811 // It's much easier to use real Glib in creating a Manager for this
812 // test here since we want the storage side-effects.
813 GLib glib;
814 ScopedTempDir temp_dir;
815 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
816 Manager manager(control_interface(),
817 dispatcher(),
818 metrics(),
819 &glib,
820 run_path(),
821 storage_path(),
822 temp_dir.path().value());
823
824 const char kProfile0[] = "profile0";
825 FilePath profile_path(
826 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
827
828 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
829 ASSERT_TRUE(file_util::PathExists(profile_path));
830
831 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
832
833 // Remove should fail since the profile is still on the stack.
834 {
835 Error error;
836 manager.RemoveProfile(kProfile0, &error);
837 EXPECT_EQ(Error::kInvalidArguments, error.type());
838 }
839
840 // Profile path should still exist.
841 EXPECT_TRUE(file_util::PathExists(profile_path));
842
843 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
844
845 // This should succeed now that the profile is off the stack.
846 {
847 Error error;
848 manager.RemoveProfile(kProfile0, &error);
849 EXPECT_EQ(Error::kSuccess, error.type());
850 }
851
852 // Profile path should no longer exist.
853 EXPECT_FALSE(file_util::PathExists(profile_path));
854
855 // Another remove succeeds, due to a foible in file_util::Delete --
856 // it is not an error to delete a file that does not exist.
857 {
858 Error error;
859 manager.RemoveProfile(kProfile0, &error);
860 EXPECT_EQ(Error::kSuccess, error.type());
861 }
862
863 // Let's create an error case that will "work". Create a non-empty
864 // directory in the place of the profile pathname.
865 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
866 {
867 Error error;
868 manager.RemoveProfile(kProfile0, &error);
869 EXPECT_EQ(Error::kOperationFailed, error.type());
870 }
871}
872
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700873TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
874 // It's much easier to use real Glib in creating a Manager for this
875 // test here since we want the storage side-effects.
876 GLib glib;
877 ScopedTempDir temp_dir;
878 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
879 Manager manager(control_interface(),
880 dispatcher(),
881 metrics(),
882 &glib,
883 run_path(),
884 storage_path(),
885 temp_dir.path().value());
886
887 const char kProfile0[] = "profile0";
888 FilePath profile_path(
889 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
890
891 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
892 ASSERT_TRUE(file_util::PathExists(profile_path));
893 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
894
895 // Ensure that even if the backing filestore is removed, we still can't
896 // create a profile twice.
897 ASSERT_TRUE(file_util::Delete(profile_path, false));
898 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
899}
900
Paul Stewart75225512012-01-26 22:51:33 -0800901// Use this matcher instead of passing RefPtrs directly into the arguments
902// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
903// system teardown.
904MATCHER_P(IsRefPtrTo, ref_address, "") {
905 return arg.get() == ref_address;
906}
907
908TEST_F(ManagerTest, HandleProfileEntryDeletion) {
909 MockServiceRefPtr s_not_in_profile(
910 new NiceMock<MockService>(control_interface(),
911 dispatcher(),
912 metrics(),
913 manager()));
914 MockServiceRefPtr s_not_in_group(
915 new NiceMock<MockService>(control_interface(),
916 dispatcher(),
917 metrics(),
918 manager()));
919 MockServiceRefPtr s_configure_fail(
920 new NiceMock<MockService>(control_interface(),
921 dispatcher(),
922 metrics(),
923 manager()));
924 MockServiceRefPtr s_configure_succeed(
925 new NiceMock<MockService>(control_interface(),
926 dispatcher(),
927 metrics(),
928 manager()));
929
930 string entry_name("entry_name");
931 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
932 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
933 .WillRepeatedly(Return("not_entry_name"));
934 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
935 .WillRepeatedly(Return(entry_name));
936 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
937 .WillRepeatedly(Return(entry_name));
938
939 manager()->RegisterService(s_not_in_profile);
940 manager()->RegisterService(s_not_in_group);
941 manager()->RegisterService(s_configure_fail);
942 manager()->RegisterService(s_configure_succeed);
943
944 scoped_refptr<MockProfile> profile0(
945 new StrictMock<MockProfile>(control_interface(), manager(), ""));
946 scoped_refptr<MockProfile> profile1(
947 new StrictMock<MockProfile>(control_interface(), manager(), ""));
948
949 s_not_in_group->set_profile(profile1);
950 s_configure_fail->set_profile(profile1);
951 s_configure_succeed->set_profile(profile1);
952
953 AdoptProfile(manager(), profile0);
954 AdoptProfile(manager(), profile1);
955
956 // No services are a member of this profile.
957 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
958
959 // No services that are members of this profile have this entry name.
960 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
961
962 // Only services that are members of the profile and group will be abandoned.
963 EXPECT_CALL(*profile1.get(),
964 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
965 EXPECT_CALL(*profile1.get(),
966 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
967 EXPECT_CALL(*profile1.get(),
968 AbandonService(IsRefPtrTo(s_configure_fail.get())))
969 .WillOnce(Return(true));
970 EXPECT_CALL(*profile1.get(),
971 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
972 .WillOnce(Return(true));
973
974 // Never allow services to re-join profile1.
975 EXPECT_CALL(*profile1.get(), ConfigureService(_))
976 .WillRepeatedly(Return(false));
977
978 // Only allow one of the members of the profile and group to successfully
979 // join profile0.
980 EXPECT_CALL(*profile0.get(),
981 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
982 EXPECT_CALL(*profile0.get(),
983 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
984 EXPECT_CALL(*profile0.get(),
985 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
986 .WillOnce(Return(false));
987 EXPECT_CALL(*profile0.get(),
988 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
989 .WillOnce(Return(true));
990
991 // Expect the failed-to-configure service to have Unload() called on it.
992 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
993 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
994 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
995 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
996
997 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
998
999 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1000 EXPECT_EQ(profile1, s_not_in_group->profile());
1001 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1002
1003 // Since we are using a MockProfile, the profile does not actually change,
1004 // since ConfigureService was not actually called on the service.
1005 EXPECT_EQ(profile1, s_configure_succeed->profile());
1006}
1007
Paul Stewart65512e12012-03-26 18:01:08 -07001008TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1009 MockServiceRefPtr s_will_remove0(
1010 new NiceMock<MockService>(control_interface(),
1011 dispatcher(),
1012 metrics(),
1013 manager()));
1014 MockServiceRefPtr s_will_remove1(
1015 new NiceMock<MockService>(control_interface(),
1016 dispatcher(),
1017 metrics(),
1018 manager()));
1019 MockServiceRefPtr s_will_not_remove0(
1020 new NiceMock<MockService>(control_interface(),
1021 dispatcher(),
1022 metrics(),
1023 manager()));
1024 MockServiceRefPtr s_will_not_remove1(
1025 new NiceMock<MockService>(control_interface(),
1026 dispatcher(),
1027 metrics(),
1028 manager()));
1029
1030 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1031 .Times(4); // Once for each registration.
1032
1033 string entry_name("entry_name");
1034 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1035 .WillRepeatedly(Return(entry_name));
1036 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1037 .WillRepeatedly(Return(entry_name));
1038 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1039 .WillRepeatedly(Return(entry_name));
1040 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1041 .WillRepeatedly(Return(entry_name));
1042
1043 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001044 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001045 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001046 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001047 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001048 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001049 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001050 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001051
1052 // One for each service added above.
1053 ASSERT_EQ(4, manager()->services_.size());
1054
1055 scoped_refptr<MockProfile> profile(
1056 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1057
1058 s_will_remove0->set_profile(profile);
1059 s_will_remove1->set_profile(profile);
1060 s_will_not_remove0->set_profile(profile);
1061 s_will_not_remove1->set_profile(profile);
1062
1063 AdoptProfile(manager(), profile);
1064
1065 // Deny any of the services re-entry to the profile.
1066 EXPECT_CALL(*profile, ConfigureService(_))
1067 .WillRepeatedly(Return(false));
1068
1069 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1070 .WillOnce(Return(true));
1071 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1072 .WillOnce(Return(true));
1073 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1074 .WillOnce(Return(true));
1075 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1076 .WillOnce(Return(true));
1077
1078 EXPECT_CALL(*s_will_remove0, Unload())
1079 .WillOnce(Return(true));
1080 EXPECT_CALL(*s_will_remove1, Unload())
1081 .WillOnce(Return(true));
1082 EXPECT_CALL(*s_will_not_remove0, Unload())
1083 .WillOnce(Return(false));
1084 EXPECT_CALL(*s_will_not_remove1, Unload())
1085 .WillOnce(Return(false));
1086
1087
1088 // This will cause all the profiles to be unloaded.
1089 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1090
1091 // 2 of the 4 services added above should have been unregistered and
1092 // removed, leaving 2.
1093 EXPECT_EQ(2, manager()->services_.size());
1094 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1095 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1096}
1097
1098TEST_F(ManagerTest, PopProfileWithUnload) {
1099 MockServiceRefPtr s_will_remove0(
1100 new NiceMock<MockService>(control_interface(),
1101 dispatcher(),
1102 metrics(),
1103 manager()));
1104 MockServiceRefPtr s_will_remove1(
1105 new NiceMock<MockService>(control_interface(),
1106 dispatcher(),
1107 metrics(),
1108 manager()));
1109 MockServiceRefPtr s_will_not_remove0(
1110 new NiceMock<MockService>(control_interface(),
1111 dispatcher(),
1112 metrics(),
1113 manager()));
1114 MockServiceRefPtr s_will_not_remove1(
1115 new NiceMock<MockService>(control_interface(),
1116 dispatcher(),
1117 metrics(),
1118 manager()));
1119
1120 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1121 .Times(5); // Once for each registration, and one after profile pop.
1122
1123 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001124 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001125 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001126 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001127 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001128 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001129 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001130 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001131
1132 // One for each service added above.
1133 ASSERT_EQ(4, manager()->services_.size());
1134
1135 scoped_refptr<MockProfile> profile0(
1136 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1137 scoped_refptr<MockProfile> profile1(
1138 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1139
1140 s_will_remove0->set_profile(profile1);
1141 s_will_remove1->set_profile(profile1);
1142 s_will_not_remove0->set_profile(profile1);
1143 s_will_not_remove1->set_profile(profile1);
1144
1145 AdoptProfile(manager(), profile0);
1146 AdoptProfile(manager(), profile1);
1147
1148 // Deny any of the services entry to profile0, so they will all be unloaded.
1149 EXPECT_CALL(*profile0, ConfigureService(_))
1150 .WillRepeatedly(Return(false));
1151
1152 EXPECT_CALL(*s_will_remove0, Unload())
1153 .WillOnce(Return(true));
1154 EXPECT_CALL(*s_will_remove1, Unload())
1155 .WillOnce(Return(true));
1156 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001157 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001158 EXPECT_CALL(*s_will_not_remove1, Unload())
1159 .WillOnce(Return(false));
1160
1161 // This will pop profile1, which should cause all our profiles to unload.
1162 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001163 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001164
1165 // 2 of the 4 services added above should have been unregistered and
1166 // removed, leaving 2.
1167 EXPECT_EQ(2, manager()->services_.size());
1168 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1169 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001170
1171 // Expect the unloaded services to lose their profile reference.
1172 EXPECT_FALSE(s_will_remove0->profile());
1173 EXPECT_FALSE(s_will_remove1->profile());
1174
1175 // If we explicitly deregister a service, the effect should be the same
1176 // with respect to the profile reference.
1177 ASSERT_TRUE(s_will_not_remove0->profile());
1178 manager()->DeregisterService(s_will_not_remove0);
1179 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001180}
1181
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001182TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001183 {
1184 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001185 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1186 flimflam::kOfflineModeProperty,
1187 PropertyStoreTest::kBoolV,
1188 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001189 }
1190 {
1191 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001192 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1193 flimflam::kCountryProperty,
1194 PropertyStoreTest::kStringV,
1195 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001196 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001197 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001198 {
1199 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001200 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1201 flimflam::kCountryProperty,
1202 PropertyStoreTest::kBoolV,
1203 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001204 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001205 }
1206 {
1207 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001208 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1209 flimflam::kOfflineModeProperty,
1210 PropertyStoreTest::kStringV,
1211 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001212 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001213 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001214 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001215 {
1216 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001217 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001218 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001219 flimflam::kEnabledTechnologiesProperty,
1220 PropertyStoreTest::kStringsV,
1221 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001222 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001223 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001224}
1225
mukesh agrawal32399322011-09-01 10:53:43 -07001226TEST_F(ManagerTest, RequestScan) {
1227 {
1228 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001229 manager()->RegisterDevice(mock_devices_[0].get());
1230 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001231 EXPECT_CALL(*mock_devices_[0], technology())
1232 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001233 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001234 EXPECT_CALL(*mock_devices_[1], technology())
1235 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001236 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001237 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001238 }
1239
1240 {
1241 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001242 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001243 EXPECT_EQ(Error::kInvalidArguments, error.type());
1244 }
1245}
1246
Darin Petkovb65c2452012-02-23 15:17:06 +01001247TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001248 KeyValueStore args;
1249 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001250 manager()->GetService(args, &e);
1251 EXPECT_EQ(Error::kInvalidArguments, e.type());
1252 EXPECT_EQ("must specify service type", e.message());
1253}
1254
1255TEST_F(ManagerTest, GetServiceUnknownType) {
1256 KeyValueStore args;
1257 Error e;
1258 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1259 manager()->GetService(args, &e);
1260 EXPECT_EQ(Error::kNotSupported, e.type());
1261 EXPECT_EQ("service type is unsupported", e.message());
1262}
1263
1264TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1265 KeyValueStore args;
1266 Error e;
1267 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1268 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001269 EXPECT_EQ(Error::kInvalidArguments, e.type());
1270 EXPECT_EQ("no wifi devices available", e.message());
1271}
1272
Darin Petkovb65c2452012-02-23 15:17:06 +01001273TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001274 KeyValueStore args;
1275 Error e;
1276 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001277 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001278 manager()->RegisterDevice(mock_wifi_);
1279 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1280 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001281 manager()->GetService(args, &e);
1282 EXPECT_TRUE(e.IsSuccess());
1283}
1284
Darin Petkov33af05c2012-02-28 10:10:30 +01001285TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1286 KeyValueStore args;
1287 Error e;
1288 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001289 scoped_refptr<MockProfile> profile(
1290 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1291 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001292 ServiceRefPtr service = manager()->GetService(args, &e);
1293 EXPECT_EQ(Error::kNotSupported, e.type());
1294 EXPECT_FALSE(service);
1295}
1296
Darin Petkovb65c2452012-02-23 15:17:06 +01001297TEST_F(ManagerTest, GetServiceVPN) {
1298 KeyValueStore args;
1299 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001300 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001301 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001302 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1303 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001304 scoped_refptr<MockProfile> profile(
1305 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1306 AdoptProfile(manager(), profile);
1307 ServiceRefPtr updated_service;
1308 EXPECT_CALL(*profile, UpdateService(_))
1309 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1310 ServiceRefPtr configured_service;
1311 EXPECT_CALL(*profile, ConfigureService(_))
1312 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001313 ServiceRefPtr service = manager()->GetService(args, &e);
1314 EXPECT_TRUE(e.IsSuccess());
1315 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001316 EXPECT_EQ(service, updated_service);
1317 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001318}
1319
Darin Petkovc63dcf02012-05-24 11:51:43 +02001320TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1321 KeyValueStore args;
1322 Error e;
1323 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1324 ServiceRefPtr service = manager()->GetService(args, &e);
1325 EXPECT_EQ(Error::kInvalidArguments, e.type());
1326 EXPECT_EQ("Missing WiMAX network id.", e.message());
1327 EXPECT_FALSE(service);
1328}
1329
Darin Petkovd1cd7972012-05-22 15:26:15 +02001330TEST_F(ManagerTest, GetServiceWiMax) {
1331 KeyValueStore args;
1332 Error e;
1333 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001334 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1335 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1336 ServiceRefPtr service = manager()->GetService(args, &e);
1337 EXPECT_TRUE(e.IsSuccess());
1338 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001339}
1340
Paul Stewart7f61e522012-03-22 11:13:45 -07001341TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1342 // Manager calls ActiveProfile() so we need at least one profile installed.
1343 scoped_refptr<MockProfile> profile(
1344 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1345 AdoptProfile(manager(), profile);
1346
1347 KeyValueStore args;
1348 args.SetString(flimflam::kProfileProperty, "xxx");
1349 Error error;
1350 manager()->ConfigureService(args, &error);
1351 EXPECT_EQ(Error::kInvalidArguments, error.type());
1352 EXPECT_EQ("Invalid profile name xxx", error.message());
1353}
1354
1355TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1356 // Manager calls ActiveProfile() so we need at least one profile installed.
1357 scoped_refptr<MockProfile> profile(
1358 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1359 AdoptProfile(manager(), profile);
1360
1361 KeyValueStore args;
1362 Error error;
1363 manager()->ConfigureService(args, &error);
1364 EXPECT_EQ(Error::kInvalidArguments, error.type());
1365 EXPECT_EQ("must specify service type", error.message());
1366}
1367
1368// A registered service in the ephemeral profile should be moved to the
1369// active profile as a part of configuration if no profile was explicitly
1370// specified.
1371TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1372 scoped_refptr<MockProfile> profile(
1373 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1374
1375 AdoptProfile(manager(), profile); // This is now the active profile.
1376
1377 const std::vector<uint8_t> ssid;
1378 scoped_refptr<MockWiFiService> service(
1379 new NiceMock<MockWiFiService>(control_interface(),
1380 dispatcher(),
1381 metrics(),
1382 manager(),
1383 mock_wifi_,
1384 ssid,
1385 "",
1386 "",
1387 false));
1388
1389 manager()->RegisterService(service);
1390 service->set_profile(GetEphemeralProfile(manager()));
1391
1392 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1393 // the same device as that used above causes a refcounting loop.
1394 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1395 dispatcher(),
1396 metrics(),
1397 manager(),
1398 "wifi1",
1399 "addr5",
1400 5));
1401 manager()->RegisterDevice(wifi);
1402 EXPECT_CALL(*wifi, GetService(_, _))
1403 .WillOnce(Return(service));
1404 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1405 .WillOnce(Return(true));
1406 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1407 .WillOnce(Return(true));
1408
1409 KeyValueStore args;
1410 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1411 Error error;
1412 manager()->ConfigureService(args, &error);
1413 EXPECT_TRUE(error.IsSuccess());
1414}
1415
1416// If were configure a service that was already registered and explicitly
1417// specify a profile, it should be moved from the profile it was previously
1418// in to the specified profile if one was requested.
1419TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1420 scoped_refptr<MockProfile> profile0(
1421 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1422 scoped_refptr<MockProfile> profile1(
1423 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1424
1425 const string kProfileName0 = "profile0";
1426 const string kProfileName1 = "profile1";
1427
1428 EXPECT_CALL(*profile0, GetRpcIdentifier())
1429 .WillRepeatedly(Return(kProfileName0));
1430 EXPECT_CALL(*profile1, GetRpcIdentifier())
1431 .WillRepeatedly(Return(kProfileName1));
1432
1433 AdoptProfile(manager(), profile0);
1434 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1435
1436 const std::vector<uint8_t> ssid;
1437 scoped_refptr<MockWiFiService> service(
1438 new NiceMock<MockWiFiService>(control_interface(),
1439 dispatcher(),
1440 metrics(),
1441 manager(),
1442 mock_wifi_,
1443 ssid,
1444 "",
1445 "",
1446 false));
1447
1448 manager()->RegisterService(service);
1449 service->set_profile(profile1);
1450
1451 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1452 // the same device as that used above causes a refcounting loop.
1453 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1454 dispatcher(),
1455 metrics(),
1456 manager(),
1457 "wifi1",
1458 "addr5",
1459 5));
1460 manager()->RegisterDevice(wifi);
1461 EXPECT_CALL(*wifi, GetService(_, _))
1462 .WillOnce(Return(service));
1463 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1464 .WillOnce(Return(true));
1465 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1466 .WillOnce(Return(true));
1467 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1468 .WillOnce(Return(true));
1469
1470 KeyValueStore args;
1471 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1472 args.SetString(flimflam::kProfileProperty, kProfileName0);
1473 Error error;
1474 manager()->ConfigureService(args, &error);
1475 EXPECT_TRUE(error.IsSuccess());
1476 service->set_profile(NULL); // Breaks refcounting loop.
1477}
1478
1479// An unregistered service should remain unregistered, but its contents should
1480// be saved to the specified profile nonetheless.
1481TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1482 scoped_refptr<MockProfile> profile0(
1483 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1484 scoped_refptr<MockProfile> profile1(
1485 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1486
1487 const string kProfileName0 = "profile0";
1488 const string kProfileName1 = "profile1";
1489
1490 EXPECT_CALL(*profile0, GetRpcIdentifier())
1491 .WillRepeatedly(Return(kProfileName0));
1492 EXPECT_CALL(*profile1, GetRpcIdentifier())
1493 .WillRepeatedly(Return(kProfileName1));
1494
1495 AdoptProfile(manager(), profile0);
1496 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1497
1498 const std::vector<uint8_t> ssid;
1499 scoped_refptr<MockWiFiService> service(
1500 new NiceMock<MockWiFiService>(control_interface(),
1501 dispatcher(),
1502 metrics(),
1503 manager(),
1504 mock_wifi_,
1505 ssid,
1506 "",
1507 "",
1508 false));
1509
1510 service->set_profile(profile1);
1511
1512 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1513 // the same device as that used above causes a refcounting loop.
1514 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1515 dispatcher(),
1516 metrics(),
1517 manager(),
1518 "wifi1",
1519 "addr5",
1520 5));
1521 manager()->RegisterDevice(wifi);
1522 EXPECT_CALL(*wifi, GetService(_, _))
1523 .WillOnce(Return(service));
1524 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1525 .WillOnce(Return(true));
1526 EXPECT_CALL(*profile0, AdoptService(_))
1527 .Times(0);
1528 EXPECT_CALL(*profile1, AdoptService(_))
1529 .Times(0);
1530
1531 KeyValueStore args;
1532 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1533 args.SetString(flimflam::kProfileProperty, kProfileName0);
1534 Error error;
1535 manager()->ConfigureService(args, &error);
1536 EXPECT_TRUE(error.IsSuccess());
1537}
1538
Paul Stewart22aa71b2011-09-16 12:15:11 -07001539TEST_F(ManagerTest, TechnologyOrder) {
1540 Error error;
1541 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1542 string(flimflam::kTypeWifi), &error);
1543 ASSERT_TRUE(error.IsSuccess());
1544 EXPECT_EQ(manager()->GetTechnologyOrder(),
1545 string(flimflam::kTypeEthernet) + "," +
1546 string(flimflam::kTypeWifi));
1547
1548 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1549 string(flimflam::kTypeWifi), &error);
1550 ASSERT_FALSE(error.IsSuccess());
1551 EXPECT_EQ(Error::kInvalidArguments, error.type());
1552 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1553 string(flimflam::kTypeWifi),
1554 manager()->GetTechnologyOrder());
1555}
1556
1557TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001558 // TODO(quiche): Some of these tests would probably fit better in
1559 // service_unittest, since the actual comparison of Services is
1560 // implemented in Service. (crosbug.com/23370)
1561
Paul Stewart22aa71b2011-09-16 12:15:11 -07001562 scoped_refptr<MockService> mock_service0(
1563 new NiceMock<MockService>(control_interface(),
1564 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001565 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001566 manager()));
1567 scoped_refptr<MockService> mock_service1(
1568 new NiceMock<MockService>(control_interface(),
1569 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001570 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001571 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001572
1573 manager()->RegisterService(mock_service0);
1574 manager()->RegisterService(mock_service1);
1575
1576 // Services should already be sorted by UniqueName
1577 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1578
1579 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001580 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001581 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1582
1583 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001584 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001585 manager()->UpdateService(mock_service1);
1586 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1587
1588 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001589 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001590 manager()->UpdateService(mock_service0);
1591 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1592
1593 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001594 EXPECT_CALL(*mock_service0.get(), technology())
1595 .WillRepeatedly(Return((Technology::kWifi)));
1596 EXPECT_CALL(*mock_service1.get(), technology())
1597 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001598
1599 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001600 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001601 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001602 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1603
1604 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1605 string(flimflam::kTypeEthernet), &error);
1606 EXPECT_TRUE(error.IsSuccess());
1607 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1608
Gaurav Shah435de2c2011-11-17 19:01:07 -08001609 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001610 mock_service0->set_priority(1);
1611 manager()->UpdateService(mock_service0);
1612 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1613
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001614 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001615 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001616 manager()->UpdateService(mock_service1);
1617 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1618
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001619 // Auto-connect.
1620 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001621 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001622 mock_service1->set_auto_connect(false);
1623 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001624 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1625
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001626 // Connectable.
1627 mock_service1->set_connectable(true);
1628 manager()->UpdateService(mock_service1);
1629 mock_service0->set_connectable(false);
1630 manager()->UpdateService(mock_service0);
1631 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1632
1633 // IsFailed.
1634 EXPECT_CALL(*mock_service0.get(), state())
1635 .WillRepeatedly(Return(Service::kStateIdle));
1636 EXPECT_CALL(*mock_service0.get(), IsFailed())
1637 .WillRepeatedly(Return(false));
1638 manager()->UpdateService(mock_service0);
1639 EXPECT_CALL(*mock_service0.get(), state())
1640 .WillRepeatedly(Return(Service::kStateFailure));
1641 EXPECT_CALL(*mock_service1.get(), IsFailed())
1642 .WillRepeatedly(Return(true));
1643 manager()->UpdateService(mock_service1);
1644 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1645
1646 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001647 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001648 .WillRepeatedly(Return(Service::kStateAssociating));
1649 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001650 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001651 manager()->UpdateService(mock_service1);
1652 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1653
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001654 // Connected.
1655 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001656 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001657 EXPECT_CALL(*mock_service0.get(), IsConnected())
1658 .WillRepeatedly(Return(true));
1659 manager()->UpdateService(mock_service0);
1660 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1661
Paul Stewarta121c442012-06-09 14:12:58 -07001662 // Portal.
1663 EXPECT_CALL(*mock_service1.get(), state())
1664 .WillRepeatedly(Return(Service::kStateConnected));
1665 EXPECT_CALL(*mock_service1.get(), IsConnected())
1666 .WillRepeatedly(Return(true));
1667 manager()->UpdateService(mock_service1);
1668 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1669
Paul Stewart22aa71b2011-09-16 12:15:11 -07001670 manager()->DeregisterService(mock_service0);
1671 manager()->DeregisterService(mock_service1);
1672}
1673
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001674TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001675 MockMetrics mock_metrics;
1676 manager()->set_metrics(&mock_metrics);
1677
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001678 scoped_refptr<MockService> mock_service0(
1679 new NiceMock<MockService>(control_interface(),
1680 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001681 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001682 manager()));
1683 scoped_refptr<MockService> mock_service1(
1684 new NiceMock<MockService>(control_interface(),
1685 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001686 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001687 manager()));
1688
1689 scoped_refptr<MockConnection> mock_connection0(
1690 new NiceMock<MockConnection>(device_info_.get()));
1691 scoped_refptr<MockConnection> mock_connection1(
1692 new NiceMock<MockConnection>(device_info_.get()));
1693
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001694 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001695 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001696 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001697 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001698 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001699 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001700
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001701 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001702 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001703
1704 mock_service1->set_priority(1);
1705 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001706 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001707
1708 mock_service1->set_priority(0);
1709 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001710 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001711
Paul Stewartce4ec192012-03-14 12:53:46 -07001712 mock_service0->set_mock_connection(mock_connection0);
1713 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001714
1715 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001716 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001717 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001718
Darin Petkova5e07ef2012-07-09 14:27:57 +02001719 ServiceWatcher service_watcher;
1720 int tag =
1721 manager()->RegisterDefaultServiceCallback(
1722 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1723 service_watcher.AsWeakPtr()));
1724 EXPECT_EQ(1, tag);
1725
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001726 mock_service1->set_priority(1);
1727 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1728 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001729 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001730 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001731 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001732
Darin Petkova5e07ef2012-07-09 14:27:57 +02001733 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001734 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001735 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001736 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001737 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001738 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001739 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001740
Paul Stewartce4ec192012-03-14 12:53:46 -07001741 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001742 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001743 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001744 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001745
1746 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001747 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001748}
1749
Darin Petkova5e07ef2012-07-09 14:27:57 +02001750TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1751 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1752 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1753
1754 MockMetrics mock_metrics;
1755 manager()->set_metrics(&mock_metrics);
1756
1757 scoped_refptr<MockService> mock_service(
1758 new NiceMock<MockService>(
1759 control_interface(), dispatcher(), metrics(), manager()));
1760 ServiceRefPtr service = mock_service;
1761 ServiceRefPtr null_service;
1762
1763 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1764 manager()->NotifyDefaultServiceChanged(null_service);
1765
1766 ServiceWatcher service_watcher1;
1767 ServiceWatcher service_watcher2;
1768 int tag1 =
1769 manager()->RegisterDefaultServiceCallback(
1770 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1771 service_watcher1.AsWeakPtr()));
1772 EXPECT_EQ(1, tag1);
1773 int tag2 =
1774 manager()->RegisterDefaultServiceCallback(
1775 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1776 service_watcher2.AsWeakPtr()));
1777 EXPECT_EQ(2, tag2);
1778
1779 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1780 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1781 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1782 manager()->NotifyDefaultServiceChanged(null_service);
1783
1784 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1785 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1786 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1787 manager()->NotifyDefaultServiceChanged(mock_service);
1788
1789 manager()->DeregisterDefaultServiceCallback(tag1);
1790 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1791 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1792 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1793 manager()->NotifyDefaultServiceChanged(mock_service);
1794 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1795
1796 manager()->DeregisterDefaultServiceCallback(tag2);
1797 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1798 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1799 manager()->NotifyDefaultServiceChanged(mock_service);
1800
1801 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1802 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1803}
1804
Gaurav Shah435de2c2011-11-17 19:01:07 -08001805TEST_F(ManagerTest, AvailableTechnologies) {
1806 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1807 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001808 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001809 manager(),
1810 "null4",
1811 "addr4",
1812 0));
1813 manager()->RegisterDevice(mock_devices_[0]);
1814 manager()->RegisterDevice(mock_devices_[1]);
1815 manager()->RegisterDevice(mock_devices_[2]);
1816 manager()->RegisterDevice(mock_devices_[3]);
1817
1818 ON_CALL(*mock_devices_[0].get(), technology())
1819 .WillByDefault(Return(Technology::kEthernet));
1820 ON_CALL(*mock_devices_[1].get(), technology())
1821 .WillByDefault(Return(Technology::kWifi));
1822 ON_CALL(*mock_devices_[2].get(), technology())
1823 .WillByDefault(Return(Technology::kCellular));
1824 ON_CALL(*mock_devices_[3].get(), technology())
1825 .WillByDefault(Return(Technology::kWifi));
1826
1827 set<string> expected_technologies;
1828 expected_technologies.insert(Technology::NameFromIdentifier(
1829 Technology::kEthernet));
1830 expected_technologies.insert(Technology::NameFromIdentifier(
1831 Technology::kWifi));
1832 expected_technologies.insert(Technology::NameFromIdentifier(
1833 Technology::kCellular));
1834 Error error;
1835 vector<string> technologies = manager()->AvailableTechnologies(&error);
1836
1837 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1838 ContainerEq(expected_technologies));
1839}
1840
1841TEST_F(ManagerTest, ConnectedTechnologies) {
1842 scoped_refptr<MockService> connected_service1(
1843 new NiceMock<MockService>(control_interface(),
1844 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001845 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001846 manager()));
1847 scoped_refptr<MockService> connected_service2(
1848 new NiceMock<MockService>(control_interface(),
1849 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001850 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001851 manager()));
1852 scoped_refptr<MockService> disconnected_service1(
1853 new NiceMock<MockService>(control_interface(),
1854 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001855 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001856 manager()));
1857 scoped_refptr<MockService> disconnected_service2(
1858 new NiceMock<MockService>(control_interface(),
1859 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001860 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001861 manager()));
1862
1863 ON_CALL(*connected_service1.get(), IsConnected())
1864 .WillByDefault(Return(true));
1865 ON_CALL(*connected_service2.get(), IsConnected())
1866 .WillByDefault(Return(true));
1867
1868 manager()->RegisterService(connected_service1);
1869 manager()->RegisterService(connected_service2);
1870 manager()->RegisterService(disconnected_service1);
1871 manager()->RegisterService(disconnected_service2);
1872
1873 manager()->RegisterDevice(mock_devices_[0]);
1874 manager()->RegisterDevice(mock_devices_[1]);
1875 manager()->RegisterDevice(mock_devices_[2]);
1876 manager()->RegisterDevice(mock_devices_[3]);
1877
1878 ON_CALL(*mock_devices_[0].get(), technology())
1879 .WillByDefault(Return(Technology::kEthernet));
1880 ON_CALL(*mock_devices_[1].get(), technology())
1881 .WillByDefault(Return(Technology::kWifi));
1882 ON_CALL(*mock_devices_[2].get(), technology())
1883 .WillByDefault(Return(Technology::kCellular));
1884 ON_CALL(*mock_devices_[3].get(), technology())
1885 .WillByDefault(Return(Technology::kWifi));
1886
1887 mock_devices_[0]->SelectService(connected_service1);
1888 mock_devices_[1]->SelectService(disconnected_service1);
1889 mock_devices_[2]->SelectService(disconnected_service2);
1890 mock_devices_[3]->SelectService(connected_service2);
1891
1892 set<string> expected_technologies;
1893 expected_technologies.insert(Technology::NameFromIdentifier(
1894 Technology::kEthernet));
1895 expected_technologies.insert(Technology::NameFromIdentifier(
1896 Technology::kWifi));
1897 Error error;
1898
1899 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1900 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1901 ContainerEq(expected_technologies));
1902}
1903
1904TEST_F(ManagerTest, DefaultTechnology) {
1905 scoped_refptr<MockService> connected_service(
1906 new NiceMock<MockService>(control_interface(),
1907 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001908 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001909 manager()));
1910 scoped_refptr<MockService> disconnected_service(
1911 new NiceMock<MockService>(control_interface(),
1912 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001913 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001914 manager()));
1915
1916 // Connected. WiFi.
1917 ON_CALL(*connected_service.get(), IsConnected())
1918 .WillByDefault(Return(true));
1919 ON_CALL(*connected_service.get(), state())
1920 .WillByDefault(Return(Service::kStateConnected));
1921 ON_CALL(*connected_service.get(), technology())
1922 .WillByDefault(Return(Technology::kWifi));
1923
1924 // Disconnected. Ethernet.
1925 ON_CALL(*disconnected_service.get(), technology())
1926 .WillByDefault(Return(Technology::kEthernet));
1927
1928 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001929 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001930 Error error;
1931 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1932
1933
1934 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001935 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001936 // Connected service should be brought to the front now.
1937 string expected_technology =
1938 Technology::NameFromIdentifier(Technology::kWifi);
1939 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1940}
1941
Paul Stewart212d60f2012-07-12 10:59:13 -07001942TEST_F(ManagerTest, Stop) {
1943 scoped_refptr<MockProfile> profile(
1944 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1945 AdoptProfile(manager(), profile);
1946 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00001947 new NiceMock<MockService>(control_interface(),
1948 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001949 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001950 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07001951 manager()->RegisterService(service);
1952 manager()->RegisterDevice(mock_devices_[0]);
1953 EXPECT_CALL(*profile.get(),
1954 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
1955 .WillOnce(Return(true));
1956 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
1957 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001958 manager()->Stop();
1959}
1960
mukesh agrawal00917ce2011-11-22 23:56:55 +00001961TEST_F(ManagerTest, UpdateServiceConnected) {
1962 scoped_refptr<MockService> mock_service(
1963 new NiceMock<MockService>(control_interface(),
1964 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001965 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001966 manager()));
1967 manager()->RegisterService(mock_service);
1968 EXPECT_FALSE(mock_service->favorite());
1969 EXPECT_FALSE(mock_service->auto_connect());
1970
Gaurav Shah435de2c2011-11-17 19:01:07 -08001971 EXPECT_CALL(*mock_service.get(), IsConnected())
1972 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001973 manager()->UpdateService(mock_service);
1974 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1975 // to mock out MakeFavorite. And mocking that out would break the
1976 // SortServices test. (crosbug.com/23370)
1977 EXPECT_TRUE(mock_service->favorite());
1978 EXPECT_TRUE(mock_service->auto_connect());
1979}
1980
Thieu Led4e9e552012-02-16 16:26:07 -08001981TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
1982 // This tests the case where the user connects to a service that is
1983 // currently associated with a profile. We want to make sure that the
1984 // favorite flag is set and that the flag is saved to the current
1985 // profile.
1986 scoped_refptr<MockService> mock_service(
1987 new NiceMock<MockService>(control_interface(),
1988 dispatcher(),
1989 metrics(),
1990 manager()));
1991 manager()->RegisterService(mock_service);
1992 EXPECT_FALSE(mock_service->favorite());
1993 EXPECT_FALSE(mock_service->auto_connect());
1994
Gary Moraind93615e2012-04-27 11:50:03 -07001995 scoped_refptr<MockProfile> profile(
1996 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08001997
Gary Moraind93615e2012-04-27 11:50:03 -07001998 mock_service->set_profile(profile);
1999 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002000 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002001 EXPECT_CALL(*profile,
2002 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002003 manager()->UpdateService(mock_service);
2004 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2005 // to mock out MakeFavorite. And mocking that out would break the
2006 // SortServices test. (crosbug.com/23370)
2007 EXPECT_TRUE(mock_service->favorite());
2008 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002009 // This releases the ref on the mock profile.
2010 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002011}
2012
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002013TEST_F(ManagerTest, SaveSuccessfulService) {
2014 scoped_refptr<MockProfile> profile(
2015 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2016 AdoptProfile(manager(), profile);
2017 scoped_refptr<MockService> service(
2018 new NiceMock<MockService>(control_interface(),
2019 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002020 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002021 manager()));
2022
2023 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2024 ServiceRefPtr expect_service(service.get());
2025
2026 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2027 .WillOnce(Return(false));
2028 manager()->RegisterService(service);
2029
2030 EXPECT_CALL(*service.get(), state())
2031 .WillRepeatedly(Return(Service::kStateConnected));
2032 EXPECT_CALL(*service.get(), IsConnected())
2033 .WillRepeatedly(Return(true));
2034 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2035 .WillOnce(Return(true));
2036 manager()->UpdateService(service);
2037}
2038
Darin Petkove7c6ad32012-06-29 10:22:09 +02002039TEST_F(ManagerTest, UpdateDevice) {
2040 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2041 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2042 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2043 AdoptProfile(manager(), profile0); // Passes ownership.
2044 AdoptProfile(manager(), profile1); // Passes ownership.
2045 AdoptProfile(manager(), profile2); // Passes ownership.
2046 DeviceRefPtr device_ref(mock_devices_[0].get());
2047 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2048 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2049 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2050 manager()->UpdateDevice(mock_devices_[0]);
2051}
2052
Paul Stewart1b253142012-01-26 14:05:52 -08002053TEST_F(ManagerTest, EnumerateProfiles) {
2054 vector<string> profile_paths;
2055 for (size_t i = 0; i < 10; i++) {
2056 scoped_refptr<MockProfile> profile(
2057 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002058 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002059 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2060 .WillOnce(Return(profile_paths.back()));
2061 AdoptProfile(manager(), profile);
2062 }
2063
2064 Error error;
2065 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2066 EXPECT_TRUE(error.IsSuccess());
2067 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2068 for (size_t i = 0; i < profile_paths.size(); i++) {
2069 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2070 }
2071}
2072
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002073TEST_F(ManagerTest, AutoConnectOnRegister) {
2074 MockServiceRefPtr service = MakeAutoConnectableService();
2075 EXPECT_CALL(*service.get(), AutoConnect());
2076 manager()->RegisterService(service);
2077 dispatcher()->DispatchPendingEvents();
2078}
2079
2080TEST_F(ManagerTest, AutoConnectOnUpdate) {
2081 MockServiceRefPtr service1 = MakeAutoConnectableService();
2082 service1->set_priority(1);
2083 MockServiceRefPtr service2 = MakeAutoConnectableService();
2084 service2->set_priority(2);
2085 manager()->RegisterService(service1);
2086 manager()->RegisterService(service2);
2087 dispatcher()->DispatchPendingEvents();
2088
2089 EXPECT_CALL(*service1.get(), AutoConnect());
2090 EXPECT_CALL(*service2.get(), state())
2091 .WillRepeatedly(Return(Service::kStateFailure));
2092 EXPECT_CALL(*service2.get(), IsFailed())
2093 .WillRepeatedly(Return(true));
2094 EXPECT_CALL(*service2.get(), IsConnected())
2095 .WillRepeatedly(Return(false));
2096 manager()->UpdateService(service2);
2097 dispatcher()->DispatchPendingEvents();
2098}
2099
2100TEST_F(ManagerTest, AutoConnectOnDeregister) {
2101 MockServiceRefPtr service1 = MakeAutoConnectableService();
2102 service1->set_priority(1);
2103 MockServiceRefPtr service2 = MakeAutoConnectableService();
2104 service2->set_priority(2);
2105 manager()->RegisterService(service1);
2106 manager()->RegisterService(service2);
2107 dispatcher()->DispatchPendingEvents();
2108
2109 EXPECT_CALL(*service1.get(), AutoConnect());
2110 manager()->DeregisterService(service2);
2111 dispatcher()->DispatchPendingEvents();
2112}
2113
Darin Petkovca621542012-07-25 14:25:56 +02002114TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2115 MockServiceRefPtr service = MakeAutoConnectableService();
2116 SetPowerState(PowerManagerProxyDelegate::kMem);
2117 SetPowerManager();
2118 EXPECT_CALL(*service, AutoConnect()).Times(0);
2119 manager()->RegisterService(service);
2120 dispatcher()->DispatchPendingEvents();
2121}
2122
2123TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2124 MockServiceRefPtr service = MakeAutoConnectableService();
2125 SetPowerState(PowerManagerProxyDelegate::kOn);
2126 SetPowerManager();
2127 EXPECT_CALL(*service, AutoConnect());
2128 manager()->RegisterService(service);
2129 dispatcher()->DispatchPendingEvents();
2130}
2131
2132TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2133 MockServiceRefPtr service = MakeAutoConnectableService();
2134 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2135 SetPowerManager();
2136 EXPECT_CALL(*service, AutoConnect());
2137 manager()->RegisterService(service);
2138 dispatcher()->DispatchPendingEvents();
2139}
2140
2141TEST_F(ManagerTest, OnPowerStateChanged) {
2142 MockServiceRefPtr service = MakeAutoConnectableService();
2143 SetPowerState(PowerManagerProxyDelegate::kOn);
2144 SetPowerManager();
2145 EXPECT_CALL(*service, AutoConnect());
2146 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002147 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002148 dispatcher()->DispatchPendingEvents();
2149
mukesh agrawal784566d2012-08-08 18:32:58 -07002150 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002151 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2152 EXPECT_CALL(*service, AutoConnect());
2153 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002154 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002155
mukesh agrawal784566d2012-08-08 18:32:58 -07002156 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002157 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2158 EXPECT_CALL(*service, AutoConnect()).Times(0);
2159 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002160 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002161}
2162
Paul Stewartc681fa02012-03-02 19:40:04 -08002163TEST_F(ManagerTest, RecheckPortal) {
2164 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2165 .WillOnce(Return(false));
2166 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2167 .WillOnce(Return(true));
2168 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2169 .Times(0);
2170
2171 manager()->RegisterDevice(mock_devices_[0]);
2172 manager()->RegisterDevice(mock_devices_[1]);
2173 manager()->RegisterDevice(mock_devices_[2]);
2174
2175 manager()->RecheckPortal(NULL);
2176}
2177
Paul Stewartd215af62012-04-24 23:25:50 -07002178TEST_F(ManagerTest, RecheckPortalOnService) {
2179 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2180 dispatcher(),
2181 metrics(),
2182 manager());
2183 EXPECT_CALL(*mock_devices_[0].get(),
2184 IsConnectedToService(IsRefPtrTo(service)))
2185 .WillOnce(Return(false));
2186 EXPECT_CALL(*mock_devices_[1].get(),
2187 IsConnectedToService(IsRefPtrTo(service)))
2188 .WillOnce(Return(true));
2189 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2190 .WillOnce(Return(true));
2191 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2192 .Times(0);
2193
2194 manager()->RegisterDevice(mock_devices_[0]);
2195 manager()->RegisterDevice(mock_devices_[1]);
2196 manager()->RegisterDevice(mock_devices_[2]);
2197
2198 manager()->RecheckPortalOnService(service);
2199}
2200
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002201TEST_F(ManagerTest, GetDefaultService) {
2202 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002203 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002204
2205 scoped_refptr<MockService> mock_service(
2206 new NiceMock<MockService>(control_interface(),
2207 dispatcher(),
2208 metrics(),
2209 manager()));
2210
2211 manager()->RegisterService(mock_service);
2212 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002213 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002214
2215 scoped_refptr<MockConnection> mock_connection(
2216 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002217 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002218 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002219 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002220
Paul Stewartce4ec192012-03-14 12:53:46 -07002221 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002222 manager()->DeregisterService(mock_service);
2223}
2224
Paul Stewart13ed2252012-03-21 12:52:46 -07002225TEST_F(ManagerTest, GetServiceWithGUID) {
2226 scoped_refptr<MockService> mock_service0(
2227 new NiceMock<MockService>(control_interface(),
2228 dispatcher(),
2229 metrics(),
2230 manager()));
2231
2232 scoped_refptr<MockService> mock_service1(
2233 new NiceMock<MockService>(control_interface(),
2234 dispatcher(),
2235 metrics(),
2236 manager()));
2237
Paul Stewartcb59fed2012-03-21 21:14:46 -07002238 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2239 .Times(0);
2240 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2241 .Times(0);
2242
Paul Stewart13ed2252012-03-21 12:52:46 -07002243 manager()->RegisterService(mock_service0);
2244 manager()->RegisterService(mock_service1);
2245
2246 const string kGUID0 = "GUID0";
2247 const string kGUID1 = "GUID1";
2248
2249 {
2250 Error error;
2251 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2252 EXPECT_FALSE(error.IsSuccess());
2253 EXPECT_FALSE(service);
2254 }
2255
2256 KeyValueStore args;
2257 args.SetString(flimflam::kGuidProperty, kGUID1);
2258
2259 {
2260 Error error;
2261 ServiceRefPtr service = manager()->GetService(args, &error);
2262 EXPECT_EQ(Error::kInvalidArguments, error.type());
2263 EXPECT_FALSE(service);
2264 }
2265
2266 mock_service0->set_guid(kGUID0);
2267 mock_service1->set_guid(kGUID1);
2268
2269 {
2270 Error error;
2271 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2272 EXPECT_TRUE(error.IsSuccess());
2273 EXPECT_EQ(mock_service0.get(), service.get());
2274 }
2275
2276 {
2277 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002278 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2279 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002280 ServiceRefPtr service = manager()->GetService(args, &error);
2281 EXPECT_TRUE(error.IsSuccess());
2282 EXPECT_EQ(mock_service1.get(), service.get());
2283 }
2284
2285 manager()->DeregisterService(mock_service0);
2286 manager()->DeregisterService(mock_service1);
2287}
2288
Gary Morain028545d2012-04-07 14:55:52 -07002289
2290TEST_F(ManagerTest, CalculateStateOffline) {
2291 MockMetrics mock_metrics;
2292 manager()->set_metrics(&mock_metrics);
2293 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2294 .Times(AnyNumber());
2295 scoped_refptr<MockService> mock_service0(
2296 new NiceMock<MockService>(control_interface(),
2297 dispatcher(),
2298 metrics(),
2299 manager()));
2300
2301 scoped_refptr<MockService> mock_service1(
2302 new NiceMock<MockService>(control_interface(),
2303 dispatcher(),
2304 metrics(),
2305 manager()));
2306
2307 EXPECT_CALL(*mock_service0.get(), IsConnected())
2308 .WillRepeatedly(Return(false));
2309 EXPECT_CALL(*mock_service1.get(), IsConnected())
2310 .WillRepeatedly(Return(false));
2311
2312 manager()->RegisterService(mock_service0);
2313 manager()->RegisterService(mock_service1);
2314
2315 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2316
2317 manager()->DeregisterService(mock_service0);
2318 manager()->DeregisterService(mock_service1);
2319}
2320
2321TEST_F(ManagerTest, CalculateStateOnline) {
2322 MockMetrics mock_metrics;
2323 manager()->set_metrics(&mock_metrics);
2324 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2325 .Times(AnyNumber());
2326 scoped_refptr<MockService> mock_service0(
2327 new NiceMock<MockService>(control_interface(),
2328 dispatcher(),
2329 metrics(),
2330 manager()));
2331
2332 scoped_refptr<MockService> mock_service1(
2333 new NiceMock<MockService>(control_interface(),
2334 dispatcher(),
2335 metrics(),
2336 manager()));
2337
2338 EXPECT_CALL(*mock_service0.get(), IsConnected())
2339 .WillRepeatedly(Return(false));
2340 EXPECT_CALL(*mock_service1.get(), IsConnected())
2341 .WillRepeatedly(Return(true));
2342 EXPECT_CALL(*mock_service0.get(), state())
2343 .WillRepeatedly(Return(Service::kStateIdle));
2344 EXPECT_CALL(*mock_service1.get(), state())
2345 .WillRepeatedly(Return(Service::kStateConnected));
2346
2347 manager()->RegisterService(mock_service0);
2348 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002349 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002350
2351 EXPECT_EQ("online", manager()->CalculateState(NULL));
2352
2353 manager()->DeregisterService(mock_service0);
2354 manager()->DeregisterService(mock_service1);
2355}
2356
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002357TEST_F(ManagerTest, StartupPortalList) {
2358 // Simulate loading value from the default profile.
2359 const string kProfileValue("wifi,vpn");
2360 manager()->props_.check_portal_list = kProfileValue;
2361
2362 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2363 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2364 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2365
2366 const string kStartupValue("cellular,ethernet");
2367 manager()->SetStartupPortalList(kStartupValue);
2368 // Ensure profile value is not overwritten, so when we save the default
2369 // profile, the correct value will still be written.
2370 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2371
2372 // However we should read back a different list.
2373 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2374 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2375 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2376
2377 const string kRuntimeValue("ppp");
2378 // Setting a runtime value over the control API should overwrite both
2379 // the profile value and what we read back.
2380 Error error;
2381 manager()->mutable_store()->SetStringProperty(
2382 flimflam::kCheckPortalListProperty,
2383 kRuntimeValue,
2384 &error);
2385 ASSERT_TRUE(error.IsSuccess());
2386 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2387 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2388 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2389 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2390}
2391
Paul Stewart036dba02012-08-07 12:34:41 -07002392TEST_F(ManagerTest, LinkMonitorEnabled) {
2393 const string kEnabledTechnologies("wifi,vpn");
2394 manager()->props_.link_monitor_technologies = kEnabledTechnologies;
2395 EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi));
2396 EXPECT_FALSE(
2397 manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular));
2398}
2399
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002400TEST_F(ManagerTest, EnableTechnology) {
2401 Error error(Error::kOperationInitiated);
2402 ResultCallback callback;
2403 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2404 EXPECT_TRUE(error.IsSuccess());
2405
Joshua Krollda798622012-06-05 12:30:48 -07002406 ON_CALL(*mock_devices_[0], technology())
2407 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002408
2409 manager()->RegisterDevice(mock_devices_[0]);
2410
2411 // Device is enabled, so expect operation is successful.
2412 mock_devices_[0]->enabled_ = true;
2413 error.Populate(Error::kOperationInitiated);
2414 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2415 EXPECT_TRUE(error.IsSuccess());
2416
2417 // Device is disabled, so expect operation in progress.
2418 mock_devices_[0]->enabled_ = false;
2419 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2420 error.Populate(Error::kOperationInitiated);
2421 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2422 EXPECT_TRUE(error.IsOngoing());
2423}
2424
2425TEST_F(ManagerTest, DisableTechnology) {
2426 Error error(Error::kOperationInitiated);
2427 ResultCallback callback;
2428 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2429 EXPECT_TRUE(error.IsSuccess());
2430
Joshua Krollda798622012-06-05 12:30:48 -07002431 ON_CALL(*mock_devices_[0], technology())
2432 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002433
2434 manager()->RegisterDevice(mock_devices_[0]);
2435
2436 // Device is disabled, so expect operation is successful.
2437 error.Populate(Error::kOperationInitiated);
2438 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2439 EXPECT_TRUE(error.IsSuccess());
2440
2441 // Device is enabled, so expect operation in progress.
2442 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2443 mock_devices_[0]->enabled_ = true;
2444 error.Populate(Error::kOperationInitiated);
2445 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2446 EXPECT_TRUE(error.IsOngoing());
2447}
2448
Chris Masone9be4a9d2011-05-16 15:44:09 -07002449} // namespace shill