From f0c8daa0eb435869a37b84a3fb4c47b99995e58b Mon Sep 17 00:00:00 2001 From: Taras Antoshchuk Date: Wed, 13 Apr 2022 01:23:05 +0200 Subject: [PATCH] Add tests for LinkProperties#EXCLUDED_ROUTES Add CTS tests for LinkProperties#EXCLUDED_ROUTES gated change. These tests check LinkProperties#getRoutes and LinkProperties#getAllRoutes behavior depending on whether the gated change is enabled. Bug: 186082280 Test: atest HostsideLinkPropertiesGatingTests Change-Id: Id353714331a19d09022d20f186c8f71343686da7 --- tests/cts/hostside/Android.bp | 3 + tests/cts/hostside/app3/Android.bp | 50 ++++++++++++ tests/cts/hostside/app3/AndroidManifest.xml | 27 +++++++ .../app3/ExcludedRoutesGatingTest.java | 80 +++++++++++++++++++ .../HostsideLinkPropertiesGatingTests.java | 71 ++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 tests/cts/hostside/app3/Android.bp create mode 100644 tests/cts/hostside/app3/AndroidManifest.xml create mode 100644 tests/cts/hostside/app3/src/com/android/cts/net/hostside/app3/ExcludedRoutesGatingTest.java create mode 100644 tests/cts/hostside/src/com/android/cts/net/HostsideLinkPropertiesGatingTests.java diff --git a/tests/cts/hostside/Android.bp b/tests/cts/hostside/Android.bp index 47ea53e828..ac84e570cd 100644 --- a/tests/cts/hostside/Android.bp +++ b/tests/cts/hostside/Android.bp @@ -26,6 +26,7 @@ java_test_host { "tradefed", ], static_libs: [ + "CompatChangeGatingTestBase", "modules-utils-build-testing", ], // Tag this module as a cts test artifact @@ -37,6 +38,8 @@ java_test_host { data: [ ":CtsHostsideNetworkTestsApp", ":CtsHostsideNetworkTestsApp2", + ":CtsHostsideNetworkTestsApp3", + ":CtsHostsideNetworkTestsApp3PreT", ":CtsHostsideNetworkTestsAppNext", ], per_testcase_directory: true, diff --git a/tests/cts/hostside/app3/Android.bp b/tests/cts/hostside/app3/Android.bp new file mode 100644 index 0000000000..69667ce8ec --- /dev/null +++ b/tests/cts/hostside/app3/Android.bp @@ -0,0 +1,50 @@ +// +// Copyright (C) 2022 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +java_defaults { + name: "CtsHostsideNetworkTestsApp3Defaults", + srcs: ["src/**/*.java"], + libs: [ + "junit", + ], + static_libs: [ + "ctstestrunner-axt", + "truth-prebuilt", + ], + + // Tag this module as a cts test artifact + test_suites: [ + "cts", + "general-tests", + ], +} + +android_test_helper_app { + name: "CtsHostsideNetworkTestsApp3", + defaults: [ + "cts_support_defaults", + "CtsHostsideNetworkTestsApp3Defaults", + ], +} + +android_test_helper_app { + name: "CtsHostsideNetworkTestsApp3PreT", + target_sdk_version: "31", + defaults: [ + "cts_support_defaults", + "CtsHostsideNetworkTestsApp3Defaults", + ], +} diff --git a/tests/cts/hostside/app3/AndroidManifest.xml b/tests/cts/hostside/app3/AndroidManifest.xml new file mode 100644 index 0000000000..eabcacbddf --- /dev/null +++ b/tests/cts/hostside/app3/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + diff --git a/tests/cts/hostside/app3/src/com/android/cts/net/hostside/app3/ExcludedRoutesGatingTest.java b/tests/cts/hostside/app3/src/com/android/cts/net/hostside/app3/ExcludedRoutesGatingTest.java new file mode 100644 index 0000000000..a1a8209db2 --- /dev/null +++ b/tests/cts/hostside/app3/src/com/android/cts/net/hostside/app3/ExcludedRoutesGatingTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.cts.net.hostside.app3; + +import static org.junit.Assert.assertEquals; + +import android.Manifest; +import android.net.IpPrefix; +import android.net.LinkProperties; +import android.net.RouteInfo; + +import androidx.test.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Tests to verify {@link LinkProperties#getRoutes} behavior, depending on + * {@LinkProperties#EXCLUDED_ROUTES} change state. + */ +@RunWith(AndroidJUnit4.class) +public class ExcludedRoutesGatingTest { + @Before + public void setUp() { + InstrumentationRegistry.getInstrumentation().getUiAutomation() + .adoptShellPermissionIdentity(Manifest.permission.LOG_COMPAT_CHANGE, + Manifest.permission.READ_COMPAT_CHANGE_CONFIG); + } + + @After + public void tearDown() { + InstrumentationRegistry.getInstrumentation().getUiAutomation() + .dropShellPermissionIdentity(); + } + + @Test + public void testExcludedRoutesChangeEnabled() { + final LinkProperties lp = makeLinkPropertiesWithExcludedRoutes(); + + // Excluded routes change is enabled: non-RTN_UNICAST routes are visible. + assertEquals(2, lp.getRoutes().size()); + assertEquals(2, lp.getAllRoutes().size()); + } + + @Test + public void testExcludedRoutesChangeDisabled() { + final LinkProperties lp = makeLinkPropertiesWithExcludedRoutes(); + + // Excluded routes change is disabled: non-RTN_UNICAST routes are filtered out. + assertEquals(0, lp.getRoutes().size()); + assertEquals(0, lp.getAllRoutes().size()); + } + + private LinkProperties makeLinkPropertiesWithExcludedRoutes() { + final LinkProperties lp = new LinkProperties(); + + lp.addRoute(new RouteInfo(new IpPrefix("10.0.0.0/8"), null, null, RouteInfo.RTN_THROW)); + lp.addRoute(new RouteInfo(new IpPrefix("2001:db8::/64"), null, null, + RouteInfo.RTN_UNREACHABLE)); + + return lp; + } +} diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideLinkPropertiesGatingTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideLinkPropertiesGatingTests.java new file mode 100644 index 0000000000..b65fb6b287 --- /dev/null +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideLinkPropertiesGatingTests.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.cts.net; + +import android.compat.cts.CompatChangeGatingTestCase; + +import java.util.Set; + +/** + * Tests for the {@link android.net.LinkProperties#EXCLUDED_ROUTES} compatibility change. + */ +public class HostsideLinkPropertiesGatingTests extends CompatChangeGatingTestCase { + private static final String TEST_APK = "CtsHostsideNetworkTestsApp3.apk"; + private static final String TEST_APK_PRE_T = "CtsHostsideNetworkTestsApp3PreT.apk"; + private static final String TEST_PKG = "com.android.cts.net.hostside.app3"; + private static final String TEST_CLASS = ".ExcludedRoutesGatingTest"; + + private static final long EXCLUDED_ROUTES_CHANGE_ID = 186082280; + + protected void tearDown() throws Exception { + uninstallPackage(TEST_PKG, true); + } + + public void testExcludedRoutesChangeEnabled() throws Exception { + installPackage(TEST_APK, true); + runDeviceCompatTest("testExcludedRoutesChangeEnabled"); + } + + public void testExcludedRoutesChangeDisabledPreT() throws Exception { + installPackage(TEST_APK_PRE_T, true); + runDeviceCompatTest("testExcludedRoutesChangeDisabled"); + } + + public void testExcludedRoutesChangeDisabledByOverride() throws Exception { + installPackage(TEST_APK, true); + runDeviceCompatTestWithChangeDisabled("testExcludedRoutesChangeDisabled"); + } + + public void testExcludedRoutesChangeEnabledByOverridePreT() throws Exception { + installPackage(TEST_APK_PRE_T, true); + runDeviceCompatTestWithChangeEnabled("testExcludedRoutesChangeEnabled"); + } + + private void runDeviceCompatTest(String methodName) throws Exception { + runDeviceCompatTest(TEST_PKG, TEST_CLASS, methodName, Set.of(), Set.of()); + } + + private void runDeviceCompatTestWithChangeEnabled(String methodName) throws Exception { + runDeviceCompatTest(TEST_PKG, TEST_CLASS, methodName, Set.of(EXCLUDED_ROUTES_CHANGE_ID), + Set.of()); + } + + private void runDeviceCompatTestWithChangeDisabled(String methodName) throws Exception { + runDeviceCompatTest(TEST_PKG, TEST_CLASS, methodName, Set.of(), + Set.of(EXCLUDED_ROUTES_CHANGE_ID)); + } +}