Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cfe26690 authored by Robin Lee's avatar Robin Lee
Browse files

Unit test for UidRange.java and UidRange.cpp

Verifies that:
 - UidRange only accepts non-negative UIDs
 - UidRange only accepts start UID <= stop UID
 - Native implementation is in sync with java implementation (via JNI)

Change-Id: I8b412781efe9cfda6d5e666e37fe0d8f5e2b6ecc
parent 10606941
Loading
Loading
Loading
Loading
+21 −7
Original line number Diff line number Diff line
@@ -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)
+79 −0
Original line number Diff line number Diff line
/*
 * 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;
}
+38 −0
Original line number Diff line number Diff line
/*
 * 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_
+112 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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.