Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1 | // 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 4 | |
| 5 | #include "shill/manager.h" |
| 6 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 7 | #include <set> |
| 8 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 9 | #include <glib.h> |
| 10 | |
| 11 | #include <base/callback_old.h> |
| 12 | #include <base/logging.h> |
| 13 | #include <base/memory/ref_counted.h> |
| 14 | #include <base/message_loop.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 15 | #include <base/stl_util-inl.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 20 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 21 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 22 | #include "shill/mock_glib.h" |
| 23 | #include "shill/mock_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 24 | #include "shill/mock_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 25 | #include "shill/property_store_unittest.h" |
| 26 | #include "shill/shill_event.h" |
| 27 | |
| 28 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 29 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | using std::vector; |
| 32 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | namespace shill { |
| 34 | using ::testing::Test; |
| 35 | using ::testing::_; |
| 36 | using ::testing::NiceMock; |
| 37 | using ::testing::Return; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 38 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 40 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 41 | ManagerTest() |
| 42 | : factory_(this), |
| 43 | mock_device_(new NiceMock<MockDevice>(&control_interface_, |
| 44 | &dispatcher_, |
| 45 | &manager_, |
| 46 | "null0", |
| 47 | -1)), |
| 48 | mock_device2_(new NiceMock<MockDevice>(&control_interface_, |
| 49 | &dispatcher_, |
| 50 | &manager_, |
| 51 | "null1", |
| 52 | -1)) { |
| 53 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 54 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 55 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 56 | bool IsDeviceRegistered(const DeviceRefPtr &device, Device::Technology tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 57 | vector<DeviceRefPtr> devices; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 58 | manager_.FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 59 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 62 | protected: |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 63 | ScopedRunnableMethodFactory<ManagerTest> factory_; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 64 | scoped_refptr<MockDevice> mock_device_; |
| 65 | scoped_refptr<MockDevice> mock_device2_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 68 | TEST_F(ManagerTest, Contains) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(manager_.store()->Contains(flimflam::kStateProperty)); |
| 70 | EXPECT_FALSE(manager_.store()->Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 73 | TEST_F(ManagerTest, DeviceRegistration) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 74 | ON_CALL(*mock_device_.get(), TechnologyIs(Device::kEthernet)) |
| 75 | .WillByDefault(Return(true)); |
| 76 | ON_CALL(*mock_device2_.get(), TechnologyIs(Device::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 77 | .WillByDefault(Return(true)); |
| 78 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 79 | manager_.RegisterDevice(mock_device_.get()); |
| 80 | manager_.RegisterDevice(mock_device2_.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 81 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(IsDeviceRegistered(mock_device_, Device::kEthernet)); |
| 83 | EXPECT_TRUE(IsDeviceRegistered(mock_device2_, Device::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(ManagerTest, DeviceDeregistration) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 87 | ON_CALL(*mock_device_.get(), TechnologyIs(Device::kEthernet)) |
| 88 | .WillByDefault(Return(true)); |
| 89 | ON_CALL(*mock_device2_.get(), TechnologyIs(Device::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 90 | .WillByDefault(Return(true)); |
| 91 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 92 | manager_.RegisterDevice(mock_device_.get()); |
| 93 | manager_.RegisterDevice(mock_device2_.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 94 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(IsDeviceRegistered(mock_device_, Device::kEthernet)); |
| 96 | ASSERT_TRUE(IsDeviceRegistered(mock_device2_, Device::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 97 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 98 | manager_.DeregisterDevice(mock_device_.get()); |
| 99 | EXPECT_FALSE(IsDeviceRegistered(mock_device_, Device::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 100 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 101 | manager_.DeregisterDevice(mock_device2_.get()); |
| 102 | EXPECT_FALSE(IsDeviceRegistered(mock_device2_, Device::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 106 | scoped_refptr<MockService> mock_service( |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 107 | new NiceMock<MockService>(&control_interface_, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 108 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 109 | &manager_)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 110 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 111 | new NiceMock<MockService>(&control_interface_, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 112 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 113 | &manager_)); |
| 114 | string service1_name(mock_service->UniqueName()); |
| 115 | string service2_name(mock_service2->UniqueName()); |
| 116 | |
| 117 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 118 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 119 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 120 | .WillRepeatedly(Return(service2_name)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 121 | |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 122 | manager_.RegisterService(mock_service); |
| 123 | manager_.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 124 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 125 | vector<string> rpc_ids = manager_.EnumerateAvailableServices(); |
| 126 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 127 | EXPECT_EQ(2, ids.size()); |
| 128 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 129 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 130 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 131 | EXPECT_TRUE(manager_.FindService(service1_name).get() != NULL); |
| 132 | EXPECT_TRUE(manager_.FindService(service2_name).get() != NULL); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 135 | TEST_F(ManagerTest, GetProperties) { |
| 136 | map<string, ::DBus::Variant> props; |
| 137 | Error error(Error::kInvalidProperty, ""); |
| 138 | { |
| 139 | ::DBus::Error dbus_error; |
| 140 | string expected("portal_list"); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 141 | manager_.store()->SetStringProperty(flimflam::kCheckPortalListProperty, |
| 142 | expected, |
| 143 | &error); |
| 144 | DBusAdaptor::GetProperties(manager_.store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 145 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 146 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 147 | expected); |
| 148 | } |
| 149 | { |
| 150 | ::DBus::Error dbus_error; |
| 151 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 152 | manager_.store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 153 | expected, |
| 154 | &error); |
| 155 | DBusAdaptor::GetProperties(manager_.store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 156 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 157 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 158 | expected); |
| 159 | } |
| 160 | } |
| 161 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 162 | TEST_F(ManagerTest, GetDevicesProperty) { |
| 163 | manager_.RegisterDevice(mock_device_.get()); |
| 164 | manager_.RegisterDevice(mock_device2_.get()); |
| 165 | { |
| 166 | map<string, ::DBus::Variant> props; |
| 167 | ::DBus::Error dbus_error; |
| 168 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 169 | DBusAdaptor::GetProperties(manager_.store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 170 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 171 | Strings devices = |
| 172 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 173 | EXPECT_EQ(2, devices.size()); |
| 174 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 177 | TEST_F(ManagerTest, MoveService) { |
| 178 | // I want to ensure that the Profiles are managing this Service object |
| 179 | // lifetime properly, so I can't hold a ref to it here. |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 180 | ProfileRefPtr profile( |
| 181 | new MockProfile(&control_interface_, &glib_, &manager_, "")); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 182 | string service_name; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 183 | { |
| 184 | ServiceRefPtr s2( |
| 185 | new MockService(&control_interface_, |
| 186 | &dispatcher_, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 187 | &manager_)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 188 | profile->AdoptService(s2); |
| 189 | s2->set_profile(profile); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 190 | service_name = s2->UniqueName(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Now, move the |service| to another profile. |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 194 | manager_.MoveToActiveProfile(profile, profile->FindService(service_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 195 | |
| 196 | // Force destruction of the original Profile, to ensure that the Service |
| 197 | // is kept alive and populated with data. |
| 198 | profile = NULL; |
| 199 | { |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 200 | ServiceRefPtr serv(manager_.ActiveProfile()->FindService(service_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 201 | ASSERT_TRUE(serv.get() != NULL); |
| 202 | Error error(Error::kInvalidProperty, ""); |
| 203 | ::DBus::Error dbus_error; |
| 204 | map<string, ::DBus::Variant> props; |
| 205 | bool expected = true; |
| 206 | serv->store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 207 | expected, |
| 208 | &error); |
| 209 | DBusAdaptor::GetProperties(serv->store(), &props, &dbus_error); |
| 210 | ASSERT_TRUE(ContainsKey(props, flimflam::kAutoConnectProperty)); |
| 211 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 212 | expected); |
| 213 | } |
| 214 | } |
| 215 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 216 | TEST_F(ManagerTest, Dispatch) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 217 | { |
| 218 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 219 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager_.store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 220 | flimflam::kOfflineModeProperty, |
| 221 | PropertyStoreTest::kBoolV, |
| 222 | &error)); |
| 223 | } |
| 224 | { |
| 225 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 226 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager_.store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 227 | flimflam::kCountryProperty, |
| 228 | PropertyStoreTest::kStringV, |
| 229 | &error)); |
| 230 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 231 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 232 | { |
| 233 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 234 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager_.store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 235 | flimflam::kCountryProperty, |
| 236 | PropertyStoreTest::kBoolV, |
| 237 | &error)); |
| 238 | EXPECT_EQ(invalid_args_, error.name()); |
| 239 | } |
| 240 | { |
| 241 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 242 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager_.store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 243 | flimflam::kOfflineModeProperty, |
| 244 | PropertyStoreTest::kStringV, |
| 245 | &error)); |
| 246 | EXPECT_EQ(invalid_args_, error.name()); |
| 247 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 248 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 249 | { |
| 250 | ::DBus::Error error; |
| 251 | EXPECT_FALSE(DBusAdaptor::DispatchOnType( |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 252 | manager_.store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 253 | flimflam::kEnabledTechnologiesProperty, |
| 254 | PropertyStoreTest::kStringsV, |
| 255 | &error)); |
| 256 | EXPECT_EQ(invalid_args_, error.name()); |
| 257 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 260 | } // namespace shill |