Do not allow any request to pass in reservationId

This change enforces that the reservationId can *only* be set by the
system. The reservationId is set by the system and the app must never be
able to change it.

Test: TH
Change-Id: I9f6a0dc6df1ec09ed2d87de09ce52cdda7611600
This commit is contained in:
Patrick Rohr 2024-12-20 10:29:28 -08:00
commit 4cef67c008
2 changed files with 16 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVIT
import static android.net.NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
import static android.net.NetworkCapabilities.RES_ID_UNSET;
import static android.net.NetworkCapabilities.TRANSPORT_TEST;
import android.annotation.FlaggedApi;
@ -256,6 +257,9 @@ public class NetworkRequest implements Parcelable {
if (nc == null) {
throw new NullPointerException();
}
if (nc.getReservationId() != RES_ID_UNSET) {
throw new IllegalArgumentException("ReservationId must only be set by the system");
}
requestId = rId;
networkCapabilities = nc;
if (type == Type.RESERVATION) {

View file

@ -42,6 +42,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@ -583,4 +584,15 @@ public class NetworkRequestTest {
assertTrue(requestNR.canBeSatisfiedBy(otherSpecificOffer));
assertTrue(requestNR.canBeSatisfiedBy(regularOffer));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.R)
public void testNetworkRequest_throwsWhenPassingCapsWithReservationId() {
final NetworkCapabilities capsWithResId = new NetworkCapabilities();
capsWithResId.setReservationId(42);
assertThrows(IllegalArgumentException.class, () -> {
new NetworkRequest(capsWithResId, TYPE_NONE, 42 /* rId */, NetworkRequest.Type.REQUEST);
});
}
}