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