Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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> |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_temp_dir.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 13 | #include <base/stl_util-inl.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" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 26 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 27 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 28 | #include "shill/mock_glib.h" |
| 29 | #include "shill/mock_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 30 | #include "shill/mock_service.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 31 | #include "shill/mock_store.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 32 | #include "shill/mock_wifi.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 33 | #include "shill/property_store_unittest.h" |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 34 | #include "shill/service_under_test.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 35 | #include "shill/wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | |
| 37 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 38 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | using std::string; |
| 40 | using std::vector; |
| 41 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 42 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 43 | using ::testing::_; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 44 | using ::testing::AnyNumber; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 45 | using ::testing::ContainerEq; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 46 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 47 | using ::testing::NiceMock; |
| 48 | using ::testing::Return; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 49 | using ::testing::StrEq; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 50 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 51 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 53 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 54 | ManagerTest() |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 55 | : mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 56 | dispatcher(), |
| 57 | manager(), |
| 58 | "wifi0", |
| 59 | "addr4", |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 60 | 4)) { |
| 61 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 62 | dispatcher(), |
| 63 | manager(), |
| 64 | "null0", |
| 65 | "addr0", |
| 66 | 0)); |
| 67 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 68 | dispatcher(), |
| 69 | manager(), |
| 70 | "null1", |
| 71 | "addr1", |
| 72 | 1)); |
| 73 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 74 | dispatcher(), |
| 75 | manager(), |
| 76 | "null2", |
| 77 | "addr2", |
| 78 | 2)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 79 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 80 | dispatcher(), |
| 81 | manager(), |
| 82 | "null3", |
| 83 | "addr3", |
| 84 | 3)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 85 | manager()->connect_profiles_to_rpc_ = false; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 86 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 87 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 88 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 89 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 90 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 91 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 92 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 93 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 94 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 95 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 96 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 97 | void AdoptProfile(Manager *manager, ProfileRefPtr profile) { |
| 98 | manager->profiles_.push_back(profile); |
| 99 | } |
| 100 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 101 | Profile *CreateProfileForManager(Manager *manager, GLib *glib) { |
| 102 | Profile::Identifier id("rather", "irrelevant"); |
| 103 | scoped_ptr<Profile> profile(new Profile(control_interface(), |
| 104 | manager, |
| 105 | id, |
| 106 | "", |
| 107 | false)); |
| 108 | FilePath final_path(storage_path()); |
| 109 | final_path = final_path.Append("test.profile"); |
| 110 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 111 | storage->set_path(final_path); |
| 112 | if (!storage->Open()) |
| 113 | return NULL; |
| 114 | profile->set_storage(storage.release()); // Passes ownership. |
| 115 | return profile.release(); |
| 116 | } |
| 117 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 118 | bool CreateBackingStoreForService(ScopedTempDir *temp_dir, |
| 119 | const string &profile_identifier, |
| 120 | const string &service_name) { |
| 121 | GLib glib; |
| 122 | KeyFileStore store(&glib); |
| 123 | store.set_path(temp_dir->path().Append(profile_identifier + ".profile")); |
| 124 | return store.Open() && |
| 125 | store.SetString(service_name, "rather", "irrelevant") && |
| 126 | store.Close(); |
| 127 | } |
| 128 | |
| 129 | Error::Type TestCreateProfile(Manager *manager, const string &name) { |
| 130 | Error error; |
| 131 | manager->CreateProfile(name, &error); |
| 132 | return error.type(); |
| 133 | } |
| 134 | |
| 135 | Error::Type TestPopAnyProfile(Manager *manager) { |
| 136 | Error error; |
| 137 | manager->PopAnyProfile(&error); |
| 138 | return error.type(); |
| 139 | } |
| 140 | |
| 141 | Error::Type TestPopProfile(Manager *manager, const string &name) { |
| 142 | Error error; |
| 143 | manager->PopProfile(name, &error); |
| 144 | return error.type(); |
| 145 | } |
| 146 | |
| 147 | Error::Type TestPushProfile(Manager *manager, const string &name) { |
| 148 | Error error; |
| 149 | manager->PushProfile(name, &error); |
| 150 | return error.type(); |
| 151 | } |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 152 | protected: |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 153 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 154 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 157 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
| 158 | return (svc0.get() == manager()->services_[0].get() && |
| 159 | svc1.get() == manager()->services_[1].get()); |
| 160 | } |
| 161 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 162 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 164 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 167 | TEST_F(ManagerTest, DeviceRegistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 168 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 169 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 170 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 171 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 172 | ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular)) |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 173 | .WillByDefault(Return(true)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 174 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 175 | manager()->RegisterDevice(mock_devices_[0]); |
| 176 | manager()->RegisterDevice(mock_devices_[1]); |
| 177 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 178 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 179 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 180 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 181 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 184 | TEST_F(ManagerTest, DeviceRegistrationAndStart) { |
| 185 | manager()->running_ = true; |
| 186 | mock_devices_[0]->powered_ = true; |
| 187 | mock_devices_[1]->powered_ = false; |
| 188 | EXPECT_CALL(*mock_devices_[0].get(), Start()) |
| 189 | .Times(1); |
| 190 | EXPECT_CALL(*mock_devices_[1].get(), Start()) |
| 191 | .Times(0); |
| 192 | manager()->RegisterDevice(mock_devices_[0]); |
| 193 | manager()->RegisterDevice(mock_devices_[1]); |
| 194 | } |
| 195 | |
| 196 | TEST_F(ManagerTest, DeviceRegistrationWithProfile) { |
| 197 | MockProfile *profile = new MockProfile(control_interface(), manager(), ""); |
| 198 | DeviceRefPtr device_ref(mock_devices_[0].get()); |
| 199 | AdoptProfile(manager(), profile); // Passes ownership. |
| 200 | EXPECT_CALL(*profile, ConfigureDevice(device_ref)); |
| 201 | EXPECT_CALL(*profile, Save()); |
| 202 | manager()->RegisterDevice(mock_devices_[0]); |
| 203 | } |
| 204 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 205 | TEST_F(ManagerTest, DeviceDeregistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 206 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 207 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 208 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 209 | .WillByDefault(Return(true)); |
| 210 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 211 | manager()->RegisterDevice(mock_devices_[0]); |
| 212 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 213 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 214 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 215 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 216 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 217 | EXPECT_CALL(*mock_devices_[0].get(), Stop()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 218 | manager()->DeregisterDevice(mock_devices_[0]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 219 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 220 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 221 | EXPECT_CALL(*mock_devices_[1].get(), Stop()); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 222 | manager()->DeregisterDevice(mock_devices_[1]); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 223 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 227 | // It's much easier and safer to use a real GLib for this test. |
| 228 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 229 | Manager manager(control_interface(), |
| 230 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 231 | &glib, |
| 232 | run_path(), |
| 233 | storage_path(), |
| 234 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 235 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 236 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 237 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 238 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 239 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 240 | new NiceMock<MockService>(control_interface(), |
| 241 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 242 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 243 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 244 | new NiceMock<MockService>(control_interface(), |
| 245 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 246 | &manager)); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 247 | string service1_name(mock_service->UniqueName()); |
| 248 | string service2_name(mock_service2->UniqueName()); |
| 249 | |
| 250 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 251 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 252 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 253 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 254 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 255 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 256 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 257 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 258 | manager.RegisterService(mock_service); |
| 259 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 260 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 261 | Error error; |
| 262 | vector<string> rpc_ids = manager.EnumerateAvailableServices(&error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 263 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 264 | EXPECT_EQ(2, ids.size()); |
| 265 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 266 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 267 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 268 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 269 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 270 | |
| 271 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 274 | TEST_F(ManagerTest, RegisterKnownService) { |
| 275 | // It's much easier and safer to use a real GLib for this test. |
| 276 | GLib glib; |
| 277 | Manager manager(control_interface(), |
| 278 | dispatcher(), |
| 279 | &glib, |
| 280 | run_path(), |
| 281 | storage_path(), |
| 282 | string()); |
| 283 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 284 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 285 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 286 | { |
| 287 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 288 | dispatcher(), |
| 289 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 290 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 291 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 292 | } // Force destruction of service1. |
| 293 | |
| 294 | ServiceRefPtr service2(new ServiceUnderTest(control_interface(), |
| 295 | dispatcher(), |
| 296 | &manager)); |
| 297 | manager.RegisterService(service2); |
| 298 | EXPECT_EQ(service2->profile().get(), profile.get()); |
| 299 | manager.Stop(); |
| 300 | } |
| 301 | |
| 302 | TEST_F(ManagerTest, RegisterUnknownService) { |
| 303 | // It's much easier and safer to use a real GLib for this test. |
| 304 | GLib glib; |
| 305 | Manager manager(control_interface(), |
| 306 | dispatcher(), |
| 307 | &glib, |
| 308 | run_path(), |
| 309 | storage_path(), |
| 310 | string()); |
| 311 | ProfileRefPtr profile(CreateProfileForManager(&manager, &glib)); |
| 312 | ASSERT_TRUE(profile.get()); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 313 | AdoptProfile(&manager, profile); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 314 | { |
| 315 | ServiceRefPtr service1(new ServiceUnderTest(control_interface(), |
| 316 | dispatcher(), |
| 317 | &manager)); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 318 | ASSERT_TRUE(profile->AdoptService(service1)); |
| 319 | ASSERT_TRUE(profile->ContainsService(service1)); |
| 320 | } // Force destruction of service1. |
| 321 | scoped_refptr<MockService> mock_service2( |
| 322 | new NiceMock<MockService>(control_interface(), |
| 323 | dispatcher(), |
| 324 | &manager)); |
| 325 | EXPECT_CALL(*mock_service2.get(), GetStorageIdentifier()) |
| 326 | .WillRepeatedly(Return(mock_service2->UniqueName())); |
| 327 | manager.RegisterService(mock_service2); |
| 328 | EXPECT_NE(mock_service2->profile().get(), profile.get()); |
| 329 | manager.Stop(); |
| 330 | } |
| 331 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 332 | TEST_F(ManagerTest, GetProperties) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 333 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 334 | AdoptProfile(manager(), profile); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 335 | map<string, ::DBus::Variant> props; |
| 336 | Error error(Error::kInvalidProperty, ""); |
| 337 | { |
| 338 | ::DBus::Error dbus_error; |
| 339 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 340 | manager()->mutable_store()->SetStringProperty( |
| 341 | flimflam::kCheckPortalListProperty, |
| 342 | expected, |
| 343 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 344 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 345 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 346 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 347 | expected); |
| 348 | } |
| 349 | { |
| 350 | ::DBus::Error dbus_error; |
| 351 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 352 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 353 | expected, |
| 354 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 355 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 356 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 357 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 358 | expected); |
| 359 | } |
| 360 | } |
| 361 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 362 | TEST_F(ManagerTest, GetDevicesProperty) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 363 | ProfileRefPtr profile(new MockProfile(control_interface(), manager(), "")); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 364 | AdoptProfile(manager(), profile); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 365 | manager()->RegisterDevice(mock_devices_[0]); |
| 366 | manager()->RegisterDevice(mock_devices_[1]); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 367 | { |
| 368 | map<string, ::DBus::Variant> props; |
| 369 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 370 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 371 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 372 | Strings devices = |
| 373 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 374 | EXPECT_EQ(2, devices.size()); |
| 375 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 378 | TEST_F(ManagerTest, MoveService) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 379 | Manager manager(control_interface(), |
| 380 | dispatcher(), |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 381 | glib(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 382 | run_path(), |
| 383 | storage_path(), |
| 384 | string()); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 385 | scoped_refptr<MockService> s2(new MockService(control_interface(), |
| 386 | dispatcher(), |
| 387 | &manager)); |
| 388 | // Inject an actual profile, backed by a fake StoreInterface |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 389 | { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 390 | Profile::Identifier id("irrelevant"); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 391 | ProfileRefPtr profile( |
| 392 | new Profile(control_interface(), &manager, id, "", false)); |
| 393 | MockStore *storage = new MockStore; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 394 | // Say we don't have |s2| the first time asked, then that we do. |
| 395 | EXPECT_CALL(*storage, ContainsGroup(s2->GetStorageIdentifier())) |
| 396 | .WillOnce(Return(false)) |
| 397 | .WillRepeatedly(Return(true)); |
| 398 | EXPECT_CALL(*storage, Flush()) |
| 399 | .Times(AnyNumber()) |
| 400 | .WillRepeatedly(Return(true)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 401 | profile->set_storage(storage); |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 402 | AdoptProfile(&manager, profile); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 403 | } |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 404 | // Create a profile that already has |s2| in it. |
| 405 | ProfileRefPtr profile(new EphemeralProfile(control_interface(), &manager)); |
| 406 | profile->AdoptService(s2); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 407 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 408 | // Now, move the Service |s2| to another profile. |
| 409 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 410 | ASSERT_TRUE(manager.MoveServiceToProfile(s2, manager.ActiveProfile())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 411 | |
| 412 | // Force destruction of the original Profile, to ensure that the Service |
| 413 | // is kept alive and populated with data. |
| 414 | profile = NULL; |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 415 | ASSERT_TRUE(manager.ActiveProfile()->ContainsService(s2)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 416 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 419 | TEST_F(ManagerTest, CreateProfile) { |
| 420 | // It's much easier to use real Glib here since we want the storage |
| 421 | // side-effects. |
| 422 | GLib glib; |
| 423 | ScopedTempDir temp_dir; |
| 424 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 425 | |
| 426 | Manager manager(control_interface(), |
| 427 | dispatcher(), |
| 428 | &glib, |
| 429 | run_path(), |
| 430 | storage_path(), |
| 431 | temp_dir.path().value()); |
| 432 | |
| 433 | // Invalid name should be rejected. |
| 434 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "")); |
| 435 | |
| 436 | // Valid name is still rejected because we can't create a profile |
| 437 | // that doesn't have a user component. Such profile names are |
| 438 | // reserved for the single DefaultProfile the manager creates |
| 439 | // at startup. |
| 440 | EXPECT_EQ(Error::kInvalidArguments, TestCreateProfile(&manager, "valid")); |
| 441 | |
| 442 | // We should succeed in creating a valid user profile. |
| 443 | const char kProfile[] = "~user/profile"; |
| 444 | EXPECT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile)); |
| 445 | |
| 446 | // We should fail in creating it a second time (already exists). |
| 447 | EXPECT_EQ(Error::kAlreadyExists, TestCreateProfile(&manager, kProfile)); |
| 448 | } |
| 449 | |
| 450 | TEST_F(ManagerTest, PushPopProfile) { |
| 451 | // It's much easier to use real Glib in creating a Manager for this |
| 452 | // test here since we want the storage side-effects. |
| 453 | GLib glib; |
| 454 | ScopedTempDir temp_dir; |
| 455 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 456 | Manager manager(control_interface(), |
| 457 | dispatcher(), |
| 458 | &glib, |
| 459 | run_path(), |
| 460 | storage_path(), |
| 461 | temp_dir.path().value()); |
| 462 | |
| 463 | // Pushing an invalid profile should fail. |
| 464 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "")); |
| 465 | |
| 466 | // Pushing a default profile name should fail. |
| 467 | EXPECT_EQ(Error::kInvalidArguments, TestPushProfile(&manager, "default")); |
| 468 | |
| 469 | const char kProfile0[] = "~user/profile0"; |
| 470 | const char kProfile1[] = "~user/profile1"; |
| 471 | |
| 472 | // Create a couple of profiles. |
| 473 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile0)); |
| 474 | ASSERT_EQ(Error::kSuccess, TestCreateProfile(&manager, kProfile1)); |
| 475 | |
| 476 | // Push these profiles on the stack. |
| 477 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile0)); |
| 478 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile1)); |
| 479 | |
| 480 | // Pushing a profile a second time should fail. |
| 481 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile0)); |
| 482 | EXPECT_EQ(Error::kAlreadyExists, TestPushProfile(&manager, kProfile1)); |
| 483 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 484 | Error error; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 485 | // Active profile should be the last one we pushed. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 486 | EXPECT_EQ(kProfile1, "~" + manager.GetActiveProfileName(&error)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 487 | |
| 488 | // Make sure a profile name that doesn't exist fails. |
| 489 | const char kProfile2Id[] = "profile2"; |
| 490 | const string kProfile2 = base::StringPrintf("~user/%s", kProfile2Id); |
| 491 | EXPECT_EQ(Error::kNotFound, TestPushProfile(&manager, kProfile2)); |
| 492 | |
| 493 | // Create a new service, with a specific storage name. |
| 494 | scoped_refptr<MockService> service( |
| 495 | new NiceMock<MockService>(control_interface(), |
| 496 | dispatcher(), |
| 497 | &manager)); |
| 498 | const char kServiceName[] = "service_storage_name"; |
| 499 | EXPECT_CALL(*service.get(), GetStorageIdentifier()) |
| 500 | .WillRepeatedly(Return(kServiceName)); |
| 501 | EXPECT_CALL(*service.get(), Load(_)) |
| 502 | .WillRepeatedly(Return(true)); |
| 503 | |
| 504 | // Add this service to the manager -- it should end up in the ephemeral |
| 505 | // profile. |
| 506 | manager.RegisterService(service); |
| 507 | ASSERT_EQ(manager.ephemeral_profile_, service->profile()); |
| 508 | |
| 509 | // Create storage for a profile that contains the service storage name. |
| 510 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile2Id, |
| 511 | kServiceName)); |
| 512 | |
| 513 | // When we push the profile, the service should move away from the |
| 514 | // ephemeral profile to this new profile since it has an entry for |
| 515 | // this service. |
| 516 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile2)); |
| 517 | EXPECT_NE(manager.ephemeral_profile_, service->profile()); |
| 518 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 519 | |
| 520 | // Insert another profile that should supersede ownership of the service. |
| 521 | const char kProfile3Id[] = "profile3"; |
| 522 | const string kProfile3 = base::StringPrintf("~user/%s", kProfile3Id); |
| 523 | ASSERT_TRUE(CreateBackingStoreForService(&temp_dir, kProfile3Id, |
| 524 | kServiceName)); |
| 525 | EXPECT_EQ(Error::kSuccess, TestPushProfile(&manager, kProfile3)); |
| 526 | EXPECT_EQ(kProfile3, "~" + service->profile()->GetFriendlyName()); |
| 527 | |
| 528 | // Popping an invalid profile name should fail. |
| 529 | EXPECT_EQ(Error::kInvalidArguments, TestPopProfile(&manager, "~")); |
| 530 | |
| 531 | // Popping an profile that is not at the top of the stack should fail. |
| 532 | EXPECT_EQ(Error::kNotSupported, TestPopProfile(&manager, kProfile0)); |
| 533 | |
| 534 | // Popping the top profile should succeed. |
| 535 | EXPECT_EQ(Error::kSuccess, TestPopProfile(&manager, kProfile3)); |
| 536 | |
| 537 | // Moreover the service should have switched profiles to profile 2. |
| 538 | EXPECT_EQ(kProfile2, "~" + service->profile()->GetFriendlyName()); |
| 539 | |
| 540 | // Popping the top profile should succeed. |
| 541 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 542 | |
| 543 | // The service should now revert to the ephemeral profile. |
| 544 | EXPECT_EQ(manager.ephemeral_profile_, service->profile()); |
| 545 | |
| 546 | // Pop the remaining two services off the stack. |
| 547 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 548 | EXPECT_EQ(Error::kSuccess, TestPopAnyProfile(&manager)); |
| 549 | |
| 550 | // Next pop should fail with "stack is empty". |
| 551 | EXPECT_EQ(Error::kNotFound, TestPopAnyProfile(&manager)); |
| 552 | } |
| 553 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 554 | TEST_F(ManagerTest, Dispatch) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 555 | { |
| 556 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 557 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 558 | flimflam::kOfflineModeProperty, |
| 559 | PropertyStoreTest::kBoolV, |
| 560 | &error)); |
| 561 | } |
| 562 | { |
| 563 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 564 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 565 | flimflam::kCountryProperty, |
| 566 | PropertyStoreTest::kStringV, |
| 567 | &error)); |
| 568 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 569 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 570 | { |
| 571 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 572 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 573 | flimflam::kCountryProperty, |
| 574 | PropertyStoreTest::kBoolV, |
| 575 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 576 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 577 | } |
| 578 | { |
| 579 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 580 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 581 | flimflam::kOfflineModeProperty, |
| 582 | PropertyStoreTest::kStringV, |
| 583 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 584 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 585 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 586 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 587 | { |
| 588 | ::DBus::Error error; |
| 589 | EXPECT_FALSE(DBusAdaptor::DispatchOnType( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 590 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 591 | flimflam::kEnabledTechnologiesProperty, |
| 592 | PropertyStoreTest::kStringsV, |
| 593 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 594 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 595 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 596 | } |
| 597 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 598 | TEST_F(ManagerTest, RequestScan) { |
| 599 | { |
| 600 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 601 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 602 | manager()->RegisterDevice(mock_devices_[1].get()); |
| 603 | EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 604 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 605 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
| 606 | EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 607 | .WillRepeatedly(Return(false)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 608 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 609 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | { |
| 613 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 614 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 615 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 616 | } |
| 617 | } |
| 618 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 619 | TEST_F(ManagerTest, GetWifiServiceNoDevice) { |
| 620 | KeyValueStore args; |
| 621 | Error e; |
| 622 | manager()->GetWifiService(args, &e); |
| 623 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 624 | EXPECT_EQ("no wifi devices available", e.message()); |
| 625 | } |
| 626 | |
| 627 | TEST_F(ManagerTest, GetWifiService) { |
| 628 | KeyValueStore args; |
| 629 | Error e; |
| 630 | WiFiServiceRefPtr wifi_service; |
| 631 | |
| 632 | manager()->RegisterDevice(mock_wifi_); |
| 633 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 634 | .WillRepeatedly(Return(wifi_service)); |
| 635 | manager()->GetWifiService(args, &e); |
| 636 | } |
| 637 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 638 | TEST_F(ManagerTest, TechnologyOrder) { |
| 639 | Error error; |
| 640 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 641 | string(flimflam::kTypeWifi), &error); |
| 642 | ASSERT_TRUE(error.IsSuccess()); |
| 643 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 644 | string(flimflam::kTypeEthernet) + "," + |
| 645 | string(flimflam::kTypeWifi)); |
| 646 | |
| 647 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 648 | string(flimflam::kTypeWifi), &error); |
| 649 | ASSERT_FALSE(error.IsSuccess()); |
| 650 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 651 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 652 | string(flimflam::kTypeWifi), |
| 653 | manager()->GetTechnologyOrder()); |
| 654 | } |
| 655 | |
| 656 | TEST_F(ManagerTest, SortServices) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 657 | // TODO(quiche): Some of these tests would probably fit better in |
| 658 | // service_unittest, since the actual comparison of Services is |
| 659 | // implemented in Service. (crosbug.com/23370) |
| 660 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 661 | scoped_refptr<MockService> mock_service0( |
| 662 | new NiceMock<MockService>(control_interface(), |
| 663 | dispatcher(), |
| 664 | manager())); |
| 665 | scoped_refptr<MockService> mock_service1( |
| 666 | new NiceMock<MockService>(control_interface(), |
| 667 | dispatcher(), |
| 668 | manager())); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 669 | |
| 670 | manager()->RegisterService(mock_service0); |
| 671 | manager()->RegisterService(mock_service1); |
| 672 | |
| 673 | // Services should already be sorted by UniqueName |
| 674 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 675 | |
| 676 | // Asking explictly to sort services should not change anything |
| 677 | manager()->SortServices(); |
| 678 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 679 | |
| 680 | // Two otherwise equal services should be reordered by strength |
| 681 | mock_service1->set_strength(1); |
| 682 | manager()->UpdateService(mock_service1); |
| 683 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 684 | |
| 685 | // Security |
Paul Stewart | 1ca3e85 | 2011-11-04 07:50:49 -0700 | [diff] [blame] | 686 | mock_service0->set_security_level(1); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 687 | manager()->UpdateService(mock_service0); |
| 688 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 689 | |
| 690 | // Technology |
| 691 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi)) |
| 692 | .WillRepeatedly(Return(true)); |
| 693 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet)) |
| 694 | .WillRepeatedly(Return(true)); |
| 695 | // NB: Redefine default (false) return values so we don't use the default rule |
| 696 | // which makes the logs noisier |
| 697 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi))) |
| 698 | .WillRepeatedly(Return(false)); |
| 699 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet))) |
| 700 | .WillRepeatedly(Return(false)); |
| 701 | |
| 702 | Error error; |
| 703 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 704 | string(flimflam::kTypeWifi), &error); |
| 705 | EXPECT_TRUE(error.IsSuccess()); |
| 706 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 707 | |
| 708 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 709 | string(flimflam::kTypeEthernet), &error); |
| 710 | EXPECT_TRUE(error.IsSuccess()); |
| 711 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 712 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 713 | // Priority. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 714 | mock_service0->set_priority(1); |
| 715 | manager()->UpdateService(mock_service0); |
| 716 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 717 | |
| 718 | // Favorite |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 719 | mock_service1->MakeFavorite(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 720 | manager()->UpdateService(mock_service1); |
| 721 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 722 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 723 | // Connecting. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 724 | EXPECT_CALL(*mock_service0.get(), state()) |
| 725 | .WillRepeatedly(Return(Service::kStateAssociating)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 726 | EXPECT_CALL(*mock_service0.get(), IsConnecting()) |
| 727 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 728 | manager()->UpdateService(mock_service0); |
| 729 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 730 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 731 | // Connected. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 732 | EXPECT_CALL(*mock_service1.get(), state()) |
| 733 | .WillRepeatedly(Return(Service::kStateConnected)); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 734 | EXPECT_CALL(*mock_service1.get(), IsConnected()) |
| 735 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 736 | manager()->UpdateService(mock_service1); |
| 737 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 738 | |
| 739 | manager()->DeregisterService(mock_service0); |
| 740 | manager()->DeregisterService(mock_service1); |
| 741 | } |
| 742 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 743 | TEST_F(ManagerTest, AvailableTechnologies) { |
| 744 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 745 | dispatcher(), |
| 746 | manager(), |
| 747 | "null4", |
| 748 | "addr4", |
| 749 | 0)); |
| 750 | manager()->RegisterDevice(mock_devices_[0]); |
| 751 | manager()->RegisterDevice(mock_devices_[1]); |
| 752 | manager()->RegisterDevice(mock_devices_[2]); |
| 753 | manager()->RegisterDevice(mock_devices_[3]); |
| 754 | |
| 755 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 756 | .WillByDefault(Return(Technology::kEthernet)); |
| 757 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 758 | .WillByDefault(Return(Technology::kWifi)); |
| 759 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 760 | .WillByDefault(Return(Technology::kCellular)); |
| 761 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 762 | .WillByDefault(Return(Technology::kWifi)); |
| 763 | |
| 764 | set<string> expected_technologies; |
| 765 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 766 | Technology::kEthernet)); |
| 767 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 768 | Technology::kWifi)); |
| 769 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 770 | Technology::kCellular)); |
| 771 | Error error; |
| 772 | vector<string> technologies = manager()->AvailableTechnologies(&error); |
| 773 | |
| 774 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 775 | ContainerEq(expected_technologies)); |
| 776 | } |
| 777 | |
| 778 | TEST_F(ManagerTest, ConnectedTechnologies) { |
| 779 | scoped_refptr<MockService> connected_service1( |
| 780 | new NiceMock<MockService>(control_interface(), |
| 781 | dispatcher(), |
| 782 | manager())); |
| 783 | scoped_refptr<MockService> connected_service2( |
| 784 | new NiceMock<MockService>(control_interface(), |
| 785 | dispatcher(), |
| 786 | manager())); |
| 787 | scoped_refptr<MockService> disconnected_service1( |
| 788 | new NiceMock<MockService>(control_interface(), |
| 789 | dispatcher(), |
| 790 | manager())); |
| 791 | scoped_refptr<MockService> disconnected_service2( |
| 792 | new NiceMock<MockService>(control_interface(), |
| 793 | dispatcher(), |
| 794 | manager())); |
| 795 | |
| 796 | ON_CALL(*connected_service1.get(), IsConnected()) |
| 797 | .WillByDefault(Return(true)); |
| 798 | ON_CALL(*connected_service2.get(), IsConnected()) |
| 799 | .WillByDefault(Return(true)); |
| 800 | |
| 801 | manager()->RegisterService(connected_service1); |
| 802 | manager()->RegisterService(connected_service2); |
| 803 | manager()->RegisterService(disconnected_service1); |
| 804 | manager()->RegisterService(disconnected_service2); |
| 805 | |
| 806 | manager()->RegisterDevice(mock_devices_[0]); |
| 807 | manager()->RegisterDevice(mock_devices_[1]); |
| 808 | manager()->RegisterDevice(mock_devices_[2]); |
| 809 | manager()->RegisterDevice(mock_devices_[3]); |
| 810 | |
| 811 | ON_CALL(*mock_devices_[0].get(), technology()) |
| 812 | .WillByDefault(Return(Technology::kEthernet)); |
| 813 | ON_CALL(*mock_devices_[1].get(), technology()) |
| 814 | .WillByDefault(Return(Technology::kWifi)); |
| 815 | ON_CALL(*mock_devices_[2].get(), technology()) |
| 816 | .WillByDefault(Return(Technology::kCellular)); |
| 817 | ON_CALL(*mock_devices_[3].get(), technology()) |
| 818 | .WillByDefault(Return(Technology::kWifi)); |
| 819 | |
| 820 | mock_devices_[0]->SelectService(connected_service1); |
| 821 | mock_devices_[1]->SelectService(disconnected_service1); |
| 822 | mock_devices_[2]->SelectService(disconnected_service2); |
| 823 | mock_devices_[3]->SelectService(connected_service2); |
| 824 | |
| 825 | set<string> expected_technologies; |
| 826 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 827 | Technology::kEthernet)); |
| 828 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 829 | Technology::kWifi)); |
| 830 | Error error; |
| 831 | |
| 832 | vector<string> technologies = manager()->ConnectedTechnologies(&error); |
| 833 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 834 | ContainerEq(expected_technologies)); |
| 835 | } |
| 836 | |
| 837 | TEST_F(ManagerTest, DefaultTechnology) { |
| 838 | scoped_refptr<MockService> connected_service( |
| 839 | new NiceMock<MockService>(control_interface(), |
| 840 | dispatcher(), |
| 841 | manager())); |
| 842 | scoped_refptr<MockService> disconnected_service( |
| 843 | new NiceMock<MockService>(control_interface(), |
| 844 | dispatcher(), |
| 845 | manager())); |
| 846 | |
| 847 | // Connected. WiFi. |
| 848 | ON_CALL(*connected_service.get(), IsConnected()) |
| 849 | .WillByDefault(Return(true)); |
| 850 | ON_CALL(*connected_service.get(), state()) |
| 851 | .WillByDefault(Return(Service::kStateConnected)); |
| 852 | ON_CALL(*connected_service.get(), technology()) |
| 853 | .WillByDefault(Return(Technology::kWifi)); |
| 854 | |
| 855 | // Disconnected. Ethernet. |
| 856 | ON_CALL(*disconnected_service.get(), technology()) |
| 857 | .WillByDefault(Return(Technology::kEthernet)); |
| 858 | |
| 859 | manager()->RegisterService(disconnected_service); |
| 860 | Error error; |
| 861 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq("")); |
| 862 | |
| 863 | |
| 864 | manager()->RegisterService(connected_service); |
| 865 | // Connected service should be brought to the front now. |
| 866 | string expected_technology = |
| 867 | Technology::NameFromIdentifier(Technology::kWifi); |
| 868 | EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology)); |
| 869 | } |
| 870 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 871 | TEST_F(ManagerTest, DisconnectServicesOnStop) { |
| 872 | scoped_refptr<MockService> mock_service( |
| 873 | new NiceMock<MockService>(control_interface(), |
| 874 | dispatcher(), |
| 875 | manager())); |
| 876 | manager()->RegisterService(mock_service); |
| 877 | EXPECT_CALL(*mock_service.get(), Disconnect()).Times(1); |
| 878 | manager()->Stop(); |
| 879 | } |
| 880 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 881 | TEST_F(ManagerTest, UpdateServiceConnected) { |
| 882 | scoped_refptr<MockService> mock_service( |
| 883 | new NiceMock<MockService>(control_interface(), |
| 884 | dispatcher(), |
| 885 | manager())); |
| 886 | manager()->RegisterService(mock_service); |
| 887 | EXPECT_FALSE(mock_service->favorite()); |
| 888 | EXPECT_FALSE(mock_service->auto_connect()); |
| 889 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame^] | 890 | EXPECT_CALL(*mock_service.get(), IsConnected()) |
| 891 | .WillRepeatedly(Return(true)); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 892 | manager()->UpdateService(mock_service); |
| 893 | // We can't EXPECT_CALL(..., MakeFavorite), because that requires us |
| 894 | // to mock out MakeFavorite. And mocking that out would break the |
| 895 | // SortServices test. (crosbug.com/23370) |
| 896 | EXPECT_TRUE(mock_service->favorite()); |
| 897 | EXPECT_TRUE(mock_service->auto_connect()); |
| 898 | } |
| 899 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 900 | } // namespace shill |