blob: dd3faafe1276e6054b2fec84f59bc7c9641f8f12 [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
Paul Stewarte73d05c2012-03-29 16:26:05 -070011#include <base/file_util.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070012#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/scoped_temp_dir.h>
14#include <base/stl_util.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070015#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070017#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070018#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070019
mukesh agrawal32399322011-09-01 10:53:43 -070020#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070021#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070022#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070023#include "shill/glib.h"
24#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070025#include "shill/key_value_store.h"
mukesh agrawal32399322011-09-01 10:53:43 -070026#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080027#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070028#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070029#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080030#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070031#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000032#include "shill/mock_metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070033#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070034#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070035#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070036#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070037#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070038#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070039#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070040#include "shill/wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070041
42using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070043using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070044using std::string;
45using std::vector;
46
Chris Masone9be4a9d2011-05-16 15:44:09 -070047namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070048using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070049using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080050using ::testing::ContainerEq;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070051using ::testing::InSequence;
Paul Stewart22aa71b2011-09-16 12:15:11 -070052using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070053using ::testing::NiceMock;
54using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070055using ::testing::ReturnRef;
Gaurav Shah435de2c2011-11-17 19:01:07 -080056using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080057using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070058using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070059
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070061 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070062 ManagerTest()
Paul Stewart22aa71b2011-09-16 12:15:11 -070063 : mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070064 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080065 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070066 manager(),
67 "wifi0",
68 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080069 4)),
70 device_info_(new NiceMock<MockDeviceInfo>(
71 control_interface(),
72 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080073 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080074 reinterpret_cast<Manager*>(NULL))),
75 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070076 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
77 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080078 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070079 manager(),
80 "null0",
81 "addr0",
82 0));
83 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
84 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080085 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070086 manager(),
87 "null1",
88 "addr1",
89 1));
90 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
91 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070093 manager(),
94 "null2",
95 "addr2",
96 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -080097 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
98 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080099 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800100 manager(),
101 "null3",
102 "addr3",
103 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700104 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800105
106 // Replace the manager's adaptor with a quieter one, and one
107 // we can do EXPECT*() against. Passes ownership.
108 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700109 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700111
Paul Stewartfdd16072011-09-16 12:41:35 -0700112 bool IsDeviceRegistered(const DeviceRefPtr &device,
113 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700114 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700115 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700116 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700117 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700118 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700119
Paul Stewarta849a3d2011-11-03 05:54:09 -0700120 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
121 manager->profiles_.push_back(profile);
122 }
123
Paul Stewart75225512012-01-26 22:51:33 -0800124 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
125 return manager->ephemeral_profile_;
126 }
127
Chris Masone6515aab2011-10-12 16:19:09 -0700128 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
129 Profile::Identifier id("rather", "irrelevant");
130 scoped_ptr<Profile> profile(new Profile(control_interface(),
131 manager,
132 id,
133 "",
134 false));
135 FilePath final_path(storage_path());
136 final_path = final_path.Append("test.profile");
137 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
138 storage->set_path(final_path);
139 if (!storage->Open())
140 return NULL;
141 profile->set_storage(storage.release()); // Passes ownership.
142 return profile.release();
143 }
144
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700145 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
146 const string &profile_identifier,
147 const string &service_name) {
148 GLib glib;
149 KeyFileStore store(&glib);
150 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
151 return store.Open() &&
152 store.SetString(service_name, "rather", "irrelevant") &&
153 store.Close();
154 }
155
156 Error::Type TestCreateProfile(Manager *manager, const string &name) {
157 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800158 string path;
159 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700160 return error.type();
161 }
162
163 Error::Type TestPopAnyProfile(Manager *manager) {
164 Error error;
165 manager->PopAnyProfile(&error);
166 return error.type();
167 }
168
169 Error::Type TestPopProfile(Manager *manager, const string &name) {
170 Error error;
171 manager->PopProfile(name, &error);
172 return error.type();
173 }
174
175 Error::Type TestPushProfile(Manager *manager, const string &name) {
176 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800177 string path;
178 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700179 return error.type();
180 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000181
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700182 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000183 typedef scoped_refptr<MockService> MockServiceRefPtr;
184
185 MockServiceRefPtr MakeAutoConnectableService() {
186 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
187 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800188 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000189 manager());
190 service->MakeFavorite();
191 service->set_connectable(true);
192 return service;
193 }
194
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700195 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700196 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800197 scoped_ptr<MockDeviceInfo> device_info_;
198
199 // This pointer is owned by the manager, and only tracked here for EXPECT*()
200 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700201};
202
Paul Stewart22aa71b2011-09-16 12:15:11 -0700203bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
204 return (svc0.get() == manager()->services_[0].get() &&
205 svc1.get() == manager()->services_[1].get());
206}
207
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700208TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700209 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
210 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700211}
212
Chris Masone9be4a9d2011-05-16 15:44:09 -0700213TEST_F(ManagerTest, DeviceRegistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700214 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700215 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700216 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700217 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700218 ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular))
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700219 .WillByDefault(Return(true));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700220
Paul Stewart22aa71b2011-09-16 12:15:11 -0700221 manager()->RegisterDevice(mock_devices_[0]);
222 manager()->RegisterDevice(mock_devices_[1]);
223 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700224
Paul Stewart22aa71b2011-09-16 12:15:11 -0700225 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
226 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
227 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700228}
229
Paul Stewarta41e38d2011-11-11 07:47:29 -0800230TEST_F(ManagerTest, DeviceRegistrationAndStart) {
231 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500232 mock_devices_[0]->enabled_persistent_ = true;
233 mock_devices_[1]->enabled_persistent_ = false;
234 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800235 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500236 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800237 .Times(0);
238 manager()->RegisterDevice(mock_devices_[0]);
239 manager()->RegisterDevice(mock_devices_[1]);
240}
241
242TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
243 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
244 DeviceRefPtr device_ref(mock_devices_[0].get());
245 AdoptProfile(manager(), profile); // Passes ownership.
246 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
247 EXPECT_CALL(*profile, Save());
248 manager()->RegisterDevice(mock_devices_[0]);
249}
250
Chris Masone9be4a9d2011-05-16 15:44:09 -0700251TEST_F(ManagerTest, DeviceDeregistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700252 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700253 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700254 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700255 .WillByDefault(Return(true));
256
Gaurav Shah435de2c2011-11-17 19:01:07 -0800257 manager()->RegisterDevice(mock_devices_[0]);
258 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700259
Paul Stewart22aa71b2011-09-16 12:15:11 -0700260 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
261 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700262
Eric Shienbrood9a245532012-03-07 14:20:39 -0500263 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800264 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700265 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700266
Eric Shienbrood9a245532012-03-07 14:20:39 -0500267 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800268 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700269 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700270}
271
272TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700273 // It's much easier and safer to use a real GLib for this test.
274 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700275 Manager manager(control_interface(),
276 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800277 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700278 &glib,
279 run_path(),
280 storage_path(),
281 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700282 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
283 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700284 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700285
Chris Masone9be4a9d2011-05-16 15:44:09 -0700286 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700287 new NiceMock<MockService>(control_interface(),
288 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800289 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700290 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700291 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700292 new NiceMock<MockService>(control_interface(),
293 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800294 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700295 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700296
mukesh agrawal51a7e932011-07-27 16:18:26 -0700297 string service1_name(mock_service->UniqueName());
298 string service2_name(mock_service2->UniqueName());
299
300 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
301 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700302 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700303 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700304 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700305 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700306 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700307
Chris Masone9d779932011-08-25 16:33:41 -0700308 manager.RegisterService(mock_service);
309 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700310
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800311 Error error;
312 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700313 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700314 EXPECT_EQ(2, ids.size());
315 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
316 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700317
Chris Masone9d779932011-08-25 16:33:41 -0700318 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
319 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
320
321 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700322}
323
Chris Masone6515aab2011-10-12 16:19:09 -0700324TEST_F(ManagerTest, RegisterKnownService) {
325 // It's much easier and safer to use a real GLib for this test.
326 GLib glib;
327 Manager manager(control_interface(),
328 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800329 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700330 &glib,
331 run_path(),
332 storage_path(),
333 string());
334 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
335 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700336 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700337 {
338 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
339 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800340 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700341 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700342 ASSERT_TRUE(profile->AdoptService(service1));
343 ASSERT_TRUE(profile->ContainsService(service1));
344 } // Force destruction of service1.
345
346 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
347 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800348 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700349 &manager));
350 manager.RegisterService(service2);
351 EXPECT_EQ(service2->profile().get(), profile.get());
352 manager.Stop();
353}
354
355TEST_F(ManagerTest, RegisterUnknownService) {
356 // It's much easier and safer to use a real GLib for this test.
357 GLib glib;
358 Manager manager(control_interface(),
359 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800360 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700361 &glib,
362 run_path(),
363 storage_path(),
364 string());
365 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
366 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700367 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700368 {
369 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
370 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800371 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700372 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700373 ASSERT_TRUE(profile->AdoptService(service1));
374 ASSERT_TRUE(profile->ContainsService(service1));
375 } // Force destruction of service1.
376 scoped_refptr<MockService> mock_service2(
377 new NiceMock<MockService>(control_interface(),
378 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800379 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700380 &manager));
381 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
382 .WillRepeatedly(Return(mock_service2->UniqueName()));
383 manager.RegisterService(mock_service2);
384 EXPECT_NE(mock_service2->profile().get(), profile.get());
385 manager.Stop();
386}
387
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000388TEST_F(ManagerTest, DeregisterUnregisteredService) {
389 // WiFi assumes that it can deregister a service that is not
390 // registered. (E.g. a hidden service can be deregistered when it
391 // loses its last endpoint, and again when WiFi is Stop()-ed.)
392 //
393 // So test that doing so doesn't cause a crash.
394 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
395 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800396 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000397 manager());
398 manager()->DeregisterService(service);
399}
400
Chris Masonea8a2c252011-06-27 22:16:30 -0700401TEST_F(ManagerTest, GetProperties) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700402 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700403 AdoptProfile(manager(), profile);
Chris Masonea8a2c252011-06-27 22:16:30 -0700404 map<string, ::DBus::Variant> props;
405 Error error(Error::kInvalidProperty, "");
406 {
407 ::DBus::Error dbus_error;
408 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700409 manager()->mutable_store()->SetStringProperty(
410 flimflam::kCheckPortalListProperty,
411 expected,
412 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700413 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700414 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
415 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
416 expected);
417 }
418 {
419 ::DBus::Error dbus_error;
420 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700421 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
422 expected,
423 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700424 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700425 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
426 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
427 expected);
428 }
429}
430
Chris Masone3c3f6a12011-07-01 10:01:41 -0700431TEST_F(ManagerTest, GetDevicesProperty) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700432 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700433 AdoptProfile(manager(), profile);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800434 manager()->RegisterDevice(mock_devices_[0]);
435 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700436 {
437 map<string, ::DBus::Variant> props;
438 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700439 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700440 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
441 Strings devices =
442 props[flimflam::kDevicesProperty].operator vector<string>();
443 EXPECT_EQ(2, devices.size());
444 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700445}
446
mukesh agrawal2366eed2012-03-20 18:21:50 -0700447TEST_F(ManagerTest, GetServicesProperty) {
448 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
449 AdoptProfile(manager(), profile);
450 map<string, ::DBus::Variant> props;
451 ::DBus::Error dbus_error;
452 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
453 map<string, ::DBus::Variant>::const_iterator prop =
454 props.find(flimflam::kServicesProperty);
455 ASSERT_FALSE(prop == props.end());
456 const ::DBus::Variant &variant = prop->second;
457 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
458}
459
Chris Masone6791a432011-07-12 13:23:19 -0700460TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700461 Manager manager(control_interface(),
462 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800463 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700464 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700465 run_path(),
466 storage_path(),
467 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700468 scoped_refptr<MockService> s2(new MockService(control_interface(),
469 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800470 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700471 &manager));
472 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700473 {
Chris Masone6515aab2011-10-12 16:19:09 -0700474 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700475 ProfileRefPtr profile(
476 new Profile(control_interface(), &manager, id, "", false));
477 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700478 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700479 .WillRepeatedly(Return(true));
480 EXPECT_CALL(*storage, Flush())
481 .Times(AnyNumber())
482 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700483 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700484 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700485 }
Chris Masone6515aab2011-10-12 16:19:09 -0700486 // Create a profile that already has |s2| in it.
487 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700488 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700489
Chris Masone6515aab2011-10-12 16:19:09 -0700490 // Now, move the Service |s2| to another profile.
491 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
492 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700493
494 // Force destruction of the original Profile, to ensure that the Service
495 // is kept alive and populated with data.
496 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700497 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700498 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700499}
500
Paul Stewart7f61e522012-03-22 11:13:45 -0700501TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
502 scoped_refptr<MockProfile> mock_profile(
503 new MockProfile(control_interface(), manager(), ""));
504 const string kProfileName("profile0");
505 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
506 .WillRepeatedly(Return(kProfileName));
507 AdoptProfile(manager(), mock_profile);
508
509 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
510 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
511 EXPECT_EQ(mock_profile.get(), profile.get());
512}
513
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800514TEST_F(ManagerTest, SetProfileForService) {
515 scoped_refptr<MockProfile> profile0(
516 new MockProfile(control_interface(), manager(), ""));
517 string profile_name0("profile0");
518 EXPECT_CALL(*profile0, GetRpcIdentifier())
519 .WillRepeatedly(Return(profile_name0));
520 AdoptProfile(manager(), profile0);
521 scoped_refptr<MockService> service(new MockService(control_interface(),
522 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800523 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800524 manager()));
525 service->set_profile(profile0);
526
527 {
528 Error error;
529 manager()->SetProfileForService(service, "foo", &error);
530 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700531 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800532 }
533
534 {
535 Error error;
536 manager()->SetProfileForService(service, profile_name0, &error);
537 EXPECT_EQ(Error::kInvalidArguments, error.type());
538 EXPECT_EQ("Service is already connected to this profile", error.message());
539 }
540
541 scoped_refptr<MockProfile> profile1(
542 new MockProfile(control_interface(), manager(), ""));
543 string profile_name1("profile1");
544 EXPECT_CALL(*profile1, GetRpcIdentifier())
545 .WillRepeatedly(Return(profile_name1));
546 AdoptProfile(manager(), profile1);
547
548 {
549 Error error;
550 EXPECT_CALL(*profile1, AdoptService(_))
551 .WillOnce(Return(true));
552 EXPECT_CALL(*profile0, AbandonService(_))
553 .WillOnce(Return(true));
554 manager()->SetProfileForService(service, profile_name1, &error);
555 EXPECT_TRUE(error.IsSuccess());
556 }
557}
558
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700559TEST_F(ManagerTest, CreateProfile) {
560 // It's much easier to use real Glib here since we want the storage
561 // side-effects.
562 GLib glib;
563 ScopedTempDir temp_dir;
564 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
565
566 Manager manager(control_interface(),
567 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800568 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700569 &glib,
570 run_path(),
571 storage_path(),
572 temp_dir.path().value());
573
574 // Invalid name should be rejected.
575 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
576
Paul Stewartd0a3b812012-03-28 22:48:22 -0700577 // A profile with invalid characters in it should similarly be rejected.
578 EXPECT_EQ(Error::kInvalidArguments,
579 TestCreateProfile(&manager, "valid_profile"));
580
581 // We should be able to create a machine profile.
582 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700583
Gary Morainb672d352012-04-25 09:19:06 -0700584 // We should succeed in creating a valid user profile. Verify the returned
585 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700586 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700587 {
588 Error error;
589 string path;
590 manager.CreateProfile(kProfile, &path, &error);
591 EXPECT_EQ(Error::kSuccess, error.type());
592 EXPECT_EQ("/profile_rpc", path);
593 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700594
595 // We should fail in creating it a second time (already exists).
596 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
597}
598
599TEST_F(ManagerTest, PushPopProfile) {
600 // It's much easier to use real Glib in creating a Manager for this
601 // test here since we want the storage side-effects.
602 GLib glib;
603 ScopedTempDir temp_dir;
604 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
605 Manager manager(control_interface(),
606 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800607 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700608 &glib,
609 run_path(),
610 storage_path(),
611 temp_dir.path().value());
612
613 // Pushing an invalid profile should fail.
614 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
615
Paul Stewartd0a3b812012-03-28 22:48:22 -0700616 // Pushing a default profile that does not exist should fail.
617 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700618
619 const char kProfile0[] = "~user/profile0";
620 const char kProfile1[] = "~user/profile1";
621
622 // Create a couple of profiles.
623 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
624 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
625
626 // Push these profiles on the stack.
627 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
628 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
629
630 // Pushing a profile a second time should fail.
631 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
632 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
633
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800634 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700635 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800636 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700637
638 // Make sure a profile name that doesn't exist fails.
639 const char kProfile2Id[] = "profile2";
640 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
641 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
642
643 // Create a new service, with a specific storage name.
644 scoped_refptr<MockService> service(
645 new NiceMock<MockService>(control_interface(),
646 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800647 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700648 &manager));
649 const char kServiceName[] = "service_storage_name";
650 EXPECT_CALL(*service.get(), GetStorageIdentifier())
651 .WillRepeatedly(Return(kServiceName));
652 EXPECT_CALL(*service.get(), Load(_))
653 .WillRepeatedly(Return(true));
654
655 // Add this service to the manager -- it should end up in the ephemeral
656 // profile.
657 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800658 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700659
660 // Create storage for a profile that contains the service storage name.
661 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
662 kServiceName));
663
664 // When we push the profile, the service should move away from the
665 // ephemeral profile to this new profile since it has an entry for
666 // this service.
667 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800668 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700669 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
670
671 // Insert another profile that should supersede ownership of the service.
672 const char kProfile3Id[] = "profile3";
673 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
674 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
675 kServiceName));
676 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
677 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
678
679 // Popping an invalid profile name should fail.
680 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
681
682 // Popping an profile that is not at the top of the stack should fail.
683 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
684
685 // Popping the top profile should succeed.
686 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
687
688 // Moreover the service should have switched profiles to profile 2.
689 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
690
691 // Popping the top profile should succeed.
692 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
693
694 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800695 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700696
697 // Pop the remaining two services off the stack.
698 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
699 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
700
701 // Next pop should fail with "stack is empty".
702 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700703
704 const char kMachineProfile0[] = "machineprofile0";
705 const char kMachineProfile1[] = "machineprofile1";
706 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
707 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
708
709 // Should be able to push a machine profile.
710 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
711
712 // Should be able to push a user profile atop a machine profile.
713 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
714
715 // Pushing a system-wide profile on top of a user profile should fail.
716 EXPECT_EQ(Error::kInvalidArguments,
717 TestPushProfile(&manager, kMachineProfile1));
718
719 // However if we pop the user profile, we should be able stack another
720 // machine profile on.
721 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
722 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700723}
724
Paul Stewarte73d05c2012-03-29 16:26:05 -0700725TEST_F(ManagerTest, RemoveProfile) {
726 // It's much easier to use real Glib in creating a Manager for this
727 // test here since we want the storage side-effects.
728 GLib glib;
729 ScopedTempDir temp_dir;
730 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
731 Manager manager(control_interface(),
732 dispatcher(),
733 metrics(),
734 &glib,
735 run_path(),
736 storage_path(),
737 temp_dir.path().value());
738
739 const char kProfile0[] = "profile0";
740 FilePath profile_path(
741 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
742
743 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
744 ASSERT_TRUE(file_util::PathExists(profile_path));
745
746 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
747
748 // Remove should fail since the profile is still on the stack.
749 {
750 Error error;
751 manager.RemoveProfile(kProfile0, &error);
752 EXPECT_EQ(Error::kInvalidArguments, error.type());
753 }
754
755 // Profile path should still exist.
756 EXPECT_TRUE(file_util::PathExists(profile_path));
757
758 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
759
760 // This should succeed now that the profile is off the stack.
761 {
762 Error error;
763 manager.RemoveProfile(kProfile0, &error);
764 EXPECT_EQ(Error::kSuccess, error.type());
765 }
766
767 // Profile path should no longer exist.
768 EXPECT_FALSE(file_util::PathExists(profile_path));
769
770 // Another remove succeeds, due to a foible in file_util::Delete --
771 // it is not an error to delete a file that does not exist.
772 {
773 Error error;
774 manager.RemoveProfile(kProfile0, &error);
775 EXPECT_EQ(Error::kSuccess, error.type());
776 }
777
778 // Let's create an error case that will "work". Create a non-empty
779 // directory in the place of the profile pathname.
780 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
781 {
782 Error error;
783 manager.RemoveProfile(kProfile0, &error);
784 EXPECT_EQ(Error::kOperationFailed, error.type());
785 }
786}
787
Paul Stewart75225512012-01-26 22:51:33 -0800788// Use this matcher instead of passing RefPtrs directly into the arguments
789// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
790// system teardown.
791MATCHER_P(IsRefPtrTo, ref_address, "") {
792 return arg.get() == ref_address;
793}
794
795TEST_F(ManagerTest, HandleProfileEntryDeletion) {
796 MockServiceRefPtr s_not_in_profile(
797 new NiceMock<MockService>(control_interface(),
798 dispatcher(),
799 metrics(),
800 manager()));
801 MockServiceRefPtr s_not_in_group(
802 new NiceMock<MockService>(control_interface(),
803 dispatcher(),
804 metrics(),
805 manager()));
806 MockServiceRefPtr s_configure_fail(
807 new NiceMock<MockService>(control_interface(),
808 dispatcher(),
809 metrics(),
810 manager()));
811 MockServiceRefPtr s_configure_succeed(
812 new NiceMock<MockService>(control_interface(),
813 dispatcher(),
814 metrics(),
815 manager()));
816
817 string entry_name("entry_name");
818 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
819 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
820 .WillRepeatedly(Return("not_entry_name"));
821 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
822 .WillRepeatedly(Return(entry_name));
823 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
824 .WillRepeatedly(Return(entry_name));
825
826 manager()->RegisterService(s_not_in_profile);
827 manager()->RegisterService(s_not_in_group);
828 manager()->RegisterService(s_configure_fail);
829 manager()->RegisterService(s_configure_succeed);
830
831 scoped_refptr<MockProfile> profile0(
832 new StrictMock<MockProfile>(control_interface(), manager(), ""));
833 scoped_refptr<MockProfile> profile1(
834 new StrictMock<MockProfile>(control_interface(), manager(), ""));
835
836 s_not_in_group->set_profile(profile1);
837 s_configure_fail->set_profile(profile1);
838 s_configure_succeed->set_profile(profile1);
839
840 AdoptProfile(manager(), profile0);
841 AdoptProfile(manager(), profile1);
842
843 // No services are a member of this profile.
844 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
845
846 // No services that are members of this profile have this entry name.
847 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
848
849 // Only services that are members of the profile and group will be abandoned.
850 EXPECT_CALL(*profile1.get(),
851 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
852 EXPECT_CALL(*profile1.get(),
853 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
854 EXPECT_CALL(*profile1.get(),
855 AbandonService(IsRefPtrTo(s_configure_fail.get())))
856 .WillOnce(Return(true));
857 EXPECT_CALL(*profile1.get(),
858 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
859 .WillOnce(Return(true));
860
861 // Never allow services to re-join profile1.
862 EXPECT_CALL(*profile1.get(), ConfigureService(_))
863 .WillRepeatedly(Return(false));
864
865 // Only allow one of the members of the profile and group to successfully
866 // join profile0.
867 EXPECT_CALL(*profile0.get(),
868 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
869 EXPECT_CALL(*profile0.get(),
870 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
871 EXPECT_CALL(*profile0.get(),
872 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
873 .WillOnce(Return(false));
874 EXPECT_CALL(*profile0.get(),
875 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
876 .WillOnce(Return(true));
877
878 // Expect the failed-to-configure service to have Unload() called on it.
879 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
880 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
881 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
882 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
883
884 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
885
886 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
887 EXPECT_EQ(profile1, s_not_in_group->profile());
888 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
889
890 // Since we are using a MockProfile, the profile does not actually change,
891 // since ConfigureService was not actually called on the service.
892 EXPECT_EQ(profile1, s_configure_succeed->profile());
893}
894
Paul Stewart65512e12012-03-26 18:01:08 -0700895TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
896 MockServiceRefPtr s_will_remove0(
897 new NiceMock<MockService>(control_interface(),
898 dispatcher(),
899 metrics(),
900 manager()));
901 MockServiceRefPtr s_will_remove1(
902 new NiceMock<MockService>(control_interface(),
903 dispatcher(),
904 metrics(),
905 manager()));
906 MockServiceRefPtr s_will_not_remove0(
907 new NiceMock<MockService>(control_interface(),
908 dispatcher(),
909 metrics(),
910 manager()));
911 MockServiceRefPtr s_will_not_remove1(
912 new NiceMock<MockService>(control_interface(),
913 dispatcher(),
914 metrics(),
915 manager()));
916
917 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
918 .Times(4); // Once for each registration.
919
920 string entry_name("entry_name");
921 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
922 .WillRepeatedly(Return(entry_name));
923 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
924 .WillRepeatedly(Return(entry_name));
925 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
926 .WillRepeatedly(Return(entry_name));
927 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
928 .WillRepeatedly(Return(entry_name));
929
930 manager()->RegisterService(s_will_remove0);
931 manager()->RegisterService(s_will_not_remove0);
932 manager()->RegisterService(s_will_remove1);
933 manager()->RegisterService(s_will_not_remove1);
934
935 // One for each service added above.
936 ASSERT_EQ(4, manager()->services_.size());
937
938 scoped_refptr<MockProfile> profile(
939 new StrictMock<MockProfile>(control_interface(), manager(), ""));
940
941 s_will_remove0->set_profile(profile);
942 s_will_remove1->set_profile(profile);
943 s_will_not_remove0->set_profile(profile);
944 s_will_not_remove1->set_profile(profile);
945
946 AdoptProfile(manager(), profile);
947
948 // Deny any of the services re-entry to the profile.
949 EXPECT_CALL(*profile, ConfigureService(_))
950 .WillRepeatedly(Return(false));
951
952 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
953 .WillOnce(Return(true));
954 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
955 .WillOnce(Return(true));
956 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
957 .WillOnce(Return(true));
958 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
959 .WillOnce(Return(true));
960
961 EXPECT_CALL(*s_will_remove0, Unload())
962 .WillOnce(Return(true));
963 EXPECT_CALL(*s_will_remove1, Unload())
964 .WillOnce(Return(true));
965 EXPECT_CALL(*s_will_not_remove0, Unload())
966 .WillOnce(Return(false));
967 EXPECT_CALL(*s_will_not_remove1, Unload())
968 .WillOnce(Return(false));
969
970
971 // This will cause all the profiles to be unloaded.
972 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
973
974 // 2 of the 4 services added above should have been unregistered and
975 // removed, leaving 2.
976 EXPECT_EQ(2, manager()->services_.size());
977 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
978 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
979}
980
981TEST_F(ManagerTest, PopProfileWithUnload) {
982 MockServiceRefPtr s_will_remove0(
983 new NiceMock<MockService>(control_interface(),
984 dispatcher(),
985 metrics(),
986 manager()));
987 MockServiceRefPtr s_will_remove1(
988 new NiceMock<MockService>(control_interface(),
989 dispatcher(),
990 metrics(),
991 manager()));
992 MockServiceRefPtr s_will_not_remove0(
993 new NiceMock<MockService>(control_interface(),
994 dispatcher(),
995 metrics(),
996 manager()));
997 MockServiceRefPtr s_will_not_remove1(
998 new NiceMock<MockService>(control_interface(),
999 dispatcher(),
1000 metrics(),
1001 manager()));
1002
1003 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1004 .Times(5); // Once for each registration, and one after profile pop.
1005
1006 manager()->RegisterService(s_will_remove0);
1007 manager()->RegisterService(s_will_not_remove0);
1008 manager()->RegisterService(s_will_remove1);
1009 manager()->RegisterService(s_will_not_remove1);
1010
1011 // One for each service added above.
1012 ASSERT_EQ(4, manager()->services_.size());
1013
1014 scoped_refptr<MockProfile> profile0(
1015 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1016 scoped_refptr<MockProfile> profile1(
1017 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1018
1019 s_will_remove0->set_profile(profile1);
1020 s_will_remove1->set_profile(profile1);
1021 s_will_not_remove0->set_profile(profile1);
1022 s_will_not_remove1->set_profile(profile1);
1023
1024 AdoptProfile(manager(), profile0);
1025 AdoptProfile(manager(), profile1);
1026
1027 // Deny any of the services entry to profile0, so they will all be unloaded.
1028 EXPECT_CALL(*profile0, ConfigureService(_))
1029 .WillRepeatedly(Return(false));
1030
1031 EXPECT_CALL(*s_will_remove0, Unload())
1032 .WillOnce(Return(true));
1033 EXPECT_CALL(*s_will_remove1, Unload())
1034 .WillOnce(Return(true));
1035 EXPECT_CALL(*s_will_not_remove0, Unload())
1036 .WillOnce(Return(false));
1037 EXPECT_CALL(*s_will_not_remove1, Unload())
1038 .WillOnce(Return(false));
1039
1040 // This will pop profile1, which should cause all our profiles to unload.
1041 manager()->PopProfileInternal();
1042
1043 // 2 of the 4 services added above should have been unregistered and
1044 // removed, leaving 2.
1045 EXPECT_EQ(2, manager()->services_.size());
1046 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1047 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1048}
1049
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001050TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001051 {
1052 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001053 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1054 flimflam::kOfflineModeProperty,
1055 PropertyStoreTest::kBoolV,
1056 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001057 }
1058 {
1059 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001060 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1061 flimflam::kCountryProperty,
1062 PropertyStoreTest::kStringV,
1063 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001064 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001065 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001066 {
1067 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001068 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1069 flimflam::kCountryProperty,
1070 PropertyStoreTest::kBoolV,
1071 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001072 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001073 }
1074 {
1075 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001076 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1077 flimflam::kOfflineModeProperty,
1078 PropertyStoreTest::kStringV,
1079 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001080 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001081 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001082 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001083 {
1084 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001085 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001086 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001087 flimflam::kEnabledTechnologiesProperty,
1088 PropertyStoreTest::kStringsV,
1089 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001090 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001091 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001092}
1093
mukesh agrawal32399322011-09-01 10:53:43 -07001094TEST_F(ManagerTest, RequestScan) {
1095 {
1096 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001097 manager()->RegisterDevice(mock_devices_[0].get());
1098 manager()->RegisterDevice(mock_devices_[1].get());
1099 EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -07001100 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001101 EXPECT_CALL(*mock_devices_[0], Scan(_));
1102 EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -07001103 .WillRepeatedly(Return(false));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001104 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001105 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001106 }
1107
1108 {
1109 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001110 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001111 EXPECT_EQ(Error::kInvalidArguments, error.type());
1112 }
1113}
1114
Darin Petkovb65c2452012-02-23 15:17:06 +01001115TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001116 KeyValueStore args;
1117 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001118 manager()->GetService(args, &e);
1119 EXPECT_EQ(Error::kInvalidArguments, e.type());
1120 EXPECT_EQ("must specify service type", e.message());
1121}
1122
1123TEST_F(ManagerTest, GetServiceUnknownType) {
1124 KeyValueStore args;
1125 Error e;
1126 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1127 manager()->GetService(args, &e);
1128 EXPECT_EQ(Error::kNotSupported, e.type());
1129 EXPECT_EQ("service type is unsupported", e.message());
1130}
1131
1132TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1133 KeyValueStore args;
1134 Error e;
1135 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1136 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001137 EXPECT_EQ(Error::kInvalidArguments, e.type());
1138 EXPECT_EQ("no wifi devices available", e.message());
1139}
1140
Darin Petkovb65c2452012-02-23 15:17:06 +01001141TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001142 KeyValueStore args;
1143 Error e;
1144 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001145 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001146 manager()->RegisterDevice(mock_wifi_);
1147 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1148 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001149 manager()->GetService(args, &e);
1150 EXPECT_TRUE(e.IsSuccess());
1151}
1152
Darin Petkov33af05c2012-02-28 10:10:30 +01001153TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1154 KeyValueStore args;
1155 Error e;
1156 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
1157 ServiceRefPtr service = manager()->GetService(args, &e);
1158 EXPECT_EQ(Error::kNotSupported, e.type());
1159 EXPECT_FALSE(service);
1160}
1161
Darin Petkovb65c2452012-02-23 15:17:06 +01001162TEST_F(ManagerTest, GetServiceVPN) {
1163 KeyValueStore args;
1164 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001165 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001166 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001167 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1168 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Darin Petkov33af05c2012-02-28 10:10:30 +01001169 ServiceRefPtr service = manager()->GetService(args, &e);
1170 EXPECT_TRUE(e.IsSuccess());
1171 EXPECT_TRUE(service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001172}
1173
Paul Stewart7f61e522012-03-22 11:13:45 -07001174TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1175 // Manager calls ActiveProfile() so we need at least one profile installed.
1176 scoped_refptr<MockProfile> profile(
1177 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1178 AdoptProfile(manager(), profile);
1179
1180 KeyValueStore args;
1181 args.SetString(flimflam::kProfileProperty, "xxx");
1182 Error error;
1183 manager()->ConfigureService(args, &error);
1184 EXPECT_EQ(Error::kInvalidArguments, error.type());
1185 EXPECT_EQ("Invalid profile name xxx", error.message());
1186}
1187
1188TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1189 // Manager calls ActiveProfile() so we need at least one profile installed.
1190 scoped_refptr<MockProfile> profile(
1191 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1192 AdoptProfile(manager(), profile);
1193
1194 KeyValueStore args;
1195 Error error;
1196 manager()->ConfigureService(args, &error);
1197 EXPECT_EQ(Error::kInvalidArguments, error.type());
1198 EXPECT_EQ("must specify service type", error.message());
1199}
1200
1201// A registered service in the ephemeral profile should be moved to the
1202// active profile as a part of configuration if no profile was explicitly
1203// specified.
1204TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1205 scoped_refptr<MockProfile> profile(
1206 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1207
1208 AdoptProfile(manager(), profile); // This is now the active profile.
1209
1210 const std::vector<uint8_t> ssid;
1211 scoped_refptr<MockWiFiService> service(
1212 new NiceMock<MockWiFiService>(control_interface(),
1213 dispatcher(),
1214 metrics(),
1215 manager(),
1216 mock_wifi_,
1217 ssid,
1218 "",
1219 "",
1220 false));
1221
1222 manager()->RegisterService(service);
1223 service->set_profile(GetEphemeralProfile(manager()));
1224
1225 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1226 // the same device as that used above causes a refcounting loop.
1227 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1228 dispatcher(),
1229 metrics(),
1230 manager(),
1231 "wifi1",
1232 "addr5",
1233 5));
1234 manager()->RegisterDevice(wifi);
1235 EXPECT_CALL(*wifi, GetService(_, _))
1236 .WillOnce(Return(service));
1237 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1238 .WillOnce(Return(true));
1239 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1240 .WillOnce(Return(true));
1241
1242 KeyValueStore args;
1243 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1244 Error error;
1245 manager()->ConfigureService(args, &error);
1246 EXPECT_TRUE(error.IsSuccess());
1247}
1248
1249// If were configure a service that was already registered and explicitly
1250// specify a profile, it should be moved from the profile it was previously
1251// in to the specified profile if one was requested.
1252TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
1253 scoped_refptr<MockProfile> profile0(
1254 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1255 scoped_refptr<MockProfile> profile1(
1256 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1257
1258 const string kProfileName0 = "profile0";
1259 const string kProfileName1 = "profile1";
1260
1261 EXPECT_CALL(*profile0, GetRpcIdentifier())
1262 .WillRepeatedly(Return(kProfileName0));
1263 EXPECT_CALL(*profile1, GetRpcIdentifier())
1264 .WillRepeatedly(Return(kProfileName1));
1265
1266 AdoptProfile(manager(), profile0);
1267 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1268
1269 const std::vector<uint8_t> ssid;
1270 scoped_refptr<MockWiFiService> service(
1271 new NiceMock<MockWiFiService>(control_interface(),
1272 dispatcher(),
1273 metrics(),
1274 manager(),
1275 mock_wifi_,
1276 ssid,
1277 "",
1278 "",
1279 false));
1280
1281 manager()->RegisterService(service);
1282 service->set_profile(profile1);
1283
1284 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1285 // the same device as that used above causes a refcounting loop.
1286 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1287 dispatcher(),
1288 metrics(),
1289 manager(),
1290 "wifi1",
1291 "addr5",
1292 5));
1293 manager()->RegisterDevice(wifi);
1294 EXPECT_CALL(*wifi, GetService(_, _))
1295 .WillOnce(Return(service));
1296 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1297 .WillOnce(Return(true));
1298 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1299 .WillOnce(Return(true));
1300 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1301 .WillOnce(Return(true));
1302
1303 KeyValueStore args;
1304 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1305 args.SetString(flimflam::kProfileProperty, kProfileName0);
1306 Error error;
1307 manager()->ConfigureService(args, &error);
1308 EXPECT_TRUE(error.IsSuccess());
1309 service->set_profile(NULL); // Breaks refcounting loop.
1310}
1311
1312// An unregistered service should remain unregistered, but its contents should
1313// be saved to the specified profile nonetheless.
1314TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1315 scoped_refptr<MockProfile> profile0(
1316 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1317 scoped_refptr<MockProfile> profile1(
1318 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1319
1320 const string kProfileName0 = "profile0";
1321 const string kProfileName1 = "profile1";
1322
1323 EXPECT_CALL(*profile0, GetRpcIdentifier())
1324 .WillRepeatedly(Return(kProfileName0));
1325 EXPECT_CALL(*profile1, GetRpcIdentifier())
1326 .WillRepeatedly(Return(kProfileName1));
1327
1328 AdoptProfile(manager(), profile0);
1329 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1330
1331 const std::vector<uint8_t> ssid;
1332 scoped_refptr<MockWiFiService> service(
1333 new NiceMock<MockWiFiService>(control_interface(),
1334 dispatcher(),
1335 metrics(),
1336 manager(),
1337 mock_wifi_,
1338 ssid,
1339 "",
1340 "",
1341 false));
1342
1343 service->set_profile(profile1);
1344
1345 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1346 // the same device as that used above causes a refcounting loop.
1347 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1348 dispatcher(),
1349 metrics(),
1350 manager(),
1351 "wifi1",
1352 "addr5",
1353 5));
1354 manager()->RegisterDevice(wifi);
1355 EXPECT_CALL(*wifi, GetService(_, _))
1356 .WillOnce(Return(service));
1357 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1358 .WillOnce(Return(true));
1359 EXPECT_CALL(*profile0, AdoptService(_))
1360 .Times(0);
1361 EXPECT_CALL(*profile1, AdoptService(_))
1362 .Times(0);
1363
1364 KeyValueStore args;
1365 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1366 args.SetString(flimflam::kProfileProperty, kProfileName0);
1367 Error error;
1368 manager()->ConfigureService(args, &error);
1369 EXPECT_TRUE(error.IsSuccess());
1370}
1371
Paul Stewart22aa71b2011-09-16 12:15:11 -07001372TEST_F(ManagerTest, TechnologyOrder) {
1373 Error error;
1374 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1375 string(flimflam::kTypeWifi), &error);
1376 ASSERT_TRUE(error.IsSuccess());
1377 EXPECT_EQ(manager()->GetTechnologyOrder(),
1378 string(flimflam::kTypeEthernet) + "," +
1379 string(flimflam::kTypeWifi));
1380
1381 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1382 string(flimflam::kTypeWifi), &error);
1383 ASSERT_FALSE(error.IsSuccess());
1384 EXPECT_EQ(Error::kInvalidArguments, error.type());
1385 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1386 string(flimflam::kTypeWifi),
1387 manager()->GetTechnologyOrder());
1388}
1389
1390TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001391 // TODO(quiche): Some of these tests would probably fit better in
1392 // service_unittest, since the actual comparison of Services is
1393 // implemented in Service. (crosbug.com/23370)
1394
Paul Stewart22aa71b2011-09-16 12:15:11 -07001395 scoped_refptr<MockService> mock_service0(
1396 new NiceMock<MockService>(control_interface(),
1397 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001398 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001399 manager()));
1400 scoped_refptr<MockService> mock_service1(
1401 new NiceMock<MockService>(control_interface(),
1402 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001403 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001404 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001405
1406 manager()->RegisterService(mock_service0);
1407 manager()->RegisterService(mock_service1);
1408
1409 // Services should already be sorted by UniqueName
1410 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1411
1412 // Asking explictly to sort services should not change anything
1413 manager()->SortServices();
1414 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1415
1416 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001417 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001418 manager()->UpdateService(mock_service1);
1419 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1420
1421 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001422 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001423 manager()->UpdateService(mock_service0);
1424 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1425
1426 // Technology
1427 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi))
1428 .WillRepeatedly(Return(true));
1429 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet))
1430 .WillRepeatedly(Return(true));
1431 // NB: Redefine default (false) return values so we don't use the default rule
1432 // which makes the logs noisier
1433 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi)))
1434 .WillRepeatedly(Return(false));
1435 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet)))
1436 .WillRepeatedly(Return(false));
1437
1438 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001439 // Default technology ordering should favor Ethernet over WiFi.
1440 manager()->SortServices();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001441 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1442
1443 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1444 string(flimflam::kTypeEthernet), &error);
1445 EXPECT_TRUE(error.IsSuccess());
1446 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1447
Gaurav Shah435de2c2011-11-17 19:01:07 -08001448 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001449 mock_service0->set_priority(1);
1450 manager()->UpdateService(mock_service0);
1451 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1452
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001453 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001454 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001455 manager()->UpdateService(mock_service1);
1456 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1457
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001458 // Auto-connect.
1459 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001460 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001461 mock_service1->set_auto_connect(false);
1462 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001463 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1464
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001465 // Connectable.
1466 mock_service1->set_connectable(true);
1467 manager()->UpdateService(mock_service1);
1468 mock_service0->set_connectable(false);
1469 manager()->UpdateService(mock_service0);
1470 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1471
1472 // IsFailed.
1473 EXPECT_CALL(*mock_service0.get(), state())
1474 .WillRepeatedly(Return(Service::kStateIdle));
1475 EXPECT_CALL(*mock_service0.get(), IsFailed())
1476 .WillRepeatedly(Return(false));
1477 manager()->UpdateService(mock_service0);
1478 EXPECT_CALL(*mock_service0.get(), state())
1479 .WillRepeatedly(Return(Service::kStateFailure));
1480 EXPECT_CALL(*mock_service1.get(), IsFailed())
1481 .WillRepeatedly(Return(true));
1482 manager()->UpdateService(mock_service1);
1483 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1484
1485 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001486 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001487 .WillRepeatedly(Return(Service::kStateAssociating));
1488 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001489 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001490 manager()->UpdateService(mock_service1);
1491 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1492
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001493 // Connected.
1494 EXPECT_CALL(*mock_service0.get(), state())
1495 .WillRepeatedly(Return(Service::kStateConnected));
1496 EXPECT_CALL(*mock_service0.get(), IsConnected())
1497 .WillRepeatedly(Return(true));
1498 manager()->UpdateService(mock_service0);
1499 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1500
Paul Stewart22aa71b2011-09-16 12:15:11 -07001501 manager()->DeregisterService(mock_service0);
1502 manager()->DeregisterService(mock_service1);
1503}
1504
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001505TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001506 MockMetrics mock_metrics;
1507 manager()->set_metrics(&mock_metrics);
1508
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001509 scoped_refptr<MockService> mock_service0(
1510 new NiceMock<MockService>(control_interface(),
1511 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001512 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001513 manager()));
1514 scoped_refptr<MockService> mock_service1(
1515 new NiceMock<MockService>(control_interface(),
1516 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001517 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001518 manager()));
1519
1520 scoped_refptr<MockConnection> mock_connection0(
1521 new NiceMock<MockConnection>(device_info_.get()));
1522 scoped_refptr<MockConnection> mock_connection1(
1523 new NiceMock<MockConnection>(device_info_.get()));
1524
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001525 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001526 manager()->RegisterService(mock_service0);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001527 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001528 manager()->RegisterService(mock_service1);
1529
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001530 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1531 manager()->SortServices();
1532
1533 mock_service1->set_priority(1);
1534 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1535 manager()->SortServices();
1536
1537 mock_service1->set_priority(0);
1538 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1539 manager()->SortServices();
1540
Paul Stewartce4ec192012-03-14 12:53:46 -07001541 mock_service0->set_mock_connection(mock_connection0);
1542 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001543
1544 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001545 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001546 manager()->SortServices();
1547
1548 mock_service1->set_priority(1);
1549 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1550 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001551 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001552 manager()->SortServices();
1553
1554 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001555 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001556 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001557 manager()->DeregisterService(mock_service1);
1558
Paul Stewartce4ec192012-03-14 12:53:46 -07001559 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001560 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001561 manager()->DeregisterService(mock_service0);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001562
1563 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
1564 manager()->SortServices();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001565}
1566
Gaurav Shah435de2c2011-11-17 19:01:07 -08001567TEST_F(ManagerTest, AvailableTechnologies) {
1568 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1569 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001570 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001571 manager(),
1572 "null4",
1573 "addr4",
1574 0));
1575 manager()->RegisterDevice(mock_devices_[0]);
1576 manager()->RegisterDevice(mock_devices_[1]);
1577 manager()->RegisterDevice(mock_devices_[2]);
1578 manager()->RegisterDevice(mock_devices_[3]);
1579
1580 ON_CALL(*mock_devices_[0].get(), technology())
1581 .WillByDefault(Return(Technology::kEthernet));
1582 ON_CALL(*mock_devices_[1].get(), technology())
1583 .WillByDefault(Return(Technology::kWifi));
1584 ON_CALL(*mock_devices_[2].get(), technology())
1585 .WillByDefault(Return(Technology::kCellular));
1586 ON_CALL(*mock_devices_[3].get(), technology())
1587 .WillByDefault(Return(Technology::kWifi));
1588
1589 set<string> expected_technologies;
1590 expected_technologies.insert(Technology::NameFromIdentifier(
1591 Technology::kEthernet));
1592 expected_technologies.insert(Technology::NameFromIdentifier(
1593 Technology::kWifi));
1594 expected_technologies.insert(Technology::NameFromIdentifier(
1595 Technology::kCellular));
1596 Error error;
1597 vector<string> technologies = manager()->AvailableTechnologies(&error);
1598
1599 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1600 ContainerEq(expected_technologies));
1601}
1602
1603TEST_F(ManagerTest, ConnectedTechnologies) {
1604 scoped_refptr<MockService> connected_service1(
1605 new NiceMock<MockService>(control_interface(),
1606 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001607 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001608 manager()));
1609 scoped_refptr<MockService> connected_service2(
1610 new NiceMock<MockService>(control_interface(),
1611 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001612 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001613 manager()));
1614 scoped_refptr<MockService> disconnected_service1(
1615 new NiceMock<MockService>(control_interface(),
1616 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001617 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001618 manager()));
1619 scoped_refptr<MockService> disconnected_service2(
1620 new NiceMock<MockService>(control_interface(),
1621 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001622 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001623 manager()));
1624
1625 ON_CALL(*connected_service1.get(), IsConnected())
1626 .WillByDefault(Return(true));
1627 ON_CALL(*connected_service2.get(), IsConnected())
1628 .WillByDefault(Return(true));
1629
1630 manager()->RegisterService(connected_service1);
1631 manager()->RegisterService(connected_service2);
1632 manager()->RegisterService(disconnected_service1);
1633 manager()->RegisterService(disconnected_service2);
1634
1635 manager()->RegisterDevice(mock_devices_[0]);
1636 manager()->RegisterDevice(mock_devices_[1]);
1637 manager()->RegisterDevice(mock_devices_[2]);
1638 manager()->RegisterDevice(mock_devices_[3]);
1639
1640 ON_CALL(*mock_devices_[0].get(), technology())
1641 .WillByDefault(Return(Technology::kEthernet));
1642 ON_CALL(*mock_devices_[1].get(), technology())
1643 .WillByDefault(Return(Technology::kWifi));
1644 ON_CALL(*mock_devices_[2].get(), technology())
1645 .WillByDefault(Return(Technology::kCellular));
1646 ON_CALL(*mock_devices_[3].get(), technology())
1647 .WillByDefault(Return(Technology::kWifi));
1648
1649 mock_devices_[0]->SelectService(connected_service1);
1650 mock_devices_[1]->SelectService(disconnected_service1);
1651 mock_devices_[2]->SelectService(disconnected_service2);
1652 mock_devices_[3]->SelectService(connected_service2);
1653
1654 set<string> expected_technologies;
1655 expected_technologies.insert(Technology::NameFromIdentifier(
1656 Technology::kEthernet));
1657 expected_technologies.insert(Technology::NameFromIdentifier(
1658 Technology::kWifi));
1659 Error error;
1660
1661 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1662 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1663 ContainerEq(expected_technologies));
1664}
1665
1666TEST_F(ManagerTest, DefaultTechnology) {
1667 scoped_refptr<MockService> connected_service(
1668 new NiceMock<MockService>(control_interface(),
1669 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001670 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001671 manager()));
1672 scoped_refptr<MockService> disconnected_service(
1673 new NiceMock<MockService>(control_interface(),
1674 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001675 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001676 manager()));
1677
1678 // Connected. WiFi.
1679 ON_CALL(*connected_service.get(), IsConnected())
1680 .WillByDefault(Return(true));
1681 ON_CALL(*connected_service.get(), state())
1682 .WillByDefault(Return(Service::kStateConnected));
1683 ON_CALL(*connected_service.get(), technology())
1684 .WillByDefault(Return(Technology::kWifi));
1685
1686 // Disconnected. Ethernet.
1687 ON_CALL(*disconnected_service.get(), technology())
1688 .WillByDefault(Return(Technology::kEthernet));
1689
1690 manager()->RegisterService(disconnected_service);
1691 Error error;
1692 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1693
1694
1695 manager()->RegisterService(connected_service);
1696 // Connected service should be brought to the front now.
1697 string expected_technology =
1698 Technology::NameFromIdentifier(Technology::kWifi);
1699 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1700}
1701
Thieu Le1271d682011-11-02 22:48:19 +00001702TEST_F(ManagerTest, DisconnectServicesOnStop) {
1703 scoped_refptr<MockService> mock_service(
1704 new NiceMock<MockService>(control_interface(),
1705 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001706 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001707 manager()));
1708 manager()->RegisterService(mock_service);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001709 EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001710 manager()->Stop();
1711}
1712
mukesh agrawal00917ce2011-11-22 23:56:55 +00001713TEST_F(ManagerTest, UpdateServiceConnected) {
1714 scoped_refptr<MockService> mock_service(
1715 new NiceMock<MockService>(control_interface(),
1716 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001717 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001718 manager()));
1719 manager()->RegisterService(mock_service);
1720 EXPECT_FALSE(mock_service->favorite());
1721 EXPECT_FALSE(mock_service->auto_connect());
1722
Gaurav Shah435de2c2011-11-17 19:01:07 -08001723 EXPECT_CALL(*mock_service.get(), IsConnected())
1724 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001725 manager()->UpdateService(mock_service);
1726 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1727 // to mock out MakeFavorite. And mocking that out would break the
1728 // SortServices test. (crosbug.com/23370)
1729 EXPECT_TRUE(mock_service->favorite());
1730 EXPECT_TRUE(mock_service->auto_connect());
1731}
1732
Thieu Led4e9e552012-02-16 16:26:07 -08001733TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
1734 // This tests the case where the user connects to a service that is
1735 // currently associated with a profile. We want to make sure that the
1736 // favorite flag is set and that the flag is saved to the current
1737 // profile.
1738 scoped_refptr<MockService> mock_service(
1739 new NiceMock<MockService>(control_interface(),
1740 dispatcher(),
1741 metrics(),
1742 manager()));
1743 manager()->RegisterService(mock_service);
1744 EXPECT_FALSE(mock_service->favorite());
1745 EXPECT_FALSE(mock_service->auto_connect());
1746
1747 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
1748 mock_service->set_profile(profile);
1749
1750 EXPECT_CALL(*mock_service.get(), IsConnected())
1751 .WillRepeatedly(Return(true));
1752 EXPECT_CALL(*mock_service.get(), SaveToCurrentProfile());
1753 manager()->UpdateService(mock_service);
1754 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1755 // to mock out MakeFavorite. And mocking that out would break the
1756 // SortServices test. (crosbug.com/23370)
1757 EXPECT_TRUE(mock_service->favorite());
1758 EXPECT_TRUE(mock_service->auto_connect());
1759}
1760
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001761TEST_F(ManagerTest, SaveSuccessfulService) {
1762 scoped_refptr<MockProfile> profile(
1763 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1764 AdoptProfile(manager(), profile);
1765 scoped_refptr<MockService> service(
1766 new NiceMock<MockService>(control_interface(),
1767 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001768 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001769 manager()));
1770
1771 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
1772 ServiceRefPtr expect_service(service.get());
1773
1774 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
1775 .WillOnce(Return(false));
1776 manager()->RegisterService(service);
1777
1778 EXPECT_CALL(*service.get(), state())
1779 .WillRepeatedly(Return(Service::kStateConnected));
1780 EXPECT_CALL(*service.get(), IsConnected())
1781 .WillRepeatedly(Return(true));
1782 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
1783 .WillOnce(Return(true));
1784 manager()->UpdateService(service);
1785}
1786
Paul Stewart1b253142012-01-26 14:05:52 -08001787TEST_F(ManagerTest, EnumerateProfiles) {
1788 vector<string> profile_paths;
1789 for (size_t i = 0; i < 10; i++) {
1790 scoped_refptr<MockProfile> profile(
1791 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05001792 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08001793 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
1794 .WillOnce(Return(profile_paths.back()));
1795 AdoptProfile(manager(), profile);
1796 }
1797
1798 Error error;
1799 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
1800 EXPECT_TRUE(error.IsSuccess());
1801 EXPECT_EQ(profile_paths.size(), returned_paths.size());
1802 for (size_t i = 0; i < profile_paths.size(); i++) {
1803 EXPECT_EQ(profile_paths[i], returned_paths[i]);
1804 }
1805}
1806
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001807TEST_F(ManagerTest, AutoConnectOnRegister) {
1808 MockServiceRefPtr service = MakeAutoConnectableService();
1809 EXPECT_CALL(*service.get(), AutoConnect());
1810 manager()->RegisterService(service);
1811 dispatcher()->DispatchPendingEvents();
1812}
1813
1814TEST_F(ManagerTest, AutoConnectOnUpdate) {
1815 MockServiceRefPtr service1 = MakeAutoConnectableService();
1816 service1->set_priority(1);
1817 MockServiceRefPtr service2 = MakeAutoConnectableService();
1818 service2->set_priority(2);
1819 manager()->RegisterService(service1);
1820 manager()->RegisterService(service2);
1821 dispatcher()->DispatchPendingEvents();
1822
1823 EXPECT_CALL(*service1.get(), AutoConnect());
1824 EXPECT_CALL(*service2.get(), state())
1825 .WillRepeatedly(Return(Service::kStateFailure));
1826 EXPECT_CALL(*service2.get(), IsFailed())
1827 .WillRepeatedly(Return(true));
1828 EXPECT_CALL(*service2.get(), IsConnected())
1829 .WillRepeatedly(Return(false));
1830 manager()->UpdateService(service2);
1831 dispatcher()->DispatchPendingEvents();
1832}
1833
1834TEST_F(ManagerTest, AutoConnectOnDeregister) {
1835 MockServiceRefPtr service1 = MakeAutoConnectableService();
1836 service1->set_priority(1);
1837 MockServiceRefPtr service2 = MakeAutoConnectableService();
1838 service2->set_priority(2);
1839 manager()->RegisterService(service1);
1840 manager()->RegisterService(service2);
1841 dispatcher()->DispatchPendingEvents();
1842
1843 EXPECT_CALL(*service1.get(), AutoConnect());
1844 manager()->DeregisterService(service2);
1845 dispatcher()->DispatchPendingEvents();
1846}
1847
Paul Stewartc681fa02012-03-02 19:40:04 -08001848TEST_F(ManagerTest, RecheckPortal) {
1849 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
1850 .WillOnce(Return(false));
1851 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
1852 .WillOnce(Return(true));
1853 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
1854 .Times(0);
1855
1856 manager()->RegisterDevice(mock_devices_[0]);
1857 manager()->RegisterDevice(mock_devices_[1]);
1858 manager()->RegisterDevice(mock_devices_[2]);
1859
1860 manager()->RecheckPortal(NULL);
1861}
1862
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001863TEST_F(ManagerTest, GetDefaultService) {
1864 EXPECT_FALSE(manager()->GetDefaultService().get());
1865
1866 scoped_refptr<MockService> mock_service(
1867 new NiceMock<MockService>(control_interface(),
1868 dispatcher(),
1869 metrics(),
1870 manager()));
1871
1872 manager()->RegisterService(mock_service);
1873 EXPECT_FALSE(manager()->GetDefaultService().get());
1874
1875 scoped_refptr<MockConnection> mock_connection(
1876 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001877 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001878 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
1879
Paul Stewartce4ec192012-03-14 12:53:46 -07001880 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001881 manager()->DeregisterService(mock_service);
1882}
1883
Paul Stewart13ed2252012-03-21 12:52:46 -07001884TEST_F(ManagerTest, GetServiceWithGUID) {
1885 scoped_refptr<MockService> mock_service0(
1886 new NiceMock<MockService>(control_interface(),
1887 dispatcher(),
1888 metrics(),
1889 manager()));
1890
1891 scoped_refptr<MockService> mock_service1(
1892 new NiceMock<MockService>(control_interface(),
1893 dispatcher(),
1894 metrics(),
1895 manager()));
1896
Paul Stewartcb59fed2012-03-21 21:14:46 -07001897 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
1898 .Times(0);
1899 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
1900 .Times(0);
1901
Paul Stewart13ed2252012-03-21 12:52:46 -07001902 manager()->RegisterService(mock_service0);
1903 manager()->RegisterService(mock_service1);
1904
1905 const string kGUID0 = "GUID0";
1906 const string kGUID1 = "GUID1";
1907
1908 {
1909 Error error;
1910 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
1911 EXPECT_FALSE(error.IsSuccess());
1912 EXPECT_FALSE(service);
1913 }
1914
1915 KeyValueStore args;
1916 args.SetString(flimflam::kGuidProperty, kGUID1);
1917
1918 {
1919 Error error;
1920 ServiceRefPtr service = manager()->GetService(args, &error);
1921 EXPECT_EQ(Error::kInvalidArguments, error.type());
1922 EXPECT_FALSE(service);
1923 }
1924
1925 mock_service0->set_guid(kGUID0);
1926 mock_service1->set_guid(kGUID1);
1927
1928 {
1929 Error error;
1930 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
1931 EXPECT_TRUE(error.IsSuccess());
1932 EXPECT_EQ(mock_service0.get(), service.get());
1933 }
1934
1935 {
1936 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07001937 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
1938 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07001939 ServiceRefPtr service = manager()->GetService(args, &error);
1940 EXPECT_TRUE(error.IsSuccess());
1941 EXPECT_EQ(mock_service1.get(), service.get());
1942 }
1943
1944 manager()->DeregisterService(mock_service0);
1945 manager()->DeregisterService(mock_service1);
1946}
1947
Gary Morain028545d2012-04-07 14:55:52 -07001948
1949TEST_F(ManagerTest, CalculateStateOffline) {
1950 MockMetrics mock_metrics;
1951 manager()->set_metrics(&mock_metrics);
1952 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
1953 .Times(AnyNumber());
1954 scoped_refptr<MockService> mock_service0(
1955 new NiceMock<MockService>(control_interface(),
1956 dispatcher(),
1957 metrics(),
1958 manager()));
1959
1960 scoped_refptr<MockService> mock_service1(
1961 new NiceMock<MockService>(control_interface(),
1962 dispatcher(),
1963 metrics(),
1964 manager()));
1965
1966 EXPECT_CALL(*mock_service0.get(), IsConnected())
1967 .WillRepeatedly(Return(false));
1968 EXPECT_CALL(*mock_service1.get(), IsConnected())
1969 .WillRepeatedly(Return(false));
1970
1971 manager()->RegisterService(mock_service0);
1972 manager()->RegisterService(mock_service1);
1973
1974 EXPECT_EQ("offline", manager()->CalculateState(NULL));
1975
1976 manager()->DeregisterService(mock_service0);
1977 manager()->DeregisterService(mock_service1);
1978}
1979
1980TEST_F(ManagerTest, CalculateStateOnline) {
1981 MockMetrics mock_metrics;
1982 manager()->set_metrics(&mock_metrics);
1983 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
1984 .Times(AnyNumber());
1985 scoped_refptr<MockService> mock_service0(
1986 new NiceMock<MockService>(control_interface(),
1987 dispatcher(),
1988 metrics(),
1989 manager()));
1990
1991 scoped_refptr<MockService> mock_service1(
1992 new NiceMock<MockService>(control_interface(),
1993 dispatcher(),
1994 metrics(),
1995 manager()));
1996
1997 EXPECT_CALL(*mock_service0.get(), IsConnected())
1998 .WillRepeatedly(Return(false));
1999 EXPECT_CALL(*mock_service1.get(), IsConnected())
2000 .WillRepeatedly(Return(true));
2001 EXPECT_CALL(*mock_service0.get(), state())
2002 .WillRepeatedly(Return(Service::kStateIdle));
2003 EXPECT_CALL(*mock_service1.get(), state())
2004 .WillRepeatedly(Return(Service::kStateConnected));
2005
2006 manager()->RegisterService(mock_service0);
2007 manager()->RegisterService(mock_service1);
2008
2009 EXPECT_EQ("online", manager()->CalculateState(NULL));
2010
2011 manager()->DeregisterService(mock_service0);
2012 manager()->DeregisterService(mock_service1);
2013}
2014
Chris Masone9be4a9d2011-05-16 15:44:09 -07002015} // namespace shill