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 | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 7 | #include <ctype.h> |
| 8 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 14 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 16 | #include <gtest/gtest.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/dbus_adaptor.h" |
| 19 | #include "shill/dhcp_provider.h" |
| 20 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 21 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/mock_control.h" |
| 23 | #include "shill/mock_device.h" |
| 24 | #include "shill/mock_glib.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 25 | #include "shill/mock_ipconfig.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 26 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 27 | #include "shill/property_store_unittest.h" |
| 28 | #include "shill/shill_event.h" |
| 29 | |
| 30 | using std::map; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | using ::testing::_; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 34 | using ::testing::AtLeast; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 35 | using ::testing::NiceMock; |
| 36 | using ::testing::Return; |
| 37 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 38 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 39 | |
| 40 | namespace shill { |
| 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() |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 45 | : device_(new Device(&control_interface_, |
| 46 | NULL, |
| 47 | NULL, |
| 48 | kDeviceName, |
| 49 | kDeviceAddress, |
| 50 | 0)) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 51 | DHCPProvider::GetInstance()->glib_ = &glib_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 52 | DHCPProvider::GetInstance()->control_interface_ = &control_interface_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 53 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 54 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 55 | |
| 56 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 57 | static const char kDeviceName[]; |
| 58 | static const char kDeviceAddress[]; |
| 59 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 60 | MockGLib glib_; |
| 61 | MockControl control_interface_; |
| 62 | DeviceRefPtr device_; |
| 63 | }; |
| 64 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 65 | const char DeviceTest::kDeviceName[] = "testdevice"; |
| 66 | const char DeviceTest::kDeviceAddress[] = "address"; |
| 67 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 68 | TEST_F(DeviceTest, Contains) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty)); |
| 70 | EXPECT_FALSE(device_->store()->Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 73 | TEST_F(DeviceTest, GetProperties) { |
| 74 | map<string, ::DBus::Variant> props; |
| 75 | Error error(Error::kInvalidProperty, ""); |
| 76 | { |
| 77 | ::DBus::Error dbus_error; |
| 78 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 79 | device_->store()->SetBoolProperty(flimflam::kPoweredProperty, |
| 80 | expected, |
| 81 | &error); |
| 82 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 83 | ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end()); |
| 84 | EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(), |
| 85 | expected); |
| 86 | } |
| 87 | { |
| 88 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 89 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 90 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 91 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 92 | string(kDeviceName)); |
| 93 | } |
| 94 | } |
| 95 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | TEST_F(DeviceTest, Dispatch) { |
| 97 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 98 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 99 | flimflam::kPoweredProperty, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 100 | PropertyStoreTest::kBoolV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 101 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 102 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 103 | // 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] | 104 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 105 | flimflam::kAddressProperty, |
| 106 | PropertyStoreTest::kStringV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 107 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 108 | EXPECT_EQ(invalid_args_, error.name()); |
| 109 | } |
| 110 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 111 | TEST_F(DeviceTest, TechnologyIs) { |
| 112 | EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet)); |
| 113 | } |
| 114 | |
| 115 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 116 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 117 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 118 | device_->DestroyIPConfig(); |
| 119 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 120 | } |
| 121 | |
| 122 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 123 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 124 | device_->DestroyIPConfig(); |
| 125 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(DeviceTest, AcquireDHCPConfig) { |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 129 | device_->ipconfig_ = new IPConfig(&control_interface_, "randomname"); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 130 | EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _)) |
| 131 | .WillOnce(Return(false)); |
| 132 | EXPECT_FALSE(device_->AcquireDHCPConfig()); |
| 133 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 134 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
| 135 | EXPECT_TRUE(device_->ipconfig_->update_callback_.get()); |
| 136 | } |
| 137 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 138 | TEST_F(DeviceTest, Load) { |
| 139 | NiceMock<MockStore> storage; |
| 140 | const string id = device_->GetStorageIdentifier(); |
| 141 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 142 | EXPECT_CALL(storage, GetBool(id, _, _)) |
| 143 | .Times(AtLeast(1)) |
| 144 | .WillRepeatedly(Return(true)); |
| 145 | EXPECT_TRUE(device_->Load(&storage)); |
| 146 | } |
| 147 | |
| 148 | TEST_F(DeviceTest, Save) { |
| 149 | NiceMock<MockStore> storage; |
| 150 | const string id = device_->GetStorageIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 151 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 152 | .Times(AtLeast(1)) |
| 153 | .WillRepeatedly(Return(true)); |
| 154 | EXPECT_CALL(storage, SetBool(id, _, _)) |
| 155 | .Times(AtLeast(1)) |
| 156 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 157 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(&control_interface_, |
| 158 | kDeviceName); |
| 159 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 160 | .WillOnce(Return(true)); |
| 161 | device_->ipconfig_ = ipconfig; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 162 | EXPECT_TRUE(device_->Save(&storage)); |
| 163 | } |
| 164 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame^] | 165 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 166 | string to_process("/device/stuff/0"); |
| 167 | ControlInterface::RpcIdToStorageId(&to_process); |
| 168 | EXPECT_TRUE(isalpha(to_process[0])); |
| 169 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 170 | } |
| 171 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 172 | } // namespace shill |