Skip to content

Commit

Permalink
mobile: Fix HTTPS proxy Android tests (envoyproxy#35883)
Browse files Browse the repository at this point in the history
Fixes envoyproxy#33014

Risk Level: low
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw authored Aug 28, 2024
1 parent 779e69e commit 8f5eca5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
30 changes: 19 additions & 11 deletions mobile/test/common/integration/test_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
namespace Envoy {
namespace {

std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> baseProxyConfig(bool http, int port) {
std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap>
baseProxyConfig(Network::Address::IpVersion version, bool http, int port) {
std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> bootstrap =
std::make_unique<envoy::config::bootstrap::v3::Bootstrap>();

Expand All @@ -39,7 +40,8 @@ std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> baseProxyConfig(bool ht
listener->set_name("base_api_listener");
auto* base_address = listener->mutable_address();
base_address->mutable_socket_address()->set_protocol(envoy::config::core::v3::SocketAddress::TCP);
base_address->mutable_socket_address()->set_address("127.0.0.1");
base_address->mutable_socket_address()->set_address(
version == Network::Address::IpVersion::v4 ? "127.0.0.1" : "::1");
base_address->mutable_socket_address()->set_port_value(port);

envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager hcm;
Expand All @@ -55,7 +57,9 @@ std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> baseProxyConfig(bool ht
route->mutable_match()->set_prefix("/");
} else {
route->mutable_match()->mutable_connect_matcher();
route->mutable_route()->add_upgrade_configs()->set_upgrade_type("CONNECT");
auto* upgrade_config = route->mutable_route()->add_upgrade_configs();
upgrade_config->set_upgrade_type("CONNECT");
upgrade_config->mutable_connect_config();
}

auto* header_value_option = route->mutable_response_headers_to_add()->Add();
Expand Down Expand Up @@ -179,9 +183,9 @@ void TestServer::start(TestServerType type, int port) {
registerMobileProtoDescriptors();
#endif
test_server_ = IntegrationTestServer::create(
"", Network::Address::IpVersion::v4, nullptr, nullptr, {}, time_system_, *api_, false,
absl::nullopt, Server::FieldValidationConfig(), 1, std::chrono::seconds(1),
Server::DrainStrategy::Gradual, nullptr, false, false, baseProxyConfig(true, port_));
"", version_, nullptr, nullptr, {}, time_system_, *api_, false, absl::nullopt,
Server::FieldValidationConfig(), 1, std::chrono::seconds(1), Server::DrainStrategy::Gradual,
nullptr, false, false, baseProxyConfig(version_, true, port_));
test_server_->waitUntilListenersReady();
ENVOY_LOG_MISC(debug, "Http proxy is now running");
return;
Expand All @@ -194,9 +198,9 @@ void TestServer::start(TestServerType type, int port) {
registerMobileProtoDescriptors();
#endif
test_server_ = IntegrationTestServer::create(
"", Network::Address::IpVersion::v4, nullptr, nullptr, {}, time_system_, *api_, false,
absl::nullopt, Server::FieldValidationConfig(), 1, std::chrono::seconds(1),
Server::DrainStrategy::Gradual, nullptr, false, false, baseProxyConfig(false, port_));
"", version_, nullptr, nullptr, {}, time_system_, *api_, false, absl::nullopt,
Server::FieldValidationConfig(), 1, std::chrono::seconds(1), Server::DrainStrategy::Gradual,
nullptr, false, false, baseProxyConfig(version_, false, port_));
test_server_->waitUntilListenersReady();
ENVOY_LOG_MISC(debug, "Https proxy is now running");
return;
Expand Down Expand Up @@ -246,8 +250,12 @@ std::string TestServer::getAddress() const {
}

std::string TestServer::getIpAddress() const {
ASSERT(upstream_);
return upstream_->localAddress()->ip()->addressAsString();
if (upstream_) {
return upstream_->localAddress()->ip()->addressAsString();
}
// Return the proxy server IP address.
ASSERT(test_server_);
return version_ == Network::Address::IpVersion::v4 ? "127.0.0.1" : "::1";
}

int TestServer::getPort() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ private Type() {}
/** The instance of {@link HttpProxyTestServer}. */
public static class HttpProxyTestServer {
private final long handle; // Used by the native code.
private final String ipAddress;
private final int port;

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

/** Returns the server IP address. */
public String getIpAddress() { return ipAddress; }

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

Expand Down
1 change: 1 addition & 0 deletions mobile/test/jni/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ cc_library(
}),
deps = [
"//library/jni:jni_helper_lib",
"//library/jni:jni_utility_lib",
"//test/common/integration:test_server_lib",
"@envoy//test/test_common:test_version_linkstamp",
"@envoy_build_config//:extension_registry",
Expand Down
10 changes: 6 additions & 4 deletions mobile/test/jni/jni_http_proxy_test_server_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

// NOLINT(namespace-envoy)

Expand All @@ -13,7 +14,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* /* reserved */) {
"io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory$HttpProxyTestServer",
/* methods= */
{
{"<init>", "(JI)V"},
{"<init>", "(JLjava/lang/String;I)V"},
},
/* static_methods= */ {}, /* fields= */
{
Expand All @@ -34,12 +35,13 @@ Java_io_envoyproxy_envoymobile_engine_testing_HttpProxyTestServerFactory_start(J

jclass java_http_proxy_server_factory_class = jni_helper.findClassFromCache(
"io/envoyproxy/envoymobile/engine/testing/HttpProxyTestServerFactory$HttpProxyTestServer");
auto java_init_method_id =
jni_helper.getMethodIdFromCache(java_http_proxy_server_factory_class, "<init>", "(JI)V");
auto java_init_method_id = jni_helper.getMethodIdFromCache(java_http_proxy_server_factory_class,
"<init>", "(JLjava/lang/String;I)V");
auto ip_address = Envoy::JNI::cppStringToJavaString(jni_helper, test_server->getIpAddress());
int port = test_server->getPort();
return jni_helper
.newObject(java_http_proxy_server_factory_class, java_init_method_id,
reinterpret_cast<jlong>(test_server), static_cast<jint>(port))
reinterpret_cast<jlong>(test_server), ip_address.get(), static_cast<jint>(port))
.release();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -54,7 +53,6 @@ class ProxyInfoIntentPerformHTTPSRequestUsingAsyncProxyTest {
httpProxyTestServer.shutdown()
}

@Ignore("https://github.com/envoyproxy/envoy/issues/33014")
@Test
fun `performs an HTTPs request through a proxy using async DNS resolution`() {
val context = ApplicationProvider.getApplicationContext<Context>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -54,17 +53,17 @@ class ProxyInfoIntentPerformHTTPSRequestUsingProxyTest {
httpProxyTestServer.shutdown()
}

@Ignore("https://github.com/envoyproxy/envoy/issues/33014")
@Test
fun `performs an HTTPs request through a proxy`() {
println("IP ADDRESS: ${httpProxyTestServer.ipAddress}")
val context = ApplicationProvider.getApplicationContext<Context>()
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.bindProcessToNetwork(connectivityManager.activeNetwork)
Shadows.shadowOf(connectivityManager)
.setProxyForNetwork(
connectivityManager.activeNetwork,
ProxyInfo.buildDirectProxy("127.0.0.1", httpProxyTestServer.port)
ProxyInfo.buildDirectProxy(httpProxyTestServer.ipAddress, httpProxyTestServer.port)
)

val onEngineRunningLatch = CountDownLatch(1)
Expand Down

0 comments on commit 8f5eca5

Please sign in to comment.