From 0ec438203d9f28ce81a7b56c4e899c640ed7ec57 Mon Sep 17 00:00:00 2001 From: Fredy Wijaya Date: Thu, 28 Mar 2024 14:53:24 -0500 Subject: [PATCH] mobile: Remove HTTP server code from TestJni (#33169) This PR removes the HTTP server code from TestJni. The tests have been updated to use either HttpTestServer or HttpProxyTestServer. This PR also moves test_server_interface.[h|cc] into its own build target. Risk Level: low (tests only) Testing: unit tests Docs Changes: n/a Release Notes: n/a Platform Specific Features: mobile Signed-off-by: Fredy Wijaya --- mobile/.bazelrc | 2 +- mobile/test/common/integration/BUILD | 18 +++- .../envoymobile/engine/testing/BUILD | 16 ++++ .../testing/HttpProxyTestServerFactory.java | 38 +++++++++ .../engine/testing/HttpTestServerFactory.java | 2 - .../envoymobile/engine/testing/TestJni.java | 84 ------------------- mobile/test/jni/BUILD | 35 +++++++- .../jni/jni_http_proxy_test_server_factory.cc | 41 +++++++++ mobile/test/jni/test_jni_impl.cc | 39 --------- mobile/test/kotlin/integration/BUILD | 1 + .../test/kotlin/integration/SendDataTest.kt | 23 +++-- mobile/test/kotlin/integration/proxying/BUILD | 6 ++ ...oIntentPerformHTTPRequestUsingProxyTest.kt | 23 +++-- ...ntentPerformHTTPSRequestBadHostnameTest.kt | 23 +++-- ...tPerformHTTPSRequestUsingAsyncProxyTest.kt | 23 +++-- ...IntentPerformHTTPSRequestUsingProxyTest.kt | 23 +++-- ...oxyPollPerformHTTPRequestUsingProxyTest.kt | 23 +++-- ...formHTTPRequestWithoutUsingPACProxyTest.kt | 20 ++++- 18 files changed, 269 insertions(+), 171 deletions(-) create mode 100644 mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory.java create mode 100644 mobile/test/jni/jni_http_proxy_test_server_factory.cc diff --git a/mobile/.bazelrc b/mobile/.bazelrc index c753f4b5195ec..3647adb588712 100644 --- a/mobile/.bazelrc +++ b/mobile/.bazelrc @@ -261,8 +261,8 @@ build:mobile-remote-ci --config=remote-ci build:mobile-remote-ci-android --config=mobile-remote-ci test:mobile-remote-ci-android --build_tests_only -test:mobile-remote-ci-android --config=mobile-test-android test:mobile-remote-ci-android --config=mobile-remote-ci +test:mobile-remote-ci-android --config=mobile-test-android build:mobile-remote-ci-cc --config=mobile-remote-ci # Temporary revert to C++17 for mobile NDK builds. diff --git a/mobile/test/common/integration/BUILD b/mobile/test/common/integration/BUILD index 9493c555350ac..0a74d56b6b10c 100644 --- a/mobile/test/common/integration/BUILD +++ b/mobile/test/common/integration/BUILD @@ -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", @@ -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 = [ diff --git a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/BUILD b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/BUILD index b6f7d4785985f..f00e319f1083b 100644 --- a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/BUILD +++ b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/BUILD @@ -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 = [ diff --git a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory.java b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory.java new file mode 100644 index 0000000000000..5d73f3943ff30 --- /dev/null +++ b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory.java @@ -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); +} diff --git a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpTestServerFactory.java b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpTestServerFactory.java index b47fb92e6a092..7b164e3f45aac 100644 --- a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpTestServerFactory.java +++ b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/HttpTestServerFactory.java @@ -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() {} } diff --git a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/TestJni.java b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/TestJni.java index 1b2e7cc85f4ea..e80df3f55b392 100644 --- a/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/TestJni.java +++ b/mobile/test/java/io/envoyproxy/envoymobile/engine/testing/TestJni.java @@ -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. * @@ -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. * @@ -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. */ @@ -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. */ @@ -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(); diff --git a/mobile/test/jni/BUILD b/mobile/test/jni/BUILD index 5f2171f6b6c68..48c6f053bc027 100644 --- a/mobile/test/jni/BUILD +++ b/mobile/test/jni/BUILD @@ -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. @@ -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, @@ -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", diff --git a/mobile/test/jni/jni_http_proxy_test_server_factory.cc b/mobile/test/jni/jni_http_proxy_test_server_factory.cc new file mode 100644 index 0000000000000..339d5f1112ec0 --- /dev/null +++ b/mobile/test/jni/jni_http_proxy_test_server_factory.cc @@ -0,0 +1,41 @@ +#include + +#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(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(), "", "(JI)V"); + int port = test_server->getServerPort(); + return jni_helper + .newObject(java_http_proxy_server_factory_class.get(), java_init_method_id, + reinterpret_cast(test_server), static_cast(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(java_handle); + test_server->shutdownTestServer(); + delete test_server; +} diff --git a/mobile/test/jni/test_jni_impl.cc b/mobile/test/jni/test_jni_impl.cc index 5e7477d47554a..fc3054b269f62 100644 --- a/mobile/test/jni/test_jni_impl.cc +++ b/mobile/test/jni/test_jni_impl.cc @@ -1,6 +1,5 @@ #include -#include "test/common/integration/test_server_interface.h" #include "test/common/integration/xds_test_server_interface.h" #include "test/test_common/utility.h" @@ -8,44 +7,6 @@ // 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) { diff --git a/mobile/test/kotlin/integration/BUILD b/mobile/test/kotlin/integration/BUILD index 3fadd88c5c992..6499eecc513c5 100644 --- a/mobile/test/kotlin/integration/BUILD +++ b/mobile/test/kotlin/integration/BUILD @@ -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", ], ) diff --git a/mobile/test/kotlin/integration/SendDataTest.kt b/mobile/test/kotlin/integration/SendDataTest.kt index b4f59e44c8324..ebdf4b0d0c0e4 100644 --- a/mobile/test/kotlin/integration/SendDataTest.kt +++ b/mobile/test/kotlin/integration/SendDataTest.kt @@ -8,11 +8,14 @@ import io.envoyproxy.envoymobile.Standard import io.envoyproxy.envoymobile.engine.AndroidJniLibrary import io.envoyproxy.envoymobile.engine.EnvoyConfiguration.TrustChainVerification import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpTestServerFactory +import io.envoyproxy.envoymobile.engine.testing.HttpTestServerFactory.HttpTestServer import java.nio.ByteBuffer import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit +import org.junit.After import org.junit.Assert.fail +import org.junit.Before import org.junit.Test private const val ASSERTION_FILTER_TYPE = @@ -25,11 +28,20 @@ class SendDataTest { JniLibrary.load() } + private lateinit var httpTestServer: HttpTestServer + + @Before + fun setUp() { + httpTestServer = HttpTestServerFactory.start(HttpTestServerFactory.Type.HTTP2_WITH_TLS) + } + + @After + fun tearDown() { + httpTestServer.shutdown() + } + @Test fun `successful sending data`() { - TestJni.startHttp2TestServer() - val port = TestJni.getServerPort() - val expectation = CountDownLatch(1) val engine = EngineBuilder(Standard()) @@ -46,7 +58,7 @@ class SendDataTest { RequestHeadersBuilder( method = RequestMethod.GET, scheme = "https", - authority = "localhost:$port", + authority = "localhost:${httpTestServer.port}", path = "/simple.txt" ) .build() @@ -71,7 +83,6 @@ class SendDataTest { expectation.await(10, TimeUnit.SECONDS) engine.terminate() - TestJni.shutdownTestServer() assertThat(expectation.count).isEqualTo(0) assertThat(responseStatus).isEqualTo(200) diff --git a/mobile/test/kotlin/integration/proxying/BUILD b/mobile/test/kotlin/integration/proxying/BUILD index 5e06812b1646f..492baa548d902 100644 --- a/mobile/test/kotlin/integration/proxying/BUILD +++ b/mobile/test/kotlin/integration/proxying/BUILD @@ -28,6 +28,7 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) @@ -54,6 +55,7 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) @@ -80,6 +82,7 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) @@ -106,6 +109,7 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) @@ -132,6 +136,7 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) @@ -158,5 +163,6 @@ envoy_mobile_android_test( "//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib", "//library/kotlin/io/envoyproxy/envoymobile:envoy_lib", "//test/java/io/envoyproxy/envoymobile/engine/testing", + "//test/java/io/envoyproxy/envoymobile/engine/testing:http_proxy_test_server_factory_lib", ], ) diff --git a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPRequestUsingProxyTest.kt b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPRequestUsingProxyTest.kt index b771f075245e0..e0c2334c38128 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPRequestUsingProxyTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPRequestUsingProxyTest.kt @@ -12,10 +12,12 @@ import io.envoyproxy.envoymobile.LogLevel import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito @@ -39,18 +41,28 @@ class ProxyInfoIntentPerformHTTPRequestUsingProxyTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTP_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Test fun `performs an HTTP request through a proxy`() { - TestJni.startHttpProxyTestServer() - val port = TestJni.getServerPort() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) .`when`(context) .getSystemService(Context.CONNECTIVITY_SERVICE) Mockito.`when`(connectivityManager.defaultProxy) - .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", port)) + .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", httpProxyTestServer.port)) val onEngineRunningLatch = CountDownLatch(1) val onRespondeHeadersLatch = CountDownLatch(1) @@ -93,6 +105,5 @@ class ProxyInfoIntentPerformHTTPRequestUsingProxyTest { assertThat(onRespondeHeadersLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } } diff --git a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestBadHostnameTest.kt b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestBadHostnameTest.kt index e0baa24faa055..ccb9c94968df9 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestBadHostnameTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestBadHostnameTest.kt @@ -12,10 +12,12 @@ import io.envoyproxy.envoymobile.LogLevel import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito @@ -39,18 +41,28 @@ class ProxyInfoIntentPerformHTTPSRequestBadHostnameTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTPS_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Test fun `attempts an HTTPs request through a proxy using an async DNS resolution that fails`() { - TestJni.startHttpsProxyTestServer() - val port = TestJni.getServerPort() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) .`when`(context) .getSystemService(Context.CONNECTIVITY_SERVICE) Mockito.`when`(connectivityManager.defaultProxy) - .thenReturn(ProxyInfo.buildDirectProxy("loopback", port)) + .thenReturn(ProxyInfo.buildDirectProxy("loopback", httpProxyTestServer.port)) val onEngineRunningLatch = CountDownLatch(1) val onErrorLatch = CountDownLatch(1) @@ -88,6 +100,5 @@ class ProxyInfoIntentPerformHTTPSRequestBadHostnameTest { assertThat(onErrorLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } } diff --git a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest.kt b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest.kt index 2af1a663feda8..94b8b606fcf3c 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest.kt @@ -12,10 +12,12 @@ import io.envoyproxy.envoymobile.LogLevel import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith @@ -40,19 +42,29 @@ class ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTPS_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Ignore("https://github.com/envoyproxy/envoy/issues/33014") @Test fun `performs an HTTPs request through a proxy using async DNS resolution`() { - TestJni.startHttpsProxyTestServer() - val port = TestJni.getServerPort() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) .`when`(context) .getSystemService(Context.CONNECTIVITY_SERVICE) Mockito.`when`(connectivityManager.defaultProxy) - .thenReturn(ProxyInfo.buildDirectProxy("localhost", port)) + .thenReturn(ProxyInfo.buildDirectProxy("localhost", httpProxyTestServer.port)) val onEngineRunningLatch = CountDownLatch(1) val onRespondeHeadersLatch = CountDownLatch(1) @@ -95,6 +107,5 @@ class ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest { assertThat(onRespondeHeadersLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } } diff --git a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingProxyTest.kt b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingProxyTest.kt index 5521050f40379..cf804385c9b00 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingProxyTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyInfoIntentPerformHTTPSRequestUsingProxyTest.kt @@ -12,10 +12,12 @@ import io.envoyproxy.envoymobile.LogLevel import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith @@ -40,19 +42,29 @@ class ProxyInfoIntentPerformHTTPSRequestUsingProxyTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTPS_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Ignore("https://github.com/envoyproxy/envoy/issues/33014") @Test fun `performs an HTTPs request through a proxy`() { - TestJni.startHttpsProxyTestServer() - val port = TestJni.getServerPort() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) .`when`(context) .getSystemService(Context.CONNECTIVITY_SERVICE) Mockito.`when`(connectivityManager.defaultProxy) - .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", port)) + .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", httpProxyTestServer.port)) val onEngineRunningLatch = CountDownLatch(1) val onRespondeHeadersLatch = CountDownLatch(1) @@ -95,6 +107,5 @@ class ProxyInfoIntentPerformHTTPSRequestUsingProxyTest { assertThat(onRespondeHeadersLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } } diff --git a/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestUsingProxyTest.kt b/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestUsingProxyTest.kt index cb23afc6c3dc2..6dc294ecd8091 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestUsingProxyTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestUsingProxyTest.kt @@ -11,10 +11,12 @@ import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.AndroidJniLibrary import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito @@ -39,18 +41,28 @@ class ProxyPollPerformHTTPRequestUsingProxyTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTP_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Test fun `performs an HTTP request through a proxy`() { - TestJni.startHttpProxyTestServer() - val port = TestJni.getServerPort() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) .`when`(context) .getSystemService(Context.CONNECTIVITY_SERVICE) Mockito.`when`(connectivityManager.defaultProxy) - .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", port)) + .thenReturn(ProxyInfo.buildDirectProxy("127.0.0.1", httpProxyTestServer.port)) val onEngineRunningLatch = CountDownLatch(1) val onRespondeHeadersLatch = CountDownLatch(1) @@ -91,6 +103,5 @@ class ProxyPollPerformHTTPRequestUsingProxyTest { assertThat(onRespondeHeadersLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } } diff --git a/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest.kt b/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest.kt index 21a08ad18722a..3266bac86be25 100644 --- a/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest.kt +++ b/mobile/test/kotlin/integration/proxying/ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest.kt @@ -11,10 +11,12 @@ import io.envoyproxy.envoymobile.LogLevel import io.envoyproxy.envoymobile.RequestHeadersBuilder import io.envoyproxy.envoymobile.RequestMethod import io.envoyproxy.envoymobile.engine.JniLibrary -import io.envoyproxy.envoymobile.engine.testing.TestJni +import io.envoyproxy.envoymobile.engine.testing.HttpProxyTestServerFactory import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import org.junit.After +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito @@ -40,10 +42,21 @@ class ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest { JniLibrary.load() } + private lateinit var httpProxyTestServer: HttpProxyTestServerFactory.HttpProxyTestServer + + @Before + fun setUp() { + httpProxyTestServer = + HttpProxyTestServerFactory.start(HttpProxyTestServerFactory.Type.HTTP_PROXY) + } + + @After + fun tearDown() { + httpProxyTestServer.shutdown() + } + @Test fun `performs an HTTP request through a proxy`() { - TestJni.startHttpProxyTestServer() - val context = Mockito.spy(ApplicationProvider.getApplicationContext()) val connectivityManager: ConnectivityManager = Mockito.mock(ConnectivityManager::class.java) Mockito.doReturn(connectivityManager) @@ -91,6 +104,5 @@ class ProxyPollPerformHTTPRequestWithoutUsingPACProxyTest { assertThat(onRespondeHeadersLatch.count).isEqualTo(0) engine.terminate() - TestJni.shutdownTestServer() } }