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 | |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 7 | #include <map> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 8 | #include <set> |
| 9 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 12 | #include <base/file_util.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/scoped_temp_dir.h> |
| 14 | #include <base/stl_util.h> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 19 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 20 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 21 | #include "shill/ephemeral_profile.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 22 | #include "shill/error.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 23 | #include "shill/glib.h" |
| 24 | #include "shill/key_file_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 25 | #include "shill/key_value_store.h" |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 26 | #include "shill/logging.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 27 | #include "shill/mock_adaptors.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 28 | #include "shill/mock_connection.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 29 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 30 | #include "shill/mock_device.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 31 | #include "shill/mock_device_info.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/mock_glib.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 33 | #include "shill/mock_metrics.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 34 | #include "shill/mock_power_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 35 | #include "shill/mock_profile.h" |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 36 | #include "shill/mock_resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 37 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 38 | #include "shill/mock_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 39 | #include "shill/mock_wifi.h" |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 40 | #include "shill/mock_wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 41 | #include "shill/property_store_unittest.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 42 | #include "shill/proxy_factory.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 43 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 44 | #include "shill/wifi_service.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 45 | #include "shill/wimax_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 46 | |
| 47 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 48 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | using std::string; |
| 50 | using std::vector; |
| 51 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 52 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 53 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 54 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 55 | using ::testing::ContainerEq; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 56 | using ::testing::DoAll; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 57 | using ::testing::InSequence; |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 58 | using ::testing::Mock; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 59 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 60 | using ::testing::NiceMock; |
| 61 | using ::testing::Return; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 62 | using ::testing::ReturnRef; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 63 | using ::testing::SaveArg; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 64 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 65 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 66 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 67 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 68 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 69 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 70 | ManagerTest() |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 71 | : power_manager_(new MockPowerManager(NULL, &proxy_factory_)), |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 72 | mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | metrics(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 75 | manager(), |
| 76 | "wifi0", |
| 77 | "addr4", |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 78 | 4)), |
| 79 | device_info_(new NiceMock<MockDeviceInfo>( |
| 80 | control_interface(), |
| 81 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 82 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 83 | reinterpret_cast<Manager*>(NULL))), |
| 84 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 85 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 86 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 87 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 88 | manager(), |
| 89 | "null0", |
| 90 | "addr0", |
| 91 | 0)); |
| 92 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 93 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 94 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 95 | manager(), |
| 96 | "null1", |
| 97 | "addr1", |
| 98 | 1)); |
| 99 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 100 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 101 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 102 | manager(), |
| 103 | "null2", |
| 104 | "addr2", |
| 105 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 106 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 107 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 108 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 109 | manager(), |
| 110 | "null3", |
| 111 | "addr3", |
| 112 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 113 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 114 | SetRunning(true); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 115 | |
| 116 | // Replace the manager's adaptor with a quieter one, and one |
| 117 | // we can do EXPECT*() against. Passes ownership. |
| 118 | manager()->adaptor_.reset(manager_adaptor_); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 119 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 120 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 121 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 122 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 123 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 124 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 125 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 126 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 127 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 128 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 129 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 130 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 131 | manager->profiles_.push_back(profile); |
| 132 | } |
| 133 | |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 134 | void SetRunning(bool running) { |
| 135 | manager()->running_ = running; |
| 136 | } |
| 137 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 138 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 139 | return manager->ephemeral_profile_; |
| 140 | } |
| 141 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 142 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 143 | Profile::Identifier id("rather", "irrelevant"); |
| 144 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 145 | manager, |
| 146 | id, |
| 147 | "", |
| 148 | false)); |
| 149 | FilePath final_path(storage_path()); |
| 150 | final_path = final_path.Append("test.profile"); |
| 151 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 152 | storage->set_path(final_path); |
| 153 | if (!storage->Open()) |
| 154 | return NULL; |
| 155 | profile->set_storage(storage.release()); // Passes ownership. |
| 156 | return profile.release(); |
| 157 | } |
| 158 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 159 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 160 | const string &profile_identifier, |
| 161 | const string &service_name) { |
| 162 | GLib glib; |
| 163 | KeyFileStore store(&glib); |
| 164 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 165 | return store.Open() && |
| 166 | store.SetString(service_name, "rather", "irrelevant") && |
| 167 | store.Close(); |
| 168 | } |
| 169 | |
| 170 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 171 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 172 | string path; |
| 173 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 174 | return error.type(); |
| 175 | } |
| 176 | |
| 177 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 178 | Error error; |
| 179 | manager->PopAnyProfile(&error); |
| 180 | return error.type(); |
| 181 | } |
| 182 | |
| 183 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 184 | Error error; |
| 185 | manager->PopProfile(name, &error); |
| 186 | return error.type(); |
| 187 | } |
| 188 | |
| 189 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 190 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 191 | string path; |
| 192 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 193 | return error.type(); |
| 194 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 195 | |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 196 | void AddMockProfileToManager(Manager *manager) { |
| 197 | scoped_refptr<MockProfile> profile( |
| 198 | new MockProfile(control_interface(), manager, "")); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 199 | EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return("/")); |
| 200 | EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false)); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 201 | AdoptProfile(manager, profile); |
| 202 | } |
| 203 | |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 204 | void CompleteServiceSort() { |
| 205 | EXPECT_FALSE(manager()->sort_services_task_.IsCancelled()); |
| 206 | dispatcher()->DispatchPendingEvents(); |
| 207 | EXPECT_TRUE(manager()->sort_services_task_.IsCancelled()); |
| 208 | } |
| 209 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 210 | RpcIdentifier GetDefaultServiceRpcIdentifier() { |
| 211 | return manager()->GetDefaultServiceRpcIdentifier(NULL); |
| 212 | } |
| 213 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 214 | void SetResolver(Resolver *resolver) { |
| 215 | manager()->resolver_ = resolver; |
| 216 | } |
| 217 | |
| 218 | void SetIgnoredDNSSearchPaths(const string &search_paths) { |
| 219 | manager()->SetIgnoredDNSSearchPaths(search_paths, NULL); |
| 220 | } |
| 221 | |
| 222 | const string &GetIgnoredDNSSearchPaths() { |
| 223 | return manager()->props_.ignored_dns_search_paths; |
| 224 | } |
| 225 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 226 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 227 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 228 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 229 | class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> { |
| 230 | public: |
| 231 | ServiceWatcher() {} |
| 232 | virtual ~ServiceWatcher() {} |
| 233 | |
| 234 | MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service)); |
| 235 | |
| 236 | private: |
| 237 | DISALLOW_COPY_AND_ASSIGN(ServiceWatcher); |
| 238 | }; |
| 239 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 240 | class TestProxyFactory : public ProxyFactory { |
| 241 | public: |
| 242 | TestProxyFactory() {} |
| 243 | |
| 244 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 245 | PowerManagerProxyDelegate */*delegate*/) { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | private: |
| 250 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 251 | }; |
| 252 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 253 | class TerminationActionTest : |
| 254 | public base::SupportsWeakPtr<TerminationActionTest> { |
| 255 | public: |
| 256 | static const char kActionName[]; |
| 257 | |
| 258 | TerminationActionTest() : manager_(NULL) {} |
| 259 | virtual ~TerminationActionTest() {} |
| 260 | |
| 261 | MOCK_METHOD1(Done, void(const Error &error)); |
| 262 | |
| 263 | void Action() { |
| 264 | manager_->TerminationActionComplete("action"); |
| 265 | } |
| 266 | |
| 267 | void set_manager(Manager *manager) { manager_ = manager; } |
| 268 | |
| 269 | private: |
| 270 | Manager *manager_; |
| 271 | DISALLOW_COPY_AND_ASSIGN(TerminationActionTest); |
| 272 | }; |
| 273 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 274 | void SetPowerState(PowerManagerProxyDelegate::SuspendState state) { |
| 275 | power_manager_->power_state_ = state; |
| 276 | } |
| 277 | |
| 278 | void SetPowerManager() { |
| 279 | manager()->set_power_manager(power_manager_.release()); |
| 280 | } |
| 281 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 282 | HookTable *GetTerminationActions() { |
| 283 | return &manager()->termination_actions_; |
| 284 | } |
| 285 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 286 | void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) { |
| 287 | manager()->OnPowerStateChanged(state); |
| 288 | } |
| 289 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 290 | void OnSuspendDelay(uint32 sequence_number) { |
| 291 | manager()->OnSuspendDelay(sequence_number); |
| 292 | } |
| 293 | |
| 294 | void OnSuspendActionsComplete(uint32 sequence_number, const Error &error) { |
| 295 | manager()->OnSuspendActionsComplete(sequence_number, error); |
| 296 | } |
| 297 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 298 | vector<string> EnumerateAvailableServices() { |
| 299 | return manager()->EnumerateAvailableServices(NULL); |
| 300 | } |
| 301 | |
| 302 | vector<string> EnumerateWatchedServices() { |
| 303 | return manager()->EnumerateWatchedServices(NULL); |
| 304 | } |
| 305 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 306 | MockServiceRefPtr MakeAutoConnectableService() { |
| 307 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 308 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 309 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 310 | manager()); |
| 311 | service->MakeFavorite(); |
| 312 | service->set_connectable(true); |
| 313 | return service; |
| 314 | } |
| 315 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 316 | TestProxyFactory proxy_factory_; |
| 317 | scoped_ptr<MockPowerManager> power_manager_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 318 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 319 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 320 | scoped_ptr<MockDeviceInfo> device_info_; |
| 321 | |
| 322 | // This pointer is owned by the manager, and only tracked here for EXPECT*() |
| 323 | ManagerMockAdaptor *manager_adaptor_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 326 | const char ManagerTest::TerminationActionTest::kActionName[] = "action"; |
| 327 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 328 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 329 | if (!manager()->sort_services_task_.IsCancelled()) { |
| 330 | manager()->SortServicesTask(); |
| 331 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 332 | return (svc0.get() == manager()->services_[0].get() && |
| 333 | svc1.get() == manager()->services_[1].get()); |
| 334 | } |
| 335 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 336 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 337 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 338 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 341 | TEST_F(ManagerTest, DeviceRegistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 342 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 343 | .WillByDefault(Return(Technology::kEthernet)); |
| 344 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 345 | .WillByDefault(Return(Technology::kWifi)); |
| 346 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 347 | .WillByDefault(Return(Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 348 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 349 | manager()->RegisterDevice(mock_devices_[0]); |
| 350 | manager()->RegisterDevice(mock_devices_[1]); |
| 351 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 352 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 353 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 354 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 355 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 358 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 359 | manager()->running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 360 | mock_devices_[0]->enabled_persistent_ = true; |
| 361 | mock_devices_[1]->enabled_persistent_ = false; |
| 362 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 363 | .Times(1); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 364 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 365 | .Times(0); |
| 366 | manager()->RegisterDevice(mock_devices_[0]); |
| 367 | manager()->RegisterDevice(mock_devices_[1]); |
| 368 | } |
| 369 | |
| 370 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 371 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 372 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 373 | AdoptProfile(manager(), profile); // Passes ownership. |
| 374 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 375 | EXPECT_CALL(*profile, UpdateDevice(device_ref)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 376 | manager()->RegisterDevice(mock_devices_[0]); |
| 377 | } |
| 378 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 379 | TEST_F(ManagerTest, DeviceDeregistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 380 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 381 | .WillByDefault(Return(Technology::kEthernet)); |
| 382 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 383 | .WillByDefault(Return(Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 384 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 385 | manager()->RegisterDevice(mock_devices_[0]); |
| 386 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 387 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 388 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 389 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 390 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 391 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 392 | AdoptProfile(manager(), profile); // Passes ownership. |
| 393 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 394 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 395 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 396 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 397 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 398 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 399 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 400 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 401 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 402 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 406 | // It's much easier and safer to use a real GLib for this test. |
| 407 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 408 | Manager manager(control_interface(), |
| 409 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 410 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 411 | &glib, |
| 412 | run_path(), |
| 413 | storage_path(), |
| 414 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 415 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 416 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 417 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 418 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 419 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 420 | new NiceMock<MockService>(control_interface(), |
| 421 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 422 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 423 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 424 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 425 | new NiceMock<MockService>(control_interface(), |
| 426 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 427 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 428 | &manager)); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 429 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 430 | string service1_name(mock_service->UniqueName()); |
| 431 | string service2_name(mock_service2->UniqueName()); |
| 432 | |
| 433 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 434 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 435 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 436 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 437 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 438 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 439 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 440 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 441 | manager.RegisterService(mock_service); |
| 442 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 443 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 444 | Error error; |
| 445 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 446 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 447 | EXPECT_EQ(2, ids.size()); |
| 448 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 449 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 450 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 451 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 452 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 453 | |
| 454 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 457 | TEST_F(ManagerTest, RegisterKnownService) { |
| 458 | // It's much easier and safer to use a real GLib for this test. |
| 459 | GLib glib; |
| 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, |
| 464 | run_path(), |
| 465 | storage_path(), |
| 466 | string()); |
| 467 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 468 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 469 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 470 | { |
| 471 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 472 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 473 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 474 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 475 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 476 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 477 | } // Force destruction of service1. |
| 478 | |
| 479 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 480 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 481 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 482 | &manager)); |
| 483 | manager.RegisterService(service2); |
| 484 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 485 | manager.Stop(); |
| 486 | } |
| 487 | |
| 488 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 489 | // It's much easier and safer to use a real GLib for this test. |
| 490 | GLib glib; |
| 491 | Manager manager(control_interface(), |
| 492 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 493 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 494 | &glib, |
| 495 | run_path(), |
| 496 | storage_path(), |
| 497 | string()); |
| 498 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 499 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 500 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 501 | { |
| 502 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 503 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 504 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 505 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 506 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 507 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 508 | } // Force destruction of service1. |
| 509 | scoped_refptr<MockService> mock_service2( |
| 510 | new NiceMock<MockService>(control_interface(), |
| 511 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 512 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 513 | &manager)); |
| 514 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 515 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 516 | manager.RegisterService(mock_service2); |
| 517 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 518 | manager.Stop(); |
| 519 | } |
| 520 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 521 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 522 | // WiFi assumes that it can deregister a service that is not |
| 523 | // registered. (E.g. a hidden service can be deregistered when it |
| 524 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 525 | // |
| 526 | // So test that doing so doesn't cause a crash. |
| 527 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 528 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 529 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 530 | manager()); |
| 531 | manager()->DeregisterService(service); |
| 532 | } |
| 533 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 534 | TEST_F(ManagerTest, GetProperties) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 535 | AddMockProfileToManager(manager()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 536 | map<string, ::DBus::Variant> props; |
| 537 | Error error(Error::kInvalidProperty, ""); |
| 538 | { |
| 539 | ::DBus::Error dbus_error; |
| 540 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 541 | manager()->mutable_store()->SetStringProperty( |
| 542 | flimflam::kCheckPortalListProperty, |
| 543 | expected, |
| 544 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 545 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 546 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 547 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 548 | expected); |
| 549 | } |
| 550 | { |
| 551 | ::DBus::Error dbus_error; |
| 552 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 553 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 554 | expected, |
| 555 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 556 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 557 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 558 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 559 | expected); |
| 560 | } |
| 561 | } |
| 562 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 563 | TEST_F(ManagerTest, GetDevicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 564 | AddMockProfileToManager(manager()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 565 | manager()->RegisterDevice(mock_devices_[0]); |
| 566 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 567 | { |
| 568 | map<string, ::DBus::Variant> props; |
| 569 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 570 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 571 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 572 | vector < ::DBus::Path> devices = |
| 573 | props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>(); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 574 | EXPECT_EQ(2, devices.size()); |
| 575 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 576 | } |
| 577 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 578 | TEST_F(ManagerTest, GetServicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 579 | AddMockProfileToManager(manager()); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 580 | map<string, ::DBus::Variant> props; |
| 581 | ::DBus::Error dbus_error; |
| 582 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
| 583 | map<string, ::DBus::Variant>::const_iterator prop = |
| 584 | props.find(flimflam::kServicesProperty); |
| 585 | ASSERT_FALSE(prop == props.end()); |
| 586 | const ::DBus::Variant &variant = prop->second; |
| 587 | ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature())); |
| 588 | } |
| 589 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 590 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 591 | Manager manager(control_interface(), |
| 592 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 593 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 594 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 595 | run_path(), |
| 596 | storage_path(), |
| 597 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 598 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 599 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 600 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 601 | &manager)); |
| 602 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 603 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 604 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 605 | ProfileRefPtr profile( |
| 606 | new Profile(control_interface(), &manager, id, "", false)); |
| 607 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 608 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 609 | .WillRepeatedly(Return(true)); |
| 610 | EXPECT_CALL(*storage, Flush()) |
| 611 | .Times(AnyNumber()) |
| 612 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 613 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 614 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 615 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 616 | // Create a profile that already has |s2| in it. |
| 617 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 618 | EXPECT_TRUE(profile->AdoptService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 619 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 620 | // Now, move the Service |s2| to another profile. |
| 621 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 622 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 623 | |
| 624 | // Force destruction of the original Profile, to ensure that the Service |
| 625 | // is kept alive and populated with data. |
| 626 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 627 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 628 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 631 | TEST_F(ManagerTest, LookupProfileByRpcIdentifier) { |
| 632 | scoped_refptr<MockProfile> mock_profile( |
| 633 | new MockProfile(control_interface(), manager(), "")); |
| 634 | const string kProfileName("profile0"); |
| 635 | EXPECT_CALL(*mock_profile, GetRpcIdentifier()) |
| 636 | .WillRepeatedly(Return(kProfileName)); |
| 637 | AdoptProfile(manager(), mock_profile); |
| 638 | |
| 639 | EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo")); |
| 640 | ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName); |
| 641 | EXPECT_EQ(mock_profile.get(), profile.get()); |
| 642 | } |
| 643 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 644 | TEST_F(ManagerTest, SetProfileForService) { |
| 645 | scoped_refptr<MockProfile> profile0( |
| 646 | new MockProfile(control_interface(), manager(), "")); |
| 647 | string profile_name0("profile0"); |
| 648 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 649 | .WillRepeatedly(Return(profile_name0)); |
| 650 | AdoptProfile(manager(), profile0); |
| 651 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 652 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 653 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 654 | manager())); |
Paul Stewart | 649f3a4 | 2012-04-24 23:22:16 -0700 | [diff] [blame] | 655 | EXPECT_FALSE(manager()->HasService(service)); |
| 656 | { |
| 657 | Error error; |
| 658 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 659 | .WillOnce(Return(true)); |
| 660 | // Expect that setting the profile of a service that does not already |
| 661 | // have one assigned does not cause a crash. |
| 662 | manager()->SetProfileForService(service, "profile0", &error); |
| 663 | EXPECT_TRUE(error.IsSuccess()); |
| 664 | } |
| 665 | |
| 666 | // The service should be registered as a side-effect of the profile being |
| 667 | // set for this service. |
| 668 | EXPECT_TRUE(manager()->HasService(service)); |
| 669 | |
| 670 | // Since we have mocked Profile::AdoptServie() above, the service's |
| 671 | // profile was not actually changed. Do so explicitly now. |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 672 | service->set_profile(profile0); |
| 673 | |
| 674 | { |
| 675 | Error error; |
| 676 | manager()->SetProfileForService(service, "foo", &error); |
| 677 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 678 | EXPECT_EQ("Unknown Profile foo requested for Service", error.message()); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | { |
| 682 | Error error; |
| 683 | manager()->SetProfileForService(service, profile_name0, &error); |
| 684 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 685 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 686 | } |
| 687 | |
| 688 | scoped_refptr<MockProfile> profile1( |
| 689 | new MockProfile(control_interface(), manager(), "")); |
| 690 | string profile_name1("profile1"); |
| 691 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 692 | .WillRepeatedly(Return(profile_name1)); |
| 693 | AdoptProfile(manager(), profile1); |
| 694 | |
| 695 | { |
| 696 | Error error; |
| 697 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 698 | .WillOnce(Return(true)); |
| 699 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 700 | .WillOnce(Return(true)); |
| 701 | manager()->SetProfileForService(service, profile_name1, &error); |
| 702 | EXPECT_TRUE(error.IsSuccess()); |
| 703 | } |
| 704 | } |
| 705 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 706 | TEST_F(ManagerTest, CreateProfile) { |
| 707 | // It's much easier to use real Glib here since we want the storage |
| 708 | // side-effects. |
| 709 | GLib glib; |
| 710 | ScopedTempDir temp_dir; |
| 711 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 712 | |
| 713 | Manager manager(control_interface(), |
| 714 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 715 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 716 | &glib, |
| 717 | run_path(), |
| 718 | storage_path(), |
| 719 | temp_dir.path().value()); |
| 720 | |
| 721 | // Invalid name should be rejected. |
| 722 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 723 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 724 | // A profile with invalid characters in it should similarly be rejected. |
| 725 | EXPECT_EQ(Error::kInvalidArguments, |
| 726 | TestCreateProfile(&manager, "valid_profile")); |
| 727 | |
| 728 | // We should be able to create a machine profile. |
| 729 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 730 | |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 731 | // We should succeed in creating a valid user profile. Verify the returned |
| 732 | // path. |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 733 | const char kProfile[] = "~user/profile"; |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 734 | { |
| 735 | Error error; |
| 736 | string path; |
| 737 | manager.CreateProfile(kProfile, &path, &error); |
| 738 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 739 | EXPECT_EQ("/profile_rpc", path); |
| 740 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 741 | |
| 742 | // We should fail in creating it a second time (already exists). |
| 743 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 744 | } |
| 745 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 746 | // We receive PopProfile when a user logs out, and it should always trigger a |
| 747 | // MemoryLog Clear() call. |
| 748 | TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) { |
| 749 | GLib glib; |
| 750 | ScopedTempDir temp_dir; |
| 751 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 752 | Manager manager(control_interface(), |
| 753 | dispatcher(), |
| 754 | metrics(), |
| 755 | &glib, |
| 756 | run_path(), |
| 757 | storage_path(), |
| 758 | temp_dir.path().value()); |
| 759 | const char kProfile0[] = "~user/profile0"; |
| 760 | const char kPurgedMessage[] = "This message should be purged"; |
| 761 | // Create a profile and push it on the stack, leave one uncreated |
| 762 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 763 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 764 | |
| 765 | // Popping a profile which isn't on top should still clear the log. |
| 766 | LOG(INFO) << kPurgedMessage; |
| 767 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 768 | kPurgedMessage)); |
| 769 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1")); |
| 770 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 771 | kPurgedMessage)); |
| 772 | |
| 773 | // Popping an invalid profile name should do the same thing. |
| 774 | LOG(INFO) << kPurgedMessage; |
| 775 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 776 | kPurgedMessage)); |
| 777 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 778 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 779 | kPurgedMessage)); |
| 780 | |
| 781 | // Successful pops also purge the message log. |
| 782 | LOG(INFO) << kPurgedMessage; |
| 783 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 784 | kPurgedMessage)); |
| 785 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0)); |
| 786 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 787 | kPurgedMessage)); |
| 788 | } |
| 789 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 790 | TEST_F(ManagerTest, PushPopProfile) { |
| 791 | // It's much easier to use real Glib in creating a Manager for this |
| 792 | // test here since we want the storage side-effects. |
| 793 | GLib glib; |
| 794 | ScopedTempDir temp_dir; |
| 795 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 796 | Manager manager(control_interface(), |
| 797 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 798 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 799 | &glib, |
| 800 | run_path(), |
| 801 | storage_path(), |
| 802 | temp_dir.path().value()); |
| 803 | |
| 804 | // Pushing an invalid profile should fail. |
| 805 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 806 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 807 | // Pushing a default profile that does not exist should fail. |
| 808 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 809 | |
| 810 | const char kProfile0[] = "~user/profile0"; |
| 811 | const char kProfile1[] = "~user/profile1"; |
| 812 | |
| 813 | // Create a couple of profiles. |
| 814 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 815 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 816 | |
| 817 | // Push these profiles on the stack. |
| 818 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 819 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 820 | |
| 821 | // Pushing a profile a second time should fail. |
| 822 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 823 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 824 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 825 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 826 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 827 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 828 | |
| 829 | // Make sure a profile name that doesn't exist fails. |
| 830 | const char kProfile2Id[] = "profile2"; |
| 831 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 832 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 833 | |
| 834 | // Create a new service, with a specific storage name. |
| 835 | scoped_refptr<MockService> service( |
| 836 | new NiceMock<MockService>(control_interface(), |
| 837 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 838 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 839 | &manager)); |
| 840 | const char kServiceName[] = "service_storage_name"; |
| 841 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 842 | .WillRepeatedly(Return(kServiceName)); |
| 843 | EXPECT_CALL(*service.get(), Load(_)) |
| 844 | .WillRepeatedly(Return(true)); |
| 845 | |
| 846 | // Add this service to the manager -- it should end up in the ephemeral |
| 847 | // profile. |
| 848 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 849 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 850 | |
| 851 | // Create storage for a profile that contains the service storage name. |
| 852 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 853 | kServiceName)); |
| 854 | |
| 855 | // When we push the profile, the service should move away from the |
| 856 | // ephemeral profile to this new profile since it has an entry for |
| 857 | // this service. |
| 858 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 859 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 860 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 861 | |
| 862 | // Insert another profile that should supersede ownership of the service. |
| 863 | const char kProfile3Id[] = "profile3"; |
| 864 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 865 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 866 | kServiceName)); |
| 867 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 868 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 869 | |
| 870 | // Popping an invalid profile name should fail. |
| 871 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 872 | |
| 873 | // Popping an profile that is not at the top of the stack should fail. |
| 874 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 875 | |
| 876 | // Popping the top profile should succeed. |
| 877 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 878 | |
| 879 | // Moreover the service should have switched profiles to profile 2. |
| 880 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 881 | |
| 882 | // Popping the top profile should succeed. |
| 883 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 884 | |
| 885 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 886 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 887 | |
| 888 | // Pop the remaining two services off the stack. |
| 889 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 890 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 891 | |
| 892 | // Next pop should fail with "stack is empty". |
| 893 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 894 | |
| 895 | const char kMachineProfile0[] = "machineprofile0"; |
| 896 | const char kMachineProfile1[] = "machineprofile1"; |
| 897 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0)); |
| 898 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1)); |
| 899 | |
| 900 | // Should be able to push a machine profile. |
| 901 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0)); |
| 902 | |
| 903 | // Should be able to push a user profile atop a machine profile. |
| 904 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 905 | |
| 906 | // Pushing a system-wide profile on top of a user profile should fail. |
| 907 | EXPECT_EQ(Error::kInvalidArguments, |
| 908 | TestPushProfile(&manager, kMachineProfile1)); |
| 909 | |
| 910 | // However if we pop the user profile, we should be able stack another |
| 911 | // machine profile on. |
| 912 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 913 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 916 | TEST_F(ManagerTest, RemoveProfile) { |
| 917 | // It's much easier to use real Glib in creating a Manager for this |
| 918 | // test here since we want the storage side-effects. |
| 919 | GLib glib; |
| 920 | ScopedTempDir temp_dir; |
| 921 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 922 | Manager manager(control_interface(), |
| 923 | dispatcher(), |
| 924 | metrics(), |
| 925 | &glib, |
| 926 | run_path(), |
| 927 | storage_path(), |
| 928 | temp_dir.path().value()); |
| 929 | |
| 930 | const char kProfile0[] = "profile0"; |
| 931 | FilePath profile_path( |
| 932 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 933 | |
| 934 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 935 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 936 | |
| 937 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 938 | |
| 939 | // Remove should fail since the profile is still on the stack. |
| 940 | { |
| 941 | Error error; |
| 942 | manager.RemoveProfile(kProfile0, &error); |
| 943 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 944 | } |
| 945 | |
| 946 | // Profile path should still exist. |
| 947 | EXPECT_TRUE(file_util::PathExists(profile_path)); |
| 948 | |
| 949 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 950 | |
| 951 | // This should succeed now that the profile is off the stack. |
| 952 | { |
| 953 | Error error; |
| 954 | manager.RemoveProfile(kProfile0, &error); |
| 955 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 956 | } |
| 957 | |
| 958 | // Profile path should no longer exist. |
| 959 | EXPECT_FALSE(file_util::PathExists(profile_path)); |
| 960 | |
| 961 | // Another remove succeeds, due to a foible in file_util::Delete -- |
| 962 | // it is not an error to delete a file that does not exist. |
| 963 | { |
| 964 | Error error; |
| 965 | manager.RemoveProfile(kProfile0, &error); |
| 966 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 967 | } |
| 968 | |
| 969 | // Let's create an error case that will "work". Create a non-empty |
| 970 | // directory in the place of the profile pathname. |
| 971 | ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo"))); |
| 972 | { |
| 973 | Error error; |
| 974 | manager.RemoveProfile(kProfile0, &error); |
| 975 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 976 | } |
| 977 | } |
| 978 | |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 979 | TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) { |
| 980 | // It's much easier to use real Glib in creating a Manager for this |
| 981 | // test here since we want the storage side-effects. |
| 982 | GLib glib; |
| 983 | ScopedTempDir temp_dir; |
| 984 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 985 | Manager manager(control_interface(), |
| 986 | dispatcher(), |
| 987 | metrics(), |
| 988 | &glib, |
| 989 | run_path(), |
| 990 | storage_path(), |
| 991 | temp_dir.path().value()); |
| 992 | |
| 993 | const char kProfile0[] = "profile0"; |
| 994 | FilePath profile_path( |
| 995 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 996 | |
| 997 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 998 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 999 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 1000 | |
| 1001 | // Ensure that even if the backing filestore is removed, we still can't |
| 1002 | // create a profile twice. |
| 1003 | ASSERT_TRUE(file_util::Delete(profile_path, false)); |
| 1004 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0)); |
| 1005 | } |
| 1006 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1007 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 1008 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 1009 | // system teardown. |
| 1010 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 1011 | return arg.get() == ref_address; |
| 1012 | } |
| 1013 | |
| 1014 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 1015 | MockServiceRefPtr s_not_in_profile( |
| 1016 | new NiceMock<MockService>(control_interface(), |
| 1017 | dispatcher(), |
| 1018 | metrics(), |
| 1019 | manager())); |
| 1020 | MockServiceRefPtr s_not_in_group( |
| 1021 | new NiceMock<MockService>(control_interface(), |
| 1022 | dispatcher(), |
| 1023 | metrics(), |
| 1024 | manager())); |
| 1025 | MockServiceRefPtr s_configure_fail( |
| 1026 | new NiceMock<MockService>(control_interface(), |
| 1027 | dispatcher(), |
| 1028 | metrics(), |
| 1029 | manager())); |
| 1030 | MockServiceRefPtr s_configure_succeed( |
| 1031 | new NiceMock<MockService>(control_interface(), |
| 1032 | dispatcher(), |
| 1033 | metrics(), |
| 1034 | manager())); |
| 1035 | |
| 1036 | string entry_name("entry_name"); |
| 1037 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 1038 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 1039 | .WillRepeatedly(Return("not_entry_name")); |
| 1040 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 1041 | .WillRepeatedly(Return(entry_name)); |
| 1042 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 1043 | .WillRepeatedly(Return(entry_name)); |
| 1044 | |
| 1045 | manager()->RegisterService(s_not_in_profile); |
| 1046 | manager()->RegisterService(s_not_in_group); |
| 1047 | manager()->RegisterService(s_configure_fail); |
| 1048 | manager()->RegisterService(s_configure_succeed); |
| 1049 | |
| 1050 | scoped_refptr<MockProfile> profile0( |
| 1051 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1052 | scoped_refptr<MockProfile> profile1( |
| 1053 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1054 | |
| 1055 | s_not_in_group->set_profile(profile1); |
| 1056 | s_configure_fail->set_profile(profile1); |
| 1057 | s_configure_succeed->set_profile(profile1); |
| 1058 | |
| 1059 | AdoptProfile(manager(), profile0); |
| 1060 | AdoptProfile(manager(), profile1); |
| 1061 | |
| 1062 | // No services are a member of this profile. |
| 1063 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 1064 | |
| 1065 | // No services that are members of this profile have this entry name. |
| 1066 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 1067 | |
| 1068 | // Only services that are members of the profile and group will be abandoned. |
| 1069 | EXPECT_CALL(*profile1.get(), |
| 1070 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1071 | EXPECT_CALL(*profile1.get(), |
| 1072 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1073 | EXPECT_CALL(*profile1.get(), |
| 1074 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 1075 | .WillOnce(Return(true)); |
| 1076 | EXPECT_CALL(*profile1.get(), |
| 1077 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1078 | .WillOnce(Return(true)); |
| 1079 | |
| 1080 | // Never allow services to re-join profile1. |
| 1081 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 1082 | .WillRepeatedly(Return(false)); |
| 1083 | |
| 1084 | // Only allow one of the members of the profile and group to successfully |
| 1085 | // join profile0. |
| 1086 | EXPECT_CALL(*profile0.get(), |
| 1087 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1088 | EXPECT_CALL(*profile0.get(), |
| 1089 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1090 | EXPECT_CALL(*profile0.get(), |
| 1091 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 1092 | .WillOnce(Return(false)); |
| 1093 | EXPECT_CALL(*profile0.get(), |
| 1094 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1095 | .WillOnce(Return(true)); |
| 1096 | |
| 1097 | // Expect the failed-to-configure service to have Unload() called on it. |
| 1098 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 1099 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 1100 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 1101 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 1102 | |
| 1103 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 1104 | |
| 1105 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 1106 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 1107 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 1108 | |
| 1109 | // Since we are using a MockProfile, the profile does not actually change, |
| 1110 | // since ConfigureService was not actually called on the service. |
| 1111 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 1112 | } |
| 1113 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1114 | TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) { |
| 1115 | MockServiceRefPtr s_will_remove0( |
| 1116 | new NiceMock<MockService>(control_interface(), |
| 1117 | dispatcher(), |
| 1118 | metrics(), |
| 1119 | manager())); |
| 1120 | MockServiceRefPtr s_will_remove1( |
| 1121 | new NiceMock<MockService>(control_interface(), |
| 1122 | dispatcher(), |
| 1123 | metrics(), |
| 1124 | manager())); |
| 1125 | MockServiceRefPtr s_will_not_remove0( |
| 1126 | new NiceMock<MockService>(control_interface(), |
| 1127 | dispatcher(), |
| 1128 | metrics(), |
| 1129 | manager())); |
| 1130 | MockServiceRefPtr s_will_not_remove1( |
| 1131 | new NiceMock<MockService>(control_interface(), |
| 1132 | dispatcher(), |
| 1133 | metrics(), |
| 1134 | manager())); |
| 1135 | |
| 1136 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1137 | .Times(4); // Once for each registration. |
| 1138 | |
| 1139 | string entry_name("entry_name"); |
| 1140 | EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier()) |
| 1141 | .WillRepeatedly(Return(entry_name)); |
| 1142 | EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier()) |
| 1143 | .WillRepeatedly(Return(entry_name)); |
| 1144 | EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier()) |
| 1145 | .WillRepeatedly(Return(entry_name)); |
| 1146 | EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier()) |
| 1147 | .WillRepeatedly(Return(entry_name)); |
| 1148 | |
| 1149 | manager()->RegisterService(s_will_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1150 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1151 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1152 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1153 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1154 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1155 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1156 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1157 | |
| 1158 | // One for each service added above. |
| 1159 | ASSERT_EQ(4, manager()->services_.size()); |
| 1160 | |
| 1161 | scoped_refptr<MockProfile> profile( |
| 1162 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1163 | |
| 1164 | s_will_remove0->set_profile(profile); |
| 1165 | s_will_remove1->set_profile(profile); |
| 1166 | s_will_not_remove0->set_profile(profile); |
| 1167 | s_will_not_remove1->set_profile(profile); |
| 1168 | |
| 1169 | AdoptProfile(manager(), profile); |
| 1170 | |
| 1171 | // Deny any of the services re-entry to the profile. |
| 1172 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1173 | .WillRepeatedly(Return(false)); |
| 1174 | |
| 1175 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0))) |
| 1176 | .WillOnce(Return(true)); |
| 1177 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1))) |
| 1178 | .WillOnce(Return(true)); |
| 1179 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0))) |
| 1180 | .WillOnce(Return(true)); |
| 1181 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1))) |
| 1182 | .WillOnce(Return(true)); |
| 1183 | |
| 1184 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1185 | .WillOnce(Return(true)); |
| 1186 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1187 | .WillOnce(Return(true)); |
| 1188 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
| 1189 | .WillOnce(Return(false)); |
| 1190 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1191 | .WillOnce(Return(false)); |
| 1192 | |
| 1193 | |
| 1194 | // This will cause all the profiles to be unloaded. |
| 1195 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name)); |
| 1196 | |
| 1197 | // 2 of the 4 services added above should have been unregistered and |
| 1198 | // removed, leaving 2. |
| 1199 | EXPECT_EQ(2, manager()->services_.size()); |
| 1200 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1201 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
| 1202 | } |
| 1203 | |
| 1204 | TEST_F(ManagerTest, PopProfileWithUnload) { |
| 1205 | MockServiceRefPtr s_will_remove0( |
| 1206 | new NiceMock<MockService>(control_interface(), |
| 1207 | dispatcher(), |
| 1208 | metrics(), |
| 1209 | manager())); |
| 1210 | MockServiceRefPtr s_will_remove1( |
| 1211 | new NiceMock<MockService>(control_interface(), |
| 1212 | dispatcher(), |
| 1213 | metrics(), |
| 1214 | manager())); |
| 1215 | MockServiceRefPtr s_will_not_remove0( |
| 1216 | new NiceMock<MockService>(control_interface(), |
| 1217 | dispatcher(), |
| 1218 | metrics(), |
| 1219 | manager())); |
| 1220 | MockServiceRefPtr s_will_not_remove1( |
| 1221 | new NiceMock<MockService>(control_interface(), |
| 1222 | dispatcher(), |
| 1223 | metrics(), |
| 1224 | manager())); |
| 1225 | |
| 1226 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1227 | .Times(5); // Once for each registration, and one after profile pop. |
| 1228 | |
| 1229 | manager()->RegisterService(s_will_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1230 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1231 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1232 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1233 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1234 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1235 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1236 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1237 | |
| 1238 | // One for each service added above. |
| 1239 | ASSERT_EQ(4, manager()->services_.size()); |
| 1240 | |
| 1241 | scoped_refptr<MockProfile> profile0( |
| 1242 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1243 | scoped_refptr<MockProfile> profile1( |
| 1244 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1245 | |
| 1246 | s_will_remove0->set_profile(profile1); |
| 1247 | s_will_remove1->set_profile(profile1); |
| 1248 | s_will_not_remove0->set_profile(profile1); |
| 1249 | s_will_not_remove1->set_profile(profile1); |
| 1250 | |
| 1251 | AdoptProfile(manager(), profile0); |
| 1252 | AdoptProfile(manager(), profile1); |
| 1253 | |
| 1254 | // Deny any of the services entry to profile0, so they will all be unloaded. |
| 1255 | EXPECT_CALL(*profile0, ConfigureService(_)) |
| 1256 | .WillRepeatedly(Return(false)); |
| 1257 | |
| 1258 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1259 | .WillOnce(Return(true)); |
| 1260 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1261 | .WillOnce(Return(true)); |
| 1262 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1263 | .WillRepeatedly(Return(false)); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1264 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1265 | .WillOnce(Return(false)); |
| 1266 | |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1267 | // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the |
| 1268 | // profile list. |
| 1269 | EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber()); |
| 1270 | EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber()); |
| 1271 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1272 | // This will pop profile1, which should cause all our profiles to unload. |
| 1273 | manager()->PopProfileInternal(); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1274 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1275 | |
| 1276 | // 2 of the 4 services added above should have been unregistered and |
| 1277 | // removed, leaving 2. |
| 1278 | EXPECT_EQ(2, manager()->services_.size()); |
| 1279 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1280 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1281 | |
| 1282 | // Expect the unloaded services to lose their profile reference. |
| 1283 | EXPECT_FALSE(s_will_remove0->profile()); |
| 1284 | EXPECT_FALSE(s_will_remove1->profile()); |
| 1285 | |
| 1286 | // If we explicitly deregister a service, the effect should be the same |
| 1287 | // with respect to the profile reference. |
| 1288 | ASSERT_TRUE(s_will_not_remove0->profile()); |
| 1289 | manager()->DeregisterService(s_will_not_remove0); |
| 1290 | EXPECT_FALSE(s_will_not_remove0->profile()); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1293 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1294 | { |
| 1295 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1296 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1297 | flimflam::kOfflineModeProperty, |
| 1298 | PropertyStoreTest::kBoolV, |
| 1299 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1300 | } |
| 1301 | { |
| 1302 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1303 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1304 | flimflam::kCountryProperty, |
| 1305 | PropertyStoreTest::kStringV, |
| 1306 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1307 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1308 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1309 | { |
| 1310 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1311 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1312 | flimflam::kCountryProperty, |
| 1313 | PropertyStoreTest::kBoolV, |
| 1314 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1315 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1316 | } |
| 1317 | { |
| 1318 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1319 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1320 | flimflam::kOfflineModeProperty, |
| 1321 | PropertyStoreTest::kStringV, |
| 1322 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1323 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1324 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1325 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1326 | { |
| 1327 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1328 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 1329 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1330 | flimflam::kEnabledTechnologiesProperty, |
| 1331 | PropertyStoreTest::kStringsV, |
| 1332 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1333 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1334 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1337 | TEST_F(ManagerTest, RequestScan) { |
| 1338 | { |
| 1339 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1340 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 1341 | manager()->RegisterDevice(mock_devices_[1].get()); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1342 | EXPECT_CALL(*mock_devices_[0], technology()) |
| 1343 | .WillRepeatedly(Return(Technology::kWifi)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1344 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1345 | EXPECT_CALL(*mock_devices_[1], technology()) |
| 1346 | .WillRepeatedly(Return(Technology::kUnknown)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1347 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1348 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | { |
| 1352 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1353 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1354 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1355 | } |
| 1356 | } |
| 1357 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1358 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1359 | KeyValueStore args; |
| 1360 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1361 | manager()->GetService(args, &e); |
| 1362 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1363 | EXPECT_EQ("must specify service type", e.message()); |
| 1364 | } |
| 1365 | |
| 1366 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 1367 | KeyValueStore args; |
| 1368 | Error e; |
| 1369 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 1370 | manager()->GetService(args, &e); |
| 1371 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1372 | EXPECT_EQ("service type is unsupported", e.message()); |
| 1373 | } |
| 1374 | |
| 1375 | TEST_F(ManagerTest, GetServiceNoWifiDevice) { |
| 1376 | KeyValueStore args; |
| 1377 | Error e; |
| 1378 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1379 | manager()->GetService(args, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1380 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1381 | EXPECT_EQ("no wifi devices available", e.message()); |
| 1382 | } |
| 1383 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1384 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1385 | KeyValueStore args; |
| 1386 | Error e; |
| 1387 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1388 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1389 | manager()->RegisterDevice(mock_wifi_); |
| 1390 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 1391 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1392 | manager()->GetService(args, &e); |
| 1393 | EXPECT_TRUE(e.IsSuccess()); |
| 1394 | } |
| 1395 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1396 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 1397 | KeyValueStore args; |
| 1398 | Error e; |
| 1399 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1400 | scoped_refptr<MockProfile> profile( |
| 1401 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1402 | AdoptProfile(manager(), profile); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1403 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1404 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1405 | EXPECT_FALSE(service); |
| 1406 | } |
| 1407 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1408 | TEST_F(ManagerTest, GetServiceVPN) { |
| 1409 | KeyValueStore args; |
| 1410 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1411 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1412 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 1413 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 1414 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1415 | scoped_refptr<MockProfile> profile( |
| 1416 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1417 | AdoptProfile(manager(), profile); |
| 1418 | ServiceRefPtr updated_service; |
| 1419 | EXPECT_CALL(*profile, UpdateService(_)) |
| 1420 | .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true))); |
| 1421 | ServiceRefPtr configured_service; |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1422 | EXPECT_CALL(*profile, LoadService(_)) |
| 1423 | .WillOnce(Return(false)); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1424 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1425 | .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true))); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1426 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1427 | EXPECT_TRUE(e.IsSuccess()); |
| 1428 | EXPECT_TRUE(service); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1429 | EXPECT_EQ(service, updated_service); |
| 1430 | EXPECT_EQ(service, configured_service); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1433 | TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) { |
| 1434 | KeyValueStore args; |
| 1435 | Error e; |
| 1436 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
| 1437 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1438 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1439 | EXPECT_EQ("Missing WiMAX network id.", e.message()); |
| 1440 | EXPECT_FALSE(service); |
| 1441 | } |
| 1442 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1443 | TEST_F(ManagerTest, GetServiceWiMax) { |
| 1444 | KeyValueStore args; |
| 1445 | Error e; |
| 1446 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1447 | args.SetString(WiMaxService::kNetworkIdProperty, "01234567"); |
| 1448 | args.SetString(flimflam::kNameProperty, "WiMAX Network"); |
| 1449 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1450 | EXPECT_TRUE(e.IsSuccess()); |
| 1451 | EXPECT_TRUE(service); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1454 | TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) { |
| 1455 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1456 | scoped_refptr<MockProfile> profile( |
| 1457 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1458 | AdoptProfile(manager(), profile); |
| 1459 | |
| 1460 | KeyValueStore args; |
| 1461 | args.SetString(flimflam::kProfileProperty, "xxx"); |
| 1462 | Error error; |
| 1463 | manager()->ConfigureService(args, &error); |
| 1464 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1465 | EXPECT_EQ("Invalid profile name xxx", error.message()); |
| 1466 | } |
| 1467 | |
| 1468 | TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) { |
| 1469 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1470 | scoped_refptr<MockProfile> profile( |
| 1471 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1472 | AdoptProfile(manager(), profile); |
| 1473 | |
| 1474 | KeyValueStore args; |
| 1475 | Error error; |
| 1476 | manager()->ConfigureService(args, &error); |
| 1477 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1478 | EXPECT_EQ("must specify service type", error.message()); |
| 1479 | } |
| 1480 | |
| 1481 | // A registered service in the ephemeral profile should be moved to the |
| 1482 | // active profile as a part of configuration if no profile was explicitly |
| 1483 | // specified. |
| 1484 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) { |
| 1485 | scoped_refptr<MockProfile> profile( |
| 1486 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1487 | |
| 1488 | AdoptProfile(manager(), profile); // This is now the active profile. |
| 1489 | |
| 1490 | const std::vector<uint8_t> ssid; |
| 1491 | scoped_refptr<MockWiFiService> service( |
| 1492 | new NiceMock<MockWiFiService>(control_interface(), |
| 1493 | dispatcher(), |
| 1494 | metrics(), |
| 1495 | manager(), |
| 1496 | mock_wifi_, |
| 1497 | ssid, |
| 1498 | "", |
| 1499 | "", |
| 1500 | false)); |
| 1501 | |
| 1502 | manager()->RegisterService(service); |
| 1503 | service->set_profile(GetEphemeralProfile(manager())); |
| 1504 | |
| 1505 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1506 | // the same device as that used above causes a refcounting loop. |
| 1507 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1508 | dispatcher(), |
| 1509 | metrics(), |
| 1510 | manager(), |
| 1511 | "wifi1", |
| 1512 | "addr5", |
| 1513 | 5)); |
| 1514 | manager()->RegisterDevice(wifi); |
| 1515 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1516 | .WillOnce(Return(service)); |
| 1517 | EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get()))) |
| 1518 | .WillOnce(Return(true)); |
| 1519 | EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get()))) |
| 1520 | .WillOnce(Return(true)); |
| 1521 | |
| 1522 | KeyValueStore args; |
| 1523 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1524 | Error error; |
| 1525 | manager()->ConfigureService(args, &error); |
| 1526 | EXPECT_TRUE(error.IsSuccess()); |
| 1527 | } |
| 1528 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1529 | // If we configure a service that was already registered and explicitly |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1530 | // specify a profile, it should be moved from the profile it was previously |
| 1531 | // in to the specified profile if one was requested. |
| 1532 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) { |
| 1533 | scoped_refptr<MockProfile> profile0( |
| 1534 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1535 | scoped_refptr<MockProfile> profile1( |
| 1536 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1537 | |
| 1538 | const string kProfileName0 = "profile0"; |
| 1539 | const string kProfileName1 = "profile1"; |
| 1540 | |
| 1541 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1542 | .WillRepeatedly(Return(kProfileName0)); |
| 1543 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1544 | .WillRepeatedly(Return(kProfileName1)); |
| 1545 | |
| 1546 | AdoptProfile(manager(), profile0); |
| 1547 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1548 | |
| 1549 | const std::vector<uint8_t> ssid; |
| 1550 | scoped_refptr<MockWiFiService> service( |
| 1551 | new NiceMock<MockWiFiService>(control_interface(), |
| 1552 | dispatcher(), |
| 1553 | metrics(), |
| 1554 | manager(), |
| 1555 | mock_wifi_, |
| 1556 | ssid, |
| 1557 | "", |
| 1558 | "", |
| 1559 | false)); |
| 1560 | |
| 1561 | manager()->RegisterService(service); |
| 1562 | service->set_profile(profile1); |
| 1563 | |
| 1564 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1565 | // the same device as that used above causes a refcounting loop. |
| 1566 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1567 | dispatcher(), |
| 1568 | metrics(), |
| 1569 | manager(), |
| 1570 | "wifi1", |
| 1571 | "addr5", |
| 1572 | 5)); |
| 1573 | manager()->RegisterDevice(wifi); |
| 1574 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1575 | .WillOnce(Return(service)); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1576 | EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get()))) |
| 1577 | .WillOnce(Return(true)); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1578 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1579 | .WillOnce(Return(true)); |
| 1580 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1581 | .WillOnce(Return(true)); |
| 1582 | EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get()))) |
| 1583 | .WillOnce(Return(true)); |
| 1584 | |
| 1585 | KeyValueStore args; |
| 1586 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1587 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1588 | Error error; |
| 1589 | manager()->ConfigureService(args, &error); |
| 1590 | EXPECT_TRUE(error.IsSuccess()); |
| 1591 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1592 | } |
| 1593 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1594 | // If we configure a service that is already a member of the specified |
| 1595 | // profile, the Manager should not call LoadService or AdoptService again |
| 1596 | // on this service. |
| 1597 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) { |
| 1598 | scoped_refptr<MockProfile> profile0( |
| 1599 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1600 | |
| 1601 | const string kProfileName0 = "profile0"; |
| 1602 | |
| 1603 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1604 | .WillRepeatedly(Return(kProfileName0)); |
| 1605 | |
| 1606 | AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile. |
| 1607 | |
| 1608 | const std::vector<uint8_t> ssid; |
| 1609 | scoped_refptr<MockWiFiService> service( |
| 1610 | new NiceMock<MockWiFiService>(control_interface(), |
| 1611 | dispatcher(), |
| 1612 | metrics(), |
| 1613 | manager(), |
| 1614 | mock_wifi_, |
| 1615 | ssid, |
| 1616 | "", |
| 1617 | "", |
| 1618 | false)); |
| 1619 | |
| 1620 | manager()->RegisterService(service); |
| 1621 | service->set_profile(profile0); |
| 1622 | |
| 1623 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1624 | // the same device as that used above causes a refcounting loop. |
| 1625 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1626 | dispatcher(), |
| 1627 | metrics(), |
| 1628 | manager(), |
| 1629 | "wifi1", |
| 1630 | "addr5", |
| 1631 | 5)); |
| 1632 | manager()->RegisterDevice(wifi); |
| 1633 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1634 | .WillOnce(Return(service)); |
| 1635 | EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get()))) |
| 1636 | .Times(0); |
| 1637 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1638 | .WillOnce(Return(true)); |
| 1639 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1640 | .Times(0); |
| 1641 | |
| 1642 | KeyValueStore args; |
| 1643 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1644 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1645 | Error error; |
| 1646 | manager()->ConfigureService(args, &error); |
| 1647 | EXPECT_TRUE(error.IsSuccess()); |
| 1648 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1649 | } |
| 1650 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1651 | // An unregistered service should remain unregistered, but its contents should |
| 1652 | // be saved to the specified profile nonetheless. |
| 1653 | TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) { |
| 1654 | scoped_refptr<MockProfile> profile0( |
| 1655 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1656 | scoped_refptr<MockProfile> profile1( |
| 1657 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 1658 | |
| 1659 | const string kProfileName0 = "profile0"; |
| 1660 | const string kProfileName1 = "profile1"; |
| 1661 | |
| 1662 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1663 | .WillRepeatedly(Return(kProfileName0)); |
| 1664 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1665 | .WillRepeatedly(Return(kProfileName1)); |
| 1666 | |
| 1667 | AdoptProfile(manager(), profile0); |
| 1668 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1669 | |
| 1670 | const std::vector<uint8_t> ssid; |
| 1671 | scoped_refptr<MockWiFiService> service( |
| 1672 | new NiceMock<MockWiFiService>(control_interface(), |
| 1673 | dispatcher(), |
| 1674 | metrics(), |
| 1675 | manager(), |
| 1676 | mock_wifi_, |
| 1677 | ssid, |
| 1678 | "", |
| 1679 | "", |
| 1680 | false)); |
| 1681 | |
| 1682 | service->set_profile(profile1); |
| 1683 | |
| 1684 | // A separate MockWiFi from mock_wifi_ is used in the Manager since using |
| 1685 | // the same device as that used above causes a refcounting loop. |
| 1686 | scoped_refptr<MockWiFi> wifi(new NiceMock<MockWiFi>(control_interface(), |
| 1687 | dispatcher(), |
| 1688 | metrics(), |
| 1689 | manager(), |
| 1690 | "wifi1", |
| 1691 | "addr5", |
| 1692 | 5)); |
| 1693 | manager()->RegisterDevice(wifi); |
| 1694 | EXPECT_CALL(*wifi, GetService(_, _)) |
| 1695 | .WillOnce(Return(service)); |
| 1696 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1697 | .WillOnce(Return(true)); |
| 1698 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 1699 | .Times(0); |
| 1700 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 1701 | .Times(0); |
| 1702 | |
| 1703 | KeyValueStore args; |
| 1704 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1705 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1706 | Error error; |
| 1707 | manager()->ConfigureService(args, &error); |
| 1708 | EXPECT_TRUE(error.IsSuccess()); |
| 1709 | } |
| 1710 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1711 | TEST_F(ManagerTest, TechnologyOrder) { |
| 1712 | Error error; |
| 1713 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 1714 | string(flimflam::kTypeWifi), &error); |
| 1715 | ASSERT_TRUE(error.IsSuccess()); |
| 1716 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 1717 | string(flimflam::kTypeEthernet) + "," + |
| 1718 | string(flimflam::kTypeWifi)); |
| 1719 | |
| 1720 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 1721 | string(flimflam::kTypeWifi), &error); |
| 1722 | ASSERT_FALSE(error.IsSuccess()); |
| 1723 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1724 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 1725 | string(flimflam::kTypeWifi), |
| 1726 | manager()->GetTechnologyOrder()); |
| 1727 | } |
| 1728 | |
| 1729 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1730 | // TODO(quiche): Some of these tests would probably fit better in |
| 1731 | // service_unittest, since the actual comparison of Services is |
| 1732 | // implemented in Service. (crosbug.com/23370) |
| 1733 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1734 | scoped_refptr<MockService> mock_service0( |
| 1735 | new NiceMock<MockService>(control_interface(), |
| 1736 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1737 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1738 | manager())); |
| 1739 | scoped_refptr<MockService> mock_service1( |
| 1740 | new NiceMock<MockService>(control_interface(), |
| 1741 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1742 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1743 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1744 | |
| 1745 | manager()->RegisterService(mock_service0); |
| 1746 | manager()->RegisterService(mock_service1); |
| 1747 | |
| 1748 | // Services should already be sorted by UniqueName |
| 1749 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1750 | |
| 1751 | // Asking explictly to sort services should not change anything |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1752 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1753 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1754 | |
| 1755 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 1756 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1757 | manager()->UpdateService(mock_service1); |
| 1758 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1759 | |
| 1760 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 1761 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1762 | manager()->UpdateService(mock_service0); |
| 1763 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1764 | |
| 1765 | // Technology |
Joshua Kroll | 053fa82 | 2012-06-05 09:50:43 -0700 | [diff] [blame] | 1766 | EXPECT_CALL(*mock_service0.get(), technology()) |
| 1767 | .WillRepeatedly(Return((Technology::kWifi))); |
| 1768 | EXPECT_CALL(*mock_service1.get(), technology()) |
| 1769 | .WillRepeatedly(Return(Technology::kEthernet)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1770 | |
| 1771 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 1772 | // Default technology ordering should favor Ethernet over WiFi. |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1773 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1774 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1775 | |
| 1776 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 1777 | string(flimflam::kTypeEthernet), &error); |
| 1778 | EXPECT_TRUE(error.IsSuccess()); |
| 1779 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1780 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1781 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1782 | mock_service0->set_priority(1); |
| 1783 | manager()->UpdateService(mock_service0); |
| 1784 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1785 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1786 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1787 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1788 | manager()->UpdateService(mock_service1); |
| 1789 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1790 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1791 | // Auto-connect. |
| 1792 | mock_service0->set_auto_connect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1793 | manager()->UpdateService(mock_service0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1794 | mock_service1->set_auto_connect(false); |
| 1795 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1796 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1797 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 1798 | // Test is-dependent-on. It doesn't make sense to have this ranking compare |
| 1799 | // to any of the others below, so we reset to the default state after |
| 1800 | // testing. |
| 1801 | EXPECT_CALL(*mock_service1.get(), |
| 1802 | IsDependentOn(ServiceRefPtr(mock_service0.get()))) |
| 1803 | .WillOnce(Return(true)) |
| 1804 | .WillRepeatedly(Return(false)); |
| 1805 | manager()->UpdateService(mock_service1); |
| 1806 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1807 | manager()->UpdateService(mock_service0); |
| 1808 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1809 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1810 | // Connectable. |
| 1811 | mock_service1->set_connectable(true); |
| 1812 | manager()->UpdateService(mock_service1); |
| 1813 | mock_service0->set_connectable(false); |
| 1814 | manager()->UpdateService(mock_service0); |
| 1815 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1816 | |
| 1817 | // IsFailed. |
| 1818 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1819 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 1820 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 1821 | .WillRepeatedly(Return(false)); |
| 1822 | manager()->UpdateService(mock_service0); |
| 1823 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1824 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1825 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 1826 | .WillRepeatedly(Return(true)); |
| 1827 | manager()->UpdateService(mock_service1); |
| 1828 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1829 | |
| 1830 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1831 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1832 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 1833 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1834 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1835 | manager()->UpdateService(mock_service1); |
| 1836 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1837 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1838 | // Connected. |
| 1839 | EXPECT_CALL(*mock_service0.get(), state()) |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 1840 | .WillRepeatedly(Return(Service::kStatePortal)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1841 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 1842 | .WillRepeatedly(Return(true)); |
| 1843 | manager()->UpdateService(mock_service0); |
| 1844 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1845 | |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 1846 | // Portal. |
| 1847 | EXPECT_CALL(*mock_service1.get(), state()) |
| 1848 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1849 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 1850 | .WillRepeatedly(Return(true)); |
| 1851 | manager()->UpdateService(mock_service1); |
| 1852 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1853 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1854 | manager()->DeregisterService(mock_service0); |
| 1855 | manager()->DeregisterService(mock_service1); |
| 1856 | } |
| 1857 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1858 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1859 | MockMetrics mock_metrics; |
| 1860 | manager()->set_metrics(&mock_metrics); |
| 1861 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1862 | scoped_refptr<MockService> mock_service0( |
| 1863 | new NiceMock<MockService>(control_interface(), |
| 1864 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1865 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1866 | manager())); |
| 1867 | scoped_refptr<MockService> mock_service1( |
| 1868 | new NiceMock<MockService>(control_interface(), |
| 1869 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1870 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1871 | manager())); |
| 1872 | |
| 1873 | scoped_refptr<MockConnection> mock_connection0( |
| 1874 | new NiceMock<MockConnection>(device_info_.get())); |
| 1875 | scoped_refptr<MockConnection> mock_connection1( |
| 1876 | new NiceMock<MockConnection>(device_info_.get())); |
| 1877 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1878 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1879 | manager()->RegisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1880 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1881 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1882 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1883 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1884 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1885 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1886 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1887 | |
| 1888 | mock_service1->set_priority(1); |
| 1889 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1890 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1891 | |
| 1892 | mock_service1->set_priority(0); |
| 1893 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1894 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1895 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1896 | mock_service0->set_mock_connection(mock_connection0); |
| 1897 | mock_service1->set_mock_connection(mock_connection1); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1898 | |
| 1899 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1900 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1901 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1902 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1903 | ServiceWatcher service_watcher; |
| 1904 | int tag = |
| 1905 | manager()->RegisterDefaultServiceCallback( |
| 1906 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1907 | service_watcher.AsWeakPtr())); |
| 1908 | EXPECT_EQ(1, tag); |
| 1909 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1910 | mock_service1->set_priority(1); |
| 1911 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 1912 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1913 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1914 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1915 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1916 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1917 | manager()->DeregisterDefaultServiceCallback(tag); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1918 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1919 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1920 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1921 | mock_service1->set_mock_connection(NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1922 | manager()->DeregisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1923 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1924 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1925 | mock_service0->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1926 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1927 | manager()->DeregisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1928 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1929 | |
| 1930 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1931 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1934 | TEST_F(ManagerTest, NotifyDefaultServiceChanged) { |
| 1935 | EXPECT_EQ(0, manager()->default_service_callback_tag_); |
| 1936 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 1937 | |
| 1938 | MockMetrics mock_metrics; |
| 1939 | manager()->set_metrics(&mock_metrics); |
| 1940 | |
| 1941 | scoped_refptr<MockService> mock_service( |
| 1942 | new NiceMock<MockService>( |
| 1943 | control_interface(), dispatcher(), metrics(), manager())); |
| 1944 | ServiceRefPtr service = mock_service; |
| 1945 | ServiceRefPtr null_service; |
| 1946 | |
| 1947 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1948 | manager()->NotifyDefaultServiceChanged(null_service); |
| 1949 | |
| 1950 | ServiceWatcher service_watcher1; |
| 1951 | ServiceWatcher service_watcher2; |
| 1952 | int tag1 = |
| 1953 | manager()->RegisterDefaultServiceCallback( |
| 1954 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1955 | service_watcher1.AsWeakPtr())); |
| 1956 | EXPECT_EQ(1, tag1); |
| 1957 | int tag2 = |
| 1958 | manager()->RegisterDefaultServiceCallback( |
| 1959 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 1960 | service_watcher2.AsWeakPtr())); |
| 1961 | EXPECT_EQ(2, tag2); |
| 1962 | |
| 1963 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service)); |
| 1964 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service)); |
| 1965 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1966 | manager()->NotifyDefaultServiceChanged(null_service); |
| 1967 | |
| 1968 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service)); |
| 1969 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 1970 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1971 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1972 | |
| 1973 | manager()->DeregisterDefaultServiceCallback(tag1); |
| 1974 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0); |
| 1975 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 1976 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1977 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1978 | EXPECT_EQ(1, manager()->default_service_callbacks_.size()); |
| 1979 | |
| 1980 | manager()->DeregisterDefaultServiceCallback(tag2); |
| 1981 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0); |
| 1982 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 1983 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 1984 | |
| 1985 | EXPECT_EQ(2, manager()->default_service_callback_tag_); |
| 1986 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 1987 | } |
| 1988 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1989 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 1990 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 1991 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1992 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1993 | manager(), |
| 1994 | "null4", |
| 1995 | "addr4", |
| 1996 | 0)); |
| 1997 | manager()->RegisterDevice(mock_devices_[0]); |
| 1998 | manager()->RegisterDevice(mock_devices_[1]); |
| 1999 | manager()->RegisterDevice(mock_devices_[2]); |
| 2000 | manager()->RegisterDevice(mock_devices_[3]); |
| 2001 | |
| 2002 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 2003 | .WillByDefault(Return(Technology::kEthernet)); |
| 2004 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 2005 | .WillByDefault(Return(Technology::kWifi)); |
| 2006 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 2007 | .WillByDefault(Return(Technology::kCellular)); |
| 2008 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 2009 | .WillByDefault(Return(Technology::kWifi)); |
| 2010 | |
| 2011 | set<string> expected_technologies; |
| 2012 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2013 | Technology::kEthernet)); |
| 2014 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2015 | Technology::kWifi)); |
| 2016 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2017 | Technology::kCellular)); |
| 2018 | Error error; |
| 2019 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 2020 | |
| 2021 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 2022 | ContainerEq(expected_technologies)); |
| 2023 | } |
| 2024 | |
| 2025 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 2026 | scoped_refptr<MockService> connected_service1( |
| 2027 | new NiceMock<MockService>(control_interface(), |
| 2028 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2029 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2030 | manager())); |
| 2031 | scoped_refptr<MockService> connected_service2( |
| 2032 | new NiceMock<MockService>(control_interface(), |
| 2033 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2034 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2035 | manager())); |
| 2036 | scoped_refptr<MockService> disconnected_service1( |
| 2037 | new NiceMock<MockService>(control_interface(), |
| 2038 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2039 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2040 | manager())); |
| 2041 | scoped_refptr<MockService> disconnected_service2( |
| 2042 | new NiceMock<MockService>(control_interface(), |
| 2043 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2044 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2045 | manager())); |
| 2046 | |
| 2047 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 2048 | .WillByDefault(Return(true)); |
| 2049 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 2050 | .WillByDefault(Return(true)); |
| 2051 | |
| 2052 | manager()->RegisterService(connected_service1); |
| 2053 | manager()->RegisterService(connected_service2); |
| 2054 | manager()->RegisterService(disconnected_service1); |
| 2055 | manager()->RegisterService(disconnected_service2); |
| 2056 | |
| 2057 | manager()->RegisterDevice(mock_devices_[0]); |
| 2058 | manager()->RegisterDevice(mock_devices_[1]); |
| 2059 | manager()->RegisterDevice(mock_devices_[2]); |
| 2060 | manager()->RegisterDevice(mock_devices_[3]); |
| 2061 | |
| 2062 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 2063 | .WillByDefault(Return(Technology::kEthernet)); |
| 2064 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 2065 | .WillByDefault(Return(Technology::kWifi)); |
| 2066 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 2067 | .WillByDefault(Return(Technology::kCellular)); |
| 2068 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 2069 | .WillByDefault(Return(Technology::kWifi)); |
| 2070 | |
| 2071 | mock_devices_[0]->SelectService(connected_service1); |
| 2072 | mock_devices_[1]->SelectService(disconnected_service1); |
| 2073 | mock_devices_[2]->SelectService(disconnected_service2); |
| 2074 | mock_devices_[3]->SelectService(connected_service2); |
| 2075 | |
| 2076 | set<string> expected_technologies; |
| 2077 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2078 | Technology::kEthernet)); |
| 2079 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2080 | Technology::kWifi)); |
| 2081 | Error error; |
| 2082 | |
| 2083 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 2084 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 2085 | ContainerEq(expected_technologies)); |
| 2086 | } |
| 2087 | |
| 2088 | TEST_F(ManagerTest, DefaultTechnology) { |
| 2089 | scoped_refptr<MockService> connected_service( |
| 2090 | new NiceMock<MockService>(control_interface(), |
| 2091 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2092 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2093 | manager())); |
| 2094 | scoped_refptr<MockService> disconnected_service( |
| 2095 | new NiceMock<MockService>(control_interface(), |
| 2096 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2097 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2098 | manager())); |
| 2099 | |
| 2100 | // Connected. WiFi. |
| 2101 | ON_CALL(*connected_service.get(), IsConnected()) |
| 2102 | .WillByDefault(Return(true)); |
| 2103 | ON_CALL(*connected_service.get(), state()) |
| 2104 | .WillByDefault(Return(Service::kStateConnected)); |
| 2105 | ON_CALL(*connected_service.get(), technology()) |
| 2106 | .WillByDefault(Return(Technology::kWifi)); |
| 2107 | |
| 2108 | // Disconnected. Ethernet. |
| 2109 | ON_CALL(*disconnected_service.get(), technology()) |
| 2110 | .WillByDefault(Return(Technology::kEthernet)); |
| 2111 | |
| 2112 | manager()->RegisterService(disconnected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2113 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2114 | Error error; |
| 2115 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 2116 | |
| 2117 | |
| 2118 | manager()->RegisterService(connected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2119 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2120 | // Connected service should be brought to the front now. |
| 2121 | string expected_technology = |
| 2122 | Technology::NameFromIdentifier(Technology::kWifi); |
| 2123 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 2124 | } |
| 2125 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2126 | TEST_F(ManagerTest, Stop) { |
| 2127 | scoped_refptr<MockProfile> profile( |
| 2128 | new NiceMock<MockProfile>(control_interface(), manager(), "")); |
| 2129 | AdoptProfile(manager(), profile); |
| 2130 | scoped_refptr<MockService> service( |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2131 | new NiceMock<MockService>(control_interface(), |
| 2132 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2133 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2134 | manager())); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2135 | manager()->RegisterService(service); |
| 2136 | manager()->RegisterDevice(mock_devices_[0]); |
| 2137 | EXPECT_CALL(*profile.get(), |
| 2138 | UpdateDevice(DeviceRefPtr(mock_devices_[0].get()))) |
| 2139 | .WillOnce(Return(true)); |
| 2140 | EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true)); |
| 2141 | EXPECT_CALL(*service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2142 | manager()->Stop(); |
| 2143 | } |
| 2144 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2145 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 2146 | scoped_refptr<MockService> mock_service( |
| 2147 | new NiceMock<MockService>(control_interface(), |
| 2148 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2149 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2150 | manager())); |
| 2151 | manager()->RegisterService(mock_service); |
| 2152 | EXPECT_FALSE(mock_service->favorite()); |
| 2153 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2154 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2155 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 2156 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2157 | manager()->UpdateService(mock_service); |
| 2158 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2159 | // to mock out MakeFavorite. And mocking that out would break the |
| 2160 | // SortServices test. (crosbug.com/23370) |
| 2161 | EXPECT_TRUE(mock_service->favorite()); |
| 2162 | EXPECT_TRUE(mock_service->auto_connect()); |
| 2163 | } |
| 2164 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2165 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 2166 | // This tests the case where the user connects to a service that is |
| 2167 | // currently associated with a profile. We want to make sure that the |
| 2168 | // favorite flag is set and that the flag is saved to the current |
| 2169 | // profile. |
| 2170 | scoped_refptr<MockService> mock_service( |
| 2171 | new NiceMock<MockService>(control_interface(), |
| 2172 | dispatcher(), |
| 2173 | metrics(), |
| 2174 | manager())); |
| 2175 | manager()->RegisterService(mock_service); |
| 2176 | EXPECT_FALSE(mock_service->favorite()); |
| 2177 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2178 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2179 | scoped_refptr<MockProfile> profile( |
| 2180 | new MockProfile(control_interface(), manager(), "")); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2181 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2182 | mock_service->set_profile(profile); |
| 2183 | EXPECT_CALL(*mock_service, IsConnected()) |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2184 | .WillRepeatedly(Return(true)); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2185 | EXPECT_CALL(*profile, |
| 2186 | UpdateService(static_cast<ServiceRefPtr>(mock_service))); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2187 | manager()->UpdateService(mock_service); |
| 2188 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2189 | // to mock out MakeFavorite. And mocking that out would break the |
| 2190 | // SortServices test. (crosbug.com/23370) |
| 2191 | EXPECT_TRUE(mock_service->favorite()); |
| 2192 | EXPECT_TRUE(mock_service->auto_connect()); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2193 | // This releases the ref on the mock profile. |
| 2194 | mock_service->set_profile(NULL); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2197 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 2198 | scoped_refptr<MockProfile> profile( |
| 2199 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 2200 | AdoptProfile(manager(), profile); |
| 2201 | scoped_refptr<MockService> service( |
| 2202 | new NiceMock<MockService>(control_interface(), |
| 2203 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2204 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2205 | manager())); |
| 2206 | |
| 2207 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 2208 | ServiceRefPtr expect_service(service.get()); |
| 2209 | |
| 2210 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 2211 | .WillOnce(Return(false)); |
| 2212 | manager()->RegisterService(service); |
| 2213 | |
| 2214 | EXPECT_CALL(*service.get(), state()) |
| 2215 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2216 | EXPECT_CALL(*service.get(), IsConnected()) |
| 2217 | .WillRepeatedly(Return(true)); |
| 2218 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 2219 | .WillOnce(Return(true)); |
| 2220 | manager()->UpdateService(service); |
| 2221 | } |
| 2222 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 2223 | TEST_F(ManagerTest, UpdateDevice) { |
| 2224 | MockProfile *profile0 = new MockProfile(control_interface(), manager(), ""); |
| 2225 | MockProfile *profile1 = new MockProfile(control_interface(), manager(), ""); |
| 2226 | MockProfile *profile2 = new MockProfile(control_interface(), manager(), ""); |
| 2227 | AdoptProfile(manager(), profile0); // Passes ownership. |
| 2228 | AdoptProfile(manager(), profile1); // Passes ownership. |
| 2229 | AdoptProfile(manager(), profile2); // Passes ownership. |
| 2230 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 2231 | EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0); |
| 2232 | EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true)); |
| 2233 | EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false)); |
| 2234 | manager()->UpdateDevice(mock_devices_[0]); |
| 2235 | } |
| 2236 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2237 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 2238 | vector<string> profile_paths; |
| 2239 | for (size_t i = 0; i < 10; i++) { |
| 2240 | scoped_refptr<MockProfile> profile( |
| 2241 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 2242 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2243 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 2244 | .WillOnce(Return(profile_paths.back())); |
| 2245 | AdoptProfile(manager(), profile); |
| 2246 | } |
| 2247 | |
| 2248 | Error error; |
| 2249 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 2250 | EXPECT_TRUE(error.IsSuccess()); |
| 2251 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 2252 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 2253 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 2254 | } |
| 2255 | } |
| 2256 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2257 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 2258 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2259 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 2260 | manager()->RegisterService(service); |
| 2261 | dispatcher()->DispatchPendingEvents(); |
| 2262 | } |
| 2263 | |
| 2264 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 2265 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 2266 | service1->set_priority(1); |
| 2267 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 2268 | service2->set_priority(2); |
| 2269 | manager()->RegisterService(service1); |
| 2270 | manager()->RegisterService(service2); |
| 2271 | dispatcher()->DispatchPendingEvents(); |
| 2272 | |
| 2273 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2274 | EXPECT_CALL(*service2.get(), state()) |
| 2275 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 2276 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 2277 | .WillRepeatedly(Return(true)); |
| 2278 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 2279 | .WillRepeatedly(Return(false)); |
| 2280 | manager()->UpdateService(service2); |
| 2281 | dispatcher()->DispatchPendingEvents(); |
| 2282 | } |
| 2283 | |
| 2284 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 2285 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 2286 | service1->set_priority(1); |
| 2287 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 2288 | service2->set_priority(2); |
| 2289 | manager()->RegisterService(service1); |
| 2290 | manager()->RegisterService(service2); |
| 2291 | dispatcher()->DispatchPendingEvents(); |
| 2292 | |
| 2293 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2294 | manager()->DeregisterService(service2); |
| 2295 | dispatcher()->DispatchPendingEvents(); |
| 2296 | } |
| 2297 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2298 | TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) { |
| 2299 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2300 | SetPowerState(PowerManagerProxyDelegate::kSuspending); |
| 2301 | SetPowerManager(); |
| 2302 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2303 | manager()->RegisterService(service); |
| 2304 | dispatcher()->DispatchPendingEvents(); |
| 2305 | } |
| 2306 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2307 | TEST_F(ManagerTest, AutoConnectOnPowerStateMem) { |
| 2308 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2309 | SetPowerState(PowerManagerProxyDelegate::kMem); |
| 2310 | SetPowerManager(); |
| 2311 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2312 | manager()->RegisterService(service); |
| 2313 | dispatcher()->DispatchPendingEvents(); |
| 2314 | } |
| 2315 | |
| 2316 | TEST_F(ManagerTest, AutoConnectOnPowerStateOn) { |
| 2317 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2318 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2319 | SetPowerManager(); |
| 2320 | EXPECT_CALL(*service, AutoConnect()); |
| 2321 | manager()->RegisterService(service); |
| 2322 | dispatcher()->DispatchPendingEvents(); |
| 2323 | } |
| 2324 | |
| 2325 | TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) { |
| 2326 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2327 | SetPowerState(PowerManagerProxyDelegate::kUnknown); |
| 2328 | SetPowerManager(); |
| 2329 | EXPECT_CALL(*service, AutoConnect()); |
| 2330 | manager()->RegisterService(service); |
| 2331 | dispatcher()->DispatchPendingEvents(); |
| 2332 | } |
| 2333 | |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 2334 | TEST_F(ManagerTest, AutoConnectWhileNotRunning) { |
| 2335 | SetRunning(false); |
| 2336 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2337 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2338 | manager()->RegisterService(service); |
| 2339 | dispatcher()->DispatchPendingEvents(); |
| 2340 | } |
| 2341 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2342 | TEST_F(ManagerTest, OnPowerStateChanged) { |
| 2343 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2344 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2345 | SetPowerManager(); |
| 2346 | EXPECT_CALL(*service, AutoConnect()); |
| 2347 | manager()->RegisterService(service); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2348 | manager()->RegisterDevice(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2349 | dispatcher()->DispatchPendingEvents(); |
| 2350 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2351 | EXPECT_CALL(*mock_devices_[0], OnAfterResume()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2352 | OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 2353 | EXPECT_CALL(*service, AutoConnect()); |
| 2354 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2355 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2356 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2357 | EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2358 | OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 2359 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2360 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2361 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2364 | TEST_F(ManagerTest, AddTerminationAction) { |
| 2365 | EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _)); |
| 2366 | EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_)); |
| 2367 | SetPowerManager(); |
| 2368 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2369 | manager()->AddTerminationAction("action1", base::Closure()); |
| 2370 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2371 | manager()->AddTerminationAction("action2", base::Closure()); |
| 2372 | } |
| 2373 | |
| 2374 | TEST_F(ManagerTest, RemoveTerminationAction) { |
| 2375 | static const char kKey1[] = "action1"; |
| 2376 | static const char kKey2[] = "action2"; |
| 2377 | |
| 2378 | MockPowerManager &power_manager = *power_manager_; |
| 2379 | SetPowerManager(); |
| 2380 | |
| 2381 | // Removing an action when the hook table is empty should not result in any |
| 2382 | // calls to the power manager. |
| 2383 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0); |
| 2384 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2385 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2386 | manager()->RemoveTerminationAction("unknown"); |
| 2387 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2388 | |
| 2389 | manager()->AddTerminationAction(kKey1, base::Closure()); |
| 2390 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2391 | manager()->AddTerminationAction(kKey2, base::Closure()); |
| 2392 | |
| 2393 | // Removing an action that ends up with a non-empty hook table should not |
| 2394 | // result in any calls to the power manager. |
| 2395 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()).Times(0); |
| 2396 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2397 | manager()->RemoveTerminationAction(kKey1); |
| 2398 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2399 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2400 | |
| 2401 | // Removing the last action should trigger unregistering from the power |
| 2402 | // manager. |
| 2403 | EXPECT_CALL(power_manager, UnregisterSuspendDelay()); |
| 2404 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)); |
| 2405 | manager()->RemoveTerminationAction(kKey2); |
| 2406 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2407 | } |
| 2408 | |
| 2409 | TEST_F(ManagerTest, RunTerminationActions) { |
| 2410 | TerminationActionTest test_action; |
| 2411 | const string kActionName = "action"; |
| 2412 | |
| 2413 | EXPECT_CALL(test_action, Done(_)); |
| 2414 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2415 | test_action.AsWeakPtr())); |
| 2416 | |
| 2417 | manager()->AddTerminationAction(TerminationActionTest::kActionName, |
| 2418 | Bind(&TerminationActionTest::Action, |
| 2419 | test_action.AsWeakPtr())); |
| 2420 | test_action.set_manager(manager()); |
| 2421 | EXPECT_CALL(test_action, Done(_)); |
| 2422 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2423 | test_action.AsWeakPtr())); |
| 2424 | } |
| 2425 | |
| 2426 | TEST_F(ManagerTest, OnSuspendDelay) { |
| 2427 | const uint32 kSeqNumber = 123; |
| 2428 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2429 | EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber)); |
| 2430 | SetPowerManager(); |
| 2431 | OnSuspendDelay(kSeqNumber); |
| 2432 | } |
| 2433 | |
| 2434 | TEST_F(ManagerTest, OnSuspendActionsComplete) { |
| 2435 | const uint32 kSeqNumber = 54321; |
| 2436 | Error error; |
| 2437 | EXPECT_CALL(*power_manager_, SuspendReady(kSeqNumber)); |
| 2438 | SetPowerManager(); |
| 2439 | OnSuspendActionsComplete(kSeqNumber, error); |
| 2440 | } |
| 2441 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 2442 | TEST_F(ManagerTest, RecheckPortal) { |
| 2443 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 2444 | .WillOnce(Return(false)); |
| 2445 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 2446 | .WillOnce(Return(true)); |
| 2447 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 2448 | .Times(0); |
| 2449 | |
| 2450 | manager()->RegisterDevice(mock_devices_[0]); |
| 2451 | manager()->RegisterDevice(mock_devices_[1]); |
| 2452 | manager()->RegisterDevice(mock_devices_[2]); |
| 2453 | |
| 2454 | manager()->RecheckPortal(NULL); |
| 2455 | } |
| 2456 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 2457 | TEST_F(ManagerTest, RecheckPortalOnService) { |
| 2458 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 2459 | dispatcher(), |
| 2460 | metrics(), |
| 2461 | manager()); |
| 2462 | EXPECT_CALL(*mock_devices_[0].get(), |
| 2463 | IsConnectedToService(IsRefPtrTo(service))) |
| 2464 | .WillOnce(Return(false)); |
| 2465 | EXPECT_CALL(*mock_devices_[1].get(), |
| 2466 | IsConnectedToService(IsRefPtrTo(service))) |
| 2467 | .WillOnce(Return(true)); |
| 2468 | EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection()) |
| 2469 | .WillOnce(Return(true)); |
| 2470 | EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_)) |
| 2471 | .Times(0); |
| 2472 | |
| 2473 | manager()->RegisterDevice(mock_devices_[0]); |
| 2474 | manager()->RegisterDevice(mock_devices_[1]); |
| 2475 | manager()->RegisterDevice(mock_devices_[2]); |
| 2476 | |
| 2477 | manager()->RecheckPortalOnService(service); |
| 2478 | } |
| 2479 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2480 | TEST_F(ManagerTest, GetDefaultService) { |
| 2481 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2482 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2483 | |
| 2484 | scoped_refptr<MockService> mock_service( |
| 2485 | new NiceMock<MockService>(control_interface(), |
| 2486 | dispatcher(), |
| 2487 | metrics(), |
| 2488 | manager())); |
| 2489 | |
| 2490 | manager()->RegisterService(mock_service); |
| 2491 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2492 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2493 | |
| 2494 | scoped_refptr<MockConnection> mock_connection( |
| 2495 | new NiceMock<MockConnection>(device_info_.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2496 | mock_service->set_mock_connection(mock_connection); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2497 | EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2498 | EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2499 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2500 | mock_service->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2501 | manager()->DeregisterService(mock_service); |
| 2502 | } |
| 2503 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2504 | TEST_F(ManagerTest, GetServiceWithGUID) { |
| 2505 | scoped_refptr<MockService> mock_service0( |
| 2506 | new NiceMock<MockService>(control_interface(), |
| 2507 | dispatcher(), |
| 2508 | metrics(), |
| 2509 | manager())); |
| 2510 | |
| 2511 | scoped_refptr<MockService> mock_service1( |
| 2512 | new NiceMock<MockService>(control_interface(), |
| 2513 | dispatcher(), |
| 2514 | metrics(), |
| 2515 | manager())); |
| 2516 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 2517 | EXPECT_CALL(*mock_service0.get(), Configure(_, _)) |
| 2518 | .Times(0); |
| 2519 | EXPECT_CALL(*mock_service1.get(), Configure(_, _)) |
| 2520 | .Times(0); |
| 2521 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2522 | manager()->RegisterService(mock_service0); |
| 2523 | manager()->RegisterService(mock_service1); |
| 2524 | |
| 2525 | const string kGUID0 = "GUID0"; |
| 2526 | const string kGUID1 = "GUID1"; |
| 2527 | |
| 2528 | { |
| 2529 | Error error; |
| 2530 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 2531 | EXPECT_FALSE(error.IsSuccess()); |
| 2532 | EXPECT_FALSE(service); |
| 2533 | } |
| 2534 | |
| 2535 | KeyValueStore args; |
| 2536 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 2537 | |
| 2538 | { |
| 2539 | Error error; |
| 2540 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 2541 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 2542 | EXPECT_FALSE(service); |
| 2543 | } |
| 2544 | |
| 2545 | mock_service0->set_guid(kGUID0); |
| 2546 | mock_service1->set_guid(kGUID1); |
| 2547 | |
| 2548 | { |
| 2549 | Error error; |
| 2550 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 2551 | EXPECT_TRUE(error.IsSuccess()); |
| 2552 | EXPECT_EQ(mock_service0.get(), service.get()); |
| 2553 | } |
| 2554 | |
| 2555 | { |
| 2556 | Error error; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 2557 | EXPECT_CALL(*mock_service1.get(), Configure(_, &error)) |
| 2558 | .Times(1); |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2559 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 2560 | EXPECT_TRUE(error.IsSuccess()); |
| 2561 | EXPECT_EQ(mock_service1.get(), service.get()); |
| 2562 | } |
| 2563 | |
| 2564 | manager()->DeregisterService(mock_service0); |
| 2565 | manager()->DeregisterService(mock_service1); |
| 2566 | } |
| 2567 | |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 2568 | |
| 2569 | TEST_F(ManagerTest, CalculateStateOffline) { |
| 2570 | MockMetrics mock_metrics; |
| 2571 | manager()->set_metrics(&mock_metrics); |
| 2572 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 2573 | .Times(AnyNumber()); |
| 2574 | scoped_refptr<MockService> mock_service0( |
| 2575 | new NiceMock<MockService>(control_interface(), |
| 2576 | dispatcher(), |
| 2577 | metrics(), |
| 2578 | manager())); |
| 2579 | |
| 2580 | scoped_refptr<MockService> mock_service1( |
| 2581 | new NiceMock<MockService>(control_interface(), |
| 2582 | dispatcher(), |
| 2583 | metrics(), |
| 2584 | manager())); |
| 2585 | |
| 2586 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 2587 | .WillRepeatedly(Return(false)); |
| 2588 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 2589 | .WillRepeatedly(Return(false)); |
| 2590 | |
| 2591 | manager()->RegisterService(mock_service0); |
| 2592 | manager()->RegisterService(mock_service1); |
| 2593 | |
| 2594 | EXPECT_EQ("offline", manager()->CalculateState(NULL)); |
| 2595 | |
| 2596 | manager()->DeregisterService(mock_service0); |
| 2597 | manager()->DeregisterService(mock_service1); |
| 2598 | } |
| 2599 | |
| 2600 | TEST_F(ManagerTest, CalculateStateOnline) { |
| 2601 | MockMetrics mock_metrics; |
| 2602 | manager()->set_metrics(&mock_metrics); |
| 2603 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 2604 | .Times(AnyNumber()); |
| 2605 | scoped_refptr<MockService> mock_service0( |
| 2606 | new NiceMock<MockService>(control_interface(), |
| 2607 | dispatcher(), |
| 2608 | metrics(), |
| 2609 | manager())); |
| 2610 | |
| 2611 | scoped_refptr<MockService> mock_service1( |
| 2612 | new NiceMock<MockService>(control_interface(), |
| 2613 | dispatcher(), |
| 2614 | metrics(), |
| 2615 | manager())); |
| 2616 | |
| 2617 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 2618 | .WillRepeatedly(Return(false)); |
| 2619 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 2620 | .WillRepeatedly(Return(true)); |
| 2621 | EXPECT_CALL(*mock_service0.get(), state()) |
| 2622 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 2623 | EXPECT_CALL(*mock_service1.get(), state()) |
| 2624 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2625 | |
| 2626 | manager()->RegisterService(mock_service0); |
| 2627 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2628 | CompleteServiceSort(); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 2629 | |
| 2630 | EXPECT_EQ("online", manager()->CalculateState(NULL)); |
| 2631 | |
| 2632 | manager()->DeregisterService(mock_service0); |
| 2633 | manager()->DeregisterService(mock_service1); |
| 2634 | } |
| 2635 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 2636 | TEST_F(ManagerTest, StartupPortalList) { |
| 2637 | // Simulate loading value from the default profile. |
| 2638 | const string kProfileValue("wifi,vpn"); |
| 2639 | manager()->props_.check_portal_list = kProfileValue; |
| 2640 | |
| 2641 | EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL)); |
| 2642 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 2643 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2644 | |
| 2645 | const string kStartupValue("cellular,ethernet"); |
| 2646 | manager()->SetStartupPortalList(kStartupValue); |
| 2647 | // Ensure profile value is not overwritten, so when we save the default |
| 2648 | // profile, the correct value will still be written. |
| 2649 | EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list); |
| 2650 | |
| 2651 | // However we should read back a different list. |
| 2652 | EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL)); |
| 2653 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 2654 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2655 | |
| 2656 | const string kRuntimeValue("ppp"); |
| 2657 | // Setting a runtime value over the control API should overwrite both |
| 2658 | // the profile value and what we read back. |
| 2659 | Error error; |
| 2660 | manager()->mutable_store()->SetStringProperty( |
| 2661 | flimflam::kCheckPortalListProperty, |
| 2662 | kRuntimeValue, |
| 2663 | &error); |
| 2664 | ASSERT_TRUE(error.IsSuccess()); |
| 2665 | EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL)); |
| 2666 | EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list); |
| 2667 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 2668 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP)); |
| 2669 | } |
| 2670 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 2671 | TEST_F(ManagerTest, LinkMonitorEnabled) { |
| 2672 | const string kEnabledTechnologies("wifi,vpn"); |
| 2673 | manager()->props_.link_monitor_technologies = kEnabledTechnologies; |
| 2674 | EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi)); |
| 2675 | EXPECT_FALSE( |
| 2676 | manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular)); |
| 2677 | } |
| 2678 | |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2679 | TEST_F(ManagerTest, EnableTechnology) { |
| 2680 | Error error(Error::kOperationInitiated); |
| 2681 | ResultCallback callback; |
| 2682 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2683 | EXPECT_TRUE(error.IsSuccess()); |
| 2684 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 2685 | ON_CALL(*mock_devices_[0], technology()) |
| 2686 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2687 | |
| 2688 | manager()->RegisterDevice(mock_devices_[0]); |
| 2689 | |
| 2690 | // Device is enabled, so expect operation is successful. |
| 2691 | mock_devices_[0]->enabled_ = true; |
| 2692 | error.Populate(Error::kOperationInitiated); |
| 2693 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2694 | EXPECT_TRUE(error.IsSuccess()); |
| 2695 | |
| 2696 | // Device is disabled, so expect operation in progress. |
| 2697 | mock_devices_[0]->enabled_ = false; |
| 2698 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _)); |
| 2699 | error.Populate(Error::kOperationInitiated); |
| 2700 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2701 | EXPECT_TRUE(error.IsOngoing()); |
| 2702 | } |
| 2703 | |
| 2704 | TEST_F(ManagerTest, DisableTechnology) { |
| 2705 | Error error(Error::kOperationInitiated); |
| 2706 | ResultCallback callback; |
| 2707 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2708 | EXPECT_TRUE(error.IsSuccess()); |
| 2709 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 2710 | ON_CALL(*mock_devices_[0], technology()) |
| 2711 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 2712 | |
| 2713 | manager()->RegisterDevice(mock_devices_[0]); |
| 2714 | |
| 2715 | // Device is disabled, so expect operation is successful. |
| 2716 | error.Populate(Error::kOperationInitiated); |
| 2717 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2718 | EXPECT_TRUE(error.IsSuccess()); |
| 2719 | |
| 2720 | // Device is enabled, so expect operation in progress. |
| 2721 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _)); |
| 2722 | mock_devices_[0]->enabled_ = true; |
| 2723 | error.Populate(Error::kOperationInitiated); |
| 2724 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 2725 | EXPECT_TRUE(error.IsOngoing()); |
| 2726 | } |
| 2727 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 2728 | TEST_F(ManagerTest, IgnoredSearchList) { |
| 2729 | scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>()); |
| 2730 | SetResolver(resolver.get()); |
| 2731 | vector<string> ignored_paths; |
| 2732 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2733 | SetIgnoredDNSSearchPaths(""); |
| 2734 | EXPECT_EQ("", GetIgnoredDNSSearchPaths()); |
| 2735 | |
| 2736 | const string kIgnored0 = "chromium.org"; |
| 2737 | ignored_paths.push_back(kIgnored0); |
| 2738 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2739 | SetIgnoredDNSSearchPaths(kIgnored0); |
| 2740 | EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths()); |
| 2741 | |
| 2742 | const string kIgnored1 = "google.com"; |
| 2743 | const string kIgnoredSum = kIgnored0 + "," + kIgnored1; |
| 2744 | ignored_paths.push_back(kIgnored1); |
| 2745 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 2746 | SetIgnoredDNSSearchPaths(kIgnoredSum); |
| 2747 | EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths()); |
| 2748 | |
| 2749 | SetResolver(Resolver::GetInstance()); |
| 2750 | } |
| 2751 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 2752 | TEST_F(ManagerTest, ServiceStateChangeEmitsServices) { |
| 2753 | // Test to make sure that every service state-change causes the |
| 2754 | // Manager to emit a new service list. |
| 2755 | scoped_refptr<MockService> mock_service( |
| 2756 | new NiceMock<MockService>(control_interface(), |
| 2757 | dispatcher(), |
| 2758 | metrics(), |
| 2759 | manager())); |
| 2760 | EXPECT_CALL(*mock_service, state()) |
| 2761 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 2762 | |
| 2763 | manager()->RegisterService(mock_service); |
| 2764 | EXPECT_CALL( |
| 2765 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 2766 | flimflam::kServicesProperty, _)).Times(1); |
| 2767 | EXPECT_CALL( |
| 2768 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 2769 | flimflam::kServiceWatchListProperty, _)).Times(1); |
| 2770 | CompleteServiceSort(); |
| 2771 | |
| 2772 | Mock::VerifyAndClearExpectations(manager_adaptor_); |
| 2773 | EXPECT_CALL( |
| 2774 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 2775 | flimflam::kServicesProperty, _)).Times(1); |
| 2776 | EXPECT_CALL( |
| 2777 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 2778 | flimflam::kServiceWatchListProperty, _)).Times(1); |
| 2779 | manager()->UpdateService(mock_service.get()); |
| 2780 | CompleteServiceSort(); |
| 2781 | |
| 2782 | manager()->DeregisterService(mock_service); |
| 2783 | } |
| 2784 | |
| 2785 | TEST_F(ManagerTest, EnumerateServices) { |
| 2786 | scoped_refptr<MockService> mock_service( |
| 2787 | new NiceMock<MockService>(control_interface(), |
| 2788 | dispatcher(), |
| 2789 | metrics(), |
| 2790 | manager())); |
| 2791 | manager()->RegisterService(mock_service); |
| 2792 | |
| 2793 | EXPECT_CALL(*mock_service, state()) |
| 2794 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2795 | EXPECT_CALL(*mock_service, IsVisible()) |
| 2796 | .WillRepeatedly(Return(false)); |
| 2797 | EXPECT_TRUE(EnumerateAvailableServices().empty()); |
| 2798 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 2799 | |
| 2800 | EXPECT_CALL(*mock_service, state()) |
| 2801 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 2802 | EXPECT_TRUE(EnumerateAvailableServices().empty()); |
| 2803 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 2804 | |
| 2805 | EXPECT_CALL(*mock_service, IsVisible()) |
| 2806 | .WillRepeatedly(Return(true)); |
| 2807 | Service::ConnectState unwatched_states[] = { |
| 2808 | Service::kStateUnknown, |
| 2809 | Service::kStateIdle, |
| 2810 | Service::kStateFailure |
| 2811 | }; |
| 2812 | for (size_t i = 0; i < arraysize(unwatched_states); ++i) { |
| 2813 | EXPECT_CALL(*mock_service, state()) |
| 2814 | .WillRepeatedly(Return(unwatched_states[i])); |
| 2815 | EXPECT_FALSE(EnumerateAvailableServices().empty()); |
| 2816 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 2817 | } |
| 2818 | |
| 2819 | Service::ConnectState watched_states[] = { |
| 2820 | Service::kStateAssociating, |
| 2821 | Service::kStateConfiguring, |
| 2822 | Service::kStateConnected, |
| 2823 | Service::kStateDisconnected, |
| 2824 | Service::kStatePortal, |
| 2825 | Service::kStateOnline |
| 2826 | }; |
| 2827 | for (size_t i = 0; i < arraysize(watched_states); ++i) { |
| 2828 | EXPECT_CALL(*mock_service, state()) |
| 2829 | .WillRepeatedly(Return(watched_states[i])); |
| 2830 | EXPECT_FALSE(EnumerateAvailableServices().empty()); |
| 2831 | EXPECT_FALSE(EnumerateWatchedServices().empty()); |
| 2832 | } |
| 2833 | |
| 2834 | manager()->DeregisterService(mock_service); |
| 2835 | } |
| 2836 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 2837 | } // namespace shill |