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