File: quic_test_server.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 6,915 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/path_utils.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/logging.h"
#include "base/message_loop/message_pump_type.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_support_android.h"
#include "base/threading/thread.h"
#include "components/cronet/android/test/cronet_test_util.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/quic/crypto/proof_source_chromium.h"
#include "net/test/test_data_directory.h"
#include "net/third_party/quiche/src/quiche/quic/tools/quic_backend_response.h"
#include "net/third_party/quiche/src/quiche/quic/tools/quic_memory_cache_backend.h"
#include "net/tools/quic/quic_simple_server.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/cronet/android/cronet_test_apk_jni/QuicTestServer_jni.h"

using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;

namespace cronet {

namespace {

static const int kServerPort = 6121;
static const std::string kConnectionClosePath = "/close_connection";

std::unique_ptr<base::Thread> g_quic_server_thread;
std::unique_ptr<quic::QuicMemoryCacheBackend> g_quic_memory_cache_backend;
std::unique_ptr<net::QuicSimpleServer> g_quic_server;
base::WaitableEvent wait_for_callback(
    base::WaitableEvent::ResetPolicy::AUTOMATIC);

template <typename T>
base::OnceCallback<void()> WrapCallbackWithSignal(
    base::OnceCallback<T> callback) {
  return base::BindOnce(base::IgnoreResult(std::move(callback)))
      .Then(base::BindOnce([] { wait_for_callback.Signal(); }));
}

template <typename T>
void ExecuteSynchronouslyOnServerThread(base::OnceCallback<T> callback,
                                        base::Location from_here = FROM_HERE) {
  CHECK(g_quic_server_thread);
  g_quic_server_thread->task_runner()->PostTask(
      from_here, WrapCallbackWithSignal(std::move(callback)));
  wait_for_callback.Wait();
}

void StartOnServerThread(const base::FilePath& test_files_root,
                         const base::FilePath& test_data_dir) {
  CHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  CHECK(!g_quic_server);
  CHECK(!g_quic_memory_cache_backend);

  // Set up in-memory cache.
  base::FilePath file_dir = test_files_root.Append("quic_data");
  CHECK(base::PathExists(file_dir)) << "Quic data does not exist";
  g_quic_memory_cache_backend =
      std::make_unique<quic::QuicMemoryCacheBackend>();
  g_quic_memory_cache_backend->InitializeBackend(file_dir.value());
  quic::QuicConfig config;

  // Set up server certs.
  base::FilePath directory = test_data_dir.Append("net/data/ssl/certificates");
  std::unique_ptr<net::ProofSourceChromium> proof_source(
      new net::ProofSourceChromium());
  CHECK(proof_source->Initialize(directory.Append("quic-chain.pem"),
                                 directory.Append("quic-leaf-cert.key"),
                                 base::FilePath()));
  g_quic_server = std::make_unique<net::QuicSimpleServer>(
      std::move(proof_source), config,
      quic::QuicCryptoServerConfig::ConfigOptions(),
      quic::AllSupportedVersions(), g_quic_memory_cache_backend.get());

  // Start listening.
  bool rv = g_quic_server->Listen(
      net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kServerPort));
  if (rv) {
    // TODO(crbug.com/40283192): Stop hardcoding server hostname.
    g_quic_memory_cache_backend->AddSpecialResponse(
        base::StringPrintf("%s:%d", "test.example.com", kServerPort),
        kConnectionClosePath,
        quic::QuicBackendResponse::SpecialResponseType::CLOSE_CONNECTION);
  }
  CHECK_GE(rv, 0) << "Quic server fails to start";
}

void SetResponseDelayOnServerThread(const std::string& path,
                                    base::TimeDelta delay) {
  CHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  CHECK(g_quic_memory_cache_backend);

  // TODO(crbug.com/40283192): Stop hardcoding server hostname.
  CHECK(g_quic_memory_cache_backend->SetResponseDelay(
      base::StringPrintf("%s:%d", "test.example.com", kServerPort), path,
      quic::QuicTime::Delta::FromMicroseconds(delay.InMicroseconds())));
}

void ShutdownOnServerThread() {
  CHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  CHECK(g_quic_server);
  CHECK(g_quic_memory_cache_backend);
  g_quic_server->Shutdown();
  g_quic_server.reset();
  g_quic_memory_cache_backend.reset();
}

}  // namespace

// Quic server is currently hardcoded to run on port 6121 of the localhost on
// the device.
void JNI_QuicTestServer_StartQuicTestServer(
    JNIEnv* env,
    const JavaParamRef<jstring>& jtest_files_root,
    const JavaParamRef<jstring>& jtest_data_dir) {
  CHECK(!g_quic_server_thread);
  base::FilePath test_data_dir(
      base::android::ConvertJavaStringToUTF8(env, jtest_data_dir));
  base::InitAndroidTestPaths(test_data_dir);
  g_quic_server_thread = std::make_unique<base::Thread>("quic server thread");
  base::Thread::Options thread_options;
  thread_options.message_pump_type = base::MessagePumpType::IO;
  bool started =
      g_quic_server_thread->StartWithOptions(std::move(thread_options));
  CHECK(started);
  base::FilePath test_files_root(
      base::android::ConvertJavaStringToUTF8(env, jtest_files_root));
  ExecuteSynchronouslyOnServerThread(
      base::BindOnce(&StartOnServerThread, test_files_root, test_data_dir));
}

ScopedJavaLocalRef<jstring> JNI_QuicTestServer_GetConnectionClosePath(
    JNIEnv* env) {
  return base::android::ConvertUTF8ToJavaString(env, kConnectionClosePath);
}

void JNI_QuicTestServer_ShutdownQuicTestServer(JNIEnv* env) {
  CHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  ExecuteSynchronouslyOnServerThread(base::BindOnce(&ShutdownOnServerThread));
  g_quic_server_thread.reset();
}

int JNI_QuicTestServer_GetServerPort(JNIEnv* env) {
  return kServerPort;
}

void JNI_QuicTestServer_DelayResponse(JNIEnv* env,
                                      const JavaParamRef<jstring>& jpath,
                                      int delayInSeconds) {
  CHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
  std::string path = base::android::ConvertJavaStringToUTF8(env, jpath);
  base::TimeDelta delay = base::Seconds(delayInSeconds);
  ExecuteSynchronouslyOnServerThread(
      base::BindOnce(&SetResponseDelayOnServerThread, path, delay));
}

int JNI_QuicTestServer_NumSessions(JNIEnv* env) {
  CHECK(g_quic_server);
  return g_quic_server->NumSessions();
}

}  // namespace cronet