Use public API of NetworkAgent in TestNetworkService
- Create NetworkAgent by using the public API of NetworkAgent. - Remove the usage of NetworkInfo. Bug: 172183305 Test: Build pass. Test: atest FrameworksNetTests Test: atest CtsNetTestCasesLatestSdk Test: atest NetworkStackIntegrationTests Test: atest TetheringIntegrationTests Change-Id: Icba59295861201c221587caa9df02275c4087fb8
This commit is contained in:
@@ -29,9 +29,9 @@ import android.net.IpPrefix;
|
|||||||
import android.net.LinkAddress;
|
import android.net.LinkAddress;
|
||||||
import android.net.LinkProperties;
|
import android.net.LinkProperties;
|
||||||
import android.net.NetworkAgent;
|
import android.net.NetworkAgent;
|
||||||
|
import android.net.NetworkAgentConfig;
|
||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkProvider;
|
||||||
import android.net.NetworkInfo.DetailedState;
|
|
||||||
import android.net.RouteInfo;
|
import android.net.RouteInfo;
|
||||||
import android.net.StringNetworkSpecifier;
|
import android.net.StringNetworkSpecifier;
|
||||||
import android.net.TestNetworkInterface;
|
import android.net.TestNetworkInterface;
|
||||||
@@ -62,7 +62,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
/** @hide */
|
/** @hide */
|
||||||
class TestNetworkService extends ITestNetworkManager.Stub {
|
class TestNetworkService extends ITestNetworkManager.Stub {
|
||||||
@NonNull private static final String TAG = TestNetworkService.class.getSimpleName();
|
@NonNull private static final String TAG = TestNetworkService.class.getSimpleName();
|
||||||
@NonNull private static final String TEST_NETWORK_TYPE = "TEST_NETWORK";
|
@NonNull private static final String TEST_NETWORK_LOGTAG = "TestNetworkAgent";
|
||||||
|
@NonNull private static final String TEST_NETWORK_PROVIDER_NAME = TAG;
|
||||||
@NonNull private static final AtomicInteger sTestTunIndex = new AtomicInteger();
|
@NonNull private static final AtomicInteger sTestTunIndex = new AtomicInteger();
|
||||||
|
|
||||||
@NonNull private final Context mContext;
|
@NonNull private final Context mContext;
|
||||||
@@ -72,6 +73,9 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
@NonNull private final HandlerThread mHandlerThread;
|
@NonNull private final HandlerThread mHandlerThread;
|
||||||
@NonNull private final Handler mHandler;
|
@NonNull private final Handler mHandler;
|
||||||
|
|
||||||
|
@NonNull private final ConnectivityManager mCm;
|
||||||
|
@NonNull private final NetworkProvider mNetworkProvider;
|
||||||
|
|
||||||
// Native method stubs
|
// Native method stubs
|
||||||
private static native int jniCreateTunTap(boolean isTun, @NonNull String iface);
|
private static native int jniCreateTunTap(boolean isTun, @NonNull String iface);
|
||||||
|
|
||||||
@@ -85,6 +89,10 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
mContext = Objects.requireNonNull(context, "missing Context");
|
mContext = Objects.requireNonNull(context, "missing Context");
|
||||||
mNMS = Objects.requireNonNull(netManager, "missing INetworkManagementService");
|
mNMS = Objects.requireNonNull(netManager, "missing INetworkManagementService");
|
||||||
mNetd = Objects.requireNonNull(NetdService.getInstance(), "could not get netd instance");
|
mNetd = Objects.requireNonNull(NetdService.getInstance(), "could not get netd instance");
|
||||||
|
mCm = mContext.getSystemService(ConnectivityManager.class);
|
||||||
|
mNetworkProvider = new NetworkProvider(mContext, mHandler.getLooper(),
|
||||||
|
TEST_NETWORK_PROVIDER_NAME);
|
||||||
|
mCm.registerNetworkProvider(mNetworkProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -150,9 +158,6 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
private static final int NETWORK_SCORE = 1; // Use a low, non-zero score.
|
private static final int NETWORK_SCORE = 1; // Use a low, non-zero score.
|
||||||
|
|
||||||
private final int mUid;
|
private final int mUid;
|
||||||
@NonNull private final NetworkInfo mNi;
|
|
||||||
@NonNull private final NetworkCapabilities mNc;
|
|
||||||
@NonNull private final LinkProperties mLp;
|
|
||||||
|
|
||||||
@GuardedBy("mBinderLock")
|
@GuardedBy("mBinderLock")
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -161,20 +166,18 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
@NonNull private final Object mBinderLock = new Object();
|
@NonNull private final Object mBinderLock = new Object();
|
||||||
|
|
||||||
private TestNetworkAgent(
|
private TestNetworkAgent(
|
||||||
@NonNull Looper looper,
|
|
||||||
@NonNull Context context,
|
@NonNull Context context,
|
||||||
@NonNull NetworkInfo ni,
|
@NonNull Looper looper,
|
||||||
|
@NonNull NetworkAgentConfig config,
|
||||||
@NonNull NetworkCapabilities nc,
|
@NonNull NetworkCapabilities nc,
|
||||||
@NonNull LinkProperties lp,
|
@NonNull LinkProperties lp,
|
||||||
int uid,
|
int uid,
|
||||||
@NonNull IBinder binder)
|
@NonNull IBinder binder,
|
||||||
|
@NonNull NetworkProvider np)
|
||||||
throws RemoteException {
|
throws RemoteException {
|
||||||
super(looper, context, TEST_NETWORK_TYPE, ni, nc, lp, NETWORK_SCORE);
|
super(context, looper, TEST_NETWORK_LOGTAG, nc, lp, NETWORK_SCORE, config, np);
|
||||||
|
|
||||||
mUid = uid;
|
mUid = uid;
|
||||||
mNi = ni;
|
|
||||||
mNc = nc;
|
|
||||||
mLp = lp;
|
|
||||||
|
|
||||||
synchronized (mBinderLock) {
|
synchronized (mBinderLock) {
|
||||||
mBinder = binder; // Binder null-checks in create()
|
mBinder = binder; // Binder null-checks in create()
|
||||||
@@ -203,9 +206,7 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void teardown() {
|
private void teardown() {
|
||||||
mNi.setDetailedState(DetailedState.DISCONNECTED, null, null);
|
unregister();
|
||||||
mNi.setIsAvailable(false);
|
|
||||||
sendNetworkInfo(mNi);
|
|
||||||
|
|
||||||
// Synchronize on mBinderLock to ensure that unlinkToDeath is never called more than
|
// Synchronize on mBinderLock to ensure that unlinkToDeath is never called more than
|
||||||
// once (otherwise it could throw an exception)
|
// once (otherwise it could throw an exception)
|
||||||
@@ -238,11 +239,6 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
Objects.requireNonNull(context, "missing Context");
|
Objects.requireNonNull(context, "missing Context");
|
||||||
// iface and binder validity checked by caller
|
// iface and binder validity checked by caller
|
||||||
|
|
||||||
// Build network info with special testing type
|
|
||||||
NetworkInfo ni = new NetworkInfo(ConnectivityManager.TYPE_TEST, 0, TEST_NETWORK_TYPE, "");
|
|
||||||
ni.setDetailedState(DetailedState.CONNECTED, null, null);
|
|
||||||
ni.setIsAvailable(true);
|
|
||||||
|
|
||||||
// Build narrow set of NetworkCapabilities, useful only for testing
|
// Build narrow set of NetworkCapabilities, useful only for testing
|
||||||
NetworkCapabilities nc = new NetworkCapabilities();
|
NetworkCapabilities nc = new NetworkCapabilities();
|
||||||
nc.clearAll(); // Remove default capabilities.
|
nc.clearAll(); // Remove default capabilities.
|
||||||
@@ -290,7 +286,12 @@ class TestNetworkService extends ITestNetworkManager.Stub {
|
|||||||
lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), null, iface));
|
lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), null, iface));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TestNetworkAgent(looper, context, ni, nc, lp, callingUid, binder);
|
final TestNetworkAgent agent = new TestNetworkAgent(context, looper,
|
||||||
|
new NetworkAgentConfig.Builder().build(), nc, lp, callingUid, binder,
|
||||||
|
mNetworkProvider);
|
||||||
|
agent.register();
|
||||||
|
agent.markConnected();
|
||||||
|
return agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user