diff options
| author | 2016-04-19 19:25:40 +0000 | |
|---|---|---|
| committer | 2016-04-19 19:25:41 +0000 | |
| commit | 79c4dca2a8be141d0b2b2ca71eb2acd7e1cb3f30 (patch) | |
| tree | 13580f9b92ccf512b963c69b86360c4d818c6be8 | |
| parent | c6184685d8f9b08abf73f83778ca129bb5c9c51d (diff) | |
| parent | cfe2669028074a204fc55afebbae630423a806f2 (diff) | |
Merge "Unit test for UidRange.java and UidRange.cpp" into nyc-dev
5 files changed, 251 insertions, 8 deletions
diff --git a/services/tests/servicestests/Android.mk b/services/tests/servicestests/Android.mk index 7ffdb35bc277..59c6970d1feb 100644 --- a/services/tests/servicestests/Android.mk +++ b/services/tests/servicestests/Android.mk @@ -28,10 +28,22 @@ LOCAL_PACKAGE_NAME := FrameworksServicesTests LOCAL_CERTIFICATE := platform -LOCAL_JNI_SHARED_LIBRARIES := \ - libapfjni \ +# These are not normally accessible from apps so they must be explicitly included. +LOCAL_JNI_SHARED_LIBRARIES := libservicestestsjni \ + libbacktrace \ + libbase \ + libbinder \ libc++ \ - libnativehelper + libcutils \ + liblog \ + liblzma \ + libnativehelper \ + libnetdaidl \ + libui \ + libunwind \ + libutils + +LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk include $(BUILD_PACKAGE) @@ -45,22 +57,24 @@ include $(CLEAR_VARS) LOCAL_MODULE_TAGS := tests -LOCAL_CFLAGS := -Wall -Werror +LOCAL_CFLAGS := -Wall -Wextra -Werror LOCAL_C_INCLUDES := \ libpcap \ hardware/google/apf -LOCAL_SRC_FILES := apf_jni.cpp +LOCAL_SRC_FILES := $(call all-cpp-files-under) LOCAL_SHARED_LIBRARIES := \ + libbinder \ + libcutils \ libnativehelper \ - liblog + libnetdaidl LOCAL_STATIC_LIBRARIES := \ libpcap \ libapf -LOCAL_MODULE := libapfjni +LOCAL_MODULE := libservicestestsjni include $(BUILD_SHARED_LIBRARY) diff --git a/services/tests/servicestests/jni/UidRangeTest.cpp b/services/tests/servicestests/jni/UidRangeTest.cpp new file mode 100644 index 000000000000..7941731a07f3 --- /dev/null +++ b/services/tests/servicestests/jni/UidRangeTest.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 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 "UidRangeTest.h" + +using android::net::UidRange; + +extern "C" +JNIEXPORT jbyteArray Java_android_net_UidRangeTest_readAndWriteNative(JNIEnv* env, jclass, + jbyteArray inParcel) { + const UidRange range = unmarshall(env, inParcel); + return marshall(env, range); +} + +extern "C" +JNIEXPORT jint Java_android_net_UidRangeTest_getStart(JNIEnv* env, jclass, jbyteArray inParcel) { + const UidRange range = unmarshall(env, inParcel); + return range.getStart(); +} + +extern "C" +JNIEXPORT jint Java_android_net_UidRangeTest_getStop(JNIEnv* env, jclass, jbyteArray inParcel) { + const UidRange range = unmarshall(env, inParcel); + return range.getStop(); +} + + +/** + * Reads exactly one UidRange from 'parcelData' assuming that it is a Parcel. Any bytes afterward + * are ignored. + */ +UidRange 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); + + UidRange range; + range.readFromParcel(&p); + return range; +} + +/** + * Creates a Java byte[] array and writes the contents of 'range' to it as a Parcel containing + * exactly one object. + * + * Every UidRange maps to a unique parcel object, so both 'marshall(e, unmarshall(e, x))' and + * 'unmarshall(e, marshall(e, x))' should be fixed points. + */ +jbyteArray marshall(JNIEnv* env, const UidRange& range) { + android::Parcel p; + range.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; +} diff --git a/services/tests/servicestests/jni/UidRangeTest.h b/services/tests/servicestests/jni/UidRangeTest.h new file mode 100644 index 000000000000..b7e745378800 --- /dev/null +++ b/services/tests/servicestests/jni/UidRangeTest.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2016 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. + */ + +#ifndef _ANDROID_NET_UIDRANGETEST_H_ +#define _ANDROID_NET_UIDRANGETEST_H_ + +#include <jni.h> + +#include "android/net/UidRange.h" + +android::net::UidRange unmarshall(JNIEnv* env, jbyteArray parcelData); + +jbyteArray marshall(JNIEnv* env, const android::net::UidRange& range); + +extern "C" +JNIEXPORT jbyteArray Java_android_net_UidRangeTest_readAndWriteNative(JNIEnv* env, jclass, + jbyteArray inParcel); + +extern "C" +JNIEXPORT jint Java_android_net_UidRangeTest_getStart(JNIEnv* env, jclass, jbyteArray inParcel); + +extern "C" +JNIEXPORT jint Java_android_net_UidRangeTest_getStop(JNIEnv* env, jclass, jbyteArray inParcel); + +#endif // _ANDROID_NET_UIDRANGETEST_H_ diff --git a/services/tests/servicestests/src/android/net/UidRangeTest.java b/services/tests/servicestests/src/android/net/UidRangeTest.java new file mode 100644 index 000000000000..221fe0f99f0b --- /dev/null +++ b/services/tests/servicestests/src/android/net/UidRangeTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2016 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. + */ + +package android.net; + +import android.os.Parcel; +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +import static org.junit.Assert.assertArrayEquals; + +public class UidRangeTest extends TestCase { + + static { + System.loadLibrary("servicestestsjni"); + } + + private static native byte[] readAndWriteNative(byte[] inParcel); + private static native int getStart(byte[] inParcel); + private static native int getStop(byte[] inParcel); + + @SmallTest + public void testNativeParcelUnparcel() { + UidRange original = new UidRange(1234, Integer.MAX_VALUE); + + byte[] inParcel = marshall(original); + byte[] outParcel = readAndWriteNative(inParcel); + UidRange roundTrip = unmarshall(outParcel); + + assertEquals(original, roundTrip); + assertArrayEquals(inParcel, outParcel); + } + + @SmallTest + public void testIndividualNativeFields() { + UidRange original = new UidRange(0x11115678, 0x22224321); + byte[] originalBytes = marshall(original); + + assertEquals(original.start, getStart(originalBytes)); + assertEquals(original.stop, getStop(originalBytes)); + } + + @SmallTest + public void testSingleItemUidRangeAllowed() { + new UidRange(123, 123); + new UidRange(0, 0); + new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE); + } + + @SmallTest + public void testNegativeUidsDisallowed() { + try { + new UidRange(-2, 100); + fail("Exception not thrown for negative start UID"); + } catch (IllegalArgumentException expected) { + } + + try { + new UidRange(-200, -100); + fail("Exception not thrown for negative stop UID"); + } catch (IllegalArgumentException expected) { + } + } + + @SmallTest + public void testStopLessThanStartDisallowed() { + final int x = 4195000; + try { + new UidRange(x, x - 1); + fail("Exception not thrown for negative-length UID range"); + } catch (IllegalArgumentException expected) { + } + } + + /** + * Write a {@link UidRange} into an empty parcel and return the underlying data. + * + * @see unmarshall(byte[]) + */ + private static byte[] marshall(UidRange range) { + Parcel p = Parcel.obtain(); + range.writeToParcel(p, /* flags */ 0); + p.setDataPosition(0); + return p.marshall(); + } + + /** + * Read raw bytes into a parcel, and read a {@link UidRange} back out of them. + * + * @see marshall(UidRange) + */ + private static UidRange unmarshall(byte[] data) { + Parcel p = Parcel.obtain(); + p.unmarshall(data, 0, data.length); + p.setDataPosition(0); + return UidRange.CREATOR.createFromParcel(p); + } +} diff --git a/services/tests/servicestests/src/com/android/server/ApfTest.java b/services/tests/servicestests/src/com/android/server/ApfTest.java index 640a6c927ed3..9ba27cbb6980 100644 --- a/services/tests/servicestests/src/com/android/server/ApfTest.java +++ b/services/tests/servicestests/src/com/android/server/ApfTest.java @@ -43,7 +43,7 @@ public class ApfTest extends AndroidTestCase { public void setUp() throws Exception { super.setUp(); // Load up native shared library containing APF interpreter exposed via JNI. - System.loadLibrary("apfjni"); + System.loadLibrary("servicestestsjni"); } // Expected return codes from APF interpreter. |