Merge "Filter out excluded routes in LinkProperties" am: ac8935bee7

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1619104

Change-Id: I595957964b5c7edea752a3f76791503a3b896ccf
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Taras Antoshchuk
2022-04-13 12:45:04 +00:00
committed by Automerger Merge Worker
5 changed files with 86 additions and 2 deletions

View File

@@ -91,6 +91,8 @@ apex {
compressible: true, compressible: true,
androidManifest: "AndroidManifest.xml", androidManifest: "AndroidManifest.xml",
compat_configs: ["connectivity-platform-compat-config"],
} }
apex_key { apex_key {

View File

@@ -92,6 +92,7 @@ java_defaults {
"modules-utils-preconditions", "modules-utils-preconditions",
], ],
libs: [ libs: [
"app-compat-annotations",
"framework-connectivity-t.stubs.module_lib", "framework-connectivity-t.stubs.module_lib",
"unsupportedappusage", "unsupportedappusage",
], ],
@@ -152,6 +153,11 @@ java_sdk_library {
], ],
} }
platform_compat_config {
name: "connectivity-platform-compat-config",
src: ":framework-connectivity",
}
cc_library_shared { cc_library_shared {
name: "libframework-connectivity-jni", name: "libframework-connectivity-jni",
min_sdk_version: "30", min_sdk_version: "30",

View File

@@ -19,12 +19,16 @@ package android.net;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build; import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.LinkPropertiesUtils; import com.android.net.module.util.LinkPropertiesUtils;
import java.net.Inet4Address; import java.net.Inet4Address;
@@ -38,6 +42,7 @@ import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.stream.Collectors;
/** /**
* Describes the properties of a network link. * Describes the properties of a network link.
@@ -52,6 +57,17 @@ import java.util.StringJoiner;
* *
*/ */
public final class LinkProperties implements Parcelable { public final class LinkProperties implements Parcelable {
/**
* The {@link #getRoutes()} now can contain excluded as well as included routes. Use
* {@link RouteInfo#getType()} to determine route type.
*
* @hide
*/
@ChangeId
@EnabledAfter(targetSdkVersion = Build.VERSION_CODES.S) // Switch to S_V2 when it is available.
@VisibleForTesting
public static final long EXCLUDED_ROUTES = 186082280;
// The interface described by the network link. // The interface described by the network link.
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private String mIfaceName; private String mIfaceName;
@@ -738,10 +754,25 @@ public final class LinkProperties implements Parcelable {
/** /**
* Returns all the {@link RouteInfo} set on this link. * Returns all the {@link RouteInfo} set on this link.
* *
* Only unicast routes are returned for apps targeting Android S or below.
*
* @return An unmodifiable {@link List} of {@link RouteInfo} for this link. * @return An unmodifiable {@link List} of {@link RouteInfo} for this link.
*/ */
public @NonNull List<RouteInfo> getRoutes() { public @NonNull List<RouteInfo> getRoutes() {
return Collections.unmodifiableList(mRoutes); if (CompatChanges.isChangeEnabled(EXCLUDED_ROUTES)) {
return Collections.unmodifiableList(mRoutes);
} else {
return Collections.unmodifiableList(getUnicastRoutes());
}
}
/**
* Returns all the {@link RouteInfo} of type {@link RouteInfo#RTN_UNICAST} set on this link.
*/
private @NonNull List<RouteInfo> getUnicastRoutes() {
return mRoutes.stream()
.filter(route -> route.getType() == RouteInfo.RTN_UNICAST)
.collect(Collectors.toList());
} }
/** /**
@@ -757,11 +788,14 @@ public final class LinkProperties implements Parcelable {
/** /**
* Returns all the routes on this link and all the links stacked above it. * Returns all the routes on this link and all the links stacked above it.
*
* Only unicast routes are returned for apps targeting Android S or below.
*
* @hide * @hide
*/ */
@SystemApi @SystemApi
public @NonNull List<RouteInfo> getAllRoutes() { public @NonNull List<RouteInfo> getAllRoutes() {
List<RouteInfo> routes = new ArrayList<>(mRoutes); final List<RouteInfo> routes = new ArrayList<>(getRoutes());
for (LinkProperties stacked: mStackedLinks.values()) { for (LinkProperties stacked: mStackedLinks.values()) {
routes.addAll(stacked.getAllRoutes()); routes.addAll(stacked.getAllRoutes());
} }

View File

@@ -36,6 +36,7 @@ java_library {
"modules-utils-build", "modules-utils-build",
"net-tests-utils", "net-tests-utils",
"net-utils-framework-common", "net-utils-framework-common",
"platform-compat-test-rules",
"platform-test-annotations", "platform-test-annotations",
], ],
libs: [ libs: [

View File

@@ -20,6 +20,7 @@ import static android.net.RouteInfo.RTN_THROW;
import static android.net.RouteInfo.RTN_UNICAST; import static android.net.RouteInfo.RTN_UNICAST;
import static android.net.RouteInfo.RTN_UNREACHABLE; import static android.net.RouteInfo.RTN_UNREACHABLE;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
import static com.android.testutils.ParcelUtils.parcelingRoundTrip; import static com.android.testutils.ParcelUtils.parcelingRoundTrip;
@@ -30,6 +31,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.compat.testing.PlatformCompatChangeRule;
import android.net.LinkProperties.ProvisioningChange; import android.net.LinkProperties.ProvisioningChange;
import android.os.Build; import android.os.Build;
import android.system.OsConstants; import android.system.OsConstants;
@@ -45,6 +47,9 @@ import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -65,6 +70,9 @@ public class LinkPropertiesTest {
@Rule @Rule
public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
@Rule
public final PlatformCompatChangeRule compatChangeRule = new PlatformCompatChangeRule();
private static final InetAddress ADDRV4 = address("75.208.6.1"); private static final InetAddress ADDRV4 = address("75.208.6.1");
private static final InetAddress ADDRV6 = address("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); private static final InetAddress ADDRV6 = address("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
private static final InetAddress DNS1 = address("75.208.7.1"); private static final InetAddress DNS1 = address("75.208.7.1");
@@ -1254,6 +1262,7 @@ public class LinkPropertiesTest {
} }
@Test @IgnoreUpTo(Build.VERSION_CODES.Q) @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
@EnableCompatChanges({LinkProperties.EXCLUDED_ROUTES})
public void testRouteAddWithSameKey() throws Exception { public void testRouteAddWithSameKey() throws Exception {
LinkProperties lp = new LinkProperties(); LinkProperties lp = new LinkProperties();
lp.setInterfaceName("wlan0"); lp.setInterfaceName("wlan0");
@@ -1268,4 +1277,36 @@ public class LinkPropertiesTest {
lp.addRoute(new RouteInfo(v4, address("192.0.2.1"), "wlan0", RTN_THROW, 1460)); lp.addRoute(new RouteInfo(v4, address("192.0.2.1"), "wlan0", RTN_THROW, 1460));
assertEquals(2, lp.getRoutes().size()); assertEquals(2, lp.getRoutes().size());
} }
@Test @IgnoreUpTo(SC_V2)
@EnableCompatChanges({LinkProperties.EXCLUDED_ROUTES})
public void testExcludedRoutesEnabled() {
final LinkProperties lp = new LinkProperties();
assertEquals(0, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV4, 0), RTN_UNREACHABLE));
assertEquals(1, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 0), RTN_THROW));
assertEquals(2, lp.getRoutes().size());
lp.addRoute(new RouteInfo(GATEWAY1));
assertEquals(3, lp.getRoutes().size());
}
@Test @IgnoreUpTo(SC_V2)
@DisableCompatChanges({LinkProperties.EXCLUDED_ROUTES})
public void testExcludedRoutesDisabled() {
final LinkProperties lp = new LinkProperties();
assertEquals(0, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV4, 0), RTN_UNREACHABLE));
assertEquals(0, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 5), RTN_THROW));
assertEquals(0, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 2), RTN_UNICAST));
assertEquals(1, lp.getRoutes().size());
}
} }