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> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 12 | #include <base/stl_util-inl.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 7156c92 | 2011-08-23 20:36:21 -0700 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 16 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 17 | #include "shill/adaptor_interfaces.h" |
| 18 | #include "shill/error.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 19 | #include "shill/key_value_store.h" |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 20 | #include "shill/mock_adaptors.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 21 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 22 | #include "shill/mock_device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 23 | #include "shill/mock_glib.h" |
| 24 | #include "shill/mock_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 25 | #include "shill/mock_service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 26 | #include "shill/mock_wifi.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 27 | #include "shill/property_store_unittest.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 28 | #include "shill/wifi_service.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 29 | |
| 30 | using std::map; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 31 | using std::set; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 35 | namespace shill { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 36 | using ::testing::_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 37 | using ::testing::Ne; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 38 | using ::testing::NiceMock; |
| 39 | using ::testing::Return; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 40 | using ::testing::Test; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 41 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | class ManagerTest : public PropertyStoreTest { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 43 | public: |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 44 | ManagerTest() |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 45 | : mock_wifi_(new NiceMock<MockWiFi>(control_interface(), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 46 | dispatcher(), |
| 47 | manager(), |
| 48 | "wifi0", |
| 49 | "addr4", |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 50 | 4)) { |
| 51 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 52 | dispatcher(), |
| 53 | manager(), |
| 54 | "null0", |
| 55 | "addr0", |
| 56 | 0)); |
| 57 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 58 | dispatcher(), |
| 59 | manager(), |
| 60 | "null1", |
| 61 | "addr1", |
| 62 | 1)); |
| 63 | mock_devices_.push_back(new NiceMock<MockDevice>(control_interface(), |
| 64 | dispatcher(), |
| 65 | manager(), |
| 66 | "null2", |
| 67 | "addr2", |
| 68 | 2)); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 69 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 70 | virtual ~ManagerTest() {} |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 71 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 72 | bool IsDeviceRegistered(const DeviceRefPtr &device, |
| 73 | Technology::Identifier tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 74 | vector<DeviceRefPtr> devices; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 75 | manager()->FilterByTechnology(tech, &devices); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 76 | return (devices.size() == 1 && devices[0].get() == device.get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 77 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 78 | bool ServiceOrderIs(ServiceRefPtr svc1, ServiceRefPtr svc2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 79 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 80 | protected: |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 81 | scoped_refptr<MockWiFi> mock_wifi_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 82 | vector<scoped_refptr<MockDevice> > mock_devices_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 85 | bool ManagerTest::ServiceOrderIs(ServiceRefPtr svc0, ServiceRefPtr svc1) { |
| 86 | return (svc0.get() == manager()->services_[0].get() && |
| 87 | svc1.get() == manager()->services_[1].get()); |
| 88 | } |
| 89 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 90 | TEST_F(ManagerTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(manager()->store().Contains(flimflam::kStateProperty)); |
| 92 | EXPECT_FALSE(manager()->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 95 | TEST_F(ManagerTest, DeviceRegistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 96 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 97 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 98 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 99 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 100 | ON_CALL(*mock_devices_[2].get(), TechnologyIs(Technology::kCellular)) |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 101 | .WillByDefault(Return(true)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 102 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 103 | manager()->RegisterDevice(mock_devices_[0]); |
| 104 | manager()->RegisterDevice(mock_devices_[1]); |
| 105 | manager()->RegisterDevice(mock_devices_[2]); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 106 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 108 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
| 109 | EXPECT_TRUE(IsDeviceRegistered(mock_devices_[2], Technology::kCellular)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_F(ManagerTest, DeviceDeregistration) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 113 | ON_CALL(*mock_devices_[0].get(), TechnologyIs(Technology::kEthernet)) |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 114 | .WillByDefault(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 115 | ON_CALL(*mock_devices_[1].get(), TechnologyIs(Technology::kWifi)) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 116 | .WillByDefault(Return(true)); |
| 117 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 118 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 119 | manager()->RegisterDevice(mock_devices_[1].get()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 120 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
| 122 | ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 123 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 124 | EXPECT_CALL(*mock_devices_[0].get(), Stop()); |
| 125 | manager()->DeregisterDevice(mock_devices_[0].get()); |
| 126 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 127 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*mock_devices_[1].get(), Stop()); |
| 129 | manager()->DeregisterDevice(mock_devices_[1].get()); |
| 130 | EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 134 | // It's much easier and safer to use a real GLib for this test. |
| 135 | GLib glib; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 136 | Manager manager(control_interface(), |
| 137 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 138 | &glib, |
| 139 | run_path(), |
| 140 | storage_path(), |
| 141 | string()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 142 | scoped_refptr<MockService> mock_service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 143 | new NiceMock<MockService>(control_interface(), |
| 144 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 145 | &manager)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 146 | scoped_refptr<MockService> mock_service2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 147 | new NiceMock<MockService>(control_interface(), |
| 148 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 149 | &manager)); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 150 | string service1_name(mock_service->UniqueName()); |
| 151 | string service2_name(mock_service2->UniqueName()); |
| 152 | |
| 153 | EXPECT_CALL(*mock_service.get(), GetRpcIdentifier()) |
| 154 | .WillRepeatedly(Return(service1_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 155 | EXPECT_CALL(*mock_service2.get(), GetRpcIdentifier()) |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 156 | .WillRepeatedly(Return(service2_name)); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 157 | // TODO(quiche): make this EXPECT_CALL work (crosbug.com/20154) |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 158 | // EXPECT_CALL(*dynamic_cast<ManagerMockAdaptor *>(manager.adaptor_.get()), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 159 | // EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, _)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 160 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 161 | manager.RegisterService(mock_service); |
| 162 | manager.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 163 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 164 | vector<string> rpc_ids = manager.EnumerateAvailableServices(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 165 | set<string> ids(rpc_ids.begin(), rpc_ids.end()); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 166 | EXPECT_EQ(2, ids.size()); |
| 167 | EXPECT_TRUE(ContainsKey(ids, mock_service->GetRpcIdentifier())); |
| 168 | EXPECT_TRUE(ContainsKey(ids, mock_service2->GetRpcIdentifier())); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 169 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 170 | EXPECT_TRUE(manager.FindService(service1_name).get() != NULL); |
| 171 | EXPECT_TRUE(manager.FindService(service2_name).get() != NULL); |
| 172 | |
| 173 | manager.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 176 | TEST_F(ManagerTest, GetProperties) { |
| 177 | map<string, ::DBus::Variant> props; |
| 178 | Error error(Error::kInvalidProperty, ""); |
| 179 | { |
| 180 | ::DBus::Error dbus_error; |
| 181 | string expected("portal_list"); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 182 | manager()->mutable_store()->SetStringProperty( |
| 183 | flimflam::kCheckPortalListProperty, |
| 184 | expected, |
| 185 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 186 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 187 | ASSERT_FALSE(props.find(flimflam::kCheckPortalListProperty) == props.end()); |
| 188 | EXPECT_EQ(props[flimflam::kCheckPortalListProperty].reader().get_string(), |
| 189 | expected); |
| 190 | } |
| 191 | { |
| 192 | ::DBus::Error dbus_error; |
| 193 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 194 | manager()->mutable_store()->SetBoolProperty(flimflam::kOfflineModeProperty, |
| 195 | expected, |
| 196 | &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 197 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 198 | ASSERT_FALSE(props.find(flimflam::kOfflineModeProperty) == props.end()); |
| 199 | EXPECT_EQ(props[flimflam::kOfflineModeProperty].reader().get_bool(), |
| 200 | expected); |
| 201 | } |
| 202 | } |
| 203 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 204 | TEST_F(ManagerTest, GetDevicesProperty) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 205 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 206 | manager()->RegisterDevice(mock_devices_[1].get()); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 207 | { |
| 208 | map<string, ::DBus::Variant> props; |
| 209 | ::DBus::Error dbus_error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 210 | DBusAdaptor::GetProperties(manager()->store(), &props, &dbus_error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 211 | ASSERT_FALSE(props.find(flimflam::kDevicesProperty) == props.end()); |
| 212 | Strings devices = |
| 213 | props[flimflam::kDevicesProperty].operator vector<string>(); |
| 214 | EXPECT_EQ(2, devices.size()); |
| 215 | } |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 218 | TEST_F(ManagerTest, MoveService) { |
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()); |
| 227 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 228 | // I want to ensure that the Profiles are managing this Service object |
| 229 | // lifetime properly, so I can't hold a ref to it here. |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 230 | ProfileRefPtr profile(new MockProfile(control_interface(), &manager, "")); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 231 | string service_name; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 232 | { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 233 | scoped_refptr<MockService> s2( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 234 | new MockService(control_interface(), |
| 235 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 236 | &manager)); |
| 237 | EXPECT_CALL(*s2.get(), Save(_)).WillOnce(Return(true)); |
| 238 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 239 | profile->AdoptService(s2); |
| 240 | s2->set_profile(profile); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 241 | service_name = s2->UniqueName(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Now, move the |service| to another profile. |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(manager.MoveToActiveProfile(profile, |
| 246 | profile->FindService(service_name))); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 247 | |
| 248 | // Force destruction of the original Profile, to ensure that the Service |
| 249 | // is kept alive and populated with data. |
| 250 | profile = NULL; |
| 251 | { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 252 | ServiceRefPtr serv(manager.ActiveProfile()->FindService(service_name)); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 253 | ASSERT_TRUE(serv.get() != NULL); |
| 254 | Error error(Error::kInvalidProperty, ""); |
| 255 | ::DBus::Error dbus_error; |
| 256 | map<string, ::DBus::Variant> props; |
| 257 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 258 | serv->mutable_store()->SetBoolProperty(flimflam::kAutoConnectProperty, |
| 259 | expected, |
| 260 | &error); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 261 | DBusAdaptor::GetProperties(serv->store(), &props, &dbus_error); |
| 262 | ASSERT_TRUE(ContainsKey(props, flimflam::kAutoConnectProperty)); |
| 263 | EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(), |
| 264 | expected); |
| 265 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 266 | manager.Stop(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 269 | TEST_F(ManagerTest, Dispatch) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 270 | { |
| 271 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 272 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 273 | flimflam::kOfflineModeProperty, |
| 274 | PropertyStoreTest::kBoolV, |
| 275 | &error)); |
| 276 | } |
| 277 | { |
| 278 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 279 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 280 | flimflam::kCountryProperty, |
| 281 | PropertyStoreTest::kStringV, |
| 282 | &error)); |
| 283 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 284 | // Attempt to write with value of wrong type should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 285 | { |
| 286 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 287 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 288 | flimflam::kCountryProperty, |
| 289 | PropertyStoreTest::kBoolV, |
| 290 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 291 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 292 | } |
| 293 | { |
| 294 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 295 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 296 | flimflam::kOfflineModeProperty, |
| 297 | PropertyStoreTest::kStringV, |
| 298 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 299 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 300 | } |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 301 | // Attempt to write R/O property should return InvalidArgs. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 302 | { |
| 303 | ::DBus::Error error; |
| 304 | EXPECT_FALSE(DBusAdaptor::DispatchOnType( |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 305 | manager()->mutable_store(), |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 306 | flimflam::kEnabledTechnologiesProperty, |
| 307 | PropertyStoreTest::kStringsV, |
| 308 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 309 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 310 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 311 | } |
| 312 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 313 | TEST_F(ManagerTest, RequestScan) { |
| 314 | { |
| 315 | Error error; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 316 | manager()->RegisterDevice(mock_devices_[0].get()); |
| 317 | manager()->RegisterDevice(mock_devices_[1].get()); |
| 318 | EXPECT_CALL(*mock_devices_[0], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 319 | .WillRepeatedly(Return(true)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 320 | EXPECT_CALL(*mock_devices_[0], Scan(_)); |
| 321 | EXPECT_CALL(*mock_devices_[1], TechnologyIs(Technology::kWifi)) |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 322 | .WillRepeatedly(Return(false)); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 323 | EXPECT_CALL(*mock_devices_[1], Scan(_)).Times(0); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 324 | manager()->RequestScan(flimflam::kTypeWifi, &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | { |
| 328 | Error error; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 329 | manager()->RequestScan("bogus_device_type", &error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 330 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 331 | } |
| 332 | } |
| 333 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 334 | TEST_F(ManagerTest, GetWifiServiceNoDevice) { |
| 335 | KeyValueStore args; |
| 336 | Error e; |
| 337 | manager()->GetWifiService(args, &e); |
| 338 | EXPECT_EQ(Error::kInvalidArguments, e.type()); |
| 339 | EXPECT_EQ("no wifi devices available", e.message()); |
| 340 | } |
| 341 | |
| 342 | TEST_F(ManagerTest, GetWifiService) { |
| 343 | KeyValueStore args; |
| 344 | Error e; |
| 345 | WiFiServiceRefPtr wifi_service; |
| 346 | |
| 347 | manager()->RegisterDevice(mock_wifi_); |
| 348 | EXPECT_CALL(*mock_wifi_, GetService(_, _)) |
| 349 | .WillRepeatedly(Return(wifi_service)); |
| 350 | manager()->GetWifiService(args, &e); |
| 351 | } |
| 352 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 353 | TEST_F(ManagerTest, TechnologyOrder) { |
| 354 | Error error; |
| 355 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 356 | string(flimflam::kTypeWifi), &error); |
| 357 | ASSERT_TRUE(error.IsSuccess()); |
| 358 | EXPECT_EQ(manager()->GetTechnologyOrder(), |
| 359 | string(flimflam::kTypeEthernet) + "," + |
| 360 | string(flimflam::kTypeWifi)); |
| 361 | |
| 362 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "x," + |
| 363 | string(flimflam::kTypeWifi), &error); |
| 364 | ASSERT_FALSE(error.IsSuccess()); |
| 365 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 366 | EXPECT_EQ(string(flimflam::kTypeEthernet) + "," + |
| 367 | string(flimflam::kTypeWifi), |
| 368 | manager()->GetTechnologyOrder()); |
| 369 | } |
| 370 | |
| 371 | TEST_F(ManagerTest, SortServices) { |
| 372 | scoped_refptr<MockService> mock_service0( |
| 373 | new NiceMock<MockService>(control_interface(), |
| 374 | dispatcher(), |
| 375 | manager())); |
| 376 | scoped_refptr<MockService> mock_service1( |
| 377 | new NiceMock<MockService>(control_interface(), |
| 378 | dispatcher(), |
| 379 | manager())); |
| 380 | string service1_name(mock_service0->UniqueName()); |
| 381 | string service2_name(mock_service1->UniqueName()); |
| 382 | |
| 383 | manager()->RegisterService(mock_service0); |
| 384 | manager()->RegisterService(mock_service1); |
| 385 | |
| 386 | // Services should already be sorted by UniqueName |
| 387 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 388 | |
| 389 | // Asking explictly to sort services should not change anything |
| 390 | manager()->SortServices(); |
| 391 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 392 | |
| 393 | // Two otherwise equal services should be reordered by strength |
| 394 | mock_service1->set_strength(1); |
| 395 | manager()->UpdateService(mock_service1); |
| 396 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 397 | |
| 398 | // Security |
| 399 | mock_service0->set_security(1); |
| 400 | manager()->UpdateService(mock_service0); |
| 401 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 402 | |
| 403 | // Technology |
| 404 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Technology::kWifi)) |
| 405 | .WillRepeatedly(Return(true)); |
| 406 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Technology::kEthernet)) |
| 407 | .WillRepeatedly(Return(true)); |
| 408 | // NB: Redefine default (false) return values so we don't use the default rule |
| 409 | // which makes the logs noisier |
| 410 | EXPECT_CALL(*mock_service0.get(), TechnologyIs(Ne(Technology::kWifi))) |
| 411 | .WillRepeatedly(Return(false)); |
| 412 | EXPECT_CALL(*mock_service1.get(), TechnologyIs(Ne(Technology::kEthernet))) |
| 413 | .WillRepeatedly(Return(false)); |
| 414 | |
| 415 | Error error; |
| 416 | manager()->SetTechnologyOrder(string(flimflam::kTypeEthernet) + "," + |
| 417 | string(flimflam::kTypeWifi), &error); |
| 418 | EXPECT_TRUE(error.IsSuccess()); |
| 419 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 420 | |
| 421 | manager()->SetTechnologyOrder(string(flimflam::kTypeWifi) + "," + |
| 422 | string(flimflam::kTypeEthernet), &error); |
| 423 | EXPECT_TRUE(error.IsSuccess()); |
| 424 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 425 | |
| 426 | // Priority |
| 427 | mock_service0->set_priority(1); |
| 428 | manager()->UpdateService(mock_service0); |
| 429 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 430 | |
| 431 | // Favorite |
| 432 | mock_service1->set_favorite(true); |
| 433 | manager()->UpdateService(mock_service1); |
| 434 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 435 | |
| 436 | // Connecting |
| 437 | EXPECT_CALL(*mock_service0.get(), state()) |
| 438 | .WillRepeatedly(Return(Service::kStateAssociating)); |
| 439 | manager()->UpdateService(mock_service0); |
| 440 | EXPECT_TRUE(ServiceOrderIs(mock_service0, mock_service1)); |
| 441 | |
| 442 | // Connected |
| 443 | EXPECT_CALL(*mock_service1.get(), state()) |
| 444 | .WillRepeatedly(Return(Service::kStateConnected)); |
| 445 | manager()->UpdateService(mock_service1); |
| 446 | EXPECT_TRUE(ServiceOrderIs(mock_service1, mock_service0)); |
| 447 | |
| 448 | manager()->DeregisterService(mock_service0); |
| 449 | manager()->DeregisterService(mock_service1); |
| 450 | } |
| 451 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 452 | } // namespace shill |