| Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 1 | // 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 Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 5 | #include "shill/device.h" | 
| Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 6 |  | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 7 | #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 Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame^] | 19 | #include "shill/mock_adaptors.h" | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #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 |  | 
|  | 26 | using std::map; | 
|  | 27 | using std::string; | 
|  | 28 | using std::vector; | 
|  | 29 | using ::testing::_; | 
|  | 30 | using ::testing::NiceMock; | 
|  | 31 | using ::testing::Return; | 
|  | 32 | using ::testing::Test; | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 33 | using ::testing::Values; | 
| Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | namespace shill { | 
|  | 36 |  | 
|  | 37 | namespace { | 
|  | 38 | const char kDeviceName[] = "testdevice"; | 
|  | 39 | }  // namespace {} | 
|  | 40 |  | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 41 | class DeviceTest : public PropertyStoreTest { | 
| Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 42 | public: | 
|  | 43 | DeviceTest() | 
|  | 44 | : device_(new Device(&control_interface_, NULL, NULL, kDeviceName, 0)) { | 
|  | 45 | DHCPProvider::GetInstance()->glib_ = &glib_; | 
|  | 46 | } | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 47 | virtual ~DeviceTest() {} | 
| Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | protected: | 
|  | 50 | MockGLib glib_; | 
|  | 51 | MockControl control_interface_; | 
|  | 52 | DeviceRefPtr device_; | 
|  | 53 | }; | 
|  | 54 |  | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 55 | TEST_F(DeviceTest, Contains) { | 
|  | 56 | EXPECT_TRUE(device_->Contains(flimflam::kNameProperty)); | 
|  | 57 | EXPECT_FALSE(device_->Contains("")); | 
|  | 58 | } | 
|  | 59 |  | 
| Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 60 | TEST_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 Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame^] | 79 | { | 
|  | 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 Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 88 | TEST_F(DeviceTest, Dispatch) { | 
|  | 89 | ::DBus::Error error; | 
|  | 90 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_.get(), | 
|  | 91 | flimflam::kPoweredProperty, | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 92 | PropertyStoreTest::kBoolV, | 
| Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 93 | &error)); | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 94 |  | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 95 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_.get(), | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 97 | flimflam::kAddressProperty, | 
|  | 98 | PropertyStoreTest::kStringV, | 
| Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 99 | &error)); | 
| Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 100 | EXPECT_EQ(invalid_args_, error.name()); | 
|  | 101 | } | 
|  | 102 |  | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 103 | TEST_P(DeviceTest, TestProperty) { | 
|  | 104 | // Ensure that an attempt to write unknown properties returns InvalidProperty. | 
|  | 105 | ::DBus::Error error; | 
| Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 106 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(&manager_, "", GetParam(), &error)); | 
| Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 107 | EXPECT_EQ(invalid_prop_, error.name()); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | INSTANTIATE_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 Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 124 | TEST_F(DeviceTest, TechnologyIs) { | 
|  | 125 | EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet)); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | TEST_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 |  | 
|  | 135 | TEST_F(DeviceTest, DestroyIPConfigNULL) { | 
|  | 136 | ASSERT_FALSE(device_->ipconfig_.get()); | 
|  | 137 | device_->DestroyIPConfig(); | 
|  | 138 | ASSERT_FALSE(device_->ipconfig_.get()); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | TEST_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 |