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