mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 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 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_temp_dir.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 13 | #include <base/stl_util-inl.h> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 14 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 18 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 19 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 20 | #include "shill/ephemeral_profile.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 21 | #include "shill/error.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 22 | #include "shill/glib.h" |
| 23 | #include "shill/key_file_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 24 | #include "shill/key_value_store.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 25 | #include "shill/mock_adaptors.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 26 | #include "shill/mock_connection.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 27 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 28 | #include "shill/mock_device.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 29 | #include "shill/mock_device_info.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 30 | #include "shill/mock_glib.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 31 | #include "shill/mock_metrics.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/mock_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 34 | #include "shill/mock_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 35 | #include "shill/mock_wifi.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 37 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | #include "shill/wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | |
| 40 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 41 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 46 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 47 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 48 | using ::testing::ContainerEq; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 49 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 50 | using ::testing::NiceMock; |
| 51 | using ::testing::Return; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 52 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 53 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 54 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 55 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 56 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 57 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 58 | ManagerTest() |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 59 | : mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 60 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 61 | metrics(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 62 | manager(), |
| 63 | "wifi0", |
| 64 | "addr4", |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 65 | 4)), |
| 66 | device_info_(new NiceMock<MockDeviceInfo>( |
| 67 | control_interface(), |
| 68 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 69 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 70 | reinterpret_cast<Manager*>(NULL))), |
| 71 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 72 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 73 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 75 | manager(), |
| 76 | "null0", |
| 77 | "addr0", |
| 78 | 0)); |
| 79 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 80 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 81 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 82 | manager(), |
| 83 | "null1", |
| 84 | "addr1", |
| 85 | 1)); |
| 86 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 87 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 88 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 89 | manager(), |
| 90 | "null2", |
| 91 | "addr2", |
| 92 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 93 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 94 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 95 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 96 | manager(), |
| 97 | "null3", |
| 98 | "addr3", |
| 99 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 100 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 101 | |
| 102 | // Replace the manager's adaptor with a quieter one, and one |
| 103 | // we can do EXPECT*() against. Passes ownership. |
| 104 | manager()->adaptor_.reset(manager_adaptor_); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 105 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 106 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 107 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 108 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 109 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 110 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 111 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 112 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 113 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 114 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 115 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 116 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 117 | manager->profiles_.push_back(profile); |
| 118 | } |
| 119 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 120 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 121 | return manager->ephemeral_profile_; |
| 122 | } |
| 123 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 124 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 125 | Profile::Identifier id("rather", "irrelevant"); |
| 126 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 127 | manager, |
| 128 | id, |
| 129 | "", |
| 130 | false)); |
| 131 | FilePath final_path(storage_path()); |
| 132 | final_path = final_path.Append("test.profile"); |
| 133 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 134 | storage->set_path(final_path); |
| 135 | if (!storage->Open()) |
| 136 | return NULL; |
| 137 | profile->set_storage(storage.release()); // Passes ownership. |
| 138 | return profile.release(); |
| 139 | } |
| 140 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 141 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 142 | const string &profile_identifier, |
| 143 | const string &service_name) { |
| 144 | GLib glib; |
| 145 | KeyFileStore store(&glib); |
| 146 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 147 | return store.Open() && |
| 148 | store.SetString(service_name, "rather", "irrelevant") && |
| 149 | store.Close(); |
| 150 | } |
| 151 | |
| 152 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 153 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 154 | string path; |
| 155 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 156 | return error.type(); |
| 157 | } |
| 158 | |
| 159 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 160 | Error error; |
| 161 | manager->PopAnyProfile(&error); |
| 162 | return error.type(); |
| 163 | } |
| 164 | |
| 165 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 166 | Error error; |
| 167 | manager->PopProfile(name, &error); |
| 168 | return error.type(); |
| 169 | } |
| 170 | |
| 171 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 172 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 173 | string path; |
| 174 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 175 | return error.type(); |
| 176 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 177 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 178 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 179 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 180 | |
| 181 | MockServiceRefPtr MakeAutoConnectableService() { |
| 182 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 183 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 184 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 185 | manager()); |
| 186 | service->MakeFavorite(); |
| 187 | service->set_connectable(true); |
| 188 | return service; |
| 189 | } |
| 190 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 191 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 192 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 193 | scoped_ptr<MockDeviceInfo> device_info_; |
| 194 | |
| 195 | // This pointer is owned by the manager, and only tracked here for EXPECT*() |
| 196 | ManagerMockAdaptor *manager_adaptor_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 199 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
| 200 | return (svc0.get() == manager()->services_[0].get() && |
| 201 | svc1.get() == manager()->services_[1].get()); |
| 202 | } |
| 203 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 204 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 205 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 206 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 209 | TEST_F(ManagerTest, DeviceRegistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 210 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 211 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 212 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 213 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 214 | ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular)) |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 215 | .WillByDefault(Return(true)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 216 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 217 | manager()->RegisterDevice(mock_devices_[0]); |
| 218 | manager()->RegisterDevice(mock_devices_[1]); |
| 219 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 220 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 221 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 222 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 223 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 226 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 227 | manager()->running_ = true; |
| 228 | mock_devices_[0]->powered_ = true; |
| 229 | mock_devices_[1]->powered_ = false; |
| 230 | EXPECT_CALL(*mock_devices_[0].get(), Start()) |
| 231 | .Times(1); |
| 232 | EXPECT_CALL(*mock_devices_[1].get(), Start()) |
| 233 | .Times(0); |
| 234 | manager()->RegisterDevice(mock_devices_[0]); |
| 235 | manager()->RegisterDevice(mock_devices_[1]); |
| 236 | } |
| 237 | |
| 238 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 239 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 240 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 241 | AdoptProfile(manager(), profile); // Passes ownership. |
| 242 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
| 243 | EXPECT_CALL(*profile, Save()); |
| 244 | manager()->RegisterDevice(mock_devices_[0]); |
| 245 | } |
| 246 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 247 | TEST_F(ManagerTest, DeviceDeregistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 248 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 249 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 250 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 251 | .WillByDefault(Return(true)); |
| 252 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 253 | manager()->RegisterDevice(mock_devices_[0]); |
| 254 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 255 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 256 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 257 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 258 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 259 | EXPECT_CALL(*mock_devices_[0].get(), Stop()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 260 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 261 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 262 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 263 | EXPECT_CALL(*mock_devices_[1].get(), Stop()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 264 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 265 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 269 | // It's much easier and safer to use a real GLib for this test. |
| 270 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 271 | Manager manager(control_interface(), |
| 272 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 273 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 274 | &glib, |
| 275 | run_path(), |
| 276 | storage_path(), |
| 277 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 278 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 279 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 280 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 281 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 282 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 283 | new NiceMock<MockService>(control_interface(), |
| 284 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 285 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 286 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 287 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 288 | new NiceMock<MockService>(control_interface(), |
| 289 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 290 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 291 | &manager)); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 292 | string service1_name(mock_service->UniqueName()); |
| 293 | string service2_name(mock_service2->UniqueName()); |
| 294 | |
| 295 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 296 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 297 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 298 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 299 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 300 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 301 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 302 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 303 | manager.RegisterService(mock_service); |
| 304 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 305 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 306 | Error error; |
| 307 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 308 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 309 | EXPECT_EQ(2, ids.size()); |
| 310 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 311 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 312 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 313 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 314 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 315 | |
| 316 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 319 | TEST_F(ManagerTest, RegisterKnownService) { |
| 320 | // It's much easier and safer to use a real GLib for this test. |
| 321 | GLib glib; |
| 322 | Manager manager(control_interface(), |
| 323 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 324 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 325 | &glib, |
| 326 | run_path(), |
| 327 | storage_path(), |
| 328 | string()); |
| 329 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 330 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 331 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 332 | { |
| 333 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 334 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 335 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 336 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 338 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 339 | } // Force destruction of service1. |
| 340 | |
| 341 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 342 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 343 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 344 | &manager)); |
| 345 | manager.RegisterService(service2); |
| 346 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 347 | manager.Stop(); |
| 348 | } |
| 349 | |
| 350 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 351 | // It's much easier and safer to use a real GLib for this test. |
| 352 | GLib glib; |
| 353 | Manager manager(control_interface(), |
| 354 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 355 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 356 | &glib, |
| 357 | run_path(), |
| 358 | storage_path(), |
| 359 | string()); |
| 360 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 361 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 362 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 363 | { |
| 364 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 365 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 366 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 367 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 368 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 369 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 370 | } // Force destruction of service1. |
| 371 | scoped_refptr<MockService> mock_service2( |
| 372 | new NiceMock<MockService>(control_interface(), |
| 373 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 374 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 375 | &manager)); |
| 376 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 377 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 378 | manager.RegisterService(mock_service2); |
| 379 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 380 | manager.Stop(); |
| 381 | } |
| 382 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 383 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 384 | // WiFi assumes that it can deregister a service that is not |
| 385 | // registered. (E.g. a hidden service can be deregistered when it |
| 386 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 387 | // |
| 388 | // So test that doing so doesn't cause a crash. |
| 389 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 390 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 391 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 392 | manager()); |
| 393 | manager()->DeregisterService(service); |
| 394 | } |
| 395 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 396 | TEST_F(ManagerTest, GetProperties) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 397 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 398 | AdoptProfile(manager(), profile); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 399 | map<string, ::DBus::Variant> props; |
| 400 | Error error(Error::kInvalidProperty, ""); |
| 401 | { |
| 402 | ::DBus::Error dbus_error; |
| 403 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 404 | manager()->mutable_store()->SetStringProperty( |
| 405 | flimflam::kCheckPortalListProperty, |
| 406 | expected, |
| 407 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 408 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 409 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 410 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 411 | expected); |
| 412 | } |
| 413 | { |
| 414 | ::DBus::Error dbus_error; |
| 415 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 416 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 417 | expected, |
| 418 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 419 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 420 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 421 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 422 | expected); |
| 423 | } |
| 424 | } |
| 425 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 426 | TEST_F(ManagerTest, GetDevicesProperty) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 427 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 428 | AdoptProfile(manager(), profile); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 429 | manager()->RegisterDevice(mock_devices_[0]); |
| 430 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 431 | { |
| 432 | map<string, ::DBus::Variant> props; |
| 433 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 434 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 435 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 436 | Strings devices = |
| 437 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 438 | EXPECT_EQ(2, devices.size()); |
| 439 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 442 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 443 | Manager manager(control_interface(), |
| 444 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 445 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 446 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 447 | run_path(), |
| 448 | storage_path(), |
| 449 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 450 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 451 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 452 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 453 | &manager)); |
| 454 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 455 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 456 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 457 | ProfileRefPtr profile( |
| 458 | new Profile(control_interface(), &manager, id, "", false)); |
| 459 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 460 | // Say we don't have |s2| the first time asked, then that we do. |
| 461 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
| 462 | .WillOnce(Return(false)) |
| 463 | .WillRepeatedly(Return(true)); |
| 464 | EXPECT_CALL(*storage, Flush()) |
| 465 | .Times(AnyNumber()) |
| 466 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 467 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 468 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 469 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 470 | // Create a profile that already has |s2| in it. |
| 471 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
| 472 | profile->AdoptService(s2); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 473 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 474 | // Now, move the Service |s2| to another profile. |
| 475 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 476 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 477 | |
| 478 | // Force destruction of the original Profile, to ensure that the Service |
| 479 | // is kept alive and populated with data. |
| 480 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 481 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 482 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 485 | TEST_F(ManagerTest, SetProfileForService) { |
| 486 | scoped_refptr<MockProfile> profile0( |
| 487 | new MockProfile(control_interface(), manager(), "")); |
| 488 | string profile_name0("profile0"); |
| 489 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 490 | .WillRepeatedly(Return(profile_name0)); |
| 491 | AdoptProfile(manager(), profile0); |
| 492 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 493 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 494 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 495 | manager())); |
| 496 | service->set_profile(profile0); |
| 497 | |
| 498 | { |
| 499 | Error error; |
| 500 | manager()->SetProfileForService(service, "foo", &error); |
| 501 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 502 | EXPECT_EQ("Unknown Profile requested for Service", error.message()); |
| 503 | } |
| 504 | |
| 505 | { |
| 506 | Error error; |
| 507 | manager()->SetProfileForService(service, profile_name0, &error); |
| 508 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 509 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 510 | } |
| 511 | |
| 512 | scoped_refptr<MockProfile> profile1( |
| 513 | new MockProfile(control_interface(), manager(), "")); |
| 514 | string profile_name1("profile1"); |
| 515 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 516 | .WillRepeatedly(Return(profile_name1)); |
| 517 | AdoptProfile(manager(), profile1); |
| 518 | |
| 519 | { |
| 520 | Error error; |
| 521 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 522 | .WillOnce(Return(true)); |
| 523 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 524 | .WillOnce(Return(true)); |
| 525 | manager()->SetProfileForService(service, profile_name1, &error); |
| 526 | EXPECT_TRUE(error.IsSuccess()); |
| 527 | } |
| 528 | } |
| 529 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 530 | TEST_F(ManagerTest, CreateProfile) { |
| 531 | // It's much easier to use real Glib here since we want the storage |
| 532 | // side-effects. |
| 533 | GLib glib; |
| 534 | ScopedTempDir temp_dir; |
| 535 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 536 | |
| 537 | Manager manager(control_interface(), |
| 538 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 539 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 540 | &glib, |
| 541 | run_path(), |
| 542 | storage_path(), |
| 543 | temp_dir.path().value()); |
| 544 | |
| 545 | // Invalid name should be rejected. |
| 546 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 547 | |
| 548 | // Valid name is still rejected because we can't create a profile |
| 549 | // that doesn't have a user component. Such profile names are |
| 550 | // reserved for the single DefaultProfile the manager creates |
| 551 | // at startup. |
| 552 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid")); |
| 553 | |
| 554 | // We should succeed in creating a valid user profile. |
| 555 | const char kProfile[] = "~user/profile"; |
| 556 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile)); |
| 557 | |
| 558 | // We should fail in creating it a second time (already exists). |
| 559 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 560 | } |
| 561 | |
| 562 | TEST_F(ManagerTest, PushPopProfile) { |
| 563 | // It's much easier to use real Glib in creating a Manager for this |
| 564 | // test here since we want the storage side-effects. |
| 565 | GLib glib; |
| 566 | ScopedTempDir temp_dir; |
| 567 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 568 | Manager manager(control_interface(), |
| 569 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 570 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 571 | &glib, |
| 572 | run_path(), |
| 573 | storage_path(), |
| 574 | temp_dir.path().value()); |
| 575 | |
| 576 | // Pushing an invalid profile should fail. |
| 577 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 578 | |
| 579 | // Pushing a default profile name should fail. |
| 580 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default")); |
| 581 | |
| 582 | const char kProfile0[] = "~user/profile0"; |
| 583 | const char kProfile1[] = "~user/profile1"; |
| 584 | |
| 585 | // Create a couple of profiles. |
| 586 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 587 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 588 | |
| 589 | // Push these profiles on the stack. |
| 590 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 591 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 592 | |
| 593 | // Pushing a profile a second time should fail. |
| 594 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 595 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 596 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 597 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 598 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 599 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 600 | |
| 601 | // Make sure a profile name that doesn't exist fails. |
| 602 | const char kProfile2Id[] = "profile2"; |
| 603 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 604 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 605 | |
| 606 | // Create a new service, with a specific storage name. |
| 607 | scoped_refptr<MockService> service( |
| 608 | new NiceMock<MockService>(control_interface(), |
| 609 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 610 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 611 | &manager)); |
| 612 | const char kServiceName[] = "service_storage_name"; |
| 613 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 614 | .WillRepeatedly(Return(kServiceName)); |
| 615 | EXPECT_CALL(*service.get(), Load(_)) |
| 616 | .WillRepeatedly(Return(true)); |
| 617 | |
| 618 | // Add this service to the manager -- it should end up in the ephemeral |
| 619 | // profile. |
| 620 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 621 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 622 | |
| 623 | // Create storage for a profile that contains the service storage name. |
| 624 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 625 | kServiceName)); |
| 626 | |
| 627 | // When we push the profile, the service should move away from the |
| 628 | // ephemeral profile to this new profile since it has an entry for |
| 629 | // this service. |
| 630 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 631 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 632 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 633 | |
| 634 | // Insert another profile that should supersede ownership of the service. |
| 635 | const char kProfile3Id[] = "profile3"; |
| 636 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 637 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 638 | kServiceName)); |
| 639 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 640 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 641 | |
| 642 | // Popping an invalid profile name should fail. |
| 643 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 644 | |
| 645 | // Popping an profile that is not at the top of the stack should fail. |
| 646 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 647 | |
| 648 | // Popping the top profile should succeed. |
| 649 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 650 | |
| 651 | // Moreover the service should have switched profiles to profile 2. |
| 652 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 653 | |
| 654 | // Popping the top profile should succeed. |
| 655 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 656 | |
| 657 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 658 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 659 | |
| 660 | // Pop the remaining two services off the stack. |
| 661 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 662 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 663 | |
| 664 | // Next pop should fail with "stack is empty". |
| 665 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
| 666 | } |
| 667 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 668 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 669 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 670 | // system teardown. |
| 671 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 672 | return arg.get() == ref_address; |
| 673 | } |
| 674 | |
| 675 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 676 | MockServiceRefPtr s_not_in_profile( |
| 677 | new NiceMock<MockService>(control_interface(), |
| 678 | dispatcher(), |
| 679 | metrics(), |
| 680 | manager())); |
| 681 | MockServiceRefPtr s_not_in_group( |
| 682 | new NiceMock<MockService>(control_interface(), |
| 683 | dispatcher(), |
| 684 | metrics(), |
| 685 | manager())); |
| 686 | MockServiceRefPtr s_configure_fail( |
| 687 | new NiceMock<MockService>(control_interface(), |
| 688 | dispatcher(), |
| 689 | metrics(), |
| 690 | manager())); |
| 691 | MockServiceRefPtr s_configure_succeed( |
| 692 | new NiceMock<MockService>(control_interface(), |
| 693 | dispatcher(), |
| 694 | metrics(), |
| 695 | manager())); |
| 696 | |
| 697 | string entry_name("entry_name"); |
| 698 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 699 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 700 | .WillRepeatedly(Return("not_entry_name")); |
| 701 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 702 | .WillRepeatedly(Return(entry_name)); |
| 703 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 704 | .WillRepeatedly(Return(entry_name)); |
| 705 | |
| 706 | manager()->RegisterService(s_not_in_profile); |
| 707 | manager()->RegisterService(s_not_in_group); |
| 708 | manager()->RegisterService(s_configure_fail); |
| 709 | manager()->RegisterService(s_configure_succeed); |
| 710 | |
| 711 | scoped_refptr<MockProfile> profile0( |
| 712 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 713 | scoped_refptr<MockProfile> profile1( |
| 714 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 715 | |
| 716 | s_not_in_group->set_profile(profile1); |
| 717 | s_configure_fail->set_profile(profile1); |
| 718 | s_configure_succeed->set_profile(profile1); |
| 719 | |
| 720 | AdoptProfile(manager(), profile0); |
| 721 | AdoptProfile(manager(), profile1); |
| 722 | |
| 723 | // No services are a member of this profile. |
| 724 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 725 | |
| 726 | // No services that are members of this profile have this entry name. |
| 727 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 728 | |
| 729 | // Only services that are members of the profile and group will be abandoned. |
| 730 | EXPECT_CALL(*profile1.get(), |
| 731 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 732 | EXPECT_CALL(*profile1.get(), |
| 733 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 734 | EXPECT_CALL(*profile1.get(), |
| 735 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 736 | .WillOnce(Return(true)); |
| 737 | EXPECT_CALL(*profile1.get(), |
| 738 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 739 | .WillOnce(Return(true)); |
| 740 | |
| 741 | // Never allow services to re-join profile1. |
| 742 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 743 | .WillRepeatedly(Return(false)); |
| 744 | |
| 745 | // Only allow one of the members of the profile and group to successfully |
| 746 | // join profile0. |
| 747 | EXPECT_CALL(*profile0.get(), |
| 748 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 749 | EXPECT_CALL(*profile0.get(), |
| 750 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 751 | EXPECT_CALL(*profile0.get(), |
| 752 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 753 | .WillOnce(Return(false)); |
| 754 | EXPECT_CALL(*profile0.get(), |
| 755 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 756 | .WillOnce(Return(true)); |
| 757 | |
| 758 | // Expect the failed-to-configure service to have Unload() called on it. |
| 759 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 760 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 761 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 762 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 763 | |
| 764 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 765 | |
| 766 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 767 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 768 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 769 | |
| 770 | // Since we are using a MockProfile, the profile does not actually change, |
| 771 | // since ConfigureService was not actually called on the service. |
| 772 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 773 | } |
| 774 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 775 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 776 | { |
| 777 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 778 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 779 | flimflam::kOfflineModeProperty, |
| 780 | PropertyStoreTest::kBoolV, |
| 781 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 782 | } |
| 783 | { |
| 784 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 785 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 786 | flimflam::kCountryProperty, |
| 787 | PropertyStoreTest::kStringV, |
| 788 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 789 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 790 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 791 | { |
| 792 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 793 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 794 | flimflam::kCountryProperty, |
| 795 | PropertyStoreTest::kBoolV, |
| 796 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 797 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 798 | } |
| 799 | { |
| 800 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 801 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 802 | flimflam::kOfflineModeProperty, |
| 803 | PropertyStoreTest::kStringV, |
| 804 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 805 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 806 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 807 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 808 | { |
| 809 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 810 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 811 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 812 | flimflam::kEnabledTechnologiesProperty, |
| 813 | PropertyStoreTest::kStringsV, |
| 814 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 815 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 816 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 817 | } |
| 818 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 819 | TEST_F(ManagerTest, RequestScan) { |
| 820 | { |
| 821 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 822 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 823 | manager()->RegisterDevice(mock_devices_[1].get()); |
| 824 | EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 825 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 826 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
| 827 | EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 828 | .WillRepeatedly(Return(false)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 829 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 830 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | { |
| 834 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 835 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 836 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 837 | } |
| 838 | } |
| 839 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 840 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 841 | KeyValueStore args; |
| 842 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 843 | manager()->GetService(args, &e); |
| 844 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 845 | EXPECT_EQ("must specify service type", e.message()); |
| 846 | } |
| 847 | |
| 848 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 849 | KeyValueStore args; |
| 850 | Error e; |
| 851 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 852 | manager()->GetService(args, &e); |
| 853 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 854 | EXPECT_EQ("service type is unsupported", e.message()); |
| 855 | } |
| 856 | |
| 857 | TEST_F(ManagerTest, GetServiceNoWifiDevice) { |
| 858 | KeyValueStore args; |
| 859 | Error e; |
| 860 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 861 | manager()->GetService(args, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 862 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 863 | EXPECT_EQ("no wifi devices available", e.message()); |
| 864 | } |
| 865 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 866 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 867 | KeyValueStore args; |
| 868 | Error e; |
| 869 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 870 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 871 | manager()->RegisterDevice(mock_wifi_); |
| 872 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 873 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 874 | manager()->GetService(args, &e); |
| 875 | EXPECT_TRUE(e.IsSuccess()); |
| 876 | } |
| 877 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 878 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 879 | KeyValueStore args; |
| 880 | Error e; |
| 881 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 882 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 883 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 884 | EXPECT_FALSE(service); |
| 885 | } |
| 886 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 887 | TEST_F(ManagerTest, GetServiceVPN) { |
| 888 | KeyValueStore args; |
| 889 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 890 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 891 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
| 892 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 893 | EXPECT_TRUE(e.IsSuccess()); |
| 894 | EXPECT_TRUE(service); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 897 | TEST_F(ManagerTest, TechnologyOrder) { |
| 898 | Error error; |
| 899 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 900 | string(flimflam::kTypeWifi), &error); |
| 901 | ASSERT_TRUE(error.IsSuccess()); |
| 902 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 903 | string(flimflam::kTypeEthernet) + "," + |
| 904 | string(flimflam::kTypeWifi)); |
| 905 | |
| 906 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 907 | string(flimflam::kTypeWifi), &error); |
| 908 | ASSERT_FALSE(error.IsSuccess()); |
| 909 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 910 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 911 | string(flimflam::kTypeWifi), |
| 912 | manager()->GetTechnologyOrder()); |
| 913 | } |
| 914 | |
| 915 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 916 | // TODO(quiche): Some of these tests would probably fit better in |
| 917 | // service_unittest, since the actual comparison of Services is |
| 918 | // implemented in Service. (crosbug.com/23370) |
| 919 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 920 | scoped_refptr<MockService> mock_service0( |
| 921 | new NiceMock<MockService>(control_interface(), |
| 922 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 923 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 924 | manager())); |
| 925 | scoped_refptr<MockService> mock_service1( |
| 926 | new NiceMock<MockService>(control_interface(), |
| 927 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 928 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 929 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 930 | |
| 931 | manager()->RegisterService(mock_service0); |
| 932 | manager()->RegisterService(mock_service1); |
| 933 | |
| 934 | // Services should already be sorted by UniqueName |
| 935 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 936 | |
| 937 | // Asking explictly to sort services should not change anything |
| 938 | manager()->SortServices(); |
| 939 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 940 | |
| 941 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 942 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 943 | manager()->UpdateService(mock_service1); |
| 944 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 945 | |
| 946 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 947 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 948 | manager()->UpdateService(mock_service0); |
| 949 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 950 | |
| 951 | // Technology |
| 952 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi)) |
| 953 | .WillRepeatedly(Return(true)); |
| 954 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet)) |
| 955 | .WillRepeatedly(Return(true)); |
| 956 | // NB: Redefine default (false) return values so we don't use the default rule |
| 957 | // which makes the logs noisier |
| 958 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi))) |
| 959 | .WillRepeatedly(Return(false)); |
| 960 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet))) |
| 961 | .WillRepeatedly(Return(false)); |
| 962 | |
| 963 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 964 | // Default technology ordering should favor Ethernet over WiFi. |
| 965 | manager()->SortServices(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 966 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 967 | |
| 968 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 969 | string(flimflam::kTypeEthernet), &error); |
| 970 | EXPECT_TRUE(error.IsSuccess()); |
| 971 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 972 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 973 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 974 | mock_service0->set_priority(1); |
| 975 | manager()->UpdateService(mock_service0); |
| 976 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 977 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 978 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 979 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 980 | manager()->UpdateService(mock_service1); |
| 981 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 982 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 983 | // Auto-connect. |
| 984 | mock_service0->set_auto_connect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 985 | manager()->UpdateService(mock_service0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 986 | mock_service1->set_auto_connect(false); |
| 987 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 988 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 989 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 990 | // Connectable. |
| 991 | mock_service1->set_connectable(true); |
| 992 | manager()->UpdateService(mock_service1); |
| 993 | mock_service0->set_connectable(false); |
| 994 | manager()->UpdateService(mock_service0); |
| 995 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 996 | |
| 997 | // IsFailed. |
| 998 | EXPECT_CALL(*mock_service0.get(), state()) |
| 999 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 1000 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 1001 | .WillRepeatedly(Return(false)); |
| 1002 | manager()->UpdateService(mock_service0); |
| 1003 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1004 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1005 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 1006 | .WillRepeatedly(Return(true)); |
| 1007 | manager()->UpdateService(mock_service1); |
| 1008 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1009 | |
| 1010 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1011 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1012 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 1013 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1014 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1015 | manager()->UpdateService(mock_service1); |
| 1016 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1017 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1018 | // Connected. |
| 1019 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1020 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1021 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 1022 | .WillRepeatedly(Return(true)); |
| 1023 | manager()->UpdateService(mock_service0); |
| 1024 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1025 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1026 | manager()->DeregisterService(mock_service0); |
| 1027 | manager()->DeregisterService(mock_service1); |
| 1028 | } |
| 1029 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1030 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1031 | MockMetrics mock_metrics; |
| 1032 | manager()->set_metrics(&mock_metrics); |
| 1033 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1034 | scoped_refptr<MockService> mock_service0( |
| 1035 | new NiceMock<MockService>(control_interface(), |
| 1036 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1037 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1038 | manager())); |
| 1039 | scoped_refptr<MockService> mock_service1( |
| 1040 | new NiceMock<MockService>(control_interface(), |
| 1041 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1042 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1043 | manager())); |
| 1044 | |
| 1045 | scoped_refptr<MockConnection> mock_connection0( |
| 1046 | new NiceMock<MockConnection>(device_info_.get())); |
| 1047 | scoped_refptr<MockConnection> mock_connection1( |
| 1048 | new NiceMock<MockConnection>(device_info_.get())); |
| 1049 | |
| 1050 | manager()->RegisterService(mock_service0); |
| 1051 | manager()->RegisterService(mock_service1); |
| 1052 | |
| 1053 | mock_service0->connection_ = mock_connection0; |
| 1054 | mock_service1->connection_ = mock_connection1; |
| 1055 | |
| 1056 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1057 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1058 | manager()->SortServices(); |
| 1059 | |
| 1060 | mock_service1->set_priority(1); |
| 1061 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 1062 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1063 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1064 | manager()->SortServices(); |
| 1065 | |
| 1066 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1067 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1068 | mock_service1->connection_ = NULL; |
| 1069 | manager()->DeregisterService(mock_service1); |
| 1070 | |
| 1071 | mock_service0->connection_ = NULL; |
| 1072 | manager()->DeregisterService(mock_service0); |
| 1073 | } |
| 1074 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1075 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 1076 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 1077 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1078 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1079 | manager(), |
| 1080 | "null4", |
| 1081 | "addr4", |
| 1082 | 0)); |
| 1083 | manager()->RegisterDevice(mock_devices_[0]); |
| 1084 | manager()->RegisterDevice(mock_devices_[1]); |
| 1085 | manager()->RegisterDevice(mock_devices_[2]); |
| 1086 | manager()->RegisterDevice(mock_devices_[3]); |
| 1087 | |
| 1088 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1089 | .WillByDefault(Return(Technology::kEthernet)); |
| 1090 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1091 | .WillByDefault(Return(Technology::kWifi)); |
| 1092 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1093 | .WillByDefault(Return(Technology::kCellular)); |
| 1094 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1095 | .WillByDefault(Return(Technology::kWifi)); |
| 1096 | |
| 1097 | set<string> expected_technologies; |
| 1098 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1099 | Technology::kEthernet)); |
| 1100 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1101 | Technology::kWifi)); |
| 1102 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1103 | Technology::kCellular)); |
| 1104 | Error error; |
| 1105 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 1106 | |
| 1107 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1108 | ContainerEq(expected_technologies)); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 1112 | scoped_refptr<MockService> connected_service1( |
| 1113 | new NiceMock<MockService>(control_interface(), |
| 1114 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1115 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1116 | manager())); |
| 1117 | scoped_refptr<MockService> connected_service2( |
| 1118 | new NiceMock<MockService>(control_interface(), |
| 1119 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1120 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1121 | manager())); |
| 1122 | scoped_refptr<MockService> disconnected_service1( |
| 1123 | new NiceMock<MockService>(control_interface(), |
| 1124 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1125 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1126 | manager())); |
| 1127 | scoped_refptr<MockService> disconnected_service2( |
| 1128 | new NiceMock<MockService>(control_interface(), |
| 1129 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1130 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1131 | manager())); |
| 1132 | |
| 1133 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 1134 | .WillByDefault(Return(true)); |
| 1135 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 1136 | .WillByDefault(Return(true)); |
| 1137 | |
| 1138 | manager()->RegisterService(connected_service1); |
| 1139 | manager()->RegisterService(connected_service2); |
| 1140 | manager()->RegisterService(disconnected_service1); |
| 1141 | manager()->RegisterService(disconnected_service2); |
| 1142 | |
| 1143 | manager()->RegisterDevice(mock_devices_[0]); |
| 1144 | manager()->RegisterDevice(mock_devices_[1]); |
| 1145 | manager()->RegisterDevice(mock_devices_[2]); |
| 1146 | manager()->RegisterDevice(mock_devices_[3]); |
| 1147 | |
| 1148 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1149 | .WillByDefault(Return(Technology::kEthernet)); |
| 1150 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1151 | .WillByDefault(Return(Technology::kWifi)); |
| 1152 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1153 | .WillByDefault(Return(Technology::kCellular)); |
| 1154 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1155 | .WillByDefault(Return(Technology::kWifi)); |
| 1156 | |
| 1157 | mock_devices_[0]->SelectService(connected_service1); |
| 1158 | mock_devices_[1]->SelectService(disconnected_service1); |
| 1159 | mock_devices_[2]->SelectService(disconnected_service2); |
| 1160 | mock_devices_[3]->SelectService(connected_service2); |
| 1161 | |
| 1162 | set<string> expected_technologies; |
| 1163 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1164 | Technology::kEthernet)); |
| 1165 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1166 | Technology::kWifi)); |
| 1167 | Error error; |
| 1168 | |
| 1169 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 1170 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1171 | ContainerEq(expected_technologies)); |
| 1172 | } |
| 1173 | |
| 1174 | TEST_F(ManagerTest, DefaultTechnology) { |
| 1175 | scoped_refptr<MockService> connected_service( |
| 1176 | new NiceMock<MockService>(control_interface(), |
| 1177 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1178 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1179 | manager())); |
| 1180 | scoped_refptr<MockService> disconnected_service( |
| 1181 | new NiceMock<MockService>(control_interface(), |
| 1182 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1183 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1184 | manager())); |
| 1185 | |
| 1186 | // Connected. WiFi. |
| 1187 | ON_CALL(*connected_service.get(), IsConnected()) |
| 1188 | .WillByDefault(Return(true)); |
| 1189 | ON_CALL(*connected_service.get(), state()) |
| 1190 | .WillByDefault(Return(Service::kStateConnected)); |
| 1191 | ON_CALL(*connected_service.get(), technology()) |
| 1192 | .WillByDefault(Return(Technology::kWifi)); |
| 1193 | |
| 1194 | // Disconnected. Ethernet. |
| 1195 | ON_CALL(*disconnected_service.get(), technology()) |
| 1196 | .WillByDefault(Return(Technology::kEthernet)); |
| 1197 | |
| 1198 | manager()->RegisterService(disconnected_service); |
| 1199 | Error error; |
| 1200 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 1201 | |
| 1202 | |
| 1203 | manager()->RegisterService(connected_service); |
| 1204 | // Connected service should be brought to the front now. |
| 1205 | string expected_technology = |
| 1206 | Technology::NameFromIdentifier(Technology::kWifi); |
| 1207 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 1208 | } |
| 1209 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1210 | TEST_F(ManagerTest, DisconnectServicesOnStop) { |
| 1211 | scoped_refptr<MockService> mock_service( |
| 1212 | new NiceMock<MockService>(control_interface(), |
| 1213 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1214 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1215 | manager())); |
| 1216 | manager()->RegisterService(mock_service); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1217 | EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1218 | manager()->Stop(); |
| 1219 | } |
| 1220 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1221 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 1222 | scoped_refptr<MockService> mock_service( |
| 1223 | new NiceMock<MockService>(control_interface(), |
| 1224 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1225 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1226 | manager())); |
| 1227 | manager()->RegisterService(mock_service); |
| 1228 | EXPECT_FALSE(mock_service->favorite()); |
| 1229 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1230 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1231 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1232 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1233 | manager()->UpdateService(mock_service); |
| 1234 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1235 | // to mock out MakeFavorite. And mocking that out would break the |
| 1236 | // SortServices test. (crosbug.com/23370) |
| 1237 | EXPECT_TRUE(mock_service->favorite()); |
| 1238 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1239 | } |
| 1240 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 1241 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 1242 | // This tests the case where the user connects to a service that is |
| 1243 | // currently associated with a profile. We want to make sure that the |
| 1244 | // favorite flag is set and that the flag is saved to the current |
| 1245 | // profile. |
| 1246 | scoped_refptr<MockService> mock_service( |
| 1247 | new NiceMock<MockService>(control_interface(), |
| 1248 | dispatcher(), |
| 1249 | metrics(), |
| 1250 | manager())); |
| 1251 | manager()->RegisterService(mock_service); |
| 1252 | EXPECT_FALSE(mock_service->favorite()); |
| 1253 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1254 | |
| 1255 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
| 1256 | mock_service->set_profile(profile); |
| 1257 | |
| 1258 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1259 | .WillRepeatedly(Return(true)); |
| 1260 | EXPECT_CALL(*mock_service.get(), SaveToCurrentProfile()); |
| 1261 | manager()->UpdateService(mock_service); |
| 1262 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1263 | // to mock out MakeFavorite. And mocking that out would break the |
| 1264 | // SortServices test. (crosbug.com/23370) |
| 1265 | EXPECT_TRUE(mock_service->favorite()); |
| 1266 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1267 | } |
| 1268 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1269 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 1270 | scoped_refptr<MockProfile> profile( |
| 1271 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1272 | AdoptProfile(manager(), profile); |
| 1273 | scoped_refptr<MockService> service( |
| 1274 | new NiceMock<MockService>(control_interface(), |
| 1275 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1276 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1277 | manager())); |
| 1278 | |
| 1279 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 1280 | ServiceRefPtr expect_service(service.get()); |
| 1281 | |
| 1282 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 1283 | .WillOnce(Return(false)); |
| 1284 | manager()->RegisterService(service); |
| 1285 | |
| 1286 | EXPECT_CALL(*service.get(), state()) |
| 1287 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1288 | EXPECT_CALL(*service.get(), IsConnected()) |
| 1289 | .WillRepeatedly(Return(true)); |
| 1290 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 1291 | .WillOnce(Return(true)); |
| 1292 | manager()->UpdateService(service); |
| 1293 | } |
| 1294 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1295 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 1296 | vector<string> profile_paths; |
| 1297 | for (size_t i = 0; i < 10; i++) { |
| 1298 | scoped_refptr<MockProfile> profile( |
| 1299 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 1300 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1301 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 1302 | .WillOnce(Return(profile_paths.back())); |
| 1303 | AdoptProfile(manager(), profile); |
| 1304 | } |
| 1305 | |
| 1306 | Error error; |
| 1307 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 1308 | EXPECT_TRUE(error.IsSuccess()); |
| 1309 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 1310 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 1311 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 1312 | } |
| 1313 | } |
| 1314 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1315 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 1316 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 1317 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 1318 | manager()->RegisterService(service); |
| 1319 | dispatcher()->DispatchPendingEvents(); |
| 1320 | } |
| 1321 | |
| 1322 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 1323 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1324 | service1->set_priority(1); |
| 1325 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1326 | service2->set_priority(2); |
| 1327 | manager()->RegisterService(service1); |
| 1328 | manager()->RegisterService(service2); |
| 1329 | dispatcher()->DispatchPendingEvents(); |
| 1330 | |
| 1331 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1332 | EXPECT_CALL(*service2.get(), state()) |
| 1333 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1334 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 1335 | .WillRepeatedly(Return(true)); |
| 1336 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 1337 | .WillRepeatedly(Return(false)); |
| 1338 | manager()->UpdateService(service2); |
| 1339 | dispatcher()->DispatchPendingEvents(); |
| 1340 | } |
| 1341 | |
| 1342 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 1343 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1344 | service1->set_priority(1); |
| 1345 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1346 | service2->set_priority(2); |
| 1347 | manager()->RegisterService(service1); |
| 1348 | manager()->RegisterService(service2); |
| 1349 | dispatcher()->DispatchPendingEvents(); |
| 1350 | |
| 1351 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1352 | manager()->DeregisterService(service2); |
| 1353 | dispatcher()->DispatchPendingEvents(); |
| 1354 | } |
| 1355 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame^] | 1356 | TEST_F(ManagerTest, RecheckPortal) { |
| 1357 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 1358 | .WillOnce(Return(false)); |
| 1359 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 1360 | .WillOnce(Return(true)); |
| 1361 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 1362 | .Times(0); |
| 1363 | |
| 1364 | manager()->RegisterDevice(mock_devices_[0]); |
| 1365 | manager()->RegisterDevice(mock_devices_[1]); |
| 1366 | manager()->RegisterDevice(mock_devices_[2]); |
| 1367 | |
| 1368 | manager()->RecheckPortal(NULL); |
| 1369 | } |
| 1370 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1371 | } // namespace shill |