Skip to content

Commit

Permalink
Add ByteBuffersTest
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 6, 2024
1 parent f88907e commit 3c880ce
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions mobile/library/java/io/envoyproxy/envoymobile/engine/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android_library(
java_library(
name = "envoy_base_engine_lib",
srcs = [
"ByteBuffers.java",
"EnvoyConfiguration.java",
"EnvoyEngine.java",
"EnvoyEngineImpl.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.envoyproxy.envoymobile.engine;

import java.nio.ByteBuffer;

public class ByteBuffers {
/**
* Copies the specified `ByteBuffer` into a new `ByteBuffer`. The `ByteBuffer` created will
* be backed by `byte[]`.
*/
public static ByteBuffer copy(ByteBuffer byteBuffer) {
byte[] bytes = new byte[byteBuffer.capacity()];
byteBuffer.get(bytes);
return ByteBuffer.wrap(bytes);
}

private ByteBuffers() {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.envoyproxy.envoymobile.engine;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;

import io.envoyproxy.envoymobile.engine.types.EnvoyHTTPCallbacks;
Expand Down Expand Up @@ -76,9 +75,7 @@ public Object onResponseTrailers(long trailerCount, long[] streamIntel) {
public Object onResponseData(ByteBuffer data, boolean endStream, long[] streamIntel) {
// Create a copy of the `data` because the `data` uses direct `ByteBuffer` and the `data` will
// be destroyed after calling this callback.
byte[] bytes = new byte[data.capacity()];
data.get(bytes);
ByteBuffer copiedData = ByteBuffer.wrap(bytes);
ByteBuffer copiedData = ByteBuffers.copy(data);
callbacks.getExecutor().execute(new Runnable() {
public void run() {
callbacks.onData(copiedData, endStream, new EnvoyStreamIntelImpl(streamIntel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public Object onRequestHeaders(long headerCount, boolean endStream, long[] strea
* @return Object[], pair of HTTP filter status and optional modified data.
*/
public Object onRequestData(ByteBuffer data, boolean endStream, long[] streamIntel) {
// Create a copy of the `data` because the `data` uses direct `ByteBuffer` and the `data` will
// be destroyed after calling this callback.
ByteBuffer copiedData = ByteBuffers.copy(data);
return toJniFilterDataStatus(
filter.onRequestData(data, endStream, new EnvoyStreamIntelImpl(streamIntel)));
filter.onRequestData(copiedData, endStream, new EnvoyStreamIntelImpl(streamIntel)));
}

/**
Expand Down Expand Up @@ -108,8 +111,11 @@ public Object onResponseHeaders(long headerCount, boolean endStream, long[] stre
* @return Object[], pair of HTTP filter status and optional modified data.
*/
public Object onResponseData(ByteBuffer data, boolean endStream, long[] streamIntel) {
// Create a copy of the `data` because the `data` uses direct `ByteBuffer` and the `data` will
// be destroyed after calling this callback.
ByteBuffer copiedData = ByteBuffers.copy(data);
return toJniFilterDataStatus(
filter.onResponseData(data, endStream, new EnvoyStreamIntelImpl(streamIntel)));
filter.onResponseData(copiedData, endStream, new EnvoyStreamIntelImpl(streamIntel)));
}

/**
Expand Down Expand Up @@ -138,6 +144,9 @@ public Object onResponseTrailers(long trailerCount, long[] streamIntel) {
*/
public Object onResumeRequest(long headerCount, ByteBuffer data, long trailerCount,
boolean endStream, long[] streamIntel) {
// Create a copy of the `data` because the `data` uses direct `ByteBuffer` and the `data` will
// be destroyed after calling this callback.
ByteBuffer copiedData = ByteBuffers.copy(data);
// Headers are optional in this call, and a negative length indicates omission.
Map<String, List<String>> headers = null;
if (headerCount >= 0) {
Expand All @@ -150,7 +159,7 @@ public Object onResumeRequest(long headerCount, ByteBuffer data, long trailerCou
assert trailerUtility.validateCount(trailerCount);
trailers = trailerUtility.retrieveHeaders();
}
return toJniFilterResumeStatus(filter.onResumeRequest(headers, data, trailers, endStream,
return toJniFilterResumeStatus(filter.onResumeRequest(headers, copiedData, trailers, endStream,
new EnvoyStreamIntelImpl(streamIntel)));
}

Expand All @@ -166,6 +175,9 @@ public Object onResumeRequest(long headerCount, ByteBuffer data, long trailerCou
*/
public Object onResumeResponse(long headerCount, ByteBuffer data, long trailerCount,
boolean endStream, long[] streamIntel) {
// Create a copy of the `data` because the `data` uses direct `ByteBuffer` and the `data` will
// be destroyed after calling this callback.
ByteBuffer copiedData = ByteBuffers.copy(data);
// Headers are optional in this call, and a negative length indicates omission.
Map<String, List<String>> headers = null;
if (headerCount >= 0) {
Expand All @@ -178,7 +190,7 @@ public Object onResumeResponse(long headerCount, ByteBuffer data, long trailerCo
assert trailerUtility.validateCount(trailerCount);
trailers = trailerUtility.retrieveHeaders();
}
return toJniFilterResumeStatus(filter.onResumeResponse(headers, data, trailers, endStream,
return toJniFilterResumeStatus(filter.onResumeResponse(headers, copiedData, trailers, endStream,
new EnvoyStreamIntelImpl(streamIntel)));
}

Expand Down
20 changes: 20 additions & 0 deletions mobile/test/java/io/envoyproxy/envoymobile/engine/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,23 @@ envoy_mobile_android_test(
"//test/kotlin/io/envoyproxy/envoymobile/mocks:mocks_lib",
],
)

envoy_mobile_android_test(
name = "byte_buffers_test",
srcs = [
"ByteBuffersTest.java",
],
associates = ["//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib"],
native_deps = [
"//test/jni:libenvoy_jni_with_test_extensions.so",
] + select({
"@platforms//os:macos": [
"//test/jni:libenvoy_jni_with_test_extensions_jnilib",
],
"//conditions:default": [],
}),
native_lib_name = "envoy_jni_with_test_extensions",
deps = [
"//library/java/io/envoyproxy/envoymobile/engine:envoy_base_engine_lib",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.envoyproxy.envoymobile.engine;

import static com.google.common.truth.Truth.assertThat;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.nio.ByteBuffer;

@RunWith(AndroidJUnit4.class)
public class ByteBuffersTest {
@Test
public void testCopy() {
ByteBuffer source = ByteBuffer.allocateDirect(3);
source.put((byte)1);
source.put((byte)2);
source.put((byte)3);
source.flip();

ByteBuffer dest = ByteBuffers.copy(source);
source.flip();
assertThat(dest).isEqualTo(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.envoyproxy.envoymobile.utilities;

import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ByteBuffersTest {}

0 comments on commit 3c880ce

Please sign in to comment.