Skip to content

Commit

Permalink
mobile: Rewrite HttpProxyTestServer JNI code
Browse files Browse the repository at this point in the history
Signed-off-by: Fredy Wijaya <fredyw@google.com>
  • Loading branch information
fredyw committed Mar 28, 2024
1 parent f6ceaf7 commit a591f42
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 170 deletions.
18 changes: 15 additions & 3 deletions mobile/test/common/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ envoy_cc_test_library(

# interface libs for test servers` jni implementations
envoy_cc_test_library(
name = "test_server_interface_lib",
name = "test_server_lib",
srcs = [
"test_server.cc",
"test_server_interface.cc",
],
hdrs = [
"test_server.h",
"test_server_interface.h",
],
data = [
"@envoy//test/config/integration/certs",
Expand All @@ -195,6 +193,20 @@ envoy_cc_test_library(
),
)

envoy_cc_test_library(
name = "test_server_interface_lib",
srcs = [
"test_server_interface.cc",
],
hdrs = [
"test_server_interface.h",
],
repository = "@envoy",
deps = [
":test_server_lib",
],
)

envoy_cc_test_library(
name = "xds_test_server_interface_lib",
srcs = [
Expand Down
16 changes: 16 additions & 0 deletions mobile/test/java/io/envoyproxy/envoymobile/engine/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ android_library(
],
)

android_library(
name = "http_proxy_test_server_factory_lib",
testonly = True,
srcs = [
"HttpProxyTestServerFactory.java",
],
data = [
"//test/jni:libenvoy_jni_http_proxy_test_server_factory.so",
],
visibility = ["//test:__subpackages__"],
deps = [
"//library/java/io/envoyproxy/envoymobile/engine:envoy_base_engine_lib",
"//library/kotlin/io/envoyproxy/envoymobile:envoy_lib",
],
)

envoy_mobile_android_test(
name = "quic_test_server_test",
srcs = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.envoyproxy.envoymobile.engine.testing;

/** An HTTP proxy test server factory. */
public final class HttpProxyTestServerFactory {
/** The supported {@link HttpProxyTestServer} types. */
public static class Type {
public static final int HTTP_PROXY = 3;
public static final int HTTPS_PROXY = 4;

private Type() {}
}

/** The instance of {@link HttpProxyTestServer}. */
public static class HttpProxyTestServer {
private final long handle; // Used by the native code.
private final int port;

private HttpProxyTestServer(long handle, int port) {
this.handle = handle;
this.port = port;
}

/** Returns the server port. */
public int getPort() { return port; }

/** Shuts down the server. */
public native void shutdown();
}

static { System.loadLibrary("envoy_jni_http_proxy_test_server_factory"); }

/**
* Starts the HTTP proxy server.
*
* @param type the value in {@link HttpProxyTestServerFactory.Type}
*/
public static native HttpProxyTestServer start(int type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public static class Type {
public static final int HTTP1_WITHOUT_TLS = 0;
public static final int HTTP2_WITH_TLS = 1;
public static final int HTTP3 = 2;
public static final int HTTP_PROXY = 3;
public static final int HTTPS_PROXY = 4;

private Type() {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,8 @@
* Wrapper class for test JNI functions
*/
public final class TestJni {

private static final AtomicBoolean sServerRunning = new AtomicBoolean();
private static final AtomicBoolean xdsServerRunning = new AtomicBoolean();

/**
* Initializes an envoy server which will terminate cleartext CONNECT requests.
*
* @throws IllegalStateException if it's already started.
*/
public static void startHttpProxyTestServer() {
if (!sServerRunning.compareAndSet(false, true)) {
throw new IllegalStateException("Server is already running");
}
nativeStartHttpProxyTestServer();
}

/**
* Initializes an envoy server which will terminate encrypted CONNECT requests.
*
* @throws IllegalStateException if it's already started.
*/
public static void startHttpsProxyTestServer() {
if (!sServerRunning.compareAndSet(false, true)) {
throw new IllegalStateException("Server is already running");
}
nativeStartHttpsProxyTestServer();
}

/**
* Initializes the xDS test server.
*
Expand All @@ -47,26 +21,6 @@ public static void initXdsTestServer() {
nativeInitXdsTestServer();
}

/*
* Starts the server. Throws an {@link IllegalStateException} if already started.
*/
public static void startHttp3TestServer() {
if (!sServerRunning.compareAndSet(false, true)) {
throw new IllegalStateException("Server is already running");
}
nativeStartHttp3TestServer();
}

/*
* Starts the server. Throws an {@link IllegalStateException} if already started.
*/
public static void startHttp2TestServer() {
if (!sServerRunning.compareAndSet(false, true)) {
throw new IllegalStateException("Server is already running");
}
nativeStartHttp2TestServer();
}

/**
* Starts the xDS test server.
*
Expand All @@ -89,16 +43,6 @@ public static void sendDiscoveryResponse(String yaml) {
nativeSendDiscoveryResponse(yaml);
}

/**
* Shutdowns the server. No-op if the server is already shutdown.
*/
public static void shutdownTestServer() {
if (!sServerRunning.compareAndSet(true, false)) {
return;
}
nativeShutdownTestServer();
}

/**
* Shutdowns the xDS test server. No-op if the server is already shutdown.
*/
Expand All @@ -109,12 +53,6 @@ public static void shutdownXdsTestServer() {
nativeShutdownXdsTestServer();
}

public static String getServerURL() {
return "https://" + getServerHost() + ":" + getServerPort();
}

public static String getServerHost() { return "test.example.com"; }

/**
* Gets the xDS test server host.
*/
Expand All @@ -125,32 +63,10 @@ public static String getServerURL() {
*/
public static int getXdsTestServerPort() { return nativeGetXdsTestServerPort(); }

/**
* Returns the server attributed port. Throws an {@link IllegalStateException} if not started.
*/
public static int getServerPort() {
if (!sServerRunning.get()) {
throw new IllegalStateException("Server not started.");
}
return nativeGetServerPort();
}

public static String createYaml(EnvoyConfiguration envoyConfiguration) {
return nativeCreateYaml(envoyConfiguration.createBootstrap());
}

private static native void nativeStartHttp3TestServer();

private static native void nativeStartHttp2TestServer();

private static native void nativeShutdownTestServer();

private static native int nativeGetServerPort();

private static native void nativeStartHttpProxyTestServer();

private static native void nativeStartHttpsProxyTestServer();

private static native void nativeInitXdsTestServer();

private static native String nativeGetXdsTestServerHost();
Expand Down
35 changes: 33 additions & 2 deletions mobile/test/jni/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cc_library(
}),
deps = [
"//library/jni:envoy_jni_lib",
"//test/common/integration:test_server_interface_lib",
"//test/common/integration:test_server_lib",
"//test/common/integration:xds_test_server_interface_lib",
],
# We need this to ensure that we link this into the .so even though there are no code references.
Expand All @@ -51,7 +51,7 @@ cc_library(
"//library/jni:envoy_jni_lib",
"//library/jni:jni_helper_lib",
"//library/jni:jni_utility_lib",
"//test/common/integration:test_server_interface_lib",
"//test/common/integration:test_server_lib",
],
# We need this to ensure that we link this into the .so even though there are no code references.
alwayslink = True,
Expand All @@ -67,6 +67,37 @@ cc_binary(
],
)

# Library which contains JNI functions for the HttpProxyTestServer.
cc_library(
name = "jni_http_proxy_test_server_factory_lib",
testonly = True,
srcs = [
"jni_http_proxy_test_server_factory.cc",
],
linkopts = select({
"@envoy//bazel:dbg_build": ["-Wl,--build-id=sha1"],
"//conditions:default": [],
}),
deps = [
"//library/jni:envoy_jni_lib",
"//library/jni:jni_helper_lib",
"//library/jni:jni_utility_lib",
"//test/common/integration:test_server_lib",
],
# We need this to ensure that we link this into the .so even though there are no code references.
alwayslink = True,
)

cc_binary(
name = "libenvoy_jni_http_proxy_test_server_factory.so",
testonly = True,
linkshared = True,
deps = [
":jni_http_proxy_test_server_factory_lib",
"@envoy_mobile_extra_jni_deps//:extra_jni_dep",
],
)

# Base binary (.so) for testing
cc_binary(
name = "libenvoy_jni_with_test_extensions.so",
Expand Down
41 changes: 41 additions & 0 deletions mobile/test/jni/jni_http_proxy_test_server_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <jni.h>

#include "test/common/integration/test_server.h"

#include "extension_registry.h"
#include "library/jni/jni_helper.h"
#include "library/jni/jni_utility.h"

// NOLINT(namespace-envoy)

extern "C" JNIEXPORT jobject JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_HttpProxyTestServerFactory_start(JNIEnv* env, jclass,
jint type) {
Envoy::JNI::JniHelper jni_helper(env);

Envoy::ExtensionRegistry::registerFactories();
Envoy::TestServer* test_server = new Envoy::TestServer();
test_server->startTestServer(static_cast<Envoy::TestServerType>(type));

auto java_http_proxy_server_factory_class = jni_helper.findClass(
"io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory$HttpProxyTestServer");
auto java_init_method_id =
jni_helper.getMethodId(java_http_proxy_server_factory_class.get(), "<init>", "(JI)V");
int port = test_server->getServerPort();
return jni_helper
.newObject(java_http_proxy_server_factory_class.get(), java_init_method_id,
reinterpret_cast<jlong>(test_server), static_cast<jint>(port))
.release();
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_HttpProxyTestServerFactory_00024HttpProxyTestServer_shutdown(
JNIEnv* env, jobject instance) {
Envoy::JNI::JniHelper jni_helper(env);
auto java_class = jni_helper.getObjectClass(instance);
auto java_handle_field_id = jni_helper.getFieldId(java_class.get(), "handle", "J");
jlong java_handle = jni_helper.getLongField(instance, java_handle_field_id);
Envoy::TestServer* test_server = reinterpret_cast<Envoy::TestServer*>(java_handle);
test_server->shutdownTestServer();
delete test_server;
}
39 changes: 0 additions & 39 deletions mobile/test/jni/test_jni_impl.cc
Original file line number Diff line number Diff line change
@@ -1,51 +1,12 @@
#include <jni.h>

#include "test/common/integration/test_server_interface.h"
#include "test/common/integration/xds_test_server_interface.h"
#include "test/test_common/utility.h"

#include "library/jni/jni_support.h"

// NOLINT(namespace-envoy)

// Quic Test ServerJniLibrary

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeStartHttpProxyTestServer(JNIEnv* env,
jclass clazz) {
start_server(Envoy::TestServerType::HTTP_PROXY);
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeStartHttpsProxyTestServer(
JNIEnv* env, jclass clazz) {
start_server(Envoy::TestServerType::HTTPS_PROXY);
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeStartHttp3TestServer(JNIEnv* env,
jclass clazz) {
start_server(Envoy::TestServerType::HTTP3);
}

extern "C" JNIEXPORT jint JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeGetServerPort(JNIEnv* env,
jclass clazz) {
return get_server_port();
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeStartHttp2TestServer(JNIEnv* env,
jclass clazz) {
start_server(Envoy::TestServerType::HTTP2_WITH_TLS);
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeShutdownTestServer(JNIEnv* env,
jclass clazz) {
shutdown_server();
}

extern "C" JNIEXPORT void JNICALL
Java_io_envoyproxy_envoymobile_engine_testing_TestJni_nativeInitXdsTestServer(JNIEnv* env,
jclass clazz) {
Expand Down
1 change: 1 addition & 0 deletions mobile/test/kotlin/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ envoy_mobile_android_test(
deps = [
"//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib",
"//test/java/io/envoyproxy/envoymobile/engine/testing",
"//test/java/io/envoyproxy/envoymobile/engine/testing:http_test_server_factory_lib",
],
)

Expand Down
Loading

0 comments on commit a591f42

Please sign in to comment.