blob: 24a28ebb826d9c246c03881da4b749b7eec57666 [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"
19#include "shill/mock_control.h"
20#include "shill/mock_device.h"
21#include "shill/mock_glib.h"
22#include "shill/property_store_unittest.h"
23#include "shill/shill_event.h"
24
25using std::map;
26using std::string;
27using std::vector;
28using ::testing::_;
29using ::testing::NiceMock;
30using ::testing::Return;
31using ::testing::Test;
Chris Masoneb925cc82011-06-22 15:39:57 -070032using ::testing::Values;
Darin Petkovafa6fc42011-06-21 16:21:08 -070033
34namespace shill {
35
36namespace {
37const char kDeviceName[] = "testdevice";
38} // namespace {}
39
Chris Masone3bd3c8c2011-06-13 08:20:26 -070040class DeviceTest : public PropertyStoreTest {
Darin Petkovafa6fc42011-06-21 16:21:08 -070041 public:
42 DeviceTest()
43 : device_(new Device(&control_interface_, NULL, NULL, kDeviceName, 0)) {
44 DHCPProvider::GetInstance()->glib_ = &glib_;
45 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070046 virtual ~DeviceTest() {}
Darin Petkovafa6fc42011-06-21 16:21:08 -070047
48 protected:
49 MockGLib glib_;
50 MockControl control_interface_;
51 DeviceRefPtr device_;
52};
53
Chris Masone3bd3c8c2011-06-13 08:20:26 -070054TEST_F(DeviceTest, Contains) {
55 EXPECT_TRUE(device_->Contains(flimflam::kNameProperty));
56 EXPECT_FALSE(device_->Contains(""));
57}
58
Chris Masonea8a2c252011-06-27 22:16:30 -070059TEST_F(DeviceTest, GetProperties) {
60 map<string, ::DBus::Variant> props;
61 Error error(Error::kInvalidProperty, "");
62 {
63 ::DBus::Error dbus_error;
64 bool expected = true;
65 device_->SetBoolProperty(flimflam::kPoweredProperty, expected, &error);
66 DBusAdaptor::GetProperties(device_.get(), &props, &dbus_error);
67 ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
68 EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
69 expected);
70 }
71 {
72 ::DBus::Error dbus_error;
73 DBusAdaptor::GetProperties(device_.get(), &props, &dbus_error);
74 ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
75 EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
76 string(kDeviceName));
77 }
78}
79
Chris Masone3bd3c8c2011-06-13 08:20:26 -070080TEST_F(DeviceTest, Dispatch) {
81 ::DBus::Error error;
82 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(),
83 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070084 PropertyStoreTest::kBoolV,
Chris Masonea8a2c252011-06-27 22:16:30 -070085 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070086
Chris Masoneb925cc82011-06-22 15:39:57 -070087 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(),
Chris Masoneb925cc82011-06-22 15:39:57 -070089 flimflam::kAddressProperty,
90 PropertyStoreTest::kStringV,
Chris Masonea8a2c252011-06-27 22:16:30 -070091 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -070092 EXPECT_EQ(invalid_args_, error.name());
93}
94
Chris Masoneb925cc82011-06-22 15:39:57 -070095TEST_P(DeviceTest, TestProperty) {
96 // Ensure that an attempt to write unknown properties returns InvalidProperty.
97 ::DBus::Error error;
Chris Masonea8a2c252011-06-27 22:16:30 -070098 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", GetParam(), &error));
Chris Masoneb925cc82011-06-22 15:39:57 -070099 EXPECT_EQ(invalid_prop_, error.name());
100}
101
102INSTANTIATE_TEST_CASE_P(
103 DeviceTestInstance,
104 DeviceTest,
105 Values(PropertyStoreTest::kBoolV,
106 PropertyStoreTest::kByteV,
107 PropertyStoreTest::kStringV,
108 PropertyStoreTest::kInt16V,
109 PropertyStoreTest::kInt32V,
110 PropertyStoreTest::kUint16V,
111 PropertyStoreTest::kUint32V,
112 PropertyStoreTest::kStringsV,
113 PropertyStoreTest::kStringmapV,
114 PropertyStoreTest::kStringmapsV));
115
Darin Petkovafa6fc42011-06-21 16:21:08 -0700116TEST_F(DeviceTest, TechnologyIs) {
117 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
118}
119
120TEST_F(DeviceTest, DestroyIPConfig) {
121 ASSERT_FALSE(device_->ipconfig_.get());
122 device_->ipconfig_ = new IPConfig(kDeviceName);
123 device_->DestroyIPConfig();
124 ASSERT_FALSE(device_->ipconfig_.get());
125}
126
127TEST_F(DeviceTest, DestroyIPConfigNULL) {
128 ASSERT_FALSE(device_->ipconfig_.get());
129 device_->DestroyIPConfig();
130 ASSERT_FALSE(device_->ipconfig_.get());
131}
132
133TEST_F(DeviceTest, AcquireDHCPConfig) {
134 device_->ipconfig_ = new IPConfig("randomname");
135 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
136 .WillOnce(Return(false));
137 EXPECT_FALSE(device_->AcquireDHCPConfig());
138 ASSERT_TRUE(device_->ipconfig_.get());
139 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
140 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
141}
142
143} // namespace shill