diff --git a/mobile/library/common/jni/jni_utility.cc b/mobile/library/common/jni/jni_utility.cc index 0373cfa555bc..3105aa0d5be2 100644 --- a/mobile/library/common/jni/jni_utility.cc +++ b/mobile/library/common/jni/jni_utility.cc @@ -1,7 +1,7 @@ #include "library/common/jni/jni_utility.h" -#include -#include +#include +#include #include "source/common/common/assert.h" @@ -355,6 +355,15 @@ void javaByteArrayToProto(JniHelper& jni_helper, jbyteArray source, RELEASE_ASSERT(success, "Failed to parse protobuf message."); } +LocalRefUniquePtr protoToJavaByteArray(JniHelper& jni_helper, + const Envoy::Protobuf::MessageLite& source) { + size_t size = source.ByteSizeLong(); + LocalRefUniquePtr byte_array = jni_helper.newByteArray(size); + auto bytes = jni_helper.getByteArrayElements(byte_array.get(), nullptr); + source.SerializeToArray(bytes.get(), size); + return byte_array; +} + std::string javaStringToString(JniHelper& jni_helper, jstring java_string) { if (!java_string) { return ""; diff --git a/mobile/library/common/jni/jni_utility.h b/mobile/library/common/jni/jni_utility.h index 2939d1f91a1f..e7db6f4289a1 100644 --- a/mobile/library/common/jni/jni_utility.h +++ b/mobile/library/common/jni/jni_utility.h @@ -124,6 +124,10 @@ void javaByteArrayToString(JniHelper& jni_helper, jbyteArray jbytes, std::string void javaByteArrayToProto(JniHelper& jni_helper, jbyteArray source, Envoy::Protobuf::MessageLite* dest); +/** Converts from Proto to Java byte array. */ +LocalRefUniquePtr protoToJavaByteArray(JniHelper& jni_helper, + const Envoy::Protobuf::MessageLite& source); + /** Converts from Java `String` to C++ string. */ std::string javaStringToString(JniHelper& jni_helper, jstring java_string); diff --git a/mobile/test/common/jni/BUILD b/mobile/test/common/jni/BUILD index 52ccb1e2a29c..d2148673c33d 100644 --- a/mobile/test/common/jni/BUILD +++ b/mobile/test/common/jni/BUILD @@ -103,3 +103,23 @@ cc_binary( ":jni_helper_test_lib", ], ) + +cc_library( + name = "jni_utility_test_lib", + srcs = [ + "jni_utility_test.cc", + ], + deps = [ + "//library/common/jni:jni_utility_lib", + ], + alwayslink = True, +) + +cc_binary( + name = "libenvoy_jni_utility_test.so", + testonly = True, + linkshared = True, + deps = [ + ":jni_utility_test_lib", + ], +) diff --git a/mobile/test/common/jni/jni_utility_test.cc b/mobile/test/common/jni/jni_utility_test.cc new file mode 100644 index 000000000000..e81dc108727b --- /dev/null +++ b/mobile/test/common/jni/jni_utility_test.cc @@ -0,0 +1,17 @@ +#include + +#include "library/common/jni/jni_utility.h" + +// NOLINT(namespace-envoy) + +// This file contains JNI implementation used by +// `test/java/io/envoyproxy/envoymobile/jni/JniUtilityTest.java` unit tests. + +extern "C" JNIEXPORT jbyteArray JNICALL +Java_io_envoyproxy_envoymobile_jni_JniUtilityTest_protoJavaByteArrayConversion(JNIEnv* env, jclass, + jbyteArray source) { + Envoy::JNI::JniHelper jni_helper(env); + Envoy::ProtobufWkt::Struct s; + Envoy::JNI::javaByteArrayToProto(jni_helper, source, &s); + return Envoy::JNI::protoToJavaByteArray(jni_helper, s).release(); +} diff --git a/mobile/test/java/io/envoyproxy/envoymobile/jni/BUILD b/mobile/test/java/io/envoyproxy/envoymobile/jni/BUILD index b82998b551f8..b6044841af2a 100644 --- a/mobile/test/java/io/envoyproxy/envoymobile/jni/BUILD +++ b/mobile/test/java/io/envoyproxy/envoymobile/jni/BUILD @@ -10,3 +10,17 @@ envoy_mobile_android_test( ], native_lib_name = "envoy_jni_helper_test", ) + +envoy_mobile_android_test( + name = "jni_utility_test", + srcs = [ + "JniUtilityTest.java", + ], + native_deps = [ + "//test/common/jni:libenvoy_jni_utility_test.so", + ], + native_lib_name = "envoy_jni_utility_test", + deps = [ + "@maven//:com_google_protobuf_protobuf_javalite", + ], +) diff --git a/mobile/test/java/io/envoyproxy/envoymobile/jni/JniUtilityTest.java b/mobile/test/java/io/envoyproxy/envoymobile/jni/JniUtilityTest.java new file mode 100644 index 000000000000..9deb8164422b --- /dev/null +++ b/mobile/test/java/io/envoyproxy/envoymobile/jni/JniUtilityTest.java @@ -0,0 +1,31 @@ +package io.envoyproxy.envoymobile.jni; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class JniUtilityTest { + public JniUtilityTest() { System.loadLibrary("envoy_jni_utility_test"); } + + //================================================================================ + // Native methods for testing. + //================================================================================ + public static native byte[] protoJavaByteArrayConversion(byte[] source); + + @Test + public void testProtoJavaByteArrayConversion() throws Exception { + Struct source = + Struct.newBuilder() + .putFields("string_key", Value.newBuilder().setStringValue("string_value").build()) + .putFields("num_key", Value.newBuilder().setNumberValue(123).build()) + .putFields("bool_key", Value.newBuilder().setBoolValue(true).build()) + .build(); + Struct dest = Struct.parseFrom(protoJavaByteArrayConversion(source.toByteArray())); + assertThat(source).isEqualTo(dest); + } +} diff --git a/tools/base/requirements.in b/tools/base/requirements.in index c13d3a06eee0..9c14febc444a 100644 --- a/tools/base/requirements.in +++ b/tools/base/requirements.in @@ -4,7 +4,7 @@ aio.api.github>=0.2.5 aiohttp>=3.8.1 aioquic>=0.9.21 cffi>=1.15.0 -clang-format==14.0.6 +clang-format==17.0.4 clang-tidy==14.0.6 colorama coloredlogs diff --git a/tools/base/requirements.txt b/tools/base/requirements.txt index 6ae32732d784..40ee311a3f75 100644 --- a/tools/base/requirements.txt +++ b/tools/base/requirements.txt @@ -394,16 +394,22 @@ charset-normalizer==3.3.0 \ # via # aiohttp # requests -clang-format==14.0.6 \ - --hash=sha256:13f2d6d4a2af004a783c65f0921afa8f0384bffcdaf500b6c2cb542edeb0b4a5 \ - --hash=sha256:810c649ab97d208cd418c897d50ab6e958eb8d96854527edd80d0dd21a75e914 \ - --hash=sha256:aaf4edecc46a24f0b572b82cf5827e292ad1c137903427627c4d5f671668cc2b \ - --hash=sha256:bd400c47665dd19afc03f98e747f78ed828abab99c6a1b07e137b35c1cd3cc26 \ - --hash=sha256:c93580945f75de7e01996f1fb3cf67e4dc424f1c864e237c85614fb99a48c7a4 \ - --hash=sha256:d5c96b500d7f8b5d2db5b75ac035be387512850ad589cdc3019666b861382136 \ - --hash=sha256:d780c04334bca80f2b60d25bf53c37bd0618520ee295a7888a11f25bde114ac4 \ - --hash=sha256:d7c1c5e404c58e55f0170f01b3c5611dce6c119e62b5d1020347e0ad97d5a047 \ - --hash=sha256:dbfd60528eb3bb7d7cfe8576faa70845fbf93601f815ef75163d36606e87f388 +clang-format==17.0.4 \ + --hash=sha256:0dff69572d0c5af484430e8825f2c9f753f55f2b39ffa1376e71502b1ac7c405 \ + --hash=sha256:309173b909135c849ce17057a3b30e2c6be004bfcdeafe9d9628efd2f43e12cb \ + --hash=sha256:3d2b3468a048bcd31514b72aba61997a77a73bd0272f1cb80f9baee585114aa3 \ + --hash=sha256:53784f6503ea58ae63e449a0eae83073ddbfb929adbe24567c405d3678797479 \ + --hash=sha256:545c599913e00ed4c06677c123658992131bb7cf6cc5e1630a8e2618988811ca \ + --hash=sha256:557fb96bbafd53f053ff36f87c88193762cdb0f1d85465e7ebb3752014120f01 \ + --hash=sha256:65f79fafcd27281a5c4e1d83c648202f5e7ec4a0b080697906204c9b896c930c \ + --hash=sha256:67e670505da1b0cb5a454ff84ad1fc030552e457177516426f669fa46301893f \ + --hash=sha256:839446174c207535eee34a3d9f2aa94455547926a4ebc94d59abd84f8ecee229 \ + --hash=sha256:9037927dbe50588c6056c333898c42c0ab9ed390a3b28565692fd587d5885da3 \ + --hash=sha256:9e2a8436c3afab54170ad1d22561cc075034bc46a510ba528b488b459b367df0 \ + --hash=sha256:ad293c300bb2613212f41b0667cbcc4b118adb6f7f0d11617937d76fd2350226 \ + --hash=sha256:b820e6eae19a215302db3dfb847c2838851a2c9a52952504c6c5ee1e1de2bdfc \ + --hash=sha256:b8513e927c8e0caaba83be8d065f8100ad94415cd1cb2945127debe7bb368de0 \ + --hash=sha256:c6e12e95b27dff188ee50e83e7fb565eecabded57d256ca51c53fd082d2363c3 # via -r requirements.in clang-tidy==14.0.6 \ --hash=sha256:02bce40a56cc344e20d2f63bef6b85acf9837954559e0091804d6e748dfc0359 \ @@ -685,7 +691,9 @@ google-apitools==0.5.32 \ google-auth[aiohttp]==2.23.3 \ --hash=sha256:6864247895eea5d13b9c57c9e03abb49cb94ce2dc7c58e91cba3248c7477c9e3 \ --hash=sha256:a8f4608e65c244ead9e0538f181a96c6e11199ec114d41f1d7b1bffa96937bda - # via gsutil + # via + # google-auth + # gsutil google-reauth==0.1.1 \ --hash=sha256:cb39074488d74c8853074dde47368bbf8f739d4a4338b89aab696c895b6d8368 \ --hash=sha256:f9f6852a55c2c5453d581cd01f3d1278e86147c03d008409800390a834235892 @@ -1027,6 +1035,7 @@ pyjwt[crypto]==2.8.0 \ # via # gidgethub # pygithub + # pyjwt pylsqpack==0.3.17 \ --hash=sha256:016cfc9dc2dea9c0b06b7aba64da89315b4c96c309c69f5ea548f4e994429781 \ --hash=sha256:0b1ed55872f8f984f655a4c853ad749b834a1fcd4c7e5a962c6f17d4d27687a2 \