diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2020-05-01 04:47:17 +0000 |
---|---|---|
committer | Remi NGUYEN VAN <reminv@google.com> | 2020-05-13 02:29:56 +0000 |
commit | 8d081d6decd669c3e618f6810024500a66ad2e04 (patch) | |
tree | 5e896938a08e9afa3e05f3928f3992ed370df464 /src/com/android/server/NetworkStackService.java | |
parent | 5c5a6898473e8b446b7e9b8c0e2f85cf69c480ee (diff) |
Add test for NetworkStackService dumpsys version
The test verifies that each method on INetworkStackConnector updates the
version received from the remote, and that the output of dumpsys version
matches the expected template.
This logic will be changed in R to include interface hashes, so a test
is important to verify that Q behavior remains the same to avoid
breaking Q conformance tests.
Test: atest NetworkStackTests:NetworkStackServiceTest
Bug: 137328719
Original-Change: https://android-review.googlesource.com/1293754
Merged-In: Icaab91d2cb4c62930f969612545e369f09f1b0c7
Change-Id: Icaab91d2cb4c62930f969612545e369f09f1b0c7
Diffstat (limited to 'src/com/android/server/NetworkStackService.java')
-rw-r--r-- | src/com/android/server/NetworkStackService.java | 93 |
1 files changed, 70 insertions, 23 deletions
diff --git a/src/com/android/server/NetworkStackService.java b/src/com/android/server/NetworkStackService.java index 5ab2744..3a702e5 100644 --- a/src/com/android/server/NetworkStackService.java +++ b/src/com/android/server/NetworkStackService.java @@ -46,6 +46,7 @@ import android.net.util.SharedLog; import android.os.Build; import android.os.HandlerThread; import android.os.IBinder; +import android.os.Looper; import android.os.RemoteException; import android.util.ArraySet; @@ -86,8 +87,7 @@ public class NetworkStackService extends Service { */ public static synchronized IBinder makeConnector(Context context) { if (sConnector == null) { - sConnector = new NetworkStackConnector( - context, new NetworkStackConnector.PermissionChecker()); + sConnector = new NetworkStackConnector(context); } return sConnector; } @@ -115,6 +115,58 @@ public class NetworkStackService extends Service { } /** + * Permission checking dependency of the connector, useful for testing. + */ + public static class PermissionChecker { + /** + * @see PermissionUtil#enforceNetworkStackCallingPermission() + */ + public void enforceNetworkStackCallingPermission() { + PermissionUtil.enforceNetworkStackCallingPermission(); + } + } + + /** + * Dependencies of {@link NetworkStackConnector}, useful for testing. + */ + public static class Dependencies { + /** @see IpMemoryStoreService */ + @NonNull + public IpMemoryStoreService makeIpMemoryStoreService(@NonNull Context context) { + return new IpMemoryStoreService(context); + } + + /** @see NetworkStackNotifier */ + @NonNull + public NetworkStackNotifier makeNotifier(@NonNull Context context, @NonNull Looper looper) { + return new NetworkStackNotifier(context, looper); + } + + /** @see DhcpServer */ + @NonNull + public DhcpServer makeDhcpServer(@NonNull Context context, @NonNull String ifName, + @NonNull DhcpServingParams params, @NonNull SharedLog log) { + return new DhcpServer(context, ifName, params, log); + } + + /** @see NetworkMonitor */ + @NonNull + public NetworkMonitor makeNetworkMonitor(@NonNull Context context, + @NonNull INetworkMonitorCallbacks cb, @NonNull Network network, + @NonNull SharedLog log, @NonNull NetworkStackServiceManager nsServiceManager) { + return new NetworkMonitor(context, cb, network, log, nsServiceManager); + } + + /** @see IpClient */ + @NonNull + public IpClient makeIpClient(@NonNull Context context, @NonNull String ifName, + @NonNull IIpClientCallbacks cb, @NonNull NetworkObserverRegistry observerRegistry, + @NonNull NetworkStackServiceManager nsServiceManager) { + return new IpClient(context, ifName, cb, observerRegistry, nsServiceManager); + } + } + + /** * Connector implementing INetworkStackConnector for clients. */ @VisibleForTesting @@ -123,6 +175,7 @@ public class NetworkStackService extends Service { private static final int NUM_VALIDATION_LOG_LINES = 20; private final Context mContext; private final PermissionChecker mPermChecker; + private final Dependencies mDeps; private final INetd mNetd; private final NetworkObserverRegistry mObserverRegistry; @GuardedBy("mIpClients") @@ -142,19 +195,6 @@ public class NetworkStackService extends Service { private final ArraySet<Integer> mFrameworkAidlVersions = new ArraySet<>(1); private final int mNetdAidlVersion; - /** - * Permission checking dependency of the connector, useful for testing. - */ - @VisibleForTesting - public static class PermissionChecker { - /** - * @see PermissionUtil#enforceNetworkStackCallingPermission() - */ - public void enforceNetworkStackCallingPermission() { - PermissionUtil.enforceNetworkStackCallingPermission(); - } - } - private SharedLog addValidationLogs(Network network, String name) { final SharedLog log = new SharedLog(NUM_VALIDATION_LOG_LINES, network + " - " + name); synchronized (mValidationLogs) { @@ -166,21 +206,27 @@ public class NetworkStackService extends Service { return log; } + NetworkStackConnector(@NonNull Context context) { + this(context, new PermissionChecker(), new Dependencies()); + } + @VisibleForTesting public NetworkStackConnector( - @NonNull Context context, @NonNull PermissionChecker permChecker) { + @NonNull Context context, @NonNull PermissionChecker permChecker, + @NonNull Dependencies deps) { mContext = context; mPermChecker = permChecker; + mDeps = deps; mNetd = INetd.Stub.asInterface( (IBinder) context.getSystemService(Context.NETD_SERVICE)); mObserverRegistry = new NetworkObserverRegistry(); - mIpMemoryStoreService = new IpMemoryStoreService(context); + mIpMemoryStoreService = mDeps.makeIpMemoryStoreService(context); // NetworkStackNotifier only shows notifications relevant for API level > Q if (ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) { final HandlerThread notifierThread = new HandlerThread( NetworkStackNotifier.class.getSimpleName()); notifierThread.start(); - mNotifier = new NetworkStackNotifier(context, notifierThread.getLooper()); + mNotifier = mDeps.makeNotifier(context, notifierThread.getLooper()); } else { mNotifier = null; } @@ -217,7 +263,7 @@ public class NetworkStackService extends Service { updateSystemAidlVersion(cb.getInterfaceVersion()); final DhcpServer server; try { - server = new DhcpServer( + server = mDeps.makeDhcpServer( mContext, ifName, DhcpServingParams.fromParcelableObject(params), @@ -240,7 +286,7 @@ public class NetworkStackService extends Service { mPermChecker.enforceNetworkStackCallingPermission(); updateSystemAidlVersion(cb.getInterfaceVersion()); final SharedLog log = addValidationLogs(network, name); - final NetworkMonitor nm = new NetworkMonitor(mContext, cb, network, log, this); + final NetworkMonitor nm = mDeps.makeNetworkMonitor(mContext, cb, network, log, this); cb.onNetworkMonitorCreated(new NetworkMonitorConnector(nm, mPermChecker)); } @@ -248,7 +294,8 @@ public class NetworkStackService extends Service { public void makeIpClient(String ifName, IIpClientCallbacks cb) throws RemoteException { mPermChecker.enforceNetworkStackCallingPermission(); updateSystemAidlVersion(cb.getInterfaceVersion()); - final IpClient ipClient = new IpClient(mContext, ifName, cb, mObserverRegistry, this); + final IpClient ipClient = mDeps.makeIpClient( + mContext, ifName, cb, mObserverRegistry, this); synchronized (mIpClients) { final Iterator<WeakReference<IpClient>> it = mIpClients.iterator(); @@ -371,10 +418,10 @@ public class NetworkStackService extends Service { @NonNull private final NetworkMonitor mNm; @NonNull - private final NetworkStackConnector.PermissionChecker mPermChecker; + private final PermissionChecker mPermChecker; public NetworkMonitorConnector(@NonNull NetworkMonitor nm, - @NonNull NetworkStackConnector.PermissionChecker permChecker) { + @NonNull PermissionChecker permChecker) { mNm = nm; mPermChecker = permChecker; } |