Move helper methods from `ApfTestUtils.java` to `ApfTestHelpers.kt`
Test: atest NetworkStackTests
Change-Id: I8a818d78ff6c73d7e5080bba20f7d4254a71c734
diff --git a/tests/unit/src/android/net/apf/ApfGeneratorTest.kt b/tests/unit/src/android/net/apf/ApfGeneratorTest.kt
index 1364af5..60f0ca8 100644
--- a/tests/unit/src/android/net/apf/ApfGeneratorTest.kt
+++ b/tests/unit/src/android/net/apf/ApfGeneratorTest.kt
@@ -23,14 +23,14 @@
import android.net.apf.ApfCounterTracker.Counter.PASSED_ARP
import android.net.apf.ApfCounterTracker.Counter.PASSED_TRANSMIT_FAILURE
import android.net.apf.ApfCounterTracker.Counter.TOTAL_PACKETS
+import android.net.apf.ApfTestHelpers.Companion.DROP
+import android.net.apf.ApfTestHelpers.Companion.MIN_PKT_SIZE
+import android.net.apf.ApfTestHelpers.Companion.PASS
+import android.net.apf.ApfTestHelpers.Companion.assertDrop
+import android.net.apf.ApfTestHelpers.Companion.assertPass
+import android.net.apf.ApfTestHelpers.Companion.assertVerdict
import android.net.apf.ApfTestHelpers.Companion.decodeCountersIntoMap
import android.net.apf.ApfTestHelpers.Companion.verifyProgramRun
-import android.net.apf.ApfTestUtils.DROP
-import android.net.apf.ApfTestUtils.MIN_PKT_SIZE
-import android.net.apf.ApfTestUtils.PASS
-import android.net.apf.ApfTestUtils.assertDrop
-import android.net.apf.ApfTestUtils.assertPass
-import android.net.apf.ApfTestUtils.assertVerdict
import android.net.apf.BaseApfGenerator.APF_VERSION_2
import android.net.apf.BaseApfGenerator.APF_VERSION_3
import android.net.apf.BaseApfGenerator.APF_VERSION_6
diff --git a/tests/unit/src/android/net/apf/ApfStandaloneTest.kt b/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
index 531c4cc..e3cd092 100644
--- a/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
+++ b/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
@@ -78,9 +78,9 @@
val packetBadEtherType =
HexDump.hexStringToByteArray("ffffffffffff047bcb463fb588a201")
val dataRegion = ByteArray(Counter.totalSize()) { 0 }
- ApfTestUtils.assertVerdict(
+ ApfTestHelpers.assertVerdict(
APF_VERSION_4,
- ApfTestUtils.DROP,
+ ApfTestHelpers.DROP,
program,
packetBadEtherType,
dataRegion
@@ -135,13 +135,12 @@
//
// raw bytes:
// ffffffffffff047bcb463fb508004500011c00010000401179d100000000ffffffff004400430108393b010106000000000b000000000000000000000000000000000000000030343a37623a63623a34363a33663a62000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000638253633501033604c0a801013204c0a80164ff
-
val dhcpRequestPkt = HexDump.hexStringToByteArray(
"ffffffffffff047bcb463fb508004500011c00010000401179d100000000ffffffff004400430108393b010106000000000b000000000000000000000000000000000000000030343a37623a63623a34363a33663a62000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000638253633501033604c0a801013204c0a80164ff"
)
- ApfTestUtils.assertVerdict(
+ ApfTestHelpers.assertVerdict(
APF_VERSION_4,
- ApfTestUtils.DROP,
+ ApfTestHelpers.DROP,
program,
dhcpRequestPkt,
dataRegion
@@ -177,7 +176,7 @@
val rsPkt = HexDump.hexStringToByteArray(
"ffffffffffff047bcb463fb586dd6000000000083afffe8000000000000030b45e42ef3d36e5ff0200000000000000000000000000028500c81d00000000"
)
- ApfTestUtils.assertVerdict(APF_VERSION_4, ApfTestUtils.DROP, program, rsPkt, dataRegion)
+ ApfTestHelpers.assertVerdict(APF_VERSION_4, ApfTestHelpers.DROP, program, rsPkt, dataRegion)
assertEquals(mapOf<Counter, Long>(
Counter.TOTAL_PACKETS to 3,
Counter.DROPPED_RS to 1,
@@ -217,9 +216,9 @@
val pingRequestPkt = HexDump.hexStringToByteArray(
"ffffffffffff047bcb463fb508004500001c000100004001a52d644f6154080808080800f7ff00000000"
)
- ApfTestUtils.assertVerdict(
+ ApfTestHelpers.assertVerdict(
APF_VERSION_4,
- ApfTestUtils.DROP,
+ ApfTestHelpers.DROP,
program,
pingRequestPkt,
dataRegion
diff --git a/tests/unit/src/android/net/apf/ApfTest.java b/tests/unit/src/android/net/apf/ApfTest.java
index c80aaa9..7d1cfc4 100644
--- a/tests/unit/src/android/net/apf/ApfTest.java
+++ b/tests/unit/src/android/net/apf/ApfTest.java
@@ -18,6 +18,10 @@
import static android.net.apf.ApfCounterTracker.Counter.getCounterEnumFromOffset;
import static android.net.apf.ApfTestHelpers.consumeInstalledProgram;
+import static android.net.apf.ApfTestHelpers.DROP;
+import static android.net.apf.ApfTestHelpers.MIN_PKT_SIZE;
+import static android.net.apf.ApfTestHelpers.PASS;
+import static android.net.apf.ApfTestHelpers.assertProgramEquals;
import static android.net.apf.BaseApfGenerator.APF_VERSION_3;
import static android.net.apf.BaseApfGenerator.APF_VERSION_4;
import static android.net.apf.BaseApfGenerator.APF_VERSION_6;
@@ -29,10 +33,6 @@
import static android.net.apf.ApfJniUtils.compareBpfApf;
import static android.net.apf.ApfJniUtils.compileToBpf;
import static android.net.apf.ApfJniUtils.dropsAllPackets;
-import static android.net.apf.ApfTestUtils.DROP;
-import static android.net.apf.ApfTestUtils.MIN_PKT_SIZE;
-import static android.net.apf.ApfTestUtils.PASS;
-import static android.net.apf.ApfTestUtils.assertProgramEquals;
import static android.os.PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED;
import static android.os.PowerManager.ACTION_DEVICE_LIGHT_IDLE_MODE_CHANGED;
import static android.system.OsConstants.AF_UNIX;
@@ -300,58 +300,58 @@
}
private void assertPass(ApfV4Generator gen) throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertPass(mApfVersion, gen);
+ ApfTestHelpers.assertPass(mApfVersion, gen);
}
private void assertDrop(ApfV4Generator gen) throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertDrop(mApfVersion, gen);
+ ApfTestHelpers.assertDrop(mApfVersion, gen);
}
private void assertPass(byte[] program, byte[] packet) {
- ApfTestUtils.assertPass(mApfVersion, program, packet);
+ ApfTestHelpers.assertPass(mApfVersion, program, packet);
}
private void assertDrop(byte[] program, byte[] packet) {
- ApfTestUtils.assertDrop(mApfVersion, program, packet);
+ ApfTestHelpers.assertDrop(mApfVersion, program, packet);
}
private void assertPass(byte[] program, byte[] packet, int filterAge) {
- ApfTestUtils.assertPass(mApfVersion, program, packet, filterAge);
+ ApfTestHelpers.assertPass(mApfVersion, program, packet, filterAge);
}
private void assertDrop(byte[] program, byte[] packet, int filterAge) {
- ApfTestUtils.assertDrop(mApfVersion, program, packet, filterAge);
+ ApfTestHelpers.assertDrop(mApfVersion, program, packet, filterAge);
}
private void assertPass(ApfV4Generator gen, byte[] packet, int filterAge)
throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertPass(mApfVersion, gen, packet, filterAge);
+ ApfTestHelpers.assertPass(mApfVersion, gen, packet, filterAge);
}
private void assertDrop(ApfV4Generator gen, byte[] packet, int filterAge)
throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertDrop(mApfVersion, gen, packet, filterAge);
+ ApfTestHelpers.assertDrop(mApfVersion, gen, packet, filterAge);
}
private void assertDataMemoryContents(int expected, byte[] program, byte[] packet,
byte[] data, byte[] expectedData) throws Exception {
- ApfTestUtils.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
+ ApfTestHelpers.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
expectedData, false /* ignoreInterpreterVersion */);
}
private void assertDataMemoryContentsIgnoreVersion(int expected, byte[] program,
byte[] packet, byte[] data, byte[] expectedData) throws Exception {
- ApfTestUtils.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
+ ApfTestHelpers.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
expectedData, true /* ignoreInterpreterVersion */);
}
private void assertVerdict(String msg, int expected, byte[] program,
byte[] packet, int filterAge) {
- ApfTestUtils.assertVerdict(mApfVersion, msg, expected, program, packet, filterAge);
+ ApfTestHelpers.assertVerdict(mApfVersion, msg, expected, program, packet, filterAge);
}
private void assertVerdict(int expected, byte[] program, byte[] packet) {
- ApfTestUtils.assertVerdict(mApfVersion, expected, program, packet);
+ ApfTestHelpers.assertVerdict(mApfVersion, expected, program, packet);
}
/**
diff --git a/tests/unit/src/android/net/apf/ApfTestHelpers.kt b/tests/unit/src/android/net/apf/ApfTestHelpers.kt
index 30d5813..6a5688e 100644
--- a/tests/unit/src/android/net/apf/ApfTestHelpers.kt
+++ b/tests/unit/src/android/net/apf/ApfTestHelpers.kt
@@ -19,9 +19,6 @@
import android.net.apf.ApfCounterTracker.Counter.APF_PROGRAM_ID
import android.net.apf.ApfCounterTracker.Counter.APF_VERSION
import android.net.apf.ApfCounterTracker.Counter.TOTAL_PACKETS
-import android.net.apf.ApfTestUtils.DROP
-import android.net.apf.ApfTestUtils.PASS
-import android.net.apf.ApfTestUtils.assertVerdict
import android.net.apf.BaseApfGenerator.APF_VERSION_6
import android.net.ip.IpClient
import com.android.net.module.util.HexDump
@@ -34,6 +31,249 @@
class ApfTestHelpers private constructor() {
companion object {
const val TIMEOUT_MS: Long = 1000
+ const val PASS: Int = 1
+ const val DROP: Int = 0
+
+ // Interpreter will just accept packets without link layer headers, so pad fake packet to at
+ // least the minimum packet size.
+ const val MIN_PKT_SIZE: Int = 15
+ private fun label(code: Int): String {
+ return when (code) {
+ PASS -> "PASS"
+ DROP -> "DROP"
+ else -> "UNKNOWN"
+ }
+ }
+
+ private fun assertReturnCodesEqual(msg: String, expected: Int, got: Int) {
+ assertEquals(label(expected), label(got), msg)
+ }
+
+ private fun assertReturnCodesEqual(expected: Int, got: Int) {
+ assertEquals(label(expected), label(got))
+ }
+
+ private fun assertVerdict(
+ apfVersion: Int,
+ expected: Int,
+ program: ByteArray,
+ packet: ByteArray,
+ filterAge: Int
+ ) {
+ val msg = """Unexpected APF verdict. To debug:
+ apf_run
+ --program ${HexDump.toHexString(program)}
+ --packet ${HexDump.toHexString(packet)}
+ --age $filterAge
+ ${if (apfVersion > 4) " --v6" else ""}
+ --trace " + " | less\n
+ """
+ assertReturnCodesEqual(
+ msg,
+ expected,
+ ApfJniUtils.apfSimulate(apfVersion, program, packet, null, filterAge)
+ )
+ }
+
+ @Throws(BaseApfGenerator.IllegalInstructionException::class)
+ private fun assertVerdict(
+ apfVersion: Int,
+ expected: Int,
+ gen: ApfV4Generator,
+ packet: ByteArray,
+ filterAge: Int
+ ) {
+ assertVerdict(apfVersion, expected, gen.generate(), packet, null, filterAge)
+ }
+
+ private fun assertVerdict(
+ apfVersion: Int,
+ expected: Int,
+ program: ByteArray,
+ packet: ByteArray,
+ data: ByteArray?,
+ filterAge: Int
+ ) {
+ val msg = """Unexpected APF verdict. To debug:
+ apf_run
+ --program ${HexDump.toHexString(program)}
+ --packet ${HexDump.toHexString(packet)}
+ ${if (data != null) "--data ${HexDump.toHexString(data)}" else ""}
+ --age $filterAge
+ ${if (apfVersion > 4) "--v6" else ""}
+ --trace | less
+ """
+ assertReturnCodesEqual(
+ msg,
+ expected,
+ ApfJniUtils.apfSimulate(apfVersion, program, packet, data, filterAge)
+ )
+ }
+
+ /**
+ * Runs the APF program with customized data region and checks the return code.
+ */
+ fun assertVerdict(
+ apfVersion: Int,
+ expected: Int,
+ program: ByteArray,
+ packet: ByteArray,
+ data: ByteArray?
+ ) {
+ assertVerdict(apfVersion, expected, program, packet, data, filterAge = 0)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is equals to expected value. If not, the
+ * customized message is printed.
+ */
+ @JvmStatic
+ fun assertVerdict(
+ apfVersion: Int,
+ msg: String,
+ expected: Int,
+ program: ByteArray?,
+ packet: ByteArray?,
+ filterAge: Int
+ ) {
+ assertReturnCodesEqual(
+ msg,
+ expected,
+ ApfJniUtils.apfSimulate(apfVersion, program, packet, null, filterAge)
+ )
+ }
+
+ /**
+ * Runs the APF program and checks the return code is equals to expected value.
+ */
+ @JvmStatic
+ fun assertVerdict(apfVersion: Int, expected: Int, program: ByteArray, packet: ByteArray) {
+ assertVerdict(apfVersion, expected, program, packet, 0)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is PASS.
+ */
+ @JvmStatic
+ fun assertPass(apfVersion: Int, program: ByteArray, packet: ByteArray, filterAge: Int) {
+ assertVerdict(apfVersion, PASS, program, packet, filterAge)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is PASS.
+ */
+ @JvmStatic
+ fun assertPass(apfVersion: Int, program: ByteArray, packet: ByteArray) {
+ assertVerdict(apfVersion, PASS, program, packet)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is DROP.
+ */
+ @JvmStatic
+ fun assertDrop(apfVersion: Int, program: ByteArray, packet: ByteArray, filterAge: Int) {
+ assertVerdict(apfVersion, DROP, program, packet, filterAge)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is DROP.
+ */
+ @JvmStatic
+ fun assertDrop(apfVersion: Int, program: ByteArray, packet: ByteArray) {
+ assertVerdict(apfVersion, DROP, program, packet)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is PASS.
+ */
+ @Throws(BaseApfGenerator.IllegalInstructionException::class)
+ @JvmStatic
+ fun assertPass(apfVersion: Int, gen: ApfV4Generator, packet: ByteArray, filterAge: Int) {
+ assertVerdict(apfVersion, PASS, gen, packet, filterAge)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is DROP.
+ */
+ @Throws(BaseApfGenerator.IllegalInstructionException::class)
+ @JvmStatic
+ fun assertDrop(apfVersion: Int, gen: ApfV4Generator, packet: ByteArray, filterAge: Int) {
+ assertVerdict(apfVersion, DROP, gen, packet, filterAge)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is PASS.
+ */
+ @Throws(BaseApfGenerator.IllegalInstructionException::class)
+ @JvmStatic
+ fun assertPass(apfVersion: Int, gen: ApfV4Generator) {
+ assertVerdict(apfVersion, PASS, gen, ByteArray(MIN_PKT_SIZE), 0)
+ }
+
+ /**
+ * Runs the APF program and checks the return code is DROP.
+ */
+ @Throws(BaseApfGenerator.IllegalInstructionException::class)
+ @JvmStatic
+ fun assertDrop(apfVersion: Int, gen: ApfV4Generator) {
+ assertVerdict(apfVersion, DROP, gen, ByteArray(MIN_PKT_SIZE), 0)
+ }
+
+ /**
+ * Checks the generated APF program equals to the expected value.
+ */
+ @Throws(AssertionError::class)
+ @JvmStatic
+ fun assertProgramEquals(expected: ByteArray, program: ByteArray?) {
+ // assertArrayEquals() would only print one byte, making debugging difficult.
+ if (!expected.contentEquals(program)) {
+ throw AssertionError(
+ "\nexpected: " + HexDump.toHexString(expected) +
+ "\nactual: " + HexDump.toHexString(program)
+ )
+ }
+ }
+
+ /**
+ * Runs the APF program and checks the return code and data regions
+ * equals to expected value.
+ */
+ @Throws(BaseApfGenerator.IllegalInstructionException::class, Exception::class)
+ @JvmStatic
+ fun assertDataMemoryContents(
+ apfVersion: Int,
+ expected: Int,
+ program: ByteArray?,
+ packet: ByteArray?,
+ data: ByteArray,
+ expectedData: ByteArray,
+ ignoreInterpreterVersion: Boolean
+ ) {
+ assertReturnCodesEqual(
+ expected,
+ ApfJniUtils.apfSimulate(apfVersion, program, packet, data, 0)
+ )
+
+ if (ignoreInterpreterVersion) {
+ val apfVersionIdx = (Counter.totalSize() +
+ APF_VERSION.offset())
+ val apfProgramIdIdx = (Counter.totalSize() +
+ APF_PROGRAM_ID.offset())
+ for (i in 0..3) {
+ data[apfVersionIdx + i] = 0
+ data[apfProgramIdIdx + i] = 0
+ }
+ }
+ // assertArrayEquals() would only print one byte, making debugging difficult.
+ if (!expectedData.contentEquals(data)) {
+ throw Exception(
+ ("\nprogram: " + HexDump.toHexString(program) +
+ "\ndata memory: " + HexDump.toHexString(data) +
+ "\nexpected: " + HexDump.toHexString(expectedData))
+ )
+ }
+ }
+
fun verifyProgramRun(
version: Int,
program: ByteArray,
diff --git a/tests/unit/src/android/net/apf/ApfTestUtils.java b/tests/unit/src/android/net/apf/ApfTestUtils.java
deleted file mode 100644
index fc9fdef..0000000
--- a/tests/unit/src/android/net/apf/ApfTestUtils.java
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
- * Copyright (C) 2023 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 android.net.apf;
-
-import static android.net.apf.ApfJniUtils.apfSimulate;
-
-import static org.junit.Assert.assertEquals;
-
-import com.android.internal.util.HexDump;
-
-import java.util.Arrays;
-
-/**
- * The util class for calling the APF interpreter and check the return value
- */
-public class ApfTestUtils {
- public static final int TIMEOUT_MS = 500;
- public static final int PASS = 1;
- public static final int DROP = 0;
- // Interpreter will just accept packets without link layer headers, so pad fake packet to at
- // least the minimum packet size.
- public static final int MIN_PKT_SIZE = 15;
-
- private ApfTestUtils() {
- }
-
- private static String label(int code) {
- switch (code) {
- case PASS:
- return "PASS";
- case DROP:
- return "DROP";
- default:
- return "UNKNOWN";
- }
- }
-
- private static void assertReturnCodesEqual(String msg, int expected, int got) {
- assertEquals(msg, label(expected), label(got));
- }
-
- private static void assertReturnCodesEqual(int expected, int got) {
- assertEquals(label(expected), label(got));
- }
-
- private static void assertVerdict(int apfVersion, int expected, byte[] program, byte[] packet,
- int filterAge) {
- final String msg = "Unexpected APF verdict. To debug:\n"
- + " apf_run --program " + HexDump.toHexString(program)
- + " --packet " + HexDump.toHexString(packet)
- + " --age " + filterAge
- + (apfVersion > 4 ? " --v6" : "")
- + " --trace " + " | less\n ";
- assertReturnCodesEqual(msg, expected,
- apfSimulate(apfVersion, program, packet, null, filterAge));
- }
-
- /**
- * Runs the APF program and checks the return code is equals to expected value. If not, the
- * customized message is printed.
- */
- public static void assertVerdict(int apfVersion, String msg, int expected, byte[] program,
- byte[] packet, int filterAge) {
- assertReturnCodesEqual(msg, expected,
- apfSimulate(apfVersion, program, packet, null, filterAge));
- }
-
- /**
- * Runs the APF program and checks the return code is equals to expected value.
- */
- public static void assertVerdict(int apfVersion, int expected, byte[] program, byte[] packet) {
- assertVerdict(apfVersion, expected, program, packet, 0);
- }
-
- /**
- * Runs the APF program and checks the return code is PASS.
- */
- public static void assertPass(int apfVersion, byte[] program, byte[] packet, int filterAge) {
- assertVerdict(apfVersion, PASS, program, packet, filterAge);
- }
-
- /**
- * Runs the APF program and checks the return code is PASS.
- */
- public static void assertPass(int apfVersion, byte[] program, byte[] packet) {
- assertVerdict(apfVersion, PASS, program, packet);
- }
-
- /**
- * Runs the APF program and checks the return code is DROP.
- */
- public static void assertDrop(int apfVersion, byte[] program, byte[] packet, int filterAge) {
- assertVerdict(apfVersion, DROP, program, packet, filterAge);
- }
-
- /**
- * Runs the APF program and checks the return code is DROP.
- */
- public static void assertDrop(int apfVersion, byte[] program, byte[] packet) {
- assertVerdict(apfVersion, DROP, program, packet);
- }
-
- /**
- * Checks the generated APF program equals to the expected value.
- */
- public static void assertProgramEquals(byte[] expected, byte[] program) throws AssertionError {
- // assertArrayEquals() would only print one byte, making debugging difficult.
- if (!Arrays.equals(expected, program)) {
- throw new AssertionError("\nexpected: " + HexDump.toHexString(expected) + "\nactual: "
- + HexDump.toHexString(program));
- }
- }
-
- /**
- * Runs the APF program and checks the return code and data regions equals to expected value.
- */
- public static void assertDataMemoryContents(int apfVersion, int expected, byte[] program,
- byte[] packet, byte[] data, byte[] expectedData, boolean ignoreInterpreterVersion)
- throws ApfV4Generator.IllegalInstructionException, Exception {
- assertReturnCodesEqual(expected,
- apfSimulate(apfVersion, program, packet, data, 0 /* filterAge */));
-
- if (ignoreInterpreterVersion) {
- final int apfVersionIdx = ApfCounterTracker.Counter.totalSize()
- + ApfCounterTracker.Counter.APF_VERSION.offset();
- final int apfProgramIdIdx = ApfCounterTracker.Counter.totalSize()
- + ApfCounterTracker.Counter.APF_PROGRAM_ID.offset();
- for (int i = 0; i < 4; ++i) {
- data[apfVersionIdx + i] = 0;
- data[apfProgramIdIdx + i] = 0;
- }
- }
- // assertArrayEquals() would only print one byte, making debugging difficult.
- if (!Arrays.equals(expectedData, data)) {
- throw new Exception("\nprogram: " + HexDump.toHexString(program) + "\ndata memory: "
- + HexDump.toHexString(data) + "\nexpected: " + HexDump.toHexString(
- expectedData));
- }
- }
-
- /**
- * Runs the APF program with customized data region and checks the return code.
- */
- public static void assertVerdict(int apfVersion, int expected, byte[] program, byte[] packet,
- byte[] data) {
- assertVerdict(apfVersion, expected, program, packet, data, 0 /* filterAge */);
- }
-
- private static void assertVerdict(int apfVersion, int expected, ApfV4Generator gen,
- byte[] packet, int filterAge) throws ApfV4Generator.IllegalInstructionException {
- assertVerdict(apfVersion, expected, gen.generate(), packet, null, filterAge);
- }
-
- private static void assertVerdict(int apfVersion, int expected, byte[] program, byte[] packet,
- byte[] data, int filterAge) {
- final String msg = "Unexpected APF verdict. To debug:\n"
- + " apf_run --program " + HexDump.toHexString(program)
- + " --packet " + HexDump.toHexString(packet)
- + (data != null ? " --data " + HexDump.toHexString(data) : "")
- + " --age " + filterAge
- + (apfVersion > 4 ? " --v6" : "")
- + " --trace " + " | less\n ";
- assertReturnCodesEqual(msg, expected,
- apfSimulate(apfVersion, program, packet, data, filterAge));
- }
-
- /**
- * Runs the APF program and checks the return code is PASS.
- */
- public static void assertPass(int apfVersion, ApfV4Generator gen, byte[] packet, int filterAge)
- throws ApfV4Generator.IllegalInstructionException {
- assertVerdict(apfVersion, PASS, gen, packet, filterAge);
- }
-
- /**
- * Runs the APF program and checks the return code is DROP.
- */
- public static void assertDrop(int apfVersion, ApfV4Generator gen, byte[] packet, int filterAge)
- throws ApfV4Generator.IllegalInstructionException {
- assertVerdict(apfVersion, DROP, gen, packet, filterAge);
- }
-
- /**
- * Runs the APF program and checks the return code is PASS.
- */
- public static void assertPass(int apfVersion, ApfV4Generator gen)
- throws ApfV4Generator.IllegalInstructionException {
- assertVerdict(apfVersion, PASS, gen, new byte[MIN_PKT_SIZE], 0);
- }
-
- /**
- * Runs the APF program and checks the return code is DROP.
- */
- public static void assertDrop(int apfVersion, ApfV4Generator gen)
- throws ApfV4Generator.IllegalInstructionException {
- assertVerdict(apfVersion, DROP, gen, new byte[MIN_PKT_SIZE], 0);
- }
-}
diff --git a/tests/unit/src/android/net/apf/LegacyApfTest.java b/tests/unit/src/android/net/apf/LegacyApfTest.java
index 2504604..0727c6b 100644
--- a/tests/unit/src/android/net/apf/LegacyApfTest.java
+++ b/tests/unit/src/android/net/apf/LegacyApfTest.java
@@ -17,10 +17,10 @@
package android.net.apf;
import static android.net.apf.ApfJniUtils.dropsAllPackets;
-import static android.net.apf.ApfTestUtils.DROP;
-import static android.net.apf.ApfTestUtils.PASS;
-import static android.net.apf.ApfTestUtils.TIMEOUT_MS;
+import static android.net.apf.ApfTestHelpers.TIMEOUT_MS;
import static android.system.OsConstants.AF_UNIX;
+import static android.net.apf.ApfTestHelpers.DROP;
+import static android.net.apf.ApfTestHelpers.PASS;
import static android.system.OsConstants.ETH_P_ARP;
import static android.system.OsConstants.ETH_P_IP;
import static android.system.OsConstants.ETH_P_IPV6;
@@ -249,58 +249,58 @@
}
private void assertPass(ApfV4Generator gen) throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertPass(mApfVersion, gen);
+ ApfTestHelpers.assertPass(mApfVersion, gen);
}
private void assertDrop(ApfV4Generator gen) throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertDrop(mApfVersion, gen);
+ ApfTestHelpers.assertDrop(mApfVersion, gen);
}
private void assertPass(byte[] program, byte[] packet) {
- ApfTestUtils.assertPass(mApfVersion, program, packet);
+ ApfTestHelpers.assertPass(mApfVersion, program, packet);
}
private void assertDrop(byte[] program, byte[] packet) {
- ApfTestUtils.assertDrop(mApfVersion, program, packet);
+ ApfTestHelpers.assertDrop(mApfVersion, program, packet);
}
private void assertPass(byte[] program, byte[] packet, int filterAge) {
- ApfTestUtils.assertPass(mApfVersion, program, packet, filterAge);
+ ApfTestHelpers.assertPass(mApfVersion, program, packet, filterAge);
}
private void assertDrop(byte[] program, byte[] packet, int filterAge) {
- ApfTestUtils.assertDrop(mApfVersion, program, packet, filterAge);
+ ApfTestHelpers.assertDrop(mApfVersion, program, packet, filterAge);
}
private void assertPass(ApfV4Generator gen, byte[] packet, int filterAge)
throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertPass(mApfVersion, gen, packet, filterAge);
+ ApfTestHelpers.assertPass(mApfVersion, gen, packet, filterAge);
}
private void assertDrop(ApfV4Generator gen, byte[] packet, int filterAge)
throws ApfV4Generator.IllegalInstructionException {
- ApfTestUtils.assertDrop(mApfVersion, gen, packet, filterAge);
+ ApfTestHelpers.assertDrop(mApfVersion, gen, packet, filterAge);
}
private void assertDataMemoryContents(int expected, byte[] program, byte[] packet,
byte[] data, byte[] expectedData) throws Exception {
- ApfTestUtils.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
+ ApfTestHelpers.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
expectedData, false /* ignoreInterpreterVersion */);
}
private void assertDataMemoryContentsIgnoreVersion(int expected, byte[] program,
byte[] packet, byte[] data, byte[] expectedData) throws Exception {
- ApfTestUtils.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
+ ApfTestHelpers.assertDataMemoryContents(mApfVersion, expected, program, packet, data,
expectedData, true /* ignoreInterpreterVersion */);
}
private void assertVerdict(String msg, int expected, byte[] program,
byte[] packet, int filterAge) {
- ApfTestUtils.assertVerdict(mApfVersion, msg, expected, program, packet, filterAge);
+ ApfTestHelpers.assertVerdict(mApfVersion, msg, expected, program, packet, filterAge);
}
private void assertVerdict(int expected, byte[] program, byte[] packet) {
- ApfTestUtils.assertVerdict(mApfVersion, expected, program, packet);
+ ApfTestHelpers.assertVerdict(mApfVersion, expected, program, packet);
}
/**