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