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