blob: afa2eda51981656009cac73b469bd07627d4e1c3 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone9be4a9d2011-05-16 15:44:09 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07004
5#include "shill/manager.h"
6
Jason Glasgowdf7c5532012-05-14 14:41:45 -04007#include <map>
Chris Masone6791a432011-07-12 13:23:19 -07008#include <set>
9
Chris Masone9be4a9d2011-05-16 15:44:09 -070010#include <glib.h>
11
Paul Stewarte73d05c2012-03-29 16:26:05 -070012#include <base/file_util.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070013#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050014#include <base/scoped_temp_dir.h>
15#include <base/stl_util.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070016#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070018#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070019#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070020
mukesh agrawal32399322011-09-01 10:53:43 -070021#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070022#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070023#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070024#include "shill/glib.h"
25#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070026#include "shill/key_value_store.h"
mukesh agrawal32399322011-09-01 10:53:43 -070027#include "shill/mock_adaptors.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080028#include "shill/mock_connection.h"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070030#include "shill/mock_device.h"
Paul Stewartc1dec4d2011-12-08 15:25:28 -080031#include "shill/mock_device_info.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070032#include "shill/mock_glib.h"
Thieu Lea20cbc22012-01-09 22:01:43 +000033#include "shill/mock_metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070034#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070035#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070036#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070037#include "shill/mock_wifi.h"
Paul Stewart7f61e522012-03-22 11:13:45 -070038#include "shill/mock_wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070040#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041#include "shill/wifi_service.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020042#include "shill/wimax_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043
44using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070045using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070046using std::string;
47using std::vector;
48
Chris Masone9be4a9d2011-05-16 15:44:09 -070049namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070050using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070051using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080052using ::testing::ContainerEq;
Paul Stewart7f5ad572012-06-04 15:18:54 -070053using ::testing::DoAll;
Paul Stewarte2bad7c2012-03-14 08:55:33 -070054using ::testing::InSequence;
Paul Stewart22aa71b2011-09-16 12:15:11 -070055using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070056using ::testing::NiceMock;
57using ::testing::Return;
Paul Stewartce4ec192012-03-14 12:53:46 -070058using ::testing::ReturnRef;
Paul Stewart7f5ad572012-06-04 15:18:54 -070059using ::testing::SaveArg;
Gaurav Shah435de2c2011-11-17 19:01:07 -080060using ::testing::StrEq;
Paul Stewart3d9bcf52011-12-12 15:02:22 -080061using ::testing::StrictMock;
Chris Masone9d779932011-08-25 16:33:41 -070062using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070063
Chris Masone3bd3c8c2011-06-13 08:20:26 -070064class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070065 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070066 ManagerTest()
Paul Stewart22aa71b2011-09-16 12:15:11 -070067 : mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080069 metrics(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070070 manager(),
71 "wifi0",
72 "addr4",
Paul Stewartc1dec4d2011-12-08 15:25:28 -080073 4)),
74 device_info_(new NiceMock<MockDeviceInfo>(
75 control_interface(),
76 reinterpret_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080077 reinterpret_cast<Metrics*>(NULL),
Paul Stewartc1dec4d2011-12-08 15:25:28 -080078 reinterpret_cast<Manager*>(NULL))),
79 manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) {
Paul Stewart22aa71b2011-09-16 12:15:11 -070080 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
81 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080082 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070083 manager(),
84 "null0",
85 "addr0",
86 0));
87 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
88 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080089 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070090 manager(),
91 "null1",
92 "addr1",
93 1));
94 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
95 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080096 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -070097 manager(),
98 "null2",
99 "addr2",
100 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800101 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
102 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800103 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800104 manager(),
105 "null3",
106 "addr3",
107 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700108 manager()->connect_profiles_to_rpc_ = false;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800109
110 // Replace the manager's adaptor with a quieter one, and one
111 // we can do EXPECT*() against. Passes ownership.
112 manager()->adaptor_.reset(manager_adaptor_);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700113 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -0700115
Paul Stewartfdd16072011-09-16 12:41:35 -0700116 bool IsDeviceRegistered(const DeviceRefPtr &device,
117 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -0700118 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -0700119 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -0700120 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700121 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700122 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700123
Paul Stewarta849a3d2011-11-03 05:54:09 -0700124 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
125 manager->profiles_.push_back(profile);
126 }
127
Paul Stewart75225512012-01-26 22:51:33 -0800128 ProfileRefPtr GetEphemeralProfile(Manager *manager) {
129 return manager->ephemeral_profile_;
130 }
131
Chris Masone6515aab2011-10-12 16:19:09 -0700132 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
133 Profile::Identifier id("rather", "irrelevant");
134 scoped_ptr<Profile> profile(new Profile(control_interface(),
135 manager,
136 id,
137 "",
138 false));
139 FilePath final_path(storage_path());
140 final_path = final_path.Append("test.profile");
141 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
142 storage->set_path(final_path);
143 if (!storage->Open())
144 return NULL;
145 profile->set_storage(storage.release()); // Passes ownership.
146 return profile.release();
147 }
148
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700149 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
150 const string &profile_identifier,
151 const string &service_name) {
152 GLib glib;
153 KeyFileStore store(&glib);
154 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
155 return store.Open() &&
156 store.SetString(service_name, "rather", "irrelevant") &&
157 store.Close();
158 }
159
160 Error::Type TestCreateProfile(Manager *manager, const string &name) {
161 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800162 string path;
163 manager->CreateProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700164 return error.type();
165 }
166
167 Error::Type TestPopAnyProfile(Manager *manager) {
168 Error error;
169 manager->PopAnyProfile(&error);
170 return error.type();
171 }
172
173 Error::Type TestPopProfile(Manager *manager, const string &name) {
174 Error error;
175 manager->PopProfile(name, &error);
176 return error.type();
177 }
178
179 Error::Type TestPushProfile(Manager *manager, const string &name) {
180 Error error;
Paul Stewart19c871d2011-12-15 16:10:13 -0800181 string path;
182 manager->PushProfile(name, &path, &error);
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700183 return error.type();
184 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000185
Paul Stewartcb3eb892012-06-07 14:24:46 -0700186 void AddMockProfileToManager(Manager *manager) {
187 scoped_refptr<MockProfile> profile(
188 new MockProfile(control_interface(), manager, ""));
189 EXPECT_CALL(*profile, GetRpcIdentifier())
190 .WillRepeatedly(Return("/"));
191 AdoptProfile(manager, profile);
192 }
193
Paul Stewartdfa46052012-06-26 09:44:14 -0700194 void CompleteServiceSort() {
195 EXPECT_FALSE(manager()->sort_services_task_.IsCancelled());
196 dispatcher()->DispatchPendingEvents();
197 EXPECT_TRUE(manager()->sort_services_task_.IsCancelled());
198 }
199
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700200 protected:
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000201 typedef scoped_refptr<MockService> MockServiceRefPtr;
202
203 MockServiceRefPtr MakeAutoConnectableService() {
204 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
205 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800206 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000207 manager());
208 service->MakeFavorite();
209 service->set_connectable(true);
210 return service;
211 }
212
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700213 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700214 vector<scoped_refptr<MockDevice> > mock_devices_;
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800215 scoped_ptr<MockDeviceInfo> device_info_;
216
217 // This pointer is owned by the manager, and only tracked here for EXPECT*()
218 ManagerMockAdaptor *manager_adaptor_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700219};
220
Paul Stewart22aa71b2011-09-16 12:15:11 -0700221bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
Paul Stewartdfa46052012-06-26 09:44:14 -0700222 if (!manager()->sort_services_task_.IsCancelled()) {
223 manager()->SortServicesTask();
224 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700225 return (svc0.get() == manager()->services_[0].get() &&
226 svc1.get() == manager()->services_[1].get());
227}
228
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700229TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700230 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
231 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700232}
233
Chris Masone9be4a9d2011-05-16 15:44:09 -0700234TEST_F(ManagerTest, DeviceRegistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700235 ON_CALL(*mock_devices_[0].get(), technology())
236 .WillByDefault(Return(Technology::kEthernet));
237 ON_CALL(*mock_devices_[1].get(), technology())
238 .WillByDefault(Return(Technology::kWifi));
239 ON_CALL(*mock_devices_[2].get(), technology())
240 .WillByDefault(Return(Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700241
Paul Stewart22aa71b2011-09-16 12:15:11 -0700242 manager()->RegisterDevice(mock_devices_[0]);
243 manager()->RegisterDevice(mock_devices_[1]);
244 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700245
Paul Stewart22aa71b2011-09-16 12:15:11 -0700246 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
247 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
248 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700249}
250
Paul Stewarta41e38d2011-11-11 07:47:29 -0800251TEST_F(ManagerTest, DeviceRegistrationAndStart) {
252 manager()->running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500253 mock_devices_[0]->enabled_persistent_ = true;
254 mock_devices_[1]->enabled_persistent_ = false;
255 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800256 .Times(1);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500257 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_))
Paul Stewarta41e38d2011-11-11 07:47:29 -0800258 .Times(0);
259 manager()->RegisterDevice(mock_devices_[0]);
260 manager()->RegisterDevice(mock_devices_[1]);
261}
262
263TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
264 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
265 DeviceRefPtr device_ref(mock_devices_[0].get());
266 AdoptProfile(manager(), profile); // Passes ownership.
267 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
268 EXPECT_CALL(*profile, Save());
269 manager()->RegisterDevice(mock_devices_[0]);
270}
271
Chris Masone9be4a9d2011-05-16 15:44:09 -0700272TEST_F(ManagerTest, DeviceDeregistration) {
Joshua Krollda798622012-06-05 12:30:48 -0700273 ON_CALL(*mock_devices_[0].get(), technology())
274 .WillByDefault(Return(Technology::kEthernet));
275 ON_CALL(*mock_devices_[1].get(), technology())
276 .WillByDefault(Return(Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700277
Gaurav Shah435de2c2011-11-17 19:01:07 -0800278 manager()->RegisterDevice(mock_devices_[0]);
279 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700280
Paul Stewart22aa71b2011-09-16 12:15:11 -0700281 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
282 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700283
Eric Shienbrood9a245532012-03-07 14:20:39 -0500284 EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800285 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700286 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700287
Eric Shienbrood9a245532012-03-07 14:20:39 -0500288 EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800289 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700290 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700291}
292
293TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700294 // It's much easier and safer to use a real GLib for this test.
295 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700296 Manager manager(control_interface(),
297 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800298 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700299 &glib,
300 run_path(),
301 storage_path(),
302 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700303 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
304 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700305 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700306
Chris Masone9be4a9d2011-05-16 15:44:09 -0700307 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700308 new NiceMock<MockService>(control_interface(),
309 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800310 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700311 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700312 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700313 new NiceMock<MockService>(control_interface(),
314 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800315 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700316 &manager));
Paul Stewartce4ec192012-03-14 12:53:46 -0700317
mukesh agrawal51a7e932011-07-27 16:18:26 -0700318 string service1_name(mock_service->UniqueName());
319 string service2_name(mock_service2->UniqueName());
320
321 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
322 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700323 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700324 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700325 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700326 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700327 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700328
Chris Masone9d779932011-08-25 16:33:41 -0700329 manager.RegisterService(mock_service);
330 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700331
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800332 Error error;
333 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700334 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700335 EXPECT_EQ(2, ids.size());
336 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
337 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700338
Chris Masone9d779932011-08-25 16:33:41 -0700339 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
340 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
341
342 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700343}
344
Chris Masone6515aab2011-10-12 16:19:09 -0700345TEST_F(ManagerTest, RegisterKnownService) {
346 // It's much easier and safer to use a real GLib for this test.
347 GLib glib;
348 Manager manager(control_interface(),
349 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800350 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700351 &glib,
352 run_path(),
353 storage_path(),
354 string());
355 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
356 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700357 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700358 {
359 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
360 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800361 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700362 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700363 ASSERT_TRUE(profile->AdoptService(service1));
364 ASSERT_TRUE(profile->ContainsService(service1));
365 } // Force destruction of service1.
366
367 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
368 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800369 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700370 &manager));
371 manager.RegisterService(service2);
372 EXPECT_EQ(service2->profile().get(), profile.get());
373 manager.Stop();
374}
375
376TEST_F(ManagerTest, RegisterUnknownService) {
377 // It's much easier and safer to use a real GLib for this test.
378 GLib glib;
379 Manager manager(control_interface(),
380 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800381 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700382 &glib,
383 run_path(),
384 storage_path(),
385 string());
386 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
387 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700388 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700389 {
390 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
391 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800392 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700393 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700394 ASSERT_TRUE(profile->AdoptService(service1));
395 ASSERT_TRUE(profile->ContainsService(service1));
396 } // Force destruction of service1.
397 scoped_refptr<MockService> mock_service2(
398 new NiceMock<MockService>(control_interface(),
399 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800400 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700401 &manager));
402 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
403 .WillRepeatedly(Return(mock_service2->UniqueName()));
404 manager.RegisterService(mock_service2);
405 EXPECT_NE(mock_service2->profile().get(), profile.get());
406 manager.Stop();
407}
408
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000409TEST_F(ManagerTest, DeregisterUnregisteredService) {
410 // WiFi assumes that it can deregister a service that is not
411 // registered. (E.g. a hidden service can be deregistered when it
412 // loses its last endpoint, and again when WiFi is Stop()-ed.)
413 //
414 // So test that doing so doesn't cause a crash.
415 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
416 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800417 metrics(),
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000418 manager());
419 manager()->DeregisterService(service);
420}
421
Chris Masonea8a2c252011-06-27 22:16:30 -0700422TEST_F(ManagerTest, GetProperties) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700423 AddMockProfileToManager(manager());
Chris Masonea8a2c252011-06-27 22:16:30 -0700424 map<string, ::DBus::Variant> props;
425 Error error(Error::kInvalidProperty, "");
426 {
427 ::DBus::Error dbus_error;
428 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700429 manager()->mutable_store()->SetStringProperty(
430 flimflam::kCheckPortalListProperty,
431 expected,
432 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700433 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700434 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
435 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
436 expected);
437 }
438 {
439 ::DBus::Error dbus_error;
440 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700441 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
442 expected,
443 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700444 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700445 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
446 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
447 expected);
448 }
449}
450
Chris Masone3c3f6a12011-07-01 10:01:41 -0700451TEST_F(ManagerTest, GetDevicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700452 AddMockProfileToManager(manager());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800453 manager()->RegisterDevice(mock_devices_[0]);
454 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700455 {
456 map<string, ::DBus::Variant> props;
457 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700458 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700459 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
Paul Stewartcb3eb892012-06-07 14:24:46 -0700460 vector < ::DBus::Path> devices =
461 props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>();
Chris Masone3c3f6a12011-07-01 10:01:41 -0700462 EXPECT_EQ(2, devices.size());
463 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700464}
465
mukesh agrawal2366eed2012-03-20 18:21:50 -0700466TEST_F(ManagerTest, GetServicesProperty) {
Paul Stewartcb3eb892012-06-07 14:24:46 -0700467 AddMockProfileToManager(manager());
mukesh agrawal2366eed2012-03-20 18:21:50 -0700468 map<string, ::DBus::Variant> props;
469 ::DBus::Error dbus_error;
470 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
471 map<string, ::DBus::Variant>::const_iterator prop =
472 props.find(flimflam::kServicesProperty);
473 ASSERT_FALSE(prop == props.end());
474 const ::DBus::Variant &variant = prop->second;
475 ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature()));
476}
477
Chris Masone6791a432011-07-12 13:23:19 -0700478TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700479 Manager manager(control_interface(),
480 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800481 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700482 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700483 run_path(),
484 storage_path(),
485 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700486 scoped_refptr<MockService> s2(new MockService(control_interface(),
487 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800488 metrics(),
Chris Masone6515aab2011-10-12 16:19:09 -0700489 &manager));
490 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700491 {
Chris Masone6515aab2011-10-12 16:19:09 -0700492 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700493 ProfileRefPtr profile(
494 new Profile(control_interface(), &manager, id, "", false));
495 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700496 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
Chris Masone6515aab2011-10-12 16:19:09 -0700497 .WillRepeatedly(Return(true));
498 EXPECT_CALL(*storage, Flush())
499 .Times(AnyNumber())
500 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700501 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700502 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700503 }
Chris Masone6515aab2011-10-12 16:19:09 -0700504 // Create a profile that already has |s2| in it.
505 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
Paul Stewart451aa7f2012-04-11 19:07:58 -0700506 EXPECT_TRUE(profile->AdoptService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700507
Chris Masone6515aab2011-10-12 16:19:09 -0700508 // Now, move the Service |s2| to another profile.
509 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
510 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700511
512 // Force destruction of the original Profile, to ensure that the Service
513 // is kept alive and populated with data.
514 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700515 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700516 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700517}
518
Paul Stewart7f61e522012-03-22 11:13:45 -0700519TEST_F(ManagerTest, LookupProfileByRpcIdentifier) {
520 scoped_refptr<MockProfile> mock_profile(
521 new MockProfile(control_interface(), manager(), ""));
522 const string kProfileName("profile0");
523 EXPECT_CALL(*mock_profile, GetRpcIdentifier())
524 .WillRepeatedly(Return(kProfileName));
525 AdoptProfile(manager(), mock_profile);
526
527 EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo"));
528 ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName);
529 EXPECT_EQ(mock_profile.get(), profile.get());
530}
531
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800532TEST_F(ManagerTest, SetProfileForService) {
533 scoped_refptr<MockProfile> profile0(
534 new MockProfile(control_interface(), manager(), ""));
535 string profile_name0("profile0");
536 EXPECT_CALL(*profile0, GetRpcIdentifier())
537 .WillRepeatedly(Return(profile_name0));
538 AdoptProfile(manager(), profile0);
539 scoped_refptr<MockService> service(new MockService(control_interface(),
540 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800541 metrics(),
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800542 manager()));
543 service->set_profile(profile0);
544
545 {
546 Error error;
547 manager()->SetProfileForService(service, "foo", &error);
548 EXPECT_EQ(Error::kInvalidArguments, error.type());
Paul Stewart7f61e522012-03-22 11:13:45 -0700549 EXPECT_EQ("Unknown Profile foo requested for Service", error.message());
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800550 }
551
552 {
553 Error error;
554 manager()->SetProfileForService(service, profile_name0, &error);
555 EXPECT_EQ(Error::kInvalidArguments, error.type());
556 EXPECT_EQ("Service is already connected to this profile", error.message());
557 }
558
559 scoped_refptr<MockProfile> profile1(
560 new MockProfile(control_interface(), manager(), ""));
561 string profile_name1("profile1");
562 EXPECT_CALL(*profile1, GetRpcIdentifier())
563 .WillRepeatedly(Return(profile_name1));
564 AdoptProfile(manager(), profile1);
565
566 {
567 Error error;
568 EXPECT_CALL(*profile1, AdoptService(_))
569 .WillOnce(Return(true));
570 EXPECT_CALL(*profile0, AbandonService(_))
571 .WillOnce(Return(true));
572 manager()->SetProfileForService(service, profile_name1, &error);
573 EXPECT_TRUE(error.IsSuccess());
574 }
575}
576
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700577TEST_F(ManagerTest, CreateProfile) {
578 // It's much easier to use real Glib here since we want the storage
579 // side-effects.
580 GLib glib;
581 ScopedTempDir temp_dir;
582 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
583
584 Manager manager(control_interface(),
585 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800586 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700587 &glib,
588 run_path(),
589 storage_path(),
590 temp_dir.path().value());
591
592 // Invalid name should be rejected.
593 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
594
Paul Stewartd0a3b812012-03-28 22:48:22 -0700595 // A profile with invalid characters in it should similarly be rejected.
596 EXPECT_EQ(Error::kInvalidArguments,
597 TestCreateProfile(&manager, "valid_profile"));
598
599 // We should be able to create a machine profile.
600 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700601
Gary Morainb672d352012-04-25 09:19:06 -0700602 // We should succeed in creating a valid user profile. Verify the returned
603 // path.
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700604 const char kProfile[] = "~user/profile";
Gary Morainb672d352012-04-25 09:19:06 -0700605 {
606 Error error;
607 string path;
608 manager.CreateProfile(kProfile, &path, &error);
609 EXPECT_EQ(Error::kSuccess, error.type());
610 EXPECT_EQ("/profile_rpc", path);
611 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700612
613 // We should fail in creating it a second time (already exists).
614 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
615}
616
617TEST_F(ManagerTest, PushPopProfile) {
618 // It's much easier to use real Glib in creating a Manager for this
619 // test here since we want the storage side-effects.
620 GLib glib;
621 ScopedTempDir temp_dir;
622 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
623 Manager manager(control_interface(),
624 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800625 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700626 &glib,
627 run_path(),
628 storage_path(),
629 temp_dir.path().value());
630
631 // Pushing an invalid profile should fail.
632 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
633
Paul Stewartd0a3b812012-03-28 22:48:22 -0700634 // Pushing a default profile that does not exist should fail.
635 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default"));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700636
637 const char kProfile0[] = "~user/profile0";
638 const char kProfile1[] = "~user/profile1";
639
640 // Create a couple of profiles.
641 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
642 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
643
644 // Push these profiles on the stack.
645 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
646 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
647
648 // Pushing a profile a second time should fail.
649 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
650 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
651
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800652 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700653 // Active profile should be the last one we pushed.
Paul Stewart1b253142012-01-26 14:05:52 -0800654 EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700655
656 // Make sure a profile name that doesn't exist fails.
657 const char kProfile2Id[] = "profile2";
658 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
659 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
660
661 // Create a new service, with a specific storage name.
662 scoped_refptr<MockService> service(
663 new NiceMock<MockService>(control_interface(),
664 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800665 metrics(),
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700666 &manager));
667 const char kServiceName[] = "service_storage_name";
668 EXPECT_CALL(*service.get(), GetStorageIdentifier())
669 .WillRepeatedly(Return(kServiceName));
670 EXPECT_CALL(*service.get(), Load(_))
671 .WillRepeatedly(Return(true));
672
673 // Add this service to the manager -- it should end up in the ephemeral
674 // profile.
675 manager.RegisterService(service);
Paul Stewart75225512012-01-26 22:51:33 -0800676 ASSERT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700677
678 // Create storage for a profile that contains the service storage name.
679 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
680 kServiceName));
681
682 // When we push the profile, the service should move away from the
683 // ephemeral profile to this new profile since it has an entry for
684 // this service.
685 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
Paul Stewart75225512012-01-26 22:51:33 -0800686 EXPECT_NE(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700687 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
688
689 // Insert another profile that should supersede ownership of the service.
690 const char kProfile3Id[] = "profile3";
691 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
692 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
693 kServiceName));
694 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
695 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
696
697 // Popping an invalid profile name should fail.
698 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
699
700 // Popping an profile that is not at the top of the stack should fail.
701 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
702
703 // Popping the top profile should succeed.
704 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
705
706 // Moreover the service should have switched profiles to profile 2.
707 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
708
709 // Popping the top profile should succeed.
710 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
711
712 // The service should now revert to the ephemeral profile.
Paul Stewart75225512012-01-26 22:51:33 -0800713 EXPECT_EQ(GetEphemeralProfile(&manager), service->profile());
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700714
715 // Pop the remaining two services off the stack.
716 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
717 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
718
719 // Next pop should fail with "stack is empty".
720 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
Paul Stewartd0a3b812012-03-28 22:48:22 -0700721
722 const char kMachineProfile0[] = "machineprofile0";
723 const char kMachineProfile1[] = "machineprofile1";
724 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0));
725 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1));
726
727 // Should be able to push a machine profile.
728 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0));
729
730 // Should be able to push a user profile atop a machine profile.
731 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
732
733 // Pushing a system-wide profile on top of a user profile should fail.
734 EXPECT_EQ(Error::kInvalidArguments,
735 TestPushProfile(&manager, kMachineProfile1));
736
737 // However if we pop the user profile, we should be able stack another
738 // machine profile on.
739 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
740 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700741}
742
Paul Stewarte73d05c2012-03-29 16:26:05 -0700743TEST_F(ManagerTest, RemoveProfile) {
744 // It's much easier to use real Glib in creating a Manager for this
745 // test here since we want the storage side-effects.
746 GLib glib;
747 ScopedTempDir temp_dir;
748 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
749 Manager manager(control_interface(),
750 dispatcher(),
751 metrics(),
752 &glib,
753 run_path(),
754 storage_path(),
755 temp_dir.path().value());
756
757 const char kProfile0[] = "profile0";
758 FilePath profile_path(
759 FilePath(storage_path()).Append(string(kProfile0) + ".profile"));
760
761 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
762 ASSERT_TRUE(file_util::PathExists(profile_path));
763
764 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
765
766 // Remove should fail since the profile is still on the stack.
767 {
768 Error error;
769 manager.RemoveProfile(kProfile0, &error);
770 EXPECT_EQ(Error::kInvalidArguments, error.type());
771 }
772
773 // Profile path should still exist.
774 EXPECT_TRUE(file_util::PathExists(profile_path));
775
776 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
777
778 // This should succeed now that the profile is off the stack.
779 {
780 Error error;
781 manager.RemoveProfile(kProfile0, &error);
782 EXPECT_EQ(Error::kSuccess, error.type());
783 }
784
785 // Profile path should no longer exist.
786 EXPECT_FALSE(file_util::PathExists(profile_path));
787
788 // Another remove succeeds, due to a foible in file_util::Delete --
789 // it is not an error to delete a file that does not exist.
790 {
791 Error error;
792 manager.RemoveProfile(kProfile0, &error);
793 EXPECT_EQ(Error::kSuccess, error.type());
794 }
795
796 // Let's create an error case that will "work". Create a non-empty
797 // directory in the place of the profile pathname.
798 ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo")));
799 {
800 Error error;
801 manager.RemoveProfile(kProfile0, &error);
802 EXPECT_EQ(Error::kOperationFailed, error.type());
803 }
804}
805
Paul Stewart75225512012-01-26 22:51:33 -0800806// Use this matcher instead of passing RefPtrs directly into the arguments
807// of EXPECT_CALL() because otherwise we may create un-cleaned-up references at
808// system teardown.
809MATCHER_P(IsRefPtrTo, ref_address, "") {
810 return arg.get() == ref_address;
811}
812
813TEST_F(ManagerTest, HandleProfileEntryDeletion) {
814 MockServiceRefPtr s_not_in_profile(
815 new NiceMock<MockService>(control_interface(),
816 dispatcher(),
817 metrics(),
818 manager()));
819 MockServiceRefPtr s_not_in_group(
820 new NiceMock<MockService>(control_interface(),
821 dispatcher(),
822 metrics(),
823 manager()));
824 MockServiceRefPtr s_configure_fail(
825 new NiceMock<MockService>(control_interface(),
826 dispatcher(),
827 metrics(),
828 manager()));
829 MockServiceRefPtr s_configure_succeed(
830 new NiceMock<MockService>(control_interface(),
831 dispatcher(),
832 metrics(),
833 manager()));
834
835 string entry_name("entry_name");
836 EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0);
837 EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier())
838 .WillRepeatedly(Return("not_entry_name"));
839 EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier())
840 .WillRepeatedly(Return(entry_name));
841 EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier())
842 .WillRepeatedly(Return(entry_name));
843
844 manager()->RegisterService(s_not_in_profile);
845 manager()->RegisterService(s_not_in_group);
846 manager()->RegisterService(s_configure_fail);
847 manager()->RegisterService(s_configure_succeed);
848
849 scoped_refptr<MockProfile> profile0(
850 new StrictMock<MockProfile>(control_interface(), manager(), ""));
851 scoped_refptr<MockProfile> profile1(
852 new StrictMock<MockProfile>(control_interface(), manager(), ""));
853
854 s_not_in_group->set_profile(profile1);
855 s_configure_fail->set_profile(profile1);
856 s_configure_succeed->set_profile(profile1);
857
858 AdoptProfile(manager(), profile0);
859 AdoptProfile(manager(), profile1);
860
861 // No services are a member of this profile.
862 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name));
863
864 // No services that are members of this profile have this entry name.
865 EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, ""));
866
867 // Only services that are members of the profile and group will be abandoned.
868 EXPECT_CALL(*profile1.get(),
869 AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
870 EXPECT_CALL(*profile1.get(),
871 AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
872 EXPECT_CALL(*profile1.get(),
873 AbandonService(IsRefPtrTo(s_configure_fail.get())))
874 .WillOnce(Return(true));
875 EXPECT_CALL(*profile1.get(),
876 AbandonService(IsRefPtrTo(s_configure_succeed.get())))
877 .WillOnce(Return(true));
878
879 // Never allow services to re-join profile1.
880 EXPECT_CALL(*profile1.get(), ConfigureService(_))
881 .WillRepeatedly(Return(false));
882
883 // Only allow one of the members of the profile and group to successfully
884 // join profile0.
885 EXPECT_CALL(*profile0.get(),
886 ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0);
887 EXPECT_CALL(*profile0.get(),
888 ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0);
889 EXPECT_CALL(*profile0.get(),
890 ConfigureService(IsRefPtrTo(s_configure_fail.get())))
891 .WillOnce(Return(false));
892 EXPECT_CALL(*profile0.get(),
893 ConfigureService(IsRefPtrTo(s_configure_succeed.get())))
894 .WillOnce(Return(true));
895
896 // Expect the failed-to-configure service to have Unload() called on it.
897 EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0);
898 EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0);
899 EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1);
900 EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0);
901
902 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name));
903
904 EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get());
905 EXPECT_EQ(profile1, s_not_in_group->profile());
906 EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile());
907
908 // Since we are using a MockProfile, the profile does not actually change,
909 // since ConfigureService was not actually called on the service.
910 EXPECT_EQ(profile1, s_configure_succeed->profile());
911}
912
Paul Stewart65512e12012-03-26 18:01:08 -0700913TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) {
914 MockServiceRefPtr s_will_remove0(
915 new NiceMock<MockService>(control_interface(),
916 dispatcher(),
917 metrics(),
918 manager()));
919 MockServiceRefPtr s_will_remove1(
920 new NiceMock<MockService>(control_interface(),
921 dispatcher(),
922 metrics(),
923 manager()));
924 MockServiceRefPtr s_will_not_remove0(
925 new NiceMock<MockService>(control_interface(),
926 dispatcher(),
927 metrics(),
928 manager()));
929 MockServiceRefPtr s_will_not_remove1(
930 new NiceMock<MockService>(control_interface(),
931 dispatcher(),
932 metrics(),
933 manager()));
934
935 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
936 .Times(4); // Once for each registration.
937
938 string entry_name("entry_name");
939 EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier())
940 .WillRepeatedly(Return(entry_name));
941 EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier())
942 .WillRepeatedly(Return(entry_name));
943 EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier())
944 .WillRepeatedly(Return(entry_name));
945 EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier())
946 .WillRepeatedly(Return(entry_name));
947
948 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -0700949 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -0700950 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -0700951 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -0700952 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -0700953 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -0700954 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -0700955 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -0700956
957 // One for each service added above.
958 ASSERT_EQ(4, manager()->services_.size());
959
960 scoped_refptr<MockProfile> profile(
961 new StrictMock<MockProfile>(control_interface(), manager(), ""));
962
963 s_will_remove0->set_profile(profile);
964 s_will_remove1->set_profile(profile);
965 s_will_not_remove0->set_profile(profile);
966 s_will_not_remove1->set_profile(profile);
967
968 AdoptProfile(manager(), profile);
969
970 // Deny any of the services re-entry to the profile.
971 EXPECT_CALL(*profile, ConfigureService(_))
972 .WillRepeatedly(Return(false));
973
974 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0)))
975 .WillOnce(Return(true));
976 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1)))
977 .WillOnce(Return(true));
978 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0)))
979 .WillOnce(Return(true));
980 EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1)))
981 .WillOnce(Return(true));
982
983 EXPECT_CALL(*s_will_remove0, Unload())
984 .WillOnce(Return(true));
985 EXPECT_CALL(*s_will_remove1, Unload())
986 .WillOnce(Return(true));
987 EXPECT_CALL(*s_will_not_remove0, Unload())
988 .WillOnce(Return(false));
989 EXPECT_CALL(*s_will_not_remove1, Unload())
990 .WillOnce(Return(false));
991
992
993 // This will cause all the profiles to be unloaded.
994 EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name));
995
996 // 2 of the 4 services added above should have been unregistered and
997 // removed, leaving 2.
998 EXPECT_EQ(2, manager()->services_.size());
999 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1000 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1001}
1002
1003TEST_F(ManagerTest, PopProfileWithUnload) {
1004 MockServiceRefPtr s_will_remove0(
1005 new NiceMock<MockService>(control_interface(),
1006 dispatcher(),
1007 metrics(),
1008 manager()));
1009 MockServiceRefPtr s_will_remove1(
1010 new NiceMock<MockService>(control_interface(),
1011 dispatcher(),
1012 metrics(),
1013 manager()));
1014 MockServiceRefPtr s_will_not_remove0(
1015 new NiceMock<MockService>(control_interface(),
1016 dispatcher(),
1017 metrics(),
1018 manager()));
1019 MockServiceRefPtr s_will_not_remove1(
1020 new NiceMock<MockService>(control_interface(),
1021 dispatcher(),
1022 metrics(),
1023 manager()));
1024
1025 EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL))
1026 .Times(5); // Once for each registration, and one after profile pop.
1027
1028 manager()->RegisterService(s_will_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001029 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001030 manager()->RegisterService(s_will_not_remove0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001031 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001032 manager()->RegisterService(s_will_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001033 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001034 manager()->RegisterService(s_will_not_remove1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001035 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001036
1037 // One for each service added above.
1038 ASSERT_EQ(4, manager()->services_.size());
1039
1040 scoped_refptr<MockProfile> profile0(
1041 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1042 scoped_refptr<MockProfile> profile1(
1043 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1044
1045 s_will_remove0->set_profile(profile1);
1046 s_will_remove1->set_profile(profile1);
1047 s_will_not_remove0->set_profile(profile1);
1048 s_will_not_remove1->set_profile(profile1);
1049
1050 AdoptProfile(manager(), profile0);
1051 AdoptProfile(manager(), profile1);
1052
1053 // Deny any of the services entry to profile0, so they will all be unloaded.
1054 EXPECT_CALL(*profile0, ConfigureService(_))
1055 .WillRepeatedly(Return(false));
1056
1057 EXPECT_CALL(*s_will_remove0, Unload())
1058 .WillOnce(Return(true));
1059 EXPECT_CALL(*s_will_remove1, Unload())
1060 .WillOnce(Return(true));
1061 EXPECT_CALL(*s_will_not_remove0, Unload())
1062 .WillOnce(Return(false));
1063 EXPECT_CALL(*s_will_not_remove1, Unload())
1064 .WillOnce(Return(false));
1065
1066 // This will pop profile1, which should cause all our profiles to unload.
1067 manager()->PopProfileInternal();
Paul Stewartdfa46052012-06-26 09:44:14 -07001068 CompleteServiceSort();
Paul Stewart65512e12012-03-26 18:01:08 -07001069
1070 // 2 of the 4 services added above should have been unregistered and
1071 // removed, leaving 2.
1072 EXPECT_EQ(2, manager()->services_.size());
1073 EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get());
1074 EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get());
1075}
1076
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001077TEST_F(ManagerTest, SetProperty) {
Chris Masonea8a2c252011-06-27 22:16:30 -07001078 {
1079 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001080 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1081 flimflam::kOfflineModeProperty,
1082 PropertyStoreTest::kBoolV,
1083 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001084 }
1085 {
1086 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001087 EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1088 flimflam::kCountryProperty,
1089 PropertyStoreTest::kStringV,
1090 &error));
Chris Masonea8a2c252011-06-27 22:16:30 -07001091 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001092 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001093 {
1094 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001095 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1096 flimflam::kCountryProperty,
1097 PropertyStoreTest::kBoolV,
1098 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001099 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001100 }
1101 {
1102 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001103 EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(),
1104 flimflam::kOfflineModeProperty,
1105 PropertyStoreTest::kStringV,
1106 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001107 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001108 }
Chris Masoneb925cc82011-06-22 15:39:57 -07001109 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -07001110 {
1111 ::DBus::Error error;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001112 EXPECT_FALSE(DBusAdaptor::SetProperty(
mukesh agrawalde29fa82011-09-16 16:16:36 -07001113 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -07001114 flimflam::kEnabledTechnologiesProperty,
1115 PropertyStoreTest::kStringsV,
1116 &error));
Chris Masone9d779932011-08-25 16:33:41 -07001117 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -07001118 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001119}
1120
mukesh agrawal32399322011-09-01 10:53:43 -07001121TEST_F(ManagerTest, RequestScan) {
1122 {
1123 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -07001124 manager()->RegisterDevice(mock_devices_[0].get());
1125 manager()->RegisterDevice(mock_devices_[1].get());
Joshua Krollda798622012-06-05 12:30:48 -07001126 EXPECT_CALL(*mock_devices_[0], technology())
1127 .WillRepeatedly(Return(Technology::kWifi));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001128 EXPECT_CALL(*mock_devices_[0], Scan(_));
Joshua Krollda798622012-06-05 12:30:48 -07001129 EXPECT_CALL(*mock_devices_[1], technology())
1130 .WillRepeatedly(Return(Technology::kUnknown));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001131 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -07001132 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001133 }
1134
1135 {
1136 Error error;
Chris Masone9d779932011-08-25 16:33:41 -07001137 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -07001138 EXPECT_EQ(Error::kInvalidArguments, error.type());
1139 }
1140}
1141
Darin Petkovb65c2452012-02-23 15:17:06 +01001142TEST_F(ManagerTest, GetServiceNoType) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001143 KeyValueStore args;
1144 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001145 manager()->GetService(args, &e);
1146 EXPECT_EQ(Error::kInvalidArguments, e.type());
1147 EXPECT_EQ("must specify service type", e.message());
1148}
1149
1150TEST_F(ManagerTest, GetServiceUnknownType) {
1151 KeyValueStore args;
1152 Error e;
1153 args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet);
1154 manager()->GetService(args, &e);
1155 EXPECT_EQ(Error::kNotSupported, e.type());
1156 EXPECT_EQ("service type is unsupported", e.message());
1157}
1158
1159TEST_F(ManagerTest, GetServiceNoWifiDevice) {
1160 KeyValueStore args;
1161 Error e;
1162 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1163 manager()->GetService(args, &e);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001164 EXPECT_EQ(Error::kInvalidArguments, e.type());
1165 EXPECT_EQ("no wifi devices available", e.message());
1166}
1167
Darin Petkovb65c2452012-02-23 15:17:06 +01001168TEST_F(ManagerTest, GetServiceWifi) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001169 KeyValueStore args;
1170 Error e;
1171 WiFiServiceRefPtr wifi_service;
Darin Petkovb65c2452012-02-23 15:17:06 +01001172 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001173 manager()->RegisterDevice(mock_wifi_);
1174 EXPECT_CALL(*mock_wifi_, GetService(_, _))
1175 .WillRepeatedly(Return(wifi_service));
Darin Petkovb65c2452012-02-23 15:17:06 +01001176 manager()->GetService(args, &e);
1177 EXPECT_TRUE(e.IsSuccess());
1178}
1179
Darin Petkov33af05c2012-02-28 10:10:30 +01001180TEST_F(ManagerTest, GetServiceVPNUnknownType) {
1181 KeyValueStore args;
1182 Error e;
1183 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001184 scoped_refptr<MockProfile> profile(
1185 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1186 AdoptProfile(manager(), profile);
Darin Petkov33af05c2012-02-28 10:10:30 +01001187 ServiceRefPtr service = manager()->GetService(args, &e);
1188 EXPECT_EQ(Error::kNotSupported, e.type());
1189 EXPECT_FALSE(service);
1190}
1191
Darin Petkovb65c2452012-02-23 15:17:06 +01001192TEST_F(ManagerTest, GetServiceVPN) {
1193 KeyValueStore args;
1194 Error e;
Darin Petkovb65c2452012-02-23 15:17:06 +01001195 args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN);
Darin Petkov33af05c2012-02-28 10:10:30 +01001196 args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn);
Darin Petkov02867712012-03-12 14:25:05 +01001197 args.SetString(flimflam::kProviderHostProperty, "10.8.0.1");
1198 args.SetString(flimflam::kProviderNameProperty, "vpn-name");
Paul Stewart7f5ad572012-06-04 15:18:54 -07001199 scoped_refptr<MockProfile> profile(
1200 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1201 AdoptProfile(manager(), profile);
1202 ServiceRefPtr updated_service;
1203 EXPECT_CALL(*profile, UpdateService(_))
1204 .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true)));
1205 ServiceRefPtr configured_service;
1206 EXPECT_CALL(*profile, ConfigureService(_))
1207 .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true)));
Darin Petkov33af05c2012-02-28 10:10:30 +01001208 ServiceRefPtr service = manager()->GetService(args, &e);
1209 EXPECT_TRUE(e.IsSuccess());
1210 EXPECT_TRUE(service);
Paul Stewart7f5ad572012-06-04 15:18:54 -07001211 EXPECT_EQ(service, updated_service);
1212 EXPECT_EQ(service, configured_service);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001213}
1214
Darin Petkovc63dcf02012-05-24 11:51:43 +02001215TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) {
1216 KeyValueStore args;
1217 Error e;
1218 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
1219 ServiceRefPtr service = manager()->GetService(args, &e);
1220 EXPECT_EQ(Error::kInvalidArguments, e.type());
1221 EXPECT_EQ("Missing WiMAX network id.", e.message());
1222 EXPECT_FALSE(service);
1223}
1224
Darin Petkovd1cd7972012-05-22 15:26:15 +02001225TEST_F(ManagerTest, GetServiceWiMax) {
1226 KeyValueStore args;
1227 Error e;
1228 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax);
Darin Petkovc63dcf02012-05-24 11:51:43 +02001229 args.SetString(WiMaxService::kNetworkIdProperty, "01234567");
1230 args.SetString(flimflam::kNameProperty, "WiMAX Network");
1231 ServiceRefPtr service = manager()->GetService(args, &e);
1232 EXPECT_TRUE(e.IsSuccess());
1233 EXPECT_TRUE(service);
Darin Petkovd1cd7972012-05-22 15:26:15 +02001234}
1235
Paul Stewart7f61e522012-03-22 11:13:45 -07001236TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) {
1237 // Manager calls ActiveProfile() so we need at least one profile installed.
1238 scoped_refptr<MockProfile> profile(
1239 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1240 AdoptProfile(manager(), profile);
1241
1242 KeyValueStore args;
1243 args.SetString(flimflam::kProfileProperty, "xxx");
1244 Error error;
1245 manager()->ConfigureService(args, &error);
1246 EXPECT_EQ(Error::kInvalidArguments, error.type());
1247 EXPECT_EQ("Invalid profile name xxx", error.message());
1248}
1249
1250TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) {
1251 // Manager calls ActiveProfile() so we need at least one profile installed.
1252 scoped_refptr<MockProfile> profile(
1253 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1254 AdoptProfile(manager(), profile);
1255
1256 KeyValueStore args;
1257 Error error;
1258 manager()->ConfigureService(args, &error);
1259 EXPECT_EQ(Error::kInvalidArguments, error.type());
1260 EXPECT_EQ("must specify service type", error.message());
1261}
1262
1263// A registered service in the ephemeral profile should be moved to the
1264// active profile as a part of configuration if no profile was explicitly
1265// specified.
1266TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) {
1267 scoped_refptr<MockProfile> profile(
1268 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1269
1270 AdoptProfile(manager(), profile); // This is now the active profile.
1271
1272 const std::vector<uint8_t> ssid;
1273 scoped_refptr<MockWiFiService> service(
1274 new NiceMock<MockWiFiService>(control_interface(),
1275 dispatcher(),
1276 metrics(),
1277 manager(),
1278 mock_wifi_,
1279 ssid,
1280 "",
1281 "",
1282 false));
1283
1284 manager()->RegisterService(service);
1285 service->set_profile(GetEphemeralProfile(manager()));
1286
1287 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1288 // the same device as that used above causes a refcounting loop.
1289 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1290 dispatcher(),
1291 metrics(),
1292 manager(),
1293 "wifi1",
1294 "addr5",
1295 5));
1296 manager()->RegisterDevice(wifi);
1297 EXPECT_CALL(*wifi, GetService(_, _))
1298 .WillOnce(Return(service));
1299 EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get())))
1300 .WillOnce(Return(true));
1301 EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get())))
1302 .WillOnce(Return(true));
1303
1304 KeyValueStore args;
1305 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1306 Error error;
1307 manager()->ConfigureService(args, &error);
1308 EXPECT_TRUE(error.IsSuccess());
1309}
1310
1311// If were configure a service that was already registered and explicitly
1312// specify a profile, it should be moved from the profile it was previously
1313// in to the specified profile if one was requested.
1314TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) {
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 manager()->RegisterService(service);
1344 service->set_profile(profile1);
1345
1346 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1347 // the same device as that used above causes a refcounting loop.
1348 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1349 dispatcher(),
1350 metrics(),
1351 manager(),
1352 "wifi1",
1353 "addr5",
1354 5));
1355 manager()->RegisterDevice(wifi);
1356 EXPECT_CALL(*wifi, GetService(_, _))
1357 .WillOnce(Return(service));
1358 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1359 .WillOnce(Return(true));
1360 EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get())))
1361 .WillOnce(Return(true));
1362 EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get())))
1363 .WillOnce(Return(true));
1364
1365 KeyValueStore args;
1366 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1367 args.SetString(flimflam::kProfileProperty, kProfileName0);
1368 Error error;
1369 manager()->ConfigureService(args, &error);
1370 EXPECT_TRUE(error.IsSuccess());
1371 service->set_profile(NULL); // Breaks refcounting loop.
1372}
1373
1374// An unregistered service should remain unregistered, but its contents should
1375// be saved to the specified profile nonetheless.
1376TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) {
1377 scoped_refptr<MockProfile> profile0(
1378 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1379 scoped_refptr<MockProfile> profile1(
1380 new NiceMock<MockProfile>(control_interface(), manager(), ""));
1381
1382 const string kProfileName0 = "profile0";
1383 const string kProfileName1 = "profile1";
1384
1385 EXPECT_CALL(*profile0, GetRpcIdentifier())
1386 .WillRepeatedly(Return(kProfileName0));
1387 EXPECT_CALL(*profile1, GetRpcIdentifier())
1388 .WillRepeatedly(Return(kProfileName1));
1389
1390 AdoptProfile(manager(), profile0);
1391 AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile.
1392
1393 const std::vector<uint8_t> ssid;
1394 scoped_refptr<MockWiFiService> service(
1395 new NiceMock<MockWiFiService>(control_interface(),
1396 dispatcher(),
1397 metrics(),
1398 manager(),
1399 mock_wifi_,
1400 ssid,
1401 "",
1402 "",
1403 false));
1404
1405 service->set_profile(profile1);
1406
1407 // A separate MockWiFi from mock_wifi_ is used in the Manager since using
1408 // the same device as that used above causes a refcounting loop.
1409 scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(),
1410 dispatcher(),
1411 metrics(),
1412 manager(),
1413 "wifi1",
1414 "addr5",
1415 5));
1416 manager()->RegisterDevice(wifi);
1417 EXPECT_CALL(*wifi, GetService(_, _))
1418 .WillOnce(Return(service));
1419 EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get())))
1420 .WillOnce(Return(true));
1421 EXPECT_CALL(*profile0, AdoptService(_))
1422 .Times(0);
1423 EXPECT_CALL(*profile1, AdoptService(_))
1424 .Times(0);
1425
1426 KeyValueStore args;
1427 args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi);
1428 args.SetString(flimflam::kProfileProperty, kProfileName0);
1429 Error error;
1430 manager()->ConfigureService(args, &error);
1431 EXPECT_TRUE(error.IsSuccess());
1432}
1433
Paul Stewart22aa71b2011-09-16 12:15:11 -07001434TEST_F(ManagerTest, TechnologyOrder) {
1435 Error error;
1436 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
1437 string(flimflam::kTypeWifi), &error);
1438 ASSERT_TRUE(error.IsSuccess());
1439 EXPECT_EQ(manager()->GetTechnologyOrder(),
1440 string(flimflam::kTypeEthernet) + "," +
1441 string(flimflam::kTypeWifi));
1442
1443 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
1444 string(flimflam::kTypeWifi), &error);
1445 ASSERT_FALSE(error.IsSuccess());
1446 EXPECT_EQ(Error::kInvalidArguments, error.type());
1447 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
1448 string(flimflam::kTypeWifi),
1449 manager()->GetTechnologyOrder());
1450}
1451
1452TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +00001453 // TODO(quiche): Some of these tests would probably fit better in
1454 // service_unittest, since the actual comparison of Services is
1455 // implemented in Service. (crosbug.com/23370)
1456
Paul Stewart22aa71b2011-09-16 12:15:11 -07001457 scoped_refptr<MockService> mock_service0(
1458 new NiceMock<MockService>(control_interface(),
1459 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001460 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001461 manager()));
1462 scoped_refptr<MockService> mock_service1(
1463 new NiceMock<MockService>(control_interface(),
1464 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001465 metrics(),
Paul Stewart22aa71b2011-09-16 12:15:11 -07001466 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001467
1468 manager()->RegisterService(mock_service0);
1469 manager()->RegisterService(mock_service1);
1470
1471 // Services should already be sorted by UniqueName
1472 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1473
1474 // Asking explictly to sort services should not change anything
Paul Stewartdfa46052012-06-26 09:44:14 -07001475 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001476 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1477
1478 // Two otherwise equal services should be reordered by strength
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001479 mock_service1->SetStrength(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001480 manager()->UpdateService(mock_service1);
1481 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1482
1483 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -07001484 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001485 manager()->UpdateService(mock_service0);
1486 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1487
1488 // Technology
Joshua Kroll053fa822012-06-05 09:50:43 -07001489 EXPECT_CALL(*mock_service0.get(), technology())
1490 .WillRepeatedly(Return((Technology::kWifi)));
1491 EXPECT_CALL(*mock_service1.get(), technology())
1492 .WillRepeatedly(Return(Technology::kEthernet));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001493
1494 Error error;
mukesh agrawal84de5d22012-02-17 19:29:15 -08001495 // Default technology ordering should favor Ethernet over WiFi.
Paul Stewartdfa46052012-06-26 09:44:14 -07001496 manager()->SortServicesTask();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001497 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1498
1499 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
1500 string(flimflam::kTypeEthernet), &error);
1501 EXPECT_TRUE(error.IsSuccess());
1502 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1503
Gaurav Shah435de2c2011-11-17 19:01:07 -08001504 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001505 mock_service0->set_priority(1);
1506 manager()->UpdateService(mock_service0);
1507 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1508
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001509 // Favorite.
mukesh agrawal00917ce2011-11-22 23:56:55 +00001510 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -07001511 manager()->UpdateService(mock_service1);
1512 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1513
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001514 // Auto-connect.
1515 mock_service0->set_auto_connect(true);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001516 manager()->UpdateService(mock_service0);
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001517 mock_service1->set_auto_connect(false);
1518 manager()->UpdateService(mock_service1);
Paul Stewart22aa71b2011-09-16 12:15:11 -07001519 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1520
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001521 // Connectable.
1522 mock_service1->set_connectable(true);
1523 manager()->UpdateService(mock_service1);
1524 mock_service0->set_connectable(false);
1525 manager()->UpdateService(mock_service0);
1526 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1527
1528 // IsFailed.
1529 EXPECT_CALL(*mock_service0.get(), state())
1530 .WillRepeatedly(Return(Service::kStateIdle));
1531 EXPECT_CALL(*mock_service0.get(), IsFailed())
1532 .WillRepeatedly(Return(false));
1533 manager()->UpdateService(mock_service0);
1534 EXPECT_CALL(*mock_service0.get(), state())
1535 .WillRepeatedly(Return(Service::kStateFailure));
1536 EXPECT_CALL(*mock_service1.get(), IsFailed())
1537 .WillRepeatedly(Return(true));
1538 manager()->UpdateService(mock_service1);
1539 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1540
1541 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -07001542 EXPECT_CALL(*mock_service1.get(), state())
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001543 .WillRepeatedly(Return(Service::kStateAssociating));
1544 EXPECT_CALL(*mock_service1.get(), IsConnecting())
Gaurav Shah435de2c2011-11-17 19:01:07 -08001545 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -07001546 manager()->UpdateService(mock_service1);
1547 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1548
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001549 // Connected.
1550 EXPECT_CALL(*mock_service0.get(), state())
Paul Stewarta121c442012-06-09 14:12:58 -07001551 .WillRepeatedly(Return(Service::kStatePortal));
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001552 EXPECT_CALL(*mock_service0.get(), IsConnected())
1553 .WillRepeatedly(Return(true));
1554 manager()->UpdateService(mock_service0);
1555 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
1556
Paul Stewarta121c442012-06-09 14:12:58 -07001557 // Portal.
1558 EXPECT_CALL(*mock_service1.get(), state())
1559 .WillRepeatedly(Return(Service::kStateConnected));
1560 EXPECT_CALL(*mock_service1.get(), IsConnected())
1561 .WillRepeatedly(Return(true));
1562 manager()->UpdateService(mock_service1);
1563 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
1564
Paul Stewart22aa71b2011-09-16 12:15:11 -07001565 manager()->DeregisterService(mock_service0);
1566 manager()->DeregisterService(mock_service1);
1567}
1568
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001569TEST_F(ManagerTest, SortServicesWithConnection) {
Thieu Lea20cbc22012-01-09 22:01:43 +00001570 MockMetrics mock_metrics;
1571 manager()->set_metrics(&mock_metrics);
1572
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001573 scoped_refptr<MockService> mock_service0(
1574 new NiceMock<MockService>(control_interface(),
1575 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001576 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001577 manager()));
1578 scoped_refptr<MockService> mock_service1(
1579 new NiceMock<MockService>(control_interface(),
1580 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001581 metrics(),
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001582 manager()));
1583
1584 scoped_refptr<MockConnection> mock_connection0(
1585 new NiceMock<MockConnection>(device_info_.get()));
1586 scoped_refptr<MockConnection> mock_connection1(
1587 new NiceMock<MockConnection>(device_info_.get()));
1588
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001589 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001590 manager()->RegisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001591 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001592 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001593 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001594 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001595
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001596 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001597 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001598
1599 mock_service1->set_priority(1);
1600 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001601 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001602
1603 mock_service1->set_priority(0);
1604 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001605 manager()->SortServicesTask();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001606
Paul Stewartce4ec192012-03-14 12:53:46 -07001607 mock_service0->set_mock_connection(mock_connection0);
1608 mock_service1->set_mock_connection(mock_connection1);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001609
1610 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001611 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001612 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001613
1614 mock_service1->set_priority(1);
1615 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false));
1616 EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001617 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get()));
Paul Stewartdfa46052012-06-26 09:44:14 -07001618 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001619
1620 EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true));
Thieu Lea20cbc22012-01-09 22:01:43 +00001621 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001622 mock_service1->set_mock_connection(NULL);
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001623 manager()->DeregisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07001624 CompleteServiceSort();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001625
Paul Stewartce4ec192012-03-14 12:53:46 -07001626 mock_service0->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001627 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001628 manager()->DeregisterService(mock_service0);
Paul Stewartdfa46052012-06-26 09:44:14 -07001629 CompleteServiceSort();
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001630
1631 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL));
Paul Stewartdfa46052012-06-26 09:44:14 -07001632 manager()->SortServicesTask();
Paul Stewartc1dec4d2011-12-08 15:25:28 -08001633}
1634
Gaurav Shah435de2c2011-11-17 19:01:07 -08001635TEST_F(ManagerTest, AvailableTechnologies) {
1636 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
1637 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001638 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001639 manager(),
1640 "null4",
1641 "addr4",
1642 0));
1643 manager()->RegisterDevice(mock_devices_[0]);
1644 manager()->RegisterDevice(mock_devices_[1]);
1645 manager()->RegisterDevice(mock_devices_[2]);
1646 manager()->RegisterDevice(mock_devices_[3]);
1647
1648 ON_CALL(*mock_devices_[0].get(), technology())
1649 .WillByDefault(Return(Technology::kEthernet));
1650 ON_CALL(*mock_devices_[1].get(), technology())
1651 .WillByDefault(Return(Technology::kWifi));
1652 ON_CALL(*mock_devices_[2].get(), technology())
1653 .WillByDefault(Return(Technology::kCellular));
1654 ON_CALL(*mock_devices_[3].get(), technology())
1655 .WillByDefault(Return(Technology::kWifi));
1656
1657 set<string> expected_technologies;
1658 expected_technologies.insert(Technology::NameFromIdentifier(
1659 Technology::kEthernet));
1660 expected_technologies.insert(Technology::NameFromIdentifier(
1661 Technology::kWifi));
1662 expected_technologies.insert(Technology::NameFromIdentifier(
1663 Technology::kCellular));
1664 Error error;
1665 vector<string> technologies = manager()->AvailableTechnologies(&error);
1666
1667 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1668 ContainerEq(expected_technologies));
1669}
1670
1671TEST_F(ManagerTest, ConnectedTechnologies) {
1672 scoped_refptr<MockService> connected_service1(
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 scoped_refptr<MockService> connected_service2(
1678 new NiceMock<MockService>(control_interface(),
1679 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001680 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001681 manager()));
1682 scoped_refptr<MockService> disconnected_service1(
1683 new NiceMock<MockService>(control_interface(),
1684 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001685 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001686 manager()));
1687 scoped_refptr<MockService> disconnected_service2(
1688 new NiceMock<MockService>(control_interface(),
1689 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001690 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001691 manager()));
1692
1693 ON_CALL(*connected_service1.get(), IsConnected())
1694 .WillByDefault(Return(true));
1695 ON_CALL(*connected_service2.get(), IsConnected())
1696 .WillByDefault(Return(true));
1697
1698 manager()->RegisterService(connected_service1);
1699 manager()->RegisterService(connected_service2);
1700 manager()->RegisterService(disconnected_service1);
1701 manager()->RegisterService(disconnected_service2);
1702
1703 manager()->RegisterDevice(mock_devices_[0]);
1704 manager()->RegisterDevice(mock_devices_[1]);
1705 manager()->RegisterDevice(mock_devices_[2]);
1706 manager()->RegisterDevice(mock_devices_[3]);
1707
1708 ON_CALL(*mock_devices_[0].get(), technology())
1709 .WillByDefault(Return(Technology::kEthernet));
1710 ON_CALL(*mock_devices_[1].get(), technology())
1711 .WillByDefault(Return(Technology::kWifi));
1712 ON_CALL(*mock_devices_[2].get(), technology())
1713 .WillByDefault(Return(Technology::kCellular));
1714 ON_CALL(*mock_devices_[3].get(), technology())
1715 .WillByDefault(Return(Technology::kWifi));
1716
1717 mock_devices_[0]->SelectService(connected_service1);
1718 mock_devices_[1]->SelectService(disconnected_service1);
1719 mock_devices_[2]->SelectService(disconnected_service2);
1720 mock_devices_[3]->SelectService(connected_service2);
1721
1722 set<string> expected_technologies;
1723 expected_technologies.insert(Technology::NameFromIdentifier(
1724 Technology::kEthernet));
1725 expected_technologies.insert(Technology::NameFromIdentifier(
1726 Technology::kWifi));
1727 Error error;
1728
1729 vector<string> technologies = manager()->ConnectedTechnologies(&error);
1730 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
1731 ContainerEq(expected_technologies));
1732}
1733
1734TEST_F(ManagerTest, DefaultTechnology) {
1735 scoped_refptr<MockService> connected_service(
1736 new NiceMock<MockService>(control_interface(),
1737 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001738 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001739 manager()));
1740 scoped_refptr<MockService> disconnected_service(
1741 new NiceMock<MockService>(control_interface(),
1742 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001743 metrics(),
Gaurav Shah435de2c2011-11-17 19:01:07 -08001744 manager()));
1745
1746 // Connected. WiFi.
1747 ON_CALL(*connected_service.get(), IsConnected())
1748 .WillByDefault(Return(true));
1749 ON_CALL(*connected_service.get(), state())
1750 .WillByDefault(Return(Service::kStateConnected));
1751 ON_CALL(*connected_service.get(), technology())
1752 .WillByDefault(Return(Technology::kWifi));
1753
1754 // Disconnected. Ethernet.
1755 ON_CALL(*disconnected_service.get(), technology())
1756 .WillByDefault(Return(Technology::kEthernet));
1757
1758 manager()->RegisterService(disconnected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001759 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001760 Error error;
1761 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
1762
1763
1764 manager()->RegisterService(connected_service);
Paul Stewartdfa46052012-06-26 09:44:14 -07001765 CompleteServiceSort();
Gaurav Shah435de2c2011-11-17 19:01:07 -08001766 // Connected service should be brought to the front now.
1767 string expected_technology =
1768 Technology::NameFromIdentifier(Technology::kWifi);
1769 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
1770}
1771
Thieu Le1271d682011-11-02 22:48:19 +00001772TEST_F(ManagerTest, DisconnectServicesOnStop) {
1773 scoped_refptr<MockService> mock_service(
1774 new NiceMock<MockService>(control_interface(),
1775 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001776 metrics(),
Thieu Le1271d682011-11-02 22:48:19 +00001777 manager()));
1778 manager()->RegisterService(mock_service);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +00001779 EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1);
Thieu Le1271d682011-11-02 22:48:19 +00001780 manager()->Stop();
1781}
1782
mukesh agrawal00917ce2011-11-22 23:56:55 +00001783TEST_F(ManagerTest, UpdateServiceConnected) {
1784 scoped_refptr<MockService> mock_service(
1785 new NiceMock<MockService>(control_interface(),
1786 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001787 metrics(),
mukesh agrawal00917ce2011-11-22 23:56:55 +00001788 manager()));
1789 manager()->RegisterService(mock_service);
1790 EXPECT_FALSE(mock_service->favorite());
1791 EXPECT_FALSE(mock_service->auto_connect());
1792
Gaurav Shah435de2c2011-11-17 19:01:07 -08001793 EXPECT_CALL(*mock_service.get(), IsConnected())
1794 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +00001795 manager()->UpdateService(mock_service);
1796 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1797 // to mock out MakeFavorite. And mocking that out would break the
1798 // SortServices test. (crosbug.com/23370)
1799 EXPECT_TRUE(mock_service->favorite());
1800 EXPECT_TRUE(mock_service->auto_connect());
1801}
1802
Thieu Led4e9e552012-02-16 16:26:07 -08001803TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) {
1804 // This tests the case where the user connects to a service that is
1805 // currently associated with a profile. We want to make sure that the
1806 // favorite flag is set and that the flag is saved to the current
1807 // profile.
1808 scoped_refptr<MockService> mock_service(
1809 new NiceMock<MockService>(control_interface(),
1810 dispatcher(),
1811 metrics(),
1812 manager()));
1813 manager()->RegisterService(mock_service);
1814 EXPECT_FALSE(mock_service->favorite());
1815 EXPECT_FALSE(mock_service->auto_connect());
1816
Gary Moraind93615e2012-04-27 11:50:03 -07001817 scoped_refptr<MockProfile> profile(
1818 new MockProfile(control_interface(), manager(), ""));
Thieu Led4e9e552012-02-16 16:26:07 -08001819
Gary Moraind93615e2012-04-27 11:50:03 -07001820 mock_service->set_profile(profile);
1821 EXPECT_CALL(*mock_service, IsConnected())
Thieu Led4e9e552012-02-16 16:26:07 -08001822 .WillRepeatedly(Return(true));
Gary Moraind93615e2012-04-27 11:50:03 -07001823 EXPECT_CALL(*profile,
1824 UpdateService(static_cast<ServiceRefPtr>(mock_service)));
Thieu Led4e9e552012-02-16 16:26:07 -08001825 manager()->UpdateService(mock_service);
1826 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
1827 // to mock out MakeFavorite. And mocking that out would break the
1828 // SortServices test. (crosbug.com/23370)
1829 EXPECT_TRUE(mock_service->favorite());
1830 EXPECT_TRUE(mock_service->auto_connect());
Gary Moraind93615e2012-04-27 11:50:03 -07001831 // This releases the ref on the mock profile.
1832 mock_service->set_profile(NULL);
Thieu Led4e9e552012-02-16 16:26:07 -08001833}
1834
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001835TEST_F(ManagerTest, SaveSuccessfulService) {
1836 scoped_refptr<MockProfile> profile(
1837 new StrictMock<MockProfile>(control_interface(), manager(), ""));
1838 AdoptProfile(manager(), profile);
1839 scoped_refptr<MockService> service(
1840 new NiceMock<MockService>(control_interface(),
1841 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001842 metrics(),
Paul Stewart3d9bcf52011-12-12 15:02:22 -08001843 manager()));
1844
1845 // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly.
1846 ServiceRefPtr expect_service(service.get());
1847
1848 EXPECT_CALL(*profile.get(), ConfigureService(expect_service))
1849 .WillOnce(Return(false));
1850 manager()->RegisterService(service);
1851
1852 EXPECT_CALL(*service.get(), state())
1853 .WillRepeatedly(Return(Service::kStateConnected));
1854 EXPECT_CALL(*service.get(), IsConnected())
1855 .WillRepeatedly(Return(true));
1856 EXPECT_CALL(*profile.get(), AdoptService(expect_service))
1857 .WillOnce(Return(true));
1858 manager()->UpdateService(service);
1859}
1860
Paul Stewart1b253142012-01-26 14:05:52 -08001861TEST_F(ManagerTest, EnumerateProfiles) {
1862 vector<string> profile_paths;
1863 for (size_t i = 0; i < 10; i++) {
1864 scoped_refptr<MockProfile> profile(
1865 new StrictMock<MockProfile>(control_interface(), manager(), ""));
Jason Glasgow5d8197b2012-01-27 08:37:32 -05001866 profile_paths.push_back(base::StringPrintf("/profile/%zd", i));
Paul Stewart1b253142012-01-26 14:05:52 -08001867 EXPECT_CALL(*profile.get(), GetRpcIdentifier())
1868 .WillOnce(Return(profile_paths.back()));
1869 AdoptProfile(manager(), profile);
1870 }
1871
1872 Error error;
1873 vector<string> returned_paths = manager()->EnumerateProfiles(&error);
1874 EXPECT_TRUE(error.IsSuccess());
1875 EXPECT_EQ(profile_paths.size(), returned_paths.size());
1876 for (size_t i = 0; i < profile_paths.size(); i++) {
1877 EXPECT_EQ(profile_paths[i], returned_paths[i]);
1878 }
1879}
1880
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001881TEST_F(ManagerTest, AutoConnectOnRegister) {
1882 MockServiceRefPtr service = MakeAutoConnectableService();
1883 EXPECT_CALL(*service.get(), AutoConnect());
1884 manager()->RegisterService(service);
1885 dispatcher()->DispatchPendingEvents();
1886}
1887
1888TEST_F(ManagerTest, AutoConnectOnUpdate) {
1889 MockServiceRefPtr service1 = MakeAutoConnectableService();
1890 service1->set_priority(1);
1891 MockServiceRefPtr service2 = MakeAutoConnectableService();
1892 service2->set_priority(2);
1893 manager()->RegisterService(service1);
1894 manager()->RegisterService(service2);
1895 dispatcher()->DispatchPendingEvents();
1896
1897 EXPECT_CALL(*service1.get(), AutoConnect());
1898 EXPECT_CALL(*service2.get(), state())
1899 .WillRepeatedly(Return(Service::kStateFailure));
1900 EXPECT_CALL(*service2.get(), IsFailed())
1901 .WillRepeatedly(Return(true));
1902 EXPECT_CALL(*service2.get(), IsConnected())
1903 .WillRepeatedly(Return(false));
1904 manager()->UpdateService(service2);
1905 dispatcher()->DispatchPendingEvents();
1906}
1907
1908TEST_F(ManagerTest, AutoConnectOnDeregister) {
1909 MockServiceRefPtr service1 = MakeAutoConnectableService();
1910 service1->set_priority(1);
1911 MockServiceRefPtr service2 = MakeAutoConnectableService();
1912 service2->set_priority(2);
1913 manager()->RegisterService(service1);
1914 manager()->RegisterService(service2);
1915 dispatcher()->DispatchPendingEvents();
1916
1917 EXPECT_CALL(*service1.get(), AutoConnect());
1918 manager()->DeregisterService(service2);
1919 dispatcher()->DispatchPendingEvents();
1920}
1921
Paul Stewartc681fa02012-03-02 19:40:04 -08001922TEST_F(ManagerTest, RecheckPortal) {
1923 EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection())
1924 .WillOnce(Return(false));
1925 EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection())
1926 .WillOnce(Return(true));
1927 EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection())
1928 .Times(0);
1929
1930 manager()->RegisterDevice(mock_devices_[0]);
1931 manager()->RegisterDevice(mock_devices_[1]);
1932 manager()->RegisterDevice(mock_devices_[2]);
1933
1934 manager()->RecheckPortal(NULL);
1935}
1936
Paul Stewartd215af62012-04-24 23:25:50 -07001937TEST_F(ManagerTest, RecheckPortalOnService) {
1938 MockServiceRefPtr service = new NiceMock<MockService>(control_interface(),
1939 dispatcher(),
1940 metrics(),
1941 manager());
1942 EXPECT_CALL(*mock_devices_[0].get(),
1943 IsConnectedToService(IsRefPtrTo(service)))
1944 .WillOnce(Return(false));
1945 EXPECT_CALL(*mock_devices_[1].get(),
1946 IsConnectedToService(IsRefPtrTo(service)))
1947 .WillOnce(Return(true));
1948 EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection())
1949 .WillOnce(Return(true));
1950 EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_))
1951 .Times(0);
1952
1953 manager()->RegisterDevice(mock_devices_[0]);
1954 manager()->RegisterDevice(mock_devices_[1]);
1955 manager()->RegisterDevice(mock_devices_[2]);
1956
1957 manager()->RecheckPortalOnService(service);
1958}
1959
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001960TEST_F(ManagerTest, GetDefaultService) {
1961 EXPECT_FALSE(manager()->GetDefaultService().get());
1962
1963 scoped_refptr<MockService> mock_service(
1964 new NiceMock<MockService>(control_interface(),
1965 dispatcher(),
1966 metrics(),
1967 manager()));
1968
1969 manager()->RegisterService(mock_service);
1970 EXPECT_FALSE(manager()->GetDefaultService().get());
1971
1972 scoped_refptr<MockConnection> mock_connection(
1973 new NiceMock<MockConnection>(device_info_.get()));
Paul Stewartce4ec192012-03-14 12:53:46 -07001974 mock_service->set_mock_connection(mock_connection);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001975 EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get());
1976
Paul Stewartce4ec192012-03-14 12:53:46 -07001977 mock_service->set_mock_connection(NULL);
Paul Stewarte2bad7c2012-03-14 08:55:33 -07001978 manager()->DeregisterService(mock_service);
1979}
1980
Paul Stewart13ed2252012-03-21 12:52:46 -07001981TEST_F(ManagerTest, GetServiceWithGUID) {
1982 scoped_refptr<MockService> mock_service0(
1983 new NiceMock<MockService>(control_interface(),
1984 dispatcher(),
1985 metrics(),
1986 manager()));
1987
1988 scoped_refptr<MockService> mock_service1(
1989 new NiceMock<MockService>(control_interface(),
1990 dispatcher(),
1991 metrics(),
1992 manager()));
1993
Paul Stewartcb59fed2012-03-21 21:14:46 -07001994 EXPECT_CALL(*mock_service0.get(), Configure(_, _))
1995 .Times(0);
1996 EXPECT_CALL(*mock_service1.get(), Configure(_, _))
1997 .Times(0);
1998
Paul Stewart13ed2252012-03-21 12:52:46 -07001999 manager()->RegisterService(mock_service0);
2000 manager()->RegisterService(mock_service1);
2001
2002 const string kGUID0 = "GUID0";
2003 const string kGUID1 = "GUID1";
2004
2005 {
2006 Error error;
2007 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2008 EXPECT_FALSE(error.IsSuccess());
2009 EXPECT_FALSE(service);
2010 }
2011
2012 KeyValueStore args;
2013 args.SetString(flimflam::kGuidProperty, kGUID1);
2014
2015 {
2016 Error error;
2017 ServiceRefPtr service = manager()->GetService(args, &error);
2018 EXPECT_EQ(Error::kInvalidArguments, error.type());
2019 EXPECT_FALSE(service);
2020 }
2021
2022 mock_service0->set_guid(kGUID0);
2023 mock_service1->set_guid(kGUID1);
2024
2025 {
2026 Error error;
2027 ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error);
2028 EXPECT_TRUE(error.IsSuccess());
2029 EXPECT_EQ(mock_service0.get(), service.get());
2030 }
2031
2032 {
2033 Error error;
Paul Stewartcb59fed2012-03-21 21:14:46 -07002034 EXPECT_CALL(*mock_service1.get(), Configure(_, &error))
2035 .Times(1);
Paul Stewart13ed2252012-03-21 12:52:46 -07002036 ServiceRefPtr service = manager()->GetService(args, &error);
2037 EXPECT_TRUE(error.IsSuccess());
2038 EXPECT_EQ(mock_service1.get(), service.get());
2039 }
2040
2041 manager()->DeregisterService(mock_service0);
2042 manager()->DeregisterService(mock_service1);
2043}
2044
Gary Morain028545d2012-04-07 14:55:52 -07002045
2046TEST_F(ManagerTest, CalculateStateOffline) {
2047 MockMetrics mock_metrics;
2048 manager()->set_metrics(&mock_metrics);
2049 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2050 .Times(AnyNumber());
2051 scoped_refptr<MockService> mock_service0(
2052 new NiceMock<MockService>(control_interface(),
2053 dispatcher(),
2054 metrics(),
2055 manager()));
2056
2057 scoped_refptr<MockService> mock_service1(
2058 new NiceMock<MockService>(control_interface(),
2059 dispatcher(),
2060 metrics(),
2061 manager()));
2062
2063 EXPECT_CALL(*mock_service0.get(), IsConnected())
2064 .WillRepeatedly(Return(false));
2065 EXPECT_CALL(*mock_service1.get(), IsConnected())
2066 .WillRepeatedly(Return(false));
2067
2068 manager()->RegisterService(mock_service0);
2069 manager()->RegisterService(mock_service1);
2070
2071 EXPECT_EQ("offline", manager()->CalculateState(NULL));
2072
2073 manager()->DeregisterService(mock_service0);
2074 manager()->DeregisterService(mock_service1);
2075}
2076
2077TEST_F(ManagerTest, CalculateStateOnline) {
2078 MockMetrics mock_metrics;
2079 manager()->set_metrics(&mock_metrics);
2080 EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_))
2081 .Times(AnyNumber());
2082 scoped_refptr<MockService> mock_service0(
2083 new NiceMock<MockService>(control_interface(),
2084 dispatcher(),
2085 metrics(),
2086 manager()));
2087
2088 scoped_refptr<MockService> mock_service1(
2089 new NiceMock<MockService>(control_interface(),
2090 dispatcher(),
2091 metrics(),
2092 manager()));
2093
2094 EXPECT_CALL(*mock_service0.get(), IsConnected())
2095 .WillRepeatedly(Return(false));
2096 EXPECT_CALL(*mock_service1.get(), IsConnected())
2097 .WillRepeatedly(Return(true));
2098 EXPECT_CALL(*mock_service0.get(), state())
2099 .WillRepeatedly(Return(Service::kStateIdle));
2100 EXPECT_CALL(*mock_service1.get(), state())
2101 .WillRepeatedly(Return(Service::kStateConnected));
2102
2103 manager()->RegisterService(mock_service0);
2104 manager()->RegisterService(mock_service1);
Paul Stewartdfa46052012-06-26 09:44:14 -07002105 CompleteServiceSort();
Gary Morain028545d2012-04-07 14:55:52 -07002106
2107 EXPECT_EQ("online", manager()->CalculateState(NULL));
2108
2109 manager()->DeregisterService(mock_service0);
2110 manager()->DeregisterService(mock_service1);
2111}
2112
Paul Stewart10e9e4e2012-04-26 19:46:28 -07002113TEST_F(ManagerTest, StartupPortalList) {
2114 // Simulate loading value from the default profile.
2115 const string kProfileValue("wifi,vpn");
2116 manager()->props_.check_portal_list = kProfileValue;
2117
2118 EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL));
2119 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2120 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2121
2122 const string kStartupValue("cellular,ethernet");
2123 manager()->SetStartupPortalList(kStartupValue);
2124 // Ensure profile value is not overwritten, so when we save the default
2125 // profile, the correct value will still be written.
2126 EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list);
2127
2128 // However we should read back a different list.
2129 EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL));
2130 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi));
2131 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2132
2133 const string kRuntimeValue("ppp");
2134 // Setting a runtime value over the control API should overwrite both
2135 // the profile value and what we read back.
2136 Error error;
2137 manager()->mutable_store()->SetStringProperty(
2138 flimflam::kCheckPortalListProperty,
2139 kRuntimeValue,
2140 &error);
2141 ASSERT_TRUE(error.IsSuccess());
2142 EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL));
2143 EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list);
2144 EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular));
2145 EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP));
2146}
2147
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002148TEST_F(ManagerTest, EnableTechnology) {
2149 Error error(Error::kOperationInitiated);
2150 ResultCallback callback;
2151 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2152 EXPECT_TRUE(error.IsSuccess());
2153
Joshua Krollda798622012-06-05 12:30:48 -07002154 ON_CALL(*mock_devices_[0], technology())
2155 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002156
2157 manager()->RegisterDevice(mock_devices_[0]);
2158
2159 // Device is enabled, so expect operation is successful.
2160 mock_devices_[0]->enabled_ = true;
2161 error.Populate(Error::kOperationInitiated);
2162 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2163 EXPECT_TRUE(error.IsSuccess());
2164
2165 // Device is disabled, so expect operation in progress.
2166 mock_devices_[0]->enabled_ = false;
2167 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _));
2168 error.Populate(Error::kOperationInitiated);
2169 manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback);
2170 EXPECT_TRUE(error.IsOngoing());
2171}
2172
2173TEST_F(ManagerTest, DisableTechnology) {
2174 Error error(Error::kOperationInitiated);
2175 ResultCallback callback;
2176 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2177 EXPECT_TRUE(error.IsSuccess());
2178
Joshua Krollda798622012-06-05 12:30:48 -07002179 ON_CALL(*mock_devices_[0], technology())
2180 .WillByDefault(Return(Technology::kEthernet));
Jason Glasgowdf7c5532012-05-14 14:41:45 -04002181
2182 manager()->RegisterDevice(mock_devices_[0]);
2183
2184 // Device is disabled, so expect operation is successful.
2185 error.Populate(Error::kOperationInitiated);
2186 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2187 EXPECT_TRUE(error.IsSuccess());
2188
2189 // Device is enabled, so expect operation in progress.
2190 EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _));
2191 mock_devices_[0]->enabled_ = true;
2192 error.Populate(Error::kOperationInitiated);
2193 manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback);
2194 EXPECT_TRUE(error.IsOngoing());
2195}
2196
2197
Chris Masone9be4a9d2011-05-16 15:44:09 -07002198} // namespace shill