From a341fbcdc32b1c2636b83b844a45181fe4b2160f Mon Sep 17 00:00:00 2001 From: Benedict Wong Date: Fri, 9 Nov 2018 14:45:34 -0800 Subject: 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 --- api/test-current.txt | 1 + core/java/android/app/SystemServiceRegistry.java | 26 +++++++++++++++++++++ core/java/android/content/Context.java | 10 ++++++++ core/java/android/net/IConnectivityManager.aidl | 2 ++ .../com/android/server/ConnectivityService.java | 27 ++++++++++++++++++++++ 5 files changed, 66 insertions(+) diff --git a/api/test-current.txt b/api/test-current.txt index d43b1c4ec275..6372197b86b5 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -234,6 +234,7 @@ package android.content { method public android.os.UserHandle getUser(); method public int getUserId(); method public void setAutofillCompatibilityEnabled(boolean); + field public static final String TEST_NETWORK_SERVICE = "test_network"; } } diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index e92efde236c4..a3ee8491e6d1 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -84,11 +84,13 @@ import android.net.IEthernetManager; import android.net.IIpMemoryStore; import android.net.IIpSecService; import android.net.INetworkPolicyManager; +import android.net.ITestNetworkManager; import android.net.IpMemoryStore; import android.net.IpSecManager; import android.net.NetworkPolicyManager; import android.net.NetworkScoreManager; import android.net.NetworkWatchlistManager; +import android.net.TestNetworkManager; import android.net.lowpan.ILowpanManager; import android.net.lowpan.LowpanManager; import android.net.nsd.INsdManager; @@ -126,6 +128,7 @@ import android.os.IUserManager; import android.os.IncidentManager; import android.os.PowerManager; import android.os.RecoverySystem; +import android.os.RemoteException; import android.os.ServiceManager; import android.os.ServiceManager.ServiceNotFoundException; import android.os.SystemUpdateManager; @@ -315,6 +318,29 @@ final class SystemServiceRegistry { return new IpSecManager(ctx, service); }}); + registerService( + Context.TEST_NETWORK_SERVICE, + TestNetworkManager.class, + new StaticApplicationContextServiceFetcher() { + @Override + public TestNetworkManager createService(Context context) + throws ServiceNotFoundException { + IBinder csBinder = + ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); + IConnectivityManager csMgr = + IConnectivityManager.Stub.asInterface(csBinder); + + final IBinder tnBinder; + try { + tnBinder = csMgr.startOrGetTestNetworkService(); + } catch (RemoteException e) { + throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE); + } + ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder); + return new TestNetworkManager(context, tnMgr); + } + }); + registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, new StaticServiceFetcher() { @Override diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 136657ff555e..5a75d5df554f 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -3016,6 +3016,7 @@ public abstract class Context { CONNECTIVITY_SERVICE, //@hide: IP_MEMORY_STORE_SERVICE, IPSEC_SERVICE, + TEST_NETWORK_SERVICE, //@hide: UPDATE_LOCK_SERVICE, //@hide: NETWORKMANAGEMENT_SERVICE, NETWORK_STATS_SERVICE, @@ -3537,6 +3538,15 @@ public abstract class Context { */ public static final String IPSEC_SERVICE = "ipsec"; + /** + * Use with {@link #getSystemService(String)} to retrieve a {@link + * android.net.TestNetworkManager} for building TUNs and limited-use Networks + * + * @see #getSystemService(String) + * @hide + */ + @TestApi public static final String TEST_NETWORK_SERVICE = "test_network"; + /** * Use with {@link #getSystemService(String)} to retrieve a {@link * android.os.IUpdateLock} for managing runtime sequences that diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl index 2df4e75c49a4..ddcf13fcd4b5 100644 --- a/core/java/android/net/IConnectivityManager.aidl +++ b/core/java/android/net/IConnectivityManager.aidl @@ -219,4 +219,6 @@ interface IConnectivityManager 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 dbfc3270787a..282db751c4ce 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -298,6 +298,15 @@ public class ConnectivityService extends IConnectivityManager.Stub 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 sMagicDecoderRing = MessageUtils.findMessageNames( @@ -6910,4 +6919,22 @@ public class ConnectivityService extends IConnectivityManager.Stub return vpn != null && vpn.getLockdown(); } } + + /** + * Returns a IBinder to a TestNetworkService. Will be lazily created as needed. + * + *

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; + } + } } -- cgit v1.2.3-59-g8ed1b