[ExtensionToolbarMac] Restrict action button drags to the container's bounds
[chromium-blink-merge.git] / chrome / browser / net / network_stats_unittest.cc
blobcd16a328d1c268da32321d7a4c0a5f88c26c0697
1 // Copyright (c) 2012 The Chromium 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.
5 #include "base/basictypes.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/net/network_stats.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/network_change_notifier.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/dns/host_resolver.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/socket/socket_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 namespace chrome_browser_net {
19 class NetworkStatsTest : public PlatformTest {
20 public:
21 NetworkStatsTest() {}
23 protected:
24 void SetUp() override {
25 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
26 base::MessageLoop::current()->RunUntilIdle();
27 mock_writes_.clear();
28 mock_reads_.clear();
31 void TearDown() override {
32 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
33 // Empty the current queue.
34 base::MessageLoop::current()->RunUntilIdle();
35 PlatformTest::TearDown();
38 void CreateToken(uint64 timestamp_micros,
39 const string& hash,
40 ProbePacket_Token* token) {
41 token->set_timestamp_micros(timestamp_micros);
42 token->mutable_hash()->assign(hash);
45 // DeterministicMockData defines the exact sequence of the read/write
46 // operations (specified by the last parameter of MockRead/MockWrite).
47 // |io_mode_write| is the IO mode for writing only. Reading is always async.
48 void MakeDeterministicMockData(uint32 max_tests,
49 uint32 max_probe_packets,
50 uint32 probe_bytes,
51 net::IoMode io_mode_write) {
52 // Only allow 0 or 1 test because the test 2 in NetworkStats is random.
53 DCHECK_LT(max_tests, 2U);
54 outputs_.resize(10);
55 ProbePacket probe_packet;
56 ProbeMessage probe_message;
57 probe_message.SetPacketHeader(ProbePacket_Type_HELLO_REQUEST,
58 &probe_packet);
59 probe_packet.set_group_id(0);
60 outputs_[0] = probe_message.MakeEncodedPacket(probe_packet);
61 mock_writes_.clear();
62 mock_writes_.push_back(net::MockWrite(
63 io_mode_write, &outputs_[0][0], outputs_[0].size(), 0));
64 // Add one probe_request.
65 probe_packet = ProbePacket(); // Clear all content.
66 ProbePacket_Token token;
67 CreateToken(1L, "1010", &token);
68 probe_message.GenerateProbeRequest(
69 token, 1, probe_bytes, 0, max_probe_packets, &probe_packet);
70 outputs_[1] = probe_message.MakeEncodedPacket(probe_packet);
71 mock_writes_.push_back(net::MockWrite(
72 io_mode_write, &outputs_[1][0], outputs_[1].size(), 2));
74 inputs_.resize(10);
75 mock_reads_.clear();
76 // Add a hello reply.
77 probe_packet = ProbePacket(); // Clear all content.
78 probe_message.SetPacketHeader(ProbePacket_Type_HELLO_REPLY, &probe_packet);
79 probe_packet.set_group_id(0);
80 CreateToken(1L, "1010", probe_packet.mutable_token());
81 inputs_[0] = probe_message.MakeEncodedPacket(probe_packet);
82 mock_reads_.push_back(
83 net::MockRead(net::ASYNC, &inputs_[0][0], inputs_[0].size(), 1));
85 for (uint32 i = 0; i < max_probe_packets; ++i) {
86 // Add a probe reply.
87 probe_packet = ProbePacket(); // Clear all content.
88 probe_message.SetPacketHeader(ProbePacket_Type_PROBE_REPLY,
89 &probe_packet);
90 int padding_size = probe_bytes - probe_packet.ByteSize() - 8;
91 probe_packet.mutable_padding()->append(
92 std::string(std::max(0, padding_size), 0));
93 probe_packet.mutable_header()->set_checksum(0);
94 probe_packet.set_group_id(1);
95 probe_packet.set_packet_index(i);
96 inputs_[1 + i] = probe_message.MakeEncodedPacket(probe_packet);
97 mock_reads_.push_back(net::MockRead(
98 net::ASYNC, &inputs_[1 + i][0], inputs_[1 + i].size(), 3 + i));
102 // Test NetworkStats::Start(...) method.
103 void TestStart(bool has_proxy,
104 uint32 max_tests,
105 uint32 max_probe_packets,
106 uint32 bytes,
107 net::IoMode io_mode) {
108 net::DeterministicMockClientSocketFactory mock_socket_factory;
109 MakeDeterministicMockData(max_tests, max_probe_packets, bytes, io_mode);
110 net::DeterministicSocketData test_data(
111 &mock_reads_[0], mock_reads_.size(),
112 &mock_writes_[0], mock_writes_.size());
113 mock_socket_factory.AddSocketDataProvider(&test_data);
114 NetworkStats* udp_stats_client = new NetworkStats(&mock_socket_factory);
115 udp_stats_client->maximum_tests_ = max_tests;
116 udp_stats_client->maximum_sequential_packets_ = max_probe_packets;
118 net::TestCompletionCallback cb;
119 scoped_ptr<net::MockHostResolver> host_resolver(
120 new net::MockHostResolver());
121 net::HostPortPair host_port_pair;
122 EXPECT_TRUE(udp_stats_client->Start(host_resolver.get(),
123 host_port_pair,
124 9999,
125 has_proxy,
126 bytes,
127 bytes,
128 cb.callback()));
129 int num_packets_run = (max_tests + 1) * 2 + max_probe_packets - 1;
130 test_data.RunFor(num_packets_run);
131 int rv = cb.WaitForResult();
132 // Check there were no errors during connect/write/read to echo UDP server.
133 EXPECT_EQ(0, rv);
136 // Make one write and then |max_probe_packets| reads.
137 void MakeDelayedMockData(NetworkStats::TestType test_type,
138 uint32 probe_bytes,
139 uint32 pacing_interval_micros,
140 uint32 max_probe_packets,
141 net::IoMode io_mode) {
142 outputs_.resize(1);
143 ProbePacket probe_packet;
144 ProbeMessage probe_message;
145 mock_writes_.clear();
146 ProbePacket_Token token;
147 CreateToken(2L, "2a2b", &token);
148 switch (test_type) {
149 case NetworkStats::PACKET_SIZE_TEST:
150 case NetworkStats::NON_PACED_PACKET_TEST:
151 pacing_interval_micros = 0;
152 break;
153 case NetworkStats::NAT_BIND_TEST:
154 // For NAT_BIND_TEST, we always set this to 1000000 to avoid the
155 // randomness in NetworkStats::SendProbeRequest() and to match
156 // the value chosen in TestStartOneTest() below.
157 pacing_interval_micros = 1000000;
158 break;
159 default: {} // Do nothing here.
161 probe_message.GenerateProbeRequest(token,
162 1, // current_test_index_ = 1.
163 probe_bytes,
164 pacing_interval_micros,
165 max_probe_packets,
166 &probe_packet);
167 outputs_[0] = probe_message.MakeEncodedPacket(probe_packet);
168 mock_writes_.push_back(
169 net::MockWrite(io_mode, &outputs_[0][0], outputs_[0].size()));
171 inputs_.resize(max_probe_packets);
172 mock_reads_.clear();
173 for (uint32 i = 0; i < max_probe_packets; ++i) {
174 // Add a probe reply.
175 probe_packet = ProbePacket(); // Clear all content.
176 probe_message.SetPacketHeader(ProbePacket_Type_PROBE_REPLY,
177 &probe_packet);
178 int padding_size = probe_bytes - probe_packet.ByteSize() - 8;
179 probe_packet.mutable_padding()->append(
180 std::string(std::max(0, padding_size), 0));
181 probe_packet.mutable_header()->set_checksum(0);
182 probe_packet.set_group_id(1);
183 probe_packet.set_packet_index(i);
184 inputs_[i] = probe_message.MakeEncodedPacket(probe_packet);
185 mock_reads_.push_back(
186 net::MockRead(io_mode, &inputs_[i][0], inputs_[i].size()));
190 // Test NetworkStats::StartOneTest(...) method.
191 void TestStartOneTest(bool has_proxy,
192 NetworkStats::TestType test_type,
193 uint32 bytes,
194 uint32 interval_micros,
195 uint32 max_probe_packets,
196 net::IoMode io_mode) {
198 net::MockClientSocketFactory mock_socket_factory;
199 MakeDelayedMockData(
200 test_type, bytes, interval_micros, max_probe_packets, io_mode);
201 net::DelayedSocketData test_data(1,
202 &mock_reads_[0],
203 mock_reads_.size(),
204 &mock_writes_[0],
205 mock_writes_.size());
206 mock_socket_factory.AddSocketDataProvider(&test_data);
207 NetworkStats* udp_stats_client = new NetworkStats(&mock_socket_factory);
208 udp_stats_client->maximum_tests_ = 1; // Only do one probe at a time.
209 udp_stats_client->maximum_sequential_packets_ = max_probe_packets;
210 udp_stats_client->maximum_NAT_packets_ = max_probe_packets;
211 // For NAT_BIND_TEST, we always set this to 1 (second) to avoid the
212 // randomness in NetworkStats::SendProbeRequest().
213 udp_stats_client->maximum_NAT_idle_seconds_ = 1;
214 udp_stats_client->start_test_after_connect_ = false;
215 udp_stats_client->inter_arrival_time_ =
216 base::TimeDelta::FromMicroseconds(interval_micros);
217 CreateToken(2L, "2a2b", &udp_stats_client->token_);
219 net::TestCompletionCallback cb;
220 scoped_ptr<net::MockHostResolver> host_resolver(
221 new net::MockHostResolver());
222 net::HostPortPair host_port_pair;
223 EXPECT_TRUE(udp_stats_client->Start(host_resolver.get(),
224 host_port_pair,
225 9999,
226 has_proxy,
227 bytes,
228 bytes,
229 cb.callback()));
230 // Test need to be added after Start() because Start() will reset
231 // test_sequence_
232 udp_stats_client->test_sequence_.push_back(test_type);
233 udp_stats_client->current_test_index_ = 1;
234 // Wait for host resolving and check if there were no errors during
235 // connect/write/read to UDP server.
236 int rv = cb.WaitForResult();
237 EXPECT_EQ(0, rv);
238 udp_stats_client->ReadData();
239 udp_stats_client->StartOneTest();
240 rv = cb.WaitForResult();
241 EXPECT_EQ(0, rv);
244 base::MessageLoopForIO message_loop_;
245 std::vector<std::string> inputs_;
246 std::vector<std::string> outputs_;
247 std::vector<net::MockRead> mock_reads_;
248 std::vector<net::MockWrite> mock_writes_;
251 TEST_F(NetworkStatsTest, ProbeTest100BHasProxyGetToken) {
252 TestStart(true, 0, 1, 100, net::ASYNC);
255 TEST_F(NetworkStatsTest, ProbeTest500BHasNoProxyGetTokenSync) {
256 TestStart(false, 0, 1, 500, net::SYNCHRONOUS);
259 TEST_F(NetworkStatsTest, ProbeTest100BHasNoProxyOneTest) {
260 TestStart(false, 1, 1, 100, net::ASYNC);
263 TEST_F(NetworkStatsTest, ProbeTest100BHasNoProxyOneTestSync) {
264 TestStart(false, 1, 1, 100, net::SYNCHRONOUS);
267 TEST_F(NetworkStatsTest, ProbeTest100BHasProxyOneTest) {
268 TestStart(true, 1, 1, 100, net::ASYNC);
271 TEST_F(NetworkStatsTest, ProbeTest100BHasProxyOneTestSync) {
272 TestStart(true, 1, 1, 100, net::SYNCHRONOUS);
275 TEST_F(NetworkStatsTest, ProbeTest500BHasProxyOneTest) {
276 TestStart(true, 1, 1, 500, net::ASYNC);
279 TEST_F(NetworkStatsTest, ProbeTest500BHasNoProxyOneTestSync) {
280 TestStart(false, 1, 1, 500, net::SYNCHRONOUS);
283 TEST_F(NetworkStatsTest, ProbeTest500BHasNoProxyOneTest) {
284 TestStart(false, 1, 1, 500, net::ASYNC);
287 TEST_F(NetworkStatsTest, ProbeTest500BHasProxyOneTestSync) {
288 TestStart(true, 1, 1, 500, net::SYNCHRONOUS);
291 TEST_F(NetworkStatsTest, ProbeTest1200BHasProxyOneTest) {
292 TestStart(true, 1, 1, 1200, net::ASYNC);
295 TEST_F(NetworkStatsTest, ProbeTest1200BHasNoProxyOneTestSync) {
296 TestStart(false, 1, 1, 1200, net::SYNCHRONOUS);
299 TEST_F(NetworkStatsTest, ProbeTest1200BHasNoProxyOneTest) {
300 TestStart(false, 1, 1, 1200, net::ASYNC);
303 TEST_F(NetworkStatsTest, ProbeTest1200BHasProxyOneTestSync) {
304 TestStart(true, 1, 1, 1200, net::SYNCHRONOUS);
307 TEST_F(NetworkStatsTest, ProbeTest100BHasNoProxyOneTestMultiPackets) {
308 TestStart(false, 1, 4, 100, net::ASYNC);
311 TEST_F(NetworkStatsTest, ProbeTest1200BHasProxyOneTestMultiPacketsSync) {
312 TestStart(true, 1, 4, 1200, net::SYNCHRONOUS);
315 TEST_F(NetworkStatsTest, StartNonPacedTest100BHasProxy) {
316 TestStartOneTest(
317 true, NetworkStats::NON_PACED_PACKET_TEST, 100, 0, 1, net::ASYNC);
320 TEST_F(NetworkStatsTest, StartNonPacedTest100BHasNoProxySync) {
321 TestStartOneTest(
322 false, NetworkStats::NON_PACED_PACKET_TEST, 100, 0, 1, net::SYNCHRONOUS);
325 TEST_F(NetworkStatsTest, StartNonPacedTest500BHasNoProxy) {
326 TestStartOneTest(
327 false, NetworkStats::NON_PACED_PACKET_TEST, 500, 3, 1, net::ASYNC);
330 TEST_F(NetworkStatsTest, StartNonPacedTest1200BHasProxySync) {
331 TestStartOneTest(
332 true, NetworkStats::NON_PACED_PACKET_TEST, 1200, 1, 1, net::SYNCHRONOUS);
335 TEST_F(NetworkStatsTest, StartNonPacedTest500BHasNoProxyMulti) {
336 TestStartOneTest(
337 false, NetworkStats::NON_PACED_PACKET_TEST, 500, 2, 3, net::ASYNC);
340 TEST_F(NetworkStatsTest, StartNonPacedTest1200BHasProxySyncMulti) {
341 TestStartOneTest(
342 true, NetworkStats::NON_PACED_PACKET_TEST, 1200, 1, 4, net::SYNCHRONOUS);
345 TEST_F(NetworkStatsTest, StartPacedTest100BHasProxy) {
346 TestStartOneTest(
347 true, NetworkStats::PACED_PACKET_TEST, 100, 0, 1, net::ASYNC);
350 TEST_F(NetworkStatsTest, StartPacedTest100BHasNoProxySync) {
351 TestStartOneTest(
352 false, NetworkStats::PACED_PACKET_TEST, 100, 0, 1, net::SYNCHRONOUS);
355 TEST_F(NetworkStatsTest, StartPacedTest500BHasNoProxy) {
356 TestStartOneTest(
357 false, NetworkStats::PACED_PACKET_TEST, 500, 3, 1, net::ASYNC);
360 TEST_F(NetworkStatsTest, StartPacedTest1200BHasProxySync) {
361 TestStartOneTest(
362 true, NetworkStats::PACED_PACKET_TEST, 1200, 1, 1, net::SYNCHRONOUS);
365 TEST_F(NetworkStatsTest, StartPacedTest500BHasNoProxyMulti) {
366 TestStartOneTest(
367 false, NetworkStats::PACED_PACKET_TEST, 500, 2, 3, net::ASYNC);
370 TEST_F(NetworkStatsTest, StartPacedTest1200BHasProxySyncMulti) {
371 TestStartOneTest(
372 true, NetworkStats::PACED_PACKET_TEST, 1200, 1, 4, net::SYNCHRONOUS);
375 TEST_F(NetworkStatsTest, StartNATBindTest100BHasProxy) {
376 TestStartOneTest(true, NetworkStats::NAT_BIND_TEST, 100, 0, 1, net::ASYNC);
379 TEST_F(NetworkStatsTest, StartNATBindTest100BHasNoProxySync) {
380 TestStartOneTest(
381 false, NetworkStats::NAT_BIND_TEST, 100, 3, 1, net::SYNCHRONOUS);
384 TEST_F(NetworkStatsTest, StartNATBindTest500BHasNoProxy) {
385 TestStartOneTest(false, NetworkStats::NAT_BIND_TEST, 500, 0, 2, net::ASYNC);
388 TEST_F(NetworkStatsTest, StartNATBindTest1200BHasProxySync) {
389 TestStartOneTest(
390 true, NetworkStats::NAT_BIND_TEST, 1200, 3, 2, net::SYNCHRONOUS);
393 TEST_F(NetworkStatsTest, StartPacketSizeTest1500BHasProxy) {
394 TestStartOneTest(
395 true, NetworkStats::PACKET_SIZE_TEST, 1500, 0, 1, net::ASYNC);
398 TEST_F(NetworkStatsTest, StartPacketSizeTest1500HasNoProxySync) {
399 TestStartOneTest(
400 false, NetworkStats::PACKET_SIZE_TEST, 1500, 0, 1, net::SYNCHRONOUS);
403 TEST_F(NetworkStatsTest, StartPacketSizeTest1500BHasNoProxy) {
404 TestStartOneTest(
405 false, NetworkStats::PACKET_SIZE_TEST, 1500, 0, 1, net::ASYNC);
408 } // namespace chrome_browser_net