quic_simple_test_server.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2016 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. #include "net/test/quic_simple_test_server.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/message_loop/message_pump_type.h"
  11. #include "base/path_service.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/synchronization/waitable_event.h"
  15. #include "base/threading/thread.h"
  16. #include "net/base/ip_address.h"
  17. #include "net/base/ip_endpoint.h"
  18. #include "net/quic/crypto/proof_source_chromium.h"
  19. #include "net/test/test_data_directory.h"
  20. #include "net/third_party/quiche/src/quiche/quic/core/quic_dispatcher.h"
  21. #include "net/third_party/quiche/src/quiche/quic/tools/quic_memory_cache_backend.h"
  22. #include "net/tools/quic/quic_simple_server.h"
  23. namespace {
  24. const char kTestServerDomain[] = "example.com";
  25. // This must match the certificate used (quic-chain.pem and quic-leaf-cert.key).
  26. const char kTestServerHost[] = "test.example.com";
  27. const char kStatusHeader[] = ":status";
  28. const char kHelloPath[] = "/hello.txt";
  29. const char kHelloBodyValue[] = "Hello from QUIC Server";
  30. const char kHelloStatus[] = "200";
  31. const char kHelloHeaderName[] = "hello_header";
  32. const char kHelloHeaderValue[] = "hello header value";
  33. const char kHelloTrailerName[] = "hello_trailer";
  34. const char kHelloTrailerValue[] = "hello trailer value";
  35. const char kSimplePath[] = "/simple.txt";
  36. const char kSimpleBodyValue[] = "Simple Hello from QUIC Server";
  37. const char kSimpleStatus[] = "200";
  38. const char kSimpleHeaderName[] = "hello_header";
  39. const char kSimpleHeaderValue[] = "hello header value";
  40. const std::string kCombinedHelloHeaderValue = std::string("foo\0bar", 7);
  41. const char kCombinedHeaderName[] = "combined";
  42. base::Thread* g_quic_server_thread = nullptr;
  43. quic::QuicMemoryCacheBackend* g_quic_cache_backend = nullptr;
  44. net::QuicSimpleServer* g_quic_server = nullptr;
  45. int g_quic_server_port = 0;
  46. } // namespace
  47. namespace net {
  48. std::string const QuicSimpleTestServer::GetDomain() {
  49. return kTestServerDomain;
  50. }
  51. std::string const QuicSimpleTestServer::GetHost() {
  52. return kTestServerHost;
  53. }
  54. HostPortPair const QuicSimpleTestServer::GetHostPort() {
  55. return HostPortPair(kTestServerHost, GetPort());
  56. }
  57. GURL QuicSimpleTestServer::GetFileURL(const std::string& file_path) {
  58. return GURL("https://test.example.com:" + base::NumberToString(GetPort()))
  59. .Resolve(file_path);
  60. }
  61. GURL QuicSimpleTestServer::GetHelloURL() {
  62. // Don't include |port| into Hello URL as it is mapped differently.
  63. return GURL("https://test.example.com").Resolve(kHelloPath);
  64. }
  65. std::string const QuicSimpleTestServer::GetStatusHeaderName() {
  66. return kStatusHeader;
  67. }
  68. // Hello Url returns response with HTTP/2 headers and trailers.
  69. std::string const QuicSimpleTestServer::GetHelloPath() {
  70. return kHelloPath;
  71. }
  72. std::string const QuicSimpleTestServer::GetHelloBodyValue() {
  73. return kHelloBodyValue;
  74. }
  75. std::string const QuicSimpleTestServer::GetHelloStatus() {
  76. return kHelloStatus;
  77. }
  78. std::string const QuicSimpleTestServer::GetHelloHeaderName() {
  79. return kHelloHeaderName;
  80. }
  81. std::string const QuicSimpleTestServer::GetHelloHeaderValue() {
  82. return kHelloHeaderValue;
  83. }
  84. std::string const QuicSimpleTestServer::GetCombinedHeaderName() {
  85. return kCombinedHeaderName;
  86. }
  87. std::string const QuicSimpleTestServer::GetHelloTrailerName() {
  88. return kHelloTrailerName;
  89. }
  90. std::string const QuicSimpleTestServer::GetHelloTrailerValue() {
  91. return kHelloTrailerValue;
  92. }
  93. // Simple Url returns response without HTTP/2 trailers.
  94. GURL QuicSimpleTestServer::GetSimpleURL() {
  95. // Don't include |port| into Simple URL as it is mapped differently.
  96. return GURL("https://test.example.com").Resolve(kSimplePath);
  97. }
  98. std::string const QuicSimpleTestServer::GetSimpleBodyValue() {
  99. return kSimpleBodyValue;
  100. }
  101. std::string const QuicSimpleTestServer::GetSimpleStatus() {
  102. return kSimpleStatus;
  103. }
  104. std::string const QuicSimpleTestServer::GetSimpleHeaderName() {
  105. return kSimpleHeaderName;
  106. }
  107. std::string const QuicSimpleTestServer::GetSimpleHeaderValue() {
  108. return kSimpleHeaderValue;
  109. }
  110. void SetupQuicMemoryCacheBackend() {
  111. spdy::Http2HeaderBlock headers;
  112. headers[kHelloHeaderName] = kHelloHeaderValue;
  113. headers[kStatusHeader] = kHelloStatus;
  114. headers[kCombinedHeaderName] = kCombinedHelloHeaderValue;
  115. spdy::Http2HeaderBlock trailers;
  116. trailers[kHelloTrailerName] = kHelloTrailerValue;
  117. g_quic_cache_backend = new quic::QuicMemoryCacheBackend();
  118. g_quic_cache_backend->AddResponse(base::StringPrintf("%s", kTestServerHost),
  119. kHelloPath, std::move(headers),
  120. kHelloBodyValue, std::move(trailers));
  121. headers[kSimpleHeaderName] = kSimpleHeaderValue;
  122. headers[kStatusHeader] = kSimpleStatus;
  123. g_quic_cache_backend->AddResponse(base::StringPrintf("%s", kTestServerHost),
  124. kSimplePath, std::move(headers),
  125. kSimpleBodyValue);
  126. }
  127. void StartQuicServerOnServerThread(const base::FilePath& test_files_root,
  128. base::WaitableEvent* server_started_event) {
  129. DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  130. DCHECK(!g_quic_server);
  131. quic::QuicConfig config;
  132. // Set up server certs.
  133. base::FilePath directory;
  134. directory = test_files_root;
  135. auto proof_source = std::make_unique<ProofSourceChromium>();
  136. CHECK(proof_source->Initialize(directory.AppendASCII("quic-chain.pem"),
  137. directory.AppendASCII("quic-leaf-cert.key"),
  138. base::FilePath()));
  139. SetupQuicMemoryCacheBackend();
  140. g_quic_server =
  141. new QuicSimpleServer(std::move(proof_source), config,
  142. quic::QuicCryptoServerConfig::ConfigOptions(),
  143. quic::AllSupportedVersions(), g_quic_cache_backend);
  144. // Start listening on an unbound port.
  145. int rv = g_quic_server->Listen(IPEndPoint(IPAddress::IPv4AllZeros(), 0));
  146. CHECK_GE(rv, 0) << "Quic server fails to start";
  147. g_quic_server_port = g_quic_server->server_address().port();
  148. server_started_event->Signal();
  149. }
  150. void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) {
  151. DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  152. g_quic_server->Shutdown();
  153. delete g_quic_server;
  154. g_quic_server = nullptr;
  155. delete g_quic_cache_backend;
  156. g_quic_cache_backend = nullptr;
  157. server_stopped_event->Signal();
  158. }
  159. void ShutdownDispatcherOnServerThread(
  160. base::WaitableEvent* dispatcher_stopped_event) {
  161. DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  162. g_quic_server->dispatcher()->Shutdown();
  163. dispatcher_stopped_event->Signal();
  164. }
  165. bool QuicSimpleTestServer::Start() {
  166. DVLOG(3) << g_quic_server_thread;
  167. DCHECK(!g_quic_server_thread);
  168. g_quic_server_thread = new base::Thread("quic server thread");
  169. base::Thread::Options thread_options;
  170. thread_options.message_pump_type = base::MessagePumpType::IO;
  171. bool started =
  172. g_quic_server_thread->StartWithOptions(std::move(thread_options));
  173. DCHECK(started);
  174. base::FilePath test_files_root = GetTestCertsDirectory();
  175. base::WaitableEvent server_started_event(
  176. base::WaitableEvent::ResetPolicy::MANUAL,
  177. base::WaitableEvent::InitialState::NOT_SIGNALED);
  178. g_quic_server_thread->task_runner()->PostTask(
  179. FROM_HERE, base::BindOnce(&StartQuicServerOnServerThread, test_files_root,
  180. &server_started_event));
  181. server_started_event.Wait();
  182. return true;
  183. }
  184. void QuicSimpleTestServer::AddResponseWithEarlyHints(
  185. const std::string& path,
  186. const spdy::Http2HeaderBlock& response_headers,
  187. const std::string& response_body,
  188. const std::vector<spdy::Http2HeaderBlock>& early_hints) {
  189. g_quic_cache_backend->AddResponseWithEarlyHints(kTestServerHost, path,
  190. response_headers.Clone(),
  191. response_body, early_hints);
  192. }
  193. // Shut down the server dispatcher, and the stream should error out.
  194. void QuicSimpleTestServer::ShutdownDispatcherForTesting() {
  195. if (!g_quic_server)
  196. return;
  197. DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  198. base::WaitableEvent dispatcher_stopped_event(
  199. base::WaitableEvent::ResetPolicy::MANUAL,
  200. base::WaitableEvent::InitialState::NOT_SIGNALED);
  201. g_quic_server_thread->task_runner()->PostTask(
  202. FROM_HERE, base::BindOnce(&ShutdownDispatcherOnServerThread,
  203. &dispatcher_stopped_event));
  204. dispatcher_stopped_event.Wait();
  205. }
  206. void QuicSimpleTestServer::Shutdown() {
  207. if (!g_quic_server)
  208. return;
  209. DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  210. base::WaitableEvent server_stopped_event(
  211. base::WaitableEvent::ResetPolicy::MANUAL,
  212. base::WaitableEvent::InitialState::NOT_SIGNALED);
  213. g_quic_server_thread->task_runner()->PostTask(
  214. FROM_HERE,
  215. base::BindOnce(&ShutdownOnServerThread, &server_stopped_event));
  216. server_stopped_event.Wait();
  217. delete g_quic_server_thread;
  218. g_quic_server_thread = nullptr;
  219. }
  220. int QuicSimpleTestServer::GetPort() {
  221. return g_quic_server_port;
  222. }
  223. } // namespace net