Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/connection_health_checker.h" |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/bind.h> |
| 12 | #include <base/callback.h> |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 13 | #include <base/cancelable_callback.h> |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_vector.h> |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "shill/mock_async_connection.h" |
| 19 | #include "shill/mock_connection.h" |
| 20 | #include "shill/mock_control.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 21 | #include "shill/mock_device_info.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 22 | #include "shill/mock_dns_client.h" |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 23 | #include "shill/mock_dns_client_factory.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 24 | #include "shill/mock_event_dispatcher.h" |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 25 | #include "shill/mock_ip_address_store.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 26 | #include "shill/mock_socket_info_reader.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 27 | #include "shill/mock_sockets.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 28 | |
| 29 | using base::Bind; |
| 30 | using base::Callback; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 31 | using base::Closure; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 32 | using base::Unretained; |
| 33 | using std::string; |
| 34 | using std::vector; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 35 | using ::testing::AtLeast; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 36 | using ::testing::DoAll; |
| 37 | using ::testing::Gt; |
| 38 | using ::testing::Invoke; |
| 39 | using ::testing::Mock; |
| 40 | using ::testing::NiceMock; |
| 41 | using ::testing::Return; |
| 42 | using ::testing::ReturnRef; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 43 | using ::testing::SaveArg; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 44 | using ::testing::Sequence; |
| 45 | using ::testing::SetArgumentPointee; |
| 46 | using ::testing::StrictMock; |
| 47 | using ::testing::Test; |
| 48 | using ::testing::_; |
| 49 | |
| 50 | namespace shill { |
| 51 | |
| 52 | namespace { |
| 53 | const char kInterfaceName[] = "int0"; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 54 | const char kIPAddress_8_8_8_8[] = "8.8.8.8"; |
| 55 | const char kProxyIPAddressRemote[] = "74.125.224.84"; |
| 56 | const char kProxyIPAddressLocal[] = "192.23.34.1"; |
| 57 | const char kProxyIPv6AddressLocal[] = "::ffff:192.23.34.1"; |
| 58 | const char kProxyURLRemote[] = "http://www.google.com"; |
| 59 | const int kProxyFD = 100; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 60 | const int16_t kProxyPortLocal = 5540; |
| 61 | const int16_t kProxyPortRemote = 80; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 62 | } // namespace |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 63 | |
| 64 | MATCHER_P(IsSameIPAddress, ip_addr, "") { |
| 65 | return arg.Equals(ip_addr); |
| 66 | } |
| 67 | |
| 68 | class ConnectionHealthCheckerTest : public Test { |
| 69 | public: |
| 70 | ConnectionHealthCheckerTest() |
| 71 | : interface_name_(kInterfaceName), |
| 72 | device_info_(&control_, &dispatcher_, |
| 73 | reinterpret_cast<Metrics*>(NULL), |
| 74 | reinterpret_cast<Manager*>(NULL)), |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 75 | connection_(new NiceMock<MockConnection>(&device_info_)), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 76 | socket_(NULL) {} |
| 77 | |
| 78 | // Invokes |
| 79 | int GetSockName(int fd, struct sockaddr *addr_out, socklen_t *sockaddr_size) { |
| 80 | struct sockaddr_in addr; |
| 81 | EXPECT_EQ(kProxyFD, fd); |
| 82 | EXPECT_LE(sizeof(sockaddr_in), *sockaddr_size); |
| 83 | addr.sin_family = AF_INET; |
| 84 | inet_pton(AF_INET, kProxyIPAddressLocal, &addr.sin_addr); |
| 85 | addr.sin_port = htons(kProxyPortLocal); |
| 86 | memcpy(addr_out, &addr, sizeof(addr)); |
| 87 | *sockaddr_size = sizeof(sockaddr_in); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int GetSockNameReturnsIPv6(int fd, struct sockaddr *addr_out, |
| 92 | socklen_t *sockaddr_size) { |
| 93 | struct sockaddr_in6 addr; |
| 94 | EXPECT_EQ(kProxyFD, fd); |
| 95 | EXPECT_LE(sizeof(sockaddr_in6), *sockaddr_size); |
| 96 | addr.sin6_family = AF_INET6; |
| 97 | inet_pton(AF_INET6, kProxyIPv6AddressLocal, &addr.sin6_addr); |
| 98 | addr.sin6_port = htons(kProxyPortLocal); |
| 99 | memcpy(addr_out, &addr, sizeof(addr)); |
| 100 | *sockaddr_size = sizeof(sockaddr_in6); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | void InvokeOnConnectionComplete(bool success, int sock_fd) { |
| 105 | health_checker_->OnConnectionComplete(success, sock_fd); |
| 106 | } |
| 107 | |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 108 | void InvokeGetDNSResultFailure() { |
| 109 | Error error(Error::kOperationFailed, ""); |
| 110 | IPAddress address(IPAddress::kFamilyUnknown); |
| 111 | health_checker_->GetDNSResult(error, address); |
| 112 | } |
| 113 | |
| 114 | void InvokeGetDNSResultSuccess(const IPAddress &address) { |
| 115 | Error error; |
| 116 | health_checker_->GetDNSResult(error, address); |
| 117 | } |
| 118 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 119 | protected: |
| 120 | void SetUp() { |
| 121 | EXPECT_CALL(*connection_.get(), interface_name()) |
| 122 | .WillRepeatedly(ReturnRef(interface_name_)); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 123 | ON_CALL(*connection_.get(), dns_servers()) |
| 124 | .WillByDefault(ReturnRef(dns_servers_)); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 125 | // ConnectionHealthChecker constructor should add some IPs |
| 126 | EXPECT_CALL(remote_ips_, AddUnique(_)).Times(AtLeast(1)); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 127 | health_checker_.reset( |
| 128 | new ConnectionHealthChecker( |
| 129 | connection_, |
| 130 | &dispatcher_, |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 131 | &remote_ips_, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 132 | Bind(&ConnectionHealthCheckerTest::ResultCallbackTarget, |
| 133 | Unretained(this)))); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 134 | Mock::VerifyAndClearExpectations(&remote_ips_); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 135 | socket_ = new StrictMock<MockSockets>(); |
| 136 | tcp_connection_ = new StrictMock<MockAsyncConnection>(); |
| 137 | socket_info_reader_ = new StrictMock<MockSocketInfoReader>(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 138 | // Passes ownership for all of these. |
| 139 | health_checker_->socket_.reset(socket_); |
| 140 | health_checker_->tcp_connection_.reset(tcp_connection_); |
| 141 | health_checker_->socket_info_reader_.reset(socket_info_reader_); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 142 | health_checker_->dns_client_factory_ = MockDNSClientFactory::GetInstance(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void TearDown() { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 146 | ExpectStop(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Accessors for private data in ConnectionHealthChecker. |
| 150 | const Sockets *socket() { |
| 151 | return health_checker_->socket_.get(); |
| 152 | } |
| 153 | const AsyncConnection *tcp_connection() { |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 154 | return health_checker_->tcp_connection_.get(); |
| 155 | } |
| 156 | ScopedVector<DNSClient> &dns_clients() { |
| 157 | return health_checker_->dns_clients_; |
| 158 | } |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 159 | int NumDNSQueries() { |
| 160 | return ConnectionHealthChecker::kNumDNSQueries; |
| 161 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 162 | int MaxFailedConnectionAttempts() { |
| 163 | return ConnectionHealthChecker::kMaxFailedConnectionAttempts; |
| 164 | } |
| 165 | int MaxSentDataPollingAttempts() { |
| 166 | return ConnectionHealthChecker::kMaxSentDataPollingAttempts; |
| 167 | } |
| 168 | int MinCongestedQueueAttempts() { |
| 169 | return ConnectionHealthChecker::kMinCongestedQueueAttempts; |
| 170 | } |
| 171 | int MinSuccessfulSendAttempts() { |
| 172 | return ConnectionHealthChecker::kMinSuccessfulSendAttempts; |
| 173 | } |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 174 | void SetTCPStateUpdateWaitMilliseconds(int new_wait) { |
| 175 | health_checker_->tcp_state_update_wait_milliseconds_ = new_wait; |
| 176 | } |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 177 | |
| 178 | // Mock Callbacks |
| 179 | MOCK_METHOD1(ResultCallbackTarget, |
| 180 | void(ConnectionHealthChecker::Result result)); |
| 181 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 182 | // Helper methods |
| 183 | IPAddress StringToIPv4Address(const string &address_string) { |
| 184 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 185 | EXPECT_TRUE(ip_address.SetAddressFromString(address_string)); |
| 186 | return ip_address; |
| 187 | } |
| 188 | // Naming: CreateSocketInfo |
| 189 | // + (Proxy/Other) : TCP connection for proxy socket / some other |
| 190 | // socket. |
| 191 | // + arg1: Pass in any SocketInfo::ConnectionState you want. |
| 192 | // + arg2: Pass in any value of transmit_queue_value you want. |
| 193 | SocketInfo CreateSocketInfoOther() { |
| 194 | return SocketInfo( |
| 195 | SocketInfo::kConnectionStateUnknown, |
| 196 | StringToIPv4Address(kIPAddress_8_8_8_8), |
| 197 | 0, |
| 198 | StringToIPv4Address(kProxyIPAddressRemote), |
| 199 | kProxyPortRemote, |
| 200 | 0, |
| 201 | 0, |
| 202 | SocketInfo::kTimerStateUnknown); |
| 203 | } |
| 204 | SocketInfo CreateSocketInfoProxy(SocketInfo::ConnectionState state) { |
| 205 | return SocketInfo( |
| 206 | state, |
| 207 | StringToIPv4Address(kProxyIPAddressLocal), |
| 208 | kProxyPortLocal, |
| 209 | StringToIPv4Address(kProxyIPAddressRemote), |
| 210 | kProxyPortRemote, |
| 211 | 0, |
| 212 | 0, |
| 213 | SocketInfo::kTimerStateUnknown); |
| 214 | } |
| 215 | SocketInfo CreateSocketInfoProxy(SocketInfo::ConnectionState state, |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 216 | SocketInfo::TimerState timer_state, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 217 | uint64_t transmit_queue_value) { |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 218 | return SocketInfo( |
| 219 | state, |
| 220 | StringToIPv4Address(kProxyIPAddressLocal), |
| 221 | kProxyPortLocal, |
| 222 | StringToIPv4Address(kProxyIPAddressRemote), |
| 223 | kProxyPortRemote, |
| 224 | transmit_queue_value, |
| 225 | 0, |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 226 | timer_state); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 227 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 228 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 229 | |
| 230 | // Expectations |
| 231 | void ExpectReset() { |
| 232 | EXPECT_EQ(connection_.get(), health_checker_->connection_.get()); |
| 233 | EXPECT_EQ(&dispatcher_, health_checker_->dispatcher_); |
| 234 | EXPECT_EQ(socket_, health_checker_->socket_.get()); |
| 235 | EXPECT_FALSE(socket_ == NULL); |
| 236 | EXPECT_EQ(socket_info_reader_, health_checker_->socket_info_reader_.get()); |
| 237 | EXPECT_FALSE(socket_info_reader_ == NULL); |
| 238 | EXPECT_FALSE(health_checker_->connection_complete_callback_.is_null()); |
| 239 | EXPECT_EQ(tcp_connection_, health_checker_->tcp_connection_.get()); |
| 240 | EXPECT_FALSE(tcp_connection_ == NULL); |
| 241 | EXPECT_FALSE(health_checker_->health_check_in_progress_); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 242 | } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 243 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 244 | // Setup ConnectionHealthChecker::GetSocketInfo to return sock_info. |
| 245 | // This only works if GetSocketInfo is called with kProxyFD. |
| 246 | // If no matching sock_info is provided (Does not belong to proxy socket), |
| 247 | // GetSocketInfo will (correctly) return false. |
| 248 | void ExpectGetSocketInfoReturns(SocketInfo sock_info) { |
| 249 | vector<SocketInfo> info_list; |
| 250 | info_list.push_back(sock_info); |
| 251 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 252 | .InSequence(seq_) |
| 253 | .WillOnce(Invoke(this, |
| 254 | &ConnectionHealthCheckerTest::GetSockName)); |
| 255 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 256 | .InSequence(seq_) |
| 257 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 258 | Return(true))); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 259 | } |
| 260 | void ExpectSuccessfulStart() { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 261 | EXPECT_CALL(remote_ips_, Empty()).WillRepeatedly(Return(false)); |
| 262 | EXPECT_CALL(remote_ips_, GetRandomIP()) |
| 263 | .WillRepeatedly(Return(StringToIPv4Address(kProxyIPAddressRemote))); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 264 | EXPECT_CALL( |
| 265 | *tcp_connection_, |
| 266 | Start(IsSameIPAddress(StringToIPv4Address(kProxyIPAddressRemote)), |
| 267 | kProxyPortRemote)) |
| 268 | .InSequence(seq_) |
| 269 | .WillOnce(Return(true)); |
| 270 | } |
| 271 | void ExpectRetry() { |
| 272 | EXPECT_CALL(*socket_, Close(kProxyFD)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 273 | .InSequence(seq_); |
| 274 | EXPECT_CALL( |
| 275 | *tcp_connection_, |
| 276 | Start(IsSameIPAddress(StringToIPv4Address(kProxyIPAddressRemote)), |
| 277 | kProxyPortRemote)) |
| 278 | .InSequence(seq_) |
| 279 | .WillOnce(Return(true)); |
| 280 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 281 | void ExpectStop() { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 282 | if (tcp_connection_) |
| 283 | EXPECT_CALL(*tcp_connection_, Stop()) |
| 284 | .InSequence(seq_); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 285 | } |
| 286 | void ExpectCleanUp() { |
| 287 | EXPECT_CALL(*socket_, Close(kProxyFD)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 288 | .InSequence(seq_); |
| 289 | EXPECT_CALL(*tcp_connection_, Stop()) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 290 | .InSequence(seq_); |
| 291 | } |
| 292 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 293 | void VerifyAndClearAllExpectations() { |
| 294 | Mock::VerifyAndClearExpectations(this); |
| 295 | Mock::VerifyAndClearExpectations(tcp_connection_); |
| 296 | Mock::VerifyAndClearExpectations(socket_); |
| 297 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 298 | } |
| 299 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 300 | // Needed for other mocks, but not for the tests directly. |
| 301 | const string interface_name_; |
| 302 | NiceMock<MockControl> control_; |
| 303 | NiceMock<MockDeviceInfo> device_info_; |
| 304 | vector<string> dns_servers_; |
| 305 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 306 | scoped_refptr<NiceMock<MockConnection> > connection_; |
| 307 | EventDispatcher dispatcher_; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 308 | MockIPAddressStore remote_ips_; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 309 | StrictMock<MockSockets> *socket_; |
| 310 | StrictMock<MockSocketInfoReader> *socket_info_reader_; |
| 311 | StrictMock<MockAsyncConnection> *tcp_connection_; |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 312 | // Expectations in the Expect* functions are put in this sequence. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 313 | // This allows us to chain calls to Expect* functions. |
| 314 | Sequence seq_; |
| 315 | |
| 316 | scoped_ptr<ConnectionHealthChecker> health_checker_; |
| 317 | }; |
| 318 | |
| 319 | TEST_F(ConnectionHealthCheckerTest, Constructor) { |
| 320 | ExpectReset(); |
| 321 | } |
| 322 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 323 | TEST_F(ConnectionHealthCheckerTest, SetConnection) { |
| 324 | scoped_refptr<NiceMock<MockConnection> > new_connection |
| 325 | = new NiceMock<MockConnection>(&device_info_); |
| 326 | // If a health check was in progress when SetConnection is called, verify |
| 327 | // that it restarts with the new connection. |
| 328 | ExpectSuccessfulStart(); |
| 329 | health_checker_->Start(); |
| 330 | VerifyAndClearAllExpectations(); |
Paul Stewart | 41a071e | 2013-04-26 07:56:04 -0700 | [diff] [blame] | 331 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 332 | EXPECT_CALL(remote_ips_, Empty()).WillRepeatedly(Return(true)); |
| 333 | EXPECT_CALL(*new_connection.get(), interface_name()) |
| 334 | .WillRepeatedly(ReturnRef(interface_name_)); |
| 335 | EXPECT_CALL(*this, |
| 336 | ResultCallbackTarget(ConnectionHealthChecker::kResultUnknown)); |
| 337 | health_checker_->SetConnection(new_connection); |
| 338 | EXPECT_NE(tcp_connection_, health_checker_->tcp_connection()); |
| 339 | EXPECT_EQ(new_connection.get(), health_checker_->connection()); |
Paul Stewart | 41a071e | 2013-04-26 07:56:04 -0700 | [diff] [blame] | 340 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 341 | // health_checker_ has reset tcp_connection_ to a new object. |
| 342 | // Since it owned tcp_connection_, the object has been destroyed. |
| 343 | tcp_connection_ = NULL; |
Paul Stewart | 41a071e | 2013-04-26 07:56:04 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 346 | TEST_F(ConnectionHealthCheckerTest, GarbageCollectDNSClients) { |
| 347 | dns_clients().clear(); |
| 348 | health_checker_->GarbageCollectDNSClients(); |
| 349 | EXPECT_TRUE(dns_clients().empty()); |
| 350 | |
| 351 | for (int i = 0; i < 3; ++i) { |
| 352 | MockDNSClient *dns_client = new MockDNSClient(); |
| 353 | EXPECT_CALL(*dns_client, IsActive()) |
| 354 | .WillOnce(Return(true)) |
| 355 | .WillOnce(Return(true)) |
| 356 | .WillOnce(Return(false)); |
| 357 | // Takes ownership. |
| 358 | dns_clients().push_back(dns_client); |
| 359 | } |
| 360 | for (int i = 0; i < 2; ++i) { |
| 361 | MockDNSClient *dns_client = new MockDNSClient(); |
| 362 | EXPECT_CALL(*dns_client, IsActive()) |
| 363 | .WillOnce(Return(false)); |
| 364 | // Takes ownership. |
| 365 | dns_clients().push_back(dns_client); |
| 366 | } |
| 367 | |
| 368 | EXPECT_EQ(5, dns_clients().size()); |
| 369 | health_checker_->GarbageCollectDNSClients(); |
| 370 | EXPECT_EQ(3, dns_clients().size()); |
| 371 | health_checker_->GarbageCollectDNSClients(); |
| 372 | EXPECT_EQ(3, dns_clients().size()); |
| 373 | health_checker_->GarbageCollectDNSClients(); |
| 374 | EXPECT_TRUE(dns_clients().empty()); |
| 375 | } |
| 376 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 377 | TEST_F(ConnectionHealthCheckerTest, AddRemoteURL) { |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 378 | HTTPURL url; |
| 379 | url.ParseFromString(kProxyURLRemote); |
| 380 | string host = url.host(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 381 | IPAddress remote_ip = StringToIPv4Address(kProxyIPAddressRemote); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 382 | IPAddress remote_ip_2 = StringToIPv4Address(kIPAddress_8_8_8_8); |
| 383 | |
| 384 | MockDNSClientFactory *dns_client_factory |
| 385 | = MockDNSClientFactory::GetInstance(); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 386 | vector<MockDNSClient *> dns_client_buffer; |
| 387 | |
| 388 | // All DNS queries fail. |
| 389 | for (int i = 0; i < NumDNSQueries(); ++i) { |
| 390 | MockDNSClient *dns_client = new MockDNSClient(); |
| 391 | EXPECT_CALL(*dns_client, Start(host, _)) |
| 392 | .WillOnce(Return(false)); |
| 393 | dns_client_buffer.push_back(dns_client); |
| 394 | } |
| 395 | // Will pass ownership of dns_clients elements. |
| 396 | for (int i = 0; i < NumDNSQueries(); ++i) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 397 | EXPECT_CALL(*dns_client_factory, CreateDNSClient(_, _, _, _, _, _)) |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 398 | .InSequence(seq_) |
| 399 | .WillOnce(Return(dns_client_buffer[i])); |
| 400 | } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 401 | EXPECT_CALL(remote_ips_, AddUnique(_)).Times(0); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 402 | health_checker_->AddRemoteURL(kProxyURLRemote); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 403 | Mock::VerifyAndClearExpectations(dns_client_factory); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 404 | Mock::VerifyAndClearExpectations(&remote_ips_); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 405 | dns_client_buffer.clear(); |
| 406 | dns_clients().clear(); |
| 407 | |
| 408 | // All but one DNS queries fail, 1 succeeds. |
| 409 | for (int i = 0; i < NumDNSQueries(); ++i) { |
| 410 | MockDNSClient *dns_client = new MockDNSClient(); |
| 411 | EXPECT_CALL(*dns_client, Start(host, _)) |
| 412 | .WillOnce(Return(true)); |
| 413 | dns_client_buffer.push_back(dns_client); |
| 414 | } |
| 415 | // Will pass ownership of dns_clients elements. |
| 416 | for (int i = 0; i < NumDNSQueries(); ++i) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 417 | EXPECT_CALL(*dns_client_factory, CreateDNSClient(_, _, _, _, _, _)) |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 418 | .InSequence(seq_) |
| 419 | .WillOnce(Return(dns_client_buffer[i])); |
| 420 | } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 421 | EXPECT_CALL(remote_ips_, AddUnique(_)); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 422 | health_checker_->AddRemoteURL(kProxyURLRemote); |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 423 | for (int i = 0; i < NumDNSQueries() - 1; ++i) { |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 424 | InvokeGetDNSResultFailure(); |
| 425 | } |
| 426 | InvokeGetDNSResultSuccess(remote_ip); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 427 | Mock::VerifyAndClearExpectations(dns_client_factory); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 428 | Mock::VerifyAndClearExpectations(&remote_ips_); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 429 | dns_client_buffer.clear(); |
| 430 | dns_clients().clear(); |
| 431 | |
| 432 | // Only 2 distinct IP addresses are returned. |
| 433 | for (int i = 0; i < NumDNSQueries(); ++i) { |
| 434 | MockDNSClient *dns_client = new MockDNSClient(); |
| 435 | EXPECT_CALL(*dns_client, Start(host, _)) |
| 436 | .WillOnce(Return(true)); |
| 437 | dns_client_buffer.push_back(dns_client); |
| 438 | } |
| 439 | // Will pass ownership of dns_clients elements. |
| 440 | for (int i = 0; i < NumDNSQueries(); ++i) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 441 | EXPECT_CALL(*dns_client_factory, CreateDNSClient(_, _, _, _, _, _)) |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 442 | .InSequence(seq_) |
| 443 | .WillOnce(Return(dns_client_buffer[i])); |
| 444 | } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 445 | EXPECT_CALL(remote_ips_, AddUnique(IsSameIPAddress(remote_ip))).Times(4); |
| 446 | EXPECT_CALL(remote_ips_, AddUnique(IsSameIPAddress(remote_ip_2))); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 447 | health_checker_->AddRemoteURL(kProxyURLRemote); |
| 448 | for (int i = 0; i < NumDNSQueries() - 1; ++i) { |
| 449 | InvokeGetDNSResultSuccess(remote_ip); |
| 450 | } |
| 451 | InvokeGetDNSResultSuccess(remote_ip_2); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 452 | Mock::VerifyAndClearExpectations(dns_client_factory); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 453 | Mock::VerifyAndClearExpectations(&remote_ips_); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 454 | dns_client_buffer.clear(); |
| 455 | dns_clients().clear(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | TEST_F(ConnectionHealthCheckerTest, GetSocketInfo) { |
| 459 | SocketInfo sock_info; |
| 460 | vector<SocketInfo> info_list; |
| 461 | |
| 462 | // GetSockName fails. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 463 | EXPECT_CALL(*socket_, GetSockName(_, _, _)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 464 | .WillOnce(Return(-1)); |
| 465 | EXPECT_FALSE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 466 | Mock::VerifyAndClearExpectations(socket_); |
| 467 | |
| 468 | // GetSockName returns IPv6. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 469 | EXPECT_CALL(*socket_, GetSockName(_, _, _)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 470 | .WillOnce( |
| 471 | Invoke(this, |
| 472 | &ConnectionHealthCheckerTest::GetSockNameReturnsIPv6)); |
| 473 | EXPECT_FALSE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 474 | Mock::VerifyAndClearExpectations(socket_); |
| 475 | |
| 476 | // LoadTcpSocketInfo fails. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 477 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 478 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 479 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 480 | .WillOnce(Return(false)); |
| 481 | EXPECT_FALSE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 482 | Mock::VerifyAndClearExpectations(socket_); |
| 483 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 484 | |
| 485 | // LoadTcpSocketInfo returns empty list. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 486 | info_list.clear(); |
| 487 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 488 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 489 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 490 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 491 | Return(true))); |
| 492 | EXPECT_FALSE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 493 | Mock::VerifyAndClearExpectations(socket_); |
| 494 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 495 | |
| 496 | // LoadTcpSocketInfo returns a list without our socket. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 497 | info_list.clear(); |
| 498 | info_list.push_back(CreateSocketInfoOther()); |
| 499 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 500 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 501 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 502 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 503 | Return(true))); |
| 504 | EXPECT_FALSE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 505 | Mock::VerifyAndClearExpectations(socket_); |
| 506 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 507 | |
| 508 | // LoadTcpSocketInfo returns a list with only our socket. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 509 | info_list.clear(); |
| 510 | info_list.push_back( |
| 511 | CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown)); |
| 512 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 513 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 514 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 515 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 516 | Return(true))); |
| 517 | EXPECT_TRUE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 518 | EXPECT_TRUE(CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown) |
| 519 | .IsSameSocketAs(sock_info)); |
| 520 | Mock::VerifyAndClearExpectations(socket_); |
| 521 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 522 | |
| 523 | // LoadTcpSocketInfo returns a list with two sockets, including ours. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 524 | info_list.clear(); |
| 525 | info_list.push_back(CreateSocketInfoOther()); |
| 526 | info_list.push_back( |
| 527 | CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown)); |
| 528 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 529 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 530 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 531 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 532 | Return(true))); |
| 533 | EXPECT_TRUE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 534 | EXPECT_TRUE(CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown) |
| 535 | .IsSameSocketAs(sock_info)); |
| 536 | Mock::VerifyAndClearExpectations(socket_); |
| 537 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 538 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 539 | info_list.clear(); |
| 540 | info_list.push_back( |
| 541 | CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown)); |
| 542 | info_list.push_back(CreateSocketInfoOther()); |
| 543 | EXPECT_CALL(*socket_, GetSockName(kProxyFD, _, _)) |
| 544 | .WillOnce(Invoke(this, &ConnectionHealthCheckerTest::GetSockName)); |
| 545 | EXPECT_CALL(*socket_info_reader_, LoadTcpSocketInfo(_)) |
| 546 | .WillOnce(DoAll(SetArgumentPointee<0>(info_list), |
| 547 | Return(true))); |
| 548 | EXPECT_TRUE(health_checker_->GetSocketInfo(kProxyFD, &sock_info)); |
| 549 | EXPECT_TRUE(CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown) |
| 550 | .IsSameSocketAs(sock_info)); |
| 551 | Mock::VerifyAndClearExpectations(socket_); |
| 552 | Mock::VerifyAndClearExpectations(socket_info_reader_); |
| 553 | } |
| 554 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 555 | TEST_F(ConnectionHealthCheckerTest, NextHealthCheckSample) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 556 | IPAddress ip = StringToIPv4Address(kProxyIPAddressRemote); |
| 557 | ON_CALL(remote_ips_, GetRandomIP()) |
| 558 | .WillByDefault(Return(ip)); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 559 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 560 | health_checker_->set_num_connection_failures(MaxFailedConnectionAttempts()); |
| 561 | ExpectStop(); |
| 562 | EXPECT_CALL( |
| 563 | *this, |
| 564 | ResultCallbackTarget(ConnectionHealthChecker::kResultConnectionFailure)); |
| 565 | health_checker_->NextHealthCheckSample(); |
| 566 | dispatcher_.DispatchPendingEvents(); |
| 567 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 568 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 569 | health_checker_->set_num_congested_queue_detected( |
| 570 | MinCongestedQueueAttempts()); |
| 571 | ExpectStop(); |
| 572 | EXPECT_CALL( |
| 573 | *this, |
| 574 | ResultCallbackTarget(ConnectionHealthChecker::kResultCongestedTxQueue)); |
| 575 | health_checker_->NextHealthCheckSample(); |
| 576 | dispatcher_.DispatchPendingEvents(); |
| 577 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 578 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 579 | health_checker_->set_num_successful_sends(MinSuccessfulSendAttempts()); |
| 580 | ExpectStop(); |
| 581 | EXPECT_CALL( |
| 582 | *this, |
| 583 | ResultCallbackTarget(ConnectionHealthChecker::kResultSuccess)); |
| 584 | health_checker_->NextHealthCheckSample(); |
| 585 | dispatcher_.DispatchPendingEvents(); |
| 586 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 587 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 588 | EXPECT_CALL(*tcp_connection_, Start(_, _)).WillOnce(Return(true)); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 589 | health_checker_->NextHealthCheckSample(); |
| 590 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 591 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 592 | // This test assumes that there are at least 2 connection attempts left |
| 593 | // before ConnectionHealthChecker gives up. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 594 | EXPECT_CALL(*tcp_connection_, Start(_, _)) |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 595 | .WillOnce(Return(false)) |
| 596 | .WillOnce(Return(true)); |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 597 | int16_t num_connection_failures = health_checker_->num_connection_failures(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 598 | health_checker_->NextHealthCheckSample(); |
| 599 | EXPECT_EQ(num_connection_failures + 1, |
| 600 | health_checker_->num_connection_failures()); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 603 | TEST_F(ConnectionHealthCheckerTest, OnConnectionComplete) { |
| 604 | // Test that num_connection_attempts is incremented on failure when |
| 605 | // (1) Async Connection fails. |
| 606 | health_checker_->set_num_connection_failures( |
| 607 | MaxFailedConnectionAttempts() - 1); |
| 608 | ExpectStop(); |
| 609 | EXPECT_CALL( |
| 610 | *this, |
| 611 | ResultCallbackTarget(ConnectionHealthChecker::kResultConnectionFailure)); |
| 612 | health_checker_->OnConnectionComplete(false, -1); |
| 613 | dispatcher_.DispatchPendingEvents(); |
| 614 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 615 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 616 | // (2) The connection state is garbled up. |
| 617 | health_checker_->set_num_connection_failures( |
| 618 | MaxFailedConnectionAttempts() - 1); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 619 | ExpectGetSocketInfoReturns( |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 620 | CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown)); |
| 621 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 622 | ExpectStop(); |
| 623 | EXPECT_CALL( |
| 624 | *this, |
| 625 | ResultCallbackTarget(ConnectionHealthChecker::kResultConnectionFailure)); |
| 626 | health_checker_->OnConnectionComplete(true, kProxyFD); |
| 627 | dispatcher_.DispatchPendingEvents(); |
| 628 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 629 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 630 | // (3) Send fails. |
| 631 | health_checker_->set_num_connection_failures( |
| 632 | MaxFailedConnectionAttempts() - 1); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 633 | ExpectGetSocketInfoReturns( |
| 634 | CreateSocketInfoProxy(SocketInfo::kConnectionStateEstablished)); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 635 | EXPECT_CALL(*socket_, Send(kProxyFD, _, Gt(0), _)).WillOnce(Return(-1)); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 636 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 637 | ExpectStop(); |
| 638 | EXPECT_CALL( |
| 639 | *this, |
| 640 | ResultCallbackTarget(ConnectionHealthChecker::kResultConnectionFailure)); |
| 641 | health_checker_->OnConnectionComplete(true, kProxyFD); |
| 642 | dispatcher_.DispatchPendingEvents(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 645 | TEST_F(ConnectionHealthCheckerTest, VerifySentData) { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 646 | // (1) Test that num_connection_attempts is incremented when the connection |
| 647 | // state is garbled up. |
| 648 | health_checker_->set_num_connection_failures( |
| 649 | MaxFailedConnectionAttempts() - 1); |
| 650 | ExpectGetSocketInfoReturns( |
| 651 | CreateSocketInfoProxy(SocketInfo::kConnectionStateUnknown)); |
| 652 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 653 | ExpectStop(); |
| 654 | EXPECT_CALL( |
| 655 | *this, |
| 656 | ResultCallbackTarget(ConnectionHealthChecker::kResultConnectionFailure)); |
| 657 | health_checker_->set_sock_fd(kProxyFD); |
| 658 | health_checker_->VerifySentData(); |
| 659 | dispatcher_.DispatchPendingEvents(); |
| 660 | VerifyAndClearAllExpectations(); |
| 661 | |
| 662 | // (2) Test that num_congested_queue_detected is incremented when all polling |
| 663 | // attempts have expired. |
| 664 | health_checker_->set_num_congested_queue_detected( |
| 665 | MinCongestedQueueAttempts() - 1); |
| 666 | health_checker_->set_num_tx_queue_polling_attempts( |
| 667 | MaxSentDataPollingAttempts()); |
| 668 | health_checker_->set_old_transmit_queue_value(0); |
| 669 | ExpectGetSocketInfoReturns( |
| 670 | CreateSocketInfoProxy(SocketInfo::kConnectionStateEstablished, |
| 671 | SocketInfo::kTimerStateRetransmitTimerPending, |
| 672 | 1)); |
| 673 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 674 | ExpectStop(); |
| 675 | EXPECT_CALL( |
| 676 | *this, |
| 677 | ResultCallbackTarget(ConnectionHealthChecker::kResultCongestedTxQueue)); |
| 678 | health_checker_->set_sock_fd(kProxyFD); |
| 679 | health_checker_->VerifySentData(); |
| 680 | dispatcher_.DispatchPendingEvents(); |
| 681 | VerifyAndClearAllExpectations(); |
| 682 | |
| 683 | // (3) Test that num_successful_sends is incremented if everything goes fine. |
| 684 | health_checker_->set_num_successful_sends(MinSuccessfulSendAttempts() - 1); |
| 685 | health_checker_->set_old_transmit_queue_value(0); |
| 686 | ExpectGetSocketInfoReturns( |
| 687 | CreateSocketInfoProxy(SocketInfo::kConnectionStateEstablished, |
| 688 | SocketInfo::kTimerStateNoTimerPending, |
| 689 | 0)); |
| 690 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 691 | ExpectStop(); |
| 692 | EXPECT_CALL( |
| 693 | *this, |
| 694 | ResultCallbackTarget(ConnectionHealthChecker::kResultSuccess)); |
| 695 | health_checker_->set_sock_fd(kProxyFD); |
| 696 | health_checker_->VerifySentData(); |
| 697 | dispatcher_.DispatchPendingEvents(); |
| 698 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 699 | |
| 700 | // (4) Test that VerifySentData correctly polls the tcpinfo twice. |
| 701 | // We want to immediately dispatch posted tasks. |
| 702 | SetTCPStateUpdateWaitMilliseconds(0); |
| 703 | health_checker_->set_num_congested_queue_detected( |
| 704 | MinCongestedQueueAttempts() - 1); |
| 705 | health_checker_->set_num_tx_queue_polling_attempts( |
| 706 | MaxSentDataPollingAttempts() - 1); |
| 707 | health_checker_->set_old_transmit_queue_value(0); |
| 708 | ExpectGetSocketInfoReturns( |
| 709 | CreateSocketInfoProxy(SocketInfo::kConnectionStateEstablished, |
| 710 | SocketInfo::kTimerStateRetransmitTimerPending, |
| 711 | 1)); |
| 712 | ExpectGetSocketInfoReturns( |
| 713 | CreateSocketInfoProxy(SocketInfo::kConnectionStateEstablished, |
| 714 | SocketInfo::kTimerStateRetransmitTimerPending, |
| 715 | 1)); |
| 716 | EXPECT_CALL(*socket_, Close(kProxyFD)); |
| 717 | ExpectStop(); |
| 718 | EXPECT_CALL( |
| 719 | *this, ResultCallbackTarget( |
| 720 | ConnectionHealthChecker::kResultCongestedTxQueue)) |
| 721 | .InSequence(seq_); |
| 722 | health_checker_->set_sock_fd(kProxyFD); |
| 723 | health_checker_->VerifySentData(); |
| 724 | dispatcher_.DispatchPendingEvents(); |
| 725 | dispatcher_.DispatchPendingEvents(); |
| 726 | // Force an extra dispatch to make sure that VerifySentData did not poll an |
| 727 | // extra time. This dispatch should be a no-op. |
| 728 | dispatcher_.DispatchPendingEvents(); |
| 729 | VerifyAndClearAllExpectations(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 732 | // Flow: Start() -> Start() |
| 733 | // Expectation: Only one AsyncConnection is setup |
| 734 | TEST_F(ConnectionHealthCheckerTest, StartStartSkipsSecond) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 735 | EXPECT_CALL(*tcp_connection_, Start(_, _)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 736 | .WillOnce(Return(true)); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 737 | EXPECT_CALL(remote_ips_, Empty()).WillRepeatedly(Return(false)); |
| 738 | EXPECT_CALL(remote_ips_, GetRandomIP()) |
| 739 | .WillOnce(Return(StringToIPv4Address(kProxyIPAddressRemote))); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 740 | health_checker_->Start(); |
| 741 | health_checker_->Start(); |
| 742 | } |
| 743 | |
| 744 | // Precondition: size(|remote_ips_|) > 0 |
| 745 | // Flow: Start() -> Stop() before ConnectionComplete() |
| 746 | // Expectation: No call to |result_callback| |
| 747 | TEST_F(ConnectionHealthCheckerTest, StartStopNoCallback) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 748 | EXPECT_CALL(*tcp_connection_, Start(_, _)) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 749 | .WillOnce(Return(true)); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 750 | EXPECT_CALL(*tcp_connection_, Stop()); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 751 | EXPECT_CALL(*this, ResultCallbackTarget(_)) |
| 752 | .Times(0); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 753 | EXPECT_CALL(remote_ips_, Empty()).WillRepeatedly(Return(false)); |
| 754 | EXPECT_CALL(remote_ips_, GetRandomIP()) |
| 755 | .WillOnce(Return(StringToIPv4Address(kProxyIPAddressRemote))); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 756 | health_checker_->Start(); |
| 757 | health_checker_->Stop(); |
| 758 | } |
| 759 | |
| 760 | // Precondition: Empty remote_ips_ |
| 761 | // Flow: Start() |
| 762 | // Expectation: call |result_callback| with kResultUnknown |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 763 | TEST_F(ConnectionHealthCheckerTest, StartImmediateFailure) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 764 | EXPECT_CALL(remote_ips_, Empty()).WillOnce(Return(true)); |
| 765 | EXPECT_CALL(*tcp_connection_, Stop()); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 766 | EXPECT_CALL(*this, ResultCallbackTarget( |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 767 | ConnectionHealthChecker::kResultUnknown)); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 768 | health_checker_->Start(); |
| 769 | Mock::VerifyAndClearExpectations(this); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 770 | Mock::VerifyAndClearExpectations(&remote_ips_); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 771 | Mock::VerifyAndClearExpectations(tcp_connection_); |
| 772 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 773 | EXPECT_CALL(remote_ips_, Empty()).WillRepeatedly(Return(false)); |
| 774 | EXPECT_CALL(remote_ips_, GetRandomIP()) |
| 775 | .WillRepeatedly(Return(StringToIPv4Address(kProxyIPAddressRemote))); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 776 | EXPECT_CALL(*tcp_connection_, |
| 777 | Start(IsSameIPAddress(StringToIPv4Address(kProxyIPAddressRemote)), |
| 778 | kProxyPortRemote)) |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 779 | .Times(MaxFailedConnectionAttempts()) |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 780 | .WillRepeatedly(Return(false)); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 781 | EXPECT_CALL(*tcp_connection_, Stop()); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 782 | EXPECT_CALL(*this, ResultCallbackTarget( |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 783 | ConnectionHealthChecker::kResultConnectionFailure)); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 784 | health_checker_->Start(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 785 | dispatcher_.DispatchPendingEvents(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 786 | Mock::VerifyAndClearExpectations(this); |
| 787 | Mock::VerifyAndClearExpectations(tcp_connection_); |
| 788 | } |
| 789 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 790 | } // namespace shill |