Changes to make eth service methods more testable

Updates to make methods in EthernetServiceImpl that rely on
EthernetTracker unit testable. This CL also includes added tests for
such methods in EthernetServiceImplTest.

Bug: 210485380
Test: atest EthernetServiceTests
Change-Id: I63969b60cc4cf9d391e2cd21d02e1bdc8988aba8
This commit is contained in:
James Mattis
2021-12-28 15:04:44 -08:00
parent cc1dac61b3
commit a5f724dfdd
4 changed files with 68 additions and 24 deletions

View File

@@ -17,17 +17,24 @@
package com.android.server.ethernet;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import com.android.server.SystemService;
public final class EthernetService extends SystemService {
private static final String TAG = "EthernetService";
final EthernetServiceImpl mImpl;
private static final String THREAD_NAME = "EthernetServiceThread";
private final EthernetServiceImpl mImpl;
public EthernetService(Context context) {
super(context);
mImpl = new EthernetServiceImpl(context);
final HandlerThread handlerThread = new HandlerThread(THREAD_NAME);
handlerThread.start();
mImpl = new EthernetServiceImpl(
context, handlerThread.getThreadHandler(),
new EthernetTracker(context, handlerThread.getThreadHandler()));
}
@Override

View File

@@ -28,7 +28,6 @@ import android.net.InternalNetworkUpdateRequest;
import android.net.IpConfiguration;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.RemoteException;
import android.util.Log;
import android.util.PrintWriterPrinter;
@@ -49,15 +48,17 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class EthernetServiceImpl extends IEthernetManager.Stub {
private static final String TAG = "EthernetServiceImpl";
private final Context mContext;
@VisibleForTesting
final AtomicBoolean mStarted = new AtomicBoolean(false);
private final Context mContext;
private final Handler mHandler;
private final EthernetTracker mTracker;
private Handler mHandler;
private EthernetTracker mTracker;
public EthernetServiceImpl(Context context) {
EthernetServiceImpl(@NonNull final Context context, @NonNull final Handler handler,
@NonNull final EthernetTracker tracker) {
mContext = context;
mHandler = handler;
mTracker = tracker;
}
private void enforceAccessPermission() {
@@ -91,14 +92,7 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
public void start() {
Log.i(TAG, "Starting Ethernet service");
HandlerThread handlerThread = new HandlerThread("EthernetServiceThread");
handlerThread.start();
mHandler = new Handler(handlerThread.getLooper());
mTracker = new EthernetTracker(mContext, mHandler);
mTracker.start();
mStarted.set(true);
}

View File

@@ -18,6 +18,8 @@ package com.android.server.ethernet;
import static android.net.TestNetworkManager.TEST_TAP_PREFIX;
import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
@@ -67,7 +69,8 @@ import java.util.concurrent.ConcurrentHashMap;
*
* <p>All public or package private methods must be thread-safe unless stated otherwise.
*/
final class EthernetTracker {
@VisibleForTesting(visibility = PACKAGE)
public class EthernetTracker {
private static final int INTERFACE_MODE_CLIENT = 1;
private static final int INTERFACE_MODE_SERVER = 2;
@@ -138,10 +141,10 @@ final class EthernetTracker {
NetworkCapabilities nc = createNetworkCapabilities(true /* clear default capabilities */);
mFactory = new EthernetNetworkFactory(handler, context, nc);
mFactory.register();
}
void start() {
mFactory.register();
mConfigStore.read();
// Default interface is just the first one we want to track.
@@ -176,7 +179,8 @@ final class EthernetTracker {
return mIpConfigurations.get(iface);
}
boolean isTrackingInterface(String iface) {
@VisibleForTesting(visibility = PACKAGE)
protected boolean isTrackingInterface(String iface) {
return mFactory.hasInterface(iface);
}