blob: 143b9f23d9941c2ff26318f59016cc7e71d549f8 [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
Chris Masone6791a432011-07-12 13:23:19 -07007#include <set>
8
Chris Masone9be4a9d2011-05-16 15:44:09 -07009#include <glib.h>
10
Chris Masone9be4a9d2011-05-16 15:44:09 -070011#include <base/logging.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070012#include <base/memory/scoped_temp_dir.h>
Chris Masone6791a432011-07-12 13:23:19 -070013#include <base/stl_util-inl.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070014#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070016#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070017#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070018
mukesh agrawal32399322011-09-01 10:53:43 -070019#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070020#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070021#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070022#include "shill/glib.h"
23#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070024#include "shill/key_value_store.h"
mukesh agrawal32399322011-09-01 10:53:43 -070025#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080026#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070027#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070028#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080029#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070030#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000031#include "shill/mock_metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070032#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070033#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070034#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070035#include "shill/mock_wifi.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070036#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070037#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038#include "shill/wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039
40using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070041using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070042using std::string;
43using std::vector;
44
Chris Masone9be4a9d2011-05-16 15:44:09 -070045namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070046using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070047using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080048using ::testing::ContainerEq;
Paul Stewart22aa71b2011-09-16 12:15:11 -070049using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070050using ::testing::NiceMock;
51using ::testing::Return;
Gaurav Shah435de2c2011-11-17 19:01:07 -080052using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080053using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070054using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070055
Chris Masone3bd3c8c2011-06-13 08:20:26 -070056class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070057 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070058 ManagerTest()
Paul Stewart22aa71b2011-09-16 12:15:11 -070059 : mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070060 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080061 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070062 manager(),
63 "wifi0",
64 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080065 4)),
66 device_info_(new NiceMock<MockDeviceInfo>(
67 control_interface(),
68 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080069 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080070 reinterpret_cast<Manager*>(NULL))),
71 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070072 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
73 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080074 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070075 manager(),
76 "null0",
77 "addr0",
78 0));
79 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
80 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080081 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070082 manager(),
83 "null1",
84 "addr1",
85 1));
86 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
87 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080088 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070089 manager(),
90 "null2",
91 "addr2",
92 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -080093 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
94 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080095 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -080096 manager(),
97 "null3",
98 "addr3",
99 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700100 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800101
102 // Replace the manager's adaptor with a quieter one, and one
103 // we can do EXPECT*() against. Passes ownership.
104 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700105 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700106 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700107
Paul Stewartfdd16072011-09-16 12:41:35 -0700108 bool IsDeviceRegistered(const DeviceRefPtr &device,
109 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700110 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700111 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700112 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700113 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700114 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700115
Paul Stewarta849a3d2011-11-03 05:54:09 -0700116 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
117 manager->profiles_.push_back(profile);
118 }
119
Paul Stewart75225512012-01-26 22:51:33 -0800120 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
121 return manager->ephemeral_profile_;
122 }
123
Chris Masone6515aab2011-10-12 16:19:09 -0700124 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
125 Profile::Identifier id("rather", "irrelevant");
126 scoped_ptr<Profile> profile(new Profile(control_interface(),
127 manager,
128 id,
129 "",
130 false));
131 FilePath final_path(storage_path());
132 final_path = final_path.Append("test.profile");
133 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
134 storage->set_path(final_path);
135 if (!storage->Open())
136 return NULL;
137 profile->set_storage(storage.release()); // Passes ownership.
138 return profile.release();
139 }
140
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700141 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
142 const string &profile_identifier,
143 const string &service_name) {
144 GLib glib;
145 KeyFileStore store(&glib);
146 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
147 return store.Open() &&
148 store.SetString(service_name, "rather", "irrelevant") &&
149 store.Close();
150 }
151
152 Error::Type TestCreateProfile(Manager *manager, const string &name) {
153 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800154 string path;
155 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700156 return error.type();
157 }
158
159 Error::Type TestPopAnyProfile(Manager *manager) {
160 Error error;
161 manager->PopAnyProfile(&error);
162 return error.type();
163 }
164
165 Error::Type TestPopProfile(Manager *manager, const string &name) {
166 Error error;
167 manager->PopProfile(name, &error);
168 return error.type();
169 }
170
171 Error::Type TestPushProfile(Manager *manager, const string &name) {
172 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800173 string path;
174 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700175 return error.type();
176 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000177
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700178 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000179 typedef scoped_refptr<MockService> MockServiceRefPtr;
180
181 MockServiceRefPtr MakeAutoConnectableService() {
182 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
183 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800184 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000185 manager());
186 service->MakeFavorite();
187 service->set_connectable(true);
188 return service;
189 }
190
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700191 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700192 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800193 scoped_ptr<MockDeviceInfo> device_info_;
194
195 // This pointer is owned by the manager, and only tracked here for EXPECT*()
196 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700197};
198
Paul Stewart22aa71b2011-09-16 12:15:11 -0700199bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
200 return (svc0.get() == manager()->services_[0].get() &&
201 svc1.get() == manager()->services_[1].get());
202}
203
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700204TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700205 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
206 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700207}
208
Chris Masone9be4a9d2011-05-16 15:44:09 -0700209TEST_F(ManagerTest, DeviceRegistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700210 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700211 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700212 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700213 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700214 ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular))
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700215 .WillByDefault(Return(true));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700216
Paul Stewart22aa71b2011-09-16 12:15:11 -0700217 manager()->RegisterDevice(mock_devices_[0]);
218 manager()->RegisterDevice(mock_devices_[1]);
219 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700220
Paul Stewart22aa71b2011-09-16 12:15:11 -0700221 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
222 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
223 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700224}
225
Paul Stewarta41e38d2011-11-11 07:47:29 -0800226TEST_F(ManagerTest, DeviceRegistrationAndStart) {
227 manager()->running_ = true;
228 mock_devices_[0]->powered_ = true;
229 mock_devices_[1]->powered_ = false;
230 EXPECT_CALL(*mock_devices_[0].get(), Start())
231 .Times(1);
232 EXPECT_CALL(*mock_devices_[1].get(), Start())
233 .Times(0);
234 manager()->RegisterDevice(mock_devices_[0]);
235 manager()->RegisterDevice(mock_devices_[1]);
236}
237
238TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
239 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
240 DeviceRefPtr device_ref(mock_devices_[0].get());
241 AdoptProfile(manager(), profile); // Passes ownership.
242 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
243 EXPECT_CALL(*profile, Save());
244 manager()->RegisterDevice(mock_devices_[0]);
245}
246
Chris Masone9be4a9d2011-05-16 15:44:09 -0700247TEST_F(ManagerTest, DeviceDeregistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700248 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700249 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700250 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700251 .WillByDefault(Return(true));
252
Gaurav Shah435de2c2011-11-17 19:01:07 -0800253 manager()->RegisterDevice(mock_devices_[0]);
254 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700255
Paul Stewart22aa71b2011-09-16 12:15:11 -0700256 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
257 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700258
Paul Stewart22aa71b2011-09-16 12:15:11 -0700259 EXPECT_CALL(*mock_devices_[0].get(), Stop());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800260 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700261 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700262
Paul Stewart22aa71b2011-09-16 12:15:11 -0700263 EXPECT_CALL(*mock_devices_[1].get(), Stop());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800264 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700265 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700266}
267
268TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700269 // It's much easier and safer to use a real GLib for this test.
270 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700271 Manager manager(control_interface(),
272 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800273 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700274 &glib,
275 run_path(),
276 storage_path(),
277 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700278 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
279 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700280 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700281
Chris Masone9be4a9d2011-05-16 15:44:09 -0700282 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700283 new NiceMock<MockService>(control_interface(),
284 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800285 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700286 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700287 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700288 new NiceMock<MockService>(control_interface(),
289 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800290 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700291 &manager));
mukesh agrawal51a7e932011-07-27 16:18:26 -0700292 string service1_name(mock_service->UniqueName());
293 string service2_name(mock_service2->UniqueName());
294
295 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
296 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700297 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700298 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700299 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700300 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700301 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700302
Chris Masone9d779932011-08-25 16:33:41 -0700303 manager.RegisterService(mock_service);
304 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700305
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800306 Error error;
307 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700308 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700309 EXPECT_EQ(2, ids.size());
310 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
311 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700312
Chris Masone9d779932011-08-25 16:33:41 -0700313 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
314 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
315
316 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700317}
318
Chris Masone6515aab2011-10-12 16:19:09 -0700319TEST_F(ManagerTest, RegisterKnownService) {
320 // It's much easier and safer to use a real GLib for this test.
321 GLib glib;
322 Manager manager(control_interface(),
323 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800324 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700325 &glib,
326 run_path(),
327 storage_path(),
328 string());
329 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
330 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700331 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700332 {
333 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
334 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800335 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700336 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700337 ASSERT_TRUE(profile->AdoptService(service1));
338 ASSERT_TRUE(profile->ContainsService(service1));
339 } // Force destruction of service1.
340
341 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
342 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800343 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700344 &manager));
345 manager.RegisterService(service2);
346 EXPECT_EQ(service2->profile().get(), profile.get());
347 manager.Stop();
348}
349
350TEST_F(ManagerTest, RegisterUnknownService) {
351 // It's much easier and safer to use a real GLib for this test.
352 GLib glib;
353 Manager manager(control_interface(),
354 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800355 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700356 &glib,
357 run_path(),
358 storage_path(),
359 string());
360 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
361 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700362 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700363 {
364 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
365 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800366 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700367 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700368 ASSERT_TRUE(profile->AdoptService(service1));
369 ASSERT_TRUE(profile->ContainsService(service1));
370 } // Force destruction of service1.
371 scoped_refptr<MockService> mock_service2(
372 new NiceMock<MockService>(control_interface(),
373 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800374 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700375 &manager));
376 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
377 .WillRepeatedly(Return(mock_service2->UniqueName()));
378 manager.RegisterService(mock_service2);
379 EXPECT_NE(mock_service2->profile().get(), profile.get());
380 manager.Stop();
381}
382
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000383TEST_F(ManagerTest, DeregisterUnregisteredService) {
384 // WiFi assumes that it can deregister a service that is not
385 // registered. (E.g. a hidden service can be deregistered when it
386 // loses its last endpoint, and again when WiFi is Stop()-ed.)
387 //
388 // So test that doing so doesn't cause a crash.
389 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
390 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800391 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000392 manager());
393 manager()->DeregisterService(service);
394}
395
Chris Masonea8a2c252011-06-27 22:16:30 -0700396TEST_F(ManagerTest, GetProperties) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700397 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700398 AdoptProfile(manager(), profile);
Chris Masonea8a2c252011-06-27 22:16:30 -0700399 map<string, ::DBus::Variant> props;
400 Error error(Error::kInvalidProperty, "");
401 {
402 ::DBus::Error dbus_error;
403 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700404 manager()->mutable_store()->SetStringProperty(
405 flimflam::kCheckPortalListProperty,
406 expected,
407 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700408 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700409 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
410 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
411 expected);
412 }
413 {
414 ::DBus::Error dbus_error;
415 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700416 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
417 expected,
418 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700419 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700420 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
421 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
422 expected);
423 }
424}
425
Chris Masone3c3f6a12011-07-01 10:01:41 -0700426TEST_F(ManagerTest, GetDevicesProperty) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700427 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700428 AdoptProfile(manager(), profile);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800429 manager()->RegisterDevice(mock_devices_[0]);
430 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700431 {
432 map<string, ::DBus::Variant> props;
433 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700434 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700435 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
436 Strings devices =
437 props[flimflam::kDevicesProperty].operator vector<string>();
438 EXPECT_EQ(2, devices.size());
439 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700440}
441
Chris Masone6791a432011-07-12 13:23:19 -0700442TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700443 Manager manager(control_interface(),
444 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800445 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700446 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700447 run_path(),
448 storage_path(),
449 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700450 scoped_refptr<MockService> s2(new MockService(control_interface(),
451 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800452 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700453 &manager));
454 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700455 {
Chris Masone6515aab2011-10-12 16:19:09 -0700456 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700457 ProfileRefPtr profile(
458 new Profile(control_interface(), &manager, id, "", false));
459 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700460 // Say we don't have |s2| the first time asked, then that we do.
461 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
462 .WillOnce(Return(false))
463 .WillRepeatedly(Return(true));
464 EXPECT_CALL(*storage, Flush())
465 .Times(AnyNumber())
466 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700467 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700468 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700469 }
Chris Masone6515aab2011-10-12 16:19:09 -0700470 // Create a profile that already has |s2| in it.
471 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
472 profile->AdoptService(s2);
Chris Masone9d779932011-08-25 16:33:41 -0700473
Chris Masone6515aab2011-10-12 16:19:09 -0700474 // Now, move the Service |s2| to another profile.
475 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
476 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700477
478 // Force destruction of the original Profile, to ensure that the Service
479 // is kept alive and populated with data.
480 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700481 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700482 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700483}
484
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800485TEST_F(ManagerTest, SetProfileForService) {
486 scoped_refptr<MockProfile> profile0(
487 new MockProfile(control_interface(), manager(), ""));
488 string profile_name0("profile0");
489 EXPECT_CALL(*profile0, GetRpcIdentifier())
490 .WillRepeatedly(Return(profile_name0));
491 AdoptProfile(manager(), profile0);
492 scoped_refptr<MockService> service(new MockService(control_interface(),
493 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800494 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800495 manager()));
496 service->set_profile(profile0);
497
498 {
499 Error error;
500 manager()->SetProfileForService(service, "foo", &error);
501 EXPECT_EQ(Error::kInvalidArguments, error.type());
502 EXPECT_EQ("Unknown Profile requested for Service", error.message());
503 }
504
505 {
506 Error error;
507 manager()->SetProfileForService(service, profile_name0, &error);
508 EXPECT_EQ(Error::kInvalidArguments, error.type());
509 EXPECT_EQ("Service is already connected to this profile", error.message());
510 }
511
512 scoped_refptr<MockProfile> profile1(
513 new MockProfile(control_interface(), manager(), ""));
514 string profile_name1("profile1");
515 EXPECT_CALL(*profile1, GetRpcIdentifier())
516 .WillRepeatedly(Return(profile_name1));
517 AdoptProfile(manager(), profile1);
518
519 {
520 Error error;
521 EXPECT_CALL(*profile1, AdoptService(_))
522 .WillOnce(Return(true));
523 EXPECT_CALL(*profile0, AbandonService(_))
524 .WillOnce(Return(true));
525 manager()->SetProfileForService(service, profile_name1, &error);
526 EXPECT_TRUE(error.IsSuccess());
527 }
528}
529
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700530TEST_F(ManagerTest, CreateProfile) {
531 // It's much easier to use real Glib here since we want the storage
532 // side-effects.
533 GLib glib;
534 ScopedTempDir temp_dir;
535 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
536
537 Manager manager(control_interface(),
538 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800539 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700540 &glib,
541 run_path(),
542 storage_path(),
543 temp_dir.path().value());
544
545 // Invalid name should be rejected.
546 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
547
548 // Valid name is still rejected because we can't create a profile
549 // that doesn't have a user component. Such profile names are
550 // reserved for the single DefaultProfile the manager creates
551 // at startup.
552 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid"));
553
554 // We should succeed in creating a valid user profile.
555 const char kProfile[] = "~user/profile";
556 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile));
557
558 // We should fail in creating it a second time (already exists).
559 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
560}
561
562TEST_F(ManagerTest, PushPopProfile) {
563 // It's much easier to use real Glib in creating a Manager for this
564 // test here since we want the storage side-effects.
565 GLib glib;
566 ScopedTempDir temp_dir;
567 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
568 Manager manager(control_interface(),
569 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800570 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700571 &glib,
572 run_path(),
573 storage_path(),
574 temp_dir.path().value());
575
576 // Pushing an invalid profile should fail.
577 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
578
579 // Pushing a default profile name should fail.
580 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default"));
581
582 const char kProfile0[] = "~user/profile0";
583 const char kProfile1[] = "~user/profile1";
584
585 // Create a couple of profiles.
586 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
587 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
588
589 // Push these profiles on the stack.
590 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
591 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
592
593 // Pushing a profile a second time should fail.
594 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
595 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
596
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800597 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700598 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800599 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700600
601 // Make sure a profile name that doesn't exist fails.
602 const char kProfile2Id[] = "profile2";
603 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
604 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
605
606 // Create a new service, with a specific storage name.
607 scoped_refptr<MockService> service(
608 new NiceMock<MockService>(control_interface(),
609 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800610 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700611 &manager));
612 const char kServiceName[] = "service_storage_name";
613 EXPECT_CALL(*service.get(), GetStorageIdentifier())
614 .WillRepeatedly(Return(kServiceName));
615 EXPECT_CALL(*service.get(), Load(_))
616 .WillRepeatedly(Return(true));
617
618 // Add this service to the manager -- it should end up in the ephemeral
619 // profile.
620 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800621 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700622
623 // Create storage for a profile that contains the service storage name.
624 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
625 kServiceName));
626
627 // When we push the profile, the service should move away from the
628 // ephemeral profile to this new profile since it has an entry for
629 // this service.
630 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800631 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700632 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
633
634 // Insert another profile that should supersede ownership of the service.
635 const char kProfile3Id[] = "profile3";
636 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
637 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
638 kServiceName));
639 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
640 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
641
642 // Popping an invalid profile name should fail.
643 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
644
645 // Popping an profile that is not at the top of the stack should fail.
646 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
647
648 // Popping the top profile should succeed.
649 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
650
651 // Moreover the service should have switched profiles to profile 2.
652 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
653
654 // Popping the top profile should succeed.
655 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
656
657 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800658 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700659
660 // Pop the remaining two services off the stack.
661 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
662 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
663
664 // Next pop should fail with "stack is empty".
665 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
666}
667
Paul Stewart75225512012-01-26 22:51:33 -0800668// Use this matcher instead of passing RefPtrs directly into the arguments
669// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
670// system teardown.
671MATCHER_P(IsRefPtrTo, ref_address, "") {
672 return arg.get() == ref_address;
673}
674
675TEST_F(ManagerTest, HandleProfileEntryDeletion) {
676 MockServiceRefPtr s_not_in_profile(
677 new NiceMock<MockService>(control_interface(),
678 dispatcher(),
679 metrics(),
680 manager()));
681 MockServiceRefPtr s_not_in_group(
682 new NiceMock<MockService>(control_interface(),
683 dispatcher(),
684 metrics(),
685 manager()));
686 MockServiceRefPtr s_configure_fail(
687 new NiceMock<MockService>(control_interface(),
688 dispatcher(),
689 metrics(),
690 manager()));
691 MockServiceRefPtr s_configure_succeed(
692 new NiceMock<MockService>(control_interface(),
693 dispatcher(),
694 metrics(),
695 manager()));
696
697 string entry_name("entry_name");
698 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
699 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
700 .WillRepeatedly(Return("not_entry_name"));
701 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
702 .WillRepeatedly(Return(entry_name));
703 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
704 .WillRepeatedly(Return(entry_name));
705
706 manager()->RegisterService(s_not_in_profile);
707 manager()->RegisterService(s_not_in_group);
708 manager()->RegisterService(s_configure_fail);
709 manager()->RegisterService(s_configure_succeed);
710
711 scoped_refptr<MockProfile> profile0(
712 new StrictMock<MockProfile>(control_interface(), manager(), ""));
713 scoped_refptr<MockProfile> profile1(
714 new StrictMock<MockProfile>(control_interface(), manager(), ""));
715
716 s_not_in_group->set_profile(profile1);
717 s_configure_fail->set_profile(profile1);
718 s_configure_succeed->set_profile(profile1);
719
720 AdoptProfile(manager(), profile0);
721 AdoptProfile(manager(), profile1);
722
723 // No services are a member of this profile.
724 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
725
726 // No services that are members of this profile have this entry name.
727 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
728
729 // Only services that are members of the profile and group will be abandoned.
730 EXPECT_CALL(*profile1.get(),
731 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
732 EXPECT_CALL(*profile1.get(),
733 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
734 EXPECT_CALL(*profile1.get(),
735 AbandonService(IsRefPtrTo(s_configure_fail.get())))
736 .WillOnce(Return(true));
737 EXPECT_CALL(*profile1.get(),
738 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
739 .WillOnce(Return(true));
740
741 // Never allow services to re-join profile1.
742 EXPECT_CALL(*profile1.get(), ConfigureService(_))
743 .WillRepeatedly(Return(false));
744
745 // Only allow one of the members of the profile and group to successfully
746 // join profile0.
747 EXPECT_CALL(*profile0.get(),
748 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
749 EXPECT_CALL(*profile0.get(),
750 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
751 EXPECT_CALL(*profile0.get(),
752 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
753 .WillOnce(Return(false));
754 EXPECT_CALL(*profile0.get(),
755 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
756 .WillOnce(Return(true));
757
758 // Expect the failed-to-configure service to have Unload() called on it.
759 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
760 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
761 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
762 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
763
764 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
765
766 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
767 EXPECT_EQ(profile1, s_not_in_group->profile());
768 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
769
770 // Since we are using a MockProfile, the profile does not actually change,
771 // since ConfigureService was not actually called on the service.
772 EXPECT_EQ(profile1, s_configure_succeed->profile());
773}
774
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700775TEST_F(ManagerTest, Dispatch) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700776 {
777 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700778 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700779 flimflam::kOfflineModeProperty,
780 PropertyStoreTest::kBoolV,
781 &error));
782 }
783 {
784 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700785 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700786 flimflam::kCountryProperty,
787 PropertyStoreTest::kStringV,
788 &error));
789 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700790 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700791 {
792 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700793 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700794 flimflam::kCountryProperty,
795 PropertyStoreTest::kBoolV,
796 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700797 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700798 }
799 {
800 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700801 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700802 flimflam::kOfflineModeProperty,
803 PropertyStoreTest::kStringV,
804 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700805 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700806 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700807 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700808 {
809 ::DBus::Error error;
810 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700811 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700812 flimflam::kEnabledTechnologiesProperty,
813 PropertyStoreTest::kStringsV,
814 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700815 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700816 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700817}
818
mukesh agrawal32399322011-09-01 10:53:43 -0700819TEST_F(ManagerTest, RequestScan) {
820 {
821 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700822 manager()->RegisterDevice(mock_devices_[0].get());
823 manager()->RegisterDevice(mock_devices_[1].get());
824 EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -0700825 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700826 EXPECT_CALL(*mock_devices_[0], Scan(_));
827 EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -0700828 .WillRepeatedly(Return(false));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700829 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -0700830 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700831 }
832
833 {
834 Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700835 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700836 EXPECT_EQ(Error::kInvalidArguments, error.type());
837 }
838}
839
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700840TEST_F(ManagerTest, GetWifiServiceNoDevice) {
841 KeyValueStore args;
842 Error e;
843 manager()->GetWifiService(args, &e);
844 EXPECT_EQ(Error::kInvalidArguments, e.type());
845 EXPECT_EQ("no wifi devices available", e.message());
846}
847
848TEST_F(ManagerTest, GetWifiService) {
849 KeyValueStore args;
850 Error e;
851 WiFiServiceRefPtr wifi_service;
852
853 manager()->RegisterDevice(mock_wifi_);
854 EXPECT_CALL(*mock_wifi_, GetService(_, _))
855 .WillRepeatedly(Return(wifi_service));
856 manager()->GetWifiService(args, &e);
857}
858
Paul Stewart22aa71b2011-09-16 12:15:11 -0700859TEST_F(ManagerTest, TechnologyOrder) {
860 Error error;
861 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
862 string(flimflam::kTypeWifi), &error);
863 ASSERT_TRUE(error.IsSuccess());
864 EXPECT_EQ(manager()->GetTechnologyOrder(),
865 string(flimflam::kTypeEthernet) + "," +
866 string(flimflam::kTypeWifi));
867
868 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
869 string(flimflam::kTypeWifi), &error);
870 ASSERT_FALSE(error.IsSuccess());
871 EXPECT_EQ(Error::kInvalidArguments, error.type());
872 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
873 string(flimflam::kTypeWifi),
874 manager()->GetTechnologyOrder());
875}
876
877TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +0000878 // TODO(quiche): Some of these tests would probably fit better in
879 // service_unittest, since the actual comparison of Services is
880 // implemented in Service. (crosbug.com/23370)
881
Paul Stewart22aa71b2011-09-16 12:15:11 -0700882 scoped_refptr<MockService> mock_service0(
883 new NiceMock<MockService>(control_interface(),
884 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800885 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700886 manager()));
887 scoped_refptr<MockService> mock_service1(
888 new NiceMock<MockService>(control_interface(),
889 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800890 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700891 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700892
893 manager()->RegisterService(mock_service0);
894 manager()->RegisterService(mock_service1);
895
896 // Services should already be sorted by UniqueName
897 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
898
899 // Asking explictly to sort services should not change anything
900 manager()->SortServices();
901 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
902
903 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100904 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700905 manager()->UpdateService(mock_service1);
906 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
907
908 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -0700909 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700910 manager()->UpdateService(mock_service0);
911 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
912
913 // Technology
914 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi))
915 .WillRepeatedly(Return(true));
916 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet))
917 .WillRepeatedly(Return(true));
918 // NB: Redefine default (false) return values so we don't use the default rule
919 // which makes the logs noisier
920 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi)))
921 .WillRepeatedly(Return(false));
922 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet)))
923 .WillRepeatedly(Return(false));
924
925 Error error;
926 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
927 string(flimflam::kTypeWifi), &error);
928 EXPECT_TRUE(error.IsSuccess());
929 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
930
931 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
932 string(flimflam::kTypeEthernet), &error);
933 EXPECT_TRUE(error.IsSuccess());
934 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
935
Gaurav Shah435de2c2011-11-17 19:01:07 -0800936 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700937 mock_service0->set_priority(1);
938 manager()->UpdateService(mock_service0);
939 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
940
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000941 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +0000942 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -0700943 manager()->UpdateService(mock_service1);
944 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
945
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000946 // Auto-connect.
947 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700948 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000949 mock_service1->set_auto_connect(false);
950 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700951 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
952
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000953 // Connectable.
954 mock_service1->set_connectable(true);
955 manager()->UpdateService(mock_service1);
956 mock_service0->set_connectable(false);
957 manager()->UpdateService(mock_service0);
958 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
959
960 // IsFailed.
961 EXPECT_CALL(*mock_service0.get(), state())
962 .WillRepeatedly(Return(Service::kStateIdle));
963 EXPECT_CALL(*mock_service0.get(), IsFailed())
964 .WillRepeatedly(Return(false));
965 manager()->UpdateService(mock_service0);
966 EXPECT_CALL(*mock_service0.get(), state())
967 .WillRepeatedly(Return(Service::kStateFailure));
968 EXPECT_CALL(*mock_service1.get(), IsFailed())
969 .WillRepeatedly(Return(true));
970 manager()->UpdateService(mock_service1);
971 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
972
973 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700974 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000975 .WillRepeatedly(Return(Service::kStateAssociating));
976 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -0800977 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700978 manager()->UpdateService(mock_service1);
979 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
980
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000981 // Connected.
982 EXPECT_CALL(*mock_service0.get(), state())
983 .WillRepeatedly(Return(Service::kStateConnected));
984 EXPECT_CALL(*mock_service0.get(), IsConnected())
985 .WillRepeatedly(Return(true));
986 manager()->UpdateService(mock_service0);
987 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
988
Paul Stewart22aa71b2011-09-16 12:15:11 -0700989 manager()->DeregisterService(mock_service0);
990 manager()->DeregisterService(mock_service1);
991}
992
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800993TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +0000994 MockMetrics mock_metrics;
995 manager()->set_metrics(&mock_metrics);
996
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800997 scoped_refptr<MockService> mock_service0(
998 new NiceMock<MockService>(control_interface(),
999 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001000 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001001 manager()));
1002 scoped_refptr<MockService> mock_service1(
1003 new NiceMock<MockService>(control_interface(),
1004 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001005 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001006 manager()));
1007
1008 scoped_refptr<MockConnection> mock_connection0(
1009 new NiceMock<MockConnection>(device_info_.get()));
1010 scoped_refptr<MockConnection> mock_connection1(
1011 new NiceMock<MockConnection>(device_info_.get()));
1012
1013 manager()->RegisterService(mock_service0);
1014 manager()->RegisterService(mock_service1);
1015
1016 mock_service0->connection_ = mock_connection0;
1017 mock_service1->connection_ = mock_connection1;
1018
1019 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001020 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001021 manager()->SortServices();
1022
1023 mock_service1->set_priority(1);
1024 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1025 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001026 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001027 manager()->SortServices();
1028
1029 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001030 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001031 mock_service1->connection_ = NULL;
1032 manager()->DeregisterService(mock_service1);
1033
1034 mock_service0->connection_ = NULL;
1035 manager()->DeregisterService(mock_service0);
1036}
1037
Gaurav Shah435de2c2011-11-17 19:01:07 -08001038TEST_F(ManagerTest, AvailableTechnologies) {
1039 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1040 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001041 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001042 manager(),
1043 "null4",
1044 "addr4",
1045 0));
1046 manager()->RegisterDevice(mock_devices_[0]);
1047 manager()->RegisterDevice(mock_devices_[1]);
1048 manager()->RegisterDevice(mock_devices_[2]);
1049 manager()->RegisterDevice(mock_devices_[3]);
1050
1051 ON_CALL(*mock_devices_[0].get(), technology())
1052 .WillByDefault(Return(Technology::kEthernet));
1053 ON_CALL(*mock_devices_[1].get(), technology())
1054 .WillByDefault(Return(Technology::kWifi));
1055 ON_CALL(*mock_devices_[2].get(), technology())
1056 .WillByDefault(Return(Technology::kCellular));
1057 ON_CALL(*mock_devices_[3].get(), technology())
1058 .WillByDefault(Return(Technology::kWifi));
1059
1060 set<string> expected_technologies;
1061 expected_technologies.insert(Technology::NameFromIdentifier(
1062 Technology::kEthernet));
1063 expected_technologies.insert(Technology::NameFromIdentifier(
1064 Technology::kWifi));
1065 expected_technologies.insert(Technology::NameFromIdentifier(
1066 Technology::kCellular));
1067 Error error;
1068 vector<string> technologies = manager()->AvailableTechnologies(&error);
1069
1070 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1071 ContainerEq(expected_technologies));
1072}
1073
1074TEST_F(ManagerTest, ConnectedTechnologies) {
1075 scoped_refptr<MockService> connected_service1(
1076 new NiceMock<MockService>(control_interface(),
1077 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001078 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001079 manager()));
1080 scoped_refptr<MockService> connected_service2(
1081 new NiceMock<MockService>(control_interface(),
1082 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001083 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001084 manager()));
1085 scoped_refptr<MockService> disconnected_service1(
1086 new NiceMock<MockService>(control_interface(),
1087 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001088 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001089 manager()));
1090 scoped_refptr<MockService> disconnected_service2(
1091 new NiceMock<MockService>(control_interface(),
1092 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001093 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001094 manager()));
1095
1096 ON_CALL(*connected_service1.get(), IsConnected())
1097 .WillByDefault(Return(true));
1098 ON_CALL(*connected_service2.get(), IsConnected())
1099 .WillByDefault(Return(true));
1100
1101 manager()->RegisterService(connected_service1);
1102 manager()->RegisterService(connected_service2);
1103 manager()->RegisterService(disconnected_service1);
1104 manager()->RegisterService(disconnected_service2);
1105
1106 manager()->RegisterDevice(mock_devices_[0]);
1107 manager()->RegisterDevice(mock_devices_[1]);
1108 manager()->RegisterDevice(mock_devices_[2]);
1109 manager()->RegisterDevice(mock_devices_[3]);
1110
1111 ON_CALL(*mock_devices_[0].get(), technology())
1112 .WillByDefault(Return(Technology::kEthernet));
1113 ON_CALL(*mock_devices_[1].get(), technology())
1114 .WillByDefault(Return(Technology::kWifi));
1115 ON_CALL(*mock_devices_[2].get(), technology())
1116 .WillByDefault(Return(Technology::kCellular));
1117 ON_CALL(*mock_devices_[3].get(), technology())
1118 .WillByDefault(Return(Technology::kWifi));
1119
1120 mock_devices_[0]->SelectService(connected_service1);
1121 mock_devices_[1]->SelectService(disconnected_service1);
1122 mock_devices_[2]->SelectService(disconnected_service2);
1123 mock_devices_[3]->SelectService(connected_service2);
1124
1125 set<string> expected_technologies;
1126 expected_technologies.insert(Technology::NameFromIdentifier(
1127 Technology::kEthernet));
1128 expected_technologies.insert(Technology::NameFromIdentifier(
1129 Technology::kWifi));
1130 Error error;
1131
1132 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1133 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1134 ContainerEq(expected_technologies));
1135}
1136
1137TEST_F(ManagerTest, DefaultTechnology) {
1138 scoped_refptr<MockService> connected_service(
1139 new NiceMock<MockService>(control_interface(),
1140 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001141 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001142 manager()));
1143 scoped_refptr<MockService> disconnected_service(
1144 new NiceMock<MockService>(control_interface(),
1145 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001146 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001147 manager()));
1148
1149 // Connected. WiFi.
1150 ON_CALL(*connected_service.get(), IsConnected())
1151 .WillByDefault(Return(true));
1152 ON_CALL(*connected_service.get(), state())
1153 .WillByDefault(Return(Service::kStateConnected));
1154 ON_CALL(*connected_service.get(), technology())
1155 .WillByDefault(Return(Technology::kWifi));
1156
1157 // Disconnected. Ethernet.
1158 ON_CALL(*disconnected_service.get(), technology())
1159 .WillByDefault(Return(Technology::kEthernet));
1160
1161 manager()->RegisterService(disconnected_service);
1162 Error error;
1163 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1164
1165
1166 manager()->RegisterService(connected_service);
1167 // Connected service should be brought to the front now.
1168 string expected_technology =
1169 Technology::NameFromIdentifier(Technology::kWifi);
1170 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1171}
1172
Thieu Le1271d682011-11-02 22:48:19 +00001173TEST_F(ManagerTest, DisconnectServicesOnStop) {
1174 scoped_refptr<MockService> mock_service(
1175 new NiceMock<MockService>(control_interface(),
1176 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001177 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001178 manager()));
1179 manager()->RegisterService(mock_service);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001180 EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001181 manager()->Stop();
1182}
1183
mukesh agrawal00917ce2011-11-22 23:56:55 +00001184TEST_F(ManagerTest, UpdateServiceConnected) {
1185 scoped_refptr<MockService> mock_service(
1186 new NiceMock<MockService>(control_interface(),
1187 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001188 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001189 manager()));
1190 manager()->RegisterService(mock_service);
1191 EXPECT_FALSE(mock_service->favorite());
1192 EXPECT_FALSE(mock_service->auto_connect());
1193
Gaurav Shah435de2c2011-11-17 19:01:07 -08001194 EXPECT_CALL(*mock_service.get(), IsConnected())
1195 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001196 manager()->UpdateService(mock_service);
1197 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1198 // to mock out MakeFavorite. And mocking that out would break the
1199 // SortServices test. (crosbug.com/23370)
1200 EXPECT_TRUE(mock_service->favorite());
1201 EXPECT_TRUE(mock_service->auto_connect());
1202}
1203
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001204TEST_F(ManagerTest, SaveSuccessfulService) {
1205 scoped_refptr<MockProfile> profile(
1206 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1207 AdoptProfile(manager(), profile);
1208 scoped_refptr<MockService> service(
1209 new NiceMock<MockService>(control_interface(),
1210 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001211 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001212 manager()));
1213
1214 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
1215 ServiceRefPtr expect_service(service.get());
1216
1217 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
1218 .WillOnce(Return(false));
1219 manager()->RegisterService(service);
1220
1221 EXPECT_CALL(*service.get(), state())
1222 .WillRepeatedly(Return(Service::kStateConnected));
1223 EXPECT_CALL(*service.get(), IsConnected())
1224 .WillRepeatedly(Return(true));
1225 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
1226 .WillOnce(Return(true));
1227 manager()->UpdateService(service);
1228}
1229
Paul Stewart1b253142012-01-26 14:05:52 -08001230TEST_F(ManagerTest, EnumerateProfiles) {
1231 vector<string> profile_paths;
1232 for (size_t i = 0; i < 10; i++) {
1233 scoped_refptr<MockProfile> profile(
1234 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05001235 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08001236 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
1237 .WillOnce(Return(profile_paths.back()));
1238 AdoptProfile(manager(), profile);
1239 }
1240
1241 Error error;
1242 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
1243 EXPECT_TRUE(error.IsSuccess());
1244 EXPECT_EQ(profile_paths.size(), returned_paths.size());
1245 for (size_t i = 0; i < profile_paths.size(); i++) {
1246 EXPECT_EQ(profile_paths[i], returned_paths[i]);
1247 }
1248}
1249
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001250TEST_F(ManagerTest, AutoConnectOnRegister) {
1251 MockServiceRefPtr service = MakeAutoConnectableService();
1252 EXPECT_CALL(*service.get(), AutoConnect());
1253 manager()->RegisterService(service);
1254 dispatcher()->DispatchPendingEvents();
1255}
1256
1257TEST_F(ManagerTest, AutoConnectOnUpdate) {
1258 MockServiceRefPtr service1 = MakeAutoConnectableService();
1259 service1->set_priority(1);
1260 MockServiceRefPtr service2 = MakeAutoConnectableService();
1261 service2->set_priority(2);
1262 manager()->RegisterService(service1);
1263 manager()->RegisterService(service2);
1264 dispatcher()->DispatchPendingEvents();
1265
1266 EXPECT_CALL(*service1.get(), AutoConnect());
1267 EXPECT_CALL(*service2.get(), state())
1268 .WillRepeatedly(Return(Service::kStateFailure));
1269 EXPECT_CALL(*service2.get(), IsFailed())
1270 .WillRepeatedly(Return(true));
1271 EXPECT_CALL(*service2.get(), IsConnected())
1272 .WillRepeatedly(Return(false));
1273 manager()->UpdateService(service2);
1274 dispatcher()->DispatchPendingEvents();
1275}
1276
1277TEST_F(ManagerTest, AutoConnectOnDeregister) {
1278 MockServiceRefPtr service1 = MakeAutoConnectableService();
1279 service1->set_priority(1);
1280 MockServiceRefPtr service2 = MakeAutoConnectableService();
1281 service2->set_priority(2);
1282 manager()->RegisterService(service1);
1283 manager()->RegisterService(service2);
1284 dispatcher()->DispatchPendingEvents();
1285
1286 EXPECT_CALL(*service1.get(), AutoConnect());
1287 manager()->DeregisterService(service2);
1288 dispatcher()->DispatchPendingEvents();
1289}
1290
Chris Masone9be4a9d2011-05-16 15:44:09 -07001291} // namespace shill