From 4cef67c00802506276dca338f7e643252f4c00d1 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Fri, 20 Dec 2024 10:29:28 -0800 Subject: [PATCH] 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 --- framework/src/android/net/NetworkRequest.java | 4 ++++ .../net/src/android/net/cts/NetworkRequestTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+) 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); + }); + } }