diff options
author | 2018-10-28 18:06:15 -0700 | |
---|---|---|
committer | 2018-10-28 18:06:15 -0700 | |
commit | 8c12dcd11a18081b365d81267c3d29f9848222f9 (patch) | |
tree | 5b1257b014bf42c809cf66ce739eb357abe2d0ee | |
parent | ddc28adf0c6921a4a4dca910cd6f01eb6f2fdab3 (diff) | |
parent | 68f4bd92bc241b45cce40d2578be02f59483567d (diff) |
Merge "Refactor UidRange by using stable aidl structure" am: d1d3b374b7 am: 53c8b8a939
am: 68f4bd92bc
Change-Id: I82e06fe93a3ed88368b649ef8d3283322191ffe7
-rw-r--r-- | core/java/android/net/UidRange.aidl | 24 | ||||
-rw-r--r-- | core/java/android/net/UidRange.java | 29 | ||||
-rw-r--r-- | services/core/java/com/android/server/NetworkManagementService.java | 2 | ||||
-rw-r--r-- | tests/net/java/android/net/UidRangeTest.java | 59 | ||||
-rw-r--r-- | tests/net/jni/UidRangeTest.cpp | 79 | ||||
-rw-r--r-- | tests/net/jni/UidRangeTest.h | 38 |
6 files changed, 39 insertions, 192 deletions
diff --git a/core/java/android/net/UidRange.aidl b/core/java/android/net/UidRange.aidl new file mode 100644 index 000000000000..f70fc8e2fefd --- /dev/null +++ b/core/java/android/net/UidRange.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2018 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; + +/** + * An inclusive range of UIDs. + * + * {@hide} + */ +parcelable UidRange;
\ No newline at end of file diff --git a/core/java/android/net/UidRange.java b/core/java/android/net/UidRange.java index 3164929943c4..793c82dc68e1 100644 --- a/core/java/android/net/UidRange.java +++ b/core/java/android/net/UidRange.java @@ -19,17 +19,14 @@ package android.net; import static android.os.UserHandle.PER_USER_RANGE; import android.os.Parcel; -import android.os.Parcelable; /** * An inclusive range of UIDs. * * @hide */ -public final class UidRange implements Parcelable { - public final int start; - public final int stop; - +public final class UidRange extends UidRangeParcel { + private UidRange() {} public UidRange(int startUid, int stopUid) { if (startUid < 0) throw new IllegalArgumentException("Invalid start UID."); if (stopUid < 0) throw new IllegalArgumentException("Invalid stop UID."); @@ -89,26 +86,18 @@ public final class UidRange implements Parcelable { return start + "-" + stop; } - // implement the Parcelable interface - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(start); - dest.writeInt(stop); - } + /** + * DO NOT override "writeToParcel" and "readFromParcel" in this class. + * The parceling code is autogenerated by the superclass. + */ public static final Creator<UidRange> CREATOR = new Creator<UidRange>() { @Override public UidRange createFromParcel(Parcel in) { - int start = in.readInt(); - int stop = in.readInt(); - - return new UidRange(start, stop); + UidRange obj = new UidRange(); + obj.readFromParcel(in); + return obj; } @Override public UidRange[] newArray(int size) { diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java index 9c56ccf2a5be..865a651fb722 100644 --- a/services/core/java/com/android/server/NetworkManagementService.java +++ b/services/core/java/com/android/server/NetworkManagementService.java @@ -69,6 +69,7 @@ import android.net.NetworkStats; import android.net.NetworkUtils; import android.net.RouteInfo; import android.net.UidRange; +import android.net.UidRangeParcel; import android.net.util.NetdService; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.KeyMgmt; @@ -1743,7 +1744,6 @@ public class NetworkManagementService extends INetworkManagementService.Stub public void setAllowOnlyVpnForUids(boolean add, UidRange[] uidRanges) throws ServiceSpecificException { mContext.enforceCallingOrSelfPermission(NETWORK_STACK, TAG); - try { mNetdService.networkRejectNonSecureVpn(add, uidRanges); } catch (ServiceSpecificException e) { diff --git a/tests/net/java/android/net/UidRangeTest.java b/tests/net/java/android/net/UidRangeTest.java index 1d1013e79219..860d73293c2b 100644 --- a/tests/net/java/android/net/UidRangeTest.java +++ b/tests/net/java/android/net/UidRangeTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import android.os.Parcel; import android.support.test.runner.AndroidJUnit4; import android.support.test.filters.SmallTest; @@ -31,34 +30,10 @@ import org.junit.Test; @SmallTest public class UidRangeTest { - static { - System.loadLibrary("frameworksnettestsjni"); - } - - private static native byte[] readAndWriteNative(byte[] inParcel); - private static native int getStart(byte[] inParcel); - private static native int getStop(byte[] inParcel); - - @Test - 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); - } - - @Test - public void testIndividualNativeFields() { - UidRange original = new UidRange(0x11115678, 0x22224321); - byte[] originalBytes = marshall(original); - - assertEquals(original.start, getStart(originalBytes)); - assertEquals(original.stop, getStop(originalBytes)); - } + /* + * UidRange is no longer passed to netd. UID ranges between the framework and netd are passed as + * UidRangeParcel objects. + */ @Test public void testSingleItemUidRangeAllowed() { @@ -91,28 +66,4 @@ public class UidRangeTest { } 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); - } -} +}
\ No newline at end of file diff --git a/tests/net/jni/UidRangeTest.cpp b/tests/net/jni/UidRangeTest.cpp deleted file mode 100644 index 7941731a07f3..000000000000 --- a/tests/net/jni/UidRangeTest.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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/tests/net/jni/UidRangeTest.h b/tests/net/jni/UidRangeTest.h deleted file mode 100644 index b7e745378800..000000000000 --- a/tests/net/jni/UidRangeTest.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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_ |