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