blob: afc2fc888bacf469256d3fd6af8cbda49a44babf [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>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/scoped_temp_dir.h>
13#include <base/stl_util.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"
Paul Stewart7f61e522012-03-22 11:13:45 -070036#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070037#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070038#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070039#include "shill/wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070040
41using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070042using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043using std::string;
44using std::vector;
45
Chris Masone9be4a9d2011-05-16 15:44:09 -070046namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070047using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070048using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080049using ::testing::ContainerEq;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070050using ::testing::InSequence;
Paul Stewart22aa71b2011-09-16 12:15:11 -070051using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070052using ::testing::NiceMock;
53using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070054using ::testing::ReturnRef;
Gaurav Shah435de2c2011-11-17 19:01:07 -080055using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080056using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070057using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070058
Chris Masone3bd3c8c2011-06-13 08:20:26 -070059class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070060 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070061 ManagerTest()
Paul Stewart22aa71b2011-09-16 12:15:11 -070062 : mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070063 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080064 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070065 manager(),
66 "wifi0",
67 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080068 4)),
69 device_info_(new NiceMock<MockDeviceInfo>(
70 control_interface(),
71 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080072 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080073 reinterpret_cast<Manager*>(NULL))),
74 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070075 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
76 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080077 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070078 manager(),
79 "null0",
80 "addr0",
81 0));
82 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
83 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080084 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070085 manager(),
86 "null1",
87 "addr1",
88 1));
89 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
90 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080091 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070092 manager(),
93 "null2",
94 "addr2",
95 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -080096 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
97 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080098 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -080099 manager(),
100 "null3",
101 "addr3",
102 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700103 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800104
105 // Replace the manager's adaptor with a quieter one, and one
106 // we can do EXPECT*() against. Passes ownership.
107 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700108 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700109 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700110
Paul Stewartfdd16072011-09-16 12:41:35 -0700111 bool IsDeviceRegistered(const DeviceRefPtr &device,
112 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700113 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700114 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700115 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700116 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700117 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700118
Paul Stewarta849a3d2011-11-03 05:54:09 -0700119 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
120 manager->profiles_.push_back(profile);
121 }
122
Paul Stewart75225512012-01-26 22:51:33 -0800123 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
124 return manager->ephemeral_profile_;
125 }
126
Chris Masone6515aab2011-10-12 16:19:09 -0700127 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
128 Profile::Identifier id("rather", "irrelevant");
129 scoped_ptr<Profile> profile(new Profile(control_interface(),
130 manager,
131 id,
132 "",
133 false));
134 FilePath final_path(storage_path());
135 final_path = final_path.Append("test.profile");
136 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
137 storage->set_path(final_path);
138 if (!storage->Open())
139 return NULL;
140 profile->set_storage(storage.release()); // Passes ownership.
141 return profile.release();
142 }
143
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700144 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
145 const string &profile_identifier,
146 const string &service_name) {
147 GLib glib;
148 KeyFileStore store(&glib);
149 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
150 return store.Open() &&
151 store.SetString(service_name, "rather", "irrelevant") &&
152 store.Close();
153 }
154
155 Error::Type TestCreateProfile(Manager *manager, const string &name) {
156 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800157 string path;
158 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700159 return error.type();
160 }
161
162 Error::Type TestPopAnyProfile(Manager *manager) {
163 Error error;
164 manager->PopAnyProfile(&error);
165 return error.type();
166 }
167
168 Error::Type TestPopProfile(Manager *manager, const string &name) {
169 Error error;
170 manager->PopProfile(name, &error);
171 return error.type();
172 }
173
174 Error::Type TestPushProfile(Manager *manager, const string &name) {
175 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800176 string path;
177 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700178 return error.type();
179 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000180
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700181 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000182 typedef scoped_refptr<MockService> MockServiceRefPtr;
183
184 MockServiceRefPtr MakeAutoConnectableService() {
185 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
186 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800187 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000188 manager());
189 service->MakeFavorite();
190 service->set_connectable(true);
191 return service;
192 }
193
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700194 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700195 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800196 scoped_ptr<MockDeviceInfo> device_info_;
197
198 // This pointer is owned by the manager, and only tracked here for EXPECT*()
199 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700200};
201
Paul Stewart22aa71b2011-09-16 12:15:11 -0700202bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
203 return (svc0.get() == manager()->services_[0].get() &&
204 svc1.get() == manager()->services_[1].get());
205}
206
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700207TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700208 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
209 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700210}
211
Chris Masone9be4a9d2011-05-16 15:44:09 -0700212TEST_F(ManagerTest, DeviceRegistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700213 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700214 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700215 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700216 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700217 ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular))
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700218 .WillByDefault(Return(true));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700219
Paul Stewart22aa71b2011-09-16 12:15:11 -0700220 manager()->RegisterDevice(mock_devices_[0]);
221 manager()->RegisterDevice(mock_devices_[1]);
222 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700223
Paul Stewart22aa71b2011-09-16 12:15:11 -0700224 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
225 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
226 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700227}
228
Paul Stewarta41e38d2011-11-11 07:47:29 -0800229TEST_F(ManagerTest, DeviceRegistrationAndStart) {
230 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500231 mock_devices_[0]->enabled_persistent_ = true;
232 mock_devices_[1]->enabled_persistent_ = false;
233 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800234 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500235 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800236 .Times(0);
237 manager()->RegisterDevice(mock_devices_[0]);
238 manager()->RegisterDevice(mock_devices_[1]);
239}
240
241TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
242 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
243 DeviceRefPtr device_ref(mock_devices_[0].get());
244 AdoptProfile(manager(), profile); // Passes ownership.
245 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
246 EXPECT_CALL(*profile, Save());
247 manager()->RegisterDevice(mock_devices_[0]);
248}
249
Chris Masone9be4a9d2011-05-16 15:44:09 -0700250TEST_F(ManagerTest, DeviceDeregistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700251 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700252 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700253 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700254 .WillByDefault(Return(true));
255
Gaurav Shah435de2c2011-11-17 19:01:07 -0800256 manager()->RegisterDevice(mock_devices_[0]);
257 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700258
Paul Stewart22aa71b2011-09-16 12:15:11 -0700259 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
260 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700261
Eric Shienbrood9a245532012-03-07 14:20:39 -0500262 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800263 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700264 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700265
Eric Shienbrood9a245532012-03-07 14:20:39 -0500266 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800267 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700268 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700269}
270
271TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700272 // It's much easier and safer to use a real GLib for this test.
273 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700274 Manager manager(control_interface(),
275 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800276 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700277 &glib,
278 run_path(),
279 storage_path(),
280 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700281 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
282 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700283 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700284
Chris Masone9be4a9d2011-05-16 15:44:09 -0700285 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700286 new NiceMock<MockService>(control_interface(),
287 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800288 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700289 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700290 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700291 new NiceMock<MockService>(control_interface(),
292 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800293 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700294 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700295
mukesh agrawal51a7e932011-07-27 16:18:26 -0700296 string service1_name(mock_service->UniqueName());
297 string service2_name(mock_service2->UniqueName());
298
299 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
300 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700301 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700302 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700303 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700304 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700305 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700306
Chris Masone9d779932011-08-25 16:33:41 -0700307 manager.RegisterService(mock_service);
308 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700309
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800310 Error error;
311 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700312 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700313 EXPECT_EQ(2, ids.size());
314 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
315 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700316
Chris Masone9d779932011-08-25 16:33:41 -0700317 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
318 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
319
320 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700321}
322
Chris Masone6515aab2011-10-12 16:19:09 -0700323TEST_F(ManagerTest, RegisterKnownService) {
324 // It's much easier and safer to use a real GLib for this test.
325 GLib glib;
326 Manager manager(control_interface(),
327 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800328 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700329 &glib,
330 run_path(),
331 storage_path(),
332 string());
333 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
334 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700335 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700336 {
337 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
338 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800339 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700340 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700341 ASSERT_TRUE(profile->AdoptService(service1));
342 ASSERT_TRUE(profile->ContainsService(service1));
343 } // Force destruction of service1.
344
345 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
346 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800347 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700348 &manager));
349 manager.RegisterService(service2);
350 EXPECT_EQ(service2->profile().get(), profile.get());
351 manager.Stop();
352}
353
354TEST_F(ManagerTest, RegisterUnknownService) {
355 // It's much easier and safer to use a real GLib for this test.
356 GLib glib;
357 Manager manager(control_interface(),
358 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800359 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700360 &glib,
361 run_path(),
362 storage_path(),
363 string());
364 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
365 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700366 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700367 {
368 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
369 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800370 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700371 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700372 ASSERT_TRUE(profile->AdoptService(service1));
373 ASSERT_TRUE(profile->ContainsService(service1));
374 } // Force destruction of service1.
375 scoped_refptr<MockService> mock_service2(
376 new NiceMock<MockService>(control_interface(),
377 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800378 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700379 &manager));
380 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
381 .WillRepeatedly(Return(mock_service2->UniqueName()));
382 manager.RegisterService(mock_service2);
383 EXPECT_NE(mock_service2->profile().get(), profile.get());
384 manager.Stop();
385}
386
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000387TEST_F(ManagerTest, DeregisterUnregisteredService) {
388 // WiFi assumes that it can deregister a service that is not
389 // registered. (E.g. a hidden service can be deregistered when it
390 // loses its last endpoint, and again when WiFi is Stop()-ed.)
391 //
392 // So test that doing so doesn't cause a crash.
393 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
394 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800395 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000396 manager());
397 manager()->DeregisterService(service);
398}
399
Chris Masonea8a2c252011-06-27 22:16:30 -0700400TEST_F(ManagerTest, GetProperties) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700401 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700402 AdoptProfile(manager(), profile);
Chris Masonea8a2c252011-06-27 22:16:30 -0700403 map<string, ::DBus::Variant> props;
404 Error error(Error::kInvalidProperty, "");
405 {
406 ::DBus::Error dbus_error;
407 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700408 manager()->mutable_store()->SetStringProperty(
409 flimflam::kCheckPortalListProperty,
410 expected,
411 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700412 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700413 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
414 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
415 expected);
416 }
417 {
418 ::DBus::Error dbus_error;
419 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700420 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
421 expected,
422 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700423 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700424 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
425 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
426 expected);
427 }
428}
429
Chris Masone3c3f6a12011-07-01 10:01:41 -0700430TEST_F(ManagerTest, GetDevicesProperty) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700431 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700432 AdoptProfile(manager(), profile);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800433 manager()->RegisterDevice(mock_devices_[0]);
434 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700435 {
436 map<string, ::DBus::Variant> props;
437 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700438 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700439 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
440 Strings devices =
441 props[flimflam::kDevicesProperty].operator vector<string>();
442 EXPECT_EQ(2, devices.size());
443 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700444}
445
mukesh agrawal2366eed2012-03-20 18:21:50 -0700446TEST_F(ManagerTest, GetServicesProperty) {
447 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
448 AdoptProfile(manager(), profile);
449 map<string, ::DBus::Variant> props;
450 ::DBus::Error dbus_error;
451 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
452 map<string, ::DBus::Variant>::const_iterator prop =
453 props.find(flimflam::kServicesProperty);
454 ASSERT_FALSE(prop == props.end());
455 const ::DBus::Variant &variant = prop->second;
456 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
457}
458
Chris Masone6791a432011-07-12 13:23:19 -0700459TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700460 Manager manager(control_interface(),
461 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800462 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700463 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700464 run_path(),
465 storage_path(),
466 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700467 scoped_refptr<MockService> s2(new MockService(control_interface(),
468 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800469 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700470 &manager));
471 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700472 {
Chris Masone6515aab2011-10-12 16:19:09 -0700473 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700474 ProfileRefPtr profile(
475 new Profile(control_interface(), &manager, id, "", false));
476 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700477 // Say we don't have |s2| the first time asked, then that we do.
478 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
479 .WillOnce(Return(false))
480 .WillRepeatedly(Return(true));
481 EXPECT_CALL(*storage, Flush())
482 .Times(AnyNumber())
483 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700484 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700485 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700486 }
Chris Masone6515aab2011-10-12 16:19:09 -0700487 // Create a profile that already has |s2| in it.
488 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
489 profile->AdoptService(s2);
Chris Masone9d779932011-08-25 16:33:41 -0700490
Chris Masone6515aab2011-10-12 16:19:09 -0700491 // Now, move the Service |s2| to another profile.
492 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
493 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700494
495 // Force destruction of the original Profile, to ensure that the Service
496 // is kept alive and populated with data.
497 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700498 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700499 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700500}
501
Paul Stewart7f61e522012-03-22 11:13:45 -0700502TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
503 scoped_refptr<MockProfile> mock_profile(
504 new MockProfile(control_interface(), manager(), ""));
505 const string kProfileName("profile0");
506 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
507 .WillRepeatedly(Return(kProfileName));
508 AdoptProfile(manager(), mock_profile);
509
510 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
511 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
512 EXPECT_EQ(mock_profile.get(), profile.get());
513}
514
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800515TEST_F(ManagerTest, SetProfileForService) {
516 scoped_refptr<MockProfile> profile0(
517 new MockProfile(control_interface(), manager(), ""));
518 string profile_name0("profile0");
519 EXPECT_CALL(*profile0, GetRpcIdentifier())
520 .WillRepeatedly(Return(profile_name0));
521 AdoptProfile(manager(), profile0);
522 scoped_refptr<MockService> service(new MockService(control_interface(),
523 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800524 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800525 manager()));
526 service->set_profile(profile0);
527
528 {
529 Error error;
530 manager()->SetProfileForService(service, "foo", &error);
531 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700532 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800533 }
534
535 {
536 Error error;
537 manager()->SetProfileForService(service, profile_name0, &error);
538 EXPECT_EQ(Error::kInvalidArguments, error.type());
539 EXPECT_EQ("Service is already connected to this profile", error.message());
540 }
541
542 scoped_refptr<MockProfile> profile1(
543 new MockProfile(control_interface(), manager(), ""));
544 string profile_name1("profile1");
545 EXPECT_CALL(*profile1, GetRpcIdentifier())
546 .WillRepeatedly(Return(profile_name1));
547 AdoptProfile(manager(), profile1);
548
549 {
550 Error error;
551 EXPECT_CALL(*profile1, AdoptService(_))
552 .WillOnce(Return(true));
553 EXPECT_CALL(*profile0, AbandonService(_))
554 .WillOnce(Return(true));
555 manager()->SetProfileForService(service, profile_name1, &error);
556 EXPECT_TRUE(error.IsSuccess());
557 }
558}
559
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700560TEST_F(ManagerTest, CreateProfile) {
561 // It's much easier to use real Glib here since we want the storage
562 // side-effects.
563 GLib glib;
564 ScopedTempDir temp_dir;
565 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
566
567 Manager manager(control_interface(),
568 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800569 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700570 &glib,
571 run_path(),
572 storage_path(),
573 temp_dir.path().value());
574
575 // Invalid name should be rejected.
576 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
577
578 // Valid name is still rejected because we can't create a profile
579 // that doesn't have a user component. Such profile names are
580 // reserved for the single DefaultProfile the manager creates
581 // at startup.
582 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid"));
583
584 // We should succeed in creating a valid user profile.
585 const char kProfile[] = "~user/profile";
586 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile));
587
588 // We should fail in creating it a second time (already exists).
589 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
590}
591
592TEST_F(ManagerTest, PushPopProfile) {
593 // It's much easier to use real Glib in creating a Manager for this
594 // test here since we want the storage side-effects.
595 GLib glib;
596 ScopedTempDir temp_dir;
597 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
598 Manager manager(control_interface(),
599 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800600 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700601 &glib,
602 run_path(),
603 storage_path(),
604 temp_dir.path().value());
605
606 // Pushing an invalid profile should fail.
607 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
608
609 // Pushing a default profile name should fail.
610 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default"));
611
612 const char kProfile0[] = "~user/profile0";
613 const char kProfile1[] = "~user/profile1";
614
615 // Create a couple of profiles.
616 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
617 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
618
619 // Push these profiles on the stack.
620 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
621 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
622
623 // Pushing a profile a second time should fail.
624 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
625 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
626
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800627 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700628 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800629 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700630
631 // Make sure a profile name that doesn't exist fails.
632 const char kProfile2Id[] = "profile2";
633 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
634 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
635
636 // Create a new service, with a specific storage name.
637 scoped_refptr<MockService> service(
638 new NiceMock<MockService>(control_interface(),
639 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800640 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700641 &manager));
642 const char kServiceName[] = "service_storage_name";
643 EXPECT_CALL(*service.get(), GetStorageIdentifier())
644 .WillRepeatedly(Return(kServiceName));
645 EXPECT_CALL(*service.get(), Load(_))
646 .WillRepeatedly(Return(true));
647
648 // Add this service to the manager -- it should end up in the ephemeral
649 // profile.
650 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800651 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700652
653 // Create storage for a profile that contains the service storage name.
654 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
655 kServiceName));
656
657 // When we push the profile, the service should move away from the
658 // ephemeral profile to this new profile since it has an entry for
659 // this service.
660 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800661 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700662 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
663
664 // Insert another profile that should supersede ownership of the service.
665 const char kProfile3Id[] = "profile3";
666 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
667 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
668 kServiceName));
669 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
670 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
671
672 // Popping an invalid profile name should fail.
673 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
674
675 // Popping an profile that is not at the top of the stack should fail.
676 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
677
678 // Popping the top profile should succeed.
679 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
680
681 // Moreover the service should have switched profiles to profile 2.
682 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
683
684 // Popping the top profile should succeed.
685 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
686
687 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800688 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700689
690 // Pop the remaining two services off the stack.
691 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
692 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
693
694 // Next pop should fail with "stack is empty".
695 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
696}
697
Paul Stewart75225512012-01-26 22:51:33 -0800698// Use this matcher instead of passing RefPtrs directly into the arguments
699// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
700// system teardown.
701MATCHER_P(IsRefPtrTo, ref_address, "") {
702 return arg.get() == ref_address;
703}
704
705TEST_F(ManagerTest, HandleProfileEntryDeletion) {
706 MockServiceRefPtr s_not_in_profile(
707 new NiceMock<MockService>(control_interface(),
708 dispatcher(),
709 metrics(),
710 manager()));
711 MockServiceRefPtr s_not_in_group(
712 new NiceMock<MockService>(control_interface(),
713 dispatcher(),
714 metrics(),
715 manager()));
716 MockServiceRefPtr s_configure_fail(
717 new NiceMock<MockService>(control_interface(),
718 dispatcher(),
719 metrics(),
720 manager()));
721 MockServiceRefPtr s_configure_succeed(
722 new NiceMock<MockService>(control_interface(),
723 dispatcher(),
724 metrics(),
725 manager()));
726
727 string entry_name("entry_name");
728 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
729 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
730 .WillRepeatedly(Return("not_entry_name"));
731 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
732 .WillRepeatedly(Return(entry_name));
733 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
734 .WillRepeatedly(Return(entry_name));
735
736 manager()->RegisterService(s_not_in_profile);
737 manager()->RegisterService(s_not_in_group);
738 manager()->RegisterService(s_configure_fail);
739 manager()->RegisterService(s_configure_succeed);
740
741 scoped_refptr<MockProfile> profile0(
742 new StrictMock<MockProfile>(control_interface(), manager(), ""));
743 scoped_refptr<MockProfile> profile1(
744 new StrictMock<MockProfile>(control_interface(), manager(), ""));
745
746 s_not_in_group->set_profile(profile1);
747 s_configure_fail->set_profile(profile1);
748 s_configure_succeed->set_profile(profile1);
749
750 AdoptProfile(manager(), profile0);
751 AdoptProfile(manager(), profile1);
752
753 // No services are a member of this profile.
754 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
755
756 // No services that are members of this profile have this entry name.
757 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
758
759 // Only services that are members of the profile and group will be abandoned.
760 EXPECT_CALL(*profile1.get(),
761 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
762 EXPECT_CALL(*profile1.get(),
763 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
764 EXPECT_CALL(*profile1.get(),
765 AbandonService(IsRefPtrTo(s_configure_fail.get())))
766 .WillOnce(Return(true));
767 EXPECT_CALL(*profile1.get(),
768 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
769 .WillOnce(Return(true));
770
771 // Never allow services to re-join profile1.
772 EXPECT_CALL(*profile1.get(), ConfigureService(_))
773 .WillRepeatedly(Return(false));
774
775 // Only allow one of the members of the profile and group to successfully
776 // join profile0.
777 EXPECT_CALL(*profile0.get(),
778 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
779 EXPECT_CALL(*profile0.get(),
780 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
781 EXPECT_CALL(*profile0.get(),
782 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
783 .WillOnce(Return(false));
784 EXPECT_CALL(*profile0.get(),
785 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
786 .WillOnce(Return(true));
787
788 // Expect the failed-to-configure service to have Unload() called on it.
789 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
790 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
791 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
792 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
793
794 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
795
796 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
797 EXPECT_EQ(profile1, s_not_in_group->profile());
798 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
799
800 // Since we are using a MockProfile, the profile does not actually change,
801 // since ConfigureService was not actually called on the service.
802 EXPECT_EQ(profile1, s_configure_succeed->profile());
803}
804
Paul Stewart65512e12012-03-26 18:01:08 -0700805TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
806 MockServiceRefPtr s_will_remove0(
807 new NiceMock<MockService>(control_interface(),
808 dispatcher(),
809 metrics(),
810 manager()));
811 MockServiceRefPtr s_will_remove1(
812 new NiceMock<MockService>(control_interface(),
813 dispatcher(),
814 metrics(),
815 manager()));
816 MockServiceRefPtr s_will_not_remove0(
817 new NiceMock<MockService>(control_interface(),
818 dispatcher(),
819 metrics(),
820 manager()));
821 MockServiceRefPtr s_will_not_remove1(
822 new NiceMock<MockService>(control_interface(),
823 dispatcher(),
824 metrics(),
825 manager()));
826
827 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
828 .Times(4); // Once for each registration.
829
830 string entry_name("entry_name");
831 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
832 .WillRepeatedly(Return(entry_name));
833 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
834 .WillRepeatedly(Return(entry_name));
835 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
836 .WillRepeatedly(Return(entry_name));
837 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
838 .WillRepeatedly(Return(entry_name));
839
840 manager()->RegisterService(s_will_remove0);
841 manager()->RegisterService(s_will_not_remove0);
842 manager()->RegisterService(s_will_remove1);
843 manager()->RegisterService(s_will_not_remove1);
844
845 // One for each service added above.
846 ASSERT_EQ(4, manager()->services_.size());
847
848 scoped_refptr<MockProfile> profile(
849 new StrictMock<MockProfile>(control_interface(), manager(), ""));
850
851 s_will_remove0->set_profile(profile);
852 s_will_remove1->set_profile(profile);
853 s_will_not_remove0->set_profile(profile);
854 s_will_not_remove1->set_profile(profile);
855
856 AdoptProfile(manager(), profile);
857
858 // Deny any of the services re-entry to the profile.
859 EXPECT_CALL(*profile, ConfigureService(_))
860 .WillRepeatedly(Return(false));
861
862 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
863 .WillOnce(Return(true));
864 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
865 .WillOnce(Return(true));
866 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
867 .WillOnce(Return(true));
868 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
869 .WillOnce(Return(true));
870
871 EXPECT_CALL(*s_will_remove0, Unload())
872 .WillOnce(Return(true));
873 EXPECT_CALL(*s_will_remove1, Unload())
874 .WillOnce(Return(true));
875 EXPECT_CALL(*s_will_not_remove0, Unload())
876 .WillOnce(Return(false));
877 EXPECT_CALL(*s_will_not_remove1, Unload())
878 .WillOnce(Return(false));
879
880
881 // This will cause all the profiles to be unloaded.
882 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
883
884 // 2 of the 4 services added above should have been unregistered and
885 // removed, leaving 2.
886 EXPECT_EQ(2, manager()->services_.size());
887 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
888 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
889}
890
891TEST_F(ManagerTest, PopProfileWithUnload) {
892 MockServiceRefPtr s_will_remove0(
893 new NiceMock<MockService>(control_interface(),
894 dispatcher(),
895 metrics(),
896 manager()));
897 MockServiceRefPtr s_will_remove1(
898 new NiceMock<MockService>(control_interface(),
899 dispatcher(),
900 metrics(),
901 manager()));
902 MockServiceRefPtr s_will_not_remove0(
903 new NiceMock<MockService>(control_interface(),
904 dispatcher(),
905 metrics(),
906 manager()));
907 MockServiceRefPtr s_will_not_remove1(
908 new NiceMock<MockService>(control_interface(),
909 dispatcher(),
910 metrics(),
911 manager()));
912
913 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
914 .Times(5); // Once for each registration, and one after profile pop.
915
916 manager()->RegisterService(s_will_remove0);
917 manager()->RegisterService(s_will_not_remove0);
918 manager()->RegisterService(s_will_remove1);
919 manager()->RegisterService(s_will_not_remove1);
920
921 // One for each service added above.
922 ASSERT_EQ(4, manager()->services_.size());
923
924 scoped_refptr<MockProfile> profile0(
925 new StrictMock<MockProfile>(control_interface(), manager(), ""));
926 scoped_refptr<MockProfile> profile1(
927 new StrictMock<MockProfile>(control_interface(), manager(), ""));
928
929 s_will_remove0->set_profile(profile1);
930 s_will_remove1->set_profile(profile1);
931 s_will_not_remove0->set_profile(profile1);
932 s_will_not_remove1->set_profile(profile1);
933
934 AdoptProfile(manager(), profile0);
935 AdoptProfile(manager(), profile1);
936
937 // Deny any of the services entry to profile0, so they will all be unloaded.
938 EXPECT_CALL(*profile0, ConfigureService(_))
939 .WillRepeatedly(Return(false));
940
941 EXPECT_CALL(*s_will_remove0, Unload())
942 .WillOnce(Return(true));
943 EXPECT_CALL(*s_will_remove1, Unload())
944 .WillOnce(Return(true));
945 EXPECT_CALL(*s_will_not_remove0, Unload())
946 .WillOnce(Return(false));
947 EXPECT_CALL(*s_will_not_remove1, Unload())
948 .WillOnce(Return(false));
949
950 // This will pop profile1, which should cause all our profiles to unload.
951 manager()->PopProfileInternal();
952
953 // 2 of the 4 services added above should have been unregistered and
954 // removed, leaving 2.
955 EXPECT_EQ(2, manager()->services_.size());
956 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
957 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
958}
959
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800960TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700961 {
962 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800963 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
964 flimflam::kOfflineModeProperty,
965 PropertyStoreTest::kBoolV,
966 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700967 }
968 {
969 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800970 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
971 flimflam::kCountryProperty,
972 PropertyStoreTest::kStringV,
973 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -0700974 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700975 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700976 {
977 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800978 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
979 flimflam::kCountryProperty,
980 PropertyStoreTest::kBoolV,
981 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700982 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700983 }
984 {
985 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800986 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
987 flimflam::kOfflineModeProperty,
988 PropertyStoreTest::kStringV,
989 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700990 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700991 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700992 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700993 {
994 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800995 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700996 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700997 flimflam::kEnabledTechnologiesProperty,
998 PropertyStoreTest::kStringsV,
999 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001000 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001001 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001002}
1003
mukesh agrawal32399322011-09-01 10:53:43 -07001004TEST_F(ManagerTest, RequestScan) {
1005 {
1006 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001007 manager()->RegisterDevice(mock_devices_[0].get());
1008 manager()->RegisterDevice(mock_devices_[1].get());
1009 EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -07001010 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001011 EXPECT_CALL(*mock_devices_[0], Scan(_));
1012 EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -07001013 .WillRepeatedly(Return(false));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001014 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001015 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001016 }
1017
1018 {
1019 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001020 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001021 EXPECT_EQ(Error::kInvalidArguments, error.type());
1022 }
1023}
1024
Darin Petkovb65c2452012-02-23 15:17:06 +01001025TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001026 KeyValueStore args;
1027 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001028 manager()->GetService(args, &e);
1029 EXPECT_EQ(Error::kInvalidArguments, e.type());
1030 EXPECT_EQ("must specify service type", e.message());
1031}
1032
1033TEST_F(ManagerTest, GetServiceUnknownType) {
1034 KeyValueStore args;
1035 Error e;
1036 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1037 manager()->GetService(args, &e);
1038 EXPECT_EQ(Error::kNotSupported, e.type());
1039 EXPECT_EQ("service type is unsupported", e.message());
1040}
1041
1042TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1043 KeyValueStore args;
1044 Error e;
1045 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1046 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001047 EXPECT_EQ(Error::kInvalidArguments, e.type());
1048 EXPECT_EQ("no wifi devices available", e.message());
1049}
1050
Darin Petkovb65c2452012-02-23 15:17:06 +01001051TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001052 KeyValueStore args;
1053 Error e;
1054 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001055 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001056 manager()->RegisterDevice(mock_wifi_);
1057 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1058 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001059 manager()->GetService(args, &e);
1060 EXPECT_TRUE(e.IsSuccess());
1061}
1062
Darin Petkov33af05c2012-02-28 10:10:30 +01001063TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1064 KeyValueStore args;
1065 Error e;
1066 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
1067 ServiceRefPtr service = manager()->GetService(args, &e);
1068 EXPECT_EQ(Error::kNotSupported, e.type());
1069 EXPECT_FALSE(service);
1070}
1071
Darin Petkovb65c2452012-02-23 15:17:06 +01001072TEST_F(ManagerTest, GetServiceVPN) {
1073 KeyValueStore args;
1074 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001075 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001076 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001077 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1078 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Darin Petkov33af05c2012-02-28 10:10:30 +01001079 ServiceRefPtr service = manager()->GetService(args, &e);
1080 EXPECT_TRUE(e.IsSuccess());
1081 EXPECT_TRUE(service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001082}
1083
Paul Stewart7f61e522012-03-22 11:13:45 -07001084TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1085 // Manager calls ActiveProfile() so we need at least one profile installed.
1086 scoped_refptr<MockProfile> profile(
1087 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1088 AdoptProfile(manager(), profile);
1089
1090 KeyValueStore args;
1091 args.SetString(flimflam::kProfileProperty, "xxx");
1092 Error error;
1093 manager()->ConfigureService(args, &error);
1094 EXPECT_EQ(Error::kInvalidArguments, error.type());
1095 EXPECT_EQ("Invalid profile name xxx", error.message());
1096}
1097
1098TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1099 // Manager calls ActiveProfile() so we need at least one profile installed.
1100 scoped_refptr<MockProfile> profile(
1101 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1102 AdoptProfile(manager(), profile);
1103
1104 KeyValueStore args;
1105 Error error;
1106 manager()->ConfigureService(args, &error);
1107 EXPECT_EQ(Error::kInvalidArguments, error.type());
1108 EXPECT_EQ("must specify service type", error.message());
1109}
1110
1111// A registered service in the ephemeral profile should be moved to the
1112// active profile as a part of configuration if no profile was explicitly
1113// specified.
1114TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1115 scoped_refptr<MockProfile> profile(
1116 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1117
1118 AdoptProfile(manager(), profile); // This is now the active profile.
1119
1120 const std::vector<uint8_t> ssid;
1121 scoped_refptr<MockWiFiService> service(
1122 new NiceMock<MockWiFiService>(control_interface(),
1123 dispatcher(),
1124 metrics(),
1125 manager(),
1126 mock_wifi_,
1127 ssid,
1128 "",
1129 "",
1130 false));
1131
1132 manager()->RegisterService(service);
1133 service->set_profile(GetEphemeralProfile(manager()));
1134
1135 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1136 // the same device as that used above causes a refcounting loop.
1137 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1138 dispatcher(),
1139 metrics(),
1140 manager(),
1141 "wifi1",
1142 "addr5",
1143 5));
1144 manager()->RegisterDevice(wifi);
1145 EXPECT_CALL(*wifi, GetService(_, _))
1146 .WillOnce(Return(service));
1147 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1148 .WillOnce(Return(true));
1149 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1150 .WillOnce(Return(true));
1151
1152 KeyValueStore args;
1153 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1154 Error error;
1155 manager()->ConfigureService(args, &error);
1156 EXPECT_TRUE(error.IsSuccess());
1157}
1158
1159// If were configure a service that was already registered and explicitly
1160// specify a profile, it should be moved from the profile it was previously
1161// in to the specified profile if one was requested.
1162TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1163 scoped_refptr<MockProfile> profile0(
1164 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1165 scoped_refptr<MockProfile> profile1(
1166 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1167
1168 const string kProfileName0 = "profile0";
1169 const string kProfileName1 = "profile1";
1170
1171 EXPECT_CALL(*profile0, GetRpcIdentifier())
1172 .WillRepeatedly(Return(kProfileName0));
1173 EXPECT_CALL(*profile1, GetRpcIdentifier())
1174 .WillRepeatedly(Return(kProfileName1));
1175
1176 AdoptProfile(manager(), profile0);
1177 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1178
1179 const std::vector<uint8_t> ssid;
1180 scoped_refptr<MockWiFiService> service(
1181 new NiceMock<MockWiFiService>(control_interface(),
1182 dispatcher(),
1183 metrics(),
1184 manager(),
1185 mock_wifi_,
1186 ssid,
1187 "",
1188 "",
1189 false));
1190
1191 manager()->RegisterService(service);
1192 service->set_profile(profile1);
1193
1194 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1195 // the same device as that used above causes a refcounting loop.
1196 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1197 dispatcher(),
1198 metrics(),
1199 manager(),
1200 "wifi1",
1201 "addr5",
1202 5));
1203 manager()->RegisterDevice(wifi);
1204 EXPECT_CALL(*wifi, GetService(_, _))
1205 .WillOnce(Return(service));
1206 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1207 .WillOnce(Return(true));
1208 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1209 .WillOnce(Return(true));
1210 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1211 .WillOnce(Return(true));
1212
1213 KeyValueStore args;
1214 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1215 args.SetString(flimflam::kProfileProperty, kProfileName0);
1216 Error error;
1217 manager()->ConfigureService(args, &error);
1218 EXPECT_TRUE(error.IsSuccess());
1219 service->set_profile(NULL); // Breaks refcounting loop.
1220}
1221
1222// An unregistered service should remain unregistered, but its contents should
1223// be saved to the specified profile nonetheless.
1224TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1225 scoped_refptr<MockProfile> profile0(
1226 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1227 scoped_refptr<MockProfile> profile1(
1228 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1229
1230 const string kProfileName0 = "profile0";
1231 const string kProfileName1 = "profile1";
1232
1233 EXPECT_CALL(*profile0, GetRpcIdentifier())
1234 .WillRepeatedly(Return(kProfileName0));
1235 EXPECT_CALL(*profile1, GetRpcIdentifier())
1236 .WillRepeatedly(Return(kProfileName1));
1237
1238 AdoptProfile(manager(), profile0);
1239 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1240
1241 const std::vector<uint8_t> ssid;
1242 scoped_refptr<MockWiFiService> service(
1243 new NiceMock<MockWiFiService>(control_interface(),
1244 dispatcher(),
1245 metrics(),
1246 manager(),
1247 mock_wifi_,
1248 ssid,
1249 "",
1250 "",
1251 false));
1252
1253 service->set_profile(profile1);
1254
1255 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1256 // the same device as that used above causes a refcounting loop.
1257 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1258 dispatcher(),
1259 metrics(),
1260 manager(),
1261 "wifi1",
1262 "addr5",
1263 5));
1264 manager()->RegisterDevice(wifi);
1265 EXPECT_CALL(*wifi, GetService(_, _))
1266 .WillOnce(Return(service));
1267 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1268 .WillOnce(Return(true));
1269 EXPECT_CALL(*profile0, AdoptService(_))
1270 .Times(0);
1271 EXPECT_CALL(*profile1, AdoptService(_))
1272 .Times(0);
1273
1274 KeyValueStore args;
1275 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1276 args.SetString(flimflam::kProfileProperty, kProfileName0);
1277 Error error;
1278 manager()->ConfigureService(args, &error);
1279 EXPECT_TRUE(error.IsSuccess());
1280}
1281
Paul Stewart22aa71b2011-09-16 12:15:11 -07001282TEST_F(ManagerTest, TechnologyOrder) {
1283 Error error;
1284 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1285 string(flimflam::kTypeWifi), &error);
1286 ASSERT_TRUE(error.IsSuccess());
1287 EXPECT_EQ(manager()->GetTechnologyOrder(),
1288 string(flimflam::kTypeEthernet) + "," +
1289 string(flimflam::kTypeWifi));
1290
1291 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1292 string(flimflam::kTypeWifi), &error);
1293 ASSERT_FALSE(error.IsSuccess());
1294 EXPECT_EQ(Error::kInvalidArguments, error.type());
1295 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1296 string(flimflam::kTypeWifi),
1297 manager()->GetTechnologyOrder());
1298}
1299
1300TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001301 // TODO(quiche): Some of these tests would probably fit better in
1302 // service_unittest, since the actual comparison of Services is
1303 // implemented in Service. (crosbug.com/23370)
1304
Paul Stewart22aa71b2011-09-16 12:15:11 -07001305 scoped_refptr<MockService> mock_service0(
1306 new NiceMock<MockService>(control_interface(),
1307 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001308 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001309 manager()));
1310 scoped_refptr<MockService> mock_service1(
1311 new NiceMock<MockService>(control_interface(),
1312 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001313 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001314 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001315
1316 manager()->RegisterService(mock_service0);
1317 manager()->RegisterService(mock_service1);
1318
1319 // Services should already be sorted by UniqueName
1320 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1321
1322 // Asking explictly to sort services should not change anything
1323 manager()->SortServices();
1324 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1325
1326 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001327 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001328 manager()->UpdateService(mock_service1);
1329 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1330
1331 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001332 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001333 manager()->UpdateService(mock_service0);
1334 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1335
1336 // Technology
1337 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi))
1338 .WillRepeatedly(Return(true));
1339 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet))
1340 .WillRepeatedly(Return(true));
1341 // NB: Redefine default (false) return values so we don't use the default rule
1342 // which makes the logs noisier
1343 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi)))
1344 .WillRepeatedly(Return(false));
1345 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet)))
1346 .WillRepeatedly(Return(false));
1347
1348 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001349 // Default technology ordering should favor Ethernet over WiFi.
1350 manager()->SortServices();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001351 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1352
1353 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1354 string(flimflam::kTypeEthernet), &error);
1355 EXPECT_TRUE(error.IsSuccess());
1356 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1357
Gaurav Shah435de2c2011-11-17 19:01:07 -08001358 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001359 mock_service0->set_priority(1);
1360 manager()->UpdateService(mock_service0);
1361 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1362
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001363 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001364 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001365 manager()->UpdateService(mock_service1);
1366 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1367
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001368 // Auto-connect.
1369 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001370 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001371 mock_service1->set_auto_connect(false);
1372 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001373 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1374
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001375 // Connectable.
1376 mock_service1->set_connectable(true);
1377 manager()->UpdateService(mock_service1);
1378 mock_service0->set_connectable(false);
1379 manager()->UpdateService(mock_service0);
1380 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1381
1382 // IsFailed.
1383 EXPECT_CALL(*mock_service0.get(), state())
1384 .WillRepeatedly(Return(Service::kStateIdle));
1385 EXPECT_CALL(*mock_service0.get(), IsFailed())
1386 .WillRepeatedly(Return(false));
1387 manager()->UpdateService(mock_service0);
1388 EXPECT_CALL(*mock_service0.get(), state())
1389 .WillRepeatedly(Return(Service::kStateFailure));
1390 EXPECT_CALL(*mock_service1.get(), IsFailed())
1391 .WillRepeatedly(Return(true));
1392 manager()->UpdateService(mock_service1);
1393 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1394
1395 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001396 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001397 .WillRepeatedly(Return(Service::kStateAssociating));
1398 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001399 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001400 manager()->UpdateService(mock_service1);
1401 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1402
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001403 // Connected.
1404 EXPECT_CALL(*mock_service0.get(), state())
1405 .WillRepeatedly(Return(Service::kStateConnected));
1406 EXPECT_CALL(*mock_service0.get(), IsConnected())
1407 .WillRepeatedly(Return(true));
1408 manager()->UpdateService(mock_service0);
1409 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1410
Paul Stewart22aa71b2011-09-16 12:15:11 -07001411 manager()->DeregisterService(mock_service0);
1412 manager()->DeregisterService(mock_service1);
1413}
1414
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001415TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001416 MockMetrics mock_metrics;
1417 manager()->set_metrics(&mock_metrics);
1418
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001419 scoped_refptr<MockService> mock_service0(
1420 new NiceMock<MockService>(control_interface(),
1421 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001422 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001423 manager()));
1424 scoped_refptr<MockService> mock_service1(
1425 new NiceMock<MockService>(control_interface(),
1426 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001427 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001428 manager()));
1429
1430 scoped_refptr<MockConnection> mock_connection0(
1431 new NiceMock<MockConnection>(device_info_.get()));
1432 scoped_refptr<MockConnection> mock_connection1(
1433 new NiceMock<MockConnection>(device_info_.get()));
1434
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001435 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001436 manager()->RegisterService(mock_service0);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001437 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001438 manager()->RegisterService(mock_service1);
1439
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001440 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1441 manager()->SortServices();
1442
1443 mock_service1->set_priority(1);
1444 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1445 manager()->SortServices();
1446
1447 mock_service1->set_priority(0);
1448 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1449 manager()->SortServices();
1450
Paul Stewartce4ec192012-03-14 12:53:46 -07001451 mock_service0->set_mock_connection(mock_connection0);
1452 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001453
1454 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001455 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001456 manager()->SortServices();
1457
1458 mock_service1->set_priority(1);
1459 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1460 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001461 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001462 manager()->SortServices();
1463
1464 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001465 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001466 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001467 manager()->DeregisterService(mock_service1);
1468
Paul Stewartce4ec192012-03-14 12:53:46 -07001469 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001470 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001471 manager()->DeregisterService(mock_service0);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001472
1473 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1474 manager()->SortServices();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001475}
1476
Gaurav Shah435de2c2011-11-17 19:01:07 -08001477TEST_F(ManagerTest, AvailableTechnologies) {
1478 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1479 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001480 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001481 manager(),
1482 "null4",
1483 "addr4",
1484 0));
1485 manager()->RegisterDevice(mock_devices_[0]);
1486 manager()->RegisterDevice(mock_devices_[1]);
1487 manager()->RegisterDevice(mock_devices_[2]);
1488 manager()->RegisterDevice(mock_devices_[3]);
1489
1490 ON_CALL(*mock_devices_[0].get(), technology())
1491 .WillByDefault(Return(Technology::kEthernet));
1492 ON_CALL(*mock_devices_[1].get(), technology())
1493 .WillByDefault(Return(Technology::kWifi));
1494 ON_CALL(*mock_devices_[2].get(), technology())
1495 .WillByDefault(Return(Technology::kCellular));
1496 ON_CALL(*mock_devices_[3].get(), technology())
1497 .WillByDefault(Return(Technology::kWifi));
1498
1499 set<string> expected_technologies;
1500 expected_technologies.insert(Technology::NameFromIdentifier(
1501 Technology::kEthernet));
1502 expected_technologies.insert(Technology::NameFromIdentifier(
1503 Technology::kWifi));
1504 expected_technologies.insert(Technology::NameFromIdentifier(
1505 Technology::kCellular));
1506 Error error;
1507 vector<string> technologies = manager()->AvailableTechnologies(&error);
1508
1509 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1510 ContainerEq(expected_technologies));
1511}
1512
1513TEST_F(ManagerTest, ConnectedTechnologies) {
1514 scoped_refptr<MockService> connected_service1(
1515 new NiceMock<MockService>(control_interface(),
1516 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001517 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001518 manager()));
1519 scoped_refptr<MockService> connected_service2(
1520 new NiceMock<MockService>(control_interface(),
1521 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001522 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001523 manager()));
1524 scoped_refptr<MockService> disconnected_service1(
1525 new NiceMock<MockService>(control_interface(),
1526 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001527 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001528 manager()));
1529 scoped_refptr<MockService> disconnected_service2(
1530 new NiceMock<MockService>(control_interface(),
1531 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001532 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001533 manager()));
1534
1535 ON_CALL(*connected_service1.get(), IsConnected())
1536 .WillByDefault(Return(true));
1537 ON_CALL(*connected_service2.get(), IsConnected())
1538 .WillByDefault(Return(true));
1539
1540 manager()->RegisterService(connected_service1);
1541 manager()->RegisterService(connected_service2);
1542 manager()->RegisterService(disconnected_service1);
1543 manager()->RegisterService(disconnected_service2);
1544
1545 manager()->RegisterDevice(mock_devices_[0]);
1546 manager()->RegisterDevice(mock_devices_[1]);
1547 manager()->RegisterDevice(mock_devices_[2]);
1548 manager()->RegisterDevice(mock_devices_[3]);
1549
1550 ON_CALL(*mock_devices_[0].get(), technology())
1551 .WillByDefault(Return(Technology::kEthernet));
1552 ON_CALL(*mock_devices_[1].get(), technology())
1553 .WillByDefault(Return(Technology::kWifi));
1554 ON_CALL(*mock_devices_[2].get(), technology())
1555 .WillByDefault(Return(Technology::kCellular));
1556 ON_CALL(*mock_devices_[3].get(), technology())
1557 .WillByDefault(Return(Technology::kWifi));
1558
1559 mock_devices_[0]->SelectService(connected_service1);
1560 mock_devices_[1]->SelectService(disconnected_service1);
1561 mock_devices_[2]->SelectService(disconnected_service2);
1562 mock_devices_[3]->SelectService(connected_service2);
1563
1564 set<string> expected_technologies;
1565 expected_technologies.insert(Technology::NameFromIdentifier(
1566 Technology::kEthernet));
1567 expected_technologies.insert(Technology::NameFromIdentifier(
1568 Technology::kWifi));
1569 Error error;
1570
1571 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1572 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1573 ContainerEq(expected_technologies));
1574}
1575
1576TEST_F(ManagerTest, DefaultTechnology) {
1577 scoped_refptr<MockService> connected_service(
1578 new NiceMock<MockService>(control_interface(),
1579 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001580 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001581 manager()));
1582 scoped_refptr<MockService> disconnected_service(
1583 new NiceMock<MockService>(control_interface(),
1584 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001585 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001586 manager()));
1587
1588 // Connected. WiFi.
1589 ON_CALL(*connected_service.get(), IsConnected())
1590 .WillByDefault(Return(true));
1591 ON_CALL(*connected_service.get(), state())
1592 .WillByDefault(Return(Service::kStateConnected));
1593 ON_CALL(*connected_service.get(), technology())
1594 .WillByDefault(Return(Technology::kWifi));
1595
1596 // Disconnected. Ethernet.
1597 ON_CALL(*disconnected_service.get(), technology())
1598 .WillByDefault(Return(Technology::kEthernet));
1599
1600 manager()->RegisterService(disconnected_service);
1601 Error error;
1602 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1603
1604
1605 manager()->RegisterService(connected_service);
1606 // Connected service should be brought to the front now.
1607 string expected_technology =
1608 Technology::NameFromIdentifier(Technology::kWifi);
1609 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1610}
1611
Thieu Le1271d682011-11-02 22:48:19 +00001612TEST_F(ManagerTest, DisconnectServicesOnStop) {
1613 scoped_refptr<MockService> mock_service(
1614 new NiceMock<MockService>(control_interface(),
1615 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001616 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001617 manager()));
1618 manager()->RegisterService(mock_service);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001619 EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001620 manager()->Stop();
1621}
1622
mukesh agrawal00917ce2011-11-22 23:56:55 +00001623TEST_F(ManagerTest, UpdateServiceConnected) {
1624 scoped_refptr<MockService> mock_service(
1625 new NiceMock<MockService>(control_interface(),
1626 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001627 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001628 manager()));
1629 manager()->RegisterService(mock_service);
1630 EXPECT_FALSE(mock_service->favorite());
1631 EXPECT_FALSE(mock_service->auto_connect());
1632
Gaurav Shah435de2c2011-11-17 19:01:07 -08001633 EXPECT_CALL(*mock_service.get(), IsConnected())
1634 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001635 manager()->UpdateService(mock_service);
1636 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1637 // to mock out MakeFavorite. And mocking that out would break the
1638 // SortServices test. (crosbug.com/23370)
1639 EXPECT_TRUE(mock_service->favorite());
1640 EXPECT_TRUE(mock_service->auto_connect());
1641}
1642
Thieu Led4e9e552012-02-16 16:26:07 -08001643TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
1644 // This tests the case where the user connects to a service that is
1645 // currently associated with a profile. We want to make sure that the
1646 // favorite flag is set and that the flag is saved to the current
1647 // profile.
1648 scoped_refptr<MockService> mock_service(
1649 new NiceMock<MockService>(control_interface(),
1650 dispatcher(),
1651 metrics(),
1652 manager()));
1653 manager()->RegisterService(mock_service);
1654 EXPECT_FALSE(mock_service->favorite());
1655 EXPECT_FALSE(mock_service->auto_connect());
1656
1657 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
1658 mock_service->set_profile(profile);
1659
1660 EXPECT_CALL(*mock_service.get(), IsConnected())
1661 .WillRepeatedly(Return(true));
1662 EXPECT_CALL(*mock_service.get(), SaveToCurrentProfile());
1663 manager()->UpdateService(mock_service);
1664 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1665 // to mock out MakeFavorite. And mocking that out would break the
1666 // SortServices test. (crosbug.com/23370)
1667 EXPECT_TRUE(mock_service->favorite());
1668 EXPECT_TRUE(mock_service->auto_connect());
1669}
1670
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001671TEST_F(ManagerTest, SaveSuccessfulService) {
1672 scoped_refptr<MockProfile> profile(
1673 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1674 AdoptProfile(manager(), profile);
1675 scoped_refptr<MockService> service(
1676 new NiceMock<MockService>(control_interface(),
1677 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001678 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001679 manager()));
1680
1681 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
1682 ServiceRefPtr expect_service(service.get());
1683
1684 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
1685 .WillOnce(Return(false));
1686 manager()->RegisterService(service);
1687
1688 EXPECT_CALL(*service.get(), state())
1689 .WillRepeatedly(Return(Service::kStateConnected));
1690 EXPECT_CALL(*service.get(), IsConnected())
1691 .WillRepeatedly(Return(true));
1692 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
1693 .WillOnce(Return(true));
1694 manager()->UpdateService(service);
1695}
1696
Paul Stewart1b253142012-01-26 14:05:52 -08001697TEST_F(ManagerTest, EnumerateProfiles) {
1698 vector<string> profile_paths;
1699 for (size_t i = 0; i < 10; i++) {
1700 scoped_refptr<MockProfile> profile(
1701 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05001702 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08001703 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
1704 .WillOnce(Return(profile_paths.back()));
1705 AdoptProfile(manager(), profile);
1706 }
1707
1708 Error error;
1709 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
1710 EXPECT_TRUE(error.IsSuccess());
1711 EXPECT_EQ(profile_paths.size(), returned_paths.size());
1712 for (size_t i = 0; i < profile_paths.size(); i++) {
1713 EXPECT_EQ(profile_paths[i], returned_paths[i]);
1714 }
1715}
1716
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001717TEST_F(ManagerTest, AutoConnectOnRegister) {
1718 MockServiceRefPtr service = MakeAutoConnectableService();
1719 EXPECT_CALL(*service.get(), AutoConnect());
1720 manager()->RegisterService(service);
1721 dispatcher()->DispatchPendingEvents();
1722}
1723
1724TEST_F(ManagerTest, AutoConnectOnUpdate) {
1725 MockServiceRefPtr service1 = MakeAutoConnectableService();
1726 service1->set_priority(1);
1727 MockServiceRefPtr service2 = MakeAutoConnectableService();
1728 service2->set_priority(2);
1729 manager()->RegisterService(service1);
1730 manager()->RegisterService(service2);
1731 dispatcher()->DispatchPendingEvents();
1732
1733 EXPECT_CALL(*service1.get(), AutoConnect());
1734 EXPECT_CALL(*service2.get(), state())
1735 .WillRepeatedly(Return(Service::kStateFailure));
1736 EXPECT_CALL(*service2.get(), IsFailed())
1737 .WillRepeatedly(Return(true));
1738 EXPECT_CALL(*service2.get(), IsConnected())
1739 .WillRepeatedly(Return(false));
1740 manager()->UpdateService(service2);
1741 dispatcher()->DispatchPendingEvents();
1742}
1743
1744TEST_F(ManagerTest, AutoConnectOnDeregister) {
1745 MockServiceRefPtr service1 = MakeAutoConnectableService();
1746 service1->set_priority(1);
1747 MockServiceRefPtr service2 = MakeAutoConnectableService();
1748 service2->set_priority(2);
1749 manager()->RegisterService(service1);
1750 manager()->RegisterService(service2);
1751 dispatcher()->DispatchPendingEvents();
1752
1753 EXPECT_CALL(*service1.get(), AutoConnect());
1754 manager()->DeregisterService(service2);
1755 dispatcher()->DispatchPendingEvents();
1756}
1757
Paul Stewartc681fa02012-03-02 19:40:04 -08001758TEST_F(ManagerTest, RecheckPortal) {
1759 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
1760 .WillOnce(Return(false));
1761 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
1762 .WillOnce(Return(true));
1763 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
1764 .Times(0);
1765
1766 manager()->RegisterDevice(mock_devices_[0]);
1767 manager()->RegisterDevice(mock_devices_[1]);
1768 manager()->RegisterDevice(mock_devices_[2]);
1769
1770 manager()->RecheckPortal(NULL);
1771}
1772
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001773TEST_F(ManagerTest, GetDefaultService) {
1774 EXPECT_FALSE(manager()->GetDefaultService().get());
1775
1776 scoped_refptr<MockService> mock_service(
1777 new NiceMock<MockService>(control_interface(),
1778 dispatcher(),
1779 metrics(),
1780 manager()));
1781
1782 manager()->RegisterService(mock_service);
1783 EXPECT_FALSE(manager()->GetDefaultService().get());
1784
1785 scoped_refptr<MockConnection> mock_connection(
1786 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001787 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001788 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
1789
Paul Stewartce4ec192012-03-14 12:53:46 -07001790 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001791 manager()->DeregisterService(mock_service);
1792}
1793
Paul Stewart13ed2252012-03-21 12:52:46 -07001794TEST_F(ManagerTest, GetServiceWithGUID) {
1795 scoped_refptr<MockService> mock_service0(
1796 new NiceMock<MockService>(control_interface(),
1797 dispatcher(),
1798 metrics(),
1799 manager()));
1800
1801 scoped_refptr<MockService> mock_service1(
1802 new NiceMock<MockService>(control_interface(),
1803 dispatcher(),
1804 metrics(),
1805 manager()));
1806
Paul Stewartcb59fed2012-03-21 21:14:46 -07001807 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
1808 .Times(0);
1809 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
1810 .Times(0);
1811
Paul Stewart13ed2252012-03-21 12:52:46 -07001812 manager()->RegisterService(mock_service0);
1813 manager()->RegisterService(mock_service1);
1814
1815 const string kGUID0 = "GUID0";
1816 const string kGUID1 = "GUID1";
1817
1818 {
1819 Error error;
1820 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
1821 EXPECT_FALSE(error.IsSuccess());
1822 EXPECT_FALSE(service);
1823 }
1824
1825 KeyValueStore args;
1826 args.SetString(flimflam::kGuidProperty, kGUID1);
1827
1828 {
1829 Error error;
1830 ServiceRefPtr service = manager()->GetService(args, &error);
1831 EXPECT_EQ(Error::kInvalidArguments, error.type());
1832 EXPECT_FALSE(service);
1833 }
1834
1835 mock_service0->set_guid(kGUID0);
1836 mock_service1->set_guid(kGUID1);
1837
1838 {
1839 Error error;
1840 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
1841 EXPECT_TRUE(error.IsSuccess());
1842 EXPECT_EQ(mock_service0.get(), service.get());
1843 }
1844
1845 {
1846 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07001847 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
1848 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07001849 ServiceRefPtr service = manager()->GetService(args, &error);
1850 EXPECT_TRUE(error.IsSuccess());
1851 EXPECT_EQ(mock_service1.get(), service.get());
1852 }
1853
1854 manager()->DeregisterService(mock_service0);
1855 manager()->DeregisterService(mock_service1);
1856}
1857
Chris Masone9be4a9d2011-05-16 15:44:09 -07001858} // namespace shill