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> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 13 | #include <base/files/scoped_temp_dir.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 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" |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 30 | #include "shill/mock_crypto_util_proxy.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 31 | #include "shill/mock_device.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 32 | #include "shill/mock_device_info.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 33 | #include "shill/mock_ethernet_eap_provider.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 34 | #include "shill/mock_glib.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 35 | #include "shill/mock_metrics.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 36 | #include "shill/mock_power_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 37 | #include "shill/mock_profile.h" |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 38 | #include "shill/mock_resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 39 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 40 | #include "shill/mock_store.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 41 | #include "shill/mock_wifi_provider.h" |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 42 | #include "shill/mock_wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 43 | #include "shill/property_store_unittest.h" |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 44 | #include "shill/proxy_factory.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 45 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 46 | #include "shill/wifi_service.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 47 | #include "shill/wimax_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 48 | |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 49 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 50 | using base::FilePath; |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 51 | using base::ScopedTempDir; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 53 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 54 | using std::string; |
| 55 | using std::vector; |
| 56 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 57 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 58 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 59 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 60 | using ::testing::ContainerEq; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 61 | using ::testing::DoAll; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 62 | using ::testing::InSequence; |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 63 | using ::testing::Mock; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 64 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 65 | using ::testing::NiceMock; |
| 66 | using ::testing::Return; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 67 | using ::testing::ReturnRef; |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 68 | using ::testing::SaveArg; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 69 | using ::testing::SetArgumentPointee; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 70 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 71 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 72 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 73 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 74 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 75 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 76 | ManagerTest() |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 77 | : power_manager_(new MockPowerManager(NULL, &proxy_factory_)), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 78 | device_info_(new NiceMock<MockDeviceInfo>( |
| 79 | control_interface(), |
| 80 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 81 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 82 | reinterpret_cast<Manager*>(NULL))), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 83 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()), |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 84 | ethernet_eap_provider_(new NiceMock<MockEthernetEapProvider>()), |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 85 | wifi_provider_(new NiceMock<MockWiFiProvider>()), |
| 86 | crypto_util_proxy_(new NiceMock<MockCryptoUtilProxy>(dispatcher(), |
| 87 | glib())) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 88 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 89 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 91 | manager(), |
| 92 | "null0", |
| 93 | "addr0", |
| 94 | 0)); |
| 95 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 96 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 97 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 98 | manager(), |
| 99 | "null1", |
| 100 | "addr1", |
| 101 | 1)); |
| 102 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 103 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 104 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 105 | manager(), |
| 106 | "null2", |
| 107 | "addr2", |
| 108 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 109 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 110 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 111 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 112 | manager(), |
| 113 | "null3", |
| 114 | "addr3", |
| 115 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 116 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 117 | SetRunning(true); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 118 | |
| 119 | // Replace the manager's adaptor with a quieter one, and one |
| 120 | // we can do EXPECT*() against. Passes ownership. |
| 121 | manager()->adaptor_.reset(manager_adaptor_); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 122 | |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 123 | // Replace the manager's Ethernet EAP provider with our mock. |
| 124 | // Passes ownership. |
| 125 | manager()->ethernet_eap_provider_.reset(ethernet_eap_provider_); |
| 126 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 127 | // Replace the manager's WiFi provider with our mock. Passes |
| 128 | // ownership. |
| 129 | manager()->wifi_provider_.reset(wifi_provider_); |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 130 | |
| 131 | // Replace the manager's crypto util proxy with our mock. Passes |
| 132 | // ownership. |
| 133 | manager()->crypto_util_proxy_.reset(crypto_util_proxy_); |
Paul Stewart | 9dd253e | 2013-04-22 08:32:59 -0700 | [diff] [blame] | 134 | |
| 135 | // Reset service serial number so service sorting by unique_name() |
| 136 | // (and by extension, sorting by order of creation) is predictable. |
| 137 | Service::serial_number_ = 10000; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 138 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 139 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 140 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 141 | void SetMetrics(Metrics *metrics) { |
| 142 | manager()->set_metrics(metrics); |
| 143 | } |
| 144 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 145 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 146 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 147 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 148 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 149 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 150 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 151 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 152 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 153 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 154 | manager->profiles_.push_back(profile); |
| 155 | } |
| 156 | |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 157 | void SetRunning(bool running) { |
| 158 | manager()->running_ = running; |
| 159 | } |
| 160 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 161 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 162 | return manager->ephemeral_profile_; |
| 163 | } |
| 164 | |
Paul Stewart | 307c250 | 2013-03-23 12:32:10 -0700 | [diff] [blame] | 165 | vector<ProfileRefPtr> &GetProfiles(Manager *manager) { |
| 166 | return manager->profiles_; |
| 167 | } |
| 168 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 169 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 170 | Profile::Identifier id("rather", "irrelevant"); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 171 | FilePath final_path(storage_path()); |
| 172 | final_path = final_path.Append("test.profile"); |
| 173 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 174 | storage->set_path(final_path); |
| 175 | if (!storage->Open()) |
| 176 | return NULL; |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 177 | Profile *profile(new Profile(control_interface(), |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 178 | metrics(), |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 179 | manager, |
| 180 | id, |
| 181 | "", |
| 182 | false)); |
| 183 | profile->set_storage(storage.release()); // Passes ownership of "storage". |
| 184 | return profile; // Passes onwership of "profile". |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 187 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 188 | const string &profile_identifier, |
| 189 | const string &service_name) { |
| 190 | GLib glib; |
| 191 | KeyFileStore store(&glib); |
| 192 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 193 | return store.Open() && |
| 194 | store.SetString(service_name, "rather", "irrelevant") && |
| 195 | store.Close(); |
| 196 | } |
| 197 | |
| 198 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 199 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 200 | string path; |
| 201 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 202 | return error.type(); |
| 203 | } |
| 204 | |
| 205 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 206 | Error error; |
| 207 | manager->PopAnyProfile(&error); |
| 208 | return error.type(); |
| 209 | } |
| 210 | |
Paul Stewart | 307c250 | 2013-03-23 12:32:10 -0700 | [diff] [blame] | 211 | Error::Type TestPopAllUserProfiles(Manager *manager) { |
| 212 | Error error; |
| 213 | manager->PopAllUserProfiles(&error); |
| 214 | return error.type(); |
| 215 | } |
| 216 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 217 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 218 | Error error; |
| 219 | manager->PopProfile(name, &error); |
| 220 | return error.type(); |
| 221 | } |
| 222 | |
| 223 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 224 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 225 | string path; |
| 226 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 227 | return error.type(); |
| 228 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 229 | |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 230 | Error::Type TestInsertUserProfile(Manager *manager, |
| 231 | const string &name, |
| 232 | const string &user_hash) { |
| 233 | Error error; |
| 234 | string path; |
| 235 | manager->InsertUserProfile(name, user_hash, &path, &error); |
| 236 | return error.type(); |
| 237 | } |
| 238 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 239 | scoped_refptr<MockProfile> AddNamedMockProfileToManager( |
| 240 | Manager *manager, const string &name) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 241 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 242 | new MockProfile(control_interface(), metrics(), manager, "")); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 243 | EXPECT_CALL(*profile, GetRpcIdentifier()).WillRepeatedly(Return(name)); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 244 | EXPECT_CALL(*profile, UpdateDevice(_)).WillRepeatedly(Return(false)); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 245 | AdoptProfile(manager, profile); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 246 | return profile; |
| 247 | } |
| 248 | |
| 249 | void AddMockProfileToManager(Manager *manager) { |
| 250 | AddNamedMockProfileToManager(manager, "/"); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 253 | void CompleteServiceSort() { |
| 254 | EXPECT_FALSE(manager()->sort_services_task_.IsCancelled()); |
| 255 | dispatcher()->DispatchPendingEvents(); |
| 256 | EXPECT_TRUE(manager()->sort_services_task_.IsCancelled()); |
| 257 | } |
| 258 | |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 259 | RpcIdentifier GetDefaultServiceRpcIdentifier() { |
| 260 | return manager()->GetDefaultServiceRpcIdentifier(NULL); |
| 261 | } |
| 262 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 263 | void SetResolver(Resolver *resolver) { |
| 264 | manager()->resolver_ = resolver; |
| 265 | } |
| 266 | |
| 267 | void SetIgnoredDNSSearchPaths(const string &search_paths) { |
| 268 | manager()->SetIgnoredDNSSearchPaths(search_paths, NULL); |
| 269 | } |
| 270 | |
| 271 | const string &GetIgnoredDNSSearchPaths() { |
| 272 | return manager()->props_.ignored_dns_search_paths; |
| 273 | } |
| 274 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 275 | WiFiServiceRefPtr ReleaseTempMockService() { |
| 276 | // Take a reference to hold during this function. |
| 277 | WiFiServiceRefPtr temp_service = temp_mock_service_; |
| 278 | temp_mock_service_ = NULL; |
| 279 | return temp_service; |
| 280 | } |
| 281 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 282 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 283 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 284 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 285 | class ServiceWatcher : public base::SupportsWeakPtr<ServiceWatcher> { |
| 286 | public: |
| 287 | ServiceWatcher() {} |
| 288 | virtual ~ServiceWatcher() {} |
| 289 | |
| 290 | MOCK_METHOD1(OnDefaultServiceChanged, void(const ServiceRefPtr &service)); |
| 291 | |
| 292 | private: |
| 293 | DISALLOW_COPY_AND_ASSIGN(ServiceWatcher); |
| 294 | }; |
| 295 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 296 | class TestProxyFactory : public ProxyFactory { |
| 297 | public: |
| 298 | TestProxyFactory() {} |
| 299 | |
| 300 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 301 | PowerManagerProxyDelegate */*delegate*/) { |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | private: |
| 306 | DISALLOW_COPY_AND_ASSIGN(TestProxyFactory); |
| 307 | }; |
| 308 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 309 | class TerminationActionTest : |
| 310 | public base::SupportsWeakPtr<TerminationActionTest> { |
| 311 | public: |
| 312 | static const char kActionName[]; |
| 313 | |
| 314 | TerminationActionTest() : manager_(NULL) {} |
| 315 | virtual ~TerminationActionTest() {} |
| 316 | |
| 317 | MOCK_METHOD1(Done, void(const Error &error)); |
| 318 | |
| 319 | void Action() { |
| 320 | manager_->TerminationActionComplete("action"); |
| 321 | } |
| 322 | |
| 323 | void set_manager(Manager *manager) { manager_ = manager; } |
| 324 | |
| 325 | private: |
| 326 | Manager *manager_; |
| 327 | DISALLOW_COPY_AND_ASSIGN(TerminationActionTest); |
| 328 | }; |
| 329 | |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 330 | class DestinationVerificationTest : |
| 331 | public base::SupportsWeakPtr<DestinationVerificationTest> { |
| 332 | public: |
| 333 | DestinationVerificationTest() {} |
| 334 | virtual ~DestinationVerificationTest() {} |
| 335 | |
| 336 | MOCK_METHOD2(ResultBoolCallbackStub, void(const Error &result, bool flag)); |
| 337 | MOCK_METHOD2(ResultStringCallbackStub, void(const Error &result, |
| 338 | const string &value)); |
| 339 | private: |
| 340 | DISALLOW_COPY_AND_ASSIGN(DestinationVerificationTest); |
| 341 | }; |
| 342 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 343 | void SetPowerState(PowerManagerProxyDelegate::SuspendState state) { |
| 344 | power_manager_->power_state_ = state; |
| 345 | } |
| 346 | |
| 347 | void SetPowerManager() { |
| 348 | manager()->set_power_manager(power_manager_.release()); |
| 349 | } |
| 350 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 351 | HookTable *GetTerminationActions() { |
| 352 | return &manager()->termination_actions_; |
| 353 | } |
| 354 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 355 | void OnPowerStateChanged(PowerManagerProxyDelegate::SuspendState state) { |
| 356 | manager()->OnPowerStateChanged(state); |
| 357 | } |
| 358 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 359 | void OnSuspendImminent(int suspend_id) { |
| 360 | manager()->OnSuspendImminent(suspend_id); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 363 | void OnSuspendActionsComplete(int suspend_id, const Error &error) { |
| 364 | manager()->OnSuspendActionsComplete(suspend_id, error); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 367 | vector<string> EnumerateAvailableServices() { |
| 368 | return manager()->EnumerateAvailableServices(NULL); |
| 369 | } |
| 370 | |
| 371 | vector<string> EnumerateWatchedServices() { |
| 372 | return manager()->EnumerateWatchedServices(NULL); |
| 373 | } |
| 374 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 375 | MockServiceRefPtr MakeAutoConnectableService() { |
| 376 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 377 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 378 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 379 | manager()); |
| 380 | service->MakeFavorite(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 381 | service->SetConnectable(true); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 382 | return service; |
| 383 | } |
| 384 | |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 385 | void SetEapProviderService(const ServiceRefPtr &service) { |
| 386 | ethernet_eap_provider_->set_service(service); |
| 387 | } |
| 388 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 389 | TestProxyFactory proxy_factory_; |
| 390 | scoped_ptr<MockPowerManager> power_manager_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 391 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 392 | scoped_ptr<MockDeviceInfo> device_info_; |
| 393 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 394 | // This service is held for the manager, and given ownership in a mock |
| 395 | // function. This ensures that when the Manager takes ownership, there |
| 396 | // is only one reference left. |
| 397 | scoped_refptr<MockWiFiService> temp_mock_service_; |
| 398 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 399 | // These pointers are owned by the manager, and only tracked here for |
| 400 | // EXPECT*() |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 401 | ManagerMockAdaptor *manager_adaptor_; |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 402 | MockEthernetEapProvider *ethernet_eap_provider_; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 403 | MockWiFiProvider *wifi_provider_; |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 404 | MockCryptoUtilProxy *crypto_util_proxy_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 407 | const char ManagerTest::TerminationActionTest::kActionName[] = "action"; |
| 408 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 409 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 410 | if (!manager()->sort_services_task_.IsCancelled()) { |
| 411 | manager()->SortServicesTask(); |
| 412 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 413 | return (svc0.get() == manager()->services_[0].get() && |
| 414 | svc1.get() == manager()->services_[1].get()); |
| 415 | } |
| 416 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 417 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 418 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 419 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 422 | TEST_F(ManagerTest, DeviceRegistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 423 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 424 | .WillByDefault(Return(Technology::kEthernet)); |
| 425 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 426 | .WillByDefault(Return(Technology::kWifi)); |
| 427 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 428 | .WillByDefault(Return(Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 429 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 430 | manager()->RegisterDevice(mock_devices_[0]); |
| 431 | manager()->RegisterDevice(mock_devices_[1]); |
| 432 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 433 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 434 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 435 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 436 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 439 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 440 | manager()->running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 441 | mock_devices_[0]->enabled_persistent_ = true; |
| 442 | mock_devices_[1]->enabled_persistent_ = false; |
| 443 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 444 | .Times(1); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 445 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 446 | .Times(0); |
| 447 | manager()->RegisterDevice(mock_devices_[0]); |
| 448 | manager()->RegisterDevice(mock_devices_[1]); |
| 449 | } |
| 450 | |
| 451 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 452 | MockProfile *profile = |
| 453 | new MockProfile(control_interface(), metrics(), manager(), ""); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 454 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 455 | AdoptProfile(manager(), profile); // Passes ownership. |
| 456 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 457 | EXPECT_CALL(*profile, UpdateDevice(device_ref)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 458 | manager()->RegisterDevice(mock_devices_[0]); |
| 459 | } |
| 460 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 461 | TEST_F(ManagerTest, DeviceDeregistration) { |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 462 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 463 | .WillByDefault(Return(Technology::kEthernet)); |
| 464 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 465 | .WillByDefault(Return(Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 466 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 467 | manager()->RegisterDevice(mock_devices_[0]); |
| 468 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 469 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 470 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 471 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 472 | |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 473 | MockProfile *profile = |
| 474 | new MockProfile(control_interface(), metrics(), manager(), ""); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 475 | AdoptProfile(manager(), profile); // Passes ownership. |
| 476 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 477 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 478 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 479 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 480 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 481 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 482 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 483 | EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1]))); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 484 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 485 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 489 | // It's much easier and safer to use a real GLib for this test. |
| 490 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 491 | Manager manager(control_interface(), |
| 492 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 493 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 494 | &glib, |
| 495 | run_path(), |
| 496 | storage_path(), |
| 497 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 498 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 499 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 500 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 501 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 502 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 503 | new NiceMock<MockService>(control_interface(), |
| 504 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 505 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 506 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 507 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 508 | new NiceMock<MockService>(control_interface(), |
| 509 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 510 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 511 | &manager)); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 512 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 513 | string service1_name(mock_service->unique_name()); |
| 514 | string service2_name(mock_service2->unique_name()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 515 | |
| 516 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 517 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 518 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 519 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 520 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 521 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 522 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 523 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 524 | manager.RegisterService(mock_service); |
| 525 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 526 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 527 | Error error; |
| 528 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 529 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 530 | EXPECT_EQ(2, ids.size()); |
| 531 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 532 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 533 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 534 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 535 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 536 | |
| 537 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 540 | TEST_F(ManagerTest, RegisterKnownService) { |
| 541 | // It's much easier and safer to use a real GLib for this test. |
| 542 | GLib glib; |
| 543 | Manager manager(control_interface(), |
| 544 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 545 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 546 | &glib, |
| 547 | run_path(), |
| 548 | storage_path(), |
| 549 | string()); |
| 550 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 551 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 552 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 553 | { |
| 554 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 555 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 556 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 557 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 558 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 559 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 560 | } // Force destruction of service1. |
| 561 | |
| 562 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 563 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 564 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 565 | &manager)); |
| 566 | manager.RegisterService(service2); |
| 567 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 568 | manager.Stop(); |
| 569 | } |
| 570 | |
| 571 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 572 | // It's much easier and safer to use a real GLib for this test. |
| 573 | GLib glib; |
| 574 | Manager manager(control_interface(), |
| 575 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 576 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 577 | &glib, |
| 578 | run_path(), |
| 579 | storage_path(), |
| 580 | string()); |
| 581 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 582 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 583 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 584 | { |
| 585 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 586 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 587 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 588 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 589 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 590 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 591 | } // Force destruction of service1. |
| 592 | scoped_refptr<MockService> mock_service2( |
| 593 | new NiceMock<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 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 598 | .WillRepeatedly(Return(mock_service2->unique_name())); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 599 | manager.RegisterService(mock_service2); |
| 600 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 601 | manager.Stop(); |
| 602 | } |
| 603 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 604 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 605 | // WiFi assumes that it can deregister a service that is not |
| 606 | // registered. (E.g. a hidden service can be deregistered when it |
| 607 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 608 | // |
| 609 | // So test that doing so doesn't cause a crash. |
| 610 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 611 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 612 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 613 | manager()); |
| 614 | manager()->DeregisterService(service); |
| 615 | } |
| 616 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 617 | TEST_F(ManagerTest, GetProperties) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 618 | AddMockProfileToManager(manager()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 619 | map<string, ::DBus::Variant> props; |
| 620 | Error error(Error::kInvalidProperty, ""); |
| 621 | { |
| 622 | ::DBus::Error dbus_error; |
| 623 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 624 | manager()->mutable_store()->SetStringProperty( |
| 625 | flimflam::kCheckPortalListProperty, |
| 626 | expected, |
| 627 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 628 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 629 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 630 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 631 | expected); |
| 632 | } |
| 633 | { |
| 634 | ::DBus::Error dbus_error; |
| 635 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 636 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 637 | expected, |
| 638 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 639 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 640 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 641 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 642 | expected); |
| 643 | } |
| 644 | } |
| 645 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 646 | TEST_F(ManagerTest, GetDevicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 647 | AddMockProfileToManager(manager()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 648 | manager()->RegisterDevice(mock_devices_[0]); |
| 649 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 650 | { |
| 651 | map<string, ::DBus::Variant> props; |
| 652 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 653 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 654 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 655 | vector < ::DBus::Path> devices = |
| 656 | props[flimflam::kDevicesProperty].operator vector< ::DBus::Path>(); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 657 | EXPECT_EQ(2, devices.size()); |
| 658 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 659 | } |
| 660 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 661 | TEST_F(ManagerTest, GetServicesProperty) { |
Paul Stewart | cb3eb89 | 2012-06-07 14:24:46 -0700 | [diff] [blame] | 662 | AddMockProfileToManager(manager()); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 663 | map<string, ::DBus::Variant> props; |
| 664 | ::DBus::Error dbus_error; |
| 665 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
| 666 | map<string, ::DBus::Variant>::const_iterator prop = |
| 667 | props.find(flimflam::kServicesProperty); |
| 668 | ASSERT_FALSE(prop == props.end()); |
| 669 | const ::DBus::Variant &variant = prop->second; |
| 670 | ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature())); |
| 671 | } |
| 672 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 673 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 674 | Manager manager(control_interface(), |
| 675 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 676 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 677 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 678 | run_path(), |
| 679 | storage_path(), |
| 680 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 681 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 682 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 683 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 684 | &manager)); |
| 685 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 686 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 687 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 688 | ProfileRefPtr profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 689 | new Profile(control_interface(), metrics(), &manager, id, "", false)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 690 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 691 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 692 | .WillRepeatedly(Return(true)); |
| 693 | EXPECT_CALL(*storage, Flush()) |
| 694 | .Times(AnyNumber()) |
| 695 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 696 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 697 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 698 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 699 | // Create a profile that already has |s2| in it. |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 700 | ProfileRefPtr profile( |
| 701 | new EphemeralProfile(control_interface(), metrics(), &manager)); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 702 | EXPECT_TRUE(profile->AdoptService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 703 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 704 | // Now, move the Service |s2| to another profile. |
| 705 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 706 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 707 | |
| 708 | // Force destruction of the original Profile, to ensure that the Service |
| 709 | // is kept alive and populated with data. |
| 710 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 711 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 712 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 715 | TEST_F(ManagerTest, LookupProfileByRpcIdentifier) { |
| 716 | scoped_refptr<MockProfile> mock_profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 717 | new MockProfile(control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 718 | const string kProfileName("profile0"); |
| 719 | EXPECT_CALL(*mock_profile, GetRpcIdentifier()) |
| 720 | .WillRepeatedly(Return(kProfileName)); |
| 721 | AdoptProfile(manager(), mock_profile); |
| 722 | |
| 723 | EXPECT_FALSE(manager()->LookupProfileByRpcIdentifier("foo")); |
| 724 | ProfileRefPtr profile = manager()->LookupProfileByRpcIdentifier(kProfileName); |
| 725 | EXPECT_EQ(mock_profile.get(), profile.get()); |
| 726 | } |
| 727 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 728 | TEST_F(ManagerTest, SetProfileForService) { |
| 729 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 730 | new MockProfile(control_interface(), metrics(), manager(), "")); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 731 | string profile_name0("profile0"); |
| 732 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 733 | .WillRepeatedly(Return(profile_name0)); |
| 734 | AdoptProfile(manager(), profile0); |
| 735 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 736 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 737 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 738 | manager())); |
Paul Stewart | 649f3a4 | 2012-04-24 23:22:16 -0700 | [diff] [blame] | 739 | EXPECT_FALSE(manager()->HasService(service)); |
| 740 | { |
| 741 | Error error; |
| 742 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 743 | .WillOnce(Return(true)); |
| 744 | // Expect that setting the profile of a service that does not already |
| 745 | // have one assigned does not cause a crash. |
| 746 | manager()->SetProfileForService(service, "profile0", &error); |
| 747 | EXPECT_TRUE(error.IsSuccess()); |
| 748 | } |
| 749 | |
| 750 | // The service should be registered as a side-effect of the profile being |
| 751 | // set for this service. |
| 752 | EXPECT_TRUE(manager()->HasService(service)); |
| 753 | |
| 754 | // Since we have mocked Profile::AdoptServie() above, the service's |
| 755 | // profile was not actually changed. Do so explicitly now. |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 756 | service->set_profile(profile0); |
| 757 | |
| 758 | { |
| 759 | Error error; |
| 760 | manager()->SetProfileForService(service, "foo", &error); |
| 761 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 762 | EXPECT_EQ("Unknown Profile foo requested for Service", error.message()); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | { |
| 766 | Error error; |
| 767 | manager()->SetProfileForService(service, profile_name0, &error); |
| 768 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 769 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 770 | } |
| 771 | |
| 772 | scoped_refptr<MockProfile> profile1( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 773 | new MockProfile(control_interface(), metrics(), manager(), "")); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 774 | string profile_name1("profile1"); |
| 775 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 776 | .WillRepeatedly(Return(profile_name1)); |
| 777 | AdoptProfile(manager(), profile1); |
| 778 | |
| 779 | { |
| 780 | Error error; |
| 781 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 782 | .WillOnce(Return(true)); |
| 783 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 784 | .WillOnce(Return(true)); |
| 785 | manager()->SetProfileForService(service, profile_name1, &error); |
| 786 | EXPECT_TRUE(error.IsSuccess()); |
| 787 | } |
| 788 | } |
| 789 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 790 | TEST_F(ManagerTest, CreateProfile) { |
| 791 | // It's much easier to use real Glib here since we want the storage |
| 792 | // side-effects. |
| 793 | GLib glib; |
| 794 | ScopedTempDir temp_dir; |
| 795 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 796 | |
| 797 | Manager manager(control_interface(), |
| 798 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 799 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 800 | &glib, |
| 801 | run_path(), |
| 802 | storage_path(), |
| 803 | temp_dir.path().value()); |
| 804 | |
| 805 | // Invalid name should be rejected. |
| 806 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 807 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 808 | // A profile with invalid characters in it should similarly be rejected. |
| 809 | EXPECT_EQ(Error::kInvalidArguments, |
| 810 | TestCreateProfile(&manager, "valid_profile")); |
| 811 | |
| 812 | // We should be able to create a machine profile. |
| 813 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, "valid")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 814 | |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 815 | // We should succeed in creating a valid user profile. Verify the returned |
| 816 | // path. |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 817 | const char kProfile[] = "~user/profile"; |
Gary Morain | b672d35 | 2012-04-25 09:19:06 -0700 | [diff] [blame] | 818 | { |
| 819 | Error error; |
| 820 | string path; |
| 821 | manager.CreateProfile(kProfile, &path, &error); |
| 822 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 823 | EXPECT_EQ("/profile_rpc", path); |
| 824 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 825 | |
| 826 | // We should fail in creating it a second time (already exists). |
| 827 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 828 | } |
| 829 | |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 830 | // We receive PopProfile when a user logs out, and it should always trigger a |
| 831 | // MemoryLog Clear() call. |
| 832 | TEST_F(ManagerTest, PopProfileShouldClearMemoryLog) { |
| 833 | GLib glib; |
| 834 | ScopedTempDir temp_dir; |
| 835 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 836 | Manager manager(control_interface(), |
| 837 | dispatcher(), |
| 838 | metrics(), |
| 839 | &glib, |
| 840 | run_path(), |
| 841 | storage_path(), |
| 842 | temp_dir.path().value()); |
| 843 | const char kProfile0[] = "~user/profile0"; |
| 844 | const char kPurgedMessage[] = "This message should be purged"; |
| 845 | // Create a profile and push it on the stack, leave one uncreated |
| 846 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 847 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 848 | |
| 849 | // Popping a profile which isn't on top should still clear the log. |
| 850 | LOG(INFO) << kPurgedMessage; |
| 851 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 852 | kPurgedMessage)); |
| 853 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, "~user/profile1")); |
| 854 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 855 | kPurgedMessage)); |
| 856 | |
| 857 | // Popping an invalid profile name should do the same thing. |
| 858 | LOG(INFO) << kPurgedMessage; |
| 859 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 860 | kPurgedMessage)); |
| 861 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 862 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 863 | kPurgedMessage)); |
| 864 | |
| 865 | // Successful pops also purge the message log. |
| 866 | LOG(INFO) << kPurgedMessage; |
| 867 | EXPECT_TRUE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 868 | kPurgedMessage)); |
| 869 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile0)); |
| 870 | EXPECT_FALSE(MemoryLog::GetInstance()->TestContainsMessageWithText( |
| 871 | kPurgedMessage)); |
| 872 | } |
| 873 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 874 | TEST_F(ManagerTest, PushPopProfile) { |
| 875 | // It's much easier to use real Glib in creating a Manager for this |
| 876 | // test here since we want the storage side-effects. |
| 877 | GLib glib; |
| 878 | ScopedTempDir temp_dir; |
| 879 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 880 | Manager manager(control_interface(), |
| 881 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 882 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 883 | &glib, |
| 884 | run_path(), |
| 885 | storage_path(), |
| 886 | temp_dir.path().value()); |
| 887 | |
| 888 | // Pushing an invalid profile should fail. |
| 889 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 890 | |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 891 | // Pushing a default profile that does not exist should fail. |
| 892 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, "default")); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 893 | |
| 894 | const char kProfile0[] = "~user/profile0"; |
| 895 | const char kProfile1[] = "~user/profile1"; |
| 896 | |
| 897 | // Create a couple of profiles. |
| 898 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 899 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 900 | |
| 901 | // Push these profiles on the stack. |
| 902 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 903 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 904 | |
| 905 | // Pushing a profile a second time should fail. |
| 906 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 907 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 908 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 909 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 910 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 911 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 912 | |
| 913 | // Make sure a profile name that doesn't exist fails. |
| 914 | const char kProfile2Id[] = "profile2"; |
| 915 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 916 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 917 | |
| 918 | // Create a new service, with a specific storage name. |
| 919 | scoped_refptr<MockService> service( |
| 920 | new NiceMock<MockService>(control_interface(), |
| 921 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 922 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 923 | &manager)); |
| 924 | const char kServiceName[] = "service_storage_name"; |
| 925 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 926 | .WillRepeatedly(Return(kServiceName)); |
| 927 | EXPECT_CALL(*service.get(), Load(_)) |
| 928 | .WillRepeatedly(Return(true)); |
| 929 | |
| 930 | // Add this service to the manager -- it should end up in the ephemeral |
| 931 | // profile. |
| 932 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 933 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 934 | |
| 935 | // Create storage for a profile that contains the service storage name. |
| 936 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 937 | kServiceName)); |
| 938 | |
| 939 | // When we push the profile, the service should move away from the |
| 940 | // ephemeral profile to this new profile since it has an entry for |
| 941 | // this service. |
| 942 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 943 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 944 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 945 | |
| 946 | // Insert another profile that should supersede ownership of the service. |
| 947 | const char kProfile3Id[] = "profile3"; |
| 948 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 949 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 950 | kServiceName)); |
| 951 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 952 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 953 | |
| 954 | // Popping an invalid profile name should fail. |
| 955 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 956 | |
| 957 | // Popping an profile that is not at the top of the stack should fail. |
| 958 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 959 | |
| 960 | // Popping the top profile should succeed. |
| 961 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 962 | |
| 963 | // Moreover the service should have switched profiles to profile 2. |
| 964 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 965 | |
| 966 | // Popping the top profile should succeed. |
| 967 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 968 | |
| 969 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 970 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 971 | |
| 972 | // Pop the remaining two services off the stack. |
| 973 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 974 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 975 | |
| 976 | // Next pop should fail with "stack is empty". |
| 977 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
Paul Stewart | d0a3b81 | 2012-03-28 22:48:22 -0700 | [diff] [blame] | 978 | |
| 979 | const char kMachineProfile0[] = "machineprofile0"; |
| 980 | const char kMachineProfile1[] = "machineprofile1"; |
| 981 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile0)); |
| 982 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kMachineProfile1)); |
| 983 | |
| 984 | // Should be able to push a machine profile. |
| 985 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile0)); |
| 986 | |
| 987 | // Should be able to push a user profile atop a machine profile. |
| 988 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 989 | |
| 990 | // Pushing a system-wide profile on top of a user profile should fail. |
| 991 | EXPECT_EQ(Error::kInvalidArguments, |
| 992 | TestPushProfile(&manager, kMachineProfile1)); |
| 993 | |
| 994 | // However if we pop the user profile, we should be able stack another |
| 995 | // machine profile on. |
| 996 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 997 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kMachineProfile1)); |
Paul Stewart | 307c250 | 2013-03-23 12:32:10 -0700 | [diff] [blame] | 998 | |
| 999 | // Add two user profiles to the top of the stack. |
| 1000 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 1001 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 1002 | vector<ProfileRefPtr> &profiles = GetProfiles(&manager); |
| 1003 | EXPECT_EQ(4, profiles.size()); |
| 1004 | |
| 1005 | // PopAllUserProfiles should remove both user profiles, leaving the two |
| 1006 | // machine profiles. |
| 1007 | EXPECT_EQ(Error::kSuccess, TestPopAllUserProfiles(&manager)); |
| 1008 | EXPECT_EQ(2, profiles.size()); |
| 1009 | EXPECT_TRUE(profiles[0]->GetUser().empty()); |
| 1010 | EXPECT_TRUE(profiles[1]->GetUser().empty()); |
Paul Stewart | f3eced9 | 2013-04-17 12:18:22 -0700 | [diff] [blame] | 1011 | |
| 1012 | // Use InsertUserProfile() instead. Although a machine profile is valid |
| 1013 | // in this state, it cannot be added via InsertUserProfile. |
| 1014 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kMachineProfile1)); |
| 1015 | EXPECT_EQ(Error::kInvalidArguments, |
| 1016 | TestInsertUserProfile(&manager, kMachineProfile1, "machinehash1")); |
| 1017 | const char kUserHash0[] = "userhash0"; |
| 1018 | const char kUserHash1[] = "userhash1"; |
| 1019 | EXPECT_EQ(Error::kSuccess, |
| 1020 | TestInsertUserProfile(&manager, kProfile0, kUserHash0)); |
| 1021 | EXPECT_EQ(Error::kSuccess, |
| 1022 | TestInsertUserProfile(&manager, kProfile1, kUserHash1)); |
| 1023 | EXPECT_EQ(3, profiles.size()); |
| 1024 | EXPECT_EQ(kUserHash0, profiles[1]->GetUserHash()); |
| 1025 | EXPECT_EQ(kUserHash1, profiles[2]->GetUserHash()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Paul Stewart | e73d05c | 2012-03-29 16:26:05 -0700 | [diff] [blame] | 1028 | TEST_F(ManagerTest, RemoveProfile) { |
| 1029 | // It's much easier to use real Glib in creating a Manager for this |
| 1030 | // test here since we want the storage side-effects. |
| 1031 | GLib glib; |
| 1032 | ScopedTempDir temp_dir; |
| 1033 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1034 | Manager manager(control_interface(), |
| 1035 | dispatcher(), |
| 1036 | metrics(), |
| 1037 | &glib, |
| 1038 | run_path(), |
| 1039 | storage_path(), |
| 1040 | temp_dir.path().value()); |
| 1041 | |
| 1042 | const char kProfile0[] = "profile0"; |
| 1043 | FilePath profile_path( |
| 1044 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 1045 | |
| 1046 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 1047 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 1048 | |
| 1049 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 1050 | |
| 1051 | // Remove should fail since the profile is still on the stack. |
| 1052 | { |
| 1053 | Error error; |
| 1054 | manager.RemoveProfile(kProfile0, &error); |
| 1055 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1056 | } |
| 1057 | |
| 1058 | // Profile path should still exist. |
| 1059 | EXPECT_TRUE(file_util::PathExists(profile_path)); |
| 1060 | |
| 1061 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 1062 | |
| 1063 | // This should succeed now that the profile is off the stack. |
| 1064 | { |
| 1065 | Error error; |
| 1066 | manager.RemoveProfile(kProfile0, &error); |
| 1067 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 1068 | } |
| 1069 | |
| 1070 | // Profile path should no longer exist. |
| 1071 | EXPECT_FALSE(file_util::PathExists(profile_path)); |
| 1072 | |
| 1073 | // Another remove succeeds, due to a foible in file_util::Delete -- |
| 1074 | // it is not an error to delete a file that does not exist. |
| 1075 | { |
| 1076 | Error error; |
| 1077 | manager.RemoveProfile(kProfile0, &error); |
| 1078 | EXPECT_EQ(Error::kSuccess, error.type()); |
| 1079 | } |
| 1080 | |
| 1081 | // Let's create an error case that will "work". Create a non-empty |
| 1082 | // directory in the place of the profile pathname. |
| 1083 | ASSERT_TRUE(file_util::CreateDirectory(profile_path.Append("foo"))); |
| 1084 | { |
| 1085 | Error error; |
| 1086 | manager.RemoveProfile(kProfile0, &error); |
| 1087 | EXPECT_EQ(Error::kOperationFailed, error.type()); |
| 1088 | } |
| 1089 | } |
| 1090 | |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1091 | TEST_F(ManagerTest, CreateDuplicateProfileWithMissingKeyfile) { |
| 1092 | // It's much easier to use real Glib in creating a Manager for this |
| 1093 | // test here since we want the storage side-effects. |
| 1094 | GLib glib; |
| 1095 | ScopedTempDir temp_dir; |
| 1096 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1097 | Manager manager(control_interface(), |
| 1098 | dispatcher(), |
| 1099 | metrics(), |
| 1100 | &glib, |
| 1101 | run_path(), |
| 1102 | storage_path(), |
| 1103 | temp_dir.path().value()); |
| 1104 | |
| 1105 | const char kProfile0[] = "profile0"; |
| 1106 | FilePath profile_path( |
| 1107 | FilePath(storage_path()).Append(string(kProfile0) + ".profile")); |
| 1108 | |
| 1109 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 1110 | ASSERT_TRUE(file_util::PathExists(profile_path)); |
| 1111 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 1112 | |
| 1113 | // Ensure that even if the backing filestore is removed, we still can't |
| 1114 | // create a profile twice. |
| 1115 | ASSERT_TRUE(file_util::Delete(profile_path, false)); |
| 1116 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile0)); |
| 1117 | } |
| 1118 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1119 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 1120 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 1121 | // system teardown. |
| 1122 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 1123 | return arg.get() == ref_address; |
| 1124 | } |
| 1125 | |
| 1126 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 1127 | MockServiceRefPtr s_not_in_profile( |
| 1128 | new NiceMock<MockService>(control_interface(), |
| 1129 | dispatcher(), |
| 1130 | metrics(), |
| 1131 | manager())); |
| 1132 | MockServiceRefPtr s_not_in_group( |
| 1133 | new NiceMock<MockService>(control_interface(), |
| 1134 | dispatcher(), |
| 1135 | metrics(), |
| 1136 | manager())); |
| 1137 | MockServiceRefPtr s_configure_fail( |
| 1138 | new NiceMock<MockService>(control_interface(), |
| 1139 | dispatcher(), |
| 1140 | metrics(), |
| 1141 | manager())); |
| 1142 | MockServiceRefPtr s_configure_succeed( |
| 1143 | new NiceMock<MockService>(control_interface(), |
| 1144 | dispatcher(), |
| 1145 | metrics(), |
| 1146 | manager())); |
| 1147 | |
| 1148 | string entry_name("entry_name"); |
| 1149 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 1150 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 1151 | .WillRepeatedly(Return("not_entry_name")); |
| 1152 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 1153 | .WillRepeatedly(Return(entry_name)); |
| 1154 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 1155 | .WillRepeatedly(Return(entry_name)); |
| 1156 | |
| 1157 | manager()->RegisterService(s_not_in_profile); |
| 1158 | manager()->RegisterService(s_not_in_group); |
| 1159 | manager()->RegisterService(s_configure_fail); |
| 1160 | manager()->RegisterService(s_configure_succeed); |
| 1161 | |
| 1162 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1163 | new StrictMock<MockProfile>( |
| 1164 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1165 | scoped_refptr<MockProfile> profile1( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1166 | new StrictMock<MockProfile>( |
| 1167 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 1168 | |
| 1169 | s_not_in_group->set_profile(profile1); |
| 1170 | s_configure_fail->set_profile(profile1); |
| 1171 | s_configure_succeed->set_profile(profile1); |
| 1172 | |
| 1173 | AdoptProfile(manager(), profile0); |
| 1174 | AdoptProfile(manager(), profile1); |
| 1175 | |
| 1176 | // No services are a member of this profile. |
| 1177 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 1178 | |
| 1179 | // No services that are members of this profile have this entry name. |
| 1180 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 1181 | |
| 1182 | // Only services that are members of the profile and group will be abandoned. |
| 1183 | EXPECT_CALL(*profile1.get(), |
| 1184 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1185 | EXPECT_CALL(*profile1.get(), |
| 1186 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1187 | EXPECT_CALL(*profile1.get(), |
| 1188 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 1189 | .WillOnce(Return(true)); |
| 1190 | EXPECT_CALL(*profile1.get(), |
| 1191 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1192 | .WillOnce(Return(true)); |
| 1193 | |
| 1194 | // Never allow services to re-join profile1. |
| 1195 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 1196 | .WillRepeatedly(Return(false)); |
| 1197 | |
| 1198 | // Only allow one of the members of the profile and group to successfully |
| 1199 | // join profile0. |
| 1200 | EXPECT_CALL(*profile0.get(), |
| 1201 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 1202 | EXPECT_CALL(*profile0.get(), |
| 1203 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 1204 | EXPECT_CALL(*profile0.get(), |
| 1205 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 1206 | .WillOnce(Return(false)); |
| 1207 | EXPECT_CALL(*profile0.get(), |
| 1208 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 1209 | .WillOnce(Return(true)); |
| 1210 | |
| 1211 | // Expect the failed-to-configure service to have Unload() called on it. |
| 1212 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 1213 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 1214 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 1215 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 1216 | |
| 1217 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 1218 | |
| 1219 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 1220 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 1221 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 1222 | |
| 1223 | // Since we are using a MockProfile, the profile does not actually change, |
| 1224 | // since ConfigureService was not actually called on the service. |
| 1225 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 1226 | } |
| 1227 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1228 | TEST_F(ManagerTest, HandleProfileEntryDeletionWithUnload) { |
| 1229 | MockServiceRefPtr s_will_remove0( |
| 1230 | new NiceMock<MockService>(control_interface(), |
| 1231 | dispatcher(), |
| 1232 | metrics(), |
| 1233 | manager())); |
| 1234 | MockServiceRefPtr s_will_remove1( |
| 1235 | new NiceMock<MockService>(control_interface(), |
| 1236 | dispatcher(), |
| 1237 | metrics(), |
| 1238 | manager())); |
| 1239 | MockServiceRefPtr s_will_not_remove0( |
| 1240 | new NiceMock<MockService>(control_interface(), |
| 1241 | dispatcher(), |
| 1242 | metrics(), |
| 1243 | manager())); |
| 1244 | MockServiceRefPtr s_will_not_remove1( |
| 1245 | new NiceMock<MockService>(control_interface(), |
| 1246 | dispatcher(), |
| 1247 | metrics(), |
| 1248 | manager())); |
| 1249 | |
| 1250 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1251 | .Times(4); // Once for each registration. |
| 1252 | |
| 1253 | string entry_name("entry_name"); |
| 1254 | EXPECT_CALL(*s_will_remove0.get(), GetStorageIdentifier()) |
| 1255 | .WillRepeatedly(Return(entry_name)); |
| 1256 | EXPECT_CALL(*s_will_remove1.get(), GetStorageIdentifier()) |
| 1257 | .WillRepeatedly(Return(entry_name)); |
| 1258 | EXPECT_CALL(*s_will_not_remove0.get(), GetStorageIdentifier()) |
| 1259 | .WillRepeatedly(Return(entry_name)); |
| 1260 | EXPECT_CALL(*s_will_not_remove1.get(), GetStorageIdentifier()) |
| 1261 | .WillRepeatedly(Return(entry_name)); |
| 1262 | |
| 1263 | manager()->RegisterService(s_will_remove0); |
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 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1266 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1267 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1268 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1269 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1270 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1271 | |
| 1272 | // One for each service added above. |
| 1273 | ASSERT_EQ(4, manager()->services_.size()); |
| 1274 | |
| 1275 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1276 | new StrictMock<MockProfile>( |
| 1277 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1278 | |
| 1279 | s_will_remove0->set_profile(profile); |
| 1280 | s_will_remove1->set_profile(profile); |
| 1281 | s_will_not_remove0->set_profile(profile); |
| 1282 | s_will_not_remove1->set_profile(profile); |
| 1283 | |
| 1284 | AdoptProfile(manager(), profile); |
| 1285 | |
| 1286 | // Deny any of the services re-entry to the profile. |
| 1287 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1288 | .WillRepeatedly(Return(false)); |
| 1289 | |
| 1290 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove0))) |
| 1291 | .WillOnce(Return(true)); |
| 1292 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_remove1))) |
| 1293 | .WillOnce(Return(true)); |
| 1294 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove0))) |
| 1295 | .WillOnce(Return(true)); |
| 1296 | EXPECT_CALL(*profile, AbandonService(ServiceRefPtr(s_will_not_remove1))) |
| 1297 | .WillOnce(Return(true)); |
| 1298 | |
| 1299 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1300 | .WillOnce(Return(true)); |
| 1301 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1302 | .WillOnce(Return(true)); |
| 1303 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
| 1304 | .WillOnce(Return(false)); |
| 1305 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1306 | .WillOnce(Return(false)); |
| 1307 | |
| 1308 | |
| 1309 | // This will cause all the profiles to be unloaded. |
| 1310 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile, entry_name)); |
| 1311 | |
| 1312 | // 2 of the 4 services added above should have been unregistered and |
| 1313 | // removed, leaving 2. |
| 1314 | EXPECT_EQ(2, manager()->services_.size()); |
| 1315 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1316 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
| 1317 | } |
| 1318 | |
| 1319 | TEST_F(ManagerTest, PopProfileWithUnload) { |
| 1320 | MockServiceRefPtr s_will_remove0( |
| 1321 | new NiceMock<MockService>(control_interface(), |
| 1322 | dispatcher(), |
| 1323 | metrics(), |
| 1324 | manager())); |
| 1325 | MockServiceRefPtr s_will_remove1( |
| 1326 | new NiceMock<MockService>(control_interface(), |
| 1327 | dispatcher(), |
| 1328 | metrics(), |
| 1329 | manager())); |
| 1330 | MockServiceRefPtr s_will_not_remove0( |
| 1331 | new NiceMock<MockService>(control_interface(), |
| 1332 | dispatcher(), |
| 1333 | metrics(), |
| 1334 | manager())); |
| 1335 | MockServiceRefPtr s_will_not_remove1( |
| 1336 | new NiceMock<MockService>(control_interface(), |
| 1337 | dispatcher(), |
| 1338 | metrics(), |
| 1339 | manager())); |
| 1340 | |
| 1341 | EXPECT_CALL(*metrics(), NotifyDefaultServiceChanged(NULL)) |
| 1342 | .Times(5); // Once for each registration, and one after profile pop. |
| 1343 | |
| 1344 | manager()->RegisterService(s_will_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1345 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1346 | manager()->RegisterService(s_will_not_remove0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1347 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1348 | manager()->RegisterService(s_will_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1349 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1350 | manager()->RegisterService(s_will_not_remove1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1351 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1352 | |
| 1353 | // One for each service added above. |
| 1354 | ASSERT_EQ(4, manager()->services_.size()); |
| 1355 | |
| 1356 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1357 | new StrictMock<MockProfile>( |
| 1358 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1359 | scoped_refptr<MockProfile> profile1( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1360 | new StrictMock<MockProfile>( |
| 1361 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1362 | |
| 1363 | s_will_remove0->set_profile(profile1); |
| 1364 | s_will_remove1->set_profile(profile1); |
| 1365 | s_will_not_remove0->set_profile(profile1); |
| 1366 | s_will_not_remove1->set_profile(profile1); |
| 1367 | |
| 1368 | AdoptProfile(manager(), profile0); |
| 1369 | AdoptProfile(manager(), profile1); |
| 1370 | |
| 1371 | // Deny any of the services entry to profile0, so they will all be unloaded. |
| 1372 | EXPECT_CALL(*profile0, ConfigureService(_)) |
| 1373 | .WillRepeatedly(Return(false)); |
| 1374 | |
| 1375 | EXPECT_CALL(*s_will_remove0, Unload()) |
| 1376 | .WillOnce(Return(true)); |
| 1377 | EXPECT_CALL(*s_will_remove1, Unload()) |
| 1378 | .WillOnce(Return(true)); |
| 1379 | EXPECT_CALL(*s_will_not_remove0, Unload()) |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1380 | .WillRepeatedly(Return(false)); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1381 | EXPECT_CALL(*s_will_not_remove1, Unload()) |
| 1382 | .WillOnce(Return(false)); |
| 1383 | |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1384 | // Ignore calls to Profile::GetRpcIdentifier because of emitted changes of the |
| 1385 | // profile list. |
| 1386 | EXPECT_CALL(*profile0, GetRpcIdentifier()).Times(AnyNumber()); |
| 1387 | EXPECT_CALL(*profile1, GetRpcIdentifier()).Times(AnyNumber()); |
| 1388 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1389 | // This will pop profile1, which should cause all our profiles to unload. |
| 1390 | manager()->PopProfileInternal(); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 1391 | CompleteServiceSort(); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1392 | |
| 1393 | // 2 of the 4 services added above should have been unregistered and |
| 1394 | // removed, leaving 2. |
| 1395 | EXPECT_EQ(2, manager()->services_.size()); |
| 1396 | EXPECT_EQ(s_will_not_remove0.get(), manager()->services_[0].get()); |
| 1397 | EXPECT_EQ(s_will_not_remove1.get(), manager()->services_[1].get()); |
Paul Stewart | fc9a1da | 2012-06-27 15:54:52 -0700 | [diff] [blame] | 1398 | |
| 1399 | // Expect the unloaded services to lose their profile reference. |
| 1400 | EXPECT_FALSE(s_will_remove0->profile()); |
| 1401 | EXPECT_FALSE(s_will_remove1->profile()); |
| 1402 | |
| 1403 | // If we explicitly deregister a service, the effect should be the same |
| 1404 | // with respect to the profile reference. |
| 1405 | ASSERT_TRUE(s_will_not_remove0->profile()); |
| 1406 | manager()->DeregisterService(s_will_not_remove0); |
| 1407 | EXPECT_FALSE(s_will_not_remove0->profile()); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1410 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1411 | { |
| 1412 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1413 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1414 | flimflam::kOfflineModeProperty, |
| 1415 | PropertyStoreTest::kBoolV, |
| 1416 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1417 | } |
| 1418 | { |
| 1419 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1420 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1421 | flimflam::kCountryProperty, |
| 1422 | PropertyStoreTest::kStringV, |
| 1423 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1424 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1425 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1426 | { |
| 1427 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1428 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1429 | flimflam::kCountryProperty, |
| 1430 | PropertyStoreTest::kBoolV, |
| 1431 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1432 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1433 | } |
| 1434 | { |
| 1435 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1436 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 1437 | flimflam::kOfflineModeProperty, |
| 1438 | PropertyStoreTest::kStringV, |
| 1439 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1440 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1441 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1442 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1443 | { |
| 1444 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1445 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 1446 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1447 | flimflam::kEnabledTechnologiesProperty, |
| 1448 | PropertyStoreTest::kStringsV, |
| 1449 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 1450 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1451 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1454 | TEST_F(ManagerTest, RequestScan) { |
| 1455 | { |
| 1456 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1457 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 1458 | manager()->RegisterDevice(mock_devices_[1].get()); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1459 | EXPECT_CALL(*mock_devices_[0], technology()) |
| 1460 | .WillRepeatedly(Return(Technology::kWifi)); |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1461 | EXPECT_CALL(*mock_devices_[0], Scan(Device::kFullScan, _)); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 1462 | EXPECT_CALL(*mock_devices_[1], technology()) |
| 1463 | .WillRepeatedly(Return(Technology::kUnknown)); |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1464 | EXPECT_CALL(*mock_devices_[1], Scan(_, _)).Times(0); |
| 1465 | manager()->RequestScan(Device::kFullScan, flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | { |
| 1469 | Error error; |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1470 | manager()->RequestScan(Device::kFullScan, "bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1471 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1472 | } |
| 1473 | } |
| 1474 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1475 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1476 | KeyValueStore args; |
| 1477 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1478 | manager()->GetService(args, &e); |
| 1479 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1480 | EXPECT_EQ("must specify service type", e.message()); |
| 1481 | } |
| 1482 | |
| 1483 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 1484 | KeyValueStore args; |
| 1485 | Error e; |
| 1486 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 1487 | manager()->GetService(args, &e); |
| 1488 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1489 | EXPECT_EQ("service type is unsupported", e.message()); |
| 1490 | } |
| 1491 | |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 1492 | TEST_F(ManagerTest, GetServiceEthernetEap) { |
| 1493 | KeyValueStore args; |
| 1494 | Error e; |
| 1495 | ServiceRefPtr service; |
| 1496 | args.SetString(flimflam::kTypeProperty, kTypeEthernetEap); |
| 1497 | SetEapProviderService(service); |
| 1498 | EXPECT_EQ(service, manager()->GetService(args, &e)); |
| 1499 | EXPECT_TRUE(e.IsSuccess()); |
| 1500 | } |
| 1501 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1502 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1503 | KeyValueStore args; |
| 1504 | Error e; |
| 1505 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1506 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1507 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1508 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1509 | manager()->GetService(args, &e); |
| 1510 | EXPECT_TRUE(e.IsSuccess()); |
| 1511 | } |
| 1512 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1513 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 1514 | KeyValueStore args; |
| 1515 | Error e; |
| 1516 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1517 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1518 | new StrictMock<MockProfile>( |
| 1519 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1520 | AdoptProfile(manager(), profile); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1521 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1522 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1523 | EXPECT_FALSE(service); |
| 1524 | } |
| 1525 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1526 | TEST_F(ManagerTest, GetServiceVPN) { |
| 1527 | KeyValueStore args; |
| 1528 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1529 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1530 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 1531 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 1532 | args.SetString(flimflam::kNameProperty, "vpn-name"); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1533 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1534 | new StrictMock<MockProfile>( |
| 1535 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1536 | AdoptProfile(manager(), profile); |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 1537 | |
| 1538 | #if defined(DISABLE_VPN) |
| 1539 | |
| 1540 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1541 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 1542 | EXPECT_FALSE(service); |
| 1543 | |
| 1544 | #else |
| 1545 | |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1546 | ServiceRefPtr updated_service; |
| 1547 | EXPECT_CALL(*profile, UpdateService(_)) |
| 1548 | .WillOnce(DoAll(SaveArg<0>(&updated_service), Return(true))); |
| 1549 | ServiceRefPtr configured_service; |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1550 | EXPECT_CALL(*profile, LoadService(_)) |
| 1551 | .WillOnce(Return(false)); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1552 | EXPECT_CALL(*profile, ConfigureService(_)) |
| 1553 | .WillOnce(DoAll(SaveArg<0>(&configured_service), Return(true))); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1554 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1555 | EXPECT_TRUE(e.IsSuccess()); |
| 1556 | EXPECT_TRUE(service); |
Paul Stewart | 7f5ad57 | 2012-06-04 15:18:54 -0700 | [diff] [blame] | 1557 | EXPECT_EQ(service, updated_service); |
| 1558 | EXPECT_EQ(service, configured_service); |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 1559 | |
| 1560 | #endif // DISABLE_VPN |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1563 | TEST_F(ManagerTest, GetServiceWiMaxNoNetworkId) { |
| 1564 | KeyValueStore args; |
| 1565 | Error e; |
| 1566 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
| 1567 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1568 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 1569 | EXPECT_EQ("Missing WiMAX network id.", e.message()); |
| 1570 | EXPECT_FALSE(service); |
| 1571 | } |
| 1572 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1573 | TEST_F(ManagerTest, GetServiceWiMax) { |
| 1574 | KeyValueStore args; |
| 1575 | Error e; |
| 1576 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWimax); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 1577 | args.SetString(WiMaxService::kNetworkIdProperty, "01234567"); |
| 1578 | args.SetString(flimflam::kNameProperty, "WiMAX Network"); |
| 1579 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 1580 | EXPECT_TRUE(e.IsSuccess()); |
| 1581 | EXPECT_TRUE(service); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1584 | TEST_F(ManagerTest, ConfigureServiceWithInvalidProfile) { |
| 1585 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1586 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1587 | new NiceMock<MockProfile>( |
| 1588 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1589 | AdoptProfile(manager(), profile); |
| 1590 | |
| 1591 | KeyValueStore args; |
| 1592 | args.SetString(flimflam::kProfileProperty, "xxx"); |
| 1593 | Error error; |
| 1594 | manager()->ConfigureService(args, &error); |
| 1595 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1596 | EXPECT_EQ("Invalid profile name xxx", error.message()); |
| 1597 | } |
| 1598 | |
| 1599 | TEST_F(ManagerTest, ConfigureServiceWithGetServiceFailure) { |
| 1600 | // Manager calls ActiveProfile() so we need at least one profile installed. |
| 1601 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1602 | new NiceMock<MockProfile>( |
| 1603 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1604 | AdoptProfile(manager(), profile); |
| 1605 | |
| 1606 | KeyValueStore args; |
| 1607 | Error error; |
| 1608 | manager()->ConfigureService(args, &error); |
| 1609 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1610 | EXPECT_EQ("must specify service type", error.message()); |
| 1611 | } |
| 1612 | |
| 1613 | // A registered service in the ephemeral profile should be moved to the |
| 1614 | // active profile as a part of configuration if no profile was explicitly |
| 1615 | // specified. |
| 1616 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithoutProfile) { |
| 1617 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1618 | new NiceMock<MockProfile>( |
| 1619 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1620 | |
| 1621 | AdoptProfile(manager(), profile); // This is now the active profile. |
| 1622 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1623 | const vector<uint8_t> ssid; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1624 | scoped_refptr<MockWiFiService> service( |
| 1625 | new NiceMock<MockWiFiService>(control_interface(), |
| 1626 | dispatcher(), |
| 1627 | metrics(), |
| 1628 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1629 | wifi_provider_, |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1630 | ssid, |
| 1631 | "", |
| 1632 | "", |
| 1633 | false)); |
| 1634 | |
| 1635 | manager()->RegisterService(service); |
| 1636 | service->set_profile(GetEphemeralProfile(manager())); |
| 1637 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1638 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1639 | .WillOnce(Return(service)); |
| 1640 | EXPECT_CALL(*profile, UpdateService(ServiceRefPtr(service.get()))) |
| 1641 | .WillOnce(Return(true)); |
| 1642 | EXPECT_CALL(*profile, AdoptService(ServiceRefPtr(service.get()))) |
| 1643 | .WillOnce(Return(true)); |
| 1644 | |
| 1645 | KeyValueStore args; |
| 1646 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1647 | Error error; |
| 1648 | manager()->ConfigureService(args, &error); |
| 1649 | EXPECT_TRUE(error.IsSuccess()); |
| 1650 | } |
| 1651 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1652 | // If we configure a service that was already registered and explicitly |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1653 | // specify a profile, it should be moved from the profile it was previously |
| 1654 | // in to the specified profile if one was requested. |
| 1655 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithProfile) { |
| 1656 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1657 | new NiceMock<MockProfile>( |
| 1658 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1659 | scoped_refptr<MockProfile> profile1( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1660 | new NiceMock<MockProfile>( |
| 1661 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1662 | |
| 1663 | const string kProfileName0 = "profile0"; |
| 1664 | const string kProfileName1 = "profile1"; |
| 1665 | |
| 1666 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1667 | .WillRepeatedly(Return(kProfileName0)); |
| 1668 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1669 | .WillRepeatedly(Return(kProfileName1)); |
| 1670 | |
| 1671 | AdoptProfile(manager(), profile0); |
| 1672 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1673 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1674 | const vector<uint8_t> ssid; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1675 | scoped_refptr<MockWiFiService> service( |
| 1676 | new NiceMock<MockWiFiService>(control_interface(), |
| 1677 | dispatcher(), |
| 1678 | metrics(), |
| 1679 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1680 | wifi_provider_, |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1681 | ssid, |
| 1682 | "", |
| 1683 | "", |
| 1684 | false)); |
| 1685 | |
| 1686 | manager()->RegisterService(service); |
| 1687 | service->set_profile(profile1); |
| 1688 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1689 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1690 | .WillOnce(Return(service)); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1691 | EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get()))) |
| 1692 | .WillOnce(Return(true)); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1693 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1694 | .WillOnce(Return(true)); |
| 1695 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1696 | .WillOnce(Return(true)); |
| 1697 | EXPECT_CALL(*profile1, AbandonService(ServiceRefPtr(service.get()))) |
| 1698 | .WillOnce(Return(true)); |
| 1699 | |
| 1700 | KeyValueStore args; |
| 1701 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1702 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1703 | Error error; |
| 1704 | manager()->ConfigureService(args, &error); |
| 1705 | EXPECT_TRUE(error.IsSuccess()); |
| 1706 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1707 | } |
| 1708 | |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1709 | // If we configure a service that is already a member of the specified |
| 1710 | // profile, the Manager should not call LoadService or AdoptService again |
| 1711 | // on this service. |
| 1712 | TEST_F(ManagerTest, ConfigureRegisteredServiceWithSameProfile) { |
| 1713 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1714 | new NiceMock<MockProfile>( |
| 1715 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1716 | |
| 1717 | const string kProfileName0 = "profile0"; |
| 1718 | |
| 1719 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1720 | .WillRepeatedly(Return(kProfileName0)); |
| 1721 | |
| 1722 | AdoptProfile(manager(), profile0); // profile0 is now the ActiveProfile. |
| 1723 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1724 | const vector<uint8_t> ssid; |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1725 | scoped_refptr<MockWiFiService> service( |
| 1726 | new NiceMock<MockWiFiService>(control_interface(), |
| 1727 | dispatcher(), |
| 1728 | metrics(), |
| 1729 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1730 | wifi_provider_, |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1731 | ssid, |
| 1732 | "", |
| 1733 | "", |
| 1734 | false)); |
| 1735 | |
| 1736 | manager()->RegisterService(service); |
| 1737 | service->set_profile(profile0); |
| 1738 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1739 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
Paul Stewart | 2c575d2 | 2012-12-07 12:28:57 -0800 | [diff] [blame] | 1740 | .WillOnce(Return(service)); |
| 1741 | EXPECT_CALL(*profile0, LoadService(ServiceRefPtr(service.get()))) |
| 1742 | .Times(0); |
| 1743 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1744 | .WillOnce(Return(true)); |
| 1745 | EXPECT_CALL(*profile0, AdoptService(ServiceRefPtr(service.get()))) |
| 1746 | .Times(0); |
| 1747 | |
| 1748 | KeyValueStore args; |
| 1749 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1750 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1751 | Error error; |
| 1752 | manager()->ConfigureService(args, &error); |
| 1753 | EXPECT_TRUE(error.IsSuccess()); |
| 1754 | service->set_profile(NULL); // Breaks refcounting loop. |
| 1755 | } |
| 1756 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1757 | // An unregistered service should remain unregistered, but its contents should |
| 1758 | // be saved to the specified profile nonetheless. |
| 1759 | TEST_F(ManagerTest, ConfigureUnregisteredServiceWithProfile) { |
| 1760 | scoped_refptr<MockProfile> profile0( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1761 | new NiceMock<MockProfile>( |
| 1762 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1763 | scoped_refptr<MockProfile> profile1( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 1764 | new NiceMock<MockProfile>( |
| 1765 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1766 | |
| 1767 | const string kProfileName0 = "profile0"; |
| 1768 | const string kProfileName1 = "profile1"; |
| 1769 | |
| 1770 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 1771 | .WillRepeatedly(Return(kProfileName0)); |
| 1772 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 1773 | .WillRepeatedly(Return(kProfileName1)); |
| 1774 | |
| 1775 | AdoptProfile(manager(), profile0); |
| 1776 | AdoptProfile(manager(), profile1); // profile1 is now the ActiveProfile. |
| 1777 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1778 | const vector<uint8_t> ssid; |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1779 | scoped_refptr<MockWiFiService> service( |
| 1780 | new NiceMock<MockWiFiService>(control_interface(), |
| 1781 | dispatcher(), |
| 1782 | metrics(), |
| 1783 | manager(), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1784 | wifi_provider_, |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1785 | ssid, |
| 1786 | "", |
| 1787 | "", |
| 1788 | false)); |
| 1789 | |
| 1790 | service->set_profile(profile1); |
| 1791 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1792 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1793 | .WillOnce(Return(service)); |
| 1794 | EXPECT_CALL(*profile0, UpdateService(ServiceRefPtr(service.get()))) |
| 1795 | .WillOnce(Return(true)); |
| 1796 | EXPECT_CALL(*profile0, AdoptService(_)) |
| 1797 | .Times(0); |
| 1798 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 1799 | .Times(0); |
| 1800 | |
| 1801 | KeyValueStore args; |
| 1802 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1803 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1804 | Error error; |
| 1805 | manager()->ConfigureService(args, &error); |
| 1806 | EXPECT_TRUE(error.IsSuccess()); |
| 1807 | } |
| 1808 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1809 | TEST_F(ManagerTest, ConfigureServiceForProfileWithNoType) { |
| 1810 | KeyValueStore args; |
| 1811 | Error error; |
| 1812 | ServiceRefPtr service = |
| 1813 | manager()->ConfigureServiceForProfile("", args, &error); |
| 1814 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 1815 | EXPECT_EQ("This method only supports WiFi services", error.message()); |
| 1816 | EXPECT_EQ(NULL, service.get()); |
| 1817 | } |
| 1818 | |
| 1819 | TEST_F(ManagerTest, ConfigureServiceForProfileWithWrongType) { |
| 1820 | KeyValueStore args; |
| 1821 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeCellular); |
| 1822 | Error error; |
| 1823 | ServiceRefPtr service = |
| 1824 | manager()->ConfigureServiceForProfile("", args, &error); |
| 1825 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 1826 | EXPECT_EQ("This method only supports WiFi services", error.message()); |
| 1827 | EXPECT_EQ(NULL, service.get()); |
| 1828 | } |
| 1829 | |
| 1830 | TEST_F(ManagerTest, ConfigureServiceForProfileWithMissingProfile) { |
| 1831 | KeyValueStore args; |
| 1832 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1833 | Error error; |
| 1834 | ServiceRefPtr service = |
| 1835 | manager()->ConfigureServiceForProfile("/profile/foo", args, &error); |
| 1836 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 1837 | EXPECT_EQ("Profile specified was not found", error.message()); |
| 1838 | EXPECT_EQ(NULL, service.get()); |
| 1839 | } |
| 1840 | |
| 1841 | TEST_F(ManagerTest, ConfigureServiceForProfileWithProfileMismatch) { |
| 1842 | const string kProfileName0 = "profile0"; |
| 1843 | const string kProfileName1 = "profile1"; |
| 1844 | scoped_refptr<MockProfile> profile0( |
| 1845 | AddNamedMockProfileToManager(manager(), kProfileName0)); |
| 1846 | |
| 1847 | KeyValueStore args; |
| 1848 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1849 | args.SetString(flimflam::kProfileProperty, kProfileName1); |
| 1850 | Error error; |
| 1851 | ServiceRefPtr service = |
| 1852 | manager()->ConfigureServiceForProfile(kProfileName0, args, &error); |
| 1853 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1854 | EXPECT_EQ("Profile argument does not match that in " |
| 1855 | "the configuration arguments", error.message()); |
| 1856 | EXPECT_EQ(NULL, service.get()); |
| 1857 | } |
| 1858 | |
| 1859 | TEST_F(ManagerTest, |
| 1860 | ConfigureServiceForProfileWithNoMatchingServiceFailGetService) { |
| 1861 | const string kProfileName0 = "profile0"; |
| 1862 | scoped_refptr<MockProfile> profile0( |
| 1863 | AddNamedMockProfileToManager(manager(), kProfileName0)); |
| 1864 | KeyValueStore args; |
| 1865 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1866 | args.SetString(flimflam::kProfileProperty, kProfileName0); |
| 1867 | |
| 1868 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 1869 | .WillOnce(Return(WiFiServiceRefPtr())); |
| 1870 | EXPECT_CALL(*wifi_provider_, GetService(_, _)) |
| 1871 | .WillOnce(Return(WiFiServiceRefPtr())); |
| 1872 | Error error; |
| 1873 | ServiceRefPtr service = |
| 1874 | manager()->ConfigureServiceForProfile(kProfileName0, args, &error); |
| 1875 | // Since we didn't set the error in the GetService expectation above... |
| 1876 | EXPECT_TRUE(error.IsSuccess()); |
| 1877 | EXPECT_EQ(NULL, service.get()); |
| 1878 | } |
| 1879 | |
| 1880 | TEST_F(ManagerTest, ConfigureServiceForProfileCreateNewService) { |
| 1881 | const string kProfileName0 = "profile0"; |
| 1882 | scoped_refptr<MockProfile> profile0( |
| 1883 | AddNamedMockProfileToManager(manager(), kProfileName0)); |
| 1884 | |
| 1885 | KeyValueStore args; |
| 1886 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1887 | |
| 1888 | scoped_refptr<MockWiFiService> mock_service( |
| 1889 | new NiceMock<MockWiFiService>(control_interface(), |
| 1890 | dispatcher(), |
| 1891 | metrics(), |
| 1892 | manager(), |
| 1893 | wifi_provider_, |
| 1894 | vector<uint8_t>(), |
| 1895 | flimflam::kModeManaged, |
| 1896 | flimflam::kSecurityNone, |
| 1897 | false)); |
| 1898 | ServiceRefPtr mock_service_generic(mock_service.get()); |
| 1899 | mock_service->set_profile(profile0); |
| 1900 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 1901 | .WillOnce(Return(WiFiServiceRefPtr())); |
| 1902 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).WillOnce(Return(mock_service)); |
| 1903 | EXPECT_CALL(*profile0, UpdateService(mock_service_generic)) |
| 1904 | .WillOnce(Return(true)); |
| 1905 | Error error; |
| 1906 | ServiceRefPtr service = |
| 1907 | manager()->ConfigureServiceForProfile(kProfileName0, args, &error); |
| 1908 | EXPECT_TRUE(error.IsSuccess()); |
| 1909 | EXPECT_EQ(mock_service.get(), service.get()); |
| 1910 | mock_service->set_profile(NULL); // Breaks reference cycle. |
| 1911 | } |
| 1912 | |
| 1913 | TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceByGUID) { |
| 1914 | scoped_refptr<MockService> mock_service( |
| 1915 | new NiceMock<MockService>(control_interface(), |
| 1916 | dispatcher(), |
| 1917 | metrics(), |
| 1918 | manager())); |
| 1919 | const string kGUID = "a guid"; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1920 | mock_service->SetGuid(kGUID, NULL); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1921 | manager()->RegisterService(mock_service); |
| 1922 | ServiceRefPtr mock_service_generic(mock_service.get()); |
| 1923 | |
| 1924 | const string kProfileName = "profile"; |
| 1925 | scoped_refptr<MockProfile> profile( |
| 1926 | AddNamedMockProfileToManager(manager(), kProfileName)); |
| 1927 | mock_service->set_profile(profile); |
| 1928 | |
| 1929 | EXPECT_CALL(*mock_service, technology()) |
| 1930 | .WillOnce(Return(Technology::kCellular)) |
| 1931 | .WillOnce(Return(Technology::kWifi)); |
| 1932 | |
| 1933 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)).Times(0); |
| 1934 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0); |
| 1935 | EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0); |
| 1936 | |
| 1937 | KeyValueStore args; |
| 1938 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1939 | args.SetString(flimflam::kGuidProperty, kGUID); |
| 1940 | |
| 1941 | // The first attempt should fail because the service reports a technology |
| 1942 | // other than "WiFi". |
| 1943 | { |
| 1944 | Error error; |
| 1945 | ServiceRefPtr service = |
| 1946 | manager()->ConfigureServiceForProfile(kProfileName, args, &error); |
| 1947 | EXPECT_EQ(NULL, service.get()); |
| 1948 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 1949 | EXPECT_EQ("This GUID matches a non-WiFi service", error.message()); |
| 1950 | } |
| 1951 | |
| 1952 | EXPECT_CALL(*mock_service, Configure(_, _)).Times(1); |
| 1953 | EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1); |
| 1954 | |
| 1955 | { |
| 1956 | Error error; |
| 1957 | ServiceRefPtr service = |
| 1958 | manager()->ConfigureServiceForProfile(kProfileName, args, &error); |
| 1959 | EXPECT_TRUE(error.IsSuccess()); |
| 1960 | EXPECT_EQ(mock_service.get(), service.get()); |
| 1961 | EXPECT_EQ(profile.get(), service->profile().get()); |
| 1962 | } |
| 1963 | mock_service->set_profile(NULL); // Breaks reference cycle. |
| 1964 | } |
| 1965 | |
| 1966 | TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceAndProfile) { |
| 1967 | const string kProfileName = "profile"; |
| 1968 | scoped_refptr<MockProfile> profile( |
| 1969 | AddNamedMockProfileToManager(manager(), kProfileName)); |
| 1970 | |
| 1971 | scoped_refptr<MockWiFiService> mock_service( |
| 1972 | new NiceMock<MockWiFiService>(control_interface(), |
| 1973 | dispatcher(), |
| 1974 | metrics(), |
| 1975 | manager(), |
| 1976 | wifi_provider_, |
| 1977 | vector<uint8_t>(), |
| 1978 | flimflam::kModeManaged, |
| 1979 | flimflam::kSecurityNone, |
| 1980 | false)); |
| 1981 | mock_service->set_profile(profile); |
| 1982 | ServiceRefPtr mock_service_generic(mock_service.get()); |
| 1983 | |
| 1984 | KeyValueStore args; |
| 1985 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 1986 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 1987 | .WillOnce(Return(mock_service)); |
| 1988 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0); |
| 1989 | EXPECT_CALL(*profile, AdoptService(mock_service_generic)).Times(0); |
| 1990 | EXPECT_CALL(*mock_service, Configure(_, _)).Times(1); |
| 1991 | EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1); |
| 1992 | |
| 1993 | Error error; |
| 1994 | ServiceRefPtr service = |
| 1995 | manager()->ConfigureServiceForProfile(kProfileName, args, &error); |
| 1996 | EXPECT_TRUE(error.IsSuccess()); |
| 1997 | EXPECT_EQ(mock_service.get(), service.get()); |
| 1998 | EXPECT_EQ(profile.get(), service->profile().get()); |
| 1999 | mock_service->set_profile(NULL); // Breaks reference cycle. |
| 2000 | } |
| 2001 | |
| 2002 | TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServiceEphemeralProfile) { |
| 2003 | const string kProfileName = "profile"; |
| 2004 | scoped_refptr<MockProfile> profile( |
| 2005 | AddNamedMockProfileToManager(manager(), kProfileName)); |
| 2006 | |
| 2007 | scoped_refptr<MockWiFiService> mock_service( |
| 2008 | new NiceMock<MockWiFiService>(control_interface(), |
| 2009 | dispatcher(), |
| 2010 | metrics(), |
| 2011 | manager(), |
| 2012 | wifi_provider_, |
| 2013 | vector<uint8_t>(), |
| 2014 | flimflam::kModeManaged, |
| 2015 | flimflam::kSecurityNone, |
| 2016 | false)); |
| 2017 | mock_service->set_profile(GetEphemeralProfile(manager())); |
| 2018 | ServiceRefPtr mock_service_generic(mock_service.get()); |
| 2019 | |
| 2020 | KeyValueStore args; |
| 2021 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 2022 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 2023 | .WillOnce(Return(mock_service)); |
| 2024 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0); |
| 2025 | EXPECT_CALL(*mock_service, Configure(_, _)).Times(1); |
| 2026 | EXPECT_CALL(*profile, UpdateService(mock_service_generic)).Times(1); |
| 2027 | |
| 2028 | Error error; |
| 2029 | ServiceRefPtr service = |
| 2030 | manager()->ConfigureServiceForProfile(kProfileName, args, &error); |
| 2031 | EXPECT_TRUE(error.IsSuccess()); |
| 2032 | EXPECT_EQ(mock_service.get(), service.get()); |
| 2033 | EXPECT_EQ(profile.get(), service->profile().get()); |
| 2034 | mock_service->set_profile(NULL); // Breaks reference cycle. |
| 2035 | } |
| 2036 | |
| 2037 | TEST_F(ManagerTest, ConfigureServiceForProfileMatchingServicePrecedingProfile) { |
| 2038 | const string kProfileName0 = "profile0"; |
| 2039 | scoped_refptr<MockProfile> profile0( |
| 2040 | AddNamedMockProfileToManager(manager(), kProfileName0)); |
| 2041 | const string kProfileName1 = "profile1"; |
| 2042 | scoped_refptr<MockProfile> profile1( |
| 2043 | AddNamedMockProfileToManager(manager(), kProfileName1)); |
| 2044 | |
| 2045 | scoped_refptr<MockWiFiService> mock_service( |
| 2046 | new NiceMock<MockWiFiService>(control_interface(), |
| 2047 | dispatcher(), |
| 2048 | metrics(), |
| 2049 | manager(), |
| 2050 | wifi_provider_, |
| 2051 | vector<uint8_t>(), |
| 2052 | flimflam::kModeManaged, |
| 2053 | flimflam::kSecurityNone, |
| 2054 | false)); |
| 2055 | manager()->RegisterService(mock_service); |
| 2056 | mock_service->set_profile(profile0); |
| 2057 | ServiceRefPtr mock_service_generic(mock_service.get()); |
| 2058 | |
| 2059 | KeyValueStore args; |
| 2060 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 2061 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 2062 | .WillOnce(Return(mock_service)); |
| 2063 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0); |
| 2064 | EXPECT_CALL(*profile0, AbandonService(_)).Times(0); |
| 2065 | EXPECT_CALL(*profile1, AdoptService(_)).Times(0); |
| 2066 | // This happens once to make the service loadable for the ConfigureService |
| 2067 | // below, and a second time after the service is modified. |
| 2068 | EXPECT_CALL(*profile1, ConfigureService(mock_service_generic)).Times(0); |
| 2069 | EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)).Times(0); |
| 2070 | EXPECT_CALL(*mock_service, Configure(_, _)).Times(1); |
| 2071 | EXPECT_CALL(*profile1, UpdateService(mock_service_generic)).Times(1); |
| 2072 | |
| 2073 | Error error; |
| 2074 | ServiceRefPtr service = |
| 2075 | manager()->ConfigureServiceForProfile(kProfileName1, args, &error); |
| 2076 | EXPECT_TRUE(error.IsSuccess()); |
| 2077 | EXPECT_EQ(mock_service.get(), service.get()); |
| 2078 | mock_service->set_profile(NULL); // Breaks reference cycle. |
| 2079 | } |
| 2080 | |
| 2081 | TEST_F(ManagerTest, |
| 2082 | ConfigureServiceForProfileMatchingServiceProceedingProfile) { |
| 2083 | const string kProfileName0 = "profile0"; |
| 2084 | scoped_refptr<MockProfile> profile0( |
| 2085 | AddNamedMockProfileToManager(manager(), kProfileName0)); |
| 2086 | const string kProfileName1 = "profile1"; |
| 2087 | scoped_refptr<MockProfile> profile1( |
| 2088 | AddNamedMockProfileToManager(manager(), kProfileName1)); |
| 2089 | |
| 2090 | scoped_refptr<MockWiFiService> matching_service( |
| 2091 | new StrictMock<MockWiFiService>(control_interface(), |
| 2092 | dispatcher(), |
| 2093 | metrics(), |
| 2094 | manager(), |
| 2095 | wifi_provider_, |
| 2096 | vector<uint8_t>(), |
| 2097 | flimflam::kModeManaged, |
| 2098 | flimflam::kSecurityNone, |
| 2099 | false)); |
| 2100 | matching_service->set_profile(profile1); |
| 2101 | |
| 2102 | // We need to get rid of our reference to this mock service as soon |
| 2103 | // as Manager::ConfigureServiceForProfile() takes a reference in its |
| 2104 | // call to WiFiProvider::CreateTemporaryService(). This way the |
| 2105 | // latter function can keep a DCHECK(service->HasOneRef() even in |
| 2106 | // unit tests. |
| 2107 | temp_mock_service_ = |
| 2108 | new NiceMock<MockWiFiService>(control_interface(), |
| 2109 | dispatcher(), |
| 2110 | metrics(), |
| 2111 | manager(), |
| 2112 | wifi_provider_, |
| 2113 | vector<uint8_t>(), |
| 2114 | flimflam::kModeManaged, |
| 2115 | flimflam::kSecurityNone, |
| 2116 | false); |
| 2117 | |
| 2118 | // Only hold a pointer here so we don't affect the refcount. |
| 2119 | MockWiFiService *mock_service_ptr = temp_mock_service_.get(); |
| 2120 | |
| 2121 | KeyValueStore args; |
| 2122 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 2123 | EXPECT_CALL(*wifi_provider_, FindSimilarService(_, _)) |
| 2124 | .WillOnce(Return(matching_service)); |
| 2125 | EXPECT_CALL(*wifi_provider_, GetService(_, _)).Times(0); |
| 2126 | EXPECT_CALL(*profile1, AbandonService(_)).Times(0); |
| 2127 | EXPECT_CALL(*profile0, AdoptService(_)).Times(0); |
| 2128 | EXPECT_CALL(*wifi_provider_, CreateTemporaryService(_, _)) |
| 2129 | .WillOnce(InvokeWithoutArgs(this, &ManagerTest::ReleaseTempMockService)); |
| 2130 | EXPECT_CALL(*profile0, ConfigureService(IsRefPtrTo(mock_service_ptr))) |
| 2131 | .Times(1); |
| 2132 | EXPECT_CALL(*mock_service_ptr, Configure(_, _)).Times(1); |
| 2133 | EXPECT_CALL(*profile0, UpdateService(IsRefPtrTo(mock_service_ptr))).Times(1); |
| 2134 | |
| 2135 | Error error; |
| 2136 | ServiceRefPtr service = |
| 2137 | manager()->ConfigureServiceForProfile(kProfileName0, args, &error); |
| 2138 | EXPECT_TRUE(error.IsSuccess()); |
| 2139 | EXPECT_EQ(NULL, service.get()); |
| 2140 | EXPECT_EQ(profile1.get(), matching_service->profile().get()); |
| 2141 | } |
| 2142 | |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 2143 | TEST_F(ManagerTest, FindMatchingService) { |
| 2144 | KeyValueStore args; |
| 2145 | { |
| 2146 | Error error; |
| 2147 | ServiceRefPtr service = manager()->FindMatchingService(args, &error); |
| 2148 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 2149 | } |
| 2150 | |
| 2151 | scoped_refptr<MockService> mock_service0( |
| 2152 | new NiceMock<MockService>(control_interface(), |
| 2153 | dispatcher(), |
| 2154 | metrics(), |
| 2155 | manager())); |
| 2156 | scoped_refptr<MockService> mock_service1( |
| 2157 | new NiceMock<MockService>(control_interface(), |
| 2158 | dispatcher(), |
| 2159 | metrics(), |
| 2160 | manager())); |
| 2161 | manager()->RegisterService(mock_service0); |
| 2162 | manager()->RegisterService(mock_service1); |
| 2163 | EXPECT_CALL(*mock_service0, DoPropertiesMatch(_)) |
| 2164 | .WillOnce(Return(true)) |
| 2165 | .WillRepeatedly(Return(false)); |
| 2166 | { |
| 2167 | Error error; |
| 2168 | EXPECT_EQ(mock_service0, manager()->FindMatchingService(args, &error)); |
| 2169 | EXPECT_TRUE(error.IsSuccess()); |
| 2170 | } |
| 2171 | EXPECT_CALL(*mock_service1, DoPropertiesMatch(_)) |
| 2172 | .WillOnce(Return(true)) |
| 2173 | .WillRepeatedly(Return(false)); |
| 2174 | { |
| 2175 | Error error; |
| 2176 | EXPECT_EQ(mock_service1, manager()->FindMatchingService(args, &error)); |
| 2177 | EXPECT_TRUE(error.IsSuccess()); |
| 2178 | } |
| 2179 | { |
| 2180 | Error error; |
| 2181 | EXPECT_FALSE(manager()->FindMatchingService(args, &error)); |
| 2182 | EXPECT_EQ(Error::kNotFound, error.type()); |
| 2183 | } |
| 2184 | } |
| 2185 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2186 | TEST_F(ManagerTest, TechnologyOrder) { |
| 2187 | Error error; |
| 2188 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 2189 | string(flimflam::kTypeWifi), &error); |
| 2190 | ASSERT_TRUE(error.IsSuccess()); |
| 2191 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 2192 | string(flimflam::kTypeEthernet) + "," + |
| 2193 | string(flimflam::kTypeWifi)); |
| 2194 | |
| 2195 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 2196 | string(flimflam::kTypeWifi), &error); |
| 2197 | ASSERT_FALSE(error.IsSuccess()); |
| 2198 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 2199 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 2200 | string(flimflam::kTypeWifi), |
| 2201 | manager()->GetTechnologyOrder()); |
| 2202 | } |
| 2203 | |
| 2204 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2205 | // TODO(quiche): Some of these tests would probably fit better in |
| 2206 | // service_unittest, since the actual comparison of Services is |
| 2207 | // implemented in Service. (crosbug.com/23370) |
| 2208 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2209 | scoped_refptr<MockService> mock_service0( |
| 2210 | new NiceMock<MockService>(control_interface(), |
| 2211 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2212 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2213 | manager())); |
| 2214 | scoped_refptr<MockService> mock_service1( |
| 2215 | new NiceMock<MockService>(control_interface(), |
| 2216 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2217 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2218 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2219 | |
| 2220 | manager()->RegisterService(mock_service0); |
| 2221 | manager()->RegisterService(mock_service1); |
| 2222 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 2223 | // Services should already be sorted by |unique_name_| |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2224 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2225 | |
| 2226 | // Asking explictly to sort services should not change anything |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2227 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2228 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2229 | |
| 2230 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 2231 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2232 | manager()->UpdateService(mock_service1); |
| 2233 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2234 | |
| 2235 | // Security |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 2236 | mock_service0->SetSecurity(Service::kCryptoAes, true, true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2237 | manager()->UpdateService(mock_service0); |
| 2238 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2239 | |
| 2240 | // Technology |
Joshua Kroll | 053fa82 | 2012-06-05 09:50:43 -0700 | [diff] [blame] | 2241 | EXPECT_CALL(*mock_service0.get(), technology()) |
| 2242 | .WillRepeatedly(Return((Technology::kWifi))); |
| 2243 | EXPECT_CALL(*mock_service1.get(), technology()) |
| 2244 | .WillRepeatedly(Return(Technology::kEthernet)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2245 | |
| 2246 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 2247 | // Default technology ordering should favor Ethernet over WiFi. |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2248 | manager()->SortServicesTask(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2249 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2250 | |
| 2251 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 2252 | string(flimflam::kTypeEthernet), &error); |
| 2253 | EXPECT_TRUE(error.IsSuccess()); |
| 2254 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2255 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2256 | // Priority. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2257 | mock_service0->SetPriority(1, NULL); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2258 | manager()->UpdateService(mock_service0); |
| 2259 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2260 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2261 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2262 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2263 | manager()->UpdateService(mock_service1); |
| 2264 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2265 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2266 | // Auto-connect. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2267 | mock_service0->SetAutoConnect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2268 | manager()->UpdateService(mock_service0); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2269 | mock_service1->SetAutoConnect(false); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2270 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2271 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2272 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 2273 | // Test is-dependent-on. It doesn't make sense to have this ranking compare |
| 2274 | // to any of the others below, so we reset to the default state after |
| 2275 | // testing. |
| 2276 | EXPECT_CALL(*mock_service1.get(), |
| 2277 | IsDependentOn(ServiceRefPtr(mock_service0.get()))) |
| 2278 | .WillOnce(Return(true)) |
| 2279 | .WillRepeatedly(Return(false)); |
| 2280 | manager()->UpdateService(mock_service1); |
| 2281 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2282 | manager()->UpdateService(mock_service0); |
| 2283 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2284 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2285 | // Connectable. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2286 | mock_service1->SetConnectable(true); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2287 | manager()->UpdateService(mock_service1); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2288 | mock_service0->SetConnectable(false); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2289 | manager()->UpdateService(mock_service0); |
| 2290 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2291 | |
| 2292 | // IsFailed. |
| 2293 | EXPECT_CALL(*mock_service0.get(), state()) |
| 2294 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 2295 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 2296 | .WillRepeatedly(Return(false)); |
| 2297 | manager()->UpdateService(mock_service0); |
| 2298 | EXPECT_CALL(*mock_service0.get(), state()) |
| 2299 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 2300 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 2301 | .WillRepeatedly(Return(true)); |
| 2302 | manager()->UpdateService(mock_service1); |
| 2303 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2304 | |
| 2305 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2306 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2307 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 2308 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2309 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2310 | manager()->UpdateService(mock_service1); |
| 2311 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2312 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2313 | // Connected. |
| 2314 | EXPECT_CALL(*mock_service0.get(), state()) |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 2315 | .WillRepeatedly(Return(Service::kStatePortal)); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2316 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 2317 | .WillRepeatedly(Return(true)); |
| 2318 | manager()->UpdateService(mock_service0); |
| 2319 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 2320 | |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 2321 | // Portal. |
| 2322 | EXPECT_CALL(*mock_service1.get(), state()) |
| 2323 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2324 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 2325 | .WillRepeatedly(Return(true)); |
| 2326 | manager()->UpdateService(mock_service1); |
| 2327 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 2328 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 2329 | manager()->DeregisterService(mock_service0); |
| 2330 | manager()->DeregisterService(mock_service1); |
| 2331 | } |
| 2332 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2333 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 2334 | MockMetrics mock_metrics(dispatcher()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 2335 | SetMetrics(&mock_metrics); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 2336 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2337 | scoped_refptr<MockService> mock_service0( |
| 2338 | new NiceMock<MockService>(control_interface(), |
| 2339 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2340 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2341 | manager())); |
| 2342 | scoped_refptr<MockService> mock_service1( |
| 2343 | new NiceMock<MockService>(control_interface(), |
| 2344 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2345 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2346 | manager())); |
| 2347 | |
| 2348 | scoped_refptr<MockConnection> mock_connection0( |
| 2349 | new NiceMock<MockConnection>(device_info_.get())); |
| 2350 | scoped_refptr<MockConnection> mock_connection1( |
| 2351 | new NiceMock<MockConnection>(device_info_.get())); |
| 2352 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2353 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2354 | manager()->RegisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2355 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2356 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2357 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2358 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2359 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2360 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2361 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2362 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2363 | mock_service1->SetPriority(1, NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2364 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2365 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2366 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2367 | mock_service1->SetPriority(0, NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2368 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2369 | manager()->SortServicesTask(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2370 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2371 | mock_service0->set_mock_connection(mock_connection0); |
| 2372 | mock_service1->set_mock_connection(mock_connection1); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2373 | |
| 2374 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 2375 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2376 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2377 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2378 | ServiceWatcher service_watcher; |
| 2379 | int tag = |
| 2380 | manager()->RegisterDefaultServiceCallback( |
| 2381 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 2382 | service_watcher.AsWeakPtr())); |
| 2383 | EXPECT_EQ(1, tag); |
| 2384 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2385 | mock_service1->SetPriority(1, NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2386 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 2387 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2388 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 2389 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2390 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2391 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2392 | manager()->DeregisterDefaultServiceCallback(tag); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2393 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2394 | EXPECT_CALL(service_watcher, OnDefaultServiceChanged(_)).Times(0); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 2395 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2396 | mock_service1->set_mock_connection(NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2397 | manager()->DeregisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2398 | CompleteServiceSort(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2399 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2400 | mock_service0->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2401 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2402 | manager()->DeregisterService(mock_service0); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2403 | CompleteServiceSort(); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2404 | |
| 2405 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2406 | manager()->SortServicesTask(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2409 | TEST_F(ManagerTest, NotifyDefaultServiceChanged) { |
| 2410 | EXPECT_EQ(0, manager()->default_service_callback_tag_); |
| 2411 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 2412 | |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 2413 | MockMetrics mock_metrics(dispatcher()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 2414 | SetMetrics(&mock_metrics); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 2415 | |
| 2416 | scoped_refptr<MockService> mock_service( |
| 2417 | new NiceMock<MockService>( |
| 2418 | control_interface(), dispatcher(), metrics(), manager())); |
| 2419 | ServiceRefPtr service = mock_service; |
| 2420 | ServiceRefPtr null_service; |
| 2421 | |
| 2422 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 2423 | manager()->NotifyDefaultServiceChanged(null_service); |
| 2424 | |
| 2425 | ServiceWatcher service_watcher1; |
| 2426 | ServiceWatcher service_watcher2; |
| 2427 | int tag1 = |
| 2428 | manager()->RegisterDefaultServiceCallback( |
| 2429 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 2430 | service_watcher1.AsWeakPtr())); |
| 2431 | EXPECT_EQ(1, tag1); |
| 2432 | int tag2 = |
| 2433 | manager()->RegisterDefaultServiceCallback( |
| 2434 | Bind(&ServiceWatcher::OnDefaultServiceChanged, |
| 2435 | service_watcher2.AsWeakPtr())); |
| 2436 | EXPECT_EQ(2, tag2); |
| 2437 | |
| 2438 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(null_service)); |
| 2439 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(null_service)); |
| 2440 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 2441 | manager()->NotifyDefaultServiceChanged(null_service); |
| 2442 | |
| 2443 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(service)); |
| 2444 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 2445 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 2446 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 2447 | |
| 2448 | manager()->DeregisterDefaultServiceCallback(tag1); |
| 2449 | EXPECT_CALL(service_watcher1, OnDefaultServiceChanged(_)).Times(0); |
| 2450 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(service)); |
| 2451 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 2452 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 2453 | EXPECT_EQ(1, manager()->default_service_callbacks_.size()); |
| 2454 | |
| 2455 | manager()->DeregisterDefaultServiceCallback(tag2); |
| 2456 | EXPECT_CALL(service_watcher2, OnDefaultServiceChanged(_)).Times(0); |
| 2457 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(service.get())); |
| 2458 | manager()->NotifyDefaultServiceChanged(mock_service); |
| 2459 | |
| 2460 | EXPECT_EQ(2, manager()->default_service_callback_tag_); |
| 2461 | EXPECT_TRUE(manager()->default_service_callbacks_.empty()); |
| 2462 | } |
| 2463 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2464 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 2465 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 2466 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2467 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2468 | manager(), |
| 2469 | "null4", |
| 2470 | "addr4", |
| 2471 | 0)); |
| 2472 | manager()->RegisterDevice(mock_devices_[0]); |
| 2473 | manager()->RegisterDevice(mock_devices_[1]); |
| 2474 | manager()->RegisterDevice(mock_devices_[2]); |
| 2475 | manager()->RegisterDevice(mock_devices_[3]); |
| 2476 | |
| 2477 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 2478 | .WillByDefault(Return(Technology::kEthernet)); |
| 2479 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 2480 | .WillByDefault(Return(Technology::kWifi)); |
| 2481 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 2482 | .WillByDefault(Return(Technology::kCellular)); |
| 2483 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 2484 | .WillByDefault(Return(Technology::kWifi)); |
| 2485 | |
| 2486 | set<string> expected_technologies; |
| 2487 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2488 | Technology::kEthernet)); |
| 2489 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2490 | Technology::kWifi)); |
| 2491 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2492 | Technology::kCellular)); |
| 2493 | Error error; |
| 2494 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 2495 | |
| 2496 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 2497 | ContainerEq(expected_technologies)); |
| 2498 | } |
| 2499 | |
| 2500 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 2501 | scoped_refptr<MockService> connected_service1( |
| 2502 | new NiceMock<MockService>(control_interface(), |
| 2503 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2504 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2505 | manager())); |
| 2506 | scoped_refptr<MockService> connected_service2( |
| 2507 | new NiceMock<MockService>(control_interface(), |
| 2508 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2509 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2510 | manager())); |
| 2511 | scoped_refptr<MockService> disconnected_service1( |
| 2512 | new NiceMock<MockService>(control_interface(), |
| 2513 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2514 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2515 | manager())); |
| 2516 | scoped_refptr<MockService> disconnected_service2( |
| 2517 | new NiceMock<MockService>(control_interface(), |
| 2518 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2519 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2520 | manager())); |
| 2521 | |
| 2522 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 2523 | .WillByDefault(Return(true)); |
| 2524 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 2525 | .WillByDefault(Return(true)); |
| 2526 | |
| 2527 | manager()->RegisterService(connected_service1); |
| 2528 | manager()->RegisterService(connected_service2); |
| 2529 | manager()->RegisterService(disconnected_service1); |
| 2530 | manager()->RegisterService(disconnected_service2); |
| 2531 | |
| 2532 | manager()->RegisterDevice(mock_devices_[0]); |
| 2533 | manager()->RegisterDevice(mock_devices_[1]); |
| 2534 | manager()->RegisterDevice(mock_devices_[2]); |
| 2535 | manager()->RegisterDevice(mock_devices_[3]); |
| 2536 | |
| 2537 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 2538 | .WillByDefault(Return(Technology::kEthernet)); |
| 2539 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 2540 | .WillByDefault(Return(Technology::kWifi)); |
| 2541 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 2542 | .WillByDefault(Return(Technology::kCellular)); |
| 2543 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 2544 | .WillByDefault(Return(Technology::kWifi)); |
| 2545 | |
| 2546 | mock_devices_[0]->SelectService(connected_service1); |
| 2547 | mock_devices_[1]->SelectService(disconnected_service1); |
| 2548 | mock_devices_[2]->SelectService(disconnected_service2); |
| 2549 | mock_devices_[3]->SelectService(connected_service2); |
| 2550 | |
| 2551 | set<string> expected_technologies; |
| 2552 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2553 | Technology::kEthernet)); |
| 2554 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 2555 | Technology::kWifi)); |
| 2556 | Error error; |
| 2557 | |
| 2558 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 2559 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 2560 | ContainerEq(expected_technologies)); |
| 2561 | } |
| 2562 | |
| 2563 | TEST_F(ManagerTest, DefaultTechnology) { |
| 2564 | scoped_refptr<MockService> connected_service( |
| 2565 | new NiceMock<MockService>(control_interface(), |
| 2566 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2567 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2568 | manager())); |
| 2569 | scoped_refptr<MockService> disconnected_service( |
| 2570 | new NiceMock<MockService>(control_interface(), |
| 2571 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2572 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2573 | manager())); |
| 2574 | |
| 2575 | // Connected. WiFi. |
| 2576 | ON_CALL(*connected_service.get(), IsConnected()) |
| 2577 | .WillByDefault(Return(true)); |
| 2578 | ON_CALL(*connected_service.get(), state()) |
| 2579 | .WillByDefault(Return(Service::kStateConnected)); |
| 2580 | ON_CALL(*connected_service.get(), technology()) |
| 2581 | .WillByDefault(Return(Technology::kWifi)); |
| 2582 | |
| 2583 | // Disconnected. Ethernet. |
| 2584 | ON_CALL(*disconnected_service.get(), technology()) |
| 2585 | .WillByDefault(Return(Technology::kEthernet)); |
| 2586 | |
| 2587 | manager()->RegisterService(disconnected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2588 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2589 | Error error; |
| 2590 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 2591 | |
| 2592 | |
| 2593 | manager()->RegisterService(connected_service); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 2594 | CompleteServiceSort(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2595 | // Connected service should be brought to the front now. |
| 2596 | string expected_technology = |
| 2597 | Technology::NameFromIdentifier(Technology::kWifi); |
| 2598 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 2599 | } |
| 2600 | |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2601 | TEST_F(ManagerTest, Stop) { |
| 2602 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 2603 | new NiceMock<MockProfile>( |
| 2604 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2605 | AdoptProfile(manager(), profile); |
| 2606 | scoped_refptr<MockService> service( |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2607 | new NiceMock<MockService>(control_interface(), |
| 2608 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2609 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2610 | manager())); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2611 | manager()->RegisterService(service); |
| 2612 | manager()->RegisterDevice(mock_devices_[0]); |
| 2613 | EXPECT_CALL(*profile.get(), |
| 2614 | UpdateDevice(DeviceRefPtr(mock_devices_[0].get()))) |
| 2615 | .WillOnce(Return(true)); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 2616 | EXPECT_CALL(*profile.get(), UpdateWiFiProvider(_)).WillOnce(Return(true)); |
Paul Stewart | 212d60f | 2012-07-12 10:59:13 -0700 | [diff] [blame] | 2617 | EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true)); |
| 2618 | EXPECT_CALL(*service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 2619 | manager()->Stop(); |
| 2620 | } |
| 2621 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2622 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 2623 | scoped_refptr<MockService> mock_service( |
| 2624 | new NiceMock<MockService>(control_interface(), |
| 2625 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2626 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2627 | manager())); |
| 2628 | manager()->RegisterService(mock_service); |
| 2629 | EXPECT_FALSE(mock_service->favorite()); |
| 2630 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2631 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 2632 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 2633 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 2634 | manager()->UpdateService(mock_service); |
| 2635 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2636 | // to mock out MakeFavorite. And mocking that out would break the |
| 2637 | // SortServices test. (crosbug.com/23370) |
| 2638 | EXPECT_TRUE(mock_service->favorite()); |
| 2639 | EXPECT_TRUE(mock_service->auto_connect()); |
| 2640 | } |
| 2641 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2642 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 2643 | // This tests the case where the user connects to a service that is |
| 2644 | // currently associated with a profile. We want to make sure that the |
| 2645 | // favorite flag is set and that the flag is saved to the current |
| 2646 | // profile. |
| 2647 | scoped_refptr<MockService> mock_service( |
| 2648 | new NiceMock<MockService>(control_interface(), |
| 2649 | dispatcher(), |
| 2650 | metrics(), |
| 2651 | manager())); |
| 2652 | manager()->RegisterService(mock_service); |
| 2653 | EXPECT_FALSE(mock_service->favorite()); |
| 2654 | EXPECT_FALSE(mock_service->auto_connect()); |
| 2655 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2656 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 2657 | new MockProfile( |
| 2658 | control_interface(), metrics(), manager(), "")); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2659 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2660 | mock_service->set_profile(profile); |
| 2661 | EXPECT_CALL(*mock_service, IsConnected()) |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2662 | .WillRepeatedly(Return(true)); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2663 | EXPECT_CALL(*profile, |
| 2664 | UpdateService(static_cast<ServiceRefPtr>(mock_service))); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2665 | manager()->UpdateService(mock_service); |
| 2666 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 2667 | // to mock out MakeFavorite. And mocking that out would break the |
| 2668 | // SortServices test. (crosbug.com/23370) |
| 2669 | EXPECT_TRUE(mock_service->favorite()); |
| 2670 | EXPECT_TRUE(mock_service->auto_connect()); |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 2671 | // This releases the ref on the mock profile. |
| 2672 | mock_service->set_profile(NULL); |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 2673 | } |
| 2674 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2675 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 2676 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 2677 | new StrictMock<MockProfile>( |
| 2678 | control_interface(), metrics(), manager(), "")); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2679 | AdoptProfile(manager(), profile); |
| 2680 | scoped_refptr<MockService> service( |
| 2681 | new NiceMock<MockService>(control_interface(), |
| 2682 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 2683 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 2684 | manager())); |
| 2685 | |
| 2686 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 2687 | ServiceRefPtr expect_service(service.get()); |
| 2688 | |
| 2689 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 2690 | .WillOnce(Return(false)); |
| 2691 | manager()->RegisterService(service); |
| 2692 | |
| 2693 | EXPECT_CALL(*service.get(), state()) |
| 2694 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 2695 | EXPECT_CALL(*service.get(), IsConnected()) |
| 2696 | .WillRepeatedly(Return(true)); |
| 2697 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 2698 | .WillOnce(Return(true)); |
| 2699 | manager()->UpdateService(service); |
| 2700 | } |
| 2701 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 2702 | TEST_F(ManagerTest, UpdateDevice) { |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 2703 | MockProfile *profile0 = |
| 2704 | new MockProfile(control_interface(), metrics(), manager(), ""); |
| 2705 | MockProfile *profile1 = |
| 2706 | new MockProfile(control_interface(), metrics(), manager(), ""); |
| 2707 | MockProfile *profile2 = |
| 2708 | new MockProfile(control_interface(), metrics(), manager(), ""); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 2709 | AdoptProfile(manager(), profile0); // Passes ownership. |
| 2710 | AdoptProfile(manager(), profile1); // Passes ownership. |
| 2711 | AdoptProfile(manager(), profile2); // Passes ownership. |
| 2712 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 2713 | EXPECT_CALL(*profile0, UpdateDevice(device_ref)).Times(0); |
| 2714 | EXPECT_CALL(*profile1, UpdateDevice(device_ref)).WillOnce(Return(true)); |
| 2715 | EXPECT_CALL(*profile2, UpdateDevice(device_ref)).WillOnce(Return(false)); |
| 2716 | manager()->UpdateDevice(mock_devices_[0]); |
| 2717 | } |
| 2718 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2719 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 2720 | vector<string> profile_paths; |
| 2721 | for (size_t i = 0; i < 10; i++) { |
| 2722 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 2723 | new StrictMock<MockProfile>( |
| 2724 | control_interface(), metrics(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 2725 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 2726 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 2727 | .WillOnce(Return(profile_paths.back())); |
| 2728 | AdoptProfile(manager(), profile); |
| 2729 | } |
| 2730 | |
| 2731 | Error error; |
| 2732 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 2733 | EXPECT_TRUE(error.IsSuccess()); |
| 2734 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 2735 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 2736 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 2737 | } |
| 2738 | } |
| 2739 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2740 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 2741 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2742 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 2743 | manager()->RegisterService(service); |
| 2744 | dispatcher()->DispatchPendingEvents(); |
| 2745 | } |
| 2746 | |
| 2747 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 2748 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2749 | service1->SetPriority(1, NULL); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2750 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2751 | service2->SetPriority(2, NULL); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2752 | manager()->RegisterService(service1); |
| 2753 | manager()->RegisterService(service2); |
| 2754 | dispatcher()->DispatchPendingEvents(); |
| 2755 | |
| 2756 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2757 | EXPECT_CALL(*service2.get(), state()) |
| 2758 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 2759 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 2760 | .WillRepeatedly(Return(true)); |
| 2761 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 2762 | .WillRepeatedly(Return(false)); |
| 2763 | manager()->UpdateService(service2); |
| 2764 | dispatcher()->DispatchPendingEvents(); |
| 2765 | } |
| 2766 | |
| 2767 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 2768 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2769 | service1->SetPriority(1, NULL); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2770 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 2771 | service2->SetPriority(2, NULL); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 2772 | manager()->RegisterService(service1); |
| 2773 | manager()->RegisterService(service2); |
| 2774 | dispatcher()->DispatchPendingEvents(); |
| 2775 | |
| 2776 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 2777 | manager()->DeregisterService(service2); |
| 2778 | dispatcher()->DispatchPendingEvents(); |
| 2779 | } |
| 2780 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2781 | TEST_F(ManagerTest, AutoConnectOnPowerStateSuspending) { |
| 2782 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2783 | SetPowerState(PowerManagerProxyDelegate::kSuspending); |
| 2784 | SetPowerManager(); |
| 2785 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2786 | manager()->RegisterService(service); |
| 2787 | dispatcher()->DispatchPendingEvents(); |
| 2788 | } |
| 2789 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2790 | TEST_F(ManagerTest, AutoConnectOnPowerStateMem) { |
| 2791 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2792 | SetPowerState(PowerManagerProxyDelegate::kMem); |
| 2793 | SetPowerManager(); |
| 2794 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2795 | manager()->RegisterService(service); |
| 2796 | dispatcher()->DispatchPendingEvents(); |
| 2797 | } |
| 2798 | |
| 2799 | TEST_F(ManagerTest, AutoConnectOnPowerStateOn) { |
| 2800 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2801 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2802 | SetPowerManager(); |
| 2803 | EXPECT_CALL(*service, AutoConnect()); |
| 2804 | manager()->RegisterService(service); |
| 2805 | dispatcher()->DispatchPendingEvents(); |
| 2806 | } |
| 2807 | |
| 2808 | TEST_F(ManagerTest, AutoConnectOnPowerStateUnknown) { |
| 2809 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2810 | SetPowerState(PowerManagerProxyDelegate::kUnknown); |
| 2811 | SetPowerManager(); |
| 2812 | EXPECT_CALL(*service, AutoConnect()); |
| 2813 | manager()->RegisterService(service); |
| 2814 | dispatcher()->DispatchPendingEvents(); |
| 2815 | } |
| 2816 | |
Paul Stewart | 63864b6 | 2012-11-07 15:10:55 -0800 | [diff] [blame] | 2817 | TEST_F(ManagerTest, AutoConnectWhileNotRunning) { |
| 2818 | SetRunning(false); |
| 2819 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2820 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2821 | manager()->RegisterService(service); |
| 2822 | dispatcher()->DispatchPendingEvents(); |
| 2823 | } |
| 2824 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2825 | TEST_F(ManagerTest, OnPowerStateChanged) { |
| 2826 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 2827 | SetPowerState(PowerManagerProxyDelegate::kOn); |
| 2828 | SetPowerManager(); |
| 2829 | EXPECT_CALL(*service, AutoConnect()); |
| 2830 | manager()->RegisterService(service); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2831 | manager()->RegisterDevice(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2832 | dispatcher()->DispatchPendingEvents(); |
| 2833 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2834 | EXPECT_CALL(*mock_devices_[0], OnAfterResume()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2835 | OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 2836 | EXPECT_CALL(*service, AutoConnect()); |
| 2837 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2838 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2839 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2840 | EXPECT_CALL(*mock_devices_[0], OnBeforeSuspend()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2841 | OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 2842 | EXPECT_CALL(*service, AutoConnect()).Times(0); |
| 2843 | dispatcher()->DispatchPendingEvents(); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 2844 | Mock::VerifyAndClearExpectations(mock_devices_[0]); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2847 | TEST_F(ManagerTest, AddTerminationAction) { |
| 2848 | EXPECT_CALL(*power_manager_, AddSuspendDelayCallback(_, _)); |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 2849 | EXPECT_CALL(*power_manager_, RegisterSuspendDelay(_, _, _)); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2850 | SetPowerManager(); |
| 2851 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2852 | manager()->AddTerminationAction("action1", base::Closure()); |
| 2853 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2854 | manager()->AddTerminationAction("action2", base::Closure()); |
| 2855 | } |
| 2856 | |
| 2857 | TEST_F(ManagerTest, RemoveTerminationAction) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2858 | const char kKey1[] = "action1"; |
| 2859 | const char kKey2[] = "action2"; |
| 2860 | const int kSuspendDelayId = 123; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2861 | |
| 2862 | MockPowerManager &power_manager = *power_manager_; |
| 2863 | SetPowerManager(); |
| 2864 | |
| 2865 | // Removing an action when the hook table is empty should not result in any |
| 2866 | // calls to the power manager. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2867 | EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2868 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2869 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2870 | manager()->RemoveTerminationAction("unknown"); |
| 2871 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2872 | |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 2873 | EXPECT_CALL(power_manager, RegisterSuspendDelay(_, _, _)) |
| 2874 | .WillOnce(DoAll(SetArgumentPointee<2>(kSuspendDelayId), Return(true))); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2875 | EXPECT_CALL(power_manager, AddSuspendDelayCallback(_, _)).Times(1); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2876 | manager()->AddTerminationAction(kKey1, base::Closure()); |
| 2877 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2878 | manager()->AddTerminationAction(kKey2, base::Closure()); |
| 2879 | |
| 2880 | // Removing an action that ends up with a non-empty hook table should not |
| 2881 | // result in any calls to the power manager. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2882 | EXPECT_CALL(power_manager, UnregisterSuspendDelay(_)).Times(0); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2883 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)).Times(0); |
| 2884 | manager()->RemoveTerminationAction(kKey1); |
| 2885 | EXPECT_FALSE(GetTerminationActions()->IsEmpty()); |
| 2886 | Mock::VerifyAndClearExpectations(&power_manager); |
| 2887 | |
| 2888 | // Removing the last action should trigger unregistering from the power |
| 2889 | // manager. |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2890 | EXPECT_CALL(power_manager, UnregisterSuspendDelay(kSuspendDelayId)) |
| 2891 | .WillOnce(Return(true)); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2892 | EXPECT_CALL(power_manager, RemoveSuspendDelayCallback(_)); |
| 2893 | manager()->RemoveTerminationAction(kKey2); |
| 2894 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
| 2895 | } |
| 2896 | |
| 2897 | TEST_F(ManagerTest, RunTerminationActions) { |
| 2898 | TerminationActionTest test_action; |
| 2899 | const string kActionName = "action"; |
| 2900 | |
| 2901 | EXPECT_CALL(test_action, Done(_)); |
| 2902 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2903 | test_action.AsWeakPtr())); |
| 2904 | |
| 2905 | manager()->AddTerminationAction(TerminationActionTest::kActionName, |
| 2906 | Bind(&TerminationActionTest::Action, |
| 2907 | test_action.AsWeakPtr())); |
| 2908 | test_action.set_manager(manager()); |
| 2909 | EXPECT_CALL(test_action, Done(_)); |
| 2910 | manager()->RunTerminationActions(Bind(&TerminationActionTest::Done, |
| 2911 | test_action.AsWeakPtr())); |
| 2912 | } |
| 2913 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2914 | TEST_F(ManagerTest, OnSuspendImminent) { |
| 2915 | const int kSuspendId = 123; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2916 | EXPECT_TRUE(GetTerminationActions()->IsEmpty()); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2917 | EXPECT_CALL(*power_manager_, |
| 2918 | ReportSuspendReadiness( |
| 2919 | manager()->suspend_delay_id_for_testing(), kSuspendId)); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2920 | SetPowerManager(); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2921 | OnSuspendImminent(kSuspendId); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | TEST_F(ManagerTest, OnSuspendActionsComplete) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2925 | const int kSuspendId = 54321; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2926 | Error error; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2927 | EXPECT_CALL(*power_manager_, |
| 2928 | ReportSuspendReadiness( |
| 2929 | manager()->suspend_delay_id_for_testing(), kSuspendId)); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2930 | SetPowerManager(); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 2931 | OnSuspendActionsComplete(kSuspendId, error); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 2932 | } |
| 2933 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 2934 | TEST_F(ManagerTest, RecheckPortal) { |
| 2935 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 2936 | .WillOnce(Return(false)); |
| 2937 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 2938 | .WillOnce(Return(true)); |
| 2939 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 2940 | .Times(0); |
| 2941 | |
| 2942 | manager()->RegisterDevice(mock_devices_[0]); |
| 2943 | manager()->RegisterDevice(mock_devices_[1]); |
| 2944 | manager()->RegisterDevice(mock_devices_[2]); |
| 2945 | |
| 2946 | manager()->RecheckPortal(NULL); |
| 2947 | } |
| 2948 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 2949 | TEST_F(ManagerTest, RecheckPortalOnService) { |
| 2950 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 2951 | dispatcher(), |
| 2952 | metrics(), |
| 2953 | manager()); |
| 2954 | EXPECT_CALL(*mock_devices_[0].get(), |
| 2955 | IsConnectedToService(IsRefPtrTo(service))) |
| 2956 | .WillOnce(Return(false)); |
| 2957 | EXPECT_CALL(*mock_devices_[1].get(), |
| 2958 | IsConnectedToService(IsRefPtrTo(service))) |
| 2959 | .WillOnce(Return(true)); |
| 2960 | EXPECT_CALL(*mock_devices_[1].get(), RestartPortalDetection()) |
| 2961 | .WillOnce(Return(true)); |
| 2962 | EXPECT_CALL(*mock_devices_[2].get(), IsConnectedToService(_)) |
| 2963 | .Times(0); |
| 2964 | |
| 2965 | manager()->RegisterDevice(mock_devices_[0]); |
| 2966 | manager()->RegisterDevice(mock_devices_[1]); |
| 2967 | manager()->RegisterDevice(mock_devices_[2]); |
| 2968 | |
| 2969 | manager()->RecheckPortalOnService(service); |
| 2970 | } |
| 2971 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2972 | TEST_F(ManagerTest, GetDefaultService) { |
| 2973 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2974 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2975 | |
| 2976 | scoped_refptr<MockService> mock_service( |
| 2977 | new NiceMock<MockService>(control_interface(), |
| 2978 | dispatcher(), |
| 2979 | metrics(), |
| 2980 | manager())); |
| 2981 | |
| 2982 | manager()->RegisterService(mock_service); |
| 2983 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2984 | EXPECT_EQ("/", GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2985 | |
| 2986 | scoped_refptr<MockConnection> mock_connection( |
| 2987 | new NiceMock<MockConnection>(device_info_.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2988 | mock_service->set_mock_connection(mock_connection); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2989 | EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get()); |
Paul Stewart | 49739c0 | 2012-08-08 17:24:03 -0700 | [diff] [blame] | 2990 | EXPECT_EQ(mock_service->GetRpcIdentifier(), GetDefaultServiceRpcIdentifier()); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2991 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 2992 | mock_service->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 2993 | manager()->DeregisterService(mock_service); |
| 2994 | } |
| 2995 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 2996 | TEST_F(ManagerTest, GetServiceWithGUID) { |
| 2997 | scoped_refptr<MockService> mock_service0( |
| 2998 | new NiceMock<MockService>(control_interface(), |
| 2999 | dispatcher(), |
| 3000 | metrics(), |
| 3001 | manager())); |
| 3002 | |
| 3003 | scoped_refptr<MockService> mock_service1( |
| 3004 | new NiceMock<MockService>(control_interface(), |
| 3005 | dispatcher(), |
| 3006 | metrics(), |
| 3007 | manager())); |
| 3008 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 3009 | EXPECT_CALL(*mock_service0.get(), Configure(_, _)) |
| 3010 | .Times(0); |
| 3011 | EXPECT_CALL(*mock_service1.get(), Configure(_, _)) |
| 3012 | .Times(0); |
| 3013 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 3014 | manager()->RegisterService(mock_service0); |
| 3015 | manager()->RegisterService(mock_service1); |
| 3016 | |
| 3017 | const string kGUID0 = "GUID0"; |
| 3018 | const string kGUID1 = "GUID1"; |
| 3019 | |
| 3020 | { |
| 3021 | Error error; |
| 3022 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 3023 | EXPECT_FALSE(error.IsSuccess()); |
| 3024 | EXPECT_FALSE(service); |
| 3025 | } |
| 3026 | |
| 3027 | KeyValueStore args; |
| 3028 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 3029 | |
| 3030 | { |
| 3031 | Error error; |
| 3032 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 3033 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 3034 | EXPECT_FALSE(service); |
| 3035 | } |
| 3036 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3037 | mock_service0->SetGuid(kGUID0, NULL); |
| 3038 | mock_service1->SetGuid(kGUID1, NULL); |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 3039 | |
| 3040 | { |
| 3041 | Error error; |
| 3042 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 3043 | EXPECT_TRUE(error.IsSuccess()); |
| 3044 | EXPECT_EQ(mock_service0.get(), service.get()); |
| 3045 | } |
| 3046 | |
| 3047 | { |
| 3048 | Error error; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 3049 | EXPECT_CALL(*mock_service1.get(), Configure(_, &error)) |
| 3050 | .Times(1); |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 3051 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 3052 | EXPECT_TRUE(error.IsSuccess()); |
| 3053 | EXPECT_EQ(mock_service1.get(), service.get()); |
| 3054 | } |
| 3055 | |
| 3056 | manager()->DeregisterService(mock_service0); |
| 3057 | manager()->DeregisterService(mock_service1); |
| 3058 | } |
| 3059 | |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3060 | |
| 3061 | TEST_F(ManagerTest, CalculateStateOffline) { |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 3062 | EXPECT_FALSE(manager()->IsOnline()); |
| 3063 | EXPECT_EQ("offline", manager()->CalculateState(NULL)); |
| 3064 | |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 3065 | MockMetrics mock_metrics(dispatcher()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 3066 | SetMetrics(&mock_metrics); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3067 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 3068 | .Times(AnyNumber()); |
| 3069 | scoped_refptr<MockService> mock_service0( |
| 3070 | new NiceMock<MockService>(control_interface(), |
| 3071 | dispatcher(), |
| 3072 | metrics(), |
| 3073 | manager())); |
| 3074 | |
| 3075 | scoped_refptr<MockService> mock_service1( |
| 3076 | new NiceMock<MockService>(control_interface(), |
| 3077 | dispatcher(), |
| 3078 | metrics(), |
| 3079 | manager())); |
| 3080 | |
| 3081 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 3082 | .WillRepeatedly(Return(false)); |
| 3083 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 3084 | .WillRepeatedly(Return(false)); |
| 3085 | |
| 3086 | manager()->RegisterService(mock_service0); |
| 3087 | manager()->RegisterService(mock_service1); |
| 3088 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 3089 | EXPECT_FALSE(manager()->IsOnline()); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3090 | EXPECT_EQ("offline", manager()->CalculateState(NULL)); |
| 3091 | |
| 3092 | manager()->DeregisterService(mock_service0); |
| 3093 | manager()->DeregisterService(mock_service1); |
| 3094 | } |
| 3095 | |
| 3096 | TEST_F(ManagerTest, CalculateStateOnline) { |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 3097 | MockMetrics mock_metrics(dispatcher()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 3098 | SetMetrics(&mock_metrics); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3099 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(_)) |
| 3100 | .Times(AnyNumber()); |
| 3101 | scoped_refptr<MockService> mock_service0( |
| 3102 | new NiceMock<MockService>(control_interface(), |
| 3103 | dispatcher(), |
| 3104 | metrics(), |
| 3105 | manager())); |
| 3106 | |
| 3107 | scoped_refptr<MockService> mock_service1( |
| 3108 | new NiceMock<MockService>(control_interface(), |
| 3109 | dispatcher(), |
| 3110 | metrics(), |
| 3111 | manager())); |
| 3112 | |
| 3113 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 3114 | .WillRepeatedly(Return(false)); |
| 3115 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 3116 | .WillRepeatedly(Return(true)); |
| 3117 | EXPECT_CALL(*mock_service0.get(), state()) |
| 3118 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 3119 | EXPECT_CALL(*mock_service1.get(), state()) |
| 3120 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 3121 | |
| 3122 | manager()->RegisterService(mock_service0); |
| 3123 | manager()->RegisterService(mock_service1); |
Paul Stewart | dfa4605 | 2012-06-26 09:44:14 -0700 | [diff] [blame] | 3124 | CompleteServiceSort(); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3125 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 3126 | EXPECT_TRUE(manager()->IsOnline()); |
Gary Morain | 028545d | 2012-04-07 14:55:52 -0700 | [diff] [blame] | 3127 | EXPECT_EQ("online", manager()->CalculateState(NULL)); |
| 3128 | |
| 3129 | manager()->DeregisterService(mock_service0); |
| 3130 | manager()->DeregisterService(mock_service1); |
| 3131 | } |
| 3132 | |
Paul Stewart | 10e9e4e | 2012-04-26 19:46:28 -0700 | [diff] [blame] | 3133 | TEST_F(ManagerTest, StartupPortalList) { |
| 3134 | // Simulate loading value from the default profile. |
| 3135 | const string kProfileValue("wifi,vpn"); |
| 3136 | manager()->props_.check_portal_list = kProfileValue; |
| 3137 | |
| 3138 | EXPECT_EQ(kProfileValue, manager()->GetCheckPortalList(NULL)); |
| 3139 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 3140 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 3141 | |
| 3142 | const string kStartupValue("cellular,ethernet"); |
| 3143 | manager()->SetStartupPortalList(kStartupValue); |
| 3144 | // Ensure profile value is not overwritten, so when we save the default |
| 3145 | // profile, the correct value will still be written. |
| 3146 | EXPECT_EQ(kProfileValue, manager()->props_.check_portal_list); |
| 3147 | |
| 3148 | // However we should read back a different list. |
| 3149 | EXPECT_EQ(kStartupValue, manager()->GetCheckPortalList(NULL)); |
| 3150 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kWifi)); |
| 3151 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 3152 | |
| 3153 | const string kRuntimeValue("ppp"); |
| 3154 | // Setting a runtime value over the control API should overwrite both |
| 3155 | // the profile value and what we read back. |
| 3156 | Error error; |
| 3157 | manager()->mutable_store()->SetStringProperty( |
| 3158 | flimflam::kCheckPortalListProperty, |
| 3159 | kRuntimeValue, |
| 3160 | &error); |
| 3161 | ASSERT_TRUE(error.IsSuccess()); |
| 3162 | EXPECT_EQ(kRuntimeValue, manager()->GetCheckPortalList(NULL)); |
| 3163 | EXPECT_EQ(kRuntimeValue, manager()->props_.check_portal_list); |
| 3164 | EXPECT_FALSE(manager()->IsPortalDetectionEnabled(Technology::kCellular)); |
| 3165 | EXPECT_TRUE(manager()->IsPortalDetectionEnabled(Technology::kPPP)); |
| 3166 | } |
| 3167 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 3168 | TEST_F(ManagerTest, LinkMonitorEnabled) { |
| 3169 | const string kEnabledTechnologies("wifi,vpn"); |
| 3170 | manager()->props_.link_monitor_technologies = kEnabledTechnologies; |
| 3171 | EXPECT_TRUE(manager()->IsTechnologyLinkMonitorEnabled(Technology::kWifi)); |
| 3172 | EXPECT_FALSE( |
| 3173 | manager()->IsTechnologyLinkMonitorEnabled(Technology::kCellular)); |
| 3174 | } |
| 3175 | |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 3176 | TEST_F(ManagerTest, IsDefaultProfile) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 3177 | EXPECT_TRUE(manager()->IsDefaultProfile(NULL)); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 3178 | scoped_ptr<MockStore> store0(new MockStore); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 3179 | EXPECT_TRUE(manager()->IsDefaultProfile(store0.get())); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 3180 | scoped_refptr<MockProfile> profile( |
Thieu Le | 5133b71 | 2013-02-19 14:47:21 -0800 | [diff] [blame] | 3181 | new MockProfile(control_interface(), metrics(), manager(), "")); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 3182 | EXPECT_CALL(*profile, GetConstStorage()).WillRepeatedly(Return(store0.get())); |
| 3183 | AdoptProfile(manager(), profile); |
| 3184 | EXPECT_TRUE(manager()->IsDefaultProfile(store0.get())); |
| 3185 | EXPECT_FALSE(manager()->IsDefaultProfile(NULL)); |
| 3186 | scoped_ptr<MockStore> store1(new MockStore); |
| 3187 | EXPECT_FALSE(manager()->IsDefaultProfile(store1.get())); |
| 3188 | } |
| 3189 | |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 3190 | TEST_F(ManagerTest, EnableTechnology) { |
| 3191 | Error error(Error::kOperationInitiated); |
| 3192 | ResultCallback callback; |
| 3193 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3194 | EXPECT_TRUE(error.IsSuccess()); |
| 3195 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 3196 | ON_CALL(*mock_devices_[0], technology()) |
| 3197 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 3198 | |
| 3199 | manager()->RegisterDevice(mock_devices_[0]); |
| 3200 | |
| 3201 | // Device is enabled, so expect operation is successful. |
| 3202 | mock_devices_[0]->enabled_ = true; |
| 3203 | error.Populate(Error::kOperationInitiated); |
| 3204 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3205 | EXPECT_TRUE(error.IsSuccess()); |
| 3206 | |
| 3207 | // Device is disabled, so expect operation in progress. |
| 3208 | mock_devices_[0]->enabled_ = false; |
| 3209 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(true, _, _)); |
| 3210 | error.Populate(Error::kOperationInitiated); |
| 3211 | manager()->EnableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3212 | EXPECT_TRUE(error.IsOngoing()); |
| 3213 | } |
| 3214 | |
| 3215 | TEST_F(ManagerTest, DisableTechnology) { |
| 3216 | Error error(Error::kOperationInitiated); |
| 3217 | ResultCallback callback; |
| 3218 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3219 | EXPECT_TRUE(error.IsSuccess()); |
| 3220 | |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 3221 | ON_CALL(*mock_devices_[0], technology()) |
| 3222 | .WillByDefault(Return(Technology::kEthernet)); |
Jason Glasgow | df7c553 | 2012-05-14 14:41:45 -0400 | [diff] [blame] | 3223 | |
| 3224 | manager()->RegisterDevice(mock_devices_[0]); |
| 3225 | |
| 3226 | // Device is disabled, so expect operation is successful. |
| 3227 | error.Populate(Error::kOperationInitiated); |
| 3228 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3229 | EXPECT_TRUE(error.IsSuccess()); |
| 3230 | |
| 3231 | // Device is enabled, so expect operation in progress. |
| 3232 | EXPECT_CALL(*mock_devices_[0], SetEnabledPersistent(false, _, _)); |
| 3233 | mock_devices_[0]->enabled_ = true; |
| 3234 | error.Populate(Error::kOperationInitiated); |
| 3235 | manager()->DisableTechnology(flimflam::kTypeEthernet, &error, callback); |
| 3236 | EXPECT_TRUE(error.IsOngoing()); |
| 3237 | } |
| 3238 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 3239 | TEST_F(ManagerTest, IgnoredSearchList) { |
| 3240 | scoped_ptr<MockResolver> resolver(new StrictMock<MockResolver>()); |
| 3241 | SetResolver(resolver.get()); |
| 3242 | vector<string> ignored_paths; |
| 3243 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 3244 | SetIgnoredDNSSearchPaths(""); |
| 3245 | EXPECT_EQ("", GetIgnoredDNSSearchPaths()); |
| 3246 | |
| 3247 | const string kIgnored0 = "chromium.org"; |
| 3248 | ignored_paths.push_back(kIgnored0); |
| 3249 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 3250 | SetIgnoredDNSSearchPaths(kIgnored0); |
| 3251 | EXPECT_EQ(kIgnored0, GetIgnoredDNSSearchPaths()); |
| 3252 | |
| 3253 | const string kIgnored1 = "google.com"; |
| 3254 | const string kIgnoredSum = kIgnored0 + "," + kIgnored1; |
| 3255 | ignored_paths.push_back(kIgnored1); |
| 3256 | EXPECT_CALL(*resolver.get(), set_ignored_search_list(ignored_paths)); |
| 3257 | SetIgnoredDNSSearchPaths(kIgnoredSum); |
| 3258 | EXPECT_EQ(kIgnoredSum, GetIgnoredDNSSearchPaths()); |
| 3259 | |
| 3260 | SetResolver(Resolver::GetInstance()); |
| 3261 | } |
| 3262 | |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 3263 | TEST_F(ManagerTest, ServiceStateChangeEmitsServices) { |
| 3264 | // Test to make sure that every service state-change causes the |
| 3265 | // Manager to emit a new service list. |
| 3266 | scoped_refptr<MockService> mock_service( |
| 3267 | new NiceMock<MockService>(control_interface(), |
| 3268 | dispatcher(), |
| 3269 | metrics(), |
| 3270 | manager())); |
| 3271 | EXPECT_CALL(*mock_service, state()) |
| 3272 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 3273 | |
| 3274 | manager()->RegisterService(mock_service); |
| 3275 | EXPECT_CALL( |
| 3276 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 3277 | flimflam::kServicesProperty, _)).Times(1); |
| 3278 | EXPECT_CALL( |
| 3279 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 3280 | flimflam::kServiceWatchListProperty, _)).Times(1); |
| 3281 | CompleteServiceSort(); |
| 3282 | |
| 3283 | Mock::VerifyAndClearExpectations(manager_adaptor_); |
| 3284 | EXPECT_CALL( |
| 3285 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 3286 | flimflam::kServicesProperty, _)).Times(1); |
| 3287 | EXPECT_CALL( |
| 3288 | *manager_adaptor_, EmitRpcIdentifierArrayChanged( |
| 3289 | flimflam::kServiceWatchListProperty, _)).Times(1); |
| 3290 | manager()->UpdateService(mock_service.get()); |
| 3291 | CompleteServiceSort(); |
| 3292 | |
| 3293 | manager()->DeregisterService(mock_service); |
| 3294 | } |
| 3295 | |
| 3296 | TEST_F(ManagerTest, EnumerateServices) { |
| 3297 | scoped_refptr<MockService> mock_service( |
| 3298 | new NiceMock<MockService>(control_interface(), |
| 3299 | dispatcher(), |
| 3300 | metrics(), |
| 3301 | manager())); |
| 3302 | manager()->RegisterService(mock_service); |
| 3303 | |
| 3304 | EXPECT_CALL(*mock_service, state()) |
| 3305 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 3306 | EXPECT_CALL(*mock_service, IsVisible()) |
| 3307 | .WillRepeatedly(Return(false)); |
| 3308 | EXPECT_TRUE(EnumerateAvailableServices().empty()); |
| 3309 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 3310 | |
| 3311 | EXPECT_CALL(*mock_service, state()) |
| 3312 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 3313 | EXPECT_TRUE(EnumerateAvailableServices().empty()); |
| 3314 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 3315 | |
| 3316 | EXPECT_CALL(*mock_service, IsVisible()) |
| 3317 | .WillRepeatedly(Return(true)); |
| 3318 | Service::ConnectState unwatched_states[] = { |
| 3319 | Service::kStateUnknown, |
| 3320 | Service::kStateIdle, |
| 3321 | Service::kStateFailure |
| 3322 | }; |
| 3323 | for (size_t i = 0; i < arraysize(unwatched_states); ++i) { |
| 3324 | EXPECT_CALL(*mock_service, state()) |
| 3325 | .WillRepeatedly(Return(unwatched_states[i])); |
| 3326 | EXPECT_FALSE(EnumerateAvailableServices().empty()); |
| 3327 | EXPECT_TRUE(EnumerateWatchedServices().empty()); |
| 3328 | } |
| 3329 | |
| 3330 | Service::ConnectState watched_states[] = { |
| 3331 | Service::kStateAssociating, |
| 3332 | Service::kStateConfiguring, |
| 3333 | Service::kStateConnected, |
| 3334 | Service::kStateDisconnected, |
| 3335 | Service::kStatePortal, |
| 3336 | Service::kStateOnline |
| 3337 | }; |
| 3338 | for (size_t i = 0; i < arraysize(watched_states); ++i) { |
| 3339 | EXPECT_CALL(*mock_service, state()) |
| 3340 | .WillRepeatedly(Return(watched_states[i])); |
| 3341 | EXPECT_FALSE(EnumerateAvailableServices().empty()); |
| 3342 | EXPECT_FALSE(EnumerateWatchedServices().empty()); |
| 3343 | } |
| 3344 | |
| 3345 | manager()->DeregisterService(mock_service); |
| 3346 | } |
| 3347 | |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3348 | TEST_F(ManagerTest, ConnectToBestServices) { |
| 3349 | scoped_refptr<MockService> wifi_service0( |
| 3350 | new NiceMock<MockService>(control_interface(), |
| 3351 | dispatcher(), |
| 3352 | metrics(), |
| 3353 | manager())); |
| 3354 | EXPECT_CALL(*wifi_service0.get(), state()) |
| 3355 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 3356 | EXPECT_CALL(*wifi_service0.get(), IsConnected()) |
| 3357 | .WillRepeatedly(Return(false)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3358 | wifi_service0->SetConnectable(true); |
| 3359 | wifi_service0->SetAutoConnect(true); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3360 | wifi_service0->SetSecurity(Service::kCryptoAes, true, true); |
| 3361 | EXPECT_CALL(*wifi_service0.get(), technology()) |
| 3362 | .WillRepeatedly(Return(Technology::kWifi)); |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3363 | EXPECT_CALL(*wifi_service0.get(), IsVisible()) |
| 3364 | .WillRepeatedly(Return(false)); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3365 | |
| 3366 | scoped_refptr<MockService> wifi_service1( |
| 3367 | new NiceMock<MockService>(control_interface(), |
| 3368 | dispatcher(), |
| 3369 | metrics(), |
| 3370 | manager())); |
| 3371 | EXPECT_CALL(*wifi_service1.get(), state()) |
| 3372 | .WillRepeatedly(Return(Service::kStateIdle)); |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3373 | EXPECT_CALL(*wifi_service1.get(), IsVisible()) |
| 3374 | .WillRepeatedly(Return(true)); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3375 | EXPECT_CALL(*wifi_service1.get(), IsConnected()) |
| 3376 | .WillRepeatedly(Return(false)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3377 | wifi_service1->SetAutoConnect(true); |
| 3378 | wifi_service1->SetConnectable(true); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3379 | wifi_service1->SetSecurity(Service::kCryptoRc4, true, true); |
| 3380 | EXPECT_CALL(*wifi_service1.get(), technology()) |
| 3381 | .WillRepeatedly(Return(Technology::kWifi)); |
| 3382 | |
| 3383 | scoped_refptr<MockService> wifi_service2( |
| 3384 | new NiceMock<MockService>(control_interface(), |
| 3385 | dispatcher(), |
| 3386 | metrics(), |
| 3387 | manager())); |
| 3388 | EXPECT_CALL(*wifi_service2.get(), state()) |
| 3389 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 3390 | EXPECT_CALL(*wifi_service2.get(), IsConnected()) |
| 3391 | .WillRepeatedly(Return(true)); |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3392 | EXPECT_CALL(*wifi_service2.get(), IsVisible()) |
| 3393 | .WillRepeatedly(Return(true)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3394 | wifi_service2->SetAutoConnect(true); |
| 3395 | wifi_service2->SetConnectable(true); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3396 | wifi_service2->SetSecurity(Service::kCryptoNone, false, false); |
| 3397 | EXPECT_CALL(*wifi_service2.get(), technology()) |
| 3398 | .WillRepeatedly(Return(Technology::kWifi)); |
| 3399 | |
| 3400 | manager()->RegisterService(wifi_service0); |
| 3401 | manager()->RegisterService(wifi_service1); |
| 3402 | manager()->RegisterService(wifi_service2); |
| 3403 | |
| 3404 | CompleteServiceSort(); |
| 3405 | EXPECT_TRUE(ServiceOrderIs(wifi_service2, wifi_service0)); |
| 3406 | |
| 3407 | scoped_refptr<MockService> cell_service( |
| 3408 | new NiceMock<MockService>(control_interface(), |
| 3409 | dispatcher(), |
| 3410 | metrics(), |
| 3411 | manager())); |
| 3412 | |
| 3413 | EXPECT_CALL(*cell_service.get(), state()) |
| 3414 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 3415 | EXPECT_CALL(*cell_service.get(), IsConnected()) |
| 3416 | .WillRepeatedly(Return(true)); |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3417 | EXPECT_CALL(*cell_service.get(), IsVisible()) |
| 3418 | .WillRepeatedly(Return(true)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3419 | wifi_service2->SetAutoConnect(true); |
| 3420 | cell_service->SetConnectable(true); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3421 | EXPECT_CALL(*cell_service.get(), technology()) |
| 3422 | .WillRepeatedly(Return(Technology::kCellular)); |
| 3423 | manager()->RegisterService(cell_service); |
| 3424 | |
| 3425 | scoped_refptr<MockService> vpn_service( |
| 3426 | new NiceMock<MockService>(control_interface(), |
| 3427 | dispatcher(), |
| 3428 | metrics(), |
| 3429 | manager())); |
| 3430 | |
| 3431 | EXPECT_CALL(*vpn_service.get(), state()) |
| 3432 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 3433 | EXPECT_CALL(*vpn_service.get(), IsConnected()) |
| 3434 | .WillRepeatedly(Return(false)); |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3435 | EXPECT_CALL(*vpn_service.get(), IsVisible()) |
| 3436 | .WillRepeatedly(Return(true)); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 3437 | wifi_service2->SetAutoConnect(false); |
| 3438 | vpn_service->SetConnectable(true); |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3439 | EXPECT_CALL(*vpn_service.get(), technology()) |
| 3440 | .WillRepeatedly(Return(Technology::kVPN)); |
| 3441 | manager()->RegisterService(vpn_service); |
| 3442 | |
| 3443 | // The connected services should be at the top. |
| 3444 | EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service)); |
| 3445 | |
Paul Stewart | f7d7d3f | 2013-04-24 14:47:44 -0700 | [diff] [blame^] | 3446 | EXPECT_CALL(*wifi_service0.get(), Connect(_, _)).Times(0); // Not visible. |
| 3447 | EXPECT_CALL(*wifi_service1.get(), Connect(_, _)); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 3448 | EXPECT_CALL(*wifi_service2.get(), Connect(_, _)).Times(0); // Lower prio. |
| 3449 | EXPECT_CALL(*cell_service.get(), Connect(_, _)).Times(0); // Is connected. |
| 3450 | EXPECT_CALL(*vpn_service.get(), Connect(_, _)).Times(0); // Not autoconnect. |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 3451 | |
| 3452 | manager()->ConnectToBestServices(NULL); |
| 3453 | dispatcher()->DispatchPendingEvents(); |
| 3454 | |
| 3455 | // After this operation, since the Connect calls above are mocked and |
| 3456 | // no actual state changes have occurred, we should expect that the |
| 3457 | // service sorting order will not have changed. |
| 3458 | EXPECT_TRUE(ServiceOrderIs(wifi_service2, cell_service)); |
| 3459 | } |
| 3460 | |
Christopher Wiley | 1057cd7 | 2013-02-28 15:21:29 -0800 | [diff] [blame] | 3461 | TEST_F(ManagerTest, VerifyDestination) { |
| 3462 | const string kFakeCertificate("fake cert"); |
| 3463 | const string kFakePublicKey("fake public key"); |
| 3464 | const string kFakeNonce("fake public key"); |
| 3465 | const string kFakeSignedData("fake signed data"); |
| 3466 | const string kFakeUdn("fake udn"); |
| 3467 | const char kSSIDStr[] = "fake ssid"; |
| 3468 | const vector<uint8_t> kSSID(kSSIDStr, kSSIDStr + arraysize(kSSIDStr)); |
| 3469 | const string kFakeData("muffin man"); |
| 3470 | scoped_refptr<MockWiFiService> mock_destination( |
| 3471 | new NiceMock<MockWiFiService>(control_interface(), |
| 3472 | dispatcher(), |
| 3473 | metrics(), |
| 3474 | manager(), |
| 3475 | wifi_provider_, |
| 3476 | kSSID, |
| 3477 | "", |
| 3478 | "none", |
| 3479 | false)); |
| 3480 | manager()->RegisterService(mock_destination); |
| 3481 | StrictMock<DestinationVerificationTest> dv_test; |
| 3482 | |
| 3483 | // Verify that if we're not connected to anything, verification fails. |
| 3484 | { |
| 3485 | EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate, |
| 3486 | kFakePublicKey, |
| 3487 | kFakeNonce, |
| 3488 | kFakeSignedData, |
| 3489 | kFakeUdn, |
| 3490 | kSSID, |
| 3491 | _, |
| 3492 | _, |
| 3493 | _)) |
| 3494 | .Times(0); |
| 3495 | Error error(Error::kOperationInitiated); |
| 3496 | ResultBoolCallback cb = Bind( |
| 3497 | &DestinationVerificationTest::ResultBoolCallbackStub, |
| 3498 | dv_test.AsWeakPtr()); |
| 3499 | manager()->VerifyDestination(kFakeCertificate, |
| 3500 | kFakePublicKey, |
| 3501 | kFakeNonce, |
| 3502 | kFakeSignedData, |
| 3503 | kFakeUdn, |
| 3504 | cb, |
| 3505 | &error); |
| 3506 | EXPECT_TRUE(error.IsFailure()); |
| 3507 | Mock::VerifyAndClearExpectations(crypto_util_proxy_); |
| 3508 | } |
| 3509 | |
| 3510 | // Making the service look online will let service lookup in |
| 3511 | // VerifyDestinatoin succeed. |
| 3512 | EXPECT_CALL(*mock_destination.get(), IsConnected()) |
| 3513 | .WillRepeatedly(Return(true)); |
| 3514 | |
| 3515 | // Lead off by verifying that the basic VerifyDestination flow works. |
| 3516 | { |
| 3517 | ResultBoolCallback passed_down_callback; |
| 3518 | EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate, |
| 3519 | kFakePublicKey, |
| 3520 | kFakeNonce, |
| 3521 | kFakeSignedData, |
| 3522 | kFakeUdn, |
| 3523 | kSSID, |
| 3524 | _, |
| 3525 | _, |
| 3526 | _)) |
| 3527 | .Times(1) |
| 3528 | .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true))); |
| 3529 | // Ask the manager to verify the current destination. This should look |
| 3530 | // up our previously registered service, and pass some metadata about |
| 3531 | // that service down to the CryptoUtilProxy to verify. |
| 3532 | Error error(Error::kOperationInitiated); |
| 3533 | ResultBoolCallback cb = Bind( |
| 3534 | &DestinationVerificationTest::ResultBoolCallbackStub, |
| 3535 | dv_test.AsWeakPtr()); |
| 3536 | manager()->VerifyDestination(kFakeCertificate, |
| 3537 | kFakePublicKey, |
| 3538 | kFakeNonce, |
| 3539 | kFakeSignedData, |
| 3540 | kFakeUdn, |
| 3541 | cb, |
| 3542 | &error); |
| 3543 | // We assert here, because if the operation is not ongoing, it is |
| 3544 | // inconsistent with shim behavior to call the callback anyway. |
| 3545 | ASSERT_TRUE(error.IsOngoing()); |
| 3546 | Mock::VerifyAndClearExpectations(crypto_util_proxy_); |
| 3547 | EXPECT_CALL(dv_test, ResultBoolCallbackStub(_, true)).Times(1); |
| 3548 | // Call the callback passed into the CryptoUtilProxy, which |
| 3549 | // should find its way into the callback passed into the manager. |
| 3550 | // In real code, that callback passed into the manager is from the |
| 3551 | // DBus adaptor. |
| 3552 | Error e; |
| 3553 | passed_down_callback.Run(e, true); |
| 3554 | Mock::VerifyAndClearExpectations(&dv_test); |
| 3555 | } |
| 3556 | |
| 3557 | // Now for a slightly more complex variant. When we encrypt data, |
| 3558 | // we do the same verification step but monkey with the callback to |
| 3559 | // link ourselves to an encrypt step afterward. |
| 3560 | { |
| 3561 | ResultBoolCallback passed_down_callback; |
| 3562 | EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate, |
| 3563 | kFakePublicKey, |
| 3564 | kFakeNonce, |
| 3565 | kFakeSignedData, |
| 3566 | kFakeUdn, |
| 3567 | kSSID, |
| 3568 | _, |
| 3569 | _, |
| 3570 | _)) |
| 3571 | .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true))); |
| 3572 | |
| 3573 | Error error(Error::kOperationInitiated); |
| 3574 | ResultStringCallback cb = Bind( |
| 3575 | &DestinationVerificationTest::ResultStringCallbackStub, |
| 3576 | dv_test.AsWeakPtr()); |
| 3577 | manager()->VerifyAndEncryptData(kFakeCertificate, |
| 3578 | kFakePublicKey, |
| 3579 | kFakeNonce, |
| 3580 | kFakeSignedData, |
| 3581 | kFakeUdn, |
| 3582 | kFakeData, |
| 3583 | cb, |
| 3584 | &error); |
| 3585 | ASSERT_TRUE(error.IsOngoing()); |
| 3586 | Mock::VerifyAndClearExpectations(crypto_util_proxy_); |
| 3587 | // Now, if we call that passed down callback, we should see encrypt being |
| 3588 | // called. |
| 3589 | ResultStringCallback second_passed_down_callback; |
| 3590 | EXPECT_CALL(*crypto_util_proxy_, EncryptData(kFakePublicKey, |
| 3591 | kFakeData, |
| 3592 | _, |
| 3593 | _)) |
| 3594 | .Times(1) |
| 3595 | .WillOnce(DoAll(SaveArg<2>(&second_passed_down_callback), |
| 3596 | Return(true))); |
| 3597 | Error e; |
| 3598 | passed_down_callback.Run(e, true); |
| 3599 | Mock::VerifyAndClearExpectations(crypto_util_proxy_); |
| 3600 | EXPECT_CALL(dv_test, ResultStringCallbackStub(_, _)).Times(1); |
| 3601 | // And if we call the second passed down callback, we should see the |
| 3602 | // original function we passed down to VerifyDestination getting called. |
| 3603 | e.Reset(); |
| 3604 | second_passed_down_callback.Run(e, ""); |
| 3605 | Mock::VerifyAndClearExpectations(&dv_test); |
| 3606 | } |
| 3607 | |
| 3608 | // If verification fails on the way to trying to encrypt, we should ditch |
| 3609 | // without calling encrypt at all. |
| 3610 | { |
| 3611 | ResultBoolCallback passed_down_callback; |
| 3612 | EXPECT_CALL(*crypto_util_proxy_, VerifyDestination(kFakeCertificate, |
| 3613 | kFakePublicKey, |
| 3614 | kFakeNonce, |
| 3615 | kFakeSignedData, |
| 3616 | kFakeUdn, |
| 3617 | kSSID, |
| 3618 | _, |
| 3619 | _, |
| 3620 | _)) |
| 3621 | .WillOnce(DoAll(SaveArg<7>(&passed_down_callback), Return(true))); |
| 3622 | |
| 3623 | Error error(Error::kOperationInitiated); |
| 3624 | ResultStringCallback cb = Bind( |
| 3625 | &DestinationVerificationTest::ResultStringCallbackStub, |
| 3626 | dv_test.AsWeakPtr()); |
| 3627 | manager()->VerifyAndEncryptData(kFakeCertificate, |
| 3628 | kFakePublicKey, |
| 3629 | kFakeNonce, |
| 3630 | kFakeSignedData, |
| 3631 | kFakeUdn, |
| 3632 | kFakeData, |
| 3633 | cb, |
| 3634 | &error); |
| 3635 | ASSERT_TRUE(error.IsOngoing()); |
| 3636 | Mock::VerifyAndClearExpectations(crypto_util_proxy_); |
| 3637 | Error e(Error::kOperationFailed); |
| 3638 | EXPECT_CALL(*crypto_util_proxy_, EncryptData(_, _, _, _)).Times(0); |
| 3639 | // Although we're ditching, this callback is what cleans up the pending |
| 3640 | // DBus call. |
| 3641 | EXPECT_CALL(dv_test, ResultStringCallbackStub(_, string(""))).Times(1); |
| 3642 | passed_down_callback.Run(e, false); |
| 3643 | Mock::VerifyAndClearExpectations(&dv_test); |
| 3644 | } |
| 3645 | } |
| 3646 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 3647 | TEST_F(ManagerTest, IsProfileBefore) { |
| 3648 | scoped_refptr<MockProfile> profile0( |
| 3649 | new NiceMock<MockProfile>( |
| 3650 | control_interface(), metrics(), manager(), "")); |
| 3651 | scoped_refptr<MockProfile> profile1( |
| 3652 | new NiceMock<MockProfile>( |
| 3653 | control_interface(), metrics(), manager(), "")); |
| 3654 | |
| 3655 | AdoptProfile(manager(), profile0); |
| 3656 | AdoptProfile(manager(), profile1); // profile1 is after profile0. |
| 3657 | EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile1)); |
| 3658 | EXPECT_FALSE(manager()->IsProfileBefore(profile1, profile0)); |
| 3659 | |
| 3660 | // A few abnormal cases, but it's good to track their behavior. |
| 3661 | scoped_refptr<MockProfile> profile2( |
| 3662 | new NiceMock<MockProfile>( |
| 3663 | control_interface(), metrics(), manager(), "")); |
| 3664 | EXPECT_TRUE(manager()->IsProfileBefore(profile0, profile2)); |
| 3665 | EXPECT_TRUE(manager()->IsProfileBefore(profile1, profile2)); |
| 3666 | EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile0)); |
| 3667 | EXPECT_FALSE(manager()->IsProfileBefore(profile2, profile1)); |
| 3668 | } |
| 3669 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 3670 | } // namespace shill |