[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
| 4 | |
| 5 | #include "base/basictypes.h" |
| 6 | #include "base/message_loop.h" |
| 7 | #include "chrome/browser/net/network_stats.h" |
| 8 | #include "net/base/host_resolver.h" |
| 9 | #include "net/base/mock_host_resolver.h" |
| 10 | #include "net/base/test_completion_callback.h" |
| 11 | #include "net/test/test_server.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "testing/platform_test.h" |
| 14 | |
| 15 | namespace chrome_browser_net { |
| 16 | |
| 17 | class NetworkStatsTest : public PlatformTest { |
| 18 | public: |
| 19 | NetworkStatsTest() {} |
| 20 | protected: |
| 21 | virtual void TearDown() { |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 22 | // Flush the message loop to make application verifiers happy. |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 23 | message_loop_.RunAllPending(); |
| 24 | } |
| 25 | MessageLoopForIO message_loop_; |
| 26 | }; |
| 27 | |
| 28 | class NetworkStatsTestUDP : public NetworkStatsTest { |
| 29 | public: |
| 30 | NetworkStatsTestUDP() |
| 31 | : test_server_(net::TestServer::TYPE_UDP_ECHO, |
| 32 | FilePath(FILE_PATH_LITERAL("net/data"))) { |
| 33 | } |
| 34 | |
| 35 | protected: |
| 36 | void RunUDPEchoTest(int bytes) { |
[email protected] | 315e7c8 | 2011-12-23 19:14:36 | [diff] [blame] | 37 | net::TestCompletionCallback cb; |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 38 | |
[email protected] | ab312f8 | 2011-08-22 20:43:07 | [diff] [blame] | 39 | scoped_ptr<net::MockHostResolver> host_resolver( |
| 40 | new net::MockHostResolver()); |
| 41 | |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 42 | UDPStatsClient* udp_stats_client = new UDPStatsClient(); |
[email protected] | ab312f8 | 2011-08-22 20:43:07 | [diff] [blame] | 43 | EXPECT_TRUE(udp_stats_client->Start(host_resolver.get(), |
| 44 | test_server_.host_port_pair(), |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 45 | bytes, |
[email protected] | 315e7c8 | 2011-12-23 19:14:36 | [diff] [blame] | 46 | cb.callback())); |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 47 | int rv = cb.WaitForResult(); |
| 48 | // Check there were no errors during connect/write/read to echo UDP server. |
| 49 | EXPECT_EQ(0, rv); |
| 50 | } |
| 51 | |
| 52 | net::TestServer test_server_; |
| 53 | }; |
| 54 | |
| 55 | class NetworkStatsTestTCP : public NetworkStatsTest { |
| 56 | public: |
| 57 | NetworkStatsTestTCP() |
| 58 | : test_server_(net::TestServer::TYPE_TCP_ECHO, |
| 59 | FilePath(FILE_PATH_LITERAL("net/data"))) { |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | void RunTCPEchoTest(int bytes) { |
[email protected] | 315e7c8 | 2011-12-23 19:14:36 | [diff] [blame] | 64 | net::TestCompletionCallback cb; |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 65 | |
| 66 | scoped_ptr<net::MockHostResolver> host_resolver( |
| 67 | new net::MockHostResolver()); |
| 68 | |
| 69 | TCPStatsClient* tcp_stats_client = new TCPStatsClient(); |
| 70 | EXPECT_TRUE(tcp_stats_client->Start(host_resolver.get(), |
| 71 | test_server_.host_port_pair(), |
| 72 | bytes, |
[email protected] | 315e7c8 | 2011-12-23 19:14:36 | [diff] [blame] | 73 | cb.callback())); |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 74 | int rv = cb.WaitForResult(); |
| 75 | // Check there were no errors during connect/write/read to echo TCP server. |
| 76 | EXPECT_EQ(0, rv); |
| 77 | } |
| 78 | |
| 79 | net::TestServer test_server_; |
| 80 | }; |
| 81 | |
| 82 | TEST_F(NetworkStatsTestUDP, UDPEcho_50B_Of_Data) { |
| 83 | ASSERT_TRUE(test_server_.Start()); |
| 84 | RunUDPEchoTest(50); |
| 85 | } |
| 86 | |
| 87 | TEST_F(NetworkStatsTestUDP, UDPEcho_1K_Of_Data) { |
| 88 | ASSERT_TRUE(test_server_.Start()); |
| 89 | RunUDPEchoTest(1024); |
| 90 | } |
| 91 | |
| 92 | TEST_F(NetworkStatsTestTCP, TCPEcho_50B_Of_Data) { |
| 93 | ASSERT_TRUE(test_server_.Start()); |
| 94 | RunTCPEchoTest(50); |
| 95 | } |
| 96 | |
| 97 | TEST_F(NetworkStatsTestTCP, TCPEcho_1K_Of_Data) { |
| 98 | ASSERT_TRUE(test_server_.Start()); |
| 99 | RunTCPEchoTest(1024); |
| 100 | } |
| 101 | |
| 102 | } // namespace chrome_browser_net |