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" |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 38 | #include "shill/static_ip_parameters.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::_; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 45 | using ::testing::AnyNumber; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 46 | using ::testing::AtLeast; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 47 | using ::testing::Mock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 48 | using ::testing::NiceMock; |
| 49 | using ::testing::Return; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 50 | using ::testing::ReturnRef; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 51 | using ::testing::StrictMock; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | using ::testing::Test; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 53 | using ::testing::Values; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 54 | |
| 55 | namespace shill { |
| 56 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 57 | class TestDevice : public Device { |
| 58 | public: |
| 59 | TestDevice(ControlInterface *control_interface, |
| 60 | EventDispatcher *dispatcher, |
| 61 | Metrics *metrics, |
| 62 | Manager *manager, |
| 63 | const std::string &link_name, |
| 64 | const std::string &address, |
| 65 | int interface_index, |
| 66 | Technology::Identifier technology) |
| 67 | : Device(control_interface, dispatcher, metrics, manager, link_name, |
| 68 | address, interface_index, technology) {} |
| 69 | ~TestDevice() {} |
| 70 | virtual void Start(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 71 | const EnabledStateChangedCallback &callback) { |
| 72 | DCHECK(error); |
| 73 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 74 | virtual void Stop(Error *error, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 75 | const EnabledStateChangedCallback &callback) { |
| 76 | DCHECK(error); |
| 77 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 80 | class DeviceTest : public PropertyStoreTest { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 81 | public: |
| 82 | DeviceTest() |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 83 | : device_(new TestDevice(control_interface(), |
| 84 | dispatcher(), |
| 85 | NULL, |
| 86 | manager(), |
| 87 | kDeviceName, |
| 88 | kDeviceAddress, |
| 89 | kDeviceInterfaceIndex, |
| 90 | Technology::kUnknown)), |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 91 | device_info_(control_interface(), NULL, NULL, NULL) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 92 | DHCPProvider::GetInstance()->glib_ = glib(); |
| 93 | DHCPProvider::GetInstance()->control_interface_ = control_interface(); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 94 | DHCPProvider::GetInstance()->dispatcher_ = dispatcher(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 95 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | virtual ~DeviceTest() {} |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 97 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 98 | virtual void SetUp() { |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 99 | device_->metrics_ = &metrics_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 100 | device_->rtnl_handler_ = &rtnl_handler_; |
| 101 | } |
| 102 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 103 | protected: |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 104 | static const char kDeviceName[]; |
| 105 | static const char kDeviceAddress[]; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 106 | static const int kDeviceInterfaceIndex; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 107 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 108 | void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
| 109 | device_->OnIPConfigUpdated(ipconfig, success); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void SelectService(const ServiceRefPtr service) { |
| 113 | device_->SelectService(service); |
| 114 | } |
| 115 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 116 | void SetConnection(ConnectionRefPtr connection) { |
| 117 | device_->connection_ = connection; |
| 118 | } |
| 119 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 120 | MockControl control_interface_; |
| 121 | DeviceRefPtr device_; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 122 | MockDeviceInfo device_info_; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 123 | MockMetrics metrics_; |
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 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 286 | TEST_F(DeviceTest, IPConfigUpdatedFailureWithStatic) { |
| 287 | scoped_refptr<MockService> service( |
| 288 | new StrictMock<MockService>(control_interface(), |
| 289 | dispatcher(), |
| 290 | metrics(), |
| 291 | manager())); |
| 292 | SelectService(service); |
| 293 | service->static_ip_parameters_.args_.SetString( |
| 294 | flimflam::kAddressProperty, "1.1.1.1"); |
| 295 | service->static_ip_parameters_.args_.SetInt(flimflam::kPrefixlenProperty, 16); |
| 296 | EXPECT_CALL(*service.get(), SetState(_)).Times(0); |
| 297 | EXPECT_CALL(*service.get(), SetConnection(_)).Times(0); |
| 298 | OnIPConfigUpdated(NULL, false); |
| 299 | } |
| 300 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 301 | TEST_F(DeviceTest, IPConfigUpdatedSuccess) { |
| 302 | scoped_refptr<MockService> service( |
| 303 | new StrictMock<MockService>(control_interface(), |
| 304 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 305 | metrics(), |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 306 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 307 | SelectService(service); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 308 | scoped_refptr<MockIPConfig> ipconfig = new MockIPConfig(control_interface(), |
| 309 | kDeviceName); |
| 310 | EXPECT_CALL(*service.get(), SetState(Service::kStateConnected)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 311 | EXPECT_CALL(*service.get(), IsConnected()) |
| 312 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 313 | EXPECT_CALL(*service.get(), IsPortalDetectionDisabled()) |
| 314 | .WillRepeatedly(Return(true)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 315 | EXPECT_CALL(*service.get(), SetState(Service::kStateOnline)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 316 | EXPECT_CALL(*service.get(), SetConnection(NotNullRefPtr())); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 317 | OnIPConfigUpdated(ipconfig.get(), true); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 320 | TEST_F(DeviceTest, Start) { |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 321 | EXPECT_FALSE(device_->running_); |
| 322 | EXPECT_FALSE(device_->enabled_); |
| 323 | EXPECT_FALSE(device_->enabled_pending_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 324 | device_->SetEnabled(true); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 325 | EXPECT_TRUE(device_->running_); |
| 326 | EXPECT_TRUE(device_->enabled_pending_); |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 327 | device_->OnEnabledStateChanged(ResultCallback(), |
| 328 | Error(Error::kOperationFailed)); |
| 329 | EXPECT_FALSE(device_->enabled_pending_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 330 | } |
| 331 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 332 | TEST_F(DeviceTest, Stop) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 333 | device_->enabled_ = true; |
| 334 | device_->enabled_pending_ = true; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 335 | device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName); |
| 336 | scoped_refptr<MockService> service( |
| 337 | new NiceMock<MockService>(&control_interface_, |
| 338 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 339 | metrics(), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 340 | manager())); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 341 | SelectService(service); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 342 | |
| 343 | EXPECT_CALL(*service.get(), state()). |
| 344 | WillRepeatedly(Return(Service::kStateConnected)); |
| 345 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 346 | UpdateEnabled()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 347 | EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()), |
| 348 | EmitBoolChanged(flimflam::kPoweredProperty, false)); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 349 | EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 350 | device_->SetEnabled(false); |
| 351 | device_->OnEnabledStateChanged(ResultCallback(), Error()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 352 | |
| 353 | EXPECT_FALSE(device_->ipconfig_.get()); |
| 354 | EXPECT_FALSE(device_->selected_service_.get()); |
| 355 | } |
| 356 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 357 | class DevicePortalDetectionTest : public DeviceTest { |
| 358 | public: |
| 359 | DevicePortalDetectionTest() |
| 360 | : connection_(new StrictMock<MockConnection>(&device_info_)), |
| 361 | manager_(control_interface(), |
| 362 | dispatcher(), |
| 363 | metrics(), |
| 364 | glib()), |
| 365 | service_(new StrictMock<MockService>(control_interface(), |
| 366 | dispatcher(), |
| 367 | metrics(), |
| 368 | &manager_)), |
| 369 | portal_detector_(new StrictMock<MockPortalDetector>(connection_)) {} |
| 370 | virtual ~DevicePortalDetectionTest() {} |
| 371 | virtual void SetUp() { |
| 372 | DeviceTest::SetUp(); |
| 373 | SelectService(service_); |
| 374 | SetConnection(connection_.get()); |
| 375 | device_->portal_detector_.reset(portal_detector_); // Passes ownership. |
| 376 | device_->manager_ = &manager_; |
| 377 | } |
| 378 | |
| 379 | protected: |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 380 | static const int kPortalAttempts; |
| 381 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 382 | bool StartPortalDetection() { return device_->StartPortalDetection(); } |
| 383 | void StopPortalDetection() { device_->StopPortalDetection(); } |
| 384 | |
| 385 | void PortalDetectorCallback(const PortalDetector::Result &result) { |
| 386 | device_->PortalDetectorCallback(result); |
| 387 | } |
| 388 | bool RequestPortalDetection() { |
| 389 | return device_->RequestPortalDetection(); |
| 390 | } |
| 391 | void SetServiceConnectedState(Service::ConnectState state) { |
| 392 | device_->SetServiceConnectedState(state); |
| 393 | } |
| 394 | void ExpectPortalDetectorReset() { |
| 395 | EXPECT_FALSE(device_->portal_detector_.get()); |
| 396 | } |
| 397 | void ExpectPortalDetectorSet() { |
| 398 | EXPECT_TRUE(device_->portal_detector_.get()); |
| 399 | } |
| 400 | void ExpectPortalDetectorIsMock() { |
| 401 | EXPECT_EQ(portal_detector_, device_->portal_detector_.get()); |
| 402 | } |
| 403 | scoped_refptr<MockConnection> connection_; |
| 404 | StrictMock<MockManager> manager_; |
| 405 | scoped_refptr<MockService> service_; |
| 406 | |
| 407 | // Used only for EXPECT_CALL(). Object is owned by device. |
| 408 | MockPortalDetector *portal_detector_; |
| 409 | }; |
| 410 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 411 | const int DevicePortalDetectionTest::kPortalAttempts = 2; |
| 412 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 413 | TEST_F(DevicePortalDetectionTest, ServicePortalDetectionDisabled) { |
| 414 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 415 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 416 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 417 | .WillRepeatedly(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 418 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 419 | EXPECT_FALSE(StartPortalDetection()); |
| 420 | } |
| 421 | |
| 422 | TEST_F(DevicePortalDetectionTest, TechnologyPortalDetectionDisabled) { |
| 423 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 424 | .WillOnce(Return(false)); |
| 425 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 426 | .WillRepeatedly(Return(true)); |
| 427 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 428 | .WillOnce(Return(true)); |
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(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 431 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 432 | EXPECT_FALSE(StartPortalDetection()); |
| 433 | } |
| 434 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 435 | TEST_F(DevicePortalDetectionTest, PortalDetectionProxyConfig) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 436 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 437 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 438 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 439 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 440 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 441 | .WillOnce(Return(true)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 442 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 443 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 444 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 445 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 446 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 447 | EXPECT_FALSE(StartPortalDetection()); |
| 448 | } |
| 449 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 450 | TEST_F(DevicePortalDetectionTest, PortalDetectionBadUrl) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 451 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 452 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 453 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 454 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 455 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 456 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 457 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 458 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 459 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 460 | .WillOnce(Return(true)); |
| 461 | const string portal_url; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 462 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 463 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 464 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 465 | EXPECT_FALSE(StartPortalDetection()); |
| 466 | } |
| 467 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 468 | TEST_F(DevicePortalDetectionTest, PortalDetectionStart) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 469 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 470 | .WillOnce(Return(false)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 471 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 472 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 473 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 474 | .WillOnce(Return(false)); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 475 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 476 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 477 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 478 | .WillOnce(Return(true)); |
| 479 | const string portal_url(PortalDetector::kDefaultURL); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 480 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 481 | .WillRepeatedly(ReturnRef(portal_url)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 482 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 483 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 484 | const string kInterfaceName("int0"); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 485 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 486 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 487 | const vector<string> kDNSServers; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 488 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 489 | .WillRepeatedly(ReturnRef(kDNSServers)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 490 | EXPECT_TRUE(StartPortalDetection()); |
| 491 | |
| 492 | // Drop all references to device_info before it falls out of scope. |
| 493 | SetConnection(NULL); |
| 494 | StopPortalDetection(); |
| 495 | } |
| 496 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 497 | TEST_F(DevicePortalDetectionTest, PortalDetectionNonFinal) { |
| 498 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 499 | .Times(0); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 500 | EXPECT_CALL(*service_.get(), SetState(_)) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 501 | .Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 502 | PortalDetectorCallback(PortalDetector::Result( |
| 503 | PortalDetector::kPhaseUnknown, |
| 504 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 505 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 506 | false)); |
| 507 | } |
| 508 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 509 | TEST_F(DevicePortalDetectionTest, PortalDetectionFailure) { |
| 510 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 511 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 512 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 513 | EXPECT_CALL(metrics_, |
| 514 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 515 | Metrics::kPortalResultConnectionFailure, |
| 516 | Metrics::kPortalResultMax)); |
| 517 | EXPECT_CALL(metrics_, |
| 518 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 519 | _, _, _, _)).Times(0); |
| 520 | EXPECT_CALL(metrics_, |
| 521 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 522 | kPortalAttempts, |
| 523 | Metrics::kMetricPortalAttemptsMin, |
| 524 | Metrics::kMetricPortalAttemptsMax, |
| 525 | Metrics::kMetricPortalAttemptsNumBuckets)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 526 | EXPECT_CALL(*connection_.get(), is_default()) |
| 527 | .WillOnce(Return(false)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 528 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 529 | PortalDetector::kPhaseConnection, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 530 | PortalDetector::kStatusFailure, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 531 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 532 | true)); |
| 533 | } |
| 534 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 535 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccess) { |
| 536 | EXPECT_CALL(*service_.get(), IsConnected()) |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 537 | .WillOnce(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 538 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 539 | EXPECT_CALL(metrics_, |
| 540 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 541 | Metrics::kPortalResultSuccess, |
| 542 | Metrics::kPortalResultMax)); |
| 543 | EXPECT_CALL(metrics_, |
| 544 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 545 | kPortalAttempts, |
| 546 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 547 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 548 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 549 | EXPECT_CALL(metrics_, |
| 550 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 551 | _, _, _, _)).Times(0); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 552 | PortalDetectorCallback(PortalDetector::Result( |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 553 | PortalDetector::kPhaseContent, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 554 | PortalDetector::kStatusSuccess, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 555 | kPortalAttempts, |
| 556 | true)); |
| 557 | } |
| 558 | |
| 559 | TEST_F(DevicePortalDetectionTest, PortalDetectionSuccessAfterFailure) { |
| 560 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 561 | .WillRepeatedly(Return(true)); |
| 562 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 563 | EXPECT_CALL(metrics_, |
| 564 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 565 | Metrics::kPortalResultConnectionFailure, |
| 566 | Metrics::kPortalResultMax)); |
| 567 | EXPECT_CALL(metrics_, |
| 568 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 569 | _, _, _, _)).Times(0); |
| 570 | EXPECT_CALL(metrics_, |
| 571 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 572 | kPortalAttempts, |
| 573 | Metrics::kMetricPortalAttemptsMin, |
| 574 | Metrics::kMetricPortalAttemptsMax, |
| 575 | Metrics::kMetricPortalAttemptsNumBuckets)); |
| 576 | EXPECT_CALL(*connection_.get(), is_default()) |
| 577 | .WillOnce(Return(false)); |
| 578 | PortalDetectorCallback(PortalDetector::Result( |
| 579 | PortalDetector::kPhaseConnection, |
| 580 | PortalDetector::kStatusFailure, |
| 581 | kPortalAttempts, |
| 582 | true)); |
| 583 | Mock::VerifyAndClearExpectations(&metrics_); |
| 584 | |
| 585 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 586 | EXPECT_CALL(metrics_, |
| 587 | SendEnumToUMA("Network.Shill.Unknown.PortalResult", |
| 588 | Metrics::kPortalResultSuccess, |
| 589 | Metrics::kPortalResultMax)); |
| 590 | EXPECT_CALL(metrics_, |
| 591 | SendToUMA("Network.Shill.Unknown.PortalAttemptsToOnline", |
| 592 | kPortalAttempts * 2, |
| 593 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 594 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 595 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets)); |
| 596 | EXPECT_CALL(metrics_, |
| 597 | SendToUMA("Network.Shill.Unknown.PortalAttempts", |
| 598 | _, _, _, _)).Times(0); |
| 599 | PortalDetectorCallback(PortalDetector::Result( |
| 600 | PortalDetector::kPhaseContent, |
| 601 | PortalDetector::kStatusSuccess, |
| 602 | kPortalAttempts, |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 603 | true)); |
| 604 | } |
| 605 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 606 | TEST_F(DevicePortalDetectionTest, RequestPortalDetection) { |
| 607 | EXPECT_CALL(*service_.get(), state()) |
| 608 | .WillOnce(Return(Service::kStateOnline)) |
| 609 | .WillRepeatedly(Return(Service::kStatePortal)); |
| 610 | EXPECT_FALSE(RequestPortalDetection()); |
| 611 | |
| 612 | EXPECT_CALL(*connection_.get(), is_default()) |
| 613 | .WillOnce(Return(false)) |
| 614 | .WillRepeatedly(Return(true)); |
| 615 | EXPECT_FALSE(RequestPortalDetection()); |
| 616 | |
| 617 | EXPECT_CALL(*portal_detector_, IsInProgress()) |
| 618 | .WillOnce(Return(true)); |
| 619 | // Portal detection already running. |
| 620 | EXPECT_TRUE(RequestPortalDetection()); |
| 621 | |
| 622 | // Make sure our running mock portal detector was not replaced. |
| 623 | ExpectPortalDetectorIsMock(); |
| 624 | |
| 625 | // Throw away our pre-fabricated portal detector, and have the device create |
| 626 | // a new one. |
| 627 | StopPortalDetection(); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 628 | EXPECT_CALL(*service_.get(), IsPortalDetectionDisabled()) |
| 629 | .WillRepeatedly(Return(false)); |
| 630 | EXPECT_CALL(*service_.get(), IsPortalDetectionAuto()) |
| 631 | .WillRepeatedly(Return(true)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 632 | EXPECT_CALL(manager_, IsPortalDetectionEnabled(device_->technology())) |
| 633 | .WillRepeatedly(Return(true)); |
| 634 | EXPECT_CALL(*service_.get(), HasProxyConfig()) |
| 635 | .WillRepeatedly(Return(false)); |
| 636 | const string kPortalCheckURL("http://portal"); |
| 637 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 638 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 639 | const string kInterfaceName("int0"); |
| 640 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 641 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
| 642 | const vector<string> kDNSServers; |
| 643 | EXPECT_CALL(*connection_.get(), dns_servers()) |
| 644 | .WillRepeatedly(ReturnRef(kDNSServers)); |
| 645 | EXPECT_TRUE(RequestPortalDetection()); |
| 646 | } |
| 647 | |
| 648 | TEST_F(DevicePortalDetectionTest, NotConnected) { |
| 649 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 650 | .WillOnce(Return(false)); |
| 651 | SetServiceConnectedState(Service::kStatePortal); |
| 652 | // We don't check for the portal detector to be reset here, because |
| 653 | // it would have been reset as a part of disconnection. |
| 654 | } |
| 655 | |
| 656 | TEST_F(DevicePortalDetectionTest, NotPortal) { |
| 657 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 658 | .WillOnce(Return(true)); |
| 659 | EXPECT_CALL(*service_.get(), SetState(Service::kStateOnline)); |
| 660 | SetServiceConnectedState(Service::kStateOnline); |
| 661 | ExpectPortalDetectorReset(); |
| 662 | } |
| 663 | |
| 664 | TEST_F(DevicePortalDetectionTest, NotDefault) { |
| 665 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 666 | .WillOnce(Return(true)); |
| 667 | EXPECT_CALL(*connection_.get(), is_default()) |
| 668 | .WillOnce(Return(false)); |
| 669 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 670 | SetServiceConnectedState(Service::kStatePortal); |
| 671 | ExpectPortalDetectorReset(); |
| 672 | } |
| 673 | |
| 674 | TEST_F(DevicePortalDetectionTest, PortalIntervalIsZero) { |
| 675 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 676 | .WillOnce(Return(true)); |
| 677 | EXPECT_CALL(*connection_.get(), is_default()) |
| 678 | .WillOnce(Return(true)); |
| 679 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 680 | .WillOnce(Return(0)); |
| 681 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 682 | SetServiceConnectedState(Service::kStatePortal); |
| 683 | ExpectPortalDetectorReset(); |
| 684 | } |
| 685 | |
| 686 | TEST_F(DevicePortalDetectionTest, RestartPortalDetection) { |
| 687 | EXPECT_CALL(*service_.get(), IsConnected()) |
| 688 | .WillOnce(Return(true)); |
| 689 | EXPECT_CALL(*connection_.get(), is_default()) |
| 690 | .WillOnce(Return(true)); |
| 691 | const int kPortalDetectionInterval = 10; |
| 692 | EXPECT_CALL(manager_, GetPortalCheckInterval()) |
| 693 | .Times(AtLeast(1)) |
| 694 | .WillRepeatedly(Return(kPortalDetectionInterval)); |
| 695 | const string kPortalCheckURL("http://portal"); |
| 696 | EXPECT_CALL(manager_, GetPortalCheckURL()) |
| 697 | .WillOnce(ReturnRef(kPortalCheckURL)); |
| 698 | EXPECT_CALL(*portal_detector_, StartAfterDelay(kPortalCheckURL, |
| 699 | kPortalDetectionInterval)) |
| 700 | .WillOnce(Return(true)); |
| 701 | EXPECT_CALL(*service_.get(), SetState(Service::kStatePortal)); |
| 702 | SetServiceConnectedState(Service::kStatePortal); |
| 703 | ExpectPortalDetectorSet(); |
| 704 | } |
| 705 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 706 | } // namespace shill |