-
Notifications
You must be signed in to change notification settings - Fork 85
Cloned QuicTestServer #1989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cloned QuicTestServer #1989
Changes from 62 commits
6e5fca3
51bab1c
15cfb05
91e1af8
a49baf9
c608e2e
22bc6bb
cec798e
1e98c4d
8a6208f
bc01022
189e3e9
7511986
2b61716
75f52ef
9004b8c
32401c4
4864676
69983dd
0789bec
c6ef82a
7aca613
79819e2
ad9170b
d09958c
4123d56
cb3e22b
9836d38
05c30c8
03c2053
274aae7
4bc0fd9
f6e5b72
34c70be
5b6dd43
02fd58f
701395b
f1a9a75
3b40d92
1fae837
71748ef
e807c7f
ea28dc4
ba0d57c
1c65b1c
3b279a8
b4ed90d
4f4c930
8109c4e
8f846be
8a0accb
d8c2585
be6bdfb
29aabd1
1b0ffc9
16f14c9
fbfa612
9ccb763
2e8cdcf
097f6f4
3373d94
8b6e8f2
1b4321e
6e998da
e6d6b26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #include "quic_test_server.h" | ||
|
|
||
| #include "test/test_common/environment.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| Network::TransportSocketFactoryPtr QuicTestServer::createUpstreamTlsContext( | ||
| testing::NiceMock<Server::Configuration::MockTransportSocketFactoryContext>& factory_context) { | ||
| envoy::extensions::transport_sockets::tls::v3::DownstreamTlsContext tls_context; | ||
| Extensions::TransportSockets::Tls::ContextManagerImpl context_manager{time_system_}; | ||
| const std::string yaml = absl::StrFormat( | ||
| R"EOF( | ||
| common_tls_context: | ||
| alpn_protocols: h3 | ||
| tls_certificates: | ||
| - certificate_chain: | ||
| filename: %s | ||
| private_key: | ||
| filename: %s | ||
| )EOF", | ||
| "../envoy/test/config/integration/certs/upstreamcert.pem", | ||
| "../envoy/test/config/integration/certs/upstreamkey.pem"); | ||
| TestUtility::loadFromYaml(yaml, tls_context); | ||
| envoy::extensions::transport_sockets::quic::v3::QuicDownstreamTransport quic_config; | ||
| quic_config.mutable_downstream_tls_context()->MergeFrom(tls_context); | ||
|
|
||
| std::vector<std::string> server_names; | ||
| auto& config_factory = Config::Utility::getAndCheckFactoryByName< | ||
| Server::Configuration::DownstreamTransportSocketConfigFactory>( | ||
| "envoy.transport_sockets.quic"); | ||
|
|
||
| return config_factory.createTransportSocketFactory(quic_config, factory_context, server_names); | ||
| } | ||
|
|
||
| QuicTestServer::QuicTestServer() | ||
| : api_(Api::createApiForTest(stats_store_, time_system_)), | ||
| version_(Network::Address::IpVersion::v4), upstream_config_(time_system_), port_(0) { | ||
| ON_CALL(factory_context_, api()).WillByDefault(testing::ReturnRef(*api_)); | ||
| ON_CALL(factory_context_, scope()).WillByDefault(testing::ReturnRef(stats_store_)); | ||
| } | ||
|
|
||
| void QuicTestServer::startQuicTestServer() { | ||
| // pre-setup: see https://github.com/envoyproxy/envoy/blob/main/test/test_runner.cc | ||
| Logger::Context logging_state(spdlog::level::level_enum::err, | ||
| "[%Y-%m-%d %T.%e][%t][%l][%n] [%g:%#] %v", lock, false, false); | ||
| // end pre-setup | ||
|
|
||
| upstream_config_.upstream_protocol_ = Http::CodecType::HTTP3; | ||
| upstream_config_.udp_fake_upstream_ = FakeUpstreamConfig::UdpConfig(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Could these both be done in the constructor since they don't seem to change per start()?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved line 49, but kept line 48: the upstream_protocol will become a parameter soon. the goal is to also use this server to H2 also. Some renaming will take place later. |
||
|
|
||
| Network::TransportSocketFactoryPtr factory = createUpstreamTlsContext(factory_context_); | ||
|
|
||
| upstream_ = std::make_unique<AutonomousUpstream>(std::move(factory), port_, version_, | ||
| upstream_config_, false); | ||
|
|
||
| // see upstream address | ||
| ENVOY_LOG_MISC(debug, "Upstream now listening on {}", upstream_->localAddress()->asString()); | ||
| } | ||
|
|
||
| void QuicTestServer::shutdownQuicTestServer() { upstream_.reset(); } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this ASSERT(upstream_)?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
|
||
| int QuicTestServer::getServerPort() { return upstream_->localAddress()->ip()->port(); } | ||
| } // namespace Envoy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #pragma once | ||
|
|
||
| #include "source/extensions/transport_sockets/tls/ssl_socket.h" | ||
|
|
||
| // test_runner setups | ||
| #include "source/exe/process_wide.h" | ||
|
|
||
| #include "envoy/extensions/transport_sockets/quic/v3/quic_transport.pb.h" | ||
| #include "test/mocks/server/transport_socket_factory_context.h" | ||
| #include "test/integration/autonomous_upstream.h" | ||
| #include "test/config/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| class QuicTestServer { | ||
| private: | ||
| testing::NiceMock<Server::Configuration::MockTransportSocketFactoryContext> factory_context_; | ||
| Stats::IsolatedStoreImpl stats_store_; | ||
| Event::GlobalTimeSystem time_system_; | ||
| Api::ApiPtr api_; | ||
| Network::Address::IpVersion version_; | ||
| std::unique_ptr<AutonomousUpstream> upstream_; | ||
| FakeUpstreamConfig upstream_config_; | ||
| int port_; | ||
| Thread::SkipAsserts skip_asserts_; | ||
| ProcessWide process_wide; | ||
| Thread::MutexBasicLockable lock; | ||
|
|
||
| Network::TransportSocketFactoryPtr createUpstreamTlsContext( | ||
| testing::NiceMock<Server::Configuration::MockTransportSocketFactoryContext>&); | ||
|
|
||
| public: | ||
| QuicTestServer(); | ||
|
|
||
| void startQuicTestServer(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brief comments here would be nice.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
|
||
| void shutdownQuicTestServer(); | ||
|
|
||
| int getServerPort(); | ||
| }; | ||
|
|
||
| } // namespace Envoy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #include "test/common/integration/quic_test_server_interface.h" | ||
|
|
||
| // NOLINT(namespace-envoy) | ||
|
|
||
| static std::shared_ptr<Envoy::QuicTestServer> strong_quic_test_server_; | ||
| static std::weak_ptr<Envoy::QuicTestServer> weak_quic_test_server_; | ||
|
|
||
| static std::shared_ptr<Envoy::QuicTestServer> quic_test_server() { | ||
| return weak_quic_test_server_.lock(); | ||
| } | ||
|
|
||
| void start_server() { | ||
| strong_quic_test_server_ = std::make_shared<Envoy::QuicTestServer>(); | ||
| weak_quic_test_server_ = strong_quic_test_server_; | ||
|
|
||
| if (auto e = quic_test_server()) { | ||
| e->startQuicTestServer(); | ||
| } | ||
| } | ||
|
|
||
| void shutdown_server() { | ||
| // Reset the primary handle to the test_server, | ||
| // but retain it long enough to synchronously shutdown. | ||
| auto e = strong_quic_test_server_; | ||
| strong_quic_test_server_.reset(); | ||
| e->shutdownQuicTestServer(); | ||
| } | ||
|
|
||
| int get_server_port() { | ||
| if (auto e = quic_test_server()) { | ||
| return e->getServerPort(); | ||
| } | ||
| return -1; // failure | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #pragma once | ||
|
|
||
| #include "test/common/integration/quic_test_server.h" | ||
|
|
||
| // NOLINT(namespace-envoy) | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { // functions | ||
| #endif | ||
|
|
||
| void start_server(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should we have brief comments for these methods?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
|
||
| void shutdown_server(); | ||
|
|
||
| int get_server_port(); | ||
|
|
||
| #ifdef __cplusplus | ||
| } // functions | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| load("@envoy//bazel:envoy_build_system.bzl", "envoy_package") | ||
| load("@rules_cc//cc:defs.bzl", "cc_binary") | ||
| load("//bazel:kotlin_lib.bzl", "envoy_mobile_so_to_jni_lib") | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| envoy_package() | ||
|
|
||
| # OS X binary (.jnilib) for Quic Test Server | ||
| envoy_mobile_so_to_jni_lib( | ||
| name = "libquic_test_server_jni.jnilib", | ||
| testonly = True, | ||
| native_dep = "libquic_test_server_jni.so", | ||
| ) | ||
|
|
||
| # Binary for Quic Test Server | ||
| cc_binary( | ||
| name = "libquic_test_server_jni.so", | ||
| testonly = True, | ||
| srcs = [ | ||
| "quic_test_server_jni_interface.cc", | ||
| "//library/common/jni:android_test_jni_interface.cc", | ||
| "//library/common/jni:jni_interface.cc", | ||
| "@local_jdk//:jni_header", | ||
| ], | ||
| copts = ["-std=c++17"], | ||
| linkshared = True, | ||
| deps = [ | ||
| "//library/common/jni:base_java_jni_lib", | ||
| "//test/common/integration:quic_test_server_interface_lib", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #include <jni.h> | ||
|
|
||
| #include "test/common/integration/quic_test_server_interface.h" | ||
|
|
||
| #include "library/common/jni/jni_support.h" | ||
| #include "library/common/jni/jni_utility.h" | ||
| #include "library/common/jni/jni_version.h" | ||
|
|
||
| // NOLINT(namespace-envoy) | ||
|
|
||
| // Quic Test ServerJniLibrary | ||
|
|
||
| extern "C" JNIEXPORT void JNICALL | ||
| Java_io_envoyproxy_envoymobile_engine_testing_QuicTestServer_nativeStartQuicTestServer( | ||
| JNIEnv* env, jclass clazz) { | ||
| jni_log("[QTS]", "starting server"); | ||
| start_server(); | ||
| } | ||
|
|
||
| extern "C" JNIEXPORT jint JNICALL | ||
| Java_io_envoyproxy_envoymobile_engine_testing_QuicTestServer_nativeGetServerPort(JNIEnv* env, | ||
| jclass clazz) { | ||
| jni_log("[QTS]", "getting server port"); | ||
| return get_server_port(); | ||
| } | ||
|
|
||
| extern "C" JNIEXPORT void JNICALL | ||
| Java_io_envoyproxy_envoymobile_engine_testing_QuicTestServer_nativeShutdownQuicTestServer( | ||
| JNIEnv* env, jclass clazz) { | ||
| jni_log("[QTS]", "shutting down server"); | ||
| shutdown_server(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| load("@envoy//bazel:envoy_build_system.bzl", "envoy_package") | ||
| load("@envoy_mobile//bazel:kotlin_test.bzl", "envoy_mobile_android_test") | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| envoy_package() | ||
|
|
||
| android_library( | ||
| name = "testing", | ||
| testonly = True, | ||
| srcs = [ | ||
| "QuicTestServer.java", | ||
| ], | ||
| visibility = ["//test:__subpackages__"], | ||
| ) | ||
|
|
||
| envoy_mobile_android_test( | ||
| name = "quic_test_server_test", | ||
| srcs = [ | ||
| "QuicTestServerTest.java", | ||
| ], | ||
| exec_properties = { | ||
| # TODO(lfpino): Remove this once the sandboxNetwork=off works for ipv4 localhost addresses. | ||
| "sandboxNetwork": "standard", | ||
| }, | ||
| library_path = "test/common/jni", | ||
| native_deps = [ | ||
| "//test/common/jni:libquic_test_server_jni.so", | ||
| "//test/common/jni:libquic_test_server_jni.jnilib", | ||
| ], | ||
| deps = [ | ||
| ":testing", | ||
| "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package io.envoyproxy.envoymobile.engine.testing; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** | ||
| * Wrapper class to start a Quic test server. | ||
| */ | ||
| public final class QuicTestServer { | ||
|
|
||
| private static final String CERT_USED = "upstreamcert.pem"; | ||
| private static final String KEY_USED = "upstreamkey.pem"; | ||
|
|
||
| private static final AtomicBoolean sServerRunning = new AtomicBoolean(); | ||
|
|
||
| /* | ||
| * Starts the server. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment what happens if the server is already running?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| */ | ||
| public static void startQuicTestServer() { | ||
| if (!sServerRunning.compareAndSet(false, true)) { | ||
| throw new IllegalStateException("Quic server is already running"); | ||
| } | ||
| nativeStartQuicTestServer(); | ||
| } | ||
|
|
||
| /** | ||
| * Shuts down the server. No-op if the server is already shut down. | ||
| */ | ||
| public static void shutdownQuicTestServer() { | ||
| if (!sServerRunning.compareAndSet(true, false)) { | ||
| return; | ||
| } | ||
| nativeShutdownQuicTestServer(); | ||
| } | ||
|
|
||
| public static String getServerURL() { | ||
| return "https://" + getServerHost() + ":" + getServerPort() + "/"; | ||
| } | ||
|
|
||
| public static String getServerHost() { return "test.example.com"; } | ||
|
|
||
| public static int getServerPort() { return nativeGetServerPort(); } | ||
|
|
||
| public static final String getServerCert() { return CERT_USED; } | ||
|
|
||
| public static final String getServerCertKey() { return KEY_USED; } | ||
|
|
||
| public static long createMockCertVerifier() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, what does the return value represent?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK - removed :-) |
||
| // to be implemented | ||
| return 0L; | ||
| } | ||
|
|
||
| private static native void nativeStartQuicTestServer(); | ||
|
|
||
| private static native void nativeShutdownQuicTestServer(); | ||
|
|
||
| private static native int nativeGetServerPort(); | ||
|
|
||
| private QuicTestServer() {} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this perhaps ASSERT(!upstream_)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done