Merge "Let LinkProperties#getRoutes() keeps returning all routes before T"

This commit is contained in:
Natasha Lee 2022-07-14 14:12:55 +00:00 • committed by Gerrit Code Review
commit 5006349209
2 changed files with 37 additions and 12 deletions

View file

@ -29,6 +29,8 @@ import android.os.Parcelable;
import android.text.TextUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.build.SdkLevel;
import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.LinkPropertiesUtils;
import java.net.Inet4Address;
@ -42,7 +44,6 @@ import java.util.Hashtable;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;
/**
* Describes the properties of a network link.
@ -759,20 +760,25 @@ public final class LinkProperties implements Parcelable {
* @return An unmodifiable {@link List} of {@link RouteInfo} for this link.
*/
public @NonNull List<RouteInfo> getRoutes() {
if (CompatChanges.isChangeEnabled(EXCLUDED_ROUTES)) {
// Before T, there's no throw routes because VpnService is not updatable, so no need to
// filter them out.
if (CompatChanges.isChangeEnabled(EXCLUDED_ROUTES) || !SdkLevel.isAtLeastT()) {
return Collections.unmodifiableList(mRoutes);
} else {
return Collections.unmodifiableList(getUnicastRoutes());
// Apps that added a throw route themselves (not obtaining LinkProperties from the
// system) will not see it in getRoutes on T+ if they do not have the compat change
// enabled (target SDK < T); but this is expected to be rare and typically only affect
// tests creating LinkProperties themselves (like CTS v12, which is only running on S).
return Collections.unmodifiableList(getNonThrowRoutes());
}
}
/**
* Returns all the {@link RouteInfo} of type {@link RouteInfo#RTN_UNICAST} set on this link.
* Returns all the {@link RouteInfo} that are not of type {@link RouteInfo#RTN_THROW} set on
* this link.
*/
private @NonNull List<RouteInfo> getUnicastRoutes() {
return mRoutes.stream()
.filter(route -> route.getType() == RouteInfo.RTN_UNICAST)
.collect(Collectors.toList());
private @NonNull List<RouteInfo> getNonThrowRoutes() {
return CollectionUtils.filter(mRoutes, route -> route.getType() != RouteInfo.RTN_THROW);
}
/**

View file

@ -1312,7 +1312,26 @@ public class LinkPropertiesTest {
assertEquals(3, lp.getRoutes().size());
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R)
@Test @IgnoreUpTo(Build.VERSION_CODES.R) @IgnoreAfter(Build.VERSION_CODES.S_V2)
@CtsNetTestCasesMaxTargetSdk31(reason = "Compat change cannot be overridden when targeting T+")
@DisableCompatChanges({LinkProperties.EXCLUDED_ROUTES})
public void testExcludedRoutesDisabled_S() {
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, 5), RTN_THROW));
// RTN_THROW routes are visible on S when added by the caller (but they are not added by
// the system). This is uncommon usage but was tested by CTSv12.
assertEquals(2, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 2), RTN_UNICAST));
assertEquals(3, lp.getRoutes().size());
}
@Test @IgnoreUpTo(Build.VERSION_CODES.S_V2)
@CtsNetTestCasesMaxTargetSdk31(reason = "Compat change cannot be overridden when targeting T+")
@DisableCompatChanges({LinkProperties.EXCLUDED_ROUTES})
public void testExcludedRoutesDisabled() {
@ -1320,12 +1339,12 @@ public class LinkPropertiesTest {
assertEquals(0, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV4, 0), RTN_UNREACHABLE));
assertEquals(0, lp.getRoutes().size());
assertEquals(1, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 5), RTN_THROW));
assertEquals(0, lp.getRoutes().size());
assertEquals(1, lp.getRoutes().size());
lp.addRoute(new RouteInfo(new IpPrefix(ADDRV6, 2), RTN_UNICAST));
assertEquals(1, lp.getRoutes().size());
assertEquals(2, lp.getRoutes().size());
}
}