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