blob: 604607e45f3f32de93749d0a36d54ab22b5812cb [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 Masone3bd3c8c2011-06-13 08:20:26 -07007#include <map>
8#include <string>
9#include <vector>
10
11#include <dbus-c++/dbus.h>
12#include <chromeos/dbus/service_constants.h>
13#include <gtest/gtest.h>
14#include <gmock/gmock.h>
15
16#include "shill/dbus_adaptor.h"
17#include "shill/dhcp_provider.h"
18#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070019#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/mock_control.h"
21#include "shill/mock_device.h"
22#include "shill/mock_glib.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070023#include "shill/mock_store.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070024#include "shill/property_store_unittest.h"
25#include "shill/shill_event.h"
26
27using std::map;
28using std::string;
29using std::vector;
30using ::testing::_;
Chris Masone5dec5f42011-07-22 14:07:55 -070031using ::testing::AtLeast;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070032using ::testing::NiceMock;
33using ::testing::Return;
34using ::testing::Test;
Darin Petkovafa6fc42011-06-21 16:21:08 -070035
36namespace shill {
37
Chris Masone3bd3c8c2011-06-13 08:20:26 -070038class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070039 public:
40 DeviceTest()
Chris Masone626719f2011-08-18 16:58:48 -070041 : device_(new Device(&control_interface_,
42 NULL,
43 NULL,
44 kDeviceName,
45 kDeviceAddress,
46 0)) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070047 DHCPProvider::GetInstance()->glib_ = &glib_;
Chris Masone19e30402011-07-19 15:48:47 -070048 DHCPProvider::GetInstance()->control_interface_ = &control_interface_;
Darin Petkovafa6fc42011-06-21 16:21:08 -070049 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070050 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070051
52 protected:
Chris Masone626719f2011-08-18 16:58:48 -070053 static const char kDeviceName[];
54 static const char kDeviceAddress[];
55
Darin Petkovafa6fc42011-06-21 16:21:08 -070056 MockGLib glib_;
57 MockControl control_interface_;
58 DeviceRefPtr device_;
59};
60
Chris Masone626719f2011-08-18 16:58:48 -070061const char DeviceTest::kDeviceName[] = "testdevice";
62const char DeviceTest::kDeviceAddress[] = "address";
63
Chris Masone3bd3c8c2011-06-13 08:20:26 -070064TEST_F(DeviceTest, Contains) {
Chris Masone27c4aa52011-07-02 13:10:14 -070065 EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty));
66 EXPECT_FALSE(device_->store()->Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070067}
68
Chris Masonea8a2c252011-06-27 22:16:30 -070069TEST_F(DeviceTest, GetProperties) {
70 map<string, ::DBus::Variant> props;
71 Error error(Error::kInvalidProperty, "");
72 {
73 ::DBus::Error dbus_error;
74 bool expected = true;
Chris Masone27c4aa52011-07-02 13:10:14 -070075 device_->store()->SetBoolProperty(flimflam::kPoweredProperty,
76 expected,
77 &error);
78 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070079 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
80 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
81 expected);
82 }
83 {
84 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -070085 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070086 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
87 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
88 string(kDeviceName));
89 }
90}
91
Chris Masone3bd3c8c2011-06-13 08:20:26 -070092TEST_F(DeviceTest, Dispatch) {
93 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -070094 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -070095 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070096 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -070097 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070098
Chris Masoneb925cc82011-06-22 15:39:57 -070099 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone27c4aa52011-07-02 13:10:14 -0700100 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masoneb925cc82011-06-22 15:39:57 -0700101 flimflam::kAddressProperty,
102 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -0700103 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700104 EXPECT_EQ(invalid_args_, error.name());
105}
106
Darin Petkovafa6fc42011-06-21 16:21:08 -0700107TEST_F(DeviceTest, TechnologyIs) {
108 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
109}
110
111TEST_F(DeviceTest, DestroyIPConfig) {
112 ASSERT_FALSE(device_->ipconfig_.get());
Chris Masone19e30402011-07-19 15:48:47 -0700113 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700114 device_->DestroyIPConfig();
115 ASSERT_FALSE(device_->ipconfig_.get());
116}
117
118TEST_F(DeviceTest, DestroyIPConfigNULL) {
119 ASSERT_FALSE(device_->ipconfig_.get());
120 device_->DestroyIPConfig();
121 ASSERT_FALSE(device_->ipconfig_.get());
122}
123
124TEST_F(DeviceTest, AcquireDHCPConfig) {
Chris Masone19e30402011-07-19 15:48:47 -0700125 device_->ipconfig_ = new IPConfig(&control_interface_, "randomname");
Darin Petkovafa6fc42011-06-21 16:21:08 -0700126 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
127 .WillOnce(Return(false));
128 EXPECT_FALSE(device_->AcquireDHCPConfig());
129 ASSERT_TRUE(device_->ipconfig_.get());
130 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
131 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
132}
133
Chris Masone5dec5f42011-07-22 14:07:55 -0700134TEST_F(DeviceTest, Load) {
135 NiceMock<MockStore> storage;
136 const string id = device_->GetStorageIdentifier();
137 EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true));
138 EXPECT_CALL(storage, GetBool(id, _, _))
139 .Times(AtLeast(1))
140 .WillRepeatedly(Return(true));
141 EXPECT_TRUE(device_->Load(&storage));
142}
143
144TEST_F(DeviceTest, Save) {
145 NiceMock<MockStore> storage;
146 const string id = device_->GetStorageIdentifier();
147 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
148 EXPECT_CALL(storage, SetString(id, _, _))
149 .Times(AtLeast(1))
150 .WillRepeatedly(Return(true));
151 EXPECT_CALL(storage, SetBool(id, _, _))
152 .Times(AtLeast(1))
153 .WillRepeatedly(Return(true));
154 EXPECT_TRUE(device_->Save(&storage));
155}
156
Darin Petkovafa6fc42011-06-21 16:21:08 -0700157} // namespace shill