Merge "Refactor UidRange by using stable aidl structure"
This commit is contained in:
24
core/java/android/net/UidRange.aidl
Normal file
24
core/java/android/net/UidRange.aidl
Normal file
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -1738,7 +1739,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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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_
|
||||
Reference in New Issue
Block a user