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