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> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/scoped_temp_dir.h> |
| 13 | #include <base/stl_util.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" |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 36 | #include "shill/mock_wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 37 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 38 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 39 | #include "shill/wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 40 | |
| 41 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 42 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | using std::vector; |
| 45 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 46 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 47 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 48 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 49 | using ::testing::ContainerEq; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 50 | using ::testing::InSequence; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 51 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 52 | using ::testing::NiceMock; |
| 53 | using ::testing::Return; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 54 | using ::testing::ReturnRef; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 55 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 56 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 57 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 58 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 59 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 60 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 61 | ManagerTest() |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 62 | : mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 63 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 64 | metrics(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 65 | manager(), |
| 66 | "wifi0", |
| 67 | "addr4", |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 68 | 4)), |
| 69 | device_info_(new NiceMock<MockDeviceInfo>( |
| 70 | control_interface(), |
| 71 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 72 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 73 | reinterpret_cast<Manager*>(NULL))), |
| 74 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 75 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 76 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 77 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 78 | manager(), |
| 79 | "null0", |
| 80 | "addr0", |
| 81 | 0)); |
| 82 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 83 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 84 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 85 | manager(), |
| 86 | "null1", |
| 87 | "addr1", |
| 88 | 1)); |
| 89 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 90 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 91 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 92 | manager(), |
| 93 | "null2", |
| 94 | "addr2", |
| 95 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 96 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 97 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 98 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 99 | manager(), |
| 100 | "null3", |
| 101 | "addr3", |
| 102 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 103 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 104 | |
| 105 | // Replace the manager's adaptor with a quieter one, and one |
| 106 | // we can do EXPECT*() against. Passes ownership. |
| 107 | manager()->adaptor_.reset(manager_adaptor_); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 108 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 109 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 110 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 111 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 112 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 113 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 114 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 115 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 116 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 117 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 118 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 119 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 120 | manager->profiles_.push_back(profile); |
| 121 | } |
| 122 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 123 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 124 | return manager->ephemeral_profile_; |
| 125 | } |
| 126 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 127 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 128 | Profile::Identifier id("rather", "irrelevant"); |
| 129 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 130 | manager, |
| 131 | id, |
| 132 | "", |
| 133 | false)); |
| 134 | FilePath final_path(storage_path()); |
| 135 | final_path = final_path.Append("test.profile"); |
| 136 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 137 | storage->set_path(final_path); |
| 138 | if (!storage->Open()) |
| 139 | return NULL; |
| 140 | profile->set_storage(storage.release()); // Passes ownership. |
| 141 | return profile.release(); |
| 142 | } |
| 143 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 144 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 145 | const string &profile_identifier, |
| 146 | const string &service_name) { |
| 147 | GLib glib; |
| 148 | KeyFileStore store(&glib); |
| 149 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 150 | return store.Open() && |
| 151 | store.SetString(service_name, "rather", "irrelevant") && |
| 152 | store.Close(); |
| 153 | } |
| 154 | |
| 155 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 156 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 157 | string path; |
| 158 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 159 | return error.type(); |
| 160 | } |
| 161 | |
| 162 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 163 | Error error; |
| 164 | manager->PopAnyProfile(&error); |
| 165 | return error.type(); |
| 166 | } |
| 167 | |
| 168 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 169 | Error error; |
| 170 | manager->PopProfile(name, &error); |
| 171 | return error.type(); |
| 172 | } |
| 173 | |
| 174 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 175 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 176 | string path; |
| 177 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 178 | return error.type(); |
| 179 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 180 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 181 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 182 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 183 | |
| 184 | MockServiceRefPtr MakeAutoConnectableService() { |
| 185 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 186 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 187 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 188 | manager()); |
| 189 | service->MakeFavorite(); |
| 190 | service->set_connectable(true); |
| 191 | return service; |
| 192 | } |
| 193 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 194 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 195 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 196 | scoped_ptr<MockDeviceInfo> device_info_; |
| 197 | |
| 198 | // This pointer is owned by the manager, and only tracked here for EXPECT*() |
| 199 | ManagerMockAdaptor *manager_adaptor_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 202 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
| 203 | return (svc0.get() == manager()->services_[0].get() && |
| 204 | svc1.get() == manager()->services_[1].get()); |
| 205 | } |
| 206 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 207 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 208 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 209 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 212 | TEST_F(ManagerTest, DeviceRegistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 213 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 214 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 215 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 216 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 217 | ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular)) |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 218 | .WillByDefault(Return(true)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 219 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 220 | manager()->RegisterDevice(mock_devices_[0]); |
| 221 | manager()->RegisterDevice(mock_devices_[1]); |
| 222 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 223 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 225 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 226 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 229 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 230 | manager()->running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 231 | mock_devices_[0]->enabled_persistent_ = true; |
| 232 | mock_devices_[1]->enabled_persistent_ = false; |
| 233 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 234 | .Times(1); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 235 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 236 | .Times(0); |
| 237 | manager()->RegisterDevice(mock_devices_[0]); |
| 238 | manager()->RegisterDevice(mock_devices_[1]); |
| 239 | } |
| 240 | |
| 241 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 242 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 243 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 244 | AdoptProfile(manager(), profile); // Passes ownership. |
| 245 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
| 246 | EXPECT_CALL(*profile, Save()); |
| 247 | manager()->RegisterDevice(mock_devices_[0]); |
| 248 | } |
| 249 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 250 | TEST_F(ManagerTest, DeviceDeregistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 251 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 252 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 253 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 254 | .WillByDefault(Return(true)); |
| 255 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 256 | manager()->RegisterDevice(mock_devices_[0]); |
| 257 | manager()->RegisterDevice(mock_devices_[1]); |
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 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 260 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 261 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 262 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 263 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 264 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 265 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 266 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 267 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 268 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 272 | // It's much easier and safer to use a real GLib for this test. |
| 273 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 274 | Manager manager(control_interface(), |
| 275 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 276 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 277 | &glib, |
| 278 | run_path(), |
| 279 | storage_path(), |
| 280 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 281 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 282 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 283 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 284 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 285 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 286 | new NiceMock<MockService>(control_interface(), |
| 287 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 288 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 289 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 290 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 291 | new NiceMock<MockService>(control_interface(), |
| 292 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 293 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 294 | &manager)); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 295 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 296 | string service1_name(mock_service->UniqueName()); |
| 297 | string service2_name(mock_service2->UniqueName()); |
| 298 | |
| 299 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 300 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 301 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 302 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 303 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 304 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 305 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 306 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 307 | manager.RegisterService(mock_service); |
| 308 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 309 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 310 | Error error; |
| 311 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 312 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 313 | EXPECT_EQ(2, ids.size()); |
| 314 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 315 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 316 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 317 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 318 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 319 | |
| 320 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 323 | TEST_F(ManagerTest, RegisterKnownService) { |
| 324 | // It's much easier and safer to use a real GLib for this test. |
| 325 | GLib glib; |
| 326 | Manager manager(control_interface(), |
| 327 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 328 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 329 | &glib, |
| 330 | run_path(), |
| 331 | storage_path(), |
| 332 | string()); |
| 333 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 334 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 335 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 336 | { |
| 337 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 338 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 339 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 340 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 341 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 342 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 343 | } // Force destruction of service1. |
| 344 | |
| 345 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 346 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 347 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 348 | &manager)); |
| 349 | manager.RegisterService(service2); |
| 350 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 351 | manager.Stop(); |
| 352 | } |
| 353 | |
| 354 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 355 | // It's much easier and safer to use a real GLib for this test. |
| 356 | GLib glib; |
| 357 | Manager manager(control_interface(), |
| 358 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 359 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 360 | &glib, |
| 361 | run_path(), |
| 362 | storage_path(), |
| 363 | string()); |
| 364 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 365 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 366 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 367 | { |
| 368 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 369 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 370 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 371 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 372 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 373 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 374 | } // Force destruction of service1. |
| 375 | scoped_refptr<MockService> mock_service2( |
| 376 | new NiceMock<MockService>(control_interface(), |
| 377 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 378 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 379 | &manager)); |
| 380 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 381 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 382 | manager.RegisterService(mock_service2); |
| 383 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 384 | manager.Stop(); |
| 385 | } |
| 386 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 387 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 388 | // WiFi assumes that it can deregister a service that is not |
| 389 | // registered. (E.g. a hidden service can be deregistered when it |
| 390 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 391 | // |
| 392 | // So test that doing so doesn't cause a crash. |
| 393 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 394 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 395 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 396 | manager()); |
| 397 | manager()->DeregisterService(service); |
| 398 | } |
| 399 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 400 | TEST_F(ManagerTest, GetProperties) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 401 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 402 | AdoptProfile(manager(), profile); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 403 | map<string, ::DBus::Variant> props; |
| 404 | Error error(Error::kInvalidProperty, ""); |
| 405 | { |
| 406 | ::DBus::Error dbus_error; |
| 407 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 408 | manager()->mutable_store()->SetStringProperty( |
| 409 | flimflam::kCheckPortalListProperty, |
| 410 | expected, |
| 411 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 412 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 413 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 414 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 415 | expected); |
| 416 | } |
| 417 | { |
| 418 | ::DBus::Error dbus_error; |
| 419 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 420 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 421 | expected, |
| 422 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 423 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 424 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 425 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 426 | expected); |
| 427 | } |
| 428 | } |
| 429 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 430 | TEST_F(ManagerTest, GetDevicesProperty) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 431 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 432 | AdoptProfile(manager(), profile); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 433 | manager()->RegisterDevice(mock_devices_[0]); |
| 434 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 435 | { |
| 436 | map<string, ::DBus::Variant> props; |
| 437 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 438 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 439 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 440 | Strings devices = |
| 441 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 442 | EXPECT_EQ(2, devices.size()); |
| 443 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 444 | } |
| 445 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 446 | TEST_F(ManagerTest, GetServicesProperty) { |
| 447 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
| 448 | AdoptProfile(manager(), profile); |
| 449 | map<string, ::DBus::Variant> props; |
| 450 | ::DBus::Error dbus_error; |
| 451 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
| 452 | map<string, ::DBus::Variant>::const_iterator prop = |
| 453 | props.find(flimflam::kServicesProperty); |
| 454 | ASSERT_FALSE(prop == props.end()); |
| 455 | const ::DBus::Variant &variant = prop->second; |
| 456 | ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature())); |
| 457 | } |
| 458 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 459 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 460 | Manager manager(control_interface(), |
| 461 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 462 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 463 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 464 | run_path(), |
| 465 | storage_path(), |
| 466 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 467 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 468 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 469 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 470 | &manager)); |
| 471 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 472 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 473 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 474 | ProfileRefPtr profile( |
| 475 | new Profile(control_interface(), &manager, id, "", false)); |
| 476 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 477 | // Say we don't have |s2| the first time asked, then that we do. |
| 478 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
| 479 | .WillOnce(Return(false)) |
| 480 | .WillRepeatedly(Return(true)); |
| 481 | EXPECT_CALL(*storage, Flush()) |
| 482 | .Times(AnyNumber()) |
| 483 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 484 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 485 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 486 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 487 | // Create a profile that already has |s2| in it. |
| 488 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
| 489 | profile->AdoptService(s2); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 490 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 491 | // Now, move the Service |s2| to another profile. |
| 492 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 493 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 494 | |
| 495 | // Force destruction of the original Profile, to ensure that the Service |
| 496 | // is kept alive and populated with data. |
| 497 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 498 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 499 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 502 | TEST_F(ManagerTest, LookupProfileByRpcIdentifier) { |
| 503 | scoped_refptr<MockProfile> mock_profile( |
| 504 | new MockProfile(control_interface(), manager(), "")); |
| 505 | const string kProfileName("profile0"); |
| 506 | EXPECT_CALL(*mock_profile, GetRpcIdentifier()) |
| 507 | .WillRepeatedly(Return(kProfileName)); |
| 508 | AdoptProfile(manager(), mock_profile); |
| 509 | |
| 510 | EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo")); |
| 511 | ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName); |
| 512 | EXPECT_EQ(mock_profile.get(), profile.get()); |
| 513 | } |
| 514 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 515 | TEST_F(ManagerTest, SetProfileForService) { |
| 516 | scoped_refptr<MockProfile> profile0( |
| 517 | new MockProfile(control_interface(), manager(), "")); |
| 518 | string profile_name0("profile0"); |
| 519 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 520 | .WillRepeatedly(Return(profile_name0)); |
| 521 | AdoptProfile(manager(), profile0); |
| 522 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 523 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 524 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 525 | manager())); |
| 526 | service->set_profile(profile0); |
| 527 | |
| 528 | { |
| 529 | Error error; |
| 530 | manager()->SetProfileForService(service, "foo", &error); |
| 531 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 532 | EXPECT_EQ("Unknown Profile foo requested for Service", error.message()); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | { |
| 536 | Error error; |
| 537 | manager()->SetProfileForService(service, profile_name0, &error); |
| 538 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 539 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 540 | } |
| 541 | |
| 542 | scoped_refptr<MockProfile> profile1( |
| 543 | new MockProfile(control_interface(), manager(), "")); |
| 544 | string profile_name1("profile1"); |
| 545 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 546 | .WillRepeatedly(Return(profile_name1)); |
| 547 | AdoptProfile(manager(), profile1); |
| 548 | |
| 549 | { |
| 550 | Error error; |
| 551 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 552 | .WillOnce(Return(true)); |
| 553 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 554 | .WillOnce(Return(true)); |
| 555 | manager()->SetProfileForService(service, profile_name1, &error); |
| 556 | EXPECT_TRUE(error.IsSuccess()); |
| 557 | } |
| 558 | } |
| 559 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 560 | TEST_F(ManagerTest, CreateProfile) { |
| 561 | // It's much easier to use real Glib here since we want the storage |
| 562 | // side-effects. |
| 563 | GLib glib; |
| 564 | ScopedTempDir temp_dir; |
| 565 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 566 | |
| 567 | Manager manager(control_interface(), |
| 568 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 569 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 570 | &glib, |
| 571 | run_path(), |
| 572 | storage_path(), |
| 573 | temp_dir.path().value()); |
| 574 | |
| 575 | // Invalid name should be rejected. |
| 576 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 577 | |
| 578 | // Valid name is still rejected because we can't create a profile |
| 579 | // that doesn't have a user component. Such profile names are |
| 580 | // reserved for the single DefaultProfile the manager creates |
| 581 | // at startup. |
| 582 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid")); |
| 583 | |
| 584 | // We should succeed in creating a valid user profile. |
| 585 | const char kProfile[] = "~user/profile"; |
| 586 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile)); |
| 587 | |
| 588 | // We should fail in creating it a second time (already exists). |
| 589 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 590 | } |
| 591 | |
| 592 | TEST_F(ManagerTest, PushPopProfile) { |
| 593 | // It's much easier to use real Glib in creating a Manager for this |
| 594 | // test here since we want the storage side-effects. |
| 595 | GLib glib; |
| 596 | ScopedTempDir temp_dir; |
| 597 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 598 | Manager manager(control_interface(), |
| 599 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 600 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 601 | &glib, |
| 602 | run_path(), |
| 603 | storage_path(), |
| 604 | temp_dir.path().value()); |
| 605 | |
| 606 | // Pushing an invalid profile should fail. |
| 607 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 608 | |
| 609 | // Pushing a default profile name should fail. |
| 610 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default")); |
| 611 | |
| 612 | const char kProfile0[] = "~user/profile0"; |
| 613 | const char kProfile1[] = "~user/profile1"; |
| 614 | |
| 615 | // Create a couple of profiles. |
| 616 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 617 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 618 | |
| 619 | // Push these profiles on the stack. |
| 620 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 621 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 622 | |
| 623 | // Pushing a profile a second time should fail. |
| 624 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 625 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 626 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 627 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 628 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 629 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 630 | |
| 631 | // Make sure a profile name that doesn't exist fails. |
| 632 | const char kProfile2Id[] = "profile2"; |
| 633 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 634 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 635 | |
| 636 | // Create a new service, with a specific storage name. |
| 637 | scoped_refptr<MockService> service( |
| 638 | new NiceMock<MockService>(control_interface(), |
| 639 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 640 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 641 | &manager)); |
| 642 | const char kServiceName[] = "service_storage_name"; |
| 643 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 644 | .WillRepeatedly(Return(kServiceName)); |
| 645 | EXPECT_CALL(*service.get(), Load(_)) |
| 646 | .WillRepeatedly(Return(true)); |
| 647 | |
| 648 | // Add this service to the manager -- it should end up in the ephemeral |
| 649 | // profile. |
| 650 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 651 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 652 | |
| 653 | // Create storage for a profile that contains the service storage name. |
| 654 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 655 | kServiceName)); |
| 656 | |
| 657 | // When we push the profile, the service should move away from the |
| 658 | // ephemeral profile to this new profile since it has an entry for |
| 659 | // this service. |
| 660 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 661 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 662 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 663 | |
| 664 | // Insert another profile that should supersede ownership of the service. |
| 665 | const char kProfile3Id[] = "profile3"; |
| 666 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 667 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 668 | kServiceName)); |
| 669 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 670 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 671 | |
| 672 | // Popping an invalid profile name should fail. |
| 673 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 674 | |
| 675 | // Popping an profile that is not at the top of the stack should fail. |
| 676 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 677 | |
| 678 | // Popping the top profile should succeed. |
| 679 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 680 | |
| 681 | // Moreover the service should have switched profiles to profile 2. |
| 682 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 683 | |
| 684 | // Popping the top profile should succeed. |
| 685 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 686 | |
| 687 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 688 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 689 | |
| 690 | // Pop the remaining two services off the stack. |
| 691 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 692 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 693 | |
| 694 | // Next pop should fail with "stack is empty". |
| 695 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
| 696 | } |
| 697 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 698 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 699 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 700 | // system teardown. |
| 701 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 702 | return arg.get() == ref_address; |
| 703 | } |
| 704 | |
| 705 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 706 | MockServiceRefPtr s_not_in_profile( |
| 707 | new NiceMock<MockService>(control_interface(), |
| 708 | dispatcher(), |
| 709 | metrics(), |
| 710 | manager())); |
| 711 | MockServiceRefPtr s_not_in_group( |
| 712 | new NiceMock<MockService>(control_interface(), |
| 713 | dispatcher(), |
| 714 | metrics(), |
| 715 | manager())); |
| 716 | MockServiceRefPtr s_configure_fail( |
| 717 | new NiceMock<MockService>(control_interface(), |
| 718 | dispatcher(), |
| 719 | metrics(), |
| 720 | manager())); |
| 721 | MockServiceRefPtr s_configure_succeed( |
| 722 | new NiceMock<MockService>(control_interface(), |
| 723 | dispatcher(), |
| 724 | metrics(), |
| 725 | manager())); |
| 726 | |
| 727 | string entry_name("entry_name"); |
| 728 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 729 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 730 | .WillRepeatedly(Return("not_entry_name")); |
| 731 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 732 | .WillRepeatedly(Return(entry_name)); |
| 733 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 734 | .WillRepeatedly(Return(entry_name)); |
| 735 | |
| 736 | manager()->RegisterService(s_not_in_profile); |
| 737 | manager()->RegisterService(s_not_in_group); |
| 738 | manager()->RegisterService(s_configure_fail); |
| 739 | manager()->RegisterService(s_configure_succeed); |
| 740 | |
| 741 | scoped_refptr<MockProfile> profile0( |
| 742 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 743 | scoped_refptr<MockProfile> profile1( |
| 744 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 745 | |
| 746 | s_not_in_group->set_profile(profile1); |
| 747 | s_configure_fail->set_profile(profile1); |
| 748 | s_configure_succeed->set_profile(profile1); |
| 749 | |
| 750 | AdoptProfile(manager(), profile0); |
| 751 | AdoptProfile(manager(), profile1); |
| 752 | |
| 753 | // No services are a member of this profile. |
| 754 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 755 | |
| 756 | // No services that are members of this profile have this entry name. |
| 757 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 758 | |
| 759 | // Only services that are members of the profile and group will be abandoned. |
| 760 | EXPECT_CALL(*profile1.get(), |
| 761 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 762 | EXPECT_CALL(*profile1.get(), |
| 763 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 764 | EXPECT_CALL(*profile1.get(), |
| 765 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 766 | .WillOnce(Return(true)); |
| 767 | EXPECT_CALL(*profile1.get(), |
| 768 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 769 | .WillOnce(Return(true)); |
| 770 | |
| 771 | // Never allow services to re-join profile1. |
| 772 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 773 | .WillRepeatedly(Return(false)); |
| 774 | |
| 775 | // Only allow one of the members of the profile and group to successfully |
| 776 | // join profile0. |
| 777 | EXPECT_CALL(*profile0.get(), |
| 778 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 779 | EXPECT_CALL(*profile0.get(), |
| 780 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 781 | EXPECT_CALL(*profile0.get(), |
| 782 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 783 | .WillOnce(Return(false)); |
| 784 | EXPECT_CALL(*profile0.get(), |
| 785 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 786 | .WillOnce(Return(true)); |
| 787 | |
| 788 | // Expect the failed-to-configure service to have Unload() called on it. |
| 789 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 790 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 791 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 792 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 793 | |
| 794 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 795 | |
| 796 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 797 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 798 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 799 | |
| 800 | // Since we are using a MockProfile, the profile does not actually change, |
| 801 | // since ConfigureService was not actually called on the service. |
| 802 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 803 | } |
| 804 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame^] | 805 | TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) { |
| 806 | MockServiceRefPtr s_will_remove0( |
| 807 | new NiceMock<MockService>(control_interface(), |
| 808 | dispatcher(), |
| 809 | metrics(), |
| 810 | manager())); |
| 811 | MockServiceRefPtr s_will_remove1( |
| 812 | new NiceMock<MockService>(control_interface(), |
| 813 | dispatcher(), |
| 814 | metrics(), |
| 815 | manager())); |
| 816 | MockServiceRefPtr s_will_not_remove0( |
| 817 | new NiceMock<MockService>(control_interface(), |
| 818 | dispatcher(), |
| 819 | metrics(), |
| 820 | manager())); |
| 821 | MockServiceRefPtr s_will_not_remove1( |
| 822 | new NiceMock<MockService>(control_interface(), |
| 823 | dispatcher(), |
| 824 | metrics(), |
| 825 | manager())); |
| 826 | |
| 827 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 828 | .Times(4); // Once for each registration. |
| 829 | |
| 830 | string entry_name("entry_name"); |
| 831 | EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier()) |
| 832 | .WillRepeatedly(Return(entry_name)); |
| 833 | EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier()) |
| 834 | .WillRepeatedly(Return(entry_name)); |
| 835 | EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier()) |
| 836 | .WillRepeatedly(Return(entry_name)); |
| 837 | EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier()) |
| 838 | .WillRepeatedly(Return(entry_name)); |
| 839 | |
| 840 | manager()->RegisterService(s_will_remove0); |
| 841 | manager()->RegisterService(s_will_not_remove0); |
| 842 | manager()->RegisterService(s_will_remove1); |
| 843 | manager()->RegisterService(s_will_not_remove1); |
| 844 | |
| 845 | // One for each service added above. |
| 846 | ASSERT_EQ(4, manager()->services_.size()); |
| 847 | |
| 848 | scoped_refptr<MockProfile> profile( |
| 849 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 850 | |
| 851 | s_will_remove0->set_profile(profile); |
| 852 | s_will_remove1->set_profile(profile); |
| 853 | s_will_not_remove0->set_profile(profile); |
| 854 | s_will_not_remove1->set_profile(profile); |
| 855 | |
| 856 | AdoptProfile(manager(), profile); |
| 857 | |
| 858 | // Deny any of the services re-entry to the profile. |
| 859 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 860 | .WillRepeatedly(Return(false)); |
| 861 | |
| 862 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0))) |
| 863 | .WillOnce(Return(true)); |
| 864 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1))) |
| 865 | .WillOnce(Return(true)); |
| 866 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0))) |
| 867 | .WillOnce(Return(true)); |
| 868 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1))) |
| 869 | .WillOnce(Return(true)); |
| 870 | |
| 871 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 872 | .WillOnce(Return(true)); |
| 873 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 874 | .WillOnce(Return(true)); |
| 875 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
| 876 | .WillOnce(Return(false)); |
| 877 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 878 | .WillOnce(Return(false)); |
| 879 | |
| 880 | |
| 881 | // This will cause all the profiles to be unloaded. |
| 882 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name)); |
| 883 | |
| 884 | // 2 of the 4 services added above should have been unregistered and |
| 885 | // removed, leaving 2. |
| 886 | EXPECT_EQ(2, manager()->services_.size()); |
| 887 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 888 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
| 889 | } |
| 890 | |
| 891 | TEST_F(ManagerTest, PopProfileWithUnload) { |
| 892 | MockServiceRefPtr s_will_remove0( |
| 893 | new NiceMock<MockService>(control_interface(), |
| 894 | dispatcher(), |
| 895 | metrics(), |
| 896 | manager())); |
| 897 | MockServiceRefPtr s_will_remove1( |
| 898 | new NiceMock<MockService>(control_interface(), |
| 899 | dispatcher(), |
| 900 | metrics(), |
| 901 | manager())); |
| 902 | MockServiceRefPtr s_will_not_remove0( |
| 903 | new NiceMock<MockService>(control_interface(), |
| 904 | dispatcher(), |
| 905 | metrics(), |
| 906 | manager())); |
| 907 | MockServiceRefPtr s_will_not_remove1( |
| 908 | new NiceMock<MockService>(control_interface(), |
| 909 | dispatcher(), |
| 910 | metrics(), |
| 911 | manager())); |
| 912 | |
| 913 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 914 | .Times(5); // Once for each registration, and one after profile pop. |
| 915 | |
| 916 | manager()->RegisterService(s_will_remove0); |
| 917 | manager()->RegisterService(s_will_not_remove0); |
| 918 | manager()->RegisterService(s_will_remove1); |
| 919 | manager()->RegisterService(s_will_not_remove1); |
| 920 | |
| 921 | // One for each service added above. |
| 922 | ASSERT_EQ(4, manager()->services_.size()); |
| 923 | |
| 924 | scoped_refptr<MockProfile> profile0( |
| 925 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 926 | scoped_refptr<MockProfile> profile1( |
| 927 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 928 | |
| 929 | s_will_remove0->set_profile(profile1); |
| 930 | s_will_remove1->set_profile(profile1); |
| 931 | s_will_not_remove0->set_profile(profile1); |
| 932 | s_will_not_remove1->set_profile(profile1); |
| 933 | |
| 934 | AdoptProfile(manager(), profile0); |
| 935 | AdoptProfile(manager(), profile1); |
| 936 | |
| 937 | // Deny any of the services entry to profile0, so they will all be unloaded. |
| 938 | EXPECT_CALL(*profile0, ConfigureService(_)) |
| 939 | .WillRepeatedly(Return(false)); |
| 940 | |
| 941 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 942 | .WillOnce(Return(true)); |
| 943 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 944 | .WillOnce(Return(true)); |
| 945 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
| 946 | .WillOnce(Return(false)); |
| 947 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 948 | .WillOnce(Return(false)); |
| 949 | |
| 950 | // This will pop profile1, which should cause all our profiles to unload. |
| 951 | manager()->PopProfileInternal(); |
| 952 | |
| 953 | // 2 of the 4 services added above should have been unregistered and |
| 954 | // removed, leaving 2. |
| 955 | EXPECT_EQ(2, manager()->services_.size()); |
| 956 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 957 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
| 958 | } |
| 959 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 960 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 961 | { |
| 962 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 963 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 964 | flimflam::kOfflineModeProperty, |
| 965 | PropertyStoreTest::kBoolV, |
| 966 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 967 | } |
| 968 | { |
| 969 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 970 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 971 | flimflam::kCountryProperty, |
| 972 | PropertyStoreTest::kStringV, |
| 973 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 974 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 975 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 976 | { |
| 977 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 978 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 979 | flimflam::kCountryProperty, |
| 980 | PropertyStoreTest::kBoolV, |
| 981 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 982 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 983 | } |
| 984 | { |
| 985 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 986 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 987 | flimflam::kOfflineModeProperty, |
| 988 | PropertyStoreTest::kStringV, |
| 989 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 990 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 991 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 992 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 993 | { |
| 994 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 995 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 996 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 997 | flimflam::kEnabledTechnologiesProperty, |
| 998 | PropertyStoreTest::kStringsV, |
| 999 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1000 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1001 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1004 | TEST_F(ManagerTest, RequestScan) { |
| 1005 | { |
| 1006 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1007 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 1008 | manager()->RegisterDevice(mock_devices_[1].get()); |
| 1009 | EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1010 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1011 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
| 1012 | EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1013 | .WillRepeatedly(Return(false)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1014 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1015 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | { |
| 1019 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1020 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1021 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1025 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1026 | KeyValueStore args; |
| 1027 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1028 | manager()->GetService(args, &e); |
| 1029 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1030 | EXPECT_EQ("must specify service type", e.message()); |
| 1031 | } |
| 1032 | |
| 1033 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 1034 | KeyValueStore args; |
| 1035 | Error e; |
| 1036 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 1037 | manager()->GetService(args, &e); |
| 1038 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1039 | EXPECT_EQ("service type is unsupported", e.message()); |
| 1040 | } |
| 1041 | |
| 1042 | TEST_F(ManagerTest, GetServiceNoWifiDevice) { |
| 1043 | KeyValueStore args; |
| 1044 | Error e; |
| 1045 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1046 | manager()->GetService(args, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1047 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1048 | EXPECT_EQ("no wifi devices available", e.message()); |
| 1049 | } |
| 1050 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1051 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1052 | KeyValueStore args; |
| 1053 | Error e; |
| 1054 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1055 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1056 | manager()->RegisterDevice(mock_wifi_); |
| 1057 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 1058 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1059 | manager()->GetService(args, &e); |
| 1060 | EXPECT_TRUE(e.IsSuccess()); |
| 1061 | } |
| 1062 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1063 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 1064 | KeyValueStore args; |
| 1065 | Error e; |
| 1066 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 1067 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1068 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1069 | EXPECT_FALSE(service); |
| 1070 | } |
| 1071 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1072 | TEST_F(ManagerTest, GetServiceVPN) { |
| 1073 | KeyValueStore args; |
| 1074 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1075 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1076 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 1077 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 1078 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1079 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1080 | EXPECT_TRUE(e.IsSuccess()); |
| 1081 | EXPECT_TRUE(service); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1084 | TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) { |
| 1085 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1086 | scoped_refptr<MockProfile> profile( |
| 1087 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1088 | AdoptProfile(manager(), profile); |
| 1089 | |
| 1090 | KeyValueStore args; |
| 1091 | args.SetString(flimflam::kProfileProperty, "xxx"); |
| 1092 | Error error; |
| 1093 | manager()->ConfigureService(args, &error); |
| 1094 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1095 | EXPECT_EQ("Invalid profile name xxx", error.message()); |
| 1096 | } |
| 1097 | |
| 1098 | TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) { |
| 1099 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1100 | scoped_refptr<MockProfile> profile( |
| 1101 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1102 | AdoptProfile(manager(), profile); |
| 1103 | |
| 1104 | KeyValueStore args; |
| 1105 | Error error; |
| 1106 | manager()->ConfigureService(args, &error); |
| 1107 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1108 | EXPECT_EQ("must specify service type", error.message()); |
| 1109 | } |
| 1110 | |
| 1111 | // A registered service in the ephemeral profile should be moved to the |
| 1112 | // active profile as a part of configuration if no profile was explicitly |
| 1113 | // specified. |
| 1114 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) { |
| 1115 | scoped_refptr<MockProfile> profile( |
| 1116 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1117 | |
| 1118 | AdoptProfile(manager(), profile); // This is now the active profile. |
| 1119 | |
| 1120 | const std::vector<uint8_t> ssid; |
| 1121 | scoped_refptr<MockWiFiService> service( |
| 1122 | new NiceMock<MockWiFiService>(control_interface(), |
| 1123 | dispatcher(), |
| 1124 | metrics(), |
| 1125 | manager(), |
| 1126 | mock_wifi_, |
| 1127 | ssid, |
| 1128 | "", |
| 1129 | "", |
| 1130 | false)); |
| 1131 | |
| 1132 | manager()->RegisterService(service); |
| 1133 | service->set_profile(GetEphemeralProfile(manager())); |
| 1134 | |
| 1135 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1136 | // the same device as that used above causes a refcounting loop. |
| 1137 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1138 | dispatcher(), |
| 1139 | metrics(), |
| 1140 | manager(), |
| 1141 | "wifi1", |
| 1142 | "addr5", |
| 1143 | 5)); |
| 1144 | manager()->RegisterDevice(wifi); |
| 1145 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1146 | .WillOnce(Return(service)); |
| 1147 | EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get()))) |
| 1148 | .WillOnce(Return(true)); |
| 1149 | EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get()))) |
| 1150 | .WillOnce(Return(true)); |
| 1151 | |
| 1152 | KeyValueStore args; |
| 1153 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1154 | Error error; |
| 1155 | manager()->ConfigureService(args, &error); |
| 1156 | EXPECT_TRUE(error.IsSuccess()); |
| 1157 | } |
| 1158 | |
| 1159 | // If were configure a service that was already registered and explicitly |
| 1160 | // specify a profile, it should be moved from the profile it was previously |
| 1161 | // in to the specified profile if one was requested. |
| 1162 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) { |
| 1163 | scoped_refptr<MockProfile> profile0( |
| 1164 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1165 | scoped_refptr<MockProfile> profile1( |
| 1166 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1167 | |
| 1168 | const string kProfileName0 = "profile0"; |
| 1169 | const string kProfileName1 = "profile1"; |
| 1170 | |
| 1171 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1172 | .WillRepeatedly(Return(kProfileName0)); |
| 1173 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1174 | .WillRepeatedly(Return(kProfileName1)); |
| 1175 | |
| 1176 | AdoptProfile(manager(), profile0); |
| 1177 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1178 | |
| 1179 | const std::vector<uint8_t> ssid; |
| 1180 | scoped_refptr<MockWiFiService> service( |
| 1181 | new NiceMock<MockWiFiService>(control_interface(), |
| 1182 | dispatcher(), |
| 1183 | metrics(), |
| 1184 | manager(), |
| 1185 | mock_wifi_, |
| 1186 | ssid, |
| 1187 | "", |
| 1188 | "", |
| 1189 | false)); |
| 1190 | |
| 1191 | manager()->RegisterService(service); |
| 1192 | service->set_profile(profile1); |
| 1193 | |
| 1194 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1195 | // the same device as that used above causes a refcounting loop. |
| 1196 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1197 | dispatcher(), |
| 1198 | metrics(), |
| 1199 | manager(), |
| 1200 | "wifi1", |
| 1201 | "addr5", |
| 1202 | 5)); |
| 1203 | manager()->RegisterDevice(wifi); |
| 1204 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1205 | .WillOnce(Return(service)); |
| 1206 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1207 | .WillOnce(Return(true)); |
| 1208 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1209 | .WillOnce(Return(true)); |
| 1210 | EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get()))) |
| 1211 | .WillOnce(Return(true)); |
| 1212 | |
| 1213 | KeyValueStore args; |
| 1214 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1215 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1216 | Error error; |
| 1217 | manager()->ConfigureService(args, &error); |
| 1218 | EXPECT_TRUE(error.IsSuccess()); |
| 1219 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1220 | } |
| 1221 | |
| 1222 | // An unregistered service should remain unregistered, but its contents should |
| 1223 | // be saved to the specified profile nonetheless. |
| 1224 | TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) { |
| 1225 | scoped_refptr<MockProfile> profile0( |
| 1226 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1227 | scoped_refptr<MockProfile> profile1( |
| 1228 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1229 | |
| 1230 | const string kProfileName0 = "profile0"; |
| 1231 | const string kProfileName1 = "profile1"; |
| 1232 | |
| 1233 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1234 | .WillRepeatedly(Return(kProfileName0)); |
| 1235 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1236 | .WillRepeatedly(Return(kProfileName1)); |
| 1237 | |
| 1238 | AdoptProfile(manager(), profile0); |
| 1239 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1240 | |
| 1241 | const std::vector<uint8_t> ssid; |
| 1242 | scoped_refptr<MockWiFiService> service( |
| 1243 | new NiceMock<MockWiFiService>(control_interface(), |
| 1244 | dispatcher(), |
| 1245 | metrics(), |
| 1246 | manager(), |
| 1247 | mock_wifi_, |
| 1248 | ssid, |
| 1249 | "", |
| 1250 | "", |
| 1251 | false)); |
| 1252 | |
| 1253 | service->set_profile(profile1); |
| 1254 | |
| 1255 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1256 | // the same device as that used above causes a refcounting loop. |
| 1257 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1258 | dispatcher(), |
| 1259 | metrics(), |
| 1260 | manager(), |
| 1261 | "wifi1", |
| 1262 | "addr5", |
| 1263 | 5)); |
| 1264 | manager()->RegisterDevice(wifi); |
| 1265 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1266 | .WillOnce(Return(service)); |
| 1267 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1268 | .WillOnce(Return(true)); |
| 1269 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 1270 | .Times(0); |
| 1271 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 1272 | .Times(0); |
| 1273 | |
| 1274 | KeyValueStore args; |
| 1275 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1276 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1277 | Error error; |
| 1278 | manager()->ConfigureService(args, &error); |
| 1279 | EXPECT_TRUE(error.IsSuccess()); |
| 1280 | } |
| 1281 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1282 | TEST_F(ManagerTest, TechnologyOrder) { |
| 1283 | Error error; |
| 1284 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 1285 | string(flimflam::kTypeWifi), &error); |
| 1286 | ASSERT_TRUE(error.IsSuccess()); |
| 1287 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 1288 | string(flimflam::kTypeEthernet) + "," + |
| 1289 | string(flimflam::kTypeWifi)); |
| 1290 | |
| 1291 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 1292 | string(flimflam::kTypeWifi), &error); |
| 1293 | ASSERT_FALSE(error.IsSuccess()); |
| 1294 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1295 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 1296 | string(flimflam::kTypeWifi), |
| 1297 | manager()->GetTechnologyOrder()); |
| 1298 | } |
| 1299 | |
| 1300 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1301 | // TODO(quiche): Some of these tests would probably fit better in |
| 1302 | // service_unittest, since the actual comparison of Services is |
| 1303 | // implemented in Service. (crosbug.com/23370) |
| 1304 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1305 | scoped_refptr<MockService> mock_service0( |
| 1306 | new NiceMock<MockService>(control_interface(), |
| 1307 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1308 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1309 | manager())); |
| 1310 | scoped_refptr<MockService> mock_service1( |
| 1311 | new NiceMock<MockService>(control_interface(), |
| 1312 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1313 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1314 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1315 | |
| 1316 | manager()->RegisterService(mock_service0); |
| 1317 | manager()->RegisterService(mock_service1); |
| 1318 | |
| 1319 | // Services should already be sorted by UniqueName |
| 1320 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1321 | |
| 1322 | // Asking explictly to sort services should not change anything |
| 1323 | manager()->SortServices(); |
| 1324 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1325 | |
| 1326 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 1327 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1328 | manager()->UpdateService(mock_service1); |
| 1329 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1330 | |
| 1331 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 1332 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1333 | manager()->UpdateService(mock_service0); |
| 1334 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1335 | |
| 1336 | // Technology |
| 1337 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi)) |
| 1338 | .WillRepeatedly(Return(true)); |
| 1339 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet)) |
| 1340 | .WillRepeatedly(Return(true)); |
| 1341 | // NB: Redefine default (false) return values so we don't use the default rule |
| 1342 | // which makes the logs noisier |
| 1343 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi))) |
| 1344 | .WillRepeatedly(Return(false)); |
| 1345 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet))) |
| 1346 | .WillRepeatedly(Return(false)); |
| 1347 | |
| 1348 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 1349 | // Default technology ordering should favor Ethernet over WiFi. |
| 1350 | manager()->SortServices(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1351 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1352 | |
| 1353 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 1354 | string(flimflam::kTypeEthernet), &error); |
| 1355 | EXPECT_TRUE(error.IsSuccess()); |
| 1356 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1357 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1358 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1359 | mock_service0->set_priority(1); |
| 1360 | manager()->UpdateService(mock_service0); |
| 1361 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1362 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1363 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1364 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1365 | manager()->UpdateService(mock_service1); |
| 1366 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1367 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1368 | // Auto-connect. |
| 1369 | mock_service0->set_auto_connect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1370 | manager()->UpdateService(mock_service0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1371 | mock_service1->set_auto_connect(false); |
| 1372 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1373 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1374 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1375 | // Connectable. |
| 1376 | mock_service1->set_connectable(true); |
| 1377 | manager()->UpdateService(mock_service1); |
| 1378 | mock_service0->set_connectable(false); |
| 1379 | manager()->UpdateService(mock_service0); |
| 1380 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1381 | |
| 1382 | // IsFailed. |
| 1383 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1384 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 1385 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 1386 | .WillRepeatedly(Return(false)); |
| 1387 | manager()->UpdateService(mock_service0); |
| 1388 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1389 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1390 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 1391 | .WillRepeatedly(Return(true)); |
| 1392 | manager()->UpdateService(mock_service1); |
| 1393 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1394 | |
| 1395 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1396 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1397 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 1398 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1399 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1400 | manager()->UpdateService(mock_service1); |
| 1401 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1402 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1403 | // Connected. |
| 1404 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1405 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1406 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 1407 | .WillRepeatedly(Return(true)); |
| 1408 | manager()->UpdateService(mock_service0); |
| 1409 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1410 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1411 | manager()->DeregisterService(mock_service0); |
| 1412 | manager()->DeregisterService(mock_service1); |
| 1413 | } |
| 1414 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1415 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1416 | MockMetrics mock_metrics; |
| 1417 | manager()->set_metrics(&mock_metrics); |
| 1418 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1419 | scoped_refptr<MockService> mock_service0( |
| 1420 | new NiceMock<MockService>(control_interface(), |
| 1421 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1422 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1423 | manager())); |
| 1424 | scoped_refptr<MockService> mock_service1( |
| 1425 | new NiceMock<MockService>(control_interface(), |
| 1426 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1427 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1428 | manager())); |
| 1429 | |
| 1430 | scoped_refptr<MockConnection> mock_connection0( |
| 1431 | new NiceMock<MockConnection>(device_info_.get())); |
| 1432 | scoped_refptr<MockConnection> mock_connection1( |
| 1433 | new NiceMock<MockConnection>(device_info_.get())); |
| 1434 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1435 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1436 | manager()->RegisterService(mock_service0); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1437 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1438 | manager()->RegisterService(mock_service1); |
| 1439 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1440 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1441 | manager()->SortServices(); |
| 1442 | |
| 1443 | mock_service1->set_priority(1); |
| 1444 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1445 | manager()->SortServices(); |
| 1446 | |
| 1447 | mock_service1->set_priority(0); |
| 1448 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1449 | manager()->SortServices(); |
| 1450 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1451 | mock_service0->set_mock_connection(mock_connection0); |
| 1452 | mock_service1->set_mock_connection(mock_connection1); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1453 | |
| 1454 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1455 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1456 | manager()->SortServices(); |
| 1457 | |
| 1458 | mock_service1->set_priority(1); |
| 1459 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 1460 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1461 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1462 | manager()->SortServices(); |
| 1463 | |
| 1464 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1465 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1466 | mock_service1->set_mock_connection(NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1467 | manager()->DeregisterService(mock_service1); |
| 1468 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1469 | mock_service0->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1470 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1471 | manager()->DeregisterService(mock_service0); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1472 | |
| 1473 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1474 | manager()->SortServices(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1477 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 1478 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 1479 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1480 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1481 | manager(), |
| 1482 | "null4", |
| 1483 | "addr4", |
| 1484 | 0)); |
| 1485 | manager()->RegisterDevice(mock_devices_[0]); |
| 1486 | manager()->RegisterDevice(mock_devices_[1]); |
| 1487 | manager()->RegisterDevice(mock_devices_[2]); |
| 1488 | manager()->RegisterDevice(mock_devices_[3]); |
| 1489 | |
| 1490 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1491 | .WillByDefault(Return(Technology::kEthernet)); |
| 1492 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1493 | .WillByDefault(Return(Technology::kWifi)); |
| 1494 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1495 | .WillByDefault(Return(Technology::kCellular)); |
| 1496 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1497 | .WillByDefault(Return(Technology::kWifi)); |
| 1498 | |
| 1499 | set<string> expected_technologies; |
| 1500 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1501 | Technology::kEthernet)); |
| 1502 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1503 | Technology::kWifi)); |
| 1504 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1505 | Technology::kCellular)); |
| 1506 | Error error; |
| 1507 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 1508 | |
| 1509 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1510 | ContainerEq(expected_technologies)); |
| 1511 | } |
| 1512 | |
| 1513 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 1514 | scoped_refptr<MockService> connected_service1( |
| 1515 | new NiceMock<MockService>(control_interface(), |
| 1516 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1517 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1518 | manager())); |
| 1519 | scoped_refptr<MockService> connected_service2( |
| 1520 | new NiceMock<MockService>(control_interface(), |
| 1521 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1522 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1523 | manager())); |
| 1524 | scoped_refptr<MockService> disconnected_service1( |
| 1525 | new NiceMock<MockService>(control_interface(), |
| 1526 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1527 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1528 | manager())); |
| 1529 | scoped_refptr<MockService> disconnected_service2( |
| 1530 | new NiceMock<MockService>(control_interface(), |
| 1531 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1532 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1533 | manager())); |
| 1534 | |
| 1535 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 1536 | .WillByDefault(Return(true)); |
| 1537 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 1538 | .WillByDefault(Return(true)); |
| 1539 | |
| 1540 | manager()->RegisterService(connected_service1); |
| 1541 | manager()->RegisterService(connected_service2); |
| 1542 | manager()->RegisterService(disconnected_service1); |
| 1543 | manager()->RegisterService(disconnected_service2); |
| 1544 | |
| 1545 | manager()->RegisterDevice(mock_devices_[0]); |
| 1546 | manager()->RegisterDevice(mock_devices_[1]); |
| 1547 | manager()->RegisterDevice(mock_devices_[2]); |
| 1548 | manager()->RegisterDevice(mock_devices_[3]); |
| 1549 | |
| 1550 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1551 | .WillByDefault(Return(Technology::kEthernet)); |
| 1552 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1553 | .WillByDefault(Return(Technology::kWifi)); |
| 1554 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1555 | .WillByDefault(Return(Technology::kCellular)); |
| 1556 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1557 | .WillByDefault(Return(Technology::kWifi)); |
| 1558 | |
| 1559 | mock_devices_[0]->SelectService(connected_service1); |
| 1560 | mock_devices_[1]->SelectService(disconnected_service1); |
| 1561 | mock_devices_[2]->SelectService(disconnected_service2); |
| 1562 | mock_devices_[3]->SelectService(connected_service2); |
| 1563 | |
| 1564 | set<string> expected_technologies; |
| 1565 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1566 | Technology::kEthernet)); |
| 1567 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1568 | Technology::kWifi)); |
| 1569 | Error error; |
| 1570 | |
| 1571 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 1572 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1573 | ContainerEq(expected_technologies)); |
| 1574 | } |
| 1575 | |
| 1576 | TEST_F(ManagerTest, DefaultTechnology) { |
| 1577 | scoped_refptr<MockService> connected_service( |
| 1578 | new NiceMock<MockService>(control_interface(), |
| 1579 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1580 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1581 | manager())); |
| 1582 | scoped_refptr<MockService> disconnected_service( |
| 1583 | new NiceMock<MockService>(control_interface(), |
| 1584 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1585 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1586 | manager())); |
| 1587 | |
| 1588 | // Connected. WiFi. |
| 1589 | ON_CALL(*connected_service.get(), IsConnected()) |
| 1590 | .WillByDefault(Return(true)); |
| 1591 | ON_CALL(*connected_service.get(), state()) |
| 1592 | .WillByDefault(Return(Service::kStateConnected)); |
| 1593 | ON_CALL(*connected_service.get(), technology()) |
| 1594 | .WillByDefault(Return(Technology::kWifi)); |
| 1595 | |
| 1596 | // Disconnected. Ethernet. |
| 1597 | ON_CALL(*disconnected_service.get(), technology()) |
| 1598 | .WillByDefault(Return(Technology::kEthernet)); |
| 1599 | |
| 1600 | manager()->RegisterService(disconnected_service); |
| 1601 | Error error; |
| 1602 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 1603 | |
| 1604 | |
| 1605 | manager()->RegisterService(connected_service); |
| 1606 | // Connected service should be brought to the front now. |
| 1607 | string expected_technology = |
| 1608 | Technology::NameFromIdentifier(Technology::kWifi); |
| 1609 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 1610 | } |
| 1611 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1612 | TEST_F(ManagerTest, DisconnectServicesOnStop) { |
| 1613 | scoped_refptr<MockService> mock_service( |
| 1614 | new NiceMock<MockService>(control_interface(), |
| 1615 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1616 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1617 | manager())); |
| 1618 | manager()->RegisterService(mock_service); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1619 | EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1620 | manager()->Stop(); |
| 1621 | } |
| 1622 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1623 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 1624 | scoped_refptr<MockService> mock_service( |
| 1625 | new NiceMock<MockService>(control_interface(), |
| 1626 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1627 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1628 | manager())); |
| 1629 | manager()->RegisterService(mock_service); |
| 1630 | EXPECT_FALSE(mock_service->favorite()); |
| 1631 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1632 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1633 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1634 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1635 | manager()->UpdateService(mock_service); |
| 1636 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1637 | // to mock out MakeFavorite. And mocking that out would break the |
| 1638 | // SortServices test. (crosbug.com/23370) |
| 1639 | EXPECT_TRUE(mock_service->favorite()); |
| 1640 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1641 | } |
| 1642 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 1643 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 1644 | // This tests the case where the user connects to a service that is |
| 1645 | // currently associated with a profile. We want to make sure that the |
| 1646 | // favorite flag is set and that the flag is saved to the current |
| 1647 | // profile. |
| 1648 | scoped_refptr<MockService> mock_service( |
| 1649 | new NiceMock<MockService>(control_interface(), |
| 1650 | dispatcher(), |
| 1651 | metrics(), |
| 1652 | manager())); |
| 1653 | manager()->RegisterService(mock_service); |
| 1654 | EXPECT_FALSE(mock_service->favorite()); |
| 1655 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1656 | |
| 1657 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
| 1658 | mock_service->set_profile(profile); |
| 1659 | |
| 1660 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1661 | .WillRepeatedly(Return(true)); |
| 1662 | EXPECT_CALL(*mock_service.get(), SaveToCurrentProfile()); |
| 1663 | manager()->UpdateService(mock_service); |
| 1664 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1665 | // to mock out MakeFavorite. And mocking that out would break the |
| 1666 | // SortServices test. (crosbug.com/23370) |
| 1667 | EXPECT_TRUE(mock_service->favorite()); |
| 1668 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1669 | } |
| 1670 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1671 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 1672 | scoped_refptr<MockProfile> profile( |
| 1673 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1674 | AdoptProfile(manager(), profile); |
| 1675 | scoped_refptr<MockService> service( |
| 1676 | new NiceMock<MockService>(control_interface(), |
| 1677 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1678 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1679 | manager())); |
| 1680 | |
| 1681 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 1682 | ServiceRefPtr expect_service(service.get()); |
| 1683 | |
| 1684 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 1685 | .WillOnce(Return(false)); |
| 1686 | manager()->RegisterService(service); |
| 1687 | |
| 1688 | EXPECT_CALL(*service.get(), state()) |
| 1689 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1690 | EXPECT_CALL(*service.get(), IsConnected()) |
| 1691 | .WillRepeatedly(Return(true)); |
| 1692 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 1693 | .WillOnce(Return(true)); |
| 1694 | manager()->UpdateService(service); |
| 1695 | } |
| 1696 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1697 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 1698 | vector<string> profile_paths; |
| 1699 | for (size_t i = 0; i < 10; i++) { |
| 1700 | scoped_refptr<MockProfile> profile( |
| 1701 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 1702 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1703 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 1704 | .WillOnce(Return(profile_paths.back())); |
| 1705 | AdoptProfile(manager(), profile); |
| 1706 | } |
| 1707 | |
| 1708 | Error error; |
| 1709 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 1710 | EXPECT_TRUE(error.IsSuccess()); |
| 1711 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 1712 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 1713 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 1714 | } |
| 1715 | } |
| 1716 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1717 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 1718 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 1719 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 1720 | manager()->RegisterService(service); |
| 1721 | dispatcher()->DispatchPendingEvents(); |
| 1722 | } |
| 1723 | |
| 1724 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 1725 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1726 | service1->set_priority(1); |
| 1727 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1728 | service2->set_priority(2); |
| 1729 | manager()->RegisterService(service1); |
| 1730 | manager()->RegisterService(service2); |
| 1731 | dispatcher()->DispatchPendingEvents(); |
| 1732 | |
| 1733 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1734 | EXPECT_CALL(*service2.get(), state()) |
| 1735 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1736 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 1737 | .WillRepeatedly(Return(true)); |
| 1738 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 1739 | .WillRepeatedly(Return(false)); |
| 1740 | manager()->UpdateService(service2); |
| 1741 | dispatcher()->DispatchPendingEvents(); |
| 1742 | } |
| 1743 | |
| 1744 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 1745 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1746 | service1->set_priority(1); |
| 1747 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1748 | service2->set_priority(2); |
| 1749 | manager()->RegisterService(service1); |
| 1750 | manager()->RegisterService(service2); |
| 1751 | dispatcher()->DispatchPendingEvents(); |
| 1752 | |
| 1753 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1754 | manager()->DeregisterService(service2); |
| 1755 | dispatcher()->DispatchPendingEvents(); |
| 1756 | } |
| 1757 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1758 | TEST_F(ManagerTest, RecheckPortal) { |
| 1759 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 1760 | .WillOnce(Return(false)); |
| 1761 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 1762 | .WillOnce(Return(true)); |
| 1763 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 1764 | .Times(0); |
| 1765 | |
| 1766 | manager()->RegisterDevice(mock_devices_[0]); |
| 1767 | manager()->RegisterDevice(mock_devices_[1]); |
| 1768 | manager()->RegisterDevice(mock_devices_[2]); |
| 1769 | |
| 1770 | manager()->RecheckPortal(NULL); |
| 1771 | } |
| 1772 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1773 | TEST_F(ManagerTest, GetDefaultService) { |
| 1774 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
| 1775 | |
| 1776 | scoped_refptr<MockService> mock_service( |
| 1777 | new NiceMock<MockService>(control_interface(), |
| 1778 | dispatcher(), |
| 1779 | metrics(), |
| 1780 | manager())); |
| 1781 | |
| 1782 | manager()->RegisterService(mock_service); |
| 1783 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
| 1784 | |
| 1785 | scoped_refptr<MockConnection> mock_connection( |
| 1786 | new NiceMock<MockConnection>(device_info_.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1787 | mock_service->set_mock_connection(mock_connection); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1788 | EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get()); |
| 1789 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1790 | mock_service->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1791 | manager()->DeregisterService(mock_service); |
| 1792 | } |
| 1793 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1794 | TEST_F(ManagerTest, GetServiceWithGUID) { |
| 1795 | scoped_refptr<MockService> mock_service0( |
| 1796 | new NiceMock<MockService>(control_interface(), |
| 1797 | dispatcher(), |
| 1798 | metrics(), |
| 1799 | manager())); |
| 1800 | |
| 1801 | scoped_refptr<MockService> mock_service1( |
| 1802 | new NiceMock<MockService>(control_interface(), |
| 1803 | dispatcher(), |
| 1804 | metrics(), |
| 1805 | manager())); |
| 1806 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1807 | EXPECT_CALL(*mock_service0.get(), Configure(_, _)) |
| 1808 | .Times(0); |
| 1809 | EXPECT_CALL(*mock_service1.get(), Configure(_, _)) |
| 1810 | .Times(0); |
| 1811 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1812 | manager()->RegisterService(mock_service0); |
| 1813 | manager()->RegisterService(mock_service1); |
| 1814 | |
| 1815 | const string kGUID0 = "GUID0"; |
| 1816 | const string kGUID1 = "GUID1"; |
| 1817 | |
| 1818 | { |
| 1819 | Error error; |
| 1820 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 1821 | EXPECT_FALSE(error.IsSuccess()); |
| 1822 | EXPECT_FALSE(service); |
| 1823 | } |
| 1824 | |
| 1825 | KeyValueStore args; |
| 1826 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 1827 | |
| 1828 | { |
| 1829 | Error error; |
| 1830 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 1831 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1832 | EXPECT_FALSE(service); |
| 1833 | } |
| 1834 | |
| 1835 | mock_service0->set_guid(kGUID0); |
| 1836 | mock_service1->set_guid(kGUID1); |
| 1837 | |
| 1838 | { |
| 1839 | Error error; |
| 1840 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 1841 | EXPECT_TRUE(error.IsSuccess()); |
| 1842 | EXPECT_EQ(mock_service0.get(), service.get()); |
| 1843 | } |
| 1844 | |
| 1845 | { |
| 1846 | Error error; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1847 | EXPECT_CALL(*mock_service1.get(), Configure(_, &error)) |
| 1848 | .Times(1); |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1849 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 1850 | EXPECT_TRUE(error.IsSuccess()); |
| 1851 | EXPECT_EQ(mock_service1.get(), service.get()); |
| 1852 | } |
| 1853 | |
| 1854 | manager()->DeregisterService(mock_service0); |
| 1855 | manager()->DeregisterService(mock_service1); |
| 1856 | } |
| 1857 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1858 | } // namespace shill |