Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 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> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
| 9 | #include <linux/if.h> // Needs typedefs from sys/socket.h. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 16 | #include <dbus-c++/dbus.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | |
| 20 | #include "shill/dbus_adaptor.h" |
| 21 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 22 | #include "shill/event_dispatcher.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 23 | #include "shill/mock_adaptors.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | #include "shill/mock_control.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 25 | #include "shill/mock_connection.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 26 | #include "shill/mock_device.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 27 | #include "shill/mock_device_info.h" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 28 | #include "shill/mock_dhcp_config.h" |
| 29 | #include "shill/mock_dhcp_provider.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 30 | #include "shill/mock_glib.h" |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 31 | #include "shill/mock_ipconfig.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 32 | #include "shill/mock_link_monitor.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 33 | #include "shill/mock_manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 34 | #include "shill/mock_metrics.h" |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 35 | #include "shill/mock_portal_detector.h" |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 36 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 37 | #include "shill/mock_service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 38 | #include "shill/mock_store.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 39 | #include "shill/portal_detector.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 40 | #include "shill/property_store_unittest.h" |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 41 | #include "shill/static_ip_parameters.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 42 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 43 | |
| 44 | using std::map; |
| 45 | using std::string; |
| 46 | using std::vector; |
| 47 | using ::testing::_; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 48 | using ::testing::AnyNumber; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 49 | using ::testing::AtLeast; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 50 | using ::testing::Invoke; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 51 | using ::testing::Mock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | using ::testing::NiceMock; |
| 53 | using ::testing::Return; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 54 | using ::testing::ReturnRef; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 55 | using ::testing::StrictMock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 56 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 57 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 58 | |
| 59 | namespace shill { |
| 60 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 61 | class TestDevice : public Device { |
| 62 | public: |
| 63 | TestDevice(ControlInterface *control_interface, |
| 64 | EventDispatcher *dispatcher, |
| 65 | Metrics *metrics, |
| 66 | Manager *manager, |
| 67 | const std::string &link_name, |
| 68 | const std::string &address, |
| 69 | int interface_index, |
| 70 | Technology::Identifier technology) |
| 71 | : Device(control_interface, dispatcher, metrics, manager, link_name, |
| 72 | address, interface_index, technology) {} |
| 73 | ~TestDevice() {} |
| 74 | virtual void Start(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 75 | const EnabledStateChangedCallback &callback) { |
| 76 | DCHECK(error); |
| 77 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 78 | virtual void Stop(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 79 | const EnabledStateChangedCallback &callback) { |
| 80 | DCHECK(error); |
| 81 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 84 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 85 | public: |
| 86 | DeviceTest() |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 87 | : device_(new TestDevice(control_interface(), |
| 88 | dispatcher(), |
| 89 | NULL, |
| 90 | manager(), |
| 91 | kDeviceName, |
| 92 | kDeviceAddress, |
| 93 | kDeviceInterfaceIndex, |
| 94 | Technology::kUnknown)), |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 95 | device_info_(control_interface(), NULL, NULL, NULL) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 96 | DHCPProvider::GetInstance()->glib_ = glib(); |
| 97 | DHCPProvider::GetInstance()->control_interface_ = control_interface(); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 98 | DHCPProvider::GetInstance()->dispatcher_ = dispatcher(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 99 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 100 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 101 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 102 | virtual void SetUp() { |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 103 | device_->metrics_ = &metrics_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 104 | device_->rtnl_handler_ = &rtnl_handler_; |
| 105 | } |
| 106 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 107 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 108 | static const char kDeviceName[]; |
| 109 | static const char kDeviceAddress[]; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 110 | static const int kDeviceInterfaceIndex; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 111 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 112 | void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
| 113 | device_->OnIPConfigUpdated(ipconfig, success); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void SelectService(const ServiceRefPtr service) { |
| 117 | device_->SelectService(service); |
| 118 | } |
| 119 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 120 | void SetConnection(ConnectionRefPtr connection) { |
| 121 | device_->connection_ = connection; |
| 122 | } |
| 123 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 124 | void SetLinkMonitor(LinkMonitor *link_monitor) { |
| 125 | device_->set_link_monitor(link_monitor); // Passes ownership. |
| 126 | } |
| 127 | |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 128 | bool HasLinkMonitor() { |
| 129 | return device_->link_monitor(); |
| 130 | } |
| 131 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 132 | bool StartLinkMonitor() { |
| 133 | return device_->StartLinkMonitor(); |
| 134 | } |
| 135 | |
| 136 | void StopLinkMonitor() { |
| 137 | device_->StopLinkMonitor(); |
| 138 | } |
| 139 | |
| 140 | uint64 GetLinkMonitorResponseTime(Error *error) { |
| 141 | return device_->GetLinkMonitorResponseTime(error); |
| 142 | } |
| 143 | |
| 144 | void SetManager(Manager *manager) { |
| 145 | device_->manager_ = manager; |
| 146 | } |
| 147 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 148 | MockControl control_interface_; |
| 149 | DeviceRefPtr device_; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 150 | MockDeviceInfo device_info_; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 151 | MockMetrics metrics_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 152 | StrictMock<MockRTNLHandler> rtnl_handler_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 155 | const char DeviceTest::kDeviceName[] = "testdevice"; |
| 156 | const char DeviceTest::kDeviceAddress[] = "address"; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 157 | const int DeviceTest::kDeviceInterfaceIndex = 0; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 158 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 159 | TEST_F(DeviceTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 160 | EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty)); |
| 161 | EXPECT_FALSE(device_->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 164 | TEST_F(DeviceTest, GetProperties) { |
| 165 | map<string, ::DBus::Variant> props; |
| 166 | Error error(Error::kInvalidProperty, ""); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 167 | ::DBus::Error dbus_error; |
| 168 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
| 169 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 170 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 171 | string(kDeviceName)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 174 | // Note: there are currently no writeable Device properties that |
| 175 | // aren't registered in a subclass. |
| 176 | TEST_F(DeviceTest, SetReadOnlyProperty) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 177 | ::DBus::Error error; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 178 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 179 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 180 | flimflam::kAddressProperty, |
| 181 | PropertyStoreTest::kStringV, |
| 182 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 183 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 186 | TEST_F(DeviceTest, ClearReadOnlyProperty) { |
| 187 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 188 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 189 | flimflam::kAddressProperty, |
| 190 | PropertyStoreTest::kStringV, |
| 191 | &error)); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | TEST_F(DeviceTest, ClearReadOnlyDerivedProperty) { |
| 195 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 196 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 197 | flimflam::kIPConfigsProperty, |
| 198 | PropertyStoreTest::kStringsV, |
| 199 | &error)); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 202 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 203 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 204 | device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 205 | device_->DestroyIPConfig(); |
| 206 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 207 | } |
| 208 | |
| 209 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 210 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 211 | device_->DestroyIPConfig(); |
| 212 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 213 | } |
| 214 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 215 | TEST_F(DeviceTest, AcquireIPConfig) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 216 | device_->ipconfig_ = new IPConfig(control_interface(), "randomname"); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 217 | scoped_ptr<MockDHCPProvider> dhcp_provider(new MockDHCPProvider()); |
| 218 | device_->dhcp_provider_ = dhcp_provider.get(); |
| 219 | scoped_refptr<MockDHCPConfig> dhcp_config(new MockDHCPConfig( |
| 220 | control_interface(), |
| 221 | kDeviceName)); |
| 222 | EXPECT_CALL(*dhcp_provider, CreateConfig(_, _, _, _)) |
| 223 | .WillOnce(Return(dhcp_config)); |
| 224 | EXPECT_CALL(*dhcp_config, RequestIP()) |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 225 | .WillOnce(Return(false)); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 226 | EXPECT_FALSE(device_->AcquireIPConfig()); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 227 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 228 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 229 | EXPECT_FALSE(device_->ipconfig_->update_callback_.is_null()); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 230 | device_->dhcp_provider_ = NULL; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 233 | TEST_F(DeviceTest, Load) { |
| 234 | NiceMock<MockStore> storage; |
| 235 | const string id = device_->GetStorageIdentifier(); |
| 236 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 237 | EXPECT_CALL(storage, GetBool(id, Device::kStoragePowered, _)) |
| 238 | .WillOnce(Return(true)); |
| 239 | EXPECT_CALL(storage, GetUint64(id, Device::kStorageReceiveByteCount, _)) |
| 240 | .WillOnce(Return(true)); |
| 241 | EXPECT_CALL(storage, GetUint64(id, Device::kStorageTransmitByteCount, _)) |
| 242 | .WillOnce(Return(true)); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(device_->Load(&storage)); |
| 244 | } |
| 245 | |
| 246 | TEST_F(DeviceTest, Save) { |
| 247 | NiceMock<MockStore> storage; |
| 248 | const string id = device_->GetStorageIdentifier(); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 249 | EXPECT_CALL(storage, SetString(id, Device::kStorageIPConfigs, _)) |
| 250 | .WillOnce(Return(true)); |
| 251 | EXPECT_CALL(storage, SetBool(id, Device::kStoragePowered, _)) |
| 252 | .WillOnce(Return(true)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 253 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 254 | kDeviceName); |
| 255 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 256 | .WillOnce(Return(true)); |
| 257 | device_->ipconfig_ = ipconfig; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 258 | EXPECT_CALL(storage, SetUint64(id, Device::kStorageReceiveByteCount, _)) |
| 259 | .WillOnce(Return(true)); |
| 260 | EXPECT_CALL(storage, SetUint64(id, Device::kStorageTransmitByteCount, _)) |
| 261 | .Times(AtLeast(true)); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 262 | EXPECT_TRUE(device_->Save(&storage)); |
| 263 | } |
| 264 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 265 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 266 | string to_process("/device/stuff/0"); |
| 267 | ControlInterface::RpcIdToStorageId(&to_process); |
| 268 | EXPECT_TRUE(isalpha(to_process[0])); |
| 269 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 270 | } |
| 271 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 272 | MATCHER(IsNullRefPtr, "") { |
| 273 | return !arg; |
| 274 | } |
| 275 | |
| 276 | MATCHER(NotNullRefPtr, "") { |
| 277 | return arg; |
| 278 | } |
| 279 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 280 | TEST_F(DeviceTest, SelectedService) { |
| 281 | EXPECT_FALSE(device_->selected_service_.get()); |
| 282 | device_->SetServiceState(Service::kStateAssociating); |
| 283 | scoped_refptr<MockService> service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 284 | new StrictMock<MockService>(control_interface(), |
| 285 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 286 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 287 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 288 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 289 | EXPECT_TRUE(device_->selected_service_.get() == service.get()); |
| 290 | |
| 291 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
| 292 | device_->SetServiceState(Service::kStateConfiguring); |
| 293 | EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange)); |
| 294 | device_->SetServiceFailure(Service::kFailureOutOfRange); |
| 295 | |
| 296 | // Service should be returned to "Idle" state |
| 297 | EXPECT_CALL(*service.get(), state()) |
| 298 | .WillOnce(Return(Service::kStateUnknown)); |
| 299 | EXPECT_CALL(*service.get(), SetState(Service::kStateIdle)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 300 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 301 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 302 | |
| 303 | // A service in the "Failure" state should not be reset to "Idle" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 304 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 305 | EXPECT_CALL(*service.get(), state()) |
| 306 | .WillOnce(Return(Service::kStateFailure)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 307 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 308 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 311 | TEST_F(DeviceTest, IPConfigUpdatedFailure) { |
| 312 | scoped_refptr<MockService> service( |
| 313 | new StrictMock<MockService>(control_interface(), |
| 314 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 315 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 316 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 317 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 318 | EXPECT_CALL(*service.get(), SetState(Service::kStateDisconnected)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 319 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 320 | OnIPConfigUpdated(NULL, false); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 321 | } |
| 322 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 323 | TEST_F(DeviceTest, IPConfigUpdatedFailureWithStatic) { |
| 324 | scoped_refptr<MockService> service( |
| 325 | new StrictMock<MockService>(control_interface(), |
| 326 | dispatcher(), |
| 327 | metrics(), |
| 328 | manager())); |
| 329 | SelectService(service); |
| 330 | service->static_ip_parameters_.args_.SetString( |
| 331 | flimflam::kAddressProperty, "1.1.1.1"); |
| 332 | service->static_ip_parameters_.args_.SetInt(flimflam::kPrefixlenProperty, 16); |
| 333 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 334 | EXPECT_CALL(*service.get(), SetConnection(_)).Times(0); |
| 335 | OnIPConfigUpdated(NULL, false); |
| 336 | } |
| 337 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 338 | TEST_F(DeviceTest, IPConfigUpdatedSuccess) { |
| 339 | scoped_refptr<MockService> service( |
| 340 | new StrictMock<MockService>(control_interface(), |
| 341 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 342 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 343 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 344 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 345 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 346 | kDeviceName); |
| 347 | EXPECT_CALL(*service.get(), SetState(Service::kStateConnected)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 348 | EXPECT_CALL(*service.get(), IsConnected()) |
| 349 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 350 | EXPECT_CALL(*service.get(), IsPortalDetectionDisabled()) |
| 351 | .WillRepeatedly(Return(true)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 352 | EXPECT_CALL(*service.get(), SetState(Service::kStateOnline)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 353 | EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 354 | OnIPConfigUpdated(ipconfig.get(), true); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Thieu Le | d176092 | 2012-09-11 14:15:35 -0700 | [diff] [blame] | 357 | TEST_F(DeviceTest, IPConfigUpdatedSuccessNoSelectedService) { |
| 358 | // Make sure shill doesn't crash if a service is disabled immediately |
| 359 | // after receiving its IP config (selected_service_ is NULL in this case). |
| 360 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 361 | kDeviceName); |
| 362 | SelectService(NULL); |
| 363 | OnIPConfigUpdated(ipconfig.get(), true); |
| 364 | } |
| 365 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 366 | TEST_F(DeviceTest, SetEnabledPersistent) { |
| 367 | EXPECT_FALSE(device_->enabled_); |
| 368 | EXPECT_FALSE(device_->enabled_pending_); |
| 369 | device_->enabled_persistent_ = false; |
| 370 | StrictMock<MockManager> manager(control_interface(), |
| 371 | dispatcher(), |
| 372 | metrics(), |
| 373 | glib()); |
| 374 | EXPECT_CALL(manager, UpdateDevice(_)); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 375 | SetManager(&manager); |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 376 | Error error; |
| 377 | device_->SetEnabledPersistent(true, &error, ResultCallback()); |
| 378 | EXPECT_TRUE(device_->enabled_persistent_); |
| 379 | EXPECT_TRUE(device_->enabled_pending_); |
| 380 | } |
| 381 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 382 | TEST_F(DeviceTest, Start) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 383 | EXPECT_FALSE(device_->running_); |
| 384 | EXPECT_FALSE(device_->enabled_); |
| 385 | EXPECT_FALSE(device_->enabled_pending_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 386 | device_->SetEnabled(true); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 387 | EXPECT_TRUE(device_->running_); |
| 388 | EXPECT_TRUE(device_->enabled_pending_); |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 389 | device_->OnEnabledStateChanged(ResultCallback(), |
| 390 | Error(Error::kOperationFailed)); |
| 391 | EXPECT_FALSE(device_->enabled_pending_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 392 | } |
| 393 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 394 | TEST_F(DeviceTest, Stop) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 395 | device_->enabled_ = true; |
| 396 | device_->enabled_pending_ = true; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 397 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
| 398 | scoped_refptr<MockService> service( |
| 399 | new NiceMock<MockService>(&control_interface_, |
| 400 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 401 | metrics(), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 402 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 403 | SelectService(service); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 404 | |
| 405 | EXPECT_CALL(*service.get(), state()). |
| 406 | WillRepeatedly(Return(Service::kStateConnected)); |
| 407 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 408 | UpdateEnabled()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 409 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 410 | EmitBoolChanged(flimflam::kPoweredProperty, false)); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 411 | EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 412 | device_->SetEnabled(false); |
| 413 | device_->OnEnabledStateChanged(ResultCallback(), Error()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 414 | |
| 415 | EXPECT_FALSE(device_->ipconfig_.get()); |
| 416 | EXPECT_FALSE(device_->selected_service_.get()); |
| 417 | } |
| 418 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 419 | TEST_F(DeviceTest, ResumeWithIPConfig) { |
| 420 | scoped_refptr<MockIPConfig> ipconfig = |
| 421 | new MockIPConfig(control_interface(), kDeviceName); |
| 422 | device_->set_ipconfig(ipconfig); |
| 423 | EXPECT_CALL(*ipconfig, RenewIP()); |
| 424 | device_->OnAfterResume(); |
| 425 | } |
| 426 | |
| 427 | TEST_F(DeviceTest, ResumeWithoutIPConfig) { |
| 428 | // Just test that we don't crash in this case. |
| 429 | ASSERT_EQ(NULL, device_->ipconfig().get()); |
| 430 | device_->OnAfterResume(); |
| 431 | } |
| 432 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 433 | TEST_F(DeviceTest, LinkMonitor) { |
| 434 | scoped_refptr<MockConnection> connection( |
| 435 | new StrictMock<MockConnection>(&device_info_)); |
| 436 | MockManager manager(control_interface(), |
| 437 | dispatcher(), |
| 438 | metrics(), |
| 439 | glib()); |
| 440 | scoped_refptr<MockService> service( |
| 441 | new StrictMock<MockService>(control_interface(), |
| 442 | dispatcher(), |
| 443 | metrics(), |
| 444 | &manager)); |
| 445 | SelectService(service); |
| 446 | SetConnection(connection.get()); |
| 447 | MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>(); |
| 448 | SetLinkMonitor(link_monitor); // Passes ownership. |
| 449 | SetManager(&manager); |
| 450 | EXPECT_CALL(*link_monitor, Start()).Times(0); |
| 451 | EXPECT_CALL(manager, IsTechnologyLinkMonitorEnabled(Technology::kUnknown)) |
| 452 | .WillOnce(Return(false)) |
| 453 | .WillRepeatedly(Return(true)); |
| 454 | EXPECT_FALSE(StartLinkMonitor()); |
| 455 | |
| 456 | EXPECT_CALL(*link_monitor, Start()) |
| 457 | .WillOnce(Return(false)) |
| 458 | .WillOnce(Return(true)); |
| 459 | EXPECT_FALSE(StartLinkMonitor()); |
| 460 | EXPECT_TRUE(StartLinkMonitor()); |
| 461 | |
| 462 | unsigned int kResponseTime = 123; |
| 463 | EXPECT_CALL(*link_monitor, GetResponseTimeMilliseconds()) |
| 464 | .WillOnce(Return(kResponseTime)); |
| 465 | { |
| 466 | Error error; |
| 467 | EXPECT_EQ(kResponseTime, GetLinkMonitorResponseTime(&error)); |
| 468 | EXPECT_TRUE(error.IsSuccess()); |
| 469 | } |
| 470 | StopLinkMonitor(); |
| 471 | { |
| 472 | Error error; |
| 473 | EXPECT_EQ(0, GetLinkMonitorResponseTime(&error)); |
| 474 | EXPECT_FALSE(error.IsSuccess()); |
| 475 | } |
| 476 | } |
| 477 | |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 478 | TEST_F(DeviceTest, LinkMonitorCancelledOnSelectService) { |
| 479 | scoped_refptr<MockConnection> connection( |
| 480 | new StrictMock<MockConnection>(&device_info_)); |
| 481 | MockManager manager(control_interface(), |
| 482 | dispatcher(), |
| 483 | metrics(), |
| 484 | glib()); |
| 485 | scoped_refptr<MockService> service( |
| 486 | new StrictMock<MockService>(control_interface(), |
| 487 | dispatcher(), |
| 488 | metrics(), |
| 489 | &manager)); |
| 490 | SelectService(service); |
| 491 | SetConnection(connection.get()); |
| 492 | MockLinkMonitor *link_monitor = new StrictMock<MockLinkMonitor>(); |
| 493 | SetLinkMonitor(link_monitor); // Passes ownership. |
| 494 | SetManager(&manager); |
| 495 | EXPECT_CALL(*service.get(), state()) |
| 496 | .WillOnce(Return(Service::kStateIdle)); |
| 497 | EXPECT_CALL(*service.get(), SetState(_)); |
| 498 | EXPECT_CALL(*service.get(), SetConnection(_)); |
| 499 | EXPECT_TRUE(HasLinkMonitor()); |
| 500 | SelectService(NULL); |
| 501 | EXPECT_FALSE(HasLinkMonitor()); |
| 502 | } |
| 503 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 504 | class DevicePortalDetectionTest : public DeviceTest { |
| 505 | public: |
| 506 | DevicePortalDetectionTest() |
| 507 | : connection_(new StrictMock<MockConnection>(&device_info_)), |
| 508 | manager_(control_interface(), |
| 509 | dispatcher(), |
| 510 | metrics(), |
| 511 | glib()), |
| 512 | service_(new StrictMock<MockService>(control_interface(), |
| 513 | dispatcher(), |
| 514 | metrics(), |
| 515 | &manager_)), |
| 516 | portal_detector_(new StrictMock<MockPortalDetector>(connection_)) {} |
| 517 | virtual ~DevicePortalDetectionTest() {} |
| 518 | virtual void SetUp() { |
| 519 | DeviceTest::SetUp(); |
| 520 | SelectService(service_); |
| 521 | SetConnection(connection_.get()); |
| 522 | device_->portal_detector_.reset(portal_detector_); // Passes ownership. |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 523 | SetManager(&manager_); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | protected: |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 527 | static const int kPortalAttempts; |
| 528 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 529 | bool StartPortalDetection() { return device_->StartPortalDetection(); } |
| 530 | void StopPortalDetection() { device_->StopPortalDetection(); } |
| 531 | |
| 532 | void PortalDetectorCallback(const PortalDetector::Result &result) { |
| 533 | device_->PortalDetectorCallback(result); |
| 534 | } |
| 535 | bool RequestPortalDetection() { |
| 536 | return device_->RequestPortalDetection(); |
| 537 | } |
| 538 | void SetServiceConnectedState(Service::ConnectState state) { |
| 539 | device_->SetServiceConnectedState(state); |
| 540 | } |
| 541 | void ExpectPortalDetectorReset() { |
| 542 | EXPECT_FALSE(device_->portal_detector_.get()); |
| 543 | } |
| 544 | void ExpectPortalDetectorSet() { |
| 545 | EXPECT_TRUE(device_->portal_detector_.get()); |
| 546 | } |
| 547 | void ExpectPortalDetectorIsMock() { |
| 548 | EXPECT_EQ(portal_detector_, device_->portal_detector_.get()); |
| 549 | } |
| 550 | scoped_refptr<MockConnection> connection_; |
| 551 | StrictMock<MockManager> manager_; |
| 552 | scoped_refptr<MockService> service_; |
| 553 | |
| 554 | // Used only for EXPECT_CALL(). Object is owned by device. |
| 555 | MockPortalDetector *portal_detector_; |
| 556 | }; |
| 557 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 558 | const int DevicePortalDetectionTest::kPortalAttempts = 2; |
| 559 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 560 | TEST_F(DevicePortalDetectionTest, ServicePortalDetectionDisabled) { |
| 561 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 562 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 563 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 564 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 565 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 566 | EXPECT_FALSE(StartPortalDetection()); |
| 567 | } |
| 568 | |
| 569 | TEST_F(DevicePortalDetectionTest, TechnologyPortalDetectionDisabled) { |
| 570 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 571 | .WillOnce(Return(false)); |
| 572 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 573 | .WillRepeatedly(Return(true)); |
| 574 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 575 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 576 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 577 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 578 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 579 | EXPECT_FALSE(StartPortalDetection()); |
| 580 | } |
| 581 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 582 | TEST_F(DevicePortalDetectionTest, PortalDetectionProxyConfig) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 583 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 584 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 585 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 586 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 587 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 588 | .WillOnce(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 589 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 590 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 591 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 592 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 593 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 594 | EXPECT_FALSE(StartPortalDetection()); |
| 595 | } |
| 596 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 597 | TEST_F(DevicePortalDetectionTest, PortalDetectionBadUrl) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 598 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 599 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 600 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 601 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 602 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 603 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 604 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 605 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 606 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 607 | .WillOnce(Return(true)); |
| 608 | const string portal_url; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 609 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 610 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 611 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 612 | EXPECT_FALSE(StartPortalDetection()); |
| 613 | } |
| 614 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 615 | TEST_F(DevicePortalDetectionTest, PortalDetectionStart) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 616 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 617 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 618 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 619 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 620 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 621 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 622 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 623 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 624 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 625 | .WillOnce(Return(true)); |
| 626 | const string portal_url(PortalDetector::kDefaultURL); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 627 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 628 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 629 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 630 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 631 | const string kInterfaceName("int0"); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 632 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 633 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 634 | const vector<string> kDNSServers; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 635 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 636 | .WillRepeatedly(ReturnRef(kDNSServers)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 637 | EXPECT_TRUE(StartPortalDetection()); |
| 638 | |
| 639 | // Drop all references to device_info before it falls out of scope. |
| 640 | SetConnection(NULL); |
| 641 | StopPortalDetection(); |
| 642 | } |
| 643 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 644 | TEST_F(DevicePortalDetectionTest, PortalDetectionNonFinal) { |
| 645 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 646 | .Times(0); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 647 | EXPECT_CALL(*service_.get(), SetState(_)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 648 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 649 | PortalDetectorCallback(PortalDetector::Result( |
| 650 | PortalDetector::kPhaseUnknown, |
| 651 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 652 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 653 | false)); |
| 654 | } |
| 655 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 656 | TEST_F(DevicePortalDetectionTest, PortalDetectionFailure) { |
| 657 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 658 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 659 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 660 | EXPECT_CALL(metrics_, |
| 661 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 662 | Metrics::kPortalResultConnectionFailure, |
| 663 | Metrics::kPortalResultMax)); |
| 664 | EXPECT_CALL(metrics_, |
| 665 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 666 | _, _, _, _)).Times(0); |
| 667 | EXPECT_CALL(metrics_, |
| 668 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 669 | kPortalAttempts, |
| 670 | Metrics::kMetricPortalAttemptsMin, |
| 671 | Metrics::kMetricPortalAttemptsMax, |
| 672 | Metrics::kMetricPortalAttemptsNumBuckets)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 673 | EXPECT_CALL(*connection_.get(), is_default()) |
| 674 | .WillOnce(Return(false)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 675 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 676 | PortalDetector::kPhaseConnection, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 677 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 678 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 679 | true)); |
| 680 | } |
| 681 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 682 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccess) { |
| 683 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 684 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 685 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 686 | EXPECT_CALL(metrics_, |
| 687 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 688 | Metrics::kPortalResultSuccess, |
| 689 | Metrics::kPortalResultMax)); |
| 690 | EXPECT_CALL(metrics_, |
| 691 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 692 | kPortalAttempts, |
| 693 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 694 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 695 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 696 | EXPECT_CALL(metrics_, |
| 697 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 698 | _, _, _, _)).Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 699 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 700 | PortalDetector::kPhaseContent, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 701 | PortalDetector::kStatusSuccess, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 702 | kPortalAttempts, |
| 703 | true)); |
| 704 | } |
| 705 | |
| 706 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccessAfterFailure) { |
| 707 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 708 | .WillRepeatedly(Return(true)); |
| 709 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 710 | EXPECT_CALL(metrics_, |
| 711 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 712 | Metrics::kPortalResultConnectionFailure, |
| 713 | Metrics::kPortalResultMax)); |
| 714 | EXPECT_CALL(metrics_, |
| 715 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 716 | _, _, _, _)).Times(0); |
| 717 | EXPECT_CALL(metrics_, |
| 718 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 719 | kPortalAttempts, |
| 720 | Metrics::kMetricPortalAttemptsMin, |
| 721 | Metrics::kMetricPortalAttemptsMax, |
| 722 | Metrics::kMetricPortalAttemptsNumBuckets)); |
| 723 | EXPECT_CALL(*connection_.get(), is_default()) |
| 724 | .WillOnce(Return(false)); |
| 725 | PortalDetectorCallback(PortalDetector::Result( |
| 726 | PortalDetector::kPhaseConnection, |
| 727 | PortalDetector::kStatusFailure, |
| 728 | kPortalAttempts, |
| 729 | true)); |
| 730 | Mock::VerifyAndClearExpectations(&metrics_); |
| 731 | |
| 732 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 733 | EXPECT_CALL(metrics_, |
| 734 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 735 | Metrics::kPortalResultSuccess, |
| 736 | Metrics::kPortalResultMax)); |
| 737 | EXPECT_CALL(metrics_, |
| 738 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 739 | kPortalAttempts * 2, |
| 740 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 741 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 742 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 743 | EXPECT_CALL(metrics_, |
| 744 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 745 | _, _, _, _)).Times(0); |
| 746 | PortalDetectorCallback(PortalDetector::Result( |
| 747 | PortalDetector::kPhaseContent, |
| 748 | PortalDetector::kStatusSuccess, |
| 749 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 750 | true)); |
| 751 | } |
| 752 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 753 | TEST_F(DevicePortalDetectionTest, RequestPortalDetection) { |
| 754 | EXPECT_CALL(*service_.get(), state()) |
| 755 | .WillOnce(Return(Service::kStateOnline)) |
| 756 | .WillRepeatedly(Return(Service::kStatePortal)); |
| 757 | EXPECT_FALSE(RequestPortalDetection()); |
| 758 | |
| 759 | EXPECT_CALL(*connection_.get(), is_default()) |
| 760 | .WillOnce(Return(false)) |
| 761 | .WillRepeatedly(Return(true)); |
| 762 | EXPECT_FALSE(RequestPortalDetection()); |
| 763 | |
| 764 | EXPECT_CALL(*portal_detector_, IsInProgress()) |
| 765 | .WillOnce(Return(true)); |
| 766 | // Portal detection already running. |
| 767 | EXPECT_TRUE(RequestPortalDetection()); |
| 768 | |
| 769 | // Make sure our running mock portal detector was not replaced. |
| 770 | ExpectPortalDetectorIsMock(); |
| 771 | |
| 772 | // Throw away our pre-fabricated portal detector, and have the device create |
| 773 | // a new one. |
| 774 | StopPortalDetection(); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 775 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 776 | .WillRepeatedly(Return(false)); |
| 777 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 778 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 779 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
| 780 | .WillRepeatedly(Return(true)); |
| 781 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
| 782 | .WillRepeatedly(Return(false)); |
| 783 | const string kPortalCheckURL("http://portal"); |
| 784 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 785 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 786 | const string kInterfaceName("int0"); |
| 787 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 788 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
| 789 | const vector<string> kDNSServers; |
| 790 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 791 | .WillRepeatedly(ReturnRef(kDNSServers)); |
| 792 | EXPECT_TRUE(RequestPortalDetection()); |
| 793 | } |
| 794 | |
| 795 | TEST_F(DevicePortalDetectionTest, NotConnected) { |
| 796 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 797 | .WillOnce(Return(false)); |
| 798 | SetServiceConnectedState(Service::kStatePortal); |
| 799 | // We don't check for the portal detector to be reset here, because |
| 800 | // it would have been reset as a part of disconnection. |
| 801 | } |
| 802 | |
| 803 | TEST_F(DevicePortalDetectionTest, NotPortal) { |
| 804 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 805 | .WillOnce(Return(true)); |
| 806 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 807 | SetServiceConnectedState(Service::kStateOnline); |
| 808 | ExpectPortalDetectorReset(); |
| 809 | } |
| 810 | |
| 811 | TEST_F(DevicePortalDetectionTest, NotDefault) { |
| 812 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 813 | .WillOnce(Return(true)); |
| 814 | EXPECT_CALL(*connection_.get(), is_default()) |
| 815 | .WillOnce(Return(false)); |
| 816 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 817 | SetServiceConnectedState(Service::kStatePortal); |
| 818 | ExpectPortalDetectorReset(); |
| 819 | } |
| 820 | |
| 821 | TEST_F(DevicePortalDetectionTest, PortalIntervalIsZero) { |
| 822 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 823 | .WillOnce(Return(true)); |
| 824 | EXPECT_CALL(*connection_.get(), is_default()) |
| 825 | .WillOnce(Return(true)); |
| 826 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 827 | .WillOnce(Return(0)); |
| 828 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 829 | SetServiceConnectedState(Service::kStatePortal); |
| 830 | ExpectPortalDetectorReset(); |
| 831 | } |
| 832 | |
| 833 | TEST_F(DevicePortalDetectionTest, RestartPortalDetection) { |
| 834 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 835 | .WillOnce(Return(true)); |
| 836 | EXPECT_CALL(*connection_.get(), is_default()) |
| 837 | .WillOnce(Return(true)); |
| 838 | const int kPortalDetectionInterval = 10; |
| 839 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 840 | .Times(AtLeast(1)) |
| 841 | .WillRepeatedly(Return(kPortalDetectionInterval)); |
| 842 | const string kPortalCheckURL("http://portal"); |
| 843 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 844 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 845 | EXPECT_CALL(*portal_detector_, StartAfterDelay(kPortalCheckURL, |
| 846 | kPortalDetectionInterval)) |
| 847 | .WillOnce(Return(true)); |
| 848 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 849 | SetServiceConnectedState(Service::kStatePortal); |
| 850 | ExpectPortalDetectorSet(); |
| 851 | } |
| 852 | |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 853 | TEST_F(DevicePortalDetectionTest, CancelledOnSelectService) { |
| 854 | ExpectPortalDetectorSet(); |
| 855 | EXPECT_CALL(*service_.get(), state()) |
| 856 | .WillOnce(Return(Service::kStateIdle)); |
| 857 | EXPECT_CALL(*service_.get(), SetState(_)); |
| 858 | EXPECT_CALL(*service_.get(), SetConnection(_)); |
| 859 | SelectService(NULL); |
| 860 | ExpectPortalDetectorReset(); |
| 861 | } |
| 862 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 863 | class DeviceByteCountTest : public DeviceTest { |
| 864 | public: |
| 865 | DeviceByteCountTest() |
| 866 | : manager_(control_interface(), |
| 867 | dispatcher(), |
| 868 | metrics(), |
| 869 | glib()), |
| 870 | rx_byte_count_(0), |
| 871 | tx_byte_count_(0), |
| 872 | rx_stored_byte_count_(0), |
| 873 | tx_stored_byte_count_(0) {} |
| 874 | virtual ~DeviceByteCountTest() {} |
| 875 | |
| 876 | virtual void SetUp() { |
| 877 | DeviceTest::SetUp(); |
| 878 | EXPECT_CALL(manager_, device_info()).WillRepeatedly(Return(&device_info_)); |
| 879 | EXPECT_CALL(device_info_, GetByteCounts(kDeviceInterfaceIndex, _, _)) |
| 880 | .WillRepeatedly(Invoke(this, &DeviceByteCountTest::ReturnByteCounts)); |
| 881 | const string id = device_->GetStorageIdentifier(); |
| 882 | EXPECT_CALL(storage_, ContainsGroup(id)).WillRepeatedly(Return(true)); |
| 883 | EXPECT_CALL(storage_, GetUint64(id, Device::kStorageReceiveByteCount, _)) |
| 884 | .WillRepeatedly( |
| 885 | Invoke(this, &DeviceByteCountTest::GetStoredReceiveCount)); |
| 886 | EXPECT_CALL(storage_, GetUint64(id, Device::kStorageTransmitByteCount, _)) |
| 887 | .WillRepeatedly( |
| 888 | Invoke(this, &DeviceByteCountTest::GetStoredTransmitCount)); |
| 889 | } |
| 890 | |
| 891 | bool ReturnByteCounts(int interface_index, uint64 *rx, uint64 *tx) { |
| 892 | *rx = rx_byte_count_; |
| 893 | *tx = tx_byte_count_; |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | bool ExpectByteCounts(DeviceRefPtr device, |
| 898 | int64 expected_rx, int64 expected_tx) { |
| 899 | int64 actual_rx = device->GetReceiveByteCount(NULL); |
| 900 | int64 actual_tx = device->GetTransmitByteCount(NULL); |
| 901 | EXPECT_EQ(expected_rx, actual_rx); |
| 902 | EXPECT_EQ(expected_tx, actual_tx); |
| 903 | return expected_rx == actual_rx && expected_tx == actual_tx; |
| 904 | } |
| 905 | |
| 906 | void ExpectSavedCounts(DeviceRefPtr device, |
| 907 | int64 expected_rx, int64 expected_tx) { |
| 908 | EXPECT_CALL(storage_, |
| 909 | SetUint64(_, Device::kStorageReceiveByteCount, expected_rx)) |
| 910 | .WillOnce(Return(true)); |
| 911 | EXPECT_CALL(storage_, |
| 912 | SetUint64(_, Device::kStorageTransmitByteCount, expected_tx)) |
| 913 | .WillOnce(Return(true)); |
| 914 | EXPECT_TRUE(device->Save(&storage_)); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | bool GetStoredReceiveCount(const string &group, const string &key, |
| 919 | uint64 *value) { |
| 920 | if (!rx_stored_byte_count_) { |
| 921 | return false; |
| 922 | } |
| 923 | *value = rx_stored_byte_count_; |
| 924 | return true; |
| 925 | } |
| 926 | |
| 927 | bool GetStoredTransmitCount(const string &group, const string &key, |
| 928 | uint64 *value) { |
| 929 | if (!tx_stored_byte_count_) { |
| 930 | return false; |
| 931 | } |
| 932 | *value = tx_stored_byte_count_; |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | protected: |
| 937 | NiceMock<MockManager> manager_; |
| 938 | NiceMock<MockStore> storage_; |
| 939 | uint64 rx_byte_count_; |
| 940 | uint64 tx_byte_count_; |
| 941 | uint64 rx_stored_byte_count_; |
| 942 | uint64 tx_stored_byte_count_; |
| 943 | }; |
| 944 | |
| 945 | |
| 946 | TEST_F(DeviceByteCountTest, GetByteCounts) { |
| 947 | // On Device initialization, byte counts should be zero, independent of |
| 948 | // the byte counts reported by the interface. |
| 949 | rx_byte_count_ = 123; |
| 950 | tx_byte_count_ = 456; |
| 951 | DeviceRefPtr device(new TestDevice(control_interface(), |
| 952 | dispatcher(), |
| 953 | NULL, |
| 954 | &manager_, |
| 955 | kDeviceName, |
| 956 | kDeviceAddress, |
| 957 | kDeviceInterfaceIndex, |
| 958 | Technology::kUnknown)); |
| 959 | EXPECT_TRUE(ExpectByteCounts(device, 0, 0)); |
| 960 | |
| 961 | // Device should report any increase in the byte counts reported in the |
| 962 | // interface. |
| 963 | const int64 delta_rx_count = 789; |
| 964 | const int64 delta_tx_count = 12; |
| 965 | rx_byte_count_ += delta_rx_count; |
| 966 | tx_byte_count_ += delta_tx_count; |
| 967 | EXPECT_TRUE(ExpectByteCounts(device, delta_rx_count, delta_tx_count)); |
| 968 | |
| 969 | // Expect the correct values to be saved to the profile. |
| 970 | ExpectSavedCounts(device, delta_rx_count, delta_tx_count); |
| 971 | |
| 972 | // If Device is loaded from a profile that does not contain stored byte |
| 973 | // counts, the byte counts reported should remain unchanged. |
| 974 | EXPECT_TRUE(device->Load(&storage_)); |
| 975 | EXPECT_TRUE(ExpectByteCounts(device, delta_rx_count, delta_tx_count)); |
| 976 | |
| 977 | // If Device is loaded from a profile that contains stored byte |
| 978 | // counts, the byte counts reported should now reflect the stored values. |
| 979 | rx_stored_byte_count_ = 345; |
| 980 | tx_stored_byte_count_ = 678; |
| 981 | EXPECT_TRUE(device->Load(&storage_)); |
| 982 | EXPECT_TRUE(ExpectByteCounts( |
| 983 | device, rx_stored_byte_count_, tx_stored_byte_count_)); |
| 984 | |
| 985 | // Increases to the interface receive count should be reflected as offsets |
| 986 | // to the stored byte counts. |
| 987 | rx_byte_count_ += delta_rx_count; |
| 988 | tx_byte_count_ += delta_tx_count; |
| 989 | EXPECT_TRUE(ExpectByteCounts(device, |
| 990 | rx_stored_byte_count_ + delta_rx_count, |
| 991 | tx_stored_byte_count_ + delta_tx_count)); |
| 992 | |
| 993 | // Expect the correct values to be saved to the profile. |
| 994 | ExpectSavedCounts(device, |
| 995 | rx_stored_byte_count_ + delta_rx_count, |
| 996 | tx_stored_byte_count_ + delta_tx_count); |
| 997 | |
| 998 | // Expect that after resetting byte counts, read-back values return to zero, |
| 999 | // and that the device requests this information to be persisted. |
| 1000 | EXPECT_CALL(manager_, UpdateDevice(device)); |
| 1001 | device->ResetByteCounters(); |
| 1002 | EXPECT_TRUE(ExpectByteCounts(device, 0, 0)); |
| 1003 | } |
| 1004 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 1005 | } // namespace shill |