Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: net/tools/quic/end_to_end_test.cc

Issue 24493003: Factor out ServerThread from the QUIC EndToEndTest in order to reuse in (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/net.gyp ('k') | net/tools/quic/quic_in_memory_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <string> 6 #include <string>
7 7
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/simple_thread.h"
13 #include "net/base/ip_endpoint.h" 12 #include "net/base/ip_endpoint.h"
14 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" 13 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
15 #include "net/quic/crypto/null_encrypter.h" 14 #include "net/quic/crypto/null_encrypter.h"
16 #include "net/quic/quic_framer.h" 15 #include "net/quic/quic_framer.h"
17 #include "net/quic/quic_packet_creator.h" 16 #include "net/quic/quic_packet_creator.h"
18 #include "net/quic/quic_protocol.h" 17 #include "net/quic/quic_protocol.h"
19 #include "net/quic/test_tools/quic_connection_peer.h" 18 #include "net/quic/test_tools/quic_connection_peer.h"
20 #include "net/quic/test_tools/quic_session_peer.h" 19 #include "net/quic/test_tools/quic_session_peer.h"
21 #include "net/quic/test_tools/reliable_quic_stream_peer.h" 20 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
22 #include "net/tools/quic/quic_epoll_connection_helper.h" 21 #include "net/tools/quic/quic_epoll_connection_helper.h"
23 #include "net/tools/quic/quic_in_memory_cache.h" 22 #include "net/tools/quic/quic_in_memory_cache.h"
24 #include "net/tools/quic/quic_server.h" 23 #include "net/tools/quic/quic_server.h"
25 #include "net/tools/quic/quic_socket_utils.h" 24 #include "net/tools/quic/quic_socket_utils.h"
26 #include "net/tools/quic/test_tools/http_message_test_utils.h" 25 #include "net/tools/quic/test_tools/http_message_test_utils.h"
27 #include "net/tools/quic/test_tools/quic_client_peer.h" 26 #include "net/tools/quic/test_tools/quic_client_peer.h"
28 #include "net/tools/quic/test_tools/quic_epoll_connection_helper_peer.h" 27 #include "net/tools/quic/test_tools/quic_epoll_connection_helper_peer.h"
29 #include "net/tools/quic/test_tools/quic_test_client.h" 28 #include "net/tools/quic/test_tools/quic_test_client.h"
29 #include "net/tools/quic/test_tools/server_thread.h"
30 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
31 31
32 using base::StringPiece; 32 using base::StringPiece;
33 using base::WaitableEvent; 33 using base::WaitableEvent;
34 using net::test::QuicConnectionPeer; 34 using net::test::QuicConnectionPeer;
35 using net::test::QuicSessionPeer; 35 using net::test::QuicSessionPeer;
36 using net::test::ReliableQuicStreamPeer; 36 using net::test::ReliableQuicStreamPeer;
37 using std::string; 37 using std::string;
38 38
39 namespace net { 39 namespace net {
(...skipping 10 matching lines...) Expand all
50 "https://www.google.com/foo/test/a/request/string/longer/than/25/bytes"; 50 "https://www.google.com/foo/test/a/request/string/longer/than/25/bytes";
51 51
52 void GenerateBody(string* body, int length) { 52 void GenerateBody(string* body, int length) {
53 body->clear(); 53 body->clear();
54 body->reserve(length); 54 body->reserve(length);
55 for (int i = 0; i < length; ++i) { 55 for (int i = 0; i < length; ++i) {
56 body->append(1, static_cast<char>(32 + i % (126 - 32))); 56 body->append(1, static_cast<char>(32 + i % (126 - 32)));
57 } 57 }
58 } 58 }
59 59
60
61 // Simple wrapper class to run server in a thread.
62 class ServerThread : public base::SimpleThread {
63 public:
64 ServerThread(IPEndPoint address,
65 const QuicConfig& config,
66 bool strike_register_no_startup_period)
67 : SimpleThread("server_thread"),
68 listening_(true, false),
69 quit_(true, false),
70 server_(config),
71 address_(address),
72 port_(0) {
73 if (strike_register_no_startup_period) {
74 server_.SetStrikeRegisterNoStartupPeriod();
75 }
76 }
77 virtual ~ServerThread() {
78 }
79
80 virtual void Run() OVERRIDE {
81 server_.Listen(address_);
82
83 port_lock_.Acquire();
84 port_ = server_.port();
85 port_lock_.Release();
86
87 listening_.Signal();
88 while (!quit_.IsSignaled()) {
89 server_.WaitForEvents();
90 }
91 server_.Shutdown();
92 }
93
94 int GetPort() {
95 port_lock_.Acquire();
96 int rc = port_;
97 port_lock_.Release();
98 return rc;
99 }
100
101 WaitableEvent* listening() { return &listening_; }
102 WaitableEvent* quit() { return &quit_; }
103
104 private:
105 WaitableEvent listening_;
106 WaitableEvent quit_;
107 base::Lock port_lock_;
108 QuicServer server_;
109 IPEndPoint address_;
110 int port_;
111
112 DISALLOW_COPY_AND_ASSIGN(ServerThread);
113 };
114
115 class EndToEndTest : public ::testing::TestWithParam<QuicVersion> { 60 class EndToEndTest : public ::testing::TestWithParam<QuicVersion> {
116 public: 61 public:
117 static void SetUpTestCase() { 62 static void SetUpTestCase() {
118 QuicInMemoryCache::GetInstance()->ResetForTests(); 63 QuicInMemoryCache::GetInstance()->ResetForTests();
119 } 64 }
120 65
121 protected: 66 protected:
122 EndToEndTest() 67 EndToEndTest()
123 : server_hostname_("example.com"), 68 : server_hostname_("example.com"),
124 server_started_(false), 69 server_started_(false),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 118
174 void StopServer() { 119 void StopServer() {
175 if (!server_started_) 120 if (!server_started_)
176 return; 121 return;
177 if (server_thread_.get()) { 122 if (server_thread_.get()) {
178 server_thread_->quit()->Signal(); 123 server_thread_->quit()->Signal();
179 server_thread_->Join(); 124 server_thread_->Join();
180 } 125 }
181 } 126 }
182 127
183 void AddToCache(const StringPiece& method, 128 void AddToCache(StringPiece method,
184 const StringPiece& path, 129 StringPiece path,
185 const StringPiece& version, 130 StringPiece version,
186 const StringPiece& response_code, 131 StringPiece response_code,
187 const StringPiece& response_detail, 132 StringPiece response_detail,
188 const StringPiece& body) { 133 StringPiece body) {
189 BalsaHeaders request_headers, response_headers; 134 QuicInMemoryCache::GetInstance()->AddOrVerifyResponse(
190 request_headers.SetRequestFirstlineFromStringPieces(method, 135 method, path, version, response_code, response_detail, body);
191 path,
192 version);
193 response_headers.SetRequestFirstlineFromStringPieces(version,
194 response_code,
195 response_detail);
196 response_headers.AppendHeader("content-length",
197 base::IntToString(body.length()));
198
199 // Check if response already exists and matches.
200 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
201 const QuicInMemoryCache::Response* cached_response =
202 cache->GetResponse(request_headers);
203 if (cached_response != NULL) {
204 string cached_response_headers_str, response_headers_str;
205 cached_response->headers().DumpToString(&cached_response_headers_str);
206 response_headers.DumpToString(&response_headers_str);
207 CHECK_EQ(cached_response_headers_str, response_headers_str);
208 CHECK_EQ(cached_response->body(), body);
209 return;
210 }
211 cache->AddResponse(request_headers, response_headers, body);
212 } 136 }
213 137
214 IPEndPoint server_address_; 138 IPEndPoint server_address_;
215 string server_hostname_; 139 string server_hostname_;
216 scoped_ptr<ServerThread> server_thread_; 140 scoped_ptr<ServerThread> server_thread_;
217 scoped_ptr<QuicTestClient> client_; 141 scoped_ptr<QuicTestClient> client_;
218 bool server_started_; 142 bool server_started_;
219 QuicConfig client_config_; 143 QuicConfig client_config_;
220 QuicConfig server_config_; 144 QuicConfig server_config_;
221 QuicVersion version_; 145 QuicVersion version_;
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 QuicEpollConnectionHelperPeer::SetWriter(helper, NULL); 658 QuicEpollConnectionHelperPeer::SetWriter(helper, NULL);
735 659
736 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error()); 660 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
737 EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error()); 661 EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error());
738 } 662 }
739 663
740 } // namespace 664 } // namespace
741 } // namespace test 665 } // namespace test
742 } // namespace tools 666 } // namespace tools
743 } // namespace net 667 } // namespace net
OLDNEW
« no previous file with comments | « net/net.gyp ('k') | net/tools/quic/quic_in_memory_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698