blob: 20ca565c9adc9962bc026e92c011c1a04ff73b9a [file] [log] [blame]
Darin Petkovafa6fc42011-06-21 16:21:08 -07001// 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 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,
54 kDeviceName,
55 kDeviceAddress,
Gaurav Shah435de2c2011-11-17 19:01:07 -080056 0,
57 Technology::kUnknown)) {
Chris Masone2176a882011-09-14 22:29:15 -070058 DHCPProvider::GetInstance()->glib_ = glib();
59 DHCPProvider::GetInstance()->control_interface_ = control_interface();
Darin Petkovafa6fc42011-06-21 16:21:08 -070060 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070062
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070063 virtual void SetUp() {
64 device_->rtnl_handler_ = &rtnl_handler_;
65 }
66
Darin Petkovafa6fc42011-06-21 16:21:08 -070067 protected:
Chris Masone626719f2011-08-18 16:58:48 -070068 static const char kDeviceName[];
69 static const char kDeviceAddress[];
70
Darin Petkovafa6fc42011-06-21 16:21:08 -070071 MockControl control_interface_;
72 DeviceRefPtr device_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070073 StrictMock<MockRTNLHandler> rtnl_handler_;
Darin Petkovafa6fc42011-06-21 16:21:08 -070074};
75
Chris Masone626719f2011-08-18 16:58:48 -070076const char DeviceTest::kDeviceName[] = "testdevice";
77const char DeviceTest::kDeviceAddress[] = "address";
78
Chris Masone3bd3c8c2011-06-13 08:20:26 -070079TEST_F(DeviceTest, Contains) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070080 EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty));
81 EXPECT_FALSE(device_->store().Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070082}
83
Chris Masonea8a2c252011-06-27 22:16:30 -070084TEST_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 agrawalde29fa82011-09-16 16:16:36 -070090 device_->mutable_store()->SetBoolProperty(flimflam::kPoweredProperty,
91 expected,
92 &error);
Chris Masone27c4aa52011-07-02 13:10:14 -070093 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070094 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 Masone27c4aa52011-07-02 13:10:14 -0700100 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700101 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
102 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
103 string(kDeviceName));
104 }
105}
106
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700107TEST_F(DeviceTest, Dispatch) {
108 ::DBus::Error error;
mukesh agrawalde29fa82011-09-16 16:16:36 -0700109 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -0700111 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700112 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700113
Chris Masoneb925cc82011-06-22 15:39:57 -0700114 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
mukesh agrawalde29fa82011-09-16 16:16:36 -0700115 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->mutable_store(),
Chris Masoneb925cc82011-06-22 15:39:57 -0700116 flimflam::kAddressProperty,
117 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700118 &error));
Chris Masone9d779932011-08-25 16:33:41 -0700119 EXPECT_EQ(invalid_args(), error.name());
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700120}
121
Darin Petkovafa6fc42011-06-21 16:21:08 -0700122TEST_F(DeviceTest, TechnologyIs) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700123 EXPECT_FALSE(device_->TechnologyIs(Technology::kEthernet));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700124}
125
126TEST_F(DeviceTest, DestroyIPConfig) {
127 ASSERT_FALSE(device_->ipconfig_.get());
Chris Masone2176a882011-09-14 22:29:15 -0700128 device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700129 device_->DestroyIPConfig();
130 ASSERT_FALSE(device_->ipconfig_.get());
131}
132
133TEST_F(DeviceTest, DestroyIPConfigNULL) {
134 ASSERT_FALSE(device_->ipconfig_.get());
135 device_->DestroyIPConfig();
136 ASSERT_FALSE(device_->ipconfig_.get());
137}
138
139TEST_F(DeviceTest, AcquireDHCPConfig) {
Chris Masone2176a882011-09-14 22:29:15 -0700140 device_->ipconfig_ = new IPConfig(control_interface(), "randomname");
141 EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _))
Darin Petkovafa6fc42011-06-21 16:21:08 -0700142 .WillOnce(Return(false));
143 EXPECT_FALSE(device_->AcquireDHCPConfig());
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 Masone5dec5f42011-07-22 14:07:55 -0700149TEST_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
159TEST_F(DeviceTest, Save) {
160 NiceMock<MockStore> storage;
161 const string id = device_->GetStorageIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -0700162 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 Masone2176a882011-09-14 22:29:15 -0700168 scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(),
Chris Masone34af2182011-08-22 11:59:36 -0700169 kDeviceName);
170 EXPECT_CALL(*ipconfig.get(), Save(_, _))
171 .WillOnce(Return(true));
172 device_->ipconfig_ = ipconfig;
Chris Masone5dec5f42011-07-22 14:07:55 -0700173 EXPECT_TRUE(device_->Save(&storage));
174}
175
Chris Masone34af2182011-08-22 11:59:36 -0700176TEST_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 Stewart03dba0b2011-08-22 16:32:45 -0700183TEST_F(DeviceTest, SelectedService) {
184 EXPECT_FALSE(device_->selected_service_.get());
185 device_->SetServiceState(Service::kStateAssociating);
186 scoped_refptr<MockService> service(
Chris Masone2176a882011-09-14 22:29:15 -0700187 new StrictMock<MockService>(control_interface(),
188 dispatcher(),
Chris Masone9d779932011-08-25 16:33:41 -0700189 manager()));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700190 device_->SelectService(service);
191 EXPECT_TRUE(device_->selected_service_.get() == service.get());
192
193 EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring));
194 device_->SetServiceState(Service::kStateConfiguring);
195 EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange));
196 device_->SetServiceFailure(Service::kFailureOutOfRange);
197
198 // Service should be returned to "Idle" state
199 EXPECT_CALL(*service.get(), state())
200 .WillOnce(Return(Service::kStateUnknown));
201 EXPECT_CALL(*service.get(), SetState(Service::kStateIdle));
202 device_->SelectService(NULL);
203
204 // A service in the "Failure" state should not be reset to "Idle"
205 device_->SelectService(service);
206 EXPECT_CALL(*service.get(), state())
207 .WillOnce(Return(Service::kStateFailure));
208 device_->SelectService(NULL);
209}
210
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700211TEST_F(DeviceTest, Stop) {
212 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
213 scoped_refptr<MockService> service(
214 new NiceMock<MockService>(&control_interface_,
215 dispatcher(),
216 manager()));
217 device_->SelectService(service);
218
219 EXPECT_CALL(*service.get(), state()).
220 WillRepeatedly(Return(Service::kStateConnected));
221 EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()),
222 UpdateEnabled());
223 EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP));
224 device_->Stop();
225
226 EXPECT_FALSE(device_->ipconfig_.get());
227 EXPECT_FALSE(device_->selected_service_.get());
228}
229
Darin Petkovafa6fc42011-06-21 16:21:08 -0700230} // namespace shill