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 | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 32 | #include "shill/mock_manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 33 | #include "shill/mock_metrics.h" |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 34 | #include "shill/mock_portal_detector.h" |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 35 | #include "shill/mock_rtnl_handler.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 36 | #include "shill/mock_service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | #include "shill/mock_store.h" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 38 | #include "shill/portal_detector.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | #include "shill/property_store_unittest.h" |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 40 | #include "shill/static_ip_parameters.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 41 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | |
| 43 | using std::map; |
| 44 | using std::string; |
| 45 | using std::vector; |
| 46 | using ::testing::_; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 47 | using ::testing::AnyNumber; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 48 | using ::testing::AtLeast; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 49 | using ::testing::Mock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 50 | using ::testing::NiceMock; |
| 51 | using ::testing::Return; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 52 | using ::testing::ReturnRef; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 53 | using ::testing::StrictMock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 54 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 55 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 56 | |
| 57 | namespace shill { |
| 58 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 59 | class TestDevice : public Device { |
| 60 | public: |
| 61 | TestDevice(ControlInterface *control_interface, |
| 62 | EventDispatcher *dispatcher, |
| 63 | Metrics *metrics, |
| 64 | Manager *manager, |
| 65 | const std::string &link_name, |
| 66 | const std::string &address, |
| 67 | int interface_index, |
| 68 | Technology::Identifier technology) |
| 69 | : Device(control_interface, dispatcher, metrics, manager, link_name, |
| 70 | address, interface_index, technology) {} |
| 71 | ~TestDevice() {} |
| 72 | virtual void Start(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 73 | const EnabledStateChangedCallback &callback) { |
| 74 | DCHECK(error); |
| 75 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 76 | virtual void Stop(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 77 | const EnabledStateChangedCallback &callback) { |
| 78 | DCHECK(error); |
| 79 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 82 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 83 | public: |
| 84 | DeviceTest() |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 85 | : device_(new TestDevice(control_interface(), |
| 86 | dispatcher(), |
| 87 | NULL, |
| 88 | manager(), |
| 89 | kDeviceName, |
| 90 | kDeviceAddress, |
| 91 | kDeviceInterfaceIndex, |
| 92 | Technology::kUnknown)), |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 93 | device_info_(control_interface(), NULL, NULL, NULL) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 94 | DHCPProvider::GetInstance()->glib_ = glib(); |
| 95 | DHCPProvider::GetInstance()->control_interface_ = control_interface(); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 96 | DHCPProvider::GetInstance()->dispatcher_ = dispatcher(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 97 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 98 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 99 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 100 | virtual void SetUp() { |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 101 | device_->metrics_ = &metrics_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 102 | device_->rtnl_handler_ = &rtnl_handler_; |
| 103 | } |
| 104 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 105 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 106 | static const char kDeviceName[]; |
| 107 | static const char kDeviceAddress[]; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 108 | static const int kDeviceInterfaceIndex; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 109 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 110 | void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
| 111 | device_->OnIPConfigUpdated(ipconfig, success); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void SelectService(const ServiceRefPtr service) { |
| 115 | device_->SelectService(service); |
| 116 | } |
| 117 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 118 | void SetConnection(ConnectionRefPtr connection) { |
| 119 | device_->connection_ = connection; |
| 120 | } |
| 121 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 122 | MockControl control_interface_; |
| 123 | DeviceRefPtr device_; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 124 | MockDeviceInfo device_info_; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 125 | MockMetrics metrics_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 126 | StrictMock<MockRTNLHandler> rtnl_handler_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 129 | const char DeviceTest::kDeviceName[] = "testdevice"; |
| 130 | const char DeviceTest::kDeviceAddress[] = "address"; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 131 | const int DeviceTest::kDeviceInterfaceIndex = 0; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 132 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 133 | TEST_F(DeviceTest, Contains) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(device_->store().Contains(flimflam::kNameProperty)); |
| 135 | EXPECT_FALSE(device_->store().Contains("")); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 138 | TEST_F(DeviceTest, GetProperties) { |
| 139 | map<string, ::DBus::Variant> props; |
| 140 | Error error(Error::kInvalidProperty, ""); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 141 | ::DBus::Error dbus_error; |
| 142 | DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error); |
| 143 | ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end()); |
| 144 | EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(), |
| 145 | string(kDeviceName)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 148 | // Note: there are currently no writeable Device properties that |
| 149 | // aren't registered in a subclass. |
| 150 | TEST_F(DeviceTest, SetReadOnlyProperty) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 151 | ::DBus::Error error; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 152 | // 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] | 153 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 154 | flimflam::kAddressProperty, |
| 155 | PropertyStoreTest::kStringV, |
| 156 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 157 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 158 | } |
| 159 | |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 160 | TEST_F(DeviceTest, ClearReadOnlyProperty) { |
| 161 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 162 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 163 | flimflam::kAddressProperty, |
| 164 | PropertyStoreTest::kStringV, |
| 165 | &error)); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_F(DeviceTest, ClearReadOnlyDerivedProperty) { |
| 169 | ::DBus::Error error; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 170 | EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(), |
| 171 | flimflam::kIPConfigsProperty, |
| 172 | PropertyStoreTest::kStringsV, |
| 173 | &error)); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 176 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 177 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 178 | device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 179 | device_->DestroyIPConfig(); |
| 180 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 184 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 185 | device_->DestroyIPConfig(); |
| 186 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 187 | } |
| 188 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 189 | TEST_F(DeviceTest, AcquireIPConfig) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 190 | device_->ipconfig_ = new IPConfig(control_interface(), "randomname"); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 191 | scoped_ptr<MockDHCPProvider> dhcp_provider(new MockDHCPProvider()); |
| 192 | device_->dhcp_provider_ = dhcp_provider.get(); |
| 193 | scoped_refptr<MockDHCPConfig> dhcp_config(new MockDHCPConfig( |
| 194 | control_interface(), |
| 195 | kDeviceName)); |
| 196 | EXPECT_CALL(*dhcp_provider, CreateConfig(_, _, _, _)) |
| 197 | .WillOnce(Return(dhcp_config)); |
| 198 | EXPECT_CALL(*dhcp_config, RequestIP()) |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 199 | .WillOnce(Return(false)); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 200 | EXPECT_FALSE(device_->AcquireIPConfig()); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 201 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 202 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 203 | EXPECT_FALSE(device_->ipconfig_->update_callback_.is_null()); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 204 | device_->dhcp_provider_ = NULL; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 207 | TEST_F(DeviceTest, Load) { |
| 208 | NiceMock<MockStore> storage; |
| 209 | const string id = device_->GetStorageIdentifier(); |
| 210 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 211 | EXPECT_CALL(storage, GetBool(id, _, _)) |
| 212 | .Times(AtLeast(1)) |
| 213 | .WillRepeatedly(Return(true)); |
| 214 | EXPECT_TRUE(device_->Load(&storage)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(DeviceTest, Save) { |
| 218 | NiceMock<MockStore> storage; |
| 219 | const string id = device_->GetStorageIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 220 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 221 | .Times(AtLeast(1)) |
| 222 | .WillRepeatedly(Return(true)); |
| 223 | EXPECT_CALL(storage, SetBool(id, _, _)) |
| 224 | .Times(AtLeast(1)) |
| 225 | .WillRepeatedly(Return(true)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 226 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 227 | kDeviceName); |
| 228 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 229 | .WillOnce(Return(true)); |
| 230 | device_->ipconfig_ = ipconfig; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 231 | EXPECT_TRUE(device_->Save(&storage)); |
| 232 | } |
| 233 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 234 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 235 | string to_process("/device/stuff/0"); |
| 236 | ControlInterface::RpcIdToStorageId(&to_process); |
| 237 | EXPECT_TRUE(isalpha(to_process[0])); |
| 238 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 239 | } |
| 240 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 241 | MATCHER(IsNullRefPtr, "") { |
| 242 | return !arg; |
| 243 | } |
| 244 | |
| 245 | MATCHER(NotNullRefPtr, "") { |
| 246 | return arg; |
| 247 | } |
| 248 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 249 | TEST_F(DeviceTest, SelectedService) { |
| 250 | EXPECT_FALSE(device_->selected_service_.get()); |
| 251 | device_->SetServiceState(Service::kStateAssociating); |
| 252 | scoped_refptr<MockService> service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 253 | new StrictMock<MockService>(control_interface(), |
| 254 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 255 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 256 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 257 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 258 | EXPECT_TRUE(device_->selected_service_.get() == service.get()); |
| 259 | |
| 260 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
| 261 | device_->SetServiceState(Service::kStateConfiguring); |
| 262 | EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange)); |
| 263 | device_->SetServiceFailure(Service::kFailureOutOfRange); |
| 264 | |
| 265 | // Service should be returned to "Idle" state |
| 266 | EXPECT_CALL(*service.get(), state()) |
| 267 | .WillOnce(Return(Service::kStateUnknown)); |
| 268 | EXPECT_CALL(*service.get(), SetState(Service::kStateIdle)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 269 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 270 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 271 | |
| 272 | // A service in the "Failure" state should not be reset to "Idle" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 273 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 274 | EXPECT_CALL(*service.get(), state()) |
| 275 | .WillOnce(Return(Service::kStateFailure)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 276 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 277 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 280 | TEST_F(DeviceTest, IPConfigUpdatedFailure) { |
| 281 | scoped_refptr<MockService> service( |
| 282 | new StrictMock<MockService>(control_interface(), |
| 283 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 284 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 285 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 286 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 287 | EXPECT_CALL(*service.get(), SetState(Service::kStateDisconnected)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 288 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 289 | OnIPConfigUpdated(NULL, false); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 290 | } |
| 291 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 292 | TEST_F(DeviceTest, IPConfigUpdatedFailureWithStatic) { |
| 293 | scoped_refptr<MockService> service( |
| 294 | new StrictMock<MockService>(control_interface(), |
| 295 | dispatcher(), |
| 296 | metrics(), |
| 297 | manager())); |
| 298 | SelectService(service); |
| 299 | service->static_ip_parameters_.args_.SetString( |
| 300 | flimflam::kAddressProperty, "1.1.1.1"); |
| 301 | service->static_ip_parameters_.args_.SetInt(flimflam::kPrefixlenProperty, 16); |
| 302 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 303 | EXPECT_CALL(*service.get(), SetConnection(_)).Times(0); |
| 304 | OnIPConfigUpdated(NULL, false); |
| 305 | } |
| 306 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 307 | TEST_F(DeviceTest, IPConfigUpdatedSuccess) { |
| 308 | scoped_refptr<MockService> service( |
| 309 | new StrictMock<MockService>(control_interface(), |
| 310 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 311 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 312 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 313 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 314 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 315 | kDeviceName); |
| 316 | EXPECT_CALL(*service.get(), SetState(Service::kStateConnected)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 317 | EXPECT_CALL(*service.get(), IsConnected()) |
| 318 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 319 | EXPECT_CALL(*service.get(), IsPortalDetectionDisabled()) |
| 320 | .WillRepeatedly(Return(true)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 321 | EXPECT_CALL(*service.get(), SetState(Service::kStateOnline)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 322 | EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 323 | OnIPConfigUpdated(ipconfig.get(), true); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame^] | 326 | TEST_F(DeviceTest, SetEnabledPersistent) { |
| 327 | EXPECT_FALSE(device_->enabled_); |
| 328 | EXPECT_FALSE(device_->enabled_pending_); |
| 329 | device_->enabled_persistent_ = false; |
| 330 | StrictMock<MockManager> manager(control_interface(), |
| 331 | dispatcher(), |
| 332 | metrics(), |
| 333 | glib()); |
| 334 | EXPECT_CALL(manager, UpdateDevice(_)); |
| 335 | device_->manager_ = &manager; |
| 336 | Error error; |
| 337 | device_->SetEnabledPersistent(true, &error, ResultCallback()); |
| 338 | EXPECT_TRUE(device_->enabled_persistent_); |
| 339 | EXPECT_TRUE(device_->enabled_pending_); |
| 340 | } |
| 341 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 342 | TEST_F(DeviceTest, Start) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 343 | EXPECT_FALSE(device_->running_); |
| 344 | EXPECT_FALSE(device_->enabled_); |
| 345 | EXPECT_FALSE(device_->enabled_pending_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 346 | device_->SetEnabled(true); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 347 | EXPECT_TRUE(device_->running_); |
| 348 | EXPECT_TRUE(device_->enabled_pending_); |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 349 | device_->OnEnabledStateChanged(ResultCallback(), |
| 350 | Error(Error::kOperationFailed)); |
| 351 | EXPECT_FALSE(device_->enabled_pending_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 352 | } |
| 353 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 354 | TEST_F(DeviceTest, Stop) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 355 | device_->enabled_ = true; |
| 356 | device_->enabled_pending_ = true; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 357 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
| 358 | scoped_refptr<MockService> service( |
| 359 | new NiceMock<MockService>(&control_interface_, |
| 360 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 361 | metrics(), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 362 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 363 | SelectService(service); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 364 | |
| 365 | EXPECT_CALL(*service.get(), state()). |
| 366 | WillRepeatedly(Return(Service::kStateConnected)); |
| 367 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 368 | UpdateEnabled()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 369 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 370 | EmitBoolChanged(flimflam::kPoweredProperty, false)); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 371 | EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 372 | device_->SetEnabled(false); |
| 373 | device_->OnEnabledStateChanged(ResultCallback(), Error()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 374 | |
| 375 | EXPECT_FALSE(device_->ipconfig_.get()); |
| 376 | EXPECT_FALSE(device_->selected_service_.get()); |
| 377 | } |
| 378 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 379 | class DevicePortalDetectionTest : public DeviceTest { |
| 380 | public: |
| 381 | DevicePortalDetectionTest() |
| 382 | : connection_(new StrictMock<MockConnection>(&device_info_)), |
| 383 | manager_(control_interface(), |
| 384 | dispatcher(), |
| 385 | metrics(), |
| 386 | glib()), |
| 387 | service_(new StrictMock<MockService>(control_interface(), |
| 388 | dispatcher(), |
| 389 | metrics(), |
| 390 | &manager_)), |
| 391 | portal_detector_(new StrictMock<MockPortalDetector>(connection_)) {} |
| 392 | virtual ~DevicePortalDetectionTest() {} |
| 393 | virtual void SetUp() { |
| 394 | DeviceTest::SetUp(); |
| 395 | SelectService(service_); |
| 396 | SetConnection(connection_.get()); |
| 397 | device_->portal_detector_.reset(portal_detector_); // Passes ownership. |
| 398 | device_->manager_ = &manager_; |
| 399 | } |
| 400 | |
| 401 | protected: |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 402 | static const int kPortalAttempts; |
| 403 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 404 | bool StartPortalDetection() { return device_->StartPortalDetection(); } |
| 405 | void StopPortalDetection() { device_->StopPortalDetection(); } |
| 406 | |
| 407 | void PortalDetectorCallback(const PortalDetector::Result &result) { |
| 408 | device_->PortalDetectorCallback(result); |
| 409 | } |
| 410 | bool RequestPortalDetection() { |
| 411 | return device_->RequestPortalDetection(); |
| 412 | } |
| 413 | void SetServiceConnectedState(Service::ConnectState state) { |
| 414 | device_->SetServiceConnectedState(state); |
| 415 | } |
| 416 | void ExpectPortalDetectorReset() { |
| 417 | EXPECT_FALSE(device_->portal_detector_.get()); |
| 418 | } |
| 419 | void ExpectPortalDetectorSet() { |
| 420 | EXPECT_TRUE(device_->portal_detector_.get()); |
| 421 | } |
| 422 | void ExpectPortalDetectorIsMock() { |
| 423 | EXPECT_EQ(portal_detector_, device_->portal_detector_.get()); |
| 424 | } |
| 425 | scoped_refptr<MockConnection> connection_; |
| 426 | StrictMock<MockManager> manager_; |
| 427 | scoped_refptr<MockService> service_; |
| 428 | |
| 429 | // Used only for EXPECT_CALL(). Object is owned by device. |
| 430 | MockPortalDetector *portal_detector_; |
| 431 | }; |
| 432 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 433 | const int DevicePortalDetectionTest::kPortalAttempts = 2; |
| 434 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 435 | TEST_F(DevicePortalDetectionTest, ServicePortalDetectionDisabled) { |
| 436 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 437 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 438 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 439 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 440 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 441 | EXPECT_FALSE(StartPortalDetection()); |
| 442 | } |
| 443 | |
| 444 | TEST_F(DevicePortalDetectionTest, TechnologyPortalDetectionDisabled) { |
| 445 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 446 | .WillOnce(Return(false)); |
| 447 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 448 | .WillRepeatedly(Return(true)); |
| 449 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 450 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 451 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 452 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 453 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 454 | EXPECT_FALSE(StartPortalDetection()); |
| 455 | } |
| 456 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 457 | TEST_F(DevicePortalDetectionTest, PortalDetectionProxyConfig) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 458 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 459 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 460 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 461 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 462 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 463 | .WillOnce(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 464 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 465 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 466 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 467 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 468 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 469 | EXPECT_FALSE(StartPortalDetection()); |
| 470 | } |
| 471 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 472 | TEST_F(DevicePortalDetectionTest, PortalDetectionBadUrl) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 473 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 474 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 475 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 476 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 477 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 478 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 479 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 480 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 481 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 482 | .WillOnce(Return(true)); |
| 483 | const string portal_url; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 484 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 485 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 486 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 487 | EXPECT_FALSE(StartPortalDetection()); |
| 488 | } |
| 489 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 490 | TEST_F(DevicePortalDetectionTest, PortalDetectionStart) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 491 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 492 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 493 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 494 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 495 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 496 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 497 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 498 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 499 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 500 | .WillOnce(Return(true)); |
| 501 | const string portal_url(PortalDetector::kDefaultURL); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 502 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 503 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 504 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 505 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 506 | const string kInterfaceName("int0"); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 507 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 508 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 509 | const vector<string> kDNSServers; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 510 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 511 | .WillRepeatedly(ReturnRef(kDNSServers)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 512 | EXPECT_TRUE(StartPortalDetection()); |
| 513 | |
| 514 | // Drop all references to device_info before it falls out of scope. |
| 515 | SetConnection(NULL); |
| 516 | StopPortalDetection(); |
| 517 | } |
| 518 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 519 | TEST_F(DevicePortalDetectionTest, PortalDetectionNonFinal) { |
| 520 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 521 | .Times(0); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 522 | EXPECT_CALL(*service_.get(), SetState(_)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 523 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 524 | PortalDetectorCallback(PortalDetector::Result( |
| 525 | PortalDetector::kPhaseUnknown, |
| 526 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 527 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 528 | false)); |
| 529 | } |
| 530 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 531 | TEST_F(DevicePortalDetectionTest, PortalDetectionFailure) { |
| 532 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 533 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 534 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 535 | EXPECT_CALL(metrics_, |
| 536 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 537 | Metrics::kPortalResultConnectionFailure, |
| 538 | Metrics::kPortalResultMax)); |
| 539 | EXPECT_CALL(metrics_, |
| 540 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 541 | _, _, _, _)).Times(0); |
| 542 | EXPECT_CALL(metrics_, |
| 543 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 544 | kPortalAttempts, |
| 545 | Metrics::kMetricPortalAttemptsMin, |
| 546 | Metrics::kMetricPortalAttemptsMax, |
| 547 | Metrics::kMetricPortalAttemptsNumBuckets)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 548 | EXPECT_CALL(*connection_.get(), is_default()) |
| 549 | .WillOnce(Return(false)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 550 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 551 | PortalDetector::kPhaseConnection, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 552 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 553 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 554 | true)); |
| 555 | } |
| 556 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 557 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccess) { |
| 558 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 559 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 560 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 561 | EXPECT_CALL(metrics_, |
| 562 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 563 | Metrics::kPortalResultSuccess, |
| 564 | Metrics::kPortalResultMax)); |
| 565 | EXPECT_CALL(metrics_, |
| 566 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 567 | kPortalAttempts, |
| 568 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 569 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 570 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 571 | EXPECT_CALL(metrics_, |
| 572 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 573 | _, _, _, _)).Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 574 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 575 | PortalDetector::kPhaseContent, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 576 | PortalDetector::kStatusSuccess, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 577 | kPortalAttempts, |
| 578 | true)); |
| 579 | } |
| 580 | |
| 581 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccessAfterFailure) { |
| 582 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 583 | .WillRepeatedly(Return(true)); |
| 584 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 585 | EXPECT_CALL(metrics_, |
| 586 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 587 | Metrics::kPortalResultConnectionFailure, |
| 588 | Metrics::kPortalResultMax)); |
| 589 | EXPECT_CALL(metrics_, |
| 590 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 591 | _, _, _, _)).Times(0); |
| 592 | EXPECT_CALL(metrics_, |
| 593 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 594 | kPortalAttempts, |
| 595 | Metrics::kMetricPortalAttemptsMin, |
| 596 | Metrics::kMetricPortalAttemptsMax, |
| 597 | Metrics::kMetricPortalAttemptsNumBuckets)); |
| 598 | EXPECT_CALL(*connection_.get(), is_default()) |
| 599 | .WillOnce(Return(false)); |
| 600 | PortalDetectorCallback(PortalDetector::Result( |
| 601 | PortalDetector::kPhaseConnection, |
| 602 | PortalDetector::kStatusFailure, |
| 603 | kPortalAttempts, |
| 604 | true)); |
| 605 | Mock::VerifyAndClearExpectations(&metrics_); |
| 606 | |
| 607 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 608 | EXPECT_CALL(metrics_, |
| 609 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 610 | Metrics::kPortalResultSuccess, |
| 611 | Metrics::kPortalResultMax)); |
| 612 | EXPECT_CALL(metrics_, |
| 613 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 614 | kPortalAttempts * 2, |
| 615 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 616 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 617 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 618 | EXPECT_CALL(metrics_, |
| 619 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 620 | _, _, _, _)).Times(0); |
| 621 | PortalDetectorCallback(PortalDetector::Result( |
| 622 | PortalDetector::kPhaseContent, |
| 623 | PortalDetector::kStatusSuccess, |
| 624 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 625 | true)); |
| 626 | } |
| 627 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 628 | TEST_F(DevicePortalDetectionTest, RequestPortalDetection) { |
| 629 | EXPECT_CALL(*service_.get(), state()) |
| 630 | .WillOnce(Return(Service::kStateOnline)) |
| 631 | .WillRepeatedly(Return(Service::kStatePortal)); |
| 632 | EXPECT_FALSE(RequestPortalDetection()); |
| 633 | |
| 634 | EXPECT_CALL(*connection_.get(), is_default()) |
| 635 | .WillOnce(Return(false)) |
| 636 | .WillRepeatedly(Return(true)); |
| 637 | EXPECT_FALSE(RequestPortalDetection()); |
| 638 | |
| 639 | EXPECT_CALL(*portal_detector_, IsInProgress()) |
| 640 | .WillOnce(Return(true)); |
| 641 | // Portal detection already running. |
| 642 | EXPECT_TRUE(RequestPortalDetection()); |
| 643 | |
| 644 | // Make sure our running mock portal detector was not replaced. |
| 645 | ExpectPortalDetectorIsMock(); |
| 646 | |
| 647 | // Throw away our pre-fabricated portal detector, and have the device create |
| 648 | // a new one. |
| 649 | StopPortalDetection(); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 650 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 651 | .WillRepeatedly(Return(false)); |
| 652 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 653 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 654 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
| 655 | .WillRepeatedly(Return(true)); |
| 656 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
| 657 | .WillRepeatedly(Return(false)); |
| 658 | const string kPortalCheckURL("http://portal"); |
| 659 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 660 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 661 | const string kInterfaceName("int0"); |
| 662 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 663 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
| 664 | const vector<string> kDNSServers; |
| 665 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 666 | .WillRepeatedly(ReturnRef(kDNSServers)); |
| 667 | EXPECT_TRUE(RequestPortalDetection()); |
| 668 | } |
| 669 | |
| 670 | TEST_F(DevicePortalDetectionTest, NotConnected) { |
| 671 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 672 | .WillOnce(Return(false)); |
| 673 | SetServiceConnectedState(Service::kStatePortal); |
| 674 | // We don't check for the portal detector to be reset here, because |
| 675 | // it would have been reset as a part of disconnection. |
| 676 | } |
| 677 | |
| 678 | TEST_F(DevicePortalDetectionTest, NotPortal) { |
| 679 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 680 | .WillOnce(Return(true)); |
| 681 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 682 | SetServiceConnectedState(Service::kStateOnline); |
| 683 | ExpectPortalDetectorReset(); |
| 684 | } |
| 685 | |
| 686 | TEST_F(DevicePortalDetectionTest, NotDefault) { |
| 687 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 688 | .WillOnce(Return(true)); |
| 689 | EXPECT_CALL(*connection_.get(), is_default()) |
| 690 | .WillOnce(Return(false)); |
| 691 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 692 | SetServiceConnectedState(Service::kStatePortal); |
| 693 | ExpectPortalDetectorReset(); |
| 694 | } |
| 695 | |
| 696 | TEST_F(DevicePortalDetectionTest, PortalIntervalIsZero) { |
| 697 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 698 | .WillOnce(Return(true)); |
| 699 | EXPECT_CALL(*connection_.get(), is_default()) |
| 700 | .WillOnce(Return(true)); |
| 701 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 702 | .WillOnce(Return(0)); |
| 703 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 704 | SetServiceConnectedState(Service::kStatePortal); |
| 705 | ExpectPortalDetectorReset(); |
| 706 | } |
| 707 | |
| 708 | TEST_F(DevicePortalDetectionTest, RestartPortalDetection) { |
| 709 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 710 | .WillOnce(Return(true)); |
| 711 | EXPECT_CALL(*connection_.get(), is_default()) |
| 712 | .WillOnce(Return(true)); |
| 713 | const int kPortalDetectionInterval = 10; |
| 714 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 715 | .Times(AtLeast(1)) |
| 716 | .WillRepeatedly(Return(kPortalDetectionInterval)); |
| 717 | const string kPortalCheckURL("http://portal"); |
| 718 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 719 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 720 | EXPECT_CALL(*portal_detector_, StartAfterDelay(kPortalCheckURL, |
| 721 | kPortalDetectionInterval)) |
| 722 | .WillOnce(Return(true)); |
| 723 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 724 | SetServiceConnectedState(Service::kStatePortal); |
| 725 | ExpectPortalDetectorSet(); |
| 726 | } |
| 727 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 728 | } // namespace shill |