summaryrefslogtreecommitdiff
path: root/libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp
diff options
context:
space:
mode:
authorRobert Quattlebaum <rquattle@google.com>2017-07-18 12:18:58 -0700
committerRobert Quattlebaum <rquattle@google.com>2017-07-18 12:25:52 -0700
commite6a1be5f36c91c12ba89bca99233bdb9d7b448e0 (patch)
tree6aa83f202600cf75d6a27bca41ca63393bca6432 /libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp
parentd38415634a6e0d2175e888618a6ebc52dfcb861e (diff)
downloadlowpan-e6a1be5f36c91c12ba89bca99233bdb9d7b448e0.tar.gz
libandroid_net_lowpan: Reintroduce new unit tests for data classesoreo-mr1-dev
This change introduces new unit tests which confirm that various data classes are being property marshalled between their Java and C++ implementations. This was originally added to platform/frameworks/base via change id I41d590b1e77dc41873c4b9e9bf1b7f1bf859f74e, but that change was reverted (via ag/2553741) due the failure of build 4190632 on the branch `git_wear-master`. Moving these specific tests into this repository avoids this failure case. This change is dependent on the following change id: I70fe6be8d71424e11a537daaa69c3f6cfae8e49d Bug: b/63707448 b/63708348 Change-Id: I41d590b1e77dc41873c4b9e9bf1b7f1bf859f74e Test: These tests both compile and pass
Diffstat (limited to 'libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp')
-rw-r--r--libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp b/libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp
new file mode 100644
index 0000000..fc860b2
--- /dev/null
+++ b/libandroid_net_lowpan/tests/jni/LowpanCredentialTest.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <memory>
+
+#include <binder/Parcel.h>
+
+#include "LowpanCredentialTest.h"
+
+using android::net::lowpan::LowpanCredential;
+
+/**
+ * Reads exactly one LowpanCredential from 'parcelData' assuming that it is a Parcel. Any bytes afterward
+ * are ignored.
+ */
+static LowpanCredential unmarshall(JNIEnv* env, jbyteArray parcelData) {
+ const int length = env->GetArrayLength(parcelData);
+
+ std::unique_ptr<uint8_t> bytes(new uint8_t[length]);
+ env->GetByteArrayRegion(parcelData, 0, length, reinterpret_cast<jbyte*>(bytes.get()));
+
+ android::Parcel p;
+ p.setData(bytes.get(), length);
+
+ LowpanCredential value;
+ value.readFromParcel(&p);
+ return value;
+}
+
+/**
+ * Creates a Java byte[] array and writes the contents of 'addr' to it as a Parcel containing
+ * exactly one object.
+ *
+ * Every LowpanCredential maps to a unique parcel object, so both 'marshall(e, unmarshall(e, x))' and
+ * 'unmarshall(e, marshall(e, x))' should be fixed points.
+ */
+static jbyteArray marshall(JNIEnv* env, const LowpanCredential& addr) {
+ android::Parcel p;
+ addr.writeToParcel(&p);
+ const int length = p.dataSize();
+
+ jbyteArray parcelData = env->NewByteArray(length);
+ env->SetByteArrayRegion(parcelData, 0, length, reinterpret_cast<const jbyte*>(p.data()));
+
+ return parcelData;
+}
+
+extern "C"
+JNIEXPORT jbyteArray Java_android_net_lowpan_LowpanCredentialTest_readAndWriteNative(JNIEnv* env, jclass,
+ jbyteArray inParcel) {
+ const LowpanCredential value = unmarshall(env, inParcel);
+ return marshall(env, value);
+}