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