Add a NetworkStackCoverageTests target.
This target will include all tests that are necessary to calculate
code coverage of the network stack. It is currently the union of
NetworkStackTests and NetworkStackIntegrationTests
Also apply jarjar rules on top of NetworkStackIntegrationTests and
NetworkStackTests, to make sure that the test classes (and not the
framework classes) are used during the test. This requires the tests to
stop using external (hidden) references to classes in
com.android.internal, as the references would be broken when applying
jarjar.
Test: atest NetworkStackIntegrationTests NetworkStackTests \
NetworkStackCoverageTests
Change-Id: Ib945cb9faa960862593a6ac2ac26058948e777a4
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 9209dc4..bdb2d74 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -38,8 +38,10 @@
"libstaticjvmtiagent",
"libnetworkstackutilsjni",
],
+ jarjar_rules: ":NetworkStackJarJarRules",
}
+// Tests for NetworkStackNext.
android_test {
name: "NetworkStackNextTests",
srcs: [], // TODO: tests that only apply to the current, non-stable API can be added here
@@ -48,6 +50,17 @@
static_libs: ["NetworkStackApiCurrentLib"],
}
+// Library containing the unit tests. This is used by the coverage test target to pull in the
+// unit test code. It is not currently used by the tests themselves because all the build
+// configuration needed by the tests is in the NetworkStackTestsDefaults rule.
+android_library {
+ name: "NetworkStackTestsLib",
+ min_sdk_version: "29",
+ defaults: ["NetworkStackTestsDefaults"],
+ static_libs: ["NetworkStackApiStableLib"],
+ visibility: ["//packages/modules/NetworkStack/tests/integration"]
+}
+
android_test {
name: "NetworkStackTests",
min_sdk_version: "29",
diff --git a/tests/unit/AndroidManifest.xml b/tests/unit/AndroidManifest.xml
index c7ea774..f45cfc7 100644
--- a/tests/unit/AndroidManifest.xml
+++ b/tests/unit/AndroidManifest.xml
@@ -17,32 +17,26 @@
package="com.android.server.networkstack.tests">
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="29" />
- <uses-permission android:name="android.permission.READ_LOGS" />
+ <!-- DO NOT add privapp permissions here: they are inherited by
+ NetworkStackCoverageTests, which is not signed by the platform key,
+ and on Q rebooting the device would cause a bootloop because of
+ the missing priv-app whitelisting. -->
+ <!-- TODO: many of the below permissions seem to be unused, remove them. -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
- <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.MANAGE_APP_TOKENS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
- <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
- <uses-permission android:name="android.permission.REAL_GET_TASKS" />
<uses-permission android:name="android.permission.GET_DETAILED_TASKS" />
<uses-permission android:name="android.permission.MANAGE_NETWORK_POLICY" />
- <uses-permission android:name="android.permission.READ_NETWORK_USAGE_HISTORY" />
- <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.MANAGE_USERS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
- <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
- <uses-permission android:name="android.permission.PACKET_KEEPALIVE_OFFLOAD" />
<uses-permission android:name="android.permission.GET_INTENT_SENDER_INTENT" />
<uses-permission android:name="android.permission.MANAGE_ACTIVITY_STACKS" />
- <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.NETWORK_STACK" />
<application android:debuggable="true">
@@ -52,4 +46,4 @@
android:targetPackage="com.android.server.networkstack.tests"
android:label="Networking service tests">
</instrumentation>
-</manifest>
\ No newline at end of file
+</manifest>
diff --git a/tests/unit/src/android/net/apf/ApfTest.java b/tests/unit/src/android/net/apf/ApfTest.java
index 006ad19..f018fde 100644
--- a/tests/unit/src/android/net/apf/ApfTest.java
+++ b/tests/unit/src/android/net/apf/ApfTest.java
@@ -26,7 +26,6 @@
import static android.system.OsConstants.IPPROTO_UDP;
import static android.system.OsConstants.SOCK_STREAM;
-import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.server.util.NetworkStackConstants.ICMPV6_ECHO_REQUEST_TYPE;
import static com.android.server.util.NetworkStackConstants.IPV6_ADDR_LEN;
@@ -39,6 +38,7 @@
import static org.mockito.Mockito.verify;
import android.content.Context;
+import android.net.InetAddresses;
import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
@@ -51,6 +51,7 @@
import android.net.ip.IpClient.IpClientCallbacksWrapper;
import android.net.metrics.IpConnectivityLog;
import android.net.metrics.RaEvent;
+import android.net.shared.Inet4AddressUtils;
import android.net.util.InterfaceParams;
import android.net.util.SharedLog;
import android.os.ConditionVariable;
@@ -85,6 +86,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
@@ -2303,7 +2305,8 @@
}
public void assertEqualsIp(String expected, int got) throws Exception {
- int want = bytesToBEInt(InetAddress.getByName(expected).getAddress());
+ int want = Inet4AddressUtils.inet4AddressToIntHTH(
+ (Inet4Address) InetAddresses.parseNumericAddress(expected));
assertEquals(want, got);
}
}
diff --git a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
index ae234b1..6bf9236 100644
--- a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
+++ b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
@@ -61,6 +61,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static java.util.stream.Collectors.toList;
+
import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -104,7 +106,6 @@
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
-import com.android.internal.util.CollectionUtils;
import com.android.networkstack.R;
import com.android.networkstack.apishim.ShimUtils;
import com.android.networkstack.metrics.DataStallDetectionStats;
@@ -257,9 +258,8 @@
return null;
}
- DnsEntry answer = CollectionUtils.find(mAnswers, e -> e.matches(hostname, type));
- if (answer != null) return answer.mAddresses;
- else return null;
+ return mAnswers.stream().filter(e -> e.matches(hostname, type))
+ .map(answer -> answer.mAddresses).findFirst().orElse(null);
}
/** Sets the answer for a given name and type. */
@@ -274,8 +274,8 @@
private List<InetAddress> generateAnswer(String[] answer) {
if (answer == null) return new ArrayList<>();
- return CollectionUtils.map(Arrays.asList(answer),
- addr -> InetAddress.parseNumericAddress(addr));
+ return Arrays.stream(answer).map(addr -> InetAddress.parseNumericAddress(addr))
+ .collect(toList());
}
/** Simulates a getAllByName call for the specified name on the specified mock network. */