blob: 635a659753c4a3b8af7160ba7d20c4bf4d799331 [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>
Chris Masone9be4a9d2011-05-16 15:44:09 -070013#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050014#include <base/scoped_temp_dir.h>
15#include <base/stl_util.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070016#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070018#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070019#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070020
mukesh agrawal32399322011-09-01 10:53:43 -070021#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070022#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070023#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070024#include "shill/glib.h"
25#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070026#include "shill/key_value_store.h"
mukesh agrawal32399322011-09-01 10:53:43 -070027#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080028#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070030#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080031#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070032#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000033#include "shill/mock_metrics.h"
Darin Petkovca621542012-07-25 14:25:56 +020034#include "shill/mock_power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070035#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070036#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070037#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070039#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070040#include "shill/property_store_unittest.h"
Darin Petkovca621542012-07-25 14:25:56 +020041#include "shill/proxy_factory.h"
Chris Masone6515aab2011-10-12 16:19:09 -070042#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070043#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020044#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070045
46using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070047using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070048using std::string;
49using std::vector;
50
Chris Masone9be4a9d2011-05-16 15:44:09 -070051namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070052using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070053using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080054using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070055using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070056using ::testing::InSequence;
mukesh agrawal784566d2012-08-08 18:32:58 -070057using ::testing::Mock;
Paul Stewart22aa71b2011-09-16 12:15:11 -070058using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070059using ::testing::NiceMock;
60using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070061using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070062using ::testing::SaveArg;
Gaurav Shah435de2c2011-11-17 19:01:07 -080063using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080064using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070065using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070066
Chris Masone3bd3c8c2011-06-13 08:20:26 -070067class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070068 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070069 ManagerTest()
Darin Petkovca621542012-07-25 14:25:56 +020070 : power_manager_(new MockPowerManager(&proxy_factory_)),
71 mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070072 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080073 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070074 manager(),
75 "wifi0",
76 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080077 4)),
78 device_info_(new NiceMock<MockDeviceInfo>(
79 control_interface(),
80 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080082 reinterpret_cast<Manager*>(NULL))),
83 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070084 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
85 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080086 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070087 manager(),
88 "null0",
89 "addr0",
90 0));
91 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
92 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080093 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070094 manager(),
95 "null1",
96 "addr1",
97 1));
98 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
99 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800100 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700101 manager(),
102 "null2",
103 "addr2",
104 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800105 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
106 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800108 manager(),
109 "null3",
110 "addr3",
111 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700112 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800113
114 // Replace the manager's adaptor with a quieter one, and one
115 // we can do EXPECT*() against. Passes ownership.
116 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700117 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700118 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700119
Paul Stewartfdd16072011-09-16 12:41:35 -0700120 bool IsDeviceRegistered(const DeviceRefPtr &device,
121 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700122 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700123 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700124 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700125 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700126 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700127
Paul Stewarta849a3d2011-11-03 05:54:09 -0700128 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
129 manager->profiles_.push_back(profile);
130 }
131
Paul Stewart75225512012-01-26 22:51:33 -0800132 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
133 return manager->ephemeral_profile_;
134 }
135
Chris Masone6515aab2011-10-12 16:19:09 -0700136 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
137 Profile::Identifier id("rather", "irrelevant");
138 scoped_ptr<Profile> profile(new Profile(control_interface(),
139 manager,
140 id,
141 "",
142 false));
143 FilePath final_path(storage_path());
144 final_path = final_path.Append("test.profile");
145 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
146 storage->set_path(final_path);
147 if (!storage->Open())
148 return NULL;
149 profile->set_storage(storage.release()); // Passes ownership.
150 return profile.release();
151 }
152
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700153 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
154 const string &profile_identifier,
155 const string &service_name) {
156 GLib glib;
157 KeyFileStore store(&glib);
158 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
159 return store.Open() &&
160 store.SetString(service_name, "rather", "irrelevant") &&
161 store.Close();
162 }
163
164 Error::Type TestCreateProfile(Manager *manager, const string &name) {
165 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800166 string path;
167 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700168 return error.type();
169 }
170
171 Error::Type TestPopAnyProfile(Manager *manager) {
172 Error error;
173 manager->PopAnyProfile(&error);
174 return error.type();
175 }
176
177 Error::Type TestPopProfile(Manager *manager, const string &name) {
178 Error error;
179 manager->PopProfile(name, &error);
180 return error.type();
181 }
182
183 Error::Type TestPushProfile(Manager *manager, const string &name) {
184 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800185 string path;
186 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700187 return error.type();
188 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000189
Paul Stewartcb3eb892012-06-07 14:24:46 -0700190 void AddMockProfileToManager(Manager *manager) {
191 scoped_refptr<MockProfile> profile(
192 new MockProfile(control_interface(), manager, ""));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200193 EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/"));
194 EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false));
Paul Stewartcb3eb892012-06-07 14:24:46 -0700195 AdoptProfile(manager, profile);
196 }
197
Paul Stewartdfa46052012-06-26 09:44:14 -0700198 void CompleteServiceSort() {
199 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
200 dispatcher()->DispatchPendingEvents();
201 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
202 }
203
Paul Stewart49739c02012-08-08 17:24:03 -0700204 RpcIdentifier GetDefaultServiceRpcIdentifier() {
205 return manager()->GetDefaultServiceRpcIdentifier(NULL);
206 }
207
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700208 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000209 typedef scoped_refptr<MockService> MockServiceRefPtr;
210
Darin Petkova5e07ef2012-07-09 14:27:57 +0200211 class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> {
212 public:
213 ServiceWatcher() {}
214 virtual ~ServiceWatcher() {}
215
216 MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service));
217
218 private:
219 DISALLOW_COPY_AND_ASSIGN(ServiceWatcher);
220 };
221
Darin Petkovca621542012-07-25 14:25:56 +0200222 class TestProxyFactory : public ProxyFactory {
223 public:
224 TestProxyFactory() {}
225
226 virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
227 PowerManagerProxyDelegate */*delegate*/) {
228 return NULL;
229 }
230
231 private:
232 DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
233 };
234
235 void SetPowerState(PowerManagerProxyDelegate::SuspendState state) {
236 power_manager_->power_state_ = state;
237 }
238
239 void SetPowerManager() {
240 manager()->set_power_manager(power_manager_.release());
241 }
242
243 void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) {
244 manager()->OnPowerStateChanged(state);
245 }
246
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000247 MockServiceRefPtr MakeAutoConnectableService() {
248 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
249 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800250 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000251 manager());
252 service->MakeFavorite();
253 service->set_connectable(true);
254 return service;
255 }
256
Darin Petkovca621542012-07-25 14:25:56 +0200257 TestProxyFactory proxy_factory_;
258 scoped_ptr<MockPowerManager> power_manager_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700259 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700260 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800261 scoped_ptr<MockDeviceInfo> device_info_;
262
263 // This pointer is owned by the manager, and only tracked here for EXPECT*()
264 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700265};
266
Paul Stewart22aa71b2011-09-16 12:15:11 -0700267bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700268 if (!manager()->sort_services_task_.IsCancelled()) {
269 manager()->SortServicesTask();
270 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700271 return (svc0.get() == manager()->services_[0].get() &&
272 svc1.get() == manager()->services_[1].get());
273}
274
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700275TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700276 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
277 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700278}
279
Chris Masone9be4a9d2011-05-16 15:44:09 -0700280TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700281 ON_CALL(*mock_devices_[0].get(), technology())
282 .WillByDefault(Return(Technology::kEthernet));
283 ON_CALL(*mock_devices_[1].get(), technology())
284 .WillByDefault(Return(Technology::kWifi));
285 ON_CALL(*mock_devices_[2].get(), technology())
286 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700287
Paul Stewart22aa71b2011-09-16 12:15:11 -0700288 manager()->RegisterDevice(mock_devices_[0]);
289 manager()->RegisterDevice(mock_devices_[1]);
290 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700291
Paul Stewart22aa71b2011-09-16 12:15:11 -0700292 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
293 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
294 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700295}
296
Paul Stewarta41e38d2011-11-11 07:47:29 -0800297TEST_F(ManagerTest, DeviceRegistrationAndStart) {
298 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500299 mock_devices_[0]->enabled_persistent_ = true;
300 mock_devices_[1]->enabled_persistent_ = false;
301 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800302 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500303 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800304 .Times(0);
305 manager()->RegisterDevice(mock_devices_[0]);
306 manager()->RegisterDevice(mock_devices_[1]);
307}
308
309TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
310 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
311 DeviceRefPtr device_ref(mock_devices_[0].get());
312 AdoptProfile(manager(), profile); // Passes ownership.
313 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
Darin Petkove7c6ad32012-06-29 10:22:09 +0200314 EXPECT_CALL(*profile, UpdateDevice(device_ref));
Paul Stewarta41e38d2011-11-11 07:47:29 -0800315 manager()->RegisterDevice(mock_devices_[0]);
316}
317
Chris Masone9be4a9d2011-05-16 15:44:09 -0700318TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700319 ON_CALL(*mock_devices_[0].get(), technology())
320 .WillByDefault(Return(Technology::kEthernet));
321 ON_CALL(*mock_devices_[1].get(), technology())
322 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700323
Gaurav Shah435de2c2011-11-17 19:01:07 -0800324 manager()->RegisterDevice(mock_devices_[0]);
325 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700326
Paul Stewart22aa71b2011-09-16 12:15:11 -0700327 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
328 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700329
Paul Stewart212d60f2012-07-12 10:59:13 -0700330 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
331 AdoptProfile(manager(), profile); // Passes ownership.
332
Eric Shienbrood9a245532012-03-07 14:20:39 -0500333 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700334 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800335 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700336 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700337
Eric Shienbrood9a245532012-03-07 14:20:39 -0500338 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Paul Stewart212d60f2012-07-12 10:59:13 -0700339 EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800340 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700341 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700342}
343
344TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700345 // It's much easier and safer to use a real GLib for this test.
346 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700347 Manager manager(control_interface(),
348 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800349 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700350 &glib,
351 run_path(),
352 storage_path(),
353 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700354 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
355 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700356 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700357
Chris Masone9be4a9d2011-05-16 15:44:09 -0700358 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700359 new NiceMock<MockService>(control_interface(),
360 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800361 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700362 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700363 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700364 new NiceMock<MockService>(control_interface(),
365 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800366 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700367 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700368
mukesh agrawal51a7e932011-07-27 16:18:26 -0700369 string service1_name(mock_service->UniqueName());
370 string service2_name(mock_service2->UniqueName());
371
372 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
373 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700374 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700375 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700376 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700377 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700378 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700379
Chris Masone9d779932011-08-25 16:33:41 -0700380 manager.RegisterService(mock_service);
381 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700382
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800383 Error error;
384 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700385 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700386 EXPECT_EQ(2, ids.size());
387 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
388 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700389
Chris Masone9d779932011-08-25 16:33:41 -0700390 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
391 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
392
393 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700394}
395
Chris Masone6515aab2011-10-12 16:19:09 -0700396TEST_F(ManagerTest, RegisterKnownService) {
397 // It's much easier and safer to use a real GLib for this test.
398 GLib glib;
399 Manager manager(control_interface(),
400 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800401 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700402 &glib,
403 run_path(),
404 storage_path(),
405 string());
406 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
407 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700408 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700409 {
410 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
411 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800412 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700413 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700414 ASSERT_TRUE(profile->AdoptService(service1));
415 ASSERT_TRUE(profile->ContainsService(service1));
416 } // Force destruction of service1.
417
418 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
419 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800420 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700421 &manager));
422 manager.RegisterService(service2);
423 EXPECT_EQ(service2->profile().get(), profile.get());
424 manager.Stop();
425}
426
427TEST_F(ManagerTest, RegisterUnknownService) {
428 // It's much easier and safer to use a real GLib for this test.
429 GLib glib;
430 Manager manager(control_interface(),
431 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800432 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700433 &glib,
434 run_path(),
435 storage_path(),
436 string());
437 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
438 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700439 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700440 {
441 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
442 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800443 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700444 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700445 ASSERT_TRUE(profile->AdoptService(service1));
446 ASSERT_TRUE(profile->ContainsService(service1));
447 } // Force destruction of service1.
448 scoped_refptr<MockService> mock_service2(
449 new NiceMock<MockService>(control_interface(),
450 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800451 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700452 &manager));
453 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
454 .WillRepeatedly(Return(mock_service2->UniqueName()));
455 manager.RegisterService(mock_service2);
456 EXPECT_NE(mock_service2->profile().get(), profile.get());
457 manager.Stop();
458}
459
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000460TEST_F(ManagerTest, DeregisterUnregisteredService) {
461 // WiFi assumes that it can deregister a service that is not
462 // registered. (E.g. a hidden service can be deregistered when it
463 // loses its last endpoint, and again when WiFi is Stop()-ed.)
464 //
465 // So test that doing so doesn't cause a crash.
466 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
467 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800468 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000469 manager());
470 manager()->DeregisterService(service);
471}
472
Chris Masonea8a2c252011-06-27 22:16:30 -0700473TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700474 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700475 map<string, ::DBus::Variant> props;
476 Error error(Error::kInvalidProperty, "");
477 {
478 ::DBus::Error dbus_error;
479 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700480 manager()->mutable_store()->SetStringProperty(
481 flimflam::kCheckPortalListProperty,
482 expected,
483 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700484 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700485 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
486 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
487 expected);
488 }
489 {
490 ::DBus::Error dbus_error;
491 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700492 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
493 expected,
494 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700495 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700496 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
497 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
498 expected);
499 }
500}
501
Chris Masone3c3f6a12011-07-01 10:01:41 -0700502TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700503 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800504 manager()->RegisterDevice(mock_devices_[0]);
505 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700506 {
507 map<string, ::DBus::Variant> props;
508 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700509 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700510 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700511 vector < ::DBus::Path> devices =
512 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700513 EXPECT_EQ(2, devices.size());
514 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700515}
516
mukesh agrawal2366eed2012-03-20 18:21:50 -0700517TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700518 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700519 map<string, ::DBus::Variant> props;
520 ::DBus::Error dbus_error;
521 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
522 map<string, ::DBus::Variant>::const_iterator prop =
523 props.find(flimflam::kServicesProperty);
524 ASSERT_FALSE(prop == props.end());
525 const ::DBus::Variant &variant = prop->second;
526 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
527}
528
Chris Masone6791a432011-07-12 13:23:19 -0700529TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700530 Manager manager(control_interface(),
531 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800532 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700533 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700534 run_path(),
535 storage_path(),
536 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700537 scoped_refptr<MockService> s2(new MockService(control_interface(),
538 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800539 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700540 &manager));
541 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700542 {
Chris Masone6515aab2011-10-12 16:19:09 -0700543 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700544 ProfileRefPtr profile(
545 new Profile(control_interface(), &manager, id, "", false));
546 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700547 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700548 .WillRepeatedly(Return(true));
549 EXPECT_CALL(*storage, Flush())
550 .Times(AnyNumber())
551 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700552 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700553 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700554 }
Chris Masone6515aab2011-10-12 16:19:09 -0700555 // Create a profile that already has |s2| in it.
556 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700557 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700558
Chris Masone6515aab2011-10-12 16:19:09 -0700559 // Now, move the Service |s2| to another profile.
560 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
561 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700562
563 // Force destruction of the original Profile, to ensure that the Service
564 // is kept alive and populated with data.
565 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700566 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700567 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700568}
569
Paul Stewart7f61e522012-03-22 11:13:45 -0700570TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
571 scoped_refptr<MockProfile> mock_profile(
572 new MockProfile(control_interface(), manager(), ""));
573 const string kProfileName("profile0");
574 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
575 .WillRepeatedly(Return(kProfileName));
576 AdoptProfile(manager(), mock_profile);
577
578 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
579 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
580 EXPECT_EQ(mock_profile.get(), profile.get());
581}
582
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800583TEST_F(ManagerTest, SetProfileForService) {
584 scoped_refptr<MockProfile> profile0(
585 new MockProfile(control_interface(), manager(), ""));
586 string profile_name0("profile0");
587 EXPECT_CALL(*profile0, GetRpcIdentifier())
588 .WillRepeatedly(Return(profile_name0));
589 AdoptProfile(manager(), profile0);
590 scoped_refptr<MockService> service(new MockService(control_interface(),
591 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800592 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800593 manager()));
Paul Stewart649f3a42012-04-24 23:22:16 -0700594 EXPECT_FALSE(manager()->HasService(service));
595 {
596 Error error;
597 EXPECT_CALL(*profile0, AdoptService(_))
598 .WillOnce(Return(true));
599 // Expect that setting the profile of a service that does not already
600 // have one assigned does not cause a crash.
601 manager()->SetProfileForService(service, "profile0", &error);
602 EXPECT_TRUE(error.IsSuccess());
603 }
604
605 // The service should be registered as a side-effect of the profile being
606 // set for this service.
607 EXPECT_TRUE(manager()->HasService(service));
608
609 // Since we have mocked Profile::AdoptServie() above, the service's
610 // profile was not actually changed. Do so explicitly now.
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800611 service->set_profile(profile0);
612
613 {
614 Error error;
615 manager()->SetProfileForService(service, "foo", &error);
616 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700617 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800618 }
619
620 {
621 Error error;
622 manager()->SetProfileForService(service, profile_name0, &error);
623 EXPECT_EQ(Error::kInvalidArguments, error.type());
624 EXPECT_EQ("Service is already connected to this profile", error.message());
625 }
626
627 scoped_refptr<MockProfile> profile1(
628 new MockProfile(control_interface(), manager(), ""));
629 string profile_name1("profile1");
630 EXPECT_CALL(*profile1, GetRpcIdentifier())
631 .WillRepeatedly(Return(profile_name1));
632 AdoptProfile(manager(), profile1);
633
634 {
635 Error error;
636 EXPECT_CALL(*profile1, AdoptService(_))
637 .WillOnce(Return(true));
638 EXPECT_CALL(*profile0, AbandonService(_))
639 .WillOnce(Return(true));
640 manager()->SetProfileForService(service, profile_name1, &error);
641 EXPECT_TRUE(error.IsSuccess());
642 }
643}
644
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700645TEST_F(ManagerTest, CreateProfile) {
646 // It's much easier to use real Glib here since we want the storage
647 // side-effects.
648 GLib glib;
649 ScopedTempDir temp_dir;
650 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
651
652 Manager manager(control_interface(),
653 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800654 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700655 &glib,
656 run_path(),
657 storage_path(),
658 temp_dir.path().value());
659
660 // Invalid name should be rejected.
661 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
662
Paul Stewartd0a3b812012-03-28 22:48:22 -0700663 // A profile with invalid characters in it should similarly be rejected.
664 EXPECT_EQ(Error::kInvalidArguments,
665 TestCreateProfile(&manager, "valid_profile"));
666
667 // We should be able to create a machine profile.
668 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700669
Gary Morainb672d352012-04-25 09:19:06 -0700670 // We should succeed in creating a valid user profile. Verify the returned
671 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700672 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700673 {
674 Error error;
675 string path;
676 manager.CreateProfile(kProfile, &path, &error);
677 EXPECT_EQ(Error::kSuccess, error.type());
678 EXPECT_EQ("/profile_rpc", path);
679 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700680
681 // We should fail in creating it a second time (already exists).
682 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
683}
684
685TEST_F(ManagerTest, PushPopProfile) {
686 // It's much easier to use real Glib in creating a Manager for this
687 // test here since we want the storage side-effects.
688 GLib glib;
689 ScopedTempDir temp_dir;
690 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
691 Manager manager(control_interface(),
692 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800693 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700694 &glib,
695 run_path(),
696 storage_path(),
697 temp_dir.path().value());
698
699 // Pushing an invalid profile should fail.
700 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
701
Paul Stewartd0a3b812012-03-28 22:48:22 -0700702 // Pushing a default profile that does not exist should fail.
703 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700704
705 const char kProfile0[] = "~user/profile0";
706 const char kProfile1[] = "~user/profile1";
707
708 // Create a couple of profiles.
709 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
710 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
711
712 // Push these profiles on the stack.
713 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
714 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
715
716 // Pushing a profile a second time should fail.
717 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
718 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
719
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800720 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700721 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800722 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700723
724 // Make sure a profile name that doesn't exist fails.
725 const char kProfile2Id[] = "profile2";
726 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
727 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
728
729 // Create a new service, with a specific storage name.
730 scoped_refptr<MockService> service(
731 new NiceMock<MockService>(control_interface(),
732 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800733 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700734 &manager));
735 const char kServiceName[] = "service_storage_name";
736 EXPECT_CALL(*service.get(), GetStorageIdentifier())
737 .WillRepeatedly(Return(kServiceName));
738 EXPECT_CALL(*service.get(), Load(_))
739 .WillRepeatedly(Return(true));
740
741 // Add this service to the manager -- it should end up in the ephemeral
742 // profile.
743 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800744 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700745
746 // Create storage for a profile that contains the service storage name.
747 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
748 kServiceName));
749
750 // When we push the profile, the service should move away from the
751 // ephemeral profile to this new profile since it has an entry for
752 // this service.
753 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800754 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700755 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
756
757 // Insert another profile that should supersede ownership of the service.
758 const char kProfile3Id[] = "profile3";
759 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
760 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
761 kServiceName));
762 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
763 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
764
765 // Popping an invalid profile name should fail.
766 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
767
768 // Popping an profile that is not at the top of the stack should fail.
769 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
770
771 // Popping the top profile should succeed.
772 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
773
774 // Moreover the service should have switched profiles to profile 2.
775 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
776
777 // Popping the top profile should succeed.
778 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
779
780 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800781 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700782
783 // Pop the remaining two services off the stack.
784 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
785 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
786
787 // Next pop should fail with "stack is empty".
788 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700789
790 const char kMachineProfile0[] = "machineprofile0";
791 const char kMachineProfile1[] = "machineprofile1";
792 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
793 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
794
795 // Should be able to push a machine profile.
796 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
797
798 // Should be able to push a user profile atop a machine profile.
799 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
800
801 // Pushing a system-wide profile on top of a user profile should fail.
802 EXPECT_EQ(Error::kInvalidArguments,
803 TestPushProfile(&manager, kMachineProfile1));
804
805 // However if we pop the user profile, we should be able stack another
806 // machine profile on.
807 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
808 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700809}
810
Paul Stewarte73d05c2012-03-29 16:26:05 -0700811TEST_F(ManagerTest, RemoveProfile) {
812 // It's much easier to use real Glib in creating a Manager for this
813 // test here since we want the storage side-effects.
814 GLib glib;
815 ScopedTempDir temp_dir;
816 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
817 Manager manager(control_interface(),
818 dispatcher(),
819 metrics(),
820 &glib,
821 run_path(),
822 storage_path(),
823 temp_dir.path().value());
824
825 const char kProfile0[] = "profile0";
826 FilePath profile_path(
827 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
828
829 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
830 ASSERT_TRUE(file_util::PathExists(profile_path));
831
832 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
833
834 // Remove should fail since the profile is still on the stack.
835 {
836 Error error;
837 manager.RemoveProfile(kProfile0, &error);
838 EXPECT_EQ(Error::kInvalidArguments, error.type());
839 }
840
841 // Profile path should still exist.
842 EXPECT_TRUE(file_util::PathExists(profile_path));
843
844 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
845
846 // This should succeed now that the profile is off the stack.
847 {
848 Error error;
849 manager.RemoveProfile(kProfile0, &error);
850 EXPECT_EQ(Error::kSuccess, error.type());
851 }
852
853 // Profile path should no longer exist.
854 EXPECT_FALSE(file_util::PathExists(profile_path));
855
856 // Another remove succeeds, due to a foible in file_util::Delete --
857 // it is not an error to delete a file that does not exist.
858 {
859 Error error;
860 manager.RemoveProfile(kProfile0, &error);
861 EXPECT_EQ(Error::kSuccess, error.type());
862 }
863
864 // Let's create an error case that will "work". Create a non-empty
865 // directory in the place of the profile pathname.
866 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
867 {
868 Error error;
869 manager.RemoveProfile(kProfile0, &error);
870 EXPECT_EQ(Error::kOperationFailed, error.type());
871 }
872}
873
Paul Stewartfc9a1da2012-06-27 15:54:52 -0700874TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) {
875 // It's much easier to use real Glib in creating a Manager for this
876 // test here since we want the storage side-effects.
877 GLib glib;
878 ScopedTempDir temp_dir;
879 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
880 Manager manager(control_interface(),
881 dispatcher(),
882 metrics(),
883 &glib,
884 run_path(),
885 storage_path(),
886 temp_dir.path().value());
887
888 const char kProfile0[] = "profile0";
889 FilePath profile_path(
890 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
891
892 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
893 ASSERT_TRUE(file_util::PathExists(profile_path));
894 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
895
896 // Ensure that even if the backing filestore is removed, we still can't
897 // create a profile twice.
898 ASSERT_TRUE(file_util::Delete(profile_path, false));
899 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0));
900}
901
Paul Stewart75225512012-01-26 22:51:33 -0800902// Use this matcher instead of passing RefPtrs directly into the arguments
903// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
904// system teardown.
905MATCHER_P(IsRefPtrTo, ref_address, "") {
906 return arg.get() == ref_address;
907}
908
909TEST_F(ManagerTest, HandleProfileEntryDeletion) {
910 MockServiceRefPtr s_not_in_profile(
911 new NiceMock<MockService>(control_interface(),
912 dispatcher(),
913 metrics(),
914 manager()));
915 MockServiceRefPtr s_not_in_group(
916 new NiceMock<MockService>(control_interface(),
917 dispatcher(),
918 metrics(),
919 manager()));
920 MockServiceRefPtr s_configure_fail(
921 new NiceMock<MockService>(control_interface(),
922 dispatcher(),
923 metrics(),
924 manager()));
925 MockServiceRefPtr s_configure_succeed(
926 new NiceMock<MockService>(control_interface(),
927 dispatcher(),
928 metrics(),
929 manager()));
930
931 string entry_name("entry_name");
932 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
933 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
934 .WillRepeatedly(Return("not_entry_name"));
935 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
936 .WillRepeatedly(Return(entry_name));
937 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
938 .WillRepeatedly(Return(entry_name));
939
940 manager()->RegisterService(s_not_in_profile);
941 manager()->RegisterService(s_not_in_group);
942 manager()->RegisterService(s_configure_fail);
943 manager()->RegisterService(s_configure_succeed);
944
945 scoped_refptr<MockProfile> profile0(
946 new StrictMock<MockProfile>(control_interface(), manager(), ""));
947 scoped_refptr<MockProfile> profile1(
948 new StrictMock<MockProfile>(control_interface(), manager(), ""));
949
950 s_not_in_group->set_profile(profile1);
951 s_configure_fail->set_profile(profile1);
952 s_configure_succeed->set_profile(profile1);
953
954 AdoptProfile(manager(), profile0);
955 AdoptProfile(manager(), profile1);
956
957 // No services are a member of this profile.
958 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
959
960 // No services that are members of this profile have this entry name.
961 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
962
963 // Only services that are members of the profile and group will be abandoned.
964 EXPECT_CALL(*profile1.get(),
965 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
966 EXPECT_CALL(*profile1.get(),
967 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
968 EXPECT_CALL(*profile1.get(),
969 AbandonService(IsRefPtrTo(s_configure_fail.get())))
970 .WillOnce(Return(true));
971 EXPECT_CALL(*profile1.get(),
972 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
973 .WillOnce(Return(true));
974
975 // Never allow services to re-join profile1.
976 EXPECT_CALL(*profile1.get(), ConfigureService(_))
977 .WillRepeatedly(Return(false));
978
979 // Only allow one of the members of the profile and group to successfully
980 // join profile0.
981 EXPECT_CALL(*profile0.get(),
982 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
983 EXPECT_CALL(*profile0.get(),
984 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
985 EXPECT_CALL(*profile0.get(),
986 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
987 .WillOnce(Return(false));
988 EXPECT_CALL(*profile0.get(),
989 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
990 .WillOnce(Return(true));
991
992 // Expect the failed-to-configure service to have Unload() called on it.
993 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
994 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
995 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
996 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
997
998 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
999
1000 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
1001 EXPECT_EQ(profile1, s_not_in_group->profile());
1002 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
1003
1004 // Since we are using a MockProfile, the profile does not actually change,
1005 // since ConfigureService was not actually called on the service.
1006 EXPECT_EQ(profile1, s_configure_succeed->profile());
1007}
1008
Paul Stewart65512e12012-03-26 18:01:08 -07001009TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
1010 MockServiceRefPtr s_will_remove0(
1011 new NiceMock<MockService>(control_interface(),
1012 dispatcher(),
1013 metrics(),
1014 manager()));
1015 MockServiceRefPtr s_will_remove1(
1016 new NiceMock<MockService>(control_interface(),
1017 dispatcher(),
1018 metrics(),
1019 manager()));
1020 MockServiceRefPtr s_will_not_remove0(
1021 new NiceMock<MockService>(control_interface(),
1022 dispatcher(),
1023 metrics(),
1024 manager()));
1025 MockServiceRefPtr s_will_not_remove1(
1026 new NiceMock<MockService>(control_interface(),
1027 dispatcher(),
1028 metrics(),
1029 manager()));
1030
1031 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1032 .Times(4); // Once for each registration.
1033
1034 string entry_name("entry_name");
1035 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
1036 .WillRepeatedly(Return(entry_name));
1037 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
1038 .WillRepeatedly(Return(entry_name));
1039 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
1040 .WillRepeatedly(Return(entry_name));
1041 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
1042 .WillRepeatedly(Return(entry_name));
1043
1044 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001045 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001046 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001047 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001048 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001049 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001050 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001051 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001052
1053 // One for each service added above.
1054 ASSERT_EQ(4, manager()->services_.size());
1055
1056 scoped_refptr<MockProfile> profile(
1057 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1058
1059 s_will_remove0->set_profile(profile);
1060 s_will_remove1->set_profile(profile);
1061 s_will_not_remove0->set_profile(profile);
1062 s_will_not_remove1->set_profile(profile);
1063
1064 AdoptProfile(manager(), profile);
1065
1066 // Deny any of the services re-entry to the profile.
1067 EXPECT_CALL(*profile, ConfigureService(_))
1068 .WillRepeatedly(Return(false));
1069
1070 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
1071 .WillOnce(Return(true));
1072 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
1073 .WillOnce(Return(true));
1074 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
1075 .WillOnce(Return(true));
1076 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
1077 .WillOnce(Return(true));
1078
1079 EXPECT_CALL(*s_will_remove0, Unload())
1080 .WillOnce(Return(true));
1081 EXPECT_CALL(*s_will_remove1, Unload())
1082 .WillOnce(Return(true));
1083 EXPECT_CALL(*s_will_not_remove0, Unload())
1084 .WillOnce(Return(false));
1085 EXPECT_CALL(*s_will_not_remove1, Unload())
1086 .WillOnce(Return(false));
1087
1088
1089 // This will cause all the profiles to be unloaded.
1090 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
1091
1092 // 2 of the 4 services added above should have been unregistered and
1093 // removed, leaving 2.
1094 EXPECT_EQ(2, manager()->services_.size());
1095 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1096 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1097}
1098
1099TEST_F(ManagerTest, PopProfileWithUnload) {
1100 MockServiceRefPtr s_will_remove0(
1101 new NiceMock<MockService>(control_interface(),
1102 dispatcher(),
1103 metrics(),
1104 manager()));
1105 MockServiceRefPtr s_will_remove1(
1106 new NiceMock<MockService>(control_interface(),
1107 dispatcher(),
1108 metrics(),
1109 manager()));
1110 MockServiceRefPtr s_will_not_remove0(
1111 new NiceMock<MockService>(control_interface(),
1112 dispatcher(),
1113 metrics(),
1114 manager()));
1115 MockServiceRefPtr s_will_not_remove1(
1116 new NiceMock<MockService>(control_interface(),
1117 dispatcher(),
1118 metrics(),
1119 manager()));
1120
1121 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1122 .Times(5); // Once for each registration, and one after profile pop.
1123
1124 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001125 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001126 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001127 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001128 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001129 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001130 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001131 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001132
1133 // One for each service added above.
1134 ASSERT_EQ(4, manager()->services_.size());
1135
1136 scoped_refptr<MockProfile> profile0(
1137 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1138 scoped_refptr<MockProfile> profile1(
1139 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1140
1141 s_will_remove0->set_profile(profile1);
1142 s_will_remove1->set_profile(profile1);
1143 s_will_not_remove0->set_profile(profile1);
1144 s_will_not_remove1->set_profile(profile1);
1145
1146 AdoptProfile(manager(), profile0);
1147 AdoptProfile(manager(), profile1);
1148
1149 // Deny any of the services entry to profile0, so they will all be unloaded.
1150 EXPECT_CALL(*profile0, ConfigureService(_))
1151 .WillRepeatedly(Return(false));
1152
1153 EXPECT_CALL(*s_will_remove0, Unload())
1154 .WillOnce(Return(true));
1155 EXPECT_CALL(*s_will_remove1, Unload())
1156 .WillOnce(Return(true));
1157 EXPECT_CALL(*s_will_not_remove0, Unload())
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001158 .WillRepeatedly(Return(false));
Paul Stewart65512e12012-03-26 18:01:08 -07001159 EXPECT_CALL(*s_will_not_remove1, Unload())
1160 .WillOnce(Return(false));
1161
1162 // This will pop profile1, which should cause all our profiles to unload.
1163 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001164 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001165
1166 // 2 of the 4 services added above should have been unregistered and
1167 // removed, leaving 2.
1168 EXPECT_EQ(2, manager()->services_.size());
1169 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1170 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
Paul Stewartfc9a1da2012-06-27 15:54:52 -07001171
1172 // Expect the unloaded services to lose their profile reference.
1173 EXPECT_FALSE(s_will_remove0->profile());
1174 EXPECT_FALSE(s_will_remove1->profile());
1175
1176 // If we explicitly deregister a service, the effect should be the same
1177 // with respect to the profile reference.
1178 ASSERT_TRUE(s_will_not_remove0->profile());
1179 manager()->DeregisterService(s_will_not_remove0);
1180 EXPECT_FALSE(s_will_not_remove0->profile());
Paul Stewart65512e12012-03-26 18:01:08 -07001181}
1182
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001183TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001184 {
1185 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001186 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1187 flimflam::kOfflineModeProperty,
1188 PropertyStoreTest::kBoolV,
1189 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001190 }
1191 {
1192 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001193 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1194 flimflam::kCountryProperty,
1195 PropertyStoreTest::kStringV,
1196 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001197 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001198 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001199 {
1200 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001201 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1202 flimflam::kCountryProperty,
1203 PropertyStoreTest::kBoolV,
1204 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001205 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001206 }
1207 {
1208 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001209 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1210 flimflam::kOfflineModeProperty,
1211 PropertyStoreTest::kStringV,
1212 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001213 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001214 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001215 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001216 {
1217 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001218 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001219 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001220 flimflam::kEnabledTechnologiesProperty,
1221 PropertyStoreTest::kStringsV,
1222 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001223 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001224 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001225}
1226
mukesh agrawal32399322011-09-01 10:53:43 -07001227TEST_F(ManagerTest, RequestScan) {
1228 {
1229 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001230 manager()->RegisterDevice(mock_devices_[0].get());
1231 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001232 EXPECT_CALL(*mock_devices_[0], technology())
1233 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001234 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001235 EXPECT_CALL(*mock_devices_[1], technology())
1236 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001237 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001238 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001239 }
1240
1241 {
1242 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001243 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001244 EXPECT_EQ(Error::kInvalidArguments, error.type());
1245 }
1246}
1247
Darin Petkovb65c2452012-02-23 15:17:06 +01001248TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001249 KeyValueStore args;
1250 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001251 manager()->GetService(args, &e);
1252 EXPECT_EQ(Error::kInvalidArguments, e.type());
1253 EXPECT_EQ("must specify service type", e.message());
1254}
1255
1256TEST_F(ManagerTest, GetServiceUnknownType) {
1257 KeyValueStore args;
1258 Error e;
1259 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1260 manager()->GetService(args, &e);
1261 EXPECT_EQ(Error::kNotSupported, e.type());
1262 EXPECT_EQ("service type is unsupported", e.message());
1263}
1264
1265TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1266 KeyValueStore args;
1267 Error e;
1268 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1269 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001270 EXPECT_EQ(Error::kInvalidArguments, e.type());
1271 EXPECT_EQ("no wifi devices available", e.message());
1272}
1273
Darin Petkovb65c2452012-02-23 15:17:06 +01001274TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001275 KeyValueStore args;
1276 Error e;
1277 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001278 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001279 manager()->RegisterDevice(mock_wifi_);
1280 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1281 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001282 manager()->GetService(args, &e);
1283 EXPECT_TRUE(e.IsSuccess());
1284}
1285
Darin Petkov33af05c2012-02-28 10:10:30 +01001286TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1287 KeyValueStore args;
1288 Error e;
1289 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001290 scoped_refptr<MockProfile> profile(
1291 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1292 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001293 ServiceRefPtr service = manager()->GetService(args, &e);
1294 EXPECT_EQ(Error::kNotSupported, e.type());
1295 EXPECT_FALSE(service);
1296}
1297
Darin Petkovb65c2452012-02-23 15:17:06 +01001298TEST_F(ManagerTest, GetServiceVPN) {
1299 KeyValueStore args;
1300 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001301 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001302 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001303 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1304 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001305 scoped_refptr<MockProfile> profile(
1306 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1307 AdoptProfile(manager(), profile);
1308 ServiceRefPtr updated_service;
1309 EXPECT_CALL(*profile, UpdateService(_))
1310 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1311 ServiceRefPtr configured_service;
1312 EXPECT_CALL(*profile, ConfigureService(_))
1313 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001314 ServiceRefPtr service = manager()->GetService(args, &e);
1315 EXPECT_TRUE(e.IsSuccess());
1316 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001317 EXPECT_EQ(service, updated_service);
1318 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001319}
1320
Darin Petkovc63dcf02012-05-24 11:51:43 +02001321TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1322 KeyValueStore args;
1323 Error e;
1324 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1325 ServiceRefPtr service = manager()->GetService(args, &e);
1326 EXPECT_EQ(Error::kInvalidArguments, e.type());
1327 EXPECT_EQ("Missing WiMAX network id.", e.message());
1328 EXPECT_FALSE(service);
1329}
1330
Darin Petkovd1cd7972012-05-22 15:26:15 +02001331TEST_F(ManagerTest, GetServiceWiMax) {
1332 KeyValueStore args;
1333 Error e;
1334 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001335 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1336 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1337 ServiceRefPtr service = manager()->GetService(args, &e);
1338 EXPECT_TRUE(e.IsSuccess());
1339 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001340}
1341
Paul Stewart7f61e522012-03-22 11:13:45 -07001342TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1343 // Manager calls ActiveProfile() so we need at least one profile installed.
1344 scoped_refptr<MockProfile> profile(
1345 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1346 AdoptProfile(manager(), profile);
1347
1348 KeyValueStore args;
1349 args.SetString(flimflam::kProfileProperty, "xxx");
1350 Error error;
1351 manager()->ConfigureService(args, &error);
1352 EXPECT_EQ(Error::kInvalidArguments, error.type());
1353 EXPECT_EQ("Invalid profile name xxx", error.message());
1354}
1355
1356TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1357 // Manager calls ActiveProfile() so we need at least one profile installed.
1358 scoped_refptr<MockProfile> profile(
1359 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1360 AdoptProfile(manager(), profile);
1361
1362 KeyValueStore args;
1363 Error error;
1364 manager()->ConfigureService(args, &error);
1365 EXPECT_EQ(Error::kInvalidArguments, error.type());
1366 EXPECT_EQ("must specify service type", error.message());
1367}
1368
1369// A registered service in the ephemeral profile should be moved to the
1370// active profile as a part of configuration if no profile was explicitly
1371// specified.
1372TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1373 scoped_refptr<MockProfile> profile(
1374 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1375
1376 AdoptProfile(manager(), profile); // This is now the active profile.
1377
1378 const std::vector<uint8_t> ssid;
1379 scoped_refptr<MockWiFiService> service(
1380 new NiceMock<MockWiFiService>(control_interface(),
1381 dispatcher(),
1382 metrics(),
1383 manager(),
1384 mock_wifi_,
1385 ssid,
1386 "",
1387 "",
1388 false));
1389
1390 manager()->RegisterService(service);
1391 service->set_profile(GetEphemeralProfile(manager()));
1392
1393 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1394 // the same device as that used above causes a refcounting loop.
1395 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1396 dispatcher(),
1397 metrics(),
1398 manager(),
1399 "wifi1",
1400 "addr5",
1401 5));
1402 manager()->RegisterDevice(wifi);
1403 EXPECT_CALL(*wifi, GetService(_, _))
1404 .WillOnce(Return(service));
1405 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1406 .WillOnce(Return(true));
1407 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1408 .WillOnce(Return(true));
1409
1410 KeyValueStore args;
1411 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1412 Error error;
1413 manager()->ConfigureService(args, &error);
1414 EXPECT_TRUE(error.IsSuccess());
1415}
1416
1417// If were configure a service that was already registered and explicitly
1418// specify a profile, it should be moved from the profile it was previously
1419// in to the specified profile if one was requested.
1420TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1421 scoped_refptr<MockProfile> profile0(
1422 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1423 scoped_refptr<MockProfile> profile1(
1424 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1425
1426 const string kProfileName0 = "profile0";
1427 const string kProfileName1 = "profile1";
1428
1429 EXPECT_CALL(*profile0, GetRpcIdentifier())
1430 .WillRepeatedly(Return(kProfileName0));
1431 EXPECT_CALL(*profile1, GetRpcIdentifier())
1432 .WillRepeatedly(Return(kProfileName1));
1433
1434 AdoptProfile(manager(), profile0);
1435 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1436
1437 const std::vector<uint8_t> ssid;
1438 scoped_refptr<MockWiFiService> service(
1439 new NiceMock<MockWiFiService>(control_interface(),
1440 dispatcher(),
1441 metrics(),
1442 manager(),
1443 mock_wifi_,
1444 ssid,
1445 "",
1446 "",
1447 false));
1448
1449 manager()->RegisterService(service);
1450 service->set_profile(profile1);
1451
1452 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1453 // the same device as that used above causes a refcounting loop.
1454 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1455 dispatcher(),
1456 metrics(),
1457 manager(),
1458 "wifi1",
1459 "addr5",
1460 5));
1461 manager()->RegisterDevice(wifi);
1462 EXPECT_CALL(*wifi, GetService(_, _))
1463 .WillOnce(Return(service));
1464 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1465 .WillOnce(Return(true));
1466 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1467 .WillOnce(Return(true));
1468 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1469 .WillOnce(Return(true));
1470
1471 KeyValueStore args;
1472 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1473 args.SetString(flimflam::kProfileProperty, kProfileName0);
1474 Error error;
1475 manager()->ConfigureService(args, &error);
1476 EXPECT_TRUE(error.IsSuccess());
1477 service->set_profile(NULL); // Breaks refcounting loop.
1478}
1479
1480// An unregistered service should remain unregistered, but its contents should
1481// be saved to the specified profile nonetheless.
1482TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1483 scoped_refptr<MockProfile> profile0(
1484 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1485 scoped_refptr<MockProfile> profile1(
1486 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1487
1488 const string kProfileName0 = "profile0";
1489 const string kProfileName1 = "profile1";
1490
1491 EXPECT_CALL(*profile0, GetRpcIdentifier())
1492 .WillRepeatedly(Return(kProfileName0));
1493 EXPECT_CALL(*profile1, GetRpcIdentifier())
1494 .WillRepeatedly(Return(kProfileName1));
1495
1496 AdoptProfile(manager(), profile0);
1497 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1498
1499 const std::vector<uint8_t> ssid;
1500 scoped_refptr<MockWiFiService> service(
1501 new NiceMock<MockWiFiService>(control_interface(),
1502 dispatcher(),
1503 metrics(),
1504 manager(),
1505 mock_wifi_,
1506 ssid,
1507 "",
1508 "",
1509 false));
1510
1511 service->set_profile(profile1);
1512
1513 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1514 // the same device as that used above causes a refcounting loop.
1515 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1516 dispatcher(),
1517 metrics(),
1518 manager(),
1519 "wifi1",
1520 "addr5",
1521 5));
1522 manager()->RegisterDevice(wifi);
1523 EXPECT_CALL(*wifi, GetService(_, _))
1524 .WillOnce(Return(service));
1525 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1526 .WillOnce(Return(true));
1527 EXPECT_CALL(*profile0, AdoptService(_))
1528 .Times(0);
1529 EXPECT_CALL(*profile1, AdoptService(_))
1530 .Times(0);
1531
1532 KeyValueStore args;
1533 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1534 args.SetString(flimflam::kProfileProperty, kProfileName0);
1535 Error error;
1536 manager()->ConfigureService(args, &error);
1537 EXPECT_TRUE(error.IsSuccess());
1538}
1539
Paul Stewart22aa71b2011-09-16 12:15:11 -07001540TEST_F(ManagerTest, TechnologyOrder) {
1541 Error error;
1542 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1543 string(flimflam::kTypeWifi), &error);
1544 ASSERT_TRUE(error.IsSuccess());
1545 EXPECT_EQ(manager()->GetTechnologyOrder(),
1546 string(flimflam::kTypeEthernet) + "," +
1547 string(flimflam::kTypeWifi));
1548
1549 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1550 string(flimflam::kTypeWifi), &error);
1551 ASSERT_FALSE(error.IsSuccess());
1552 EXPECT_EQ(Error::kInvalidArguments, error.type());
1553 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1554 string(flimflam::kTypeWifi),
1555 manager()->GetTechnologyOrder());
1556}
1557
1558TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001559 // TODO(quiche): Some of these tests would probably fit better in
1560 // service_unittest, since the actual comparison of Services is
1561 // implemented in Service. (crosbug.com/23370)
1562
Paul Stewart22aa71b2011-09-16 12:15:11 -07001563 scoped_refptr<MockService> mock_service0(
1564 new NiceMock<MockService>(control_interface(),
1565 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001566 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001567 manager()));
1568 scoped_refptr<MockService> mock_service1(
1569 new NiceMock<MockService>(control_interface(),
1570 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001571 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001572 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001573
1574 manager()->RegisterService(mock_service0);
1575 manager()->RegisterService(mock_service1);
1576
1577 // Services should already be sorted by UniqueName
1578 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1579
1580 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001581 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001582 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1583
1584 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001585 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001586 manager()->UpdateService(mock_service1);
1587 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1588
1589 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001590 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001591 manager()->UpdateService(mock_service0);
1592 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1593
1594 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001595 EXPECT_CALL(*mock_service0.get(), technology())
1596 .WillRepeatedly(Return((Technology::kWifi)));
1597 EXPECT_CALL(*mock_service1.get(), technology())
1598 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001599
1600 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001601 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001602 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001603 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1604
1605 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1606 string(flimflam::kTypeEthernet), &error);
1607 EXPECT_TRUE(error.IsSuccess());
1608 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1609
Gaurav Shah435de2c2011-11-17 19:01:07 -08001610 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001611 mock_service0->set_priority(1);
1612 manager()->UpdateService(mock_service0);
1613 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1614
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001615 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001616 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001617 manager()->UpdateService(mock_service1);
1618 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1619
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001620 // Auto-connect.
1621 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001622 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001623 mock_service1->set_auto_connect(false);
1624 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001625 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1626
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001627 // Connectable.
1628 mock_service1->set_connectable(true);
1629 manager()->UpdateService(mock_service1);
1630 mock_service0->set_connectable(false);
1631 manager()->UpdateService(mock_service0);
1632 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1633
1634 // IsFailed.
1635 EXPECT_CALL(*mock_service0.get(), state())
1636 .WillRepeatedly(Return(Service::kStateIdle));
1637 EXPECT_CALL(*mock_service0.get(), IsFailed())
1638 .WillRepeatedly(Return(false));
1639 manager()->UpdateService(mock_service0);
1640 EXPECT_CALL(*mock_service0.get(), state())
1641 .WillRepeatedly(Return(Service::kStateFailure));
1642 EXPECT_CALL(*mock_service1.get(), IsFailed())
1643 .WillRepeatedly(Return(true));
1644 manager()->UpdateService(mock_service1);
1645 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1646
1647 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001648 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001649 .WillRepeatedly(Return(Service::kStateAssociating));
1650 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001651 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001652 manager()->UpdateService(mock_service1);
1653 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1654
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001655 // Connected.
1656 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001657 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001658 EXPECT_CALL(*mock_service0.get(), IsConnected())
1659 .WillRepeatedly(Return(true));
1660 manager()->UpdateService(mock_service0);
1661 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1662
Paul Stewarta121c442012-06-09 14:12:58 -07001663 // Portal.
1664 EXPECT_CALL(*mock_service1.get(), state())
1665 .WillRepeatedly(Return(Service::kStateConnected));
1666 EXPECT_CALL(*mock_service1.get(), IsConnected())
1667 .WillRepeatedly(Return(true));
1668 manager()->UpdateService(mock_service1);
1669 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1670
Paul Stewart22aa71b2011-09-16 12:15:11 -07001671 manager()->DeregisterService(mock_service0);
1672 manager()->DeregisterService(mock_service1);
1673}
1674
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001675TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001676 MockMetrics mock_metrics;
1677 manager()->set_metrics(&mock_metrics);
1678
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001679 scoped_refptr<MockService> mock_service0(
1680 new NiceMock<MockService>(control_interface(),
1681 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001682 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001683 manager()));
1684 scoped_refptr<MockService> mock_service1(
1685 new NiceMock<MockService>(control_interface(),
1686 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001687 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001688 manager()));
1689
1690 scoped_refptr<MockConnection> mock_connection0(
1691 new NiceMock<MockConnection>(device_info_.get()));
1692 scoped_refptr<MockConnection> mock_connection1(
1693 new NiceMock<MockConnection>(device_info_.get()));
1694
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001695 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001696 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001697 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001698 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001699 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001700 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001701
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001702 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001703 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001704
1705 mock_service1->set_priority(1);
1706 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001707 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001708
1709 mock_service1->set_priority(0);
1710 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001711 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001712
Paul Stewartce4ec192012-03-14 12:53:46 -07001713 mock_service0->set_mock_connection(mock_connection0);
1714 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001715
1716 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001717 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001718 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001719
Darin Petkova5e07ef2012-07-09 14:27:57 +02001720 ServiceWatcher service_watcher;
1721 int tag =
1722 manager()->RegisterDefaultServiceCallback(
1723 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1724 service_watcher.AsWeakPtr()));
1725 EXPECT_EQ(1, tag);
1726
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001727 mock_service1->set_priority(1);
1728 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1729 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001730 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_));
Thieu Lea20cbc22012-01-09 22:01:43 +00001731 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001732 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001733
Darin Petkova5e07ef2012-07-09 14:27:57 +02001734 manager()->DeregisterDefaultServiceCallback(tag);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001735 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +02001736 EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0);
Thieu Lea20cbc22012-01-09 22:01:43 +00001737 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001738 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001739 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001740 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001741
Paul Stewartce4ec192012-03-14 12:53:46 -07001742 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001743 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001744 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001745 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001746
1747 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001748 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001749}
1750
Darin Petkova5e07ef2012-07-09 14:27:57 +02001751TEST_F(ManagerTest, NotifyDefaultServiceChanged) {
1752 EXPECT_EQ(0, manager()->default_service_callback_tag_);
1753 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1754
1755 MockMetrics mock_metrics;
1756 manager()->set_metrics(&mock_metrics);
1757
1758 scoped_refptr<MockService> mock_service(
1759 new NiceMock<MockService>(
1760 control_interface(), dispatcher(), metrics(), manager()));
1761 ServiceRefPtr service = mock_service;
1762 ServiceRefPtr null_service;
1763
1764 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1765 manager()->NotifyDefaultServiceChanged(null_service);
1766
1767 ServiceWatcher service_watcher1;
1768 ServiceWatcher service_watcher2;
1769 int tag1 =
1770 manager()->RegisterDefaultServiceCallback(
1771 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1772 service_watcher1.AsWeakPtr()));
1773 EXPECT_EQ(1, tag1);
1774 int tag2 =
1775 manager()->RegisterDefaultServiceCallback(
1776 Bind(&ServiceWatcher::OnDefaultServiceChanged,
1777 service_watcher2.AsWeakPtr()));
1778 EXPECT_EQ(2, tag2);
1779
1780 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service));
1781 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service));
1782 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1783 manager()->NotifyDefaultServiceChanged(null_service);
1784
1785 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service));
1786 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1787 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1788 manager()->NotifyDefaultServiceChanged(mock_service);
1789
1790 manager()->DeregisterDefaultServiceCallback(tag1);
1791 EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0);
1792 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service));
1793 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1794 manager()->NotifyDefaultServiceChanged(mock_service);
1795 EXPECT_EQ(1, manager()->default_service_callbacks_.size());
1796
1797 manager()->DeregisterDefaultServiceCallback(tag2);
1798 EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0);
1799 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get()));
1800 manager()->NotifyDefaultServiceChanged(mock_service);
1801
1802 EXPECT_EQ(2, manager()->default_service_callback_tag_);
1803 EXPECT_TRUE(manager()->default_service_callbacks_.empty());
1804}
1805
Gaurav Shah435de2c2011-11-17 19:01:07 -08001806TEST_F(ManagerTest, AvailableTechnologies) {
1807 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1808 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001809 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001810 manager(),
1811 "null4",
1812 "addr4",
1813 0));
1814 manager()->RegisterDevice(mock_devices_[0]);
1815 manager()->RegisterDevice(mock_devices_[1]);
1816 manager()->RegisterDevice(mock_devices_[2]);
1817 manager()->RegisterDevice(mock_devices_[3]);
1818
1819 ON_CALL(*mock_devices_[0].get(), technology())
1820 .WillByDefault(Return(Technology::kEthernet));
1821 ON_CALL(*mock_devices_[1].get(), technology())
1822 .WillByDefault(Return(Technology::kWifi));
1823 ON_CALL(*mock_devices_[2].get(), technology())
1824 .WillByDefault(Return(Technology::kCellular));
1825 ON_CALL(*mock_devices_[3].get(), technology())
1826 .WillByDefault(Return(Technology::kWifi));
1827
1828 set<string> expected_technologies;
1829 expected_technologies.insert(Technology::NameFromIdentifier(
1830 Technology::kEthernet));
1831 expected_technologies.insert(Technology::NameFromIdentifier(
1832 Technology::kWifi));
1833 expected_technologies.insert(Technology::NameFromIdentifier(
1834 Technology::kCellular));
1835 Error error;
1836 vector<string> technologies = manager()->AvailableTechnologies(&error);
1837
1838 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1839 ContainerEq(expected_technologies));
1840}
1841
1842TEST_F(ManagerTest, ConnectedTechnologies) {
1843 scoped_refptr<MockService> connected_service1(
1844 new NiceMock<MockService>(control_interface(),
1845 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001846 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001847 manager()));
1848 scoped_refptr<MockService> connected_service2(
1849 new NiceMock<MockService>(control_interface(),
1850 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001851 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001852 manager()));
1853 scoped_refptr<MockService> disconnected_service1(
1854 new NiceMock<MockService>(control_interface(),
1855 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001856 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001857 manager()));
1858 scoped_refptr<MockService> disconnected_service2(
1859 new NiceMock<MockService>(control_interface(),
1860 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001861 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001862 manager()));
1863
1864 ON_CALL(*connected_service1.get(), IsConnected())
1865 .WillByDefault(Return(true));
1866 ON_CALL(*connected_service2.get(), IsConnected())
1867 .WillByDefault(Return(true));
1868
1869 manager()->RegisterService(connected_service1);
1870 manager()->RegisterService(connected_service2);
1871 manager()->RegisterService(disconnected_service1);
1872 manager()->RegisterService(disconnected_service2);
1873
1874 manager()->RegisterDevice(mock_devices_[0]);
1875 manager()->RegisterDevice(mock_devices_[1]);
1876 manager()->RegisterDevice(mock_devices_[2]);
1877 manager()->RegisterDevice(mock_devices_[3]);
1878
1879 ON_CALL(*mock_devices_[0].get(), technology())
1880 .WillByDefault(Return(Technology::kEthernet));
1881 ON_CALL(*mock_devices_[1].get(), technology())
1882 .WillByDefault(Return(Technology::kWifi));
1883 ON_CALL(*mock_devices_[2].get(), technology())
1884 .WillByDefault(Return(Technology::kCellular));
1885 ON_CALL(*mock_devices_[3].get(), technology())
1886 .WillByDefault(Return(Technology::kWifi));
1887
1888 mock_devices_[0]->SelectService(connected_service1);
1889 mock_devices_[1]->SelectService(disconnected_service1);
1890 mock_devices_[2]->SelectService(disconnected_service2);
1891 mock_devices_[3]->SelectService(connected_service2);
1892
1893 set<string> expected_technologies;
1894 expected_technologies.insert(Technology::NameFromIdentifier(
1895 Technology::kEthernet));
1896 expected_technologies.insert(Technology::NameFromIdentifier(
1897 Technology::kWifi));
1898 Error error;
1899
1900 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1901 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1902 ContainerEq(expected_technologies));
1903}
1904
1905TEST_F(ManagerTest, DefaultTechnology) {
1906 scoped_refptr<MockService> connected_service(
1907 new NiceMock<MockService>(control_interface(),
1908 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001909 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001910 manager()));
1911 scoped_refptr<MockService> disconnected_service(
1912 new NiceMock<MockService>(control_interface(),
1913 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001914 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001915 manager()));
1916
1917 // Connected. WiFi.
1918 ON_CALL(*connected_service.get(), IsConnected())
1919 .WillByDefault(Return(true));
1920 ON_CALL(*connected_service.get(), state())
1921 .WillByDefault(Return(Service::kStateConnected));
1922 ON_CALL(*connected_service.get(), technology())
1923 .WillByDefault(Return(Technology::kWifi));
1924
1925 // Disconnected. Ethernet.
1926 ON_CALL(*disconnected_service.get(), technology())
1927 .WillByDefault(Return(Technology::kEthernet));
1928
1929 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001930 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001931 Error error;
1932 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1933
1934
1935 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001936 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001937 // Connected service should be brought to the front now.
1938 string expected_technology =
1939 Technology::NameFromIdentifier(Technology::kWifi);
1940 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1941}
1942
Paul Stewart212d60f2012-07-12 10:59:13 -07001943TEST_F(ManagerTest, Stop) {
1944 scoped_refptr<MockProfile> profile(
1945 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1946 AdoptProfile(manager(), profile);
1947 scoped_refptr<MockService> service(
Thieu Le1271d682011-11-02 22:48:19 +00001948 new NiceMock<MockService>(control_interface(),
1949 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001950 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001951 manager()));
Paul Stewart212d60f2012-07-12 10:59:13 -07001952 manager()->RegisterService(service);
1953 manager()->RegisterDevice(mock_devices_[0]);
1954 EXPECT_CALL(*profile.get(),
1955 UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
1956 .WillOnce(Return(true));
1957 EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
1958 EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001959 manager()->Stop();
1960}
1961
mukesh agrawal00917ce2011-11-22 23:56:55 +00001962TEST_F(ManagerTest, UpdateServiceConnected) {
1963 scoped_refptr<MockService> mock_service(
1964 new NiceMock<MockService>(control_interface(),
1965 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001966 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001967 manager()));
1968 manager()->RegisterService(mock_service);
1969 EXPECT_FALSE(mock_service->favorite());
1970 EXPECT_FALSE(mock_service->auto_connect());
1971
Gaurav Shah435de2c2011-11-17 19:01:07 -08001972 EXPECT_CALL(*mock_service.get(), IsConnected())
1973 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001974 manager()->UpdateService(mock_service);
1975 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1976 // to mock out MakeFavorite. And mocking that out would break the
1977 // SortServices test. (crosbug.com/23370)
1978 EXPECT_TRUE(mock_service->favorite());
1979 EXPECT_TRUE(mock_service->auto_connect());
1980}
1981
Thieu Led4e9e552012-02-16 16:26:07 -08001982TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
1983 // This tests the case where the user connects to a service that is
1984 // currently associated with a profile. We want to make sure that the
1985 // favorite flag is set and that the flag is saved to the current
1986 // profile.
1987 scoped_refptr<MockService> mock_service(
1988 new NiceMock<MockService>(control_interface(),
1989 dispatcher(),
1990 metrics(),
1991 manager()));
1992 manager()->RegisterService(mock_service);
1993 EXPECT_FALSE(mock_service->favorite());
1994 EXPECT_FALSE(mock_service->auto_connect());
1995
Gary Moraind93615e2012-04-27 11:50:03 -07001996 scoped_refptr<MockProfile> profile(
1997 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08001998
Gary Moraind93615e2012-04-27 11:50:03 -07001999 mock_service->set_profile(profile);
2000 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08002001 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07002002 EXPECT_CALL(*profile,
2003 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08002004 manager()->UpdateService(mock_service);
2005 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
2006 // to mock out MakeFavorite. And mocking that out would break the
2007 // SortServices test. (crosbug.com/23370)
2008 EXPECT_TRUE(mock_service->favorite());
2009 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07002010 // This releases the ref on the mock profile.
2011 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08002012}
2013
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002014TEST_F(ManagerTest, SaveSuccessfulService) {
2015 scoped_refptr<MockProfile> profile(
2016 new StrictMock<MockProfile>(control_interface(), manager(), ""));
2017 AdoptProfile(manager(), profile);
2018 scoped_refptr<MockService> service(
2019 new NiceMock<MockService>(control_interface(),
2020 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08002021 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08002022 manager()));
2023
2024 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
2025 ServiceRefPtr expect_service(service.get());
2026
2027 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
2028 .WillOnce(Return(false));
2029 manager()->RegisterService(service);
2030
2031 EXPECT_CALL(*service.get(), state())
2032 .WillRepeatedly(Return(Service::kStateConnected));
2033 EXPECT_CALL(*service.get(), IsConnected())
2034 .WillRepeatedly(Return(true));
2035 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
2036 .WillOnce(Return(true));
2037 manager()->UpdateService(service);
2038}
2039
Darin Petkove7c6ad32012-06-29 10:22:09 +02002040TEST_F(ManagerTest, UpdateDevice) {
2041 MockProfile *profile0 = new MockProfile(control_interface(), manager(), "");
2042 MockProfile *profile1 = new MockProfile(control_interface(), manager(), "");
2043 MockProfile *profile2 = new MockProfile(control_interface(), manager(), "");
2044 AdoptProfile(manager(), profile0); // Passes ownership.
2045 AdoptProfile(manager(), profile1); // Passes ownership.
2046 AdoptProfile(manager(), profile2); // Passes ownership.
2047 DeviceRefPtr device_ref(mock_devices_[0].get());
2048 EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0);
2049 EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true));
2050 EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false));
2051 manager()->UpdateDevice(mock_devices_[0]);
2052}
2053
Paul Stewart1b253142012-01-26 14:05:52 -08002054TEST_F(ManagerTest, EnumerateProfiles) {
2055 vector<string> profile_paths;
2056 for (size_t i = 0; i < 10; i++) {
2057 scoped_refptr<MockProfile> profile(
2058 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05002059 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08002060 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
2061 .WillOnce(Return(profile_paths.back()));
2062 AdoptProfile(manager(), profile);
2063 }
2064
2065 Error error;
2066 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
2067 EXPECT_TRUE(error.IsSuccess());
2068 EXPECT_EQ(profile_paths.size(), returned_paths.size());
2069 for (size_t i = 0; i < profile_paths.size(); i++) {
2070 EXPECT_EQ(profile_paths[i], returned_paths[i]);
2071 }
2072}
2073
mukesh agrawal8a3188d2011-12-01 20:56:44 +00002074TEST_F(ManagerTest, AutoConnectOnRegister) {
2075 MockServiceRefPtr service = MakeAutoConnectableService();
2076 EXPECT_CALL(*service.get(), AutoConnect());
2077 manager()->RegisterService(service);
2078 dispatcher()->DispatchPendingEvents();
2079}
2080
2081TEST_F(ManagerTest, AutoConnectOnUpdate) {
2082 MockServiceRefPtr service1 = MakeAutoConnectableService();
2083 service1->set_priority(1);
2084 MockServiceRefPtr service2 = MakeAutoConnectableService();
2085 service2->set_priority(2);
2086 manager()->RegisterService(service1);
2087 manager()->RegisterService(service2);
2088 dispatcher()->DispatchPendingEvents();
2089
2090 EXPECT_CALL(*service1.get(), AutoConnect());
2091 EXPECT_CALL(*service2.get(), state())
2092 .WillRepeatedly(Return(Service::kStateFailure));
2093 EXPECT_CALL(*service2.get(), IsFailed())
2094 .WillRepeatedly(Return(true));
2095 EXPECT_CALL(*service2.get(), IsConnected())
2096 .WillRepeatedly(Return(false));
2097 manager()->UpdateService(service2);
2098 dispatcher()->DispatchPendingEvents();
2099}
2100
2101TEST_F(ManagerTest, AutoConnectOnDeregister) {
2102 MockServiceRefPtr service1 = MakeAutoConnectableService();
2103 service1->set_priority(1);
2104 MockServiceRefPtr service2 = MakeAutoConnectableService();
2105 service2->set_priority(2);
2106 manager()->RegisterService(service1);
2107 manager()->RegisterService(service2);
2108 dispatcher()->DispatchPendingEvents();
2109
2110 EXPECT_CALL(*service1.get(), AutoConnect());
2111 manager()->DeregisterService(service2);
2112 dispatcher()->DispatchPendingEvents();
2113}
2114
Darin Petkovca621542012-07-25 14:25:56 +02002115TEST_F(ManagerTest, AutoConnectOnPowerStateMem) {
2116 MockServiceRefPtr service = MakeAutoConnectableService();
2117 SetPowerState(PowerManagerProxyDelegate::kMem);
2118 SetPowerManager();
2119 EXPECT_CALL(*service, AutoConnect()).Times(0);
2120 manager()->RegisterService(service);
2121 dispatcher()->DispatchPendingEvents();
2122}
2123
2124TEST_F(ManagerTest, AutoConnectOnPowerStateOn) {
2125 MockServiceRefPtr service = MakeAutoConnectableService();
2126 SetPowerState(PowerManagerProxyDelegate::kOn);
2127 SetPowerManager();
2128 EXPECT_CALL(*service, AutoConnect());
2129 manager()->RegisterService(service);
2130 dispatcher()->DispatchPendingEvents();
2131}
2132
2133TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) {
2134 MockServiceRefPtr service = MakeAutoConnectableService();
2135 SetPowerState(PowerManagerProxyDelegate::kUnknown);
2136 SetPowerManager();
2137 EXPECT_CALL(*service, AutoConnect());
2138 manager()->RegisterService(service);
2139 dispatcher()->DispatchPendingEvents();
2140}
2141
2142TEST_F(ManagerTest, OnPowerStateChanged) {
2143 MockServiceRefPtr service = MakeAutoConnectableService();
2144 SetPowerState(PowerManagerProxyDelegate::kOn);
2145 SetPowerManager();
2146 EXPECT_CALL(*service, AutoConnect());
2147 manager()->RegisterService(service);
mukesh agrawal784566d2012-08-08 18:32:58 -07002148 manager()->RegisterDevice(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002149 dispatcher()->DispatchPendingEvents();
2150
mukesh agrawal784566d2012-08-08 18:32:58 -07002151 EXPECT_CALL(*mock_devices_[0], OnAfterResume());
Darin Petkovca621542012-07-25 14:25:56 +02002152 OnPowerStateChanged(PowerManagerProxyDelegate::kOn);
2153 EXPECT_CALL(*service, AutoConnect());
2154 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002155 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002156
mukesh agrawal784566d2012-08-08 18:32:58 -07002157 EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend());
Darin Petkovca621542012-07-25 14:25:56 +02002158 OnPowerStateChanged(PowerManagerProxyDelegate::kMem);
2159 EXPECT_CALL(*service, AutoConnect()).Times(0);
2160 dispatcher()->DispatchPendingEvents();
mukesh agrawal784566d2012-08-08 18:32:58 -07002161 Mock::VerifyAndClearExpectations(mock_devices_[0]);
Darin Petkovca621542012-07-25 14:25:56 +02002162}
2163
Paul Stewartc681fa02012-03-02 19:40:04 -08002164TEST_F(ManagerTest, RecheckPortal) {
2165 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
2166 .WillOnce(Return(false));
2167 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
2168 .WillOnce(Return(true));
2169 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
2170 .Times(0);
2171
2172 manager()->RegisterDevice(mock_devices_[0]);
2173 manager()->RegisterDevice(mock_devices_[1]);
2174 manager()->RegisterDevice(mock_devices_[2]);
2175
2176 manager()->RecheckPortal(NULL);
2177}
2178
Paul Stewartd215af62012-04-24 23:25:50 -07002179TEST_F(ManagerTest, RecheckPortalOnService) {
2180 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
2181 dispatcher(),
2182 metrics(),
2183 manager());
2184 EXPECT_CALL(*mock_devices_[0].get(),
2185 IsConnectedToService(IsRefPtrTo(service)))
2186 .WillOnce(Return(false));
2187 EXPECT_CALL(*mock_devices_[1].get(),
2188 IsConnectedToService(IsRefPtrTo(service)))
2189 .WillOnce(Return(true));
2190 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
2191 .WillOnce(Return(true));
2192 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
2193 .Times(0);
2194
2195 manager()->RegisterDevice(mock_devices_[0]);
2196 manager()->RegisterDevice(mock_devices_[1]);
2197 manager()->RegisterDevice(mock_devices_[2]);
2198
2199 manager()->RecheckPortalOnService(service);
2200}
2201
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002202TEST_F(ManagerTest, GetDefaultService) {
2203 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002204 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002205
2206 scoped_refptr<MockService> mock_service(
2207 new NiceMock<MockService>(control_interface(),
2208 dispatcher(),
2209 metrics(),
2210 manager()));
2211
2212 manager()->RegisterService(mock_service);
2213 EXPECT_FALSE(manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002214 EXPECT_EQ("/", GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002215
2216 scoped_refptr<MockConnection> mock_connection(
2217 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07002218 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002219 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
Paul Stewart49739c02012-08-08 17:24:03 -07002220 EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier());
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002221
Paul Stewartce4ec192012-03-14 12:53:46 -07002222 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07002223 manager()->DeregisterService(mock_service);
2224}
2225
Paul Stewart13ed2252012-03-21 12:52:46 -07002226TEST_F(ManagerTest, GetServiceWithGUID) {
2227 scoped_refptr<MockService> mock_service0(
2228 new NiceMock<MockService>(control_interface(),
2229 dispatcher(),
2230 metrics(),
2231 manager()));
2232
2233 scoped_refptr<MockService> mock_service1(
2234 new NiceMock<MockService>(control_interface(),
2235 dispatcher(),
2236 metrics(),
2237 manager()));
2238
Paul Stewartcb59fed2012-03-21 21:14:46 -07002239 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
2240 .Times(0);
2241 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
2242 .Times(0);
2243
Paul Stewart13ed2252012-03-21 12:52:46 -07002244 manager()->RegisterService(mock_service0);
2245 manager()->RegisterService(mock_service1);
2246
2247 const string kGUID0 = "GUID0";
2248 const string kGUID1 = "GUID1";
2249
2250 {
2251 Error error;
2252 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2253 EXPECT_FALSE(error.IsSuccess());
2254 EXPECT_FALSE(service);
2255 }
2256
2257 KeyValueStore args;
2258 args.SetString(flimflam::kGuidProperty, kGUID1);
2259
2260 {
2261 Error error;
2262 ServiceRefPtr service = manager()->GetService(args, &error);
2263 EXPECT_EQ(Error::kInvalidArguments, error.type());
2264 EXPECT_FALSE(service);
2265 }
2266
2267 mock_service0->set_guid(kGUID0);
2268 mock_service1->set_guid(kGUID1);
2269
2270 {
2271 Error error;
2272 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2273 EXPECT_TRUE(error.IsSuccess());
2274 EXPECT_EQ(mock_service0.get(), service.get());
2275 }
2276
2277 {
2278 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002279 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2280 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002281 ServiceRefPtr service = manager()->GetService(args, &error);
2282 EXPECT_TRUE(error.IsSuccess());
2283 EXPECT_EQ(mock_service1.get(), service.get());
2284 }
2285
2286 manager()->DeregisterService(mock_service0);
2287 manager()->DeregisterService(mock_service1);
2288}
2289
Gary Morain028545d2012-04-07 14:55:52 -07002290
2291TEST_F(ManagerTest, CalculateStateOffline) {
2292 MockMetrics mock_metrics;
2293 manager()->set_metrics(&mock_metrics);
2294 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2295 .Times(AnyNumber());
2296 scoped_refptr<MockService> mock_service0(
2297 new NiceMock<MockService>(control_interface(),
2298 dispatcher(),
2299 metrics(),
2300 manager()));
2301
2302 scoped_refptr<MockService> mock_service1(
2303 new NiceMock<MockService>(control_interface(),
2304 dispatcher(),
2305 metrics(),
2306 manager()));
2307
2308 EXPECT_CALL(*mock_service0.get(), IsConnected())
2309 .WillRepeatedly(Return(false));
2310 EXPECT_CALL(*mock_service1.get(), IsConnected())
2311 .WillRepeatedly(Return(false));
2312
2313 manager()->RegisterService(mock_service0);
2314 manager()->RegisterService(mock_service1);
2315
2316 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2317
2318 manager()->DeregisterService(mock_service0);
2319 manager()->DeregisterService(mock_service1);
2320}
2321
2322TEST_F(ManagerTest, CalculateStateOnline) {
2323 MockMetrics mock_metrics;
2324 manager()->set_metrics(&mock_metrics);
2325 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2326 .Times(AnyNumber());
2327 scoped_refptr<MockService> mock_service0(
2328 new NiceMock<MockService>(control_interface(),
2329 dispatcher(),
2330 metrics(),
2331 manager()));
2332
2333 scoped_refptr<MockService> mock_service1(
2334 new NiceMock<MockService>(control_interface(),
2335 dispatcher(),
2336 metrics(),
2337 manager()));
2338
2339 EXPECT_CALL(*mock_service0.get(), IsConnected())
2340 .WillRepeatedly(Return(false));
2341 EXPECT_CALL(*mock_service1.get(), IsConnected())
2342 .WillRepeatedly(Return(true));
2343 EXPECT_CALL(*mock_service0.get(), state())
2344 .WillRepeatedly(Return(Service::kStateIdle));
2345 EXPECT_CALL(*mock_service1.get(), state())
2346 .WillRepeatedly(Return(Service::kStateConnected));
2347
2348 manager()->RegisterService(mock_service0);
2349 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002350 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002351
2352 EXPECT_EQ("online", manager()->CalculateState(NULL));
2353
2354 manager()->DeregisterService(mock_service0);
2355 manager()->DeregisterService(mock_service1);
2356}
2357
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002358TEST_F(ManagerTest, StartupPortalList) {
2359 // Simulate loading value from the default profile.
2360 const string kProfileValue("wifi,vpn");
2361 manager()->props_.check_portal_list = kProfileValue;
2362
2363 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2364 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2365 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2366
2367 const string kStartupValue("cellular,ethernet");
2368 manager()->SetStartupPortalList(kStartupValue);
2369 // Ensure profile value is not overwritten, so when we save the default
2370 // profile, the correct value will still be written.
2371 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2372
2373 // However we should read back a different list.
2374 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2375 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2376 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2377
2378 const string kRuntimeValue("ppp");
2379 // Setting a runtime value over the control API should overwrite both
2380 // the profile value and what we read back.
2381 Error error;
2382 manager()->mutable_store()->SetStringProperty(
2383 flimflam::kCheckPortalListProperty,
2384 kRuntimeValue,
2385 &error);
2386 ASSERT_TRUE(error.IsSuccess());
2387 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2388 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2389 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2390 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2391}
2392
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002393TEST_F(ManagerTest, EnableTechnology) {
2394 Error error(Error::kOperationInitiated);
2395 ResultCallback callback;
2396 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2397 EXPECT_TRUE(error.IsSuccess());
2398
Joshua Krollda798622012-06-05 12:30:48 -07002399 ON_CALL(*mock_devices_[0], technology())
2400 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002401
2402 manager()->RegisterDevice(mock_devices_[0]);
2403
2404 // Device is enabled, so expect operation is successful.
2405 mock_devices_[0]->enabled_ = true;
2406 error.Populate(Error::kOperationInitiated);
2407 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2408 EXPECT_TRUE(error.IsSuccess());
2409
2410 // Device is disabled, so expect operation in progress.
2411 mock_devices_[0]->enabled_ = false;
2412 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2413 error.Populate(Error::kOperationInitiated);
2414 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2415 EXPECT_TRUE(error.IsOngoing());
2416}
2417
2418TEST_F(ManagerTest, DisableTechnology) {
2419 Error error(Error::kOperationInitiated);
2420 ResultCallback callback;
2421 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2422 EXPECT_TRUE(error.IsSuccess());
2423
Joshua Krollda798622012-06-05 12:30:48 -07002424 ON_CALL(*mock_devices_[0], technology())
2425 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002426
2427 manager()->RegisterDevice(mock_devices_[0]);
2428
2429 // Device is disabled, so expect operation is successful.
2430 error.Populate(Error::kOperationInitiated);
2431 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2432 EXPECT_TRUE(error.IsSuccess());
2433
2434 // Device is enabled, so expect operation in progress.
2435 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2436 mock_devices_[0]->enabled_ = true;
2437 error.Populate(Error::kOperationInitiated);
2438 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2439 EXPECT_TRUE(error.IsOngoing());
2440}
2441
Chris Masone9be4a9d2011-05-16 15:44:09 -07002442} // namespace shill