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, TechnologyIs) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 177 | EXPECT_FALSE(device_->TechnologyIs(Technology::kEthernet)); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | TEST_F(DeviceTest, DestroyIPConfig) { |
| 181 | ASSERT_FALSE(device_->ipconfig_.get()); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 182 | device_->ipconfig_ = new IPConfig(control_interface(), kDeviceName); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 183 | device_->DestroyIPConfig(); |
| 184 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 185 | } |
| 186 | |
| 187 | TEST_F(DeviceTest, DestroyIPConfigNULL) { |
| 188 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 189 | device_->DestroyIPConfig(); |
| 190 | ASSERT_FALSE(device_->ipconfig_.get()); |
| 191 | } |
| 192 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 193 | TEST_F(DeviceTest, AcquireIPConfig) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 194 | device_->ipconfig_ = new IPConfig(control_interface(), "randomname"); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 195 | scoped_ptr<MockDHCPProvider> dhcp_provider(new MockDHCPProvider()); |
| 196 | device_->dhcp_provider_ = dhcp_provider.get(); |
| 197 | scoped_refptr<MockDHCPConfig> dhcp_config(new MockDHCPConfig( |
| 198 | control_interface(), |
| 199 | kDeviceName)); |
| 200 | EXPECT_CALL(*dhcp_provider, CreateConfig(_, _, _, _)) |
| 201 | .WillOnce(Return(dhcp_config)); |
| 202 | EXPECT_CALL(*dhcp_config, RequestIP()) |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 203 | .WillOnce(Return(false)); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 204 | EXPECT_FALSE(device_->AcquireIPConfig()); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 205 | ASSERT_TRUE(device_->ipconfig_.get()); |
| 206 | EXPECT_EQ(kDeviceName, device_->ipconfig_->device_name()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 207 | EXPECT_FALSE(device_->ipconfig_->update_callback_.is_null()); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 208 | device_->dhcp_provider_ = NULL; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 211 | TEST_F(DeviceTest, Load) { |
| 212 | NiceMock<MockStore> storage; |
| 213 | const string id = device_->GetStorageIdentifier(); |
| 214 | EXPECT_CALL(storage, ContainsGroup(id)).WillOnce(Return(true)); |
| 215 | EXPECT_CALL(storage, GetBool(id, _, _)) |
| 216 | .Times(AtLeast(1)) |
| 217 | .WillRepeatedly(Return(true)); |
| 218 | EXPECT_TRUE(device_->Load(&storage)); |
| 219 | } |
| 220 | |
| 221 | TEST_F(DeviceTest, Save) { |
| 222 | NiceMock<MockStore> storage; |
| 223 | const string id = device_->GetStorageIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 224 | EXPECT_CALL(storage, SetString(id, _, _)) |
| 225 | .Times(AtLeast(1)) |
| 226 | .WillRepeatedly(Return(true)); |
| 227 | EXPECT_CALL(storage, SetBool(id, _, _)) |
| 228 | .Times(AtLeast(1)) |
| 229 | .WillRepeatedly(Return(true)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 230 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 231 | kDeviceName); |
| 232 | EXPECT_CALL(*ipconfig.get(), Save(_, _)) |
| 233 | .WillOnce(Return(true)); |
| 234 | device_->ipconfig_ = ipconfig; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 235 | EXPECT_TRUE(device_->Save(&storage)); |
| 236 | } |
| 237 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 238 | TEST_F(DeviceTest, StorageIdGeneration) { |
| 239 | string to_process("/device/stuff/0"); |
| 240 | ControlInterface::RpcIdToStorageId(&to_process); |
| 241 | EXPECT_TRUE(isalpha(to_process[0])); |
| 242 | EXPECT_EQ(string::npos, to_process.find('/')); |
| 243 | } |
| 244 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 245 | MATCHER(IsNullRefPtr, "") { |
| 246 | return !arg; |
| 247 | } |
| 248 | |
| 249 | MATCHER(NotNullRefPtr, "") { |
| 250 | return arg; |
| 251 | } |
| 252 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 253 | TEST_F(DeviceTest, SelectedService) { |
| 254 | EXPECT_FALSE(device_->selected_service_.get()); |
| 255 | device_->SetServiceState(Service::kStateAssociating); |
| 256 | scoped_refptr<MockService> service( |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 257 | new StrictMock<MockService>(control_interface(), |
| 258 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 259 | metrics(), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 260 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 261 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 262 | EXPECT_TRUE(device_->selected_service_.get() == service.get()); |
| 263 | |
| 264 | EXPECT_CALL(*service.get(), SetState(Service::kStateConfiguring)); |
| 265 | device_->SetServiceState(Service::kStateConfiguring); |
| 266 | EXPECT_CALL(*service.get(), SetFailure(Service::kFailureOutOfRange)); |
| 267 | device_->SetServiceFailure(Service::kFailureOutOfRange); |
| 268 | |
| 269 | // Service should be returned to "Idle" state |
| 270 | EXPECT_CALL(*service.get(), state()) |
| 271 | .WillOnce(Return(Service::kStateUnknown)); |
| 272 | EXPECT_CALL(*service.get(), SetState(Service::kStateIdle)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 273 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 274 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 275 | |
| 276 | // A service in the "Failure" state should not be reset to "Idle" |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 277 | SelectService(service); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 278 | EXPECT_CALL(*service.get(), state()) |
| 279 | .WillOnce(Return(Service::kStateFailure)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 280 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 281 | SelectService(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 284 | TEST_F(DeviceTest, IPConfigUpdatedFailure) { |
| 285 | scoped_refptr<MockService> service( |
| 286 | new StrictMock<MockService>(control_interface(), |
| 287 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 288 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 289 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 290 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 291 | EXPECT_CALL(*service.get(), SetState(Service::kStateDisconnected)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 292 | EXPECT_CALL(*service.get(), SetConnection(IsNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 293 | OnIPConfigUpdated(NULL, false); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 294 | } |
| 295 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 296 | TEST_F(DeviceTest, IPConfigUpdatedFailureWithStatic) { |
| 297 | scoped_refptr<MockService> service( |
| 298 | new StrictMock<MockService>(control_interface(), |
| 299 | dispatcher(), |
| 300 | metrics(), |
| 301 | manager())); |
| 302 | SelectService(service); |
| 303 | service->static_ip_parameters_.args_.SetString( |
| 304 | flimflam::kAddressProperty, "1.1.1.1"); |
| 305 | service->static_ip_parameters_.args_.SetInt(flimflam::kPrefixlenProperty, 16); |
| 306 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 307 | EXPECT_CALL(*service.get(), SetConnection(_)).Times(0); |
| 308 | OnIPConfigUpdated(NULL, false); |
| 309 | } |
| 310 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 311 | TEST_F(DeviceTest, IPConfigUpdatedSuccess) { |
| 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 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 319 | kDeviceName); |
| 320 | EXPECT_CALL(*service.get(), SetState(Service::kStateConnected)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 321 | EXPECT_CALL(*service.get(), IsConnected()) |
| 322 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 323 | EXPECT_CALL(*service.get(), IsPortalDetectionDisabled()) |
| 324 | .WillRepeatedly(Return(true)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 325 | EXPECT_CALL(*service.get(), SetState(Service::kStateOnline)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 326 | EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 327 | OnIPConfigUpdated(ipconfig.get(), true); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 330 | TEST_F(DeviceTest, Start) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 331 | EXPECT_FALSE(device_->running_); |
| 332 | EXPECT_FALSE(device_->enabled_); |
| 333 | EXPECT_FALSE(device_->enabled_pending_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 334 | device_->SetEnabled(true); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 335 | EXPECT_TRUE(device_->running_); |
| 336 | EXPECT_TRUE(device_->enabled_pending_); |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 337 | device_->OnEnabledStateChanged(ResultCallback(), |
| 338 | Error(Error::kOperationFailed)); |
| 339 | EXPECT_FALSE(device_->enabled_pending_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 340 | } |
| 341 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 342 | TEST_F(DeviceTest, Stop) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 343 | device_->enabled_ = true; |
| 344 | device_->enabled_pending_ = true; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 345 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
| 346 | scoped_refptr<MockService> service( |
| 347 | new NiceMock<MockService>(&control_interface_, |
| 348 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 349 | metrics(), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 350 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 351 | SelectService(service); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 352 | |
| 353 | EXPECT_CALL(*service.get(), state()). |
| 354 | WillRepeatedly(Return(Service::kStateConnected)); |
| 355 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 356 | UpdateEnabled()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 357 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 358 | EmitBoolChanged(flimflam::kPoweredProperty, false)); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 359 | EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 360 | device_->SetEnabled(false); |
| 361 | device_->OnEnabledStateChanged(ResultCallback(), Error()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 362 | |
| 363 | EXPECT_FALSE(device_->ipconfig_.get()); |
| 364 | EXPECT_FALSE(device_->selected_service_.get()); |
| 365 | } |
| 366 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 367 | class DevicePortalDetectionTest : public DeviceTest { |
| 368 | public: |
| 369 | DevicePortalDetectionTest() |
| 370 | : connection_(new StrictMock<MockConnection>(&device_info_)), |
| 371 | manager_(control_interface(), |
| 372 | dispatcher(), |
| 373 | metrics(), |
| 374 | glib()), |
| 375 | service_(new StrictMock<MockService>(control_interface(), |
| 376 | dispatcher(), |
| 377 | metrics(), |
| 378 | &manager_)), |
| 379 | portal_detector_(new StrictMock<MockPortalDetector>(connection_)) {} |
| 380 | virtual ~DevicePortalDetectionTest() {} |
| 381 | virtual void SetUp() { |
| 382 | DeviceTest::SetUp(); |
| 383 | SelectService(service_); |
| 384 | SetConnection(connection_.get()); |
| 385 | device_->portal_detector_.reset(portal_detector_); // Passes ownership. |
| 386 | device_->manager_ = &manager_; |
| 387 | } |
| 388 | |
| 389 | protected: |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 390 | static const int kPortalAttempts; |
| 391 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 392 | bool StartPortalDetection() { return device_->StartPortalDetection(); } |
| 393 | void StopPortalDetection() { device_->StopPortalDetection(); } |
| 394 | |
| 395 | void PortalDetectorCallback(const PortalDetector::Result &result) { |
| 396 | device_->PortalDetectorCallback(result); |
| 397 | } |
| 398 | bool RequestPortalDetection() { |
| 399 | return device_->RequestPortalDetection(); |
| 400 | } |
| 401 | void SetServiceConnectedState(Service::ConnectState state) { |
| 402 | device_->SetServiceConnectedState(state); |
| 403 | } |
| 404 | void ExpectPortalDetectorReset() { |
| 405 | EXPECT_FALSE(device_->portal_detector_.get()); |
| 406 | } |
| 407 | void ExpectPortalDetectorSet() { |
| 408 | EXPECT_TRUE(device_->portal_detector_.get()); |
| 409 | } |
| 410 | void ExpectPortalDetectorIsMock() { |
| 411 | EXPECT_EQ(portal_detector_, device_->portal_detector_.get()); |
| 412 | } |
| 413 | scoped_refptr<MockConnection> connection_; |
| 414 | StrictMock<MockManager> manager_; |
| 415 | scoped_refptr<MockService> service_; |
| 416 | |
| 417 | // Used only for EXPECT_CALL(). Object is owned by device. |
| 418 | MockPortalDetector *portal_detector_; |
| 419 | }; |
| 420 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 421 | const int DevicePortalDetectionTest::kPortalAttempts = 2; |
| 422 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 423 | TEST_F(DevicePortalDetectionTest, ServicePortalDetectionDisabled) { |
| 424 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 425 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 426 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 427 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 428 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 429 | EXPECT_FALSE(StartPortalDetection()); |
| 430 | } |
| 431 | |
| 432 | TEST_F(DevicePortalDetectionTest, TechnologyPortalDetectionDisabled) { |
| 433 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 434 | .WillOnce(Return(false)); |
| 435 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 436 | .WillRepeatedly(Return(true)); |
| 437 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 438 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 439 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 440 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 441 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 442 | EXPECT_FALSE(StartPortalDetection()); |
| 443 | } |
| 444 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 445 | TEST_F(DevicePortalDetectionTest, PortalDetectionProxyConfig) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 446 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 447 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 448 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 449 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 450 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 451 | .WillOnce(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 452 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 453 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 454 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 455 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 456 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 457 | EXPECT_FALSE(StartPortalDetection()); |
| 458 | } |
| 459 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 460 | TEST_F(DevicePortalDetectionTest, PortalDetectionBadUrl) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 461 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 462 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 463 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 464 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 465 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 466 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 467 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 468 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 469 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 470 | .WillOnce(Return(true)); |
| 471 | const string portal_url; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 472 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 473 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 474 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 475 | EXPECT_FALSE(StartPortalDetection()); |
| 476 | } |
| 477 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 478 | TEST_F(DevicePortalDetectionTest, PortalDetectionStart) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 479 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 480 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 481 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 482 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 483 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 484 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 485 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 486 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 487 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 488 | .WillOnce(Return(true)); |
| 489 | const string portal_url(PortalDetector::kDefaultURL); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 490 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 491 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 492 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 493 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 494 | const string kInterfaceName("int0"); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 495 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 496 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 497 | const vector<string> kDNSServers; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 498 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 499 | .WillRepeatedly(ReturnRef(kDNSServers)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 500 | EXPECT_TRUE(StartPortalDetection()); |
| 501 | |
| 502 | // Drop all references to device_info before it falls out of scope. |
| 503 | SetConnection(NULL); |
| 504 | StopPortalDetection(); |
| 505 | } |
| 506 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 507 | TEST_F(DevicePortalDetectionTest, PortalDetectionNonFinal) { |
| 508 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 509 | .Times(0); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 510 | EXPECT_CALL(*service_.get(), SetState(_)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 511 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 512 | PortalDetectorCallback(PortalDetector::Result( |
| 513 | PortalDetector::kPhaseUnknown, |
| 514 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 515 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 516 | false)); |
| 517 | } |
| 518 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 519 | TEST_F(DevicePortalDetectionTest, PortalDetectionFailure) { |
| 520 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 521 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 522 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 523 | EXPECT_CALL(metrics_, |
| 524 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 525 | Metrics::kPortalResultConnectionFailure, |
| 526 | Metrics::kPortalResultMax)); |
| 527 | EXPECT_CALL(metrics_, |
| 528 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 529 | _, _, _, _)).Times(0); |
| 530 | EXPECT_CALL(metrics_, |
| 531 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 532 | kPortalAttempts, |
| 533 | Metrics::kMetricPortalAttemptsMin, |
| 534 | Metrics::kMetricPortalAttemptsMax, |
| 535 | Metrics::kMetricPortalAttemptsNumBuckets)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 536 | EXPECT_CALL(*connection_.get(), is_default()) |
| 537 | .WillOnce(Return(false)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 538 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 539 | PortalDetector::kPhaseConnection, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 540 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 541 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 542 | true)); |
| 543 | } |
| 544 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 545 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccess) { |
| 546 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 547 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 548 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 549 | EXPECT_CALL(metrics_, |
| 550 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 551 | Metrics::kPortalResultSuccess, |
| 552 | Metrics::kPortalResultMax)); |
| 553 | EXPECT_CALL(metrics_, |
| 554 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 555 | kPortalAttempts, |
| 556 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 557 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 558 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 559 | EXPECT_CALL(metrics_, |
| 560 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 561 | _, _, _, _)).Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 562 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 563 | PortalDetector::kPhaseContent, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 564 | PortalDetector::kStatusSuccess, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 565 | kPortalAttempts, |
| 566 | true)); |
| 567 | } |
| 568 | |
| 569 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccessAfterFailure) { |
| 570 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 571 | .WillRepeatedly(Return(true)); |
| 572 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 573 | EXPECT_CALL(metrics_, |
| 574 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 575 | Metrics::kPortalResultConnectionFailure, |
| 576 | Metrics::kPortalResultMax)); |
| 577 | EXPECT_CALL(metrics_, |
| 578 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 579 | _, _, _, _)).Times(0); |
| 580 | EXPECT_CALL(metrics_, |
| 581 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 582 | kPortalAttempts, |
| 583 | Metrics::kMetricPortalAttemptsMin, |
| 584 | Metrics::kMetricPortalAttemptsMax, |
| 585 | Metrics::kMetricPortalAttemptsNumBuckets)); |
| 586 | EXPECT_CALL(*connection_.get(), is_default()) |
| 587 | .WillOnce(Return(false)); |
| 588 | PortalDetectorCallback(PortalDetector::Result( |
| 589 | PortalDetector::kPhaseConnection, |
| 590 | PortalDetector::kStatusFailure, |
| 591 | kPortalAttempts, |
| 592 | true)); |
| 593 | Mock::VerifyAndClearExpectations(&metrics_); |
| 594 | |
| 595 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 596 | EXPECT_CALL(metrics_, |
| 597 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 598 | Metrics::kPortalResultSuccess, |
| 599 | Metrics::kPortalResultMax)); |
| 600 | EXPECT_CALL(metrics_, |
| 601 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 602 | kPortalAttempts * 2, |
| 603 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 604 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 605 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 606 | EXPECT_CALL(metrics_, |
| 607 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 608 | _, _, _, _)).Times(0); |
| 609 | PortalDetectorCallback(PortalDetector::Result( |
| 610 | PortalDetector::kPhaseContent, |
| 611 | PortalDetector::kStatusSuccess, |
| 612 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 613 | true)); |
| 614 | } |
| 615 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 616 | TEST_F(DevicePortalDetectionTest, RequestPortalDetection) { |
| 617 | EXPECT_CALL(*service_.get(), state()) |
| 618 | .WillOnce(Return(Service::kStateOnline)) |
| 619 | .WillRepeatedly(Return(Service::kStatePortal)); |
| 620 | EXPECT_FALSE(RequestPortalDetection()); |
| 621 | |
| 622 | EXPECT_CALL(*connection_.get(), is_default()) |
| 623 | .WillOnce(Return(false)) |
| 624 | .WillRepeatedly(Return(true)); |
| 625 | EXPECT_FALSE(RequestPortalDetection()); |
| 626 | |
| 627 | EXPECT_CALL(*portal_detector_, IsInProgress()) |
| 628 | .WillOnce(Return(true)); |
| 629 | // Portal detection already running. |
| 630 | EXPECT_TRUE(RequestPortalDetection()); |
| 631 | |
| 632 | // Make sure our running mock portal detector was not replaced. |
| 633 | ExpectPortalDetectorIsMock(); |
| 634 | |
| 635 | // Throw away our pre-fabricated portal detector, and have the device create |
| 636 | // a new one. |
| 637 | StopPortalDetection(); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 638 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 639 | .WillRepeatedly(Return(false)); |
| 640 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 641 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 642 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
| 643 | .WillRepeatedly(Return(true)); |
| 644 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
| 645 | .WillRepeatedly(Return(false)); |
| 646 | const string kPortalCheckURL("http://portal"); |
| 647 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 648 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 649 | const string kInterfaceName("int0"); |
| 650 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 651 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
| 652 | const vector<string> kDNSServers; |
| 653 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 654 | .WillRepeatedly(ReturnRef(kDNSServers)); |
| 655 | EXPECT_TRUE(RequestPortalDetection()); |
| 656 | } |
| 657 | |
| 658 | TEST_F(DevicePortalDetectionTest, NotConnected) { |
| 659 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 660 | .WillOnce(Return(false)); |
| 661 | SetServiceConnectedState(Service::kStatePortal); |
| 662 | // We don't check for the portal detector to be reset here, because |
| 663 | // it would have been reset as a part of disconnection. |
| 664 | } |
| 665 | |
| 666 | TEST_F(DevicePortalDetectionTest, NotPortal) { |
| 667 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 668 | .WillOnce(Return(true)); |
| 669 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 670 | SetServiceConnectedState(Service::kStateOnline); |
| 671 | ExpectPortalDetectorReset(); |
| 672 | } |
| 673 | |
| 674 | TEST_F(DevicePortalDetectionTest, NotDefault) { |
| 675 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 676 | .WillOnce(Return(true)); |
| 677 | EXPECT_CALL(*connection_.get(), is_default()) |
| 678 | .WillOnce(Return(false)); |
| 679 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 680 | SetServiceConnectedState(Service::kStatePortal); |
| 681 | ExpectPortalDetectorReset(); |
| 682 | } |
| 683 | |
| 684 | TEST_F(DevicePortalDetectionTest, PortalIntervalIsZero) { |
| 685 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 686 | .WillOnce(Return(true)); |
| 687 | EXPECT_CALL(*connection_.get(), is_default()) |
| 688 | .WillOnce(Return(true)); |
| 689 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 690 | .WillOnce(Return(0)); |
| 691 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 692 | SetServiceConnectedState(Service::kStatePortal); |
| 693 | ExpectPortalDetectorReset(); |
| 694 | } |
| 695 | |
| 696 | TEST_F(DevicePortalDetectionTest, RestartPortalDetection) { |
| 697 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 698 | .WillOnce(Return(true)); |
| 699 | EXPECT_CALL(*connection_.get(), is_default()) |
| 700 | .WillOnce(Return(true)); |
| 701 | const int kPortalDetectionInterval = 10; |
| 702 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 703 | .Times(AtLeast(1)) |
| 704 | .WillRepeatedly(Return(kPortalDetectionInterval)); |
| 705 | const string kPortalCheckURL("http://portal"); |
| 706 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 707 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 708 | EXPECT_CALL(*portal_detector_, StartAfterDelay(kPortalCheckURL, |
| 709 | kPortalDetectionInterval)) |
| 710 | .WillOnce(Return(true)); |
| 711 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 712 | SetServiceConnectedState(Service::kStatePortal); |
| 713 | ExpectPortalDetectorSet(); |
| 714 | } |
| 715 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 716 | } // namespace shill |