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