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