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:
lucaslin
2020-12-01 23:03:02 +08:00
parent f2d92dc1ee
commit 4ead42c078

View File

@@ -29,9 +29,9 @@ import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkAgent;
import android.net.NetworkAgentConfig;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkProvider;
import android.net.RouteInfo;
import android.net.StringNetworkSpecifier;
import android.net.TestNetworkInterface;
@@ -62,7 +62,8 @@ import java.util.concurrent.atomic.AtomicInteger;
/** @hide */
class TestNetworkService extends ITestNetworkManager.Stub {
@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 final Context mContext;
@@ -72,6 +73,9 @@ class TestNetworkService extends ITestNetworkManager.Stub {
@NonNull private final HandlerThread mHandlerThread;
@NonNull private final Handler mHandler;
@NonNull private final ConnectivityManager mCm;
@NonNull private final NetworkProvider mNetworkProvider;
// Native method stubs
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");
mNMS = Objects.requireNonNull(netManager, "missing INetworkManagementService");
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 final int mUid;
@NonNull private final NetworkInfo mNi;
@NonNull private final NetworkCapabilities mNc;
@NonNull private final LinkProperties mLp;
@GuardedBy("mBinderLock")
@NonNull
@@ -161,20 +166,18 @@ class TestNetworkService extends ITestNetworkManager.Stub {
@NonNull private final Object mBinderLock = new Object();
private TestNetworkAgent(
@NonNull Looper looper,
@NonNull Context context,
@NonNull NetworkInfo ni,
@NonNull Looper looper,
@NonNull NetworkAgentConfig config,
@NonNull NetworkCapabilities nc,
@NonNull LinkProperties lp,
int uid,
@NonNull IBinder binder)
@NonNull IBinder binder,
@NonNull NetworkProvider np)
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;
mNi = ni;
mNc = nc;
mLp = lp;
synchronized (mBinderLock) {
mBinder = binder; // Binder null-checks in create()
@@ -203,9 +206,7 @@ class TestNetworkService extends ITestNetworkManager.Stub {
}
private void teardown() {
mNi.setDetailedState(DetailedState.DISCONNECTED, null, null);
mNi.setIsAvailable(false);
sendNetworkInfo(mNi);
unregister();
// Synchronize on mBinderLock to ensure that unlinkToDeath is never called more than
// once (otherwise it could throw an exception)
@@ -238,11 +239,6 @@ class TestNetworkService extends ITestNetworkManager.Stub {
Objects.requireNonNull(context, "missing Context");
// 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
NetworkCapabilities nc = new NetworkCapabilities();
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));
}
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;
}
/**