Add support for Multiple enterprise slice

Bug: 194332512
Test: unit test
CTS-Coverage-Bug: 211133973
Change-Id: Ie8be08a7cfa9155168d1da146d02fd1643248bdc
This commit is contained in:
Sooraj Sasindran
2022-01-21 13:37:08 -08:00
parent 29d5f0adbe
commit f4a58dcd45
9 changed files with 388 additions and 161 deletions

View File

@@ -294,7 +294,7 @@ package android.net {
ctor public NetworkCapabilities(android.net.NetworkCapabilities); ctor public NetworkCapabilities(android.net.NetworkCapabilities);
method public int describeContents(); method public int describeContents();
method @NonNull public int[] getCapabilities(); method @NonNull public int[] getCapabilities();
method @NonNull public int[] getEnterpriseCapabilitySubLevels(); method @NonNull public int[] getEnterpriseIds();
method public int getLinkDownstreamBandwidthKbps(); method public int getLinkDownstreamBandwidthKbps();
method public int getLinkUpstreamBandwidthKbps(); method public int getLinkUpstreamBandwidthKbps();
method @Nullable public android.net.NetworkSpecifier getNetworkSpecifier(); method @Nullable public android.net.NetworkSpecifier getNetworkSpecifier();
@@ -302,6 +302,7 @@ package android.net {
method public int getSignalStrength(); method public int getSignalStrength();
method @Nullable public android.net.TransportInfo getTransportInfo(); method @Nullable public android.net.TransportInfo getTransportInfo();
method public boolean hasCapability(int); method public boolean hasCapability(int);
method public boolean hasEnterpriseId(int);
method public boolean hasTransport(int); method public boolean hasTransport(int);
method public void writeToParcel(android.os.Parcel, int); method public void writeToParcel(android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.net.NetworkCapabilities> CREATOR; field @NonNull public static final android.os.Parcelable.Creator<android.net.NetworkCapabilities> CREATOR;
@@ -332,6 +333,11 @@ package android.net {
field public static final int NET_CAPABILITY_VALIDATED = 16; // 0x10 field public static final int NET_CAPABILITY_VALIDATED = 16; // 0x10
field public static final int NET_CAPABILITY_WIFI_P2P = 6; // 0x6 field public static final int NET_CAPABILITY_WIFI_P2P = 6; // 0x6
field public static final int NET_CAPABILITY_XCAP = 9; // 0x9 field public static final int NET_CAPABILITY_XCAP = 9; // 0x9
field public static final int NET_ENTERPRISE_ID_1 = 1; // 0x1
field public static final int NET_ENTERPRISE_ID_2 = 2; // 0x2
field public static final int NET_ENTERPRISE_ID_3 = 3; // 0x3
field public static final int NET_ENTERPRISE_ID_4 = 4; // 0x4
field public static final int NET_ENTERPRISE_ID_5 = 5; // 0x5
field public static final int SIGNAL_STRENGTH_UNSPECIFIED = -2147483648; // 0x80000000 field public static final int SIGNAL_STRENGTH_UNSPECIFIED = -2147483648; // 0x80000000
field public static final int TRANSPORT_BLUETOOTH = 2; // 0x2 field public static final int TRANSPORT_BLUETOOTH = 2; // 0x2
field public static final int TRANSPORT_CELLULAR = 0; // 0x0 field public static final int TRANSPORT_CELLULAR = 0; // 0x0

View File

@@ -133,11 +133,6 @@ package android.net {
public final class NetworkCapabilities implements android.os.Parcelable { public final class NetworkCapabilities implements android.os.Parcelable {
method @Nullable public java.util.Set<android.util.Range<java.lang.Integer>> getUids(); method @Nullable public java.util.Set<android.util.Range<java.lang.Integer>> getUids();
method public boolean hasForbiddenCapability(int); method public boolean hasForbiddenCapability(int);
field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 = 1; // 0x1
field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2 = 2; // 0x2
field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3 = 3; // 0x3
field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4 = 4; // 0x4
field public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5 = 5; // 0x5
field public static final long REDACT_ALL = -1L; // 0xffffffffffffffffL field public static final long REDACT_ALL = -1L; // 0xffffffffffffffffL
field public static final long REDACT_FOR_ACCESS_FINE_LOCATION = 1L; // 0x1L field public static final long REDACT_FOR_ACCESS_FINE_LOCATION = 1L; // 0x1L
field public static final long REDACT_FOR_LOCAL_MAC_ADDRESS = 2L; // 0x2L field public static final long REDACT_FOR_LOCAL_MAC_ADDRESS = 2L; // 0x2L
@@ -166,6 +161,7 @@ package android.net {
method @NonNull public java.util.List<java.lang.Integer> getExcludedUids(); method @NonNull public java.util.List<java.lang.Integer> getExcludedUids();
method @NonNull public java.util.List<java.lang.Integer> getIncludedUids(); method @NonNull public java.util.List<java.lang.Integer> getIncludedUids();
method public int getPreference(); method public int getPreference();
method public int getPreferenceEnterpriseId();
method public void writeToParcel(@NonNull android.os.Parcel, int); method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.net.ProfileNetworkPreference> CREATOR; field @NonNull public static final android.os.Parcelable.Creator<android.net.ProfileNetworkPreference> CREATOR;
} }
@@ -176,6 +172,7 @@ package android.net {
method @NonNull public android.net.ProfileNetworkPreference.Builder setExcludedUids(@Nullable java.util.List<java.lang.Integer>); method @NonNull public android.net.ProfileNetworkPreference.Builder setExcludedUids(@Nullable java.util.List<java.lang.Integer>);
method @NonNull public android.net.ProfileNetworkPreference.Builder setIncludedUids(@Nullable java.util.List<java.lang.Integer>); method @NonNull public android.net.ProfileNetworkPreference.Builder setIncludedUids(@Nullable java.util.List<java.lang.Integer>);
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);
} }
public final class TestNetworkInterface implements android.os.Parcelable { public final class TestNetworkInterface implements android.os.Parcelable {

View File

@@ -294,11 +294,11 @@ package android.net {
ctor public NetworkCapabilities.Builder(); ctor public NetworkCapabilities.Builder();
ctor public NetworkCapabilities.Builder(@NonNull android.net.NetworkCapabilities); ctor public NetworkCapabilities.Builder(@NonNull android.net.NetworkCapabilities);
method @NonNull public android.net.NetworkCapabilities.Builder addCapability(int); method @NonNull public android.net.NetworkCapabilities.Builder addCapability(int);
method @NonNull public android.net.NetworkCapabilities.Builder addEnterpriseCapabilitySubLevel(int); method @NonNull public android.net.NetworkCapabilities.Builder addEnterpriseId(int);
method @NonNull public android.net.NetworkCapabilities.Builder addTransportType(int); method @NonNull public android.net.NetworkCapabilities.Builder addTransportType(int);
method @NonNull public android.net.NetworkCapabilities build(); method @NonNull public android.net.NetworkCapabilities build();
method @NonNull public android.net.NetworkCapabilities.Builder removeCapability(int); method @NonNull public android.net.NetworkCapabilities.Builder removeCapability(int);
method @NonNull public android.net.NetworkCapabilities.Builder removeEnterpriseCapabilitySubLevel(int); method @NonNull public android.net.NetworkCapabilities.Builder removeEnterpriseId(int);
method @NonNull public android.net.NetworkCapabilities.Builder removeTransportType(int); method @NonNull public android.net.NetworkCapabilities.Builder removeTransportType(int);
method @NonNull @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) public android.net.NetworkCapabilities.Builder setAdministratorUids(@NonNull int[]); method @NonNull @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) public android.net.NetworkCapabilities.Builder setAdministratorUids(@NonNull int[]);
method @NonNull public android.net.NetworkCapabilities.Builder setLinkDownstreamBandwidthKbps(int); method @NonNull public android.net.NetworkCapabilities.Builder setLinkDownstreamBandwidthKbps(int);

View File

@@ -16,6 +16,7 @@
package android.net; package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST;
import static android.net.NetworkRequest.Type.LISTEN; import static android.net.NetworkRequest.Type.LISTEN;
import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST; import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST;
@@ -5537,6 +5538,9 @@ public class ConnectivityManager {
ProfileNetworkPreference.Builder preferenceBuilder = ProfileNetworkPreference.Builder preferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
preferenceBuilder.setPreference(preference); preferenceBuilder.setPreference(preference);
if (preference != PROFILE_NETWORK_PREFERENCE_DEFAULT) {
preferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
}
setProfileNetworkPreferences(profile, setProfileNetworkPreferences(profile,
List.of(preferenceBuilder.build()), executor, listener); List.of(preferenceBuilder.build()), executor, listener);
} }

View File

@@ -16,8 +16,6 @@
package android.net; package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
import android.annotation.IntDef; import android.annotation.IntDef;
@@ -149,67 +147,83 @@ public final class NetworkCapabilities implements Parcelable {
private String mRequestorPackageName; private String mRequestorPackageName;
/** /**
* enterprise capability sub level 1 * Enterprise capability identifier 1.
* @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) public static final int NET_ENTERPRISE_ID_1 = 1;
public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 = 1;
/** /**
* enterprise capability sub level 2 * Enterprise capability identifier 2.
* @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) public static final int NET_ENTERPRISE_ID_2 = 2;
public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2 = 2;
/** /**
* enterprise capability sub level 3 * Enterprise capability identifier 3.
* @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) public static final int NET_ENTERPRISE_ID_3 = 3;
public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3 = 3;
/** /**
* enterprise capability sub level 4 * Enterprise capability identifier 4.
* @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) public static final int NET_ENTERPRISE_ID_4 = 4;
public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4 = 4;
/** /**
* enterprise capability sub level 5 * Enterprise capability identifier 5.
* @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) public static final int NET_ENTERPRISE_ID_5 = 5;
public static final int NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5 = 5;
/** @hide */ /** @hide */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = { "NET_CAPABILITY_ENTERPRISE_SUB_LEVEL" }, value = { @IntDef(prefix = { "NET_CAPABILITY_ENTERPRISE_SUB_LEVEL" }, value = {
NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, NET_ENTERPRISE_ID_1,
NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, NET_ENTERPRISE_ID_2,
NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3, NET_ENTERPRISE_ID_3,
NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4, NET_ENTERPRISE_ID_4,
NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5, NET_ENTERPRISE_ID_5,
}) })
public @interface EnterpriseCapabilitySubLevel { public @interface EnterpriseId {
} }
/** /**
* Bitfield representing the network's enterprise capability sublevel. If any are specified * Bitfield representing the network's enterprise capability identifier. If any are specified
* they will be satisfied by any Network that matches all of them. * they will be satisfied by any Network that matches all of them.
* {@see addEnterpriseCapabilitySubLevel} for details on how masks are added * {@see addEnterpriseId} for details on how masks are added
*/ */
private int mEnterpriseCapabilitySubLevel; private int mEnterpriseId;
/** /**
* @return all the enteprise capabilities sub level set on this {@code NetworkCapability} * Get enteprise identifiers set.
* instance. *
* Get all the enterprise capabilities identifier set on this {@code NetworkCapability}
* If NET_CAPABILITY_ENTERPRISE is set and no enterprise ID is set, it is
* considered to have NET_CAPABILITY_ENTERPRISE by default.
* @return all the enterprise capabilities identifier set.
* *
*/ */
public @NonNull @EnterpriseCapabilitySubLevel int[] getEnterpriseCapabilitySubLevels() { public @NonNull @EnterpriseId int[] getEnterpriseIds() {
return NetworkCapabilitiesUtils.unpackBits(mEnterpriseCapabilitySubLevel); if (hasCapability(NET_CAPABILITY_ENTERPRISE) && mEnterpriseId == 0) {
return new int[]{NET_ENTERPRISE_ID_1};
}
return NetworkCapabilitiesUtils.unpackBits(mEnterpriseId);
}
/**
* Tests for the presence of an enterprise capability identifier on this instance.
*
* If NET_CAPABILITY_ENTERPRISE is set and no enterprise ID is set, it is
* considered to have NET_CAPABILITY_ENTERPRISE by default.
* @param enterpriseId the enterprise capability identifier to be tested for.
* @return {@code true} if set on this instance.
*/
public boolean hasEnterpriseId(
@EnterpriseId int enterpriseId) {
if (enterpriseId == NET_ENTERPRISE_ID_1) {
if (hasCapability(NET_CAPABILITY_ENTERPRISE) && mEnterpriseId == 0) {
return true;
}
}
return isValidEnterpriseId(enterpriseId)
&& ((mEnterpriseId & (1L << enterpriseId)) != 0);
} }
public NetworkCapabilities() { public NetworkCapabilities() {
@@ -258,7 +272,7 @@ public final class NetworkCapabilities implements Parcelable {
mRequestorPackageName = null; mRequestorPackageName = null;
mSubIds = new ArraySet<>(); mSubIds = new ArraySet<>();
mUnderlyingNetworks = null; mUnderlyingNetworks = null;
mEnterpriseCapabilitySubLevel = 0; mEnterpriseId = 0;
} }
/** /**
@@ -291,7 +305,7 @@ public final class NetworkCapabilities implements Parcelable {
// mUnderlyingNetworks is an unmodifiable list if non-null, so a defensive copy is not // mUnderlyingNetworks is an unmodifiable list if non-null, so a defensive copy is not
// necessary. // necessary.
mUnderlyingNetworks = nc.mUnderlyingNetworks; mUnderlyingNetworks = nc.mUnderlyingNetworks;
mEnterpriseCapabilitySubLevel = nc.mEnterpriseCapabilitySubLevel; mEnterpriseId = nc.mEnterpriseId;
} }
/** /**
@@ -784,34 +798,34 @@ public final class NetworkCapabilities implements Parcelable {
} }
/** /**
* Adds the given enterprise capability sub level to this {@code NetworkCapability} instance. * Adds the given enterprise capability identifier to this {@code NetworkCapability} instance.
* Note that when searching for a network to satisfy a request, all capabilities sub level * Note that when searching for a network to satisfy a request, all capabilities identifier
* requested must be satisfied. * requested must be satisfied.
* *
* @param enterpriseCapabilitySubLevel the enterprise capability sub level to be added. * @param enterpriseId the enterprise capability identifier to be added.
* @return This NetworkCapabilities instance, to facilitate chaining. * @return This NetworkCapabilities instance, to facilitate chaining.
* @hide * @hide
*/ */
private @NonNull NetworkCapabilities addEnterpriseCapabilitySubLevel( public @NonNull NetworkCapabilities addEnterpriseId(
@EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @EnterpriseId int enterpriseId) {
checkValidEnterpriseCapabilitySublevel(enterpriseCapabilitySubLevel); checkValidEnterpriseId(enterpriseId);
mEnterpriseCapabilitySubLevel |= 1 << enterpriseCapabilitySubLevel; mEnterpriseId |= 1 << enterpriseId;
return this; return this;
} }
/** /**
* Removes (if found) the given enterprise capability sublevel from this * Removes (if found) the given enterprise capability identifier from this
* {@code NetworkCapability} instance that were added via addEnterpriseCapabilitySubLevel(int) * {@code NetworkCapability} instance that were added via addEnterpriseId(int)
* *
* @param enterpriseCapabilitySubLevel the enterprise capability sublevel to be removed. * @param enterpriseId the enterprise capability identifier to be removed.
* @return This NetworkCapabilities instance, to facilitate chaining. * @return This NetworkCapabilities instance, to facilitate chaining.
* @hide * @hide
*/ */
private @NonNull NetworkCapabilities removeEnterpriseCapabilitySubLevel( private @NonNull NetworkCapabilities removeEnterpriseId(
@EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @EnterpriseId int enterpriseId) {
checkValidEnterpriseCapabilitySublevel(enterpriseCapabilitySubLevel); checkValidEnterpriseId(enterpriseId);
final int mask = ~(1 << enterpriseCapabilitySubLevel); final int mask = ~(1 << enterpriseId);
mEnterpriseCapabilitySubLevel &= mask; mEnterpriseId &= mask;
return this; return this;
} }
@@ -915,16 +929,19 @@ public final class NetworkCapabilities implements Parcelable {
return null; return null;
} }
private boolean equalsEnterpriseCapabilitiesSubLevel(@NonNull NetworkCapabilities nc) { private boolean equalsEnterpriseCapabilitiesId(@NonNull NetworkCapabilities nc) {
return nc.mEnterpriseCapabilitySubLevel == this.mEnterpriseCapabilitySubLevel; return nc.mEnterpriseId == this.mEnterpriseId;
} }
private boolean satisfiedByEnterpriseCapabilitiesSubLevel(@NonNull NetworkCapabilities nc) { private boolean satisfiedByEnterpriseCapabilitiesId(@NonNull NetworkCapabilities nc) {
final int requestedEnterpriseCapabilitiesSubLevel = mEnterpriseCapabilitySubLevel; final int requestedEnterpriseCapabilitiesId = mEnterpriseId;
final int providedEnterpriseCapabailitiesSubLevel = nc.mEnterpriseCapabilitySubLevel; final int providedEnterpriseCapabailitiesId = nc.mEnterpriseId;
if ((providedEnterpriseCapabailitiesSubLevel & requestedEnterpriseCapabilitiesSubLevel) if ((providedEnterpriseCapabailitiesId & requestedEnterpriseCapabilitiesId)
== requestedEnterpriseCapabilitiesSubLevel) { == requestedEnterpriseCapabilitiesId) {
return true;
} else if (providedEnterpriseCapabailitiesId == 0
&& (requestedEnterpriseCapabilitiesId == (1L << NET_ENTERPRISE_ID_1))) {
return true; return true;
} else { } else {
return false; return false;
@@ -1836,7 +1853,7 @@ public final class NetworkCapabilities implements Parcelable {
&& satisfiedByTransportTypes(nc) && satisfiedByTransportTypes(nc)
&& (onlyImmutable || satisfiedByLinkBandwidths(nc)) && (onlyImmutable || satisfiedByLinkBandwidths(nc))
&& satisfiedBySpecifier(nc) && satisfiedBySpecifier(nc)
&& satisfiedByEnterpriseCapabilitiesSubLevel(nc) && satisfiedByEnterpriseCapabilitiesId(nc)
&& (onlyImmutable || satisfiedBySignalStrength(nc)) && (onlyImmutable || satisfiedBySignalStrength(nc))
&& (onlyImmutable || satisfiedByUids(nc)) && (onlyImmutable || satisfiedByUids(nc))
&& (onlyImmutable || satisfiedBySSID(nc)) && (onlyImmutable || satisfiedBySSID(nc))
@@ -1940,7 +1957,7 @@ public final class NetworkCapabilities implements Parcelable {
&& equalsAdministratorUids(that) && equalsAdministratorUids(that)
&& equalsSubscriptionIds(that) && equalsSubscriptionIds(that)
&& equalsUnderlyingNetworks(that) && equalsUnderlyingNetworks(that)
&& equalsEnterpriseCapabilitiesSubLevel(that); && equalsEnterpriseCapabilitiesId(that);
} }
@Override @Override
@@ -1965,7 +1982,7 @@ public final class NetworkCapabilities implements Parcelable {
+ Arrays.hashCode(mAdministratorUids) * 61 + Arrays.hashCode(mAdministratorUids) * 61
+ Objects.hashCode(mSubIds) * 67 + Objects.hashCode(mSubIds) * 67
+ Objects.hashCode(mUnderlyingNetworks) * 71 + Objects.hashCode(mUnderlyingNetworks) * 71
+ mEnterpriseCapabilitySubLevel * 73; + mEnterpriseId * 73;
} }
@Override @Override
@@ -2001,7 +2018,7 @@ public final class NetworkCapabilities implements Parcelable {
dest.writeString(mRequestorPackageName); dest.writeString(mRequestorPackageName);
dest.writeIntArray(CollectionUtils.toIntArray(mSubIds)); dest.writeIntArray(CollectionUtils.toIntArray(mSubIds));
dest.writeTypedList(mUnderlyingNetworks); dest.writeTypedList(mUnderlyingNetworks);
dest.writeInt(mEnterpriseCapabilitySubLevel); dest.writeInt(mEnterpriseId);
} }
public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR = public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
@@ -2031,7 +2048,7 @@ public final class NetworkCapabilities implements Parcelable {
netCap.mSubIds.add(subIdInts[i]); netCap.mSubIds.add(subIdInts[i]);
} }
netCap.setUnderlyingNetworks(in.createTypedArrayList(Network.CREATOR)); netCap.setUnderlyingNetworks(in.createTypedArrayList(Network.CREATOR));
netCap.mEnterpriseCapabilitySubLevel = in.readInt(); netCap.mEnterpriseId = in.readInt();
return netCap; return netCap;
} }
@Override @Override
@@ -2123,10 +2140,10 @@ public final class NetworkCapabilities implements Parcelable {
sb.append(" SubscriptionIds: ").append(mSubIds); sb.append(" SubscriptionIds: ").append(mSubIds);
} }
if (0 != mEnterpriseCapabilitySubLevel) { if (0 != mEnterpriseId) {
sb.append(" EnterpriseCapabilitySublevel: "); sb.append(" EnterpriseId: ");
appendStringRepresentationOfBitMaskToStringBuilder(sb, mEnterpriseCapabilitySubLevel, appendStringRepresentationOfBitMaskToStringBuilder(sb, mEnterpriseId,
NetworkCapabilities::enterpriseCapabilitySublevelNameOf, "&"); NetworkCapabilities::enterpriseIdNameOf, "&");
} }
sb.append(" UnderlyingNetworks: "); sb.append(" UnderlyingNetworks: ");
@@ -2228,7 +2245,7 @@ public final class NetworkCapabilities implements Parcelable {
} }
} }
private static @NonNull String enterpriseCapabilitySublevelNameOf( private static @NonNull String enterpriseIdNameOf(
@NetCapability int capability) { @NetCapability int capability) {
return Integer.toString(capability); return Integer.toString(capability);
} }
@@ -2273,17 +2290,17 @@ public final class NetworkCapabilities implements Parcelable {
} }
} }
private static boolean isValidEnterpriseCapabilitySubLevel( private static boolean isValidEnterpriseId(
@NetworkCapabilities.EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @NetworkCapabilities.EnterpriseId int enterpriseId) {
return enterpriseCapabilitySubLevel >= NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1 return enterpriseId >= NET_ENTERPRISE_ID_1
&& enterpriseCapabilitySubLevel <= NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5; && enterpriseId <= NET_ENTERPRISE_ID_5;
} }
private static void checkValidEnterpriseCapabilitySublevel( private static void checkValidEnterpriseId(
@NetworkCapabilities.EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @NetworkCapabilities.EnterpriseId int enterpriseId) {
if (!isValidEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel)) { if (!isValidEnterpriseId(enterpriseId)) {
throw new IllegalArgumentException("enterprise capability sublevel " throw new IllegalArgumentException("enterprise capability identifier "
+ enterpriseCapabilitySubLevel + " is out of range"); + enterpriseId + " is out of range");
} }
} }
@@ -2613,33 +2630,33 @@ public final class NetworkCapabilities implements Parcelable {
} }
/** /**
* Adds the given enterprise capability sub level. * Adds the given enterprise capability identifier.
* Note that when searching for a network to satisfy a request, all capabilities sub level * Note that when searching for a network to satisfy a request, all capabilities identifier
* requested must be satisfied. Enterprise capability sub-level is applicable only * requested must be satisfied. Enterprise capability identifier is applicable only
* for NET_CAPABILITY_ENTERPRISE capability * for NET_CAPABILITY_ENTERPRISE capability
* *
* @param enterpriseCapabilitySubLevel enterprise capability sub-level. * @param enterpriseId enterprise capability identifier.
* *
* @return this builder * @return this builder
*/ */
@NonNull @NonNull
public Builder addEnterpriseCapabilitySubLevel( public Builder addEnterpriseId(
@EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @EnterpriseId int enterpriseId) {
mCaps.addEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel); mCaps.addEnterpriseId(enterpriseId);
return this; return this;
} }
/** /**
* Removes the given enterprise capability sub level. Enterprise capability sub-level is * Removes the given enterprise capability identifier. Enterprise capability identifier is
* applicable only for NET_CAPABILITY_ENTERPRISE capability * applicable only for NET_CAPABILITY_ENTERPRISE capability
* *
* @param enterpriseCapabilitySubLevel the enterprise capability subLevel * @param enterpriseId the enterprise capability identifier
* @return this builder * @return this builder
*/ */
@NonNull @NonNull
public Builder removeEnterpriseCapabilitySubLevel( public Builder removeEnterpriseId(
@EnterpriseCapabilitySubLevel int enterpriseCapabilitySubLevel) { @EnterpriseId int enterpriseId) {
mCaps.removeEnterpriseCapabilitySubLevel(enterpriseCapabilitySubLevel); mCaps.removeEnterpriseId(enterpriseId);
return this; return this;
} }
@@ -2902,10 +2919,10 @@ public final class NetworkCapabilities implements Parcelable {
} }
} }
if ((mCaps.getEnterpriseCapabilitySubLevels().length != 0) if ((mCaps.getEnterpriseIds().length != 0)
&& !mCaps.hasCapability(NET_CAPABILITY_ENTERPRISE)) { && !mCaps.hasCapability(NET_CAPABILITY_ENTERPRISE)) {
throw new IllegalStateException("Enterprise capability sublevel is applicable only" throw new IllegalStateException("Enterprise capability identifier is applicable"
+ " with ENTERPRISE capability."); + " only with ENTERPRISE capability.");
} }
return new NetworkCapabilities(mCaps); return new NetworkCapabilities(mCaps);
} }

View File

@@ -18,6 +18,8 @@ package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT; import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
@@ -38,12 +40,15 @@ import java.util.Objects;
@SystemApi(client = MODULE_LIBRARIES) @SystemApi(client = MODULE_LIBRARIES)
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 List<Integer> mIncludedUids; private final List<Integer> mIncludedUids;
private final List<Integer> mExcludedUids; private final List<Integer> mExcludedUids;
private ProfileNetworkPreference(int preference, List<Integer> includedUids, private ProfileNetworkPreference(int preference, List<Integer> includedUids,
List<Integer> excludedUids) { List<Integer> excludedUids,
@NetworkCapabilities.EnterpriseId int preferenceEnterpriseId) {
mPreference = preference; mPreference = preference;
mPreferenceEnterpriseId = preferenceEnterpriseId;
if (includedUids != null) { if (includedUids != null) {
mIncludedUids = new ArrayList<>(includedUids); mIncludedUids = new ArrayList<>(includedUids);
} else { } else {
@@ -61,6 +66,7 @@ public final class ProfileNetworkPreference implements Parcelable {
mPreference = in.readInt(); mPreference = in.readInt();
mIncludedUids = in.readArrayList(Integer.class.getClassLoader()); mIncludedUids = in.readArrayList(Integer.class.getClassLoader());
mExcludedUids = in.readArrayList(Integer.class.getClassLoader()); mExcludedUids = in.readArrayList(Integer.class.getClassLoader());
mPreferenceEnterpriseId = in.readInt();
} }
public int getPreference() { public int getPreference() {
@@ -95,12 +101,30 @@ public final class ProfileNetworkPreference implements Parcelable {
return new ArrayList<>(mExcludedUids); return new ArrayList<>(mExcludedUids);
} }
/**
* Get preference enterprise identifier.
*
* Preference enterprise identifier will be used to create different network preferences
* within enterprise preference category.
* Valid values starts from PROFILE_NETWORK_PREFERENCE_ENTERPRISE_ID_1 to
* NetworkCapabilities.NET_ENTERPRISE_ID_5.
* Preference identifier is not applicable if preference is set as
* PROFILE_NETWORK_PREFERENCE_DEFAULT. Default value is
* NetworkCapabilities.NET_ENTERPRISE_ID_1.
* @return Preference enterprise identifier.
*
*/
public @NetworkCapabilities.EnterpriseId int getPreferenceEnterpriseId() {
return mPreferenceEnterpriseId;
}
@Override @Override
public String toString() { public String toString() {
return "ProfileNetworkPreference{" return "ProfileNetworkPreference{"
+ "mPreference=" + getPreference() + "mPreference=" + getPreference()
+ "mIncludedUids=" + mIncludedUids.toString() + "mIncludedUids=" + mIncludedUids.toString()
+ "mExcludedUids=" + mExcludedUids.toString() + "mExcludedUids=" + mExcludedUids.toString()
+ "mPreferenceEnterpriseId=" + mPreferenceEnterpriseId
+ '}'; + '}';
} }
@@ -111,12 +135,14 @@ public final class ProfileNetworkPreference implements Parcelable {
final ProfileNetworkPreference that = (ProfileNetworkPreference) o; final ProfileNetworkPreference that = (ProfileNetworkPreference) o;
return mPreference == that.mPreference return mPreference == that.mPreference
&& (Objects.equals(mIncludedUids, that.mIncludedUids)) && (Objects.equals(mIncludedUids, that.mIncludedUids))
&& (Objects.equals(mExcludedUids, that.mExcludedUids)); && (Objects.equals(mExcludedUids, that.mExcludedUids))
&& mPreferenceEnterpriseId == that.mPreferenceEnterpriseId;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return mPreference return mPreference
+ mPreferenceEnterpriseId * 2
+ (Objects.hashCode(mIncludedUids) * 11) + (Objects.hashCode(mIncludedUids) * 11)
+ (Objects.hashCode(mExcludedUids) * 13); + (Objects.hashCode(mExcludedUids) * 13);
} }
@@ -130,6 +156,7 @@ public final class ProfileNetworkPreference implements Parcelable {
PROFILE_NETWORK_PREFERENCE_DEFAULT; PROFILE_NETWORK_PREFERENCE_DEFAULT;
private @NonNull List<Integer> mIncludedUids = new ArrayList<>(); private @NonNull List<Integer> mIncludedUids = new ArrayList<>();
private @NonNull List<Integer> mExcludedUids = new ArrayList<>(); private @NonNull List<Integer> mExcludedUids = new ArrayList<>();
private int mPreferenceEnterpriseId;
/** /**
* Constructs an empty Builder with PROFILE_NETWORK_PREFERENCE_DEFAULT profile preference * Constructs an empty Builder with PROFILE_NETWORK_PREFERENCE_DEFAULT profile preference
@@ -191,6 +218,23 @@ public final class ProfileNetworkPreference implements Parcelable {
return this; return this;
} }
/**
* Check if given preference enterprise identifier is valid
*
* Valid values starts from PROFILE_NETWORK_PREFERENCE_ENTERPRISE_ID_1 to
* NetworkCapabilities.NET_ENTERPRISE_ID_5.
* @return True if valid else false
* @hide
*/
private boolean isEnterpriseIdentifierValid(
@NetworkCapabilities.EnterpriseId int identifier) {
if ((identifier >= NET_ENTERPRISE_ID_1)
&& (identifier <= NET_ENTERPRISE_ID_5)) {
return true;
}
return false;
}
/** /**
* Returns an instance of {@link ProfileNetworkPreference} created from the * Returns an instance of {@link ProfileNetworkPreference} created from the
* fields set on this builder. * fields set on this builder.
@@ -201,7 +245,35 @@ public final class ProfileNetworkPreference implements Parcelable {
throw new IllegalArgumentException("Both includedUids and excludedUids " throw new IllegalArgumentException("Both includedUids and excludedUids "
+ "cannot be nonempty"); + "cannot be nonempty");
} }
return new ProfileNetworkPreference(mPreference, mIncludedUids, mExcludedUids);
if (((mPreference != PROFILE_NETWORK_PREFERENCE_DEFAULT)
&& (!isEnterpriseIdentifierValid(mPreferenceEnterpriseId)))
|| ((mPreference == PROFILE_NETWORK_PREFERENCE_DEFAULT)
&& (mPreferenceEnterpriseId != 0))) {
throw new IllegalStateException("Invalid preference enterprise identifier");
}
return new ProfileNetworkPreference(mPreference, mIncludedUids,
mExcludedUids, mPreferenceEnterpriseId);
}
/**
* Set the preference enterprise identifier.
*
* Preference enterprise identifier will be used to create different network preferences
* within enterprise preference category.
* Valid values starts from NetworkCapabilities.NET_ENTERPRISE_ID_1 to
* NetworkCapabilities.NET_ENTERPRISE_ID_5.
* Preference identifier is not applicable if preference is set as
* PROFILE_NETWORK_PREFERENCE_DEFAULT. Default value is
* NetworkCapabilities.NET_ENTERPRISE_ID_1.
* @param preferenceId preference sub level
* @return The builder to facilitate chaining.
*/
@NonNull
public Builder setPreferenceEnterpriseId(
@NetworkCapabilities.EnterpriseId int preferenceId) {
mPreferenceEnterpriseId = preferenceId;
return this;
} }
} }
@@ -210,6 +282,7 @@ public final class ProfileNetworkPreference implements Parcelable {
dest.writeInt(mPreference); dest.writeInt(mPreference);
dest.writeList(mIncludedUids); dest.writeList(mIncludedUids);
dest.writeList(mExcludedUids); dest.writeList(mExcludedUids);
dest.writeInt(mPreferenceEnterpriseId);
} }
@Override @Override

View File

@@ -73,6 +73,8 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID;
import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE; import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE;
import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY; import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY;
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5;
import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION;
import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS;
import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
@@ -10147,11 +10149,19 @@ public class ConnectivityService extends IConnectivityManager.Stub
switch (preference.getPreference()) { switch (preference.getPreference()) {
case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT: case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT:
nc = null; nc = null;
if (preference.getPreferenceEnterpriseId() != 0) {
throw new IllegalArgumentException(
"Invalid enterprise identifier in setProfileNetworkPreferences");
}
break; break;
case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK: case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK:
allowFallback = false; allowFallback = false;
// continue to process the enterprise preference. // continue to process the enterprise preference.
case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE: case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE:
if (!isEnterpriseIdentifierValid(preference.getPreferenceEnterpriseId())) {
throw new IllegalArgumentException(
"Invalid enterprise identifier in setProfileNetworkPreferences");
}
final Set<UidRange> uidRangeSet = final Set<UidRange> uidRangeSet =
getUidListToBeAppliedForNetworkPreference(profile, preference); getUidListToBeAppliedForNetworkPreference(profile, preference);
if (!isRangeAlreadyInPreferenceList(preferenceList, uidRangeSet)) { if (!isRangeAlreadyInPreferenceList(preferenceList, uidRangeSet)) {
@@ -10161,6 +10171,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
"Overlapping uid range in setProfileNetworkPreferences"); "Overlapping uid range in setProfileNetworkPreferences");
} }
nc.addCapability(NET_CAPABILITY_ENTERPRISE); nc.addCapability(NET_CAPABILITY_ENTERPRISE);
nc.addEnterpriseId(
preference.getPreferenceEnterpriseId());
nc.removeCapability(NET_CAPABILITY_NOT_RESTRICTED); nc.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
break; break;
default: default:
@@ -10203,6 +10215,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
return uidRangeSet; return uidRangeSet;
} }
private boolean isEnterpriseIdentifierValid(
@NetworkCapabilities.EnterpriseId int identifier) {
if ((identifier >= NET_ENTERPRISE_ID_1)
&& (identifier <= NET_ENTERPRISE_ID_5)) {
return true;
}
return false;
}
private void validateNetworkCapabilitiesOfProfileNetworkPreference( private void validateNetworkCapabilitiesOfProfileNetworkPreference(
@Nullable final NetworkCapabilities nc) { @Nullable final NetworkCapabilities nc) {
if (null == nc) return; // Null caps are always allowed. It means to remove the setting. if (null == nc) return; // Null caps are always allowed. It means to remove the setting.

View File

@@ -23,11 +23,6 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL;
import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS; import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS;
import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS; import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE; import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4;
import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5;
import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND;
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS; import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS;
@@ -42,6 +37,11 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVIT
import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_2;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_3;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_4;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_5;
import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION;
import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS;
import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
@@ -790,78 +790,79 @@ public class NetworkCapabilitiesTest {
} }
@Test @IgnoreUpTo(SC_V2) // TODO: Use to Build.VERSION_CODES.SC_V2 when available @Test @IgnoreUpTo(SC_V2) // TODO: Use to Build.VERSION_CODES.SC_V2 when available
public void testEnterpriseCapabilitySubLevel() { public void testEnterpriseId() {
final NetworkCapabilities nc1 = new NetworkCapabilities.Builder() final NetworkCapabilities nc1 = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_ENTERPRISE) .addCapability(NET_CAPABILITY_ENTERPRISE)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.build(); .build();
assertEquals(1, nc1.getEnterpriseCapabilitySubLevels().length); assertEquals(1, nc1.getEnterpriseIds().length);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, assertEquals(NET_ENTERPRISE_ID_1,
nc1.getEnterpriseCapabilitySubLevels()[0]); nc1.getEnterpriseIds()[0]);
final NetworkCapabilities nc2 = new NetworkCapabilities.Builder() final NetworkCapabilities nc2 = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_ENTERPRISE) .addCapability(NET_CAPABILITY_ENTERPRISE)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .addEnterpriseId(NET_ENTERPRISE_ID_2)
.build(); .build();
assertEquals(2, nc2.getEnterpriseCapabilitySubLevels().length); assertEquals(2, nc2.getEnterpriseIds().length);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, assertEquals(NET_ENTERPRISE_ID_1,
nc2.getEnterpriseCapabilitySubLevels()[0]); nc2.getEnterpriseIds()[0]);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, assertEquals(NET_ENTERPRISE_ID_2,
nc2.getEnterpriseCapabilitySubLevels()[1]); nc2.getEnterpriseIds()[1]);
final NetworkCapabilities nc3 = new NetworkCapabilities.Builder() final NetworkCapabilities nc3 = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_ENTERPRISE) .addCapability(NET_CAPABILITY_ENTERPRISE)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .addEnterpriseId(NET_ENTERPRISE_ID_2)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3) .addEnterpriseId(NET_ENTERPRISE_ID_3)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4) .addEnterpriseId(NET_ENTERPRISE_ID_4)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5) .addEnterpriseId(NET_ENTERPRISE_ID_5)
.build(); .build();
assertEquals(5, nc3.getEnterpriseCapabilitySubLevels().length); assertEquals(5, nc3.getEnterpriseIds().length);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1, assertEquals(NET_ENTERPRISE_ID_1,
nc3.getEnterpriseCapabilitySubLevels()[0]); nc3.getEnterpriseIds()[0]);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2, assertEquals(NET_ENTERPRISE_ID_2,
nc3.getEnterpriseCapabilitySubLevels()[1]); nc3.getEnterpriseIds()[1]);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_3, assertEquals(NET_ENTERPRISE_ID_3,
nc3.getEnterpriseCapabilitySubLevels()[2]); nc3.getEnterpriseIds()[2]);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_4, assertEquals(NET_ENTERPRISE_ID_4,
nc3.getEnterpriseCapabilitySubLevels()[3]); nc3.getEnterpriseIds()[3]);
assertEquals(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_5, assertEquals(NET_ENTERPRISE_ID_5,
nc3.getEnterpriseCapabilitySubLevels()[4]); nc3.getEnterpriseIds()[4]);
final Class<IllegalArgumentException> illegalArgumentExceptionClass = final Class<IllegalArgumentException> illegalArgumentExceptionClass =
IllegalArgumentException.class; IllegalArgumentException.class;
assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder() assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder()
.addEnterpriseCapabilitySubLevel(6) .addEnterpriseId(6)
.build()); .build());
assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder() assertThrows(illegalArgumentExceptionClass, () -> new NetworkCapabilities.Builder()
.removeEnterpriseCapabilitySubLevel(6) .removeEnterpriseId(6)
.build()); .build());
final Class<IllegalStateException> illegalStateException = final Class<IllegalStateException> illegalStateException =
IllegalStateException.class; IllegalStateException.class;
assertThrows(illegalStateException, () -> new NetworkCapabilities.Builder() assertThrows(illegalStateException, () -> new NetworkCapabilities.Builder()
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.build()); .build());
final NetworkCapabilities nc4 = new NetworkCapabilities.Builder() final NetworkCapabilities nc4 = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_ENTERPRISE) .addCapability(NET_CAPABILITY_ENTERPRISE)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .addEnterpriseId(NET_ENTERPRISE_ID_2)
.removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .removeEnterpriseId(NET_ENTERPRISE_ID_1)
.removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .removeEnterpriseId(NET_ENTERPRISE_ID_2)
.build(); .build();
assertEquals(0, nc4.getEnterpriseCapabilitySubLevels().length); assertEquals(1, nc4.getEnterpriseIds().length);
assertTrue(nc4.hasEnterpriseId(NET_ENTERPRISE_ID_1));
final NetworkCapabilities nc5 = new NetworkCapabilities.Builder() final NetworkCapabilities nc5 = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_CBS) .addCapability(NET_CAPABILITY_CBS)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .addEnterpriseId(NET_ENTERPRISE_ID_1)
.addEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .addEnterpriseId(NET_ENTERPRISE_ID_2)
.removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_1) .removeEnterpriseId(NET_ENTERPRISE_ID_1)
.removeEnterpriseCapabilitySubLevel(NET_CAPABILITY_ENTERPRISE_SUB_LEVEL_2) .removeEnterpriseId(NET_ENTERPRISE_ID_2)
.build(); .build();
assertTrue(nc4.satisfiedByNetworkCapabilities(nc1)); assertTrue(nc4.satisfiedByNetworkCapabilities(nc1));
assertFalse(nc1.satisfiedByNetworkCapabilities(nc4)); assertTrue(nc1.satisfiedByNetworkCapabilities(nc4));
assertFalse(nc3.satisfiedByNetworkCapabilities(nc2)); assertFalse(nc3.satisfiedByNetworkCapabilities(nc2));
assertTrue(nc2.satisfiedByNetworkCapabilities(nc3)); assertTrue(nc2.satisfiedByNetworkCapabilities(nc3));

View File

@@ -101,6 +101,7 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_VSIM; import static android.net.NetworkCapabilities.NET_CAPABILITY_VSIM;
import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P;
import static android.net.NetworkCapabilities.NET_CAPABILITY_XCAP; import static android.net.NetworkCapabilities.NET_CAPABILITY_XCAP;
import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION;
import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS;
import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
@@ -13780,6 +13781,14 @@ public class ConnectivityServiceTest {
return new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), workNc); return new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), workNc);
} }
private TestNetworkAgentWrapper makeEnterpriseNetworkAgent(int enterpriseId) throws Exception {
final NetworkCapabilities workNc = new NetworkCapabilities();
workNc.addCapability(NET_CAPABILITY_ENTERPRISE);
workNc.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
workNc.addEnterpriseId(enterpriseId);
return new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), workNc);
}
private TestNetworkCallback mEnterpriseCallback; private TestNetworkCallback mEnterpriseCallback;
private UserHandle setupEnterpriseNetwork() { private UserHandle setupEnterpriseNetwork() {
final UserHandle userHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID); final UserHandle userHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID);
@@ -13827,7 +13836,8 @@ public class ConnectivityServiceTest {
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical( inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE)); mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
final TestNetworkAgentWrapper workAgent = makeEnterpriseNetworkAgent(); final TestNetworkAgentWrapper workAgent =
makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId());
if (connectWorkProfileAgentAhead) { if (connectWorkProfileAgentAhead) {
workAgent.connect(false); workAgent.connect(false);
} }
@@ -13841,7 +13851,7 @@ public class ConnectivityServiceTest {
== PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK) { == PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK) {
allowFallback = false; allowFallback = false;
} }
if (allowFallback) { if (allowFallback && !connectWorkProfileAgentAhead) {
// Setting a network preference for this user will create a new set of routing rules for // Setting a network preference for this user will create a new set of routing rules for
// the UID range that corresponds to this user, inorder to define the default network // the UID range that corresponds to this user, inorder to define the default network
// for these apps separately. This is true because the multi-layer request relevant to // for these apps separately. This is true because the multi-layer request relevant to
@@ -13858,7 +13868,7 @@ public class ConnectivityServiceTest {
// The enterprise network is not ready yet. // The enterprise network is not ready yet.
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback); assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
if (allowFallback) { if (allowFallback && !connectWorkProfileAgentAhead) {
assertNoCallbacks(profileDefaultNetworkCallback); assertNoCallbacks(profileDefaultNetworkCallback);
} else if (!connectWorkProfileAgentAhead) { } else if (!connectWorkProfileAgentAhead) {
profileDefaultNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent); profileDefaultNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent);
@@ -13884,7 +13894,7 @@ public class ConnectivityServiceTest {
uidRangeFor(testHandle, profileNetworkPreference), uidRangeFor(testHandle, profileNetworkPreference),
PREFERENCE_ORDER_PROFILE)); PREFERENCE_ORDER_PROFILE));
if (allowFallback) { if (allowFallback && !connectWorkProfileAgentAhead) {
inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig( inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
mCellNetworkAgent.getNetwork().netId, mCellNetworkAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreference), uidRangeFor(testHandle, profileNetworkPreference),
@@ -13897,7 +13907,10 @@ public class ConnectivityServiceTest {
workAgent.mNetworkMonitor.forceReevaluation(Process.myUid()); workAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent, profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent,
nc -> nc.hasCapability(NET_CAPABILITY_VALIDATED) nc -> nc.hasCapability(NET_CAPABILITY_VALIDATED)
&& nc.hasCapability(NET_CAPABILITY_ENTERPRISE)); && nc.hasCapability(NET_CAPABILITY_ENTERPRISE)
&& nc.hasEnterpriseId(
profileNetworkPreference.getPreferenceEnterpriseId())
&& nc.getEnterpriseIds().length == 1);
if (disAllowProfileDefaultNetworkCallback != null) { if (disAllowProfileDefaultNetworkCallback != null) {
assertNoCallbacks(disAllowProfileDefaultNetworkCallback); assertNoCallbacks(disAllowProfileDefaultNetworkCallback);
} }
@@ -13988,7 +14001,8 @@ public class ConnectivityServiceTest {
// If the control comes here, callbacks seem to behave correctly in the presence of // If the control comes here, callbacks seem to behave correctly in the presence of
// a default network when the enterprise network goes up and down. Now, make sure they // a default network when the enterprise network goes up and down. Now, make sure they
// also behave correctly in the absence of a system-wide default network. // also behave correctly in the absence of a system-wide default network.
final TestNetworkAgentWrapper workAgent2 = makeEnterpriseNetworkAgent(); final TestNetworkAgentWrapper workAgent2 =
makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId());
workAgent2.connect(false); workAgent2.connect(false);
profileDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(workAgent2); profileDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(workAgent2);
@@ -14006,7 +14020,10 @@ public class ConnectivityServiceTest {
workAgent2.mNetworkMonitor.forceReevaluation(Process.myUid()); workAgent2.mNetworkMonitor.forceReevaluation(Process.myUid());
profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent2, profileDefaultNetworkCallback.expectCapabilitiesThat(workAgent2,
nc -> nc.hasCapability(NET_CAPABILITY_ENTERPRISE) nc -> nc.hasCapability(NET_CAPABILITY_ENTERPRISE)
&& !nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); && !nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)
&& nc.hasEnterpriseId(
profileNetworkPreference.getPreferenceEnterpriseId())
&& nc.getEnterpriseIds().length == 1);
if (disAllowProfileDefaultNetworkCallback != null) { if (disAllowProfileDefaultNetworkCallback != null) {
assertNoCallbacks(disAllowProfileDefaultNetworkCallback); assertNoCallbacks(disAllowProfileDefaultNetworkCallback);
} }
@@ -14037,10 +14054,11 @@ public class ConnectivityServiceTest {
@Test @Test
public void testPreferenceForUserNetworkUpDown() throws Exception { public void testPreferenceForUserNetworkUpDown() throws Exception {
final UserHandle testHandle = setupEnterpriseNetwork(); final UserHandle testHandle = setupEnterpriseNetwork();
registerDefaultNetworkCallbacks();
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
registerDefaultNetworkCallbacks(); profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), false, profileNetworkPreferenceBuilder.build(), false,
testHandle, mProfileDefaultNetworkCallback, null); testHandle, mProfileDefaultNetworkCallback, null);
@@ -14058,6 +14076,7 @@ public class ConnectivityServiceTest {
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference( profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), false, profileNetworkPreferenceBuilder.build(), false,
@@ -14078,6 +14097,7 @@ public class ConnectivityServiceTest {
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference( profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK); PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference( testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), true, testHandle, profileNetworkPreferenceBuilder.build(), true, testHandle,
@@ -14094,6 +14114,7 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID))); List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID)));
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
@@ -14112,6 +14133,7 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)));
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
@@ -14130,6 +14152,7 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)));
registerDefaultNetworkCallbacks(); registerDefaultNetworkCallbacks();
@@ -14149,6 +14172,7 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder.setExcludedUids( profileNetworkPreferenceBuilder.setExcludedUids(
List.of(testHandle.getUid(0) - 1)); List.of(testHandle.getUid(0) - 1));
final TestOnCompleteListener listener = new TestOnCompleteListener(); final TestOnCompleteListener listener = new TestOnCompleteListener();
@@ -14178,6 +14202,7 @@ public class ConnectivityServiceTest {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder2 = ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder2 =
new ProfileNetworkPreference.Builder(); new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE); profileNetworkPreferenceBuilder2.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder2.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
profileNetworkPreferenceBuilder2.setIncludedUids( profileNetworkPreferenceBuilder2.setIncludedUids(
List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2))); List.of(testHandle.getUid(TEST_WORK_PROFILE_APP_UID_2)));
profileNetworkPreferenceBuilder.setIncludedUids( profileNetworkPreferenceBuilder.setIncludedUids(
@@ -14212,6 +14237,84 @@ public class ConnectivityServiceTest {
r -> r.run(), listener)); r -> r.run(), listener));
} }
/**
* Make sure per-profile networking preference behaves as expected when the enterprise network
* goes up and down while the preference is active. Make sure they behave as expected whether
* there is a general default network or not when configured to fallback to default network
* along with already connected enterprise work agent
*/
@Test
public void testPreferenceForUserNetworkUpDownWithFallbackWithAlreadyConnectedWorkAgent()
throws Exception {
final UserHandle testHandle = setupEnterpriseNetwork();
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), true,
testHandle, mProfileDefaultNetworkCallback,
null);
}
/**
* Make sure per-profile networking preference behaves as expected when the enterprise network
* goes up and down while the preference is active for a given enterprise identifier
*/
@Test
public void testPreferenceForUserNetworkUpDownWithDefaultEnterpriseId()
throws Exception {
final UserHandle testHandle = setupEnterpriseNetwork();
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), true,
testHandle, mProfileDefaultNetworkCallback,
null);
}
/**
* Make sure per-profile networking preference behaves as expected when the enterprise network
* goes up and down while the preference is active for a given enterprise identifier
*/
@Test
public void testPreferenceForUserNetworkUpDownWithId2()
throws Exception {
final UserHandle testHandle = setupEnterpriseNetwork();
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(
NetworkCapabilities.NET_ENTERPRISE_ID_2);
registerDefaultNetworkCallbacks();
testPreferenceForUserNetworkUpDownForGivenPreference(
profileNetworkPreferenceBuilder.build(), true,
testHandle, mProfileDefaultNetworkCallback, null);
}
/**
* Make sure per-profile networking preference behaves as expected when the enterprise network
* goes up and down while the preference is active for a given enterprise identifier
*/
@Test
public void testPreferenceForUserNetworkUpDownWithInvalidId()
throws Exception {
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(0);
registerDefaultNetworkCallbacks();
assertThrows("Should not be able to set invalid enterprise id",
IllegalStateException.class, () -> profileNetworkPreferenceBuilder.build());
}
/** /**
* Test that, in a given networking context, calling setPreferenceForUser to set per-profile * Test that, in a given networking context, calling setPreferenceForUser to set per-profile
* defaults on then off works as expected. * defaults on then off works as expected.
@@ -14362,10 +14465,15 @@ public class ConnectivityServiceTest {
public void testProfileNetworkPrefWrongPreference() throws Exception { public void testProfileNetworkPrefWrongPreference() throws Exception {
final UserHandle testHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID); final UserHandle testHandle = UserHandle.of(TEST_WORK_PROFILE_USER_ID);
mServiceContext.setWorkProfile(testHandle, true); mServiceContext.setWorkProfile(testHandle, true);
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK + 1);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
assertThrows("Should not be able to set an illegal preference", assertThrows("Should not be able to set an illegal preference",
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mCm.setProfileNetworkPreference(testHandle, () -> mCm.setProfileNetworkPreferences(testHandle,
PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK + 1, List.of(profileNetworkPreferenceBuilder.build()),
null, null)); null, null));
} }