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 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 7 | #include <set> |
| 8 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 9 | #include <glib.h> |
| 10 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/scoped_temp_dir.h> |
| 13 | #include <base/stl_util.h> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 14 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 18 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 19 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 20 | #include "shill/ephemeral_profile.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 21 | #include "shill/error.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 22 | #include "shill/glib.h" |
| 23 | #include "shill/key_file_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 24 | #include "shill/key_value_store.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 25 | #include "shill/mock_adaptors.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 26 | #include "shill/mock_connection.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 27 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 28 | #include "shill/mock_device.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 29 | #include "shill/mock_device_info.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 30 | #include "shill/mock_glib.h" |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 31 | #include "shill/mock_metrics.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/mock_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 34 | #include "shill/mock_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 35 | #include "shill/mock_wifi.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 37 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | #include "shill/wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | |
| 40 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 41 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 46 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 47 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 48 | using ::testing::ContainerEq; |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 49 | using ::testing::InSequence; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 50 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 51 | using ::testing::NiceMock; |
| 52 | using ::testing::Return; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 53 | using ::testing::ReturnRef; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 54 | using ::testing::StrEq; |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 55 | using ::testing::StrictMock; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 56 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 57 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 58 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 59 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 60 | ManagerTest() |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 61 | : mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 62 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 63 | metrics(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 64 | manager(), |
| 65 | "wifi0", |
| 66 | "addr4", |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 67 | 4)), |
| 68 | device_info_(new NiceMock<MockDeviceInfo>( |
| 69 | control_interface(), |
| 70 | reinterpret_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 71 | reinterpret_cast<Metrics*>(NULL), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 72 | reinterpret_cast<Manager*>(NULL))), |
| 73 | manager_adaptor_(new NiceMock<ManagerMockAdaptor>()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 74 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 75 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 76 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 77 | manager(), |
| 78 | "null0", |
| 79 | "addr0", |
| 80 | 0)); |
| 81 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 82 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 83 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 84 | manager(), |
| 85 | "null1", |
| 86 | "addr1", |
| 87 | 1)); |
| 88 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 89 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 91 | manager(), |
| 92 | "null2", |
| 93 | "addr2", |
| 94 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 95 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 96 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 97 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 98 | manager(), |
| 99 | "null3", |
| 100 | "addr3", |
| 101 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 102 | manager()->connect_profiles_to_rpc_ = false; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 103 | |
| 104 | // Replace the manager's adaptor with a quieter one, and one |
| 105 | // we can do EXPECT*() against. Passes ownership. |
| 106 | manager()->adaptor_.reset(manager_adaptor_); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 107 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 108 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 109 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 110 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 111 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 112 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 113 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 114 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 115 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 116 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 117 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 118 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 119 | manager->profiles_.push_back(profile); |
| 120 | } |
| 121 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 122 | ProfileRefPtr GetEphemeralProfile(Manager *manager) { |
| 123 | return manager->ephemeral_profile_; |
| 124 | } |
| 125 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 126 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 127 | Profile::Identifier id("rather", "irrelevant"); |
| 128 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 129 | manager, |
| 130 | id, |
| 131 | "", |
| 132 | false)); |
| 133 | FilePath final_path(storage_path()); |
| 134 | final_path = final_path.Append("test.profile"); |
| 135 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 136 | storage->set_path(final_path); |
| 137 | if (!storage->Open()) |
| 138 | return NULL; |
| 139 | profile->set_storage(storage.release()); // Passes ownership. |
| 140 | return profile.release(); |
| 141 | } |
| 142 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 143 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 144 | const string &profile_identifier, |
| 145 | const string &service_name) { |
| 146 | GLib glib; |
| 147 | KeyFileStore store(&glib); |
| 148 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 149 | return store.Open() && |
| 150 | store.SetString(service_name, "rather", "irrelevant") && |
| 151 | store.Close(); |
| 152 | } |
| 153 | |
| 154 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 155 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 156 | string path; |
| 157 | manager->CreateProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 158 | return error.type(); |
| 159 | } |
| 160 | |
| 161 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 162 | Error error; |
| 163 | manager->PopAnyProfile(&error); |
| 164 | return error.type(); |
| 165 | } |
| 166 | |
| 167 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 168 | Error error; |
| 169 | manager->PopProfile(name, &error); |
| 170 | return error.type(); |
| 171 | } |
| 172 | |
| 173 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 174 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 175 | string path; |
| 176 | manager->PushProfile(name, &path, &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 177 | return error.type(); |
| 178 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 179 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 180 | protected: |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 181 | typedef scoped_refptr<MockService> MockServiceRefPtr; |
| 182 | |
| 183 | MockServiceRefPtr MakeAutoConnectableService() { |
| 184 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 185 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 186 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 187 | manager()); |
| 188 | service->MakeFavorite(); |
| 189 | service->set_connectable(true); |
| 190 | return service; |
| 191 | } |
| 192 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 193 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 194 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 195 | scoped_ptr<MockDeviceInfo> device_info_; |
| 196 | |
| 197 | // This pointer is owned by the manager, and only tracked here for EXPECT*() |
| 198 | ManagerMockAdaptor *manager_adaptor_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 201 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
| 202 | return (svc0.get() == manager()->services_[0].get() && |
| 203 | svc1.get() == manager()->services_[1].get()); |
| 204 | } |
| 205 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 206 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 207 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 208 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 211 | TEST_F(ManagerTest, DeviceRegistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 212 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 213 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 214 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 215 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 216 | ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular)) |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 217 | .WillByDefault(Return(true)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 218 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 219 | manager()->RegisterDevice(mock_devices_[0]); |
| 220 | manager()->RegisterDevice(mock_devices_[1]); |
| 221 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 222 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 223 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 224 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 225 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 228 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 229 | manager()->running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame^] | 230 | mock_devices_[0]->enabled_persistent_ = true; |
| 231 | mock_devices_[1]->enabled_persistent_ = false; |
| 232 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(true)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 233 | .Times(1); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame^] | 234 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(_)) |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 235 | .Times(0); |
| 236 | manager()->RegisterDevice(mock_devices_[0]); |
| 237 | manager()->RegisterDevice(mock_devices_[1]); |
| 238 | } |
| 239 | |
| 240 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 241 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 242 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 243 | AdoptProfile(manager(), profile); // Passes ownership. |
| 244 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
| 245 | EXPECT_CALL(*profile, Save()); |
| 246 | manager()->RegisterDevice(mock_devices_[0]); |
| 247 | } |
| 248 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 249 | TEST_F(ManagerTest, DeviceDeregistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 250 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 251 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 252 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 253 | .WillByDefault(Return(true)); |
| 254 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 255 | manager()->RegisterDevice(mock_devices_[0]); |
| 256 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 257 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 258 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 259 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 260 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame^] | 261 | EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 262 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 263 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 264 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame^] | 265 | EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 266 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 267 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 271 | // It's much easier and safer to use a real GLib for this test. |
| 272 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 273 | Manager manager(control_interface(), |
| 274 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 275 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 276 | &glib, |
| 277 | run_path(), |
| 278 | storage_path(), |
| 279 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 280 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 281 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 282 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 283 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 284 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 285 | new NiceMock<MockService>(control_interface(), |
| 286 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 287 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 288 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 289 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 290 | new NiceMock<MockService>(control_interface(), |
| 291 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 292 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 293 | &manager)); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 294 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 295 | string service1_name(mock_service->UniqueName()); |
| 296 | string service2_name(mock_service2->UniqueName()); |
| 297 | |
| 298 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 299 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 300 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 301 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 302 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 303 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 304 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 305 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 306 | manager.RegisterService(mock_service); |
| 307 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 308 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 309 | Error error; |
| 310 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 311 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 312 | EXPECT_EQ(2, ids.size()); |
| 313 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 314 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 315 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 316 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 317 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 318 | |
| 319 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 322 | TEST_F(ManagerTest, RegisterKnownService) { |
| 323 | // It's much easier and safer to use a real GLib for this test. |
| 324 | GLib glib; |
| 325 | Manager manager(control_interface(), |
| 326 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 327 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 328 | &glib, |
| 329 | run_path(), |
| 330 | storage_path(), |
| 331 | string()); |
| 332 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 333 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 334 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 335 | { |
| 336 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 337 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 338 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 339 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 340 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 341 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 342 | } // Force destruction of service1. |
| 343 | |
| 344 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 345 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 346 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 347 | &manager)); |
| 348 | manager.RegisterService(service2); |
| 349 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 350 | manager.Stop(); |
| 351 | } |
| 352 | |
| 353 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 354 | // It's much easier and safer to use a real GLib for this test. |
| 355 | GLib glib; |
| 356 | Manager manager(control_interface(), |
| 357 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 358 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 359 | &glib, |
| 360 | run_path(), |
| 361 | storage_path(), |
| 362 | string()); |
| 363 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 364 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 365 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 366 | { |
| 367 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 368 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 369 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 370 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 371 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 372 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 373 | } // Force destruction of service1. |
| 374 | scoped_refptr<MockService> mock_service2( |
| 375 | new NiceMock<MockService>(control_interface(), |
| 376 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 377 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 378 | &manager)); |
| 379 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 380 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 381 | manager.RegisterService(mock_service2); |
| 382 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 383 | manager.Stop(); |
| 384 | } |
| 385 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 386 | TEST_F(ManagerTest, DeregisterUnregisteredService) { |
| 387 | // WiFi assumes that it can deregister a service that is not |
| 388 | // registered. (E.g. a hidden service can be deregistered when it |
| 389 | // loses its last endpoint, and again when WiFi is Stop()-ed.) |
| 390 | // |
| 391 | // So test that doing so doesn't cause a crash. |
| 392 | MockServiceRefPtr service = new NiceMock<MockService>(control_interface(), |
| 393 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 394 | metrics(), |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 395 | manager()); |
| 396 | manager()->DeregisterService(service); |
| 397 | } |
| 398 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 399 | TEST_F(ManagerTest, GetProperties) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 400 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 401 | AdoptProfile(manager(), profile); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 402 | map<string, ::DBus::Variant> props; |
| 403 | Error error(Error::kInvalidProperty, ""); |
| 404 | { |
| 405 | ::DBus::Error dbus_error; |
| 406 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 407 | manager()->mutable_store()->SetStringProperty( |
| 408 | flimflam::kCheckPortalListProperty, |
| 409 | expected, |
| 410 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 411 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 412 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 413 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 414 | expected); |
| 415 | } |
| 416 | { |
| 417 | ::DBus::Error dbus_error; |
| 418 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 419 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 420 | expected, |
| 421 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 422 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 423 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 424 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 425 | expected); |
| 426 | } |
| 427 | } |
| 428 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 429 | TEST_F(ManagerTest, GetDevicesProperty) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 430 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 431 | AdoptProfile(manager(), profile); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 432 | manager()->RegisterDevice(mock_devices_[0]); |
| 433 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 434 | { |
| 435 | map<string, ::DBus::Variant> props; |
| 436 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 437 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 438 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 439 | Strings devices = |
| 440 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 441 | EXPECT_EQ(2, devices.size()); |
| 442 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 443 | } |
| 444 | |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 445 | TEST_F(ManagerTest, GetServicesProperty) { |
| 446 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
| 447 | AdoptProfile(manager(), profile); |
| 448 | map<string, ::DBus::Variant> props; |
| 449 | ::DBus::Error dbus_error; |
| 450 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
| 451 | map<string, ::DBus::Variant>::const_iterator prop = |
| 452 | props.find(flimflam::kServicesProperty); |
| 453 | ASSERT_FALSE(prop == props.end()); |
| 454 | const ::DBus::Variant &variant = prop->second; |
| 455 | ASSERT_TRUE(DBusAdaptor::IsPaths(variant.signature())); |
| 456 | } |
| 457 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 458 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 459 | Manager manager(control_interface(), |
| 460 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 461 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 462 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 463 | run_path(), |
| 464 | storage_path(), |
| 465 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 466 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 467 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 468 | metrics(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 469 | &manager)); |
| 470 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 471 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 472 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 473 | ProfileRefPtr profile( |
| 474 | new Profile(control_interface(), &manager, id, "", false)); |
| 475 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 476 | // Say we don't have |s2| the first time asked, then that we do. |
| 477 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
| 478 | .WillOnce(Return(false)) |
| 479 | .WillRepeatedly(Return(true)); |
| 480 | EXPECT_CALL(*storage, Flush()) |
| 481 | .Times(AnyNumber()) |
| 482 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 483 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 484 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 485 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 486 | // Create a profile that already has |s2| in it. |
| 487 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
| 488 | profile->AdoptService(s2); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 489 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 490 | // Now, move the Service |s2| to another profile. |
| 491 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 492 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 493 | |
| 494 | // Force destruction of the original Profile, to ensure that the Service |
| 495 | // is kept alive and populated with data. |
| 496 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 497 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 498 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 501 | TEST_F(ManagerTest, SetProfileForService) { |
| 502 | scoped_refptr<MockProfile> profile0( |
| 503 | new MockProfile(control_interface(), manager(), "")); |
| 504 | string profile_name0("profile0"); |
| 505 | EXPECT_CALL(*profile0, GetRpcIdentifier()) |
| 506 | .WillRepeatedly(Return(profile_name0)); |
| 507 | AdoptProfile(manager(), profile0); |
| 508 | scoped_refptr<MockService> service(new MockService(control_interface(), |
| 509 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 510 | metrics(), |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 511 | manager())); |
| 512 | service->set_profile(profile0); |
| 513 | |
| 514 | { |
| 515 | Error error; |
| 516 | manager()->SetProfileForService(service, "foo", &error); |
| 517 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 518 | EXPECT_EQ("Unknown Profile requested for Service", error.message()); |
| 519 | } |
| 520 | |
| 521 | { |
| 522 | Error error; |
| 523 | manager()->SetProfileForService(service, profile_name0, &error); |
| 524 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 525 | EXPECT_EQ("Service is already connected to this profile", error.message()); |
| 526 | } |
| 527 | |
| 528 | scoped_refptr<MockProfile> profile1( |
| 529 | new MockProfile(control_interface(), manager(), "")); |
| 530 | string profile_name1("profile1"); |
| 531 | EXPECT_CALL(*profile1, GetRpcIdentifier()) |
| 532 | .WillRepeatedly(Return(profile_name1)); |
| 533 | AdoptProfile(manager(), profile1); |
| 534 | |
| 535 | { |
| 536 | Error error; |
| 537 | EXPECT_CALL(*profile1, AdoptService(_)) |
| 538 | .WillOnce(Return(true)); |
| 539 | EXPECT_CALL(*profile0, AbandonService(_)) |
| 540 | .WillOnce(Return(true)); |
| 541 | manager()->SetProfileForService(service, profile_name1, &error); |
| 542 | EXPECT_TRUE(error.IsSuccess()); |
| 543 | } |
| 544 | } |
| 545 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 546 | TEST_F(ManagerTest, CreateProfile) { |
| 547 | // It's much easier to use real Glib here since we want the storage |
| 548 | // side-effects. |
| 549 | GLib glib; |
| 550 | ScopedTempDir temp_dir; |
| 551 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 552 | |
| 553 | Manager manager(control_interface(), |
| 554 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 555 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 556 | &glib, |
| 557 | run_path(), |
| 558 | storage_path(), |
| 559 | temp_dir.path().value()); |
| 560 | |
| 561 | // Invalid name should be rejected. |
| 562 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 563 | |
| 564 | // Valid name is still rejected because we can't create a profile |
| 565 | // that doesn't have a user component. Such profile names are |
| 566 | // reserved for the single DefaultProfile the manager creates |
| 567 | // at startup. |
| 568 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid")); |
| 569 | |
| 570 | // We should succeed in creating a valid user profile. |
| 571 | const char kProfile[] = "~user/profile"; |
| 572 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile)); |
| 573 | |
| 574 | // We should fail in creating it a second time (already exists). |
| 575 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 576 | } |
| 577 | |
| 578 | TEST_F(ManagerTest, PushPopProfile) { |
| 579 | // It's much easier to use real Glib in creating a Manager for this |
| 580 | // test here since we want the storage side-effects. |
| 581 | GLib glib; |
| 582 | ScopedTempDir temp_dir; |
| 583 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 584 | Manager manager(control_interface(), |
| 585 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 586 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 587 | &glib, |
| 588 | run_path(), |
| 589 | storage_path(), |
| 590 | temp_dir.path().value()); |
| 591 | |
| 592 | // Pushing an invalid profile should fail. |
| 593 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 594 | |
| 595 | // Pushing a default profile name should fail. |
| 596 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default")); |
| 597 | |
| 598 | const char kProfile0[] = "~user/profile0"; |
| 599 | const char kProfile1[] = "~user/profile1"; |
| 600 | |
| 601 | // Create a couple of profiles. |
| 602 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 603 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 604 | |
| 605 | // Push these profiles on the stack. |
| 606 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 607 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 608 | |
| 609 | // Pushing a profile a second time should fail. |
| 610 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 611 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 612 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 613 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 614 | // Active profile should be the last one we pushed. |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 615 | EXPECT_EQ(kProfile1, "~" + manager.ActiveProfile()->GetFriendlyName()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 616 | |
| 617 | // Make sure a profile name that doesn't exist fails. |
| 618 | const char kProfile2Id[] = "profile2"; |
| 619 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 620 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 621 | |
| 622 | // Create a new service, with a specific storage name. |
| 623 | scoped_refptr<MockService> service( |
| 624 | new NiceMock<MockService>(control_interface(), |
| 625 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 626 | metrics(), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 627 | &manager)); |
| 628 | const char kServiceName[] = "service_storage_name"; |
| 629 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 630 | .WillRepeatedly(Return(kServiceName)); |
| 631 | EXPECT_CALL(*service.get(), Load(_)) |
| 632 | .WillRepeatedly(Return(true)); |
| 633 | |
| 634 | // Add this service to the manager -- it should end up in the ephemeral |
| 635 | // profile. |
| 636 | manager.RegisterService(service); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 637 | ASSERT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 638 | |
| 639 | // Create storage for a profile that contains the service storage name. |
| 640 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 641 | kServiceName)); |
| 642 | |
| 643 | // When we push the profile, the service should move away from the |
| 644 | // ephemeral profile to this new profile since it has an entry for |
| 645 | // this service. |
| 646 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 647 | EXPECT_NE(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 648 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 649 | |
| 650 | // Insert another profile that should supersede ownership of the service. |
| 651 | const char kProfile3Id[] = "profile3"; |
| 652 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 653 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 654 | kServiceName)); |
| 655 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 656 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 657 | |
| 658 | // Popping an invalid profile name should fail. |
| 659 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 660 | |
| 661 | // Popping an profile that is not at the top of the stack should fail. |
| 662 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 663 | |
| 664 | // Popping the top profile should succeed. |
| 665 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 666 | |
| 667 | // Moreover the service should have switched profiles to profile 2. |
| 668 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 669 | |
| 670 | // Popping the top profile should succeed. |
| 671 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 672 | |
| 673 | // The service should now revert to the ephemeral profile. |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 674 | EXPECT_EQ(GetEphemeralProfile(&manager), service->profile()); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 675 | |
| 676 | // Pop the remaining two services off the stack. |
| 677 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 678 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 679 | |
| 680 | // Next pop should fail with "stack is empty". |
| 681 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
| 682 | } |
| 683 | |
Paul Stewart | 7522551 | 2012-01-26 22:51:33 -0800 | [diff] [blame] | 684 | // Use this matcher instead of passing RefPtrs directly into the arguments |
| 685 | // of EXPECT_CALL() because otherwise we may create un-cleaned-up references at |
| 686 | // system teardown. |
| 687 | MATCHER_P(IsRefPtrTo, ref_address, "") { |
| 688 | return arg.get() == ref_address; |
| 689 | } |
| 690 | |
| 691 | TEST_F(ManagerTest, HandleProfileEntryDeletion) { |
| 692 | MockServiceRefPtr s_not_in_profile( |
| 693 | new NiceMock<MockService>(control_interface(), |
| 694 | dispatcher(), |
| 695 | metrics(), |
| 696 | manager())); |
| 697 | MockServiceRefPtr s_not_in_group( |
| 698 | new NiceMock<MockService>(control_interface(), |
| 699 | dispatcher(), |
| 700 | metrics(), |
| 701 | manager())); |
| 702 | MockServiceRefPtr s_configure_fail( |
| 703 | new NiceMock<MockService>(control_interface(), |
| 704 | dispatcher(), |
| 705 | metrics(), |
| 706 | manager())); |
| 707 | MockServiceRefPtr s_configure_succeed( |
| 708 | new NiceMock<MockService>(control_interface(), |
| 709 | dispatcher(), |
| 710 | metrics(), |
| 711 | manager())); |
| 712 | |
| 713 | string entry_name("entry_name"); |
| 714 | EXPECT_CALL(*s_not_in_profile.get(), GetStorageIdentifier()).Times(0); |
| 715 | EXPECT_CALL(*s_not_in_group.get(), GetStorageIdentifier()) |
| 716 | .WillRepeatedly(Return("not_entry_name")); |
| 717 | EXPECT_CALL(*s_configure_fail.get(), GetStorageIdentifier()) |
| 718 | .WillRepeatedly(Return(entry_name)); |
| 719 | EXPECT_CALL(*s_configure_succeed.get(), GetStorageIdentifier()) |
| 720 | .WillRepeatedly(Return(entry_name)); |
| 721 | |
| 722 | manager()->RegisterService(s_not_in_profile); |
| 723 | manager()->RegisterService(s_not_in_group); |
| 724 | manager()->RegisterService(s_configure_fail); |
| 725 | manager()->RegisterService(s_configure_succeed); |
| 726 | |
| 727 | scoped_refptr<MockProfile> profile0( |
| 728 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 729 | scoped_refptr<MockProfile> profile1( |
| 730 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 731 | |
| 732 | s_not_in_group->set_profile(profile1); |
| 733 | s_configure_fail->set_profile(profile1); |
| 734 | s_configure_succeed->set_profile(profile1); |
| 735 | |
| 736 | AdoptProfile(manager(), profile0); |
| 737 | AdoptProfile(manager(), profile1); |
| 738 | |
| 739 | // No services are a member of this profile. |
| 740 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile0, entry_name)); |
| 741 | |
| 742 | // No services that are members of this profile have this entry name. |
| 743 | EXPECT_FALSE(manager()->HandleProfileEntryDeletion(profile1, "")); |
| 744 | |
| 745 | // Only services that are members of the profile and group will be abandoned. |
| 746 | EXPECT_CALL(*profile1.get(), |
| 747 | AbandonService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 748 | EXPECT_CALL(*profile1.get(), |
| 749 | AbandonService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 750 | EXPECT_CALL(*profile1.get(), |
| 751 | AbandonService(IsRefPtrTo(s_configure_fail.get()))) |
| 752 | .WillOnce(Return(true)); |
| 753 | EXPECT_CALL(*profile1.get(), |
| 754 | AbandonService(IsRefPtrTo(s_configure_succeed.get()))) |
| 755 | .WillOnce(Return(true)); |
| 756 | |
| 757 | // Never allow services to re-join profile1. |
| 758 | EXPECT_CALL(*profile1.get(), ConfigureService(_)) |
| 759 | .WillRepeatedly(Return(false)); |
| 760 | |
| 761 | // Only allow one of the members of the profile and group to successfully |
| 762 | // join profile0. |
| 763 | EXPECT_CALL(*profile0.get(), |
| 764 | ConfigureService(IsRefPtrTo(s_not_in_profile.get()))).Times(0); |
| 765 | EXPECT_CALL(*profile0.get(), |
| 766 | ConfigureService(IsRefPtrTo(s_not_in_group.get()))).Times(0); |
| 767 | EXPECT_CALL(*profile0.get(), |
| 768 | ConfigureService(IsRefPtrTo(s_configure_fail.get()))) |
| 769 | .WillOnce(Return(false)); |
| 770 | EXPECT_CALL(*profile0.get(), |
| 771 | ConfigureService(IsRefPtrTo(s_configure_succeed.get()))) |
| 772 | .WillOnce(Return(true)); |
| 773 | |
| 774 | // Expect the failed-to-configure service to have Unload() called on it. |
| 775 | EXPECT_CALL(*s_not_in_profile.get(), Unload()).Times(0); |
| 776 | EXPECT_CALL(*s_not_in_group.get(), Unload()).Times(0); |
| 777 | EXPECT_CALL(*s_configure_fail.get(), Unload()).Times(1); |
| 778 | EXPECT_CALL(*s_configure_succeed.get(), Unload()).Times(0); |
| 779 | |
| 780 | EXPECT_TRUE(manager()->HandleProfileEntryDeletion(profile1, entry_name)); |
| 781 | |
| 782 | EXPECT_EQ(GetEphemeralProfile(manager()), s_not_in_profile->profile().get()); |
| 783 | EXPECT_EQ(profile1, s_not_in_group->profile()); |
| 784 | EXPECT_EQ(GetEphemeralProfile(manager()), s_configure_fail->profile()); |
| 785 | |
| 786 | // Since we are using a MockProfile, the profile does not actually change, |
| 787 | // since ConfigureService was not actually called on the service. |
| 788 | EXPECT_EQ(profile1, s_configure_succeed->profile()); |
| 789 | } |
| 790 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 791 | TEST_F(ManagerTest, SetProperty) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 792 | { |
| 793 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 794 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 795 | flimflam::kOfflineModeProperty, |
| 796 | PropertyStoreTest::kBoolV, |
| 797 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 798 | } |
| 799 | { |
| 800 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 801 | EXPECT_TRUE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 802 | flimflam::kCountryProperty, |
| 803 | PropertyStoreTest::kStringV, |
| 804 | &error)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 805 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 806 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 807 | { |
| 808 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 809 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 810 | flimflam::kCountryProperty, |
| 811 | PropertyStoreTest::kBoolV, |
| 812 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 813 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 814 | } |
| 815 | { |
| 816 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 817 | EXPECT_FALSE(DBusAdaptor::SetProperty(manager()->mutable_store(), |
| 818 | flimflam::kOfflineModeProperty, |
| 819 | PropertyStoreTest::kStringV, |
| 820 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 821 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 822 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 823 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 824 | { |
| 825 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 826 | EXPECT_FALSE(DBusAdaptor::SetProperty( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 827 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 828 | flimflam::kEnabledTechnologiesProperty, |
| 829 | PropertyStoreTest::kStringsV, |
| 830 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 831 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 832 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 833 | } |
| 834 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 835 | TEST_F(ManagerTest, RequestScan) { |
| 836 | { |
| 837 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 838 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 839 | manager()->RegisterDevice(mock_devices_[1].get()); |
| 840 | EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 841 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 842 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
| 843 | EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 844 | .WillRepeatedly(Return(false)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 845 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 846 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | { |
| 850 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 851 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 852 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 853 | } |
| 854 | } |
| 855 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 856 | TEST_F(ManagerTest, GetServiceNoType) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 857 | KeyValueStore args; |
| 858 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 859 | manager()->GetService(args, &e); |
| 860 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 861 | EXPECT_EQ("must specify service type", e.message()); |
| 862 | } |
| 863 | |
| 864 | TEST_F(ManagerTest, GetServiceUnknownType) { |
| 865 | KeyValueStore args; |
| 866 | Error e; |
| 867 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeEthernet); |
| 868 | manager()->GetService(args, &e); |
| 869 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 870 | EXPECT_EQ("service type is unsupported", e.message()); |
| 871 | } |
| 872 | |
| 873 | TEST_F(ManagerTest, GetServiceNoWifiDevice) { |
| 874 | KeyValueStore args; |
| 875 | Error e; |
| 876 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 877 | manager()->GetService(args, &e); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 878 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 879 | EXPECT_EQ("no wifi devices available", e.message()); |
| 880 | } |
| 881 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 882 | TEST_F(ManagerTest, GetServiceWifi) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 883 | KeyValueStore args; |
| 884 | Error e; |
| 885 | WiFiServiceRefPtr wifi_service; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 886 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 887 | manager()->RegisterDevice(mock_wifi_); |
| 888 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 889 | .WillRepeatedly(Return(wifi_service)); |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 890 | manager()->GetService(args, &e); |
| 891 | EXPECT_TRUE(e.IsSuccess()); |
| 892 | } |
| 893 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 894 | TEST_F(ManagerTest, GetServiceVPNUnknownType) { |
| 895 | KeyValueStore args; |
| 896 | Error e; |
| 897 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 898 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 899 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 900 | EXPECT_FALSE(service); |
| 901 | } |
| 902 | |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 903 | TEST_F(ManagerTest, GetServiceVPN) { |
| 904 | KeyValueStore args; |
| 905 | Error e; |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 906 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 907 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 908 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 909 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 910 | ServiceRefPtr service = manager()->GetService(args, &e); |
| 911 | EXPECT_TRUE(e.IsSuccess()); |
| 912 | EXPECT_TRUE(service); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 915 | TEST_F(ManagerTest, TechnologyOrder) { |
| 916 | Error error; |
| 917 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 918 | string(flimflam::kTypeWifi), &error); |
| 919 | ASSERT_TRUE(error.IsSuccess()); |
| 920 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 921 | string(flimflam::kTypeEthernet) + "," + |
| 922 | string(flimflam::kTypeWifi)); |
| 923 | |
| 924 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 925 | string(flimflam::kTypeWifi), &error); |
| 926 | ASSERT_FALSE(error.IsSuccess()); |
| 927 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 928 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 929 | string(flimflam::kTypeWifi), |
| 930 | manager()->GetTechnologyOrder()); |
| 931 | } |
| 932 | |
| 933 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 934 | // TODO(quiche): Some of these tests would probably fit better in |
| 935 | // service_unittest, since the actual comparison of Services is |
| 936 | // implemented in Service. (crosbug.com/23370) |
| 937 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 938 | scoped_refptr<MockService> mock_service0( |
| 939 | new NiceMock<MockService>(control_interface(), |
| 940 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 941 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 942 | manager())); |
| 943 | scoped_refptr<MockService> mock_service1( |
| 944 | new NiceMock<MockService>(control_interface(), |
| 945 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 946 | metrics(), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 947 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 948 | |
| 949 | manager()->RegisterService(mock_service0); |
| 950 | manager()->RegisterService(mock_service1); |
| 951 | |
| 952 | // Services should already be sorted by UniqueName |
| 953 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 954 | |
| 955 | // Asking explictly to sort services should not change anything |
| 956 | manager()->SortServices(); |
| 957 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 958 | |
| 959 | // Two otherwise equal services should be reordered by strength |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 960 | mock_service1->SetStrength(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 961 | manager()->UpdateService(mock_service1); |
| 962 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 963 | |
| 964 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 965 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 966 | manager()->UpdateService(mock_service0); |
| 967 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 968 | |
| 969 | // Technology |
| 970 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi)) |
| 971 | .WillRepeatedly(Return(true)); |
| 972 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet)) |
| 973 | .WillRepeatedly(Return(true)); |
| 974 | // NB: Redefine default (false) return values so we don't use the default rule |
| 975 | // which makes the logs noisier |
| 976 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi))) |
| 977 | .WillRepeatedly(Return(false)); |
| 978 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet))) |
| 979 | .WillRepeatedly(Return(false)); |
| 980 | |
| 981 | Error error; |
mukesh agrawal | 84de5d2 | 2012-02-17 19:29:15 -0800 | [diff] [blame] | 982 | // Default technology ordering should favor Ethernet over WiFi. |
| 983 | manager()->SortServices(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 984 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 985 | |
| 986 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 987 | string(flimflam::kTypeEthernet), &error); |
| 988 | EXPECT_TRUE(error.IsSuccess()); |
| 989 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 990 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 991 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 992 | mock_service0->set_priority(1); |
| 993 | manager()->UpdateService(mock_service0); |
| 994 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 995 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 996 | // Favorite. |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 997 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 998 | manager()->UpdateService(mock_service1); |
| 999 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1000 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1001 | // Auto-connect. |
| 1002 | mock_service0->set_auto_connect(true); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1003 | manager()->UpdateService(mock_service0); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1004 | mock_service1->set_auto_connect(false); |
| 1005 | manager()->UpdateService(mock_service1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1006 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1007 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1008 | // Connectable. |
| 1009 | mock_service1->set_connectable(true); |
| 1010 | manager()->UpdateService(mock_service1); |
| 1011 | mock_service0->set_connectable(false); |
| 1012 | manager()->UpdateService(mock_service0); |
| 1013 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1014 | |
| 1015 | // IsFailed. |
| 1016 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1017 | .WillRepeatedly(Return(Service::kStateIdle)); |
| 1018 | EXPECT_CALL(*mock_service0.get(), IsFailed()) |
| 1019 | .WillRepeatedly(Return(false)); |
| 1020 | manager()->UpdateService(mock_service0); |
| 1021 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1022 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1023 | EXPECT_CALL(*mock_service1.get(), IsFailed()) |
| 1024 | .WillRepeatedly(Return(true)); |
| 1025 | manager()->UpdateService(mock_service1); |
| 1026 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1027 | |
| 1028 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1029 | EXPECT_CALL(*mock_service1.get(), state()) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1030 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 1031 | EXPECT_CALL(*mock_service1.get(), IsConnecting()) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1032 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1033 | manager()->UpdateService(mock_service1); |
| 1034 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 1035 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1036 | // Connected. |
| 1037 | EXPECT_CALL(*mock_service0.get(), state()) |
| 1038 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1039 | EXPECT_CALL(*mock_service0.get(), IsConnected()) |
| 1040 | .WillRepeatedly(Return(true)); |
| 1041 | manager()->UpdateService(mock_service0); |
| 1042 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 1043 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 1044 | manager()->DeregisterService(mock_service0); |
| 1045 | manager()->DeregisterService(mock_service1); |
| 1046 | } |
| 1047 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1048 | TEST_F(ManagerTest, SortServicesWithConnection) { |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1049 | MockMetrics mock_metrics; |
| 1050 | manager()->set_metrics(&mock_metrics); |
| 1051 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1052 | scoped_refptr<MockService> mock_service0( |
| 1053 | new NiceMock<MockService>(control_interface(), |
| 1054 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1055 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1056 | manager())); |
| 1057 | scoped_refptr<MockService> mock_service1( |
| 1058 | new NiceMock<MockService>(control_interface(), |
| 1059 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1060 | metrics(), |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1061 | manager())); |
| 1062 | |
| 1063 | scoped_refptr<MockConnection> mock_connection0( |
| 1064 | new NiceMock<MockConnection>(device_info_.get())); |
| 1065 | scoped_refptr<MockConnection> mock_connection1( |
| 1066 | new NiceMock<MockConnection>(device_info_.get())); |
| 1067 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1068 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1069 | manager()->RegisterService(mock_service0); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1070 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1071 | manager()->RegisterService(mock_service1); |
| 1072 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1073 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1074 | manager()->SortServices(); |
| 1075 | |
| 1076 | mock_service1->set_priority(1); |
| 1077 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1078 | manager()->SortServices(); |
| 1079 | |
| 1080 | mock_service1->set_priority(0); |
| 1081 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1082 | manager()->SortServices(); |
| 1083 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1084 | mock_service0->set_mock_connection(mock_connection0); |
| 1085 | mock_service1->set_mock_connection(mock_connection1); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1086 | |
| 1087 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1088 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1089 | manager()->SortServices(); |
| 1090 | |
| 1091 | mock_service1->set_priority(1); |
| 1092 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(false)); |
| 1093 | EXPECT_CALL(*mock_connection1.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1094 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service1.get())); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1095 | manager()->SortServices(); |
| 1096 | |
| 1097 | EXPECT_CALL(*mock_connection0.get(), SetIsDefault(true)); |
Thieu Le | a20cbc2 | 2012-01-09 22:01:43 +0000 | [diff] [blame] | 1098 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(mock_service0.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1099 | mock_service1->set_mock_connection(NULL); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1100 | manager()->DeregisterService(mock_service1); |
| 1101 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1102 | mock_service0->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1103 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1104 | manager()->DeregisterService(mock_service0); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1105 | |
| 1106 | EXPECT_CALL(mock_metrics, NotifyDefaultServiceChanged(NULL)); |
| 1107 | manager()->SortServices(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1110 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 1111 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 1112 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1113 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1114 | manager(), |
| 1115 | "null4", |
| 1116 | "addr4", |
| 1117 | 0)); |
| 1118 | manager()->RegisterDevice(mock_devices_[0]); |
| 1119 | manager()->RegisterDevice(mock_devices_[1]); |
| 1120 | manager()->RegisterDevice(mock_devices_[2]); |
| 1121 | manager()->RegisterDevice(mock_devices_[3]); |
| 1122 | |
| 1123 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1124 | .WillByDefault(Return(Technology::kEthernet)); |
| 1125 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1126 | .WillByDefault(Return(Technology::kWifi)); |
| 1127 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1128 | .WillByDefault(Return(Technology::kCellular)); |
| 1129 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1130 | .WillByDefault(Return(Technology::kWifi)); |
| 1131 | |
| 1132 | set<string> expected_technologies; |
| 1133 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1134 | Technology::kEthernet)); |
| 1135 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1136 | Technology::kWifi)); |
| 1137 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1138 | Technology::kCellular)); |
| 1139 | Error error; |
| 1140 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 1141 | |
| 1142 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1143 | ContainerEq(expected_technologies)); |
| 1144 | } |
| 1145 | |
| 1146 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 1147 | scoped_refptr<MockService> connected_service1( |
| 1148 | new NiceMock<MockService>(control_interface(), |
| 1149 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1150 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1151 | manager())); |
| 1152 | scoped_refptr<MockService> connected_service2( |
| 1153 | new NiceMock<MockService>(control_interface(), |
| 1154 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1155 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1156 | manager())); |
| 1157 | scoped_refptr<MockService> disconnected_service1( |
| 1158 | new NiceMock<MockService>(control_interface(), |
| 1159 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1160 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1161 | manager())); |
| 1162 | scoped_refptr<MockService> disconnected_service2( |
| 1163 | new NiceMock<MockService>(control_interface(), |
| 1164 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1165 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1166 | manager())); |
| 1167 | |
| 1168 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 1169 | .WillByDefault(Return(true)); |
| 1170 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 1171 | .WillByDefault(Return(true)); |
| 1172 | |
| 1173 | manager()->RegisterService(connected_service1); |
| 1174 | manager()->RegisterService(connected_service2); |
| 1175 | manager()->RegisterService(disconnected_service1); |
| 1176 | manager()->RegisterService(disconnected_service2); |
| 1177 | |
| 1178 | manager()->RegisterDevice(mock_devices_[0]); |
| 1179 | manager()->RegisterDevice(mock_devices_[1]); |
| 1180 | manager()->RegisterDevice(mock_devices_[2]); |
| 1181 | manager()->RegisterDevice(mock_devices_[3]); |
| 1182 | |
| 1183 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 1184 | .WillByDefault(Return(Technology::kEthernet)); |
| 1185 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 1186 | .WillByDefault(Return(Technology::kWifi)); |
| 1187 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 1188 | .WillByDefault(Return(Technology::kCellular)); |
| 1189 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 1190 | .WillByDefault(Return(Technology::kWifi)); |
| 1191 | |
| 1192 | mock_devices_[0]->SelectService(connected_service1); |
| 1193 | mock_devices_[1]->SelectService(disconnected_service1); |
| 1194 | mock_devices_[2]->SelectService(disconnected_service2); |
| 1195 | mock_devices_[3]->SelectService(connected_service2); |
| 1196 | |
| 1197 | set<string> expected_technologies; |
| 1198 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1199 | Technology::kEthernet)); |
| 1200 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 1201 | Technology::kWifi)); |
| 1202 | Error error; |
| 1203 | |
| 1204 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 1205 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 1206 | ContainerEq(expected_technologies)); |
| 1207 | } |
| 1208 | |
| 1209 | TEST_F(ManagerTest, DefaultTechnology) { |
| 1210 | scoped_refptr<MockService> connected_service( |
| 1211 | new NiceMock<MockService>(control_interface(), |
| 1212 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1213 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1214 | manager())); |
| 1215 | scoped_refptr<MockService> disconnected_service( |
| 1216 | new NiceMock<MockService>(control_interface(), |
| 1217 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1218 | metrics(), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1219 | manager())); |
| 1220 | |
| 1221 | // Connected. WiFi. |
| 1222 | ON_CALL(*connected_service.get(), IsConnected()) |
| 1223 | .WillByDefault(Return(true)); |
| 1224 | ON_CALL(*connected_service.get(), state()) |
| 1225 | .WillByDefault(Return(Service::kStateConnected)); |
| 1226 | ON_CALL(*connected_service.get(), technology()) |
| 1227 | .WillByDefault(Return(Technology::kWifi)); |
| 1228 | |
| 1229 | // Disconnected. Ethernet. |
| 1230 | ON_CALL(*disconnected_service.get(), technology()) |
| 1231 | .WillByDefault(Return(Technology::kEthernet)); |
| 1232 | |
| 1233 | manager()->RegisterService(disconnected_service); |
| 1234 | Error error; |
| 1235 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 1236 | |
| 1237 | |
| 1238 | manager()->RegisterService(connected_service); |
| 1239 | // Connected service should be brought to the front now. |
| 1240 | string expected_technology = |
| 1241 | Technology::NameFromIdentifier(Technology::kWifi); |
| 1242 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 1243 | } |
| 1244 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1245 | TEST_F(ManagerTest, DisconnectServicesOnStop) { |
| 1246 | scoped_refptr<MockService> mock_service( |
| 1247 | new NiceMock<MockService>(control_interface(), |
| 1248 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1249 | metrics(), |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1250 | manager())); |
| 1251 | manager()->RegisterService(mock_service); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 1252 | EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 1253 | manager()->Stop(); |
| 1254 | } |
| 1255 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1256 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 1257 | scoped_refptr<MockService> mock_service( |
| 1258 | new NiceMock<MockService>(control_interface(), |
| 1259 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1260 | metrics(), |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1261 | manager())); |
| 1262 | manager()->RegisterService(mock_service); |
| 1263 | EXPECT_FALSE(mock_service->favorite()); |
| 1264 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1265 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 1266 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1267 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 1268 | manager()->UpdateService(mock_service); |
| 1269 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1270 | // to mock out MakeFavorite. And mocking that out would break the |
| 1271 | // SortServices test. (crosbug.com/23370) |
| 1272 | EXPECT_TRUE(mock_service->favorite()); |
| 1273 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1274 | } |
| 1275 | |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 1276 | TEST_F(ManagerTest, UpdateServiceConnectedPersistFavorite) { |
| 1277 | // This tests the case where the user connects to a service that is |
| 1278 | // currently associated with a profile. We want to make sure that the |
| 1279 | // favorite flag is set and that the flag is saved to the current |
| 1280 | // profile. |
| 1281 | scoped_refptr<MockService> mock_service( |
| 1282 | new NiceMock<MockService>(control_interface(), |
| 1283 | dispatcher(), |
| 1284 | metrics(), |
| 1285 | manager())); |
| 1286 | manager()->RegisterService(mock_service); |
| 1287 | EXPECT_FALSE(mock_service->favorite()); |
| 1288 | EXPECT_FALSE(mock_service->auto_connect()); |
| 1289 | |
| 1290 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
| 1291 | mock_service->set_profile(profile); |
| 1292 | |
| 1293 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 1294 | .WillRepeatedly(Return(true)); |
| 1295 | EXPECT_CALL(*mock_service.get(), SaveToCurrentProfile()); |
| 1296 | manager()->UpdateService(mock_service); |
| 1297 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 1298 | // to mock out MakeFavorite. And mocking that out would break the |
| 1299 | // SortServices test. (crosbug.com/23370) |
| 1300 | EXPECT_TRUE(mock_service->favorite()); |
| 1301 | EXPECT_TRUE(mock_service->auto_connect()); |
| 1302 | } |
| 1303 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1304 | TEST_F(ManagerTest, SaveSuccessfulService) { |
| 1305 | scoped_refptr<MockProfile> profile( |
| 1306 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
| 1307 | AdoptProfile(manager(), profile); |
| 1308 | scoped_refptr<MockService> service( |
| 1309 | new NiceMock<MockService>(control_interface(), |
| 1310 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1311 | metrics(), |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 1312 | manager())); |
| 1313 | |
| 1314 | // Re-cast this back to a ServiceRefPtr, so EXPECT arguments work correctly. |
| 1315 | ServiceRefPtr expect_service(service.get()); |
| 1316 | |
| 1317 | EXPECT_CALL(*profile.get(), ConfigureService(expect_service)) |
| 1318 | .WillOnce(Return(false)); |
| 1319 | manager()->RegisterService(service); |
| 1320 | |
| 1321 | EXPECT_CALL(*service.get(), state()) |
| 1322 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 1323 | EXPECT_CALL(*service.get(), IsConnected()) |
| 1324 | .WillRepeatedly(Return(true)); |
| 1325 | EXPECT_CALL(*profile.get(), AdoptService(expect_service)) |
| 1326 | .WillOnce(Return(true)); |
| 1327 | manager()->UpdateService(service); |
| 1328 | } |
| 1329 | |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1330 | TEST_F(ManagerTest, EnumerateProfiles) { |
| 1331 | vector<string> profile_paths; |
| 1332 | for (size_t i = 0; i < 10; i++) { |
| 1333 | scoped_refptr<MockProfile> profile( |
| 1334 | new StrictMock<MockProfile>(control_interface(), manager(), "")); |
Jason Glasgow | 5d8197b | 2012-01-27 08:37:32 -0500 | [diff] [blame] | 1335 | profile_paths.push_back(base::StringPrintf("/profile/%zd", i)); |
Paul Stewart | 1b25314 | 2012-01-26 14:05:52 -0800 | [diff] [blame] | 1336 | EXPECT_CALL(*profile.get(), GetRpcIdentifier()) |
| 1337 | .WillOnce(Return(profile_paths.back())); |
| 1338 | AdoptProfile(manager(), profile); |
| 1339 | } |
| 1340 | |
| 1341 | Error error; |
| 1342 | vector<string> returned_paths = manager()->EnumerateProfiles(&error); |
| 1343 | EXPECT_TRUE(error.IsSuccess()); |
| 1344 | EXPECT_EQ(profile_paths.size(), returned_paths.size()); |
| 1345 | for (size_t i = 0; i < profile_paths.size(); i++) { |
| 1346 | EXPECT_EQ(profile_paths[i], returned_paths[i]); |
| 1347 | } |
| 1348 | } |
| 1349 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1350 | TEST_F(ManagerTest, AutoConnectOnRegister) { |
| 1351 | MockServiceRefPtr service = MakeAutoConnectableService(); |
| 1352 | EXPECT_CALL(*service.get(), AutoConnect()); |
| 1353 | manager()->RegisterService(service); |
| 1354 | dispatcher()->DispatchPendingEvents(); |
| 1355 | } |
| 1356 | |
| 1357 | TEST_F(ManagerTest, AutoConnectOnUpdate) { |
| 1358 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1359 | service1->set_priority(1); |
| 1360 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1361 | service2->set_priority(2); |
| 1362 | manager()->RegisterService(service1); |
| 1363 | manager()->RegisterService(service2); |
| 1364 | dispatcher()->DispatchPendingEvents(); |
| 1365 | |
| 1366 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1367 | EXPECT_CALL(*service2.get(), state()) |
| 1368 | .WillRepeatedly(Return(Service::kStateFailure)); |
| 1369 | EXPECT_CALL(*service2.get(), IsFailed()) |
| 1370 | .WillRepeatedly(Return(true)); |
| 1371 | EXPECT_CALL(*service2.get(), IsConnected()) |
| 1372 | .WillRepeatedly(Return(false)); |
| 1373 | manager()->UpdateService(service2); |
| 1374 | dispatcher()->DispatchPendingEvents(); |
| 1375 | } |
| 1376 | |
| 1377 | TEST_F(ManagerTest, AutoConnectOnDeregister) { |
| 1378 | MockServiceRefPtr service1 = MakeAutoConnectableService(); |
| 1379 | service1->set_priority(1); |
| 1380 | MockServiceRefPtr service2 = MakeAutoConnectableService(); |
| 1381 | service2->set_priority(2); |
| 1382 | manager()->RegisterService(service1); |
| 1383 | manager()->RegisterService(service2); |
| 1384 | dispatcher()->DispatchPendingEvents(); |
| 1385 | |
| 1386 | EXPECT_CALL(*service1.get(), AutoConnect()); |
| 1387 | manager()->DeregisterService(service2); |
| 1388 | dispatcher()->DispatchPendingEvents(); |
| 1389 | } |
| 1390 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1391 | TEST_F(ManagerTest, RecheckPortal) { |
| 1392 | EXPECT_CALL(*mock_devices_[0].get(), RequestPortalDetection()) |
| 1393 | .WillOnce(Return(false)); |
| 1394 | EXPECT_CALL(*mock_devices_[1].get(), RequestPortalDetection()) |
| 1395 | .WillOnce(Return(true)); |
| 1396 | EXPECT_CALL(*mock_devices_[2].get(), RequestPortalDetection()) |
| 1397 | .Times(0); |
| 1398 | |
| 1399 | manager()->RegisterDevice(mock_devices_[0]); |
| 1400 | manager()->RegisterDevice(mock_devices_[1]); |
| 1401 | manager()->RegisterDevice(mock_devices_[2]); |
| 1402 | |
| 1403 | manager()->RecheckPortal(NULL); |
| 1404 | } |
| 1405 | |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1406 | TEST_F(ManagerTest, GetDefaultService) { |
| 1407 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
| 1408 | |
| 1409 | scoped_refptr<MockService> mock_service( |
| 1410 | new NiceMock<MockService>(control_interface(), |
| 1411 | dispatcher(), |
| 1412 | metrics(), |
| 1413 | manager())); |
| 1414 | |
| 1415 | manager()->RegisterService(mock_service); |
| 1416 | EXPECT_FALSE(manager()->GetDefaultService().get()); |
| 1417 | |
| 1418 | scoped_refptr<MockConnection> mock_connection( |
| 1419 | new NiceMock<MockConnection>(device_info_.get())); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1420 | mock_service->set_mock_connection(mock_connection); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1421 | EXPECT_EQ(mock_service.get(), manager()->GetDefaultService().get()); |
| 1422 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 1423 | mock_service->set_mock_connection(NULL); |
Paul Stewart | e2bad7c | 2012-03-14 08:55:33 -0700 | [diff] [blame] | 1424 | manager()->DeregisterService(mock_service); |
| 1425 | } |
| 1426 | |
Paul Stewart | 13ed225 | 2012-03-21 12:52:46 -0700 | [diff] [blame] | 1427 | TEST_F(ManagerTest, GetServiceWithGUID) { |
| 1428 | scoped_refptr<MockService> mock_service0( |
| 1429 | new NiceMock<MockService>(control_interface(), |
| 1430 | dispatcher(), |
| 1431 | metrics(), |
| 1432 | manager())); |
| 1433 | |
| 1434 | scoped_refptr<MockService> mock_service1( |
| 1435 | new NiceMock<MockService>(control_interface(), |
| 1436 | dispatcher(), |
| 1437 | metrics(), |
| 1438 | manager())); |
| 1439 | |
| 1440 | manager()->RegisterService(mock_service0); |
| 1441 | manager()->RegisterService(mock_service1); |
| 1442 | |
| 1443 | const string kGUID0 = "GUID0"; |
| 1444 | const string kGUID1 = "GUID1"; |
| 1445 | |
| 1446 | { |
| 1447 | Error error; |
| 1448 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 1449 | EXPECT_FALSE(error.IsSuccess()); |
| 1450 | EXPECT_FALSE(service); |
| 1451 | } |
| 1452 | |
| 1453 | KeyValueStore args; |
| 1454 | args.SetString(flimflam::kGuidProperty, kGUID1); |
| 1455 | |
| 1456 | { |
| 1457 | Error error; |
| 1458 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 1459 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 1460 | EXPECT_FALSE(service); |
| 1461 | } |
| 1462 | |
| 1463 | mock_service0->set_guid(kGUID0); |
| 1464 | mock_service1->set_guid(kGUID1); |
| 1465 | |
| 1466 | { |
| 1467 | Error error; |
| 1468 | ServiceRefPtr service = manager()->GetServiceWithGUID(kGUID0, &error); |
| 1469 | EXPECT_TRUE(error.IsSuccess()); |
| 1470 | EXPECT_EQ(mock_service0.get(), service.get()); |
| 1471 | } |
| 1472 | |
| 1473 | { |
| 1474 | Error error; |
| 1475 | ServiceRefPtr service = manager()->GetService(args, &error); |
| 1476 | EXPECT_TRUE(error.IsSuccess()); |
| 1477 | EXPECT_EQ(mock_service1.get(), service.get()); |
| 1478 | } |
| 1479 | |
| 1480 | manager()->DeregisterService(mock_service0); |
| 1481 | manager()->DeregisterService(mock_service1); |
| 1482 | } |
| 1483 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1484 | } // namespace shill |