blob: c452931eac64f8d3a000ffd6a059d20f6bc45d6c [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
59TEST_F(DeviceTest, Dispatch) {
60 ::DBus::Error error;
61 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(),
62 flimflam::kPoweredProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070063 PropertyStoreTest::kBoolV,
64 error));
65 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(),
66 flimflam::kBgscanMethodProperty,
67 PropertyStoreTest::kStringV,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070068 error));
69 EXPECT_TRUE(DBusAdaptor::DispatchOnType(
70 device_.get(),
71 flimflam::kBgscanSignalThresholdProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070072 PropertyStoreTest::kInt32V,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070073 error));
74 EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(),
75 flimflam::kScanIntervalProperty,
Chris Masoneb925cc82011-06-22 15:39:57 -070076 PropertyStoreTest::kUint16V,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070077 error));
78
Chris Masoneb925cc82011-06-22 15:39:57 -070079 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masone3bd3c8c2011-06-13 08:20:26 -070080 EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(),
Chris Masoneb925cc82011-06-22 15:39:57 -070081 flimflam::kAddressProperty,
82 PropertyStoreTest::kStringV,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070083 error));
84 EXPECT_EQ(invalid_args_, error.name());
85}
86
Chris Masoneb925cc82011-06-22 15:39:57 -070087TEST_P(DeviceTest, TestProperty) {
88 // Ensure that an attempt to write unknown properties returns InvalidProperty.
89 ::DBus::Error error;
90 EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", GetParam(), error));
91 EXPECT_EQ(invalid_prop_, error.name());
92}
93
94INSTANTIATE_TEST_CASE_P(
95 DeviceTestInstance,
96 DeviceTest,
97 Values(PropertyStoreTest::kBoolV,
98 PropertyStoreTest::kByteV,
99 PropertyStoreTest::kStringV,
100 PropertyStoreTest::kInt16V,
101 PropertyStoreTest::kInt32V,
102 PropertyStoreTest::kUint16V,
103 PropertyStoreTest::kUint32V,
104 PropertyStoreTest::kStringsV,
105 PropertyStoreTest::kStringmapV,
106 PropertyStoreTest::kStringmapsV));
107
Darin Petkovafa6fc42011-06-21 16:21:08 -0700108TEST_F(DeviceTest, TechnologyIs) {
109 EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet));
110}
111
112TEST_F(DeviceTest, DestroyIPConfig) {
113 ASSERT_FALSE(device_->ipconfig_.get());
114 device_->ipconfig_ = new IPConfig(kDeviceName);
115 device_->DestroyIPConfig();
116 ASSERT_FALSE(device_->ipconfig_.get());
117}
118
119TEST_F(DeviceTest, DestroyIPConfigNULL) {
120 ASSERT_FALSE(device_->ipconfig_.get());
121 device_->DestroyIPConfig();
122 ASSERT_FALSE(device_->ipconfig_.get());
123}
124
125TEST_F(DeviceTest, AcquireDHCPConfig) {
126 device_->ipconfig_ = new IPConfig("randomname");
127 EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
128 .WillOnce(Return(false));
129 EXPECT_FALSE(device_->AcquireDHCPConfig());
130 ASSERT_TRUE(device_->ipconfig_.get());
131 EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name());
132 EXPECT_TRUE(device_->ipconfig_->update_callback_.get());
133}
134
135} // namespace shill