blob: d6a382a5ed04fc7f271f418e715e09f4a16ce364 [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>
Chris Masone6791a432011-07-12 13:23:19 -070012#include <base/stl_util-inl.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013#include <chromeos/dbus/service_constants.h>
Chris Masone7156c922011-08-23 20:36:21 -070014#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070015#include <gtest/gtest.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070016
mukesh agrawal32399322011-09-01 10:53:43 -070017#include "shill/adaptor_interfaces.h"
18#include "shill/error.h"
19#include "shill/mock_adaptors.h"
Chris Masoned7732e42011-05-20 11:08:56 -070020#include "shill/mock_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070021#include "shill/mock_device.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070022#include "shill/mock_glib.h"
23#include "shill/mock_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070024#include "shill/mock_service.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070025#include "shill/property_store_unittest.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070026
27using std::map;
Chris Masone6791a432011-07-12 13:23:19 -070028using std::set;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070029using std::string;
30using std::vector;
31
Chris Masone9be4a9d2011-05-16 15:44:09 -070032namespace shill {
Chris Masone9be4a9d2011-05-16 15:44:09 -070033using ::testing::_;
34using ::testing::NiceMock;
35using ::testing::Return;
Chris Masone9d779932011-08-25 16:33:41 -070036using ::testing::Test;
Chris Masone9be4a9d2011-05-16 15:44:09 -070037
Chris Masone3bd3c8c2011-06-13 08:20:26 -070038class ManagerTest : public PropertyStoreTest {
Chris Masone9be4a9d2011-05-16 15:44:09 -070039 public:
Chris Masone3c3f6a12011-07-01 10:01:41 -070040 ManagerTest()
Chris Masone2176a882011-09-14 22:29:15 -070041 : mock_device_(new NiceMock<MockDevice>(control_interface(),
42 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -070043 manager(),
Chris Masone3c3f6a12011-07-01 10:01:41 -070044 "null0",
Chris Masone626719f2011-08-18 16:58:48 -070045 "addr0",
Darin Petkov6f9eaa32011-08-09 15:26:44 -070046 0)),
Chris Masone2176a882011-09-14 22:29:15 -070047 mock_device2_(new NiceMock<MockDevice>(control_interface(),
48 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -070049 manager(),
Darin Petkov6f9eaa32011-08-09 15:26:44 -070050 "null2",
Chris Masone626719f2011-08-18 16:58:48 -070051 "addr2",
Darin Petkov6f9eaa32011-08-09 15:26:44 -070052 2)),
Chris Masone2176a882011-09-14 22:29:15 -070053 mock_device3_(new NiceMock<MockDevice>(control_interface(),
54 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -070055 manager(),
Darin Petkov6f9eaa32011-08-09 15:26:44 -070056 "null3",
Chris Masone626719f2011-08-18 16:58:48 -070057 "addr3",
Darin Petkov6f9eaa32011-08-09 15:26:44 -070058 3)) {
Chris Masone3c3f6a12011-07-01 10:01:41 -070059 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060 virtual ~ManagerTest() {}
Chris Masone9be4a9d2011-05-16 15:44:09 -070061
Chris Masone2b105542011-06-22 10:58:09 -070062 bool IsDeviceRegistered(const DeviceRefPtr &device, Device::Technology tech) {
Chris Masonec1e50412011-06-07 13:04:53 -070063 vector<DeviceRefPtr> devices;
Chris Masone9d779932011-08-25 16:33:41 -070064 manager()->FilterByTechnology(tech, &devices);
Chris Masone2b105542011-06-22 10:58:09 -070065 return (devices.size() == 1 && devices[0].get() == device.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -070066 }
67
Paul Stewartf1ce5d22011-05-19 13:10:20 -070068 protected:
Chris Masone3c3f6a12011-07-01 10:01:41 -070069 scoped_refptr<MockDevice> mock_device_;
70 scoped_refptr<MockDevice> mock_device2_;
Darin Petkov6f9eaa32011-08-09 15:26:44 -070071 scoped_refptr<MockDevice> mock_device3_;
Chris Masone9be4a9d2011-05-16 15:44:09 -070072};
73
Chris Masone3bd3c8c2011-06-13 08:20:26 -070074TEST_F(ManagerTest, Contains) {
Chris Masone9d779932011-08-25 16:33:41 -070075 EXPECT_TRUE(manager()->store()->Contains(flimflam::kStateProperty));
76 EXPECT_FALSE(manager()->store()->Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070077}
78
Chris Masone9be4a9d2011-05-16 15:44:09 -070079TEST_F(ManagerTest, DeviceRegistration) {
Chris Masone3c3f6a12011-07-01 10:01:41 -070080 ON_CALL(*mock_device_.get(), TechnologyIs(Device::kEthernet))
81 .WillByDefault(Return(true));
82 ON_CALL(*mock_device2_.get(), TechnologyIs(Device::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -070083 .WillByDefault(Return(true));
Darin Petkov6f9eaa32011-08-09 15:26:44 -070084 ON_CALL(*mock_device3_.get(), TechnologyIs(Device::kCellular))
85 .WillByDefault(Return(true));
Chris Masone9be4a9d2011-05-16 15:44:09 -070086
Chris Masone9d779932011-08-25 16:33:41 -070087 manager()->RegisterDevice(mock_device_);
88 manager()->RegisterDevice(mock_device2_);
89 manager()->RegisterDevice(mock_device3_);
Chris Masone9be4a9d2011-05-16 15:44:09 -070090
Chris Masone3c3f6a12011-07-01 10:01:41 -070091 EXPECT_TRUE(IsDeviceRegistered(mock_device_, Device::kEthernet));
92 EXPECT_TRUE(IsDeviceRegistered(mock_device2_, Device::kWifi));
Darin Petkov6f9eaa32011-08-09 15:26:44 -070093 EXPECT_TRUE(IsDeviceRegistered(mock_device3_, Device::kCellular));
Chris Masone9be4a9d2011-05-16 15:44:09 -070094}
95
96TEST_F(ManagerTest, DeviceDeregistration) {
Chris Masone3c3f6a12011-07-01 10:01:41 -070097 ON_CALL(*mock_device_.get(), TechnologyIs(Device::kEthernet))
98 .WillByDefault(Return(true));
99 ON_CALL(*mock_device2_.get(), TechnologyIs(Device::kWifi))
Chris Masone9be4a9d2011-05-16 15:44:09 -0700100 .WillByDefault(Return(true));
101
Chris Masone9d779932011-08-25 16:33:41 -0700102 manager()->RegisterDevice(mock_device_.get());
103 manager()->RegisterDevice(mock_device2_.get());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700104
Chris Masone3c3f6a12011-07-01 10:01:41 -0700105 ASSERT_TRUE(IsDeviceRegistered(mock_device_, Device::kEthernet));
106 ASSERT_TRUE(IsDeviceRegistered(mock_device2_, Device::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700107
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700108 EXPECT_CALL(*mock_device_.get(), Stop());
Chris Masone9d779932011-08-25 16:33:41 -0700109 manager()->DeregisterDevice(mock_device_.get());
Chris Masone3c3f6a12011-07-01 10:01:41 -0700110 EXPECT_FALSE(IsDeviceRegistered(mock_device_, Device::kEthernet));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700111
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700112 EXPECT_CALL(*mock_device2_.get(), Stop());
Chris Masone9d779932011-08-25 16:33:41 -0700113 manager()->DeregisterDevice(mock_device2_.get());
Chris Masone3c3f6a12011-07-01 10:01:41 -0700114 EXPECT_FALSE(IsDeviceRegistered(mock_device2_, Device::kWifi));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700115}
116
117TEST_F(ManagerTest, ServiceRegistration) {
Chris Masone9d779932011-08-25 16:33:41 -0700118 // It's much easier and safer to use a real GLib for this test.
119 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700120 Manager manager(control_interface(),
121 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700122 &glib,
123 run_path(),
124 storage_path(),
125 string());
Chris Masone9be4a9d2011-05-16 15:44:09 -0700126 scoped_refptr<MockService> mock_service(
Chris Masone2176a882011-09-14 22:29:15 -0700127 new NiceMock<MockService>(control_interface(),
128 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700129 &manager));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700130 scoped_refptr<MockService> mock_service2(
Chris Masone2176a882011-09-14 22:29:15 -0700131 new NiceMock<MockService>(control_interface(),
132 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700133 &manager));
mukesh agrawal51a7e932011-07-27 16:18:26 -0700134 string service1_name(mock_service->UniqueName());
135 string service2_name(mock_service2->UniqueName());
136
137 EXPECT_CALL(*mock_service.get(), GetRpcIdentifier())
138 .WillRepeatedly(Return(service1_name));
Chris Masone6791a432011-07-12 13:23:19 -0700139 EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier())
mukesh agrawal51a7e932011-07-27 16:18:26 -0700140 .WillRepeatedly(Return(service2_name));
mukesh agrawal32399322011-09-01 10:53:43 -0700141 // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154)
Chris Masone9d779932011-08-25 16:33:41 -0700142 // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()),
mukesh agrawal32399322011-09-01 10:53:43 -0700143 // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _));
Chris Masone9be4a9d2011-05-16 15:44:09 -0700144
Chris Masone9d779932011-08-25 16:33:41 -0700145 manager.RegisterService(mock_service);
146 manager.RegisterService(mock_service2);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700147
Chris Masone9d779932011-08-25 16:33:41 -0700148 vector<string> rpc_ids = manager.EnumerateAvailableServices();
Chris Masone6791a432011-07-12 13:23:19 -0700149 set<string> ids(rpc_ids.begin(), rpc_ids.end());
mukesh agrawal51a7e932011-07-27 16:18:26 -0700150 EXPECT_EQ(2, ids.size());
151 EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier()));
152 EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier()));
Chris Masone6791a432011-07-12 13:23:19 -0700153
Chris Masone9d779932011-08-25 16:33:41 -0700154 EXPECT_TRUE(manager.FindService(service1_name).get() != NULL);
155 EXPECT_TRUE(manager.FindService(service2_name).get() != NULL);
156
157 manager.Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700158}
159
Chris Masonea8a2c252011-06-27 22:16:30 -0700160TEST_F(ManagerTest, GetProperties) {
161 map<string, ::DBus::Variant> props;
162 Error error(Error::kInvalidProperty, "");
163 {
164 ::DBus::Error dbus_error;
165 string expected("portal_list");
Chris Masone9d779932011-08-25 16:33:41 -0700166 manager()->store()->SetStringProperty(flimflam::kCheckPortalListProperty,
167 expected,
168 &error);
169 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700170 ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end());
171 EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(),
172 expected);
173 }
174 {
175 ::DBus::Error dbus_error;
176 bool expected = true;
Chris Masone9d779932011-08-25 16:33:41 -0700177 manager()->store()->SetBoolProperty(flimflam::kOfflineModeProperty,
178 expected,
179 &error);
180 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700181 ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end());
182 EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(),
183 expected);
184 }
185}
186
Chris Masone3c3f6a12011-07-01 10:01:41 -0700187TEST_F(ManagerTest, GetDevicesProperty) {
Chris Masone9d779932011-08-25 16:33:41 -0700188 manager()->RegisterDevice(mock_device_.get());
189 manager()->RegisterDevice(mock_device2_.get());
Chris Masone3c3f6a12011-07-01 10:01:41 -0700190 {
191 map<string, ::DBus::Variant> props;
192 ::DBus::Error dbus_error;
193 bool expected = true;
Chris Masone9d779932011-08-25 16:33:41 -0700194 DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error);
Chris Masone3c3f6a12011-07-01 10:01:41 -0700195 ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end());
196 Strings devices =
197 props[flimflam::kDevicesProperty].operator vector<string>();
198 EXPECT_EQ(2, devices.size());
199 }
Chris Masone3c3f6a12011-07-01 10:01:41 -0700200}
201
Chris Masone6791a432011-07-12 13:23:19 -0700202TEST_F(ManagerTest, MoveService) {
Chris Masone9d779932011-08-25 16:33:41 -0700203 // It's much easier and safer to use a real GLib for this test.
204 GLib glib;
Chris Masone2176a882011-09-14 22:29:15 -0700205 Manager manager(control_interface(),
206 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700207 &glib,
208 run_path(),
209 storage_path(),
210 string());
211
Chris Masone6791a432011-07-12 13:23:19 -0700212 // I want to ensure that the Profiles are managing this Service object
213 // lifetime properly, so I can't hold a ref to it here.
Chris Masone2176a882011-09-14 22:29:15 -0700214 ProfileRefPtr profile(new MockProfile(control_interface(), &manager, ""));
mukesh agrawal51a7e932011-07-27 16:18:26 -0700215 string service_name;
Chris Masone6791a432011-07-12 13:23:19 -0700216 {
Chris Masone9d779932011-08-25 16:33:41 -0700217 scoped_refptr<MockService> s2(
Chris Masone2176a882011-09-14 22:29:15 -0700218 new MockService(control_interface(),
219 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700220 &manager));
221 EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true));
222
Chris Masone6791a432011-07-12 13:23:19 -0700223 profile->AdoptService(s2);
224 s2->set_profile(profile);
mukesh agrawal51a7e932011-07-27 16:18:26 -0700225 service_name = s2->UniqueName();
Chris Masone6791a432011-07-12 13:23:19 -0700226 }
227
228 // Now, move the |service| to another profile.
Chris Masone9d779932011-08-25 16:33:41 -0700229 ASSERT_TRUE(manager.MoveToActiveProfile(profile,
230 profile->FindService(service_name)));
Chris Masone6791a432011-07-12 13:23:19 -0700231
232 // Force destruction of the original Profile, to ensure that the Service
233 // is kept alive and populated with data.
234 profile = NULL;
235 {
Chris Masone9d779932011-08-25 16:33:41 -0700236 ServiceRefPtr serv(manager.ActiveProfile()->FindService(service_name));
Chris Masone6791a432011-07-12 13:23:19 -0700237 ASSERT_TRUE(serv.get() != NULL);
238 Error error(Error::kInvalidProperty, "");
239 ::DBus::Error dbus_error;
240 map<string, ::DBus::Variant> props;
241 bool expected = true;
242 serv->store()->SetBoolProperty(flimflam::kAutoConnectProperty,
243 expected,
244 &error);
245 DBusAdaptor::GetProperties(serv->store(), &props, &dbus_error);
246 ASSERT_TRUE(ContainsKey(props, flimflam::kAutoConnectProperty));
247 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
248 expected);
249 }
Chris Masone9d779932011-08-25 16:33:41 -0700250 manager.Stop();
Chris Masone6791a432011-07-12 13:23:19 -0700251}
252
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700253TEST_F(ManagerTest, Dispatch) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700254 {
255 ::DBus::Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700256 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700257 flimflam::kOfflineModeProperty,
258 PropertyStoreTest::kBoolV,
259 &error));
260 }
261 {
262 ::DBus::Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700263 EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700264 flimflam::kCountryProperty,
265 PropertyStoreTest::kStringV,
266 &error));
267 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700268 // Attempt to write with value of wrong type should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700269 {
270 ::DBus::Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700271 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700272 flimflam::kCountryProperty,
273 PropertyStoreTest::kBoolV,
274 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700275 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700276 }
277 {
278 ::DBus::Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700279 EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700280 flimflam::kOfflineModeProperty,
281 PropertyStoreTest::kStringV,
282 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700283 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700284 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700285 // Attempt to write R/O property should return InvalidArgs.
Chris Masonea8a2c252011-06-27 22:16:30 -0700286 {
287 ::DBus::Error error;
288 EXPECT_FALSE(DBusAdaptor::DispatchOnType(
Chris Masone9d779932011-08-25 16:33:41 -0700289 manager()->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700290 flimflam::kEnabledTechnologiesProperty,
291 PropertyStoreTest::kStringsV,
292 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700293 EXPECT_EQ(invalid_args(), error.name());
Chris Masonea8a2c252011-06-27 22:16:30 -0700294 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700295}
296
mukesh agrawal32399322011-09-01 10:53:43 -0700297TEST_F(ManagerTest, RequestScan) {
298 {
299 Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700300 manager()->RegisterDevice(mock_device_.get());
301 manager()->RegisterDevice(mock_device2_.get());
mukesh agrawal32399322011-09-01 10:53:43 -0700302 EXPECT_CALL(*mock_device_, TechnologyIs(Device::kWifi))
303 .WillRepeatedly(Return(true));
304 EXPECT_CALL(*mock_device_, Scan());
305 EXPECT_CALL(*mock_device2_, TechnologyIs(Device::kWifi))
306 .WillRepeatedly(Return(false));
307 EXPECT_CALL(*mock_device2_, Scan()).Times(0);
Chris Masone9d779932011-08-25 16:33:41 -0700308 manager()->RequestScan(flimflam::kTypeWifi, &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700309 }
310
311 {
312 Error error;
Chris Masone9d779932011-08-25 16:33:41 -0700313 manager()->RequestScan("bogus_device_type", &error);
mukesh agrawal32399322011-09-01 10:53:43 -0700314 EXPECT_EQ(Error::kInvalidArguments, error.type());
315 }
316}
317
Chris Masone9be4a9d2011-05-16 15:44:09 -0700318} // namespace shill