diff --git a/framework/src/android/net/NetworkRequest.java b/framework/src/android/net/NetworkRequest.java index 5ae25abf80..b95363a05d 100644 --- a/framework/src/android/net/NetworkRequest.java +++ b/framework/src/android/net/NetworkRequest.java @@ -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) { diff --git a/tests/cts/net/src/android/net/cts/NetworkRequestTest.java b/tests/cts/net/src/android/net/cts/NetworkRequestTest.java index 2226f4cb1a..1ca5a77f33 100644 --- a/tests/cts/net/src/android/net/cts/NetworkRequestTest.java +++ b/tests/cts/net/src/android/net/cts/NetworkRequestTest.java @@ -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); + }); + } }