Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -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. |
| 4 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 6 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 7 | #include <ctype.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
| 9 | #include <linux/if.h> // Needs typedefs from sys/socket.h. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 16 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | |
| 20 | #include "shill/dbus_adaptor.h" |
| 21 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 22 | #include "shill/event_dispatcher.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 23 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 24 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 25 | #include "shill/mock_control.h" |
| 26 | #include "shill/mock_device.h" |
| 27 | #include "shill/mock_glib.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 28 | #include "shill/mock_ipconfig.h" |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 29 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 30 | #include "shill/mock_service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 31 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | #include "shill/property_store_unittest.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 33 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 34 | |
| 35 | using std::map; |
| 36 | using std::string; |
| 37 | using std::vector; |
| 38 | using ::testing::_; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 39 | using ::testing::AtLeast; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 40 | using ::testing::NiceMock; |
| 41 | using ::testing::Return; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 42 | using ::testing::StrictMock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 43 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 44 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 45 | |
| 46 | namespace shill { |
| 47 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 48 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 49 | public: |
| 50 | DeviceTest() |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 51 | : device_(new Device(control_interface(), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 52 | NULL, |
| 53 | NULL, |
| 54 | kDeviceName, |
| 55 | kDeviceAddress, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 56 | 0, |
| 57 | Technology::kUnknown)) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 58 | DHCPProvider::GetInstance()->glib_ = glib(); |
| 59 | DHCPProvider::GetInstance()->control_interface_ = control_interface(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 60 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 61 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 62 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 63 | virtual void SetUp() { |
| 64 | device_->rtnl_handler_ = &rtnl_handler_; |
| 65 | } |
| 66 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 67 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 68 | static const char kDeviceName[]; |
| 69 | static const char kDeviceAddress[]; |
| 70 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 71 | MockControl control_interface_; |
| 72 | DeviceRefPtr device_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 73 | StrictMock<MockRTNLHandler> rtnl_handler_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 76 | const char DeviceTest::kDeviceName[] = "testdevice"; |
| 77 | const char DeviceTest::kDeviceAddress[] = "address"; |
| 78 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 79 | TEST_F(DeviceTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty)); |
| 81 | EXPECT_FALSE(device_->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 84 | TEST_F(DeviceTest, GetProperties) { |
| 85 | map<string, ::DBus::Variant> props; |
| 86 | Error error(Error::kInvalidProperty, ""); |
| 87 | { |
| 88 | ::DBus::Error dbus_error; |
| 89 | bool expected = true; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 90 | device_->mutable_store()->SetBoolProperty(flimflam::kPoweredProperty, |
| 91 | expected, |
| 92 | &error); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 93 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 94 | ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end()); |
| 95 | EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(), |
| 96 | expected); |
| 97 | } |
| 98 | { |
| 99 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 100 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 101 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 102 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 103 | string(kDeviceName)); |
| 104 | } |
| 105 | } |
| 106 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 107 | TEST_F(DeviceTest, Dispatch) { |
| 108 | ::DBus::Error error; |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 110 | flimflam::kPoweredProperty, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 111 | PropertyStoreTest::kBoolV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 112 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 113 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 114 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 115 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->mutable_store(), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 116 | flimflam::kAddressProperty, |
| 117 | PropertyStoreTest::kStringV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 118 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 119 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 122 | TEST_F(DeviceTest, TechnologyIs) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 123 | EXPECT_FALSE(device_->TechnologyIs(Technology::kEthernet)); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 127 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 128 | device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 129 | device_->DestroyIPConfig(); |
| 130 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 131 | } |
| 132 | |
| 133 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 134 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 135 | device_->DestroyIPConfig(); |
| 136 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 137 | } |
| 138 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 139 | TEST_F(DeviceTest, AcquireIPConfig) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 140 | device_->ipconfig_ = new IPConfig(control_interface(), "randomname"); |
| 141 | EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _)) |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 142 | .WillOnce(Return(false)); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 143 | EXPECT_FALSE(device_->AcquireIPConfig()); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 144 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 145 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
| 146 | EXPECT_TRUE(device_->ipconfig_->update_callback_.get()); |
| 147 | } |
| 148 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 149 | TEST_F(DeviceTest, Load) { |
| 150 | NiceMock<MockStore> storage; |
| 151 | const string id = device_->GetStorageIdentifier(); |
| 152 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 153 | EXPECT_CALL(storage, GetBool(id, _, _)) |
| 154 | .Times(AtLeast(1)) |
| 155 | .WillRepeatedly(Return(true)); |
| 156 | EXPECT_TRUE(device_->Load(&storage)); |
| 157 | } |
| 158 | |
| 159 | TEST_F(DeviceTest, Save) { |
| 160 | NiceMock<MockStore> storage; |
| 161 | const string id = device_->GetStorageIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 162 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 163 | .Times(AtLeast(1)) |
| 164 | .WillRepeatedly(Return(true)); |
| 165 | EXPECT_CALL(storage, SetBool(id, _, _)) |
| 166 | .Times(AtLeast(1)) |
| 167 | .WillRepeatedly(Return(true)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 168 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 169 | kDeviceName); |
| 170 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 171 | .WillOnce(Return(true)); |
| 172 | device_->ipconfig_ = ipconfig; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 173 | EXPECT_TRUE(device_->Save(&storage)); |
| 174 | } |
| 175 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 176 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 177 | string to_process("/device/stuff/0"); |
| 178 | ControlInterface::RpcIdToStorageId(&to_process); |
| 179 | EXPECT_TRUE(isalpha(to_process[0])); |
| 180 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 181 | } |
| 182 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 183 | MATCHER(IsNullRefPtr, "") { |
| 184 | return !arg; |
| 185 | } |
| 186 | |
| 187 | MATCHER(NotNullRefPtr, "") { |
| 188 | return arg; |
| 189 | } |
| 190 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 191 | TEST_F(DeviceTest, SelectedService) { |
| 192 | EXPECT_FALSE(device_->selected_service_.get()); |
| 193 | device_->SetServiceState(Service::kStateAssociating); |
| 194 | scoped_refptr<MockService> service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 195 | new StrictMock<MockService>(control_interface(), |
| 196 | dispatcher(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 197 | manager())); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 198 | device_->SelectService(service); |
| 199 | EXPECT_TRUE(device_->selected_service_.get() == service.get()); |
| 200 | |
| 201 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
| 202 | device_->SetServiceState(Service::kStateConfiguring); |
| 203 | EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange)); |
| 204 | device_->SetServiceFailure(Service::kFailureOutOfRange); |
| 205 | |
| 206 | // Service should be returned to "Idle" state |
| 207 | EXPECT_CALL(*service.get(), state()) |
| 208 | .WillOnce(Return(Service::kStateUnknown)); |
| 209 | EXPECT_CALL(*service.get(), SetState(Service::kStateIdle)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 210 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 211 | device_->SelectService(NULL); |
| 212 | |
| 213 | // A service in the "Failure" state should not be reset to "Idle" |
| 214 | device_->SelectService(service); |
| 215 | EXPECT_CALL(*service.get(), state()) |
| 216 | .WillOnce(Return(Service::kStateFailure)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 217 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 218 | device_->SelectService(NULL); |
| 219 | } |
| 220 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 221 | TEST_F(DeviceTest, IPConfigUpdatedFailure) { |
| 222 | scoped_refptr<MockService> service( |
| 223 | new StrictMock<MockService>(control_interface(), |
| 224 | dispatcher(), |
| 225 | manager())); |
| 226 | device_->SelectService(service); |
| 227 | EXPECT_CALL(*service.get(), SetState(Service::kStateDisconnected)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 228 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 229 | device_->IPConfigUpdatedCallback(NULL, false); |
| 230 | } |
| 231 | |
| 232 | TEST_F(DeviceTest, IPConfigUpdatedSuccess) { |
| 233 | scoped_refptr<MockService> service( |
| 234 | new StrictMock<MockService>(control_interface(), |
| 235 | dispatcher(), |
| 236 | manager())); |
| 237 | device_->SelectService(service); |
| 238 | device_->manager_ = manager(); |
| 239 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 240 | kDeviceName); |
| 241 | EXPECT_CALL(*service.get(), SetState(Service::kStateConnected)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 242 | EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr())); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 243 | device_->IPConfigUpdatedCallback(ipconfig.get(), true); |
| 244 | } |
| 245 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 246 | TEST_F(DeviceTest, Stop) { |
| 247 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
| 248 | scoped_refptr<MockService> service( |
| 249 | new NiceMock<MockService>(&control_interface_, |
| 250 | dispatcher(), |
| 251 | manager())); |
| 252 | device_->SelectService(service); |
| 253 | |
| 254 | EXPECT_CALL(*service.get(), state()). |
| 255 | WillRepeatedly(Return(Service::kStateConnected)); |
| 256 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 257 | UpdateEnabled()); |
| 258 | EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP)); |
| 259 | device_->Stop(); |
| 260 | |
| 261 | EXPECT_FALSE(device_->ipconfig_.get()); |
| 262 | EXPECT_FALSE(device_->selected_service_.get()); |
| 263 | } |
| 264 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 265 | } // namespace shill |