Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [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/traffic_monitor.h" |
| 6 | |
| 7 | #include <base/bind.h> |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 8 | #include <base/stringprintf.h> |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 10 | #include <netinet/in.h> |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 11 | |
| 12 | #include "shill/mock_device.h" |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 13 | #include "shill/mock_connection_info_reader.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 14 | #include "shill/mock_event_dispatcher.h" |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 15 | #include "shill/mock_ipconfig.h" |
| 16 | #include "shill/mock_socket_info_reader.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 17 | #include "shill/nice_mock_control.h" |
| 18 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 19 | using base::Bind; |
| 20 | using base::StringPrintf; |
| 21 | using base::Unretained; |
| 22 | using std::string; |
| 23 | using std::vector; |
| 24 | using testing::_; |
| 25 | using testing::Mock; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 26 | using testing::NiceMock; |
| 27 | using testing::Return; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 28 | using testing::ReturnRef; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 29 | using testing::Test; |
| 30 | |
| 31 | namespace shill { |
| 32 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 33 | class TrafficMonitorTest : public Test { |
| 34 | public: |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 35 | static const string kLocalIpAddr; |
| 36 | static const uint16 kLocalPort1; |
| 37 | static const uint16 kLocalPort2; |
| 38 | static const uint16 kLocalPort3; |
| 39 | static const uint16 kLocalPort4; |
| 40 | static const uint16 kLocalPort5; |
| 41 | static const string kRemoteIpAddr; |
| 42 | static const uint16 kRemotePort; |
| 43 | static const uint64 kTxQueueLength1; |
| 44 | static const uint64 kTxQueueLength2; |
| 45 | static const uint64 kTxQueueLength3; |
| 46 | static const uint64 kTxQueueLength4; |
| 47 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 48 | TrafficMonitorTest() |
| 49 | : device_(new MockDevice(&control_, |
| 50 | &dispatcher_, |
| 51 | reinterpret_cast<Metrics *>(NULL), |
| 52 | reinterpret_cast<Manager *>(NULL), |
| 53 | "netdev0", |
| 54 | "00:11:22:33:44:55", |
| 55 | 1)), |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 56 | ipconfig_(new MockIPConfig(&control_, "netdev0")), |
| 57 | mock_socket_info_reader_(new MockSocketInfoReader), |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 58 | mock_connection_info_reader_(new MockConnectionInfoReader), |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 59 | monitor_(device_, &dispatcher_), |
| 60 | local_addr_(IPAddress::kFamilyIPv4), |
| 61 | remote_addr_(IPAddress::kFamilyIPv4) { |
| 62 | local_addr_.SetAddressFromString(kLocalIpAddr); |
| 63 | remote_addr_.SetAddressFromString(kRemoteIpAddr); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 66 | MOCK_METHOD0(OnNoOutgoingPackets, void()); |
| 67 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 68 | protected: |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 69 | virtual void SetUp() { |
| 70 | monitor_.socket_info_reader_.reset( |
| 71 | mock_socket_info_reader_); // Passes ownership |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 72 | monitor_.connection_info_reader_.reset( |
| 73 | mock_connection_info_reader_); // Passes ownership |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 74 | |
Gaurav Shah | 61c0eac | 2013-04-09 17:09:42 -0700 | [diff] [blame] | 75 | device_->set_ipconfig(ipconfig_); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 76 | ipconfig_properties_.address = kLocalIpAddr; |
| 77 | EXPECT_CALL(*ipconfig_.get(), properties()) |
| 78 | .WillRepeatedly(ReturnRef(ipconfig_properties_)); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void VerifyStopped() { |
| 82 | EXPECT_TRUE(monitor_.sample_traffic_callback_.IsCancelled()); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 83 | EXPECT_EQ(0, monitor_.accummulated_congested_tx_queues_samples_); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void VerifyStarted() { |
| 87 | EXPECT_FALSE(monitor_.sample_traffic_callback_.IsCancelled()); |
| 88 | } |
| 89 | |
| 90 | void SetupMockSocketInfos(const vector<SocketInfo> &socket_infos) { |
| 91 | mock_socket_infos_ = socket_infos; |
| 92 | EXPECT_CALL(*mock_socket_info_reader_, LoadTcpSocketInfo(_)) |
| 93 | .WillRepeatedly( |
| 94 | Invoke(this, &TrafficMonitorTest::MockLoadTcpSocketInfo)); |
| 95 | } |
| 96 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 97 | void SetupMockConnectionInfos( |
| 98 | const vector<ConnectionInfo> &connection_infos) { |
| 99 | mock_connection_infos_ = connection_infos; |
| 100 | EXPECT_CALL(*mock_connection_info_reader_, LoadConnectionInfo(_)) |
| 101 | .WillRepeatedly( |
| 102 | Invoke(this, &TrafficMonitorTest::MockLoadConnectionInfo)); |
| 103 | } |
| 104 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 105 | bool MockLoadTcpSocketInfo(vector<SocketInfo> *info_list) { |
| 106 | *info_list = mock_socket_infos_; |
| 107 | return true; |
| 108 | } |
| 109 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 110 | bool MockLoadConnectionInfo(vector<ConnectionInfo> *info_list) { |
| 111 | *info_list = mock_connection_infos_; |
| 112 | return true; |
| 113 | } |
| 114 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 115 | string FormatIPPort(const IPAddress &ip, const uint16 port) { |
| 116 | return StringPrintf("%s:%d", ip.ToString().c_str(), port); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | NiceMockControl control_; |
| 120 | NiceMock<MockEventDispatcher> dispatcher_; |
| 121 | scoped_refptr<MockDevice> device_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 122 | scoped_refptr<MockIPConfig> ipconfig_; |
| 123 | IPConfig::Properties ipconfig_properties_; |
| 124 | MockSocketInfoReader *mock_socket_info_reader_; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 125 | MockConnectionInfoReader *mock_connection_info_reader_; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 126 | TrafficMonitor monitor_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 127 | vector<SocketInfo> mock_socket_infos_; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 128 | vector<ConnectionInfo> mock_connection_infos_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 129 | IPAddress local_addr_; |
| 130 | IPAddress remote_addr_; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 133 | // static |
| 134 | const string TrafficMonitorTest::kLocalIpAddr = "127.0.0.1"; |
| 135 | const uint16 TrafficMonitorTest::kLocalPort1 = 1234; |
| 136 | const uint16 TrafficMonitorTest::kLocalPort2 = 2345; |
| 137 | const uint16 TrafficMonitorTest::kLocalPort3 = 3456; |
| 138 | const uint16 TrafficMonitorTest::kLocalPort4 = 4567; |
| 139 | const uint16 TrafficMonitorTest::kLocalPort5 = 4567; |
| 140 | const string TrafficMonitorTest::kRemoteIpAddr = "192.168.1.1"; |
| 141 | const uint16 TrafficMonitorTest::kRemotePort = 5678; |
| 142 | const uint64 TrafficMonitorTest::kTxQueueLength1 = 111; |
| 143 | const uint64 TrafficMonitorTest::kTxQueueLength2 = 222; |
| 144 | const uint64 TrafficMonitorTest::kTxQueueLength3 = 333; |
| 145 | const uint64 TrafficMonitorTest::kTxQueueLength4 = 444; |
| 146 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 147 | TEST_F(TrafficMonitorTest, StartAndStop) { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 148 | // Stop without start |
| 149 | monitor_.Stop(); |
| 150 | VerifyStopped(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 151 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 152 | // Normal start |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 153 | monitor_.Start(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 154 | VerifyStarted(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 155 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 156 | // Stop after start |
| 157 | monitor_.Stop(); |
| 158 | VerifyStopped(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 159 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 160 | // Stop again without start |
| 161 | monitor_.Stop(); |
| 162 | VerifyStopped(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 165 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthValid) { |
| 166 | vector<SocketInfo> socket_infos; |
| 167 | socket_infos.push_back( |
| 168 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 169 | local_addr_, |
| 170 | TrafficMonitorTest::kLocalPort1, |
| 171 | remote_addr_, |
| 172 | TrafficMonitorTest::kRemotePort, |
| 173 | TrafficMonitorTest::kTxQueueLength1, |
| 174 | 0, |
| 175 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 176 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 177 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 178 | EXPECT_EQ(1, tx_queue_lengths.size()); |
| 179 | string ip_port = FormatIPPort(local_addr_, TrafficMonitorTest::kLocalPort1); |
| 180 | EXPECT_EQ(TrafficMonitorTest::kTxQueueLength1, tx_queue_lengths[ip_port]); |
| 181 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 182 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 183 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthInvalidDevice) { |
| 184 | vector<SocketInfo> socket_infos; |
| 185 | IPAddress foreign_ip_addr(IPAddress::kFamilyIPv4); |
| 186 | foreign_ip_addr.SetAddressFromString("192.167.1.1"); |
| 187 | socket_infos.push_back( |
| 188 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 189 | foreign_ip_addr, |
| 190 | TrafficMonitorTest::kLocalPort1, |
| 191 | remote_addr_, |
| 192 | TrafficMonitorTest::kRemotePort, |
| 193 | TrafficMonitorTest::kTxQueueLength1, |
| 194 | 0, |
| 195 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 196 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 197 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 198 | EXPECT_EQ(0, tx_queue_lengths.size()); |
| 199 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 200 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 201 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthZero) { |
| 202 | vector<SocketInfo> socket_infos; |
| 203 | socket_infos.push_back( |
| 204 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 205 | local_addr_, |
| 206 | TrafficMonitorTest::kLocalPort1, |
| 207 | remote_addr_, |
| 208 | TrafficMonitorTest::kRemotePort, |
| 209 | 0, |
| 210 | 0, |
| 211 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 212 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 213 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 214 | EXPECT_EQ(0, tx_queue_lengths.size()); |
| 215 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 216 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 217 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthInvalidConnectionState) { |
| 218 | vector<SocketInfo> socket_infos; |
| 219 | socket_infos.push_back( |
| 220 | SocketInfo(SocketInfo::kConnectionStateSynSent, |
| 221 | local_addr_, |
| 222 | TrafficMonitorTest::kLocalPort1, |
| 223 | remote_addr_, |
| 224 | TrafficMonitorTest::kRemotePort, |
| 225 | TrafficMonitorTest::kTxQueueLength1, |
| 226 | 0, |
| 227 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 228 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 229 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 230 | EXPECT_EQ(0, tx_queue_lengths.size()); |
| 231 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 232 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 233 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthInvalidTimerState) { |
| 234 | vector<SocketInfo> socket_infos; |
| 235 | socket_infos.push_back( |
| 236 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 237 | local_addr_, |
| 238 | TrafficMonitorTest::kLocalPort1, |
| 239 | remote_addr_, |
| 240 | TrafficMonitorTest::kRemotePort, |
| 241 | TrafficMonitorTest::kTxQueueLength1, |
| 242 | 0, |
| 243 | SocketInfo::kTimerStateNoTimerPending)); |
| 244 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 245 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 246 | EXPECT_EQ(0, tx_queue_lengths.size()); |
| 247 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 248 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 249 | TEST_F(TrafficMonitorTest, BuildIPPortToTxQueueLengthMultipleEntries) { |
| 250 | vector<SocketInfo> socket_infos; |
| 251 | socket_infos.push_back( |
| 252 | SocketInfo(SocketInfo::kConnectionStateSynSent, |
| 253 | local_addr_, |
| 254 | TrafficMonitorTest::kLocalPort1, |
| 255 | remote_addr_, |
| 256 | TrafficMonitorTest::kRemotePort, |
| 257 | TrafficMonitorTest::kTxQueueLength1, |
| 258 | 0, |
| 259 | SocketInfo::kTimerStateNoTimerPending)); |
| 260 | socket_infos.push_back( |
| 261 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 262 | local_addr_, |
| 263 | TrafficMonitorTest::kLocalPort2, |
| 264 | remote_addr_, |
| 265 | TrafficMonitorTest::kRemotePort, |
| 266 | TrafficMonitorTest::kTxQueueLength2, |
| 267 | 0, |
| 268 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 269 | socket_infos.push_back( |
| 270 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 271 | local_addr_, |
| 272 | TrafficMonitorTest::kLocalPort3, |
| 273 | remote_addr_, |
| 274 | TrafficMonitorTest::kRemotePort, |
| 275 | TrafficMonitorTest::kTxQueueLength3, |
| 276 | 0, |
| 277 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 278 | socket_infos.push_back( |
| 279 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 280 | local_addr_, |
| 281 | TrafficMonitorTest::kLocalPort4, |
| 282 | remote_addr_, |
| 283 | TrafficMonitorTest::kRemotePort, |
| 284 | TrafficMonitorTest::kTxQueueLength4, |
| 285 | 0, |
| 286 | SocketInfo::kTimerStateNoTimerPending)); |
| 287 | socket_infos.push_back( |
| 288 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 289 | local_addr_, |
| 290 | TrafficMonitorTest::kLocalPort5, |
| 291 | remote_addr_, |
| 292 | TrafficMonitorTest::kRemotePort, |
| 293 | 0, |
| 294 | 0, |
| 295 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 296 | TrafficMonitor::IPPortToTxQueueLengthMap tx_queue_lengths; |
| 297 | monitor_.BuildIPPortToTxQueueLength(socket_infos, &tx_queue_lengths); |
| 298 | EXPECT_EQ(2, tx_queue_lengths.size()); |
| 299 | string ip_port = FormatIPPort(local_addr_, TrafficMonitorTest::kLocalPort2); |
| 300 | EXPECT_EQ(kTxQueueLength2, tx_queue_lengths[ip_port]); |
| 301 | ip_port = FormatIPPort(local_addr_, TrafficMonitorTest::kLocalPort3); |
| 302 | EXPECT_EQ(kTxQueueLength3, tx_queue_lengths[ip_port]); |
| 303 | } |
| 304 | |
| 305 | TEST_F(TrafficMonitorTest, SampleTrafficStuckTxQueueSameQueueLength) { |
| 306 | vector<SocketInfo> socket_infos; |
| 307 | socket_infos.push_back( |
| 308 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 309 | local_addr_, |
| 310 | TrafficMonitorTest::kLocalPort1, |
| 311 | remote_addr_, |
| 312 | TrafficMonitorTest::kRemotePort, |
| 313 | TrafficMonitorTest::kTxQueueLength1, |
| 314 | 0, |
| 315 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 316 | SetupMockSocketInfos(socket_infos); |
| 317 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 318 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 319 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 320 | monitor_.SampleTraffic(); |
| 321 | Mock::VerifyAndClearExpectations(this); |
| 322 | |
| 323 | // Mimic same queue length by using same mock socket info. |
| 324 | EXPECT_CALL(*this, OnNoOutgoingPackets()); |
| 325 | monitor_.SampleTraffic(); |
| 326 | Mock::VerifyAndClearExpectations(this); |
| 327 | |
| 328 | // Perform another sampling pass and make sure the callback is only |
| 329 | // triggered once. |
| 330 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 331 | monitor_.SampleTraffic(); |
| 332 | } |
| 333 | |
| 334 | TEST_F(TrafficMonitorTest, SampleTrafficStuckTxQueueIncreasingQueueLength) { |
| 335 | vector<SocketInfo> socket_infos; |
| 336 | socket_infos.push_back( |
| 337 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 338 | local_addr_, |
| 339 | TrafficMonitorTest::kLocalPort1, |
| 340 | remote_addr_, |
| 341 | TrafficMonitorTest::kRemotePort, |
| 342 | TrafficMonitorTest::kTxQueueLength1, |
| 343 | 0, |
| 344 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 345 | SetupMockSocketInfos(socket_infos); |
| 346 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 347 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 348 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 349 | monitor_.SampleTraffic(); |
| 350 | Mock::VerifyAndClearExpectations(this); |
| 351 | |
| 352 | socket_infos.clear(); |
| 353 | socket_infos.push_back( |
| 354 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 355 | local_addr_, |
| 356 | TrafficMonitorTest::kLocalPort1, |
| 357 | remote_addr_, |
| 358 | TrafficMonitorTest::kRemotePort, |
| 359 | TrafficMonitorTest::kTxQueueLength1 + 1, |
| 360 | 0, |
| 361 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 362 | SetupMockSocketInfos(socket_infos); |
| 363 | EXPECT_CALL(*this, OnNoOutgoingPackets()); |
| 364 | monitor_.SampleTraffic(); |
| 365 | } |
| 366 | |
| 367 | TEST_F(TrafficMonitorTest, SampleTrafficStuckTxQueueVariousQueueLengths) { |
| 368 | vector<SocketInfo> socket_infos; |
| 369 | socket_infos.push_back( |
| 370 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 371 | local_addr_, |
| 372 | TrafficMonitorTest::kLocalPort1, |
| 373 | remote_addr_, |
| 374 | TrafficMonitorTest::kRemotePort, |
| 375 | TrafficMonitorTest::kTxQueueLength2, |
| 376 | 0, |
| 377 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 378 | SetupMockSocketInfos(socket_infos); |
| 379 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 380 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 381 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 382 | monitor_.SampleTraffic(); |
| 383 | Mock::VerifyAndClearExpectations(this); |
| 384 | |
| 385 | socket_infos.clear(); |
| 386 | socket_infos.push_back( |
| 387 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 388 | local_addr_, |
| 389 | TrafficMonitorTest::kLocalPort1, |
| 390 | remote_addr_, |
| 391 | TrafficMonitorTest::kRemotePort, |
| 392 | TrafficMonitorTest::kTxQueueLength1, |
| 393 | 0, |
| 394 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 395 | SetupMockSocketInfos(socket_infos); |
| 396 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 397 | monitor_.SampleTraffic(); |
| 398 | Mock::VerifyAndClearExpectations(this); |
| 399 | |
| 400 | socket_infos.clear(); |
| 401 | socket_infos.push_back( |
| 402 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 403 | local_addr_, |
| 404 | TrafficMonitorTest::kLocalPort1, |
| 405 | remote_addr_, |
| 406 | TrafficMonitorTest::kRemotePort, |
| 407 | TrafficMonitorTest::kTxQueueLength2, |
| 408 | 0, |
| 409 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 410 | SetupMockSocketInfos(socket_infos); |
| 411 | EXPECT_CALL(*this, OnNoOutgoingPackets()); |
| 412 | monitor_.SampleTraffic(); |
| 413 | } |
| 414 | |
| 415 | TEST_F(TrafficMonitorTest, SampleTrafficUnstuckTxQueueZeroQueueLength) { |
| 416 | vector<SocketInfo> socket_infos; |
| 417 | socket_infos.push_back( |
| 418 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 419 | local_addr_, |
| 420 | TrafficMonitorTest::kLocalPort1, |
| 421 | remote_addr_, |
| 422 | TrafficMonitorTest::kRemotePort, |
| 423 | TrafficMonitorTest::kTxQueueLength1, |
| 424 | 0, |
| 425 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 426 | SetupMockSocketInfos(socket_infos); |
| 427 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 428 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 429 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 430 | monitor_.SampleTraffic(); |
| 431 | |
| 432 | socket_infos.clear(); |
| 433 | socket_infos.push_back( |
| 434 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 435 | local_addr_, |
| 436 | TrafficMonitorTest::kLocalPort1, |
| 437 | remote_addr_, |
| 438 | TrafficMonitorTest::kRemotePort, |
| 439 | 0, |
| 440 | 0, |
| 441 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 442 | SetupMockSocketInfos(socket_infos); |
| 443 | monitor_.SampleTraffic(); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 444 | EXPECT_EQ(0, monitor_.accummulated_congested_tx_queues_samples_); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST_F(TrafficMonitorTest, SampleTrafficUnstuckTxQueueNoConnection) { |
| 448 | vector<SocketInfo> socket_infos; |
| 449 | socket_infos.push_back( |
| 450 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 451 | local_addr_, |
| 452 | TrafficMonitorTest::kLocalPort1, |
| 453 | remote_addr_, |
| 454 | TrafficMonitorTest::kRemotePort, |
| 455 | TrafficMonitorTest::kTxQueueLength1, |
| 456 | 0, |
| 457 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 458 | SetupMockSocketInfos(socket_infos); |
| 459 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 460 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 461 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 462 | monitor_.SampleTraffic(); |
| 463 | |
| 464 | socket_infos.clear(); |
| 465 | SetupMockSocketInfos(socket_infos); |
| 466 | monitor_.SampleTraffic(); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 467 | EXPECT_EQ(0, monitor_.accummulated_congested_tx_queues_samples_); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | TEST_F(TrafficMonitorTest, SampleTrafficUnstuckTxQueueStateChanged) { |
| 471 | vector<SocketInfo> socket_infos; |
| 472 | socket_infos.push_back( |
| 473 | SocketInfo(SocketInfo::kConnectionStateEstablished, |
| 474 | local_addr_, |
| 475 | TrafficMonitorTest::kLocalPort1, |
| 476 | remote_addr_, |
| 477 | TrafficMonitorTest::kRemotePort, |
| 478 | TrafficMonitorTest::kTxQueueLength1, |
| 479 | 0, |
| 480 | SocketInfo::kTimerStateRetransmitTimerPending)); |
| 481 | SetupMockSocketInfos(socket_infos); |
| 482 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 483 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 484 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 485 | monitor_.SampleTraffic(); |
| 486 | |
| 487 | socket_infos.clear(); |
| 488 | socket_infos.push_back( |
| 489 | SocketInfo(SocketInfo::kConnectionStateClose, |
| 490 | local_addr_, |
| 491 | TrafficMonitorTest::kLocalPort1, |
| 492 | remote_addr_, |
| 493 | TrafficMonitorTest::kRemotePort, |
| 494 | 0, |
| 495 | 0, |
| 496 | SocketInfo::kTimerStateNoTimerPending)); |
| 497 | SetupMockSocketInfos(socket_infos); |
| 498 | monitor_.SampleTraffic(); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 499 | EXPECT_EQ(0, monitor_.accummulated_congested_tx_queues_samples_); |
| 500 | } |
| 501 | |
| 502 | TEST_F(TrafficMonitorTest, SampleTrafficDnsTimedOut) { |
| 503 | vector<ConnectionInfo> connection_infos; |
| 504 | connection_infos.push_back( |
| 505 | ConnectionInfo(IPPROTO_UDP, |
| 506 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 507 | true, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 508 | remote_addr_, TrafficMonitor::kDnsPort, |
| 509 | remote_addr_, TrafficMonitor::kDnsPort, |
| 510 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 511 | SetupMockConnectionInfos(connection_infos); |
| 512 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 513 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 514 | // Make sure the no routing event is not fired before the threshold is |
| 515 | // exceeded. |
| 516 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 517 | for (int count = 1; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 518 | ++count) { |
| 519 | monitor_.SampleTraffic(); |
| 520 | } |
| 521 | Mock::VerifyAndClearExpectations(this); |
| 522 | |
| 523 | // This call should cause the threshold to exceed. |
| 524 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(1); |
| 525 | monitor_.SampleTraffic(); |
| 526 | Mock::VerifyAndClearExpectations(this); |
| 527 | |
| 528 | // Make sure the event is only fired once. |
| 529 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 530 | monitor_.SampleTraffic(); |
| 531 | } |
| 532 | |
| 533 | TEST_F(TrafficMonitorTest, SampleTrafficDnsOutstanding) { |
| 534 | vector<ConnectionInfo> connection_infos; |
| 535 | connection_infos.push_back( |
| 536 | ConnectionInfo(IPPROTO_UDP, |
| 537 | TrafficMonitor::kDnsTimedOutThresholdSeconds + 1, |
| 538 | true, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 539 | remote_addr_, TrafficMonitor::kDnsPort, |
| 540 | remote_addr_, TrafficMonitor::kDnsPort, |
| 541 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 542 | SetupMockConnectionInfos(connection_infos); |
| 543 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 544 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 545 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 546 | for (int count = 0; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 547 | ++count) { |
| 548 | monitor_.SampleTraffic(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | TEST_F(TrafficMonitorTest, SampleTrafficDnsSuccessful) { |
| 553 | vector<ConnectionInfo> connection_infos; |
| 554 | connection_infos.push_back( |
| 555 | ConnectionInfo(IPPROTO_UDP, |
| 556 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 557 | false, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 558 | remote_addr_, TrafficMonitor::kDnsPort, |
| 559 | remote_addr_, TrafficMonitor::kDnsPort, |
| 560 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 561 | SetupMockConnectionInfos(connection_infos); |
| 562 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 563 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 564 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 565 | for (int count = 1; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 566 | ++count) { |
| 567 | monitor_.SampleTraffic(); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | TEST_F(TrafficMonitorTest, SampleTrafficDnsFailureThenSuccess) { |
| 572 | vector<ConnectionInfo> connection_infos; |
| 573 | connection_infos.push_back( |
| 574 | ConnectionInfo(IPPROTO_UDP, |
| 575 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 576 | true, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 577 | remote_addr_, TrafficMonitor::kDnsPort, |
| 578 | remote_addr_, TrafficMonitor::kDnsPort, |
| 579 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 580 | SetupMockConnectionInfos(connection_infos); |
| 581 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 582 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 583 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 584 | for (int count = 1; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 585 | ++count) { |
| 586 | monitor_.SampleTraffic(); |
| 587 | } |
| 588 | Mock::VerifyAndClearExpectations(this); |
| 589 | |
| 590 | connection_infos.clear(); |
| 591 | connection_infos.push_back( |
| 592 | ConnectionInfo(IPPROTO_UDP, |
| 593 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 594 | false, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 595 | remote_addr_, TrafficMonitor::kDnsPort, |
| 596 | remote_addr_, TrafficMonitor::kDnsPort, |
| 597 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 598 | SetupMockConnectionInfos(connection_infos); |
| 599 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 600 | monitor_.SampleTraffic(); |
| 601 | EXPECT_EQ(0, monitor_.accummulated_dns_failures_samples_); |
| 602 | } |
| 603 | |
| 604 | TEST_F(TrafficMonitorTest, SampleTrafficDnsTimedOutInvalidProtocol) { |
| 605 | vector<ConnectionInfo> connection_infos; |
| 606 | connection_infos.push_back( |
| 607 | ConnectionInfo(IPPROTO_TCP, |
| 608 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 609 | true, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 610 | remote_addr_, TrafficMonitor::kDnsPort, |
| 611 | remote_addr_, TrafficMonitor::kDnsPort, |
| 612 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 613 | SetupMockConnectionInfos(connection_infos); |
| 614 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 615 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 616 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 617 | for (int count = 0; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 618 | ++count) { |
| 619 | monitor_.SampleTraffic(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | TEST_F(TrafficMonitorTest, SampleTrafficDnsTimedOutInvalidSourceIp) { |
| 624 | vector<ConnectionInfo> connection_infos; |
| 625 | connection_infos.push_back( |
| 626 | ConnectionInfo(IPPROTO_UDP, |
| 627 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 628 | true, remote_addr_, TrafficMonitorTest::kLocalPort1, |
| 629 | remote_addr_, TrafficMonitor::kDnsPort, |
| 630 | remote_addr_, TrafficMonitor::kDnsPort, |
| 631 | remote_addr_, TrafficMonitorTest::kLocalPort1)); |
| 632 | SetupMockConnectionInfos(connection_infos); |
| 633 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 634 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 635 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 636 | for (int count = 0; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 637 | ++count) { |
| 638 | monitor_.SampleTraffic(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | TEST_F(TrafficMonitorTest, SampleTrafficDnsTimedOutOutsideTimeWindow) { |
| 643 | vector<ConnectionInfo> connection_infos; |
| 644 | connection_infos.push_back( |
| 645 | ConnectionInfo(IPPROTO_UDP, |
| 646 | TrafficMonitor::kDnsTimedOutThresholdSeconds - |
| 647 | TrafficMonitor::kSamplingIntervalMilliseconds / 1000, |
| 648 | true, remote_addr_, TrafficMonitorTest::kLocalPort1, |
| 649 | remote_addr_, TrafficMonitor::kDnsPort, |
| 650 | remote_addr_, TrafficMonitor::kDnsPort, |
| 651 | remote_addr_, TrafficMonitorTest::kLocalPort1)); |
| 652 | SetupMockConnectionInfos(connection_infos); |
| 653 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 654 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 655 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 656 | for (int count = 0; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 657 | ++count) { |
| 658 | monitor_.SampleTraffic(); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | TEST_F(TrafficMonitorTest, SampleTrafficNonDnsTimedOut) { |
| 663 | const uint16 kNonDnsPort = 54; |
| 664 | vector<ConnectionInfo> connection_infos; |
| 665 | connection_infos.push_back( |
| 666 | ConnectionInfo(IPPROTO_UDP, |
| 667 | TrafficMonitor::kDnsTimedOutThresholdSeconds - 1, |
| 668 | true, local_addr_, TrafficMonitorTest::kLocalPort1, |
| 669 | remote_addr_, kNonDnsPort, |
| 670 | remote_addr_, kNonDnsPort, |
| 671 | local_addr_, TrafficMonitorTest::kLocalPort1)); |
| 672 | SetupMockConnectionInfos(connection_infos); |
| 673 | monitor_.set_tcp_out_traffic_not_routed_callback( |
| 674 | Bind(&TrafficMonitorTest::OnNoOutgoingPackets, Unretained(this))); |
| 675 | EXPECT_CALL(*this, OnNoOutgoingPackets()).Times(0); |
| 676 | for (int count = 0; count < TrafficMonitor::kMinimumFailedSamplesToTrigger; |
| 677 | ++count) { |
| 678 | monitor_.SampleTraffic(); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | TEST_F(TrafficMonitorTest, SampleTrafficDnsStatsReset) { |
| 683 | vector<ConnectionInfo> connection_infos; |
| 684 | SetupMockConnectionInfos(connection_infos); |
| 685 | monitor_.accummulated_dns_failures_samples_ = 1; |
| 686 | monitor_.SampleTraffic(); |
| 687 | EXPECT_EQ(0, monitor_.accummulated_dns_failures_samples_); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | } // namespace shill |