From 633d9811d8fde66b8902c5e9e3cb95242ac1f104 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Thu, 28 Mar 2024 11:17:09 -0700 Subject: [PATCH 1/4] Move retrieving APF capabilities into helper function Retrieving the capabilities will be required for every test, so move it to a helper function. Also includes two more minor improvements: - Use runShellCommandOrThrow() instead of runShellCommand() so we correctly fail the test if the shell command returns an error. - Use truth for all assertions because it creates better error messages. See truth.dev for more details. Test: builds && TH Change-Id: I61b6263eac9bf70eae4e1841baae9e3fa8ca4d6d --- .../src/android/net/cts/ApfIntegrationTest.kt | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt index 3ecbdc63cc..b682b99e54 100644 --- a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt +++ b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt @@ -19,19 +19,19 @@ import android.content.pm.PackageManager.FEATURE_WIFI import android.net.ConnectivityManager import android.net.NetworkCapabilities import android.net.NetworkRequest +import android.net.apf.ApfCapabilities import android.os.Build import android.platform.test.annotations.AppModeFull import android.system.OsConstants import androidx.test.platform.app.InstrumentationRegistry import com.android.compatibility.common.util.PropertyUtil.isVendorApiLevelNewerThan -import com.android.compatibility.common.util.SystemUtil +import com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow import com.android.testutils.DevSdkIgnoreRule import com.android.testutils.DevSdkIgnoreRunner import com.android.testutils.NetworkStackModuleTest import com.android.testutils.RecorderCallback.CallbackEntry.LinkPropertiesChanged import com.android.testutils.TestableNetworkCallback import com.google.common.truth.Truth.assertThat -import kotlin.test.assertEquals import kotlin.test.assertNotNull import org.junit.After import org.junit.Assume.assumeTrue @@ -77,15 +77,20 @@ class ApfIntegrationTest { } } + fun getApfCapabilities(): ApfCapabilities { + val caps = runShellCommandOrThrow("cmd network_stack apf $ifname capabilities").trim() + val (version, maxLen, packetFormat) = caps.split(",").map { it.toInt() } + return ApfCapabilities(version, maxLen, packetFormat) + } + @Test fun testGetApfCapabilities() { - val caps = SystemUtil.runShellCommand("cmd network_stack apf $ifname capabilities").trim() - val (version, maxLen, packetFormat) = caps.split(",").map { it.toInt() } - assertEquals(4, version) - assertThat(maxLen).isAtLeast(1024) + val caps = getApfCapabilities() + assertThat(caps.apfVersionSupported).isEqualTo(4) + assertThat(caps.maximumApfProgramSize).isAtLeast(1024) if (isVendorApiLevelNewerThan(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)) { - assertThat(maxLen).isAtLeast(2000) + assertThat(caps.maximumApfProgramSize).isAtLeast(2000) } - assertEquals(OsConstants.ARPHRD_ETHER, packetFormat) + assertThat(caps.apfPacketFormat).isEqualTo(OsConstants.ARPHRD_ETHER) } } From e9fb2b2fb513af99ac68a644f317d910a3433473 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Thu, 28 Mar 2024 15:41:42 -0700 Subject: [PATCH 2/4] Enable new ApfFilter experiment in CTS test APF adb commands are not implemented in LegacyApfFilter. Enable the experiment flag for all tests. Test: atest ApfIntegrationTest Change-Id: I468ef54498a6f780dff22d25119732b42b404199 --- .../src/android/net/cts/ApfIntegrationTest.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt index b682b99e54..8c1501ed6c 100644 --- a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt +++ b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt @@ -15,6 +15,7 @@ */ package android.net.cts +import android.Manifest.permission.WRITE_DEVICE_CONFIG import android.content.pm.PackageManager.FEATURE_WIFI import android.net.ConnectivityManager import android.net.NetworkCapabilities @@ -22,6 +23,8 @@ import android.net.NetworkRequest import android.net.apf.ApfCapabilities import android.os.Build import android.platform.test.annotations.AppModeFull +import android.provider.DeviceConfig +import android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY import android.system.OsConstants import androidx.test.platform.app.InstrumentationRegistry import com.android.compatibility.common.util.PropertyUtil.isVendorApiLevelNewerThan @@ -31,21 +34,43 @@ import com.android.testutils.DevSdkIgnoreRunner import com.android.testutils.NetworkStackModuleTest import com.android.testutils.RecorderCallback.CallbackEntry.LinkPropertiesChanged import com.android.testutils.TestableNetworkCallback +import com.android.testutils.runAsShell import com.google.common.truth.Truth.assertThat import kotlin.test.assertNotNull import org.junit.After import org.junit.Assume.assumeTrue import org.junit.Before +import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith private const val TIMEOUT_MS = 2000L +private const val APF_NEW_RA_FILTER_VERSION = "apf_new_ra_filter_version" @AppModeFull(reason = "CHANGE_NETWORK_STATE permission can't be granted to instant apps") @RunWith(DevSdkIgnoreRunner::class) @NetworkStackModuleTest @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU) class ApfIntegrationTest { + companion object { + @BeforeClass + @JvmStatic + fun setupOnce() { + // TODO: check that there is no active wifi network. Otherwise, ApfFilter has already been + // created. + // APF adb cmds are only implemented in ApfFilter.java. Enable experiment to prevent + // LegacyApfFilter.java from being used. + runAsShell(WRITE_DEVICE_CONFIG) { + DeviceConfig.setProperty( + NAMESPACE_CONNECTIVITY, + APF_NEW_RA_FILTER_VERSION, + "1", // value => force enabled + false // makeDefault + ) + } + } + } + private val context by lazy { InstrumentationRegistry.getInstrumentation().context } private val cm by lazy { context.getSystemService(ConnectivityManager::class.java)!! } private val pm by lazy { context.packageManager } From ee085cc201283d42574e1d519d64da3453c6e2e6 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Fri, 29 Mar 2024 15:10:49 -0700 Subject: [PATCH 3/4] Pause APF filter before starting test adb shell cmd apf install requires APF to be paused beforehand. Resume in tearDown. Test: atest ApfIntegrationTest Change-Id: I1b1345a629dc03446ae3b74ab06f40a06085cd28 --- tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt index 8c1501ed6c..0cdd5a860e 100644 --- a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt +++ b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt @@ -93,6 +93,7 @@ class ApfIntegrationTest { ifname = assertNotNull(it.lp.interfaceName) true } + runShellCommandOrThrow("cmd network_stack apf $ifname pause") } @After @@ -100,6 +101,7 @@ class ApfIntegrationTest { if (::networkCallback.isInitialized) { cm.unregisterNetworkCallback(networkCallback) } + runShellCommandOrThrow("cmd network_stack apf $ifname resume") } fun getApfCapabilities(): ApfCapabilities { From efdfaf2c086d508e13a4cf56d70afa68fce55586 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Thu, 28 Mar 2024 10:46:19 -0700 Subject: [PATCH 4/4] Grant prohr temporary ownership of ApfIntegrationTests In order to make fast progress on developing some initial APF integration tests, grant prohr@ temporary ownership. Changes will be reviewed with lorenzo@ out-of-band before release. Test: n/a Change-Id: Ic4b586aeb95902717d8e0e57f063cd7302f9b70c --- tests/cts/OWNERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cts/OWNERS b/tests/cts/OWNERS index cb4ca59431..acf506deb3 100644 --- a/tests/cts/OWNERS +++ b/tests/cts/OWNERS @@ -9,4 +9,6 @@ per-file **IpSec* = benedictwong@google.com, nharold@google.com # For incremental changes on EthernetManagerTest to increase coverage for existing behavior and for # testing bug fixes. per-file net/src/android/net/cts/EthernetManagerTest.kt = prohr@google.com #{LAST_RESORT_SUGGESTION} +# Temporary ownership to develop APF CTS tests. +per-file net/src/android/net/cts/ApfIntegrationTest.kt = prohr@google.com #{LAST_RESORT_SUGGESTION}