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
{
24 void SetUp() override
{
25 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
26 base::MessageLoop::current()->RunUntilIdle();
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
,
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
,
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);
55 ProbePacket probe_packet
;
56 ProbeMessage probe_message
;
57 probe_message
.SetPacketHeader(ProbePacket_Type_HELLO_REQUEST
,
59 probe_packet
.set_group_id(0);
60 outputs_
[0] = probe_message
.MakeEncodedPacket(probe_packet
);
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));
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
) {
87 probe_packet
= ProbePacket(); // Clear all content.
88 probe_message
.SetPacketHeader(ProbePacket_Type_PROBE_REPLY
,
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
,
105 uint32 max_probe_packets
,
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(),
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.
136 // Make one write and then |max_probe_packets| reads.
137 void MakeDelayedMockData(NetworkStats::TestType test_type
,
139 uint32 pacing_interval_micros
,
140 uint32 max_probe_packets
,
141 net::IoMode io_mode
) {
143 ProbePacket probe_packet
;
144 ProbeMessage probe_message
;
145 mock_writes_
.clear();
146 ProbePacket_Token token
;
147 CreateToken(2L, "2a2b", &token
);
149 case NetworkStats::PACKET_SIZE_TEST
:
150 case NetworkStats::NON_PACED_PACKET_TEST
:
151 pacing_interval_micros
= 0;
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;
159 default: {} // Do nothing here.
161 probe_message
.GenerateProbeRequest(token
,
162 1, // current_test_index_ = 1.
164 pacing_interval_micros
,
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
);
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
,
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
,
194 uint32 interval_micros
,
195 uint32 max_probe_packets
,
196 net::IoMode io_mode
) {
198 net::MockClientSocketFactory mock_socket_factory
;
200 test_type
, bytes
, interval_micros
, max_probe_packets
, io_mode
);
201 net::DelayedSocketData
test_data(1,
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(),
230 // Test need to be added after Start() because Start() will reset
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();
238 udp_stats_client
->ReadData();
239 udp_stats_client
->StartOneTest();
240 rv
= cb
.WaitForResult();
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
) {
317 true, NetworkStats::NON_PACED_PACKET_TEST
, 100, 0, 1, net::ASYNC
);
320 TEST_F(NetworkStatsTest
, StartNonPacedTest100BHasNoProxySync
) {
322 false, NetworkStats::NON_PACED_PACKET_TEST
, 100, 0, 1, net::SYNCHRONOUS
);
325 TEST_F(NetworkStatsTest
, StartNonPacedTest500BHasNoProxy
) {
327 false, NetworkStats::NON_PACED_PACKET_TEST
, 500, 3, 1, net::ASYNC
);
330 TEST_F(NetworkStatsTest
, StartNonPacedTest1200BHasProxySync
) {
332 true, NetworkStats::NON_PACED_PACKET_TEST
, 1200, 1, 1, net::SYNCHRONOUS
);
335 TEST_F(NetworkStatsTest
, StartNonPacedTest500BHasNoProxyMulti
) {
337 false, NetworkStats::NON_PACED_PACKET_TEST
, 500, 2, 3, net::ASYNC
);
340 TEST_F(NetworkStatsTest
, StartNonPacedTest1200BHasProxySyncMulti
) {
342 true, NetworkStats::NON_PACED_PACKET_TEST
, 1200, 1, 4, net::SYNCHRONOUS
);
345 TEST_F(NetworkStatsTest
, StartPacedTest100BHasProxy
) {
347 true, NetworkStats::PACED_PACKET_TEST
, 100, 0, 1, net::ASYNC
);
350 TEST_F(NetworkStatsTest
, StartPacedTest100BHasNoProxySync
) {
352 false, NetworkStats::PACED_PACKET_TEST
, 100, 0, 1, net::SYNCHRONOUS
);
355 TEST_F(NetworkStatsTest
, StartPacedTest500BHasNoProxy
) {
357 false, NetworkStats::PACED_PACKET_TEST
, 500, 3, 1, net::ASYNC
);
360 TEST_F(NetworkStatsTest
, StartPacedTest1200BHasProxySync
) {
362 true, NetworkStats::PACED_PACKET_TEST
, 1200, 1, 1, net::SYNCHRONOUS
);
365 TEST_F(NetworkStatsTest
, StartPacedTest500BHasNoProxyMulti
) {
367 false, NetworkStats::PACED_PACKET_TEST
, 500, 2, 3, net::ASYNC
);
370 TEST_F(NetworkStatsTest
, StartPacedTest1200BHasProxySyncMulti
) {
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
) {
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
) {
390 true, NetworkStats::NAT_BIND_TEST
, 1200, 3, 2, net::SYNCHRONOUS
);
393 TEST_F(NetworkStatsTest
, StartPacketSizeTest1500BHasProxy
) {
395 true, NetworkStats::PACKET_SIZE_TEST
, 1500, 0, 1, net::ASYNC
);
398 TEST_F(NetworkStatsTest
, StartPacketSizeTest1500HasNoProxySync
) {
400 false, NetworkStats::PACKET_SIZE_TEST
, 1500, 0, 1, net::SYNCHRONOUS
);
403 TEST_F(NetworkStatsTest
, StartPacketSizeTest1500BHasNoProxy
) {
405 false, NetworkStats::PACKET_SIZE_TEST
, 1500, 0, 1, net::ASYNC
);
408 } // namespace chrome_browser_net