blob: 8142277196b7647f57a5c0cc65dae86629591f47 [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
38namespace {
39const char kDeviceName[] = "testdevice";
40} // namespace {}
41
Chris Masone3bd3c8c2011-06-13 08:20:26 -070042class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070043 public:
44 DeviceTest()
45 : device_(new Device(&control_interface_, NULL, NULL, kDeviceName, 0)) {
46 DHCPProvider::GetInstance()->glib_ = &glib_;
Chris Masone19e30402011-07-19 15:48:47 -070047 DHCPProvider::GetInstance()->control_interface_ = &control_interface_;
Darin Petkovafa6fc42011-06-21 16:21:08 -070048 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070049 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070050
51 protected:
52 MockGLib glib_;
53 MockControl control_interface_;
54 DeviceRefPtr device_;
55};
56
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057TEST_F(DeviceTest, Contains) {
Chris Masone27c4aa52011-07-02 13:10:14 -070058 EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty));
59 EXPECT_FALSE(device_->store()->Contains(""));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060}
61
Chris Masonea8a2c252011-06-27 22:16:30 -070062TEST_F(DeviceTest, GetProperties) {
63 map<string, ::DBus::Variant> props;
64 Error error(Error::kInvalidProperty, "");
65 {
66 ::DBus::Error dbus_error;
67 bool expected = true;
Chris Masone27c4aa52011-07-02 13:10:14 -070068 device_->store()->SetBoolProperty(flimflam::kPoweredProperty,
69 expected,
70 &error);
71 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070072 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
73 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
74 expected);
75 }
76 {
77 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -070078 DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070079 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
80 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
81 string(kDeviceName));
82 }
83}
84
Chris Masone3bd3c8c2011-06-13 08:20:26 -070085TEST_F(DeviceTest, Dispatch) {
86 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -070087 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070089 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -070090 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070091
Chris Masoneb925cc82011-06-22 15:39:57 -070092 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone27c4aa52011-07-02 13:10:14 -070093 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(),
Chris Masoneb925cc82011-06-22 15:39:57 -070094 flimflam::kAddressProperty,
95 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -070096 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070097 EXPECT_EQ(invalid_args_, error.name());
98}
99
Darin Petkovafa6fc42011-06-21 16:21:08 -0700100TEST_F(DeviceTest, TechnologyIs) {
101 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
102}
103
104TEST_F(DeviceTest, DestroyIPConfig) {
105 ASSERT_FALSE(device_->ipconfig_.get());
Chris Masone19e30402011-07-19 15:48:47 -0700106 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700107 device_->DestroyIPConfig();
108 ASSERT_FALSE(device_->ipconfig_.get());
109}
110
111TEST_F(DeviceTest, DestroyIPConfigNULL) {
112 ASSERT_FALSE(device_->ipconfig_.get());
113 device_->DestroyIPConfig();
114 ASSERT_FALSE(device_->ipconfig_.get());
115}
116
117TEST_F(DeviceTest, AcquireDHCPConfig) {
Chris Masone19e30402011-07-19 15:48:47 -0700118 device_->ipconfig_ = new IPConfig(&control_interface_, "randomname");
Darin Petkovafa6fc42011-06-21 16:21:08 -0700119 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
120 .WillOnce(Return(false));
121 EXPECT_FALSE(device_->AcquireDHCPConfig());
122 ASSERT_TRUE(device_->ipconfig_.get());
123 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
124 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
125}
126
Chris Masone5dec5f42011-07-22 14:07:55 -0700127TEST_F(DeviceTest, Load) {
128 NiceMock<MockStore> storage;
129 const string id = device_->GetStorageIdentifier();
130 EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true));
131 EXPECT_CALL(storage, GetBool(id, _, _))
132 .Times(AtLeast(1))
133 .WillRepeatedly(Return(true));
134 EXPECT_TRUE(device_->Load(&storage));
135}
136
137TEST_F(DeviceTest, Save) {
138 NiceMock<MockStore> storage;
139 const string id = device_->GetStorageIdentifier();
140 device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
141 EXPECT_CALL(storage, SetString(id, _, _))
142 .Times(AtLeast(1))
143 .WillRepeatedly(Return(true));
144 EXPECT_CALL(storage, SetBool(id, _, _))
145 .Times(AtLeast(1))
146 .WillRepeatedly(Return(true));
147 EXPECT_TRUE(device_->Save(&storage));
148}
149
Darin Petkovafa6fc42011-06-21 16:21:08 -0700150} // namespace shill