blob: 74256a7338f4cfe03c38433e929a468cdf55da57 [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>
8
Chris Masone3bd3c8c2011-06-13 08:20:26 -07009#include <map>
10#include <string>
11#include <vector>
12
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013#include <chromeos/dbus/service_constants.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <dbus-c++/dbus.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <gmock/gmock.h>
Chris Masone34af2182011-08-22 11:59:36 -070016#include <gtest/gtest.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017
18#include "shill/dbus_adaptor.h"
19#include "shill/dhcp_provider.h"
20#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070021#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022#include "shill/mock_control.h"
23#include "shill/mock_device.h"
24#include "shill/mock_glib.h"
Chris Masone34af2182011-08-22 11:59:36 -070025#include "shill/mock_ipconfig.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070026#include "shill/mock_store.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070027#include "shill/property_store_unittest.h"
28#include "shill/shill_event.h"
29
30using std::map;
31using std::string;
32using std::vector;
33using ::testing::_;
Chris Masone5dec5f42011-07-22 14:07:55 -070034using ::testing::AtLeast;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070035using ::testing::NiceMock;
36using ::testing::Return;
37using ::testing::Test;
Chris Masone34af2182011-08-22 11:59:36 -070038using ::testing::Values;
Darin Petkovafa6fc42011-06-21 16:21:08 -070039
40namespace shill {
41
Chris Masone3bd3c8c2011-06-13 08:20:26 -070042class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070043 public:
44 DeviceTest()
Chris Masone626719f2011-08-18 16:58:48 -070045 : device_(new Device(&control_interface_,
46 NULL,
47 NULL,
48 kDeviceName,
49 kDeviceAddress,
50 0)) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070051 DHCPProvider::GetInstance()->glib_ = &glib_;
Chris Masone19e30402011-07-19 15:48:47 -070052 DHCPProvider::GetInstance()->control_interface_ = &control_interface_;
Darin Petkovafa6fc42011-06-21 16:21:08 -070053 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070054 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070055
56 protected:
Chris Masone626719f2011-08-18 16:58:48 -070057 static const char kDeviceName[];
58 static const char kDeviceAddress[];
59
Darin Petkovafa6fc42011-06-21 16:21:08 -070060 MockGLib glib_;
61 MockControl control_interface_;
62 DeviceRefPtr device_;
63};
64
Chris Masone626719f2011-08-18 16:58:48 -070065const char DeviceTest::kDeviceName[] = "testdevice";
66const char DeviceTest::kDeviceAddress[] = "address";
67
Chris Masone3bd3c8c2011-06-13 08:20:26 -070068TEST_F(DeviceTest, Contains) {
Chris Masone27c4aa52011-07-02 13:10:14 -070069 EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty));
70 EXPECT_FALSE(device_->store()->Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070071}
72
Chris Masonea8a2c252011-06-27 22:16:30 -070073TEST_F(DeviceTest, GetProperties) {
74 map<string, ::DBus::Variant> props;
75 Error error(Error::kInvalidProperty, "");
76 {
77 ::DBus::Error dbus_error;
78 bool expected = true;
Chris Masone27c4aa52011-07-02 13:10:14 -070079 device_->store()->SetBoolProperty(flimflam::kPoweredProperty,
80 expected,
81 &error);
82 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070083 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
84 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
85 expected);
86 }
87 {
88 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -070089 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070090 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
91 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
92 string(kDeviceName));
93 }
94}
95
Chris Masone3bd3c8c2011-06-13 08:20:26 -070096TEST_F(DeviceTest, Dispatch) {
97 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -070098 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -070099 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -0700100 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700101 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700102
Chris Masoneb925cc82011-06-22 15:39:57 -0700103 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone27c4aa52011-07-02 13:10:14 -0700104 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masoneb925cc82011-06-22 15:39:57 -0700105 flimflam::kAddressProperty,
106 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700107 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108 EXPECT_EQ(invalid_args_, error.name());
109}
110
Darin Petkovafa6fc42011-06-21 16:21:08 -0700111TEST_F(DeviceTest, TechnologyIs) {
112 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
113}
114
115TEST_F(DeviceTest, DestroyIPConfig) {
116 ASSERT_FALSE(device_->ipconfig_.get());
Chris Masone19e30402011-07-19 15:48:47 -0700117 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700118 device_->DestroyIPConfig();
119 ASSERT_FALSE(device_->ipconfig_.get());
120}
121
122TEST_F(DeviceTest, DestroyIPConfigNULL) {
123 ASSERT_FALSE(device_->ipconfig_.get());
124 device_->DestroyIPConfig();
125 ASSERT_FALSE(device_->ipconfig_.get());
126}
127
128TEST_F(DeviceTest, AcquireDHCPConfig) {
Chris Masone19e30402011-07-19 15:48:47 -0700129 device_->ipconfig_ = new IPConfig(&control_interface_, "randomname");
Darin Petkovafa6fc42011-06-21 16:21:08 -0700130 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
131 .WillOnce(Return(false));
132 EXPECT_FALSE(device_->AcquireDHCPConfig());
133 ASSERT_TRUE(device_->ipconfig_.get());
134 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
135 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
136}
137
Chris Masone5dec5f42011-07-22 14:07:55 -0700138TEST_F(DeviceTest, Load) {
139 NiceMock<MockStore> storage;
140 const string id = device_->GetStorageIdentifier();
141 EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true));
142 EXPECT_CALL(storage, GetBool(id, _, _))
143 .Times(AtLeast(1))
144 .WillRepeatedly(Return(true));
145 EXPECT_TRUE(device_->Load(&storage));
146}
147
148TEST_F(DeviceTest, Save) {
149 NiceMock<MockStore> storage;
150 const string id = device_->GetStorageIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -0700151 EXPECT_CALL(storage, SetString(id, _, _))
152 .Times(AtLeast(1))
153 .WillRepeatedly(Return(true));
154 EXPECT_CALL(storage, SetBool(id, _, _))
155 .Times(AtLeast(1))
156 .WillRepeatedly(Return(true));
Chris Masone34af2182011-08-22 11:59:36 -0700157 scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(&control_interface_,
158 kDeviceName);
159 EXPECT_CALL(*ipconfig.get(), Save(_, _))
160 .WillOnce(Return(true));
161 device_->ipconfig_ = ipconfig;
Chris Masone5dec5f42011-07-22 14:07:55 -0700162 EXPECT_TRUE(device_->Save(&storage));
163}
164
Chris Masone34af2182011-08-22 11:59:36 -0700165TEST_F(DeviceTest, StorageIdGeneration) {
166 string to_process("/device/stuff/0");
167 ControlInterface::RpcIdToStorageId(&to_process);
168 EXPECT_TRUE(isalpha(to_process[0]));
169 EXPECT_EQ(string::npos, to_process.find('/'));
170}
171
Darin Petkovafa6fc42011-06-21 16:21:08 -0700172} // namespace shill