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