Merge "Use int array for included and excluded uids"

This commit is contained in:
Sooraj Sasindran
2022-03-16 17:56:08 +00:00
committed by Gerrit Code Review
7 changed files with 160 additions and 79 deletions

View File

@@ -172,8 +172,8 @@ package android.net {
public final class ProfileNetworkPreference implements android.os.Parcelable { public final class ProfileNetworkPreference implements android.os.Parcelable {
method public int describeContents(); method public int describeContents();
method @NonNull public java.util.List<java.lang.Integer> getExcludedUids(); method @NonNull public int[] getExcludedUids();
method @NonNull public java.util.List<java.lang.Integer> getIncludedUids(); method @NonNull public int[] getIncludedUids();
method public int getPreference(); method public int getPreference();
method public int getPreferenceEnterpriseId(); method public int getPreferenceEnterpriseId();
method public void writeToParcel(@NonNull android.os.Parcel, int); method public void writeToParcel(@NonNull android.os.Parcel, int);
@@ -183,8 +183,8 @@ package android.net {
public static final class ProfileNetworkPreference.Builder { public static final class ProfileNetworkPreference.Builder {
ctor public ProfileNetworkPreference.Builder(); ctor public ProfileNetworkPreference.Builder();
method @NonNull public android.net.ProfileNetworkPreference build(); method @NonNull public android.net.ProfileNetworkPreference build();
method @NonNull public android.net.ProfileNetworkPreference.Builder setExcludedUids(@Nullable java.util.List<java.lang.Integer>); method @NonNull public android.net.ProfileNetworkPreference.Builder setExcludedUids(@NonNull int[]);
method @NonNull public android.net.ProfileNetworkPreference.Builder setIncludedUids(@Nullable java.util.List<java.lang.Integer>); method @NonNull public android.net.ProfileNetworkPreference.Builder setIncludedUids(@NonNull int[]);
method @NonNull public android.net.ProfileNetworkPreference.Builder setPreference(int); method @NonNull public android.net.ProfileNetworkPreference.Builder setPreference(int);
method @NonNull public android.net.ProfileNetworkPreference.Builder setPreferenceEnterpriseId(int); method @NonNull public android.net.ProfileNetworkPreference.Builder setPreferenceEnterpriseId(int);
} }

View File

@@ -147,27 +147,32 @@ public final class NetworkCapabilities implements Parcelable {
private String mRequestorPackageName; private String mRequestorPackageName;
/** /**
* Enterprise capability identifier 1. * Enterprise capability identifier 1. It will be used to uniquely identify specific
* enterprise network.
*/ */
public static final int NET_ENTERPRISE_ID_1 = 1; public static final int NET_ENTERPRISE_ID_1 = 1;
/** /**
* Enterprise capability identifier 2. * Enterprise capability identifier 2. It will be used to uniquely identify specific
* enterprise network.
*/ */
public static final int NET_ENTERPRISE_ID_2 = 2; public static final int NET_ENTERPRISE_ID_2 = 2;
/** /**
* Enterprise capability identifier 3. * Enterprise capability identifier 3. It will be used to uniquely identify specific
* enterprise network.
*/ */
public static final int NET_ENTERPRISE_ID_3 = 3; public static final int NET_ENTERPRISE_ID_3 = 3;
/** /**
* Enterprise capability identifier 4. * Enterprise capability identifier 4. It will be used to uniquely identify specific
* enterprise network.
*/ */
public static final int NET_ENTERPRISE_ID_4 = 4; public static final int NET_ENTERPRISE_ID_4 = 4;
/** /**
* Enterprise capability identifier 5. * Enterprise capability identifier 5. It will be used to uniquely identify specific
* enterprise network.
*/ */
public static final int NET_ENTERPRISE_ID_5 = 5; public static final int NET_ENTERPRISE_ID_5 = 5;

View File

@@ -22,14 +22,12 @@ import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5; import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.net.ConnectivityManager.ProfileNetworkPreferencePolicy; import android.net.ConnectivityManager.ProfileNetworkPreferencePolicy;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -41,31 +39,31 @@ import java.util.Objects;
public final class ProfileNetworkPreference implements Parcelable { public final class ProfileNetworkPreference implements Parcelable {
private final @ProfileNetworkPreferencePolicy int mPreference; private final @ProfileNetworkPreferencePolicy int mPreference;
private final @NetworkCapabilities.EnterpriseId int mPreferenceEnterpriseId; private final @NetworkCapabilities.EnterpriseId int mPreferenceEnterpriseId;
private final List<Integer> mIncludedUids; private int[] mIncludedUids = new int[0];
private final List<Integer> mExcludedUids; private int[] mExcludedUids = new int[0];
private ProfileNetworkPreference(int preference, List<Integer> includedUids, private ProfileNetworkPreference(int preference, int[] includedUids,
List<Integer> excludedUids, int[] excludedUids,
@NetworkCapabilities.EnterpriseId int preferenceEnterpriseId) { @NetworkCapabilities.EnterpriseId int preferenceEnterpriseId) {
mPreference = preference; mPreference = preference;
mPreferenceEnterpriseId = preferenceEnterpriseId; mPreferenceEnterpriseId = preferenceEnterpriseId;
if (includedUids != null) { if (includedUids != null) {
mIncludedUids = new ArrayList<>(includedUids); mIncludedUids = includedUids.clone();
} else { } else {
mIncludedUids = new ArrayList<>(); mIncludedUids = new int[0];
} }
if (excludedUids != null) { if (excludedUids != null) {
mExcludedUids = new ArrayList<>(excludedUids); mExcludedUids = excludedUids.clone();
} else { } else {
mExcludedUids = new ArrayList<>(); mExcludedUids = new int[0];
} }
} }
private ProfileNetworkPreference(Parcel in) { private ProfileNetworkPreference(Parcel in) {
mPreference = in.readInt(); mPreference = in.readInt();
mIncludedUids = in.readArrayList(Integer.class.getClassLoader()); in.readIntArray(mIncludedUids);
mExcludedUids = in.readArrayList(Integer.class.getClassLoader()); in.readIntArray(mExcludedUids);
mPreferenceEnterpriseId = in.readInt(); mPreferenceEnterpriseId = in.readInt();
} }
@@ -74,31 +72,31 @@ public final class ProfileNetworkPreference implements Parcelable {
} }
/** /**
* Get the list of UIDs subject to this preference. * Get the array of UIDs subject to this preference.
* *
* Included UIDs and Excluded UIDs can't both be non-empty. * Included UIDs and Excluded UIDs can't both be non-empty.
* if both are empty, it means this request applies to all uids in the user profile. * if both are empty, it means this request applies to all uids in the user profile.
* if included is not empty, then only included UIDs are applied. * if included is not empty, then only included UIDs are applied.
* if excluded is not empty, then it is all uids in the user profile except these UIDs. * if excluded is not empty, then it is all uids in the user profile except these UIDs.
* @return List of uids included for the profile preference. * @return Array of uids included for the profile preference.
* {@see #getExcludedUids()} * {@see #getExcludedUids()}
*/ */
public @NonNull List<Integer> getIncludedUids() { public @NonNull int[] getIncludedUids() {
return new ArrayList<>(mIncludedUids); return mIncludedUids.clone();
} }
/** /**
* Get the list of UIDS excluded from this preference. * Get the array of UIDS excluded from this preference.
* *
* <ul>Included UIDs and Excluded UIDs can't both be non-empty.</ul> * <ul>Included UIDs and Excluded UIDs can't both be non-empty.</ul>
* <ul>If both are empty, it means this request applies to all uids in the user profile.</ul> * <ul>If both are empty, it means this request applies to all uids in the user profile.</ul>
* <ul>If included is not empty, then only included UIDs are applied.</ul> * <ul>If included is not empty, then only included UIDs are applied.</ul>
* <ul>If excluded is not empty, then it is all uids in the user profile except these UIDs.</ul> * <ul>If excluded is not empty, then it is all uids in the user profile except these UIDs.</ul>
* @return List of uids not included for the profile preference. * @return Array of uids not included for the profile preference.
* {@see #getIncludedUids()} * {@see #getIncludedUids()}
*/ */
public @NonNull List<Integer> getExcludedUids() { public @NonNull int[] getExcludedUids() {
return new ArrayList<>(mExcludedUids); return mExcludedUids.clone();
} }
/** /**
@@ -134,8 +132,8 @@ public final class ProfileNetworkPreference implements Parcelable {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
final ProfileNetworkPreference that = (ProfileNetworkPreference) o; final ProfileNetworkPreference that = (ProfileNetworkPreference) o;
return mPreference == that.mPreference return mPreference == that.mPreference
&& (Objects.equals(mIncludedUids, that.mIncludedUids)) && (Arrays.equals(mIncludedUids, that.mIncludedUids))
&& (Objects.equals(mExcludedUids, that.mExcludedUids)) && (Arrays.equals(mExcludedUids, that.mExcludedUids))
&& mPreferenceEnterpriseId == that.mPreferenceEnterpriseId; && mPreferenceEnterpriseId == that.mPreferenceEnterpriseId;
} }
@@ -143,8 +141,8 @@ public final class ProfileNetworkPreference implements Parcelable {
public int hashCode() { public int hashCode() {
return mPreference return mPreference
+ mPreferenceEnterpriseId * 2 + mPreferenceEnterpriseId * 2
+ (Objects.hashCode(mIncludedUids) * 11) + (Arrays.hashCode(mIncludedUids) * 11)
+ (Objects.hashCode(mExcludedUids) * 13); + (Arrays.hashCode(mExcludedUids) * 13);
} }
/** /**
@@ -154,8 +152,8 @@ public final class ProfileNetworkPreference implements Parcelable {
public static final class Builder { public static final class Builder {
private @ProfileNetworkPreferencePolicy int mPreference = private @ProfileNetworkPreferencePolicy int mPreference =
PROFILE_NETWORK_PREFERENCE_DEFAULT; PROFILE_NETWORK_PREFERENCE_DEFAULT;
private @NonNull List<Integer> mIncludedUids = new ArrayList<>(); private int[] mIncludedUids = new int[0];
private @NonNull List<Integer> mExcludedUids = new ArrayList<>(); private int[] mExcludedUids = new int[0];
private int mPreferenceEnterpriseId; private int mPreferenceEnterpriseId;
/** /**
@@ -177,44 +175,38 @@ public final class ProfileNetworkPreference implements Parcelable {
} }
/** /**
* This is a list of uids for which profile perefence is set. * This is a array of uids for which profile perefence is set.
* Null would mean that this preference applies to all uids in the profile. * Empty would mean that this preference applies to all uids in the profile.
* {@see #setExcludedUids(List<Integer>)} * {@see #setExcludedUids(int[])}
* Included UIDs and Excluded UIDs can't both be non-empty. * Included UIDs and Excluded UIDs can't both be non-empty.
* if both are empty, it means this request applies to all uids in the user profile. * if both are empty, it means this request applies to all uids in the user profile.
* if included is not empty, then only included UIDs are applied. * if included is not empty, then only included UIDs are applied.
* if excluded is not empty, then it is all uids in the user profile except these UIDs. * if excluded is not empty, then it is all uids in the user profile except these UIDs.
* @param uids list of uids that are included * @param uids Array of uids that are included
* @return The builder to facilitate chaining. * @return The builder to facilitate chaining.
*/ */
@NonNull @NonNull
public Builder setIncludedUids(@Nullable List<Integer> uids) { public Builder setIncludedUids(@NonNull int[] uids) {
if (uids != null) { Objects.requireNonNull(uids);
mIncludedUids = new ArrayList<Integer>(uids); mIncludedUids = uids.clone();
} else {
mIncludedUids = new ArrayList<Integer>();
}
return this; return this;
} }
/** /**
* This is a list of uids that are excluded for the profile perefence. * This is a array of uids that are excluded for the profile perefence.
* {@see #setIncludedUids(List<Integer>)} * {@see #setIncludedUids(int[])}
* Included UIDs and Excluded UIDs can't both be non-empty. * Included UIDs and Excluded UIDs can't both be non-empty.
* if both are empty, it means this request applies to all uids in the user profile. * if both are empty, it means this request applies to all uids in the user profile.
* if included is not empty, then only included UIDs are applied. * if included is not empty, then only included UIDs are applied.
* if excluded is not empty, then it is all uids in the user profile except these UIDs. * if excluded is not empty, then it is all uids in the user profile except these UIDs.
* @param uids list of uids that are not included * @param uids Array of uids that are not included
* @return The builder to facilitate chaining. * @return The builder to facilitate chaining.
*/ */
@NonNull @NonNull
public Builder setExcludedUids(@Nullable List<Integer> uids) { public Builder setExcludedUids(@NonNull int[] uids) {
if (uids != null) { Objects.requireNonNull(uids);
mExcludedUids = new ArrayList<Integer>(uids); mExcludedUids = uids.clone();
} else {
mExcludedUids = new ArrayList<Integer>();
}
return this; return this;
} }
@@ -241,7 +233,7 @@ public final class ProfileNetworkPreference implements Parcelable {
*/ */
@NonNull @NonNull
public ProfileNetworkPreference build() { public ProfileNetworkPreference build() {
if (mIncludedUids.size() > 0 && mExcludedUids.size() > 0) { if (mIncludedUids.length > 0 && mExcludedUids.length > 0) {
throw new IllegalArgumentException("Both includedUids and excludedUids " throw new IllegalArgumentException("Both includedUids and excludedUids "
+ "cannot be nonempty"); + "cannot be nonempty");
} }
@@ -280,8 +272,8 @@ public final class ProfileNetworkPreference implements Parcelable {
@Override @Override
public void writeToParcel(@NonNull android.os.Parcel dest, int flags) { public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
dest.writeInt(mPreference); dest.writeInt(mPreference);
dest.writeList(mIncludedUids); dest.writeIntArray(mIncludedUids);
dest.writeList(mExcludedUids); dest.writeIntArray(mExcludedUids);
dest.writeInt(mPreferenceEnterpriseId); dest.writeInt(mPreferenceEnterpriseId);
} }

View File

@@ -10625,15 +10625,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
@NonNull final UserHandle profile, @NonNull final UserHandle profile,
@NonNull final ProfileNetworkPreference profileNetworkPreference) { @NonNull final ProfileNetworkPreference profileNetworkPreference) {
final UidRange profileUids = UidRange.createForUser(profile); final UidRange profileUids = UidRange.createForUser(profile);
Set<UidRange> uidRangeSet = UidRangeUtils.convertListToUidRange( Set<UidRange> uidRangeSet = UidRangeUtils.convertArrayToUidRange(
profileNetworkPreference.getIncludedUids()); profileNetworkPreference.getIncludedUids());
if (uidRangeSet.size() > 0) { if (uidRangeSet.size() > 0) {
if (!UidRangeUtils.isRangeSetInUidRange(profileUids, uidRangeSet)) { if (!UidRangeUtils.isRangeSetInUidRange(profileUids, uidRangeSet)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Allow uid range is outside the uid range of profile."); "Allow uid range is outside the uid range of profile.");
} }
} else { } else {
ArraySet<UidRange> disallowUidRangeSet = UidRangeUtils.convertListToUidRange( ArraySet<UidRange> disallowUidRangeSet = UidRangeUtils.convertArrayToUidRange(
profileNetworkPreference.getExcludedUids()); profileNetworkPreference.getExcludedUids());
if (disallowUidRangeSet.size() > 0) { if (disallowUidRangeSet.size() > 0) {
if (!UidRangeUtils.isRangeSetInUidRange(profileUids, disallowUidRangeSet)) { if (!UidRangeUtils.isRangeSetInUidRange(profileUids, disallowUidRangeSet)) {

View File

@@ -21,6 +21,7 @@ import android.net.UidRange;
import android.util.ArraySet; import android.util.ArraySet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@@ -125,7 +126,7 @@ public final class UidRangeUtils {
} }
/** /**
* Convert a list of uid to set of UidRanges. * Convert a list of uids to set of UidRanges.
* @param uids list of uids * @param uids list of uids
* @return set of UidRanges * @return set of UidRanges
* @hide * @hide
@@ -153,4 +154,34 @@ public final class UidRangeUtils {
uidRangeSet.add(new UidRange(start, stop)); uidRangeSet.add(new UidRange(start, stop));
return uidRangeSet; return uidRangeSet;
} }
/**
* Convert an array of uids to set of UidRanges.
* @param uids array of uids
* @return set of UidRanges
* @hide
*/
public static ArraySet<UidRange> convertArrayToUidRange(@NonNull int[] uids) {
Objects.requireNonNull(uids);
final ArraySet<UidRange> uidRangeSet = new ArraySet<UidRange>();
if (uids.length == 0) {
return uidRangeSet;
}
int[] uidsNew = uids.clone();
Arrays.sort(uidsNew);
int start = uidsNew[0];
int stop = start;
for (int i : uidsNew) {
if (i <= stop + 1) {
stop = i;
} else {
uidRangeSet.add(new UidRange(start, stop));
start = i;
stop = i;
}
}
uidRangeSet.add(new UidRange(start, stop));
return uidRangeSet;
}
} }

View File

@@ -13870,12 +13870,13 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference profileNetworkPreference) { ProfileNetworkPreference profileNetworkPreference) {
final Set<UidRange> uidRangeSet; final Set<UidRange> uidRangeSet;
UidRange range = UidRange.createForUser(handle); UidRange range = UidRange.createForUser(handle);
if (profileNetworkPreference.getIncludedUids().size() != 0) { if (profileNetworkPreference.getIncludedUids().length != 0) {
uidRangeSet = UidRangeUtils.convertListToUidRange( uidRangeSet = UidRangeUtils.convertArrayToUidRange(
profileNetworkPreference.getIncludedUids()); profileNetworkPreference.getIncludedUids());
} else if (profileNetworkPreference.getExcludedUids().size() != 0) {
} else if (profileNetworkPreference.getExcludedUids().length != 0) {
uidRangeSet = UidRangeUtils.removeRangeSetFromUidRange( uidRangeSet = UidRangeUtils.removeRangeSetFromUidRange(
range, UidRangeUtils.convertListToUidRange( range, UidRangeUtils.convertArrayToUidRange(
profileNetworkPreference.getExcludedUids())); profileNetworkPreference.getExcludedUids()));
} else { } else {
uidRangeSet = new ArraySet<>(); uidRangeSet = new ArraySet<>();
@@ -14247,7 +14248,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID)});
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), false, testHandle, profileNetworkPreferenceBuilder.build(), false, testHandle,
@@ -14266,7 +14267,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), false, profileNetworkPreferenceBuilder.build(), false,
@@ -14285,7 +14286,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), false, profileNetworkPreferenceBuilder.build(), false,
@@ -14305,7 +14306,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(0) - 1)); new int[]{testHandle.getUid(0) - 1});
final TestOnCompleteListener listener = new TestOnCompleteListener(); final TestOnCompleteListener listener = new TestOnCompleteListener();
Assert.assertThrows(IllegalArgumentException.class, () -> mCm.setProfileNetworkPreferences( Assert.assertThrows(IllegalArgumentException.class, () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build()), testHandle, List.of(profileNetworkPreferenceBuilder.build()),
@@ -14313,7 +14314,7 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(0) - 1)); new int[]{testHandle.getUid(0) - 1});
Assert.assertThrows(IllegalArgumentException.class, Assert.assertThrows(IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreferences( () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build()), testHandle, List.of(profileNetworkPreferenceBuilder.build()),
@@ -14322,9 +14323,9 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(0) - 1)); new int[]{testHandle.getUid(0) - 1});
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
Assert.assertThrows(IllegalArgumentException.class, Assert.assertThrows(IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreferences( () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build()), testHandle, List.of(profileNetworkPreferenceBuilder.build()),
@@ -14335,9 +14336,9 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder2.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); profileNetworkPreferenceBuilder2.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder2.setIncludedUids( profileNetworkPreferenceBuilder2.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
Assert.assertThrows(IllegalArgumentException.class, Assert.assertThrows(IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreferences( () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build(), testHandle, List.of(profileNetworkPreferenceBuilder.build(),
@@ -14346,9 +14347,9 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder2.setExcludedUids( profileNetworkPreferenceBuilder2.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
Assert.assertThrows(IllegalArgumentException.class, Assert.assertThrows(IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreferences( () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build(), testHandle, List.of(profileNetworkPreferenceBuilder.build(),
@@ -14358,9 +14359,9 @@ public class ConnectivityServiceTest {
profileNetworkPreferenceBuilder2.setPreference( profileNetworkPreferenceBuilder2.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder2.setExcludedUids( profileNetworkPreferenceBuilder2.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)});
Assert.assertThrows(IllegalArgumentException.class, Assert.assertThrows(IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreferences( () -> mCm.setProfileNetworkPreferences(
testHandle, List.of(profileNetworkPreferenceBuilder.build(), testHandle, List.of(profileNetworkPreferenceBuilder.build(),

View File

@@ -351,4 +351,55 @@ public class UidRangeUtilsTest {
expected.add(uids6); expected.add(uids6);
assertEquals(expected, UidRangeUtils.convertListToUidRange(input)); assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
} }
@Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
public void testConvertArrayToUidRange() {
final UidRange uids1_1 = new UidRange(1, 1);
final UidRange uids1_2 = new UidRange(1, 2);
final UidRange uids100_100 = new UidRange(100, 100);
final UidRange uids10_10 = new UidRange(10, 10);
final UidRange uids10_14 = new UidRange(10, 14);
final UidRange uids20_24 = new UidRange(20, 24);
final Set<UidRange> expected = new ArraySet<>();
int[] input = new int[0];
assertThrows(NullPointerException.class, () -> UidRangeUtils.convertArrayToUidRange(null));
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[] {1};
expected.add(uids1_1);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[]{1, 2};
expected.clear();
expected.add(uids1_2);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[]{1, 100};
expected.clear();
expected.add(uids1_1);
expected.add(uids100_100);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[]{100, 1};
expected.clear();
expected.add(uids1_1);
expected.add(uids100_100);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[]{100, 1, 2, 1, 10};
expected.clear();
expected.add(uids1_2);
expected.add(uids10_10);
expected.add(uids100_100);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
input = new int[]{10, 11, 12, 13, 14, 20, 21, 22, 23, 24};
expected.clear();
expected.add(uids10_14);
expected.add(uids20_24);
assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
}
} }