Integrate testNetworkService and Manager with Connectivity stack
This change adds TestAPIs for tests to retrive an instance of
ConnectivityManager, allowing it to build test TUN interfaces, as well
as test networks.
This also integrates the TestNetwork types with ConnectivityManager,
creating virtual networks if the network agent is a test agent.
Bug: 72950854
Test: Compiles, CTS tests using this passing correctly
Change-Id: Ic1a04aa66014d1c66a74e65dbace3218437403ae
Merged-In: I741ef9cdf4bd4125d9129af3a030edf32f438e4f
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index 2df4e75..ddcf13f 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -219,4 +219,6 @@
void registerTetheringEventCallback(ITetheringEventCallback callback, String callerPkg);
void unregisterTetheringEventCallback(ITetheringEventCallback callback, String callerPkg);
+
+ IBinder startOrGetTestNetworkService();
}
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index dbfc327..282db75 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -298,6 +298,15 @@
private INetworkPolicyManager mPolicyManager;
private NetworkPolicyManagerInternal mPolicyManagerInternal;
+ /**
+ * TestNetworkService (lazily) created upon first usage. Locked to prevent creation of multiple
+ * instances.
+ */
+ @GuardedBy("mTNSLock")
+ private TestNetworkService mTNS;
+
+ private final Object mTNSLock = new Object();
+
private String mCurrentTcpBufferSizes;
private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
@@ -6910,4 +6919,22 @@
return vpn != null && vpn.getLockdown();
}
}
+
+ /**
+ * Returns a IBinder to a TestNetworkService. Will be lazily created as needed.
+ *
+ * <p>The TestNetworkService must be run in the system server due to TUN creation.
+ */
+ @Override
+ public IBinder startOrGetTestNetworkService() {
+ synchronized (mTNSLock) {
+ TestNetworkService.enforceTestNetworkPermissions(mContext);
+
+ if (mTNS == null) {
+ mTNS = new TestNetworkService(mContext, mNMS);
+ }
+
+ return mTNS;
+ }
+ }
}