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" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 23 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | #include "shill/property_store_unittest.h" |
| 25 | #include "shill/shill_event.h" |
| 26 | |
| 27 | using std::map; |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | using ::testing::_; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 31 | using ::testing::AtLeast; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | using ::testing::NiceMock; |
| 33 | using ::testing::Return; |
| 34 | using ::testing::Test; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 35 | |
| 36 | namespace shill { |
| 37 | |
| 38 | namespace { |
| 39 | const char kDeviceName[] = "testdevice"; |
| 40 | } // namespace {} |
| 41 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 43 | public: |
| 44 | DeviceTest() |
| 45 | : device_(new Device(&control_interface_, NULL, NULL, kDeviceName, 0)) { |
| 46 | DHCPProvider::GetInstance()->glib_ = &glib_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 47 | DHCPProvider::GetInstance()->control_interface_ = &control_interface_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 48 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 50 | |
| 51 | protected: |
| 52 | MockGLib glib_; |
| 53 | MockControl control_interface_; |
| 54 | DeviceRefPtr device_; |
| 55 | }; |
| 56 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 57 | TEST_F(DeviceTest, Contains) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 58 | EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty)); |
| 59 | EXPECT_FALSE(device_->store()->Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 62 | TEST_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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 68 | device_->store()->SetBoolProperty(flimflam::kPoweredProperty, |
| 69 | expected, |
| 70 | &error); |
| 71 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 72 | 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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 78 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 79 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 80 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 81 | string(kDeviceName)); |
| 82 | } |
| 83 | } |
| 84 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 85 | TEST_F(DeviceTest, Dispatch) { |
| 86 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 88 | flimflam::kPoweredProperty, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 89 | PropertyStoreTest::kBoolV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 90 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 91 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 92 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 93 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 94 | flimflam::kAddressProperty, |
| 95 | PropertyStoreTest::kStringV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 96 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 97 | EXPECT_EQ(invalid_args_, error.name()); |
| 98 | } |
| 99 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 100 | TEST_F(DeviceTest, TechnologyIs) { |
| 101 | EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 105 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 106 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 107 | device_->DestroyIPConfig(); |
| 108 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 112 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 113 | device_->DestroyIPConfig(); |
| 114 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 115 | } |
| 116 | |
| 117 | TEST_F(DeviceTest, AcquireDHCPConfig) { |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 118 | device_->ipconfig_ = new IPConfig(&control_interface_, "randomname"); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 119 | 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 Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 127 | TEST_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 | |
| 137 | TEST_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 Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 150 | } // namespace shill |