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