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