blob: 8a4d223905aa2fa3dfd6e9be5db6c4d23a1ac0a1 [file] [log] [blame]
Chris Masone9be4a9d2011-05-16 15:44:09 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07004
5#include "shill/manager.h"
6
Chris Masone6791a432011-07-12 13:23:19 -07007#include <set>
8
Chris Masone9be4a9d2011-05-16 15:44:09 -07009#include <glib.h>
10
Chris Masone9be4a9d2011-05-16 15:44:09 -070011#include <base/logging.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070012#include <base/memory/scoped_temp_dir.h>
Chris Masone6791a432011-07-12 13:23:19 -070013#include <base/stl_util-inl.h>
Paul Stewart5dc40aa2011-10-28 19:43:43 -070014#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070016#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070017#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070018
mukesh agrawal32399322011-09-01 10:53:43 -070019#include "shill/adaptor_interfaces.h"
Chris Masone6515aab2011-10-12 16:19:09 -070020#include "shill/ephemeral_profile.h"
mukesh agrawal32399322011-09-01 10:53:43 -070021#include "shill/error.h"
Chris Masone6515aab2011-10-12 16:19:09 -070022#include "shill/glib.h"
23#include "shill/key_file_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070024#include "shill/key_value_store.h"
mukesh agrawal32399322011-09-01 10:53:43 -070025#include "shill/mock_adaptors.h"
Chris Masoned7732e42011-05-20 11:08:56 -070026#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070027#include "shill/mock_device.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070028#include "shill/mock_glib.h"
29#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070030#include "shill/mock_service.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070031#include "shill/mock_store.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070032#include "shill/mock_wifi.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033#include "shill/property_store_unittest.h"
Chris Masone6515aab2011-10-12 16:19:09 -070034#include "shill/service_under_test.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070035#include "shill/wifi_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070036
37using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070038using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039using std::string;
40using std::vector;
41
Chris Masone9be4a9d2011-05-16 15:44:09 -070042namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070043using ::testing::_;
Chris Masone6515aab2011-10-12 16:19:09 -070044using ::testing::AnyNumber;
Gaurav Shah435de2c2011-11-17 19:01:07 -080045using ::testing::ContainerEq;
Paul Stewart22aa71b2011-09-16 12:15:11 -070046using ::testing::Ne;
Chris Masone9be4a9d2011-05-16 15:44:09 -070047using ::testing::NiceMock;
48using ::testing::Return;
Gaurav Shah435de2c2011-11-17 19:01:07 -080049using ::testing::StrEq;
Chris Masone9d779932011-08-25 16:33:41 -070050using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070051
Chris Masone3bd3c8c2011-06-13 08:20:26 -070052class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070053 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070054 ManagerTest()
Paul Stewart22aa71b2011-09-16 12:15:11 -070055 : mock_wifi_(new NiceMock<MockWiFi>(control_interface(),
mukesh agrawal7a4e4002011-09-06 11:26:05 -070056 dispatcher(),
57 manager(),
58 "wifi0",
59 "addr4",
Paul Stewart22aa71b2011-09-16 12:15:11 -070060 4)) {
61 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
62 dispatcher(),
63 manager(),
64 "null0",
65 "addr0",
66 0));
67 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
68 dispatcher(),
69 manager(),
70 "null1",
71 "addr1",
72 1));
73 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
74 dispatcher(),
75 manager(),
76 "null2",
77 "addr2",
78 2));
Gaurav Shah435de2c2011-11-17 19:01:07 -080079 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
80 dispatcher(),
81 manager(),
82 "null3",
83 "addr3",
84 3));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070085 manager()->connect_profiles_to_rpc_ = false;
Chris Masone3c3f6a12011-07-01 10:01:41 -070086 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070087 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -070088
Paul Stewartfdd16072011-09-16 12:41:35 -070089 bool IsDeviceRegistered(const DeviceRefPtr &device,
90 Technology::Identifier tech) {
Chris Masonec1e50412011-06-07 13:04:53 -070091 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -070092 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -070093 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -070094 }
Paul Stewart22aa71b2011-09-16 12:15:11 -070095 bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2);
Chris Masone9be4a9d2011-05-16 15:44:09 -070096
Paul Stewarta849a3d2011-11-03 05:54:09 -070097 void AdoptProfile(Manager *manager, ProfileRefPtr profile) {
98 manager->profiles_.push_back(profile);
99 }
100
Chris Masone6515aab2011-10-12 16:19:09 -0700101 Profile *CreateProfileForManager(Manager *manager, GLib *glib) {
102 Profile::Identifier id("rather", "irrelevant");
103 scoped_ptr<Profile> profile(new Profile(control_interface(),
104 manager,
105 id,
106 "",
107 false));
108 FilePath final_path(storage_path());
109 final_path = final_path.Append("test.profile");
110 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
111 storage->set_path(final_path);
112 if (!storage->Open())
113 return NULL;
114 profile->set_storage(storage.release()); // Passes ownership.
115 return profile.release();
116 }
117
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700118 bool CreateBackingStoreForService(ScopedTempDir *temp_dir,
119 const string &profile_identifier,
120 const string &service_name) {
121 GLib glib;
122 KeyFileStore store(&glib);
123 store.set_path(temp_dir->path().Append(profile_identifier + ".profile"));
124 return store.Open() &&
125 store.SetString(service_name, "rather", "irrelevant") &&
126 store.Close();
127 }
128
129 Error::Type TestCreateProfile(Manager *manager, const string &name) {
130 Error error;
131 manager->CreateProfile(name, &error);
132 return error.type();
133 }
134
135 Error::Type TestPopAnyProfile(Manager *manager) {
136 Error error;
137 manager->PopAnyProfile(&error);
138 return error.type();
139 }
140
141 Error::Type TestPopProfile(Manager *manager, const string &name) {
142 Error error;
143 manager->PopProfile(name, &error);
144 return error.type();
145 }
146
147 Error::Type TestPushProfile(Manager *manager, const string &name) {
148 Error error;
149 manager->PushProfile(name, &error);
150 return error.type();
151 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700152 protected:
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700153 scoped_refptr<MockWiFi> mock_wifi_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700154 vector<scoped_refptr<MockDevice> > mock_devices_;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700155};
156
Paul Stewart22aa71b2011-09-16 12:15:11 -0700157bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) {
158 return (svc0.get() == manager()->services_[0].get() &&
159 svc1.get() == manager()->services_[1].get());
160}
161
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700162TEST_F(ManagerTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700163 EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty));
164 EXPECT_FALSE(manager()->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700165}
166
Chris Masone9be4a9d2011-05-16 15:44:09 -0700167TEST_F(ManagerTest, DeviceRegistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700168 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700169 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700170 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700171 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700172 ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular))
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700173 .WillByDefault(Return(true));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700174
Paul Stewart22aa71b2011-09-16 12:15:11 -0700175 manager()->RegisterDevice(mock_devices_[0]);
176 manager()->RegisterDevice(mock_devices_[1]);
177 manager()->RegisterDevice(mock_devices_[2]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700178
Paul Stewart22aa71b2011-09-16 12:15:11 -0700179 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
180 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
181 EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700182}
183
Paul Stewarta41e38d2011-11-11 07:47:29 -0800184TEST_F(ManagerTest, DeviceRegistrationAndStart) {
185 manager()->running_ = true;
186 mock_devices_[0]->powered_ = true;
187 mock_devices_[1]->powered_ = false;
188 EXPECT_CALL(*mock_devices_[0].get(), Start())
189 .Times(1);
190 EXPECT_CALL(*mock_devices_[1].get(), Start())
191 .Times(0);
192 manager()->RegisterDevice(mock_devices_[0]);
193 manager()->RegisterDevice(mock_devices_[1]);
194}
195
196TEST_F(ManagerTest, DeviceRegistrationWithProfile) {
197 MockProfile *profile = new MockProfile(control_interface(), manager(), "");
198 DeviceRefPtr device_ref(mock_devices_[0].get());
199 AdoptProfile(manager(), profile); // Passes ownership.
200 EXPECT_CALL(*profile, ConfigureDevice(device_ref));
201 EXPECT_CALL(*profile, Save());
202 manager()->RegisterDevice(mock_devices_[0]);
203}
204
Chris Masone9be4a9d2011-05-16 15:44:09 -0700205TEST_F(ManagerTest, DeviceDeregistration) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700206 ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet))
Chris Masone3c3f6a12011-07-01 10:01:41 -0700207 .WillByDefault(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700208 ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700209 .WillByDefault(Return(true));
210
Gaurav Shah435de2c2011-11-17 19:01:07 -0800211 manager()->RegisterDevice(mock_devices_[0]);
212 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700213
Paul Stewart22aa71b2011-09-16 12:15:11 -0700214 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
215 ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700216
Paul Stewart22aa71b2011-09-16 12:15:11 -0700217 EXPECT_CALL(*mock_devices_[0].get(), Stop());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800218 manager()->DeregisterDevice(mock_devices_[0]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700219 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700220
Paul Stewart22aa71b2011-09-16 12:15:11 -0700221 EXPECT_CALL(*mock_devices_[1].get(), Stop());
Gaurav Shah435de2c2011-11-17 19:01:07 -0800222 manager()->DeregisterDevice(mock_devices_[1]);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700223 EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700224}
225
226TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700227 // It's much easier and safer to use a real GLib for this test.
228 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700229 Manager manager(control_interface(),
230 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700231 &glib,
232 run_path(),
233 storage_path(),
234 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700235 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
236 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700237 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700238
Chris Masone9be4a9d2011-05-16 15:44:09 -0700239 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700240 new NiceMock<MockService>(control_interface(),
241 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700242 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700243 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700244 new NiceMock<MockService>(control_interface(),
245 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700246 &manager));
mukesh agrawal51a7e932011-07-27 16:18:26 -0700247 string service1_name(mock_service->UniqueName());
248 string service2_name(mock_service2->UniqueName());
249
250 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
251 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700252 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700253 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700254 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700255 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700256 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700257
Chris Masone9d779932011-08-25 16:33:41 -0700258 manager.RegisterService(mock_service);
259 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700260
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800261 Error error;
262 vector<string> rpc_ids = manager.EnumerateAvailableServices(&error);
Chris Masone6791a432011-07-12 13:23:19 -0700263 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700264 EXPECT_EQ(2, ids.size());
265 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
266 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700267
Chris Masone9d779932011-08-25 16:33:41 -0700268 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
269 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
270
271 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700272}
273
Chris Masone6515aab2011-10-12 16:19:09 -0700274TEST_F(ManagerTest, RegisterKnownService) {
275 // It's much easier and safer to use a real GLib for this test.
276 GLib glib;
277 Manager manager(control_interface(),
278 dispatcher(),
279 &glib,
280 run_path(),
281 storage_path(),
282 string());
283 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
284 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700285 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700286 {
287 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
288 dispatcher(),
289 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700290 ASSERT_TRUE(profile->AdoptService(service1));
291 ASSERT_TRUE(profile->ContainsService(service1));
292 } // Force destruction of service1.
293
294 ServiceRefPtr service2(new ServiceUnderTest(control_interface(),
295 dispatcher(),
296 &manager));
297 manager.RegisterService(service2);
298 EXPECT_EQ(service2->profile().get(), profile.get());
299 manager.Stop();
300}
301
302TEST_F(ManagerTest, RegisterUnknownService) {
303 // It's much easier and safer to use a real GLib for this test.
304 GLib glib;
305 Manager manager(control_interface(),
306 dispatcher(),
307 &glib,
308 run_path(),
309 storage_path(),
310 string());
311 ProfileRefPtr profile(CreateProfileForManager(&manager, &glib));
312 ASSERT_TRUE(profile.get());
Paul Stewarta849a3d2011-11-03 05:54:09 -0700313 AdoptProfile(&manager, profile);
Chris Masone6515aab2011-10-12 16:19:09 -0700314 {
315 ServiceRefPtr service1(new ServiceUnderTest(control_interface(),
316 dispatcher(),
317 &manager));
Chris Masone6515aab2011-10-12 16:19:09 -0700318 ASSERT_TRUE(profile->AdoptService(service1));
319 ASSERT_TRUE(profile->ContainsService(service1));
320 } // Force destruction of service1.
321 scoped_refptr<MockService> mock_service2(
322 new NiceMock<MockService>(control_interface(),
323 dispatcher(),
324 &manager));
325 EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier())
326 .WillRepeatedly(Return(mock_service2->UniqueName()));
327 manager.RegisterService(mock_service2);
328 EXPECT_NE(mock_service2->profile().get(), profile.get());
329 manager.Stop();
330}
331
Chris Masonea8a2c252011-06-27 22:16:30 -0700332TEST_F(ManagerTest, GetProperties) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700333 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700334 AdoptProfile(manager(), profile);
Chris Masonea8a2c252011-06-27 22:16:30 -0700335 map<string, ::DBus::Variant> props;
336 Error error(Error::kInvalidProperty, "");
337 {
338 ::DBus::Error dbus_error;
339 string expected("portal_list");
mukesh agrawalde29fa82011-09-16 16:16:36 -0700340 manager()->mutable_store()->SetStringProperty(
341 flimflam::kCheckPortalListProperty,
342 expected,
343 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700344 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700345 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
346 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
347 expected);
348 }
349 {
350 ::DBus::Error dbus_error;
351 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700352 manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty,
353 expected,
354 &error);
Chris Masone9d779932011-08-25 16:33:41 -0700355 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700356 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
357 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
358 expected);
359 }
360}
361
Chris Masone3c3f6a12011-07-01 10:01:41 -0700362TEST_F(ManagerTest, GetDevicesProperty) {
Chris Masoneb9c00592011-10-06 13:10:39 -0700363 ProfileRefPtr profile(new MockProfile(control_interface(), manager(), ""));
Paul Stewarta849a3d2011-11-03 05:54:09 -0700364 AdoptProfile(manager(), profile);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800365 manager()->RegisterDevice(mock_devices_[0]);
366 manager()->RegisterDevice(mock_devices_[1]);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700367 {
368 map<string, ::DBus::Variant> props;
369 ::DBus::Error dbus_error;
Chris Masone9d779932011-08-25 16:33:41 -0700370 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700371 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
372 Strings devices =
373 props[flimflam::kDevicesProperty].operator vector<string>();
374 EXPECT_EQ(2, devices.size());
375 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700376}
377
Chris Masone6791a432011-07-12 13:23:19 -0700378TEST_F(ManagerTest, MoveService) {
Chris Masone2176a882011-09-14 22:29:15 -0700379 Manager manager(control_interface(),
380 dispatcher(),
Chris Masone6515aab2011-10-12 16:19:09 -0700381 glib(),
Chris Masone9d779932011-08-25 16:33:41 -0700382 run_path(),
383 storage_path(),
384 string());
Chris Masone6515aab2011-10-12 16:19:09 -0700385 scoped_refptr<MockService> s2(new MockService(control_interface(),
386 dispatcher(),
387 &manager));
388 // Inject an actual profile, backed by a fake StoreInterface
Chris Masoneb9c00592011-10-06 13:10:39 -0700389 {
Chris Masone6515aab2011-10-12 16:19:09 -0700390 Profile::Identifier id("irrelevant");
Chris Masoneb9c00592011-10-06 13:10:39 -0700391 ProfileRefPtr profile(
392 new Profile(control_interface(), &manager, id, "", false));
393 MockStore *storage = new MockStore;
Chris Masone6515aab2011-10-12 16:19:09 -0700394 // Say we don't have |s2| the first time asked, then that we do.
395 EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier()))
396 .WillOnce(Return(false))
397 .WillRepeatedly(Return(true));
398 EXPECT_CALL(*storage, Flush())
399 .Times(AnyNumber())
400 .WillRepeatedly(Return(true));
Chris Masoneb9c00592011-10-06 13:10:39 -0700401 profile->set_storage(storage);
Paul Stewarta849a3d2011-11-03 05:54:09 -0700402 AdoptProfile(&manager, profile);
Chris Masoneb9c00592011-10-06 13:10:39 -0700403 }
Chris Masone6515aab2011-10-12 16:19:09 -0700404 // Create a profile that already has |s2| in it.
405 ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager));
406 profile->AdoptService(s2);
Chris Masone9d779932011-08-25 16:33:41 -0700407
Chris Masone6515aab2011-10-12 16:19:09 -0700408 // Now, move the Service |s2| to another profile.
409 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
410 ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile()));
Chris Masone6791a432011-07-12 13:23:19 -0700411
412 // Force destruction of the original Profile, to ensure that the Service
413 // is kept alive and populated with data.
414 profile = NULL;
Chris Masone6515aab2011-10-12 16:19:09 -0700415 ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2));
Chris Masone9d779932011-08-25 16:33:41 -0700416 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700417}
418
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700419TEST_F(ManagerTest, CreateProfile) {
420 // It's much easier to use real Glib here since we want the storage
421 // side-effects.
422 GLib glib;
423 ScopedTempDir temp_dir;
424 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
425
426 Manager manager(control_interface(),
427 dispatcher(),
428 &glib,
429 run_path(),
430 storage_path(),
431 temp_dir.path().value());
432
433 // Invalid name should be rejected.
434 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, ""));
435
436 // Valid name is still rejected because we can't create a profile
437 // that doesn't have a user component. Such profile names are
438 // reserved for the single DefaultProfile the manager creates
439 // at startup.
440 EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid"));
441
442 // We should succeed in creating a valid user profile.
443 const char kProfile[] = "~user/profile";
444 EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile));
445
446 // We should fail in creating it a second time (already exists).
447 EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile));
448}
449
450TEST_F(ManagerTest, PushPopProfile) {
451 // It's much easier to use real Glib in creating a Manager for this
452 // test here since we want the storage side-effects.
453 GLib glib;
454 ScopedTempDir temp_dir;
455 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
456 Manager manager(control_interface(),
457 dispatcher(),
458 &glib,
459 run_path(),
460 storage_path(),
461 temp_dir.path().value());
462
463 // Pushing an invalid profile should fail.
464 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, ""));
465
466 // Pushing a default profile name should fail.
467 EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default"));
468
469 const char kProfile0[] = "~user/profile0";
470 const char kProfile1[] = "~user/profile1";
471
472 // Create a couple of profiles.
473 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0));
474 ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1));
475
476 // Push these profiles on the stack.
477 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0));
478 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1));
479
480 // Pushing a profile a second time should fail.
481 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0));
482 EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1));
483
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800484 Error error;
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700485 // Active profile should be the last one we pushed.
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800486 EXPECT_EQ(kProfile1, "~" + manager.GetActiveProfileName(&error));
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700487
488 // Make sure a profile name that doesn't exist fails.
489 const char kProfile2Id[] = "profile2";
490 const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id);
491 EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2));
492
493 // Create a new service, with a specific storage name.
494 scoped_refptr<MockService> service(
495 new NiceMock<MockService>(control_interface(),
496 dispatcher(),
497 &manager));
498 const char kServiceName[] = "service_storage_name";
499 EXPECT_CALL(*service.get(), GetStorageIdentifier())
500 .WillRepeatedly(Return(kServiceName));
501 EXPECT_CALL(*service.get(), Load(_))
502 .WillRepeatedly(Return(true));
503
504 // Add this service to the manager -- it should end up in the ephemeral
505 // profile.
506 manager.RegisterService(service);
507 ASSERT_EQ(manager.ephemeral_profile_, service->profile());
508
509 // Create storage for a profile that contains the service storage name.
510 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id,
511 kServiceName));
512
513 // When we push the profile, the service should move away from the
514 // ephemeral profile to this new profile since it has an entry for
515 // this service.
516 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2));
517 EXPECT_NE(manager.ephemeral_profile_, service->profile());
518 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
519
520 // Insert another profile that should supersede ownership of the service.
521 const char kProfile3Id[] = "profile3";
522 const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id);
523 ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id,
524 kServiceName));
525 EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3));
526 EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName());
527
528 // Popping an invalid profile name should fail.
529 EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~"));
530
531 // Popping an profile that is not at the top of the stack should fail.
532 EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0));
533
534 // Popping the top profile should succeed.
535 EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3));
536
537 // Moreover the service should have switched profiles to profile 2.
538 EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName());
539
540 // Popping the top profile should succeed.
541 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
542
543 // The service should now revert to the ephemeral profile.
544 EXPECT_EQ(manager.ephemeral_profile_, service->profile());
545
546 // Pop the remaining two services off the stack.
547 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
548 EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager));
549
550 // Next pop should fail with "stack is empty".
551 EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager));
552}
553
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700554TEST_F(ManagerTest, Dispatch) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700555 {
556 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700557 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700558 flimflam::kOfflineModeProperty,
559 PropertyStoreTest::kBoolV,
560 &error));
561 }
562 {
563 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700564 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700565 flimflam::kCountryProperty,
566 PropertyStoreTest::kStringV,
567 &error));
568 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700569 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700570 {
571 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700572 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700573 flimflam::kCountryProperty,
574 PropertyStoreTest::kBoolV,
575 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700576 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700577 }
578 {
579 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700580 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700581 flimflam::kOfflineModeProperty,
582 PropertyStoreTest::kStringV,
583 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700584 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700585 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700586 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700587 {
588 ::DBus::Error error;
589 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
mukesh agrawalde29fa82011-09-16 16:16:36 -0700590 manager()->mutable_store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700591 flimflam::kEnabledTechnologiesProperty,
592 PropertyStoreTest::kStringsV,
593 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700594 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700595 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700596}
597
mukesh agrawal32399322011-09-01 10:53:43 -0700598TEST_F(ManagerTest, RequestScan) {
599 {
600 Error error;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700601 manager()->RegisterDevice(mock_devices_[0].get());
602 manager()->RegisterDevice(mock_devices_[1].get());
603 EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -0700604 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700605 EXPECT_CALL(*mock_devices_[0], Scan(_));
606 EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi))
mukesh agrawal32399322011-09-01 10:53:43 -0700607 .WillRepeatedly(Return(false));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700608 EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -0700609 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700610 }
611
612 {
613 Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700614 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700615 EXPECT_EQ(Error::kInvalidArguments, error.type());
616 }
617}
618
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700619TEST_F(ManagerTest, GetWifiServiceNoDevice) {
620 KeyValueStore args;
621 Error e;
622 manager()->GetWifiService(args, &e);
623 EXPECT_EQ(Error::kInvalidArguments, e.type());
624 EXPECT_EQ("no wifi devices available", e.message());
625}
626
627TEST_F(ManagerTest, GetWifiService) {
628 KeyValueStore args;
629 Error e;
630 WiFiServiceRefPtr wifi_service;
631
632 manager()->RegisterDevice(mock_wifi_);
633 EXPECT_CALL(*mock_wifi_, GetService(_, _))
634 .WillRepeatedly(Return(wifi_service));
635 manager()->GetWifiService(args, &e);
636}
637
Paul Stewart22aa71b2011-09-16 12:15:11 -0700638TEST_F(ManagerTest, TechnologyOrder) {
639 Error error;
640 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
641 string(flimflam::kTypeWifi), &error);
642 ASSERT_TRUE(error.IsSuccess());
643 EXPECT_EQ(manager()->GetTechnologyOrder(),
644 string(flimflam::kTypeEthernet) + "," +
645 string(flimflam::kTypeWifi));
646
647 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," +
648 string(flimflam::kTypeWifi), &error);
649 ASSERT_FALSE(error.IsSuccess());
650 EXPECT_EQ(Error::kInvalidArguments, error.type());
651 EXPECT_EQ(string(flimflam::kTypeEthernet) + "," +
652 string(flimflam::kTypeWifi),
653 manager()->GetTechnologyOrder());
654}
655
656TEST_F(ManagerTest, SortServices) {
mukesh agrawal00917ce2011-11-22 23:56:55 +0000657 // TODO(quiche): Some of these tests would probably fit better in
658 // service_unittest, since the actual comparison of Services is
659 // implemented in Service. (crosbug.com/23370)
660
Paul Stewart22aa71b2011-09-16 12:15:11 -0700661 scoped_refptr<MockService> mock_service0(
662 new NiceMock<MockService>(control_interface(),
663 dispatcher(),
664 manager()));
665 scoped_refptr<MockService> mock_service1(
666 new NiceMock<MockService>(control_interface(),
667 dispatcher(),
668 manager()));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700669
670 manager()->RegisterService(mock_service0);
671 manager()->RegisterService(mock_service1);
672
673 // Services should already be sorted by UniqueName
674 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
675
676 // Asking explictly to sort services should not change anything
677 manager()->SortServices();
678 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
679
680 // Two otherwise equal services should be reordered by strength
681 mock_service1->set_strength(1);
682 manager()->UpdateService(mock_service1);
683 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
684
685 // Security
Paul Stewart1ca3e852011-11-04 07:50:49 -0700686 mock_service0->set_security_level(1);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700687 manager()->UpdateService(mock_service0);
688 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
689
690 // Technology
691 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi))
692 .WillRepeatedly(Return(true));
693 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet))
694 .WillRepeatedly(Return(true));
695 // NB: Redefine default (false) return values so we don't use the default rule
696 // which makes the logs noisier
697 EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi)))
698 .WillRepeatedly(Return(false));
699 EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet)))
700 .WillRepeatedly(Return(false));
701
702 Error error;
703 manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," +
704 string(flimflam::kTypeWifi), &error);
705 EXPECT_TRUE(error.IsSuccess());
706 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
707
708 manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," +
709 string(flimflam::kTypeEthernet), &error);
710 EXPECT_TRUE(error.IsSuccess());
711 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
712
Gaurav Shah435de2c2011-11-17 19:01:07 -0800713 // Priority.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700714 mock_service0->set_priority(1);
715 manager()->UpdateService(mock_service0);
716 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
717
718 // Favorite
mukesh agrawal00917ce2011-11-22 23:56:55 +0000719 mock_service1->MakeFavorite();
Paul Stewart22aa71b2011-09-16 12:15:11 -0700720 manager()->UpdateService(mock_service1);
721 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
722
Gaurav Shah435de2c2011-11-17 19:01:07 -0800723 // Connecting.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700724 EXPECT_CALL(*mock_service0.get(), state())
725 .WillRepeatedly(Return(Service::kStateAssociating));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800726 EXPECT_CALL(*mock_service0.get(), IsConnecting())
727 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700728 manager()->UpdateService(mock_service0);
729 EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1));
730
Gaurav Shah435de2c2011-11-17 19:01:07 -0800731 // Connected.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700732 EXPECT_CALL(*mock_service1.get(), state())
733 .WillRepeatedly(Return(Service::kStateConnected));
Gaurav Shah435de2c2011-11-17 19:01:07 -0800734 EXPECT_CALL(*mock_service1.get(), IsConnected())
735 .WillRepeatedly(Return(true));
Paul Stewart22aa71b2011-09-16 12:15:11 -0700736 manager()->UpdateService(mock_service1);
737 EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0));
738
739 manager()->DeregisterService(mock_service0);
740 manager()->DeregisterService(mock_service1);
741}
742
Gaurav Shah435de2c2011-11-17 19:01:07 -0800743TEST_F(ManagerTest, AvailableTechnologies) {
744 mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(),
745 dispatcher(),
746 manager(),
747 "null4",
748 "addr4",
749 0));
750 manager()->RegisterDevice(mock_devices_[0]);
751 manager()->RegisterDevice(mock_devices_[1]);
752 manager()->RegisterDevice(mock_devices_[2]);
753 manager()->RegisterDevice(mock_devices_[3]);
754
755 ON_CALL(*mock_devices_[0].get(), technology())
756 .WillByDefault(Return(Technology::kEthernet));
757 ON_CALL(*mock_devices_[1].get(), technology())
758 .WillByDefault(Return(Technology::kWifi));
759 ON_CALL(*mock_devices_[2].get(), technology())
760 .WillByDefault(Return(Technology::kCellular));
761 ON_CALL(*mock_devices_[3].get(), technology())
762 .WillByDefault(Return(Technology::kWifi));
763
764 set<string> expected_technologies;
765 expected_technologies.insert(Technology::NameFromIdentifier(
766 Technology::kEthernet));
767 expected_technologies.insert(Technology::NameFromIdentifier(
768 Technology::kWifi));
769 expected_technologies.insert(Technology::NameFromIdentifier(
770 Technology::kCellular));
771 Error error;
772 vector<string> technologies = manager()->AvailableTechnologies(&error);
773
774 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
775 ContainerEq(expected_technologies));
776}
777
778TEST_F(ManagerTest, ConnectedTechnologies) {
779 scoped_refptr<MockService> connected_service1(
780 new NiceMock<MockService>(control_interface(),
781 dispatcher(),
782 manager()));
783 scoped_refptr<MockService> connected_service2(
784 new NiceMock<MockService>(control_interface(),
785 dispatcher(),
786 manager()));
787 scoped_refptr<MockService> disconnected_service1(
788 new NiceMock<MockService>(control_interface(),
789 dispatcher(),
790 manager()));
791 scoped_refptr<MockService> disconnected_service2(
792 new NiceMock<MockService>(control_interface(),
793 dispatcher(),
794 manager()));
795
796 ON_CALL(*connected_service1.get(), IsConnected())
797 .WillByDefault(Return(true));
798 ON_CALL(*connected_service2.get(), IsConnected())
799 .WillByDefault(Return(true));
800
801 manager()->RegisterService(connected_service1);
802 manager()->RegisterService(connected_service2);
803 manager()->RegisterService(disconnected_service1);
804 manager()->RegisterService(disconnected_service2);
805
806 manager()->RegisterDevice(mock_devices_[0]);
807 manager()->RegisterDevice(mock_devices_[1]);
808 manager()->RegisterDevice(mock_devices_[2]);
809 manager()->RegisterDevice(mock_devices_[3]);
810
811 ON_CALL(*mock_devices_[0].get(), technology())
812 .WillByDefault(Return(Technology::kEthernet));
813 ON_CALL(*mock_devices_[1].get(), technology())
814 .WillByDefault(Return(Technology::kWifi));
815 ON_CALL(*mock_devices_[2].get(), technology())
816 .WillByDefault(Return(Technology::kCellular));
817 ON_CALL(*mock_devices_[3].get(), technology())
818 .WillByDefault(Return(Technology::kWifi));
819
820 mock_devices_[0]->SelectService(connected_service1);
821 mock_devices_[1]->SelectService(disconnected_service1);
822 mock_devices_[2]->SelectService(disconnected_service2);
823 mock_devices_[3]->SelectService(connected_service2);
824
825 set<string> expected_technologies;
826 expected_technologies.insert(Technology::NameFromIdentifier(
827 Technology::kEthernet));
828 expected_technologies.insert(Technology::NameFromIdentifier(
829 Technology::kWifi));
830 Error error;
831
832 vector<string> technologies = manager()->ConnectedTechnologies(&error);
833 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
834 ContainerEq(expected_technologies));
835}
836
837TEST_F(ManagerTest, DefaultTechnology) {
838 scoped_refptr<MockService> connected_service(
839 new NiceMock<MockService>(control_interface(),
840 dispatcher(),
841 manager()));
842 scoped_refptr<MockService> disconnected_service(
843 new NiceMock<MockService>(control_interface(),
844 dispatcher(),
845 manager()));
846
847 // Connected. WiFi.
848 ON_CALL(*connected_service.get(), IsConnected())
849 .WillByDefault(Return(true));
850 ON_CALL(*connected_service.get(), state())
851 .WillByDefault(Return(Service::kStateConnected));
852 ON_CALL(*connected_service.get(), technology())
853 .WillByDefault(Return(Technology::kWifi));
854
855 // Disconnected. Ethernet.
856 ON_CALL(*disconnected_service.get(), technology())
857 .WillByDefault(Return(Technology::kEthernet));
858
859 manager()->RegisterService(disconnected_service);
860 Error error;
861 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(""));
862
863
864 manager()->RegisterService(connected_service);
865 // Connected service should be brought to the front now.
866 string expected_technology =
867 Technology::NameFromIdentifier(Technology::kWifi);
868 EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
869}
870
Thieu Le1271d682011-11-02 22:48:19 +0000871TEST_F(ManagerTest, DisconnectServicesOnStop) {
872 scoped_refptr<MockService> mock_service(
873 new NiceMock<MockService>(control_interface(),
874 dispatcher(),
875 manager()));
876 manager()->RegisterService(mock_service);
877 EXPECT_CALL(*mock_service.get(), Disconnect()).Times(1);
878 manager()->Stop();
879}
880
mukesh agrawal00917ce2011-11-22 23:56:55 +0000881TEST_F(ManagerTest, UpdateServiceConnected) {
882 scoped_refptr<MockService> mock_service(
883 new NiceMock<MockService>(control_interface(),
884 dispatcher(),
885 manager()));
886 manager()->RegisterService(mock_service);
887 EXPECT_FALSE(mock_service->favorite());
888 EXPECT_FALSE(mock_service->auto_connect());
889
Gaurav Shah435de2c2011-11-17 19:01:07 -0800890 EXPECT_CALL(*mock_service.get(), IsConnected())
891 .WillRepeatedly(Return(true));
mukesh agrawal00917ce2011-11-22 23:56:55 +0000892 manager()->UpdateService(mock_service);
893 // We can't EXPECT_CALL(..., MakeFavorite), because that requires us
894 // to mock out MakeFavorite. And mocking that out would break the
895 // SortServices test. (crosbug.com/23370)
896 EXPECT_TRUE(mock_service->favorite());
897 EXPECT_TRUE(mock_service->auto_connect());
898}
899
Chris Masone9be4a9d2011-05-16 15:44:09 -0700900} // namespace shill