blob: 46f2ed8dcb09d576abeb5fb1f46918f74cb814f8 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovafa6fc42011-06-21 16:21:08 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkovafa6fc42011-06-21 16:21:08 -07005#include "shill/device.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -07006
Chris Masone34af2182011-08-22 11:59:36 -07007#include <ctype.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07008#include <sys/socket.h>
9#include <linux/if.h> // Needs typedefs from sys/socket.h.
Chris Masone34af2182011-08-22 11:59:36 -070010
Chris Masone3bd3c8c2011-06-13 08:20:26 -070011#include <map>
12#include <string>
13#include <vector>
14
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
Chris Masone34af2182011-08-22 11:59:36 -070016#include <dbus-c++/dbus.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <gmock/gmock.h>
Chris Masone34af2182011-08-22 11:59:36 -070018#include <gtest/gtest.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019
20#include "shill/dbus_adaptor.h"
21#include "shill/dhcp_provider.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070022#include "shill/event_dispatcher.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070023#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070024#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070025#include "shill/mock_control.h"
26#include "shill/mock_device.h"
27#include "shill/mock_glib.h"
Chris Masone34af2182011-08-22 11:59:36 -070028#include "shill/mock_ipconfig.h"
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070029#include "shill/mock_rtnl_handler.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070030#include "shill/mock_service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070031#include "shill/mock_store.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070032#include "shill/property_store_unittest.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080033#include "shill/technology.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070034
35using std::map;
36using std::string;
37using std::vector;
38using ::testing::_;
Chris Masone5dec5f42011-07-22 14:07:55 -070039using ::testing::AtLeast;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070040using ::testing::NiceMock;
41using ::testing::Return;
Paul Stewart03dba0b2011-08-22 16:32:45 -070042using ::testing::StrictMock;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043using ::testing::Test;
Chris Masone34af2182011-08-22 11:59:36 -070044using ::testing::Values;
Darin Petkovafa6fc42011-06-21 16:21:08 -070045
46namespace shill {
47
Chris Masone3bd3c8c2011-06-13 08:20:26 -070048class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070049 public:
50 DeviceTest()
Chris Masone2176a882011-09-14 22:29:15 -070051 : device_(new Device(control_interface(),
Chris Masone626719f2011-08-18 16:58:48 -070052 NULL,
53 NULL,
Thieu Le3426c8f2012-01-11 17:35:11 -080054 NULL,
Chris Masone626719f2011-08-18 16:58:48 -070055 kDeviceName,
56 kDeviceAddress,
Gaurav Shah435de2c2011-11-17 19:01:07 -080057 0,
58 Technology::kUnknown)) {
Chris Masone2176a882011-09-14 22:29:15 -070059 DHCPProvider::GetInstance()->glib_ = glib();
60 DHCPProvider::GetInstance()->control_interface_ = control_interface();
Darin Petkovafa6fc42011-06-21 16:21:08 -070061 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070062 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070063
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070064 virtual void SetUp() {
65 device_->rtnl_handler_ = &rtnl_handler_;
66 }
67
Darin Petkovafa6fc42011-06-21 16:21:08 -070068 protected:
Chris Masone626719f2011-08-18 16:58:48 -070069 static const char kDeviceName[];
70 static const char kDeviceAddress[];
71
Darin Petkovafa6fc42011-06-21 16:21:08 -070072 MockControl control_interface_;
73 DeviceRefPtr device_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070074 StrictMock<MockRTNLHandler> rtnl_handler_;
Darin Petkovafa6fc42011-06-21 16:21:08 -070075};
76
Chris Masone626719f2011-08-18 16:58:48 -070077const char DeviceTest::kDeviceName[] = "testdevice";
78const char DeviceTest::kDeviceAddress[] = "address";
79
Chris Masone3bd3c8c2011-06-13 08:20:26 -070080TEST_F(DeviceTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070081 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
82 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070083}
84
Chris Masonea8a2c252011-06-27 22:16:30 -070085TEST_F(DeviceTest, GetProperties) {
86 map<string, ::DBus::Variant> props;
87 Error error(Error::kInvalidProperty, "");
88 {
89 ::DBus::Error dbus_error;
90 bool expected = true;
mukesh agrawalde29fa82011-09-16 16:16:36 -070091 device_->mutable_store()->SetBoolProperty(flimflam::kPoweredProperty,
92 expected,
93 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -070094 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070095 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
96 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
97 expected);
98 }
99 {
100 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700101 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700102 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
103 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
104 string(kDeviceName));
105 }
106}
107
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108TEST_F(DeviceTest, Dispatch) {
109 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700110 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700111 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -0700112 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700113 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114
Chris Masoneb925cc82011-06-22 15:39:57 -0700115 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
mukesh agrawalde29fa82011-09-16 16:16:36 -0700116 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masoneb925cc82011-06-22 15:39:57 -0700117 flimflam::kAddressProperty,
118 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700119 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700120 EXPECT_EQ(invalid_args(), error.name());
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700121}
122
Darin Petkovafa6fc42011-06-21 16:21:08 -0700123TEST_F(DeviceTest, TechnologyIs) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700124 EXPECT_FALSE(device_->TechnologyIs(Technology::kEthernet));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700125}
126
127TEST_F(DeviceTest, DestroyIPConfig) {
128 ASSERT_FALSE(device_->ipconfig_.get());
Chris Masone2176a882011-09-14 22:29:15 -0700129 device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700130 device_->DestroyIPConfig();
131 ASSERT_FALSE(device_->ipconfig_.get());
132}
133
134TEST_F(DeviceTest, DestroyIPConfigNULL) {
135 ASSERT_FALSE(device_->ipconfig_.get());
136 device_->DestroyIPConfig();
137 ASSERT_FALSE(device_->ipconfig_.get());
138}
139
Paul Stewart2bf1d352011-12-06 15:02:55 -0800140TEST_F(DeviceTest, AcquireIPConfig) {
Chris Masone2176a882011-09-14 22:29:15 -0700141 device_->ipconfig_ = new IPConfig(control_interface(), "randomname");
142 EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _))
Darin Petkovafa6fc42011-06-21 16:21:08 -0700143 .WillOnce(Return(false));
Paul Stewart2bf1d352011-12-06 15:02:55 -0800144 EXPECT_FALSE(device_->AcquireIPConfig());
Darin Petkovafa6fc42011-06-21 16:21:08 -0700145 ASSERT_TRUE(device_->ipconfig_.get());
146 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
147 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
148}
149
Chris Masone5dec5f42011-07-22 14:07:55 -0700150TEST_F(DeviceTest, Load) {
151 NiceMock<MockStore> storage;
152 const string id = device_->GetStorageIdentifier();
153 EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true));
154 EXPECT_CALL(storage, GetBool(id, _, _))
155 .Times(AtLeast(1))
156 .WillRepeatedly(Return(true));
157 EXPECT_TRUE(device_->Load(&storage));
158}
159
160TEST_F(DeviceTest, Save) {
161 NiceMock<MockStore> storage;
162 const string id = device_->GetStorageIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -0700163 EXPECT_CALL(storage, SetString(id, _, _))
164 .Times(AtLeast(1))
165 .WillRepeatedly(Return(true));
166 EXPECT_CALL(storage, SetBool(id, _, _))
167 .Times(AtLeast(1))
168 .WillRepeatedly(Return(true));
Chris Masone2176a882011-09-14 22:29:15 -0700169 scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(),
Chris Masone34af2182011-08-22 11:59:36 -0700170 kDeviceName);
171 EXPECT_CALL(*ipconfig.get(), Save(_, _))
172 .WillOnce(Return(true));
173 device_->ipconfig_ = ipconfig;
Chris Masone5dec5f42011-07-22 14:07:55 -0700174 EXPECT_TRUE(device_->Save(&storage));
175}
176
Chris Masone34af2182011-08-22 11:59:36 -0700177TEST_F(DeviceTest, StorageIdGeneration) {
178 string to_process("/device/stuff/0");
179 ControlInterface::RpcIdToStorageId(&to_process);
180 EXPECT_TRUE(isalpha(to_process[0]));
181 EXPECT_EQ(string::npos, to_process.find('/'));
182}
183
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800184MATCHER(IsNullRefPtr, "") {
185 return !arg;
186}
187
188MATCHER(NotNullRefPtr, "") {
189 return arg;
190}
191
Paul Stewart03dba0b2011-08-22 16:32:45 -0700192TEST_F(DeviceTest, SelectedService) {
193 EXPECT_FALSE(device_->selected_service_.get());
194 device_->SetServiceState(Service::kStateAssociating);
195 scoped_refptr<MockService> service(
Chris Masone2176a882011-09-14 22:29:15 -0700196 new StrictMock<MockService>(control_interface(),
197 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800198 metrics(),
Chris Masone9d779932011-08-25 16:33:41 -0700199 manager()));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700200 device_->SelectService(service);
201 EXPECT_TRUE(device_->selected_service_.get() == service.get());
202
203 EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring));
204 device_->SetServiceState(Service::kStateConfiguring);
205 EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange));
206 device_->SetServiceFailure(Service::kFailureOutOfRange);
207
208 // Service should be returned to "Idle" state
209 EXPECT_CALL(*service.get(), state())
210 .WillOnce(Return(Service::kStateUnknown));
211 EXPECT_CALL(*service.get(), SetState(Service::kStateIdle));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800212 EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr()));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700213 device_->SelectService(NULL);
214
215 // A service in the "Failure" state should not be reset to "Idle"
216 device_->SelectService(service);
217 EXPECT_CALL(*service.get(), state())
218 .WillOnce(Return(Service::kStateFailure));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800219 EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr()));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700220 device_->SelectService(NULL);
221}
222
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800223TEST_F(DeviceTest, IPConfigUpdatedFailure) {
224 scoped_refptr<MockService> service(
225 new StrictMock<MockService>(control_interface(),
226 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800227 metrics(),
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800228 manager()));
229 device_->SelectService(service);
230 EXPECT_CALL(*service.get(), SetState(Service::kStateDisconnected));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800231 EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr()));
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800232 device_->IPConfigUpdatedCallback(NULL, false);
233}
234
235TEST_F(DeviceTest, IPConfigUpdatedSuccess) {
236 scoped_refptr<MockService> service(
237 new StrictMock<MockService>(control_interface(),
238 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800239 metrics(),
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800240 manager()));
241 device_->SelectService(service);
242 device_->manager_ = manager();
243 scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(),
244 kDeviceName);
245 EXPECT_CALL(*service.get(), SetState(Service::kStateConnected));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800246 EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr()));
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800247 device_->IPConfigUpdatedCallback(ipconfig.get(), true);
248}
249
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700250TEST_F(DeviceTest, Stop) {
251 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
252 scoped_refptr<MockService> service(
253 new NiceMock<MockService>(&control_interface_,
254 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800255 metrics(),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700256 manager()));
257 device_->SelectService(service);
258
259 EXPECT_CALL(*service.get(), state()).
260 WillRepeatedly(Return(Service::kStateConnected));
261 EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()),
262 UpdateEnabled());
263 EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP));
264 device_->Stop();
265
266 EXPECT_FALSE(device_->ipconfig_.get());
267 EXPECT_FALSE(device_->selected_service_.get());
268}
269
Darin Petkovafa6fc42011-06-21 16:21:08 -0700270} // namespace shill