blob: 87eb6270ea4d6334f83812823c10f3b76d2bf2b6 [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"
23#include "shill/property_store_unittest.h"
24#include "shill/shill_event.h"
25
26using std::map;
27using std::string;
28using std::vector;
29using ::testing::_;
30using ::testing::NiceMock;
31using ::testing::Return;
32using ::testing::Test;
Chris Masoneb925cc82011-06-22 15:39:57 -070033using ::testing::Values;
Darin Petkovafa6fc42011-06-21 16:21:08 -070034
35namespace shill {
36
37namespace {
38const char kDeviceName[] = "testdevice";
39} // namespace {}
40
Chris Masone3bd3c8c2011-06-13 08:20:26 -070041class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070042 public:
43 DeviceTest()
44 : device_(new Device(&control_interface_, NULL, NULL, kDeviceName, 0)) {
45 DHCPProvider::GetInstance()->glib_ = &glib_;
46 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070048
49 protected:
50 MockGLib glib_;
51 MockControl control_interface_;
52 DeviceRefPtr device_;
53};
54
Chris Masone3bd3c8c2011-06-13 08:20:26 -070055TEST_F(DeviceTest, Contains) {
56 EXPECT_TRUE(device_->Contains(flimflam::kNameProperty));
57 EXPECT_FALSE(device_->Contains(""));
58}
59
Chris Masonea8a2c252011-06-27 22:16:30 -070060TEST_F(DeviceTest, GetProperties) {
61 map<string, ::DBus::Variant> props;
62 Error error(Error::kInvalidProperty, "");
63 {
64 ::DBus::Error dbus_error;
65 bool expected = true;
66 device_->SetBoolProperty(flimflam::kPoweredProperty, expected, &error);
67 DBusAdaptor::GetProperties(device_.get(), &props, &dbus_error);
68 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
69 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
70 expected);
71 }
72 {
73 ::DBus::Error dbus_error;
74 DBusAdaptor::GetProperties(device_.get(), &props, &dbus_error);
75 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
76 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
77 string(kDeviceName));
78 }
Chris Masone95207da2011-06-29 16:50:49 -070079 {
80 ::DBus::Error dbus_error;
81 DBusAdaptor::GetProperties(device_.get(), &props, &dbus_error);
82 ASSERT_FALSE(props.find(flimflam::kDBusObjectProperty) == props.end());
83 EXPECT_EQ(props[flimflam::kDBusObjectProperty].reader().get_string(),
84 string(DeviceMockAdaptor::kRpcId));
85 }
Chris Masonea8a2c252011-06-27 22:16:30 -070086}
87
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088TEST_F(DeviceTest, Dispatch) {
89 ::DBus::Error error;
90 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(),
91 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070092 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -070093 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070094
Chris Masoneb925cc82011-06-22 15:39:57 -070095 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone3bd3c8c2011-06-13 08:20:26 -070096 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(),
Chris Masoneb925cc82011-06-22 15:39:57 -070097 flimflam::kAddressProperty,
98 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -070099 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700100 EXPECT_EQ(invalid_args_, error.name());
101}
102
Chris Masoneb925cc82011-06-22 15:39:57 -0700103TEST_P(DeviceTest, TestProperty) {
104 // Ensure that an attempt to write unknown properties returns InvalidProperty.
105 ::DBus::Error error;
Chris Masonea8a2c252011-06-27 22:16:30 -0700106 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", GetParam(), &error));
Chris Masoneb925cc82011-06-22 15:39:57 -0700107 EXPECT_EQ(invalid_prop_, error.name());
108}
109
110INSTANTIATE_TEST_CASE_P(
111 DeviceTestInstance,
112 DeviceTest,
113 Values(PropertyStoreTest::kBoolV,
114 PropertyStoreTest::kByteV,
115 PropertyStoreTest::kStringV,
116 PropertyStoreTest::kInt16V,
117 PropertyStoreTest::kInt32V,
118 PropertyStoreTest::kUint16V,
119 PropertyStoreTest::kUint32V,
120 PropertyStoreTest::kStringsV,
121 PropertyStoreTest::kStringmapV,
122 PropertyStoreTest::kStringmapsV));
123
Darin Petkovafa6fc42011-06-21 16:21:08 -0700124TEST_F(DeviceTest, TechnologyIs) {
125 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
126}
127
128TEST_F(DeviceTest, DestroyIPConfig) {
129 ASSERT_FALSE(device_->ipconfig_.get());
130 device_->ipconfig_ = new IPConfig(kDeviceName);
131 device_->DestroyIPConfig();
132 ASSERT_FALSE(device_->ipconfig_.get());
133}
134
135TEST_F(DeviceTest, DestroyIPConfigNULL) {
136 ASSERT_FALSE(device_->ipconfig_.get());
137 device_->DestroyIPConfig();
138 ASSERT_FALSE(device_->ipconfig_.get());
139}
140
141TEST_F(DeviceTest, AcquireDHCPConfig) {
142 device_->ipconfig_ = new IPConfig("randomname");
143 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
144 .WillOnce(Return(false));
145 EXPECT_FALSE(device_->AcquireDHCPConfig());
146 ASSERT_TRUE(device_->ipconfig_.get());
147 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
148 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
149}
150
151} // namespace shill