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