Merge "Add combine() and equals() for NetworkCapabilities admin UIDs." am: e4df39ba4b
Change-Id: Ic8b353d233eda759100952e126d77e9dd48005e5
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
|
import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
|
||||||
|
|
||||||
import android.annotation.IntDef;
|
import android.annotation.IntDef;
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
@@ -118,7 +120,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
mTransportInfo = nc.mTransportInfo;
|
mTransportInfo = nc.mTransportInfo;
|
||||||
mSignalStrength = nc.mSignalStrength;
|
mSignalStrength = nc.mSignalStrength;
|
||||||
setUids(nc.mUids); // Will make the defensive copy
|
setUids(nc.mUids); // Will make the defensive copy
|
||||||
setAdministratorUids(nc.mAdministratorUids);
|
setAdministratorUids(nc.getAdministratorUids());
|
||||||
mOwnerUid = nc.mOwnerUid;
|
mOwnerUid = nc.mOwnerUid;
|
||||||
mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
|
mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
|
||||||
mSSID = nc.mSSID;
|
mSSID = nc.mSSID;
|
||||||
@@ -919,6 +921,9 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
* <p>For NetworkCapability instances being sent from the System Server, this value MUST be
|
* <p>For NetworkCapability instances being sent from the System Server, this value MUST be
|
||||||
* empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
|
* empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
|
||||||
* receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
|
* receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
|
||||||
|
*
|
||||||
|
* <p>When received from an app in a NetworkRequest this is always cleared out by the system
|
||||||
|
* server. This field is never used for matching NetworkRequests to NetworkAgents.
|
||||||
*/
|
*/
|
||||||
@NonNull private int[] mAdministratorUids = new int[0];
|
@NonNull private int[] mAdministratorUids = new int[0];
|
||||||
|
|
||||||
@@ -927,10 +932,11 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
*
|
*
|
||||||
* <p>UIDs included in administratorUids gain administrator privileges over this Network.
|
* <p>UIDs included in administratorUids gain administrator privileges over this Network.
|
||||||
* Examples of UIDs that should be included in administratorUids are:
|
* Examples of UIDs that should be included in administratorUids are:
|
||||||
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>Carrier apps with privileges for the relevant subscription
|
* <li>Carrier apps with privileges for the relevant subscription
|
||||||
* <li>Active VPN apps
|
* <li>Active VPN apps
|
||||||
* <li>Other application groups with a particular Network-related role
|
* <li>Other application groups with a particular Network-related role
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
|
* <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
|
||||||
@@ -938,7 +944,10 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
* <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
|
* <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
|
||||||
* always be included in administratorUids.
|
* always be included in administratorUids.
|
||||||
*
|
*
|
||||||
|
* <p>The administrator UIDs are set by network agents.
|
||||||
|
*
|
||||||
* @param administratorUids the UIDs to be set as administrators of this Network.
|
* @param administratorUids the UIDs to be set as administrators of this Network.
|
||||||
|
* @see #mAdministratorUids
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -950,7 +959,12 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
/**
|
/**
|
||||||
* Retrieves the UIDs that are administrators of this Network.
|
* Retrieves the UIDs that are administrators of this Network.
|
||||||
*
|
*
|
||||||
|
* <p>This is only populated in NetworkCapabilities objects that come from network agents for
|
||||||
|
* networks that are managed by specific apps on the system, such as carrier privileged apps or
|
||||||
|
* wifi suggestion apps. This will include the network owner.
|
||||||
|
*
|
||||||
* @return the int[] of UIDs that are administrators of this Network
|
* @return the int[] of UIDs that are administrators of this Network
|
||||||
|
* @see #mAdministratorUids
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -960,6 +974,40 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
|
return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if the set of administrator UIDs of this network is the same as that of the passed one.
|
||||||
|
*
|
||||||
|
* <p>The administrator UIDs must be in sorted order.
|
||||||
|
*
|
||||||
|
* <p>nc is assumed non-null. Else, NPE.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@VisibleForTesting(visibility = PRIVATE)
|
||||||
|
public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) {
|
||||||
|
return Arrays.equals(mAdministratorUids, nc.mAdministratorUids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combine the administrator UIDs of the capabilities.
|
||||||
|
*
|
||||||
|
* <p>This is only legal if either of the administrators lists are empty, or if they are equal.
|
||||||
|
* Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs.
|
||||||
|
*
|
||||||
|
* <p>If both administrator lists are non-empty but not equal, they conflict with each other. In
|
||||||
|
* this case, it would not make sense to add them together.
|
||||||
|
*/
|
||||||
|
private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) {
|
||||||
|
if (nc.mAdministratorUids.length == 0) return;
|
||||||
|
if (mAdministratorUids.length == 0) {
|
||||||
|
mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!equalsAdministratorUids(nc)) {
|
||||||
|
throw new IllegalStateException("Can't combine two different administrator UID lists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value indicating that link bandwidth is unspecified.
|
* Value indicating that link bandwidth is unspecified.
|
||||||
* @hide
|
* @hide
|
||||||
@@ -1455,6 +1503,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
combineUids(nc);
|
combineUids(nc);
|
||||||
combineSSIDs(nc);
|
combineSSIDs(nc);
|
||||||
combineRequestor(nc);
|
combineRequestor(nc);
|
||||||
|
combineAdministratorUids(nc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1568,7 +1617,8 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
&& equalsUids(that)
|
&& equalsUids(that)
|
||||||
&& equalsSSID(that)
|
&& equalsSSID(that)
|
||||||
&& equalsPrivateDnsBroken(that)
|
&& equalsPrivateDnsBroken(that)
|
||||||
&& equalsRequestor(that);
|
&& equalsRequestor(that)
|
||||||
|
&& equalsAdministratorUids(that);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1588,7 +1638,8 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
+ Objects.hashCode(mTransportInfo) * 41
|
+ Objects.hashCode(mTransportInfo) * 41
|
||||||
+ Objects.hashCode(mPrivateDnsBroken) * 43
|
+ Objects.hashCode(mPrivateDnsBroken) * 43
|
||||||
+ Objects.hashCode(mRequestorUid) * 47
|
+ Objects.hashCode(mRequestorUid) * 47
|
||||||
+ Objects.hashCode(mRequestorPackageName) * 53;
|
+ Objects.hashCode(mRequestorPackageName) * 53
|
||||||
|
+ Arrays.hashCode(mAdministratorUids) * 59;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1609,7 +1660,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||||||
dest.writeArraySet(mUids);
|
dest.writeArraySet(mUids);
|
||||||
dest.writeString(mSSID);
|
dest.writeString(mSSID);
|
||||||
dest.writeBoolean(mPrivateDnsBroken);
|
dest.writeBoolean(mPrivateDnsBroken);
|
||||||
dest.writeIntArray(mAdministratorUids);
|
dest.writeIntArray(getAdministratorUids());
|
||||||
dest.writeInt(mOwnerUid);
|
dest.writeInt(mOwnerUid);
|
||||||
dest.writeInt(mRequestorUid);
|
dest.writeInt(mRequestorUid);
|
||||||
dest.writeString(mRequestorPackageName);
|
dest.writeString(mRequestorPackageName);
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ import androidx.test.runner.AndroidJUnit4;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
@@ -280,6 +281,7 @@ public class NetworkCapabilitiesTest {
|
|||||||
.addCapability(NET_CAPABILITY_NOT_METERED);
|
.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||||
if (isAtLeastR()) {
|
if (isAtLeastR()) {
|
||||||
netCap.setOwnerUid(123);
|
netCap.setOwnerUid(123);
|
||||||
|
netCap.setAdministratorUids(new int[] {5, 11});
|
||||||
}
|
}
|
||||||
assertParcelingIsLossless(netCap);
|
assertParcelingIsLossless(netCap);
|
||||||
netCap.setSSID(TEST_SSID);
|
netCap.setSSID(TEST_SSID);
|
||||||
@@ -491,6 +493,25 @@ public class NetworkCapabilitiesTest {
|
|||||||
assertFalse(nc2.appliesToUid(12));
|
assertFalse(nc2.appliesToUid(12));
|
||||||
assertTrue(nc1.appliesToUid(22));
|
assertTrue(nc1.appliesToUid(22));
|
||||||
assertTrue(nc2.appliesToUid(22));
|
assertTrue(nc2.appliesToUid(22));
|
||||||
|
|
||||||
|
final int[] adminUids = {3, 6, 12};
|
||||||
|
nc1.setAdministratorUids(adminUids);
|
||||||
|
nc2.combineCapabilities(nc1);
|
||||||
|
assertTrue(nc2.equalsAdministratorUids(nc1));
|
||||||
|
assertArrayEquals(nc2.getAdministratorUids(), adminUids);
|
||||||
|
|
||||||
|
final int[] adminUidsOtherOrder = {3, 12, 6};
|
||||||
|
nc1.setAdministratorUids(adminUids);
|
||||||
|
assertTrue(nc2.equalsAdministratorUids(nc1));
|
||||||
|
|
||||||
|
final int[] adminUids2 = {11, 1, 12, 3, 6};
|
||||||
|
nc1.setAdministratorUids(adminUids2);
|
||||||
|
assertFalse(nc2.equalsAdministratorUids(nc1));
|
||||||
|
assertFalse(Arrays.equals(nc2.getAdministratorUids(), adminUids2));
|
||||||
|
try {
|
||||||
|
nc2.combineCapabilities(nc1);
|
||||||
|
fail("Shouldn't be able to combine different lists of admin UIDs");
|
||||||
|
} catch (IllegalStateException expected) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user