blob: 0d5dd26518c011fdb0dac505c5f3345956093094 [file] [log] [blame]
[email protected]d67d1052011-06-09 05:11:411// 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
15namespace chrome_browser_net {
16
17class NetworkStatsTest : public PlatformTest {
18 public:
19 NetworkStatsTest() {}
20 protected:
21 virtual void TearDown() {
[email protected]64e95e12011-08-17 17:41:0222 // Flush the message loop to make application verifiers happy.
[email protected]d67d1052011-06-09 05:11:4123 message_loop_.RunAllPending();
24 }
25 MessageLoopForIO message_loop_;
26};
27
28class 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]315e7c82011-12-23 19:14:3637 net::TestCompletionCallback cb;
[email protected]d67d1052011-06-09 05:11:4138
[email protected]ab312f82011-08-22 20:43:0739 scoped_ptr<net::MockHostResolver> host_resolver(
40 new net::MockHostResolver());
41
[email protected]d67d1052011-06-09 05:11:4142 UDPStatsClient* udp_stats_client = new UDPStatsClient();
[email protected]ab312f82011-08-22 20:43:0743 EXPECT_TRUE(udp_stats_client->Start(host_resolver.get(),
44 test_server_.host_port_pair(),
[email protected]d67d1052011-06-09 05:11:4145 bytes,
[email protected]315e7c82011-12-23 19:14:3646 cb.callback()));
[email protected]d67d1052011-06-09 05:11:4147 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
55class 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]315e7c82011-12-23 19:14:3664 net::TestCompletionCallback cb;
[email protected]d67d1052011-06-09 05:11:4165
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]315e7c82011-12-23 19:14:3673 cb.callback()));
[email protected]d67d1052011-06-09 05:11:4174 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
82TEST_F(NetworkStatsTestUDP, UDPEcho_50B_Of_Data) {
83 ASSERT_TRUE(test_server_.Start());
84 RunUDPEchoTest(50);
85}
86
87TEST_F(NetworkStatsTestUDP, UDPEcho_1K_Of_Data) {
88 ASSERT_TRUE(test_server_.Start());
89 RunUDPEchoTest(1024);
90}
91
92TEST_F(NetworkStatsTestTCP, TCPEcho_50B_Of_Data) {
93 ASSERT_TRUE(test_server_.Start());
94 RunTCPEchoTest(50);
95}
96
97TEST_F(NetworkStatsTestTCP, TCPEcho_1K_Of_Data) {
98 ASSERT_TRUE(test_server_.Start());
99 RunTCPEchoTest(1024);
100}
101
102} // namespace chrome_browser_net