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

Side by Side Diff: components/grpc_support/test/quic_test_server.cc

Issue 2487863003: Revert of Revert "Revert of Moving gRPC support interfaces out of cronet and into a new component. (patchset … (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « components/grpc_support/test/quic_test_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
5 #include "components/grpc_support/test/quic_test_server.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/path_service.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/chromium/crypto/proof_source_chromium.h"
19 #include "net/spdy/spdy_header_block.h"
20 #include "net/test/test_data_directory.h"
21 #include "net/tools/quic/quic_in_memory_cache.h"
22 #include "net/tools/quic/quic_simple_server.h"
23
24 namespace grpc_support {
25
26 // This must match the certificate used (quic_test.example.com.crt and
27 // quic_test.example.com.key.pkcs8).
28 const char kTestServerHost[] = "test.example.com";
29 const char kTestServerUrl[] = "https://test.example.com/hello.txt";
30
31 const char kStatusHeader[] = ":status";
32
33 const char kHelloPath[] = "/hello.txt";
34 const char kHelloBodyValue[] = "Hello from QUIC Server";
35 const char kHelloStatus[] = "200";
36
37 const char kHelloHeaderName[] = "hello_header";
38 const char kHelloHeaderValue[] = "hello header value";
39
40 const char kHelloTrailerName[] = "hello_trailer";
41 const char kHelloTrailerValue[] = "hello trailer value";
42
43 base::Thread* g_quic_server_thread = nullptr;
44 net::QuicSimpleServer* g_quic_server = nullptr;
45 int g_quic_server_port = 0;
46
47 void SetupQuicInMemoryCache() {
48 static bool setup_done = false;
49 if (setup_done)
50 return;
51 setup_done = true;
52 net::SpdyHeaderBlock headers;
53 headers[kHelloHeaderName] = kHelloHeaderValue;
54 headers[kStatusHeader] = kHelloStatus;
55 net::SpdyHeaderBlock trailers;
56 trailers[kHelloTrailerName] = kHelloTrailerValue;
57 net::QuicInMemoryCache::GetInstance()->AddResponse(
58 base::StringPrintf("%s", kTestServerHost),
59 kHelloPath, std::move(headers), kHelloBodyValue, std::move(trailers));
60 }
61
62 void StartQuicServerOnServerThread(const base::FilePath& test_files_root,
63 base::WaitableEvent* server_started_event) {
64 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
65 DCHECK(!g_quic_server);
66
67 net::QuicConfig config;
68 // Set up server certs.
69 base::FilePath directory;
70 directory = test_files_root;
71 std::unique_ptr<net::ProofSourceChromium> proof_source(
72 new net::ProofSourceChromium());
73 CHECK(proof_source->Initialize(
74 directory.AppendASCII("quic_test.example.com.crt"),
75 directory.AppendASCII("quic_test.example.com.key.pkcs8"),
76 directory.AppendASCII("quic_test.example.com.key.sct")));
77 g_quic_server =
78 new net::QuicSimpleServer(std::move(proof_source), config,
79 net::QuicCryptoServerConfig::ConfigOptions(),
80 net::AllSupportedVersions());
81
82 // Start listening on an unbound port.
83 int rv = g_quic_server->Listen(
84 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0));
85 CHECK_GE(rv, 0) << "Quic server fails to start";
86 g_quic_server_port = g_quic_server->server_address().port();
87
88 SetupQuicInMemoryCache();
89
90 server_started_event->Signal();
91 }
92
93 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) {
94 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
95 g_quic_server->Shutdown();
96 delete g_quic_server;
97 g_quic_server = nullptr;
98 server_stopped_event->Signal();
99 }
100
101 bool StartQuicTestServer() {
102 LOG(INFO) << g_quic_server_thread;
103 DCHECK(!g_quic_server_thread);
104 g_quic_server_thread = new base::Thread("quic server thread");
105 base::Thread::Options thread_options;
106 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
107 bool started = g_quic_server_thread->StartWithOptions(thread_options);
108 DCHECK(started);
109 base::FilePath test_files_root = net::GetTestCertsDirectory();
110
111 base::WaitableEvent server_started_event(
112 base::WaitableEvent::ResetPolicy::MANUAL,
113 base::WaitableEvent::InitialState::NOT_SIGNALED);
114 g_quic_server_thread->task_runner()->PostTask(
115 FROM_HERE, base::Bind(&StartQuicServerOnServerThread, test_files_root,
116 &server_started_event));
117 server_started_event.Wait();
118 return true;
119 }
120
121 void ShutdownQuicTestServer() {
122 if (!g_quic_server_thread)
123 return;
124 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
125 base::WaitableEvent server_stopped_event(
126 base::WaitableEvent::ResetPolicy::MANUAL,
127 base::WaitableEvent::InitialState::NOT_SIGNALED);
128 g_quic_server_thread->task_runner()->PostTask(
129 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event));
130 server_stopped_event.Wait();
131 delete g_quic_server_thread;
132 g_quic_server_thread = nullptr;
133 }
134
135 int GetQuicTestServerPort() {
136 return g_quic_server_port;
137 }
138
139 } // namespace grpc_support
OLDNEW
« no previous file with comments | « components/grpc_support/test/quic_test_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698