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" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 26 | #include "shill/mock_service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 27 | #include "shill/mock_store.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 28 | #include "shill/property_store_unittest.h" |
| 29 | #include "shill/shill_event.h" |
| 30 | |
| 31 | using std::map; |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | using ::testing::_; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 35 | using ::testing::AtLeast; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | using ::testing::NiceMock; |
| 37 | using ::testing::Return; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 38 | using ::testing::StrictMock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 40 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 41 | |
| 42 | namespace shill { |
| 43 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 44 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 45 | public: |
| 46 | DeviceTest() |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 47 | : device_(new Device(&control_interface_, |
| 48 | NULL, |
| 49 | NULL, |
| 50 | kDeviceName, |
| 51 | kDeviceAddress, |
| 52 | 0)) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 53 | DHCPProvider::GetInstance()->glib_ = &glib_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 54 | DHCPProvider::GetInstance()->control_interface_ = &control_interface_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 55 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 56 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 57 | |
| 58 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 59 | static const char kDeviceName[]; |
| 60 | static const char kDeviceAddress[]; |
| 61 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 62 | MockGLib glib_; |
| 63 | MockControl control_interface_; |
| 64 | DeviceRefPtr device_; |
| 65 | }; |
| 66 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 67 | const char DeviceTest::kDeviceName[] = "testdevice"; |
| 68 | const char DeviceTest::kDeviceAddress[] = "address"; |
| 69 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 70 | TEST_F(DeviceTest, Contains) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 71 | EXPECT_TRUE(device_->store()->Contains(flimflam::kNameProperty)); |
| 72 | EXPECT_FALSE(device_->store()->Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 75 | TEST_F(DeviceTest, GetProperties) { |
| 76 | map<string, ::DBus::Variant> props; |
| 77 | Error error(Error::kInvalidProperty, ""); |
| 78 | { |
| 79 | ::DBus::Error dbus_error; |
| 80 | bool expected = true; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 81 | device_->store()->SetBoolProperty(flimflam::kPoweredProperty, |
| 82 | expected, |
| 83 | &error); |
| 84 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 85 | ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end()); |
| 86 | EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(), |
| 87 | expected); |
| 88 | } |
| 89 | { |
| 90 | ::DBus::Error dbus_error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 91 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 92 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 93 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 94 | string(kDeviceName)); |
| 95 | } |
| 96 | } |
| 97 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 98 | TEST_F(DeviceTest, Dispatch) { |
| 99 | ::DBus::Error error; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 100 | EXPECT_TRUE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 101 | flimflam::kPoweredProperty, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 102 | PropertyStoreTest::kBoolV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 103 | &error)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 104 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 105 | // 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] | 106 | EXPECT_FALSE(DBusAdaptor::DispatchOnType(device_->store(), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 107 | flimflam::kAddressProperty, |
| 108 | PropertyStoreTest::kStringV, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 109 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame^] | 110 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 113 | TEST_F(DeviceTest, TechnologyIs) { |
| 114 | EXPECT_FALSE(device_->TechnologyIs(Device::kEthernet)); |
| 115 | } |
| 116 | |
| 117 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 118 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 119 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 120 | device_->DestroyIPConfig(); |
| 121 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 122 | } |
| 123 | |
| 124 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 125 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 126 | device_->DestroyIPConfig(); |
| 127 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(DeviceTest, AcquireDHCPConfig) { |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 131 | device_->ipconfig_ = new IPConfig(&control_interface_, "randomname"); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 132 | EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _)) |
| 133 | .WillOnce(Return(false)); |
| 134 | EXPECT_FALSE(device_->AcquireDHCPConfig()); |
| 135 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 136 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
| 137 | EXPECT_TRUE(device_->ipconfig_->update_callback_.get()); |
| 138 | } |
| 139 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 140 | TEST_F(DeviceTest, Load) { |
| 141 | NiceMock<MockStore> storage; |
| 142 | const string id = device_->GetStorageIdentifier(); |
| 143 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 144 | EXPECT_CALL(storage, GetBool(id, _, _)) |
| 145 | .Times(AtLeast(1)) |
| 146 | .WillRepeatedly(Return(true)); |
| 147 | EXPECT_TRUE(device_->Load(&storage)); |
| 148 | } |
| 149 | |
| 150 | TEST_F(DeviceTest, Save) { |
| 151 | NiceMock<MockStore> storage; |
| 152 | const string id = device_->GetStorageIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 153 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 154 | .Times(AtLeast(1)) |
| 155 | .WillRepeatedly(Return(true)); |
| 156 | EXPECT_CALL(storage, SetBool(id, _, _)) |
| 157 | .Times(AtLeast(1)) |
| 158 | .WillRepeatedly(Return(true)); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 159 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(&control_interface_, |
| 160 | kDeviceName); |
| 161 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 162 | .WillOnce(Return(true)); |
| 163 | device_->ipconfig_ = ipconfig; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(device_->Save(&storage)); |
| 165 | } |
| 166 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 167 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 168 | string to_process("/device/stuff/0"); |
| 169 | ControlInterface::RpcIdToStorageId(&to_process); |
| 170 | EXPECT_TRUE(isalpha(to_process[0])); |
| 171 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 172 | } |
| 173 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 174 | TEST_F(DeviceTest, SelectedService) { |
| 175 | EXPECT_FALSE(device_->selected_service_.get()); |
| 176 | device_->SetServiceState(Service::kStateAssociating); |
| 177 | scoped_refptr<MockService> service( |
| 178 | new StrictMock<MockService>(&control_interface_, |
| 179 | &dispatcher_, |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame^] | 180 | manager())); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 181 | device_->SelectService(service); |
| 182 | EXPECT_TRUE(device_->selected_service_.get() == service.get()); |
| 183 | |
| 184 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
| 185 | device_->SetServiceState(Service::kStateConfiguring); |
| 186 | EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange)); |
| 187 | device_->SetServiceFailure(Service::kFailureOutOfRange); |
| 188 | |
| 189 | // Service should be returned to "Idle" state |
| 190 | EXPECT_CALL(*service.get(), state()) |
| 191 | .WillOnce(Return(Service::kStateUnknown)); |
| 192 | EXPECT_CALL(*service.get(), SetState(Service::kStateIdle)); |
| 193 | device_->SelectService(NULL); |
| 194 | |
| 195 | // A service in the "Failure" state should not be reset to "Idle" |
| 196 | device_->SelectService(service); |
| 197 | EXPECT_CALL(*service.get(), state()) |
| 198 | .WillOnce(Return(Service::kStateFailure)); |
| 199 | device_->SelectService(NULL); |
| 200 | } |
| 201 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 202 | } // namespace shill |